Blob Blame History Raw
diff -up hedgewars-src-0.9.18/cmake_modules/FindLua.cmake.lua-52 hedgewars-src-0.9.18/cmake_modules/FindLua.cmake
--- hedgewars-src-0.9.18/cmake_modules/FindLua.cmake.lua-52	2012-11-02 13:41:54.000000000 -0400
+++ hedgewars-src-0.9.18/cmake_modules/FindLua.cmake	2013-05-19 17:12:39.488053196 -0400
@@ -17,12 +17,12 @@ else (ANDROID)
             SET(LUA_DEFAULT lua)
         ELSE(APPLE)
             #locate the system's lua library
-            FIND_LIBRARY(LUA_DEFAULT NAMES lua51 lua5.1 lua-5.1 lua PATHS /lib /usr/lib /usr/local/lib /usr/pkg/lib)
+            FIND_LIBRARY(LUA_DEFAULT NAMES lua52 lua5.2 lua-5.2 lua51 lua5.1 lua-5.1 lua PATHS /lib /usr/lib /usr/local/lib /usr/pkg/lib)
             IF(${LUA_DEFAULT} MATCHES "LUA_DEFAULT-NOTFOUND")
 	        set(LUA_DEFAULT lua)
             ELSE()
                 set(LUA_FOUND true)
-                message(STATUS "LibLua 5.1 found at ${LUA_DEFAULT}")
+                message(STATUS "LibLua found at ${LUA_DEFAULT}")
                 #remove the path (fpc doesn't like it - why?)
                 GET_FILENAME_COMPONENT(LUA_DEFAULT ${LUA_DEFAULT} NAME)
             ENDIF()
diff -up hedgewars-src-0.9.18/hedgewars/LuaPas.pas.lua-52 hedgewars-src-0.9.18/hedgewars/LuaPas.pas
--- hedgewars-src-0.9.18/hedgewars/LuaPas.pas.lua-52	2012-11-02 13:41:54.000000000 -0400
+++ hedgewars-src-0.9.18/hedgewars/LuaPas.pas	2013-05-19 17:12:39.489053196 -0400
@@ -3,1066 +3,911 @@
 
 unit LuaPas;
 
-(*
- * A complete Pascal wrapper for Lua 5.1 DLL module.
- *
- * Created by Geo Massar, 2006
- * Distributed as free/open source.
- *)
-
-interface
-uses uConsts;
-{.$DEFINE LUA_GETHOOK}
-
-type
-{$IFNDEF PAS2C}    
-    size_t   = Cardinal;
-{$ENDIF}
-    Psize_t  = ^size_t;
-    PPointer = ^Pointer;
-
-    lua_State = record end;
-    Plua_State = ^lua_State;
-
-{$DEFINE LuaLibName:= cLuaLibrary}
-
-
-(*****************************************************************************)
-(*                               luaconfig.h                                 *)
-(*****************************************************************************)
-
-(*
-** $Id: luaconf.h,v 1.81 2006/02/10 17:44:06 roberto Exp $
-** Configuration file for Lua
-** See Copyright Notice in lua.h
-*)
-
-(*
-** {==================================================================
-@@ LUA_NUMBER is the type of numbers in Lua.
-** CHANGE the following definitions only if you want to build Lua
-** with a number type different from double. You may also need to
-** change lua_number2int & lua_number2integer.
-** ===================================================================
-*)
-type
-    LUA_NUMBER_  = Double;            // ending underscore is needed in Pascal
-    LUA_INTEGER_ = PtrInt;
-
-(*
-@@ LUA_IDSIZE gives the maximum size for the description of the source
-@* of a function in debug information.
-** CHANGE it if you want a different size.
-*)
-const
-    LUA_IDSIZE = 60;
-
-(*
-@@ LUAL_BUFFERSIZE is the buffer size used by the lauxlib buffer system.
-*)
-const
-    LUAL_BUFFERSIZE = 1024;
-
-(*
-@@ LUA_PROMPT is the default prompt used by stand-alone Lua.
-@@ LUA_PROMPT2 is the default continuation prompt used by stand-alone Lua.
-** CHANGE them if you want different prompts. (You can also change the
-** prompts dynamically, assigning to globals _PROMPT/_PROMPT2.)
-*)
-const
-    LUA_PROMPT  = '> ';
-    LUA_PROMPT2 = '>> ';
-
-(*
-@@ lua_readline defines how to show a prompt and then read a line from
-@* the standard input.
-@@ lua_saveline defines how to "save" a read line in a "history".
-@@ lua_freeline defines how to free a line read by lua_readline.
-** CHANGE them if you want to improve this functionality (e.g., by using
-** GNU readline and history facilities).
-*)
-{function  lua_readline(L : Plua_State;
-var b : PChar; p : PChar): Boolean;}
-
-procedure lua_saveline(L : Plua_State; idx : LongInt);
-
-procedure lua_freeline(L : Plua_State; b : PChar);
-
-(*
-@@ lua_stdin_is_tty detects whether the standard input is a 'tty' (that
-@* is, whether we're running lua interactively).
-** CHANGE it if you have a better definition for non-POSIX/non-Windows
-** systems.
-*/
-#include <io.h>
-#include <stdio.h>
-#define lua_stdin_is_tty()  _isatty(_fileno(stdin))
-*)
-const
-    lua_stdin_is_tty = TRUE;
-
-(*****************************************************************************)
-(*                                  lua.h                                    *)
-(*****************************************************************************)
-
-(*
-** $Id: lua.h,v 1.216 2006/01/10 12:50:13 roberto Exp $
-** Lua - An Extensible Extension Language
+(******************************************************************************
+ *                                                                            *
+ *  File:        lua52.pas                                                    *
+ *                                                                            *
+ *  Authors:     TeCGraf           (C headers + actual Lua libraries)         *
+ *               Lavergne Thomas   (original translation to Pascal)           *
+ *               Bram Kuijvenhoven (update to Lua 5.1.1 for FreePascal)       *
+ *               Egor Skriptunoff  (update to Lua 5.2.1 for FreePascal)       *
+ *               Vladimir Klimov   (Delphi compatibility)                     *
+ *                                                                            *
+ *  Description: Basic Lua library                                            *
+ *               Lua auxiliary library                                        *
+ *               Standard Lua libraries                                       *
+ *  This is 3-in-1 replacement for FPC modules lua.pas,lauxlib.pas,lualib.pas *
+ *                                                                            *
+ ******************************************************************************)
+
+(*
+** $Id: lua.h,v 1.283 2012/04/20 13:18:26 roberto Exp $
+** $Id: lauxlib.h,v 1.120 2011/11/29 15:55:08 roberto Exp $
+** $Id: lualib.h,v 1.43 2011/12/08 12:11:37 roberto Exp $
+** Lua - A Scripting Language
 ** Lua.org, PUC-Rio, Brazil (http://www.lua.org)
 ** See Copyright Notice at the end of this file
 *)
-
-const
-    LUA_VERSION     = 'Lua 5.1';
-    LUA_VERSION_NUM = 501;
-    LUA_COPYRIGHT   = 'Copyright (C) 1994-2006 Tecgraf, PUC-Rio';
-    LUA_AUTHORS     = 'R. Ierusalimschy, L. H. de Figueiredo & W. Celes';
-
-    (* mark for precompiled code (`<esc>Lua') *)
-    //LUA_SIGNATURE = #27'Lua';
-
-    (* option for multiple returns in `lua_pcall' and `lua_call' *)
-    LUA_MULTRET = -1;
-
-    (*
-    ** pseudo-indices
-    *)
-    LUA_REGISTRYINDEX = -10000;
-    LUA_ENVIRONINDEX  = -10001;
-    LUA_GLOBALSINDEX  = -10002;
-
-function lua_upvalueindex(idx : LongInt) : LongInt;   // a marco
-
-const
-   (* thread status; 0 is OK *)
-    LUA_YIELD_    = 1;     // Note: the ending underscore is needed in Pascal
-    LUA_ERRRUN    = 2;
-    LUA_ERRSYNTAX = 3;
-    LUA_ERRMEM    = 4;
-    LUA_ERRERR    = 5;
-
-type
-   lua_CFunction = function(L : Plua_State) : LongInt; cdecl;
-
-    (*
-    ** functions that read/write blocks when loading/dumping Lua chunks
-    *)
-    lua_Reader = function (L : Plua_State; ud : Pointer; sz : Psize_t) : PChar; cdecl;
-    lua_Writer = function (L : Plua_State; const p : Pointer; sz : size_t; ud : Pointer) : LongInt; cdecl;
-
-  (*
-  ** prototype for memory-allocation functions
-  *)
-  lua_Alloc = function (ud, ptr : Pointer; osize, nsize : size_t) : Pointer; cdecl;
-
-const
-    (*
-    ** basic types
-    *)
-    LUA_TNONE          = -1;
-    LUA_TNIL           = 0;
-    LUA_TBOOLEAN       = 1;
-    LUA_TLIGHTUSERDATA = 2;
-    LUA_TNUMBER        = 3;
-    LUA_TSTRING        = 4;
-    LUA_TTABLE         = 5;
-    LUA_TFUNCTION      = 6;
-    LUA_TUSERDATA      = 7;
-    LUA_TTHREAD        = 8;
-
-    (* minimum Lua stack available to a C function *)
-    LUA_MINSTACK = 20;
-
-type
-    (* type of numbers in Lua *)
-    lua_Number = LUA_NUMBER_;
-
-    (* type for integer functions *)
-    lua_Integer = LUA_INTEGER_;
-
-(*
-** state manipulation
-*)
-function  lua_newstate(f : lua_Alloc; ud : Pointer) : Plua_State;
-    cdecl; external LuaLibName;
-    
-procedure lua_close(L: Plua_State);
-    cdecl; external LuaLibName;
-function  lua_newthread(L : Plua_State) : Plua_State;
-    cdecl; external LuaLibName;
-
-function  lua_atpanic(L : Plua_State; panicf : lua_CFunction) : lua_CFunction;
-    cdecl; external LuaLibName;
-
-
-(*
-** basic stack manipulation
-*)
-function  lua_gettop(L : Plua_State) : LongInt;
-    cdecl; external LuaLibName;
-    
-procedure lua_settop(L : Plua_State; idx : LongInt);
-    cdecl; external LuaLibName;
-    
-procedure lua_pushvalue(L : Plua_State; idx : LongInt);
-    cdecl; external LuaLibName;
-    
-procedure lua_remove(L : Plua_State; idx : LongInt);
-    cdecl; external LuaLibName;
-    
-procedure lua_insert(L : Plua_State; idx : LongInt);
-    cdecl; external LuaLibName;
-    
-procedure lua_replace(L : Plua_State; idx : LongInt);
-    cdecl; external LuaLibName;
-    
-function  lua_checkstack(L : Plua_State; sz : LongInt) : LongBool;
-    cdecl; external LuaLibName;
-
-procedure lua_xmove(src, dest : Plua_State; n : LongInt);
-    cdecl; external LuaLibName;
-
-
-(*
-** access functions (stack -> C)
-*)
-function lua_isnumber(L : Plua_State; idx : LongInt) : LongBool;
-    cdecl; external LuaLibName;
-    
-function lua_isstring(L : Plua_State; idx : LongInt) : LongBool;
-    cdecl; external LuaLibName;
-    
-function lua_iscfunction(L : Plua_State; idx : LongInt) : LongBool;
-    cdecl; external LuaLibName;
-    
-function lua_isuserdata(L : Plua_State; idx : LongInt) : LongBool;
-    cdecl; external LuaLibName;
-    
-function lua_type(L : Plua_State; idx : LongInt) : LongInt;
-    cdecl; external LuaLibName;
-    
-function lua_typename(L : Plua_State; tp : LongInt) : PChar;
-    cdecl; external LuaLibName;
-
-    
-function lua_equal(L : Plua_State; idx1, idx2 : LongInt) : LongBool;
-    cdecl; external LuaLibName;
-    
-function lua_rawequal(L : Plua_State; idx1, idx2 : LongInt) : LongBool;
-    cdecl; external LuaLibName;
-    
-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;
-    
-    
-function lua_tolstring(L : Plua_State; idx : LongInt; len : Psize_t) : PChar;
-    cdecl; external LuaLibName;
-    
-function lua_objlen(L : Plua_State; idx : LongInt) : size_t;
-    cdecl; external LuaLibName;
-    
-function lua_tocfunction(L : Plua_State; idx : LongInt) : lua_CFunction;
-    cdecl; external LuaLibName;
-    
-function lua_touserdata(L : Plua_State; idx : LongInt) : Pointer;
-    cdecl; external LuaLibName;
-    
-function lua_tothread(L : Plua_State; idx : LongInt) : Plua_State;
-    cdecl; external LuaLibName;
-    
-function lua_topointer(L : Plua_State; idx : LongInt) : Pointer;
-    cdecl; external LuaLibName;
-
-
-(*
-** push functions (C -> stack)
-*)
-procedure lua_pushnil(L : Plua_State);
-    cdecl; external LuaLibName;
-    
-procedure lua_pushnumber(L : Plua_State; n : lua_Number);
-    cdecl; external LuaLibName;
-    
-procedure lua_pushinteger(L : Plua_State; n : lua_Integer);
-    cdecl; external LuaLibName;
-    
-procedure lua_pushlstring(L : Plua_State; const s : PChar; ls : size_t);
-    cdecl; external LuaLibName;
-    
-procedure lua_pushstring(L : Plua_State; const s : PChar);
-    cdecl; external LuaLibName;
-    
-    
-function  lua_pushvfstring(L : Plua_State;
-    const fmt : PChar; argp : Pointer) : PChar;
-    cdecl; external LuaLibName;
-    
-function  lua_pushfstring(L : Plua_State; const fmt : PChar) : PChar; varargs;
-    cdecl; external LuaLibName;
-    
-procedure lua_pushcclosure(L : Plua_State; fn : lua_CFunction; n : LongInt);
-    cdecl; external LuaLibName;
-    
-procedure lua_pushboolean(L : Plua_State; b : LongBool);
-    cdecl; external LuaLibName;
-    
-procedure lua_pushlightuserdata(L : Plua_State; p : Pointer);
-    cdecl; external LuaLibName;
-    
-function  lua_pushthread(L : Plua_state) : Cardinal;
-    cdecl; external LuaLibName;
-
-
-(*
-** get functions (Lua -> stack)
-*)
-procedure lua_gettable(L : Plua_State ; idx : LongInt);
-    cdecl; external LuaLibName;
-    
-procedure lua_getfield(L : Plua_State; idx : LongInt; k : PChar);
-    cdecl; external LuaLibName;
-    
-procedure lua_rawget(L : Plua_State; idx : LongInt);
-    cdecl; external LuaLibName;
-    
-procedure lua_rawgeti(L : Plua_State; idx, n : LongInt);
-    cdecl; external LuaLibName;
-    
-procedure lua_createtable(L : Plua_State; narr, nrec : LongInt);
-    cdecl; external LuaLibName;
-    
-function  lua_newuserdata(L : Plua_State; sz : size_t) : Pointer;
-    cdecl; external LuaLibName;
-    
-function  lua_getmetatable(L : Plua_State; objindex : LongInt) : LongBool;
-    cdecl; external LuaLibName;
-    
-procedure lua_getfenv(L : Plua_State; idx : LongInt);
-    cdecl; external LuaLibName;
-
-
 (*
-** set functions (stack -> Lua)
+** Translated to pascal by Lavergne Thomas
+** Notes :
+**    - Pointers type was prefixed with 'P'
+**    - lua_upvalueindex constant was transformed to function
+**    - Some compatibility function was isolated because with it you must have
+**      lualib.
+**    - LUA_VERSION was suffixed by '_' for avoiding name collision.
+** Bug reports :
+**    - thomas.lavergne@laposte.net
+**   In french or in english
+*)
+(*
+** Updated to Lua 5.1.1 by Bram Kuijvenhoven (bram at kuijvenhoven dot net),
+**   Hexis BV (http://www.hexis.nl), the Netherlands
+** Notes:
+**    - Only tested with FPC (FreePascal Compiler)
+**    - Using LuaBinaries styled DLL/SO names, which include version names
+**    - LUA_YIELD was suffixed by '_' for avoiding name collision
+*)
+(*
+** Updated to Lua 5.2.1 by Egor Skriptunoff
+** Notes:
+**    - Only tested with FPC (FreePascal Compiler)
+**    - Functions dealing with luaL_Reg were overloaded to accept pointer
+**      or open array parameter.  In any case, do not forget to terminate
+**      your array with "sentinel".
+**    - All floating-point exceptions were forcibly disabled in Windows
+**      to overcome well-known bug
+** Bug reports:
+**    - egor.skriptunoff at gmail.com
+**   In russian or in english
+*)
+(*
+** Delphi compatibility by Vladimir Klimov
+** Notes:
+**    - fixed luaL_error syntax
+**    - PChar replaced with PAnsiChar, String with AnsiString due to since
+**		D2009 both PChar and String are unicode
+** Bug reports:
+**    - wintarif@narod.ru
+**   russian or english
 *)
-procedure lua_settable(L : Plua_State; idx : LongInt);
-    cdecl; external LuaLibName;
-    
-procedure lua_setfield(L : Plua_State; idx : LongInt; const k : PChar);
-    cdecl; external LuaLibName;
-    
-procedure lua_rawset(L : Plua_State; idx : LongInt);
-    cdecl; external LuaLibName;
-    
-procedure lua_rawseti(L : Plua_State; idx , n: LongInt);
-    cdecl; external LuaLibName;
-    
-function lua_setmetatable(L : Plua_State; objindex : LongInt): LongBool;
-    cdecl; external LuaLibName;
-    
-function lua_setfenv(L : Plua_State; idx : LongInt): LongBool;
-    cdecl; external LuaLibName;
 
-(*
-** `load' and `call' functions (load and run Lua code)
-*)
-procedure lua_call(L : Plua_State; nargs, nresults : LongInt);
-    cdecl; external LuaLibName;
-    
-function  lua_pcall(L : Plua_State; nargs, nresults, errfunc : LongInt) : LongInt;
-    cdecl; external LuaLibName;
-    
-function  lua_cpcall(L : Plua_State; func : lua_CFunction; ud : Pointer) : LongInt;
-    cdecl; external LuaLibName;
-    
-function  lua_load(L : Plua_State; reader : lua_Reader; dt : Pointer; const chunkname : PChar) : LongInt;
-    cdecl; external LuaLibName;
-    
 
-function lua_dump(L : Plua_State; writer : lua_Writer; data: Pointer) : LongInt;
-    cdecl; external LuaLibName;
+//--------------------------
+// What was not translated:
+//--------------------------
+// macro
+//    #define luaL_opt(L,f,n,d)  (lua_isnoneornil(L,(n)) ? (d) : f(L,(n)))
+
+// Generic Buffer manipulation functions and macros were not translated.
+// They are not required in Pascal programs due to powerful String type.
+//    luaL_addchar, luaL_addsize, luaL_buffinit, luaL_prepbuffsize,
+//    luaL_addlstring, luaL_addstring, luaL_addvalue, luaL_pushresult,
+//    luaL_pushresultsize, luaL_buffinitsize, luaL_prepbuffer
+
+// Functions defined with LUA_COMPAT_MODULE are deprecated.
+// They were translated but commented intentionally.
+// Uncomment them if you really need.
+//    luaL_pushmodule, luaL_openlib, luaL_register
 
 
-(*
-** coroutine functions
-*)
-function lua_yield(L : Plua_State; nresults : LongInt) : LongInt;
-    cdecl; external LuaLibName;
-    
-function lua_resume(L : Plua_State; narg : LongInt) : LongInt;
-    cdecl; external LuaLibName;
-    
-function lua_status(L : Plua_State) : LongInt;
-    cdecl; external LuaLibName;
+interface
 
-(*
-** garbage-collection functions and options
-*)
 const
-    LUA_GCSTOP       = 0;
-    LUA_GCRESTART    = 1;
-    LUA_GCCOLLECT    = 2;
-    LUA_GCCOUNT      = 3;
-    LUA_GCCOUNTB     = 4;
-    LUA_GCSTEP       = 5;
-    LUA_GCSETPAUSE   = 6;
-    LUA_GCSETSTEPMUL = 7;
-
-function lua_gc(L : Plua_State; what, data : LongInt) : LongInt;
-    cdecl; external LuaLibName;
-
-(*
-** miscellaneous functions
-*)
-function lua_error(L : Plua_State) : LongInt;
-    cdecl; external LuaLibName;
-
-function lua_next(L : Plua_State; idx : LongInt) : LongInt;
-    cdecl; external LuaLibName;
-
-procedure lua_concat(L : Plua_State; n : LongInt);
-    cdecl; external LuaLibName;
-
-function  lua_getallocf(L : Plua_State; ud : PPointer) : lua_Alloc;
-    cdecl; external LuaLibName;
-    
-procedure lua_setallocf(L : Plua_State; f : lua_Alloc; ud : Pointer);
-    cdecl; external LuaLibName;
-
-(*
-** ===============================================================
-** some useful macros
-** ===============================================================
-*)
-procedure lua_pop(L : Plua_State; n : LongInt);
-
-procedure lua_newtable(L : Plua_State);
-
-procedure lua_register(L : Plua_State; n : PChar; f : lua_CFunction);
-
-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;
-function lua_isnil(L : Plua_State; n : LongInt) : Boolean;
-function lua_isboolean(L : Plua_State; n : LongInt) : Boolean;
-function lua_isthread(L : Plua_State; n : LongInt) : Boolean;
-function lua_isnone(L : Plua_State; n : LongInt) : Boolean;
-function lua_isnoneornil(L : Plua_State; n : LongInt) : Boolean;
-
-procedure lua_pushliteral(L : Plua_State; s : PChar);
-
-procedure lua_setglobal(L : Plua_State; s : PChar);
-procedure lua_getglobal(L : Plua_State; s : PChar);
-
-function lua_tostring(L : Plua_State; idx : LongInt) : AnsiString;
-
-
-(*
-** compatibility macros and functions
-*)
-function lua_open : Plua_State;
-
-procedure lua_getregistry(L : Plua_State);
-
-function lua_getgccount(L : Plua_State) : LongInt;
-
-type
-    lua_Chuckreader = lua_Reader;
-    lua_Chuckwriter = lua_Writer;
-
-(* ====================================================================== *)
-
-(*
-** {======================================================================
-** Debug API
-** =======================================================================
-*)
+{$IFDEF MSWINDOWS}
+   LUA_LIB_NAME = 'lua52.dll';
+{$ELSE}
+   LUA_LIB_NAME = 'liblua-5.2.so';
+{$ENDIF}
 
-(*
-** Event codes
-*)
 const
-    LUA_HOOKCALL    = 0;
-    LUA_HOOKRET     = 1;
-    LUA_HOOKLINE    = 2;
-    LUA_HOOKCOUNT   = 3;
-    LUA_HOOKTAILRET = 4;
-
-
-(*
-** Event masks
-*)
-    LUA_MASKCALL  = 1 shl LUA_HOOKCALL;
-    LUA_MASKRET   = 1 shl LUA_HOOKRET;
-    LUA_MASKLINE  = 1 shl LUA_HOOKLINE;
-    LUA_MASKCOUNT = 1 shl LUA_HOOKCOUNT;
+   LUA_VERSION_MAJOR   = '5';
+   LUA_VERSION_MINOR   = '2';
+   LUA_VERSION_NUM     = 502;
+   LUA_VERSION_RELEASE = '1';
+   LUA_VERSION_        = 'Lua 5.2'; // LUA_VERSION was suffixed by '_' for avoiding name collision
+   LUA_RELEASE         = 'Lua 5.2.1';
+   LUA_COPYRIGHT       = 'Lua 5.2.1  Copyright (C) 1994-2012 Lua.org, PUC-Rio';
+   LUA_AUTHORS         = 'R. Ierusalimschy, L. H. de Figueiredo, W. Celes';
+   LUA_SIGNATURE       = #27'Lua';  // mark for precompiled code '<esc>Lua'
+   LUA_MULTRET         = -1;        // option for multiple returns in 'lua_pcall' and 'lua_call'
+
+   // pseudo-indices
+   LUA_REGISTRYINDEX   = -1001000;
+
+function lua_upvalueindex(I: Integer): Integer;
+
+// thread status
+const
+   LUA_OK        = 0;
+   LUA_YIELD_    = 1;   // LUA_YIELD was suffixed by '_' for avoiding name collision
+   LUA_ERRRUN    = 2;
+   LUA_ERRSYNTAX = 3;
+   LUA_ERRMEM    = 4;
+   LUA_ERRGCMM   = 5;
+   LUA_ERRERR    = 6;
+   LUA_ERRFILE   = LUA_ERRERR + 1;   // extra error code for `luaL_load'
 
 type
-    lua_Debug = packed record
-    event : LongInt;
-    name : PChar;          (* (n) *)
-    namewhat : PChar;      (* (n) `global', `local', `field', `method' *)
-    what : PChar;          (* (S) `Lua', `C', `main', `tail' *)
-    source : PChar;        (* (S) *)
-    currentline : LongInt; (* (l) *)
-    nups : LongInt;        (* (u) number of upvalues *)
-    linedefined : LongInt; (* (S) *)
-    short_src : array [0..LUA_IDSIZE-1] of Char; (* (S) *)
-    (* private part *)
-    i_ci : LongInt;        (* active function *)
-    end;
-    Plua_Debug = ^lua_Debug;
-
-  (* Functions to be called by the debuger in specific events *)
-  lua_Hook = procedure (L : Plua_State; ar : Plua_Debug); cdecl;
-
-
-function lua_getstack(L : Plua_State; level : LongInt; ar : Plua_Debug) : LongInt;
-    cdecl; external LuaLibName;
-    
-function lua_getinfo(L : Plua_State; const what : PChar; ar: Plua_Debug): LongInt;
-    cdecl; external LuaLibName;
-    
-function lua_getlocal(L : Plua_State; ar : Plua_Debug; n : LongInt) : PChar;
-    cdecl; external LuaLibName;
-    
-function lua_setlocal(L : Plua_State; ar : Plua_Debug; n : LongInt) : PChar;
-    cdecl; external LuaLibName;
-    
-function lua_getupvalue(L : Plua_State; funcindex, n : LongInt) : PChar;
-    cdecl; external LuaLibName;
-    
-function lua_setupvalue(L : Plua_State; funcindex, n : LongInt) : PChar;
-    cdecl; external LuaLibName;
-
-function lua_sethook(L : Plua_State; func : lua_Hook; mask, count: LongInt): LongInt;
-    cdecl; external LuaLibName;
-  
-{$IFDEF LUA_GETHOOK}
-function lua_gethook(L : Plua_State) : lua_Hook;
-    cdecl; external LuaLibName;
+   // Type of Numbers in Lua
+{$IFDEF FPC}
+   lua_Integer  = PtrInt;
+   lua_Unsigned = PtrUInt;
+{$ELSE} // Delphi
+  {$IF CompilerVersion < 20.0}
+   lua_Integer  = Integer;
+   lua_Unsigned = Cardinal;
+  {$ELSE}
+   lua_Integer  = NativeInt;
+   lua_Unsigned = NativeUInt;
+  {$IFEND}
 {$ENDIF}
+   lua_Number   = Double;
 
-function lua_gethookmask(L : Plua_State) : LongInt;
-    cdecl; external LuaLibName;
-    
-function lua_gethookcount(L : Plua_State) : LongInt;
-    cdecl; external LuaLibName;
-
-
-(*****************************************************************************)
-(*                                  lualib.h                                 *)
-(*****************************************************************************)
-
-(*
-** $Id: lualib.h,v 1.36 2005/12/27 17:12:00 roberto Exp $
-** Lua standard libraries
-** See Copyright Notice at the end of this file
-*)
-
-const
-    (* Key to file-handle type *)
-    LUA_FILEHANDLE  = 'FILE*';
-
-    LUA_COLIBNAME   = 'coroutine';
-    LUA_TABLIBNAME  = 'table';
-    LUA_IOLIBNAME   = 'io';
-    LUA_OSLIBNAME   = 'os';
-    LUA_STRLIBNAME  = 'string';
-    LUA_MATHLIBNAME = 'math';
-    LUA_DBLIBNAME   = 'debug';
-    LUA_LOADLIBNAME = 'package';
+   Plua_Number  = ^lua_Number;
 
-function luaopen_base(L : Plua_State) : LongInt;
-    cdecl; external LuaLibName;
+   size_t = Cardinal;
+   Psize_t = ^size_t;
 
-function luaopen_table(L : Plua_State) : LongInt;
-    cdecl; external LuaLibName;
-
-function luaopen_io(L : Plua_State) : LongInt;
-    cdecl; external LuaLibName;
-
-function luaopen_os(L : Plua_State) : LongInt;
-    cdecl; external LuaLibName;
-
-function luaopen_string(L : Plua_State) : LongInt;
-    cdecl; external LuaLibName;
-
-function luaopen_math(L : Plua_State) : LongInt;
-    cdecl; external LuaLibName;
-
-function luaopen_debug(L : Plua_State) : LongInt;
-    cdecl; external LuaLibName;
-
-function luaopen_package(L : Plua_State) : LongInt;
-    cdecl; external LuaLibName;
-
-procedure luaL_openlibs(L : Plua_State);
-    cdecl; external LuaLibName;
-
-procedure lua_assert(x : Boolean);    // a macro
-
-
-(*****************************************************************************)
-(*                                  lauxlib.h                                *)
-(*****************************************************************************)
-
-(*
-** $Id: lauxlib.h,v 1.87 2005/12/29 15:32:11 roberto Exp $
-** Auxiliary functions for building Lua libraries
-** See Copyright Notice at the end of this file.
-*)
+   Plua_State = Pointer;
 
-// 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);
+   lua_CFunction = function(L: Plua_State): Integer; cdecl;
+
+   // functions that read/write blocks when loading/dumping Lua chunks
+   lua_Reader = function(L: Plua_State; ud: Pointer; sz: Psize_t): PAnsiChar; cdecl;
+   lua_Writer = function(L: Plua_State; const p: Pointer; sz: size_t; ud: Pointer): Integer; cdecl;
+
+   // prototype for memory-allocation functions
+   lua_Alloc = function(ud, ptr: Pointer; osize, nsize: size_t): Pointer; cdecl;
+
+const
+   // basic types
+   LUA_TNONE          = -1;
+   LUA_TNIL           = 0;
+   LUA_TBOOLEAN       = 1;
+   LUA_TLIGHTUSERDATA = 2;
+   LUA_TNUMBER        = 3;
+   LUA_TSTRING        = 4;
+   LUA_TTABLE         = 5;
+   LUA_TFUNCTION      = 6;
+   LUA_TUSERDATA      = 7;
+   LUA_TTHREAD        = 8;
+   LUA_NUMTAGS        = 9;
+
+   // minimum Lua stack available to a C function
+   LUA_MINSTACK = 20;
+
+   // predefined values in the registry */
+   LUA_RIDX_MAINTHREAD = 1;
+   LUA_RIDX_GLOBALS    = 2;
+   LUA_RIDX_LAST       = LUA_RIDX_GLOBALS;
+
+// state manipulation
+function lua_newstate(f: lua_Alloc; ud: Pointer): Plua_state; cdecl;
+procedure lua_close(L: Plua_State); cdecl;
+function lua_newthread(L: Plua_State): Plua_State; cdecl;
+function lua_atpanic(L: Plua_State; panicf: lua_CFunction): lua_CFunction; cdecl;
+function lua_version(L: Plua_State): Plua_Number; cdecl;
+
+// basic stack manipulation
+function lua_absindex(L: Plua_State; idx: Integer): Integer; cdecl;
+function lua_gettop(L: Plua_State): Integer; cdecl;
+procedure lua_settop(L: Plua_State; idx: Integer); cdecl;
+procedure lua_pushvalue(L: Plua_State; Idx: Integer); cdecl;
+procedure lua_remove(L: Plua_State; idx: Integer); cdecl;
+procedure lua_insert(L: Plua_State; idx: Integer); cdecl;
+procedure lua_replace(L: Plua_State; idx: Integer); cdecl;
+procedure lua_copy(L: Plua_State; fromidx, toidx: Integer); cdecl;
+function lua_checkstack(L: Plua_State; sz: Integer): LongBool; cdecl;
+procedure lua_xmove(from, to_: Plua_State; n: Integer); cdecl;
+
+// access functions (stack -> C)
+function lua_isnumber(L: Plua_State; idx: Integer): LongBool; cdecl;
+function lua_isstring(L: Plua_State; idx: Integer): LongBool; cdecl;
+function lua_iscfunction(L: Plua_State; idx: Integer): LongBool; cdecl;
+function lua_isuserdata(L: Plua_State; idx: Integer): LongBool; cdecl;
+function lua_type(L: Plua_State; idx: Integer): Integer; cdecl;
+function lua_typename(L: Plua_State; tp: Integer): PAnsiChar; cdecl;
+function lua_tonumberx(L: Plua_State; idx: Integer; isnum: PInteger): lua_Number; cdecl;
+function lua_tointegerx(L: Plua_State; idx: Integer; isnum: PInteger): lua_Integer; cdecl;
+function lua_tounsignedx(L: Plua_State; idx: Integer; isnum: PInteger): lua_Unsigned; cdecl;
+function lua_toboolean(L: Plua_State; idx: Integer): LongBool; cdecl;
+function lua_tolstring(L: Plua_State; idx: Integer; len: Psize_t): PAnsiChar; cdecl;
+function lua_rawlen(L: Plua_State; idx: Integer): size_t; cdecl;
+function lua_tocfunction(L: Plua_State; idx: Integer): lua_CFunction; cdecl;
+function lua_touserdata(L: Plua_State; idx: Integer): Pointer; cdecl;
+function lua_tothread(L: Plua_State; idx: Integer): Plua_State; cdecl;
+function lua_topointer(L: Plua_State; idx: Integer): Pointer; cdecl;
+
+//  Arithmetic functions
+const
+   LUA_OPADD = 0; (* ORDER TM *)
+   LUA_OPSUB = 1;
+   LUA_OPMUL = 2;
+   LUA_OPDIV = 3;
+   LUA_OPMOD = 4;
+   LUA_OPPOW = 5;
+   LUA_OPUNM = 6;
+procedure lua_arith(L: Plua_State; op: Integer); cdecl;
+
+//  Comparison functions
+const
+   LUA_OPEQ = 0;
+   LUA_OPLT = 1;
+   LUA_OPLE = 2;
+function lua_rawequal(L: Plua_State; idx1, idx2: Integer): LongBool; cdecl;
+function lua_compare(L: Plua_State; idx1, idx2, op: Integer): LongBool; cdecl;
+
+// push functions (C -> stack)
+procedure lua_pushnil(L: Plua_State); cdecl;
+procedure lua_pushnumber(L: Plua_State; n: lua_Number); cdecl;
+procedure lua_pushinteger(L: Plua_State; n: lua_Integer); cdecl;
+procedure lua_pushunsigned(L: Plua_State; n: lua_Unsigned); cdecl;
+procedure lua_pushlstring(L: Plua_State; const s: PAnsiChar; l_: size_t); cdecl;
+procedure lua_pushstring(L: Plua_State; const s: PAnsiChar); cdecl; overload;
+procedure lua_pushstring(L: Plua_State; const s: AnsiString); overload; // added for Pascal
+function lua_pushvfstring(L: Plua_State; const fmt: PAnsiChar; argp: Pointer): PAnsiChar; cdecl;
+function lua_pushfstring(L: Plua_State; const fmt: PAnsiChar): PAnsiChar; cdecl; varargs;
+procedure lua_pushcclosure(L: Plua_State; fn: lua_CFunction; n: Integer); cdecl;
+procedure lua_pushboolean(L: Plua_State; b: LongBool); cdecl;
+procedure lua_pushlightuserdata(L: Plua_State; p: Pointer); cdecl;
+procedure lua_pushthread(L: Plua_State); cdecl;
+
+// get functions (Lua -> stack)
+procedure lua_getglobal(L: Plua_State; const var_: PAnsiChar); cdecl;
+procedure lua_gettable(L: Plua_State; idx: Integer); cdecl;
+procedure lua_getfield(L: Plua_state; idx: Integer; k: PAnsiChar); cdecl;
+procedure lua_rawget(L: Plua_State; idx: Integer); cdecl;
+procedure lua_rawgeti(L: Plua_State; idx, n: Integer); cdecl;
+procedure lua_rawgetp(L: Plua_State; idx: Integer; p: Pointer); cdecl;
+procedure lua_createtable(L: Plua_State; narr, nrec: Integer); cdecl;
+function lua_newuserdata(L: Plua_State; sz: size_t): Pointer; cdecl;
+function lua_getmetatable(L: Plua_State; objindex: Integer): Integer; cdecl;
+procedure lua_getuservalue(L: Plua_State; idx: Integer); cdecl;
+
+// set functions (stack -> Lua)
+procedure lua_setglobal(L: Plua_State; const var_: PAnsiChar); cdecl;
+procedure lua_settable(L: Plua_State; idx: Integer); cdecl;
+procedure lua_setfield(L: Plua_State; idx: Integer; k: PAnsiChar); cdecl;
+procedure lua_rawset(L: Plua_State; idx: Integer); cdecl;
+procedure lua_rawseti(L: Plua_State; idx, n: Integer); cdecl;
+procedure lua_rawsetp(L: Plua_State; idx: Integer; p: Pointer); cdecl;
+function lua_setmetatable(L: Plua_State; objindex: Integer): Integer; cdecl;
+procedure lua_setuservalue(L: Plua_State; idx: Integer); cdecl;
+
+// 'load' and 'call' functions (load and run Lua code)
+procedure lua_callk(L: Plua_State; nargs, nresults, ctx: Integer; k: lua_CFunction); cdecl;
+procedure lua_call(L: Plua_State; nargs, nresults: Integer);
+function lua_getctx(L: Plua_State; ctx: PInteger): Integer; cdecl;
+function lua_pcallk(L: Plua_State; nargs, nresults, errfunc, ctx: Integer; k: lua_CFunction): Integer; cdecl;
+function lua_pcall(L: Plua_State; nargs, nresults, errf: Integer): Integer;
+function lua_load(L: Plua_State; reader: lua_Reader; dt: Pointer; const chunkname, mode: PAnsiChar): Integer; cdecl;
+function lua_dump(L: Plua_State; writer: lua_Writer; data: Pointer): Integer; cdecl;
+
+// coroutine functions
+function lua_yieldk(L: Plua_State; nresults, ctx: Integer; k: lua_CFunction): Integer; cdecl;
+function lua_yield(L: Plua_State; nresults: Integer): Integer;
+function lua_resume(L, from: Plua_State; narg: Integer): Integer; cdecl;
+function lua_status(L: Plua_State): Integer; cdecl;
+
+//  garbage-collection function and options
+const
+   LUA_GCSTOP        = 0;
+   LUA_GCRESTART     = 1;
+   LUA_GCCOLLECT     = 2;
+   LUA_GCCOUNT       = 3;
+   LUA_GCCOUNTB      = 4;
+   LUA_GCSTEP        = 5;
+   LUA_GCSETPAUSE    = 6;
+   LUA_GCSETSTEPMUL  = 7;
+   LUA_GCSETMAJORINC = 8;
+   LUA_GCISRUNNING   = 9;
+   LUA_GCGEN         = 10;
+   LUA_GCINC         = 11;
+function lua_gc(L: Plua_State; what, data: Integer): Integer; cdecl;
+
+// miscellaneous functions
+function lua_error(L: Plua_State): Integer; cdecl;
+function lua_next(L: Plua_State; idx: Integer): Integer; cdecl;
+procedure lua_concat(L: Plua_State; n: Integer); cdecl;
+procedure lua_len(L: Plua_State; idx: Integer); cdecl;
+function lua_getallocf(L: Plua_State; ud: PPointer): lua_Alloc; cdecl;
+procedure lua_setallocf(L: Plua_State; f: lua_Alloc; ud: Pointer); cdecl;
+
+// some useful macros
+function lua_tonumber(L: Plua_State; idx: Integer): lua_Number;
+function lua_tointeger(L: Plua_State; idx: Integer): lua_Integer;
+function lua_tounsigned(L: Plua_State; idx: Integer): lua_Unsigned;
+procedure lua_pop(L: Plua_State; n: Integer);
+procedure lua_newtable(L: Plua_state);
+procedure lua_register(L: Plua_State; const n: PAnsiChar; f: lua_CFunction);
+procedure lua_pushcfunction(L: Plua_State; f: lua_CFunction);
+function lua_isfunction(L: Plua_State; n: Integer): Boolean;
+function lua_istable(L: Plua_State; n: Integer): Boolean;
+function lua_islightuserdata(L: Plua_State; n: Integer): Boolean;
+function lua_isnil(L: Plua_State; n: Integer): Boolean;
+function lua_isboolean(L: Plua_State; n: Integer): Boolean;
+function lua_isthread(L: Plua_State; n: Integer): Boolean;
+function lua_isnone(L: Plua_State; n: Integer): Boolean;
+function lua_isnoneornil(L: Plua_State; n: Integer): Boolean;
+procedure lua_pushliteral(L: Plua_State; s: PAnsiChar);
+procedure lua_pushglobaltable(L: Plua_State);
+function lua_tostring(L: Plua_State; i: Integer): PAnsiChar;
+
+// Debug API
+const
+   // Event codes
+   LUA_HOOKCALL     = 0;
+   LUA_HOOKRET      = 1;
+   LUA_HOOKLINE     = 2;
+   LUA_HOOKCOUNT    = 3;
+   LUA_HOOKTAILCALL = 4;
+
+   // Event masks
+   LUA_MASKCALL  = 1 shl Ord(LUA_HOOKCALL);
+   LUA_MASKRET   = 1 shl Ord(LUA_HOOKRET);
+   LUA_MASKLINE  = 1 shl Ord(LUA_HOOKLINE);
+   LUA_MASKCOUNT = 1 shl Ord(LUA_HOOKCOUNT);
 
-const
-    LUA_ERRFILE = LUA_ERRERR + 1;
+   LUA_IDSIZE = 60;
 
 type
-    luaL_Reg = packed record
-    name : PChar;
-    func : lua_CFunction;
-    end;
-    PluaL_Reg = ^luaL_Reg;
-
-
-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;
-    cdecl; external LuaLibName;
-function luaL_callmeta(L : Plua_State; obj : LongInt; const e : PChar) : LongInt;
-    cdecl; external LuaLibName;
-function luaL_typerror(L : Plua_State; narg : LongInt; const tname : PChar) : LongInt;
-    cdecl; external LuaLibName;
-function luaL_argerror(L : Plua_State; numarg : LongInt; const extramsg : PChar) : LongInt;
-    cdecl; external LuaLibName;
-function luaL_checklstring(L : Plua_State; numArg : LongInt; ls : Psize_t) : PChar;
-    cdecl; external LuaLibName;
-function luaL_optlstring(L : Plua_State; numArg : LongInt; const def: PChar; ls: Psize_t) : PChar;
-    cdecl; external LuaLibName;
-function luaL_checknumber(L : Plua_State; numArg : LongInt) : lua_Number;
-    cdecl; external LuaLibName;
-function luaL_optnumber(L : Plua_State; nArg : LongInt; def : lua_Number) : lua_Number;
-    cdecl; external LuaLibName;
-
-function luaL_checkinteger(L : Plua_State; numArg : LongInt) : lua_Integer;
-    cdecl; external LuaLibName;
-function luaL_optinteger(L : Plua_State; nArg : LongInt; def : lua_Integer) : lua_Integer;
-    cdecl; external LuaLibName;
-
-procedure luaL_checkstack(L : Plua_State; sz : LongInt; const msg : PChar);
-    cdecl; external LuaLibName;
-procedure luaL_checktype(L : Plua_State; narg, t : LongInt);
-    cdecl; external LuaLibName;
-procedure luaL_checkany(L : Plua_State; narg : LongInt);
-    cdecl; external LuaLibName;
-
-function luaL_newmetatable(L : Plua_State; const tname : PChar) : LongInt;
-    cdecl; external LuaLibName;
-function luaL_checkudata(L : Plua_State; ud : LongInt; const tname : PChar) : Pointer;
-    cdecl; external LuaLibName;
-
-procedure luaL_where(L : Plua_State; lvl : LongInt);
-    cdecl; external LuaLibName;
-function  luaL_error(L : Plua_State; const fmt : PChar) : LongInt; varargs;
-    cdecl; external LuaLibName;
-
-function luaL_checkoption(L : Plua_State; narg : LongInt; const def : PChar; const lst : array of PChar) : LongInt;
-    cdecl; external LuaLibName;
-
-function  luaL_ref(L : Plua_State; t : LongInt) : LongInt;
-    cdecl; external LuaLibName;
-procedure luaL_unref(L : Plua_State; t, ref : LongInt);
-    cdecl; external LuaLibName;
-
-function luaL_loadfile(L : Plua_State; const filename : PChar) : LongInt;
-    cdecl; external LuaLibName;
-function luaL_loadbuffer(L : Plua_State; const buff : PChar; sz : size_t; const name: PChar) : LongInt;
-    cdecl; external LuaLibName;
-
-function luaL_loadstring(L : Plua_State; const s : Pchar) : LongInt;
-    cdecl; external LuaLibName;
-
-function luaL_newstate : Plua_State;
-    cdecl; external LuaLibName;
-
-function luaL_gsub(L : Plua_State; const s, p, r : PChar) : PChar;
-    cdecl; external LuaLibName;
-
-function luaL_findtable(L : Plua_State; idx : LongInt; const fname : PChar; szhint : LongInt) : PChar;
-    cdecl; external LuaLibName;
-
-
-(*
-** ===============================================================
-** some useful macros
-** ===============================================================
-*)
-
-function luaL_argcheck(L : Plua_State; cond : Boolean; numarg : LongInt; extramsg : PChar): LongInt;
-function luaL_checkstring(L : Plua_State; n : LongInt) : PChar;
-function luaL_optstring(L : Plua_State; n : LongInt; d : PChar) : PChar;
-function luaL_checkint(L : Plua_State; n : LongInt) : lua_Integer;
-function luaL_optint(L : Plua_State; n : LongInt; d : lua_Integer): lua_Integer;
-function luaL_checklong(L : Plua_State; n : LongInt) : lua_Integer;
-function luaL_optlong(L : Plua_State; n : LongInt; d : lua_Integer) : lua_Integer;
-
-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
-#define luaL_opt(L,f,n,d) (lua_isnoneornil(L,(n)) ? (d) : f(L,(n)))
-*)
-
-(*
-** {======================================================
-** Generic Buffer manipulation
-** =======================================================
-*)
+   lua_Debug = record            (* activation record *)
+      event: Integer;
+      name: PAnsiChar;               (* (n) *)
+      namewhat: PAnsiChar;           (* (n) `global', `local', `field', `method' *)
+      what: PAnsiChar;               (* (S) `Lua', `C', `main', `tail'*)
+      source: PAnsiChar;             (* (S) *)
+      currentline: Integer;      (* (l) *)
+      linedefined: Integer;      (* (S) *)
+      lastlinedefined: Integer;  (* (S) *)
+      nups: Byte;                (* (u) number of upvalues *)
+      nparams: Byte;             (* (u) number of parameters *)
+      isvararg: ByteBool;        (* (u) *)
+      istailcall: ByteBool;      (* (t) *)
+      short_src: array[0..LUA_IDSIZE - 1] of Char; (* (S) *)
+      (* private part *)
+      i_ci: Pointer;             (* active function *)  // ptr to struct CallInfo
+   end;
+   Plua_Debug = ^lua_Debug;
+
+   // Functions to be called by the debugger in specific events
+   lua_Hook = procedure(L: Plua_State; ar: Plua_Debug); cdecl;
+
+function lua_getstack(L: Plua_State; level: Integer; ar: Plua_Debug): Integer; cdecl;
+function lua_getinfo(L: Plua_State; const what: PAnsiChar; ar: Plua_Debug): Integer; cdecl;
+function lua_getlocal(L: Plua_State; const ar: Plua_Debug; n: Integer): PAnsiChar; cdecl;
+function lua_setlocal(L: Plua_State; const ar: Plua_Debug; n: Integer): PAnsiChar; cdecl;
+function lua_getupvalue(L: Plua_State; funcindex, n: Integer): PAnsiChar; cdecl;
+function lua_setupvalue(L: Plua_State; funcindex, n: Integer): PAnsiChar; cdecl;
+function lua_upvalueid(L: Plua_State; funcindex, n: Integer): Pointer; cdecl;
+procedure lua_upvaluejoin(L: Plua_State; funcindex1, n1, funcindex2, n2: Integer); cdecl;
+function lua_sethook(L: Plua_State; func: lua_Hook; mask: Integer; count: Integer): Integer; cdecl;
+function lua_gethook(L: Plua_State): lua_Hook; cdecl;
+function lua_gethookmask(L: Plua_State): Integer; cdecl;
+function lua_gethookcount(L: Plua_State): Integer; cdecl;
+
+// pre-defined references
+const
+   LUA_NOREF  = -2;
+   LUA_REFNIL = -1;
+
+// compatibility with ref system
+procedure lua_unref(L: Plua_State; ref: Integer);
+procedure lua_getref(L: Plua_State; ref: Integer);
 
 type
-    luaL_Buffer = packed record
-    p : PChar;       (* current position in buffer *)
-    lvl : LongInt;   (* number of strings in the stack (level) *)
-    L : Plua_State;
-    buffer : array [0..LUAL_BUFFERSIZE-1] of Char;
-    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);
-    cdecl; external LuaLibName;
-function  luaL_prepbuffer(B : PluaL_Buffer) : PChar;
-    cdecl; external LuaLibName;
-procedure luaL_addlstring(B : PluaL_Buffer; const s : PChar; ls : size_t);
-    cdecl; external LuaLibName;
-procedure luaL_addstring(B : PluaL_Buffer; const s : PChar);
-    cdecl; external LuaLibName;
-procedure luaL_addvalue(B : PluaL_Buffer);
-    cdecl; external LuaLibName;
-procedure luaL_pushresult(B : PluaL_Buffer);
-    cdecl; external LuaLibName;
-
-(* ====================================================== *)
-
-
-(* compatibility with ref system *)
-
-(* pre-defined references *)
-const
-    LUA_NOREF  = -2;
-    LUA_REFNIL = -1;
-
-function lua_ref(L : Plua_State; lock : Boolean) : LongInt;
-
-procedure lua_unref(L : Plua_State; ref : LongInt);
-
-procedure lua_getref(L : Plua_State; ref : LongInt);
-
-
-(******************************************************************************)
-(******************************************************************************)
-(******************************************************************************)
+   luaL_Reg = record
+      name: PAnsiChar;
+      func: lua_CFunction;
+   end;
+   PluaL_Reg = ^luaL_Reg;
+
+procedure luaL_checkversion_(L: Plua_State; ver: lua_Number); cdecl;
+procedure luaL_checkversion(L: Plua_State);
+function luaL_getmetafield(L: Plua_State; obj: Integer; const e: PAnsiChar): Integer; cdecl;
+function luaL_callmeta(L: Plua_State; obj: Integer; const e: PAnsiChar): Integer; cdecl;
+function luaL_tolstring(L: Plua_State; idx: Integer; len: Psize_t): PAnsiChar; cdecl;
+function luaL_argerror(L: Plua_State; numarg: Integer; const extramsg: PAnsiChar): Integer; cdecl;
+function luaL_checklstring(L: Plua_State; numArg: Integer; l_: Psize_t): PAnsiChar; cdecl;
+function luaL_optlstring(L: Plua_State; numArg: Integer; const def: PAnsiChar; l_: Psize_t): PAnsiChar; cdecl;
+function luaL_checknumber(L: Plua_State; numArg: Integer): lua_Number; cdecl;
+function luaL_optnumber(L: Plua_State; nArg: Integer; def: lua_Number): lua_Number; cdecl;
+function luaL_checkinteger(L: Plua_State; numArg: Integer): lua_Integer; cdecl;
+function luaL_optinteger(L: Plua_State; nArg: Integer; def: lua_Integer): lua_Integer; cdecl;
+function luaL_checkunsigned(L: Plua_State; numArg: Integer): lua_Unsigned; cdecl;
+function luaL_optunsigned(L: Plua_State; numArg: Integer; def: lua_Unsigned): lua_Unsigned; cdecl;
+procedure luaL_checkstack(L: Plua_State; sz: Integer; const msg: PAnsiChar); cdecl;
+procedure luaL_checktype(L: Plua_State; narg, t: Integer); cdecl;
+procedure luaL_checkany(L: Plua_State; narg: Integer); cdecl;
+function luaL_newmetatable(L: Plua_State; const tname: PAnsiChar): Integer; cdecl;
+procedure luaL_setmetatable(L: Plua_State; const tname: PAnsiChar); cdecl;
+function luaL_testudata(L: Plua_State; ud: Integer; const tname: PAnsiChar): Pointer; cdecl;
+function luaL_checkudata(L: Plua_State; ud: Integer; const tname: PAnsiChar): Pointer; cdecl;
+procedure luaL_where(L: Plua_State; lvl: Integer); cdecl;
+function luaL_error(L: Plua_State; const fmt: PAnsiChar): Integer; cdecl; varargs;
+function luaL_checkoption(L: Plua_State; narg: Integer; def: PAnsiChar; lst: PPAnsiChar): Integer; cdecl;
+function luaL_fileresult(L: Plua_State; stat: Integer; const fname: PAnsiChar): Integer; cdecl;
+function luaL_execresult(L: Plua_State; stat: Integer): Integer; cdecl;
+function luaL_ref(L: Plua_State; t: Integer): Integer; cdecl;
+procedure luaL_unref(L: Plua_State; t, ref: Integer); cdecl;
+function luaL_loadfilex(L: Plua_State; const filename, mode: PAnsiChar): Integer; cdecl;
+function luaL_loadfile(L: Plua_State; const filename: PAnsiChar): Integer;
+function luaL_loadbufferx(L: Plua_State; const buff: PAnsiChar; sz: size_t; const name, mode: PAnsiChar): Integer; cdecl;
+function luaL_loadstring(L: Plua_State; const s: PAnsiChar): Integer; cdecl;
+function luaL_newstate: Plua_State; cdecl;
+function luaL_len(L: Plua_State; idx: Integer): Integer; cdecl;
+function luaL_gsub(L: Plua_State; const s, p, r: PAnsiChar): PAnsiChar; cdecl;
+procedure luaL_setfuncs(L: Plua_State; lr: array of luaL_Reg; nup: Integer); overload;
+procedure luaL_setfuncs(L: Plua_State; lr: PluaL_Reg; nup: Integer); cdecl; overload;
+function luaL_getsubtable(L: Plua_State; idx: Integer; const fname: PAnsiChar): Integer; cdecl;
+procedure luaL_traceback(L, L1: Plua_State; msg: PAnsiChar; level: Integer); cdecl;
+procedure luaL_requiref(L: Plua_State; const modname: PAnsiChar; openf: lua_CFunction; glb: LongBool); cdecl;
+
+// some useful macros
+procedure luaL_newlibtable(L: Plua_State; lr: array of luaL_Reg); overload;
+procedure luaL_newlibtable(L: Plua_State; lr: PluaL_Reg); overload;
+procedure luaL_newlib(L: Plua_State; lr: array of luaL_Reg); overload;
+procedure luaL_newlib(L: Plua_State; lr: PluaL_Reg); overload;
+procedure luaL_argcheck(L: Plua_State; cond: Boolean; numarg: Integer; extramsg: PAnsiChar);
+function luaL_checkstring(L: Plua_State; n: Integer): PAnsiChar;
+function luaL_optstring(L: Plua_State; n: Integer; d: PAnsiChar): PAnsiChar;
+function luaL_checkint(L: Plua_State; n: Integer): Integer;
+function luaL_optint(L: Plua_State; n, d: Integer): Integer;
+function luaL_checklong(L: Plua_State; n: Integer): LongInt;
+function luaL_optlong(L: Plua_State; n: Integer; d: LongInt): LongInt;
+function luaL_typename(L: Plua_State; i: Integer): PAnsiChar;
+function luaL_dofile(L: Plua_State; const filename: PAnsiChar): Integer;
+function luaL_dostring(L: Plua_State; const str: PAnsiChar): Integer;
+procedure luaL_getmetatable(L: Plua_State; tname: PAnsiChar);
+function luaL_loadbuffer(L: Plua_State; const buff: PAnsiChar; size: size_t; const name: PAnsiChar): Integer;
+
+const
+   LUA_COLIBNAME   = 'coroutine';
+   LUA_TABLIBNAME  = 'table';
+   LUA_IOLIBNAME   = 'io';
+   LUA_OSLIBNAME   = 'os';
+   LUA_STRLINAME   = 'string';
+   LUA_BITLIBNAME  = 'bit32';
+   LUA_MATHLIBNAME = 'math';
+   LUA_DBLIBNAME   = 'debug';
+   LUA_LOADLIBNAME = 'package';
+
+function luaopen_base(L: Plua_State): LongBool; cdecl;
+function luaopen_coroutine(L: Plua_State): LongBool; cdecl;
+function luaopen_table(L: Plua_State): LongBool; cdecl;
+function luaopen_io(L: Plua_State): LongBool; cdecl;
+function luaopen_os(L: Plua_State): LongBool; cdecl;
+function luaopen_string(L: Plua_State): LongBool; cdecl;
+function luaopen_bit32(L: Plua_State): LongBool; cdecl;
+function luaopen_math(L: Plua_State): LongBool; cdecl;
+function luaopen_debug(L: Plua_State): LongBool; cdecl;
+function luaopen_package(L: Plua_State): LongBool; cdecl;
+
+// open all previous libraries
+procedure luaL_openlibs(L: Plua_State); cdecl;
+
+(* deprecated functions *)
+procedure luaL_pushmodule(L: Plua_State; modname: PAnsiChar; sizehint: Integer); cdecl; deprecated;
+procedure luaL_openlib(L: Plua_State; const libname: PAnsiChar; lr: PluaL_Reg; nup: Integer); cdecl ; overload; deprecated;
+procedure luaL_openlib(L: Plua_State; const libname: PAnsiChar; lr: array of luaL_Reg; nup: Integer); overload; deprecated;
+procedure luaL_register(L: Plua_State; const libname: PAnsiChar; lr: PluaL_Reg); cdecl; overload; deprecated;
+procedure luaL_register(L: Plua_State; const libname: PAnsiChar; lr: array of luaL_Reg); overload; deprecated;
+(* end of deprecated functions *)
 
 implementation
 
-uses
-  SysUtils;
+(* deprecated functions *)
+procedure luaL_pushmodule(L: Plua_State; modname: PAnsiChar; sizehint: Integer); cdecl; external LUA_LIB_NAME;
+procedure luaL_openlib(L: Plua_State; const libname: PAnsiChar; lr: PluaL_Reg; nup: Integer); cdecl; external LUA_LIB_NAME;
 
-(*****************************************************************************)
-(*                            luaconfig.h                                    *)
-(*****************************************************************************)
-{
-function  lua_readline(L : Plua_State;
-var
-    b : PChar; p : PChar): Boolean;
-var
-    s : AnsiString;
+procedure luaL_openlib(L: Plua_State; const libname: PAnsiChar; lr: array of luaL_Reg; nup: Integer);
 begin
-    Write(p);                        // show prompt
-    ReadLn(s);                       // get line
-    b := PChar(s);                   //   and return it
-    lua_readline := (b[0] <> #4);          // test for ctrl-D
+   luaL_openlib(L, libname, @lr, nup);
 end;
-}
-procedure lua_saveline(L : Plua_State; idx : LongInt);
+
+procedure luaL_register(L: Plua_State; const libname: PAnsiChar; lr: array of luaL_Reg);
 begin
+   luaL_openlib(L, libname, @lr, 0);
 end;
 
-procedure lua_freeline(L : Plua_State; b : PChar);
+procedure luaL_register(L: Plua_State; const libname: PAnsiChar; lr: PluaL_Reg); cdecl;
 begin
+   luaL_openlib(L, libname, lr, 0);
 end;
+(* end of deprecated functions *)
 
+function lua_upvalueindex(I: Integer): Integer;
+begin
+   Result := LUA_REGISTRYINDEX - i;
+end;
 
-(*****************************************************************************)
-(*                                  lua.h                                    *)
-(*****************************************************************************)
+function lua_newstate(f: lua_Alloc; ud: Pointer): Plua_State; cdecl; external LUA_LIB_NAME;
+procedure lua_close(L: Plua_State); cdecl; external LUA_LIB_NAME;
+function lua_newthread(L: Plua_State): Plua_State; cdecl; external LUA_LIB_NAME;
+function lua_atpanic(L: Plua_State; panicf: lua_CFunction): lua_CFunction; cdecl; external LUA_LIB_NAME;
+function lua_version(L: Plua_State): Plua_Number; cdecl; external LUA_LIB_NAME;
+function lua_absindex(L: Plua_State; idx: Integer): Integer; cdecl; external LUA_LIB_NAME;
+function lua_gettop(L: Plua_State): Integer; cdecl; external LUA_LIB_NAME;
+procedure lua_settop(L: Plua_State; idx: Integer); cdecl; external LUA_LIB_NAME;
+procedure lua_pushvalue(L: Plua_State; Idx: Integer); cdecl; external LUA_LIB_NAME;
+procedure lua_remove(L: Plua_State; idx: Integer); cdecl; external LUA_LIB_NAME;
+procedure lua_insert(L: Plua_State; idx: Integer); cdecl; external LUA_LIB_NAME;
+procedure lua_replace(L: Plua_State; idx: Integer); cdecl; external LUA_LIB_NAME;
+procedure lua_copy(L: Plua_State; fromidx, toidx: Integer); cdecl; external LUA_LIB_NAME;
+function lua_checkstack(L: Plua_State; sz: Integer): LongBool; cdecl; external LUA_LIB_NAME;
+procedure lua_xmove(from, to_: Plua_State; n: Integer); cdecl; external LUA_LIB_NAME;
+function lua_isnumber(L: Plua_State; idx: Integer): LongBool; cdecl; external LUA_LIB_NAME;
+function lua_isstring(L: Plua_State; idx: Integer): LongBool; cdecl; external LUA_LIB_NAME;
+function lua_iscfunction(L: Plua_State; idx: Integer): LongBool; cdecl; external LUA_LIB_NAME;
+function lua_isuserdata(L: Plua_State; idx: Integer): LongBool; cdecl; external LUA_LIB_NAME;
+function lua_type(L: Plua_State; idx: Integer): Integer; cdecl; external LUA_LIB_NAME;
+function lua_typename(L: Plua_State; tp: Integer): PAnsiChar; cdecl; external LUA_LIB_NAME;
+function lua_tonumberx(L: Plua_State; idx: Integer; isnum: PInteger): lua_Number; cdecl; external LUA_LIB_NAME;
+function lua_tointegerx(L: Plua_State; idx: Integer; isnum: PInteger): lua_Integer; cdecl; external LUA_LIB_NAME;
+function lua_tounsignedx(L: Plua_State; idx: Integer; isnum: PInteger): lua_Unsigned; cdecl; external LUA_LIB_NAME;
+procedure lua_arith(L: Plua_State; op: Integer); cdecl; external LUA_LIB_NAME;
+function lua_rawequal(L: Plua_State; idx1, idx2: Integer): LongBool; cdecl; external LUA_LIB_NAME;
+function lua_compare(L: Plua_State; idx1, idx2, op: Integer): LongBool; cdecl; external LUA_LIB_NAME;
 
-function lua_upvalueindex(idx : LongInt) : LongInt;
+function lua_tonumber(L: Plua_State; idx: Integer): lua_Number;
 begin
-lua_upvalueindex := LUA_GLOBALSINDEX - idx;
+   Result := lua_tonumberx(L, idx, nil);
 end;
 
-procedure lua_pop(L : Plua_State; n : LongInt);
+function lua_tointeger(L: Plua_State; idx: Integer): lua_Integer;
 begin
-lua_settop(L, -n - 1);
+   Result := lua_tointegerx(L, idx, nil);
 end;
 
-procedure lua_newtable(L : Plua_State);
+function lua_tounsigned(L: Plua_State; idx: Integer): lua_Unsigned;
 begin
-lua_createtable(L, 0, 0);
+   Result := lua_tounsignedx(L, idx, nil);
 end;
 
-procedure lua_register(L : Plua_State; n : PChar; f : lua_CFunction);
+function lua_toboolean(L: Plua_State; idx: Integer): LongBool; cdecl; external LUA_LIB_NAME;
+function lua_tolstring(L: Plua_State; idx: Integer; len: Psize_t): PAnsiChar; cdecl; external LUA_LIB_NAME;
+function lua_rawlen(L: Plua_State; idx: Integer): size_t; cdecl; external LUA_LIB_NAME;
+function lua_tocfunction(L: Plua_State; idx: Integer): lua_CFunction; cdecl; external LUA_LIB_NAME;
+function lua_touserdata(L: Plua_State; idx: Integer): Pointer; cdecl; external LUA_LIB_NAME;
+function lua_tothread(L: Plua_State; idx: Integer): Plua_State; cdecl; external LUA_LIB_NAME;
+function lua_topointer(L: Plua_State; idx: Integer): Pointer; cdecl; external LUA_LIB_NAME;
+procedure lua_pushnil(L: Plua_State); cdecl; external LUA_LIB_NAME;
+procedure lua_pushnumber(L: Plua_State; n: lua_Number); cdecl; external LUA_LIB_NAME;
+procedure lua_pushinteger(L: Plua_State; n: lua_Integer); cdecl; external LUA_LIB_NAME;
+procedure lua_pushunsigned(L: Plua_State; n: lua_Unsigned); cdecl; external LUA_LIB_NAME;
+procedure lua_pushlstring(L: Plua_State; const s: PAnsiChar; l_: size_t); cdecl; external LUA_LIB_NAME;
+procedure lua_pushstring(L: Plua_State; const s: PAnsiChar); cdecl; external LUA_LIB_NAME;
+
+procedure lua_pushstring(L: Plua_State; const s: AnsiString);
 begin
-lua_pushcfunction(L, f);
-lua_setglobal(L, n);
+   lua_pushlstring(L, PAnsiChar(s), Length(s));
 end;
 
-procedure lua_pushcfunction(L : Plua_State; f : lua_CFunction);
+function lua_pushvfstring(L: Plua_State; const fmt: PAnsiChar; argp: Pointer): PAnsiChar; cdecl; external LUA_LIB_NAME;
+function lua_pushfstring(L: Plua_State; const fmt: PAnsiChar): PAnsiChar; cdecl; varargs; external LUA_LIB_NAME;
+procedure lua_pushcclosure(L: Plua_State; fn: lua_CFunction; n: Integer); cdecl; external LUA_LIB_NAME;
+procedure lua_pushboolean(L: Plua_State; b: LongBool); cdecl; external LUA_LIB_NAME;
+procedure lua_pushlightuserdata(L: Plua_State; p: Pointer); cdecl; external LUA_LIB_NAME;
+procedure lua_pushthread(L: Plua_State); cdecl; external LUA_LIB_NAME;
+procedure lua_getglobal(L: Plua_State; const var_: PAnsiChar); cdecl; external LUA_LIB_NAME;
+procedure lua_gettable(L: Plua_State; idx: Integer); cdecl; external LUA_LIB_NAME;
+procedure lua_getfield(L: Plua_state; idx: Integer; k: PAnsiChar); cdecl; external LUA_LIB_NAME;
+procedure lua_rawget(L: Plua_State; idx: Integer); cdecl; external LUA_LIB_NAME;
+procedure lua_rawgeti(L: Plua_State; idx, n: Integer); cdecl; external LUA_LIB_NAME;
+procedure lua_rawgetp(L: Plua_State; idx: Integer; p: Pointer); cdecl; external LUA_LIB_NAME;
+procedure lua_createtable(L: Plua_State; narr, nrec: Integer); cdecl; external LUA_LIB_NAME;
+function lua_newuserdata(L: Plua_State; sz: size_t): Pointer; cdecl; external LUA_LIB_NAME;
+function lua_getmetatable(L: Plua_State; objindex: Integer): Integer; cdecl; external LUA_LIB_NAME;
+procedure lua_getuservalue(L: Plua_State; idx: Integer); cdecl; external LUA_LIB_NAME;
+procedure lua_setglobal(L: Plua_State; const var_: PAnsiChar); cdecl; external LUA_LIB_NAME;
+procedure lua_settable(L: Plua_State; idx: Integer); cdecl; external LUA_LIB_NAME;
+procedure lua_setfield(L: Plua_State; idx: Integer; k: PAnsiChar); cdecl; external LUA_LIB_NAME;
+procedure lua_rawset(L: Plua_State; idx: Integer); cdecl; external LUA_LIB_NAME;
+procedure lua_rawseti(L: Plua_State; idx, n: Integer); cdecl; external LUA_LIB_NAME;
+procedure lua_rawsetp(L: Plua_State; idx: Integer; p: Pointer); cdecl; external LUA_LIB_NAME;
+function lua_setmetatable(L: Plua_State; objindex: Integer): Integer; cdecl; external LUA_LIB_NAME;
+procedure lua_setuservalue(L: Plua_State; idx: Integer); cdecl; external LUA_LIB_NAME;
+procedure lua_callk(L: Plua_State; nargs, nresults, ctx: Integer; k: lua_CFunction); cdecl; external LUA_LIB_NAME;
+function lua_getctx(L: Plua_State; ctx: PInteger): Integer; cdecl; external LUA_LIB_NAME;
+function lua_pcallk(L: Plua_State; nargs, nresults, errfunc, ctx: Integer; k: lua_CFunction): Integer; cdecl; external LUA_LIB_NAME;
+function lua_load(L: Plua_State; reader: lua_Reader; dt: Pointer; const chunkname, mode: PAnsiChar): Integer; cdecl; external LUA_LIB_NAME;
+function lua_dump(L: Plua_State; writer: lua_Writer; data: Pointer): Integer; cdecl; external LUA_LIB_NAME;
+function lua_yieldk(L: Plua_State; nresults, ctx: Integer; k: lua_CFunction): Integer; cdecl; external LUA_LIB_NAME;
+
+procedure lua_call(L: Plua_State; nargs, nresults: Integer);
 begin
-    lua_pushcclosure(L, f, 0);
+   lua_callk(L, nargs, nresults, 0, nil);
 end;
 
-function  lua_strlen(L : Plua_State; idx : LongInt) : LongInt;
+function lua_pcall(L: Plua_State; nargs, nresults, errf: Integer): Integer;
 begin
-    lua_strlen := lua_objlen(L, idx);
+   Result := lua_pcallk(L, nargs, nresults, errf, 0, nil);
 end;
 
-function lua_isfunction(L : Plua_State; n : LongInt) : Boolean;
+function lua_yield(L: Plua_State; nresults: Integer): Integer;
 begin
-    lua_isfunction := lua_type(L, n) = LUA_TFUNCTION;
+   Result := lua_yieldk(L, nresults, 0, nil);
 end;
 
-function lua_istable(L : Plua_State; n : LongInt) : Boolean;
+function lua_resume(L, from: Plua_State; narg: Integer): Integer; cdecl; external LUA_LIB_NAME;
+function lua_status(L: Plua_State): Integer; cdecl; external LUA_LIB_NAME;
+function lua_gc(L: Plua_State; what, data: Integer): Integer; cdecl; external LUA_LIB_NAME;
+function lua_error(L: Plua_State): Integer; cdecl; external LUA_LIB_NAME;
+function lua_next(L: Plua_State; idx: Integer): Integer; cdecl; external LUA_LIB_NAME;
+procedure lua_concat(L: Plua_State; n: Integer); cdecl; external LUA_LIB_NAME;
+procedure lua_len(L: Plua_State; idx: Integer); cdecl; external LUA_LIB_NAME;
+function lua_getallocf(L: Plua_State; ud: PPointer): lua_Alloc; cdecl; external LUA_LIB_NAME;
+procedure lua_setallocf(L: Plua_State; f: lua_Alloc; ud: Pointer); cdecl; external LUA_LIB_NAME;
+
+procedure lua_pop(L: Plua_State; n: Integer);
 begin
-    lua_istable := lua_type(L, n) = LUA_TTABLE;
+   lua_settop(L, - n - 1);
 end;
 
-function lua_islightuserdata(L : Plua_State; n : LongInt) : Boolean;
+procedure lua_newtable(L: Plua_State);
 begin
-    lua_islightuserdata := lua_type(L, n) = LUA_TLIGHTUSERDATA;
+   lua_createtable(L, 0, 0);
 end;
 
-function lua_isnil(L : Plua_State; n : LongInt) : Boolean;
+procedure lua_register(L: Plua_State; const n: PAnsiChar; f: lua_CFunction);
 begin
-    lua_isnil := lua_type(L, n) = LUA_TNIL;
+   lua_pushcfunction(L, f);
+   lua_setglobal(L, n);
 end;
 
-function lua_isboolean(L : Plua_State; n : LongInt) : Boolean;
+procedure lua_pushcfunction(L: Plua_State; f: lua_CFunction);
 begin
-    lua_isboolean := lua_type(L, n) = LUA_TBOOLEAN;
+   lua_pushcclosure(L, f, 0);
 end;
 
-function lua_isthread(L : Plua_State; n : LongInt) : Boolean;
+function lua_isfunction(L: Plua_State; n: Integer): Boolean;
 begin
-    lua_isthread := lua_type(L, n) = LUA_TTHREAD;
+   Result := lua_type(L, n) = LUA_TFUNCTION;
 end;
 
-function lua_isnone(L : Plua_State; n : LongInt) : Boolean;
+function lua_istable(L: Plua_State; n: Integer): Boolean;
 begin
-    lua_isnone := lua_type(L, n) = LUA_TNONE;
+   Result := lua_type(L, n) = LUA_TTABLE;
 end;
 
-function lua_isnoneornil(L : Plua_State; n : LongInt) : Boolean;
+function lua_islightuserdata(L: Plua_State; n: Integer): Boolean;
 begin
-    lua_isnoneornil := lua_type(L, n) <= 0;
+   Result := lua_type(L, n) = LUA_TLIGHTUSERDATA;
 end;
 
-procedure lua_pushliteral(L : Plua_State; s : PChar);
+function lua_isnil(L: Plua_State; n: Integer): Boolean;
 begin
-    lua_pushlstring(L, s, StrLen(s));
+   Result := lua_type(L, n) = LUA_TNIL;
 end;
 
-procedure lua_setglobal(L : Plua_State; s : PChar);
+function lua_isboolean(L: Plua_State; n: Integer): Boolean;
 begin
-    lua_setfield(L, LUA_GLOBALSINDEX, s);
+   Result := lua_type(L, n) = LUA_TBOOLEAN;
 end;
 
-procedure lua_getglobal(L: Plua_State; s: PChar);
+function lua_isthread(L: Plua_State; n: Integer): Boolean;
 begin
-    lua_getfield(L, LUA_GLOBALSINDEX, s);
+   Result := lua_type(L, n) = LUA_TTHREAD;
 end;
 
-function lua_tostring(L : Plua_State; idx : LongInt) : AnsiString;
+function lua_isnone(L: Plua_State; n: Integer): Boolean;
 begin
-    lua_tostring := StrPas(lua_tolstring(L, idx, nil));
+   Result := lua_type(L, n) = LUA_TNONE;
 end;
 
-function lua_open : Plua_State;
+function lua_isnoneornil(L: Plua_State; n: Integer): Boolean;
 begin
-    lua_open := luaL_newstate;
+   Result := lua_type(L, n) <= 0;
 end;
 
-procedure lua_getregistry(L : Plua_State);
+procedure lua_pushliteral(L: Plua_State; s: PAnsiChar);
 begin
-    lua_pushvalue(L, LUA_REGISTRYINDEX);
+   lua_pushlstring(L, s, Length(s));
 end;
 
-function lua_getgccount(L : Plua_State) : LongInt;
+procedure lua_pushglobaltable(L: Plua_State);
 begin
-    lua_getgccount := lua_gc(L, LUA_GCCOUNT, 0);
+   lua_rawgeti(L, LUA_REGISTRYINDEX, LUA_RIDX_GLOBALS);
 end;
 
+function lua_tostring(L: Plua_State; i: Integer): PAnsiChar;
+begin
+   Result := lua_tolstring(L, i, nil);
+end;
 
-(*****************************************************************************)
-(*                                  lualib.h                                 *)
-(*****************************************************************************)
+function lua_getstack(L: Plua_State; level: Integer; ar: Plua_Debug): Integer; cdecl; external LUA_LIB_NAME;
+function lua_getinfo(L: Plua_State; const what: PAnsiChar; ar: Plua_Debug): Integer; cdecl; external LUA_LIB_NAME;
+function lua_getlocal(L: Plua_State; const ar: Plua_Debug; n: Integer): PAnsiChar; cdecl; external LUA_LIB_NAME;
+function lua_setlocal(L: Plua_State; const ar: Plua_Debug; n: Integer): PAnsiChar; cdecl; external LUA_LIB_NAME;
+function lua_getupvalue(L: Plua_State; funcindex, n: Integer): PAnsiChar; cdecl; external LUA_LIB_NAME;
+function lua_setupvalue(L: Plua_State; funcindex, n: Integer): PAnsiChar; cdecl; external LUA_LIB_NAME;
+function lua_upvalueid(L: Plua_State; funcindex, n: Integer): Pointer; cdecl; external LUA_LIB_NAME;
+procedure lua_upvaluejoin(L: Plua_State; funcindex1, n1, funcindex2, n2: Integer); cdecl; external LUA_LIB_NAME;
+function lua_sethook(L: Plua_State; func: lua_Hook; mask: Integer; count: Integer): Integer; cdecl; external LUA_LIB_NAME;
+function lua_gethook(L: Plua_State): lua_Hook; cdecl; external LUA_LIB_NAME;
+function lua_gethookmask(L: Plua_State): Integer; cdecl; external LUA_LIB_NAME;
+function lua_gethookcount(L: Plua_State): Integer; cdecl; external LUA_LIB_NAME;
 
-procedure lua_assert(x : Boolean);
+procedure lua_unref(L: Plua_State; ref: Integer);
 begin
+   luaL_unref(L, LUA_REGISTRYINDEX, ref);
 end;
 
+procedure lua_getref(L: Plua_State; ref: Integer);
+begin
+   lua_rawgeti(L, LUA_REGISTRYINDEX, ref);
+end;
 
-(*****************************************************************************)
-(*                                  lauxlib.h    n                           *)
-(*****************************************************************************)
+procedure luaL_traceback(L, L1: Plua_State; msg: PAnsiChar; level: Integer); cdecl; external LUA_LIB_NAME;
+function luaL_argerror(L: Plua_State; numarg: Integer; const extramsg: PAnsiChar): Integer; cdecl; external LUA_LIB_NAME;
+procedure luaL_where(L: Plua_State; lvl: Integer); cdecl; external LUA_LIB_NAME;
+function luaL_newmetatable(L: Plua_State; const tname: PAnsiChar): Integer; cdecl; external LUA_LIB_NAME;
+procedure luaL_setmetatable(L: Plua_State; const tname: PAnsiChar); cdecl; external LUA_LIB_NAME;
+function luaL_testudata(L: Plua_State; ud: Integer; const tname: PAnsiChar): Pointer; cdecl; external LUA_LIB_NAME;
+function luaL_checkudata(L: Plua_State; ud: Integer; const tname: PAnsiChar): Pointer; cdecl; external LUA_LIB_NAME;
+function luaL_error(L: Plua_State; const fmt: PAnsiChar): Integer; cdecl; varargs; external LUA_LIB_NAME;
+function luaL_checkoption(L: Plua_State; narg: Integer; def: PAnsiChar; lst: PPAnsiChar): Integer; cdecl; external LUA_LIB_NAME;
+procedure luaL_checkstack(L: Plua_State; sz: Integer; const msg: PAnsiChar); cdecl; external LUA_LIB_NAME;
+procedure luaL_checktype(L: Plua_State; narg, t: Integer); cdecl; external LUA_LIB_NAME;
+procedure luaL_checkany(L: Plua_State; narg: Integer); cdecl; external LUA_LIB_NAME;
+function luaL_checklstring(L: Plua_State; numArg: Integer; l_: Psize_t): PAnsiChar; cdecl; external LUA_LIB_NAME;
+function luaL_optlstring(L: Plua_State; numArg: Integer; const def: PAnsiChar; l_: Psize_t): PAnsiChar; cdecl; external LUA_LIB_NAME;
+function luaL_checknumber(L: Plua_State; numArg: Integer): lua_Number; cdecl; external LUA_LIB_NAME;
+function luaL_optnumber(L: Plua_State; nArg: Integer; def: lua_Number): lua_Number; cdecl; external LUA_LIB_NAME;
+function luaL_checkinteger(L: Plua_State; numArg: Integer): lua_Integer; cdecl; external LUA_LIB_NAME;
+function luaL_optinteger(L: Plua_State; nArg: Integer; def: lua_Integer): lua_Integer; cdecl; external LUA_LIB_NAME;
+function luaL_checkunsigned(L: Plua_State; numArg: Integer): lua_Unsigned; cdecl; external LUA_LIB_NAME;
+function luaL_optunsigned(L: Plua_State; numArg: Integer; def: lua_Unsigned): lua_Unsigned; cdecl; external LUA_LIB_NAME;
 
-function luaL_getn(L : Plua_State; idx : LongInt) : LongInt;
+procedure luaL_argcheck(L: Plua_State; cond: Boolean; numarg: Integer; extramsg: PAnsiChar);
 begin
-    luaL_getn := lua_objlen(L, idx);
+   if not cond then
+      luaL_argerror(L, numarg, extramsg);
 end;
 
-procedure luaL_setn(L : plua_State; i, j : LongInt);
+function luaL_checkstring(L: Plua_State; n: Integer): PAnsiChar;
 begin
-    (* no op *)
+   Result := luaL_checklstring(L, n, nil);
 end;
 
-function luaL_argcheck(L : Plua_State; cond : Boolean; numarg : LongInt; extramsg : PChar): LongInt;
+function luaL_optstring(L: Plua_State; n: Integer; d: PAnsiChar): PAnsiChar;
 begin
-    if not cond then
-        luaL_argcheck := luaL_argerror(L, numarg, extramsg)
-    else
-        luaL_argcheck := 0;
+   Result := luaL_optlstring(L, n, d, nil);
 end;
 
-function luaL_checkstring(L : Plua_State; n : LongInt) : PChar;
+function luaL_checkint(L: Plua_State; n: Integer): Integer;
 begin
-    luaL_checkstring := luaL_checklstring(L, n, nil);
+   Result := luaL_checkinteger(L, n);
 end;
 
-function luaL_optstring(L : Plua_State; n : LongInt; d : PChar) : PChar;
+function luaL_checklong(L: Plua_State; n: Integer): LongInt;
 begin
-    luaL_optstring := luaL_optlstring(L, n, d, nil);
+   Result := luaL_checkinteger(L, n);
 end;
 
-function luaL_checkint(L : Plua_State; n : LongInt) : lua_Integer;
+function luaL_optint(L: Plua_State; n, d: Integer): Integer;
 begin
-    luaL_checkint := luaL_checkinteger(L, n);
+   Result := luaL_optinteger(L, n, d);
 end;
 
-function luaL_optint(L : Plua_State; n : LongInt; d : lua_Integer): lua_Integer;
+function luaL_optlong(L: Plua_State; n: Integer; d: LongInt): LongInt;
 begin
-    luaL_optint := luaL_optinteger(L, n, d);
+   Result := luaL_optinteger(L, n, d);
 end;
 
-function luaL_checklong(L : Plua_State; n : LongInt) : lua_Integer;
+function luaL_typename(L: Plua_State; i: Integer): PAnsiChar;
 begin
-    luaL_checklong := luaL_checkinteger(L, n);
+   Result := lua_typename(L, lua_type(L, i));
 end;
 
-function luaL_optlong(L : Plua_State; n : LongInt; d : lua_Integer) : lua_Integer;
+function luaL_dofile(L: Plua_State; const filename: PAnsiChar): Integer;
 begin
-    luaL_optlong := luaL_optinteger(L, n, d);
+   Result := luaL_loadfile(L, filename);
+   if Result = 0 then
+      Result := lua_pcall(L, 0, LUA_MULTRET, 0);
 end;
 
-function luaL_typename(L : Plua_State; idx : LongInt) : PChar;
+function luaL_dostring(L: Plua_State; const str: PAnsiChar): Integer;
 begin
-    luaL_typename := lua_typename( L, lua_type(L, idx) );
+   Result := luaL_loadstring(L, str);
+   if Result = 0 then
+      Result := lua_pcall(L, 0, LUA_MULTRET, 0);
 end;
 
-function luaL_dofile(L : Plua_State; fn : PChar) : LongInt;
+procedure luaL_getmetatable(L: Plua_State; tname: PAnsiChar);
 begin
-    luaL_dofile := luaL_loadfile(L, fn);
-    if luaL_dofile = 0 then
-        luaL_dofile := lua_pcall(L, 0, 0, 0);
+   lua_getfield(L, LUA_REGISTRYINDEX, tname);
 end;
 
-function luaL_dostring(L : Plua_State; s : PChar) : LongInt;
+function luaL_fileresult(L: Plua_State; stat: Integer; const fname: PAnsiChar): Integer; cdecl; external LUA_LIB_NAME;
+function luaL_execresult(L: Plua_State; stat: Integer): Integer; cdecl; external LUA_LIB_NAME;
+function luaL_ref(L: Plua_State; t: Integer): Integer; cdecl; external LUA_LIB_NAME;
+procedure luaL_unref(L: Plua_State; t, ref: Integer); cdecl; external LUA_LIB_NAME;
+function luaL_loadfilex(L: Plua_State; const filename, mode: PAnsiChar): Integer; cdecl; external LUA_LIB_NAME;
+function luaL_loadbufferx(L: Plua_State; const buff: PAnsiChar; sz: size_t; const name, mode: PAnsiChar): Integer; cdecl; external LUA_LIB_NAME;
+
+function luaL_loadfile(L: Plua_State; const filename: PAnsiChar): Integer;
 begin
-    luaL_dostring := luaL_loadstring(L, s);
-    if luaL_dostring = 0 then
-        luaL_dostring := lua_pcall(L, 0, 0, 0);
+   Result := luaL_loadfilex(L, filename, nil);
 end;
 
-procedure luaL_getmetatable(L : Plua_State; n : PChar);
+function luaL_loadbuffer(L: Plua_State; const buff: PAnsiChar; size: size_t; const name: PAnsiChar): Integer;
 begin
-    lua_getfield(L, LUA_REGISTRYINDEX, n);
+   Result := luaL_loadbufferx(L, buff, size, name, nil);
 end;
 
-procedure luaL_addchar(B : PluaL_Buffer; c : Char);
+function luaL_loadstring(L: Plua_State; const s: PAnsiChar): Integer; cdecl; external LUA_LIB_NAME;
+function luaL_getmetafield(L: Plua_State; obj: Integer; const e: PAnsiChar): Integer; cdecl; external LUA_LIB_NAME;
+function luaL_callmeta(L: Plua_State; obj: Integer; const e: PAnsiChar): Integer; cdecl; external LUA_LIB_NAME;
+function luaL_tolstring(L: Plua_State; idx: Integer; len: Psize_t): PAnsiChar; cdecl; external LUA_LIB_NAME;
+procedure luaL_requiref(L: Plua_State; const modname: PAnsiChar; openf: lua_CFunction; glb: LongBool); cdecl; external LUA_LIB_NAME;
+procedure luaL_setfuncs(L: Plua_State; lr: PluaL_Reg; nup: Integer); cdecl; external LUA_LIB_NAME;
+
+procedure luaL_setfuncs(L: Plua_State; lr: array of luaL_Reg; nup: Integer);
 begin
-    if not(B^.p < B^.buffer + LUAL_BUFFERSIZE) then
-        luaL_prepbuffer(B);
-    (B^.p^) := c;
-    Inc(B^.p);
+   luaL_setfuncs(L, @lr, nup);
 end;
 
-procedure luaL_putchar(B : PluaL_Buffer; c : Char);
+procedure luaL_newlibtable(L: Plua_State; lr: array of luaL_Reg);
 begin
-    luaL_addchar(B, c);
+   lua_createtable(L, 0, High(lr));
 end;
 
-procedure luaL_addsize(B : PluaL_Buffer; n : LongInt);
+procedure luaL_newlibtable(L: Plua_State; lr: PluaL_Reg);
+var
+  n: Integer;
 begin
-  Inc(B^.p, n);
+  n := 0;
+  while lr^.name <> nil do begin
+     inc(n);
+     inc(lr);
+  end;
+  lua_createtable(L, 0, n);
 end;
 
-function lua_ref(L : Plua_State; lock : Boolean) : LongInt;
+procedure luaL_newlib(L: Plua_State; lr: array of luaL_Reg);
 begin
-    if lock then
-        lua_ref := luaL_ref(L, LUA_REGISTRYINDEX)
-    else
-        begin
-        lua_pushstring(L, _P'unlocked references are obsolete');
-        lua_error(L);
-        lua_ref := 0;
-        end;
+   luaL_newlibtable(L, lr);
+   luaL_setfuncs(L, @lr, 0);
 end;
 
-procedure lua_unref(L : Plua_State; ref : LongInt);
+procedure luaL_newlib(L: Plua_State; lr: PluaL_Reg);
 begin
-    luaL_unref(L, LUA_REGISTRYINDEX, ref);
+   luaL_newlibtable(L, lr);
+   luaL_setfuncs(L, lr, 0);
 end;
 
-procedure lua_getref(L : Plua_State; ref : LongInt);
+function luaL_gsub(L: Plua_State; const s, p, r: PAnsiChar): PAnsiChar; cdecl; external LUA_LIB_NAME;
+function luaL_getsubtable(L: Plua_State; idx: Integer; const fname: PAnsiChar): Integer; cdecl; external LUA_LIB_NAME;
+function luaL_newstate: Plua_State; cdecl; external LUA_LIB_NAME;
+function luaL_len(L: Plua_State; idx: Integer): Integer; cdecl; external LUA_LIB_NAME;
+procedure luaL_checkversion_(L: Plua_State; ver: lua_Number); cdecl; external LUA_LIB_NAME;
+
+procedure luaL_checkversion(L: Plua_State);
 begin
-    lua_rawgeti(L, LUA_REGISTRYINDEX, ref);
+   luaL_checkversion_(L, LUA_VERSION_NUM);
 end;
 
+function luaopen_base(L: Plua_State): LongBool; cdecl; external LUA_LIB_NAME;
+function luaopen_coroutine(L: Plua_State): LongBool; cdecl; external LUA_LIB_NAME;
+function luaopen_table(L: Plua_State): LongBool; cdecl; external LUA_LIB_NAME;
+function luaopen_io(L: Plua_State): LongBool; cdecl; external LUA_LIB_NAME;
+function luaopen_os(L: Plua_State): LongBool; cdecl; external LUA_LIB_NAME;
+function luaopen_string(L: Plua_State): LongBool; cdecl; external LUA_LIB_NAME;
+function luaopen_bit32(L: Plua_State): LongBool; cdecl; external LUA_LIB_NAME;
+function luaopen_math(L: Plua_State): LongBool; cdecl; external LUA_LIB_NAME;
+function luaopen_debug(L: Plua_State): LongBool; cdecl; external LUA_LIB_NAME;
+function luaopen_package(L: Plua_State): LongBool; cdecl; external LUA_LIB_NAME;
+procedure luaL_openlibs(L: Plua_State); cdecl; external LUA_LIB_NAME;
+
+initialization
+{$IFDEF MSWINDOWS}
+   Set8087CW($133F);  // disable all floating-point exceptions
+{$ENDIF}
 
 (******************************************************************************
-* Original copyright for the lua source and headers:
-*  1994-2004 Tecgraf, PUC-Rio.
-*  www.lua.org.
-*
+* Copyright (C) 1994-2012 Lua.org, PUC-Rio.
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
diff -up hedgewars-src-0.9.18/hedgewars/uScript.pas.lua-52 hedgewars-src-0.9.18/hedgewars/uScript.pas
--- hedgewars-src-0.9.18/hedgewars/uScript.pas.lua-52	2012-11-02 13:41:54.000000000 -0400
+++ hedgewars-src-0.9.18/hedgewars/uScript.pas	2013-05-19 17:13:54.451050517 -0400
@@ -115,7 +115,7 @@ begin
         lua_pushnil(L);
         end
     else
-        lua_pushinteger(L, lua_tointeger(L, 2) and lua_tointeger(L, 1));
+        lua_pushinteger(L, lua_tointegerx(L, 2, NIL) and lua_tointegerx(L, 1, NIL));
     lc_band := 1;
 end;
 
@@ -127,7 +127,7 @@ begin
         lua_pushnil(L);
         end
     else
-        lua_pushinteger(L, lua_tointeger(L, 2) or lua_tointeger(L, 1));
+        lua_pushinteger(L, lua_tointegerx(L, 2, NIL) or lua_tointegerx(L, 1, NIL));
     lc_bor := 1;
 end;
 
@@ -139,7 +139,7 @@ begin
         lua_pushnil(L);
         end
     else
-        lua_pushinteger(L, not lua_tointeger(L, 1));
+        lua_pushinteger(L, not lua_tointegerx(L, 1, NIL));
     lc_bnot := 1;
 end;
 
@@ -151,7 +151,7 @@ begin
         lua_pushnil(L);
         end
     else
-        lua_pushinteger(L, lua_tointeger(L, 1) div lua_tointeger(L, 2));
+        lua_pushinteger(L, lua_tointegerx(L, 1, NIL) div lua_tointegerx(L, 2, NIL));
     lc_div := 1;
 end;
 
@@ -169,7 +169,7 @@ begin
     if lua_gettop(L) <> 1 then
         LuaError('Lua: Wrong number of parameters passed to SetInputMask!')
     else
-        InputMask:= lua_tointeger(L, 1);
+        InputMask:= lua_tointegerx(L, 1, NIL);
     lc_setinputmask:= 0
 end;
 
@@ -208,7 +208,7 @@ function lc_showmission(L : Plua_State)
 begin
     if lua_gettop(L) = 5 then
         begin
-        ShowMission(lua_tostring(L, 1), lua_tostring(L, 2), lua_tostring(L, 3), lua_tointeger(L, 4), lua_tointeger(L, 5));
+        ShowMission(lua_tostring(L, 1), lua_tostring(L, 2), lua_tostring(L, 3), lua_tointegerx(L, 4, NIL), lua_tointegerx(L, 5, NIL));
         end
     else
         LuaError('Lua: Wrong number of parameters passed to ShowMission!');
@@ -228,7 +228,7 @@ begin
         AddCaption(lua_tostring(L, 1), cWhiteColor, capgrpMessage)
     else if lua_gettop(L) = 3 then
         begin
-        AddCaption(lua_tostring(L, 1), lua_tointeger(L, 2) shr 8, TCapGroup(lua_tointeger(L, 3)));
+        AddCaption(lua_tostring(L, 1), lua_tointegerx(L, 2, NIL) shr 8, TCapGroup(lua_tointegerx(L, 3, NIL)));
         end
     else
         LuaError('Lua: Wrong number of parameters passed to AddCaption!');
@@ -267,7 +267,7 @@ begin
         end
     else
         begin
-        gear := SpawnFakeCrateAt(lua_tointeger(L, 1), lua_tointeger(L, 2),
+        gear := SpawnFakeCrateAt(lua_tointegerx(L, 1, NIL), lua_tointegerx(L, 2, NIL),
         HealthCrate, lua_toboolean(L, 3), lua_toboolean(L, 4));
         lua_pushinteger(L, gear^.uid);
         end;
@@ -284,7 +284,7 @@ begin
         end
     else
         begin
-        gear := SpawnFakeCrateAt(lua_tointeger(L, 1), lua_tointeger(L, 2),
+        gear := SpawnFakeCrateAt(lua_tointegerx(L, 1, NIL), lua_tointegerx(L, 2, NIL),
         AmmoCrate, lua_toboolean(L, 3), lua_toboolean(L, 4));
         lua_pushinteger(L, gear^.uid);
         end;
@@ -301,7 +301,7 @@ begin
         end
     else
         begin  
-        gear := SpawnFakeCrateAt(lua_tointeger(L, 1), lua_tointeger(L, 2),
+        gear := SpawnFakeCrateAt(lua_tointegerx(L, 1, NIL), lua_tointegerx(L, 2, NIL),
         UtilityCrate, lua_toboolean(L, 3), lua_toboolean(L, 4));
         lua_pushinteger(L, gear^.uid);
         end;
@@ -320,10 +320,10 @@ begin
     else
         begin
         if lua_gettop(L) = 3 then
-            health:= lua_tointeger(L, 3)
+            health:= lua_tointegerx(L, 3, NIL)
         else
             health:= cHealthCaseAmount;
-        gear := SpawnCustomCrateAt(lua_tointeger(L, 1), lua_tointeger(L, 2), HealthCrate, health, 0);
+        gear := SpawnCustomCrateAt(lua_tointegerx(L, 1, NIL), lua_tointegerx(L, 2, NIL), HealthCrate, health, 0);
         if gear <> nil then
             lua_pushinteger(L, gear^.uid)
         else
@@ -343,8 +343,8 @@ begin
     else
         begin
         if (lua_gettop(L) = 3) then 
-             gear := SpawnCustomCrateAt(lua_tointeger(L, 1), lua_tointeger(L, 2), AmmoCrate, lua_tointeger(L, 3), 0)
-        else gear := SpawnCustomCrateAt(lua_tointeger(L, 1), lua_tointeger(L, 2), AmmoCrate, lua_tointeger(L, 3), lua_tointeger(L, 4));
+             gear := SpawnCustomCrateAt(lua_tointegerx(L, 1, NIL), lua_tointegerx(L, 2, NIL), AmmoCrate, lua_tointegerx(L, 3, NIL), 0)
+        else gear := SpawnCustomCrateAt(lua_tointegerx(L, 1, NIL), lua_tointegerx(L, 2, NIL), AmmoCrate, lua_tointegerx(L, 3, NIL), lua_tointegerx(L, 4, NIL));
         if gear <> nil then
             lua_pushinteger(L, gear^.uid)
         else
@@ -364,8 +364,8 @@ begin
     else
         begin
         if (lua_gettop(L) = 3) then
-             gear := SpawnCustomCrateAt(lua_tointeger(L, 1), lua_tointeger(L, 2), UtilityCrate, lua_tointeger(L, 3), 0)
-        else gear := SpawnCustomCrateAt(lua_tointeger(L, 1), lua_tointeger(L, 2), UtilityCrate, lua_tointeger(L, 3), lua_tointeger(L, 4));
+             gear := SpawnCustomCrateAt(lua_tointegerx(L, 1, NIL), lua_tointegerx(L, 2, NIL), UtilityCrate, lua_tointegerx(L, 3, NIL), 0)
+        else gear := SpawnCustomCrateAt(lua_tointegerx(L, 1, NIL), lua_tointegerx(L, 2, NIL), UtilityCrate, lua_tointegerx(L, 3, NIL), lua_tointegerx(L, 4, NIL));
         if gear <> nil then
             lua_pushinteger(L, gear^.uid)
         else
@@ -387,13 +387,13 @@ begin
         end
     else
         begin
-        x:= lua_tointeger(L, 1);
-        y:= lua_tointeger(L, 2);
-        gt:= TGearType(lua_tointeger(L, 3));
-        s:= lua_tointeger(L, 4);
-        dx:= int2hwFloat(lua_tointeger(L, 5)) / 1000000;
-        dy:= int2hwFloat(lua_tointeger(L, 6)) / 1000000;
-        t:= lua_tointeger(L, 7);
+        x:= lua_tointegerx(L, 1, NIL);
+        y:= lua_tointegerx(L, 2, NIL);
+        gt:= TGearType(lua_tointegerx(L, 3, NIL));
+        s:= lua_tointegerx(L, 4, NIL);
+        dx:= int2hwFloat(lua_tointegerx(L, 5, NIL)) / 1000000;
+        dy:= int2hwFloat(lua_tointegerx(L, 6, NIL)) / 1000000;
+        t:= lua_tointegerx(L, 7, NIL);
 
         gear:= AddGear(x, y, gt, s, dx, dy, t);
         lastGearByUID:= gear;
@@ -411,7 +411,7 @@ begin
         end
     else
         begin
-        gear:= GearByUID(lua_tointeger(L, 1));
+        gear:= GearByUID(lua_tointegerx(L, 1, NIL));
         if gear <> nil then
             gear^.Message:= gear^.Message or gmDelete;
         end;
@@ -431,10 +431,10 @@ begin
         end
     else
         begin
-        x:= lua_tointeger(L, 1);
-        y:= lua_tointeger(L, 2);
-        vgt:= TVisualGearType(lua_tointeger(L, 3));
-        s:= lua_tointeger(L, 4);
+        x:= lua_tointegerx(L, 1, NIL);
+        y:= lua_tointegerx(L, 2, NIL);
+        vgt:= TVisualGearType(lua_tointegerx(L, 3, NIL));
+        s:= lua_tointegerx(L, 4, NIL);
         c:= lua_toboolean(L, 5);
 
         vg:= AddVisualGear(x, y, vgt, s, c);
@@ -458,7 +458,7 @@ begin
         end
     else
         begin
-        vg:= VisualGearByUID(lua_tointeger(L, 1));
+        vg:= VisualGearByUID(lua_tointegerx(L, 1, NIL));
         if vg <> nil then
             DeleteVisualGear(vg);
         end;
@@ -476,7 +476,7 @@ begin
         end
     else
         begin
-        vg:= VisualGearByUID(lua_tointeger(L, 1));
+        vg:= VisualGearByUID(lua_tointegerx(L, 1, NIL));
         if vg <> nil then
             begin
             lua_pushinteger(L, round(vg^.X));
@@ -509,20 +509,20 @@ begin
         end
     else
         begin
-        vg:= VisualGearByUID(lua_tointeger(L, 1));
+        vg:= VisualGearByUID(lua_tointegerx(L, 1, NIL));
         if vg <> nil then
             begin
-            vg^.X:= lua_tointeger(L, 2);
-            vg^.Y:= lua_tointeger(L, 3);
-            vg^.dX:= lua_tonumber(L, 4);
-            vg^.dY:= lua_tonumber(L, 5);
-            vg^.Angle:= lua_tonumber(L, 6);
-            vg^.Frame:= lua_tointeger(L, 7);
-            if lua_tointeger(L, 8) <> 0 then
-                vg^.FrameTicks:= lua_tointeger(L, 8);  // find a better way to do this. maybe need to break all these up.
-            vg^.State:= lua_tointeger(L, 9);
-            vg^.Timer:= lua_tointeger(L, 10);
-            vg^.Tint:= lua_tointeger(L, 11);
+            vg^.X:= lua_tointegerx(L, 2, NIL);
+            vg^.Y:= lua_tointegerx(L, 3, NIL);
+            vg^.dX:= lua_tonumberx(L, 4, NIL);
+            vg^.dY:= lua_tonumberx(L, 5, NIL);
+            vg^.Angle:= lua_tonumberx(L, 6, NIL);
+            vg^.Frame:= lua_tointegerx(L, 7, NIL);
+            if lua_tointegerx(L, 8, NIL) <> 0 then
+                vg^.FrameTicks:= lua_tointegerx(L, 8, NIL);  // find a better way to do this. maybe need to break all these up.
+            vg^.State:= lua_tointegerx(L, 9, NIL);
+            vg^.Timer:= lua_tointegerx(L, 10, NIL);
+            vg^.Tint:= lua_tointegerx(L, 11, NIL);
             end
         end;
     lc_setvisualgearvalues:= 0;
@@ -553,7 +553,7 @@ begin
         end
     else
         begin
-        gear:= GearByUID(lua_tointeger(L, 1));
+        gear:= GearByUID(lua_tointegerx(L, 1, NIL));
         if gear <> nil then
             lua_pushinteger(L, ord(gear^.Kind))
         else
@@ -572,7 +572,7 @@ begin
         end
     else
         begin
-        gear:= GearByUID(lua_tointeger(L, 1));
+        gear:= GearByUID(lua_tointegerx(L, 1, NIL));
         if gear <> nil then
             lua_pushinteger(L, gear^.message)
         else
@@ -591,7 +591,7 @@ begin
         end
     else
         begin
-        gear:= GearByUID(lua_tointeger(L, 1));
+        gear:= GearByUID(lua_tointegerx(L, 1, NIL));
         if gear <> nil then
             lua_pushinteger(L, hwRound(gear^.elasticity * _10000))
         else
@@ -607,9 +607,9 @@ begin
         LuaError('Lua: Wrong number of parameters passed to SetGearMessage!')
     else
         begin
-        gear:= GearByUID(lua_tointeger(L, 1));
+        gear:= GearByUID(lua_tointegerx(L, 1, NIL));
         if gear <> nil then
-            gear^.message:= lua_tointeger(L, 2);
+            gear^.message:= lua_tointegerx(L, 2, NIL);
         end;
     lc_setgearmessage:= 0
 end;
@@ -624,7 +624,7 @@ begin
         end
     else
         begin
-        gear:= GearByUID(lua_tointeger(L, 1));
+        gear:= GearByUID(lua_tointegerx(L, 1, NIL));
         if gear <> nil then
             lua_pushinteger(L, gear^.Pos)
         else
@@ -640,9 +640,9 @@ begin
         LuaError('Lua: Wrong number of parameters passed to SetGearPos!')
     else
         begin
-        gear:= GearByUID(lua_tointeger(L, 1));
+        gear:= GearByUID(lua_tointegerx(L, 1, NIL));
         if gear <> nil then
-            gear^.Pos:= lua_tointeger(L, 2);
+            gear^.Pos:= lua_tointegerx(L, 2, NIL);
         end;
     lc_setgearpos:= 0
 end;
@@ -657,7 +657,7 @@ begin
         end
     else
         begin
-        gear:= GearByUID(lua_tointeger(L, 1));
+        gear:= GearByUID(lua_tointegerx(L, 1, NIL));
         if gear <> nil then
             lua_pushinteger(L, gear^.CollisionMask)
         else
@@ -673,9 +673,9 @@ begin
         LuaError('Lua: Wrong number of parameters passed to SetGearCollisionMask!')
     else
         begin
-        gear:= GearByUID(lua_tointeger(L, 1));
+        gear:= GearByUID(lua_tointegerx(L, 1, NIL));
         if gear <> nil then
-            gear^.CollisionMask:= lua_tointeger(L, 2);
+            gear^.CollisionMask:= lua_tointegerx(L, 2, NIL);
         end;
     lc_setgearcollisionmask:= 0
 end;
@@ -687,7 +687,7 @@ begin
         LuaError('Lua: Wrong number of parameters passed to GetHogLevel!')
     else
         begin
-        gear := GearByUID(lua_tointeger(L, 1));
+        gear := GearByUID(lua_tointegerx(L, 1, NIL));
         if (gear <> nil) and (gear^.Kind = gtHedgehog) and (gear^.Hedgehog <> nil) then
             lua_pushinteger(L, gear^.Hedgehog^.BotLevel)
         else
@@ -703,9 +703,9 @@ begin
         LuaError('Lua: Wrong number of parameters passed to SetHogLevel!')
     else
         begin
-        gear:= GearByUID(lua_tointeger(L, 1));
+        gear:= GearByUID(lua_tointegerx(L, 1, NIL));
         if (gear <> nil) and (gear^.Kind = gtHedgehog) and (gear^.Hedgehog <> nil) then
-            gear^.Hedgehog^.BotLevel:= lua_tointeger(L, 2);
+            gear^.Hedgehog^.BotLevel:= lua_tointegerx(L, 2, NIL);
         end;
     lc_sethoglevel:= 0
 end;
@@ -720,7 +720,7 @@ begin
         end
     else
         begin
-        gear:= GearByUID(lua_tointeger(L, 1));
+        gear:= GearByUID(lua_tointegerx(L, 1, NIL));
         if (gear <> nil) and (gear^.Kind = gtHedgehog) and (gear^.Hedgehog <> nil) then
             begin
             lua_pushinteger(L, gear^.Hedgehog^.Team^.Clan^.ClanIndex)
@@ -738,7 +738,7 @@ begin
         LuaError('Lua: Wrong number of parameters passed to GetClanColor!');
         lua_pushnil(L); // return value on stack (nil)
         end
-    else lua_pushinteger(L, ClansArray[lua_tointeger(L, 1)]^.Color shl 8 or $FF);
+    else lua_pushinteger(L, ClansArray[lua_tointegerx(L, 1, NIL)]^.Color shl 8 or $FF);
     lc_getclancolor:= 1
 end;
 
@@ -754,8 +754,8 @@ begin
         LuaError('Lua: Wrong number of parameters passed to SetClanColor!')
     else
         begin
-        clan := ClansArray[lua_tointeger(L, 1)];
-        clan^.Color:= lua_tointeger(L, 2) shr 8;
+        clan := ClansArray[lua_tointegerx(L, 1, NIL)];
+        clan^.Color:= lua_tointegerx(L, 2, NIL) shr 8;
         for i:= 0 to Pred(clan^.TeamsNumber) do
             begin
             team:= clan^.Teams[i];
@@ -802,7 +802,7 @@ begin
         end
     else
         begin
-        gear:= GearByUID(lua_tointeger(L, 1));
+        gear:= GearByUID(lua_tointegerx(L, 1, NIL));
         if (gear <> nil) and (gear^.Kind = gtHedgehog) and (gear^.Hedgehog <> nil) then
             begin
             lua_pushstring(L, str2pchar(gear^.Hedgehog^.Team^.TeamName))
@@ -823,7 +823,7 @@ begin
         end
     else
         begin
-        gear:= GearByUID(lua_tointeger(L, 1));
+        gear:= GearByUID(lua_tointegerx(L, 1, NIL));
         if (gear <> nil) and (gear^.Kind = gtHedgehog) and (gear^.Hedgehog <> nil) then
             begin
             lua_pushstring(L, str2pchar(gear^.Hedgehog^.Name))
@@ -845,7 +845,7 @@ begin
         end
     else
         begin
-        gear:= GearByUID(lua_tointeger(L, 1));
+        gear:= GearByUID(lua_tointegerx(L, 1, NIL));
         if (gear <> nil) and (gear^.Kind = gtHedgehog) and (gear^.Hedgehog <> nil) then
 
         hogName:= lua_tostring(L, 2);
@@ -868,7 +868,7 @@ begin
         end
     else
         begin
-        gear:= GearByUID(lua_tointeger(L, 1));
+        gear:= GearByUID(lua_tointegerx(L, 1, NIL));
         if gear <> nil then
             lua_pushinteger(L, gear^.Timer)
         else
@@ -887,7 +887,7 @@ begin
         end
     else
         begin
-        gear:= GearByUID(lua_tointeger(L, 1));
+        gear:= GearByUID(lua_tointegerx(L, 1, NIL));
         if gear <> nil then
             lua_pushinteger(L, gear^.Health)
         else
@@ -906,7 +906,7 @@ begin
         end
     else
         begin
-        gear:= GearByUID(lua_tointeger(L, 1));
+        gear:= GearByUID(lua_tointegerx(L, 1, NIL));
         if gear <> nil then
             lua_pushinteger(L, hwRound(gear^.X))
         else
@@ -925,7 +925,7 @@ begin
         end
     else
         begin
-        gear:= GearByUID(lua_tointeger(L, 1));
+        gear:= GearByUID(lua_tointegerx(L, 1, NIL));
         if gear <> nil then
             lua_pushinteger(L, hwRound(gear^.Y))
         else
@@ -943,8 +943,8 @@ begin
         end
     else
         begin
-        gears:= GearByUID(lua_tointeger(L, 1));
-        geard:= GearByUID(lua_tointeger(L, 2));
+        gears:= GearByUID(lua_tointegerx(L, 1, NIL));
+        geard:= GearByUID(lua_tointegerx(L, 2, NIL));
         if (gears <> nil) and (geard <> nil) then
             begin
             geard^.X:= gears^.X;
@@ -963,7 +963,7 @@ begin
         LuaError('Lua: Wrong number of parameters passed to FollowGear!')
     else
         begin
-        gear:= GearByUID(lua_tointeger(L, 1));
+        gear:= GearByUID(lua_tointegerx(L, 1, NIL));
         if gear <> nil then FollowGear:= gear
         end;
     lc_followgear:= 0
@@ -975,13 +975,13 @@ var gear : PGear;
        s : LongWord;
 begin
     if lua_gettop(L) = 4 then
-        s:= lua_tointeger(L, 4)
+        s:= lua_tointegerx(L, 4, NIL)
     else
         s:= 0;
 
     if (lua_gettop(L) = 4) or (lua_gettop(L) = 3) then
         begin
-        gear:= GearByUID(lua_tointeger(L, 1));
+        gear:= GearByUID(lua_tointegerx(L, 1, NIL));
         if gear <> nil then
             begin
             vgear:= AddVisualGear(0, 0, vgtSpeechBubble, s, true);
@@ -989,7 +989,7 @@ begin
                begin
                vgear^.Text:= lua_tostring(L, 2);
                vgear^.Hedgehog:= gear^.Hedgehog;
-               vgear^.FrameTicks:= lua_tointeger(L, 3);
+               vgear^.FrameTicks:= lua_tointegerx(L, 3, NIL);
                if (vgear^.FrameTicks < 1) or (vgear^.FrameTicks > 3) then
                    vgear^.FrameTicks:= 1;
                lua_pushinteger(L, vgear^.Uid)
@@ -1009,7 +1009,7 @@ begin
         LuaError('Lua: Wrong number of parameters passed to SwitchHog!')
     else
         begin
-        gear:= GearByUID(lua_tointeger(L, 1));
+        gear:= GearByUID(lua_tointegerx(L, 1, NIL));
 // should we allow this when there is no current hedgehog? might do some odd(er) things to turn sequence.
         if (gear <> nil) and (gear^.Kind = gtHedgehog) and (gear^.Hedgehog <> nil) and (CurrentHedgehog <> nil) then
             begin
@@ -1040,16 +1040,16 @@ begin
 
     if lua_gettop(L) = 3 then
     begin
-    gear:= GearByUID(lua_tointeger(L, 1));
+    gear:= GearByUID(lua_tointegerx(L, 1, NIL));
         if (gear <> nil) and (gear^.Hedgehog <> nil) then
-            AddAmmoAmount(gear^.Hedgehog^, TAmmoType(lua_tointeger(L, 2)), lua_tointeger(L,3) );
+            AddAmmoAmount(gear^.Hedgehog^, TAmmoType(lua_tointegerx(L, 2, NIL)), lua_tointegerx(L,3,NIL) );
     end else
     
     if lua_gettop(L) = 2 then
     begin
-    gear:= GearByUID(lua_tointeger(L, 1));
+    gear:= GearByUID(lua_tointegerx(L, 1, NIL));
         if (gear <> nil) and (gear^.Hedgehog <> nil) then
-            AddAmmo(gear^.Hedgehog^, TAmmoType(lua_tointeger(L, 2)));
+            AddAmmo(gear^.Hedgehog^, TAmmoType(lua_tointegerx(L, 2, NIL)));
     end else
     begin
         LuaError('Lua: Wrong number of parameters passed to AddAmmo!');
@@ -1064,12 +1064,12 @@ var gear : PGear;
 begin
     if (lua_gettop(L) = 3) or (lua_gettop(L) = 2) then
         begin
-        gear:= GearByUID(lua_tointeger(L, 1));
+        gear:= GearByUID(lua_tointegerx(L, 1, NIL));
         if (gear <> nil) and (gear^.Hedgehog <> nil) then
             if lua_gettop(L) = 2 then
-                AddAmmo(gear^.Hedgehog^, TAmmoType(lua_tointeger(L, 2)))
+                AddAmmo(gear^.Hedgehog^, TAmmoType(lua_tointegerx(L, 2, NIL)))
             else
-                SetAmmo(gear^.Hedgehog^, TAmmoType(lua_tointeger(L, 2)), lua_tointeger(L, 3))
+                SetAmmo(gear^.Hedgehog^, TAmmoType(lua_tointegerx(L, 2, NIL)), lua_tointegerx(L, 3, NIL))
         end
     else LuaError('Lua: Wrong number of parameters passed to AddAmmo!');
     lc_addammo:= 0
@@ -1081,10 +1081,10 @@ var gear : PGear;
 begin
     if (lua_gettop(L) = 2) then
         begin
-        gear:= GearByUID(lua_tointeger(L, 1));
+        gear:= GearByUID(lua_tointegerx(L, 1, NIL));
         if (gear <> nil) and (gear^.Hedgehog <> nil) then 
             begin
-            ammo:= GetAmmoEntry(gear^.Hedgehog^, TAmmoType(lua_tointeger(L, 2)));
+            ammo:= GetAmmoEntry(gear^.Hedgehog^, TAmmoType(lua_tointegerx(L, 2, NIL)));
             if ammo^.AmmoType = amNothing then
                 lua_pushinteger(L, 0)
             else
@@ -1109,10 +1109,10 @@ begin
         end
     else
         begin
-        gear:= GearByUID(lua_tointeger(L, 1));
+        gear:= GearByUID(lua_tointegerx(L, 1, NIL));
         if gear <> nil then
             begin
-            gear^.Health:= lua_tointeger(L, 2);
+            gear^.Health:= lua_tointegerx(L, 2, NIL);
 
         if (gear^.Kind = gtHedgehog) and (gear^.Hedgehog <> nil) then
             begin  
@@ -1135,8 +1135,8 @@ begin
         end
     else
         begin
-        gear:= GearByUID(lua_tointeger(L, 1));
-        if gear <> nil then gear^.Timer:= lua_tointeger(L, 2)
+        gear:= GearByUID(lua_tointegerx(L, 1, NIL));
+        if gear <> nil then gear^.Timer:= lua_tointegerx(L, 2, NIL)
         end;
     lc_settimer:= 0
 end;
@@ -1147,9 +1147,9 @@ begin
     if lua_gettop(L) <> 3 then
         LuaError('Lua: Wrong number of parameters passed to SetEffect!')
     else begin
-        gear := GearByUID(lua_tointeger(L, 1));
+        gear := GearByUID(lua_tointegerx(L, 1, NIL));
         if (gear <> nil) and (gear^.Hedgehog <> nil) then
-            gear^.Hedgehog^.Effects[THogEffect(lua_tointeger(L, 2))]:= lua_tointeger(L, 3);
+            gear^.Hedgehog^.Effects[THogEffect(lua_tointegerx(L, 2, NIL))]:= lua_tointegerx(L, 3, NIL);
     end;
     lc_seteffect := 0;
 end;
@@ -1162,9 +1162,9 @@ begin
         end
     else
         begin
-        gear:= GearByUID(lua_tointeger(L, 1));
+        gear:= GearByUID(lua_tointegerx(L, 1, NIL));
         if (gear <> nil) and (gear^.Hedgehog <> nil) then
-            lua_pushinteger(L, gear^.Hedgehog^.Effects[THogEffect(lua_tointeger(L, 2))])
+            lua_pushinteger(L, gear^.Hedgehog^.Effects[THogEffect(lua_tointegerx(L, 2, NIL))])
         else
             lua_pushinteger(L, 0)
         end;
@@ -1180,10 +1180,10 @@ begin
         end
     else
         begin
-        gear:= GearByUID(lua_tointeger(L, 1));
+        gear:= GearByUID(lua_tointegerx(L, 1, NIL));
         if gear <> nil then
             begin
-            gear^.State:= lua_tointeger(L, 2);
+            gear^.State:= lua_tointegerx(L, 2, NIL);
             SetAllToActive;
             end
         end;
@@ -1199,7 +1199,7 @@ begin
         end
     else
         begin
-        gear:= GearByUID(lua_tointeger(L, 1));
+        gear:= GearByUID(lua_tointegerx(L, 1, NIL));
         if gear <> nil then
             lua_pushinteger(L, gear^.State)
         else
@@ -1218,7 +1218,7 @@ begin
         end
     else
         begin
-        gear:= GearByUID(lua_tointeger(L, 1));
+        gear:= GearByUID(lua_tointegerx(L, 1, NIL));
         if gear <> nil then
             lua_pushinteger(L, gear^.Tag)
         else
@@ -1236,10 +1236,10 @@ begin
         end
     else
         begin
-        gear:= GearByUID(lua_tointeger(L, 1));
+        gear:= GearByUID(lua_tointegerx(L, 1, NIL));
         if gear <> nil then
             begin
-            gear^.Tag:= lua_tointeger(L, 2);
+            gear^.Tag:= lua_tointegerx(L, 2, NIL);
             SetAllToActive;
             end
         end;
@@ -1264,10 +1264,10 @@ begin
         LuaError('Lua: Wrong number of parameters passed to FindPlace!')
     else
         begin
-        gear:= GearByUID(lua_tointeger(L, 1));
+        gear:= GearByUID(lua_tointegerx(L, 1, NIL));
         fall:= lua_toboolean(L, 2);
-        left:= lua_tointeger(L, 3);
-        right:= lua_tointeger(L, 4);
+        left:= lua_tointegerx(L, 3, NIL);
+        right:= lua_tointegerx(L, 4, NIL);
         if lua_gettop(L) = 5 then
             tryhard:= lua_toboolean(L, 5);
         if gear <> nil then
@@ -1284,12 +1284,12 @@ function lc_playsound(L : Plua_State) :
 var gear: PGear;
 begin
     if lua_gettop(L) = 1 then
-        PlaySound(TSound(lua_tointeger(L, 1)))
+        PlaySound(TSound(lua_tointegerx(L, 1, NIL)))
     else if lua_gettop(L) = 2 then
         begin
-        gear:= GearByUID(lua_tointeger(L, 2));
+        gear:= GearByUID(lua_tointegerx(L, 2, NIL));
         if (gear <> nil) and (gear^.Kind = gtHedgehog) and (gear^.Hedgehog <> nil) then
-            AddVoice(TSound(lua_tointeger(L, 1)),gear^.Hedgehog^.Team^.Voicepack)
+            AddVoice(TSound(lua_tointegerx(L, 1, NIL)),gear^.Hedgehog^.Team^.Voicepack)
         end
     else LuaError('Lua: Wrong number of parameters passed to PlaySound!');
     lc_playsound:= 0;
@@ -1345,7 +1345,7 @@ begin
         end
     else
         begin
-        gear:= GearByUID(lua_tointeger(L, 1));
+        gear:= GearByUID(lua_tointegerx(L, 1, NIL));
         if gear <> nil then
             gear^.dX.isNegative:= lua_toboolean(L, 2);
         end;
@@ -1363,7 +1363,7 @@ begin
         end
     else
         begin
-        gear:= GearByUID(lua_tointeger(L, 1));
+        gear:= GearByUID(lua_tointegerx(L, 1, NIL));
         if gear <> nil then
             begin
             lua_pushinteger(L, hwRound(gear^.X));
@@ -1387,12 +1387,12 @@ begin
         LuaError('Lua: Wrong number of parameters passed to SetGearPosition!')
     else
         begin
-        gear:= GearByUID(lua_tointeger(L, 1));
+        gear:= GearByUID(lua_tointegerx(L, 1, NIL));
         if gear <> nil then
             begin
             col:= gear^.CollisionIndex >= 0;
-            x:= lua_tointeger(L, 2);
-            y:= lua_tointeger(L, 3);
+            x:= lua_tointegerx(L, 2, NIL);
+            y:= lua_tointegerx(L, 3, NIL);
             if col then
                 DeleteCI(gear);
             gear^.X:= int2hwfloat(x);
@@ -1416,7 +1416,7 @@ begin
         end
     else
         begin
-        gear:= GearByUID(lua_tointeger(L, 1));
+        gear:= GearByUID(lua_tointegerx(L, 1, NIL));
         if gear <> nil then
             begin
             lua_pushinteger(L, gear^.Target.X);
@@ -1438,11 +1438,11 @@ begin
         LuaError('Lua: Wrong number of parameters passed to SetGearTarget!')
     else
         begin
-        gear:= GearByUID(lua_tointeger(L, 1));
+        gear:= GearByUID(lua_tointegerx(L, 1, NIL));
         if gear <> nil then
             begin
-            gear^.Target.X:= lua_tointeger(L, 2);
-            gear^.Target.Y:= lua_tointeger(L, 3)
+            gear^.Target.X:= lua_tointegerx(L, 2, NIL);
+            gear^.Target.Y:= lua_tointegerx(L, 3, NIL)
             end
         end;
     lc_setgeartarget:= 0
@@ -1460,7 +1460,7 @@ begin
         end
     else
         begin
-        gear:= GearByUID(lua_tointeger(L, 1));
+        gear:= GearByUID(lua_tointegerx(L, 1, NIL));
         if gear <> nil then
             begin
             t:= hwRound(gear^.dX * 1000000);
@@ -1480,11 +1480,11 @@ begin
         LuaError('Lua: Wrong number of parameters passed to SetGearVelocity!')
     else
         begin
-        gear:= GearByUID(lua_tointeger(L, 1));
+        gear:= GearByUID(lua_tointegerx(L, 1, NIL));
         if gear <> nil then
             begin
-            gear^.dX:= int2hwFloat(lua_tointeger(L, 2)) / 1000000;
-            gear^.dY:= int2hwFloat(lua_tointeger(L, 3)) / 1000000;
+            gear^.dX:= int2hwFloat(lua_tointegerx(L, 2, NIL)) / 1000000;
+            gear^.dY:= int2hwFloat(lua_tointegerx(L, 3, NIL)) / 1000000;
             SetAllToActive;
             end
         end;
@@ -1497,7 +1497,7 @@ begin
         LuaError('Lua: Wrong number of parameters passed to SetZoom!')
     else
         begin
-        ZoomValue:= lua_tonumber(L, 1);
+        ZoomValue:= lua_tonumberx(L, 1, NIL);
         if ZoomValue < cMaxZoomLevel then
             ZoomValue:= cMaxZoomLevel;
         if ZoomValue > cMinZoomLevel then
@@ -1525,9 +1525,9 @@ begin
     if (np < 4) or (np > 5) then
         LuaError('Lua: Wrong number of parameters passed to SetAmmo!')
     else if np = 4 then
-        ScriptSetAmmo(TAmmoType(lua_tointeger(L, 1)), lua_tointeger(L, 2), lua_tointeger(L, 3), lua_tointeger(L, 4), 1)
+        ScriptSetAmmo(TAmmoType(lua_tointegerx(L, 1, NIL)), lua_tointegerx(L, 2, NIL), lua_tointegerx(L, 3, NIL), lua_tointegerx(L, 4, NIL), 1)
     else
-        ScriptSetAmmo(TAmmoType(lua_tointeger(L, 1)), lua_tointeger(L, 2), lua_tointeger(L, 3), lua_tointeger(L, 4), lua_tointeger(L, 5));
+        ScriptSetAmmo(TAmmoType(lua_tointegerx(L, 1, NIL)), lua_tointegerx(L, 2, NIL), lua_tointegerx(L, 3, NIL), lua_tointegerx(L, 4, NIL), lua_tointegerx(L, 5, NIL));
     lc_setammo:= 0
 end;
 
@@ -1557,7 +1557,7 @@ begin
         end
     else
         begin
-        m:= lua_tointeger(L, 1);
+        m:= lua_tointegerx(L, 1, NIL);
         if (m > 0) then
             lua_pushinteger(L, GetRandom(m))
         else
@@ -1575,7 +1575,7 @@ begin
         LuaError('Lua: Wrong number of parameters passed to SetWind!')
     else
         begin
-        cWindSpeed:= int2hwfloat(lua_tointeger(L, 1)) / 100 * cMaxWindSpeed;
+        cWindSpeed:= int2hwfloat(lua_tointegerx(L, 1, NIL)) / 100 * cMaxWindSpeed;
         cWindSpeedf:= SignAs(cWindSpeed,cWindSpeed).QWordValue / SignAs(_1,_1).QWordValue;
         if cWindSpeed.isNegative then
             CWindSpeedf := -cWindSpeedf;
@@ -1630,7 +1630,7 @@ begin
         end
     else
         begin
-        gear:= GearByUID(lua_tointeger(L, 1));
+        gear:= GearByUID(lua_tointegerx(L, 1, NIL));
         if gear <> nil then
             lua_pushinteger(L, gear^.Radius)
         else
@@ -1645,7 +1645,7 @@ begin
     if lua_gettop(L) <> 1 then
         LuaError('Lua: Wrong number of parameters passed to GetHogHat!')
     else begin
-        gear := GearByUID(lua_tointeger(L, 1));
+        gear := GearByUID(lua_tointegerx(L, 1, NIL));
         if (gear <> nil) and (gear^.Kind = gtHedgehog) and (gear^.Hedgehog <> nil) then
             lua_pushstring(L, str2pchar(gear^.Hedgehog^.Hat))
         else
@@ -1665,7 +1665,7 @@ begin
         end
     else
         begin
-        gear:= GearByUID(lua_tointeger(L, 1));
+        gear:= GearByUID(lua_tointegerx(L, 1, NIL));
         if (gear <> nil) and (gear^.Kind = gtHedgehog) and (gear^.Hedgehog <> nil) then
             hat:= lua_tostring(L, 2);
             gear^.Hedgehog^.Hat:= hat;
@@ -1684,9 +1684,9 @@ begin
         LuaError('Lua: Wrong number of parameters passed to PlaceGirder!')
     else
         TryPlaceOnLand(
-            lua_tointeger(L, 1) - SpritesData[sprAmGirder].Width div 2,
-            lua_tointeger(L, 2) - SpritesData[sprAmGirder].Height div 2,
-            sprAmGirder, lua_tointeger(L, 3), true, false);
+            lua_tointegerx(L, 1, NIL) - SpritesData[sprAmGirder].Width div 2,
+            lua_tointegerx(L, 2, NIL) - SpritesData[sprAmGirder].Height div 2,
+            sprAmGirder, lua_tointegerx(L, 3, NIL), true, false);
     lc_placegirder:= 0
 end;
 
@@ -1726,7 +1726,7 @@ begin
         LuaError('Lua: Wrong number of parameters passed to HideHog!')
     else
         begin
-        gear:= GearByUID(lua_tointeger(L, 1));
+        gear:= GearByUID(lua_tointegerx(L, 1, NIL));
         hiddenHedgehogs[hiddenHedgehogsNumber]:=gear^.hedgehog;
         inc(hiddenHedgehogsNumber);
         HideHog(gear^.hedgehog);
@@ -1745,7 +1745,7 @@ begin
           i := 0;
           while (i < hiddenHedgehogsNumber) do
             begin
-            if hiddenHedgehogs[i]^.gearHidden^.uid = LongWord(lua_tointeger(L, 1)) then
+            if hiddenHedgehogs[i]^.gearHidden^.uid = LongWord(lua_tointegerx(L, 1, NIL)) then
               begin
                 hog := hiddenHedgehogs[i];
                 RestoreHog(hog);
@@ -1773,10 +1773,10 @@ begin
     else
         begin
         rtn:= TestRectancleForObstacle(
-                    lua_tointeger(L, 1),
-                    lua_tointeger(L, 2),
-                    lua_tointeger(L, 3),
-                    lua_tointeger(L, 4),
+                    lua_tointegerx(L, 1, NIL),
+                    lua_tointegerx(L, 2, NIL),
+                    lua_tointegerx(L, 3, NIL),
+                    lua_tointegerx(L, 4, NIL),
                     lua_toboolean(L, 5)
                     );
         lua_pushboolean(L, rtn);
@@ -1825,7 +1825,7 @@ end;
 function ScriptGetInteger(name : shortstring) : LongInt;
 begin
     lua_getglobal(luaState, Str2PChar(name));
-    ScriptGetInteger:= lua_tointeger(luaState, -1);
+    ScriptGetInteger:= lua_tointegerx(luaState, -1, NIL);
     lua_pop(luaState, 1);
 end;
 
@@ -1966,7 +1966,7 @@ if not FileExists(s) then
 if not FileExists(s) then
     exit;
 
-ret:= luaL_loadfile(luaState, Str2PChar(s));
+ret:= luaL_loadfilex(luaState, Str2PChar(s), NIL);
 if ret <> 0 then
     begin
     LuaError('Lua: Failed to load ' + name + '(error ' + IntToStr(ret) + ')');
@@ -1976,7 +1976,7 @@ else
     begin
     WriteLnToConsole('Lua: ' + name + ' loaded');
     // call the script file
-    lua_pcall(luaState, 0, 0, 0);
+    lua_pcallk(luaState, 0, 0, 0, 0, NIL);
     ScriptLoaded:= true
     end
 end;
@@ -2013,7 +2013,7 @@ if not ScriptLoaded or (not ScriptExists
     exit;
 SetGlobals;
 lua_getglobal(luaState, Str2PChar(fname));
-if lua_pcall(luaState, 0, 0, 0) <> 0 then
+if lua_pcallk(luaState, 0, 0, 0, 0, NIL) <> 0 then
     begin
     LuaError('Lua: Error while calling ' + fname + ': ' + lua_tostring(luaState, -1));
     lua_pop(luaState, 1)
@@ -2030,7 +2030,7 @@ if not ScriptExists('ParseCommandOverrid
 lua_getglobal(luaState, Str2PChar('ParseCommandOverride'));
 lua_pushstring(luaState, Str2PChar(key));
 lua_pushstring(luaState, Str2PChar(value));
-if lua_pcall(luaState, 2, 1, 0) <> 0 then
+if lua_pcallk(luaState, 2, 1, 0, 0, NIL) <> 0 then
     begin
     LuaError('Lua: Error while calling ParseCommandOverride: ' + lua_tostring(luaState, -1));
     lua_pop(luaState, 1)
@@ -2069,14 +2069,14 @@ lua_pushinteger(luaState, par2);
 lua_pushinteger(luaState, par3);
 lua_pushinteger(luaState, par4);
 ScriptCall:= 0;
-if lua_pcall(luaState, 4, 1, 0) <> 0 then
+if lua_pcallk(luaState, 4, 1, 0, 0, NIL) <> 0 then
     begin
     LuaError('Lua: Error while calling ' + fname + ': ' + lua_tostring(luaState, -1));
     lua_pop(luaState, 1)
     end
 else
     begin
-    ScriptCall:= lua_tointeger(luaState, -1);
+    ScriptCall:= lua_tointegerx(luaState, -1, NIL);
     lua_pop(luaState, 1)
     end;
 GetGlobals;
@@ -2184,8 +2184,8 @@ var at : TGearType;
     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);