重写require后调用"lua_getfield()"时崩溃
Crashes when calling `lua_getfield()` after overriding the require
这个问题与@Henri_Menke对这个问题的回答有关:如何在C++中获得预加载的模块名称
我正在尝试用我自己的版本覆盖require
函数,这样我就可以在Lua脚本中获得预加载的模块名称。
这是我的代码:
#include "lua.hpp"
void main()
{
lua_State *L = luaL_newstate();
luaL_openlibs(L);
lua_settop(L, 0);
luaL_dostring(L, "local require_original = requiren"
"function require(name, ...)n"
"current_module = namen"
"require_original(name, ...)n"
"current_module = niln"
"endn"); //if I comment out this chunk, it works fine
luaL_dostring(L, "package.preload['test'] = function ()n"
"local test = {}n"
"print('While loading:', current_module)n"
"function test.update() print('Updated!') endn"
"return testn"
"endn");
lua_getglobal(L, "require");
lua_pushstring(L, "test");
if (lua_pcall(L, 1, LUA_MULTRET, 0))
{
std::cout << "Error: " << lua_tostring(L, -1) << std::endl;
lua_pop(L, 1);
}
int top = lua_gettop(L);
lua_getfield(L, -1, "update"); //crashes here
if (lua_isfunction(L, -1))
{
lua_pushnil(L);
if (lua_pcall(L, 1, LUA_MULTRET, 0))
{
std::cout << "Error: " << lua_tostring(L, -1) << std::endl;
lua_pop(L, 1);
}
}
lua_close(L);
}
但是,它在调用lua_getfield(L, -1, "update");
时崩溃。
当我注释掉Lua脚本的第一块(第一个luaL_dostring
(时,它不再崩溃。
我不明白如果我使用自己版本的require
,它为什么会崩溃。
我该怎么解决这个问题?
我忘记从重写的require
函数返回模块表。很抱歉。
#include <iostream>
#include "lua.hpp"
int main() {
lua_State *L = luaL_newstate();
luaL_openlibs(L);
lua_settop(L, 0);
luaL_dostring(L, "local require_original = requiren"
"function require(name, ...)n"
"current_module = namen"
"local val = table.pack(require_original(name, ...))n"
"current_module = niln"
"return table.unpack(val,1,val.n)n"
"endn"); //if I comment out this chunk, it works fine
luaL_dostring(L, "package.preload['test'] = function ()n"
"local test = {}n"
"print('While loading:', current_module)n"
"function test.update() print('Updated!') endn"
"return testn"
"endn");
lua_getglobal(L, "require");
lua_pushstring(L, "test");
if (lua_pcall(L, 1, LUA_MULTRET, 0))
{
std::cout << "Error: " << lua_tostring(L, -1) << std::endl;
lua_pop(L, 1);
}
int top = lua_gettop(L);
lua_getfield(L, -1, "update"); //crashes here
if (lua_isfunction(L, -1))
{
lua_pushnil(L);
if (lua_pcall(L, 1, LUA_MULTRET, 0))
{
std::cout << "Error: " << lua_tostring(L, -1) << std::endl;
lua_pop(L, 1);
}
}
lua_close(L);
}
$ clang++ -Wall -Wextra -Wpedantic -I /usr/include/lua5.2 test.cpp -llua5.2
test.cpp:28:9: warning: unused variable 'top' [-Wunused-variable]
int top = lua_gettop(L);
^
1 warning generated.
$ ./a.out
While loading: test
Updated!
相关文章:
- 当回溯以零开始时,如何调试崩溃
- 内联映射初始化的动态atexit析构函数崩溃
- 执行函数时导致崩溃的变量
- 程序崩溃并显示"std::out_of_range"错误
- CoInitialize()在单独的线程上崩溃而不返回
- 使用调试/崩溃报告将应用程序部署到客户端
- 为什么所有C++编译器都会崩溃或挂起此代码
- 为什么lambda在clang上崩溃而不是在gcc上崩溃
- 为什么我的多线程作业队列崩溃
- ExtractIconEx:可以工作,但偶尔会崩溃
- 为什么引用传递会导致此崩溃(C++)
- 试图创建流或fopen时程序崩溃
- 类对象数组的问题会导致崩溃
- 排序时无法执行交换操作.我做的时候它会崩溃.为什么
- 为什么要增加导致崩溃的指针
- 在虚幻引擎中删除NXOpen对象时崩溃
- 为什么它只打印双链接列表的第一个值,而我的程序却崩溃了
- 应用程序崩溃并显示"symbol _ZdlPvm, version Qt_5 not defined in file libQt5Core.so.5 with link time reference"
- Visual Studio在尝试读取resource.txt文件时崩溃
- 将方法转换为调用该方法的成员函子对象会导致崩溃