Forwarding function calls from Lua->C->...

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Forwarding function calls from Lua->C->...

Reuben Thomas-3
For my EPOC port of Lua I have written an interface to OPL, EPOC's
BASIC-like language. The interface is written in C++. I want to be able to
register OPL routines in Lua. The way I imagine doing this is having an OPL
interface routine LuaRegister:(state&,f$) that takes the name of an OPL
function, f$.

So far, so good. But what do I actually register when I call
lua_register(state, ...)? I can't register an OPL function directly, so I
have to give the address of some C(++) function in my interface DLL. But if
I always give the same function, then how do I know which registered OPL
function was supposed to be called when Lua makes the call to C?

I suppose I want a way of fixing a parameter of the call that Lua makes to
be the name of the OPL function, or the equivalent.

Note that I can't just have one C++ function per OPL function I want to
register, as I don't want to have to recompile the C++ component each time I
want to register a new function in my OPL program; from the C++ interface's
point of view, I want to register new OPL functions dynamically.

I suppose I can always execute:

lua_dostring(L, "luaname = function(a1,a2,a3) _opl(\"oplname\",a1,a2,a3) end");

where _opl is the C++ dispatcher, which I register as normal with
lua_register and a1,a2,a3 are the arguments to the OPL procedure (this bit
of the string is constructed at registration-time). However, it all looks a
little fragile (plus of course, there's the run-time parsing overhead,
though I suppose I shouldn't be registering too many functions!).

Any suggestions for better ways of doing this? (Sorry if I've missed
something on the mailing list already.)

A hint: OPL is traditionally BASIC-like in its type system: strings, ints
(big and small) and floats, plus one-dimensional arrays. I would envisage
only supporting ints, floats and strings in the calling interface, and
recording type information when each OPL function is registered so that the
Lua quantities can be converted appropriately for argument passing.

-- 
http://sc3d.org/rrt/ | free, a.  already paid for (Peyton Jones)


Reply | Threaded
Open this post in threaded view
|

Re: Forwarding function calls from Lua->C->...

Luiz Henrique de Figueiredo
>So far, so good. But what do I actually register when I call
>lua_register(state, ...)? I can't register an OPL function directly, so I
>have to give the address of some C(++) function in my interface DLL. But if
>I always give the same function, then how do I know which registered OPL
>function was supposed to be called when Lua makes the call to C?
>
>I suppose I want a way of fixing a parameter of the call that Lua makes to
>be the name of the OPL function, or the equivalent.

Push the name or a pointer to the actual OPL function as an upvalue to the
fixed C function and register this function in Lua as a C closure.

The emulation module lua3 does something similar because C functions in 3.x
have different signatures than in 4.x.
--lhf

Reply | Threaded
Open this post in threaded view
|

Re: Forwarding function calls from Lua->C->...

Reuben Thomas-3
> Push the name or a pointer to the actual OPL function as an upvalue to the
> fixed C function and register this function in Lua as a C closure.

Yes, I'd forgotten that C functions can have upvalues. Thanks!

-- 
http://sc3d.org/rrt/ | RSA, n.  safety in numbers