-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathLuaUtils.pas
More file actions
1573 lines (1410 loc) · 42.6 KB
/
Copy pathLuaUtils.pas
File metadata and controls
1573 lines (1410 loc) · 42.6 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
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
//******************************************************************************
//*** LUA SCRIPT DELPHI UTILITIES ***
//*** ***
//*** (c) 2006 Jean-Fran輟is Goulet, Massimo Magnano, Kuma ***
//*** ***
//*** ***
//******************************************************************************
// File : LuaUtils.pas
//
// Description : Useful functions to work with Lua in Delphi.
//
//******************************************************************************
//** See Copyright Notice in lua.h
//Revision 1.2
// MaxM Adds :
// GetCurrentFuncName, LuaSetTablePropertyFuncs
//
//Revision 1.1
// MaxM Adds :
// LuaPCallFunction
//
//Revision 1.0
// MaxM Adds :
// LuaPushVariant
// LuaToVariant
// LuaGetTableInteger, LuaGet\SetTableTMethod
// LuaLoadBufferFromFile
// Solved Bugs : Stack problem in LuaProcessTableName
// LuaToInteger why Round?, Trunc is better
unit LuaUtils;
interface
uses
SysUtils, Classes, ComCtrls, luaconf, lua, lualib, lauxlib, Variants;
const
ERR_Script = 'Script Error : ';
type
TOnLuaStdout = procedure(S: PChar; N: Integer);
TOnLuaStdoutEx = procedure(F, S: PChar; L, N: Integer);
ELuaException = class(Exception)
Title: string;
Line: Integer;
Msg: string;
constructor Create(Title: string; Line: Integer; Msg: string);
end;
TVariantArray = array of Variant;
PVariantArray = ^TVariantArray;
function Quote(const Str: string): string;
function Dequote(const QuotedStr: string): string;
function lua_print(L: Plua_State): Integer; cdecl;
function lua_printex(L: Plua_State): Integer; cdecl;
function lua_io_write(L: Plua_State): Integer; cdecl;
function lua_io_writeex(L: Plua_State): Integer; cdecl;
function LuaToPointer(L: Plua_State; Index: Integer): Pointer;
procedure LuaPushPointer(L: Plua_State; P: Pointer);
function LuaToBoolean(L: PLua_State; Index: Integer): Boolean;
procedure LuaPushBoolean(L: PLua_State; B: Boolean);
function LuaToInteger(L: PLua_State; Index: Integer): Integer;
procedure LuaPushInteger(L: PLua_State; N: Integer);
function LuaToVariant(L: Plua_State; Index: Integer): Variant;
procedure LuaPushVariant(L: Plua_State; N: Variant);
function LuaToString(L: PLua_State; Index: Integer): string;
procedure LuaPushString(L: PLua_State; const S: string);
function LuaIncIndex(L: Plua_State; Index: Integer): Integer;
function LuaAbsIndex(L: Plua_State; Index: Integer): Integer;
procedure LuaGetTable(L: Plua_State; TableIndex: Integer; const Key: string);
function LuaGetTableBoolean(L: Plua_State; TableIndex: Integer; const Key: string): Boolean;
function LuaGetTableNumber(L: Plua_State; TableIndex: Integer; const Key: string): Double;
function LuaGetTableInteger(L: Plua_State; TableIndex: Integer; const Key: string): Integer;
function LuaGetTableString(L: Plua_State; TableIndex: Integer; const Key: string): string;
function LuaGetTableFunction(L: Plua_State; TableIndex: Integer; const Key: string): lua_CFunction;
function LuaGetTableLightUserData(L: Plua_State; TableIndex: Integer; const Key: string): Pointer;
function LuaGetTableTMethod(L: Plua_State; TableIndex: Integer; const Key: string): TMethod;
procedure LuaRawGetTable(L: Plua_State; TableIndex: Integer; const Key: string);
function LuaRawGetTableBoolean(L: Plua_State; TableIndex: Integer; const Key: string): Boolean;
function LuaRawGetTableNumber(L: Plua_State; TableIndex: Integer; const Key: string): Double;
function LuaRawGetTableString(L: Plua_State; TableIndex: Integer; const Key: string): string;
function LuaRawGetTableFunction(L: Plua_State; TableIndex: Integer; const Key: string): lua_CFunction;
function LuaRawGetTableLightUserData(L: Plua_State; TableIndex: Integer; const Key: string): Pointer;
procedure LuaSetTableValue(L: PLua_State; TableIndex: Integer; const Key: string; ValueIndex: Integer);
procedure LuaSetTableNil(L: Plua_State; TableIndex: Integer; const Key: string);
procedure LuaSetTableBoolean(L: Plua_State; TableIndex: Integer; const Key: string; B: Boolean);
procedure LuaSetTableNumber(L: Plua_State; TableIndex: Integer; const Key: string; N: Double);
procedure LuaSetTableString(L: Plua_State; TableIndex: Integer; const Key: string; S: string);
procedure LuaSetTableFunction(L: Plua_State; TableIndex: Integer; const Key: string; F: lua_CFunction);
procedure LuaSetTableLightUserData(L: Plua_State; TableIndex: Integer; const Key: string; P: Pointer);
procedure LuaSetTableTMethod(L: Plua_State; TableIndex: Integer; const Key: string; M: TMethod);
procedure LuaSetTableClear(L: Plua_State; TableIndex: Integer);
procedure LuaSetTablePropertyFuncs(L: PLua_State; TableIndex: Integer;
ReadFunc: lua_CFunction;
WriteFunc: lua_CFunction = nil);
procedure LuaRawSetTableValue(L: PLua_State; TableIndex: Integer; const Key: string; ValueIndex: Integer);
procedure LuaRawSetTableNil(L: Plua_State; TableIndex: Integer; const Key: string);
procedure LuaRawSetTableBoolean(L: Plua_State; TableIndex: Integer; const Key: string; B: Boolean);
procedure LuaRawSetTableNumber(L: Plua_State; TableIndex: Integer; const Key: string; N: Double);
procedure LuaRawSetTableString(L: Plua_State; TableIndex: Integer; const Key: string; S: string);
procedure LuaRawSetTableFunction(L: Plua_State; TableIndex: Integer; const Key: string; F: lua_CFunction);
procedure LuaRawSetTableLightUserData(L: Plua_State; TableIndex: Integer; const Key: string; P: Pointer);
procedure LuaRawSetTableClear(L: Plua_State; TableIndex: Integer);
function LuaGetMetaFunction(L: Plua_State; Index: Integer; Key: string): lua_CFunction;
procedure LuaSetMetaFunction(L: Plua_State; Index: Integer; Key: string; F: lua_CFunction);
procedure LuaShowStack(L: Plua_State; Caption: string = '');
function LuaStackToStr(L: Plua_State; Index: Integer; MaxTable: Integer = -1; SubTableMax: Integer = 99): string;
procedure LuaRegisterCustom(L: PLua_State; TableIndex: Integer; const Name: PChar; F: lua_CFunction);
procedure LuaRegister(L: Plua_State; const Name: PChar; F: lua_CFunction);
procedure LuaRegisterMetatable(L: Plua_State; const Name: PChar; F: lua_CFunction);
procedure LuaRegisterProperty(L: PLua_State; const Name: PChar; ReadFunc, WriteFunc: lua_CFunction);
procedure LuaStackToStrings(L: Plua_State; Lines: TStrings; MaxTable: Integer = -1; SubTableMax: Integer = 99);
procedure LuaLocalToStrings(L: Plua_State; Lines: TStrings; MaxTable: Integer = -1; Level: Integer = 0; SubTableMax: Integer = 99);
procedure LuaGlobalToStrings(L: PLua_State; Lines: TStrings; MaxTable: Integer = -1; SubTableMax: Integer = 99);
procedure LuaTableToStrings(L: Plua_State; Index: Integer; Lines: TStrings; MaxTable: Integer = -1; SubTableMax: Integer = 99);
procedure LuaTableToTreeView(L: Plua_State; Index: Integer; TV: TTreeView; MaxTable: Integer = -1; SubTableMax: Integer = 99);
function LuaGetIdentValue(L: Plua_State; Ident: string; MaxTable: Integer = -1): string;
procedure LuaSetIdentValue(L: Plua_State; Ident, Value: string; MaxTable: Integer = -1);
procedure LuaLoadBuffer(L: Plua_State; const Code: string; const Name: string);
procedure LuaLoadBufferFromFile(L: Plua_State; const Filename: string; const Name: string);
procedure LuaPCall(L: Plua_State; NArgs, NResults, ErrFunc: Integer);
function LuaPCallFunction(L: Plua_State; FunctionName: string;
const Args: array of Variant;
Results: PVariantArray;
ErrFunc: Integer = 0;
NResults: Integer = LUA_MULTRET): Integer;
procedure LuaError(L: Plua_State; const Msg: string);
procedure LuaErrorFmt(L: Plua_State; const Fmt: string; const Args: array of const);
function LuaDataStrToStrings(const TableStr: string; Strings: TStrings): string;
function LuaDoFile(L: Plua_State): Integer; cdecl;
function LuaGetCurrentFuncName(L: Plua_State): string;
function Lua_VoidFunc(L: Plua_State): Integer; cdecl;
procedure CheckArg(L: Plua_State; N: Integer);
const
LuaGlobalVariableStr = '[LUA_GLOBALSINDEX]';
var
OnLuaStdoutEx: TOnLuaStdoutEx;
OnLuaStdout: TOnLuaStdout;
DefaultMaxTable: Integer;
SubTableCount: Integer;
implementation
uses
Dialogs;
const
QuoteStr = '"';
CR = #$0D;
LF = #$0A;
CRLF = CR + LF;
procedure CheckArg(L: Plua_State; N: Integer);
var
lN: Integer;
begin
lN := lua_gettop(L);
if (lN <> N) then
luaL_error(L, 'BAD parameter call!');
end;
function Quote(const Str: string): string;
begin
Result := AnsiQuotedStr(Str, QuoteStr);
end;
function Dequote(const QuotedStr: string): string;
begin
Result := AnsiDequotedStr(QuotedStr, QuoteStr);
end;
function fwriteex(F, S: PChar; Un, Len: Integer; L, Dummy: Integer): Integer;
var
Size: Integer;
begin
Size := Un * Len;
if (Assigned(OnLuaStdoutEx)) then
OnLuaStdoutEx(F, S, L, Size);
Result := Size;
end;
function fwrite(S: PChar; Un, Len: Integer; Dummy: Integer): Integer;
var
Size: Integer;
begin
Size := Un * Len;
if (Assigned(OnLuaStdout)) then
OnLuaStdout(S, Size);
Result := Size;
end;
function fputsex(const F, S: string; L, Dummy: Integer): Integer;
begin
Result := fwriteex(PChar(F), PChar(S), SizeOf(Char), L, Length(S), Dummy);
end;
function fputs(const S: string; Dummy: Integer): Integer;
begin
Result := fwrite(PChar(S), SizeOf(Char), Length(S), Dummy);
end;
function lua_printex(L: Plua_State): Integer; cdecl;
const
TAB = #$08;
NL = #$0A;
stdout = 0;
var
N, I: Integer;
S: PChar;
Debug: lua_Debug;
AR: Plua_Debug;
begin
AR := @Debug;
lua_getstack(L, 1, AR); {* stack informations *}
lua_getinfo(L, 'Snlu', AR); {* debug informations *}
N := lua_gettop(L); (* number of arguments *)
lua_getglobal(L, 'tostring');
for I := 1 to N do
begin
lua_pushvalue(L, -1); (* function to be called *)
lua_pushvalue(L, i); (* value to print *)
lua_call(L, 1, 1);
S := lua_tostring(L, -1); (* get result *)
if (S = nil) then
begin
Result := luaL_error(L, '`tostring'' must return a string to `print''');
Exit;
end;
if (I > 1) then fputs(TAB, stdout);
fputsex(AR.source, S, AR.currentline, stdout);
lua_pop(L, 1); (* pop result *)
end;
fputsex(AR.source, NL, AR.currentline, stdout);
Result := 0;
end;
function lua_print(L: Plua_State): Integer; cdecl;
const
TAB = #$08;
NL = #$0A;
stdout = 0;
var
N, I: Integer;
S: PChar;
begin
N := lua_gettop(L); (* number of arguments *)
lua_getglobal(L, 'tostring');
for I := 1 to N do
begin
lua_pushvalue(L, -1); (* function to be called *)
lua_pushvalue(L, i); (* value to print *)
lua_call(L, 1, 1);
S := lua_tostring(L, -1); (* get result *)
if (S = nil) then
begin
Result := luaL_error(L, '`tostring'' must return a string to `print''');
Exit;
end;
if (I > 1) then fputs(TAB, stdout);
fputs(S, stdout);
lua_pop(L, 1); (* pop result *)
end;
fputs(NL, stdout);
Result := 0;
end;
function lua_io_writeex(L: Plua_State): Integer; cdecl;
function pushresult(L: Plua_State; I: Boolean; FileName: PChar): Integer;
begin
lua_pushboolean(L, 1);
Result := 1;
end;
const
F = 0;
var
NArgs: Integer;
Status: Boolean;
Arg: Integer;
Len: Integer;
S: PChar;
Debug: lua_Debug;
AR: Plua_Debug;
begin
AR := @Debug;
lua_getstack(L, 1, AR); {* stack informations *}
lua_getinfo(L, 'Snlu', AR); {* debug informations *}
Arg := 1;
NArgs := lua_gettop(L);
Status := True;
while (NArgs > 0) do
begin
Dec(NArgs);
if (lua_type(L, Arg) = LUA_TNUMBER) then
begin
(* optimization: could be done exactly as for strings *)
Status := Status and
(fputsex(AR.source, Format(LUA_NUMBER_FMT, [lua_tonumber(L, Arg)]), AR.currentline, 0) > 0);
end else
begin
S := luaL_checklstring(L, Arg, @Len);
Status := Status and (fwriteex(AR.source, S, SizeOf(Char), Len, AR.currentline, F) = Len);
end;
Inc(Arg);
end;
Result := pushresult(L, Status, nil);
end;
function lua_io_write(L: Plua_State): Integer; cdecl;
function pushresult(L: Plua_State; I: Boolean; FileName: PChar): Integer;
begin
lua_pushboolean(L, 1);
Result := 1;
end;
const
F = 0;
var
NArgs: Integer;
Status: Boolean;
Arg: Integer;
Len: Integer;
S: PChar;
begin
Arg := 1;
NArgs := lua_gettop(L);
Status := True;
while (NArgs > 0) do
begin
Dec(NArgs);
if (lua_type(L, Arg) = LUA_TNUMBER) then
begin
(* optimization: could be done exactly as for strings *)
Status := Status and
(fputs(Format(LUA_NUMBER_FMT, [lua_tonumber(L, Arg)]), 0) > 0);
end else
begin
S := luaL_checklstring(L, Arg, @Len);
Status := Status and (fwrite(S, SizeOf(Char), Len, F) = Len);
end;
Inc(Arg);
end;
Result := pushresult(L, Status, nil);
end;
function LuaToBoolean(L: PLua_State; Index: Integer): Boolean;
begin
Result := (lua_toboolean(L, Index) <> 0);
end;
procedure LuaPushBoolean(L: PLua_State; B: Boolean);
begin
lua_pushboolean(L, Integer(B));
end;
function LuaToInteger(L: PLua_State; Index: Integer): Integer;
begin
Result := Trunc(lua_tonumber(L, Index)); //Round(lua_tonumber(L, Index));
end;
procedure LuaPushInteger(L: PLua_State; N: Integer);
begin
lua_pushnumber(L, N);
end;
function LuaToVariant(L: Plua_State; Index: Integer): Variant;
var
dataType: Integer;
dataNum: Double;
begin
dataType := lua_type(L, Index);
case dataType of
LUA_TSTRING: Result := VarAsType(LuaToString(L, Index), varString);
LUA_TUSERDATA,
LUA_TLIGHTUSERDATA: Result := VarAsType(Integer(lua_touserdata(L, Index)), varInteger);
LUA_TNONE,
LUA_TNIL: Result := varNull;
LUA_TBOOLEAN: Result := VarAsType(LuaToBoolean(L, Index), varBoolean);
LUA_TNUMBER: begin
dataNum := lua_tonumber(L, Index);
if (Abs(dataNum) > MAXINT)
then Result := VarAsType(dataNum, varDouble)
else begin
if (Frac(dataNum) <> 0)
then Result := VarAsType(dataNum, varDouble)
else Result := VarAsType(dataNum, varInteger)
end;
end;
end;
end;
procedure LuaPushVariant(L: Plua_State; N: Variant);
begin
case VarType(N) of
varEmpty,
varNull: lua_pushnil(L);
varBoolean: LuaPushBoolean(L, N);
varStrArg,
varOleStr,
varString: LuaPushString(L, N);
varDate: LuaPushString(L, DateTimeToStr(VarToDateTime(N)));
else lua_pushnumber(L, N);
end;
end;
function LuaToString(L: PLua_State; Index: Integer): string;
var
Size: Integer;
begin
Size := lua_strlen(L, Index);
SetLength(Result, Size);
if (Size > 0) then
Move(lua_tostring(L, Index)^, Result[1], Size);
end;
function LuaToPointer(L: Plua_State; Index: Integer): Pointer;
begin
Result := lua_touserdata(L, Index);
end;
procedure LuaPushPointer(L: Plua_State; P: Pointer);
begin
lua_pushlightuserdata(L, P);
end;
procedure LuaPushString(L: PLua_State; const S: string);
begin
lua_pushstring(L, PChar(S));
end;
function LuaIncIndex(L: Plua_State; Index: Integer): Integer;
// 相対インデックス -1 ~ -N へ変換
begin
if ((Index = LUA_GLOBALSINDEX) or (Index = LUA_REGISTRYINDEX)) then
begin
Result := Index;
Exit;
end;
Result := LuaAbsIndex(L, Index) - lua_gettop(L) - 1;
end;
function LuaAbsIndex(L: Plua_State; Index: Integer): Integer;
// 絶対インデックス 1 ~ N へ変換
begin
if ((Index = LUA_GLOBALSINDEX) or (Index = LUA_REGISTRYINDEX)) then
begin
Result := Index;
Exit;
end;
if (Index < 0) then
Result := Index + lua_gettop(L) + 1
else
Result := Index;
end;
procedure LuaPushKeyString(L: PLua_State; var Index: Integer; const Key: string);
begin
Index := LuaAbsIndex(L, Index);
lua_pushstring(L, PChar(Key));
end;
procedure LuaGetTable(L: Plua_State; TableIndex: Integer; const Key: string);
begin
LuaPushKeyString(L, TableIndex, Key);
lua_gettable(L, TableIndex);
end;
function LuaGetTableBoolean(L: Plua_State; TableIndex: Integer; const Key: string): Boolean;
begin
LuaGetTable(L, TableIndex, Key);
Result := (lua_toboolean(L, -1) <> 0);
lua_pop(L, 1);
end;
function LuaGetTableNumber(L: Plua_State; TableIndex: Integer; const Key: string): Double;
begin
LuaGetTable(L, TableIndex, Key);
Result := lua_tonumber(L, -1);
lua_pop(L, 1);
end;
function LuaGetTableInteger(L: Plua_State; TableIndex: Integer; const Key: string): Integer;
begin
LuaGetTable(L, TableIndex, Key);
Result := LuaToInteger(L, -1);
lua_pop(L, 1);
end;
function LuaGetTableString(L: Plua_State; TableIndex: Integer; const Key: string): string;
begin
LuaGetTable(L, TableIndex, Key);
Result := lua_tostring(L, -1);
lua_pop(L, 1);
end;
function LuaGetTableFunction(L: Plua_State; TableIndex: Integer; const Key: string): lua_CFunction;
begin
LuaGetTable(L, TableIndex, Key);
Result := lua_tocfunction(L, -1);
lua_pop(L, 1);
end;
function LuaGetTableLightUserData(L: Plua_State; TableIndex: Integer; const Key: string): Pointer;
begin
LuaGetTable(L, TableIndex, Key);
Result := lua_touserdata(L, -1);
lua_pop(L, 1);
end;
function LuaGetTableTMethod(L: Plua_State; TableIndex: Integer; const Key: string): TMethod;
begin
Result.Code := LuaGetTableLightUserData(L, TableIndex, Key + '_Code'); //Code is the Method Pointer
Result.Data := LuaGetTableLightUserData(L, TableIndex, Key + '_Data'); //Data is the object Pointer
end;
procedure LuaRawGetTable(L: Plua_State; TableIndex: Integer; const Key: string);
begin
LuaPushKeyString(L, TableIndex, Key);
lua_rawget(L, TableIndex);
end;
function LuaRawGetTableBoolean(L: Plua_State; TableIndex: Integer; const Key: string): Boolean;
begin
LuaRawGetTable(L, TableIndex, Key);
Result := (lua_toboolean(L, -1) <> 0);
lua_pop(L, 1);
end;
function LuaRawGetTableNumber(L: Plua_State; TableIndex: Integer; const Key: string): Double;
begin
LuaRawGetTable(L, TableIndex, Key);
Result := lua_tonumber(L, -1);
lua_pop(L, 1);
end;
function LuaRawGetTableString(L: Plua_State; TableIndex: Integer; const Key: string): string;
begin
LuaRawGetTable(L, TableIndex, Key);
Result := lua_tostring(L, -1);
lua_pop(L, 1);
end;
function LuaRawGetTableFunction(L: Plua_State; TableIndex: Integer; const Key: string): lua_CFunction;
begin
LuaRawGetTable(L, TableIndex, Key);
Result := lua_tocfunction(L, -1);
lua_pop(L, 1);
end;
function LuaRawGetTableLightUserData(L: Plua_State; TableIndex: Integer; const Key: string): Pointer;
begin
LuaRawGetTable(L, TableIndex, Key);
Result := lua_touserdata(L, -1);
lua_pop(L, 1);
end;
procedure LuaSetTableValue(L: PLua_State; TableIndex: Integer; const Key: string; ValueIndex: Integer);
begin
TableIndex := LuaAbsIndex(L, TableIndex);
ValueIndex := LuaAbsIndex(L, ValueIndex);
lua_pushstring(L, PChar(Key));
lua_pushvalue(L, ValueIndex);
lua_settable(L, TableIndex);
end;
procedure LuaSetTableNil(L: Plua_State; TableIndex: Integer; const Key: string);
begin
LuaPushKeyString(L, TableIndex, Key);
lua_pushnil(L);
lua_settable(L, TableIndex);
end;
procedure LuaSetTableBoolean(L: Plua_State; TableIndex: Integer; const Key: string; B: Boolean);
begin
LuaPushKeyString(L, TableIndex, Key);
lua_pushboolean(L, Integer(B));
lua_settable(L, TableIndex);
end;
procedure LuaSetTableNumber(L: Plua_State; TableIndex: Integer; const Key: string; N: Double);
begin
LuaPushKeyString(L, TableIndex, Key);
lua_pushnumber(L, N);
lua_settable(L, TableIndex);
end;
procedure LuaSetTableString(L: Plua_State; TableIndex: Integer; const Key: string; S: string);
begin
LuaPushKeyString(L, TableIndex, Key);
lua_pushstring(L, PChar(S));
lua_settable(L, TableIndex);
end;
procedure LuaSetTableFunction(L: Plua_State; TableIndex: Integer; const Key: string; F: lua_CFunction);
begin
LuaPushKeyString(L, TableIndex, Key);
lua_pushcfunction(L, F);
lua_settable(L, TableIndex);
end;
procedure LuaSetTableLightUserData(L: Plua_State; TableIndex: Integer; const Key: string; P: Pointer);
begin
LuaPushKeyString(L, TableIndex, Key);
lua_pushlightuserdata(L, P);
lua_settable(L, TableIndex);
end;
procedure LuaSetTableTMethod(L: Plua_State; TableIndex: Integer; const Key: string; M: TMethod);
begin
LuaSetTableLightUserData(L, TableIndex, Key + '_Code', M.Code);
LuaSetTableLightUserData(L, TableIndex, Key + '_Data', M.Data);
end;
procedure LuaSetTableClear(L: Plua_State; TableIndex: Integer);
begin
TableIndex := LuaAbsIndex(L, TableIndex);
lua_pushnil(L);
while (lua_next(L, TableIndex) <> 0) do
begin
lua_pushnil(L);
lua_replace(L, -1 - 1);
lua_settable(L, TableIndex);
lua_pushnil(L);
end;
end;
procedure LuaSetTablePropertyFuncs(L: PLua_State; TableIndex: Integer;
ReadFunc: lua_CFunction;
WriteFunc: lua_CFunction = nil);
begin
if assigned(ReadFunc)
then LuaSetMetaFunction(L, TableIndex, '__index', ReadFunc);
if assigned(WriteFunc)
then LuaSetMetaFunction(L, TableIndex, '__newindex', WriteFunc);
end;
procedure LuaRawSetTableValue(L: PLua_State; TableIndex: Integer; const Key: string; ValueIndex: Integer);
begin
TableIndex := LuaAbsIndex(L, TableIndex);
ValueIndex := LuaAbsIndex(L, ValueIndex);
lua_pushstring(L, PChar(Key));
lua_pushvalue(L, ValueIndex);
lua_rawset(L, TableIndex);
end;
procedure LuaRawSetTableNil(L: Plua_State; TableIndex: Integer; const Key: string);
begin
LuaPushKeyString(L, TableIndex, Key);
lua_pushnil(L);
lua_rawset(L, TableIndex);
end;
procedure LuaRawSetTableBoolean(L: Plua_State; TableIndex: Integer; const Key: string; B: Boolean);
begin
LuaPushKeyString(L, TableIndex, Key);
lua_pushboolean(L, Integer(B));
lua_rawset(L, TableIndex);
end;
procedure LuaRawSetTableNumber(L: Plua_State; TableIndex: Integer; const Key: string; N: Double);
begin
LuaPushKeyString(L, TableIndex, Key);
lua_pushnumber(L, N);
lua_rawset(L, TableIndex);
end;
procedure LuaRawSetTableString(L: Plua_State; TableIndex: Integer; const Key: string; S: string);
begin
LuaPushKeyString(L, TableIndex, Key);
lua_pushstring(L, PChar(S));
lua_rawset(L, TableIndex);
end;
procedure LuaRawSetTableFunction(L: Plua_State; TableIndex: Integer; const Key: string; F: lua_CFunction);
begin
LuaPushKeyString(L, TableIndex, Key);
lua_pushcfunction(L, F);
lua_rawset(L, TableIndex);
end;
procedure LuaRawSetTableLightUserData(L: Plua_State; TableIndex: Integer; const Key: string; P: Pointer);
begin
LuaPushKeyString(L, TableIndex, Key);
lua_pushlightuserdata(L, P);
lua_rawset(L, TableIndex);
end;
procedure LuaRawSetTableClear(L: Plua_State; TableIndex: Integer);
begin
TableIndex := LuaAbsIndex(L, TableIndex);
lua_pushnil(L);
while (lua_next(L, TableIndex) <> 0) do
begin
lua_pushnil(L);
lua_replace(L, -1 - 1);
lua_rawset(L, TableIndex);
lua_pushnil(L);
end;
end;
function LuaGetMetaFunction(L: Plua_State; Index: Integer; Key: string): lua_CFunction;
// メタ関数の取得
begin
Result := nil;
Index := LuaAbsIndex(L, Index);
if (lua_getmetatable(L, Index) = 0) then
Exit;
LuaGetTable(L, -1, Key);
if lua_iscfunction(L, -1) then
Result := lua_tocfunction(L, -1);
lua_pop(L, 2);
end;
procedure LuaSetMetaFunction(L: Plua_State; Index: Integer; Key: string; F: lua_CFunction);
// メタ関数の設定
// Key = __add, __sub, __mul, __div, __pow, __unm, __concat,
// __eq, __lt, __le, __index, __newindex, __call
// [メモ]
// __newindex は 新規代入時しか呼ばれないので注意
// table をグローバル変数とするとこうなる。
//
// a=1 -- (a=nilなので)メタ関数呼び出される
// a=2 -- メタ関数は呼び出されない
// a=3 -- メタ関数は呼び出されない
// a=nil
// a=4 -- (a=nilなので)メタ関数呼び出される
//
// lua 付属の trace-globals では__newindex と __index をセットで上書きして
// グローバル変数へのアクセスをローカル変数へのアクセスに切り替えてグロー
// バル変数の実体は常に table[key] = nil を保たせて __newindex イベントを
// 発生させている。
begin
Index := LuaAbsIndex(L, Index);
if (lua_getmetatable(L, Index) = 0) then
lua_newtable(L);
LuaRawSetTableFunction(L, -1, Key, F);
lua_setmetatable(L, Index);
end;
// Convert the last item at 'Index' from the stack to a string
// nil : nil
// Number : FloatToStr
// Boolean: True/False
// stirng : "..."
// Table : { Key1=Value Key2=Value }
function LuaStackToStr(L: Plua_State; Index: Integer; MaxTable: Integer; SubTableMax: Integer): string;
var
pGLobalsIndexPtr: Pointer;
function TableToStr(Index: Integer): string;
var
Key, Value: string;
Count: Integer;
begin
Result := '{ ';
Count := 0;
lua_pushnil(L);
// Go through the current table
while (lua_next(L, Index) <> 0) do
begin
Inc(Count);
if (Count > MaxTable) then
begin
Result := Result + '... ';
lua_pop(L, 2);
Break;
end;
// Key to string
if lua_type(L, -2) = LUA_TNUMBER then
Key := '[' + Dequote(LuaStackToStr(L, -2, MaxTable, SubTableMax)) + ']'
else
Key := Dequote(LuaStackToStr(L, -2, MaxTable, SubTableMax));
// Value to string...
if ((Key = '_G') or (lua_topointer(L, -1) = pGLobalsIndexPtr)) then
Value := LuaGlobalVariableStr
else
Value := LuaStackToStr(L, -1, MaxTable, SubTableMax);
if (lua_type(L, -1) = LUA_TFUNCTION) then
Result := Result + Format('%s()=%p ', [Key, lua_topointer(L, -1)])
else
Result := Result + Format('%s=%s ', [Key, Value]);
// Pop current value from stack leaving current key on top of the stack for lua_next
lua_pop(L, 1);
end;
Result := Result + '}';
end;
begin
if (MaxTable < 0) then
MaxTable := DefaultMaxTable;
pGLobalsIndexPtr := lua_topointer(L, LUA_GLOBALSINDEX); // Retrieve globals index poiner for later conditions
lua_checkstack(L, SubTableMax * 3); // Ensure there is enough space on stack to work with according to user's setting
Index := LuaAbsIndex(L, Index);
case (lua_type(L, Index)) of
LUA_TNIL:
Result := 'nil';
LUA_TNUMBER:
Result := Format('%g', [lua_tonumber(L, Index)]);
LUA_TBOOLEAN:
Result := BoolToStr(lua_toboolean(L, Index) <> 0, True);
LUA_TSTRING:
Result := '"' + lua_tostring(L, Index) + '"';
LUA_TTABLE:
begin
if SubTableCount < SubTableMax then
begin
SubTableCount := SubTableCount + 1;
Result := TableToStr(Index);
SubTableCount := SubTableCount - 1;
end
else
Result := '[SUB_TABLE_MAX_LEVEL_HAS_BEEN_REACHED]';
end;
LUA_TFUNCTION:
if lua_iscfunction(L, Index) then
Result := Format('CFUNC:%p', [Pointer(lua_tocfunction(L, Index))])
else
Result := Format('FUNC:%p', [lua_topointer(L, Index)]);
LUA_TUSERDATA:
Result := Format('USERDATA:%p', [lua_touserdata(L, Index)]);
LUA_TTHREAD:
Result := Format('THREAD:%p', [lua_tothread(L, Index)]);
LUA_TLIGHTUSERDATA:
Result := Format('LIGHTUSERDATA:%p', [lua_touserdata(L, Index)]);
else
Assert(False);
end;
end;
procedure LuaShowStack(L: Plua_State; Caption: string);
var
I, N: Integer;
S: string;
begin
N := lua_gettop(L);
S := '[' + Caption + ']';
for I := N downto 1 do
begin
S := S + CRLF + Format('%3d,%3d:%s', [LuaAbsIndex(L, I), LuaIncIndex(L, I),
LuaStackToStr(L, I, -1)]);
end;
ShowMessage(S);
end;
procedure LuaProcessTableName(L: Plua_State; const Name: PChar;
var LastName: string; var TableIndex, Count: Integer);
// Name のテーブル要素をスタックに積んで、
// スタックに積んだ数と Name の最終要素の名前とその親テーブルのインデックスを返す
// テーブルが無い場合は作成する
// LuaProcessTableName(L, 'print', S, TI, Count) → S = print, TI = LUA_GLOBALSINDEX, Count = 0
// LuaProcessTableName(L, 'io.write', S, TI, Count) → S = write, TI -> io, Count = 1
// LuaProcessTableName(L, 'a.b.c.func', S, TI, Count) → S = func, TI -> a.b.c, Count = 3
var
S: string;
function GetToken: string;
var
Index: Integer;
begin
Index := Pos('.', S);
if (Index = 0) then
begin
Result := S;
S := '';
Exit;
end;
Result := Copy(S, 1, Index - 1);
S := Copy(S, Index + 1, Length(S));
end;
begin
S := Name;
Count := 0;
LastName := GetToken;
while (S <> '') do
begin
Inc(Count);
TableIndex := LuaAbsIndex(L, TableIndex);
LuaGetTable(L, TableIndex, LastName);
if (lua_type(L, -1) <> LUA_TTABLE) then
begin
lua_pop(L, 1);
lua_pushstring(L, PChar(LastName));
lua_newtable(L);
lua_rawset(L, TableIndex);
LuaGetTable(L, TableIndex, LastName);
end;
TableIndex := -1;
LastName := GetToken;
end;
end;
procedure LuaRegisterCustom(L: PLua_State; TableIndex: Integer; const Name: PChar; F: lua_CFunction);
var
Count: Integer;
S: string;
begin
LuaProcessTableName(L, Name, S, TableIndex, Count);
LuaRawSetTableFunction(L, TableIndex, S, F);
lua_pop(L, Count);
end;
procedure LuaRegister(L: Plua_State; const Name: PChar; F: lua_CFunction);
// 関数の登録
// LuaRegister(L, 'print', lua_print);
// LuaRegister(L, 'io.write', lua_io_write); // テーブル io が無い場合は作成
// LuaRegister(L, 'a.b.c.func', a_b_c_func); // テーブル a.b.c が無い場合は作成
begin
LuaRegisterCustom(L, LUA_GLOBALSINDEX, Name, F);
end;
procedure LuaRegisterMetatable(L: Plua_State; const Name: PChar; F: lua_CFunction);
begin
LuaRegisterCustom(L, LUA_REGISTRYINDEX, Name, F);
end;
procedure LuaRegisterProperty(L: PLua_State; const Name: PChar; ReadFunc, WriteFunc: lua_CFunction);
var
Count: Integer;
TI: Integer;
S: string;
begin
TI := LUA_GLOBALSINDEX;
LuaProcessTableName(L, Name, S, TI, Count);
TI := LuaAbsIndex(L, TI);
LuaGetTable(L, TI, S);
if (lua_type(L, -1) <> LUA_TTABLE) then
begin
lua_pop(L, 1);
lua_pushstring(L, PChar(S));
lua_newtable(L);
lua_settable(L, TI);
LuaGetTable(L, TI, S);
end;
if (Assigned(ReadFunc)) then
LuaSetMetaFunction(L, -1, '__index', ReadFunc);
if (Assigned(WriteFunc)) then
LuaSetMetaFunction(L, -1, '__newindex', WriteFunc);
lua_pop(L, Count + 1);
end;
procedure LuaStackToStrings(L: Plua_State; Lines: TStrings; MaxTable: Integer = -1; SubTableMax: Integer = 99);
var
I: Integer;
begin
Lines.Clear;
for I := lua_gettop(L) downto 1 do
Lines.Add(LuaStackToStr(L, I, MaxTable, SubTableMax));
end;
procedure LuaLocalToStrings(L: Plua_State; Lines: TStrings; MaxTable: Integer; Level: Integer; SubTableMax: Integer);
var
Name: PChar;
Index: Integer;
Debug: lua_Debug;
AR: Plua_Debug;
begin
AR := @Debug;
Lines.Clear;
Index := 1;
if (lua_getstack(L, Level, AR) = 0) then
Exit;
Name := lua_getlocal(L, AR, Index);
while (Name <> nil) do