Search code examples
rebolrebol3

Wrapping shared variables using Rebol 3 FFI


Atronix Rebol 3 FFI looks pretty good in wrapping external functions, but I cannot find any references about wrapping external variables using it.

For example, Curses/NCurses library have the external variable stdscr defined in C as

extern WINDOW *stdscr;

I want to use it in my Rebol code. Ideally I want to use it as a common Rebol variable, but a read-only access (as a result of a function call, for example) would be great too.

Is it possible with Rebol 3 FFI?

I know that this practice might be considered harmful, but sometimes external libraries are written this way.


Solution

  • You can do this with the commit. Prebuild binaries can be downloaded from here (only in development releases)

    Here is the example code:

    rebol []
    
    ncurses: make library! %libncursesw.so
    
    stdscr: make struct! compose/deep [
        [
            extern: [(ncurses) "stdscr"]
        ]
        ptr [pointer]
    ]
    
    print ["stdscr:" stdscr/ptr]
    close ncurses