Replacement for luaL_getMetaTable

I want to include Lua-Scripting (Lua 5.1) in a Delphi application. For this I use Thomas Laverne header files.

Now I am trying to register the userdata type following this example: http://www.lua.org/pil/28.2.html

The "new array function" uses the * luaL_getmetatable * command.

static int newarray (lua_State *L) { int n = luaL_checkint(L, 1); size_t nbytes = sizeof(NumArray) + (n - 1)*sizeof(double); NumArray *a = (NumArray *)lua_newuserdata(L, nbytes); luaL_getmetatable(L, "LuaBook.array"); lua_setmetatable(L, -2); a->size = n; return 1; /* new userdatum is already on the stack */ } 

Unfortunately, the * luaL_getmetatable * function is marked as old in my header file and commented out. I tried activating it again, but as expected, I will get an error because the dll entry point was not found.

This is a Delphi translation of this example (using a different data type without an array)

 Type tMyType = tWhatever; pMyType = ^tMyType; {...} Function newusertype(aState : pLua_State) : LongInt; cdecl; Var NewData : pMyType; Begin Result := 0; NewData := lua_newuserdata(aState, SizeOf(tMyType )); NewData^ := GetInitValue; luaL_getMetaTable(aState, 'myexcample.mytype'); // Error/unknown function lua_setmetatable(aState, -2); Result := 1; End; 

Now I am looking for a replacement for luaL_getMetaTable. I did not find any information about this. In fact, I did not find any information that luaL_getMetaTable is out of date, but it looks like this: (.

+4
source share
1 answer

use lua_newmetatable(aState, 'myexample.mytype') . The fact is that (if you want to continue only if the metathe already exists), you will need to evaluate whether it returns 0! If it returns 0, then he wants to create a metatable ... in this case, you can lua_pop(aState, 1) .

Just remember that lua_newmetatable is a function that returns an integer (which should actually be boolean).

Otherwise, you can wait a few weeks for me to release Lua4Delphi version 2, which makes all this super easy (and the Professional version actually automates the registration of Delphi types and instances with Lua)

+3
source

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


All Articles