【问题标题】:Strange lua push/pop behavior with the C API使用 C API 的奇怪的 lua 推送/弹出行为
【发布时间】:2020-11-17 23:46:44
【问题描述】:

我注意到,在我的目标平台(32-bitPowerPC 架构)上使用C APIlua 中将值推入/弹出堆栈的行为非常奇怪。考虑以下测试函数:

void test_pushing_and_popping(lua_State *lua) {
    printf("### STARTING PUSHING/POPPING TEST ###\n");
    lua_dump_stack(lua);
    const auto value = static_cast<uint32_t>(0x206D7578);
    lua_pushinteger(lua, value);
    lua_pushnumber(lua, value);
    const auto popped_integer = lua_tointeger(lua, 1);
    const auto popped_float = lua_tonumber(lua, 2);
    printf("Popped integer: %llx\n", popped_integer);
    printf("Popped float: %f\n", popped_float);
    lua_dump_stack(lua);
    printf("Asserting equality... ");
    assert(popped_integer == popped_float);
    printf("OK!\n");
    printf("Wiping the stack...\n");
    lua_settop(lua, 0);
    lua_dump_stack(lua);
    printf("### PUSHING/POPPING TEST ENDED ###\n");
}

我希望这段代码能够成功运行,并且断言将被传递,因为相同的值被推送,然后分别以 lua_pushintegerlua_pushnumberlua_tointegerlua_tonumber 弹出。

确实,如果我将Windows 上的代码作为32-bit 二进制文件运行,它可以工作:

### STARTING PUSHING/POPPING TEST ###
Dumping the lua stack...
Stack element count: 0
Popped integer: 206d7578
Popped float: 544044408.000000
Dumping the lua stack...
Stack element count: 2
1       number  0x206D7578
2       number  0x206D7578
Asserting equality... OK!
Wiping the stack...
Dumping the lua stack...
Stack element count: 0
### PUSHING/POPPING TEST ENDED ###

同样在 Linux 上作为 64-bit 二进制它可以工作。

但是,这是我的目标平台上的输出:

### STARTING PUSHING/POPPING TEST ###
Dumping the lua stack...
Stack element count: 0
Popped integer: 8002b2bc00e3a9ac <-- Wrong!
Popped float: 544044416.000000   <-- Wrong!
Dumping the lua stack...
Stack element count: 2
1   number 0x80000000 <-- Wrong!
2   number 0x206d7580 <-- Wrong!
Asserting equality... <-- Crash (assertion fail)!

什么?第一个值是完全错误的,甚至第二个值也是错误的。 544044416.000000 转换为十六进制是0x206d7580,它也不等于0x206D7578 的推送值。哪里来的不准确?)。

我很确定我没有损坏任何东西,因为我在初始化 lua 后立即运行了测试:

printf("Initializing lua...\n");
lua = luaL_newstate(); /* Opens Lua */
luaL_openlibs(lua); /* Opens the standard libraries */
test_pushing_and_popping(lua);

有人知道可能是什么问题吗?

【问题讨论】:

  • 您确定popped_integerunsigned long long 并且popped_numberdouble?如果不是,您使用了错误的 printf 代码
  • 好话,定义是:#define LUA_NUMBER float #define LUA_INTEGER int
  • 也就是说popped_integer 不是unsigned long longpopped_number 不是double?这就是为什么有人说要避免使用printf而只使用cout
  • 对,我正在使用-DLUA_32BITS,否则它不会编译
  • 现在你知道一个问题了。

标签: c++ lua 32-bit powerpc lua-api


【解决方案1】:

我得到的初始编译错误的措辞如下:

#error "Compiler does not support 'long long'. Use option '-DLUA_32BITS' \
  or '-DLUA_C89_NUMBERS' (see file 'luaconf.h' for details)"

原来我使用了-DLUA_32BITS 编译标志,导致数据类型大小太小:

#define LUA_INTEGER int
#define LUA_NUMBER float

使用最大可能是可以通过标志完成的方法:-DLUA_C89_NUMBERS

这将数据类型定义为:

#define LUA_INTEGER long
#define LUA_NUMBER double

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多