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

替同事做了个洛奇英雄传自动染色程序,关于屏幕取色的.因为里面他对颜色的要求比较复杂,改动也比较大,于是我让他把逻辑写在 lua 脚本里面.

[delphi]view plaincopy

  1. uses LUA, LUALIB;

[delphi]view plaincopy

  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. lua_pcall(Lua.LuaInstance, 3, 1,0) ;
  19. Result := (lua_toInteger(Lua.LuaInstance,-1) = 1);
  20. Lua.Free;
  21. end;

LUA 里面的内容是这样的

[delphi]view plaincopy

  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. return 0;
  6. end

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

lua.pas

[delphi]view plaincopy

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

lualib.pas

[delphi]view plaincopy

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

http://blog.csdn.net/warrially/article/details/7534252