[gambit-list] Converting uint32 to scheme object

Hendrik Boom hendrik at topoi.pooq.com
Wed Jan 6 23:25:45 EST 2016


On Wed, Jan 06, 2016 at 11:00:26PM -0500, Marc Feeley wrote:
> I can see a few problems with the glue code you wrote to interface your C function to Lua:
> 
> - it isn’t checking that the number of parameters received by the function is correct
> - it isn’t checking that the first parameter is a pointer of type TIFF*
> - it isn’t checking that the first parameter is not NULL
> - it doesn’t appear to do the right thing when val is a uint32 with the upper bit set (I suspect it will return a negative integer)
> - if there is a type error (say the second parameter is not an integer), will there be a precise error message or exception indicating that the second parameter was not of the right type? can the exception be caught in Lua?
> - I suspect that lua_pushinteger may allocate memory (for a bignum or flonum?), so what happens if this allocation causes the heap to overflow? can this be caught in Lua?
> 
> In this particular example, Gambit’s glue code is not much longer than the one for Lua, but it doesn’t suffer from these issues.  For a typical function with simple types for parameters and result, Gambit’s FFI does not require any glue code.

Let me guess that your FFI accomplishes its goals by generating and 
compiling C code.  You can do a lot that way.

I believe that Lua was originally an interpreter written in portable C, 
and there was no mechanism in C to call a statically unknown function 
with a statically unknown number of parameters of statically unknown type. 
C is not the only language with this problem, and the Lua C interface is 
about as good as you can get under this constraint.

I don't know if there is such a mechanism now; I've heard that there are 
such gcc extensions, but that's not as portable as one might like.

> 
> So as you can probably tell, I’m more enclined to qualify Lua’s FFI as “simple” and not “brilliant”… :-)

A simplicity born of necessity.

Gambit had entirely different constraints.  Didn't it originate as a 
compiler from Scheme to C?

-- hendrik



More information about the Gambit-list mailing list