用preload加载Lua导出模块
来源:互联网 发布:拍摄淘宝图片技巧 编辑:程序博客网 时间:2024/05/31 19:22
用preload加载Lua导出模块
(金庆的专栏 2017.5)
参考:
How to make exported module non-global?
https://github.com/SteveKChiu/lua-intf/issues/135
动态库可以这样导出模块:
extern "C"
#if defined(_MSC_VER) || defined(__BORLANDC__) || defined(__CODEGEARC__)
__declspec(dllexport)
#endif
int luaopen_modname(lua_State* L)
{
LuaRef mod = LuaRef::createTable(L);
LuaBinding(mod)
...;
mod.pushToStack();
return 1;
}
如果不是动态库,可以这样导出全局模块 c_util:
int main()
{
...
LuaBinding(L).beginModule("c_util")
.addFunction("foo", []() { return 123; })
.endModule();
...
}
如果不想让它成为全局模块,则需要在 package.preload 表中注册一个加载函数.
Lua程序设计 第3版 英文版 programming in lua 3ed
The preload searcher allows the definition of an arbitrary function to load a module.
It uses a table, called package.preload, to map module names to loader functions.
When searching for a module name, this searcher simply looks for the given name in the table.
If it finds a function there, it returns this function as the module loader.
Otherwise, it returns nil.
This searcher provides a generic method to handle some non-conventional situations.
For instance, a C library statically linked to Lua can register its luaopen_ function into the preload table,
so that it will be called only when (and if) the user requires that module.
In this way, the program does not waste time opening the module if it is not used.
代码示例:
extern "C" int open_my_module(lua_State* L)
{
LuaRef mod = LuaRef::createTable(L);
LuaBinding(mod)
.addFunction("get_my_svr_id", &Util::GetMySvrId)
;
mod.pushToStack();
return 1;
}
int main()
{
...
LuaRef table(L, "package.preload");
table["c_util"] = LuaRef::createFunctionWith(L, open_my_module);
...
}
Lua 测试:
assert(c_util == nil)
local t = require("c_util")
assert("table" == type(t))
assert("function" == type(t.get_my_svr_id))
(金庆的专栏 2017.5)
参考:
How to make exported module non-global?
https://github.com/SteveKChiu/lua-intf/issues/135
动态库可以这样导出模块:
extern "C"
#if defined(_MSC_VER) || defined(__BORLANDC__) || defined(__CODEGEARC__)
__declspec(dllexport)
#endif
int luaopen_modname(lua_State* L)
{
LuaRef mod = LuaRef::createTable(L);
LuaBinding(mod)
...;
mod.pushToStack();
return 1;
}
如果不是动态库,可以这样导出全局模块 c_util:
int main()
{
...
LuaBinding(L).beginModule("c_util")
.addFunction("foo", []() { return 123; })
.endModule();
...
}
如果不想让它成为全局模块,则需要在 package.preload 表中注册一个加载函数.
Lua程序设计 第3版 英文版 programming in lua 3ed
The preload searcher allows the definition of an arbitrary function to load a module.
It uses a table, called package.preload, to map module names to loader functions.
When searching for a module name, this searcher simply looks for the given name in the table.
If it finds a function there, it returns this function as the module loader.
Otherwise, it returns nil.
This searcher provides a generic method to handle some non-conventional situations.
For instance, a C library statically linked to Lua can register its luaopen_ function into the preload table,
so that it will be called only when (and if) the user requires that module.
In this way, the program does not waste time opening the module if it is not used.
代码示例:
extern "C" int open_my_module(lua_State* L)
{
LuaRef mod = LuaRef::createTable(L);
LuaBinding(mod)
.addFunction("get_my_svr_id", &Util::GetMySvrId)
;
mod.pushToStack();
return 1;
}
int main()
{
...
LuaRef table(L, "package.preload");
table["c_util"] = LuaRef::createFunctionWith(L, open_my_module);
...
}
Lua 测试:
assert(c_util == nil)
local t = require("c_util")
assert("table" == type(t))
assert("function" == type(t.get_my_svr_id))
0 0
- 用preload加载Lua导出模块
- Lua动态加载模块
- lua 重新加载模块
- Lua动态加载模块
- Lua加载模块
- Lua自定义方式加载模块
- Lua自定义方式加载模块
- PRELOAD
- cocos2dx lua 加载cocostudio 导出场景
- 以自定义方式加载 lua 模块
- 以自定义方式加载 lua 模块
- Lua 5.3 动态加载C模块
- Cocos2d-lua(四)加载CocosStudio导出的UI界面
- LUA模块
- lua模块
- lua 模块
- Linux内核模块:初始化、加载/卸载、模块参数、导出符号、错误处理、模块装载竞争
- lua加载lua文件
- 4.1 Spring beans的创建和配置(XML方式)
- 2017.5.10 华容道 失败总结
- FineUIMvc新特性速递(大间距模式,隐藏菜单垂直滚动条)
- Android.mk浅分析
- Mongodb学习(2)概念学习——ACID原则
- 用preload加载Lua导出模块
- windows10在python2.7的anaconda下安装py3.5.2并安装tensorflow遇到的问题
- Java实现RSA非对称加密
- oracle自增序列
- Git生成SSHkey
- SQL之操作字符串函数CONCAT、SUBSTR、SUBSTRING、SUBSTRING_INDEX、LEFT、RIGHT
- 老王大数据教程(二) Hadoop eclipse 开发
- 逻辑回归学习总结
- .\Opt\TMES.axf: Error: L6406E: No space in execution regions with .ANY selector matching indicate_se