hedgewars/LuaPas.pas
changeset 10238 7e20804db6a2
parent 10230 361d36ec5181
child 10293 201ea4989985
--- a/hedgewars/LuaPas.pas	Wed May 07 00:04:02 2014 +0400
+++ b/hedgewars/LuaPas.pas	Wed May 07 19:26:05 2014 +0200
@@ -8,10 +8,6 @@
  *
  * 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
@@ -242,27 +238,6 @@
 (*
 ** 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;
 
@@ -290,6 +265,12 @@
 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;
 
@@ -405,15 +386,8 @@
 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;
@@ -486,6 +460,8 @@
 
 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;
@@ -498,13 +474,7 @@
 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;
@@ -513,7 +483,6 @@
 (*
 ** compatibility macros and functions
 *)
-(*
 function lua_open : Plua_State;
 
 procedure lua_getregistry(L : Plua_State);
@@ -523,7 +492,6 @@
 type
     lua_Chuckreader = lua_Reader;
     lua_Chuckwriter = lua_Writer;
-*)
 
 (* ====================================================================== *)
 
@@ -668,6 +636,10 @@
 ** 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;
 
@@ -679,8 +651,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;
@@ -764,6 +736,10 @@
 
 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
@@ -785,7 +761,11 @@
     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);
@@ -843,28 +823,6 @@
     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;
@@ -904,6 +862,11 @@
     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;
@@ -949,8 +912,6 @@
     lua_pushlstring(L, s, StrLen(s));
 end;
 
-
-{$IFDEF LUA51}
 procedure lua_setglobal(L : Plua_State; s : PChar);
 begin
     lua_setfield(L, LUA_GLOBALSINDEX, s);
@@ -960,7 +921,6 @@
 begin
     lua_getfield(L, LUA_GLOBALSINDEX, s);
 end;
-{$ENDIF}
 
 function lua_tostring(L : Plua_State; idx : LongInt) : shortstring;
 begin
@@ -973,7 +933,7 @@
     p:= lua_tolstring(L, idx, nil);
     lua_tostringA := ansistring(p);
 end;
-(*
+
 function lua_open : Plua_State;
 begin
     lua_open := luaL_newstate;
@@ -988,7 +948,7 @@
 begin
     lua_getgccount := lua_gc(L, LUA_GCCOUNT, 0);
 end;
-*)
+
 
 (*****************************************************************************)
 (*                                  lualib.h                                 *)
@@ -1003,6 +963,11 @@
 (*                                  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 *)
@@ -1051,11 +1016,38 @@
     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);