Lua_touserdata returns null

I got stuck trying to get my help information. One of my methods returns an instance of an object. Each time createUserInfo is called, it returns userInfoObject in lua.

However, when I call the method for the userInfo object from Lua, I cannot get the userInfo object reference ( lua_touserdata (L, 1) )

static int getUserName (lua_State *L){ UserInfo **userInfo = (UserInfo**)lua_touserdata(L,1); // The following is throwing null! Need help. // Not able to access the userInfo object. NSLog(@"UserInfo Object: %@", *userInfo); } static const luaL_reg userInstance_methods[] = { {"getUserName", getUserName}, {NULL, NULL} } int createUserInfo(lua_State *L){ UserInfo *userInfo = [[UserInfo alloc] init]; UserInfoData **userInfoData = (UserInfoData **)lua_newuserdata(L, sizeof(userInfo*)); *userInfoData = userInfo; luaL_openlib(L, "userInstance", userInstance_methods, 0); luaL_getmetatable(L, "userInfoMeta"); lua_setmetatable(L, -2); return 1; } // I have binded newUserInfo to the createUserInfo method. // I have also created the metatable for this userInfo Object in the init method. // luaL_newmetatable(L, "userInfoMeta"); // lua_pushstring(L, "__index"); // lua_pushvalue(L, -2); // lua_settable(L, -3); // luaL_register(L, NULL, userInstance_methods); 

Please let me know if I am missing something!

My LuaCode fragment:

 local library = require('plugin.user') local userInfo = library.newUserInfo() print(userInfo.getUserName()) 

Update I got rid of null after using lua_upvalueindex (1). This returns a link to an instance of user information.

 UserInfo **userInfo = (UserInfo**)lua_touserdata(L,lua_upvalueindex( 1 )); 

Hope this helps others too!

+4
source share
2 answers

I think this may be how you deal with userdata metadata. In particular, I think that you are returning from createUserInfo() , this is a table, not userdata. I suggest that you create metadata once, for example. in luaopen, and then just set this to the new user data. Something like that...

 int createUserInfo(lua_State *L) { UserInfo *userInfo = [[UserInfo alloc] init]; UserInfoData **userInfoData = (UserInfoData **)lua_newuserdata(L, sizeof(userInfo)); *userInfoData = userInfo; luaL_getmetatable(L, "userInfoMeta"); lua_setmetatable(L, -2); return 1; } LUALIB_API int luaopen_XXX(lua_State *L) { luaL_newmetatable(L,"userInfoMeta"); luaL_openlib(L, NULL, userInstance_methods, 0); lua_pushvalue(L, -1); lua_setfield(L, -2, "__index"); ... 
+2
source

lua_upvalueindex (1) fixed a nil error.

 UserInfo **userInfo = (UserInfo**)lua_touserdata(L,lua_upvalueindex( 1 )); 

I want to briefly explain what really happens. a function in C will receive a stack of parameters passed to the method. There is an example array in the Lua online doc, where all of its methods accept the first parameter of an array instance. so lua_touserdata (L, 1) worked fine, since the first parameter is an array instance.

An example from lua.org shows

 a = array.new(10) --size 10 array.insert(a, 1, 1) --array.insert(instance, index, value). 

lua_touserdata (L, 1) works as the first parameter - an array instance.

In my case, I called the method through an instance without any parameters . So the lua stack was empty in my C function, and lua_touserdata (L, 1) threw zero.

Example:

 a = array.new(10) a.showValues() --the method is not over array. it is called on instance. 

So, in order to access the instance in showValues, I need to call lua_touserdata (L, lua_upvalueindex (1)). This will give an array instance object.

0
source

Source: https://habr.com/ru/post/1500290/


All Articles