|
I have a Lua script that looks basically like so: x, y = lookup(a, b, c) if x then use(m, y, n) end “lookup” actually is a registered C function that does a hash table lookup. On a miss, “x” is set to false and “y” to nil; on a hit, “x” is set to true and “y” is set to a new userdata with a metatable assigned to it using: F = lua_newuserdata(L, sizeof (*F)); (“F” is populated with lookup results here) luaL_getmetatable(L, name); lua_setmetatable(L, -2); So on a hit, “y” (in the Lua script) should be set to “F” (from the C side), and internally the metatable “name” is associated with it. On completion of this, I can confirm that there’s a metatable associated with F. With “F” at the top of the stack: (gdb) print (void *) lua_topointer(L, -1) $7 = (void *) 0xa1348b8 So that’s F; then: (gdb) print lua_getmetatable(L, -1) $8 = 1 (gdb) print (void *) lua_topointer(L, -1) $9 = (void *) 0xa131c90
ud = lua_touserdata(L, n); if (ud != NULL) if (lua_getmetatable(L, n) != 0) { lua_getfield(L, LUA_REGISTRYINDEX, name); if (lua_rawequal(L, -1, -2)) { lua_pop(L, 2); return ud; } lua_pop(L, 2); } } return NULL; However, lua_getmetatable() returns 0, apparently unable to find the associated metatable. I’ve confirmed that the pointer created by func() is the same as the one being passed to use(). Any suggestions? I’m really puzzled; it shouldn’t be garbage collection because the script hasn’t terminated and the values haven’t been reassigned on the script side, so that pointer should still reference something valid (and I checked that too; it does). Thanks, -MSK |