问题
I found in the lua sample code these calls:
luaopen_table(L);
luaopen_io(L);
luaopen_string(L);
luaopen_math(L);
I searched in lua header files and I found other functions with luaopen:
LUALIB_API int (luaopen_base) (lua_State *L);
LUALIB_API int (luaopen_table) (lua_State *L);
LUALIB_API int (luaopen_io) (lua_State *L);
LUALIB_API int (luaopen_os) (lua_State *L);
LUALIB_API int (luaopen_string) (lua_State *L);
LUALIB_API int (luaopen_math) (lua_State *L);
LUALIB_API int (luaopen_debug) (lua_State *L);
LUALIB_API int (luaopen_package) (lua_State *L);
Can you please explain what these functions mean? For example, may I use tables if I don't call luaopen_table? I didn't find any documentation about this!
回答1:
If you're using Lua 5.1, which is the latest version, the Reference Manual has an answer :
To have access to these libraries, the C host program should call the luaL_openlibs function, which opens all standard libraries. Alternatively, it can open them individually by calling luaopen_base (for the basic library), luaopen_package (for the package library), luaopen_string (for the string library), luaopen_table (for the table library), luaopen_math (for the mathematical library), luaopen_io (for the I/O library), luaopen_os (for the Operating System library), and luaopen_debug (for the debug library). These functions are declared in lualib.h and should not be called directly: you must call them like any other Lua C function, e.g., by using lua_call.
[...]
The luaopen_* functions (to open libraries) cannot be called directly, like a regular C function. They must be called through Lua, like a Lua function.
And yes, you can still use tables if you don't import the table library, they are built-in. You just don't have access to the table manipulation functions.
回答2:
You have to push them onto the stack to call them like you would a normal lua c function.
lua_State *l = lua_open();
lua_pushcfunction(l,luaopen_base);
lua_call(l,0,0);
lua_pushcfunction(l,luaopen_math);
lua_call(l,0,0);
lua_pushcfunction(l,luaopen_string);
lua_call(l,0,0);
lua_pushcfunction(l,luaopen_table);
lua_call(l,0,0);
回答3:
For those who browse this question looking for how to use these functions in modern Lua:
As of Lua 5.3 you need to luaL_requiref these, based on the source code in luaL_openlibs. I found no reference to that in any manual. So here is an example that opens up only the base library which allows lua to print to standard output.
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
int main( int argc, char *argv[] ) {
lua_State *lua = luaL_newstate();
luaL_requiref( lua, "_G", luaopen_base, 1 );
lua_pop( lua, 1 );
luaL_dostring( lua, "print \"Hello, lua\"" );
lua_close( lua );
return 0;
}
I cannot say what exactly all these libraries mean, nor how not loading them will affect you, but you can load them individually with the example given above. For example,
luaL_requiref( lua, LUA_IOLIBNAME, luaopen_io, 1 );
lua_pop( lua, 1 );
would load only the I/O library. See also the manual.
来源:https://stackoverflow.com/questions/5242958/luaopen-functions