lua globals and C code

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

lua globals and C code

Gordon A. Acocella
With Lua 5.0 alpha I am getting very close to being able to replace perl
with Lua for general-purpose scripting (all I need now are dynamically
loadable packages like poslib and luasocket).  Anyway, it still isn't
working quite the way I had hoped.  I wanted to do something like this:

    poslib = {}
    local f, e = loadlib("lposlib.dll", "lua_poslibopen")
    if not f then
        error(e)
    end
    setglobals(f, poslib)  -- (this does not work)
    f()

But, I cannot set the globals table for a C function, and if I wrap it
inside a Lua function it has no effect (all poslib functions are still
entered in the original globals table).  Ideally, I would expect C
functions to have function-specific globals tables just like Lua
functions.

Are there any plans to change this?  Is there a simple work around I am
missing?

Gordon

Reply | Threaded
Open this post in threaded view
|

Re: lua globals and C code

Denis Andreev
Hi!

We use our own dynamic package system (luax).

GAA>     poslib = {}
GAA>     local f, e = loadlib("lposlib.dll", "lua_poslibopen")
GAA>     if not f then
GAA>         error(e)
GAA>     end
GAA>     setglobals(f, poslib)  -- (this does not work)
GAA>     f()
GAA> But, I cannot set the globals table for a C function

May by you try this may:

poslib = {}
local f, e = loadlib("lposlib.dll", "lua_poslibopen")
assert(f, e)
f(poslib)

and in your C code:

static int poslib_globals_ref = 0;

static int lua_poslibopen(lua_Stack *L) {
       poslib_globals_ref = lua_ref(L, 1);
       ...
       return 0
}

And in other functions you may use this reference.

--Denq


Reply | Threaded
Open this post in threaded view
|

RE: lua globals and C code

Asko Kauppi-2
In reply to this post by Gordon A. Acocella
Does that mean that you must compile the Lua engine into each and every
dynamic module you do? After all, you need a way to do something with the
'lua_State' pointer...

- Asko Kauppi
  Flextronics Finland

--
Flextronics Design Finland
Box 23, 39201 Kyröskoski, Finland
+358 205 345 251 phone
+358 205 345 332 fax
www.flextronics.com


> -----Original Message-----
> From:	Denis Andreev [SMTP:[hidden email]]
> Sent:	Wednesday, August 28, 2002 11:07 AM
> To:	Multiple recipients of list
> Subject:	Re: lua globals and C code
> 
> 
> Hi!
> 
> We use our own dynamic package system (luax).
> 
> GAA>     poslib = {}
> GAA>     local f, e = loadlib("lposlib.dll", "lua_poslibopen")
> GAA>     if not f then
> GAA>         error(e)
> GAA>     end
> GAA>     setglobals(f, poslib)  -- (this does not work)
> GAA>     f()
> GAA> But, I cannot set the globals table for a C function
> 
> May by you try this may:
> 
> poslib = {}
> local f, e = loadlib("lposlib.dll", "lua_poslibopen")
> assert(f, e)
> f(poslib)
> 
> and in your C code:
> 
> static int poslib_globals_ref = 0;
> 
> static int lua_poslibopen(lua_Stack *L) {
>        poslib_globals_ref = lua_ref(L, 1);
>        ...
>        return 0
> }
> 
> And in other functions you may use this reference.
> 
> --Denq
###########################################
This message has been scanned by F-Secure Anti-Virus for Microsoft Exchange.


Reply | Threaded
Open this post in threaded view
|

Re: lua globals and C code

Luiz Henrique de Figueiredo
>all poslib functions are still entered in the original globals table

The posix library will be updated to use the new scheme of putting things
inside tables as soon as Lua 5.0 final is released.

For the time being, you'll have to change lua_poslibopen to use the new scheme
yourself. See lmathlib.c for an example. Sorry about that.
--lhf

Reply | Threaded
Open this post in threaded view
|

Re: lua globals and C code

Gordon A. Acocella
Luiz Henrique de Figueiredo wrote:
> 
> ...
> For the time being, you'll have to change lua_poslibopen to use the new scheme
> yourself. See lmathlib.c for an example. Sorry about that.

OK.  This doesn't look too hard.  I will try it.

I would like to say, though, that something still bothers me about
accessing globals from C and Lua being different.  I can't think of a
concrete reason why this might be a problem, but it would be more
consistent if *all* functions could have their own globals table.

Anwyay, thanks for all the hard work on 5.0 alpha.  I'm sure Lua 5.0 is
going to be great!

Gordon

Reply | Threaded
Open this post in threaded view
|

Re: lua globals and C code

mnicolet
In reply to this post by Asko Kauppi-2
Dear Asko
----- Original Message -----
From: "Asko Kauppi" <[hidden email]>
To: "Multiple recipients of list" <[hidden email]>
Cc: <[hidden email]>
Sent: Wednesday, August 28, 2002 5:24 AM
Subject: RE: lua globals and C code



Does that mean that you must compile the Lua engine into each and every
dynamic module you do? After all, you need a way to do something with the
'lua_State' pointer...

- Asko Kauppi
  Flextronics Finland

--
Flextronics Design Finland
Box 23, 39201 Kyröskoski, Finland
+358 205 345 251 phone
+358 205 345 332 fax
www.flextronics.com


> -----Original Message-----
> From: Denis Andreev [SMTP:[hidden email]]
> Sent: Wednesday, August 28, 2002 11:07 AM
> To: Multiple recipients of list
> Subject: Re: lua globals and C code
>
>
> Hi!
>
> We use our own dynamic package system (luax).
>
> GAA>     poslib = {}
> GAA>     local f, e = loadlib("lposlib.dll", "lua_poslibopen")
> GAA>     if not f then
> GAA>         error(e)
> GAA>     end
> GAA>     setglobals(f, poslib)  -- (this does not work)
> GAA>     f()
> GAA> But, I cannot set the globals table for a C function
>
> May by you try this may:
>
> poslib = {}
> local f, e = loadlib("lposlib.dll", "lua_poslibopen")
> assert(f, e)
> f(poslib)
>
> and in your C code:
>
> static int poslib_globals_ref = 0;
>
> static int lua_poslibopen(lua_Stack *L) {
>        poslib_globals_ref = lua_ref(L, 1);
>        ...
>        return 0
> }
>
> And in other functions you may use this reference.
>
> --Denq
###########################################
This message has been scanned by F-Secure Anti-Virus for Microsoft Exchange.