Repository navigation
Expand file tree
/
Copy pathLuaClasses.pas
More file actions
856 lines (724 loc) · 24.7 KB
/
Copy pathLuaClasses.pas
File metadata and controls
856 lines (724 loc) · 24.7 KB
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
unit LuaClasses;
{**
* This file is part of the "Tyro"
*
* @license MIT
*
* @author Zaher Dirkey
*
*
* TODO http://docwiki.embarcadero.com/RADStudio/Rio/en/Supporting_Properties_and_Methods_in_Custom_Variants
*}
{$ifdef fpc}
{$mode delphi}
{$else}
{$RTTI EXPLICIT METHODS([vcPublic, vcProtected, vcPublished])}
{$endif}
{$H+}{$M+}
{$MINENUMSIZE 4}
interface
uses
Classes, SysUtils, Rtti, SyncObjs,
LuaAPI;
type
{ Re-exported Lua types so callers can use LuaClasses without depending on LuaAPI directly }
//Plua_State = LuaAPI.Plua_State;
lua_CFunction = LuaAPI.lua_CFunction;
lua_Debug = LuaAPI.lua_Debug;
TLuaStatus = (luaNone, luaReady, luaRunning, luaTerminated);
{ TLuaObject }
TLuaObject = class abstract(TObject)
private
protected
function Setter(L: PLua_State): integer; virtual;
function Getter(L: PLua_State): integer; virtual;
procedure EnumMethods;
procedure RegisterMethod(const AName: string; AParams: TStringList); virtual;
public
function __setter(L: PLua_State): integer; cdecl;
function __getter(L: PLua_State): integer; cdecl;
public
//Name: string;
procedure Register; virtual;
constructor Create;
published
end;
TLua = record
private
FVersion: Double;
function GetStatus: TLuaStatus;
procedure SetStatus(AStatus: TLuaStatus);
public
State: Plua_State;
procedure Init(SafeMode: Boolean = True; HookCount: Integer = 1000);
procedure Close;
procedure SetReady;
procedure SetTerminated;
property Version: Double read FVersion;
property Status: TLuaStatus read GetStatus write SetStatus;
end;
{ TLuaParam }
TLuaMethod = function(L: Plua_State): integer of object cdecl;
TLuaFunction = lua_CFunction;
{ TLuaHelper }
{ Every string in TLuaHelper is declared as plain `string` so the same code
compiles for both FPC and Delphi: `string` is AnsiString under FPC and
UnicodeString under Delphi. The UTF8String conversion happens once, at
each LuaApi call site, instead of leaking UTF8String through the public
API and forcing every caller to convert by hand. }
TLuaHelper = record helper for lua_State
private
function GetArgsCount: Integer;
public
property ArgsCount: Integer read GetArgsCount;
function FunctionExists(const FunctionName: string): Boolean;
procedure RegisterGlobal(const Name: string; LuaFunction: TLuaFunction); overload;
procedure RegisterGlobal(const Name: string; Method: TLuaMethod); overload;
procedure RegisterGlobal(const Name: string; Value: string); overload;
procedure RegisterGlobal(const Name: string; Value: Integer); overload;
procedure RegisterGlobal(const Name: string; Value: Double); overload;
procedure Register(const Name: string; Value: string); overload;
procedure Register(const Name: string; Value: Integer); overload;
procedure Register(const Name: string; Value: Double); overload;
{
RegisterMethod_1 gives the C callback access to the actual Lua table, not just the underlying C/Delphi object.
RegisterMethod_2 only knows the C object.
Here is why a callback needs the Lua table:
The benefit is that RegisterMethod_1 gives the C callback access to the actual Lua table, not just the underlying C/Delphi object.
RegisterMethod_2 only knows the C object. RegisterMethod_1 knows both.
* Lua-side state: To read or write fields stored directly in the Lua table (e.g., custom properties not exposed to C).
* Wrapper identity: If multiple Lua tables wrap the same underlying C object, the callback knows exactly which Lua table to interact with.
* C-to-Lua callbacks: If the method registers an event (like "on animation finish"), the C code needs the Lua table reference to call back into Lua later.
* Garbage Collection: Holding the table as an upvalue prevents the Lua table from being garbage-collected while the closure exists.
Summary: Use RegisterMethod_2 for pure C-side logic. Use RegisterMethod_1 when the method needs to interact with the Lua-side wrapper.
}
//RegisterMethod_1
procedure Register(const Name: string; Method: TLuaMethod); overload;
procedure RegisterMethod(const Name: string; Method: TLuaMethod); overload;
//
//RegisterMethod_2
procedure Register(const Table, Name: string; AObject:TObject; Method: TLuaMethod; AToMeta: Boolean = False); overload;
procedure Register(const Name: string; LuaFunction: TLuaFunction); overload;
//Must be last one after object fields
procedure Register(const Table: string; AObject: TLuaObject); overload;
//Set a method into the table that is just below the top of the stack (no self table injected); used for metamethods
procedure RegisterMeta(const Name: string; Method: TLuaMethod); overload;
//Low-level stack access used by the C callbacks; keeps the callers independent of LuaAPI
function ToString(Index: Integer): string; overload;
function ToString(Index: Integer; const Name: string): string; overload;
function PopString: string;
function ToNumber(Index: Integer): Double;
function ToBoolean(Index: Integer): Boolean;
function ToInteger(Index: Integer): lua_Integer; overload;
function ToInteger(Index: Integer; const Name: string): Integer; overload;
function PopInteger: Integer; overload;
function IsInteger(Index: Integer): Boolean;
function IsNumber(Index: Integer): Boolean;
function IsNil(Index: Integer): Boolean;
function IsString(Index: Integer): Boolean;
function IsTable(Index: Integer): Boolean;
procedure PushBoolean(Value: Boolean);
procedure PushInteger(Value: lua_Integer);
procedure PushNumber(Value: Double);
procedure PushString(const Value: string);
procedure PushNil;
procedure PushValue(Index: Integer);
procedure Pop(Count: Integer = 1);
procedure NewTable;
procedure BeginTable;
procedure SetMetaTable(Index: Integer);
procedure EndTableGlobal(Table: string; AObject: TLuaObject = nil);
procedure GetField(Index: Integer; const Name: string);
procedure SetField(Index: Integer; const Name: string);
procedure Remove(Index: Integer);
function GetStack(Level: Integer; var AInfo: lua_Debug): Boolean;
function GetInfo(const What: string; var AInfo: lua_Debug): Boolean;
procedure RegisterTable(Table: string);
//Run
function LoadString(const Script: string): Integer;
function Run(Ref: Integer; out Output: string): Boolean; overload;
function RunString(Script: string; out Output: string): Boolean;
end;
implementation
function lua_table_method_callback(L: Plua_State): integer; cdecl;
var
Method: TMethod;
begin
Method.Data := lua_topointer(L, lua_upvalueindex(1));
Method.Code := lua_topointer(L, lua_upvalueindex(2));
if Method.Data = nil then
raise Exception.Create('Lua: cannot execute object method!');
Result := TLuaMethod(Method)(L);
end;
procedure lua_register_global_method(L: Plua_State; Name: string; Method: TLuaMethod);
begin
lua_pushlightuserdata(L, TMethod(method).Data);
lua_pushlightuserdata(L, TMethod(method).Code);
lua_pushcclosure(L, @lua_table_method_callback, 2);
lua_setglobal(L, PUTF8Char(UTF8String(Name)));
end;
procedure lua_push_method(L: Plua_State; Name: string; method: TLuaMethod);
begin
lua_pushlightuserdata(L, TMethod(method).Data);
lua_pushlightuserdata(L, TMethod(method).Code);
lua_pushcclosure(L, @lua_table_method_callback, 2);
lua_setfield(L, -2, PUTF8Char(UTF8String(Name)));
end;
//Fetch the global table by name, pushing it. When the global is absent or holds
//a non-table value (nil/string/number), the old value is discarded and a fresh
//table is pushed instead. Returns True when the table was just created.
//The stack is balanced on exit (exactly the table, nothing else), so callers
//cannot leak the nil that lua_getglobal pushes for a missing global.
function lua_get_or_create_table(L: Plua_State; const table: string): Boolean;
begin
lua_getglobal(L, PUTF8Char(UTF8String(table)));
if lua_type(L, -1) = LUA_TTABLE then
Result := False
else
begin
lua_pop(L, 1); //discard the non-table value (or nil)
lua_newtable(L);
Result := True;
end;
end;
procedure lua_register_table(L: Plua_State; table: string);
begin
//table
lua_newtable(L);
//metatable
lua_newtable(L); //Yes again
lua_setmetatable(L, -2);
//end metatable
lua_setglobal(L, PUTF8Char(UTF8String(table))); //set table name
//end table
end;
procedure lua_register_table_index(L: Plua_State; table: string; obj: TLuaObject);
begin
//table
lua_get_or_create_table(L, table); //push the table
//metatable
lua_newtable(L);
lua_push_method(L, '__index', obj.__getter);
lua_push_method(L, '__newindex', obj.__setter);
lua_setmetatable(L, -2);
//end metatable
lua_setglobal(L, PUTF8Char(UTF8String(table))); //set table name; pops the table
end;
procedure lua_register_table_method(L: Plua_State; table: string; Name: string; obj: TObject; method: TLuaMethod; AToMeta: Boolean);
begin
lua_get_or_create_table(L, table); //push the table
if AToMeta then
begin
if lua_getmetatable(L, -1) = 0 then
begin
lua_pop(L, 1);
lua_newtable(L);
lua_setmetatable(L, -2);
lua_getmetatable(L, -1);
end;
end;
lua_push_method(L, Name, method);
if AToMeta then
lua_pop(L, 2) //pop table and metatable from stack
else
lua_setglobal(L, PUTF8Char(UTF8String(table))); //set table name; pops the table
end;
procedure lua_register_table_value(L: Plua_State; table, Name: string; Value: integer);
begin
lua_get_or_create_table(L, table); //push the table
lua_pushinteger(L, Value);
lua_setfield(L, -2, PUTF8Char(UTF8String(Name)));
lua_setglobal(L, PUTF8Char(UTF8String(table))); //set table name; pops the table
end;
procedure lua_register_string(L: Plua_State; Name: string; Value: string);
begin
lua_pushstring(L, PUTF8Char(UTF8String(Value)));
lua_setfield(L, -2, PUTF8Char(UTF8String(Name)));
end;
procedure lua_register_integer(L: Plua_State; Name: string; Value: integer);
begin
lua_pushinteger(L, Value);
lua_setfield(L, -2, PUTF8Char(UTF8String(Name)));
end;
procedure lua_register_number(L: Plua_State; Name: string; Value: Double);
begin
lua_pushnumber(L, Value);
lua_setfield(L, -2, PUTF8Char(UTF8String(Name)));
end;
procedure lua_register_global_integer(L: Plua_State; Name: string; Value: integer);
begin
lua_pushinteger(L, Value);
lua_setglobal(L, PUTF8Char(UTF8String(Name)));
end;
procedure lua_register_global_number(L: Plua_State; Name: string; Value: double);
begin
lua_pushnumber(L, Value);
lua_setglobal(L, PUTF8Char(UTF8String(Name)));
end;
procedure lua_register_global_string(L: Plua_State; Name: string; Value: string);
begin
lua_pushstring(L, PUTF8Char(UTF8String(Value)));
lua_setglobal(L, PUTF8Char(UTF8String(Name)));
end;
function LuaAlloc({%H-}ud, ptr: Pointer; {%H-}osize, nsize: size_t): Pointer; cdecl;
begin
//C realloc contract used by Lua:
// ptr=nil, nsize>0 -> allocate (ReallocMem(nil, n) allocates)
// ptr<>nil, nsize>0 -> grow/shrink in place (may move)
// nsize=0 -> free and return nil (Lua never keeps a result on free)
//Any failure signals Lua by returning nil.
Result := nil;
try
if nsize = 0 then
begin
if ptr <> nil then
FreeMem(ptr);
Exit;
end;
Result := ptr;
ReallocMem(Result, nsize);
except
Result := nil;
end;
end;
type
TLuaExtraSpace = class(TObject)
public
Status: TLuaStatus;
end;
PLuaExtraSpace = ^TLuaExtraSpace;
function LuaStatusPointer(L: Plua_State): TLuaExtraSpace; inline;
begin
Result := TLuaExtraSpace(PPointer(lua_getextraspace(L))^);
end;
procedure HookCallback(L: Plua_State; ar: Plua_Debug); cdecl;
var
AExtraSpace: TLuaExtraSpace;
begin
AExtraSpace := LuaStatusPointer(L);
if (AExtraSpace <> nil) and
(TInterlocked.Add(Integer(AExtraSpace.Status), 0) = Ord(luaTerminated)) then
luaL_error(L, PUTF8Char('Terminated by user!'));
end;
{ TLuaObject }
function TLuaObject.__setter(L: PLua_State): integer; cdecl;
begin
Result := Setter(L);
end;
function TLuaObject.__getter(L: PLua_State): integer; cdecl;
begin
Result := Getter(L);
end;
function TLuaObject.Setter(L: PLua_State): integer;
begin
Result := 0;
end;
function TLuaObject.Getter(L: PLua_State): integer;
begin
Result := 0;
end;
procedure TLuaObject.EnumMethods;
var
aContext: TRttiContext;
aMethods: TArray<TRttiMethod>;
procedure EnumParams(aMethod: TRttiMethod);
var
//aType: TRttiType;
aParams: TStringList;
aMethodParameter: TRttiParameter;
aMethodParameters: TArray<TRttiParameter>;
begin
aParams := TStringList.Create;
try
aParams.NameValueSeparator := ':';
aMethodParameters := aMethod.GetParameters;
for aMethodParameter in aMethodParameters do
begin
aParams.AddPair(aMethodParameter.Name, aMethodParameter.ParamType.Name);
end;
RegisterMethod(aMethod.Name, aParams);
finally
aParams.Free;
end;
end;
var
aType: TRttiType;
aMethod: TRttiMethod;
begin
aContext := TRttiContext.Create;
try
aType := aContext.GetType(ClassType);
if aType <> nil then
begin
aMethods := aType.GetMethods;
for aMethod in aMethods do
EnumParams(aMethod);
end;
finally
aContext.Free;
end;
end;
procedure TLuaObject.RegisterMethod(const AName: string; AParams: TStringList);
begin
end;
procedure TLuaObject.Register;
begin
end;
constructor TLuaObject.Create;
begin
inherited Create;
// EnumMethods; not yet
end;
{ TLua }
function TLua.GetStatus: TLuaStatus;
var
AExtraSpace: TLuaExtraSpace;
begin
if State = nil then
Exit(luaNone);
AExtraSpace := LuaStatusPointer(State);
if AExtraSpace = nil then
Exit(luaNone);
Result := TLuaStatus(TInterlocked.Add(Integer(AExtraSpace.Status), 0));
end;
procedure TLua.SetStatus(AStatus: TLuaStatus);
var
AExtraSpace: TLuaExtraSpace;
begin
if State = nil then
Exit;
AExtraSpace := LuaStatusPointer(State);
if AExtraSpace <> nil then
TInterlocked.Exchange(Integer(AExtraSpace.Status), Ord(AStatus));
end;
procedure TLua.SetReady;
begin
SetStatus(luaReady);
end;
procedure TLua.SetTerminated;
begin
SetStatus(luaTerminated);
end;
procedure TLua.Init(SafeMode: Boolean; HookCount: Integer);
var
AExtraSpace: TLuaExtraSpace;
begin
//Both in-tree call sites keep the record in a zeroed class field, but a
//record that already owns a Lua state must release it first: Self :=
//Default(TLua) below would otherwise just drop the pointer (leaking the
//state, its extraspace status and every allocate).
if State <> nil then
Close;
Self := Default(TLua);
AExtraSpace := TLuaExtraSpace.Create;
AExtraSpace.Status := luaNone;
State := lua_newstate(@LuaAlloc, nil, 0);
if State = nil then
begin
AExtraSpace.Free;
raise Exception.Create('Unable to create Lua state');
end;
PPointer(lua_getextraspace(State))^ := AExtraSpace;
FVersion := lua_version(State);
//All libraries
luaL_openselectedlibs(State, -1, 0);
//Remove direct file system access through io and os: openfile() is the only
//way a script gets at a file, and it refuses anything outside the workspace
lua_pushnil(State);
if SafeMode then
begin
lua_setglobal(State, PUTF8Char('io'));
lua_pushnil(State);
lua_setglobal(State, PUTF8Char('os'));
//Close the loaders that would walk around that guard: dofile and loadfile run
//any Lua file on disk and require can pull in a native module. require itself
//is put back by the script class with a loader that only reads inside the
//workspace and the app folder; see TLuaScript.Require_func.
lua_pushnil(State);
lua_setglobal(State, PUTF8Char('dofile'));
lua_pushnil(State);
lua_setglobal(State, PUTF8Char('loadfile'));
lua_pushnil(State);
lua_setglobal(State, PUTF8Char('require'));
//lua_pushnil(State);
//lua_setglobal(State, PUTF8Char('package'));
//lua_pushnil(State);
//debug goes too, since it reaches the registry and the call stack.
//lua_setglobal(State, PUTF8Char('debug'));
end;
SetReady;
if HookCount > 0 then
lua_sethook(State, @HookCallback, LUA_MASKCOUNT, HookCount);
end;
procedure TLua.Close;
var
AExtraSpace: TLuaExtraSpace;
begin
if State = nil then
Exit;
AExtraSpace := LuaStatusPointer(State);
lua_close(State);
State := nil;
if AExtraSpace <> nil then
AExtraSpace.Free;
end;
{ TLuaHelper }
function TLuaHelper.GetArgsCount: Integer;
begin
Result := lua_gettop(@Self);
end;
procedure TLuaHelper.RegisterGlobal(const Name: string; Method: TLuaMethod);
begin
lua_register_global_method(@Self, Name, Method);
end;
procedure TLuaHelper.RegisterGlobal(const Name: string; LuaFunction: TLuaFunction);
begin
lua_reg_global_function(@Self, PUTF8Char(UTF8String(Name)), LuaFunction);
end;
procedure TLuaHelper.RegisterGlobal(const Name: string; Value: string);
begin
lua_register_global_string(@Self, Name, Value);
end;
procedure TLuaHelper.RegisterGlobal(const Name: string; Value: Integer);
begin
lua_register_global_integer(@Self, Name, Value);
end;
procedure TLuaHelper.RegisterGlobal(const Name: string; Value: Double);
begin
lua_register_global_number(@Self, Name, Value);
end;
procedure TLuaHelper.Register(const Name: string; Value: string);
begin
lua_register_string(@Self, Name, Value);
end;
procedure TLuaHelper.Register(const Name: string; Value: Integer);
begin
lua_register_integer(@Self, Name, Value);
end;
procedure TLuaHelper.Register(const Name: string; Value: Double);
begin
lua_register_number(@Self, Name, Value);
end;
function lua_method_callback(L: Plua_State): integer; cdecl;
var
Method: TMethod;
begin
Method.Data := lua_topointer(L, lua_upvalueindex(1));
Method.Code := lua_topointer(L, lua_upvalueindex(2));
if Method.Data = nil then
raise Exception.Create('Lua: cannot execute object method!');
Result := TLuaMethod(Method)(L);
end;
function lua_method_callback_index(L: Plua_State): integer; cdecl;
var
Method: TMethod;
begin
Method.Data := lua_topointer(L, lua_upvalueindex(1));
Method.code := lua_topointer(L, lua_upvalueindex(2));
lua_pushvalue(L, lua_upvalueindex(3));
lua_insert(L, 1);
if Method.Data = nil then
raise Exception.Create('Lua: cannot execute object method!');
Result := TLuaMethod(Method)(L);
end;
procedure TLuaHelper.Register(const Name: string; Method: TLuaMethod);
begin
lua_pushlightuserdata(@Self, TMethod(method).Data);
lua_pushlightuserdata(@Self, TMethod(method).Code);
lua_pushvalue(@Self, -3);
lua_pushcclosure(@Self, @lua_method_callback_index, 3);
lua_setfield(@Self, -2, PUTF8Char(UTF8String(Name)));
end;
procedure TLuaHelper.RegisterMethod(const Name: string; Method: TLuaMethod);
begin
lua_pushlightuserdata(@Self, TMethod(method).Data);
lua_pushlightuserdata(@Self, TMethod(method).Code);
lua_pushcclosure(@Self, @lua_method_callback, 2);
lua_setfield(@Self, -2, PUTF8Char(UTF8String(Name)));
end;
procedure TLuaHelper.Register(const Table, Name: string; AObject: TObject; Method: TLuaMethod; AToMeta: Boolean);
begin
lua_register_table_method(@Self, Table, Name, AObject, Method, AToMeta);
end;
procedure TLuaHelper.RegisterMeta(const Name: string; Method: TLuaMethod);
begin
lua_push_method(@Self, Name, Method);
end;
function TLuaHelper.ToString(Index: Integer): string;
begin
//lua_tostring returns the PChar straight from Lua, so it is already UTF-8
Result := string(UTF8String(lua_tostring(@Self, Index)));
end;
function TLuaHelper.ToInteger(Index: Integer): lua_Integer;
begin
Result := lua_tointeger(@Self, Index);
end;
function TLuaHelper.ToNumber(Index: Integer): Double;
begin
Result := lua_tonumber(@Self, Index);
end;
function TLuaHelper.ToString(Index: Integer; const Name: string): string;
begin
GetField(Index, Name);
Result := PopString;
end;
function TLuaHelper.ToBoolean(Index: Integer): Boolean;
begin
Result := lua_toboolean(@Self, Index);
end;
function TLuaHelper.ToInteger(Index: Integer; const Name: string): Integer;
begin
GetField(Index, Name);
Result := PopInteger;
end;
function TLuaHelper.IsInteger(Index: Integer): Boolean;
begin
Result := lua_isinteger(@Self, Index);
end;
function TLuaHelper.IsNil(Index: Integer): Boolean;
begin
Result := lua_isnil(@Self, Index);
end;
function TLuaHelper.IsNumber(Index: Integer): Boolean;
begin
Result := lua_isnumber(@Self, Index);
end;
function TLuaHelper.IsString(Index: Integer): Boolean;
begin
Result := lua_isstring(@Self, Index);
end;
function TLuaHelper.IsTable(Index: Integer): Boolean;
begin
Result := lua_istable(@Self, Index);
end;
procedure TLuaHelper.PushBoolean(Value: Boolean);
begin
lua_pushboolean(@Self, Value);
end;
procedure TLuaHelper.PushInteger(Value: lua_Integer);
begin
lua_pushinteger(@Self, Value);
end;
procedure TLuaHelper.PushNumber(Value: Double);
begin
lua_pushnumber(@Self, Value);
end;
procedure TLuaHelper.PushString(const Value: string);
begin
lua_pushstring(@Self, PUTF8Char(UTF8String(Value)));
end;
procedure TLuaHelper.PushNil;
begin
lua_pushnil(@Self);
end;
procedure TLuaHelper.PushValue(Index: Integer);
begin
lua_pushvalue(@Self, Index);
end;
procedure TLuaHelper.Pop(Count: Integer);
begin
lua_pop(@Self, Count);
end;
function TLuaHelper.PopInteger: Integer;
begin
Result := ToInteger(-1);
Pop(1);
end;
function TLuaHelper.PopString: string;
begin
Result := ToString(-1);
Pop(1);
end;
procedure TLuaHelper.NewTable;
begin
lua_newtable(@Self);
end;
procedure TLuaHelper.GetField(Index: Integer; const Name: string);
begin
lua_getfield(@Self, Index, PUTF8Char(UTF8String(Name)));
end;
procedure TLuaHelper.SetField(Index: Integer; const Name: string);
begin
lua_setfield(@Self, Index, PUTF8Char(UTF8String(Name)));
end;
procedure TLuaHelper.SetMetaTable(Index: Integer);
begin
lua_setmetatable(@Self, Index);
end;
procedure TLuaHelper.Remove(Index: Integer);
begin
lua_remove(@Self, Index);
end;
function TLuaHelper.GetStack(Level: Integer; var AInfo: lua_Debug): Boolean;
begin
Result := lua_getstack(@Self, Level, AInfo) > 0;
end;
function TLuaHelper.GetInfo(const What: string; var AInfo: lua_Debug): Boolean;
begin
Result := lua_getinfo(@Self, PUTF8Char(UTF8String(What)), AInfo) > 0;
end;
procedure TLuaHelper.Register(const Name: string; LuaFunction: TLuaFunction);
begin
lua_reg_function(@Self, PUTF8Char(UTF8String(Name)), LuaFunction);
end;
procedure TLuaHelper.Register(const Table: string; AObject: TLuaObject);
begin
lua_register_table_index(@Self, Table, AObject); //Should be last one for window
end;
procedure TLuaHelper.RegisterTable(Table: string);
begin
lua_register_table(@Self, Table);
end;
function TLuaHelper.LoadString(const Script: string): Integer; // returns registry ref
begin
if luaL_loadstring(@Self, PUTF8Char(UTF8String(Script))) <> LUA_OK then
begin
lua_pop(@Self, 1);
Result := LUA_NOREF;
end
else
Result := luaL_ref(@Self, LUA_REGISTRYINDEX);
end;
function TLuaHelper.Run(Ref: Integer; out Output: string): Boolean;
begin
lua_rawgeti(@Self, LUA_REGISTRYINDEX, Ref); // push compiled chunk
Result := lua_pcall(@Self, 0, LUA_MULTRET, 0) = LUA_OK;
if Result then
Output := ''
else
begin
if lua_isstring(@Self, -1) then
//lua_tostring returns the PChar straight from Lua, so it is already UTF-8
Output := string(UTF8String(lua_tostring(@Self, -1)))
else
Output := '(unknown error)';
lua_pop(@Self, 1);
end;
end;
function TLuaHelper.RunString(Script: string; out Output: string): Boolean;
var
Ref: Integer;
begin
Ref := LoadString(Script);
if Ref <> LUA_NOREF then
Result := Run(Ref, Output)
else
Result := False;
end;
function TLuaHelper.FunctionExists(const FunctionName: string): Boolean;
begin
lua_getglobal(@Self, PUTF8Char(UTF8String(FunctionName)));
Result := lua_isfunction(@Self, -1);
lua_pop(@Self, 1);
end;
procedure TLuaHelper.BeginTable;
begin
lua_newtable(@Self);
end;
procedure TLuaHelper.EndTableGlobal(Table: string; AObject: TLuaObject);
begin
lua_setglobal(@Self, PUTF8Char(UTF8String(Table)));
if AObject <> nil then
Register(Table, AObject); //Should be last one
end;
end.