├── Examples ├── Example 1 - Hello world │ ├── example1.lua │ ├── Example1.res │ └── Example1.dpr ├── Example 3 - Hello world (FMX) │ ├── example3.lua │ ├── Example3.dpr │ ├── Unit8.fmx │ └── Unit8.pas ├── Example 6 - Call Lua functions │ ├── example6.lua │ └── Example6.dpr ├── Example 2 - Extend TVerySimpleLua │ ├── example2.lua │ ├── Example2.dpr │ └── MyLua.pas ├── Example 4 - Benchmark (FMX) │ ├── Example4.dpr │ ├── example4.lua │ ├── Unit8.fmx │ ├── Unit8.pas │ └── MyLua.pas └── Example 5 - Create a Package │ ├── Example5.dpr │ ├── example5.lua │ ├── MyPackage2.pas │ ├── MyLua.pas │ └── MyPackage.pas ├── DLL ├── iOS │ └── liblua.a ├── Win32 │ └── lua5.3.0.dll ├── Win64 │ └── lua5.3.0.dll ├── Android │ ├── x86 │ │ └── liblua.so │ ├── armeabi │ │ └── liblua.so │ └── armeabi-v7a │ │ └── liblua.so └── MacOSX │ └── liblua5.3.0.dylib ├── .gitignore ├── Readme.txt ├── LICENSE └── Source ├── VerySimple.Lua.pas └── VerySimple.Lua.Lib.pas /Examples/Example 1 - Hello world/example1.lua: -------------------------------------------------------------------------------- 1 | print("Hello, Lua speaking."); -------------------------------------------------------------------------------- /Examples/Example 3 - Hello world (FMX)/example3.lua: -------------------------------------------------------------------------------- 1 | print("Hello world from Lua"); -------------------------------------------------------------------------------- /DLL/iOS/liblua.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Dennis1000/verysimplelua/HEAD/DLL/iOS/liblua.a -------------------------------------------------------------------------------- /DLL/Win32/lua5.3.0.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Dennis1000/verysimplelua/HEAD/DLL/Win32/lua5.3.0.dll -------------------------------------------------------------------------------- /DLL/Win64/lua5.3.0.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Dennis1000/verysimplelua/HEAD/DLL/Win64/lua5.3.0.dll -------------------------------------------------------------------------------- /Examples/Example 6 - Call Lua functions/example6.lua: -------------------------------------------------------------------------------- 1 | 2 | function add(x, y) 3 | return x + y 4 | end 5 | -------------------------------------------------------------------------------- /DLL/Android/x86/liblua.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Dennis1000/verysimplelua/HEAD/DLL/Android/x86/liblua.so -------------------------------------------------------------------------------- /DLL/Android/armeabi/liblua.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Dennis1000/verysimplelua/HEAD/DLL/Android/armeabi/liblua.so -------------------------------------------------------------------------------- /DLL/MacOSX/liblua5.3.0.dylib: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Dennis1000/verysimplelua/HEAD/DLL/MacOSX/liblua5.3.0.dylib -------------------------------------------------------------------------------- /DLL/Android/armeabi-v7a/liblua.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Dennis1000/verysimplelua/HEAD/DLL/Android/armeabi-v7a/liblua.so -------------------------------------------------------------------------------- /Examples/Example 1 - Hello world/Example1.res: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Dennis1000/verysimplelua/HEAD/Examples/Example 1 - Hello world/Example1.res -------------------------------------------------------------------------------- /Examples/Example 2 - Extend TVerySimpleLua/example2.lua: -------------------------------------------------------------------------------- 1 | print("Hello, Lua speaking again."); 2 | 3 | p1,p2 = HelloWorld(10, 20, 30); 4 | print "Results:"; 5 | print (p1, p2); 6 | 7 | HelloWorld2(); 8 | HelloWorld3(); 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /Examples/Example 3 - Hello world (FMX)/Example3.dpr: -------------------------------------------------------------------------------- 1 | program Example3; 2 | 3 | uses 4 | System.StartUpCopy, 5 | FMX.Forms, 6 | Unit8 in 'Unit8.pas' {Form8}, 7 | VerySimple.Lua.Lib in '..\..\Source\VerySimple.Lua.Lib.pas', 8 | VerySimple.Lua in '..\..\Source\VerySimple.Lua.pas'; 9 | 10 | {$R *.res} 11 | 12 | begin 13 | Application.Initialize; 14 | Application.CreateForm(TForm8, Form8); 15 | Application.Run; 16 | end. 17 | -------------------------------------------------------------------------------- /Examples/Example 4 - Benchmark (FMX)/Example4.dpr: -------------------------------------------------------------------------------- 1 | program Example4; 2 | 3 | uses 4 | System.StartUpCopy, 5 | FMX.Forms, 6 | Unit8 in 'Unit8.pas' {Form8}, 7 | VerySimple.Lua.Lib in '..\..\Source\VerySimple.Lua.Lib.pas', 8 | VerySimple.Lua in '..\..\Source\VerySimple.Lua.pas', 9 | MyLua in 'MyLua.pas'; 10 | 11 | {$R *.res} 12 | 13 | begin 14 | Application.Initialize; 15 | Application.CreateForm(TForm8, Form8); 16 | Application.Run; 17 | end. 18 | -------------------------------------------------------------------------------- /Examples/Example 4 - Benchmark (FMX)/example4.lua: -------------------------------------------------------------------------------- 1 | print("Starting benchmark1"); 2 | 3 | RunEachSeconds=20; 4 | 5 | 6 | 7 | i=0; 8 | count=0; 9 | 10 | Start(RunEachSeconds); 11 | repeat 12 | i=i+1; 13 | count=count+HelloWorld(i); 14 | until Finished(); 15 | assert(count~=0); 16 | 17 | print (i/RunEachSeconds, ' object function calls/second'); 18 | 19 | 20 | 21 | k=0; 22 | count=0; 23 | Start(RunEachSeconds); 24 | repeat 25 | k=k+1; 26 | count=count+HelloWorld3(k); 27 | until Finished(); 28 | assert(count~=0); 29 | 30 | print (k/RunEachSeconds, ' static function calls/second ' .. string.format("%.2f",(k*100/i)-100) .. '%'); 31 | 32 | 33 | -------------------------------------------------------------------------------- /Examples/Example 1 - Hello world/Example1.dpr: -------------------------------------------------------------------------------- 1 | program Example1; 2 | {$APPTYPE CONSOLE} 3 | 4 | uses 5 | SysUtils, 6 | Classes, 7 | VerySimple.Lua in '..\..\Source\VerySimple.Lua.pas', 8 | VerySimple.Lua.Lib in '..\..\Source\VerySimple.Lua.Lib.pas'; 9 | 10 | var 11 | Lua: TVerySimpleLua; 12 | 13 | begin 14 | try 15 | (* Example 1 - Simple Lua Script Execution *) 16 | Lua := TVerySimpleLua.Create; 17 | Lua.LibraryPath := '..\..\DLL\Win32\' + LUA_LIBRARY; 18 | Lua.DoFile('example1.lua'); 19 | Lua.Free; 20 | readln; 21 | 22 | except 23 | on E: Exception do 24 | writeln(E.ClassName, ': ', E.Message); 25 | end; 26 | end. 27 | -------------------------------------------------------------------------------- /Examples/Example 2 - Extend TVerySimpleLua/Example2.dpr: -------------------------------------------------------------------------------- 1 | program Example2; 2 | {$APPTYPE CONSOLE} 3 | 4 | uses 5 | SysUtils, 6 | Classes, 7 | VerySimple.Lua in '..\..\Source\VerySimple.Lua.pas', 8 | VerySimple.Lua.Lib in '..\..\Source\VerySimple.Lua.Lib.pas', 9 | MyLua in 'MyLua.pas'; 10 | 11 | var 12 | Lua: TMyLua; 13 | 14 | begin 15 | try 16 | (* Example 2 - Extend Lua with own class *) 17 | Lua := TMyLua.Create; 18 | Lua.LibraryPath := '..\..\DLL\Win32\' + LUA_LIBRARY; 19 | Lua.DoFile('example2.lua'); 20 | Lua.Free; 21 | readln; 22 | 23 | except 24 | on E: Exception do 25 | writeln(E.ClassName, ': ', E.Message); 26 | end; 27 | end. 28 | -------------------------------------------------------------------------------- /Examples/Example 5 - Create a Package/Example5.dpr: -------------------------------------------------------------------------------- 1 | program Example5; 2 | 3 | {$APPTYPE CONSOLE} 4 | 5 | {$R *.res} 6 | 7 | uses 8 | System.SysUtils, 9 | MyLua in 'MyLua.pas', 10 | MyPackage in 'MyPackage.pas', 11 | MyPackage2 in 'MyPackage2.pas', 12 | VerySimple.Lua.Lib in '..\..\Source\VerySimple.Lua.Lib.pas', 13 | VerySimple.Lua in '..\..\Source\VerySimple.Lua.pas'; 14 | 15 | var 16 | MyLua: TMyLua; 17 | begin 18 | try 19 | { TODO -oUser -cConsole Main : Insert code here } 20 | 21 | MyLua := TMyLua.Create; 22 | MyLua.DoFile('example5.lua'); 23 | MyLua.Free; 24 | readln; 25 | 26 | except 27 | on E: Exception do 28 | Writeln(E.ClassName, ': ', E.Message); 29 | end; 30 | end. 31 | -------------------------------------------------------------------------------- /Examples/Example 5 - Create a Package/example5.lua: -------------------------------------------------------------------------------- 1 | -- a debug inspect function 2 | 3 | function inspect(table, name) 4 | print ("--- " .. name .. " consists of"); 5 | for n,v in pairs(table) do print(n, v) end; 6 | print(); 7 | end 8 | 9 | 10 | -- show currently loaded packages 11 | 12 | inspect(package['preload'],'package[preload]'); 13 | 14 | 15 | -- now "load" two packages 16 | 17 | MyPackage = require "MyPackage"; 18 | MyPackage2 = require "MyPackage2"; 19 | 20 | 21 | -- show their content 22 | 23 | inspect(MyPackage, "MyPackage"); 24 | inspect(MyPackage2, "MyPackage2"); 25 | 26 | 27 | -- and exceute some functions 28 | 29 | 30 | p1 = MyPackage:Myfunction1(); 31 | print (p1); 32 | 33 | p1 = MyPackage:Myfunction2(); 34 | print (p1); 35 | 36 | 37 | p1 = MyPackage2:Double(100); 38 | print (p1); 39 | 40 | -------------------------------------------------------------------------------- /Examples/Example 3 - Hello world (FMX)/Unit8.fmx: -------------------------------------------------------------------------------- 1 | object Form8: TForm8 2 | Left = 0 3 | Top = 0 4 | Caption = 'Form8' 5 | ClientHeight = 480 6 | ClientWidth = 640 7 | FormFactor.Width = 320 8 | FormFactor.Height = 480 9 | FormFactor.Devices = [Desktop] 10 | DesignerMasterStyle = 0 11 | object Memo1: TMemo 12 | Touch.InteractiveGestures = [Pan, LongTap, DoubleTap] 13 | Align = Client 14 | Size.Width = 640.000000000000000000 15 | Size.Height = 440.000000000000000000 16 | Size.PlatformDefault = False 17 | TabOrder = 1 18 | end 19 | object Button1: TButton 20 | Align = Bottom 21 | Position.Y = 440.000000000000000000 22 | Size.Width = 640.000000000000000000 23 | Size.Height = 40.000000000000000000 24 | Size.PlatformDefault = False 25 | TabOrder = 2 26 | Text = 'Run' 27 | OnClick = Button1Click 28 | end 29 | end 30 | -------------------------------------------------------------------------------- /Examples/Example 4 - Benchmark (FMX)/Unit8.fmx: -------------------------------------------------------------------------------- 1 | object Form8: TForm8 2 | Left = 0 3 | Top = 0 4 | Caption = 'Form8' 5 | ClientHeight = 480 6 | ClientWidth = 640 7 | FormFactor.Width = 320 8 | FormFactor.Height = 480 9 | FormFactor.Devices = [Desktop] 10 | DesignerMasterStyle = 0 11 | object Memo1: TMemo 12 | Touch.InteractiveGestures = [Pan, LongTap, DoubleTap] 13 | Align = Client 14 | Size.Width = 640.000000000000000000 15 | Size.Height = 440.000000000000000000 16 | Size.PlatformDefault = False 17 | TabOrder = 1 18 | end 19 | object Button1: TButton 20 | Align = Bottom 21 | Position.Y = 440.000000000000000000 22 | Size.Width = 640.000000000000000000 23 | Size.Height = 40.000000000000000000 24 | Size.PlatformDefault = False 25 | TabOrder = 2 26 | Text = 'Run' 27 | OnClick = Button1Click 28 | end 29 | end 30 | -------------------------------------------------------------------------------- /Examples/Example 5 - Create a Package/MyPackage2.pas: -------------------------------------------------------------------------------- 1 | unit MyPackage2; 2 | 3 | interface 4 | 5 | uses 6 | VerySimple.Lua, VerySimple.Lua.Lib; 7 | 8 | {$M+} 9 | type 10 | TMyPackage2 = class 11 | published 12 | function Double(L: lua_State): Integer; 13 | end; 14 | 15 | 16 | implementation 17 | 18 | { TMyPackage2 } 19 | 20 | function TMyPackage2.Double(L: lua_State): Integer; 21 | var 22 | ArgCount: Integer; 23 | Value: Integer; 24 | begin 25 | ArgCount := Lua_GetTop(L); 26 | 27 | // Get last argument as value - ArgCount is different if called via MyPackage2.Double() or MayPackage2:Double()! 28 | Value := Lua_ToInteger(L, ArgCount); 29 | 30 | // Double the value 31 | Value := Value * 2; 32 | 33 | // Push the result 34 | Lua_PushInteger(L, Value); 35 | 36 | // There is one value on the result stack 37 | Result := 1; 38 | end; 39 | 40 | end. 41 | -------------------------------------------------------------------------------- /Examples/Example 6 - Call Lua functions/Example6.dpr: -------------------------------------------------------------------------------- 1 | program Example1; 2 | {$APPTYPE CONSOLE} 3 | 4 | uses 5 | SysUtils, 6 | Classes, 7 | VerySimple.Lua in '..\..\Source\VerySimple.Lua.pas', 8 | VerySimple.Lua.Lib in '..\..\Source\VerySimple.Lua.Lib.pas'; 9 | 10 | var 11 | Lua: TVerySimpleLua; 12 | 13 | function LuaAdd(L: lua_State; x, y: integer): integer; 14 | begin 15 | lua_getglobal(L, 'add'); // name of the function 16 | lua_pushinteger(L, x); // first parameter 17 | lua_pushinteger(L, y); // second parameter 18 | lua_call(L, 2, 1); // call function with 2 parameters and 1 result 19 | Result := lua_tointeger(L, -1); // get result 20 | lua_pop(L, 1); // remove result from stack 21 | end; 22 | 23 | 24 | begin 25 | try 26 | (* Example 6 - Call a lua function *) 27 | Lua := TVerySimpleLua.Create; 28 | Lua.LibraryPath := '..\..\DLL\Win32\' + LUA_LIBRARY; 29 | Lua.DoFile('example6.lua'); 30 | 31 | Writeln(LuaAdd(Lua.LuaState, 10, 20)); 32 | Lua.Free; 33 | readln; 34 | 35 | except 36 | on E: Exception do 37 | writeln(E.ClassName, ': ', E.Message); 38 | end; 39 | end. 40 | -------------------------------------------------------------------------------- /Examples/Example 4 - Benchmark (FMX)/Unit8.pas: -------------------------------------------------------------------------------- 1 | unit Unit8; 2 | 3 | interface 4 | 5 | uses 6 | System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants, 7 | FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.StdCtrls, FMX.Layouts, FMX.Memo; 8 | 9 | type 10 | TForm8 = class(TForm) 11 | Memo1: TMemo; 12 | Button1: TButton; 13 | procedure Button1Click(Sender: TObject); 14 | private 15 | { Private declarations } 16 | public 17 | { Public declarations } 18 | procedure OnPrint(Msg: String); 19 | end; 20 | 21 | var 22 | Form8: TForm8; 23 | 24 | implementation 25 | 26 | {$R *.fmx} 27 | 28 | uses 29 | VerySimple.Lua, VerySimple.Lua.Lib, System.IOUtils, MyLua; 30 | 31 | 32 | procedure TForm8.Button1Click(Sender: TObject); 33 | var 34 | Lua: TMyLua; 35 | begin 36 | Lua := TMyLua.Create; 37 | Lua.OnPrint := OnPrint; // Redirect console output to memo 38 | Lua.DoFile('example4.lua'); 39 | Lua.Free; 40 | end; 41 | 42 | procedure TForm8.OnPrint(Msg: String); 43 | begin 44 | Memo1.Lines.Add(Msg); 45 | Memo1.GoToTextEnd; 46 | Application.ProcessMessages; 47 | end; 48 | 49 | end. 50 | -------------------------------------------------------------------------------- /Examples/Example 5 - Create a Package/MyLua.pas: -------------------------------------------------------------------------------- 1 | unit MyLua; 2 | 3 | {$M+} 4 | 5 | interface 6 | 7 | uses 8 | VerySimple.Lua, VerySimple.Lua.Lib, System.SysUtils, MyPackage, MyPackage2; 9 | 10 | type 11 | // MyLua example class 12 | TMyLua = class(TVerySimpleLua) 13 | private 14 | MyPackage: TMyPackage; 15 | MyPackage2: TMyPackage2; 16 | public 17 | constructor Create; override; 18 | destructor Destroy; override; 19 | procedure Open; override; 20 | end; 21 | 22 | implementation 23 | 24 | constructor TMyLua.Create; 25 | begin 26 | inherited; 27 | 28 | LibraryPath := '..\..\DLL\Win32\' + LUA_LIBRARY; 29 | MyPackage := TMyPackage.Create; 30 | MyPackage2 := TMyPackage2.Create; 31 | end; 32 | 33 | destructor TMyLua.Destroy; 34 | begin 35 | MyPackage.Free; 36 | MyPackage2.Free; 37 | inherited; 38 | end; 39 | 40 | procedure TMyLua.Open; 41 | begin 42 | inherited; 43 | 44 | // We're registering two packages: one called "MyPackage" 45 | MyPackage.PackageReg(LuaState); //call our own package register function 46 | 47 | // and create a second package and auto register those package functions 48 | RegisterPackage('MyPackage2', MyPackage2); 49 | end; 50 | 51 | end. 52 | -------------------------------------------------------------------------------- /Examples/Example 3 - Hello world (FMX)/Unit8.pas: -------------------------------------------------------------------------------- 1 | unit Unit8; 2 | 3 | interface 4 | 5 | uses 6 | System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants, 7 | FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.StdCtrls, FMX.Layouts, FMX.Memo; 8 | 9 | type 10 | TForm8 = class(TForm) 11 | Memo1: TMemo; 12 | Button1: TButton; 13 | procedure Button1Click(Sender: TObject); 14 | private 15 | { Private declarations } 16 | public 17 | { Public declarations } 18 | procedure OnPrint(Msg: String); 19 | end; 20 | 21 | var 22 | Form8: TForm8; 23 | 24 | implementation 25 | 26 | {$R *.fmx} 27 | 28 | uses 29 | VerySimple.Lua, VerySimple.Lua.Lib, System.IOUtils; 30 | 31 | 32 | procedure TForm8.Button1Click(Sender: TObject); 33 | var 34 | Lua: TVerySimpleLua; 35 | begin 36 | Lua := TVerySimpleLua.Create; 37 | 38 | {$IF defined(WIN32)} 39 | Lua.LibraryPath := '..\..\..\..\DLL\WIN32\' + LUA_LIBRARY; 40 | Lua.FilePath := '..\..\'; 41 | 42 | {$ELSEIF defined(WIN64)} 43 | Lua.LibraryPath := '..\..\..\..\DLL\WIN64\' + LUA_LIBRARY; 44 | Lua.FilePath := '..\..\'; 45 | {$ENDIF} 46 | 47 | Lua.OnPrint := OnPrint; // Redirect console output to memo 48 | Lua.DoFile('example3.lua'); 49 | Lua.Free; 50 | end; 51 | 52 | procedure TForm8.OnPrint(Msg: String); 53 | begin 54 | Memo1.Lines.Add(Msg); 55 | Memo1.GoToTextEnd; 56 | Application.ProcessMessages; 57 | end; 58 | 59 | end. 60 | -------------------------------------------------------------------------------- /Examples/Example 5 - Create a Package/MyPackage.pas: -------------------------------------------------------------------------------- 1 | unit MyPackage; 2 | 3 | interface 4 | 5 | {$M+} 6 | uses 7 | VerySimple.Lua, VerySimple.Lua.Lib; 8 | 9 | type 10 | TMyPackage = class 11 | protected 12 | public 13 | function LoadPackage(L: lua_State): Integer; 14 | procedure PackageReg(L: lua_State); 15 | published 16 | function Myfunction1(L: lua_State): Integer; 17 | function Myfunction2(L: lua_State): Integer; 18 | end; 19 | 20 | 21 | implementation 22 | 23 | { TMyPackage } 24 | 25 | uses 26 | System.SysUtils; 27 | 28 | function TMyPackage.LoadPackage(L: lua_State): Integer; 29 | begin 30 | lua_newtable(L); 31 | 32 | // Manually add Myfunction1 33 | TVerySimpleLua.PushFunction(L, Self, MethodAddress('Myfunction1'), 'Myfunction1'); 34 | lua_rawset(L, -3); 35 | 36 | // Manually add Myfunction2 37 | TVerySimpleLua.PushFunction(L, Self, MethodAddress('Myfunction2'), 'Myfunction2'); 38 | lua_rawset(L, -3); 39 | 40 | Result := 1; 41 | end; 42 | 43 | function TMyPackage.Myfunction1(L: lua_State): Integer; 44 | begin 45 | // Push a return value 46 | Lua_PushInteger(L, 54); 47 | Result := 1; 48 | end; 49 | 50 | function TMyPackage.Myfunction2(L: lua_State): Integer; 51 | begin 52 | // Push a return value 53 | Lua_PushInteger(L, 174); 54 | Result := 1; 55 | end; 56 | 57 | procedure TMyPackage.PackageReg(L: lua_State); 58 | begin 59 | // Register Lua package 'MyPackage' with the TMyPackage package loader procedure 60 | TVerySimpleLua.RegisterPackage(L, 'MyPackage', Self, 'LoadPackage'); 61 | end; 62 | 63 | end. 64 | 65 | -------------------------------------------------------------------------------- /Examples/Example 2 - Extend TVerySimpleLua/MyLua.pas: -------------------------------------------------------------------------------- 1 | unit MyLua; 2 | 3 | {$M+} 4 | 5 | interface 6 | 7 | uses 8 | VerySimple.Lua, VerySimple.Lua.Lib; 9 | 10 | type 11 | // MyLua example class 12 | TMyLua = class(TVerySimpleLua) 13 | published 14 | // lua functions this published methods are automatically added 15 | // to the lua function table if called with TLua.Create(True) or Create() 16 | function HelloWorld(LuaState: lua_State): Integer; 17 | class function HelloWorld2(LuaState: lua_State): Integer; 18 | class function HelloWorld3(LuaState: lua_State): Integer; cdecl; static; 19 | end; 20 | 21 | implementation 22 | 23 | // Print arguments and return two values (101 and 102) 24 | function TMyLua.HelloWorld(LuaState: lua_State): Integer; 25 | var 26 | ArgCount: Integer; 27 | I: integer; 28 | begin 29 | ArgCount := Lua_GetTop(LuaState); 30 | 31 | writeln('Hello World from Delphi'); 32 | writeln('Arguments: ', ArgCount); 33 | 34 | for I := 1 to ArgCount do 35 | writeln('Arg', I, ': ', Lua_ToInteger(LuaState, I)); 36 | 37 | // Clear stack 38 | Lua_Pop(LuaState, Lua_GetTop(LuaState)); 39 | 40 | // Push return values 41 | Lua_PushInteger(LuaState, 101); 42 | Lua_PushInteger(LuaState, 102); 43 | Result := 2; 44 | end; 45 | 46 | 47 | // Print out a Hello World text 48 | class function TMyLua.HelloWorld2(LuaState: lua_State): Integer; 49 | begin 50 | writeln('Hello World2 from Delphi'); 51 | Result := 0; 52 | end; 53 | 54 | // Print out a Hello World text 55 | class function TMyLua.HelloWorld3(LuaState: lua_State): Integer; 56 | begin 57 | writeln('Hello World3 from Delphi'); 58 | Result := 0; 59 | end; 60 | 61 | 62 | end. 63 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Uncomment these types if you want even more clean repository. But be careful. 2 | # It can make harm to an existing project source. Read explanations below. 3 | # 4 | # Resource files are binaries containing manifest, project icon and version info. 5 | # They can not be viewed as text or compared by diff-tools. Consider replacing them with .rc files. 6 | #*.res 7 | # 8 | # Type library file (binary). In old Delphi versions it should be stored. 9 | # Since Delphi 2009 it is produced from .ridl file and can safely be ignored. 10 | #*.tlb 11 | # 12 | # Diagram Portfolio file. Used by the diagram editor up to Delphi 7. 13 | # Uncomment this if you are not using diagrams or use newer Delphi version. 14 | #*.ddp 15 | # 16 | # Visual LiveBindings file. Added in Delphi XE2. 17 | # Uncomment this if you are not using LiveBindings Designer. 18 | #*.vlb 19 | # 20 | # Deployment Manager configuration file for your project. Added in Delphi XE2. 21 | # Uncomment this if it is not mobile development and you do not use remote debug feature. 22 | #*.deployproj 23 | # 24 | # C++ object files produced when C/C++ Output file generation is configured. 25 | # Uncomment this if you are not using external objects (zlib library for example). 26 | #*.obj 27 | # 28 | 29 | # Delphi compiler-generated binaries (safe to delete) 30 | *.exe 31 | #*.dll 32 | *.bpl 33 | *.bpi 34 | *.dcp 35 | #*.so 36 | *.apk 37 | *.drc 38 | *.map 39 | *.dres 40 | *.rsm 41 | *.tds 42 | *.dcu 43 | *.lib 44 | #*.a 45 | *.o 46 | *.ocx 47 | 48 | # Delphi autogenerated files (duplicated info) 49 | *.cfg 50 | *.hpp 51 | *Resource.rc 52 | 53 | # Delphi local files (user-specific info) 54 | *.local 55 | *.identcache 56 | *.projdata 57 | *.tvsconfig 58 | *.dsk 59 | 60 | # Delphi history and backups 61 | __history/ 62 | __recovery/ 63 | *.~* 64 | 65 | # Castalia statistics file (since XE7 Castalia is distributed with Delphi) 66 | *.stat 67 | -------------------------------------------------------------------------------- /Examples/Example 4 - Benchmark (FMX)/MyLua.pas: -------------------------------------------------------------------------------- 1 | unit MyLua; 2 | 3 | {$M+} 4 | 5 | interface 6 | 7 | uses 8 | VerySimple.Lua, VerySimple.Lua.Lib, System.Diagnostics; 9 | 10 | type 11 | // MyLua example class 12 | TMyLua = class(TVerySimpleLua) 13 | public 14 | MilliSeconds: Int64; 15 | Sw: TStopWatch; 16 | lastbench: Int64; 17 | published 18 | constructor Create; override; 19 | function Start(LuaState: lua_State): Integer; 20 | function Finished(LuaState: lua_State): Integer; 21 | function HelloWorld(LuaState: lua_State): Integer; 22 | class function HelloWorld2(LuaState: lua_State): Integer; 23 | class function HelloWorld3(LuaState: lua_State): Integer; cdecl; static; 24 | end; 25 | 26 | implementation 27 | 28 | uses 29 | System.SysUtils, System.IOUtils; 30 | 31 | constructor TMyLua.Create; 32 | begin 33 | inherited; 34 | 35 | {$IF defined(WIN32)} 36 | LibraryPath := '..\..\..\..\DLL\WIN32\' + LUA_LIBRARY; 37 | FilePath := '..\..\'; 38 | 39 | {$ELSEIF defined(WIN64)} 40 | LibraryPath := '..\..\..\..\DLL\WIN64\' + LUA_LIBRARY; 41 | FilePath := '..\..\'; 42 | {$ENDIF} 43 | 44 | Sw := TStopwatch.Create; 45 | end; 46 | 47 | function TMyLua.HelloWorld(LuaState: lua_State): Integer; 48 | begin 49 | lua_pushinteger(LuaState, Lua_ToInteger(LuaState, 1) * 2); 50 | Result := 1; 51 | end; 52 | 53 | class function TMyLua.HelloWorld2(LuaState: lua_State): Integer; 54 | begin 55 | lua_pushinteger(LuaState, Lua_ToInteger(LuaState, 1) * 2); 56 | Result := 1; 57 | end; 58 | 59 | class function TMyLua.HelloWorld3(LuaState: lua_State): Integer; 60 | begin 61 | lua_pushinteger(LuaState, Lua_ToInteger(LuaState, 1) * 2); 62 | Result := 1; 63 | end; 64 | 65 | 66 | function TMyLua.Start(LuaState: lua_State): Integer; 67 | begin 68 | MilliSeconds := Lua_ToInteger(LuaState, 1) * 1000; 69 | Result := 0; 70 | lastbench := 0; 71 | Sw.Reset; 72 | Sw.Start; 73 | end; 74 | 75 | function TMyLua.Finished(LuaState: lua_State): Integer; 76 | var 77 | ElapsedSeconds: Int64; 78 | begin 79 | ElapsedSeconds := Sw.ElapsedMilliseconds div 1000; 80 | 81 | if ElapsedSeconds > lastbench then 82 | begin 83 | lastbench := ElapsedSeconds; 84 | OnPrint('...'); 85 | end; 86 | 87 | if Sw.ElapsedMilliseconds > MilliSeconds then 88 | lua_pushboolean(LuaState, 1) 89 | else 90 | lua_pushboolean(LuaState, 0); 91 | Result := 1; 92 | end; 93 | 94 | end. 95 | -------------------------------------------------------------------------------- /Readme.txt: -------------------------------------------------------------------------------- 1 | ============================================== 2 | VerySimple.Lua - Lua 5.3.0 for Delphi XE5-10.1 3 | ============================================== 4 | (c) 2009-2016 Dennis D. Spreen 5 | http://blog.spreendigital.de/2015/02/18/verysimple-lua-2-0-a-cross-platform-lua-5-3-0-wrapper-for-delphi-xe5-xe7/ 6 | 7 | History 8 | 2.1 DS Added [hidden] attribute 9 | Added OnError property 10 | Fixed MULTIRET lua call 11 | Fixed function luaL_testudata 12 | 2.0.3 DS Fix XE5 - Added Pointer cast for RegisterFunc 13 | 2.0.2 DS Fix: added missing lua_isinteger function 14 | 2.0.1 DS Fix: fixed Register function 15 | Added Example 6 - Call a Lua function 16 | 2.0 DS Updated Lua Lib to 5.3.0 17 | Rewrite of Delphi register functions 18 | Removed Class only functions 19 | Removed a lot of convenience overloaded functions 20 | Support for mobile compiler 21 | 1.4 DS Rewrite of Lua function calls, they use now published static 22 | methods, no need for a Callbacklist required anymore 23 | Added Package functions 24 | Added Class registering functions 25 | 1.3 DS Improved Callback, now uses pointer instead of object index 26 | Modified RegisterFunctions to allow methods from other class 27 | to be registered, moved object table into TLua class 28 | 1.2 DS Added example on how to extend lua with a delphi dll 29 | 1.1 DS Improved global object table, this optimizes the delphi 30 | function calls 31 | 1.0 DS Initial Release 32 | 33 | 34 | This is a Lua Wrapper for Delphi XE5-D10.1 which 35 | automatically creates OOP callback functions for Delphi <-> Lua: 36 | 37 | 38 | uses 39 | VerySimple.Lua, VerySimple.Lua.Lib; 40 | 41 | type 42 | TMyLua = class(TVerySimpleLua) 43 | published 44 | function HelloWorld(LuaState: TLuaState): Integer; 45 | end; 46 | 47 | function TMyLua.HelloWorld(LuaState: TLuaState): Integer; 48 | var 49 | ArgCount: Integer; 50 | I: integer; 51 | begin 52 | ArgCount := Lua_GetTop(LuaState); 53 | 54 | writeln('Delphi: Hello World'); 55 | writeln('Arguments: ', ArgCount); 56 | 57 | for I := 1 to ArgCount do 58 | writeln('Arg1', I, ': ', Lua_ToInteger(LuaState, I)); 59 | 60 | // Clear stack 61 | Lua_Pop(LuaState, Lua_GetTop(LuaState)); 62 | 63 | // Push return values 64 | Lua_PushInteger(LuaState, 101); 65 | Lua_PushInteger(LuaState, 102); 66 | Result := 2; 67 | end; 68 | 69 | 70 | var 71 | MyLua: TVerySimpleLua; 72 | 73 | begin 74 | MyLua := TMyLua.Create; 75 | MyLua.DoFile('Helloworld.lua'); 76 | MyLua.Free; 77 | end; 78 | 79 | 80 | 81 | helloworld.lua 82 | 83 | print("LuaDelphi Test"); 84 | p1,p2 = HelloWorld(1,2,3) 85 | print "Results:"; 86 | print (p1); 87 | print (p2); 88 | 89 | 90 | ************************************************************************** 91 | Copyright 2016 Dennis D. Spreen (http://blog.spreendigital.de/) 92 | 93 | VerySimple.Lua is distributed under the terms of the Mozilla Public License, 94 | v. 2.0. If a copy of the MPL was not distributed with your software, 95 | You can obtain one at http://mozilla.org/MPL/2.0/. 96 | 97 | ************************************************************************** 98 | 99 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Mozilla Public License Version 2.0 2 | ================================== 3 | 4 | 1. Definitions 5 | -------------- 6 | 7 | 1.1. "Contributor" 8 | means each individual or legal entity that creates, contributes to 9 | the creation of, or owns Covered Software. 10 | 11 | 1.2. "Contributor Version" 12 | means the combination of the Contributions of others (if any) used 13 | by a Contributor and that particular Contributor's Contribution. 14 | 15 | 1.3. "Contribution" 16 | means Covered Software of a particular Contributor. 17 | 18 | 1.4. "Covered Software" 19 | means Source Code Form to which the initial Contributor has attached 20 | the notice in Exhibit A, the Executable Form of such Source Code 21 | Form, and Modifications of such Source Code Form, in each case 22 | including portions thereof. 23 | 24 | 1.5. "Incompatible With Secondary Licenses" 25 | means 26 | 27 | (a) that the initial Contributor has attached the notice described 28 | in Exhibit B to the Covered Software; or 29 | 30 | (b) that the Covered Software was made available under the terms of 31 | version 1.1 or earlier of the License, but not also under the 32 | terms of a Secondary License. 33 | 34 | 1.6. "Executable Form" 35 | means any form of the work other than Source Code Form. 36 | 37 | 1.7. "Larger Work" 38 | means a work that combines Covered Software with other material, in 39 | a separate file or files, that is not Covered Software. 40 | 41 | 1.8. "License" 42 | means this document. 43 | 44 | 1.9. "Licensable" 45 | means having the right to grant, to the maximum extent possible, 46 | whether at the time of the initial grant or subsequently, any and 47 | all of the rights conveyed by this License. 48 | 49 | 1.10. "Modifications" 50 | means any of the following: 51 | 52 | (a) any file in Source Code Form that results from an addition to, 53 | deletion from, or modification of the contents of Covered 54 | Software; or 55 | 56 | (b) any new file in Source Code Form that contains any Covered 57 | Software. 58 | 59 | 1.11. "Patent Claims" of a Contributor 60 | means any patent claim(s), including without limitation, method, 61 | process, and apparatus claims, in any patent Licensable by such 62 | Contributor that would be infringed, but for the grant of the 63 | License, by the making, using, selling, offering for sale, having 64 | made, import, or transfer of either its Contributions or its 65 | Contributor Version. 66 | 67 | 1.12. "Secondary License" 68 | means either the GNU General Public License, Version 2.0, the GNU 69 | Lesser General Public License, Version 2.1, the GNU Affero General 70 | Public License, Version 3.0, or any later versions of those 71 | licenses. 72 | 73 | 1.13. "Source Code Form" 74 | means the form of the work preferred for making modifications. 75 | 76 | 1.14. "You" (or "Your") 77 | means an individual or a legal entity exercising rights under this 78 | License. For legal entities, "You" includes any entity that 79 | controls, is controlled by, or is under common control with You. For 80 | purposes of this definition, "control" means (a) the power, direct 81 | or indirect, to cause the direction or management of such entity, 82 | whether by contract or otherwise, or (b) ownership of more than 83 | fifty percent (50%) of the outstanding shares or beneficial 84 | ownership of such entity. 85 | 86 | 2. License Grants and Conditions 87 | -------------------------------- 88 | 89 | 2.1. Grants 90 | 91 | Each Contributor hereby grants You a world-wide, royalty-free, 92 | non-exclusive license: 93 | 94 | (a) under intellectual property rights (other than patent or trademark) 95 | Licensable by such Contributor to use, reproduce, make available, 96 | modify, display, perform, distribute, and otherwise exploit its 97 | Contributions, either on an unmodified basis, with Modifications, or 98 | as part of a Larger Work; and 99 | 100 | (b) under Patent Claims of such Contributor to make, use, sell, offer 101 | for sale, have made, import, and otherwise transfer either its 102 | Contributions or its Contributor Version. 103 | 104 | 2.2. Effective Date 105 | 106 | The licenses granted in Section 2.1 with respect to any Contribution 107 | become effective for each Contribution on the date the Contributor first 108 | distributes such Contribution. 109 | 110 | 2.3. Limitations on Grant Scope 111 | 112 | The licenses granted in this Section 2 are the only rights granted under 113 | this License. No additional rights or licenses will be implied from the 114 | distribution or licensing of Covered Software under this License. 115 | Notwithstanding Section 2.1(b) above, no patent license is granted by a 116 | Contributor: 117 | 118 | (a) for any code that a Contributor has removed from Covered Software; 119 | or 120 | 121 | (b) for infringements caused by: (i) Your and any other third party's 122 | modifications of Covered Software, or (ii) the combination of its 123 | Contributions with other software (except as part of its Contributor 124 | Version); or 125 | 126 | (c) under Patent Claims infringed by Covered Software in the absence of 127 | its Contributions. 128 | 129 | This License does not grant any rights in the trademarks, service marks, 130 | or logos of any Contributor (except as may be necessary to comply with 131 | the notice requirements in Section 3.4). 132 | 133 | 2.4. Subsequent Licenses 134 | 135 | No Contributor makes additional grants as a result of Your choice to 136 | distribute the Covered Software under a subsequent version of this 137 | License (see Section 10.2) or under the terms of a Secondary License (if 138 | permitted under the terms of Section 3.3). 139 | 140 | 2.5. Representation 141 | 142 | Each Contributor represents that the Contributor believes its 143 | Contributions are its original creation(s) or it has sufficient rights 144 | to grant the rights to its Contributions conveyed by this License. 145 | 146 | 2.6. Fair Use 147 | 148 | This License is not intended to limit any rights You have under 149 | applicable copyright doctrines of fair use, fair dealing, or other 150 | equivalents. 151 | 152 | 2.7. Conditions 153 | 154 | Sections 3.1, 3.2, 3.3, and 3.4 are conditions of the licenses granted 155 | in Section 2.1. 156 | 157 | 3. Responsibilities 158 | ------------------- 159 | 160 | 3.1. Distribution of Source Form 161 | 162 | All distribution of Covered Software in Source Code Form, including any 163 | Modifications that You create or to which You contribute, must be under 164 | the terms of this License. You must inform recipients that the Source 165 | Code Form of the Covered Software is governed by the terms of this 166 | License, and how they can obtain a copy of this License. You may not 167 | attempt to alter or restrict the recipients' rights in the Source Code 168 | Form. 169 | 170 | 3.2. Distribution of Executable Form 171 | 172 | If You distribute Covered Software in Executable Form then: 173 | 174 | (a) such Covered Software must also be made available in Source Code 175 | Form, as described in Section 3.1, and You must inform recipients of 176 | the Executable Form how they can obtain a copy of such Source Code 177 | Form by reasonable means in a timely manner, at a charge no more 178 | than the cost of distribution to the recipient; and 179 | 180 | (b) You may distribute such Executable Form under the terms of this 181 | License, or sublicense it under different terms, provided that the 182 | license for the Executable Form does not attempt to limit or alter 183 | the recipients' rights in the Source Code Form under this License. 184 | 185 | 3.3. Distribution of a Larger Work 186 | 187 | You may create and distribute a Larger Work under terms of Your choice, 188 | provided that You also comply with the requirements of this License for 189 | the Covered Software. If the Larger Work is a combination of Covered 190 | Software with a work governed by one or more Secondary Licenses, and the 191 | Covered Software is not Incompatible With Secondary Licenses, this 192 | License permits You to additionally distribute such Covered Software 193 | under the terms of such Secondary License(s), so that the recipient of 194 | the Larger Work may, at their option, further distribute the Covered 195 | Software under the terms of either this License or such Secondary 196 | License(s). 197 | 198 | 3.4. Notices 199 | 200 | You may not remove or alter the substance of any license notices 201 | (including copyright notices, patent notices, disclaimers of warranty, 202 | or limitations of liability) contained within the Source Code Form of 203 | the Covered Software, except that You may alter any license notices to 204 | the extent required to remedy known factual inaccuracies. 205 | 206 | 3.5. Application of Additional Terms 207 | 208 | You may choose to offer, and to charge a fee for, warranty, support, 209 | indemnity or liability obligations to one or more recipients of Covered 210 | Software. However, You may do so only on Your own behalf, and not on 211 | behalf of any Contributor. You must make it absolutely clear that any 212 | such warranty, support, indemnity, or liability obligation is offered by 213 | You alone, and You hereby agree to indemnify every Contributor for any 214 | liability incurred by such Contributor as a result of warranty, support, 215 | indemnity or liability terms You offer. You may include additional 216 | disclaimers of warranty and limitations of liability specific to any 217 | jurisdiction. 218 | 219 | 4. Inability to Comply Due to Statute or Regulation 220 | --------------------------------------------------- 221 | 222 | If it is impossible for You to comply with any of the terms of this 223 | License with respect to some or all of the Covered Software due to 224 | statute, judicial order, or regulation then You must: (a) comply with 225 | the terms of this License to the maximum extent possible; and (b) 226 | describe the limitations and the code they affect. Such description must 227 | be placed in a text file included with all distributions of the Covered 228 | Software under this License. Except to the extent prohibited by statute 229 | or regulation, such description must be sufficiently detailed for a 230 | recipient of ordinary skill to be able to understand it. 231 | 232 | 5. Termination 233 | -------------- 234 | 235 | 5.1. The rights granted under this License will terminate automatically 236 | if You fail to comply with any of its terms. However, if You become 237 | compliant, then the rights granted under this License from a particular 238 | Contributor are reinstated (a) provisionally, unless and until such 239 | Contributor explicitly and finally terminates Your grants, and (b) on an 240 | ongoing basis, if such Contributor fails to notify You of the 241 | non-compliance by some reasonable means prior to 60 days after You have 242 | come back into compliance. Moreover, Your grants from a particular 243 | Contributor are reinstated on an ongoing basis if such Contributor 244 | notifies You of the non-compliance by some reasonable means, this is the 245 | first time You have received notice of non-compliance with this License 246 | from such Contributor, and You become compliant prior to 30 days after 247 | Your receipt of the notice. 248 | 249 | 5.2. If You initiate litigation against any entity by asserting a patent 250 | infringement claim (excluding declaratory judgment actions, 251 | counter-claims, and cross-claims) alleging that a Contributor Version 252 | directly or indirectly infringes any patent, then the rights granted to 253 | You by any and all Contributors for the Covered Software under Section 254 | 2.1 of this License shall terminate. 255 | 256 | 5.3. In the event of termination under Sections 5.1 or 5.2 above, all 257 | end user license agreements (excluding distributors and resellers) which 258 | have been validly granted by You or Your distributors under this License 259 | prior to termination shall survive termination. 260 | 261 | ************************************************************************ 262 | * * 263 | * 6. Disclaimer of Warranty * 264 | * ------------------------- * 265 | * * 266 | * Covered Software is provided under this License on an "as is" * 267 | * basis, without warranty of any kind, either expressed, implied, or * 268 | * statutory, including, without limitation, warranties that the * 269 | * Covered Software is free of defects, merchantable, fit for a * 270 | * particular purpose or non-infringing. The entire risk as to the * 271 | * quality and performance of the Covered Software is with You. * 272 | * Should any Covered Software prove defective in any respect, You * 273 | * (not any Contributor) assume the cost of any necessary servicing, * 274 | * repair, or correction. This disclaimer of warranty constitutes an * 275 | * essential part of this License. No use of any Covered Software is * 276 | * authorized under this License except under this disclaimer. * 277 | * * 278 | ************************************************************************ 279 | 280 | ************************************************************************ 281 | * * 282 | * 7. Limitation of Liability * 283 | * -------------------------- * 284 | * * 285 | * Under no circumstances and under no legal theory, whether tort * 286 | * (including negligence), contract, or otherwise, shall any * 287 | * Contributor, or anyone who distributes Covered Software as * 288 | * permitted above, be liable to You for any direct, indirect, * 289 | * special, incidental, or consequential damages of any character * 290 | * including, without limitation, damages for lost profits, loss of * 291 | * goodwill, work stoppage, computer failure or malfunction, or any * 292 | * and all other commercial damages or losses, even if such party * 293 | * shall have been informed of the possibility of such damages. This * 294 | * limitation of liability shall not apply to liability for death or * 295 | * personal injury resulting from such party's negligence to the * 296 | * extent applicable law prohibits such limitation. Some * 297 | * jurisdictions do not allow the exclusion or limitation of * 298 | * incidental or consequential damages, so this exclusion and * 299 | * limitation may not apply to You. * 300 | * * 301 | ************************************************************************ 302 | 303 | 8. Litigation 304 | ------------- 305 | 306 | Any litigation relating to this License may be brought only in the 307 | courts of a jurisdiction where the defendant maintains its principal 308 | place of business and such litigation shall be governed by laws of that 309 | jurisdiction, without reference to its conflict-of-law provisions. 310 | Nothing in this Section shall prevent a party's ability to bring 311 | cross-claims or counter-claims. 312 | 313 | 9. Miscellaneous 314 | ---------------- 315 | 316 | This License represents the complete agreement concerning the subject 317 | matter hereof. If any provision of this License is held to be 318 | unenforceable, such provision shall be reformed only to the extent 319 | necessary to make it enforceable. Any law or regulation which provides 320 | that the language of a contract shall be construed against the drafter 321 | shall not be used to construe this License against a Contributor. 322 | 323 | 10. Versions of the License 324 | --------------------------- 325 | 326 | 10.1. New Versions 327 | 328 | Mozilla Foundation is the license steward. Except as provided in Section 329 | 10.3, no one other than the license steward has the right to modify or 330 | publish new versions of this License. Each version will be given a 331 | distinguishing version number. 332 | 333 | 10.2. Effect of New Versions 334 | 335 | You may distribute the Covered Software under the terms of the version 336 | of the License under which You originally received the Covered Software, 337 | or under the terms of any subsequent version published by the license 338 | steward. 339 | 340 | 10.3. Modified Versions 341 | 342 | If you create software not governed by this License, and you want to 343 | create a new license for such software, you may create and use a 344 | modified version of this License if you rename the license and remove 345 | any references to the name of the license steward (except to note that 346 | such modified license differs from this License). 347 | 348 | 10.4. Distributing Source Code Form that is Incompatible With Secondary 349 | Licenses 350 | 351 | If You choose to distribute Source Code Form that is Incompatible With 352 | Secondary Licenses under the terms of this version of the License, the 353 | notice described in Exhibit B of this License must be attached. 354 | 355 | Exhibit A - Source Code Form License Notice 356 | ------------------------------------------- 357 | 358 | This Source Code Form is subject to the terms of the Mozilla Public 359 | License, v. 2.0. If a copy of the MPL was not distributed with this 360 | file, You can obtain one at http://mozilla.org/MPL/2.0/. 361 | 362 | If it is not possible or desirable to put the notice in a particular 363 | file, then You may include the notice in a location (such as a LICENSE 364 | file in a relevant directory) where a recipient would be likely to look 365 | for such a notice. 366 | 367 | You may add additional accurate notices of copyright ownership. 368 | 369 | Exhibit B - "Incompatible With Secondary Licenses" Notice 370 | --------------------------------------------------------- 371 | 372 | This Source Code Form is "Incompatible With Secondary Licenses", as 373 | defined by the Mozilla Public License, v. 2.0. 374 | -------------------------------------------------------------------------------- /Source/VerySimple.Lua.pas: -------------------------------------------------------------------------------- 1 | { 2 | /** 3 | * @package VerySimple.Lua 4 | * @copyright Copyright (c) 2009-2015 Dennis D. Spreen (http://blog.spreendigital.de/) 5 | * @license Mozilla Public License Version 2.0 6 | * @author Dennis D. Spreen 7 | * @version 2.1 8 | * @url http://blog.spreendigital.de/2015/02/18/verysimple-lua-2-0-a-cross-platform-lua-5-3-0-wrapper-for-delphi-xe5-xe7/ 9 | */ 10 | 11 | History 12 | 2.1 DS Added [hidden] attribute 13 | Added OnError property 14 | Fixed MULTIRET lua call 15 | Fixed function luaL_testudata 16 | 2.0.3 DS Fix XE5 - Added Pointer cast for RegisterFunc 17 | 2.0.2 DS Fix: added missing lua_isinteger function 18 | 2.0.1 DS Fix: fixed Register function 19 | 2.0 DS Updated Lua Lib to 5.3.0 20 | Rewrite of Delphi register functions 21 | Removed Class only functions 22 | Removed a lot of convenience overloaded functions 23 | Support for mobile compiler 24 | 1.4 DS Rewrite of Lua function calls, they use now published static 25 | methods, no need for a Callbacklist required anymore 26 | Added Package functions 27 | Added Class registering functions 28 | 1.3 DS Improved Callback, now uses pointer instead of object index 29 | Modified RegisterFunctions to allow methods from other class 30 | to be registered, moved object table into TLua class 31 | 1.2 DS Added example on how to extend lua with a delphi dll 32 | 1.1 DS Improved global object table, this optimizes the delphi 33 | function calls 34 | 1.0 DS Initial Release 35 | 36 | Copyright 2009-2016 Dennis D. Spreen (email: dennis@spreendigital.de) 37 | 38 | VerySimple.Lua is distributed under the terms of the Mozilla Public License, 39 | v. 2.0. If a copy of the MPL was not distributed with your software, 40 | You can obtain one at http://mozilla.org/MPL/2.0/ 41 | 42 | } 43 | 44 | unit VerySimple.Lua; 45 | 46 | interface 47 | 48 | {$M+} 49 | 50 | uses 51 | {$IF defined(POSIX)} 52 | Posix.Dlfcn, Posix.SysTypes, Posix.StdDef, 53 | {$ENDIF} 54 | 55 | {$IF defined(MSWINDOWS)} 56 | Winapi.Windows, 57 | {$ELSEIF defined(MACOS)} 58 | {$IFDEF IOS} 59 | {$DEFINE STATICLIBRARY} 60 | {$ENDIF} 61 | 62 | {$ELSEIF defined(ANDROID)} 63 | {$ENDIF} 64 | System.Rtti, System.Classes, System.SysUtils, System.IOUtils, Generics.Collections, 65 | VerySimple.Lua.Lib; 66 | 67 | type 68 | HiddenAttribute = class(TCustomAttribute); 69 | 70 | TOnLuaPrint = procedure(Msg: String) of object; 71 | 72 | ELuaLibraryNotFound = class(Exception); 73 | ELuaLibraryLoadError = class(Exception); 74 | ELuaLibraryMethodNotFound = class(Exception); 75 | 76 | TVerySimpleLua = class(TObject) 77 | private 78 | FLuaState: Lua_State; // Lua instance 79 | FOnPrint: TOnLuaPrint; 80 | FOnError: TOnLuaPrint; 81 | FFilePath: String; 82 | FLibraryPath: String; 83 | FAutoRegister: boolean; 84 | FOpened: Boolean; 85 | protected 86 | procedure DoPrint(Msg: String); virtual; 87 | procedure DoError(Msg: String); virtual; 88 | function Report(L: Lua_State; Status: Integer): Integer; virtual; 89 | 90 | // Internal package registration 91 | class procedure RegisterPackage(L: lua_State; Data: Pointer; Code: Pointer; PackageName: String); overload; virtual; 92 | public 93 | // constructor with Autoregister published functions or without (default) 94 | constructor Create; virtual; 95 | destructor Destroy; override; 96 | procedure Open; virtual; 97 | procedure Close; virtual; 98 | 99 | // Former protected functions to check for published/attributes of methods and properties 100 | class function ValidMethod(Method: TRttiMethod): Boolean; virtual; 101 | class function ValidProperty(LProperty: TRttiProperty): Boolean; virtual; 102 | 103 | // Convenience Lua function(s) 104 | function DoFile(Filename: String): Integer; virtual;// load file and execute 105 | function DoString(Value: String): Integer; virtual; 106 | function DoChunk(L: Lua_State; Status: Integer): Integer; virtual; 107 | function DoCall(L: Lua_State; NArg, NRes: Integer): Integer; virtual; 108 | function DoStream(Stream: TStream; Size:Int64=0; ChunkName: String=''): Integer; virtual; 109 | function LoadFile(Filename: String): Integer; virtual; 110 | function LoadString(Value: String): Integer; virtual; 111 | function Run: Integer; virtual; 112 | 113 | // functions for manually registering new lua functions 114 | class procedure PushFunction(L: lua_State; Data, Code: Pointer; FuncName: String); overload; 115 | class procedure PushFunction(L: lua_State; Data, Code: Pointer); overload; 116 | 117 | class procedure RegisterFunction(L: lua_State; Func: lua_CFunction; FuncName: String); overload; virtual; 118 | procedure RegisterFunction(Func: lua_CFunction; FuncName: String); overload; virtual; 119 | 120 | class procedure RegisterFunction(L: lua_State; Data: Pointer; Code: Pointer; FuncName: String); overload; virtual; 121 | class procedure RegisterFunction(L: lua_State; AClass: TClass; Func, FuncName: String); overload; virtual; 122 | class procedure RegisterFunction(L: lua_State; AObject: TObject; Func, FuncName: String); overload; virtual; 123 | class procedure RegisterTableFunction(L: lua_State; Data: Pointer; Code: Pointer; FuncName: String); overload; virtual; 124 | class procedure RegisterTableFunction(L: lua_State; AObject: TObject; Func: String); overload; virtual; 125 | class procedure RegisterClassFunction(L: lua_State; AObject: TObject; Func, FuncName: String); overload; virtual; 126 | 127 | procedure RegisterFunction(AClass: TClass; Func: String); overload; virtual; 128 | procedure RegisterFunction(AClass: TClass; Func, FuncName: String); overload; virtual; 129 | procedure RegisterFunction(AObject: TObject; Func, FuncName: String); overload; virtual; 130 | procedure RegisterFunction(AObject: TObject; Func: String); overload; virtual; 131 | procedure RegisterFunction(Func: String); overload; virtual; 132 | procedure RegisterFunction(Func, FuncName: String); overload; virtual; 133 | 134 | class procedure RegisterFunctions(L: lua_State; AClass: TClass); overload; virtual; 135 | class procedure RegisterFunctions(L: lua_State; AObject: TObject); overload; virtual; 136 | 137 | // Register all published functions in a Table on top of the stack 138 | class procedure RegisterTableFunctions(L: lua_State; AObject: TObject); overload; virtual; 139 | 140 | // automatically register all functions published by a class or an object 141 | procedure RegisterFunctions(AClass: TClass); overload; virtual; 142 | procedure RegisterFunctions(AObject: TObject); overload; virtual; 143 | 144 | // *** 145 | // package register functions 146 | // *** 147 | // Register all published functions for a Package table on the stack 148 | class procedure RegisterPackageFunctions(L: lua_State; AObject: TObject); overload; virtual; 149 | 150 | // Register a Package with a specific package loader (cdecl static function) 151 | class procedure RegisterPackage(L: lua_State; PackageName: String; InitFunc: lua_CFunction); overload; virtual; 152 | procedure RegisterPackage(PackageName: String; InitFunc: lua_CFunction); overload; inline; 153 | 154 | // Register a Package with a specific object package loader 155 | class procedure RegisterPackage(L: lua_State; PackageName: String; AObject: TObject; PackageLoader: String); overload; virtual; 156 | procedure RegisterPackage(PackageName: String; AObject: TObject; PackageLoader: String); overload; inline; 157 | 158 | // Register a Package with the default package loader (auto register all published functions) 159 | class procedure RegisterPackage(L: lua_State; PackageName: String; AObject: TObject); overload; virtual; 160 | procedure RegisterPackage(PackageName: String; AObject: TObject); overload; inline; 161 | 162 | // *** 163 | // library functions 164 | // *** 165 | class procedure LoadLuaLibrary(const LibraryPath: String); virtual; 166 | class procedure FreeLuaLibrary; virtual; 167 | class function LuaLibraryLoaded: Boolean; virtual; 168 | 169 | // *** 170 | // properties 171 | // *** 172 | property AutoRegister: Boolean read FAutoRegister write FAutoRegister; 173 | property FilePath: String read FFilePath write FFilePath; 174 | property LibraryPath: String read FLibraryPath write FLibraryPath; 175 | property OnPrint: TOnLuaPrint read FOnPrint write FOnPrint; 176 | property OnError: TOnLuaPrint read FOnError write FOnError; 177 | property Opened: Boolean read FOpened; 178 | property LuaState: Lua_State read FLuaState write FLuaState; 179 | 180 | published 181 | function print(L: Lua_State): Integer; virtual; 182 | end; 183 | 184 | 185 | implementation 186 | 187 | uses 188 | System.TypInfo; 189 | 190 | const 191 | ChunkSize = 4096; 192 | 193 | type 194 | TLuaProc = function(L: Lua_State): Integer of object; // Lua Function 195 | 196 | TLuaChunkStream = class(TOBject) 197 | public 198 | Stream: TStream; // Source Stream 199 | Size: Int64; 200 | Chunk: TMemoryStream; // Chunk Stream 201 | function Read(sz: Psize_t): Pointer; 202 | constructor Create; virtual; 203 | destructor Destroy; override; 204 | end; 205 | 206 | var 207 | LibraryHandle: HMODULE; 208 | 209 | {* 210 | ** Message handler used to run all chunks 211 | *} 212 | function MsgHandler(L: Lua_State): Integer; cdecl; 213 | var 214 | Msg: MarshaledAString; 215 | begin 216 | Msg := lua_tostring(L, 1); 217 | if (Msg = NIL) then //* is error object not a string? 218 | if (luaL_callmeta(L, 1, '__tostring') <> 0) and //* does it have a metamethod */ 219 | (lua_type(L, -1) = LUA_TSTRING) then //* that produces a string? */ 220 | begin 221 | Result := 1; //* that is the message */} 222 | Exit; 223 | end 224 | else 225 | Msg := lua_pushfstring(L, '(error object is a %s value)', 226 | [luaL_typename(L, 1)]); 227 | 228 | luaL_traceback(L, L, Msg, 1); //* append a standard traceback */ 229 | Result := 1; //* return the traceback */ 230 | end; 231 | 232 | // 233 | // This function is called by Lua, it extracts the object by 234 | // pointer to the objects method by name, which is then called. 235 | // 236 | // @param Lua_State L Pointer to Lua instance 237 | // @return Integer Number of result arguments on stack 238 | // 239 | function LuaCallBack(L: Lua_State): Integer; cdecl; 240 | var 241 | Routine: TMethod; // Code and Data for the call back method 242 | begin 243 | // Retrieve closure values (=object Pointer) 244 | Routine.Data := lua_topointer(L, lua_upvalueindex(1)); 245 | Routine.Code := lua_topointer(L, lua_upvalueindex(2)); 246 | 247 | // Call object function 248 | Result := TLuaProc(Routine)(L); 249 | end; 250 | 251 | 252 | function LuaLoadPackage(L: Lua_State): Integer; cdecl; 253 | var 254 | Obj: TObject; 255 | begin 256 | // Retrieve closure values (=object Pointer) 257 | Obj := lua_topointer(L, lua_upvalueindex(1)); 258 | 259 | // Create new table 260 | lua_newtable(L); 261 | 262 | // Register Package functions 263 | TVerySimpleLua.RegisterPackageFunctions(L, Obj); 264 | 265 | // Return table 266 | Result := 1; 267 | end; 268 | 269 | 270 | { TVerySimpleLua } 271 | 272 | 273 | 274 | procedure TVerySimpleLua.Close; 275 | begin 276 | if not FOpened then 277 | Exit; 278 | 279 | FOpened := False; 280 | 281 | // Close instance 282 | Lua_Close(LuaState); 283 | end; 284 | 285 | constructor TVerySimpleLua.Create; 286 | begin 287 | FAutoRegister := True; 288 | 289 | {$IF defined(IOS)} 290 | FilePath := TPath.GetDocumentsPath + PathDelim; 291 | {$ELSEIF defined(ANDROID)} 292 | LibraryPath := IncludeTrailingPathDelimiter(System.IOUtils.TPath.GetLibraryPath) + LUA_LIBRARY; 293 | FilePath := TPath.GetDocumentsPath + PathDelim; 294 | {$ENDIF} 295 | end; 296 | 297 | 298 | // 299 | // Dispose Lua instance 300 | // 301 | 302 | destructor TVerySimpleLua.Destroy; 303 | begin 304 | Close; 305 | inherited; 306 | end; 307 | 308 | 309 | // 310 | // Wrapper for Lua File load and Execution 311 | // 312 | // @param String Filename Lua Script file name 313 | // @return Integer 314 | // 315 | 316 | function TVerySimpleLua.DoChunk(L: Lua_State; Status: Integer): Integer; 317 | begin 318 | if Status = LUA_OK then 319 | Status := DoCall(L, 0, LUA_MULTRET); 320 | Result := Report(L, Status); 321 | end; 322 | 323 | procedure TVerySimpleLua.DoError(Msg: String); 324 | begin 325 | 326 | end; 327 | 328 | {* 329 | ** Interface to 'lua_pcall', which sets appropriate message function 330 | ** and C-signal handler. Used to run all chunks. 331 | *} 332 | function TVerySimpleLua.DoCall(L: Lua_State; NArg, NRes: Integer): Integer; 333 | var 334 | Status: Integer; 335 | Base: Integer; 336 | begin 337 | Base := lua_gettop(L) - NArg; // function index 338 | lua_pushcfunction(L, msghandler); // push message handler 339 | lua_insert(L, base); // put it under function and args 340 | Status := lua_pcall(L, narg, nres, base); 341 | lua_remove(L, base); // remove message handler from the stack */ 342 | Result := Status; 343 | end; 344 | 345 | function TVerySimpleLua.DoFile(Filename: String): Integer; 346 | var 347 | Marshall: TMarshaller; 348 | Path: String; 349 | begin 350 | if not Opened then 351 | Open; 352 | 353 | Path := FilePath + FileName; 354 | Result := dochunk(LuaState, lual_loadfile(LuaState, Marshall.AsAnsi(Path).ToPointer)); 355 | end; 356 | 357 | 358 | 359 | procedure TVerySimpleLua.DoPrint(Msg: String); 360 | begin 361 | if Assigned(FOnPrint) then 362 | FOnPrint(Msg) 363 | else 364 | Writeln(Msg); 365 | end; 366 | 367 | function TVerySimpleLua.DoString(Value: String): Integer; 368 | var 369 | Marshall: TMarshaller; 370 | begin 371 | if not Opened then 372 | Open; 373 | 374 | Result := luaL_dostring(LuaState, Marshall.AsAnsi(Value).ToPointer); 375 | end; 376 | 377 | function LuaReader(L: lua_State; ud: Pointer; sz: Psize_t): Pointer; cdecl; 378 | var 379 | ChunkStream: TLuaChunkStream; 380 | begin 381 | ChunkStream := Ud; 382 | Result := ChunkStream.Read(sz); 383 | end; 384 | 385 | 386 | function TVerySimpleLua.DoStream(Stream: TStream; Size: Int64=0; ChunkName: String=''): Integer; 387 | var 388 | ChunkStream: TLuaChunkStream; 389 | Marshall: TMarshaller; 390 | begin 391 | if not Opened then 392 | Open; 393 | 394 | ChunkStream := TLuaChunkStream.Create; 395 | try 396 | ChunkStream.Stream := Stream; 397 | if Size = 0 then 398 | Size := Stream.Size; 399 | ChunkStream.Size := Size; 400 | Result := lua_load(LuaState, LuaReader, ChunkStream, Marshall.AsAnsi(ChunkName).ToPointer, NIL); 401 | if Result = 0 then 402 | Result := dochunk(LuaState, Result); 403 | Result := Report(LuaState, Result); 404 | finally 405 | ChunkStream.Free; 406 | end; 407 | end; 408 | 409 | 410 | function TVerySimpleLua.Run: Integer; 411 | begin 412 | if not Opened then 413 | Open; 414 | 415 | Result := dochunk(LuaState, LUA_OK); 416 | end; 417 | 418 | 419 | 420 | 421 | class procedure TVerySimpleLua.RegisterFunction(L: lua_State; Func: lua_CFunction; 422 | FuncName: String); 423 | var 424 | Marshall: TMarshaller; 425 | begin 426 | lua_register(L, Marshall.AsAnsi(FuncName).ToPointer, Func); 427 | end; 428 | 429 | class procedure TVerySimpleLua.PushFunction(L: lua_State; Data, Code: Pointer); 430 | begin 431 | // prepare Closure value (CallBack Object Pointer) 432 | lua_pushlightuserdata(L, Data); 433 | lua_pushlightuserdata(L, Code); 434 | 435 | // set new Lua function with Closure values 436 | lua_pushcclosure(L, LuaCallBack, 2); 437 | end; 438 | 439 | 440 | class procedure TVerySimpleLua.PushFunction(L: lua_State; Data, Code: Pointer; FuncName: String); 441 | var 442 | Marshall: TMarshaller; 443 | begin 444 | // prepare Closure value (Method Name) 445 | lua_pushstring(L, Marshall.AsAnsi(FuncName).ToPointer); 446 | PushFunction(L, Data, Code); 447 | end; 448 | 449 | 450 | class procedure TVerySimpleLua.RegisterFunction(L: lua_State; Data, Code: Pointer; FuncName: String); 451 | var 452 | Marshall: TMarshaller; 453 | begin 454 | PushFunction(L, Data, Code, FuncName); 455 | 456 | // set table using the method's name 457 | lua_setglobal(L, Marshall.AsAnsi(FuncName).ToPointer); 458 | end; 459 | 460 | 461 | class procedure TVerySimpleLua.RegisterFunction(L: lua_State; AObject: TObject; Func, FuncName: String); 462 | begin 463 | RegisterFunction(L, AObject, AObject.MethodAddress(Func), FuncName); 464 | end; 465 | 466 | class procedure TVerySimpleLua.RegisterClassFunction(L: lua_State; AObject: TObject; Func, FuncName: String); 467 | begin 468 | RegisterFunction(L, Pointer(AObject.ClassType), AObject.ClassType.MethodAddress(Func), FuncName); 469 | end; 470 | 471 | 472 | class procedure TVerySimpleLua.RegisterFunction(L: lua_State; AClass: TClass; Func, FuncName: String); 473 | begin 474 | RegisterFunction(L, AClass, AClass.MethodAddress(Func), FuncName); 475 | end; 476 | 477 | procedure TVerySimpleLua.RegisterFunction(Func: lua_CFunction;FuncName: String); 478 | begin 479 | RegisterFunction(LuaState, Func, FuncName); 480 | end; 481 | 482 | procedure TVerySimpleLua.RegisterFunction(AClass: TClass; Func: String); 483 | begin 484 | RegisterFunction(LuaState, AClass, AClass.MethodAddress(Func), Func); 485 | end; 486 | 487 | procedure TVerySimpleLua.RegisterFunction(AClass: TClass; Func, FuncName: String); 488 | begin 489 | RegisterFunction(LuaState, AClass, AClass.MethodAddress(Func), FuncName); 490 | end; 491 | 492 | 493 | procedure TVerySimpleLua.RegisterFunction(AObject: TObject; Func, FuncName: String); 494 | begin 495 | RegisterFunction(LuaState, AObject, Func, FuncName); 496 | end; 497 | 498 | procedure TVerySimpleLua.RegisterFunction(AObject: TObject; Func: String); 499 | begin 500 | RegisterFunction(LuaState, AObject, Func, Func); 501 | end; 502 | 503 | procedure TVerySimpleLua.RegisterFunction(Func: String); 504 | begin 505 | RegisterFunction(LuaState, self, Func, Func); 506 | end; 507 | 508 | procedure TVerySimpleLua.RegisterFunction(Func, FuncName: String); 509 | begin 510 | RegisterFunction(LuaState, self, Func, FuncName); 511 | end; 512 | 513 | 514 | 515 | class procedure TVerySimpleLua.RegisterFunctions(L: lua_State; AClass: TClass); 516 | var 517 | LContext: TRttiContext; 518 | LType: TRttiType; 519 | LMethod: TRttiMethod; 520 | begin 521 | LContext := TRttiContext.Create; 522 | try 523 | LType := LContext.GetType(AClass); 524 | for LMethod in LType.GetMethods do 525 | if ValidMethod(LMethod) then 526 | RegisterFunction(L, AClass, LMethod.Name, LMethod.Name); 527 | finally 528 | LContext.Free; 529 | end; 530 | end; 531 | 532 | class function TVerySimpleLua.ValidMethod(Method: TRttiMethod): Boolean; 533 | var 534 | Params: TArray; 535 | Param: TRttiParameter; 536 | Attribute: TCustomAttribute; 537 | begin 538 | Result := False; 539 | 540 | // Only published functions with an Integer result allowed 541 | if (Method.Visibility <> mvPublished) or (not Assigned(Method.ReturnType)) or 542 | (Method.ReturnType.TypeKind <> tkInteger) then 543 | Exit; 544 | 545 | // Only functions with 1 parameter allowed 546 | Params := Method.GetParameters; 547 | if Length(Params) <> 1 then 548 | Exit; 549 | 550 | // Only functions with a Pointer as parameter allowed 551 | Param := Params[0]; 552 | if Param.ParamType.TypeKind <> tkPointer then 553 | Exit; 554 | 555 | // Check for Attributes 556 | for Attribute in Method.GetAttributes do 557 | if Attribute is HiddenAttribute then 558 | Exit; 559 | 560 | Result := True; 561 | end; 562 | 563 | 564 | class function TVerySimpleLua.ValidProperty(LProperty: TRttiProperty): Boolean; 565 | var 566 | Attribute: TCustomAttribute; 567 | begin 568 | Result := False; 569 | 570 | // Only published property allowed 571 | if (LProperty.Visibility <> mvPublished) then 572 | Exit; 573 | 574 | // Check for Attributes 575 | for Attribute in LProperty.GetAttributes do 576 | if Attribute is HiddenAttribute then 577 | Exit; 578 | 579 | Result := True; 580 | end; 581 | 582 | class procedure TVerySimpleLua.RegisterFunctions(L: lua_State; AObject: TObject); 583 | var 584 | LContext: TRttiContext; 585 | LType: TRttiType; 586 | LMethod: TRttiMethod; 587 | begin 588 | LContext := TRttiContext.Create; 589 | try 590 | LType := LContext.GetType(AObject.ClassType); 591 | for LMethod in LType.GetMethods do 592 | begin 593 | // Only published functions with an Integer result allowed 594 | if not ValidMethod(LMethod) then 595 | Continue; 596 | 597 | // Register the method based on calling convention and method kind 598 | if LMethod.MethodKind = mkFunction then 599 | RegisterFunction(L, AObject, AObject.MethodAddress(LMethod.Name), LMethod.Name) 600 | else 601 | if LMethod.MethodKind = mkClassFunction then 602 | if (LMethod.IsStatic) and (LMethod.CallingConvention = ccCdecl) then 603 | RegisterFunction(L, lua_CFunction(AObject.MethodAddress(LMethod.Name)), LMethod.Name) 604 | else 605 | RegisterFunction(L, Pointer(AObject.ClassType), AObject.ClassType.MethodAddress(LMethod.Name), LMethod.Name); 606 | end; 607 | finally 608 | LContext.Free; 609 | end; 610 | end; 611 | 612 | class procedure TVerySimpleLua.RegisterPackageFunctions(L: lua_State; AObject: TObject); 613 | var 614 | LContext: TRttiContext; 615 | LType: TRttiType; 616 | LMethod: TRttiMethod; 617 | begin 618 | LContext := TRttiContext.Create; 619 | try 620 | LType := LContext.GetType(AObject.ClassType); 621 | for LMethod in LType.GetMethods do 622 | begin 623 | // Only published functions with an Integer result allowed 624 | if not ValidMethod(LMethod) then 625 | Continue; 626 | 627 | PushFunction(L, AObject, LMethod.CodeAddress, LMethod.Name); 628 | lua_rawset(L, -3); 629 | end; 630 | finally 631 | LContext.Free; 632 | end; 633 | end; 634 | 635 | 636 | class procedure TVerySimpleLua.RegisterTableFunction(L: lua_State; Data, Code: Pointer; FuncName: String); 637 | var 638 | Marshall: TMarshaller; 639 | begin 640 | PushFunction(L, Data, Code); 641 | 642 | // set table field 643 | lua_setfield(L, -2, Marshall.AsAnsi(FuncName).ToPointer); 644 | end; 645 | 646 | class procedure TVerySimpleLua.RegisterTableFunction(L: lua_State; AObject: TObject; Func: String); 647 | begin 648 | RegisterTableFunction(L, Pointer(AObject), AObject.MethodAddress(Func), Func); 649 | end; 650 | 651 | class procedure TVerySimpleLua.RegisterTableFunctions(L: lua_State; AObject: TObject); 652 | var 653 | LContext: TRttiContext; 654 | LType: TRttiType; 655 | LMethod: TRttiMethod; 656 | begin 657 | LContext := TRttiContext.Create; 658 | try 659 | LType := LContext.GetType(AObject.ClassType); 660 | for LMethod in LType.GetMethods do 661 | begin 662 | // Only published functions with an Integer result allowed 663 | if not ValidMethod(LMethod) then 664 | Continue; 665 | 666 | // Register the method based on calling convention and method kind 667 | if LMethod.MethodKind = mkFunction then 668 | RegisterTableFunction(L, AObject, AObject.MethodAddress(LMethod.Name), LMethod.Name) 669 | end; 670 | finally 671 | LContext.Free; 672 | end; 673 | end; 674 | 675 | function TVerySimpleLua.Report(L: Lua_State; Status: Integer): Integer; 676 | var 677 | Msg: String; 678 | begin 679 | if (Status <> LUA_OK) then 680 | begin 681 | Msg := UTF8ToString(lua_tostring(L, -1)); 682 | DoError(Msg); 683 | if Assigned(FOnError) then 684 | begin 685 | FOnError(Msg); 686 | end; 687 | lua_pop(L, 1); //* remove message 688 | end; 689 | Result := Status; 690 | end; 691 | 692 | class procedure TVerySimpleLua.RegisterPackage(L: lua_State; PackageName: String; AObject: TObject); 693 | var 694 | Marshall: TMarshaller; 695 | begin 696 | lua_getglobal(L, 'package'); // get local package table 697 | lua_getfield(L, -1 , 'preload'); // get preload field 698 | 699 | // prepare Closure value (Object Object Pointer) 700 | lua_pushlightuserdata(L, AObject); 701 | 702 | // set new Lua function with Closure values 703 | lua_pushcclosure(L, LuaLoadPackage, 1); 704 | 705 | lua_setfield(L, -2 , Marshall.AsAnsi(PackageName).ToPointer); 706 | lua_pop(L, 2); 707 | end; 708 | 709 | procedure TVerySimpleLua.RegisterFunctions(AClass: TClass); 710 | begin 711 | RegisterFunctions(LuaState, AClass); 712 | end; 713 | 714 | procedure TVerySimpleLua.RegisterFunctions(AObject: TObject); 715 | begin 716 | RegisterFunctions(LuaState, AObject); 717 | end; 718 | 719 | 720 | 721 | // ******************************* 722 | // Package registration procedures 723 | // ******************************* 724 | 725 | class procedure TVerySimpleLua.RegisterPackage(L: lua_State; PackageName: String; 726 | InitFunc: lua_CFunction); 727 | var 728 | Marshall: TMarshaller; 729 | begin 730 | lua_getglobal(L, 'package'); // get local package table 731 | lua_getfield(L, -1 , 'preload'); // get preload field 732 | lua_pushcfunction(L, InitFunc); 733 | lua_setfield(L, -2 , Marshall.AsAnsi(PackageName).ToPointer); 734 | lua_pop(L, 2); 735 | end; 736 | 737 | procedure TVerySimpleLua.RegisterPackage(PackageName: String; InitFunc: lua_CFunction); 738 | begin 739 | RegisterPackage(LuaState, PackageName, InitFunc); 740 | end; 741 | 742 | 743 | class procedure TVerySimpleLua.RegisterPackage(L: lua_State; Data, Code: Pointer; PackageName: String); 744 | var 745 | Marshall: TMarshaller; 746 | begin 747 | lua_getglobal(L, 'package'); // get local package table 748 | lua_getfield(L, -1 , 'preload'); // get preload field 749 | 750 | // prepare Closure value (CallBack Object Pointer) 751 | lua_pushlightuserdata(L, Data); 752 | lua_pushlightuserdata(L, Code); 753 | 754 | // set new Lua function with Closure values 755 | lua_pushcclosure(L, LuaCallBack, 2); 756 | 757 | lua_setfield(L, -2 , Marshall.AsAnsi(PackageName).ToPointer); 758 | lua_pop(L, 2); 759 | end; 760 | 761 | 762 | procedure TVerySimpleLua.RegisterPackage(PackageName: String; AObject: TObject); 763 | begin 764 | RegisterPackage(LuaState, PackageName, AObject); 765 | end; 766 | 767 | class procedure TVerySimpleLua.RegisterPackage(L: lua_State; PackageName: String; AObject: TObject; 768 | PackageLoader: String); 769 | var 770 | LContext: TRttiContext; 771 | LType: TRttiType; 772 | LMethod: TRttiMethod; 773 | Address: Pointer; 774 | begin 775 | LContext := TRttiContext.Create; 776 | try 777 | LType := LContext.GetType(AObject.ClassType); 778 | LMethod := LType.GetMethod(PackageLoader); 779 | Address := LMethod.CodeAddress; 780 | RegisterPackage(L, AObject, Address, PackageName); 781 | finally 782 | LContext.Free; 783 | end; 784 | end; 785 | 786 | procedure TVerySimpleLua.RegisterPackage(PackageName: String; AObject: TObject; PackageLoader: String); 787 | begin 788 | RegisterPackage(LuaState, PackageName, AObject, PackageLoader); 789 | end; 790 | 791 | 792 | (* 793 | ** Dynamic library manipulation 794 | *) 795 | 796 | 797 | function GetAddress(Name: String): Pointer; 798 | begin 799 | Result := GetProcAddress(LibraryHandle, PWideChar(Name)); 800 | if not Assigned(Result) then 801 | raise ELuaLibraryMethodNotFound.Create('Entry point "' + QuotedStr(Name) + '" not found'); 802 | end; 803 | 804 | 805 | function TVerySimpleLua.LoadFile(Filename: String): Integer; 806 | var 807 | Marshall: TMarshaller; 808 | begin 809 | if not Opened then 810 | Open; 811 | Result := lual_loadfile(LuaState, Marshall.AsAnsi(Filename).ToPointer); 812 | end; 813 | 814 | 815 | class procedure TVerySimpleLua.LoadLuaLibrary(const LibraryPath: String); 816 | var 817 | LoadPath: String; 818 | begin 819 | FreeLuaLibrary; 820 | 821 | if LibraryPath = '' then 822 | LoadPath := LUA_LIBRARY 823 | else 824 | LoadPath := LibraryPath; 825 | 826 | {$IFNDEF STATICLIBRARY} 827 | 828 | // check if Library exists 829 | if not FileExists(LoadPath) then 830 | raise ELuaLibraryNotFound.Create('Lua library "' + QuotedStr(LoadPath) + '" not found'); 831 | 832 | // try to load the library 833 | LibraryHandle := LoadLibrary(PChar(LoadPath)); 834 | if LibraryHandle = 0 then 835 | raise ELuaLibraryLoadError.Create('Failed to load Lua library "' + QuotedStr(LoadPath) + '"' 836 | {$IF defined(POSIX)} + (String(dlerror)){$ENDIF}); 837 | 838 | lua_newstate := GetAddress('lua_newstate'); 839 | lua_close := GetAddress('lua_close'); 840 | lua_newthread := GetAddress('lua_newthread'); 841 | lua_atpanic := GetAddress('lua_atpanic'); 842 | lua_version := GetAddress('lua_version'); 843 | 844 | lua_absindex := GetAddress('lua_absindex'); 845 | lua_gettop := GetAddress('lua_gettop'); 846 | lua_settop := GetAddress('lua_settop'); 847 | lua_pushvalue := GetAddress('lua_pushvalue'); 848 | lua_rotate := GetAddress('lua_rotate'); 849 | lua_copy := GetAddress('lua_copy'); 850 | lua_checkstack := GetAddress('lua_checkstack'); 851 | lua_xmove := GetAddress('lua_xmove'); 852 | 853 | lua_isnumber := GetAddress('lua_isnumber'); 854 | lua_isstring := GetAddress('lua_isstring'); 855 | lua_iscfunction := GetAddress('lua_iscfunction'); 856 | lua_isinteger := GetAddress('lua_isinteger'); 857 | lua_isuserdata := GetAddress('lua_isuserdata'); 858 | lua_type := GetAddress('lua_type'); 859 | lua_typename := GetAddress('lua_typename'); 860 | 861 | lua_tonumberx := GetAddress('lua_tonumberx'); 862 | lua_tointegerx := GetAddress('lua_tointegerx'); 863 | lua_toboolean := GetAddress('lua_toboolean'); 864 | lua_tolstring := GetAddress('lua_tolstring'); 865 | lua_rawlen := GetAddress('lua_rawlen'); 866 | lua_tocfunction := GetAddress('lua_tocfunction'); 867 | lua_touserdata := GetAddress('lua_touserdata'); 868 | lua_tothread := GetAddress('lua_tothread'); 869 | lua_topointer := GetAddress('lua_topointer'); 870 | 871 | lua_arith := GetAddress('lua_arith'); 872 | lua_rawequal := GetAddress('lua_rawequal'); 873 | lua_compare := GetAddress('lua_compare'); 874 | 875 | lua_pushnil := GetAddress('lua_pushnil'); 876 | lua_pushnumber := GetAddress('lua_pushnumber'); 877 | lua_pushinteger := GetAddress('lua_pushinteger'); 878 | lua_pushlstring := GetAddress('lua_pushlstring'); 879 | lua_pushstring := GetAddress('lua_pushstring'); 880 | lua_pushvfstring := GetAddress('lua_pushvfstring'); 881 | lua_pushfstring := GetAddress('lua_pushfstring'); 882 | lua_pushcclosure := GetAddress('lua_pushcclosure'); 883 | lua_pushboolean := GetAddress('lua_pushboolean'); 884 | lua_pushlightuserdata := GetAddress('lua_pushlightuserdata'); 885 | lua_pushthread := GetAddress('lua_pushthread'); 886 | 887 | lua_getglobal := GetAddress('lua_getglobal'); 888 | lua_gettable := GetAddress('lua_gettable'); 889 | lua_getfield := GetAddress('lua_getfield'); 890 | lua_geti := GetAddress('lua_geti'); 891 | lua_rawget := GetAddress('lua_rawget'); 892 | lua_rawgeti := GetAddress('lua_rawgeti'); 893 | lua_rawgetp := GetAddress('lua_rawgetp'); 894 | 895 | lua_createtable := GetAddress('lua_createtable'); 896 | lua_newuserdata := GetAddress('lua_newuserdata'); 897 | lua_getmetatable := GetAddress('lua_getmetatable'); 898 | lua_getuservalue := GetAddress('lua_getuservalue'); 899 | 900 | lua_setglobal := GetAddress('lua_setglobal'); 901 | lua_settable := GetAddress('lua_settable'); 902 | lua_setfield := GetAddress('lua_setfield'); 903 | lua_seti := GetAddress('lua_seti'); 904 | lua_rawset := GetAddress('lua_rawset'); 905 | lua_rawseti := GetAddress('lua_rawseti'); 906 | lua_rawsetp := GetAddress('lua_rawsetp'); 907 | lua_setmetatable := GetAddress('lua_setmetatable'); 908 | lua_setuservalue := GetAddress('lua_setuservalue'); 909 | 910 | lua_callk := GetAddress('lua_callk'); 911 | lua_pcallk := GetAddress('lua_pcallk'); 912 | lua_load := GetAddress('lua_load'); 913 | lua_dump := GetAddress('lua_dump'); 914 | 915 | lua_yieldk := GetAddress('lua_yieldk'); 916 | lua_resume := GetAddress('lua_resume'); 917 | lua_status := GetAddress('lua_status'); 918 | lua_isyieldable := GetAddress('lua_isyieldable'); 919 | 920 | lua_gc := GetAddress('lua_gc'); 921 | 922 | lua_error := GetAddress('lua_error'); 923 | lua_next := GetAddress('lua_next'); 924 | lua_concat := GetAddress('lua_concat'); 925 | lua_len := GetAddress('lua_len'); 926 | 927 | lua_stringtonumber := GetAddress('lua_stringtonumber'); 928 | lua_getallocf := GetAddress('lua_getallocf'); 929 | lua_setallocf := GetAddress('lua_setallocf'); 930 | 931 | lua_getstack := GetAddress('lua_getstack'); 932 | lua_getinfo := GetAddress('lua_getinfo'); 933 | lua_getlocal := GetAddress('lua_getlocal'); 934 | lua_setlocal := GetAddress('lua_setlocal'); 935 | lua_getupvalue := GetAddress('lua_getupvalue'); 936 | lua_setupvalue := GetAddress('lua_setupvalue'); 937 | lua_upvalueid := GetAddress('lua_upvalueid'); 938 | lua_upvaluejoin := GetAddress('lua_upvaluejoin'); 939 | 940 | lua_sethook := GetAddress('lua_sethook'); 941 | lua_gethook := GetAddress('lua_gethook'); 942 | lua_gethookmask := GetAddress('lua_gethookmask'); 943 | lua_gethookcount := GetAddress('lua_gethookcount'); 944 | 945 | luaopen_base := GetAddress('luaopen_base'); 946 | luaopen_coroutine := GetAddress('luaopen_coroutine'); 947 | luaopen_table := GetAddress('luaopen_table'); 948 | luaopen_io := GetAddress('luaopen_io'); 949 | luaopen_os := GetAddress('luaopen_os'); 950 | luaopen_string := GetAddress('luaopen_string'); 951 | luaopen_utf8 := GetAddress('luaopen_utf8'); 952 | luaopen_bit32 := GetAddress('luaopen_bit32'); 953 | luaopen_math := GetAddress('luaopen_math'); 954 | luaopen_debug := GetAddress('luaopen_debug'); 955 | luaopen_package := GetAddress('luaopen_package'); 956 | 957 | luaL_openlibs := GetAddress('luaL_openlibs'); 958 | 959 | luaL_checkversion_ := GetAddress('luaL_checkversion_'); 960 | luaL_getmetafield := GetAddress('luaL_getmetafield'); 961 | luaL_callmeta := GetAddress('luaL_callmeta'); 962 | luaL_tolstring := GetAddress('luaL_tolstring'); 963 | luaL_argerror := GetAddress('luaL_argerror'); 964 | luaL_checklstring := GetAddress('luaL_checklstring'); 965 | luaL_optlstring := GetAddress('luaL_optlstring'); 966 | luaL_checknumber := GetAddress('luaL_checknumber'); 967 | luaL_optnumber := GetAddress('luaL_optnumber'); 968 | luaL_checkinteger := GetAddress('luaL_checkinteger'); 969 | luaL_optinteger := GetAddress('luaL_optinteger'); 970 | 971 | luaL_checkstack := GetAddress('luaL_checkstack'); 972 | luaL_checktype := GetAddress('luaL_checktype'); 973 | luaL_checkany := GetAddress('luaL_checkany'); 974 | 975 | luaL_newmetatable := GetAddress('luaL_newmetatable'); 976 | luaL_setmetatable := GetAddress('luaL_setmetatable'); 977 | luaL_testudata := GetAddress('luaL_testudata'); 978 | luaL_checkudata := GetAddress('luaL_checkudata'); 979 | 980 | luaL_where := GetAddress('luaL_where'); 981 | luaL_error := GetAddress('luaL_error'); 982 | 983 | luaL_checkoption := GetAddress('luaL_checkoption'); 984 | luaL_fileresult := GetAddress('luaL_fileresult'); 985 | luaL_execresult := GetAddress('luaL_execresult'); 986 | 987 | luaL_ref := GetAddress('luaL_ref'); 988 | luaL_unref := GetAddress('luaL_unref'); 989 | 990 | luaL_loadfilex := GetAddress('luaL_loadfilex'); 991 | luaL_loadbufferx := GetAddress('luaL_loadbufferx'); 992 | luaL_loadstring := GetAddress('luaL_loadstring'); 993 | luaL_newstate := GetAddress('luaL_newstate'); 994 | luaL_len := GetAddress('luaL_len'); 995 | 996 | luaL_gsub := GetAddress('luaL_gsub'); 997 | luaL_setfuncs := GetAddress('luaL_setfuncs'); 998 | 999 | luaL_getsubtable := GetAddress('luaL_getsubtable'); 1000 | luaL_traceback := GetAddress('luaL_traceback'); 1001 | luaL_requiref := GetAddress('luaL_requiref'); 1002 | 1003 | luaL_buffinit := GetAddress('luaL_buffinit'); 1004 | luaL_prepbuffsize := GetAddress('luaL_prepbuffsize'); 1005 | luaL_addlstring := GetAddress('luaL_addlstring'); 1006 | luaL_addstring := GetAddress('luaL_addstring'); 1007 | luaL_addvalue := GetAddress('luaL_addvalue'); 1008 | luaL_pushresult := GetAddress('luaL_pushresult'); 1009 | luaL_pushresultsize := GetAddress('luaL_pushresultsize'); 1010 | luaL_buffinitsize := GetAddress('luaL_buffinitsize'); 1011 | {$ENDIF} 1012 | end; 1013 | 1014 | function TVerySimpleLua.LoadString(Value: String): Integer; 1015 | var 1016 | Marshall: TMarshaller; 1017 | begin 1018 | if not Opened then 1019 | Open; 1020 | Result := luaL_loadstring(LuaState, Marshall.AsAnsi(Value).ToPointer); 1021 | end; 1022 | 1023 | class function TVerySimpleLua.LuaLibraryLoaded: Boolean; 1024 | begin 1025 | Result := (LibraryHandle <> 0); 1026 | end; 1027 | 1028 | 1029 | procedure TVerySimpleLua.Open; 1030 | begin 1031 | if FOpened then 1032 | Exit; 1033 | 1034 | FOpened := True; 1035 | 1036 | // Load Lua Lib if not already done 1037 | if not LuaLibraryLoaded then 1038 | LoadLuaLibrary(LibraryPath); 1039 | 1040 | // Open Library 1041 | LuaState := lual_newstate; // opens Lua 1042 | lual_openlibs(LuaState); // load all libs 1043 | 1044 | // register all published functions 1045 | if FAutoRegister then 1046 | RegisterFunctions(Self); 1047 | end; 1048 | 1049 | class procedure TVerySimpleLua.FreeLuaLibrary; 1050 | begin 1051 | if LibraryHandle <> 0 then 1052 | begin 1053 | FreeLibrary(LibraryHandle); 1054 | LibraryHandle := 0; 1055 | end; 1056 | end; 1057 | 1058 | 1059 | function TVerySimpleLua.Print(L: Lua_State): Integer; 1060 | var 1061 | N, I: Integer; 1062 | S: MarshaledAString; 1063 | Sz: size_t; 1064 | Msg: String; 1065 | begin 1066 | Msg := ''; 1067 | 1068 | N := lua_gettop(L); //* number of arguments */ 1069 | lua_getglobal(L, 'tostring'); 1070 | for I := 1 to N do 1071 | begin 1072 | lua_pushvalue(L, -1); //* function to be called */ 1073 | lua_pushvalue(L, i); //* value to print */ 1074 | lua_call(L, 1, 1); 1075 | S := lua_tolstring(L, -1, @Sz); //* get result */ 1076 | if S = NIL then 1077 | begin 1078 | Result := luaL_error(L, '"tostring" must return a string to "print"',[]); 1079 | Exit; 1080 | end; 1081 | 1082 | if I > 1 then 1083 | Msg := Msg + #9; 1084 | Msg := Msg + String(S); 1085 | lua_pop(L, 1); //* pop result */ 1086 | end; 1087 | Result := 0; 1088 | 1089 | DoPrint(Msg); 1090 | end; 1091 | 1092 | 1093 | 1094 | { TLuaChunkStream } 1095 | 1096 | constructor TLuaChunkStream.Create; 1097 | begin 1098 | Chunk := TMemoryStream.Create; 1099 | end; 1100 | 1101 | destructor TLuaChunkStream.Destroy; 1102 | begin 1103 | Chunk.Free; 1104 | inherited; 1105 | end; 1106 | 1107 | function TLuaChunkStream.Read(sz: Psize_t): Pointer; 1108 | var 1109 | LocalChunkSize: Int64; 1110 | begin 1111 | Chunk.Clear; 1112 | 1113 | if Size >= ChunkSize then 1114 | LocalChunkSize := ChunkSize 1115 | else 1116 | LocalChunkSize := Size; 1117 | 1118 | if LocalChunkSize > 0 then 1119 | begin 1120 | Chunk.CopyFrom(Stream, LocalChunkSize); 1121 | Size := Size - LocalChunkSize; 1122 | end; 1123 | 1124 | sz^ := LocalChunkSize; 1125 | Result := Chunk.Memory; 1126 | end; 1127 | 1128 | initialization 1129 | LibraryHandle := 0; 1130 | 1131 | finalization 1132 | if LibraryHandle <> 0 then 1133 | TVerySimpleLua.FreeLuaLibrary; 1134 | end. 1135 | -------------------------------------------------------------------------------- /Source/VerySimple.Lua.Lib.pas: -------------------------------------------------------------------------------- 1 | {* 2 | ** $Id: lua.h,v 1.325 2014/12/26 17:24:27 roberto Exp $ 3 | ** Lua - A Scripting Language 4 | ** Lua.org, PUC-Rio, Brazil (http://www.lua.org) 5 | ** See Copyright Notice at the end of this file 6 | *} 7 | 8 | {* 9 | ** Translated to Delphi by Dennis D. Spreen 10 | ** Notes: 11 | as lua_State is a not defined structure, it is not used as Plua_State 12 | LUA_VERSION suffixed by '_' for avoiding name collision 13 | lua_xmove parameter 'to' suffixed by '_' for avoiding name collision 14 | Comparison and arithmetic functions consts moved to constants 15 | garbage-collection function and options consts moved to constants 16 | compatibility macros for unsigned conversions not translated 17 | event codes consts moved to constants 18 | LUA_YIELD thread status suffixed by '_' for avoiding name collision 19 | *} 20 | 21 | unit VerySimple.Lua.Lib; 22 | 23 | interface 24 | 25 | // *********************************** 26 | // Microsoft Windows declarations 27 | // *********************************** 28 | {$IF defined(MSWINDOWS)} 29 | uses 30 | System.Classes, Winapi.Windows, System.SysUtils, System.IOUtils; 31 | 32 | const 33 | LUA_LIBRARY = 'lua5.3.0.dll'; {Do not Localize} 34 | 35 | // *********************************** 36 | // MacOs & iOS declarations 37 | // *********************************** 38 | {$ELSEIF defined(MACOS)} 39 | uses 40 | System.Classes, System.IOUtils, System.SysUtils, Posix.SysTypes; 41 | 42 | const 43 | {$IFDEF IOS} 44 | {$DEFINE STATICLIBRARY} 45 | {$IFDEF CPUARM} // iOS device 46 | LUA_LIBRARY = 'liblua.a'; {Do not Localize} 47 | {$ELSE} // iOS Simulator 48 | LUA_LIBRARY = 'liblua_sim.a'; {Do not Localize} 49 | {$ENDIF} 50 | {$ELSE} // MacOS 51 | LUA_LIBRARY = 'liblua5.3.0.dylib'; {Do not Localize} 52 | {$ENDIF} 53 | 54 | // *********************************** 55 | // Android declarations 56 | // *********************************** 57 | {$ELSEIF defined(ANDROID)} 58 | uses 59 | System.Classes, System.IOUtils, System.SysUtils, Posix.SysTypes; 60 | 61 | const 62 | LUA_LIBRARY = 'liblua.so'; {Do not Localize} 63 | {$ENDIF} 64 | 65 | {* 66 | ** luaconf.h 67 | *} 68 | LUA_IDSIZE = 60; 69 | LUAI_FIRSTPSEUDOIDX = -1001000; 70 | 71 | {* 72 | ** lua.h 73 | *} 74 | LUA_VERSION_MAJOR = '5'; 75 | LUA_VERSION_MINOR = '3'; 76 | LUA_VERSION_NUM = 503; 77 | LUA_VERSION_RELEASE = '0'; 78 | 79 | LUA_VERSION_ = 'Lua ' + LUA_VERSION_MAJOR + '.' + LUA_VERSION_MINOR; 80 | LUA_RELEASE = LUA_VERSION_ + '.' + LUA_VERSION_RELEASE; 81 | LUA_COPYRIGHT = LUA_RELEASE + ' Copyright (C) 1994-2015 Lua.org, PUC-Rio'; 82 | LUA_AUTHORS = 'R. Ierusalimschy, L. H. de Figueiredo, W. Celes'; 83 | 84 | 85 | {* mark for precompiled code ('Lua') *} 86 | LUA_SIGNATURE = #$1b'Lua'; 87 | 88 | {* option for multiple returns in 'lua_pcall' and 'lua_call' *} 89 | LUA_MULTRET = -1; 90 | 91 | 92 | {* 93 | ** pseudo-indices 94 | *} 95 | LUA_REGISTRYINDEX = LUAI_FIRSTPSEUDOIDX; 96 | 97 | {* thread status *} 98 | LUA_OK = 0; 99 | LUA_YIELD_ = 1; 100 | LUA_ERRRUN = 2; 101 | LUA_ERRSYNTAX = 3; 102 | LUA_ERRMEM = 4; 103 | LUA_ERRGCMM = 5; 104 | LUA_ERRERR = 6; 105 | 106 | {* 107 | ** basic types 108 | *} 109 | LUA_TNONE = (-1); 110 | 111 | LUA_TNIL = 0; 112 | LUA_TBOOLEAN = 1; 113 | LUA_TLIGHTUSERDATA = 2; 114 | LUA_TNUMBER = 3; 115 | LUA_TSTRING = 4; 116 | LUA_TTABLE = 5; 117 | LUA_TFUNCTION = 6; 118 | LUA_TUSERDATA = 7; 119 | LUA_TTHREAD = 8; 120 | 121 | LUA_NUMTAGS = 9; 122 | 123 | 124 | {* minimum Lua stack available to a C function *} 125 | LUA_MINSTACK = 20; 126 | 127 | 128 | {* predefined values in the registry *} 129 | LUA_RIDX_MAINTHREAD = 1; 130 | LUA_RIDX_GLOBALS = 2; 131 | LUA_RIDX_LAST = LUA_RIDX_GLOBALS; 132 | 133 | {* 134 | ** Comparison and arithmetic functions 135 | *} 136 | 137 | LUA_OPADD = 0; {* ORDER TM, ORDER OP *} 138 | LUA_OPSUB = 1; 139 | LUA_OPMUL = 2; 140 | LUA_OPMOD = 3; 141 | LUA_OPPOW = 4; 142 | LUA_OPDIV = 5; 143 | LUA_OPIDIV = 6; 144 | LUA_OPBAND = 7; 145 | LUA_OPBOR = 8; 146 | LUA_OPBXOR = 9; 147 | LUA_OPSHL = 10; 148 | LUA_OPSHR = 11; 149 | LUA_OPUNM = 12; 150 | LUA_OPBNOT = 13; 151 | 152 | LUA_OPEQ = 0; 153 | LUA_OPLT = 1; 154 | LUA_OPLE = 2; 155 | 156 | {* 157 | ** garbage-collection function and options 158 | *} 159 | 160 | LUA_GCSTOP = 0; 161 | LUA_GCRESTART = 1; 162 | LUA_GCCOLLECT = 2; 163 | LUA_GCCOUNT = 3; 164 | LUA_GCCOUNTB = 4; 165 | LUA_GCSTEP = 5; 166 | LUA_GCSETPAUSE = 6; 167 | LUA_GCSETSTEPMUL = 7; 168 | LUA_GCISRUNNING = 9; 169 | 170 | {* 171 | ** Event codes 172 | *} 173 | LUA_HOOKCALL = 0; 174 | LUA_HOOKRET = 1; 175 | LUA_HOOKLINE = 2; 176 | LUA_HOOKCOUNT = 3; 177 | LUA_HOOKTAILCALL = 4; 178 | 179 | 180 | {* 181 | ** Event masks 182 | *} 183 | LUA_MASKCALL = (1 SHL LUA_HOOKCALL); 184 | LUA_MASKRET = (1 SHL LUA_HOOKRET); 185 | LUA_MASKLINE = (1 SHL LUA_HOOKLINE); 186 | LUA_MASKCOUNT = (1 SHL LUA_HOOKCOUNT); 187 | 188 | {* 189 | ** lualib.h 190 | *} 191 | LUA_COLIBNAME = 'coroutine'; 192 | LUA_TABLIBNAME = 'table'; 193 | LUA_IOLIBNAME = 'io'; 194 | LUA_OSLIBNAME = 'os'; 195 | LUA_STRLIBNAME = 'string'; 196 | LUA_UTF8LIBNAME = 'utf8'; 197 | LUA_BITLIBNAME = 'bit32'; 198 | LUA_MATHLIBNAME = 'math'; 199 | LUA_DBLIBNAME = 'debug'; 200 | LUA_LOADLIBNAME = 'package'; 201 | 202 | {* 203 | ** lauxlib.h 204 | *} 205 | 206 | LUAL_NUMSIZES = sizeof(NativeInt)*16 + sizeof(Double); 207 | 208 | {* pre-defined references *} 209 | LUA_NOREF = -2; 210 | LUA_REFNIL = -1; 211 | 212 | {* 213 | @@ LUAL_BUFFERSIZE is the buffer size used by the lauxlib buffer system. 214 | ** CHANGE it if it uses too much C-stack space. 215 | *} 216 | LUAL_BUFFERSIZE = Integer($80 * sizeof(Pointer) * sizeof(NativeInt)); 217 | 218 | {* 219 | ** A file handle is a userdata with metatable 'LUA_FILEHANDLE' and 220 | ** initial structure 'luaL_Stream' (it may contain other fields 221 | ** after that initial structure). 222 | *} 223 | 224 | LUA_FILEHANDLE = 'FILE*'; 225 | 226 | type 227 | lua_State = Pointer; 228 | ptrdiff_t = NativeInt; 229 | 230 | {* type of numbers in Lua *} 231 | lua_Number = Double; 232 | 233 | {* type for integer functions *} 234 | lua_Integer = Int64;//NativeInt; 235 | 236 | {* unsigned integer type *} 237 | lua_Unsigned = Uint64; //NativeUInt; 238 | 239 | {* type for continuation-function contexts *} 240 | lua_KContext = ptrdiff_t; 241 | 242 | 243 | {* 244 | ** Type for C functions registered with Lua 245 | *} 246 | lua_CFunction = function(L: lua_State): Integer; cdecl; 247 | 248 | {* 249 | ** Type for continuation functions 250 | *} 251 | lua_KFunction = function(L: lua_State; status: Integer; ctx: lua_KContext): Integer; cdecl; 252 | 253 | 254 | {* 255 | ** Type for functions that read/write blocks when loading/dumping Lua chunks 256 | *} 257 | lua_Reader = function(L: lua_State; ud: Pointer; sz: Psize_t): Pointer; cdecl; 258 | lua_Writer = function(L: lua_State; p: Pointer; sz: size_t; ud: Pointer): Integer; cdecl; 259 | 260 | {* 261 | ** Type for memory-allocation functions 262 | *} 263 | lua_Alloc = function(ud: Pointer; ptr: Pointer; osize: size_t; nsize: size_t): Pointer; cdecl; 264 | 265 | 266 | {* 267 | ** generic extra include file 268 | *} 269 | {#if defined(LUA_USER_H) 270 | #include LUA_USER_H 271 | #endif} 272 | 273 | 274 | {* 275 | ** RCS ident string 276 | *} 277 | //extern const char lua_ident[]; 278 | 279 | lua_Debug = record (* activation record *) 280 | event: Integer; 281 | name: MarshaledAString; (* (n) *) 282 | namewhat: MarshaledAString; (* (n) `global', `local', `field', `method' *) 283 | what: MarshaledAString; (* (S) `Lua', `C', `main', `tail'*) 284 | source: MarshaledAString; (* (S) *) 285 | currentline: Integer; (* (l) *) 286 | linedefined: Integer; (* (S) *) 287 | lastlinedefined: Integer; (* (S) *) 288 | nups: Byte; (* (u) number of upvalues *) 289 | nparams: Byte; (* (u) number of parameters *) 290 | isvararg: ByteBool; (* (u) *) 291 | istailcall: ByteBool; (* (t) *) 292 | short_src: array[0..LUA_IDSIZE - 1] of Char; (* (S) *) 293 | (* private part *) 294 | i_ci: Pointer; (* active function *) // ptr to struct CallInfo 295 | end; 296 | Plua_Debug = ^lua_Debug; 297 | 298 | 299 | {* Functions to be called by the debugger in specific events *} 300 | lua_Hook = procedure(L: lua_State; ar: Plua_Debug); cdecl; 301 | 302 | luaL_Reg = record 303 | name: MarshaledAString; 304 | func: lua_CFunction; 305 | end; 306 | PluaL_Reg = ^luaL_Reg; 307 | 308 | {* 309 | ** Generic Buffer manipulation 310 | *} 311 | 312 | luaL_Buffer = record 313 | b: MarshaledAString; {* buffer address *} 314 | size: size_t; {* buffer size *} 315 | n: size_t; {* number of characters in buffer *} 316 | L: lua_State; 317 | initb: array[0..LUAL_BUFFERSIZE - 1] of Byte; {* initial buffer *} 318 | end; 319 | 320 | Plual_Buffer = ^lual_Buffer; 321 | 322 | {* 323 | ** File handles for IO library 324 | *} 325 | luaL_Stream = record 326 | f: Pointer; {* stream (NULL for incompletely created streams) *} 327 | closef: lua_CFunction; {* to close stream (NULL for closed streams) *} 328 | end; 329 | 330 | // Use procedure entries as variables (only if dynamic library) 331 | {$IFNDEF STATICLIBRARY} 332 | var 333 | {$ENDIF} 334 | 335 | {* 336 | ** state manipulation 337 | *} 338 | {$IFDEF STATICLIBRARY} 339 | function lua_newstate(f: lua_Alloc; ud: Pointer): lua_State; cdecl; external LUA_LIBRARY; 340 | procedure lua_close(L: lua_State); cdecl; external LUA_LIBRARY; 341 | function lua_newthread(L: lua_State): lua_State; cdecl; external LUA_LIBRARY; 342 | function lua_atpanic(L: lua_State; panicf: lua_CFunction): lua_CFunction; cdecl; external LUA_LIBRARY; 343 | function lua_version(L: lua_State): lua_Number; cdecl; external LUA_LIBRARY; 344 | {$ELSE} 345 | lua_newstate: function(f: lua_Alloc; ud: Pointer): lua_State; cdecl; 346 | lua_close: procedure (L: lua_State); cdecl; 347 | lua_newthread: function(L: lua_State): lua_State; cdecl; 348 | lua_atpanic: function(L: lua_State; panicf: lua_CFunction): lua_CFunction; cdecl; 349 | lua_version: function(L: lua_State): lua_Number; cdecl; 350 | {$ENDIF} 351 | 352 | 353 | {* 354 | ** basic stack manipulation 355 | *} 356 | {$IFDEF STATICLIBRARY} 357 | function lua_absindex(L: lua_State; idx: Integer): Integer; cdecl; external LUA_LIBRARY; 358 | function lua_gettop(L: lua_State): Integer; cdecl; external LUA_LIBRARY; 359 | procedure lua_settop(L: lua_State; idx: Integer); cdecl; external LUA_LIBRARY; 360 | procedure lua_pushvalue(L: lua_State; idx: Integer); cdecl; external LUA_LIBRARY; 361 | procedure lua_rotate(L: lua_State; idx: Integer; n: Integer); cdecl; external LUA_LIBRARY; 362 | procedure lua_copy(L: lua_State; fromidx: Integer; toidx: Integer); cdecl; external LUA_LIBRARY; 363 | function lua_checkstack(L: lua_State; n: Integer): Integer; cdecl; external LUA_LIBRARY; 364 | procedure lua_xmove(from: lua_State; to_: lua_State; n: Integer); cdecl; external LUA_LIBRARY; 365 | {$ELSE} 366 | lua_absindex: function(L: lua_State; idx: Integer): Integer; cdecl; 367 | lua_gettop: function(L: lua_State): Integer; cdecl; 368 | lua_settop: procedure(L: lua_State; idx: Integer); cdecl; 369 | lua_pushvalue: procedure(L: lua_State; idx: Integer); cdecl; 370 | lua_rotate: procedure(L: lua_State; idx: Integer; n: Integer); cdecl; 371 | lua_copy: procedure(L: lua_State; fromidx: Integer; toidx: Integer); cdecl; 372 | lua_checkstack: function(L: lua_State; n: Integer): Integer; cdecl; 373 | lua_xmove: procedure(from: lua_State; to_: lua_State; n: Integer); cdecl; 374 | {$ENDIF} 375 | 376 | 377 | {* 378 | ** access functions (stack -> C) 379 | *} 380 | {$IFDEF STATICLIBRARY} 381 | function lua_isnumber(L: lua_State; idx: Integer): Integer; cdecl; external LUA_LIBRARY; 382 | function lua_isstring(L: lua_State; idx: Integer): Integer; cdecl; external LUA_LIBRARY; 383 | function lua_iscfunction(L: lua_State; idx: Integer): Integer; cdecl; external LUA_LIBRARY; 384 | function lua_isinteger(L: lua_State; idx: Integer): Integer; cdecl; external LUA_LIBRARY; 385 | function lua_isuserdata(L: lua_State; idx: Integer): Integer; cdecl; external LUA_LIBRARY; 386 | function lua_type(L: lua_State; idx: Integer): Integer; cdecl; external LUA_LIBRARY; 387 | function lua_typename(L: lua_State; tp: Integer): MarshaledAString; cdecl; external LUA_LIBRARY; 388 | 389 | function lua_tonumberx(L: lua_State; idx: Integer; isnum: PLongBool): lua_Number; cdecl; external LUA_LIBRARY; 390 | function lua_tointegerx(L: lua_State; idx: Integer; isnum: PLongBool): lua_Integer; cdecl; external LUA_LIBRARY; 391 | function lua_toboolean(L: lua_State; idx: Integer): Integer; cdecl; external LUA_LIBRARY; 392 | function lua_tolstring(L: lua_State; idx: Integer; len: Psize_t): MarshaledAString; cdecl; external LUA_LIBRARY; 393 | function lua_rawlen(L: lua_State; idx: Integer): size_t; cdecl; external LUA_LIBRARY; 394 | function lua_tocfunction(L: lua_State; idx: Integer): lua_CFunction; cdecl; external LUA_LIBRARY; 395 | function lua_touserdata(L: lua_State; idx: Integer): Pointer; cdecl; external LUA_LIBRARY; 396 | function lua_tothread(L: lua_State; idx: Integer): lua_State; cdecl; external LUA_LIBRARY; 397 | function lua_topointer(L: lua_State; idx: Integer): Pointer; cdecl; external LUA_LIBRARY; 398 | {$ELSE} 399 | lua_isnumber: function(L: lua_State; idx: Integer): Integer; cdecl; 400 | lua_isstring: function(L: lua_State; idx: Integer): Integer; cdecl; 401 | lua_iscfunction: function(L: lua_State; idx: Integer): Integer; cdecl; 402 | lua_isinteger: function(L: lua_State; idx: Integer): Integer; cdecl; 403 | lua_isuserdata: function(L: lua_State; idx: Integer): Integer; cdecl; 404 | lua_type: function(L: lua_State; idx: Integer): Integer; cdecl; 405 | lua_typename: function(L: lua_State; tp: Integer): MarshaledAString; cdecl; 406 | 407 | lua_tonumberx: function(L: lua_State; idx: Integer; isnum: PLongBool): lua_Number; cdecl; 408 | lua_tointegerx: function(L: lua_State; idx: Integer; isnum: PLongBool): lua_Integer; cdecl; 409 | lua_toboolean: function(L: lua_State; idx: Integer): Integer; cdecl; 410 | lua_tolstring: function(L: lua_State; idx: Integer; len: Psize_t): MarshaledAString; cdecl; 411 | lua_rawlen: function(L: lua_State; idx: Integer): size_t; cdecl; 412 | lua_tocfunction: function(L: lua_State; idx: Integer): lua_CFunction; cdecl; 413 | lua_touserdata: function(L: lua_State; idx: Integer): Pointer; cdecl; 414 | lua_tothread: function(L: lua_State; idx: Integer): lua_State; cdecl; 415 | lua_topointer: function(L: lua_State; idx: Integer): Pointer; cdecl; 416 | {$ENDIF} 417 | 418 | {* 419 | ** Comparison and arithmetic functions 420 | *} 421 | {$IFDEF STATICLIBRARY} 422 | procedure lua_arith(L: lua_State; op: Integer); cdecl; external LUA_LIBRARY; 423 | function lua_rawequal(L: lua_State; idx1: Integer; idx2: Integer): Integer; cdecl; external LUA_LIBRARY; 424 | function lua_compare(L: lua_State; idx1: Integer; idx2: Integer; op: Integer): Integer; cdecl; external LUA_LIBRARY; 425 | {$ELSE} 426 | lua_arith: procedure(L: lua_State; op: Integer); cdecl; 427 | lua_rawequal: function(L: lua_State; idx1: Integer; idx2: Integer): Integer; cdecl; 428 | lua_compare: function(L: lua_State; idx1: Integer; idx2: Integer; op: Integer): Integer; cdecl; 429 | {$ENDIF} 430 | 431 | 432 | {* 433 | ** push functions (C -> stack) 434 | *} 435 | {$IFDEF STATICLIBRARY} 436 | procedure lua_pushnil(L: lua_State); cdecl; external LUA_LIBRARY; 437 | procedure lua_pushnumber(L: lua_State; n: lua_Number); cdecl; external LUA_LIBRARY; 438 | procedure lua_pushinteger(L: lua_State; n: lua_Integer); cdecl; external LUA_LIBRARY; 439 | function lua_pushlstring(L: lua_State; s: MarshaledAString; len: size_t): MarshaledAString; cdecl; external LUA_LIBRARY; 440 | function lua_pushstring(L: lua_State; s: MarshaledAString): MarshaledAString; cdecl; external LUA_LIBRARY; 441 | function lua_pushvfstring(L: lua_State; fmt: MarshaledAString; argp: Pointer): MarshaledAString; cdecl; external LUA_LIBRARY; 442 | function lua_pushfstring(L: lua_State; fmt: MarshaledAString; args: array of const): MarshaledAString; cdecl; external LUA_LIBRARY; 443 | procedure lua_pushcclosure(L: lua_State; fn: lua_CFunction; n: Integer); cdecl; external LUA_LIBRARY; 444 | procedure lua_pushboolean(L: lua_State; b: Integer); cdecl; external LUA_LIBRARY; 445 | procedure lua_pushlightuserdata(L: lua_State; p: Pointer); cdecl; external LUA_LIBRARY; 446 | function lua_pushthread(L: lua_State): Integer; cdecl; external LUA_LIBRARY; 447 | {$ELSE} 448 | lua_pushnil: procedure(L: lua_State); cdecl; 449 | lua_pushnumber: procedure(L: lua_State; n: lua_Number); cdecl; 450 | lua_pushinteger: procedure(L: lua_State; n: lua_Integer); cdecl; 451 | lua_pushlstring: function(L: lua_State; s: MarshaledAString; len: size_t): MarshaledAString; cdecl; 452 | lua_pushstring: function(L: lua_State; s: MarshaledAString): MarshaledAString; cdecl; 453 | lua_pushvfstring: function(L: lua_State; fmt: MarshaledAString; argp: Pointer): MarshaledAString; cdecl; 454 | lua_pushfstring: function(L: lua_State; fmt: MarshaledAString; args: array of const): MarshaledAString; cdecl; 455 | lua_pushcclosure: procedure(L: lua_State; fn: lua_CFunction; n: Integer); cdecl; 456 | lua_pushboolean: procedure(L: lua_State; b: Integer); cdecl; 457 | lua_pushlightuserdata: procedure(L: lua_State; p: Pointer); cdecl; 458 | lua_pushthread: function(L: lua_State): Integer; cdecl; 459 | {$ENDIF} 460 | 461 | 462 | {* 463 | ** get functions (Lua -> stack) 464 | *} 465 | {$IFDEF STATICLIBRARY} 466 | function lua_getglobal(L: lua_State; const name: MarshaledAString): Integer; cdecl; external LUA_LIBRARY; 467 | function lua_gettable(L: lua_State; idx: Integer): Integer; cdecl; external LUA_LIBRARY; 468 | function lua_getfield(L: lua_State; idx: Integer; k: MarshaledAString): Integer; cdecl; external LUA_LIBRARY; 469 | function lua_geti(L: lua_State; idx: Integer; n: lua_Integer): Integer; cdecl; external LUA_LIBRARY; 470 | function lua_rawget(L: lua_State; idx: Integer): Integer; cdecl; external LUA_LIBRARY; 471 | function lua_rawgeti(L: lua_State; idx: Integer; n: lua_Integer): Integer; cdecl; external LUA_LIBRARY; 472 | function lua_rawgetp(L: lua_State; idx: Integer; p: Pointer): Integer; cdecl; external LUA_LIBRARY; 473 | 474 | procedure lua_createtable(L: lua_State; narr: Integer; nrec: Integer); cdecl; external LUA_LIBRARY; 475 | function lua_newuserdata(L: lua_State; sz: size_t): Pointer; cdecl; external LUA_LIBRARY; 476 | function lua_getmetatable(L: lua_State; objindex: Integer): Integer; cdecl; external LUA_LIBRARY; 477 | function lua_getuservalue(L: lua_State; idx: Integer): Integer; cdecl; external LUA_LIBRARY; 478 | {$ELSE} 479 | lua_getglobal: function(L: lua_State; const name: MarshaledAString): Integer; cdecl; 480 | lua_gettable: function(L: lua_State; idx: Integer): Integer; cdecl; 481 | lua_getfield: function(L: lua_State; idx: Integer; k: MarshaledAString): Integer; cdecl; 482 | lua_geti: function(L: lua_State; idx: Integer; n: lua_Integer): Integer; cdecl; 483 | lua_rawget: function(L: lua_State; idx: Integer): Integer; cdecl; 484 | lua_rawgeti: function(L: lua_State; idx: Integer; n: lua_Integer): Integer; cdecl; 485 | lua_rawgetp: function(L: lua_State; idx: Integer; p: Pointer): Integer; cdecl; 486 | 487 | lua_createtable: procedure(L: lua_State; narr: Integer; nrec: Integer); cdecl; 488 | lua_newuserdata: function(L: lua_State; sz: size_t): Pointer; cdecl; 489 | lua_getmetatable: function(L: lua_State; objindex: Integer): Integer; cdecl; 490 | lua_getuservalue: function(L: lua_State; idx: Integer): Integer; cdecl; 491 | {$ENDIF} 492 | 493 | 494 | {* 495 | ** set functions (stack -> Lua) 496 | *} 497 | {$IFDEF STATICLIBRARY} 498 | procedure lua_setglobal(L: lua_State; name: MarshaledAString); cdecl; external LUA_LIBRARY; 499 | procedure lua_settable(L: lua_State; idx: Integer); cdecl; external LUA_LIBRARY; 500 | procedure lua_setfield(L: lua_State; idx: Integer; k: MarshaledAString); cdecl; external LUA_LIBRARY; 501 | procedure lua_seti(L: lua_State; idx: Integer; n: lua_Integer); cdecl; external LUA_LIBRARY; 502 | procedure lua_rawset(L: lua_State; idx: Integer); cdecl; external LUA_LIBRARY; 503 | procedure lua_rawseti(L: lua_State; idx: Integer; n: lua_Integer); cdecl; external LUA_LIBRARY; 504 | procedure lua_rawsetp(L: lua_State; idx: Integer; p: Pointer); cdecl; external LUA_LIBRARY; 505 | function lua_setmetatable(L: lua_State; objindex: Integer): Integer; cdecl; external LUA_LIBRARY; 506 | procedure lua_setuservalue(L: lua_State; idx: Integer); cdecl; external LUA_LIBRARY; 507 | {$ELSE} 508 | lua_setglobal: procedure(L: lua_State; name: MarshaledAString); cdecl; 509 | lua_settable: procedure(L: lua_State; idx: Integer); cdecl; 510 | lua_setfield: procedure(L: lua_State; idx: Integer; k: MarshaledAString); cdecl; 511 | lua_seti: procedure(L: lua_State; idx: Integer; n: lua_Integer); cdecl; 512 | lua_rawset: procedure(L: lua_State; idx: Integer); cdecl; 513 | lua_rawseti: procedure(L: lua_State; idx: Integer; n: lua_Integer); cdecl; 514 | lua_rawsetp: procedure(L: lua_State; idx: Integer; p: Pointer); cdecl; 515 | lua_setmetatable: function(L: lua_State; objindex: Integer): Integer; cdecl; 516 | lua_setuservalue: procedure(L: lua_State; idx: Integer); cdecl; 517 | {$ENDIF} 518 | 519 | 520 | {* 521 | ** 'load' and 'call' functions (load and run Lua code) 522 | *} 523 | {$IFDEF STATICLIBRARY} 524 | procedure lua_callk(L: lua_State; nargs: Integer; nresults: Integer; ctx: lua_KContext; k: lua_KFunction); cdecl; external LUA_LIBRARY; 525 | 526 | function lua_pcallk(L: lua_State; nargs: Integer; nresults: Integer; errfunc: Integer; 527 | ctx: lua_KContext; k: lua_KFunction): Integer; cdecl; external LUA_LIBRARY; 528 | 529 | function lua_load(L: lua_State; reader: lua_Reader; dt: Pointer; const chunkname: MarshaledAString; 530 | const mode: MarshaledAString): Integer; cdecl; external LUA_LIBRARY; 531 | 532 | function lua_dump(L: lua_State; writer: lua_Writer; data: Pointer; strip: Integer): Integer; cdecl; external LUA_LIBRARY; 533 | {$ELSE} 534 | lua_callk: procedure(L: lua_State; nargs: Integer; nresults: Integer; ctx: lua_KContext; k: lua_KFunction); cdecl; 535 | 536 | lua_pcallk: function(L: lua_State; nargs: Integer; nresults: Integer; errfunc: Integer; 537 | ctx: lua_KContext; k: lua_KFunction): Integer; cdecl; 538 | 539 | lua_load: function(L: lua_State; reader: lua_Reader; dt: Pointer; const chunkname: MarshaledAString; 540 | const mode: MarshaledAString): Integer; cdecl; 541 | 542 | lua_dump: function(L: lua_State; writer: lua_Writer; data: Pointer; strip: Integer): Integer; cdecl; 543 | {$ENDIF} 544 | 545 | 546 | {* 547 | ** coroutine functions 548 | *} 549 | {$IFDEF STATICLIBRARY} 550 | function lua_yieldk(L: lua_State; nresults: Integer; ctx: lua_KContext; k: lua_KFunction): Integer; cdecl; external LUA_LIBRARY; 551 | function lua_resume(L: lua_State; from: lua_State; narg: Integer): Integer; cdecl; external LUA_LIBRARY; 552 | function lua_status(L: lua_State): Integer; cdecl; external LUA_LIBRARY; 553 | function lua_isyieldable(L: lua_State): Integer; cdecl; external LUA_LIBRARY; 554 | {$ELSE} 555 | lua_yieldk: function(L: lua_State; nresults: Integer; ctx: lua_KContext; k: lua_KFunction): Integer; cdecl; 556 | lua_resume: function(L: lua_State; from: lua_State; narg: Integer): Integer; cdecl; 557 | lua_status: function(L: lua_State): Integer; cdecl; 558 | lua_isyieldable: function(L: lua_State): Integer; cdecl; 559 | {$ENDIF} 560 | 561 | {* 562 | ** garbage-collection function and options 563 | *} 564 | {$IFDEF STATICLIBRARY} 565 | function lua_gc(L: lua_State; what: Integer; data: Integer): Integer; cdecl; external LUA_LIBRARY; 566 | {$ELSE} 567 | lua_gc: function(L: lua_State; what: Integer; data: Integer): Integer; cdecl; 568 | {$ENDIF} 569 | 570 | 571 | {* 572 | ** miscellaneous functions 573 | *} 574 | {$IFDEF STATICLIBRARY} 575 | function lua_error(L: lua_State): Integer; cdecl; external LUA_LIBRARY; 576 | function lua_next(L: lua_State; idx: Integer): Integer; cdecl; external LUA_LIBRARY; 577 | 578 | procedure lua_concat(L: lua_State; n: Integer); cdecl; external LUA_LIBRARY; 579 | procedure lua_len(L: lua_State; idx: Integer); cdecl; external LUA_LIBRARY; 580 | 581 | function lua_stringtonumber(L: lua_State; const s: MarshaledAString): size_t; cdecl; external LUA_LIBRARY; 582 | 583 | function lua_getallocf(L: lua_State; ud: PPointer): lua_Alloc; cdecl; external LUA_LIBRARY; 584 | procedure lua_setallocf(L: lua_State; f: lua_Alloc; ud: Pointer); cdecl; external LUA_LIBRARY; 585 | {$ELSE} 586 | lua_error: function(L: lua_State): Integer; cdecl; 587 | lua_next: function(L: lua_State; idx: Integer): Integer; cdecl; 588 | 589 | lua_concat: procedure(L: lua_State; n: Integer); cdecl; 590 | lua_len: procedure(L: lua_State; idx: Integer); cdecl; 591 | 592 | lua_stringtonumber: function(L: lua_State; const s: MarshaledAString): size_t; cdecl; 593 | 594 | lua_getallocf: function(L: lua_State; ud: PPointer): lua_Alloc; cdecl; 595 | lua_setallocf: procedure(L: lua_State; f: lua_Alloc; ud: Pointer); cdecl; 596 | {$ENDIF} 597 | 598 | 599 | {* 600 | ** ====================================================================== 601 | ** Debug API 602 | ** ====================================================================== 603 | *} 604 | {$IFDEF STATICLIBRARY} 605 | function lua_getstack(L: lua_State; level: Integer; ar: Plua_Debug): Integer; cdecl; external LUA_LIBRARY; 606 | function lua_getinfo(L: lua_State; const what: MarshaledAString; ar: Plua_Debug): Integer; cdecl; external LUA_LIBRARY; 607 | function lua_getlocal(L: lua_State; const ar: Plua_Debug; n: Integer): MarshaledAString; cdecl; external LUA_LIBRARY; 608 | function lua_setlocal(L: lua_State; const ar: Plua_Debug; n: Integer): MarshaledAString; cdecl; external LUA_LIBRARY; 609 | function lua_getupvalue(L: lua_State; funcindex, n: Integer): MarshaledAString; cdecl; external LUA_LIBRARY; 610 | function lua_setupvalue(L: lua_State; funcindex, n: Integer): MarshaledAString; cdecl; external LUA_LIBRARY; 611 | function lua_upvalueid(L: lua_State; fidx, n: Integer): Pointer; cdecl; external LUA_LIBRARY; 612 | procedure lua_upvaluejoin(L: lua_State; fix1, n1, fidx2, n2: Integer); cdecl; external LUA_LIBRARY; 613 | procedure lua_sethook(L: lua_State; func: lua_Hook; mask: Integer; count: Integer); cdecl; external LUA_LIBRARY; 614 | function lua_gethook(L: lua_State): lua_Hook; cdecl; external LUA_LIBRARY; 615 | function lua_gethookmask(L: lua_State): Integer; cdecl; external LUA_LIBRARY; 616 | function lua_gethookcount(L: lua_State): Integer; cdecl; external LUA_LIBRARY; 617 | {$ELSE} 618 | lua_getstack: function(L: lua_State; level: Integer; ar: Plua_Debug): Integer; cdecl; 619 | lua_getinfo: function(L: lua_State; const what: MarshaledAString; ar: Plua_Debug): Integer; cdecl; 620 | lua_getlocal: function(L: lua_State; const ar: Plua_Debug; n: Integer): MarshaledAString; cdecl; 621 | lua_setlocal: function(L: lua_State; const ar: Plua_Debug; n: Integer): MarshaledAString; cdecl; 622 | lua_getupvalue: function(L: lua_State; funcindex, n: Integer): MarshaledAString; cdecl; 623 | lua_setupvalue: function(L: lua_State; funcindex, n: Integer): MarshaledAString; cdecl; 624 | lua_upvalueid: function(L: lua_State; fidx, n: Integer): Pointer; cdecl; 625 | lua_upvaluejoin: procedure(L: lua_State; fix1, n1, fidx2, n2: Integer); cdecl; 626 | lua_sethook: procedure(L: lua_State; func: lua_Hook; mask: Integer; count: Integer); cdecl; 627 | lua_gethook: function(L: lua_State): lua_Hook; cdecl; 628 | lua_gethookmask: function(L: lua_State): Integer; cdecl; 629 | lua_gethookcount: function(L: lua_State): Integer; cdecl; 630 | {$ENDIF} 631 | 632 | 633 | 634 | {* 635 | ** ====================================================================== 636 | ** lualib.h 637 | ** ====================================================================== 638 | *} 639 | {$IFDEF STATICLIBRARY} 640 | function luaopen_base(L: lua_State): Integer; cdecl; external LUA_LIBRARY; 641 | function luaopen_coroutine(L: lua_State): Integer; cdecl; external LUA_LIBRARY; 642 | function luaopen_table(L: lua_State): Integer; cdecl; external LUA_LIBRARY; 643 | function luaopen_io(L: lua_State): Integer; cdecl; external LUA_LIBRARY; 644 | function luaopen_os(L: lua_State): Integer; cdecl; external LUA_LIBRARY; 645 | function luaopen_string(L: lua_State): Integer; cdecl; external LUA_LIBRARY; 646 | function luaopen_utf8(L: lua_State): Integer; cdecl; external LUA_LIBRARY; 647 | function luaopen_bit32(L: lua_State): Integer; cdecl; external LUA_LIBRARY; 648 | function luaopen_math(L: lua_State): Integer; cdecl; external LUA_LIBRARY; 649 | function luaopen_debug(L: lua_State): Integer; cdecl; external LUA_LIBRARY; 650 | function luaopen_package(L: lua_State): Integer; cdecl; external LUA_LIBRARY; 651 | 652 | {* open all previous libraries *} 653 | procedure luaL_openlibs(L: lua_State); cdecl; external LUA_LIBRARY; 654 | {$ELSE} 655 | luaopen_base: function(L: lua_State): Integer; cdecl; 656 | luaopen_coroutine: function(L: lua_State): Integer; cdecl; 657 | luaopen_table: function(L: lua_State): Integer; cdecl; 658 | luaopen_io: function(L: lua_State): Integer; cdecl; 659 | luaopen_os: function(L: lua_State): Integer; cdecl; 660 | luaopen_string: function(L: lua_State): Integer; cdecl; 661 | luaopen_utf8: function(L: lua_State): Integer; cdecl; 662 | luaopen_bit32: function(L: lua_State): Integer; cdecl; 663 | luaopen_math: function(L: lua_State): Integer; cdecl; 664 | luaopen_debug: function(L: lua_State): Integer; cdecl; 665 | luaopen_package: function(L: lua_State): Integer; cdecl; 666 | 667 | {* open all previous libraries *} 668 | luaL_openlibs: procedure(L: lua_State); cdecl; 669 | {$ENDIF} 670 | 671 | 672 | {* 673 | ** ====================================================================== 674 | ** lauxlib.h 675 | ** ====================================================================== 676 | *} 677 | {$IFDEF STATICLIBRARY} 678 | procedure luaL_checkversion_(L: lua_State; ver: lua_Number; sz: size_t); cdecl; external LUA_LIBRARY; 679 | function luaL_getmetafield(L: lua_State; obj: Integer; e: MarshaledAString): Integer; cdecl; external LUA_LIBRARY; 680 | function luaL_callmeta(L: lua_State; obj: Integer; e: MarshaledAString): Integer; cdecl; external LUA_LIBRARY; 681 | function luaL_tolstring(L: lua_State; idx: Integer; len: Psize_t): MarshaledAString; cdecl; external LUA_LIBRARY; 682 | function luaL_argerror(L: lua_State; arg: Integer; extramsg: MarshaledAString): Integer; cdecl; external LUA_LIBRARY; 683 | function luaL_checklstring(L: lua_State; arg: Integer; l_: Psize_t): MarshaledAString; cdecl; external LUA_LIBRARY; 684 | function luaL_optlstring(L: lua_State; arg: Integer; const def: MarshaledAString; l_: Psize_t): MarshaledAString; cdecl; external LUA_LIBRARY; 685 | function luaL_checknumber(L: lua_State; arg: Integer): lua_Number; cdecl; external LUA_LIBRARY; 686 | function luaL_optnumber(L: lua_State; arg: Integer; def: lua_Number): lua_Number; cdecl; external LUA_LIBRARY; 687 | function luaL_checkinteger(L: lua_State; arg: Integer): lua_Integer; cdecl; external LUA_LIBRARY; 688 | function luaL_optinteger(L: lua_State; arg: Integer; def: lua_Integer): lua_Integer; cdecl; external LUA_LIBRARY; 689 | 690 | procedure luaL_checkstack(L: lua_State; sz: Integer; const msg: MarshaledAString); cdecl; external LUA_LIBRARY; 691 | procedure luaL_checktype(L: lua_State; arg: Integer; t: Integer); cdecl; external LUA_LIBRARY; 692 | procedure luaL_checkany(L: lua_State; arg: Integer); cdecl; external LUA_LIBRARY; 693 | 694 | function luaL_newmetatable(L: lua_State; const tname: MarshaledAString): Integer; cdecl; external LUA_LIBRARY; 695 | procedure luaL_setmetatable(L: lua_State; const tname: MarshaledAString); cdecl; external LUA_LIBRARY; 696 | function luaL_testudata(L: lua_State; ud: Integer; const tname: MarshaledAString): Pointer; cdecl; external LUA_LIBRARY; 697 | function luaL_checkudata(L: lua_State; ud: Integer; const tname: MarshaledAString): Pointer; cdecl; external LUA_LIBRARY; 698 | 699 | procedure luaL_where(L: lua_State; lvl: Integer); cdecl; external LUA_LIBRARY; 700 | function luaL_error(L: lua_State; fmt: MarshaledAString; args: array of const): Integer; cdecl; external LUA_LIBRARY; 701 | 702 | function luaL_checkoption(L: lua_State; arg: Integer; const def: MarshaledAString; const lst: PMarshaledAString): Integer; cdecl; external LUA_LIBRARY; 703 | function luaL_fileresult(L: lua_State; stat: Integer; fname: MarshaledAString): Integer; cdecl; external LUA_LIBRARY; 704 | function luaL_execresult(L: lua_State; stat: Integer): Integer; cdecl; external LUA_LIBRARY; 705 | 706 | 707 | function luaL_ref(L: lua_State; t: Integer): Integer; cdecl; external LUA_LIBRARY; 708 | procedure luaL_unref(L: lua_State; t: Integer; ref: Integer); cdecl; external LUA_LIBRARY; 709 | function luaL_loadfilex(L: lua_State; const filename: MarshaledAString; const mode: MarshaledAString): Integer; cdecl; external LUA_LIBRARY; 710 | 711 | function luaL_loadbufferx(L: lua_State; const buff: MarshaledAString; sz: size_t; 712 | const name: MarshaledAString; const mode: MarshaledAString): Integer; cdecl; external LUA_LIBRARY; 713 | function luaL_loadstring(L: lua_State; const s: MarshaledAString): Integer; cdecl; external LUA_LIBRARY; 714 | 715 | function luaL_newstate(): lua_State; cdecl; external LUA_LIBRARY; 716 | function luaL_len(L: lua_State; idx: Integer): lua_Integer; cdecl; external LUA_LIBRARY; 717 | 718 | function luaL_gsub(L: lua_State; const s: MarshaledAString; const p: MarshaledAString; const r: MarshaledAString): MarshaledAString; cdecl; external LUA_LIBRARY; 719 | procedure luaL_setfuncs(L: lua_State; const l_: PluaL_Reg; nup: Integer); cdecl; external LUA_LIBRARY; 720 | 721 | function luaL_getsubtable(L: lua_State; idx: Integer; const fname: MarshaledAString): Integer; cdecl; external LUA_LIBRARY; 722 | 723 | procedure luaL_traceback(L: lua_State; L1: lua_State; const msg: MarshaledAString; level: Integer); cdecl; external LUA_LIBRARY; 724 | 725 | procedure luaL_requiref(L: lua_State; const modname: MarshaledAString; openf: lua_CFunction; glb: Integer); cdecl; external LUA_LIBRARY; 726 | {$ELSE} 727 | luaL_checkversion_: procedure(L: lua_State; ver: lua_Number; sz: size_t); cdecl; 728 | luaL_getmetafield: function(L: lua_State; obj: Integer; e: MarshaledAString): Integer; cdecl; 729 | luaL_callmeta: function(L: lua_State; obj: Integer; e: MarshaledAString): Integer; cdecl; 730 | luaL_tolstring: function(L: lua_State; idx: Integer; len: Psize_t): MarshaledAString; cdecl; 731 | luaL_argerror: function(L: lua_State; arg: Integer; extramsg: MarshaledAString): Integer; cdecl; 732 | luaL_checklstring: function(L: lua_State; arg: Integer; l_: Psize_t): MarshaledAString; cdecl; 733 | luaL_optlstring: function(L: lua_State; arg: Integer; const def: MarshaledAString; l_: Psize_t): MarshaledAString; cdecl; 734 | luaL_checknumber: function(L: lua_State; arg: Integer): lua_Number; cdecl; 735 | luaL_optnumber: function(L: lua_State; arg: Integer; def: lua_Number): lua_Number; cdecl; 736 | luaL_checkinteger: function(L: lua_State; arg: Integer): lua_Integer; cdecl; 737 | luaL_optinteger: function(L: lua_State; arg: Integer; def: lua_Integer): lua_Integer; cdecl; 738 | 739 | luaL_checkstack: procedure(L: lua_State; sz: Integer; const msg: MarshaledAString); cdecl; 740 | luaL_checktype: procedure(L: lua_State; arg: Integer; t: Integer); cdecl; 741 | luaL_checkany: procedure(L: lua_State; arg: Integer); cdecl; 742 | 743 | luaL_newmetatable: function(L: lua_State; const tname: MarshaledAString): Integer; cdecl; 744 | luaL_setmetatable: procedure(L: lua_State; const tname: MarshaledAString); cdecl; 745 | luaL_testudata: procedure(L: lua_State; ud: Integer; const tname: MarshaledAString); cdecl; 746 | luaL_checkudata: function(L: lua_State; ud: Integer; const tname: MarshaledAString): Pointer; cdecl; 747 | 748 | luaL_where: procedure(L: lua_State; lvl: Integer); cdecl; 749 | luaL_error: function(L: lua_State; fmt: MarshaledAString; args: array of const): Integer; cdecl; 750 | 751 | luaL_checkoption: function(L: lua_State; arg: Integer; const def: MarshaledAString; const lst: PMarshaledAString): Integer; cdecl; 752 | luaL_fileresult: function(L: lua_State; stat: Integer; fname: MarshaledAString): Integer; cdecl; 753 | luaL_execresult: function(L: lua_State; stat: Integer): Integer; cdecl; 754 | 755 | 756 | luaL_ref: function(L: lua_State; t: Integer): Integer; cdecl; 757 | luaL_unref: procedure(L: lua_State; t: Integer; ref: Integer); cdecl; 758 | luaL_loadfilex: function(L: lua_State; const filename: MarshaledAString; const mode: MarshaledAString): Integer; cdecl; 759 | 760 | luaL_loadbufferx: function(L: lua_State; const buff: MarshaledAString; sz: size_t; 761 | const name: MarshaledAString; const mode: MarshaledAString): Integer; cdecl; 762 | luaL_loadstring: function(L: lua_State; const s: MarshaledAString): Integer; cdecl; 763 | 764 | luaL_newstate: function(): lua_State; cdecl; 765 | luaL_len: function(L: lua_State; idx: Integer): lua_Integer; cdecl; 766 | 767 | luaL_gsub: function(L: lua_State; const s: MarshaledAString; const p: MarshaledAString; const r: MarshaledAString): MarshaledAString; cdecl; 768 | luaL_setfuncs: procedure(L: lua_State; const l_: PluaL_Reg; nup: Integer); cdecl; 769 | 770 | luaL_getsubtable: function(L: lua_State; idx: Integer; const fname: MarshaledAString): Integer; cdecl; 771 | 772 | luaL_traceback: procedure(L: lua_State; L1: lua_State; const msg: MarshaledAString; level: Integer); cdecl; 773 | 774 | luaL_requiref: procedure(L: lua_State; const modname: MarshaledAString; openf: lua_CFunction; glb: Integer); cdecl; 775 | {$ENDIF} 776 | 777 | 778 | 779 | {* 780 | ** ====================================================== 781 | ** Generic Buffer manipulation 782 | ** ====================================================== 783 | *} 784 | {$IFDEF STATICLIBRARY} 785 | procedure luaL_buffinit(L: lua_State; B: PluaL_Buffer); cdecl; external LUA_LIBRARY; 786 | function luaL_prepbuffsize(B: Plual_buffer; sz: size_t): Pointer; cdecl; external LUA_LIBRARY; 787 | procedure luaL_addlstring(B: Plual_buffer; const s: MarshaledAString; l: size_t); cdecl; external LUA_LIBRARY; 788 | procedure luaL_addstring(B: Plual_buffer; const s: MarshaledAString); cdecl; external LUA_LIBRARY; 789 | procedure luaL_addvalue(B: Plual_buffer); cdecl; external LUA_LIBRARY; 790 | procedure luaL_pushresult(B: Plual_buffer); cdecl; external LUA_LIBRARY; 791 | procedure luaL_pushresultsize(B: Plual_buffer; sz: size_t); cdecl; external LUA_LIBRARY; 792 | function luaL_buffinitsize(L: lua_State; B: Plual_buffer; sz: size_t): Pointer; cdecl; external LUA_LIBRARY; 793 | {$ELSE} 794 | luaL_buffinit: procedure(L: lua_State; B: PluaL_Buffer); cdecl; 795 | luaL_prepbuffsize: function(B: Plual_buffer; sz: size_t): Pointer; cdecl; 796 | luaL_addlstring: procedure(B: Plual_buffer; const s: MarshaledAString; l: size_t); cdecl; 797 | luaL_addstring: procedure(B: Plual_buffer; const s: MarshaledAString); cdecl; 798 | luaL_addvalue: procedure(B: Plual_buffer); cdecl; 799 | luaL_pushresult: procedure(B: Plual_buffer); cdecl; 800 | luaL_pushresultsize: procedure(B: Plual_buffer; sz: size_t); cdecl; 801 | luaL_buffinitsize: function(L: lua_State; B: Plual_buffer; sz: size_t): Pointer; cdecl; 802 | {$ENDIF} 803 | 804 | 805 | {* ====================================================== *} 806 | 807 | 808 | {* compatibility with old module system */ 809 | #if defined(LUA_COMPAT_MODULE) 810 | 811 | LUALIB_API void (luaL_pushmodule) (L: lua_State; const char *modname, 812 | int sizehint); 813 | LUALIB_API void (luaL_openlib) (L: lua_State; const char *libname, 814 | const luaL_Reg *l, int nup); 815 | 816 | #define luaL_register(L,n,l) (luaL_openlib(L,(n),(l),0)) 817 | 818 | #endif} 819 | 820 | 821 | {* 822 | ** {============================================================ 823 | ** Compatibility with deprecated conversions 824 | ** ============================================================= 825 | */ 826 | #if defined(LUA_COMPAT_APIINTCASTS) 827 | 828 | #define luaL_checkunsigned(L,a) ((lua_Unsigned)luaL_checkinteger(L,a)) 829 | #define luaL_optunsigned(L,a,d) \ 830 | ((lua_Unsigned)luaL_optinteger(L,a,(lua_Integer)(d))) 831 | 832 | #define luaL_checkint(L,n) ((int)luaL_checkinteger(L, (n))) 833 | #define luaL_optint(L,n,d) ((int)luaL_optinteger(L, (n), (d))) 834 | 835 | #define luaL_checklong(L,n) ((long)luaL_checkinteger(L, (n))) 836 | #define luaL_optlong(L,n,d) ((long)luaL_optinteger(L, (n), (d))) 837 | 838 | #endif 839 | } 840 | 841 | 842 | {* 843 | ** ============================================================== 844 | ** some useful macros 845 | ** ============================================================== 846 | *} 847 | //#define lua_getextraspace(L) ((void *)((char *)(L) - LUA_EXTRASPACE)) 848 | function lua_tonumber(L: lua_State; idx: Integer): lua_Number; inline; 849 | function lua_tointeger(L: lua_State; idx: Integer): lua_Integer; inline; 850 | procedure lua_pop(L: lua_State; n: Integer); inline; 851 | procedure lua_newtable(L: lua_state); inline; 852 | procedure lua_register(L: lua_State; const n: MarshaledAString; f: lua_CFunction); inline; 853 | procedure lua_pushcfunction(L: lua_State; f: lua_CFunction); inline; 854 | function lua_isfunction(L: lua_State; n: Integer): Boolean; inline; 855 | function lua_istable(L: lua_State; n: Integer): Boolean; inline; 856 | function lua_islightuserdata(L: lua_State; n: Integer): Boolean; inline; 857 | function lua_isnil(L: lua_State; n: Integer): Boolean; inline; 858 | function lua_isboolean(L: lua_State; n: Integer): Boolean; inline; 859 | function lua_isthread(L: lua_State; n: Integer): Boolean; inline; 860 | function lua_isnone(L: lua_State; n: Integer): Boolean; inline; 861 | function lua_isnoneornil(L: lua_State; n: Integer): Boolean; inline; 862 | procedure lua_pushliteral(L: lua_State; s: MarshaledAString); inline; 863 | procedure lua_pushglobaltable(L: lua_State); inline; 864 | function lua_tostring(L: lua_State; i: Integer): MarshaledAString; 865 | procedure lua_insert(L: lua_State; idx: Integer); inline; 866 | procedure lua_remove(L: lua_State; idx: Integer); inline; 867 | procedure lua_replace(L: lua_State; idx: Integer); inline; 868 | 869 | {* 870 | ** =============================================================== 871 | ** some useful lauxlib macros 872 | ** =============================================================== 873 | *} 874 | 875 | procedure luaL_newlibtable(L: lua_State; lr: array of luaL_Reg); overload; 876 | procedure luaL_newlibtable(L: lua_State; lr: PluaL_Reg); overload; 877 | procedure luaL_newlib(L: lua_State; lr: array of luaL_Reg); overload; 878 | procedure luaL_newlib(L: lua_State; lr: PluaL_Reg); overload; 879 | procedure luaL_argcheck(L: lua_State; cond: Boolean; arg: Integer; extramsg: MarshaledAString); 880 | function luaL_checkstring(L: lua_State; n: Integer): MarshaledAString; 881 | function luaL_optstring(L: lua_State; n: Integer; d: MarshaledAString): MarshaledAString; 882 | function luaL_typename(L: lua_State; i: Integer): MarshaledAString; 883 | function luaL_dofile(L: lua_State; const fn: MarshaledAString): Integer; 884 | function luaL_dostring(L: lua_State; const s: MarshaledAString): Integer; 885 | procedure luaL_getmetatable(L: lua_State; n: MarshaledAString); 886 | function luaL_loadbuffer(L: lua_State; const s: MarshaledAString; sz: size_t; const n: MarshaledAString): Integer; 887 | { 888 | 889 | #define luaL_addchar(B,c) \ 890 | ((void)((B)->n < (B)->size || luaL_prepbuffsize((B), 1)), \ 891 | ((B)->b[(B)->n++] = (c))) 892 | 893 | procedure luaL_addsize(B: PluaL_Buffer; s: MarshaledAString); 894 | 895 | #define B,s) ((B)->n += (s))} 896 | 897 | 898 | {* 899 | ** ============================================================== 900 | ** other macros needed 901 | ** ============================================================== 902 | *} 903 | procedure lua_call(L: lua_State; nargs: Integer; nresults: Integer); inline; 904 | function lua_pcall(L: lua_State; nargs: Integer; nresults: Integer; errfunc: Integer): Integer; inline; 905 | function lua_yield(L: lua_State; nresults: Integer): Integer; inline; 906 | function lua_upvalueindex(i: Integer): Integer; inline; 907 | procedure luaL_checkversion(L: lua_State); inline; 908 | function lual_loadfile(L: lua_State; const filename: MarshaledAString): Integer; inline; 909 | function luaL_prepbuffer(B: Plual_buffer): MarshaledAString; inline; 910 | 911 | {* 912 | ** ================================================================== 913 | ** "Abstraction Layer" for basic report of messages and errors 914 | ** ================================================================== 915 | *} 916 | 917 | {* print a string * 918 | #if !defined(lua_writestring) 919 | #define lua_writestring(s,l) fwrite((s), sizeof(char), (l), stdout) 920 | #endif 921 | 922 | /* print a newline and flush the output */ 923 | #if !defined(lua_writeline) 924 | #define lua_writeline() (lua_writestring("\n", 1), fflush(stdout)) 925 | #endif 926 | 927 | /* print an error message */ 928 | #if !defined(lua_writestringerror) 929 | #define lua_writestringerror(s,p) \ 930 | (fprintf(stderr, (s), (p)), fflush(stderr)) 931 | #endif 932 | } 933 | 934 | {* 935 | ** ============================================================== 936 | ** compatibility macros for unsigned conversions 937 | ** =============================================================== 938 | */ 939 | #if defined(LUA_COMPAT_APIINTCASTS) 940 | 941 | #define lua_pushunsigned(L,n) lua_pushinteger(L, (lua_Integer)(n)) 942 | #define lua_tounsignedx(L,i,is) ((lua_Unsigned)lua_tointegerx(L,i,is)) 943 | #define lua_tounsigned(L,i) lua_tounsignedx(L,(i),NULL) 944 | 945 | #endif 946 | * ============================================================== *} 947 | 948 | {* ====================================================================== *} 949 | 950 | implementation 951 | 952 | function lua_tonumber(L: lua_State; idx: Integer): lua_Number; inline; 953 | begin 954 | Result := lua_tonumberx(L, idx, NIL); 955 | end; 956 | 957 | function lua_tointeger(L: lua_State; idx: Integer): lua_Integer; inline; 958 | begin 959 | Result := lua_tointegerx(L, idx, NIL); 960 | end; 961 | 962 | procedure lua_pop(L: lua_State; n: Integer); inline; 963 | begin 964 | lua_settop(L, -(n)-1); 965 | end; 966 | 967 | procedure lua_newtable(L: lua_state); inline; 968 | begin 969 | lua_createtable(L, 0, 0); 970 | end; 971 | 972 | procedure lua_register(L: lua_State; const n: MarshaledAString; f: lua_CFunction); 973 | begin 974 | lua_pushcfunction(L, f); 975 | lua_setglobal(L, n); 976 | end; 977 | 978 | procedure lua_pushcfunction(L: lua_State; f: lua_CFunction); 979 | begin 980 | lua_pushcclosure(L, f, 0); 981 | end; 982 | 983 | function lua_isfunction(L: lua_State; n: Integer): Boolean; 984 | begin 985 | Result := (lua_type(L, n) = LUA_TFUNCTION); 986 | end; 987 | 988 | function lua_istable(L: lua_State; n: Integer): Boolean; 989 | begin 990 | Result := (lua_type(L, n) = LUA_TTABLE); 991 | end; 992 | 993 | function lua_islightuserdata(L: lua_State; n: Integer): Boolean; 994 | begin 995 | Result := (lua_type(L, n) = LUA_TLIGHTUSERDATA); 996 | end; 997 | 998 | function lua_isnil(L: lua_State; n: Integer): Boolean; 999 | begin 1000 | Result := (lua_type(L, n) = LUA_TNIL); 1001 | end; 1002 | 1003 | function lua_isboolean(L: lua_State; n: Integer): Boolean; 1004 | begin 1005 | Result := (lua_type(L, n) = LUA_TBOOLEAN); 1006 | end; 1007 | 1008 | function lua_isthread(L: lua_State; n: Integer): Boolean; 1009 | begin 1010 | Result := (lua_type(L, n) = LUA_TTHREAD); 1011 | end; 1012 | 1013 | function lua_isnone(L: lua_State; n: Integer): Boolean; 1014 | begin 1015 | Result := (lua_type(L, n) = LUA_TNONE); 1016 | end; 1017 | 1018 | function lua_isnoneornil(L: lua_State; n: Integer): Boolean; 1019 | begin 1020 | Result := (lua_type(L, n) <= 0); 1021 | end; 1022 | 1023 | procedure lua_pushliteral(L: lua_State; s: MarshaledAString); 1024 | begin 1025 | lua_pushlstring(L, s, Length(s)); 1026 | end; 1027 | 1028 | procedure lua_pushglobaltable(L: lua_State); 1029 | begin 1030 | lua_rawgeti(L, LUA_REGISTRYINDEX, LUA_RIDX_GLOBALS); 1031 | end; 1032 | 1033 | function lua_tostring(L: lua_State; i: Integer): MarshaledAString; 1034 | begin 1035 | Result := lua_tolstring(L, i, NIL); 1036 | end; 1037 | 1038 | procedure lua_insert(L: lua_State; idx: Integer); 1039 | begin 1040 | lua_rotate(L, idx, 1); 1041 | end; 1042 | 1043 | procedure lua_remove(L: lua_State; idx: Integer); 1044 | begin 1045 | lua_rotate(L, idx, -1); 1046 | lua_pop(L, 1); 1047 | end; 1048 | 1049 | procedure lua_replace(L: lua_State; idx: Integer); 1050 | begin 1051 | lua_copy(L, -1, idx); 1052 | lua_pop(L, 1); 1053 | end; 1054 | 1055 | procedure lua_call(L: lua_State; nargs: Integer; nresults: Integer); inline; 1056 | begin 1057 | lua_callk(L, nargs, nresults, 0, NIL); 1058 | end; 1059 | 1060 | function lua_pcall(L: lua_State; nargs: Integer; nresults: Integer; errfunc: Integer): Integer; inline; 1061 | begin 1062 | Result := lua_pcallk(L, nargs, nresults, errfunc, 0, NIL); 1063 | end; 1064 | 1065 | function lua_yield(L: lua_State; nresults: Integer): Integer; inline; 1066 | begin 1067 | Result := lua_yieldk(L, nresults, 0, NIL); 1068 | end; 1069 | 1070 | function lua_upvalueindex(i: Integer): Integer; inline; 1071 | begin 1072 | Result := LUA_REGISTRYINDEX - i; 1073 | end; 1074 | 1075 | {* 1076 | ** =============================================================== 1077 | ** some useful lauxlib macros 1078 | ** =============================================================== 1079 | *} 1080 | 1081 | procedure luaL_newlibtable(L: lua_State; lr: array of luaL_Reg); overload; 1082 | begin 1083 | lua_createtable(L, 0, High(lr)); 1084 | end; 1085 | 1086 | procedure luaL_newlibtable(L: lua_State; lr: PluaL_Reg); overload; 1087 | var 1088 | n: Integer; 1089 | begin 1090 | n := 0; 1091 | while lr^.name <> nil do 1092 | begin 1093 | inc(n); 1094 | inc(lr); 1095 | end; 1096 | lua_createtable(L, 0, n); 1097 | end; 1098 | 1099 | procedure luaL_newlib(L: lua_State; lr: array of luaL_Reg); overload; 1100 | begin 1101 | luaL_newlibtable(L, lr); 1102 | luaL_setfuncs(L, @lr, 0); 1103 | end; 1104 | 1105 | procedure luaL_newlib(L: lua_State; lr: PluaL_Reg); overload; 1106 | begin 1107 | luaL_newlibtable(L, lr); 1108 | luaL_setfuncs(L, lr, 0); 1109 | end; 1110 | 1111 | procedure luaL_argcheck(L: lua_State; cond: Boolean; arg: Integer; extramsg: MarshaledAString); 1112 | begin 1113 | if not cond then 1114 | luaL_argerror(L, arg, extramsg); 1115 | end; 1116 | 1117 | 1118 | function luaL_checkstring(L: lua_State; n: Integer): MarshaledAString; 1119 | begin 1120 | Result := luaL_checklstring(L, n, nil); 1121 | end; 1122 | 1123 | function luaL_optstring(L: lua_State; n: Integer; d: MarshaledAString): MarshaledAString; 1124 | begin 1125 | Result := luaL_optlstring(L, n, d, nil); 1126 | end; 1127 | 1128 | function luaL_typename(L: lua_State; i: Integer): MarshaledAString; 1129 | begin 1130 | Result := lua_typename(L, lua_type(L, i)); 1131 | end; 1132 | 1133 | function luaL_dofile(L: lua_State; const fn: MarshaledAString): Integer; 1134 | begin 1135 | Result := luaL_loadfile(L, fn); 1136 | if Result = 0 then 1137 | Result := lua_pcall(L, 0, LUA_MULTRET, 0); 1138 | end; 1139 | 1140 | 1141 | function luaL_dostring(L: lua_State; const s: MarshaledAString): Integer; 1142 | begin 1143 | Result := luaL_loadstring(L, s); 1144 | if Result = 0 then 1145 | Result := lua_pcall(L, 0, LUA_MULTRET, 0); 1146 | end; 1147 | 1148 | procedure luaL_getmetatable(L: lua_State; n: MarshaledAString); 1149 | begin 1150 | lua_getfield(L, LUA_REGISTRYINDEX, n); 1151 | end; 1152 | 1153 | function luaL_loadbuffer(L: lua_State; const s: MarshaledAString; sz: size_t; const n: MarshaledAString): Integer; 1154 | begin 1155 | Result := luaL_loadbufferx(L, s, sz, n, NIL); 1156 | end; 1157 | 1158 | 1159 | procedure luaL_checkversion(L: lua_State); inline; 1160 | begin 1161 | luaL_checkversion_(L, LUA_VERSION_NUM, LUAL_NUMSIZES); 1162 | end; 1163 | 1164 | function lual_loadfile(L: lua_State; const filename: MarshaledAString): Integer; inline; 1165 | begin 1166 | Result := luaL_loadfilex(L, filename, NIL); 1167 | end; 1168 | 1169 | function luaL_prepbuffer(B: Plual_buffer): MarshaledAString; inline; 1170 | begin 1171 | Result := luaL_prepbuffsize(B, LUAL_BUFFERSIZE); 1172 | end; 1173 | 1174 | 1175 | 1176 | {****************************************************************************** 1177 | * Copyright (C) 1994-2015 Lua.org, PUC-Rio. 1178 | * 1179 | * Permission is hereby granted, free of charge, to any person obtaining 1180 | * a copy of this software and associated documentation files (the 1181 | * "Software"), to deal in the Software without restriction, including 1182 | * without limitation the rights to use, copy, modify, merge, publish, 1183 | * distribute, sublicense, and/or sell copies of the Software, and to 1184 | * permit persons to whom the Software is furnished to do so, subject to 1185 | * the following conditions: 1186 | * 1187 | * The above copyright notice and this permission notice shall be 1188 | * included in all copies or substantial portions of the Software. 1189 | * 1190 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 1191 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 1192 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 1193 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 1194 | * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 1195 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 1196 | * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 1197 | ******************************************************************************} 1198 | end. 1199 | --------------------------------------------------------------------------------