[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Why does luaL_openlibs invoke the interpreter?
- From: Dustin Juliano <velusdarkbind@...>
- Date: Sat, 7 Feb 2009 22:02:36 -0500
Hello All,
Foreground:
When I mention "interpreter" I am meaning the Lua engine itself, not  
the supplied interpreter example or some external binary that has  
already been compiled. This is in reference to the engine that has  
been embedded into the host application. An invocation of the  
interpreter is a call to lua_call or lua_pcall, from the context I am  
meaning.
Problem:
I have been embedding Lua 5.1 and have been stepping through the  
internals to understand it better. I noticed the following.
Inside [linit.c] on (line 35)
lua_call(L, 1, 0);
Here is the entire function:
------------------------------------------------------------------------------------
LUALIB_API void luaL_openlibs (lua_State *L) {
  const luaL_Reg *lib = lualibs;
  for (; lib->func; lib++) {
    lua_pushcfunction(L, lib->func);
    lua_pushstring(L, lib->name);
    lua_call(L, 1, 0);
  }
}
------------------------------------------------------------------------------------
Setting a breakpoint, one can see that it just ends up at:
luaL_register
which, finally, brings one to:
luaI_openlib
Here is the function I modified:
------------------------------------------------------------------------------------
LUALIB_API int luaL_openlibs (lua_State *L) {
  const luaL_Reg *lib = lualibs;
  for (; lib->func; lib++) {
	lib->func(L);
  }
	return 0;
}
------------------------------------------------------------------------------------
This compiles and executes without any apparent issues; however, it is  
clear to see that luaopen_* functions were meant to be called from Lua  
and not from C, due to their conformance to the Lua C Function  
protocol--it is clear they are Lua Library Functions.
These observations have raised several questions:
1) Why invoke the interpreter when you can directly call these  
function pointers to initialize the library, at will?
2) Why are the luaopen_* (luaopen_base, luaopen_package, ...,  
luaopen_debug) all Lua C Library functions? That is, why do they not  
just perform their intended purpose without having to expose  
themselves to Lua at all?
Possible answer to #2 is that this was meant to serve either as an  
example or to conform to a precept of dynamic loading. Perhaps in the  
case that the library is compiled as a DLL. The INSTALL text file  
indicates that the "library" is everything except the compiler and  
interpreter. Thus, making it a DLL would not be the best choice.  
Rather, it would require that the Lua Library (as opposed just the  
engine itself) would be the dynamic library, instead. Then all of this  
would make more sense as to why luaL_openlibs invokes the interpreter/ 
engine.
I would appreciate any feedback. I am in the process of writing a C++  
binding for Lua. I was hoping to truly understand the deepest levels  
of the registration process for the Lua - C boundary.
Thanks in advance!