-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathLua.pas
2619 lines (1949 loc) · 67.5 KB
/
Lua.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{
Wrapper classes for Lua API
Created by M. van Renswoude, April 2014:
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
}
unit Lua;
interface
uses
System.Classes,
System.Generics.Collections,
System.Generics.Defaults,
System.Rtti,
System.SysUtils,
Lua.API;
type
ELuaException = class(Exception);
ELuaInitException = class(ELuaException);
ELuaUnsupportedParameterException = class(ELuaException);
ELuaUnsupportedVariableException = class(ELuaException);
ELuaNoFunctionException = class(ELuaException);
ELuaNativeCodeException = class(ELuaException);
TLuaLibrary = (Base, Coroutine, Table, IO, OS, StringLib, Bit32, Math, Debug, Package, All);
TLuaLibraries = set of TLuaLibrary;
TLuaDataType = (LuaNone, LuaNil, LuaNumber, LuaBoolean, LuaString, LuaTable,
LuaFunction, LuaUserData, LuaThread, LuaLightUserData, LuaCClosure);
TLuaVariableType = (VariableNone, VariableBoolean, VariableInteger,
VariableNumber, VariableUserData, VariableString,
VariableTable, VariableFunction, VariableCFunction);
ILuaTable = interface;
ILuaFunction = interface;
ILuaContext = interface;
TLuaCFunction = reference to procedure(Context: ILuaContext);
TLuaCMethod = procedure(Context: ILuaContext) of object;
TLuaPushFunction = reference to procedure(AFunction: TLuaCFunction);
ILuaVariable = interface
['{ADA0D4FB-F0FB-4493-8FEC-6FC92C80117F}']
function GetVariableType: TLuaVariableType;
function GetDataType: TLuaDataType;
function GetAsBoolean: Boolean;
function GetAsInteger: Integer;
function GetAsNumber: Double;
function GetAsUserData: Pointer;
function GetAsString: string;
function GetAsTable: ILuaTable;
function GetAsFunction: ILuaFunction;
function GetAsCFunction: TLuaCFunction;
procedure SetAsBoolean(ABoolean: Boolean);
procedure SetAsInteger(AInteger: Integer);
procedure SetAsNumber(ANumber: Double);
procedure SetAsUserData(AUserData: Pointer);
procedure SetAsString(AString: string);
procedure SetAsTable(ATable: ILuaTable);
property VariableType: TLuaVariableType read GetVariableType;
property DataType: TLuaDataType read GetDataType;
property AsBoolean: Boolean read GetAsBoolean write SetAsBoolean;
property AsInteger: Integer read GetAsInteger write SetAsInteger;
property AsNumber: Double read GetAsNumber write SetAsNumber;
property AsUserData: Pointer read GetAsUserData write SetAsUserData;
property AsString: string read GetAsString write SetAsString;
property AsTable: ILuaTable read GetAsTable write SetAsTable;
property AsFunction: ILuaFunction read GetAsFunction;
property AsCFunction: TLuaCFunction read GetAsCFunction;
end;
TLuaImplicitVariable = record
Variable: ILuaVariable;
class operator Implicit(AValue: ILuaVariable): TLuaImplicitVariable;
class operator Implicit(AValue: Boolean): TLuaImplicitVariable;
class operator Implicit(AValue: Integer): TLuaImplicitVariable;
class operator Implicit(AValue: Double): TLuaImplicitVariable;
class operator Implicit(AValue: Pointer): TLuaImplicitVariable;
class operator Implicit(const AValue: string): TLuaImplicitVariable;
class operator Implicit(AValue: ILuaTable): TLuaImplicitVariable;
class operator Implicit(AValue: TLuaCFunction): TLuaImplicitVariable;
class operator Implicit(AValue: TLuaImplicitVariable): ILuaVariable;
class operator Implicit(AValue: TLuaImplicitVariable): Boolean;
class operator Implicit(AValue: TLuaImplicitVariable): Integer;
class operator Implicit(AValue: TLuaImplicitVariable): Double;
class operator Implicit(AValue: TLuaImplicitVariable): Pointer;
class operator Implicit(AValue: TLuaImplicitVariable): string;
class operator Implicit(AValue: TLuaImplicitVariable): ILuaTable;
end;
TLuaKeyValuePair = record
Key: ILuaVariable;
Value: ILuaVariable;
end;
ILuaTableEnumerator = interface
['{4C3F4E20-F9E7-42E6-9446-78C535AF2E30}']
function GetCurrent: TLuaKeyValuePair;
function MoveNext: Boolean;
property Current: TLuaKeyValuePair read GetCurrent;
end;
ILuaTable = interface
['{57FD52A1-7D53-485B-A630-29841C498387}']
function GetEnumerator: ILuaTableEnumerator;
function HasValue(AKey: TLuaImplicitVariable): Boolean;
function GetValue(AKey: TLuaImplicitVariable): ILuaVariable;
procedure SetValue(AKey: TLuaImplicitVariable; AValue: TLuaImplicitVariable);
end;
TLuaTableEnumerator = class(TInterfacedObject, ILuaTableEnumerator)
private
FEnumerator: TEnumerator<TPair<ILuaVariable, ILuaVariable>>;
public
constructor Create(AEnumerator: TEnumerator<TPair<ILuaVariable, ILuaVariable>>);
destructor Destroy; override;
{ ILuaTableEnumerator }
function GetCurrent: TLuaKeyValuePair;
function MoveNext: Boolean;
property Current: TLuaKeyValuePair read GetCurrent;
end;
TLuaVariableEqualityComparer = class(TInterfacedObject, IEqualityComparer<ILuaVariable>)
public
{ IEqualityComparer<ILuaVariable> }
function Equals(const Left, Right: ILuaVariable): Boolean; reintroduce;
function GetHashCode(const Value: ILuaVariable): Integer; reintroduce;
end;
TLuaTable = class(TInterfacedObject, ILuaTable)
private
FTable: TDictionary<ILuaVariable, ILuaVariable>;
public
constructor Create;
destructor Destroy; override;
{ ILuaTable }
function GetEnumerator: ILuaTableEnumerator;
function HasValue(AKey: TLuaImplicitVariable): Boolean;
function GetValue(AKey: TLuaImplicitVariable): ILuaVariable;
procedure SetValue(AKey: TLuaImplicitVariable; AValue: TLuaImplicitVariable);
end;
ILuaParametersEnumerator = interface
function GetCurrent: ILuaVariable;
function MoveNext: Boolean;
property Current: ILuaVariable read GetCurrent;
end;
ILuaReadParameters = interface(ILuaVariable)
['{FB611D9E-B51D-460B-B5AB-B567EF853222}']
function GetCount: Integer;
function GetItem(Index: Integer): ILuaVariable;
function GetEnumerator: ILuaParametersEnumerator;
function ToString: string;
property Count: Integer read GetCount;
property Items[Index: Integer]: ILuaVariable read GetItem; default;
end;
ILuaWriteParameters = interface
['{5CEEB16B-158E-44BE-8CAD-DC2C330A244A}']
function GetCount: Integer;
procedure Push(ABoolean: Boolean); overload;
procedure Push(AInteger: Integer); overload;
procedure Push(ANumber: Double); overload;
procedure Push(AUserData: Pointer); overload;
procedure Push(const AString: string); overload;
procedure Push(ATable: ILuaTable); overload;
property Count: Integer read GetCount;
end;
TCustomLuaParameters = class(TInterfacedObject, ILuaVariable, ILuaReadParameters)
protected
function GetDefaultVariable: ILuaVariable;
public
{ ILuaVariable }
function GetVariableType: TLuaVariableType;
function GetDataType: TLuaDataType;
function GetAsBoolean: Boolean;
function GetAsInteger: Integer;
function GetAsNumber: Double;
function GetAsUserData: Pointer;
function GetAsString: string;
function GetAsTable: ILuaTable;
function GetAsFunction: ILuaFunction;
function GetAsCFunction: TLuaCFunction;
procedure SetAsBoolean(ABoolean: Boolean);
procedure SetAsInteger(AInteger: Integer);
procedure SetAsNumber(ANumber: Double);
procedure SetAsUserData(AUserData: Pointer);
procedure SetAsString(AString: string);
procedure SetAsTable(ATable: ILuaTable);
{ ILuaReadParameters }
function GetCount: Integer; virtual; abstract;
function GetItem(Index: Integer): ILuaVariable; virtual; abstract;
function GetEnumerator: ILuaParametersEnumerator;
function ToString: string; override;
end;
TLuaParameters = class(TCustomLuaParameters, ILuaWriteParameters)
private
FParameters: TList<ILuaVariable>;
public
constructor Create;
destructor Destroy; override;
function GetCount: Integer; override;
function GetItem(Index: Integer): ILuaVariable; override;
{ ILuaWriteParameters }
procedure Push(ABoolean: Boolean); overload;
procedure Push(AInteger: Integer); overload;
procedure Push(ANumber: Double); overload;
procedure Push(AUserData: Pointer); overload;
procedure Push(const AString: string); overload;
procedure Push(ATable: ILuaTable); overload;
end;
ILuaFunction = interface
['{1BE5E470-0318-410E-8D5B-94BFE04A3DBE}']
function Call(): ILuaReadParameters; overload;
function Call(AParameters: array of const): ILuaReadParameters; overload;
function Call(AParameters: ILuaReadParameters): ILuaReadParameters; overload;
end;
ILuaContext = interface
['{1F999593-E3D1-4195-9463-A42025AE9830}']
function GetParameters: ILuaReadParameters;
function GetResult: ILuaWriteParameters;
property Parameters: ILuaReadParameters read GetParameters;
property Result: ILuaWriteParameters read GetResult;
end;
TCustomLuaRegistration = class(TObject)
private
FName: string;
protected
property Name: string read FName write FName;
public
constructor Create(const AName: string);
procedure Apply(AState: lua_State; APushFunction: TLuaPushFunction); virtual; abstract;
end;
TLuaFunctionRegistration = class(TCustomLuaRegistration)
private
FCallback: TLuaCFunction;
protected
property Callback: TLuaCFunction read FCallback;
public
constructor Create(const AName: string; ACallback: TLuaCFunction);
procedure Apply(AState: lua_State; APushFunction: TLuaPushFunction); override;
end;
TLuaFunctionTable = TDictionary<string, TLuaCFunction>;
TLuaTableRegistration = class(TCustomLuaRegistration)
private
FFunctionTable: TLuaFunctionTable;
protected
property FunctionTable: TLuaFunctionTable read FFunctionTable;
public
constructor Create(const AName: string);
destructor Destroy; override;
procedure RegisterFunction(const AName: string; AFunction: TLuaCFunction); virtual;
procedure Apply(AState: lua_State; APushFunction: TLuaPushFunction); override;
end;
TLuaRegistrationList = TObjectList<TCustomLuaRegistration>;
TLuaRegisteredFunctionDictionary = TDictionary<Integer, TLuaCFunction>;
TLuaScript = class(TObject)
private
FStream: TStream;
FStreamOwnership: TStreamOwnership;
FBuffer: PAnsiChar;
public
constructor Create(const AData: string); overload;
constructor Create(const AStream: TStream; AOwnership: TStreamOwnership = soReference); overload;
destructor Destroy; override;
function GetNextChunk(out ASize: NativeUint): PAnsiChar; virtual;
end;
TLua = class(TObject)
private
FState: lua_State;
FLoaded: Boolean;
FRegistrations: TLuaRegistrationList;
FRegisteredFunctions: TLuaRegisteredFunctionDictionary;
FRegisteredFunctionCookie: Integer;
FAutoOpenLibraries: TLuaLibraries;
FHasRun: Boolean;
FRttiContext: TRttiContext;
protected
function GetHasState: Boolean; virtual;
function GetState: lua_State; virtual;
function DoAlloc(APointer: Pointer; AOldSize, ANewSize: NativeUint): Pointer; virtual;
procedure DoNewState; virtual;
procedure DoClose; virtual;
procedure DoRegistration(ARegistration: TCustomLuaRegistration); virtual;
procedure SetAutoOpenLibraries(const Value: TLuaLibraries); virtual;
protected
procedure CheckState; virtual;
procedure CheckIsFunction; virtual;
procedure AfterLoad; virtual;
procedure AddRegistration(ARegistration: TCustomLuaRegistration); virtual;
function GetRegisteredFunctionCookie: Integer; virtual;
function RunRegisteredFunction(ACookie: Integer): Integer; virtual;
property Loaded: Boolean read FLoaded write FLoaded;
property HasRun: Boolean read FHasRun write FHasRun;
property Registrations: TLuaRegistrationList read FRegistrations;
property RegisteredFunctions: TLuaRegisteredFunctionDictionary read FRegisteredFunctions;
public
constructor Create;
destructor Destroy; override;
procedure LoadFromString(const AData: string; AAutoRun: Boolean = True; const AChunkName: string = ''); virtual;
procedure LoadFromStream(AStream: TStream; AOwnership: TStreamOwnership = soReference; AAutoRun: Boolean = True; const AChunkName: string = ''); virtual;
procedure LoadFromFile(const AFileName: string; AAutoRun: Boolean = True; const AChunkName: string = ''); virtual;
procedure LoadFromScript(AScript: TLuaScript; AOwnership: TStreamOwnership = soReference; AAutoRun: Boolean = True; const AChunkName: string = ''); virtual;
function GetGlobalVariable(const AName: string): ILuaVariable; virtual;
procedure SetGlobalVariable(const AName: string; AVariable: TLuaImplicitVariable); virtual;
procedure RegisterFunction(const AName: string; AFunction: TLuaCFunction); virtual;
// procedure UnregisterFunction();
{ Registers all published methods of the specified object. If ATableName
is provided, the methods will be bundled in a global table and accessible as
ATableName.Method(). If not provided, the methods are accessible directly
as global functions. }
procedure RegisterFunctions(AObject: TObject; const ATableName: string = ''; AIncludePublicVisibility: Boolean = False); virtual;
// procedure UnregisterFunctions();
procedure OpenLibraries(ALibraries: TLuaLibraries); virtual;
{ Get or set the current path(s) used for require calls. Paths must be separated
by semicolons and questions marks will be replaced with the requested file name,
as per the Lua documentation at: https://www.lua.org/pil/8.1.html }
function GetRequirePath: string;
procedure SetRequirePath(const APath: string);
procedure AddRequirePath(const APath: string);
{ These methods should only be called right after one of the
LoadFrom methods, which must have AutoRun set to False. }
procedure Run; virtual;
procedure GetByteCode(AStream: TStream; APop: Boolean = False); virtual;
procedure Capture(const AName: string); virtual;
function Call(const AFunctionName: string): ILuaReadParameters; overload; virtual;
function Call(const AFunctionName: string; AParameters: array of const): ILuaReadParameters; overload; virtual;
function Call(const AFunctionName: string; AParameters: ILuaReadParameters): ILuaReadParameters; overload; virtual;
property HasState: Boolean read GetHasState;
property State: lua_State read GetState;
property AutoOpenLibraries: TLuaLibraries read FAutoOpenLibraries write SetAutoOpenLibraries default [TLuaLibrary.All];
end;
TLuaHelpers = class(TObject)
private
class var RegistryKeyCounter: Int64;
public
class function GetLuaDataType(AType: Integer): TLuaDataType;
class function GetLuaVariableType(ADataType: TLuaDataType): TLuaVariableType;
class function CreateParameters(AParameters: array of const): ILuaReadParameters;
class procedure PushVariable(AState: lua_State; AVariable: ILuaVariable); overload;
class procedure PushVariable(AState: lua_State; AVariable: ILuaVariable; AVariableType: TLuaVariableType); overload;
class procedure PushString(AState: lua_State; const AValue: string);
class procedure PushTable(AState: lua_State; ATable: ILuaTable);
class procedure PushCFunction(AState: lua_State; AClosure: TLuaCFunction);
class function AllocLuaString(const AValue: string): PAnsiChar;
class procedure FreeLuaString(AValue: PAnsiChar);
class procedure RaiseLastLuaError(AState: lua_State);
class function LuaToString(AState: lua_State; AIndex: Integer): string;
class function CallFunction(AState: lua_State; AParameters: ILuaReadParameters): ILuaReadParameters;
class function NewRegistryKey: string;
end;
implementation
uses
System.Math,
System.SyncObjs,
System.TypInfo;
type
PLuaScript = ^TLuaScript;
TLuaParametersEnumerator = class(TInterfacedObject, ILuaParametersEnumerator)
private
FParameters: ILuaReadParameters;
FIndex: Integer;
protected
property Parameters: ILuaReadParameters read FParameters;
public
constructor Create(AParameters: ILuaReadParameters);
function GetCurrent: ILuaVariable;
function MoveNext: Boolean;
end;
TLuaStackParameters = class(TCustomLuaParameters)
private
FState: lua_State;
FCount: Integer;
protected
property State: lua_State read FState;
public
constructor Create(AState: lua_State; ACount: Integer = -1);
{ ILuaReadParameters }
function GetCount: Integer; override;
function GetItem(Index: Integer): ILuaVariable; override;
end;
TLuaStackVariable = class(TInterfacedObject, ILuaVariable)
private
FState: lua_State;
FIndex: Integer;
FTable: ILuaTable;
protected
property State: lua_State read FState;
property Index: Integer read FIndex;
public
constructor Create(AState: lua_State; AIndex: Integer);
{ ILuaVariable }
function GetDataType: TLuaDataType;
function GetVariableType: TLuaVariableType;
function GetAsBoolean: Boolean;
function GetAsInteger: Integer;
function GetAsNumber: Double;
function GetAsUserData: Pointer;
function GetAsString: string;
function GetAsTable: ILuaTable;
function GetAsFunction: ILuaFunction;
function GetAsCFunction: TLuaCFunction;
procedure SetAsBoolean(ABoolean: Boolean);
procedure SetAsInteger(AInteger: Integer);
procedure SetAsNumber(ANumber: Double);
procedure SetAsUserData(AUserData: Pointer);
procedure SetAsString(AString: string);
procedure SetAsTable(ATable: ILuaTable);
end;
TLuaResultParameters = class(TCustomLuaParameters)
private
FParameters: TList<ILuaVariable>;
public
constructor Create(AState: lua_State; ACount: Integer);
destructor Destroy; override;
function GetCount: Integer; override;
function GetItem(Index: Integer): ILuaVariable; override;
end;
TLuaVariable = class(TInterfacedObject, ILuaVariable)
private
FVariableType: TLuaVariableType;
FDataType: TLuaDataType;
FBooleanValue: Boolean;
FIntegerValue: Integer;
FNumberValue: Double;
FUserDataValue: Pointer;
FStringValue: string;
FTableValue: ILuaTable;
FFunctionValue: ILuaFunction;
FCFunctionValue: TLuaCFunction;
protected
property VariableType: TLuaVariableType read FVariableType write FVariableType;
property DataType: TLuaDataType read FDataType write FDataType;
property BooleanValue: Boolean read FBooleanValue write FBooleanValue;
property IntegerValue: Integer read FIntegerValue write FIntegerValue;
property NumberValue: Double read FNumberValue write FNumberValue;
property UserDataValue: Pointer read FUserDataValue write FUserDataValue;
property StringValue: string read FStringValue write FStringValue;
property TableValue: ILuaTable read FTableValue write FTableValue;
property FunctionValue: ILuaFunction read FFunctionValue write FFunctionValue;
property CFunctionValue: TLuaCFunction read FCFunctionValue write FCFunctionValue;
public
constructor Create; overload;
constructor Create(ABoolean: Boolean); overload;
constructor Create(AInteger: Integer); overload;
constructor Create(ANumber: Double); overload;
constructor Create(AUserData: Pointer); overload;
constructor Create(const AString: string); overload;
constructor Create(ATable: ILuaTable); overload;
constructor Create(AFunction: ILuaFunction); overload;
constructor Create(AFunction: TLuaCFunction); overload;
{ ILuaParameter }
function GetVariableType: TLuaVariableType;
function GetDataType: TLuaDataType;
function GetAsBoolean: Boolean;
function GetAsInteger: Integer;
function GetAsNumber: Double;
function GetAsUserData: Pointer;
function GetAsString: string;
function GetAsTable: ILuaTable;
function GetAsFunction: ILuaFunction;
function GetAsCFunction: TLuaCFunction;
procedure SetAsBoolean(ABoolean: Boolean);
procedure SetAsInteger(AInteger: Integer);
procedure SetAsNumber(ANumber: Double);
procedure SetAsUserData(AUserData: Pointer);
procedure SetAsString(AString: string);
procedure SetAsTable(ATable: ILuaTable);
end;
TLuaCachedVariable = class(TLuaVariable)
public
constructor Create(AState: lua_State; AIndex: Integer);
end;
TLuaCachedTable = class(TLuaTable)
public
constructor Create(AState: lua_State; AIndex: Integer);
end;
TLuaStackWriteParameters = class(TInterfacedObject, ILuaWriteParameters)
private
FState: lua_State;
FCount: Integer;
protected
procedure Pushed;
property State: lua_State read FState;
public
constructor Create(AState: lua_State);
{ ILuaWriteParameters }
function GetCount: Integer;
procedure Push(ABoolean: Boolean); overload;
procedure Push(AInteger: Integer); overload;
procedure Push(ANumber: Double); overload;
procedure Push(AUserData: Pointer); overload;
procedure Push(const AString: string); overload;
procedure Push(ATable: ILuaTable); overload;
end;
TLuaFunction = class(TInterfacedObject, ILuaFunction)
private
FState: lua_State;
FRegistryKey: string;
protected
property State: lua_State read FState;
property RegistryKey: string read FRegistryKey;
public
constructor Create(AState: lua_State; AIndex: Integer);
destructor Destroy; override;
{ ILuaFunction }
function Call(): ILuaReadParameters; overload;
function Call(AParameters: array of const): ILuaReadParameters; overload;
function Call(AParameters: ILuaReadParameters): ILuaReadParameters; overload;
end;
TLuaContext = class(TInterfacedObject, ILuaContext)
private
FParameters: ILuaReadParameters;
FResult: ILuaWriteParameters;
public
constructor Create(AState: lua_State);
{ ILuaContext }
function GetParameters: ILuaReadParameters;
function GetResult: ILuaWriteParameters;
end;
{ TLuaHelpers }
class function TLuaHelpers.GetLuaDataType(AType: Integer): TLuaDataType;
begin
case AType of
LUA_TNIL: Result := LuaNil;
LUA_TNUMBER: Result := LuaNumber;
LUA_TBOOLEAN: Result := LuaBoolean;
LUA_TSTRING: Result := LuaString;
LUA_TTABLE: Result := LuaTable;
LUA_TFUNCTION: Result := LuaFunction;
LUA_TUSERDATA: Result := LuaUserData;
LUA_TTHREAD: Result := LuaThread;
LUA_TLIGHTUSERDATA: Result := LuaLightUserData;
else
Result := LuaNone;
end;
end;
class function TLuaHelpers.GetLuaVariableType(ADataType: TLuaDataType): TLuaVariableType;
begin
case ADataType of
LuaNumber: Result := VariableNumber;
LuaBoolean: Result := VariableBoolean;
LuaString: Result := VariableString;
LuaTable: Result := VariableTable;
LuaFunction: Result := VariableFunction;
LuaUserData: Result := VariableUserData;
LuaLightUserData: Result := VariableUserData;
LuaCClosure: Result := VariableCFunction;
else
Result := VariableNone;
end;
end;
class function TLuaHelpers.CreateParameters(AParameters: array of const): ILuaReadParameters;
var
parameterIndex: Integer;
parameter: TVarRec;
resultParameters: TLuaParameters;
table: ILuaTable;
begin
resultParameters := TLuaParameters.Create;
for parameterIndex := Low(AParameters) to High(AParameters) do
begin
parameter := AParameters[parameterIndex];
case parameter.VType of
vtInteger: resultParameters.Push(parameter.VInteger);
vtBoolean: resultParameters.Push(parameter.VBoolean);
vtChar: resultParameters.Push(string(parameter.VChar));
vtExtended: resultParameters.Push(parameter.VExtended^);
vtString: resultParameters.Push(string(parameter.VString));
vtPointer: resultParameters.Push(parameter.VPointer);
vtPChar: resultParameters.Push(string(parameter.VPChar));
vtObject:
if parameter.VObject is TLuaTable then
resultParameters.Push(TLuaTable(parameter.VObject));
vtWideChar: resultParameters.Push(string(parameter.VWideChar));
vtPWideChar: resultParameters.Push(string(parameter.VPWideChar));
vtAnsiString: resultParameters.Push(string(PAnsiChar(parameter.VAnsiString)));
vtCurrency: resultParameters.Push(parameter.VCurrency^);
// vtVariant: resultParameters.Push(parameter.VVariant);
vtInterface:
if Supports(IInterface(parameter.VInterface), ILuaTable, table) then
resultParameters.Push(table);
vtWideString: resultParameters.Push(string(PWideString(parameter.VWideString)));
vtInt64: resultParameters.Push(parameter.VInt64^);
vtUnicodeString: resultParameters.Push(string(PUnicodeString(parameter.VUnicodeString)));
else
raise ELuaUnsupportedParameterException.CreateFmt('Parameter type %d not supported (index: %d)', [parameter.VType, parameterIndex]);
end;
end;
Result := resultParameters;
end;
class procedure TLuaHelpers.PushVariable(AState: lua_State; AVariable: ILuaVariable);
begin
PushVariable(AState, AVariable, AVariable.VariableType);
end;
class procedure TLuaHelpers.PushVariable(AState: lua_State; AVariable: ILuaVariable; AVariableType: TLuaVariableType);
begin
case AVariableType of
VariableNone: lua_pushnil(AState);
VariableBoolean: lua_pushboolean(AState, IfThen(AVariable.AsBoolean, 1, 0));
VariableInteger: lua_pushinteger(AState, AVariable.AsInteger);
VariableNumber: lua_pushnumber(AState, AVariable.AsNumber);
VariableUserData:
if AVariable.AsUserData = nil then
lua_pushnil(AState)
else
lua_pushlightuserdata(AState, AVariable.AsUserData);
VariableString: PushString(AState, AVariable.AsString);
VariableTable: PushTable(AState, AVariable.AsTable);
VariableCFunction: PushCFunction(AState, AVariable.AsCFunction);
else
raise ELuaUnsupportedVariableException.CreateFmt('Variable type not supported: %d', [Ord(AVariableType)]);
end;
end;
class procedure TLuaHelpers.PushString(AState: lua_State; const AValue: string);
var
stringValue: PAnsiChar;
begin
stringValue := AllocLuaString(AValue);
try
lua_pushlstring(AState, stringValue, Length(AValue));
finally
FreeLuaString(stringValue);
end;
end;
class procedure TLuaHelpers.PushTable(AState: lua_State; ATable: ILuaTable);
var
pair: TLuaKeyValuePair;
begin
lua_newtable(AState);
for pair in ATable do
begin
PushVariable(AState, pair.Key);
PushVariable(AState, pair.Value);
lua_settable(AState, -3);
end;
end;
function LuaWrapperClosure(L: lua_State): Integer; cdecl;
var
method: TMethod;
closure: TLuaCFunction absolute method;
context: ILuaContext;
begin
method.Code := lua_touserdata(L, lua_upvalueindex(1));
method.Data := lua_touserdata(L, lua_upvalueindex(2));
context := TLuaContext.Create(L);
try
closure(context);
Result := context.Result.Count;
except
on E:Exception do
Result := luaL_error(L, PAnsiChar(AnsiString(E.Message)), nil);
end;
end;
class procedure TLuaHelpers.PushCFunction(AState: lua_State; AClosure: TLuaCFunction);
var
method: TMethod absolute AClosure;
begin
{ Assume the reference to AClosure is being kept alive by the caller for as long
as it's needed, for example by the ILuaVariable that contains it. That way we
don't need to keep our own reference, as we can't determine when it's ok to go out
of scope, which would result in memory increase on every use. }
lua_pushlightuserdata(AState, method.Code);
lua_pushlightuserdata(AState, method.Data);
lua_pushcclosure(AState, @LuaWrapperClosure, 2);
end;
// Casting strings directly to PAnsiChar (via AnsiString) causes corruption
// with table values, at least in Delphi XE2. Can't really explain why, seems
// the input string goes out of scope, so let's just go through the motions
// to create a copy and be safe.
class function TLuaHelpers.AllocLuaString(const AValue: string): PAnsiChar;
begin
if Length(AValue) > 0 then
begin
GetMem(Result, Length(AValue) + 1);
StrPCopy(Result, AnsiString(AValue));
end else
Result := nil;
end;
class procedure TLuaHelpers.FreeLuaString(AValue: PAnsiChar);
begin
FreeMem(AValue);
end;
// If someone cares to reproduce this issue and optimize the code, use these
// two and the TableLuaFunction test should fail with a corrupted value
// (#11#0#0#0#11#0#0#0#11#0#0#0#11#0#0#0#11).
(*
class function TLuaHelpers.AllocLuaString(const AValue: string): PAnsiChar;
begin
if Length(AValue) > 0 then
Result := PAnsiChar(AnsiString(AValue))
else
Result := nil;
end;
class procedure TLuaHelpers.FreeLuaString(AValue: PAnsiChar);
begin
end;
*)
class procedure TLuaHelpers.RaiseLastLuaError(AState: lua_State);
var
errorMessage: string;
begin
errorMessage := LuaToString(AState, -1);
lua_pop(AState, 1);
raise ELuaException.Create(errorMessage);
end;
class function TLuaHelpers.LuaToString(AState: lua_State; AIndex: Integer): string;
var
len: NativeUint;
value: PAnsiChar;
stringValue: RawByteString;
begin
value := lua_tolstring(AState, AIndex, @len);
SetString(stringValue, value, len);
Result := string(stringValue);
end;
class function TLuaHelpers.CallFunction(AState: lua_State; AParameters: ILuaReadParameters): ILuaReadParameters;
var
stackIndex: Integer;
parameterCount: Integer;
parameter: ILuaVariable;
begin
{ Assumption: the function to call is the top item on the stack }
stackIndex := Pred(lua_gettop(AState));
parameterCount := 0;
if Assigned(AParameters) then
begin
parameterCount := AParameters.Count;
for parameter in AParameters do
PushVariable(AState, parameter);
end;
if lua_pcall(AState, parameterCount, LUA_MULTRET, 0) <> 0 then
RaiseLastLuaError(AState);
Result := TLuaResultParameters.Create(AState, lua_gettop(AState) - stackIndex);
end;
class function TLuaHelpers.NewRegistryKey: string;
begin
// This could be incremented on a per-State basis, but this'll do for now.
Result := Format('DelphiLuaWrapper_%d', [TInterlocked.Increment(RegistryKeyCounter)]);
end;
{ TLuaImplicitVariable }
class operator TLuaImplicitVariable.Implicit(AValue: ILuaVariable): TLuaImplicitVariable;
begin
Result.Variable := AValue;
end;
class operator TLuaImplicitVariable.Implicit(AValue: Boolean): TLuaImplicitVariable;
begin
Result.Variable := TLuaVariable.Create(AValue);
end;
class operator TLuaImplicitVariable.Implicit(AValue: Integer): TLuaImplicitVariable;
begin
Result.Variable := TLuaVariable.Create(AValue);
end;
class operator TLuaImplicitVariable.Implicit(AValue: Double): TLuaImplicitVariable;
begin
Result.Variable := TLuaVariable.Create(AValue);
end;
class operator TLuaImplicitVariable.Implicit(AValue: Pointer): TLuaImplicitVariable;
begin
Result.Variable := TLuaVariable.Create(AValue);
end;
class operator TLuaImplicitVariable.Implicit(const AValue: string): TLuaImplicitVariable;
begin
Result.Variable := TLuaVariable.Create(AValue);
end;
class operator TLuaImplicitVariable.Implicit(AValue: ILuaTable): TLuaImplicitVariable;
begin
Result.Variable := TLuaVariable.Create(AValue);
end;
class operator TLuaImplicitVariable.Implicit(AValue: TLuaCFunction): TLuaImplicitVariable;
begin
Result.Variable := TLuaVariable.Create(AValue);
end;
class operator TLuaImplicitVariable.Implicit(AValue: TLuaImplicitVariable): ILuaVariable;
begin
Result := AValue.Variable;
end;
class operator TLuaImplicitVariable.Implicit(AValue: TLuaImplicitVariable): Boolean;
begin
Result := AValue.Variable.AsBoolean;
end;