试图从C加载penlight lua模块

Trying to load penlight lua modules from C

本文关键字:penlight lua 模块 加载      更新时间:2023-10-16

我开始用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_pcalllua_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函数的部分)。

第二种方法是加载但不运行模块,并使用模块名称作为键将加载的代码(这是一个Lua函数)放在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模块。