【发布时间】:2020-11-17 23:46:44
【问题描述】:
我注意到,在我的目标平台(32-bitPowerPC 架构)上使用C API 在lua 中将值推入/弹出堆栈的行为非常奇怪。考虑以下测试函数:
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_pushinteger、lua_pushnumber、lua_tointeger 和 lua_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_integer是unsigned long long并且popped_number是double?如果不是,您使用了错误的 printf 代码 -
好话,定义是:
#define LUA_NUMBER float #define LUA_INTEGER int -
也就是说
popped_integer不是unsigned long long和popped_number不是double?这就是为什么有人说要避免使用printf而只使用cout。 -
对,我正在使用
-DLUA_32BITS,否则它不会编译 -
现在你知道一个问题了。
标签: c++ lua 32-bit powerpc lua-api