gm2
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Type safety in GM2?


From: Benjamin Kowarsch
Subject: Re: Type safety in GM2?
Date: Wed, 25 Mar 2020 00:38:08 +0900

I am assuming that this is for interfacing to a C library where the C defined type is a pointer type that you want to use within Modula-2.

In general terms, you only get the type safety if the compiler has a definition for the type. If you cast a C pointer to ADDRESS, then as far as the Modula-2 compiler is concerned this is of type ADDRESS and per language definition any pointer is compatible with ADDRESS.

However, when interfacing to C, it is advisable to use a layered approach

* a low level library that directly interfaces to the C API
* a user level library that imports the low level library and wraps the types and functions, adding type safety

I have done this in one of my projects and the code is on github.

https://github.com/m2sf/m2pp

The code base is cross-platform (AmigaOS, MacOS X, MS-DOS, OS/2, OpenVMS, Posix, Windows), multi-dialect (PIM3/4 and ISO) and multi-compiler (ca.14 or 15 different M2 compilers) and thus there is a lowest level interface to the various OS' C APIs, often with separate implementations for different platforms. At this level there is no type safety, but on top is a Modula-2 library that imports the low-level interface, wraps the types and functions and provides type safety for the user level. User level code should never directly use the low level libraries.

For example, there are several low level libraries to interface to the C stdio library for POSIX platforms:

https://github.com/m2sf/m2pp/tree/master/src/posix
https://github.com/m2sf/m2pp/tree/master/src/imp/posix

At this level there is an unsafe file type ...

TYPE FILE = ADDRESS;

On top of the low-level library, I implemented a file IO library in Modula-2

https://github.com/m2sf/m2pp/blob/master/src/BasicFileIO.def
https://github.com/m2sf/m2pp/tree/master/src/imp/BasicFileIO

At this level, there is a safe file type ...

TYPE File; (* OPAQUE *)

This is the user-level library that the project code uses. The low-level libraries are only called by this library.

hope this helps.







On Tue, 24 Mar 2020 at 22:36, Hưng Hưng <address@hidden> wrote:
I found I can't substitute C typed pointer which I don't know how to translate to GM2 with ADDRESS. But the problem arise that when I declare them as ADDRESS I could pass anything to them and the compiler doesn't complain but the resulted program will crash.

e.g: the library assumes the passed type to be a structure typedef-ed with void pointer that I don't know how to translate to GM2 so I substitute it with ADDRESS, but I found I could pass an INTEGER pointer to it without any complains from the compiler but my program would crash.

How to at least tell the compiler to check if the correct type was passed or is it impossible without a properly translated type to GM2?

I found the developers also used this trick heavily. Check ncurses.def and you will see they also substitute pointer to not translated structure with ADDRESS.

reply via email to

[Prev in Thread] Current Thread [Next in Thread]