delphi6直连redis服务(用lua脚本redis模块)
一、创建一个exe程序
创建一个exe程序,引用LuaRedis.pas单元(此单元自己封装的代码,目前主要封装了获取key和设置key/value功能),代码如下:

unit Unit1;interfaceusesWindows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,Dialogs, StdCtrls;typeTForm1 = class(TForm)btn2: TButton;Memo1: TMemo;edt1: TEdit;edt2: TEdit;edt3: TEdit;btn3: TButton;edt4: TEdit;procedure btn2Click(Sender: TObject);procedure btn3Click(Sender: TObject);private{ Private declarations }public{ Public declarations }end;varForm1: TForm1;implementation
usesLuaRedis;{$R *.dfm}procedure TForm1.btn2Click(Sender: TObject);
varsTmp, sErrorMsg: string;
beginMemo1.Text := '';trysTmp := getValue(Trim(edt1.Text), sErrorMsg); // value111Memo1.Text := sTmp + '->' + sErrorMsg;excepton e:Exception do beginShowMessage('【异常】' + e.Message);Exit;end;end;
end;procedure TForm1.btn3Click(Sender: TObject);
varsKey, sValue, sErrorMsg: string;
beginMemo1.Text := '';trysKey := Trim(edt2.Text);if setKeyValue(sKey, Trim(edt3.Text), StrToIntDef(Trim(edt4.Text), 0), sErrorMsg) then begin // key111 value111sValue := getValue(Trim(edt2.Text), sErrorMsg);end;Memo1.Text := sKey + ':' + sValue + '->' + sErrorMsg;excepton e:Exception do beginShowMessage('【异常】' + e.Message);Exit;end;end;
end;end.
二、封装LuaRedis.pas单元代码
上面说的封装LuaRedis.pas单元(通过registerLuaState()和destroyLuaState()来加载和销毁lua的状态机),同时引用Lua.pas, LuaLib.pas单元,代码如下:
unit LuaRedis;interfaceusesWindows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, Lua, LuaLib;constC_LuaPathFileName = './luaScript/mytest.lua';function getValue(myKey: string; var sErrorMsg: string): string;function setKeyValue(myKey, myValue: string; myExpire: Integer; var sErrorMsg: string): Boolean;varLuaObj: TLua;L: TLuaState;implementationfunction loadLuaScript(sLuaPathFileName: String; var sErrorMsg: string): Boolean;
varnTmp: Integer;
beginResult := False;trysErrorMsg := '默认提示消息(loadLuaScript)!';if not FileExists(sLuaPathFileName) then beginsErrorMsg := '未查到路径中此Lua脚本文件(' + sLuaPathFileName + ')';Exit;end;nTmp := LuaObj.DoFile(sLuaPathFileName);case nTmp ofLUA_OK : beginsErrorMsg := '成功加载路径中此Lua脚本文件!';Result := True;end;LUA_ERRSYNTAX : sErrorMsg := '在预编译Lua文件时发现了语法错误!';LUA_ERRMEM : sErrorMsg := '在执行Lua文件时内存分配错误!';LUA_ERRRUN : sErrorMsg := '在执行Lua文件中在调用函数时发生了运行时错误!';elsesErrorMsg := '加载失败路径中此Lua脚本文件!';end;excepton e:Exception do beginResult := False;sErrorMsg := '【异常】方法(loadLuaScript):' + e.Message;Exit;end;end;
end;function getValue(myKey: string; var sErrorMsg: string): string;
varnRet: Integer;
begintrysErrorMsg := '默认提示消息(getValue)!';if not loadLuaScript(C_LuaPathFileName, sErrorMsg) then beginExit;end;lua_getglobal(L, 'getValue');lua_pushstring(L, PAnsiChar(myKey));nRet := lua_pcall(L, 1, 1, 0);if nRet = 0 then beginResult := lua_toString(L, -1);sErrorMsg := 'Lua脚本正常执行';Exit;end elsesErrorMsg := 'Lua脚本执行失败!';excepton e:Exception do beginResult := '';sErrorMsg := '【异常】方法(getValue):' + e.Message;Exit;end;end;
end;function setKeyValue(myKey, myValue: string; myExpire: Integer; var sErrorMsg: string): Boolean;
varnRet: Integer;
beginResult := False;trysErrorMsg := '默认提示消息(setKeyValue)!';if not loadLuaScript(C_LuaPathFileName, sErrorMsg) then beginExit;end;lua_getglobal(L, 'setKeyValue');lua_pushstring(L, PAnsiChar(myKey));lua_pushstring(L, PAnsiChar(myValue));lua_pushinteger(L, myExpire);nRet := lua_pcall(L, 3, 1, 0);if nRet = 0 then beginResult := (lua_toInteger(L, -1) = 1);sErrorMsg := 'Lua脚本正常执行';Exit;end elsesErrorMsg := 'Lua脚本执行失败!';excepton e:Exception do beginResult := False;sErrorMsg := '【异常】方法(setKeyValue):' + e.Message;Exit;end;end;
end;procedure registerLuaState();
beginif LuaObj = nil then beginLuaObj := TLua.Create;if L = nil then beginL := LuaObj.LuaInstance;end;// luaL_openlibs 是一个由 luaL(Lua 实用程序库)提供的函数,它用于打开一组标准的Lua库。// 这些库通常包括基础库(base)、字符串处理库(string)、表操作库(table)、数学函数库(math)、I/O库(io)、操作系统库(os)等。// 这个函数通常在你的C/C++程序中与Lua交互时使用,以提供完整的Lua标准库功能。luaL_openlibs(L);end;
end;procedure destroyLuaState();
beginif LuaObj <> nil then beginif L <> nil then beginL := nil;end;LuaObj.Free;end;
end;initializationregisterLuaState();finalizationdestroyLuaState();end.
三、封装的Lua, LuaLib单元
封装的Lua, LuaLib单元,代码如下:
{
/** * @package Delphi Lua * @copyright Copyright (c) 2009 Dennis D. Spreen (http://www.spreendigital.de/blog) * @license http://opensource.org/licenses/gpl-license.php GNU Public License * @author Dennis D. Spreen <dennis@spreendigital.de> * @version 1.3 * @revision $Id: Lua.pas 102 2009-09-30 11:39:41Z dennis.spreen $ */ History
1.3 DS Improved Callback, now uses pointer instead of object index Modified RegisterFunctions to allow methods from other class to be registered, moved object table into TLua class
1.2 DS Added example on how to extend lua with a delphi dll
1.1 DS Improved global object table, this optimizes the delphi function calls
1.0 DS Initial Release Copyright 2009 Dennis D. Spreen (email : dennis@spreendigital.de) This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version. This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details. You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
} unit Lua; interface uses Classes, LuaLib; type TLuaState = Lua_State; TLua = class(TObject) private fAutoRegister: Boolean; CallbackList: TList; // internal callback list public LuaInstance: TLuaState; // Lua instance constructor Create(AutoRegister: Boolean = True); overload; virtual; destructor Destroy; override; function DoFile(Filename: String): Integer; virtual;// load file and execute procedure RegisterFunction(FuncName: AnsiString; MethodName: AnsiString = ''; Obj: TObject = NIL); virtual; //register function procedure AutoRegisterFunctions(Obj: TObject); // register all published functions procedure UnregisterFunctions(Obj: TObject); // unregister all object functions end; implementation type TProc = function(L: TLuaState): Integer of object; // Lua Function TCallback = class Routine: TMethod; // Code and Data for the method Exec: TProc; // Resulting execution function end; //
// This function is called by Lua, it extracts the object by
// pointer to the objects method by name, which is then called.
//
// @param Lua_State L Pointer to Lua instance
// @return Integer Number of result arguments on stack
//
function LuaCallBack(L: Lua_State): Integer; cdecl;
var CallBack: TCallBack; // The Object stored in the Object Table
begin // Retrieve first Closure Value (=Object Pointer) CallBack := lua_topointer(L, lua_upvalueindex(1)); // Execute only if Object is valid if (assigned(CallBack) and assigned(CallBack.Exec)) then Result := CallBack.Exec(L) else Result := 0;
end; { TLua } //
// Create a new Lua instance and optionally create Lua functions
//
// @param Boolean AutoRegister (optional)
// @return TLua Lua Instance
//
constructor TLua.Create(AutoRegister: Boolean = True);
begin inherited Create; // Load Lua Lib if not already done if (not LuaLibLoaded) then LoadLuaLib(); // Open Library LuaInstance := Lua_Open(); luaopen_base(LuaInstance); fAutoRegister := AutoRegister; // Create Object List on initialization CallBackList := TList.Create; // if set then register published functions if (AutoRegister) then AutoRegisterFunctions(self);
end; //
// Dispose Lua instance
//
destructor TLua.Destroy;
begin // Unregister all functions if previously autoregistered if (fAutoRegister) then UnregisterFunctions(Self); // dispose Object List on finalization CallBackList.Free; // Close instance Lua_Close(LuaInstance); inherited;
end; //
// Wrapper for Lua File load and Execution
//
// @param String Filename Lua Script file name
// @return Integer
//
function TLua.DoFile(Filename: String): Integer;
begin Result := lual_dofile(LuaInstance, PAnsiChar(AnsiString(Filename)));
end; //
// Register a new Lua Function and map it to the Objects method name
//
// @param AnsiString FuncName Lua Function Name
// @param AnsiString MethodName (optional) Objects Method name
//
procedure TLua.RegisterFunction(FuncName: AnsiString; MethodName: AnsiString = ''; Obj: TObject = NIL);
var CallBack: TCallBack; // Callback Object
begin // if method name not specified use Lua function name if (MethodName = '') then MethodName := FuncName; // if not object specified use this object if (Obj = NIL) then Obj := Self; // Add Callback Object to the Object Index CallBack := TCallBack.Create; CallBack.Routine.Data := Obj; CallBack.Routine.Code := Obj.MethodAddress(String(MethodName)); CallBack.Exec := TProc(CallBack.Routine); CallbackList.Add(CallBack); // prepare Closure value (Method Name) lua_pushstring(LuaInstance, PAnsiChar(FuncName)); // prepare Closure value (CallBack Object Pointer) lua_pushlightuserdata(LuaInstance, CallBack); // set new Lua function with Closure value lua_pushcclosure(LuaInstance, LuaCallBack, 1); lua_settable(LuaInstance, LUA_GLOBALSINDEX);
end; //
// UnRegister all new Lua Function
//
// @param TObject Object Object with prev registered lua functions
//
procedure TLua.UnregisterFunctions(Obj: TObject);
var I: Integer; CallBack: TCallBack;
begin // remove obj from object list for I := CallBackList.Count downto 1 do begin CallBack := CallBackList[I-1]; if (assigned(CallBack)) and (CallBack.Routine.Data = Obj) then begin CallBack.Free; CallBackList.Items[I-1] := NIL; CallBackList.Delete(I-1); end; end;
end; //
// Register all published methods as Lua Functions
//
procedure TLua.AutoRegisterFunctions(Obj: TObject);
type PPointer = ^Pointer; PMethodRec = ^TMethodRec; TMethodRec = packed record wSize: Word; pCode: Pointer; sName: ShortString; end;
var MethodTable: PAnsiChar; MethodRec: PMethodRec; wCount: Word; nMethod: Integer;
begin // Get a pointer to the class's published method table MethodTable := PAnsiChar(Pointer(PAnsiChar(Obj.ClassType) + vmtMethodTable)^); if (MethodTable <> Nil) then begin // Get the count of the methods in the table Move(MethodTable^, wCount, 2); // Position the MethodRec pointer at the first method in the table // (skip over the 2-byte method count) MethodRec := PMethodRec(MethodTable + 2); // Iterate through all the published methods of this class for nMethod := 0 to wCount - 1 do begin // Add the method name to the lua functions RegisterFunction(MethodRec.sName, MethodRec.sName, Obj); // Skip to the next method MethodRec := PMethodRec(PAnsiChar(MethodRec) + MethodRec.wSize); end; end;
end; end.
(******************************************************************************
* Original copyright for the lua source and headers:
* 1994-2004 Tecgraf, PUC-Rio.
* www.lua.org.
*
* Copyright for the Delphi adaptation:
* 2005 Rolf Meyerhoff
* www.matrix44.de
*
* Copyright for the Lua 5.1 adaptation:
* 2007 Marco Antonio Abreu
* www.marcoabreu.eti.br
*
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
******************************************************************************)
unit LuaLib; interface const LUA_VERSION = 'Lua 5.1'; LUA_RELEASE = 'Lua 5.1.2'; LUA_COPYRIGHT = 'Copyright (C) 1994-2004 Tecgraf, PUC-Rio'; LUA_AUTHORS = 'R. Ierusalimschy, L. H. de Figueiredo & W. Celes'; LUA_PASCAL_51_AUTHOR = 'Marco Antonio Abreu'; LUA_PASCAL_51_COPYRIGHT = 'Copyright (C) 2007 Marco Antonio Abreu'; (* mark for precompiled code (`<esc>Lua') *) LUA_SIGNATURE = #27'Lua'; (* option for multiple returns in `lua_pcall' and `lua_call' *) LUA_MULTRET = -1; (* ** pseudo-indices *) LUA_REGISTRYINDEX = -10000; LUA_ENVIRONINDEX = -10001; LUA_GLOBALSINDEX = -10002; (* thread status; 0 is OK *)LUA_OK = 0; // (通常定义为 0):文件已成功加载,但尚未执行。此时,你可以使用 lua_pcall 或 lua_call 来执行它。LUA_TRD_YIELD = 1;LUA_ERRRUN = 2; // 在运行错误处理函数(由 lua_atpanic 设置)时发生了错误LUA_ERRSYNTAX = 3; // 在预编译文件时发现了语法错误。LUA_ERRMEM = 4; // 内存分配错误。LUA_ERRERR = 5; (* extra error code for `luaL_load' *) LUA_ERRFILE = LUA_ERRERR + 1; (* ** basic types *) LUA_TNONE = -1; LUA_TNIL = 0; LUA_TBOOLEAN = 1; LUA_TLIGHTUSERDATA = 2; LUA_TNUMBER = 3; LUA_TSTRING = 4; LUA_TTABLE = 5; LUA_TFUNCTION = 6; LUA_TUSERDATA = 7; LUA_TTHREAD = 8; (* minimum Lua stack available to a C function *) LUA_MINSTACK = 20; (* ** garbage-collection function and options *) LUA_GCSTOP = 0; LUA_GCRESTART = 1; LUA_GCCOLLECT = 2; LUA_GCCOUNT = 3; LUA_GCCOUNTB = 4; LUA_GCSTEP = 5; LUA_GCSETPAUSE = 6; LUA_GCSETSTEPMUL = 7; (* ** {====================================================================== ** Debug API ** ======================================================================= *) (* ** Event codes *) LUA_HOOKCALL = 0; LUA_HOOKRET = 1; LUA_HOOKLINE = 2; LUA_HOOKCOUNT = 3; LUA_HOOKTAILRET = 4; (* ** Event masks *) LUA_MASKCALL = (1 shl LUA_HOOKCALL); LUA_MASKRET = (1 shl LUA_HOOKRET); LUA_MASKLINE = (1 shl LUA_HOOKLINE); LUA_MASKCOUNT = (1 shl LUA_HOOKCOUNT); (* ** {====================================================================== ** useful definitions for Lua kernel and libraries ** ======================================================================= *) (* @@ LUA_NUMBER_SCAN is the format for reading numbers. @@ LUA_NUMBER_FMT is the format for writing numbers. @@ lua_number2str converts a number to a string. @@ LUAI_MAXNUMBER2STR is maximum size of previous conversion. @@ lua_str2number converts a string to a number. *) LUA_NUMBER_SCAN = '%lf'; LUA_NUMBER_FMT = '%.14g'; LUAI_MAXNUMBER2STR = 32; (* 16 digits, sign, point, and \0 *) (* pre-defined references *) LUA_NOREF = -2; LUA_REFNIL = -1; LUA_IDSIZE = 60; (* ** package names *) LUA_COLIBNAME = 'coroutine'; LUA_TABLIBNAME = 'table'; LUA_IOLIBNAME = 'io'; LUA_OSLIBNAME = 'os'; LUA_STRLIBNAME = 'string'; LUA_MATHLIBNAME = 'math'; LUA_DBLIBNAME = 'debug'; LUA_LOADLIBNAME = 'package'; (* ** {====================================================== ** Generic Buffer manipulation ** ======================================================= *) BUFSIZ = 512; (* From stdio.h *) LUAL_BUFFERSIZE = BUFSIZ; type lua_State = type Pointer; lua_CFunction = function(L: lua_State): Integer; cdecl; (* ** functions that read/write blocks when loading/dumping Lua chunks *) lua_Reader = function(L: lua_State; data: Pointer; var size: Cardinal): PAnsiChar; cdecl; lua_Writer = function(L: lua_State; p: Pointer; sz: Cardinal; ud: Pointer): Integer; cdecl; (* ** prototype for memory-allocation functions *) lua_Alloc = function(ud, ptr: Pointer; osize, nsize: Cardinal): Pointer; cdecl; (* type of numbers in Lua *) lua_Number = type double; (* type for integer functions *) lua_Integer = type integer; lua_Debug = packed record event: Integer; name: PAnsiChar; (* (n) *) namewhat: PAnsiChar; (* (n) `global', `local', `field', `method' *) what: PAnsiChar; (* (S) `Lua', `C', `main', `tail' *) source: PAnsiChar; (* (S) *) currentline: Integer; (* (l) *) nups: Integer; (* (u) number of upvalues *) linedefined: Integer; (* (S) *) lastlinedefine: Integer; (* (S) *) short_src: array[0..LUA_IDSIZE - 1] of AnsiChar; (* (S) *) (* private part *) i_ci: Integer; (* active function *) end; (* Functions to be called by the debuger in specific events *) lua_Hook = procedure(L: lua_State; var ar: lua_Debug); cdecl; (* Lua Record *) PluaL_reg = ^luaL_reg; luaL_reg = packed record name: PAnsiChar; func: lua_CFunction; end; (* ** {====================================================== ** Generic Buffer manipulation ** ======================================================= *) luaL_Buffer = packed record p: PAnsiChar; (* current position in buffer *) lvl: Integer; (* number of strings in the stack (level) *) L: lua_State; buffer: array[0..LUAL_BUFFERSIZE - 1] of AnsiChar; end; var (* ** state manipulation *) lua_newstate: function(f: lua_Alloc; ud: Pointer): lua_State; cdecl; lua_close: procedure(L: lua_State); cdecl; lua_newthread: function(L: lua_State): lua_State; cdecl; lua_atpanic: function(L: lua_State; panicf: lua_CFunction): lua_CFunction; cdecl; (* ** basic stack manipulation *) lua_gettop: function(L: lua_State): Integer; cdecl; lua_settop: procedure(L: lua_State; idx: Integer); cdecl; lua_pushvalue: procedure(L: lua_State; idx: Integer); cdecl; lua_remove: procedure(L: lua_State; idx: Integer); cdecl; lua_insert: procedure(L: lua_State; idx: Integer); cdecl; lua_replace: procedure(L: lua_State; idx: Integer); cdecl; lua_checkstack: function(L: lua_State; extra: Integer): LongBool; cdecl; lua_xmove: procedure(from, dest: lua_State; n: Integer); cdecl; (* ** access functions (stack -> C/Pascal) *) lua_isnumber: function(L: lua_State; idx: Integer): LongBool; cdecl; lua_isstring: function(L: lua_State; idx: Integer): LongBool; cdecl; lua_iscfunction: function(L: lua_State; idx: Integer): LongBool; cdecl; lua_isuserdata: function(L: lua_State; idx: Integer): LongBool; cdecl; lua_type: function(L: lua_State; idx: Integer): Integer; cdecl; lua_typename: function(L: lua_State; tp: Integer): PAnsiChar; cdecl; lua_equal: function(L: lua_State; idx1, idx2: Integer): LongBool; cdecl; lua_rawequal: function(L: lua_State; idx1, idx2: Integer): LongBool; cdecl; lua_lessthan: function(L: lua_State; idx1, idx2: Integer): LongBool; cdecl; lua_tonumber: function(L: lua_State; idx: Integer): lua_Number; cdecl; lua_tointeger: function(L: lua_State; idx: Integer): lua_Integer; cdecl; lua_toboolean: function(L: lua_State; idx: Integer): LongBool; cdecl; lua_tolstring: function(L: lua_State; idx: Integer; var len: Cardinal): PAnsiChar; cdecl; lua_objlen: function(L: lua_State; idx: Integer): Cardinal; cdecl; lua_tocfunction: function(L: lua_State; idx: Integer): lua_CFunction; cdecl; lua_touserdata: function(L: lua_State; idx: Integer): Pointer; cdecl; lua_tothread: function(L: lua_State; idx: Integer): lua_State; cdecl; lua_topointer: function(L: lua_State; idx: Integer): Pointer; cdecl; (* ** push functions (C/Pascal -> stack) *) lua_pushnil: procedure(L: lua_State); cdecl; lua_pushnumber: procedure(L: lua_State; n: lua_Number); cdecl; lua_pushinteger: procedure(L: lua_State; n: lua_Integer); cdecl; lua_pushlstring: procedure(L: lua_State; s: PAnsiChar; len: Cardinal); cdecl; lua_pushstring: procedure(L: lua_State; s: PAnsiChar); cdecl; lua_pushvfstring: function(L: lua_State; fmt, argp: PAnsiChar): PAnsiChar; cdecl; lua_pushfstring: function(L: lua_State; fmt: PAnsiChar; args: array of const): PAnsiChar; cdecl; lua_pushcclosure: procedure(L: lua_State; fn: lua_CFunction; n: Integer); cdecl; lua_pushboolean: procedure(L: lua_State; b: LongBool); cdecl; lua_pushlightuserdata: procedure(L: lua_State; p: Pointer); cdecl; lua_pushthread: function(L: lua_State): Integer; cdecl; (* ** get functions (Lua -> stack) *) lua_gettable: procedure(L: lua_State; idx: Integer); cdecl; lua_getfield: procedure(L: lua_State; idx: Integer; k: PAnsiChar); cdecl; lua_rawget: procedure(L: lua_State; idx: Integer); cdecl; lua_rawgeti: procedure(L: lua_State; idx, n: Integer); cdecl; lua_createtable: procedure(L: lua_State; narr, nrec: Integer); cdecl; lua_newuserdata: function(L: lua_State; size: Cardinal): Pointer; cdecl; lua_getmetatable: function(L: lua_State; idx: Integer): LongBool; cdecl; lua_getfenv: procedure(L: lua_State; idx: Integer); cdecl; (* ** set functions (stack -> Lua) *) lua_settable: procedure(L: lua_State; idx: Integer); cdecl; lua_setfield: procedure(L: lua_State; idx: Integer; k: PAnsiChar ); cdecl; lua_rawset: procedure(L: lua_State; idx: Integer); cdecl; lua_rawseti: procedure(L: lua_State; idx, n: Integer); cdecl; lua_setmetatable: function(L: lua_State; idx: Integer): LongBool; cdecl; lua_setfenv: function(L: lua_State; idx: Integer): LongBool; cdecl; (* ** `load' and `call' functions (load and run Lua code) *) lua_call: procedure(L: lua_State; nargs, nresults: Integer); cdecl; lua_pcall: function(L: lua_State; nargs, nresults, errfunc: Integer): Integer; cdecl; lua_cpcall: function(L: lua_State; func: lua_CFunction; ud: Pointer): Integer; cdecl; lua_load: function(L: lua_State; reader: lua_Reader; data: Pointer; chunkname: PAnsiChar): Integer; cdecl; lua_dump: function(L: lua_State; writer: lua_Writer; data: Pointer): Integer; cdecl; (* ** coroutine functions *) lua_yield: function(L: lua_State; nresults: Integer): Integer; cdecl; lua_resume: function(L: lua_State; narg: Integer): Integer; cdecl; lua_status: function(L: lua_State): Integer; cdecl; (* ** garbage-collection functions *) lua_gc: function(L: lua_State; what, data: Integer): Integer; cdecl; (* ** miscellaneous functions *) lua_error: function(L: lua_State): Integer; cdecl; lua_next: function(L: lua_State; idx: Integer): Integer; cdecl; lua_concat: procedure(L: lua_State; n: Integer); cdecl; lua_getallocf: function(L: lua_State; ud: Pointer): lua_Alloc; cdecl; lua_setallocf: procedure(L: lua_State; f: lua_Alloc; ud: Pointer); cdecl; (* ** {====================================================================== ** Debug API ** ======================================================================= *) lua_getstack: function(L: lua_State; level: Integer; var ar: lua_Debug): Integer; cdecl; lua_getinfo: function(L: lua_State; what: PAnsiChar; var ar: lua_Debug): Integer; cdecl; lua_getlocal: function(L: lua_State; var ar: lua_Debug; n: Integer): PAnsiChar; cdecl; lua_setlocal: function(L: lua_State; var ar: lua_Debug; n: Integer): PAnsiChar; cdecl; lua_getupvalue: function(L: lua_State; funcindex, n: Integer): PAnsiChar; cdecl; lua_setupvalue: function(L: lua_State; funcindex, n: Integer): PAnsiChar; cdecl; lua_sethook: function(L: lua_State; func: lua_Hook; mask, count: Integer): Integer; cdecl; lua_gethook: function(L: lua_State): lua_Hook; cdecl; lua_gethookmask: function(L: lua_State): Integer; cdecl; lua_gethookcount: function(L: lua_State): Integer; cdecl; (* lua libraries *) luaopen_base: function(L: lua_State): Integer; cdecl; luaopen_debug: function(L: lua_State): Integer; cdecl; luaopen_io: function(L: lua_State): Integer; cdecl; luaopen_math: function(L: lua_State): Integer; cdecl; luaopen_os: function(L: lua_State): Integer; cdecl; luaopen_package: function(L: lua_State): Integer; cdecl; luaopen_string: function(L: lua_State): Integer; cdecl; luaopen_table: function(L: lua_State): Integer; cdecl; (* open all previous libraries *) luaL_openlibs: procedure(L: lua_State); cdecl; luaL_register: procedure(L: lua_State; libname: PAnsiChar; lr: PluaL_reg); cdecl; luaL_getmetafield: function(L: lua_State; obj: Integer; e: PAnsiChar): Integer; cdecl; luaL_callmeta: function(L: lua_State; obj: Integer; e: PAnsiChar): Integer; cdecl; luaL_typerror: function(L: lua_State; narg: Integer; tname: PAnsiChar): Integer; cdecl; luaL_argerror: function(L: lua_State; narg: Integer; extramsg: PAnsiChar): Integer; cdecl; luaL_checklstring: function(L: lua_State; narg: Integer; var len: Cardinal): PAnsiChar; cdecl; luaL_optlstring: function(L: lua_State; narg: Integer; d: PAnsiChar; var len: Cardinal): PAnsiChar; cdecl; luaL_checknumber: function(L: lua_State; narg: Integer): lua_Number; cdecl; luaL_optnumber: function(L: lua_State; narg: Integer; d: lua_Number): lua_Number; cdecl; luaL_checkinteger: function(L: lua_State; narg: Integer): lua_Integer; cdecl; luaL_optinteger: function(L: lua_State; narg: Integer; d: lua_Integer): lua_Integer; cdecl; luaL_checkstack: procedure(L: lua_State; sz: Integer; msg: PAnsiChar); cdecl; luaL_checktype: procedure(L: lua_State; narg, t: Integer); cdecl; luaL_checkany: procedure(L: lua_State; narg: Integer); cdecl; luaL_newmetatable: function(L: lua_State; tname: PAnsiChar): Integer; cdecl; luaL_checkudata: function(L: lua_State; narg: Integer; tname: PAnsiChar): Pointer; cdecl; luaL_checkoption: function(L: lua_State; narg: Integer; def: PAnsiChar; lst: array of PAnsiChar): Integer; cdecl; luaL_where: procedure(L: lua_State; lvl: Integer); cdecl; luaL_error: function(L: lua_State; fmt: PAnsiChar; args: array of const): Integer; cdecl; luaL_ref: function(L: lua_State; t: Integer): Integer; cdecl; luaL_unref: procedure(L: lua_State; t, ref: Integer); cdecl; {$ifdef LUA_COMPAT_GETN} luaL_getn: function(L: lua_State; t: Integer): Integer; cdecl; luaL_setn: procedure(L: lua_State; t, n: Integer); cdecl;
{$endif} luaL_loadfile: function(L: lua_State; filename: PAnsiChar): Integer; cdecl; luaL_loadbuffer: function(L: lua_State; buff: PAnsiChar; sz: Cardinal; name: PAnsiChar): Integer; cdecl; luaL_loadstring: function(L: lua_State; s: PAnsiChar): Integer; cdecl; luaL_newstate: function(): lua_State; cdecl; luaL_gsub: function(L: lua_State; s, p, r: PAnsiChar): PAnsiChar; cdecl; luaL_findtable: function(L: lua_State; idx: Integer; fname: PAnsiChar; szhint: Integer): PAnsiChar; cdecl; luaL_buffinit: procedure(L: lua_State; var B: luaL_Buffer); cdecl; luaL_prepbuffer: function(var B: luaL_Buffer): PAnsiChar; cdecl; luaL_addlstring: procedure(var B: luaL_Buffer; s: PAnsiChar; l: Cardinal); cdecl; luaL_addstring: procedure(var B: luaL_Buffer; s: PAnsiChar); cdecl; luaL_addvalue: procedure(var B: luaL_Buffer); cdecl; luaL_pushresult: procedure(var B: luaL_Buffer); cdecl; (* ** =============================================================== ** some useful macros ** =============================================================== *) {$ifndef LUA_COMPAT_GETN} function luaL_getn(L: lua_State; t: Integer): Integer; procedure luaL_setn(L: lua_State; t, n: Integer);
{$endif} (* pseudo-indices *) function lua_upvalueindex(i: Integer): Integer; (* to help testing the libraries *) procedure lua_assert(c: Boolean); function lua_number2str(s: Lua_Number; n: Integer): String; function lua_str2number(s: String; p: integer): Lua_Number; (* argument and parameters checks *) function luaL_argcheck(L: lua_State; cond: Boolean; narg: Integer; extramsg: PAnsiChar): Integer; function luaL_checkstring(L: lua_State; narg: Integer): PAnsiChar; function luaL_optstring(L: lua_State; narg: Integer; d: PAnsiChar): PAnsiChar; function luaL_checkint(L: lua_State; narg: Integer): Integer; function luaL_optint(L: lua_State; narg, d: Integer): Integer; function luaL_checklong(L: lua_State; narg: Integer): LongInt; function luaL_optlong(L: lua_State; narg: Integer; d: LongInt): LongInt; function luaL_typename(L: lua_State; idx: Integer): PAnsiChar; function luaL_dofile(L: lua_State; filename: PAnsiChar): Integer; function luaL_dostring(L: lua_State; str: PAnsiChar): Integer; procedure luaL_getmetatable(L: lua_State; tname: PAnsiChar); (* Generic Buffer manipulation *) procedure luaL_addchar(var B: luaL_Buffer; c: AnsiChar); procedure luaL_putchar(var B: luaL_Buffer; c: AnsiChar); procedure luaL_addsize(var B: luaL_Buffer; n: Cardinal); function luaL_check_lstr(L: lua_State; numArg: Integer; var ls: Cardinal): PAnsiChar; function luaL_opt_lstr(L: lua_State; numArg: Integer; def: PAnsiChar; var ls: Cardinal): PAnsiChar; function luaL_check_number(L: lua_State; numArg: Integer): lua_Number; function luaL_opt_number(L: lua_State; nArg: Integer; def: lua_Number): lua_Number; function luaL_arg_check(L: lua_State; cond: Boolean; numarg: Integer; extramsg: PAnsiChar): Integer; function luaL_check_string(L: lua_State; n: Integer): PAnsiChar; function luaL_opt_string(L: lua_State; n: Integer; d: PAnsiChar): PAnsiChar; function luaL_check_int(L: lua_State; n: Integer): Integer; function luaL_check_long(L: lua_State; n: LongInt): LongInt; function luaL_opt_int(L: lua_State; n, d: Integer): Integer; function luaL_opt_long(L: lua_State; n: Integer; d: LongInt): LongInt; procedure lua_pop(L: lua_State; n: Integer); procedure lua_newtable(L: lua_State); procedure lua_register(L: lua_state; name: PAnsiChar; f: lua_CFunction); procedure lua_pushcfunction(L: lua_State; f: lua_CFunction); function lua_strlen(L: lua_State; i: Integer): Cardinal; function lua_isfunction(L: lua_State; idx: Integer): Boolean; function lua_istable(L: lua_State; idx: Integer): Boolean; function lua_islightuserdata(L: lua_State; idx: Integer): Boolean; function lua_isnil(L: lua_State; idx: Integer): Boolean; function lua_isboolean(L: lua_State; idx: Integer): Boolean; function lua_isthread(L: lua_State; idx: Integer): Boolean; function lua_isnone(L: lua_State; idx: Integer): Boolean; function lua_isnoneornil(L: lua_State; idx: Integer): Boolean; procedure lua_pushliteral(L: lua_State; s: PAnsiChar); procedure lua_setglobal(L: lua_State; name: PAnsiChar); procedure lua_getglobal(L: lua_State; name: PAnsiChar); function lua_tostring(L: lua_State; idx: Integer): PAnsiChar; (* ** compatibility macros and functions *) function lua_open(): lua_State; procedure lua_getregistry(L: lua_State); function lua_getgccount(L: lua_State): Integer; (* compatibility with ref system *) function lua_ref(L: lua_State; lock: Boolean): Integer; procedure lua_unref(L: lua_State; ref: Integer); procedure lua_getref(L: lua_State; ref: Integer); (* ** Dynamic library manipulation *) function GetProcAddr( fHandle: THandle; const methodName: String; bErrorIfNotExists: Boolean = True ): Pointer; procedure SetLuaLibFileName( newLuaLibFileName: String ); function GetLuaLibFileName(): String; function LoadLuaLib( newLuaLibFileName: String = '' ): Integer; procedure FreeLuaLib(); function LuaLibLoaded: Boolean; implementation uses SysUtils, Math,
{$ifdef MSWINDOWS} Windows
{$endif}
; var fLibHandle: Integer = 0;
{$ifdef MSWINDOWS} fLuaLibFileName: String = 'Lua5.1.dll';
{$endif}
{$ifdef LINUX} fLuaLibFileName: String = 'liblua.so.5.1';
{$endif} (*
** Dynamic library manipulation
*) function GetProcAddr( fHandle: THandle; const methodName: String; bErrorIfNotExists: Boolean = True ): Pointer;
begin Result := GetProcAddress( fHandle, PAnsiChar( AnsiString(methodName) ) ); if bErrorIfNotExists and ( Result = nil ) then Raise Exception.Create( 'Cannot load method ' + QuotedStr( methodName ) + ' from dynamic library.' );
end; procedure SetLuaLibFileName( newLuaLibFileName: String );
begin fLuaLibFileName := newLuaLibFileName;
end; function GetLuaLibFileName(): String;
begin Result := fLuaLibFileName;
end; function LuaLibLoaded: Boolean;
begin Result := fLibHandle <> 0;
end; function LoadLuaLib(newLuaLibFileName: String): Integer;
begin FreeLuaLib(); if newLuaLibFileName <> '' then SetLuaLibFileName( newLuaLibFileName ); if not FileExists( GetLuaLibFileName() ) then begin Result := -1; exit; end; // fLibHandle := LoadLibrary(PWideChar( (GetLuaLibFileName() ) )); fLibHandle := LoadLibrary(PAnsiChar( (GetLuaLibFileName() ) ));if fLibHandle = 0 then begin Result := -2; exit; end; lua_newstate := GetProcAddr( fLibHandle, 'lua_newstate' ); lua_close := GetProcAddr( fLibHandle, 'lua_close' ); lua_newthread := GetProcAddr( fLibHandle, 'lua_newthread' ); lua_atpanic := GetProcAddr( fLibHandle, 'lua_atpanic' ); lua_gettop := GetProcAddr( fLibHandle, 'lua_gettop' ); lua_settop := GetProcAddr( fLibHandle, 'lua_settop' ); lua_pushvalue := GetProcAddr( fLibHandle, 'lua_pushvalue' ); lua_remove := GetProcAddr( fLibHandle, 'lua_remove' ); lua_insert := GetProcAddr( fLibHandle, 'lua_insert' ); lua_replace := GetProcAddr( fLibHandle, 'lua_replace' ); lua_checkstack := GetProcAddr( fLibHandle, 'lua_checkstack' ); lua_xmove := GetProcAddr( fLibHandle, 'lua_xmove' ); lua_isnumber := GetProcAddr( fLibHandle, 'lua_isnumber' ); lua_isstring := GetProcAddr( fLibHandle, 'lua_isstring' ); lua_iscfunction := GetProcAddr( fLibHandle, 'lua_iscfunction' ); lua_isuserdata := GetProcAddr( fLibHandle, 'lua_isuserdata' ); lua_type := GetProcAddr( fLibHandle, 'lua_type' ); lua_typename := GetProcAddr( fLibHandle, 'lua_typename' ); lua_equal := GetProcAddr( fLibHandle, 'lua_equal' ); lua_rawequal := GetProcAddr( fLibHandle, 'lua_rawequal' ); lua_lessthan := GetProcAddr( fLibHandle, 'lua_lessthan' ); lua_tonumber := GetProcAddr( fLibHandle, 'lua_tonumber' ); lua_tointeger := GetProcAddr( fLibHandle, 'lua_tointeger' ); lua_toboolean := GetProcAddr( fLibHandle, 'lua_toboolean' ); lua_tolstring := GetProcAddr( fLibHandle, 'lua_tolstring' ); lua_objlen := GetProcAddr( fLibHandle, 'lua_objlen' ); lua_tocfunction := GetProcAddr( fLibHandle, 'lua_tocfunction' ); lua_touserdata := GetProcAddr( fLibHandle, 'lua_touserdata' ); lua_tothread := GetProcAddr( fLibHandle, 'lua_tothread' ); lua_topointer := GetProcAddr( fLibHandle, 'lua_topointer' ); lua_pushnil := GetProcAddr( fLibHandle, 'lua_pushnil' ); lua_pushnumber := GetProcAddr( fLibHandle, 'lua_pushnumber' ); lua_pushinteger := GetProcAddr( fLibHandle, 'lua_pushinteger' ); lua_pushlstring := GetProcAddr( fLibHandle, 'lua_pushlstring' ); lua_pushstring := GetProcAddr( fLibHandle, 'lua_pushstring' ); lua_pushvfstring := GetProcAddr( fLibHandle, 'lua_pushvfstring' ); lua_pushfstring := GetProcAddr( fLibHandle, 'lua_pushfstring' ); lua_pushcclosure := GetProcAddr( fLibHandle, 'lua_pushcclosure' ); lua_pushboolean := GetProcAddr( fLibHandle, 'lua_pushboolean' ); lua_pushlightuserdata := GetProcAddr( fLibHandle, 'lua_pushlightuserdata' ); lua_pushthread := GetProcAddr( fLibHandle, 'lua_pushthread' ); lua_gettable := GetProcAddr( fLibHandle, 'lua_gettable' ); lua_getfield := GetProcAddr( fLibHandle, 'lua_getfield' ); lua_rawget := GetProcAddr( fLibHandle, 'lua_rawget' ); lua_rawgeti := GetProcAddr( fLibHandle, 'lua_rawgeti' ); lua_createtable := GetProcAddr( fLibHandle, 'lua_createtable' ); lua_newuserdata := GetProcAddr( fLibHandle, 'lua_newuserdata' ); lua_getmetatable := GetProcAddr( fLibHandle, 'lua_getmetatable' ); lua_getfenv := GetProcAddr( fLibHandle, 'lua_getfenv' ); lua_settable := GetProcAddr( fLibHandle, 'lua_settable' ); lua_setfield := GetProcAddr( fLibHandle, 'lua_setfield' ); lua_rawset := GetProcAddr( fLibHandle, 'lua_rawset' ); lua_rawseti := GetProcAddr( fLibHandle, 'lua_rawseti' ); lua_setmetatable := GetProcAddr( fLibHandle, 'lua_setmetatable' ); lua_setfenv := GetProcAddr( fLibHandle, 'lua_setfenv' ); lua_call := GetProcAddr( fLibHandle, 'lua_call' ); lua_pcall := GetProcAddr( fLibHandle, 'lua_pcall' ); lua_cpcall := GetProcAddr( fLibHandle, 'lua_cpcall' ); lua_load := GetProcAddr( fLibHandle, 'lua_load' ); lua_dump := GetProcAddr( fLibHandle, 'lua_dump' ); lua_yield := GetProcAddr( fLibHandle, 'lua_yield' ); lua_resume := GetProcAddr( fLibHandle, 'lua_resume' ); lua_status := GetProcAddr( fLibHandle, 'lua_status' ); lua_gc := GetProcAddr( fLibHandle, 'lua_gc' ); lua_error := GetProcAddr( fLibHandle, 'lua_error' ); lua_next := GetProcAddr( fLibHandle, 'lua_next' ); lua_concat := GetProcAddr( fLibHandle, 'lua_concat' ); lua_getallocf := GetProcAddr( fLibHandle, 'lua_getallocf' ); lua_setallocf := GetProcAddr( fLibHandle, 'lua_setallocf' ); lua_getstack := GetProcAddr( fLibHandle, 'lua_getstack' ); lua_getinfo := GetProcAddr( fLibHandle, 'lua_getinfo' ); lua_getlocal := GetProcAddr( fLibHandle, 'lua_getlocal' ); lua_setlocal := GetProcAddr( fLibHandle, 'lua_setlocal' ); lua_getupvalue := GetProcAddr( fLibHandle, 'lua_getupvalue' ); lua_setupvalue := GetProcAddr( fLibHandle, 'lua_setupvalue' ); lua_sethook := GetProcAddr( fLibHandle, 'lua_sethook' ); lua_gethook := GetProcAddr( fLibHandle, 'lua_gethook' ); lua_gethookmask := GetProcAddr( fLibHandle, 'lua_gethookmask' ); lua_gethookcount := GetProcAddr( fLibHandle, 'lua_gethookcount' ); luaopen_base := GetProcAddr( fLibHandle, 'luaopen_base' ); luaopen_table := GetProcAddr( fLibHandle, 'luaopen_table' ); luaopen_io := GetProcAddr( fLibHandle, 'luaopen_io' ); luaopen_os := GetProcAddr( fLibHandle, 'luaopen_os' ); luaopen_string := GetProcAddr( fLibHandle, 'luaopen_string' ); luaopen_math := GetProcAddr( fLibHandle, 'luaopen_math' ); luaopen_debug := GetProcAddr( fLibHandle, 'luaopen_debug' ); luaopen_package := GetProcAddr( fLibHandle, 'luaopen_package' ); luaL_openlibs := GetProcAddr( fLibHandle, 'luaL_openlibs' ); luaL_register := GetProcAddr( fLibHandle, 'luaL_register' ); luaL_getmetafield := GetProcAddr( fLibHandle, 'luaL_getmetafield' ); luaL_callmeta := GetProcAddr( fLibHandle, 'luaL_callmeta' ); luaL_typerror := GetProcAddr( fLibHandle, 'luaL_typerror' ); luaL_argerror := GetProcAddr( fLibHandle, 'luaL_argerror' ); luaL_checklstring := GetProcAddr( fLibHandle, 'luaL_checklstring' ); luaL_optlstring := GetProcAddr( fLibHandle, 'luaL_optlstring' ); luaL_checknumber := GetProcAddr( fLibHandle, 'luaL_checknumber' ); luaL_optnumber := GetProcAddr( fLibHandle, 'luaL_optnumber' ); luaL_checkinteger := GetProcAddr( fLibHandle, 'luaL_checkinteger' ); luaL_optinteger := GetProcAddr( fLibHandle, 'luaL_optinteger' ); luaL_checkstack := GetProcAddr( fLibHandle, 'luaL_checkstack' ); luaL_checktype := GetProcAddr( fLibHandle, 'luaL_checktype' ); luaL_checkany := GetProcAddr( fLibHandle, 'luaL_checkany' ); luaL_newmetatable := GetProcAddr( fLibHandle, 'luaL_newmetatable' ); luaL_checkudata := GetProcAddr( fLibHandle, 'luaL_checkudata' ); luaL_where := GetProcAddr( fLibHandle, 'luaL_where' ); luaL_error := GetProcAddr( fLibHandle, 'luaL_error' ); luaL_checkoption := GetProcAddr( fLibHandle, 'luaL_checkoption' ); luaL_ref := GetProcAddr( fLibHandle, 'luaL_ref' ); luaL_unref := GetProcAddr( fLibHandle, 'luaL_unref' );
{$ifdef LUA_COMPAT_GETN} luaL_getn := GetProcAddr( fLibHandle, 'luaL_getn' ); luaL_setn := GetProcAddr( fLibHandle, 'luaL_setn' );
{$endif} luaL_loadfile := GetProcAddr( fLibHandle, 'luaL_loadfile' ); luaL_loadbuffer := GetProcAddr( fLibHandle, 'luaL_loadbuffer' ); luaL_loadstring := GetProcAddr( fLibHandle, 'luaL_loadstring' ); luaL_newstate := GetProcAddr( fLibHandle, 'luaL_newstate' ); luaL_gsub := GetProcAddr( fLibHandle, 'luaL_gsub' ); luaL_findtable := GetProcAddr( fLibHandle, 'luaL_findtable' ); luaL_buffinit := GetProcAddr( fLibHandle, 'luaL_buffinit' ); luaL_prepbuffer := GetProcAddr( fLibHandle, 'luaL_prepbuffer' ); luaL_addlstring := GetProcAddr( fLibHandle, 'luaL_addlstring' ); luaL_addstring := GetProcAddr( fLibHandle, 'luaL_addstring' ); luaL_addvalue := GetProcAddr( fLibHandle, 'luaL_addvalue' ); luaL_pushresult := GetProcAddr( fLibHandle, 'luaL_pushresult' ); Result := fLibHandle;
end; procedure FreeLuaLib();
begin lua_newstate := nil; lua_close := nil; lua_newthread := nil; lua_atpanic := nil; lua_gettop := nil; lua_settop := nil; lua_pushvalue := nil; lua_remove := nil; lua_insert := nil; lua_replace := nil; lua_checkstack := nil; lua_xmove := nil; lua_isnumber := nil; lua_isstring := nil; lua_iscfunction := nil; lua_isuserdata := nil; lua_type := nil; lua_typename := nil; lua_equal := nil; lua_rawequal := nil; lua_lessthan := nil; lua_tonumber := nil; lua_tointeger := nil; lua_toboolean := nil; lua_tolstring := nil; lua_objlen := nil; lua_tocfunction := nil; lua_touserdata := nil; lua_tothread := nil; lua_topointer := nil; lua_pushnil := nil; lua_pushnumber := nil; lua_pushinteger := nil; lua_pushlstring := nil; lua_pushstring := nil; lua_pushvfstring := nil; lua_pushfstring := nil; lua_pushcclosure := nil; lua_pushboolean := nil; lua_pushlightuserdata := nil; lua_pushthread := nil; lua_gettable := nil; lua_getfield := nil; lua_rawget := nil; lua_rawgeti := nil; lua_createtable := nil; lua_newuserdata := nil; lua_getmetatable := nil; lua_getfenv := nil; lua_settable := nil; lua_setfield := nil; lua_rawset := nil; lua_rawseti := nil; lua_setmetatable := nil; lua_setfenv := nil; lua_call := nil; lua_pcall := nil; lua_cpcall := nil; lua_load := nil; lua_dump := nil; lua_yield := nil; lua_resume := nil; lua_status := nil; lua_gc := nil; lua_error := nil; lua_next := nil; lua_concat := nil; lua_getallocf := nil; lua_setallocf := nil; lua_getstack := nil; lua_getinfo := nil; lua_getlocal := nil; lua_setlocal := nil; lua_getupvalue := nil; lua_setupvalue := nil; lua_sethook := nil; lua_gethook := nil; lua_gethookmask := nil; lua_gethookcount := nil; luaopen_base := nil; luaopen_table := nil; luaopen_io := nil; luaopen_os := nil; luaopen_string := nil; luaopen_math := nil; luaopen_debug := nil; luaopen_package := nil; luaL_openlibs := nil; luaL_register := nil; luaL_getmetafield := nil; luaL_callmeta := nil; luaL_typerror := nil; luaL_argerror := nil; luaL_checklstring := nil; luaL_optlstring := nil; luaL_checknumber := nil; luaL_optnumber := nil; luaL_checkinteger := nil; luaL_optinteger := nil; luaL_checkstack := nil; luaL_checktype := nil; luaL_checkany := nil; luaL_newmetatable := nil; luaL_checkudata := nil; luaL_where := nil; luaL_error := nil; luaL_checkoption := nil; luaL_ref := nil; luaL_unref := nil;
{$ifdef LUA_COMPAT_GETN} luaL_getn := nil; luaL_setn := nil;
{$endif} luaL_loadfile := nil; luaL_loadbuffer := nil; luaL_loadstring := nil; luaL_newstate := nil; luaL_gsub := nil; luaL_findtable := nil; luaL_buffinit := nil; luaL_prepbuffer := nil; luaL_addlstring := nil; luaL_addstring := nil; luaL_addvalue := nil; luaL_pushresult := nil; if fLibHandle <> 0 then begin FreeLibrary( fLibHandle ); fLibHandle := 0; end;
end; {$ifndef LUA_COMPAT_GETN}
function luaL_getn(L: lua_State; t: Integer): Integer;
begin Result := lua_objlen(L, t);
end; procedure luaL_setn(L: lua_State; t, n: Integer);
begin
end;
{$endif} function lua_upvalueindex(i: Integer): Integer;
begin Result := LUA_GLOBALSINDEX - i;
end; procedure lua_pop(L: lua_State; n: Integer);
begin lua_settop(L, -(n) - 1);
end; procedure lua_newtable(L: lua_State);
begin lua_createtable(L, 0, 0);
end; function lua_strlen(L: lua_State; i: Integer): Cardinal;
begin result := lua_objlen(L, i);
end; procedure lua_register(L: lua_state; name: PAnsiChar; f: lua_CFunction);
begin lua_pushcfunction(L, f); lua_setglobal(L, name);
end; procedure lua_pushcfunction(L: lua_State; f: lua_CFunction);
begin lua_pushcclosure(L, f, 0);
end; function lua_isfunction(L: lua_State; idx: Integer): Boolean;
begin Result := lua_type(L, idx) = LUA_TFUNCTION;
end; function lua_istable(L: lua_State; idx: Integer): Boolean;
begin Result := lua_type(L, idx) = LUA_TTABLE;
end; function lua_islightuserdata(L: lua_State; idx: Integer): Boolean;
begin Result := lua_type(L, idx) = LUA_TLIGHTUSERDATA;
end; function lua_isnil(L: lua_State; idx: Integer): Boolean;
begin Result := lua_type(L, idx) = LUA_TNIL;
end; function lua_isboolean(L: lua_State; idx: Integer): Boolean;
begin Result := lua_type(L, idx) = LUA_TBOOLEAN;
end; function lua_isthread(L: lua_State; idx: Integer): Boolean;
begin Result := lua_type(L, idx) = LUA_TTHREAD;
end; function lua_isnone(L: lua_State; idx: Integer): Boolean;
begin Result := lua_type(L, idx) = LUA_TNONE;
end; function lua_isnoneornil(L: lua_State; idx: Integer): Boolean;
begin Result := lua_type(L, idx) <= 0;
end; procedure lua_pushliteral(L: lua_State; s: PAnsiChar);
begin lua_pushlstring(L, s, StrLen(s));
end; procedure lua_setglobal(L: lua_State; name: PAnsiChar);
begin lua_setfield(L, LUA_GLOBALSINDEX, name);
end; procedure lua_getglobal(L: lua_State; name: PAnsiChar);
begin lua_getfield(L, LUA_GLOBALSINDEX, name);
end; function lua_tostring(L: lua_State; idx: Integer): PAnsiChar;
var len: Cardinal;
begin Result := lua_tolstring(L, idx, len);
end; function lua_getgccount(L: lua_State): Integer;
begin Result := lua_gc(L, LUA_GCCOUNT, 0);
end; function lua_open(): lua_State;
begin Result := luaL_newstate();
end; procedure lua_getregistry(L: lua_State);
begin lua_pushvalue(L, LUA_REGISTRYINDEX);
end; function lua_ref(L: lua_State; lock: Boolean): Integer;
begin if lock then Result := luaL_ref(L, LUA_REGISTRYINDEX) else begin lua_pushstring(L, 'unlocked references are obsolete'); Result := lua_error(L); end;
end; procedure lua_unref(L: lua_State; ref: Integer);
begin luaL_unref(L, LUA_REGISTRYINDEX, ref);
end; procedure lua_getref(L: lua_State; ref: Integer);
begin lua_rawgeti(L, LUA_REGISTRYINDEX, ref);
end; procedure lua_assert(c: Boolean);
begin
end; function lua_number2str(s: Lua_Number; n: Integer): String;
begin Result := FormatFloat( LUA_NUMBER_FMT, RoundTo( s, n ) );
end; function lua_str2number(s: String; p: integer): Lua_Number;
begin Result := RoundTo( StrToFloat( s ), p );
end; function luaL_argcheck(L: lua_State; cond: Boolean; narg: Integer; extramsg: PAnsiChar): Integer;
begin if cond then Result := 0 else Result := luaL_argerror(L, narg, extramsg);
end; function luaL_checkstring(L: lua_State; narg: Integer): PAnsiChar;
var ls: Cardinal;
begin Result := luaL_checklstring(L, narg, ls);
end; function luaL_optstring(L: lua_State; narg: Integer; d: PAnsiChar): PAnsiChar;
var ls: Cardinal;
begin Result := luaL_optlstring(L, narg, d, ls);
end; function luaL_checkint(L: lua_State; narg: Integer): Integer;
begin Result := Trunc(luaL_checkinteger(L, narg));
end; function luaL_optint(L: lua_State; narg, d: Integer): Integer;
begin Result := Trunc(luaL_optinteger(L, narg, d));
end; function luaL_checklong(L: lua_State; narg: Integer): LongInt;
begin Result := Trunc(luaL_checkinteger(L, narg));
end; function luaL_optlong(L: lua_State; narg: Integer; d: LongInt): LongInt;
begin Result := Trunc(luaL_optinteger(L, narg, d));
end; function luaL_typename(L: lua_State; idx: Integer): PAnsiChar;
begin Result := lua_typename(L, lua_type(L, idx));
end; function luaL_dofile(L: lua_State; filename: PAnsiChar): Integer;
begin Result := luaL_loadfile(L, filename); If Result = 0 Then Result := lua_pcall(L, 0, LUA_MULTRET, 0);
end; function luaL_dostring(L: lua_State; str: PAnsiChar): Integer;
begin Result := luaL_loadstring(L, str); If Result = 0 Then Result := lua_pcall(L, 0, LUA_MULTRET, 0);
end; procedure luaL_getmetatable(L: lua_State; tname: PAnsiChar);
begin lua_getfield(L, LUA_REGISTRYINDEX, tname);
end; procedure luaL_addchar(var B: luaL_Buffer; c: AnsiChar);
begin if Integer(B.p) < Integer(B.buffer + LUAL_BUFFERSIZE) then luaL_prepbuffer(B); B.p^ := c; Inc(B.p);
{ // original C code
#define luaL_addchar(B,c) \ ((void)((B)->p < ((B)->buffer+LUAL_BUFFERSIZE) || luaL_prepbuffer(B)), \ (*(B)->p++ = (char)(c)))
}
end; procedure luaL_putchar(var B: luaL_Buffer; c: AnsiChar);
begin luaL_addchar(B, c);
end; procedure luaL_addsize(var B: luaL_Buffer; n: Cardinal);
begin Inc(B.p, n);
end; function luaL_check_lstr(L: lua_State; numArg: Integer; var ls: Cardinal): PAnsiChar;
begin Result := luaL_checklstring(L, numArg, ls);
end; function luaL_opt_lstr(L: lua_State; numArg: Integer; def: PAnsiChar; var ls: Cardinal): PAnsiChar;
begin Result := luaL_optlstring(L, numArg, def, ls);
end; function luaL_check_number(L: lua_State; numArg: Integer): lua_Number;
begin Result := luaL_checknumber(L, numArg);
end; function luaL_opt_number(L: lua_State; nArg: Integer; def: lua_Number): lua_Number;
begin Result := luaL_optnumber(L, nArg, def);
end; function luaL_arg_check(L: lua_State; cond: Boolean; numarg: Integer; extramsg: PAnsiChar): Integer;
begin Result := luaL_argcheck(L, cond, numarg, extramsg);
end; function luaL_check_string(L: lua_State; n: Integer): PAnsiChar;
begin Result := luaL_checkstring(L, n);
end; function luaL_opt_string(L: lua_State; n: Integer; d: PAnsiChar): PAnsiChar;
begin Result := luaL_optstring(L, n, d);
end; function luaL_check_int(L: lua_State; n: Integer): Integer;
begin Result := luaL_checkint(L, n);
end; function luaL_check_long(L: lua_State; n: LongInt): LongInt;
begin Result := luaL_checklong(L, n);
end; function luaL_opt_int(L: lua_State; n, d: Integer): Integer;
begin Result := luaL_optint(L, n, d);
end; function luaL_opt_long(L: lua_State; n: Integer; d: LongInt): LongInt;
begin Result := luaL_optlong(L, n, d);
end; end.
四、单元层级截图如下(上传有bin程序):


五、备注
1、自己运行redis程序,保证正常(这是前提,自行检索相关内容);
2、对上面的bin解压缩后,会看到luaScript里面有自己写的lua脚本,里面有redis的ip地址,默认是127.0.0.1的,可以自行使用;
3、运行主程序exe,就能set和get相关redis功能;
相关文章:
delphi6直连redis服务(用lua脚本redis模块)
一、创建一个exe程序 创建一个exe程序,引用LuaRedis.pas单元(此单元自己封装的代码,目前主要封装了获取key和设置key/value功能),代码如下: unit Unit1;interfaceusesWindows, Messages, SysUtils, Variant…...
嵌入式Linux:编译和使用Protobuf库
目录 1、开发环境和工具 2、安装和编译Protobuf、Protobuf-C库 3、编写和编译proto文件 4、修改makefile文件 5、测试示例 6、参考资料 Protobuf(Protocol Buffers)是由 Google 开发的一种轻量级、高效的结构化数据序列化方式,用于在不同应用…...
导航app为什么知道还有几秒变绿灯?
在使用地图导航app行驶至信号灯的交叉路口时,这些应用程序会贴心地告知用户距信号灯变化还有多少秒,无论是即将转为绿灯还是红灯。这一智能化提示不仅使得驾驶员能适时做好起步或刹车的准备,有效缓解了因等待时间不确定而产生的焦虑情绪&…...
设计模式 六大原则之单一职责原则
文章目录 概述代码例子小结 概述 先看下定义吧,如下: 单一职责原则的定义描述非常简单,也不难理解。一个类只负责完成一个职责或者功能。也就是说在类的设计中, 我们不要设计大而全的类,而是要设计粒度小、功能单一的类。 代码例…...
DOM重点核心(注册事件+DOM事件流)
目录 1.注册事件 注册时间概述 addEventListener() 删除事件 2.DOM事件流 DOM事件流理论 事件对象 事件对象的常见属性和方法 e.targe 和 this的区别 阻止默认行为 阻止冒泡 事件委托 禁止右键菜单和禁止选中文字 获得鼠标的坐标(可视区、页面、浏览器…...
浅谈操作系统中的重要概念——线程(3)——设计模式
文章目录 一、什么是设计模式?二、单例模式2.1、饿汉模式2.2、懒汉模式2.3、多线程情况下调用 饿汉模式与懒汉模式 谁是安全的??(重点) 三、工厂模式3.1、什么是工厂模式?3.1.1、构造方法存在的缺陷3.1.1.1…...
nginx配置域名与IP访问服务冲突问题
在最近的一次开发中遇到一个问题,我在云服务器上部署了两个服务,A服务和B服务, A服务在服务器中用的端口是80端口,所以我在浏览器访问的地址就是 B服务在服务器中用的是9818端口,所以我在浏览器访问的是 现在我给B服务…...
2024OD机试卷-字符串序列判定 (java\python\c++)
题目:字符串序列判定 题目描述 输入两个字符串 S 和 L ,都只包含英文小写字母。S长度 ≤ 100,L长度 ≤ 500,000。判定S是否是L的有效子串。 判定规则:S 中的每个字符在 L 中都能找到(可以不连续),且 S 在L中字符的前后顺序与 S 中顺序要保持一致。(例如,S = ” ace…...
7-128 最长公共子串
一个序列中去掉若干(也可以不去掉)元素剩下的部分称为其子序列。对于给定的序列X = <x1,x2,…,xm>,称序列Z = <z1,z2,…,zk>为X的一个子序列,仅当在X中存在一个递增序号序列<i1,i2,…,ik>,对所有的j(1,2,…,k)满足 xij…...
【瑞萨RA6M3】2. UART 实验
https://blog.csdn.net/qq_35181236/article/details/132789258 使用 uart9 配置 打印 void hal_entry(void) {/* TODO: add your own code here */fsp_err_t err;uint8_t c;/* 配置串口 */err g_uart9.p_api->open(g_uart9.p_ctrl, g_uart9.p_cfg);while (1){g_uart9.…...
js遇到需要正则匹配来修改img标签+清除行内样式
方法一 var regex0 new RegExp("(i?)(\<img)([^\>]\>)", "gmi") //正则匹配表达式this.newcontent this.content.replace(regex0,"$2 styledisplay:block;margin: auto;width:120px; $3") //下面这个则需要在$2 $3左右添加和修改东…...
Vue学习v-if与v-else-if
Vue学习v-if与v-else-if 一、前言1、v-if2、v-else-if3、v-else4、示例 一、前言 v-if 和 v-else-if 是 Vue.js 中用于条件渲染的指令,它们通常与 v-else 一起使用。下面我来详细解释一下它们的用法和区别: 1、v-if 用法:v-if 是一个指令&…...
linux进阶高级配置,你需要知道的有哪些2-firewalld防火墙(一)
1、防火墙的技术上分类: 包过滤:firewalld属于这种 应用代理: 状态检测:ASA 2、firewalld的两种配置模式: 运行时配置 :立即生效 永久配置:重新加载服务生效 3、常用的区域: trust…...
Centos 中如何汉化man命令
刚学Linux,记不住命令和选项,很依赖里面的 man 查看命令,但因为着实看不懂,有没有什么办法把man查看命令的信息改成中文 在CentOS 7中,你可以通过安装man-pages-zh包来获取中文的man手册。以下是具体的步骤:…...
原生小程序开发如何使用 tailwindcss
原生小程序开发如何使用 tailwindcss 原生小程序开发如何使用 tailwindcss 前言什么是 weapp-tailwindcss ?0. 准备环境以及小程序项目1. 安装与配置 tailwindcss 0. 使用包管理器安装 tailwindcss1. 在项目目录下创建 postcss.config.js 并注册 tailwindcss2. 配置 tailwind…...
spring alibaba中的seata分布式事务
Seata AT 模式设计思路 一阶段:业务数据和回滚日志记录在同一个本地事务中提交,释放本地锁和连接资源。 核心在于对业务sql进行解决解析,转换成undolog,并同时入库存 二阶段: 提交异步化,非常快速地完成…...
MQTT学习(二)
订阅主题和订阅确认 SUBSCRIBE——订阅主题 之前的CONNECT报文,分为 固定报头:必须存在,用于描述报文信息。里面有指出什么类型的报文,报文的等级。可变报头:不一定存在。主要看什么样子类型的报文。有效载荷部分&a…...
入职Java,不会git被开除了。。。
入职Java,不会git被开除了。。。 文章目录 入职Java,不会git被开除了。。。前言一、Git是什么?二、Git的核心概念三、Git的工作流程四、Git的常用命令五、总结 🌈你好呀!我是 山顶风景独好 💝欢迎来到我的博…...
Mysql 隔离级别
MySQL的事务隔离级别是指在处理并发事务时,为保证数据的一致性和事务的独立性,数据库系统提供的不同级别控制策略。根据ACID特性中的隔离性(Isolation),MySQL支持四种标准的事务隔离级别,每种级别有不同的并…...
每日一学—K邻算法:在风险传导中的创新应用与实践价值
文章目录 📋 前言🎯 K邻算法的实践意义🎯 创新应用与案例分析🔥 参与方式 📋 前言 在当今工业领域,图思维方式与图数据技术的应用日益广泛,成为图数据探索、挖掘与应用的坚实基础。本文旨在分享…...
微信小程序之bind和catch
这两个呢,都是绑定事件用的,具体使用有些小区别。 官方文档: 事件冒泡处理不同 bind:绑定的事件会向上冒泡,即触发当前组件的事件后,还会继续触发父组件的相同事件。例如,有一个子视图绑定了b…...
使用分级同态加密防御梯度泄漏
抽象 联邦学习 (FL) 支持跨分布式客户端进行协作模型训练,而无需共享原始数据,这使其成为在互联和自动驾驶汽车 (CAV) 等领域保护隐私的机器学习的一种很有前途的方法。然而,最近的研究表明&…...
Go 语言接口详解
Go 语言接口详解 核心概念 接口定义 在 Go 语言中,接口是一种抽象类型,它定义了一组方法的集合: // 定义接口 type Shape interface {Area() float64Perimeter() float64 } 接口实现 Go 接口的实现是隐式的: // 矩形结构体…...
条件运算符
C中的三目运算符(也称条件运算符,英文:ternary operator)是一种简洁的条件选择语句,语法如下: 条件表达式 ? 表达式1 : 表达式2• 如果“条件表达式”为true,则整个表达式的结果为“表达式1”…...
【AI学习】三、AI算法中的向量
在人工智能(AI)算法中,向量(Vector)是一种将现实世界中的数据(如图像、文本、音频等)转化为计算机可处理的数值型特征表示的工具。它是连接人类认知(如语义、视觉特征)与…...
【python异步多线程】异步多线程爬虫代码示例
claude生成的python多线程、异步代码示例,模拟20个网页的爬取,每个网页假设要0.5-2秒完成。 代码 Python多线程爬虫教程 核心概念 多线程:允许程序同时执行多个任务,提高IO密集型任务(如网络请求)的效率…...
Swagger和OpenApi的前世今生
Swagger与OpenAPI的关系演进是API标准化进程中的重要篇章,二者共同塑造了现代RESTful API的开发范式。 本期就扒一扒其技术演进的关键节点与核心逻辑: 🔄 一、起源与初创期:Swagger的诞生(2010-2014) 核心…...
Android 之 kotlin 语言学习笔记三(Kotlin-Java 互操作)
参考官方文档:https://developer.android.google.cn/kotlin/interop?hlzh-cn 一、Java(供 Kotlin 使用) 1、不得使用硬关键字 不要使用 Kotlin 的任何硬关键字作为方法的名称 或字段。允许使用 Kotlin 的软关键字、修饰符关键字和特殊标识…...
Maven 概述、安装、配置、仓库、私服详解
目录 1、Maven 概述 1.1 Maven 的定义 1.2 Maven 解决的问题 1.3 Maven 的核心特性与优势 2、Maven 安装 2.1 下载 Maven 2.2 安装配置 Maven 2.3 测试安装 2.4 修改 Maven 本地仓库的默认路径 3、Maven 配置 3.1 配置本地仓库 3.2 配置 JDK 3.3 IDEA 配置本地 Ma…...
鸿蒙DevEco Studio HarmonyOS 5跑酷小游戏实现指南
1. 项目概述 本跑酷小游戏基于鸿蒙HarmonyOS 5开发,使用DevEco Studio作为开发工具,采用Java语言实现,包含角色控制、障碍物生成和分数计算系统。 2. 项目结构 /src/main/java/com/example/runner/├── MainAbilitySlice.java // 主界…...
