试图从C加载penlight lua模块
Trying to load penlight lua modules from C
我开始用lua了,到目前为止,我有一个相当令人沮丧的经历。我需要加载penlight的部分,以便加载"mine"。需要pl.import_into的Lua。我需要从C/c++做这个。
我使用lua 5.1;使用5.2不是一个选项,所以我没有luaL_requiref
我的代码大致是这样的:void luaLoad(lua_State* L, const char* f) {
if (luaL_dofile(L, f) || lua_pcall(L, 0, 0, 0)) {
const char* err = lua_tostring(L, -1);
lua_pop(L, -1);
throw std::runtime_error("Lua load error");
}
}
int main(void) {
lua_State *L = luaL_newstate();
luaL_openlibs(L);
luaLoad(L, "~/Penlight-1.3.1/lua/pl/compat.lua");
luaLoad(L, "~/Penlight-1.3.1/lua/pl/utils.lua");
luaLoad(L, "~/Penlight-1.3.1/lua/pl/import_into.lua");
luaLoad(L, "mine.lua");
...
}
我开始尝试加载import_into。Lua,它需要utils。Lua和传递性兼容。Lua .
在我的luaLoad方法中,如果我删除lua_pcall, utils。Lua "没有看到" comat . Lua:
utils.lua : module 'pl.compat' not found`
使用lua_pcall,我得到一个
attempt to call a table value` error.
是否试图从C加载penlight根本错误?
第一:lua_pcall
是多余的。luaL_dofile
已经做了一个lua_pcall
。lua_pop
的调用也是错误的。第二:为什么不直接修改package.path
,以便Lua的require
函数可以找到必要的模块?
如果你不能或不想这样做,有两种方法:
除了运行模块代码之外,还需要使用模块名称作为键将其结果存储在package.loaded
表中。这样,在penlight代码中调用的require
函数可以稍后找到这些模块。
void luaLoad(lua_State* L, char const* m, const char* f) {
int top = lua_gettop(L);
if (luaL_dofile(L, f)) {
const char* err = lua_tostring(L, -1);
lua_pop(L, 1); /* the 2nd parameter is a number not a stack index! */
throw std::runtime_error("Lua load error");
} else {
lua_settop(L, top+1); /* discard anything but first return value */
lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED"); /* get `package.loaded` */
lua_pushvalue(L, -2); /* `lua_setfield` needs the return value at stack top */
lua_setfield(L, -2, m); /* store return value as `package.loaded[m]` */
lus_settop(L, top); /* restore Lua stack */
}
}
的优点是您可以使用相同的函数来运行您的mine.lua
。缺点是你必须以正确的顺序加载模块,如果其中一个模块使用module
函数,代码就会变得有点复杂(基本上你是在重新实现Lua的require
函数的部分)。
package.preload
表中。require
函数稍后可以拾取它们并从那里运行它们。
void luaPreload(lua_State* L, char const* m, char const* f) {
if (luaL_loadfile(L, f)) {
char const* err = lua_tostring(L, -1);
lua_pop(L, 1);
throw std:runtime_error("Lua load error");
} else {
lua_getglobal(L, "package"); /* there is no shorthand for `package.preload` */
lua_getfield(L, -1, "preload");
lua_pushvalue(L, -3); /* `lua_setfield` needs the function at stack top */
lua_setfield(L, -2, m); /* store chunk as `package.preload[m]` */
lua_pop(L, 3); /* restore Lua stack */
}
}
优点是require会自动按正确的顺序运行模块,并且module
函数不需要特殊处理。缺点是您需要单独的代码来运行mine.lua
。
所有这些方法都假设mine.lua
使用Lua的require
函数来访问penlight模块。
- C++Lua用户数据扰乱了Mathfu浮点值
- 使用 LuaBridge 或 Lua 将对象传递给 Lua 函数C++
- 将 Lua 函数块转换为 C 字符串
- 我可以从 lua 5.0.2 加载用 c++ 编写的 lua 5.3.5 dll 吗?
- 使用 lua 协程 API 和lua_close时出现分段错误
- 使用 C++ 自动 Lua 绑定
- 调用 lua 函数的地址为 C/C++?
- lua 5.0.2 模块和 5.3.5 有什么区别?
- 如何将 Lua 添加到C++项目
- 使用 Lua 中的C++库对象
- 如何在C++中将用户数据从一个 Lua 块传递到另一个块
- 如何使用重新定义的打印函数打印Lua表?
- Lua table in C++ dll
- 在运行时环境中使用 C++ w/ Lua
- 将shared_ptr传递给Lua
- luaL_len is missing in Lua 5.1
- 将向量传递给<struct> Lua 表
- 将 Lua 与 C++ DLL 结合使用
- Lua C API - 从C++分配和使用类的对象成员
- 试图从C加载penlight lua模块