Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Panic while trying to iterate LuaTable #134

Open
VictorKoenders opened this issue Mar 3, 2017 · 6 comments
Open

Panic while trying to iterate LuaTable #134

VictorKoenders opened this issue Mar 3, 2017 · 6 comments
Labels

Comments

@VictorKoenders
Copy link

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

@tomaka tomaka added the bug label Mar 3, 2017
@VictorKoenders
Copy link
Author

VictorKoenders commented Mar 7, 2017

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
Copy link
Owner

tomaka commented Mar 7, 2017

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

@VictorKoenders
Copy link
Author

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

@tomaka
Copy link
Owner

tomaka commented Mar 7, 2017

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
Copy link
Author

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
Copy link
Owner

tomaka commented Mar 7, 2017

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants