gpt4 book ai didi

多次调用loadbuffer后的lua行号

转载 作者:行者123 更新时间:2023-12-01 22:09:57 34 4
gpt4 key购买 nike

我使用 loadbuffer 将两个字符串加载到一个 lua_state 中。

if( luaL_loadbuffer( L, str.c_str(), str.size(), "line") != 0 )
{
printf( "%s\n", lua_tostring ((lua_State *)L, -1));
}
lua_pcall(L, 0, 0, 0);

if( luaL_loadbuffer( L, str2.c_str(), str2.size(), "line2") != 0 )
{
printf( "%s\n", lua_tostring ((lua_State *)L, -1));
}
lua_pcall(L, 0, 0, 0);

例如:

function f ()


print( "Hello World!")
end

function g ()

f(
end

第二个字符串中被遗忘的 ) 会引发错误:

[string "line2"]:9: unexpected Symbol

但是9是字符串1加字符串2的行号。行号应该是3。

有没有办法在调用 loadbuffer 之前重置行号计数器?

最佳答案

我想这个链接描述了您的情况: http://www.corsix.org/content/common-lua-pitfall-loading-code

您正在加载两个信息 block ,调用这些 block 会将它们连续放入全局表中。 lua_pcall(L, 0, 0, 0); 不是调用您的 f()g(),而是构建您的 lua代码顺序。

您的代码可能会简化为:

if (luaL_dostring(L, str.c_str()))
{
printf("%s\n", lua_tostring (L, -1));
}
if (luaL_dostring(L, str2.c_str()));
{
printf("%s\n", lua_tostring (L, -1));
}

这还可以防止在加载失败时调用 block ;

关于多次调用loadbuffer后的lua行号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16959280/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com