Trending September 2023 # Learn The Working Of The Userdata Type In Lua Programming # Suggested October 2023 # Top 15 Popular | Cersearch.com

Trending September 2023 # Learn The Working Of The Userdata Type In Lua Programming # Suggested October 2023 # Top 15 Popular

You are reading the article Learn The Working Of The Userdata Type In Lua Programming updated in September 2023 on the website Cersearch.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested October 2023 Learn The Working Of The Userdata Type In Lua Programming

Introduction to Lua userdata

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Note – There are two kinds of userdata.

Light userdata- Here the block of memory is managed by the host.

Full userdata – Here the block of memory is managed by Lua.

We can create a userdata from C/C++ API, using the lua_newuserdata() method, that creates and push userdata to the stack and return the pointer to its content to initialize it.

Working of the userdata type in Lua programming Examples

Example of userdata type in Lua programming to show how to create the userdata in C++ –

Program Example #1

extern “C” { } namespace { void PushSimpleValue(lua_State* lua, int value) { auto& udata = *(int*)lua_newuserdata(lua, sizeof(int)); udata = value; lua_newtable(lua); lua_pushstring(lua, “__call”); lua_pushcfunction(lua, [](lua_State* lua){ auto& udata = *(int*)lua_touserdata(lua, 1); lua_pushinteger(lua, udata); return 1; }); lua_settable(lua, -3); lua_setmetatable(lua, -2); } struct Struct { int x = 0; int y = 0; }; void PushSimpleStruct(lua_State* lua, const Struct& s) { auto& udata = *(Struct*)lua_newuserdata(lua, sizeof(Struct)); udata = s; lua_newtable(lua); lua_pushstring(lua, “__index”); lua_pushcfunction(lua, [](lua_State* lua){ auto& udata = *(Struct*)lua_touserdata(lua, 1); const std::string key = lua_tostring(lua, 2); if (key == “x”) { lua_pushinteger(lua, udata.x); } else if (key == “y”) { lua_pushinteger(lua, udata.y); } else { lua_pushnil(lua); } return 1; }); lua_settable(lua, -3); lua_setmetatable(lua, -2); } struct TObject { int v; std::string s; }; void OwnedObjectPush(lua_State* lua, TObject&& t) { auto udata = (TObject*)lua_newuserdata(lua, sizeof(TObject)); new (udata) TObject(std::move(t)); lua_newtable(lua); lua_pushstring(lua, “__gc”); lua_pushcfunction(lua, [](lua_State* lua){ (void)printf(“Releasing shared object.n”); auto udata = (TObject*)lua_touserdata(lua, 1); return 0; }); lua_settable(lua, -3); lua_pushstring(lua, “__index”); lua_pushcfunction(lua, [](lua_State* lua){ auto& udata = *(TObject*)lua_touserdata(lua, 1); const std::string key = lua_tostring(lua, 2); if (key == “v”) { lua_pushinteger(lua, udata.v); } else if (key == “s”) { lua_pushlstring(lua, udata.s.c_str(), udata.s.length()); } else { lua_pushnil(lua); } return 1; }); lua_settable(lua, -3); lua_setmetatable(lua, -2); } lua, ); lua_newtable(lua); lua_pushstring(lua, “__gc”); lua_pushcfunction(lua, [](lua_State* lua){ (void)printf(“Shared object releasing.n”); return 0; }); lua_settable(lua, -3); lua_pushstring(lua, “__index”); lua_pushcfunction(lua, [](lua_State* lua){ const std::string key = lua_tostring(lua, 2); if (key == “v”) { lua_pushinteger(lua, urdata.v); } else if (key == “s”) { lua_pushlstring(lua, urdata.s.c_str(), urdata.s.length()); } else { lua_pushnil(lua); } return 1; }); lua_settable(lua, -3); lua_setmetatable(lua, -2); } void WithLua(lua_State* lua, const char* chunk, int nargs) { lua_insert(lua, -nargs – 1); (void)lua_call(lua, nargs, 0); } } int main() { const auto lua = luaL_newstate(); lua_gc(lua, LUA_GCSTOP, 0); luaL_openlibs(lua); lua_gc(lua, LUA_GCRESTART, 0); PushSimpleValue(lua, 100); WithLua(lua, R”lua( local urdata = … print(“Simple value: ” .. urdata()) )lua”, 1); TObject Tbobj1; Tbobj1.v = 99; Tbobj1.s = “Hello,”; OwnedObjectPush(lua, std::move(Tbobj1)); WithLua(lua, R”lua( local urdata = … print(“An owned object: (v:” .. urdata.v .. “, s:'” .. urdata.s .. “‘)”) )lua”, 1); Struct st; st.x = 20; st.y = 60; PushSimpleStruct(lua, st); WithLua(lua, R”lua( local urdata = … print(“THe Simple struct: (x:” .. urdata.x .. “, y:” .. urdata.y .. “)”) )lua”, 1); new TObject(), [&](TObject* p){ delete p; (void)printf(“The shared object was destroyed!n”); } }; PushShared(lua, Tbobj2); Tbobj2.reset(); WithLua(lua, R”lua( local urdata = … print(“The Shared object: (v:” .. urdata.v .. “, s:'” .. urdata.s .. “‘)”) )lua”, 1); (void)printf(“Destroy Lua instance.n”); lua_close(lua); (void)printf(“Done.n”); return EXIT_SUCCESS; }

Output:

Conclusion

The userdata is one of the basic types in Lua. Userdata represents an arbitrary C/C++ data that can be used in Lua.

Recommended Articles

We hope that this EDUCBA information on “Lua userdata” was beneficial to you. You can view EDUCBA’s recommended articles for more information.

You're reading Learn The Working Of The Userdata Type In Lua Programming

Update the detailed information about Learn The Working Of The Userdata Type In Lua Programming on the Cersearch.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!