lua State加载部分库
2015-10-28 18:03
423 查看
在lua中,通常我们用luaL_openlibs(L)加载所有的lub标准库,但是有时候我们想只加载部分,有没有什么好的办法呢?在luaproc看到如下办法:
Calls the C function
上面是5.1的做法。luaproc新做法如下:
static void registerlib( lua_State *L, const char *name, lua_CFunction f ) { lua_getglobal( L, "package" ); lua_getfield( L, -1, "preload" ); lua_pushcfunction( L, f ); lua_setfield( L, -2, name ); lua_pop( L, 2 ); } static void openlibs( lua_State *L ) { lua_cpcall( L, luaopen_base, NULL ); lua_cpcall( L, luaopen_package, NULL ); registerlib( L, "io", luaopen_io ); registerlib( L, "os", luaopen_os ); registerlib( L, "table", luaopen_table ); registerlib( L, "string", luaopen_string ); registerlib( L, "math", luaopen_math ); registerlib( L, "debug", luaopen_debug ); }
int lua_cpcall (lua_State *L, lua_CFunction func, void *ud);
Calls the C function
funcin protected mode.
funcstarts with only one element in its stack, a light userdata containing
ud. In case of errors,
lua_cpcallreturns the same error codes as
lua_pcall, plus the error object on the top of the stack; otherwise, it returns zero, and does not change the stack. All values returned by
funcare discarded.
上面是5.1的做法。luaproc新做法如下:
static void luaproc_reglualib( lua_State *L, const char *name, lua_CFunction f ) { lua_getglobal( L, "package" ); lua_getfield( L, -1, "preload" ); lua_pushcfunction( L, f ); lua_setfield( L, -2, name ); lua_pop( L, 2 ); } static void luaproc_openlualibs( lua_State *L ) { requiref( L, "_G", luaopen_base, FALSE ); requiref( L, "package", luaopen_package, TRUE ); luaproc_reglualib( L, "io", luaopen_io ); luaproc_reglualib( L, "os", luaopen_os ); luaproc_reglualib( L, "table", luaopen_table ); luaproc_reglualib( L, "string", luaopen_string ); luaproc_reglualib( L, "math", luaopen_math ); luaproc_reglualib( L, "debug", luaopen_debug ); #if (LUA_VERSION_NUM == 502) luaproc_reglualib( L, "bit32", luaopen_bit32 ); #endif #if (LUA_VERSION_NUM >= 502) luaproc_reglualib( L, "coroutine", luaopen_coroutine ); #endif #if (LUA_VERSION_NUM >= 503) luaproc_reglualib( L, "utf8", luaopen_utf8 ); #endif }
相关文章推荐
- Lua 迭代器
- Lua 变长参数(variable number of arguments)
- Deploy openresty + lua + zabbix
- 使用ngx_lua发送文件给服务器
- robot framework万能的evaluate
- Lua
- lua文件读写
- Lua中的操作系统库
- Lua table 引用
- Lua 语言 15 分钟快速入门
- Lua中的基本函数库
- Lua资料总结
- 用Java内置的JS引擎实现Infix Expression Evaluation
- Lua学习简单正则匹配笔记
- Lua的function、closure和upvalue
- MAC上Lua开发环境搭建
- LuaJava:跟着LuaJava一步一步学习JNI -1
- 转让lua性能executeGlobalFunction
- Lua开发环境搭建(二)--使用Editplus编写Lua脚本
- Evaluate Reverse Polish Notation