From 330ca90b8af24c1fdc56aab80809d0992c53a181 Mon Sep 17 00:00:00 2001 From: k-m_schindler Date: Fri, 3 May 2013 23:59:09 +0000 Subject: extent for lua 5.2 git-svn-id: svn://svn.code.sf.net/p/ultrastardx/svn/trunk@2981 b956fd51-792f-4845-bead-9b4dfca2ff2c --- src/lib/Lua/ULua.pas | 57 +++++++++++++++++++++++++++++++++++++++++++++------- src/lua/ULuaCore.pas | 15 +++++++------- 2 files changed, 57 insertions(+), 15 deletions(-) diff --git a/src/lib/Lua/ULua.pas b/src/lib/Lua/ULua.pas index 41cb7909..9e6b4d31 100644 --- a/src/lib/Lua/ULua.pas +++ b/src/lib/Lua/ULua.pas @@ -407,6 +407,7 @@ function lua_tolstring(L : Plua_State; idx : Integer; function lua_objlen(L : Plua_State; idx : Integer) : size_t; cdecl; external LUA_LIB_NAME; {$ELSEIF LUA_VERSION_NUM >= 502} +function lua_objlen(L : Plua_State; idx : Integer) : size_t; function lua_rawlen(L : Plua_State; idx : Integer) : size_t; cdecl; external LUA_LIB_NAME; {$IFEND} @@ -889,11 +890,13 @@ procedure luaL_openlib(L : Plua_State; const libname : PChar; const lr : PluaL_Reg; nup : Integer); cdecl; external LUA_LIB_NAME; procedure luaL_register(L : Plua_State; const libname : PChar; - const lr : PluaL_Reg); + const lr : PluaL_Reg); cdecl; external LUA_LIB_NAME; -{$IFEND} +{$ELSEIF LUA_VERSION_NUM >= 502} +// Added for simplicity --KMS +procedure luaL_register(L : Plua_State; const libname : PChar; + const lr : PluaL_Reg); -{$IF LUA_VERSION_NUM >= 502} procedure luaL_checkversion(L : Plua_State); procedure luaL_checkversion_(L : Plua_State; ver : lua_Number); cdecl; external LUA_LIB_NAME; @@ -1025,6 +1028,7 @@ function luaL_gsub(L : Plua_State; const s, p, r : PChar) : PChar; function luaL_findtable(L : Plua_State; idx : Integer; const fname : PChar; szhint : Integer) : PChar; cdecl; external LUA_LIB_NAME; +procedure luaL_setfuncs (L : Plua_State; const lr : PluaL_Reg; nup : Integer); {$ELSEIF LUA_VERSION_NUM >= 502} procedure luaL_setfuncs(L : Plua_State; const lr : PluaL_Reg; nup : Integer); overload; @@ -1215,6 +1219,11 @@ begin end; {$IF LUA_VERSION_NUM >= 502} +function lua_objlen(L : Plua_State; idx : Integer) : size_t; +begin + lua_objlen := lua_rawlen(L, idx); +end; + procedure lua_call(L : Plua_State; nargs, nresults : Integer); begin lua_callk(L, nargs, nresults, 0, NIL); @@ -1377,6 +1386,14 @@ begin end; {$ELSEIF LUA_VERSION_NUM >= 502} +procedure luaL_register(L : Plua_State; const libname : PChar; + const lr : PluaL_Reg); +begin + lua_newtable(L); + luaL_setfuncs(L, lr, 0); + lua_pushvalue(L, -1); + lua_setglobal(L, libname); +end; procedure luaL_checkversion(L : Plua_State); begin @@ -1384,10 +1401,12 @@ begin end; {$IFEND} +{$IF LUA_VERSION_NUM >= 502} function luaL_loadfile(L : Plua_State; const filename: PChar): Integer; begin Result := luaL_loadfilex(L, filename, nil); end; +{$IFEND} {$IF LUA_VERSION_NUM >= 502} function luaL_loadbuffer(L : Plua_State; const buff : PChar; @@ -1397,18 +1416,42 @@ begin end; {$IFEND} +{$IF LUA_VERSION_NUM = 501} +(* +** Adapted from Lua 5.2.0 and this page +** http://lua-users.org/wiki/CompatibilityWithLuaFive +** It enables the replacement of luaL_register by luaL_setfuncs in ULuaCore +** As of now, it is not used. +*) +procedure luaL_setfuncs (L : Plua_State; const lr : PluaL_Reg; nup : Integer); +var + i : integer; +begin + luaL_checkstack(L, nup, 'too many upvalues'); + repeat (* fill the table with given functions *) + for i := 1 to nup do (* copy upvalues to the top *) + lua_pushvalue(L, -nup); + lua_pushstring(L, lr.name); + lua_pushcclosure(L, lr.func, nup); (* closure with those upvalues *) + lua_settable(L, -(nup + 3)); +// inc(lr); + until (lr.name = NIL); + lua_pop(L, nup); (* remove upvalues *) +end; +{$ELSEIF LUA_VERSION_NUM >= 502} procedure luaL_setfuncs(L : Plua_State; const lr : array of luaL_Reg; nup : Integer); overload; begin luaL_setfuncs(L, @lr, nup); end; +{$IFEND} -procedure luaL_newlibtable(L : Plua_State; lr : array of luaL_Reg); +procedure luaL_newlibtable(L : Plua_State; lr : array of luaL_Reg); overload; begin lua_createtable(L, 0, High(lr)); end; -procedure luaL_newlibtable(L : Plua_State; lr : PluaL_Reg); +procedure luaL_newlibtable(L : Plua_State; lr : PluaL_Reg); overload; var n: Integer; begin @@ -1421,13 +1464,13 @@ begin lua_createtable(L, 0, n); end; -procedure luaL_newlib(L : Plua_State; lr : array of luaL_Reg); +procedure luaL_newlib(L : Plua_State; lr : array of luaL_Reg); overload; begin luaL_newlibtable(L, lr); luaL_setfuncs(L, @lr, 0); end; -procedure luaL_newlib(L : Plua_State; lr : PluaL_Reg); +procedure luaL_newlib(L : Plua_State; lr : PluaL_Reg); overload; begin luaL_newlibtable(L, lr); luaL_setfuncs(L, lr, 0); diff --git a/src/lua/ULuaCore.pas b/src/lua/ULuaCore.pas index d1b6e7d2..a0398eb4 100644 --- a/src/lua/ULuaCore.pas +++ b/src/lua/ULuaCore.pas @@ -564,16 +564,17 @@ begin {**** replace the standard require with our custom require function } // first move standard require function to _require - lua_getfield(L, LUA_GLOBALSINDEX, PChar('require')); - lua_setfield(L, LUA_GLOBALSINDEX, PChar('_require')); + lua_getglobal(L, PChar('require')); + + lua_setglobal(L, PChar('_require')); // then save custom require function to require lua_pushcfunction(L, TLua_CustomRequire); - lua_setfield(L, LUA_GLOBALSINDEX, PChar('require')); + + lua_setglobal(L, PChar('require')); {**** now we create the usdx table } // at first functions from ULuaUsdx -// Function luaL_register is deprecated. Use luaL_setfuncs so that your module does not create globals. (Modules are not expected to set global variables anymore.) luaL_register(L, 'Usdx', @ULuaUsdx_Lib_f[0]); end; @@ -651,7 +652,6 @@ begin begin Id := lua_ToInteger(L, lua_upvalueindex(1)); -// Function luaL_register is deprecated. Use luaL_setfuncs so that your module does not create globals. (Modules are not expected to set global variables anymore.) luaL_register(L, PChar('Usdx.' + LuaCore.Modules[Id].Name), @LuaCore.Modules[Id].Functions[0]); // set the modules table as global "modulename" @@ -846,8 +846,7 @@ begin // we need at least one stack slot free lua_checkstack(State, 1); - // lua_getglobal(State, PChar(Name)); // this is just a macro: - lua_getfield(State, LUA_GLOBALSINDEX, PChar(Name)); + lua_getglobal(State, PChar(Name)); if (lua_isfunction(State, -1)) then begin // we got a function @@ -1015,7 +1014,7 @@ begin while (lua_getTop(L) >= 1) do begin // get luas require function - lua_getfield(L, LUA_GLOBALSINDEX, PChar('_require')); + lua_getglobal(L, PChar('_require')); // move it under the top param lua_insert(L, -2); -- cgit v1.2.3