lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]



On 2-Nov-06, at 8:49 PM, Glenn Maynard wrote:

(What is it that requires these functions to be called this way?)

Some libraries require the existence of a current function environment, and may change it to some other table in order to facilitate setting the environment of the functions (and possibly userdata) created by the library.

If the luaopen_* function is not lua_call'ed, and is not in the context of a lua_call, there will be no call frame to get the function environment from.

Additionally, in accordance with the new module system, the luaopen_* function may wish to know the name it was loaded as, which should be provided as the first argument to the lua_call.

Since the luaopen_* function may change the function environment, it is a good idea to give it a fresh one; linit.c uses the following idiom, which I've been following:

  lua_pushcfunction(L, luaopen_foo);
  lua_pushstring(L, "foo");
  lua_call(L, 1, 0);

I believe that's the same initialization which will be done by require(), so the luaopen_* function doesn't need to know whether it's been dynamically loaded or statically compiled.