delphi调用LUA函数来处理一些逻辑

替同事做了个洛奇英雄传自动染色程序,关于屏幕取色的.

因为里面他对颜色的要求比较复杂,改动也比较大,于是我让他把逻辑写在 lua 脚本里面.

[delphi] view plain copy
 
  1. uses LUA, LUALIB;  
[delphi] view plain copy
 
  1. function lua_CheckColor(r,g,b:Integer):Boolean;  
  2. var  
  3. Lua : TLua;  
  4. begin  
  5.   Lua := TLua.Create;   
  6.   luaopen_debug(LuaInstance); //如果要使用debug库  
  7. //  luaopen_io(LuaInstance);  
  8.   luaopen_math(LuaInstance);// 如果要使用math库 不然就会attempt to index global 'math' (a nil value)  
  9.   luaopen_os(LuaInstance);  
  10. //  luaopen_package(LuaInstance);  
  11.   luaopen_string(LuaInstance);  
  12.   luaopen_table(LuaInstance);  
  13.   Lua.DoFile('lua_GetColor.lua');  
  14.   lua_getglobal(Lua.LuaInstance,'nogi_GetColor');  
  15.   lua_pushnumber(Lua.LuaInstance, r); //将脚本中add函数使用的参数压栈  
  16.   lua_pushnumber(Lua.LuaInstance, g); //将脚本中add函数使用的参数压栈  
  17.   lua_pushnumber(Lua.LuaInstance, b); //将脚本中add函数使用的参数压栈  
  18.   
  19.   
  20.   lua_pcall(Lua.LuaInstance, 3, 1,0) ;  
  21.   
  22.   
  23.   Result := (lua_toInteger(Lua.LuaInstance,-1) = 1);  
  24.   Lua.Free;  
  25. end;  


LUA 里面的内容是这样的

[delphi] view plain copy
 
  1. function nogi_GetColor(nR,nG,nB)  
  2.     if nR <= 25 and nG <= 25 and nB <= 25 then -- 取出25以下黑色  
  3.         return 1;  
  4.     end;  
  5.       
  6.     return 0;  
  7. end  



附上我这里带的 LUA.PAS 和 LUALIB.PAS

lua.pas

[delphi] view plain copy
 
  1. /** 
  2.  * @package     Delphi Lua 
  3.  * @copyright   Copyright (c) 2009 Dennis D. Spreen (http://www.spreendigital.de/blog) 
  4.  * @license     http://opensource.org/licenses/gpl-license.php GNU Public License 
  5.  * @author      Dennis D. Spreen <dennis@spreendigital.de> 
  6.  * @version     1.3 
  7.  * @revision    $Id: Lua.pas 102 2009-09-30 11:39:41Z dennis.spreen $ 
  8.  */ 
  9.  
  10. History 
  11. 1.3     DS      Improved Callback, now uses pointer instead of object index 
  12.                 Modified RegisterFunctions to allow methods from other class 
  13.                 to be registered, moved object table into TLua class 
  14. 1.2 DS  Added example on how to extend lua with a delphi dll 
  15. 1.1     DS      Improved global object table, this optimizes the delphi 
  16.                 function calls 
  17. 1.0     DS      Initial Release 
  18.  
  19. Copyright 2009  Dennis D. Spreen (email : dennis@spreendigital.de) 
  20.  
  21. This program is free software; you can redistribute it and/or modify 
  22. it under the terms of the GNU General Public License as published by 
  23. the Free Software Foundation; either version 2 of the License, or 
  24. (at your option) any later version. 
  25.  
  26. This program is distributed in the hope that it will be useful, 
  27. but WITHOUT ANY WARRANTY; without even the implied warranty of 
  28. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
  29. GNU General Public License for more details. 
  30.  
  31. You should have received a copy of the GNU General Public License 
  32. along with this program; if not, write to the Free Software 
  33. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA 
  34. }  
  35.   
  36. unit Lua;  
  37.   
  38. interface  
  39.   
  40. uses  
  41.   Classes,  
  42.   LuaLib;  
  43.   
  44. type  
  45.   TLuaState = Lua_State;  
  46.   
  47.   TLua = class(TObject)  
  48.   private  
  49.     fAutoRegister: Boolean;  
  50.     CallbackList: TList;  // internal callback list  
  51.   public  
  52.     LuaInstance: TLuaState;  // Lua instance  
  53.     constructor Create(AutoRegister: Boolean = True); overload; virtual;  
  54.     destructor Destroy; override;  
  55.     function DoFile(Filename: String): Integer; virtual;// load file and execute  
  56.     procedure RegisterFunction(FuncName: AnsiString; MethodName: AnsiString = ''; Obj: TObject = NIL); virtual; //register function  
  57.     procedure AutoRegisterFunctions(Obj: TObject);  // register all published functions  
  58.     procedure UnregisterFunctions(Obj: TObject); // unregister all object functions  
  59.   end;  
  60.   
  61. implementation  
  62.   
  63. type  
  64.   TProc = function(L: TLuaState): Integer of object; // Lua Function  
  65.   
  66.   TCallback = class  
  67.     Routine: TMethod;  // Code and Data for the method  
  68.     Exec: TProc;       // Resulting execution function  
  69.   end;  
  70.   
  71. //  
  72. // This function is called by Lua, it extracts the object by  
  73. // pointer to the objects method by name, which is then called.  
  74. //  
  75. // @param       Lua_State   L   Pointer to Lua instance  
  76. // @return      Integer         Number of result arguments on stack  
  77. //  
  78. function LuaCallBack(L: Lua_State): Integer; cdecl;  
  79. var  
  80.   CallBack: TCallBack;       // The Object stored in the Object Table  
  81. begin  
  82.   // Retrieve first Closure Value (=Object Pointer)  
  83.   CallBack := lua_topointer(L, lua_upvalueindex(1));  
  84.   
  85.   // Execute only if Object is valid  
  86.   if (assigned(CallBack) and assigned(CallBack.Exec)) then  
  87.     Result := CallBack.Exec(L)  
  88.   else  
  89.     Result := 0;  
  90. end;  
  91.   
  92. { TLua }  
  93.   
  94. //  
  95. // Create a new Lua instance and optionally create Lua functions  
  96. //  
  97. // @param       Boolean      AutoRegister       (optional)  
  98. // @return      TLua                            Lua Instance  
  99. //  
  100. constructor TLua.Create(AutoRegister: Boolean = True);  
  101. begin  
  102.   inherited Create;  
  103.   // Load Lua Lib if not already done  
  104.   if (not LuaLibLoaded) then  
  105.     LoadLuaLib;  
  106.   
  107.   // Open Library  
  108.   LuaInstance := Lua_Open();  
  109.   luaopen_base(LuaInstance);  
  110.   
  111.   fAutoRegister := AutoRegister;  
  112.   
  113.   // Create Object List on initialization  
  114.   CallBackList := TList.Create;  
  115.   
  116.   // if set then register published functions  
  117.   if (AutoRegister) then  
  118.     AutoRegisterFunctions(self);  
  119. end;  
  120.   
  121. //  
  122. // Dispose Lua instance  
  123. //  
  124. destructor TLua.Destroy;  
  125. begin  
  126.   // Unregister all functions if previously autoregistered  
  127.   if (fAutoRegister) then  
  128.     UnregisterFunctions(Self);  
  129.   
  130.   // dispose Object List on finalization  
  131.   CallBackList.Free;  
  132.   
  133.   // Close instance  
  134.   Lua_Close(LuaInstance);  
  135.   inherited;  
  136. end;  
  137.   
  138. //  
  139. // Wrapper for Lua File load and Execution  
  140. //  
  141. // @param       String  Filename        Lua Script file name  
  142. // @return      Integer  
  143. //  
  144. function TLua.DoFile(Filename: String): Integer;  
  145. begin  
  146.   Result := lual_dofile(LuaInstance, PAnsiChar(AnsiString(Filename)));  
  147. end;  
  148.   
  149. //  
  150. // Register a new Lua Function and map it to the Objects method name  
  151. //  
  152. // @param       AnsiString      FuncName        Lua Function Name  
  153. // @param       AnsiString      MethodName      (optional) Objects Method name  
  154. //  
  155. procedure TLua.RegisterFunction(FuncName: AnsiString; MethodName: AnsiString = ''; Obj: TObject = NIL);  
  156. var  
  157.   CallBack: TCallBack; // Callback Object  
  158. begin  
  159.   // if method name not specified use Lua function name  
  160.   if (MethodName = '') then  
  161.     MethodName := FuncName;  
  162.   
  163.   // if not object specified use this object  
  164.   if (Obj = NIL) then  
  165.     Obj := Self;  
  166.   
  167.   // Add Callback Object to the Object Index  
  168.   CallBack := TCallBack.Create;  
  169.   CallBack.Routine.Data := Obj;  
  170.   CallBack.Routine.Code := Obj.MethodAddress(String(MethodName));  
  171.   CallBack.Exec := TProc(CallBack.Routine);  
  172.   CallbackList.Add(CallBack);  
  173.   
  174.   // prepare Closure value (Method Name)  
  175.   lua_pushstring(LuaInstance, PAnsiChar(FuncName));  
  176.   
  177.   // prepare Closure value (CallBack Object Pointer)  
  178.   lua_pushlightuserdata(LuaInstance, CallBack);  
  179.   
  180.   // set new Lua function with Closure value  
  181.   lua_pushcclosure(LuaInstance, LuaCallBack, 1);  
  182.   lua_settable(LuaInstance, LUA_GLOBALSINDEX);  
  183. end;  
  184.   
  185. //  
  186. // UnRegister all new Lua Function  
  187. //  
  188. // @param       TObject     Object      Object with prev registered lua functions  
  189. //  
  190. procedure TLua.UnregisterFunctions(Obj: TObject);  
  191. var  
  192.   I: Integer;  
  193.   CallBack: TCallBack;  
  194. begin  
  195.   // remove obj from object list  
  196.   for I := CallBackList.Count downto do  
  197.   begin  
  198.     CallBack := CallBackList[I-1];  
  199.     if (assigned(CallBack)) and (CallBack.Routine.Data = Obj) then  
  200.     begin  
  201.       CallBack.Free;  
  202.       CallBackList.Items[I-1] := NIL;  
  203.       CallBackList.Delete(I-1);  
  204.     end;  
  205.   end;  
  206. end;  
  207.   
  208. //  
  209. // Register all published methods as Lua Functions  
  210. //  
  211. procedure TLua.AutoRegisterFunctions(Obj: TObject);  
  212. type  
  213.   PPointer = ^Pointer;  
  214.   PMethodRec = ^TMethodRec;  
  215.   
  216.   TMethodRec = packed record  
  217.     wSize: Word;  
  218.     pCode: Pointer;  
  219.     sName: ShortString;  
  220.   end;  
  221. var  
  222.   MethodTable: PAnsiChar;  
  223.   MethodRec: PMethodRec;  
  224.   wCount: Word;  
  225.   nMethod: Integer;  
  226. begin  
  227.   // Get a pointer to the class's published method table  
  228.   MethodTable := PAnsiChar(Pointer(PAnsiChar(Obj.ClassType) + vmtMethodTable)^);  
  229.   
  230.   if (MethodTable <> Nil) then  
  231.   begin  
  232.     // Get the count of the methods in the table  
  233.     Move(MethodTable^, wCount, 2);  
  234.   
  235.     // Position the MethodRec pointer at the first method in the table  
  236.     // (skip over the 2-byte method count)  
  237.     MethodRec := PMethodRec(MethodTable + 2);  
  238.   
  239.     // Iterate through all the published methods of this class  
  240.     for nMethod := to wCount - do  
  241.     begin  
  242.       // Add the method name to the lua functions  
  243.       RegisterFunction(MethodRec.sName, MethodRec.sName, Obj);  
  244.       // Skip to the next method  
  245.       MethodRec := PMethodRec(PAnsiChar(MethodRec) + MethodRec.wSize);  
  246.     end;  
  247.   end;  
  248. end;  
  249.   
  250.   
  251. end.  



lualib.pas

[delphi] view plain copy
 
    1. (****************************************************************************** 
    2. * Original copyright for the lua source and headers: 
    3. *  1994-2004 Tecgraf, PUC-Rio. 
    4. *  www.lua.org. 
    5. * Copyright for the Delphi adaptation: 
    6. *  2005 Rolf Meyerhoff 
    7. *  www.matrix44.de 
    8. * Copyright for the Lua 5.1 adaptation: 
    9. *  2007 Marco Antonio Abreu 
    10. *  www.marcoabreu.eti.br 
    11. *  All rights reserved. 
    12. * Permission is hereby granted, free of charge, to any person obtaining 
    13. * a copy of this software and associated documentation files (the 
    14. * "Software"), to deal in the Software without restriction, including 
    15. * without limitation the rights to use, copy, modify, merge, publish, 
    16. * distribute, sublicense, and/or sell copies of the Software, and to 
    17. * permit persons to whom the Software is furnished to do so, subject to 
    18. * the following conditions: 
    19. * The above copyright notice and this permission notice shall be 
    20. * included in all copies or substantial portions of the Software. 
    21. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 
    22. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 
    23. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 
    24. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 
    25. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 
    26. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 
    27. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 
    28. ******************************************************************************)  
    29. unit LuaLib;  
    30.   
    31. interface  
    32.   
    33. const  
    34.   LUA_VERSION   = 'Lua 5.1';  
    35.   LUA_RELEASE   = 'Lua 5.1.2';  
    36.   LUA_COPYRIGHT = 'Copyright (C) 1994-2004 Tecgraf, PUC-Rio';  
    37.   LUA_AUTHORS   = 'R. Ierusalimschy, L. H. de Figueiredo & W. Celes';  
    38.   
    39.   LUA_PASCAL_51_AUTHOR = 'Marco Antonio Abreu';  
    40.   LUA_PASCAL_51_COPYRIGHT = 'Copyright (C) 2007 Marco Antonio Abreu';  
    41.   
    42.   (* mark for precompiled code (`<esc>Lua') *)  
    43.   LUA_SIGNATURE = #27'Lua';  
    44.   
    45.   (* option for multiple returns in `lua_pcall' and `lua_call' *)  
    46.   LUA_MULTRET   = -1;  
    47.   
    48.   (* 
    49.   ** pseudo-indices 
    50.   *)  
    51.   LUA_REGISTRYINDEX = -10000;  
    52.   LUA_ENVIRONINDEX  = -10001;  
    53.   LUA_GLOBALSINDEX  = -10002;  
    54.   
    55.   (* thread status; 0 is OK *)  
    56.   LUA_TRD_YIELD = 1;  
    57.   LUA_ERRRUN    = 2;  
    58.   LUA_ERRSYNTAX = 3;  
    59.   LUA_ERRMEM    = 4;  
    60.   LUA_ERRERR    = 5;  
    61.   
    62.   (* extra error code for `luaL_load' *)  
    63.   LUA_ERRFILE   = LUA_ERRERR + 1;  
    64.   
    65.   (* 
    66.   ** basic types 
    67.   *)  
    68.   LUA_TNONE          = -1;  
    69.   LUA_TNIL           = 0;  
    70.   LUA_TBOOLEAN       = 1;  
    71.   LUA_TLIGHTUSERDATA = 2;  
    72.   LUA_TNUMBER        = 3;  
    73.   LUA_TSTRING        = 4;  
    74.   LUA_TTABLE         = 5;  
    75.   LUA_TFUNCTION      = 6;  
    76.   LUA_TUSERDATA      = 7;  
    77.   LUA_TTHREAD        = 8;  
    78.   
    79.   (* minimum Lua stack available to a C function *)  
    80.   LUA_MINSTACK       = 20;  
    81.   
    82.   (* 
    83.   ** garbage-collection function and options 
    84.   *)  
    85.   LUA_GCSTOP        = 0;  
    86.   LUA_GCRESTART     = 1;  
    87.   LUA_GCCOLLECT     = 2;  
    88.   LUA_GCCOUNT       = 3;  
    89.   LUA_GCCOUNTB      = 4;  
    90.   LUA_GCSTEP        = 5;  
    91.   LUA_GCSETPAUSE    = 6;  
    92.   LUA_GCSETSTEPMUL  = 7;  
    93.   
    94.   (* 
    95.   ** {====================================================================== 
    96.   ** Debug API 
    97.   ** ======================================================================= 
    98.   *)  
    99.   
    100.   (* 
    101.   ** Event codes 
    102.   *)  
    103.   LUA_HOOKCALL    = 0;  
    104.   LUA_HOOKRET     = 1;  
    105.   LUA_HOOKLINE    = 2;  
    106.   LUA_HOOKCOUNT   = 3;  
    107.   LUA_HOOKTAILRET = 4;  
    108.   
    109.   (* 
    110.   ** Event masks 
    111.   *)  
    112.   LUA_MASKCALL  = (shl LUA_HOOKCALL);  
    113.   LUA_MASKRET   = (shl LUA_HOOKRET);  
    114.   LUA_MASKLINE  = (shl LUA_HOOKLINE);  
    115.   LUA_MASKCOUNT = (shl LUA_HOOKCOUNT);  
    116.   
    117.   (* 
    118.   ** {====================================================================== 
    119.   ** useful definitions for Lua kernel and libraries 
    120.   ** ======================================================================= 
    121.   *)  
    122.   
    123.   (* 
    124.   @@ LUA_NUMBER_SCAN is the format for reading numbers. 
    125.   @@ LUA_NUMBER_FMT is the format for writing numbers. 
    126.   @@ lua_number2str converts a number to a string. 
    127.   @@ LUAI_MAXNUMBER2STR is maximum size of previous conversion. 
    128.   @@ lua_str2number converts a string to a number. 
    129.   *)  
    130.   LUA_NUMBER_SCAN   =   '%lf';  
    131.   LUA_NUMBER_FMT     =  '%.14g';  
    132.   LUAI_MAXNUMBER2STR =  32;  (* 16 digits, sign, point, and  *)  
    133.   
    134.   (* pre-defined references *)  
    135.   LUA_NOREF  = -2;  
    136.   LUA_REFNIL = -1;  
    137.   
    138.   LUA_IDSIZE = 60;  
    139.   
    140.   (* 
    141.   ** package names 
    142.   *)  
    143.   LUA_COLIBNAME   = 'coroutine';  
    144.   LUA_TABLIBNAME  = 'table';  
    145.   LUA_IOLIBNAME   = 'io';  
    146.   LUA_OSLIBNAME   = 'os';  
    147.   LUA_STRLIBNAME  = 'string';  
    148.   LUA_MATHLIBNAME = 'math';  
    149.   LUA_DBLIBNAME   = 'debug';  
    150.   LUA_LOADLIBNAME = 'package';  
    151.   
    152.   (* 
    153.   ** {====================================================== 
    154.   ** Generic Buffer manipulation 
    155.   ** ======================================================= 
    156.   *)  
    157.   
    158.   BUFSIZ = 512; (* From stdio.h *)  
    159.   LUAL_BUFFERSIZE = BUFSIZ;  
    160.   
    161. type  
    162.   lua_State = type Pointer;  
    163.   
    164.   lua_CFunction = function(L: lua_State): Integer; cdecl;  
    165.   
    166.   (* 
    167.   ** functions that read/write blocks when loading/dumping Lua chunks 
    168.   *)  
    169.   lua_Reader = function(L: lua_State; data: Pointer; var size: Cardinal): PAnsiChar; cdecl;  
    170.   lua_Writer = function(L: lua_State; p: Pointer; sz: Cardinal; ud: Pointer): Integer; cdecl;  
    171.   
    172.   (* 
    173.   ** prototype for memory-allocation functions 
    174.   *)  
    175.   lua_Alloc = function(ud, ptr: Pointer; osize, nsize: Cardinal): Pointer; cdecl;  
    176.   
    177.   (* type of numbers in Lua *)  
    178.   lua_Number  = type double;  
    179.   (* type for integer functions *)  
    180.   lua_Integer = type integer;  
    181.   
    182.   lua_Debug = packed record  
    183.     event: Integer;  
    184.     name: PAnsiChar; (* (n) *)  
    185.     namewhat: PAnsiChar; (* (n) `global', `local', `field', `method' *)  
    186.     what: PAnsiChar; (* (S) `Lua', `C', `main', `tail' *)  
    187.     source: PAnsiChar; (* (S) *)  
    188.     currentline: Integer; (* (l) *)  
    189.     nups: Integer;  (* (u) number of upvalues *)  
    190.     linedefined: Integer; (* (S) *)  
    191.     lastlinedefine: Integer;    (* (S) *)  
    192.     short_src: array[0..LUA_IDSIZE - 1] of AnsiChar; (* (S) *)  
    193.     (* private part *)  
    194.     i_ci: Integer; (* active function *)  
    195.   end;  
    196.   
    197.   (* Functions to be called by the debuger in specific events *)  
    198.   lua_Hook = procedure(L: lua_State; var ar: lua_Debug); cdecl;  
    199.   
    200.   (* Lua Record *)  
    201.   PluaL_reg = ^luaL_reg;  
    202.   luaL_reg = packed record  
    203.     name: PAnsiChar;  
    204.     func: lua_CFunction;  
    205.   end;  
    206.   
    207.   (* 
    208.   ** {====================================================== 
    209.   ** Generic Buffer manipulation 
    210.   ** ======================================================= 
    211.   *)  
    212.   
    213.   luaL_Buffer = packed record  
    214.     p: PAnsiChar; (* current position in buffer *)  
    215.     lvl: Integer;  (* number of strings in the stack (level) *)  
    216.     L: lua_State;  
    217.     buffer: array[0..LUAL_BUFFERSIZE - 1] of AnsiChar;  
    218.   end;  
    219.   
    220. var  
    221.   (* 
    222.   ** state manipulation 
    223.   *)  
    224.   lua_newstate:  function(f: lua_Alloc; ud: Pointer): lua_State; cdecl;  
    225.   lua_close:     procedure(L: lua_State); cdecl;  
    226.   lua_newthread: function(L: lua_State): lua_State; cdecl;  
    227.   lua_atpanic:   function(L: lua_State; panicf: lua_CFunction): lua_CFunction; cdecl;  
    228.   
    229.   (* 
    230.   ** basic stack manipulation 
    231.   *)  
    232.   lua_gettop:     function(L: lua_State): Integer; cdecl;  
    233.   lua_settop:     procedure(L: lua_State; idx: Integer); cdecl;  
    234.   lua_pushvalue:  procedure(L: lua_State; idx: Integer); cdecl;  
    235.   lua_remove:     procedure(L: lua_State; idx: Integer); cdecl;  
    236.   lua_insert:     procedure(L: lua_State; idx: Integer); cdecl;  
    237.   lua_replace:    procedure(L: lua_State; idx: Integer); cdecl;  
    238.   lua_checkstack: function(L: lua_State; extra: Integer): LongBool; cdecl;  
    239.   lua_xmove:      procedure(from, dest: lua_State; n: Integer); cdecl;  
    240.   
    241.   (* 
    242.   ** access functions (stack -> C/Pascal) 
    243.   *)  
    244.   
    245.   lua_isnumber:    function(L: lua_State; idx: Integer): LongBool; cdecl;  
    246.   lua_isstring:    function(L: lua_State; idx: Integer): LongBool; cdecl;  
    247.   lua_iscfunction: function(L: lua_State; idx: Integer): LongBool; cdecl;  
    248.   lua_isuserdata:  function(L: lua_State; idx: Integer): LongBool; cdecl;  
    249.   lua_type:        function(L: lua_State; idx: Integer): Integer; cdecl;  
    250.   lua_typename:    function(L: lua_State; tp: Integer): PAnsiChar; cdecl;  
    251.   
    252.   lua_equal:       function(L: lua_State; idx1, idx2: Integer): LongBool; cdecl;  
    253.   lua_rawequal:    function(L: lua_State; idx1, idx2: Integer): LongBool; cdecl;  
    254.   lua_lessthan:    function(L: lua_State; idx1, idx2: Integer): LongBool; cdecl;  
    255.   
    256.   lua_tonumber:    function(L: lua_State; idx: Integer): lua_Number; cdecl;  
    257.   lua_tointeger:   function(L: lua_State; idx: Integer): lua_Integer; cdecl;  
    258.   lua_toboolean:   function(L: lua_State; idx: Integer): LongBool; cdecl;  
    259.   lua_tolstring:   function(L: lua_State; idx: Integer; var len: Cardinal): PAnsiChar; cdecl;  
    260.   lua_objlen:      function(L: lua_State; idx: Integer): Cardinal; cdecl;  
    261.   lua_tocfunction: function(L: lua_State; idx: Integer): lua_CFunction; cdecl;  
    262.   lua_touserdata:  function(L: lua_State; idx: Integer): Pointer; cdecl;  
    263.   lua_tothread:    function(L: lua_State; idx: Integer): lua_State; cdecl;  
    264.   lua_topointer:   function(L: lua_State; idx: Integer): Pointer; cdecl;  
    265.   
    266.   (* 
    267.   ** push functions (C/Pascal -> stack) 
    268.   *)  
    269.   lua_pushnil:      procedure(L: lua_State); cdecl;  
    270.   lua_pushnumber:   procedure(L: lua_State; n: lua_Number); cdecl;  
    271.   lua_pushinteger:  procedure(L: lua_State; n: lua_Integer); cdecl;  
    272.   lua_pushlstring:  procedure(L: lua_State; s: PAnsiChar; len: Cardinal); cdecl;  
    273.   lua_pushstring:   procedure(L: lua_State; s: PAnsiChar); cdecl;  
    274.   lua_pushvfstring: function(L: lua_State; fmt, argp: PAnsiChar): PAnsiChar; cdecl;  
    275.   
    276.   lua_pushfstring:  function(L: lua_State; fmt: PAnsiChar; args: array of const): PAnsiChar; cdecl;  
    277.   lua_pushcclosure: procedure(L: lua_State; fn: lua_CFunction; n: Integer); cdecl;  
    278.   lua_pushboolean:  procedure(L: lua_State; b: LongBool); cdecl;  
    279.   lua_pushlightuserdata: procedure(L: lua_State; p: Pointer); cdecl;  
    280.   lua_pushthread:   function(L: lua_State): Integer; cdecl;  
    281.   
    282.   (* 
    283.   ** get functions (Lua -> stack) 
    284.   *)  
    285.   lua_gettable:     procedure(L: lua_State; idx: Integer); cdecl;  
    286.   lua_getfield:     procedure(L: lua_State; idx: Integer; k: PAnsiChar); cdecl;  
    287.   lua_rawget:       procedure(L: lua_State; idx: Integer); cdecl;  
    288.   lua_rawgeti:      procedure(L: lua_State; idx, n: Integer); cdecl;  
    289.   lua_createtable:  procedure(L: lua_State; narr, nrec: Integer); cdecl;  
    290.   lua_newuserdata:  function(L: lua_State; size: Cardinal): Pointer; cdecl;  
    291.   lua_getmetatable: function(L: lua_State; idx: Integer): LongBool; cdecl;  
    292.   lua_getfenv:      procedure(L: lua_State; idx: Integer); cdecl;  
    293.   
    294.   (* 
    295.   ** set functions (stack -> Lua) 
    296.   *)  
    297.   lua_settable:     procedure(L: lua_State; idx: Integer); cdecl;  
    298.   lua_setfield:     procedure(L: lua_State; idx: Integer; k: PAnsiChar ); cdecl;  
    299.   lua_rawset:       procedure(L: lua_State; idx: Integer); cdecl;  
    300.   lua_rawseti:      procedure(L: lua_State; idx, n: Integer); cdecl;  
    301.   lua_setmetatable: function(L: lua_State; idx: Integer): LongBool; cdecl;  
    302.   lua_setfenv:      function(L: lua_State; idx: Integer): LongBool; cdecl;  
    303.   
    304.   (* 
    305.   ** `load' and `call' functions (load and run Lua code) 
    306.   *)  
    307.   lua_call:   procedure(L: lua_State; nargs, nresults: Integer); cdecl;  
    308.   lua_pcall:  function(L: lua_State; nargs, nresults, errfunc: Integer): Integer; cdecl;  
    309.   lua_cpcall: function(L: lua_State; func: lua_CFunction; ud: Pointer): Integer; cdecl;  
    310.   lua_load:   function(L: lua_State; reader: lua_Reader; data: Pointer; chunkname: PAnsiChar): Integer; cdecl;  
    311.   lua_dump:   function(L: lua_State; writer: lua_Writer; data: Pointer): Integer; cdecl;  
    312.   
    313.   (* 
    314.   ** coroutine functions 
    315.   *)  
    316.   lua_yield:  function(L: lua_State; nresults: Integer): Integer; cdecl;  
    317.   lua_resume: function(L: lua_State; narg: Integer): Integer; cdecl;  
    318.   lua_status: function(L: lua_State): Integer; cdecl;  
    319.   
    320.   (* 
    321.   ** garbage-collection functions 
    322.   *)  
    323.   lua_gc: function(L: lua_State; what, data: Integer): Integer; cdecl;  
    324.   
    325.   (* 
    326.   ** miscellaneous functions 
    327.   *)  
    328.   
    329.   lua_error:  function(L: lua_State): Integer; cdecl;  
    330.   lua_next:   function(L: lua_State; idx: Integer): Integer; cdecl;  
    331.   lua_concat: procedure(L: lua_State; n: Integer); cdecl;  
    332.   
    333.   lua_getallocf: function(L: lua_State; ud: Pointer): lua_Alloc; cdecl;  
    334.   lua_setallocf: procedure(L: lua_State; f: lua_Alloc; ud: Pointer); cdecl;  
    335.   
    336.   (* 
    337.   ** {====================================================================== 
    338.   ** Debug API 
    339.   ** ======================================================================= 
    340.   *)  
    341.   
    342.   lua_getstack:   function(L: lua_State; level: Integer; var ar: lua_Debug): Integer; cdecl;  
    343.   lua_getinfo:    function(L: lua_State; what: PAnsiChar; var ar: lua_Debug): Integer; cdecl;  
    344.   lua_getlocal:   function(L: lua_State; var ar: lua_Debug; n: Integer): PAnsiChar; cdecl;  
    345.   lua_setlocal:   function(L: lua_State; var ar: lua_Debug; n: Integer): PAnsiChar; cdecl;  
    346.   lua_getupvalue: function(L: lua_State; funcindex, n: Integer): PAnsiChar; cdecl;  
    347.   lua_setupvalue: function(L: lua_State; funcindex, n: Integer): PAnsiChar; cdecl;  
    348.   
    349.   lua_sethook:      function(L: lua_State; func: lua_Hook; mask, count: Integer): Integer; cdecl;  
    350.   lua_gethook:      function(L: lua_State): lua_Hook; cdecl;  
    351.   lua_gethookmask:  function(L: lua_State): Integer; cdecl;  
    352.   lua_gethookcount: function(L: lua_State): Integer; cdecl;  
    353.   
    354.   (* lua libraries *)  
    355.   luaopen_base:    function(L: lua_State): Integer; cdecl;  
    356.   luaopen_debug:   function(L: lua_State): Integer; cdecl;  
    357.   luaopen_io:      function(L: lua_State): Integer; cdecl;  
    358.   luaopen_math:    function(L: lua_State): Integer; cdecl;  
    359.   luaopen_os:      function(L: lua_State): Integer; cdecl;  
    360.   luaopen_package: function(L: lua_State): Integer; cdecl;  
    361.   luaopen_string:  function(L: lua_State): Integer; cdecl;  
    362.   luaopen_table:   function(L: lua_State): Integer; cdecl;  
    363.   (* open all previous libraries *)  
    364.   luaL_openlibs:   procedure(L: lua_State); cdecl;  
    365.   
    366.   luaL_register:     procedure(L: lua_State; libname: PAnsiChar; lr: PluaL_reg); cdecl;  
    367.   luaL_getmetafield: function(L: lua_State; obj: Integer; e: PAnsiChar): Integer; cdecl;  
    368.   luaL_callmeta:     function(L: lua_State; obj: Integer; e: PAnsiChar): Integer; cdecl;  
    369.   luaL_typerror:     function(L: lua_State; narg: Integer; tname: PAnsiChar): Integer; cdecl;  
    370.   luaL_argerror:     function(L: lua_State; narg: Integer; extramsg: PAnsiChar): Integer; cdecl;  
    371.   luaL_checklstring: function(L: lua_State; narg: Integer; var len: Cardinal): PAnsiChar; cdecl;  
    372.   luaL_optlstring:   function(L: lua_State; narg: Integer; d: PAnsiChar; var len: Cardinal): PAnsiChar; cdecl;  
    373.   luaL_checknumber:  function(L: lua_State; narg: Integer): lua_Number; cdecl;  
    374.   luaL_optnumber:    function(L: lua_State; narg: Integer; d: lua_Number): lua_Number; cdecl;  
    375.   
    376.   luaL_checkinteger: function(L: lua_State; narg: Integer): lua_Integer; cdecl;  
    377.   luaL_optinteger:   function(L: lua_State; narg: Integer; d: lua_Integer): lua_Integer; cdecl;  
    378.   
    379.   luaL_checkstack: procedure(L: lua_State; sz: Integer; msg: PAnsiChar); cdecl;  
    380.   luaL_checktype:  procedure(L: lua_State; narg, t: Integer); cdecl;  
    381.   luaL_checkany:   procedure(L: lua_State; narg: Integer); cdecl;  
    382.   
    383.   luaL_newmetatable: function(L: lua_State; tname: PAnsiChar): Integer; cdecl;  
    384.   luaL_checkudata:   function(L: lua_State; narg: Integer; tname: PAnsiChar): Pointer; cdecl;  
    385.   
    386.   luaL_checkoption: function(L: lua_State; narg: Integer; def: PAnsiChar; lst: array of PAnsiChar): Integer; cdecl;  
    387.   
    388.   luaL_where: procedure(L: lua_State; lvl: Integer); cdecl;  
    389.   luaL_error: function(L: lua_State; fmt: PAnsiChar; args: array of const): Integer; cdecl;  
    390.   
    391.   luaL_ref:   function(L: lua_State; t: Integer): Integer; cdecl;  
    392.   luaL_unref: procedure(L: lua_State; t, ref: Integer); cdecl;  
    393.   
    394. {$ifdef LUA_COMPAT_GETN}  
    395.   luaL_getn: function(L: lua_State; t: Integer): Integer; cdecl;  
    396.   luaL_setn: procedure(L: lua_State; t, n: Integer); cdecl;  
    397. {$endif}  
    398.   
    399.   luaL_loadfile:   function(L: lua_State; filename: PAnsiChar): Integer; cdecl;  
    400.   luaL_loadbuffer: function(L: lua_State; buff: PAnsiChar; sz: Cardinal; name: PAnsiChar): Integer; cdecl;  
    401.   luaL_loadstring: function(L: lua_State; s: PAnsiChar): Integer; cdecl;  
    402.   
    403.   luaL_newstate:  function(): lua_State; cdecl;  
    404.   luaL_gsub:      function(L: lua_State; s, p, r: PAnsiChar): PAnsiChar; cdecl;  
    405.   luaL_findtable: function(L: lua_State; idx: Integer; fname: PAnsiChar; szhint: Integer): PAnsiChar; cdecl;  
    406.   
    407.   luaL_buffinit:   procedure(L: lua_State; var B: luaL_Buffer); cdecl;  
    408.   luaL_prepbuffer: function(var B: luaL_Buffer): PAnsiChar; cdecl;  
    409.   luaL_addlstring: procedure(var B: luaL_Buffer; s: PAnsiChar; l: Cardinal); cdecl;  
    410.   luaL_addstring:  procedure(var B: luaL_Buffer; s: PAnsiChar); cdecl;  
    411.   luaL_addvalue:   procedure(var B: luaL_Buffer); cdecl;  
    412.   luaL_pushresult: procedure(var B: luaL_Buffer); cdecl;  
    413.   
    414.   (* 
    415.   ** =============================================================== 
    416.   ** some useful macros 
    417.   ** =============================================================== 
    418.   *)  
    419.   
    420. {$ifndef LUA_COMPAT_GETN}  
    421.   function  luaL_getn(L: lua_State; t: Integer): Integer;  
    422.   procedure luaL_setn(L: lua_State; t, n: Integer);   
    423. {$endif}  
    424.   
    425.   (* pseudo-indices *)  
    426.   function lua_upvalueindex(i: Integer): Integer;  
    427.   
    428.   (* to help testing the libraries *)  
    429.   procedure lua_assert(c: Boolean);  
    430.   
    431.   function lua_number2str(s: Lua_Number; n: Integer): String;  
    432.   function lua_str2number(s: String; p: integer): Lua_Number;  
    433.   
    434.   (* argument and parameters checks *)  
    435.   function luaL_argcheck(L: lua_State; cond: Boolean; narg: Integer; extramsg: PAnsiChar): Integer;  
    436.   function luaL_checkstring(L: lua_State; narg: Integer): PAnsiChar;  
    437.   function luaL_optstring(L: lua_State; narg: Integer; d: PAnsiChar): PAnsiChar;  
    438.   function luaL_checkint(L: lua_State; narg: Integer): Integer;  
    439.   function luaL_optint(L: lua_State; narg, d: Integer): Integer;  
    440.   function luaL_checklong(L: lua_State; narg: Integer): LongInt;  
    441.   function luaL_optlong(L: lua_State; narg: Integer; d: LongInt): LongInt;  
    442.   
    443.   function luaL_typename(L: lua_State; idx: Integer): PAnsiChar;  
    444.   function luaL_dofile(L: lua_State; filename: PAnsiChar): Integer;  
    445.   function luaL_dostring(L: lua_State; str: PAnsiChar): Integer;  
    446.   
    447.   procedure luaL_getmetatable(L: lua_State; tname: PAnsiChar);  
    448.   
    449.   (* Generic Buffer manipulation *)  
    450.   procedure luaL_addchar(var B: luaL_Buffer; c: AnsiChar);  
    451.   procedure luaL_putchar(var B: luaL_Buffer; c: AnsiChar);  
    452.   procedure luaL_addsize(var B: luaL_Buffer; n: Cardinal);  
    453.   
    454.   function luaL_check_lstr(L: lua_State; numArg: Integer; var ls: Cardinal): PAnsiChar;  
    455.   function luaL_opt_lstr(L: lua_State; numArg: Integer; def: PAnsiChar; var ls: Cardinal): PAnsiChar;  
    456.   function luaL_check_number(L: lua_State; numArg: Integer): lua_Number;  
    457.   function luaL_opt_number(L: lua_State; nArg: Integer; def: lua_Number): lua_Number;  
    458.   function luaL_arg_check(L: lua_State; cond: Boolean; numarg: Integer; extramsg: PAnsiChar): Integer;  
    459.   function luaL_check_string(L: lua_State; n: Integer): PAnsiChar;  
    460.   function luaL_opt_string(L: lua_State; n: Integer; d: PAnsiChar): PAnsiChar;  
    461.   function luaL_check_int(L: lua_State; n: Integer): Integer;  
    462.   function luaL_check_long(L: lua_State; n: LongInt): LongInt;  
    463.   function luaL_opt_int(L: lua_State; n, d: Integer): Integer;  
    464.   function luaL_opt_long(L: lua_State; n: Integer; d: LongInt): LongInt;  
    465.   
    466.   procedure lua_pop(L: lua_State; n: Integer);  
    467.   procedure lua_newtable(L: lua_State);  
    468.   procedure lua_register(L: lua_state; name: PAnsiChar; f: lua_CFunction);  
    469.   procedure lua_pushcfunction(L: lua_State; f: lua_CFunction);  
    470.   function  lua_strlen(L: lua_State; i: Integer): Cardinal;  
    471.   
    472.   function lua_isfunction(L: lua_State; idx: Integer): Boolean;  
    473.   function lua_istable(L: lua_State; idx: Integer): Boolean;  
    474.   function lua_islightuserdata(L: lua_State; idx: Integer): Boolean;  
    475.   function lua_isnil(L: lua_State; idx: Integer): Boolean;  
    476.   function lua_isboolean(L: lua_State; idx: Integer): Boolean;  
    477.   function lua_isthread(L: lua_State; idx: Integer): Boolean;  
    478.   function lua_isnone(L: lua_State; idx: Integer): Boolean;  
    479.   function lua_isnoneornil(L: lua_State; idx: Integer): Boolean;  
    480.   
    481.   procedure lua_pushliteral(L: lua_State; s: PAnsiChar);  
    482.   procedure lua_setglobal(L: lua_State; name: PAnsiChar);  
    483.   procedure lua_getglobal(L: lua_State; name: PAnsiChar);  
    484.   function  lua_tostring(L: lua_State; idx: Integer): PAnsiChar;  
    485.   
    486.   (* 
    487.   ** compatibility macros and functions 
    488.   *)  
    489.   
    490.   function  lua_open(): lua_State;  
    491.   procedure lua_getregistry(L: lua_State);  
    492.   function  lua_getgccount(L: lua_State): Integer;  
    493.   
    494.   (* compatibility with ref system *)  
    495.   function  lua_ref(L: lua_State; lock: Boolean): Integer;  
    496.   procedure lua_unref(L: lua_State; ref: Integer);  
    497.   procedure lua_getref(L: lua_State; ref: Integer);  
    498.   
    499.   (* 
    500.   ** Dynamic library manipulation 
    501.   *)  
    502.   function  GetProcAddr( fHandle: THandle; const methodName: String; bErrorIfNotExists: Boolean = True ): Pointer;  
    503.   procedure SetLuaLibFileName( newLuaLibFileName: String );  
    504.   function  GetLuaLibFileName(): String;  
    505.   function  LoadLuaLib( newLuaLibFileName: String = '' ): Integer;  
    506.   procedure FreeLuaLib();  
    507.   function LuaLibLoaded: Boolean;  
    508.   
    509. implementation  
    510.   
    511. uses  
    512.   SysUtils, Math,  
    513. {$ifdef MSWINDOWS}  
    514.   Windows  
    515. {$endif}  
    516. ;  
    517.   
    518. var  
    519.   fLibHandle: Integer = 0;  
    520. {$ifdef MSWINDOWS}  
    521.   fLuaLibFileName: String = 'Lua5.1.dll';  
    522. {$endif}  
    523. {$ifdef LINUX}  
    524.   fLuaLibFileName: String = 'liblua.so.5.1';  
    525. {$endif}  
    526.   
    527. (* 
    528. ** Dynamic library manipulation 
    529. *)  
    530.   
    531. function GetProcAddr( fHandle: THandle; const methodName: String; bErrorIfNotExists: Boolean = True ): Pointer;  
    532. begin  
    533.   Result := GetProcAddress( fHandle, PAnsiChar( AnsiString(methodName) ) );  
    534.   
    535.   if bErrorIfNotExists and ( Result = nil ) then  
    536.      Raise Exception.Create( 'Cannot load method ' + QuotedStr( methodName ) + ' from dynamic library.' );  
    537. end;  
    538.   
    539. procedure SetLuaLibFileName( newLuaLibFileName: String );  
    540. begin  
    541.   fLuaLibFileName := newLuaLibFileName;  
    542. end;  
    543.   
    544. function GetLuaLibFileName(): String;  
    545. begin  
    546.   Result := fLuaLibFileName;  
    547. end;  
    548.   
    549. function LuaLibLoaded: Boolean;  
    550. begin  
    551.    Result := fLibHandle <> 0;  
    552. end;  
    553.   
    554.   
    555.   
    556. function LoadLuaLib(newLuaLibFileName: String): Integer;  
    557. begin  
    558.   FreeLuaLib();  
    559.   
    560.   if newLuaLibFileName <> '' then  
    561.      SetLuaLibFileName( newLuaLibFileName );  
    562.   
    563.   if not FileExists( GetLuaLibFileName() ) then begin  
    564.      Result := -1;  
    565.      exit;  
    566.   end;  
    567.   
    568.   fLibHandle := LoadLibrary(PWideChar( (GetLuaLibFileName() ) ));  
    569.   
    570.   if fLibHandle = then begin  
    571.      Result := -2;  
    572.      exit;  
    573.   end;  
    574.   
    575.   lua_newstate       := GetProcAddr( fLibHandle, 'lua_newstate' );  
    576.   lua_close          := GetProcAddr( fLibHandle, 'lua_close' );  
    577.   lua_newthread      := GetProcAddr( fLibHandle, 'lua_newthread' );  
    578.   lua_atpanic        := GetProcAddr( fLibHandle, 'lua_atpanic' );  
    579.   lua_gettop         := GetProcAddr( fLibHandle, 'lua_gettop' );  
    580.   lua_settop         := GetProcAddr( fLibHandle, 'lua_settop' );  
    581.   lua_pushvalue      := GetProcAddr( fLibHandle, 'lua_pushvalue' );  
    582.   lua_remove         := GetProcAddr( fLibHandle, 'lua_remove' );  
    583.   lua_insert         := GetProcAddr( fLibHandle, 'lua_insert' );  
    584.   lua_replace        := GetProcAddr( fLibHandle, 'lua_replace' );  
    585.   lua_checkstack     := GetProcAddr( fLibHandle, 'lua_checkstack' );  
    586.   lua_xmove          := GetProcAddr( fLibHandle, 'lua_xmove' );  
    587.   lua_isnumber       := GetProcAddr( fLibHandle, 'lua_isnumber' );  
    588.   lua_isstring       := GetProcAddr( fLibHandle, 'lua_isstring' );  
    589.   lua_iscfunction    := GetProcAddr( fLibHandle, 'lua_iscfunction' );  
    590.   lua_isuserdata     := GetProcAddr( fLibHandle, 'lua_isuserdata' );  
    591.   lua_type           := GetProcAddr( fLibHandle, 'lua_type' );  
    592.   lua_typename       := GetProcAddr( fLibHandle, 'lua_typename' );  
    593.   lua_equal          := GetProcAddr( fLibHandle, 'lua_equal' );  
    594.   lua_rawequal       := GetProcAddr( fLibHandle, 'lua_rawequal' );  
    595.   lua_lessthan       := GetProcAddr( fLibHandle, 'lua_lessthan' );  
    596.   lua_tonumber       := GetProcAddr( fLibHandle, 'lua_tonumber' );  
    597.   lua_tointeger      := GetProcAddr( fLibHandle, 'lua_tointeger' );  
    598.   lua_toboolean      := GetProcAddr( fLibHandle, 'lua_toboolean' );  
    599.   lua_tolstring      := GetProcAddr( fLibHandle, 'lua_tolstring' );  
    600.   lua_objlen         := GetProcAddr( fLibHandle, 'lua_objlen' );  
    601.   lua_tocfunction    := GetProcAddr( fLibHandle, 'lua_tocfunction' );  
    602.   lua_touserdata     := GetProcAddr( fLibHandle, 'lua_touserdata' );  
    603.   lua_tothread       := GetProcAddr( fLibHandle, 'lua_tothread' );  
    604.   lua_topointer      := GetProcAddr( fLibHandle, 'lua_topointer' );  
    605.   lua_pushnil        := GetProcAddr( fLibHandle, 'lua_pushnil' );  
    606.   lua_pushnumber     := GetProcAddr( fLibHandle, 'lua_pushnumber' );  
    607.   lua_pushinteger    := GetProcAddr( fLibHandle, 'lua_pushinteger' );  
    608.   lua_pushlstring    := GetProcAddr( fLibHandle, 'lua_pushlstring' );  
    609.   lua_pushstring     := GetProcAddr( fLibHandle, 'lua_pushstring' );  
    610.   lua_pushvfstring   := GetProcAddr( fLibHandle, 'lua_pushvfstring' );  
    611.   lua_pushfstring    := GetProcAddr( fLibHandle, 'lua_pushfstring' );  
    612.   lua_pushcclosure   := GetProcAddr( fLibHandle, 'lua_pushcclosure' );  
    613.   lua_pushboolean    := GetProcAddr( fLibHandle, 'lua_pushboolean' );  
    614.   lua_pushlightuserdata := GetProcAddr( fLibHandle, 'lua_pushlightuserdata' );  
    615.   lua_pushthread     := GetProcAddr( fLibHandle, 'lua_pushthread' );  
    616.   lua_gettable       := GetProcAddr( fLibHandle, 'lua_gettable' );  
    617.   lua_getfield       := GetProcAddr( fLibHandle, 'lua_getfield' );  
    618.   lua_rawget         := GetProcAddr( fLibHandle, 'lua_rawget' );  
    619.   lua_rawgeti        := GetProcAddr( fLibHandle, 'lua_rawgeti' );  
    620.   lua_createtable    := GetProcAddr( fLibHandle, 'lua_createtable' );  
    621.   lua_newuserdata    := GetProcAddr( fLibHandle, 'lua_newuserdata' );  
    622.   lua_getmetatable   := GetProcAddr( fLibHandle, 'lua_getmetatable' );  
    623.   lua_getfenv        := GetProcAddr( fLibHandle, 'lua_getfenv' );  
    624.   lua_settable       := GetProcAddr( fLibHandle, 'lua_settable' );  
    625.   lua_setfield       := GetProcAddr( fLibHandle, 'lua_setfield' );  
    626.   lua_rawset         := GetProcAddr( fLibHandle, 'lua_rawset' );  
    627.   lua_rawseti        := GetProcAddr( fLibHandle, 'lua_rawseti' );  
    628.   lua_setmetatable   := GetProcAddr( fLibHandle, 'lua_setmetatable' );  
    629.   lua_setfenv        := GetProcAddr( fLibHandle, 'lua_setfenv' );  
    630.   lua_call           := GetProcAddr( fLibHandle, 'lua_call' );  
    631.   lua_pcall          := GetProcAddr( fLibHandle, 'lua_pcall' );  
    632.   lua_cpcall         := GetProcAddr( fLibHandle, 'lua_cpcall' );  
    633.   lua_load           := GetProcAddr( fLibHandle, 'lua_load' );  
    634.   lua_dump           := GetProcAddr( fLibHandle, 'lua_dump' );  
    635.   lua_yield          := GetProcAddr( fLibHandle, 'lua_yield' );  
    636.   lua_resume         := GetProcAddr( fLibHandle, 'lua_resume' );  
    637.   lua_status         := GetProcAddr( fLibHandle, 'lua_status' );  
    638.   lua_gc             := GetProcAddr( fLibHandle, 'lua_gc' );  
    639.   lua_error          := GetProcAddr( fLibHandle, 'lua_error' );  
    640.   lua_next           := GetProcAddr( fLibHandle, 'lua_next' );  
    641.   lua_concat         := GetProcAddr( fLibHandle, 'lua_concat' );  
    642.   lua_getallocf      := GetProcAddr( fLibHandle, 'lua_getallocf' );  
    643.   lua_setallocf      := GetProcAddr( fLibHandle, 'lua_setallocf' );  
    644.   lua_getstack       := GetProcAddr( fLibHandle, 'lua_getstack' );  
    645.   lua_getinfo        := GetProcAddr( fLibHandle, 'lua_getinfo' );  
    646.   lua_getlocal       := GetProcAddr( fLibHandle, 'lua_getlocal' );  
    647.   lua_setlocal       := GetProcAddr( fLibHandle, 'lua_setlocal' );  
    648.   lua_getupvalue     := GetProcAddr( fLibHandle, 'lua_getupvalue' );  
    649.   lua_setupvalue     := GetProcAddr( fLibHandle, 'lua_setupvalue' );  
    650.   lua_sethook        := GetProcAddr( fLibHandle, 'lua_sethook' );  
    651.   lua_gethook        := GetProcAddr( fLibHandle, 'lua_gethook' );  
    652.   lua_gethookmask    := GetProcAddr( fLibHandle, 'lua_gethookmask' );  
    653.   lua_gethookcount   := GetProcAddr( fLibHandle, 'lua_gethookcount' );  
    654.   luaopen_base       := GetProcAddr( fLibHandle, 'luaopen_base' );  
    655.   luaopen_table      := GetProcAddr( fLibHandle, 'luaopen_table' );  
    656.   luaopen_io         := GetProcAddr( fLibHandle, 'luaopen_io' );  
    657.   luaopen_os         := GetProcAddr( fLibHandle, 'luaopen_os' );  
    658.   luaopen_string     := GetProcAddr( fLibHandle, 'luaopen_string' );  
    659.   luaopen_math       := GetProcAddr( fLibHandle, 'luaopen_math' );  
    660.   luaopen_debug      := GetProcAddr( fLibHandle, 'luaopen_debug' );  
    661.   luaopen_package    := GetProcAddr( fLibHandle, 'luaopen_package' );  
    662.   luaL_openlibs      := GetProcAddr( fLibHandle, 'luaL_openlibs' );  
    663.   luaL_register      := GetProcAddr( fLibHandle, 'luaL_register' );  
    664.   luaL_getmetafield  := GetProcAddr( fLibHandle, 'luaL_getmetafield' );  
    665.   luaL_callmeta      := GetProcAddr( fLibHandle, 'luaL_callmeta' );  
    666.   luaL_typerror      := GetProcAddr( fLibHandle, 'luaL_typerror' );  
    667.   luaL_argerror      := GetProcAddr( fLibHandle, 'luaL_argerror' );  
    668.   luaL_checklstring  := GetProcAddr( fLibHandle, 'luaL_checklstring' );  
    669.   luaL_optlstring    := GetProcAddr( fLibHandle, 'luaL_optlstring' );  
    670.   luaL_checknumber   := GetProcAddr( fLibHandle, 'luaL_checknumber' );  
    671.   luaL_optnumber     := GetProcAddr( fLibHandle, 'luaL_optnumber' );  
    672.   luaL_checkinteger  := GetProcAddr( fLibHandle, 'luaL_checkinteger' );  
    673.   luaL_optinteger    := GetProcAddr( fLibHandle, 'luaL_optinteger' );  
    674.   luaL_checkstack    := GetProcAddr( fLibHandle, 'luaL_checkstack' );  
    675.   luaL_checktype     := GetProcAddr( fLibHandle, 'luaL_checktype' );  
    676.   luaL_checkany      := GetProcAddr( fLibHandle, 'luaL_checkany' );  
    677.   luaL_newmetatable  := GetProcAddr( fLibHandle, 'luaL_newmetatable' );  
    678.   luaL_checkudata    := GetProcAddr( fLibHandle, 'luaL_checkudata' );  
    679.   luaL_where         := GetProcAddr( fLibHandle, 'luaL_where' );  
    680.   luaL_error         := GetProcAddr( fLibHandle, 'luaL_error' );  
    681.   luaL_checkoption   := GetProcAddr( fLibHandle, 'luaL_checkoption' );  
    682.   luaL_ref           := GetProcAddr( fLibHandle, 'luaL_ref' );  
    683.   luaL_unref         := GetProcAddr( fLibHandle, 'luaL_unref' );  
    684. {$ifdef LUA_COMPAT_GETN}  
    685.   luaL_getn          := GetProcAddr( fLibHandle, 'luaL_getn' );  
    686.   luaL_setn          := GetProcAddr( fLibHandle, 'luaL_setn' );  
    687. {$endif}  
    688.   luaL_loadfile      := GetProcAddr( fLibHandle, 'luaL_loadfile' );  
    689.   luaL_loadbuffer    := GetProcAddr( fLibHandle, 'luaL_loadbuffer' );  
    690.   luaL_loadstring    := GetProcAddr( fLibHandle, 'luaL_loadstring' );  
    691.   luaL_newstate      := GetProcAddr( fLibHandle, 'luaL_newstate' );  
    692.   luaL_gsub          := GetProcAddr( fLibHandle, 'luaL_gsub' );  
    693.   luaL_findtable     := GetProcAddr( fLibHandle, 'luaL_findtable' );  
    694.   luaL_buffinit      := GetProcAddr( fLibHandle, 'luaL_buffinit' );  
    695.   luaL_prepbuffer    := GetProcAddr( fLibHandle, 'luaL_prepbuffer' );  
    696.   luaL_addlstring    := GetProcAddr( fLibHandle, 'luaL_addlstring' );  
    697.   luaL_addstring     := GetProcAddr( fLibHandle, 'luaL_addstring' );  
    698.   luaL_addvalue      := GetProcAddr( fLibHandle, 'luaL_addvalue' );  
    699.   luaL_pushresult    := GetProcAddr( fLibHandle, 'luaL_pushresult' );  
    700.   
    701.   Result := fLibHandle;  
    702. end;  
    703.   
    704. procedure FreeLuaLib();  
    705. begin  
    706.   lua_newstate       := nil;  
    707.   lua_close          := nil;  
    708.   lua_newthread      := nil;  
    709.   lua_atpanic        := nil;  
    710.   lua_gettop         := nil;  
    711.   lua_settop         := nil;  
    712.   lua_pushvalue      := nil;  
    713.   lua_remove         := nil;  
    714.   lua_insert         := nil;  
    715.   lua_replace        := nil;  
    716.   lua_checkstack     := nil;  
    717.   lua_xmove          := nil;  
    718.   lua_isnumber       := nil;  
    719.   lua_isstring       := nil;  
    720.   lua_iscfunction    := nil;  
    721.   lua_isuserdata     := nil;  
    722.   lua_type           := nil;  
    723.   lua_typename       := nil;  
    724.   lua_equal          := nil;  
    725.   lua_rawequal       := nil;  
    726.   lua_lessthan       := nil;  
    727.   lua_tonumber       := nil;  
    728.   lua_tointeger      := nil;  
    729.   lua_toboolean      := nil;  
    730.   lua_tolstring      := nil;  
    731.   lua_objlen         := nil;  
    732.   lua_tocfunction    := nil;  
    733.   lua_touserdata     := nil;  
    734.   lua_tothread       := nil;  
    735.   lua_topointer      := nil;  
    736.   lua_pushnil        := nil;  
    737.   lua_pushnumber     := nil;  
    738.   lua_pushinteger    := nil;  
    739.   lua_pushlstring    := nil;  
    740.   lua_pushstring     := nil;  
    741.   lua_pushvfstring   := nil;  
    742.   lua_pushfstring    := nil;  
    743.   lua_pushcclosure   := nil;  
    744.   lua_pushboolean    := nil;  
    745.   lua_pushlightuserdata := nil;  
    746.   lua_pushthread     := nil;  
    747.   lua_gettable       := nil;  
    748.   lua_getfield       := nil;  
    749.   lua_rawget         := nil;  
    750.   lua_rawgeti        := nil;  
    751.   lua_createtable    := nil;  
    752.   lua_newuserdata    := nil;  
    753.   lua_getmetatable   := nil;  
    754.   lua_getfenv        := nil;  
    755.   lua_settable       := nil;  
    756.   lua_setfield       := nil;  
    757.   lua_rawset         := nil;  
    758.   lua_rawseti        := nil;  
    759.   lua_setmetatable   := nil;  
    760.   lua_setfenv        := nil;  
    761.   lua_call           := nil;  
    762.   lua_pcall          := nil;  
    763.   lua_cpcall         := nil;  
    764.   lua_load           := nil;  
    765.   lua_dump           := nil;  
    766.   lua_yield          := nil;  
    767.   lua_resume         := nil;  
    768.   lua_status         := nil;  
    769.   lua_gc             := nil;  
    770.   lua_error          := nil;  
    771.   lua_next           := nil;  
    772.   lua_concat         := nil;  
    773.   lua_getallocf      := nil;  
    774.   lua_setallocf      := nil;  
    775.   lua_getstack       := nil;  
    776.   lua_getinfo        := nil;  
    777.   lua_getlocal       := nil;  
    778.   lua_setlocal       := nil;  
    779.   lua_getupvalue     := nil;  
    780.   lua_setupvalue     := nil;  
    781.   lua_sethook        := nil;  
    782.   lua_gethook        := nil;  
    783.   lua_gethookmask    := nil;  
    784.   lua_gethookcount   := nil;  
    785.   luaopen_base       := nil;  
    786.   luaopen_table      := nil;  
    787.   luaopen_io         := nil;  
    788.   luaopen_os         := nil;  
    789.   luaopen_string     := nil;  
    790.   luaopen_math       := nil;  
    791.   luaopen_debug      := nil;  
    792.   luaopen_package    := nil;  
    793.   luaL_openlibs      := nil;  
    794.   luaL_register      := nil;  
    795.   luaL_getmetafield  := nil;  
    796.   luaL_callmeta      := nil;  
    797.   luaL_typerror      := nil;  
    798.   luaL_argerror      := nil;  
    799.   luaL_checklstring  := nil;  
    800.   luaL_optlstring    := nil;  
    801.   luaL_checknumber   := nil;  
    802.   luaL_optnumber     := nil;  
    803.   luaL_checkinteger  := nil;  
    804.   luaL_optinteger    := nil;  
    805.   luaL_checkstack    := nil;  
    806.   luaL_checktype     := nil;  
    807.   luaL_checkany      := nil;  
    808.   luaL_newmetatable  := nil;  
    809.   luaL_checkudata    := nil;  
    810.   luaL_where         := nil;  
    811.   luaL_error         := nil;  
    812.   luaL_checkoption   := nil;  
    813.   luaL_ref           := nil;  
    814.   luaL_unref         := nil;  
    815. {$ifdef LUA_COMPAT_GETN}  
    816.   luaL_getn          := nil;  
    817.   luaL_setn          := nil;  
    818. {$endif}  
    819.   luaL_loadfile      := nil;  
    820.   luaL_loadbuffer    := nil;  
    821.   luaL_loadstring    := nil;  
    822.   luaL_newstate      := nil;  
    823.   luaL_gsub          := nil;  
    824.   luaL_findtable     := nil;  
    825.   luaL_buffinit      := nil;  
    826.   luaL_prepbuffer    := nil;  
    827.   luaL_addlstring    := nil;  
    828.   luaL_addstring     := nil;  
    829.   luaL_addvalue      := nil;  
    830.   luaL_pushresult    := nil;  
    831.   
    832.   if fLibHandle <> then begin  
    833.      FreeLibrary( fLibHandle );  
    834.      fLibHandle := 0;  
    835.   end;  
    836. end;  
    837.   
    838. {$ifndef LUA_COMPAT_GETN}  
    839. function luaL_getn(L: lua_State; t: Integer): Integer;  
    840. begin  
    841.   Result := lua_objlen(L, t);  
    842. end;  
    843.   
    844. procedure luaL_setn(L: lua_State; t, n: Integer);  
    845. begin  
    846. end;  
    847. {$endif}  
    848.   
    849. function lua_upvalueindex(i: Integer): Integer;  
    850. begin  
    851.   Result := LUA_GLOBALSINDEX - i;  
    852. end;  
    853.   
    854. procedure lua_pop(L: lua_State; n: Integer);  
    855. begin  
    856.   lua_settop(L, -(n) - 1);  
    857. end;  
    858.   
    859. procedure lua_newtable(L: lua_State);  
    860. begin  
    861.   lua_createtable(L, 0, 0);  
    862. end;  
    863.   
    864. function lua_strlen(L: lua_State; i: Integer): Cardinal;  
    865. begin  
    866.   result := lua_objlen(L, i);  
    867. end;  
    868.   
    869. procedure lua_register(L: lua_state; name: PAnsiChar; f: lua_CFunction);  
    870. begin  
    871.   lua_pushcfunction(L, f);  
    872.   lua_setglobal(L, name);  
    873. end;  
    874.   
    875. procedure lua_pushcfunction(L: lua_State; f: lua_CFunction);  
    876. begin  
    877.   lua_pushcclosure(L, f, 0);  
    878. end;  
    879.   
    880. function lua_isfunction(L: lua_State; idx: Integer): Boolean;  
    881. begin  
    882.   Result := lua_type(L, idx) = LUA_TFUNCTION;  
    883. end;  
    884.   
    885. function lua_istable(L: lua_State; idx: Integer): Boolean;  
    886. begin  
    887.   Result := lua_type(L, idx) = LUA_TTABLE;  
    888. end;  
    889.   
    890. function lua_islightuserdata(L: lua_State; idx: Integer): Boolean;  
    891. begin  
    892.   Result := lua_type(L, idx) = LUA_TLIGHTUSERDATA;  
    893. end;  
    894.   
    895. function lua_isnil(L: lua_State; idx: Integer): Boolean;  
    896. begin  
    897.   Result := lua_type(L, idx) = LUA_TNIL;  
    898. end;  
    899.   
    900. function lua_isboolean(L: lua_State; idx: Integer): Boolean;  
    901. begin  
    902.   Result := lua_type(L, idx) = LUA_TBOOLEAN;  
    903. end;  
    904.   
    905. function lua_isthread(L: lua_State; idx: Integer): Boolean;  
    906. begin  
    907.   Result := lua_type(L, idx) = LUA_TTHREAD;  
    908. end;  
    909.   
    910. function lua_isnone(L: lua_State; idx: Integer): Boolean;  
    911. begin  
    912.   Result := lua_type(L, idx) = LUA_TNONE;  
    913. end;  
    914.   
    915. function lua_isnoneornil(L: lua_State; idx: Integer): Boolean;  
    916. begin  
    917.   Result := lua_type(L, idx) <= 0;  
    918. end;  
    919.   
    920. procedure lua_pushliteral(L: lua_State; s: PAnsiChar);  
    921. begin  
    922.   lua_pushlstring(L, s, StrLen(s));  
    923. end;  
    924.   
    925. procedure lua_setglobal(L: lua_State; name: PAnsiChar);  
    926. begin  
    927.   lua_setfield(L, LUA_GLOBALSINDEX, name);  
    928. end;  
    929.   
    930. procedure lua_getglobal(L: lua_State; name: PAnsiChar);  
    931. begin  
    932.   lua_getfield(L, LUA_GLOBALSINDEX, name);  
    933. end;  
    934.   
    935. function lua_tostring(L: lua_State; idx: Integer): PAnsiChar;  
    936. var  
    937.   len: Cardinal;  
    938. begin  
    939.   Result := lua_tolstring(L, idx, len);  
    940. end;  
    941.   
    942. function lua_getgccount(L: lua_State): Integer;  
    943. begin  
    944.   Result := lua_gc(L, LUA_GCCOUNT, 0);  
    945. end;  
    946.   
    947. function lua_open(): lua_State;  
    948. begin  
    949.   Result := luaL_newstate();  
    950. end;  
    951.   
    952. procedure lua_getregistry(L: lua_State);  
    953. begin  
    954.   lua_pushvalue(L, LUA_REGISTRYINDEX);  
    955. end;  
    956.   
    957. function lua_ref(L: lua_State; lock: Boolean): Integer;  
    958. begin  
    959.   if lock then  
    960.      Result := luaL_ref(L, LUA_REGISTRYINDEX)  
    961.   else begin  
    962.      lua_pushstring(L, 'unlocked references are obsolete');  
    963.      Result := lua_error(L);  
    964.   end;  
    965. end;  
    966.   
    967. procedure lua_unref(L: lua_State; ref: Integer);  
    968. begin  
    969.   luaL_unref(L, LUA_REGISTRYINDEX, ref);  
    970. end;  
    971.   
    972. procedure lua_getref(L: lua_State; ref: Integer);  
    973. begin  
    974.   lua_rawgeti(L, LUA_REGISTRYINDEX, ref);  
    975. end;  
    976.   
    977. procedure lua_assert(c: Boolean);  
    978. begin  
    979. end;  
    980.   
    981. function lua_number2str(s: Lua_Number; n: Integer): String;  
    982. begin  
    983.   Result := FormatFloat( LUA_NUMBER_FMT, RoundTo( s, n ) );  
    984. end;  
    985.   
    986. function lua_str2number(s: String; p: integer): Lua_Number;  
    987. begin  
    988.   Result := RoundTo( StrToFloat( s ), p );  
    989. end;  
    990.   
    991. function luaL_argcheck(L: lua_State; cond: Boolean; narg: Integer; extramsg: PAnsiChar): Integer;  
    992. begin  
    993.   if cond then  
    994.      Result := 0  
    995.   else  
    996.      Result := luaL_argerror(L, narg, extramsg);  
    997. end;  
    998.   
    999. function luaL_checkstring(L: lua_State; narg: Integer): PAnsiChar;  
    1000. var  
    1001.   ls: Cardinal;  
    1002. begin  
    1003.   Result := luaL_checklstring(L, narg, ls);  
    1004. end;  
    1005.   
    1006. function luaL_optstring(L: lua_State; narg: Integer; d: PAnsiChar): PAnsiChar;  
    1007. var  
    1008.   ls: Cardinal;  
    1009. begin  
    1010.   Result := luaL_optlstring(L, narg, d, ls);  
    1011. end;  
    1012.   
    1013. function luaL_checkint(L: lua_State; narg: Integer): Integer;  
    1014. begin  
    1015.   Result := Trunc(luaL_checkinteger(L, narg));  
    1016. end;  
    1017.   
    1018. function luaL_optint(L: lua_State; narg, d: Integer): Integer;  
    1019. begin  
    1020.   Result := Trunc(luaL_optinteger(L, narg, d));  
    1021. end;  
    1022.   
    1023. function luaL_checklong(L: lua_State; narg: Integer): LongInt;  
    1024. begin  
    1025.   Result := Trunc(luaL_checkinteger(L, narg));  
    1026. end;  
    1027.   
    1028. function luaL_optlong(L: lua_State; narg: Integer; d: LongInt): LongInt;  
    1029. begin  
    1030.   Result := Trunc(luaL_optinteger(L, narg, d));  
    1031. end;  
    1032.   
    1033. function luaL_typename(L: lua_State; idx: Integer): PAnsiChar;  
    1034. begin  
    1035.   Result := lua_typename(L, lua_type(L, idx));  
    1036. end;  
    1037.   
    1038. function luaL_dofile(L: lua_State; filename: PAnsiChar): Integer;  
    1039. begin  
    1040.   Result := luaL_loadfile(L, filename);  
    1041.   
    1042.   If Result = 0 Then  
    1043.      Result := lua_pcall(L, 0, LUA_MULTRET, 0);  
    1044. end;  
    1045.   
    1046. function luaL_dostring(L: lua_State; str: PAnsiChar): Integer;  
    1047. begin  
    1048.   Result := luaL_loadstring(L, str);  
    1049.     
    1050.   If Result = 0 Then  
    1051.      Result := lua_pcall(L, 0, LUA_MULTRET, 0);  
    1052. end;  
    1053.   
    1054. procedure luaL_getmetatable(L: lua_State; tname: PAnsiChar);  
    1055. begin  
    1056.    lua_getfield(L, LUA_REGISTRYINDEX, tname);  
    1057. end;  
    1058.   
    1059. procedure luaL_addchar(var B: luaL_Buffer; c: AnsiChar);  
    1060. begin  
    1061.   if Integer(B.p) < Integer(B.buffer + LUAL_BUFFERSIZE) then  
    1062.      luaL_prepbuffer(B);  
    1063.   
    1064.   B.p^ := c;  
    1065.   Inc(B.p);  
    1066. {  // original C code 
    1067. #define luaL_addchar(B,c)  
    1068.   ((void)((B)->p < ((B)->buffer+LUAL_BUFFERSIZE) || luaL_prepbuffer(B)),  
    1069.    (*(B)->p++ = (char)(c))) 
    1070. }  
    1071. end;  
    1072.   
    1073. procedure luaL_putchar(var B: luaL_Buffer; c: AnsiChar);  
    1074. begin  
    1075.   luaL_addchar(B, c);  
    1076. end;  
    1077.   
    1078. procedure luaL_addsize(var B: luaL_Buffer; n: Cardinal);  
    1079. begin  
    1080.   Inc(B.p, n);  
    1081. end;  
    1082.   
    1083. function luaL_check_lstr(L: lua_State; numArg: Integer; var ls: Cardinal): PAnsiChar;  
    1084. begin  
    1085.   Result := luaL_checklstring(L, numArg, ls);  
    1086. end;  
    1087.   
    1088. function luaL_opt_lstr(L: lua_State; numArg: Integer; def: PAnsiChar; var ls: Cardinal): PAnsiChar;  
    1089. begin  
    1090.   Result := luaL_optlstring(L, numArg, def, ls);  
    1091. end;  
    1092.   
    1093. function luaL_check_number(L: lua_State; numArg: Integer): lua_Number;  
    1094. begin  
    1095.   Result := luaL_checknumber(L, numArg);  
    1096. end;  
    1097.   
    1098. function luaL_opt_number(L: lua_State; nArg: Integer; def: lua_Number): lua_Number;  
    1099. begin  
    1100.   Result := luaL_optnumber(L, nArg, def);  
    1101. end;  
    1102.   
    1103. function luaL_arg_check(L: lua_State; cond: Boolean; numarg: Integer; extramsg: PAnsiChar): Integer;  
    1104. begin  
    1105.   Result := luaL_argcheck(L, cond, numarg, extramsg);  
    1106. end;  
    1107.   
    1108. function luaL_check_string(L: lua_State; n: Integer): PAnsiChar;  
    1109. begin  
    1110.   Result := luaL_checkstring(L, n);  
    1111. end;  
    1112.   
    1113. function luaL_opt_string(L: lua_State; n: Integer; d: PAnsiChar): PAnsiChar;  
    1114. begin  
    1115.   Result := luaL_optstring(L, n, d);  
    1116. end;  
    1117.   
    1118. function luaL_check_int(L: lua_State; n: Integer): Integer;  
    1119. begin  
    1120.   Result := luaL_checkint(L, n);  
    1121. end;  
    1122.   
    1123. function luaL_check_long(L: lua_State; n: LongInt): LongInt;  
    1124. begin  
    1125.   Result := luaL_checklong(L, n);  
    1126. end;  
    1127.   
    1128. function luaL_opt_int(L: lua_State; n, d: Integer): Integer;  
    1129. begin  
    1130.   Result := luaL_optint(L, n, d);  
    1131. end;  
    1132.   
    1133. function luaL_opt_long(L: lua_State; n: Integer; d: LongInt): LongInt;  
    1134. begin  
    1135.   Result := luaL_optlong(L, n, d);  
    1136. end;  
    1137.   
    1138. end.  
原文地址:https://www.cnblogs.com/yzryc/p/6401803.html