tomaka / hlua

Rust library to interface with Lua
MIT License
510 stars 48 forks source link

Panic while trying to iterate LuaTable #134

Open VictorKoenders opened 7 years ago

VictorKoenders commented 7 years ago

I'm trying to map a LuaTable into a simpler enum to work with, but when I try to iterate a hlua::LuaTable I get a panic deep in the C lua code. https://gist.github.com/VictorKoenders/646393fe46dd6c6f282968a69fec6183

Is there a better way of doing this?

Rust 1.17.0 nightly Cargo 0.18.0 hlua 0.3.1

VictorKoenders commented 7 years ago

The functions that are being called are:

lua_newstate(0x7ff6746c0e40, 0x0)
lua_atpanic(0x25c332b96d0, 0x7ff6746c0ec0)
luaL_openlibs(0x25c332b96d0)
lua_load(0x25c332b96d0, 0x7ff6746bc9a0, 0xa6f2aff2e0, 0x7ff67470f858, 0x0)
lua_pushvalue(0x25c332b96d0, -1)
lua_pcallk(0x25c332b96d0, 0, 1, 0, 0, None)
lua_settop(0x25c332b96d0, -2)
lua_settop(0x25c332b96d0, -2)
lua_getglobal(0x25c332b96d0, 0x25c332b2dd0)
lua_type(0x25c332b96d0, -1)
lua_tonumberx(0x25c332b96d0, -1, 0xa6f2aff584)
lua_type(0x25c332b96d0, -1)
lua_tolstring(0x25c332b96d0, -1, 0xa6f2aff500)
lua_type(0x25c332b96d0, -1)
lua_settop(0x25c332b96d0, -2)
lua_getglobal(0x25c332b96d0, 0x25c332b2e80)
lua_type(0x25c332b96d0, -1)
lua_type(0x25c332b96d0, -1)
lua_pushnil(0x25c332b96d0)
lua_next(0x25c332b96d0, -2)
lua_tolstring(0x25c332b96d0, -2, 0xa6f2aff120)
lua_tonumberx(0x25c332b96d0, -1, 0xa6f2afefa4)
lua_type(0x25c332b96d0, -1)
lua_tolstring(0x25c332b96d0, -1, 0xa6f2afef20)
lua_type(0x25c332b96d0, -1)
lua_settop(0x25c332b96d0, -2)
"0" is of type "other"
lua_next(0x25c332b96d0, -2)
lua_tolstring(0x25c332b96d0, -1, 0x0)
thread 'main' panicked at 'PANIC: unprotected error in call to Lua API (invalid key to 'next')
', libs\hlua-master\hlua\src\lib.rs:411
note: Run with `RUST_BACKTRACE=1` for a backtrace.
lua_settop(0x25c332b96d0, -2)
lua_settop(0x25c332b96d0, -2)
lua_close(0x25c332b96d0)
tomaka commented 7 years ago

That can't be correct, it doesn't even starts iterating on the table.

VictorKoenders commented 7 years ago

You're right, I ran the wrong script. I've updated the original message

tomaka commented 7 years ago

Oh I think the problem is:

While traversing a table, do not call lua_tolstring directly on a key, unless you know that the key is actually a string. Recall that lua_tolstring changes the value at the given index; this confuses the next call to lua_next.

(as documented here: http://pgl.yoyo.org/luai/i/lua_next)

I have recently modified this code to use this function, and I guess it's wrong to do that.

VictorKoenders commented 7 years ago

Ah yes changing let key: String = key; to let key: hlua::AnyLuaValue = key; Fixed the issue. The next value was an LuaNumber(0) so that's where hlua tried reading a string.

Thank you!

tomaka commented 7 years ago

Err, it's still a bug. Don't close this.