minimal changes for compatibility with lua 5.2, thanks to v4hn for fixing standard library loads. note: ALSO compatibile with (internal) lua 5.1, until something bothers to upgrade bundled lua version
authorsheepluva
Mon, 28 Apr 2014 16:52:00 +0200
changeset 10230 361d36ec5181
parent 10229 5d38aaff5564
child 10231 b2a68e75e55c
minimal changes for compatibility with lua 5.2, thanks to v4hn for fixing standard library loads. note: ALSO compatibile with (internal) lua 5.1, until something bothers to upgrade bundled lua version
INSTALL
cmake_modules/FindLua.cmake
hedgewars/CMakeLists.txt
hedgewars/LuaPas.pas
hedgewars/uScript.pas
--- a/INSTALL	Fri Apr 25 23:17:49 2014 +0400
+++ b/INSTALL	Mon Apr 28 16:52:00 2014 +0200
@@ -7,7 +7,7 @@
  - SDL_mixer >= 1.2
  - SDL_image >= 1.2
  - SDL_ttf >= 2.0
- - Lua >= 5.1.0
+ - Lua >= 5.2.0
  - Physfs >= 2.0.0
 For server:
  - Glasgow Haskell Compiler >= 6.10
--- a/cmake_modules/FindLua.cmake	Fri Apr 25 23:17:49 2014 +0400
+++ b/cmake_modules/FindLua.cmake	Mon Apr 28 16:52:00 2014 +0200
@@ -17,10 +17,9 @@
 
 find_path(LUA_INCLUDE_DIR lua.h
                           PATHS /usr/include /usr/local/include /usr/pkg/include
-                          PATH_SUFFIXES lua5.1 lua51)
-find_library(LUA_LIBRARY NAMES lua51 lua5.1 lua-5.1 lua
+                          PATH_SUFFIXES lua5.2 lua52 lua5.1 lua51)
+find_library(LUA_LIBRARY NAMES lua52 lua5.2 lua-5.2 lua51 lua5.1 lua-5.1 lua
                          PATHS /lib /usr/lib /usr/local/lib /usr/pkg/lib)
 
 find_package_handle_standard_args(Lua DEFAULT_MSG LUA_LIBRARY LUA_INCLUDE_DIR)
 mark_as_advanced(LUA_INCLUDE_DIR LUA_LIBRARY)
-
--- a/hedgewars/CMakeLists.txt	Fri Apr 25 23:17:49 2014 +0400
+++ b/hedgewars/CMakeLists.txt	Mon Apr 28 16:52:00 2014 +0200
@@ -148,6 +148,7 @@
     add_flag_append(CMAKE_Pascal_FLAGS "-Fl${LUA_LIBRARY_DIR} -XLAlua=${LUA_LIBRARY_NAME}")
 else()
     add_definitions(-dLUA_INTERNAL)
+    add_definitions(-dLUA51)
     list(APPEND HW_LINK_LIBS lua)
     add_flag_append(CMAKE_Pascal_FLAGS "-XLAlua=${lua_output_name}")
 endif()
--- a/hedgewars/LuaPas.pas	Fri Apr 25 23:17:49 2014 +0400
+++ b/hedgewars/LuaPas.pas	Mon Apr 28 16:52:00 2014 +0200
@@ -8,6 +8,10 @@
  *
  * Created by Geo Massar, 2006
  * Distributed as free/open source.
+ *
+ * Note: This file contains custom modification for Hedgewars
+ * These changes should accomplish compatibility for both lua 5.1 and 5.2
+ * Differences: In 5.1 set/getglobal ARE macros and tonumber/tointeger are NOT
  *)
 
 interface
@@ -238,6 +242,27 @@
 (*
 ** access functions (stack -> C)
 *)
+
+{$IFNDEF LUA51}
+
+function lua_tointegerx(L : Plua_State; idx : LongInt; success : PInteger) : lua_Integer;
+    cdecl; external LuaLibName;
+function lua_tointeger(L : Plua_State; idx : LongInt) : lua_Integer;
+
+function lua_tonumberx(L : Plua_State; idx : LongInt; success : PInteger) : lua_Number;
+    cdecl; external LuaLibName;
+function lua_tonumber(L : Plua_State; idx : LongInt) : lua_Number;
+
+{$ELSE}
+
+function lua_tointeger(L : Plua_State; idx : LongInt) : lua_Integer;
+    cdecl; external LuaLibName;
+
+function lua_tonumber(L : Plua_State; idx : LongInt) : lua_Number;
+    cdecl; external LuaLibName;
+
+{$ENDIF}
+
 function lua_isnumber(L : Plua_State; idx : LongInt) : LongBool;
     cdecl; external LuaLibName;
 
@@ -265,12 +290,6 @@
 function lua_lessthan(L : Plua_State; idx1, idx2 : LongInt) : LongBool;
     cdecl; external LuaLibName;
 
-function lua_tonumber(L : Plua_State; idx : LongInt) : lua_Number;
-    cdecl; external LuaLibName;
-
-function lua_tointeger(L : Plua_State; idx : LongInt) : lua_Integer;
-    cdecl; external LuaLibName;
-
 function lua_toboolean(L : Plua_State; idx : LongInt) : LongBool;
     cdecl; external LuaLibName;
 
@@ -386,8 +405,15 @@
 procedure lua_call(L : Plua_State; nargs, nresults : LongInt);
     cdecl; external LuaLibName;
 
+
+{$IFNDEF LUA51}
+function  lua_pcallk(L: Plua_State; nargs, nresults, errfunc, ctx: Integer; k: lua_CFunction): Integer;
+    cdecl; external LuaLibName;
+function  lua_pcall(L : Plua_State; nargs, nresults, errfunc : LongInt) : LongInt;
+{$ELSE}
 function  lua_pcall(L : Plua_State; nargs, nresults, errfunc : LongInt) : LongInt;
     cdecl; external LuaLibName;
+{$ENDIF}
 
 function  lua_cpcall(L : Plua_State; func : lua_CFunction; ud : Pointer) : LongInt;
     cdecl; external LuaLibName;
@@ -460,8 +486,6 @@
 
 procedure lua_pushcfunction(L : Plua_State; f : lua_CFunction);
 
-function  lua_strlen(L : Plua_State; idx : LongInt) : LongInt;
-
 function lua_isfunction(L : Plua_State; n : LongInt) : Boolean;
 function lua_istable(L : Plua_State; n : LongInt) : Boolean;
 function lua_islightuserdata(L : Plua_State; n : LongInt) : Boolean;
@@ -474,7 +498,13 @@
 procedure lua_pushliteral(L : Plua_State; s : PChar);
 
 procedure lua_setglobal(L : Plua_State; s : PChar);
+{$IFNDEF LUA51}
+    cdecl;  external LuaLibName;
+{$ENDIF}
 procedure lua_getglobal(L : Plua_State; s : PChar);
+{$IFNDEF LUA51}
+    cdecl;  external LuaLibName;
+{$ENDIF}
 
 function lua_tostring(L : Plua_State; idx : LongInt) : shortstring;
 function lua_tostringA(L : Plua_State; idx : LongInt) : ansistring;
@@ -483,6 +513,7 @@
 (*
 ** compatibility macros and functions
 *)
+(*
 function lua_open : Plua_State;
 
 procedure lua_getregistry(L : Plua_State);
@@ -492,6 +523,7 @@
 type
     lua_Chuckreader = lua_Reader;
     lua_Chuckwriter = lua_Writer;
+*)
 
 (* ====================================================================== *)
 
@@ -636,10 +668,6 @@
 ** See Copyright Notice at the end of this file.
 *)
 
-// not compatibility with the behavior of setn/getn in Lua 5.0
-function  luaL_getn(L : Plua_State; idx : LongInt) : LongInt;
-procedure luaL_setn(L : Plua_State; i, j : LongInt);
-
 const
     LUA_ERRFILE = LUA_ERRERR + 1;
 
@@ -651,8 +679,8 @@
     PluaL_Reg = ^luaL_Reg;
 
 
-procedure luaL_openlib(L : Plua_State; const libname : PChar; const lr : PluaL_Reg; nup : LongInt);
-    cdecl; external LuaLibName;
+(*procedure luaL_openlib(L : Plua_State; const libname : PChar; const lr : PluaL_Reg; nup : LongInt);
+    cdecl; external LuaLibName;*)
 procedure luaL_register(L : Plua_State; const libname : PChar; const lr : PluaL_Reg);
     cdecl; external LuaLibName;
 function luaL_getmetafield(L : Plua_State; obj : LongInt; const e : PChar) : LongInt;
@@ -736,10 +764,6 @@
 
 function luaL_typename(L : Plua_State; idx : LongInt) : PChar;
 
-function luaL_dofile(L : Plua_State; fn : PChar) : LongInt;
-
-function luaL_dostring(L : Plua_State; s : PChar) : LongInt;
-
 procedure luaL_getmetatable(L : Plua_State; n : PChar);
 
 (* not implemented yet
@@ -761,11 +785,7 @@
     end;
     PluaL_Buffer = ^luaL_Buffer;
 
-procedure luaL_addchar(B : PluaL_Buffer; c : Char);
-
 (* compatibility only *)
-procedure luaL_putchar(B : PluaL_Buffer; c : Char);
-
 procedure luaL_addsize(B : PluaL_Buffer; n : LongInt);
 
 procedure luaL_buffinit(L : Plua_State; B : PluaL_Buffer);
@@ -823,6 +843,28 @@
     lua_readline := (b[0] <> #4);          // test for ctrl-D
 end;
 }
+
+{$IFNDEF LUA51}
+
+(* compatibility with 5.2 *)
+
+function lua_tointeger(L : Plua_State; idx : LongInt) : lua_Integer;
+begin
+    lua_tointeger := lua_tointegerx(L, idx, nil);
+end;
+
+function lua_tonumber(L : Plua_State; idx : LongInt) : lua_Number;
+begin
+    lua_tonumber := lua_tonumberx(L, idx, nil);
+end;
+
+function lua_pcall(L : Plua_State; nargs, nresults, errfunc : LongInt) : LongInt;
+begin
+    lua_pcall := lua_pcallk(L, nargs, nresults, errfunc, 0, nil);
+end;
+
+{$ENDIF}
+
 procedure lua_saveline(L : Plua_State; idx : LongInt);
 begin
 end;
@@ -862,11 +904,6 @@
     lua_pushcclosure(L, f, 0);
 end;
 
-function  lua_strlen(L : Plua_State; idx : LongInt) : LongInt;
-begin
-    lua_strlen := lua_objlen(L, idx);
-end;
-
 function lua_isfunction(L : Plua_State; n : LongInt) : Boolean;
 begin
     lua_isfunction := lua_type(L, n) = LUA_TFUNCTION;
@@ -912,6 +949,8 @@
     lua_pushlstring(L, s, StrLen(s));
 end;
 
+
+{$IFDEF LUA51}
 procedure lua_setglobal(L : Plua_State; s : PChar);
 begin
     lua_setfield(L, LUA_GLOBALSINDEX, s);
@@ -921,6 +960,7 @@
 begin
     lua_getfield(L, LUA_GLOBALSINDEX, s);
 end;
+{$ENDIF}
 
 function lua_tostring(L : Plua_State; idx : LongInt) : shortstring;
 begin
@@ -933,7 +973,7 @@
     p:= lua_tolstring(L, idx, nil);
     lua_tostringA := ansistring(p);
 end;
-
+(*
 function lua_open : Plua_State;
 begin
     lua_open := luaL_newstate;
@@ -948,7 +988,7 @@
 begin
     lua_getgccount := lua_gc(L, LUA_GCCOUNT, 0);
 end;
-
+*)
 
 (*****************************************************************************)
 (*                                  lualib.h                                 *)
@@ -963,11 +1003,6 @@
 (*                                  lauxlib.h    n                           *)
 (*****************************************************************************)
 
-function luaL_getn(L : Plua_State; idx : LongInt) : LongInt;
-begin
-    luaL_getn := lua_objlen(L, idx);
-end;
-
 procedure luaL_setn(L : plua_State; i, j : LongInt);
 begin
     (* no op *)
@@ -1016,38 +1051,11 @@
     luaL_typename := lua_typename( L, lua_type(L, idx) );
 end;
 
-function luaL_dofile(L : Plua_State; fn : PChar) : LongInt;
-begin
-    luaL_dofile := luaL_loadfile(L, fn);
-    if luaL_dofile = 0 then
-        luaL_dofile := lua_pcall(L, 0, 0, 0);
-end;
-
-function luaL_dostring(L : Plua_State; s : PChar) : LongInt;
-begin
-    luaL_dostring := luaL_loadstring(L, s);
-    if luaL_dostring = 0 then
-        luaL_dostring := lua_pcall(L, 0, 0, 0);
-end;
-
 procedure luaL_getmetatable(L : Plua_State; n : PChar);
 begin
     lua_getfield(L, LUA_REGISTRYINDEX, n);
 end;
 
-procedure luaL_addchar(B : PluaL_Buffer; c : Char);
-begin
-    if not(B^.p < B^.buffer + LUAL_BUFFERSIZE) then
-        luaL_prepbuffer(B);
-    (B^.p^) := c;
-    Inc(B^.p);
-end;
-
-procedure luaL_putchar(B : PluaL_Buffer; c : Char);
-begin
-    luaL_addchar(B, c);
-end;
-
 procedure luaL_addsize(B : PluaL_Buffer; n : LongInt);
 begin
   Inc(B^.p, n);
--- a/hedgewars/uScript.pas	Fri Apr 25 23:17:49 2014 +0400
+++ b/hedgewars/uScript.pas	Mon Apr 28 16:52:00 2014 +0200
@@ -2498,14 +2498,11 @@
     cg : TCapGroup;
 begin
 // initialize lua
-luaState:= lua_open;
-TryDo(luaState <> nil, 'lua_open failed', true);
+luaState:= luaL_newstate();
+TryDo(luaState <> nil, 'luaL_newstate() failed', true);
 
 // open internal libraries
-luaopen_base(luaState);
-luaopen_string(luaState);
-luaopen_math(luaState);
-luaopen_table(luaState);
+luaL_openlibs(luaState);
 
 // import some variables
 ScriptSetString(_S'L', cLocale);