用preload加载Lua导出模块

用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))

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值