7 changed files with 429 additions and 3 deletions
@ -1 +1,5 @@
@@ -1 +1,5 @@
|
||||
__history |
||||
__history |
||||
*.local |
||||
*.identcache |
||||
bin |
||||
lib |
@ -0,0 +1,19 @@
@@ -0,0 +1,19 @@
|
||||
program DelphiLuaInteractiveTests; |
||||
|
||||
uses |
||||
Vcl.Forms, |
||||
MainFrm in 'MainFrm.pas' {MainForm}, |
||||
Lua.API in '..\Lua.API.pas', |
||||
Lua in '..\Lua.pas'; |
||||
|
||||
{$R *.res} |
||||
|
||||
var |
||||
MainForm: TMainForm; |
||||
|
||||
begin |
||||
Application.Initialize; |
||||
Application.MainFormOnTaskbar := True; |
||||
Application.CreateForm(TMainForm, MainForm); |
||||
Application.Run; |
||||
end. |
Binary file not shown.
@ -0,0 +1,114 @@
@@ -0,0 +1,114 @@
|
||||
object MainForm: TMainForm |
||||
Left = 0 |
||||
Top = 0 |
||||
Caption = 'DelphiLua Interactive Test' |
||||
ClientHeight = 547 |
||||
ClientWidth = 678 |
||||
Color = clBtnFace |
||||
Font.Charset = DEFAULT_CHARSET |
||||
Font.Color = clWindowText |
||||
Font.Height = -11 |
||||
Font.Name = 'Tahoma' |
||||
Font.Style = [] |
||||
OldCreateOrder = False |
||||
Position = poDesktopCenter |
||||
OnDestroy = FormDestroy |
||||
PixelsPerInch = 96 |
||||
TextHeight = 13 |
||||
object PageControl: TPageControl |
||||
AlignWithMargins = True |
||||
Left = 8 |
||||
Top = 8 |
||||
Width = 662 |
||||
Height = 416 |
||||
Margins.Left = 8 |
||||
Margins.Top = 8 |
||||
Margins.Right = 8 |
||||
Margins.Bottom = 8 |
||||
ActivePage = FunctionReferenceTab |
||||
Align = alClient |
||||
TabOrder = 0 |
||||
object FunctionReferenceTab: TTabSheet |
||||
Caption = 'Function reference' |
||||
object FunctionReferenceRichEdit: TRichEdit |
||||
AlignWithMargins = True |
||||
Left = 8 |
||||
Top = 49 |
||||
Width = 638 |
||||
Height = 331 |
||||
Margins.Left = 8 |
||||
Margins.Top = 8 |
||||
Margins.Right = 8 |
||||
Margins.Bottom = 8 |
||||
Align = alClient |
||||
Font.Charset = ANSI_CHARSET |
||||
Font.Color = clWindowText |
||||
Font.Height = -11 |
||||
Font.Name = 'Courier New' |
||||
Font.Style = [] |
||||
Lines.Strings = ( |
||||
'function engine.onStartup()' |
||||
' engine.myFunction()' |
||||
' engine.display.log("Hello from display.log!")' |
||||
'end' |
||||
'' |
||||
'' |
||||
'function engine.myFunction()' |
||||
' print("Hello from myFunction!")' |
||||
' engine.display.log("Hello from myFunction'#39's display.log!")' |
||||
'end') |
||||
ParentFont = False |
||||
ScrollBars = ssBoth |
||||
TabOrder = 1 |
||||
end |
||||
object FunctionReferenceButtonPanel: TPanel |
||||
Left = 0 |
||||
Top = 0 |
||||
Width = 654 |
||||
Height = 41 |
||||
Align = alTop |
||||
BevelOuter = bvNone |
||||
TabOrder = 0 |
||||
object FunctionReferenceGetButton: TButton |
||||
Left = 8 |
||||
Top = 13 |
||||
Width = 121 |
||||
Height = 25 |
||||
Caption = 'Lookup functions' |
||||
TabOrder = 0 |
||||
OnClick = FunctionReferenceGetButtonClick |
||||
end |
||||
object FunctionReferenceCallStartupButton: TButton |
||||
Left = 135 |
||||
Top = 13 |
||||
Width = 121 |
||||
Height = 25 |
||||
Caption = 'Call onStartup' |
||||
TabOrder = 1 |
||||
OnClick = FunctionReferenceCallStartupButtonClick |
||||
end |
||||
end |
||||
end |
||||
end |
||||
object LogRichEdit: TRichEdit |
||||
AlignWithMargins = True |
||||
Left = 8 |
||||
Top = 432 |
||||
Width = 662 |
||||
Height = 107 |
||||
Margins.Left = 8 |
||||
Margins.Top = 0 |
||||
Margins.Right = 8 |
||||
Margins.Bottom = 8 |
||||
Align = alBottom |
||||
Font.Charset = ANSI_CHARSET |
||||
Font.Color = clWindowText |
||||
Font.Height = -11 |
||||
Font.Name = 'Tahoma' |
||||
Font.Style = [] |
||||
ParentFont = False |
||||
ReadOnly = True |
||||
ScrollBars = ssVertical |
||||
TabOrder = 1 |
||||
end |
||||
end |
@ -0,0 +1,128 @@
@@ -0,0 +1,128 @@
|
||||
unit MainFrm; |
||||
|
||||
interface |
||||
uses |
||||
System.Classes, |
||||
Vcl.ComCtrls, |
||||
Vcl.Controls, |
||||
Vcl.ExtCtrls, |
||||
Vcl.Forms, |
||||
Vcl.StdCtrls, |
||||
|
||||
Lua; |
||||
|
||||
|
||||
type |
||||
TMainForm = class(TForm) |
||||
PageControl: TPageControl; |
||||
FunctionReferenceTab: TTabSheet; |
||||
FunctionReferenceRichEdit: TRichEdit; |
||||
LogRichEdit: TRichEdit; |
||||
FunctionReferenceButtonPanel: TPanel; |
||||
FunctionReferenceGetButton: TButton; |
||||
FunctionReferenceCallStartupButton: TButton; |
||||
|
||||
procedure FormDestroy(Sender: TObject); |
||||
procedure FunctionReferenceGetButtonClick(Sender: TObject); |
||||
procedure FunctionReferenceCallStartupButtonClick(Sender: TObject); |
||||
private |
||||
FFunctionReferenceLua: TLua; |
||||
FFunctionReferenceEngine: ILuaTable; |
||||
FFunctionReferenceEngineDisplay: ILuaTable; |
||||
FFunctionReferenceStartup: ILuaFunction; |
||||
|
||||
procedure CreateLua(var ALua: TLua; ACode: TStrings); |
||||
|
||||
procedure Log(const AMessage: string); |
||||
procedure Print(AContext: ILuaContext); |
||||
end; |
||||
|
||||
|
||||
implementation |
||||
uses |
||||
System.SysUtils; |
||||
|
||||
|
||||
{$R *.dfm} |
||||
|
||||
{ TMainForm } |
||||
procedure TMainForm.FormDestroy(Sender: TObject); |
||||
begin |
||||
FFunctionReferenceEngineDisplay := nil; |
||||
FFunctionReferenceEngine := nil; |
||||
FFunctionReferenceStartup := nil; |
||||
FreeAndNil(FFunctionReferenceLua); |
||||
end; |
||||
|
||||
|
||||
procedure TMainForm.FunctionReferenceGetButtonClick(Sender: TObject); |
||||
begin |
||||
try |
||||
CreateLua(FFunctionReferenceLua, FunctionReferenceRichEdit.Lines); |
||||
|
||||
Log('Initializing global variable "engine"...'); |
||||
FFunctionReferenceEngineDisplay := TLuaTable.Create; |
||||
FFunctionReferenceEngineDisplay.SetValue('log', |
||||
procedure(Context: ILuaContext) |
||||
begin |
||||
Log(Context.Parameters.ToString); |
||||
end); |
||||
|
||||
FFunctionReferenceEngine := TLuaTable.Create; |
||||
FFunctionReferenceEngine.SetValue('display', FFunctionReferenceEngineDisplay); |
||||
|
||||
FFunctionReferenceLua.SetGlobalVariable('engine', FFunctionReferenceEngine); |
||||
|
||||
Log('Running code...'); |
||||
FFunctionReferenceLua.Run; |
||||
|
||||
Log('Reading back global variable "engine"...'); |
||||
FFunctionReferenceEngine := FFunctionReferenceLua.GetGlobalVariable('engine').AsTable; |
||||
|
||||
Log('Reading engine.onStartup...'); |
||||
FFunctionReferenceStartup := FFunctionReferenceEngine.GetValue('onStartup').AsFunction; |
||||
if not Assigned(FFunctionReferenceStartup) then |
||||
begin |
||||
Log('Error: onStartup not found!'); |
||||
Exit; |
||||
end; |
||||
|
||||
Log('Done.'); |
||||
except |
||||
on E:Exception do |
||||
Log(E.ClassName + ': ' + E.Message); |
||||
end; |
||||
end; |
||||
|
||||
|
||||
procedure TMainForm.FunctionReferenceCallStartupButtonClick(Sender: TObject); |
||||
begin |
||||
FFunctionReferenceStartup.Call(); |
||||
end; |
||||
|
||||
|
||||
procedure TMainForm.CreateLua(var ALua: TLua; ACode: TStrings); |
||||
begin |
||||
FreeAndNil(ALua); |
||||
Log('Loading Lua code...'); |
||||
|
||||
ALua := TLua.Create; |
||||
ALua.RegisterFunction('print', Print); |
||||
|
||||
ALua.LoadFromString(ACode.Text, False); |
||||
end; |
||||
|
||||
|
||||
procedure TMainForm.Log(const AMessage: string); |
||||
begin |
||||
LogRichEdit.Lines.Add(AMessage); |
||||
LogRichEdit.SelStart := MaxInt; |
||||
end; |
||||
|
||||
|
||||
procedure TMainForm.Print(AContext: ILuaContext); |
||||
begin |
||||
Log(AContext.Parameters.ToString); |
||||
end; |
||||
|
||||
end. |
Loading…
Reference in new issue