5.4 luaL_checkversion vs. loadlib()

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

5.4 luaL_checkversion vs. loadlib()

Andrew Gierth
Prior to 5.4, luaL_checkversion checks that there isn't more than one
copy of the interpreter code involved, that is to say that a static
variable will have the same address regardless of what module it got
called from.

5.4 abandons this check; apparently it was believed to be unnecessary?

But I think it _is_ necessary and that you can probably (I haven't
actually done the test case yet) get unexpected errors from having
multiple copies of the interpreter. Specifically, ll_loadlib relies on
registry[&CLIBS], where &CLIBS is a lightuserdata with the address of a
static var. If there are multiple copies of the interpreter and you call
require or loadlib from one other than the one that created the state,
it'll end up calling lua_getfield on a nil value, which will throw an
error.

Am I wrong?

--
Andrew.

Reply | Threaded
Open this post in threaded view
|

Re: 5.4 luaL_checkversion vs. loadlib()

Luiz Henrique de Figueiredo
> Specifically, ll_loadlib relies on
> registry[&CLIBS], where &CLIBS is a lightuserdata with the address of a
> static var. If there are multiple copies of the interpreter and you call
> require or loadlib from one other than the one that created the state,
> it'll end up calling lua_getfield on a nil value, which will throw an
> error.

The first thing that luaopen_package does is to create an entry
registry[&CLIBS].

Reply | Threaded
Open this post in threaded view
|

Re: 5.4 luaL_checkversion vs. loadlib()

Andrew Gierth
>>>>> "Luiz" == Luiz Henrique de Figueiredo <[hidden email]> writes:

 >> Specifically, ll_loadlib relies on
 >> registry[&CLIBS], where &CLIBS is a lightuserdata with the address of a
 >> static var. If there are multiple copies of the interpreter and you call
 >> require or loadlib from one other than the one that created the state,
 >> it'll end up calling lua_getfield on a nil value, which will throw an
 >> error.

 Luiz> The first thing that luaopen_package does is to create an entry
 Luiz> registry[&CLIBS].

That's not the issue.

Suppose module A contains a copy of the lua interpreter, creates a
lua_State and runs luaL_openlibs etc.

Then it calls module B, which _also_ contains a copy of the (same
version) of the interpreter. B calls luaL_checkversion, which (in 5.4)
passes because it's the same version, even though it's a different copy
of the code with different addresses.

B calls require, and it errors out because B's &CLIBS is not the same
value as A's &CLIBS.

In 5.3, the luaL_checkversion would have failed, but in 5.4 it does not.

--
Andrew.