gm2
[Top][All Lists]
Advanced

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

Re: Type safety in GM2?


From: Gaius Mulley
Subject: Re: Type safety in GM2?
Date: Tue, 24 Mar 2020 18:47:36 +0000
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.5 (gnu/linux)

Benjamin Kowarsch <address@hidden> writes:

> 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.


Hi,

I'd like to echo Benjamin's advice that a layered approach is definitely
the way to go - I did this with PGE:

https://github.com/gaiusm/pge

which provides a Python Pygame with a 2D game engine using
swig/Python3/C++ and Modula-2 (the majority of the code is Modula-2).
Here is the layered approach:

http://floppsie.comp.glam.ac.uk/Southwales/gaius/gameengine/29.html

slide 3 contains the layering info.  Some of the layers do very little,
but they do provide sanity :-)

regards,
Gaius



reply via email to

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