Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

luaopen functions

Tags:

scripting

lua

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!

like image 613
Mircea Ispas Avatar asked Feb 09 '26 15:02

Mircea Ispas


1 Answers

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.

like image 173
fouronnes Avatar answered Feb 13 '26 06:02

fouronnes



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!