-
Notifications
You must be signed in to change notification settings - Fork 5
/
LuaObject.pas
499 lines (418 loc) · 13 KB
/
LuaObject.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
unit LuaObject;
{$IFDEF FPC}
{$mode objfpc}{$H+}
{$ENDIF}
{$I pLua.inc}
interface
uses
Classes, SysUtils, lua, Variants, pLuaObject, pLua;
type
TLuaObject = class;
{ TLuaObject }
TLuaObject = class
protected
L : PLua_State;
FLuaReference : integer;
FParent : TLuaObject;
FChildren : TList;
function GetLuaProp(PropName : AnsiString): Variant;
procedure SetLuaProp(PropName : AnsiString; const AValue: Variant);
function GetPropValue(propName : AnsiString): Variant; virtual;
function GetPropObject(propName: AnsiString) : Boolean; virtual;
function SetPropValue(PropName : AnsiString; const AValue: Variant) : Boolean; virtual;
function SetPropObject(propName: AnsiString) : Boolean; virtual;
function PropIsObject(propName : AnsiString): Boolean; virtual;
procedure CommonCreate(LuaState : PLua_State; AParent : TLuaObject = nil); virtual;
public
constructor Create(LuaState : PLua_State; AParent : TLuaObject = nil); virtual; overload;
constructor Create(LuaState: PLua_State; LuaClassName, LuaName: AnsiString); virtual; overload;
destructor Destroy; override;
procedure PushSelf;
procedure CallEvent(EventName : AnsiString); overload;
function CallEvent(EventName : AnsiString; args : Array of Variant; Results: PVariantArray = nil) : Integer; overload;
function EventExists(EventName: AnsiString): Boolean;
property LState : PLua_State read L;
property LuaProp[PropName : AnsiString] : Variant read GetLuaProp write SetLuaProp;
end;
TLuaObjectRegisterMethodsCallback = procedure(L : Plua_State; classTable : Integer);
TLuaObjectNewCallback = function(L : PLua_State; AParent : TLuaObject=nil):TLuaObject;
var
LuaObjects : TList;
procedure ClearObjects;
procedure LuaCopyTable(L: Plua_State; IdxFrom, IdxTo, MtTo : Integer);
function LuaToTLuaObject(L: Plua_State; Idx : Integer) : TLuaObject;
procedure RegisterLuaObject(L: Plua_State);
procedure RegisterTLuaObject(L : Plua_State; ObjectName : AnsiString; CreateFunc : lua_CFunction; MethodsCallback : TLuaObjectRegisterMethodsCallback = nil);
procedure RegisterObjectInstance(L : Plua_State; aClassName, InstanceName : AnsiString; ObjectInstance : TLuaObject);
procedure RegisterMethod(L : Plua_State; TheMethodName : AnsiString; TheMethodAddress : lua_CFunction; classTable : Integer);
function new_LuaObject(L : PLua_State; aClassName : AnsiString; NewCallback : TLuaObjectNewCallback) : Integer; extdecl;
procedure PushTLuaObject(L : PLua_State; ObjectInstance : TLuaObject);
function new_TLuaObject(L : PLua_State) : Integer; extdecl;
function index_TLuaObject(L : PLua_State) : Integer; extdecl;
function newindex_TLuaObject(L : PLua_State) : Integer; extdecl;
function gc_TLuaObject(L : PLua_State) : Integer; extdecl;
procedure RegisterClassTLuaObject(L : Plua_State);
implementation
uses
typinfo;
const
LuaTLuaObjectClassName = 'TLuaObject';
constructor TLuaObject.Create(LuaState : PLua_State; AParent : TLuaObject = nil);
begin
CommonCreate(LuaState, nil);
// Create a reference to the object table, this way lua won't GC its version
FLuaReference := luaL_ref(L, LUA_REGISTRYINDEX);
lua_rawgeti (L, LUA_REGISTRYINDEX, FLuaReference);
LuaObjects.Add(Self);
end;
constructor TLuaObject.Create(LuaState: PLua_State; LuaClassName, LuaName: AnsiString);
begin
CommonCreate(LuaState, nil);
RegisterObjectInstance(LuaState, LuaClassName, LuaName, self);
end;
destructor TLuaObject.Destroy;
var
lo : TLuaObject;
begin
LuaObjects.Remove(Self);
if assigned(FParent) then
FParent.FChildren.Remove(Self);
while FChildren.Count > 0 do
begin
lo := TLuaObject(FChildren[FChildren.Count-1]);
FChildren.Delete(FChildren.Count-1);
lo.Free;
end;
FChildren.Free;
luaL_unref(L, LUA_REGISTRYINDEX, FLuaReference);
inherited Destroy;
end;
procedure TLuaObject.PushSelf;
begin
lua_rawgeti(L, LUA_REGISTRYINDEX, FLuaReference);
end;
procedure TLuaObject.CallEvent(EventName: AnsiString);
begin
CallEvent(EventName, []);
end;
function TLuaObject.CallEvent(EventName : AnsiString; args: array of Variant; Results: PVariantArray) : Integer;
begin
result := -1;
if not EventExists(EventName) then
exit;
PushSelf;
result := plua_callfunction(L, EventName, args, results, lua_gettop(L));
end;
function TLuaObject.EventExists(EventName: AnsiString): Boolean;
begin
PushSelf;
result := plua_functionexists(L, EventName, lua_gettop(L));
lua_pop(L, 1);
end;
function TLuaObject.GetLuaProp(PropName : AnsiString): Variant;
var
idx : Integer;
begin
lua_rawgeti (L, LUA_REGISTRYINDEX, FLuaReference); // Place our object on the stack
idx := lua_gettop(L);
lua_pushliteral(L, PChar(PropName)); // Place the event name on the stack
lua_gettable(L, idx); // try to get the item
result := plua_tovariant(L, lua_gettop(L));
lua_pop(L, 2);
end;
procedure TLuaObject.SetLuaProp(PropName : AnsiString; const AValue: Variant);
var
idx : Integer;
begin
lua_rawgeti (L, LUA_REGISTRYINDEX, FLuaReference); // Place our object on the stack
idx := lua_gettop(L);
lua_pushstring(L, PChar(propName));
plua_pushvariant(L, AValue);
lua_rawset(L, idx);
end;
function TLuaObject.GetPropValue(propName: AnsiString): Variant;
begin
if IsPublishedProp(self, propName) then
result := typinfo.GetPropValue(self, propName)
else
result := NULL;
end;
function TLuaObject.GetPropObject(propName: AnsiString) : Boolean;
begin
result := false;
end;
function TLuaObject.SetPropValue(PropName: AnsiString; const AValue: Variant) : Boolean;
begin
result := IsPublishedProp(self, propName);
if result then
typinfo.SetPropValue(self, propName, AValue);
end;
function TLuaObject.SetPropObject(propName: AnsiString) : Boolean;
begin
result := false;
end;
function TLuaObject.PropIsObject(propName: AnsiString): Boolean;
begin
result := false;
end;
procedure TLuaObject.CommonCreate(LuaState: PLua_State; AParent: TLuaObject);
begin
L := LuaState;
FParent := AParent;
if assigned(FParent) then
FParent.FChildren.Add(Self);
FChildren := TList.Create;
end;
{ Global LUA Methods }
procedure LuaCopyTable(L: Plua_State; IdxFrom, IdxTo, MtTo : Integer);
var
id:Integer;
tbl : Integer;
key, val : Variant;
cf : lua_CFunction;
begin
lua_pushnil(L);
while(lua_next(L, IdxFrom)<>0)do
begin
key := plua_tovariant(L, -2);
if CompareText(key, '__') = 1 then
tbl := MtTo
else
tbl := IdxTo;
case lua_type(L, -1) of
LUA_TFUNCTION : begin
cf := lua_tocfunction(L, -1);
plua_pushvariant(L, key);
lua_pushcfunction(L, cf);
lua_rawset(L, tbl);
end;
LUA_TTABLE : begin
id := lua_gettop(L);
LuaCopyTable(L, id, IdxTo, MtTo);
end;
else
val := plua_tovariant(L, -1);
plua_pushvariant(L, key);
plua_pushvariant(L, val);
lua_rawset(L, tbl);
end;
lua_pop(L, 1);
end;
end;
function LuaToTLuaObject(L: Plua_State; Idx : Integer) : TLuaObject;
begin
result := nil;
if lua_type(L, Idx) = LUA_TTABLE then
begin
Idx := plua_absindex(L, Idx);
lua_pushstring(L, '_Self');
lua_gettable(L, Idx);
result := TLuaObject(PtrUint(lua_tointeger(L, -1)));
lua_pop(L, 1);
end
else
luaL_error(L, PChar('Class table expected.'));
end;
procedure PushTLuaObject(L: PLua_State; ObjectInstance: TLuaObject);
begin
lua_rawgeti(L, LUA_REGISTRYINDEX, ObjectInstance.FLuaReference);
end;
function new_TLuaObject(L : PLua_State) : Integer; extdecl;
var
P, E : TLuaObject;
n, idx, idx2, mt : Integer;
begin
n := lua_gettop(L);
if lua_type(L, 1) <> LUA_TTABLE then
lua_remove(L, 1);
if n = 1 then
P := LuaToTLuaObject(L, 1)
else
P := nil;
lua_newtable(L);
E := TLuaObject.Create(L, P);
idx := lua_gettop(L);
lua_pushliteral(L, '_Self');
lua_pushinteger(L, PtrUint(Pointer(E)));
lua_rawset(L, idx);
lua_newtable(L);
mt := lua_gettop(L);
lua_pushliteral(L, LuaTLuaObjectClassName);
lua_gettable(L, LUA_GLOBALSINDEX);
idx2 := lua_gettop(L);
LuaCopyTable(L, idx2, idx, mt);
lua_setmetatable(L, idx);
lua_pop(L, 1);
result := 1;
end;
function index_TLuaObject(L : PLua_State) : Integer; extdecl;
var
E : TLuaObject;
propName : AnsiString;
v : Variant;
begin
E := LuaToTLuaObject(L, 1);
lua_remove(L, 1);
if E = nil then
begin
result := 0;
exit;
end;
propName := plua_tostring(L, 1);
index_TLuaObject := 1;
if E.PropIsObject(propName) then
begin
if not E.GetPropObject(propName) then
index_TLuaObject := 0;
end
else
begin
v := E.GetPropValue(propName);
if v = NULL then
index_TLuaObject := 0
else
plua_pushvariant(L, v);
end;
end;
function newindex_TLuaObject(L : PLua_State) : Integer; extdecl;
var
TableIndex, ValueIndex : Integer;
E : TLuaObject;
propName : AnsiString;
begin
result := 0;
E := LuaToTLuaObject(L, 1);
if E = nil then
begin
exit;
end;
propName := plua_tostring(L, 2);
if E.PropIsObject(propName) and E.SetPropObject(propName) then
else if not E.SetPropValue(propName, plua_tovariant(L, 3)) then
begin
// This is a standard handler, no value was found in the object instance
// so we push the value into the Lua Object reference.
TableIndex := plua_absindex(L, 1);
ValueIndex := plua_absindex(L, 3);
lua_pushstring(L, PChar(propName));
lua_pushvalue(L, ValueIndex);
lua_rawset(L, TableIndex);
end;
end;
function gc_TLuaObject(L : PLua_State) : Integer; extdecl;
var
E : TLuaObject;
begin
E := LuaToTLuaObject(L, 1);
// Release the object
if assigned(E) then
E.Free;
result := 0;
end;
procedure RegisterObjectInstance(L: Plua_State; aClassName, InstanceName: AnsiString; ObjectInstance : TLuaObject);
var
idx, idx2, mt : Integer;
begin
lua_pushliteral(L, PChar(InstanceName));
lua_newtable(L);
ObjectInstance.FLuaReference := luaL_ref(L, LUA_REGISTRYINDEX);
lua_rawgeti (L, LUA_REGISTRYINDEX, ObjectInstance.FLuaReference);
LuaObjects.Add(ObjectInstance);
idx := lua_gettop(L);
lua_pushliteral(L, '_Self');
lua_pushinteger(L, PtrUint(Pointer(ObjectInstance)));
lua_rawset(L, idx);
lua_newtable(L);
mt := lua_gettop(L);
lua_pushliteral(L, PChar(aClassName));
lua_gettable(L, LUA_GLOBALSINDEX);
idx2 := lua_gettop(L);
LuaCopyTable(L, idx2, idx, mt);
lua_setmetatable(L, idx);
lua_pop(L, 1);
lua_settable(L, LUA_GLOBALSINDEX);
end;
procedure RegisterMethod(L : Plua_State; TheMethodName : AnsiString; TheMethodAddress : lua_CFunction; classTable : Integer);
begin
lua_pushliteral(L, PChar(TheMethodName));
lua_pushcfunction(L, TheMethodAddress);
lua_rawset(L, classTable);
end;
function new_LuaObject(L : PLua_State; aClassName : AnsiString; NewCallback : TLuaObjectNewCallback): Integer; extdecl;
var
P, E : TLuaObject;
n, idx, idx2, mt : Integer;
begin
n := lua_gettop(L);
if lua_type(L, 1) <> LUA_TTABLE then
lua_remove(L, 1);
if n > 1 then
P := LuaToTLuaObject(L, 2)
else
P := nil;
lua_newtable(L);
E := NewCallback(L, P);
idx := lua_gettop(L);
lua_pushliteral(L, '_Self');
lua_pushinteger(L, PtrUint(Pointer(E)));
lua_rawset(L, idx);
lua_newtable(L);
mt := lua_gettop(L);
lua_pushliteral(L, PChar(aClassName));
lua_gettable(L, LUA_GLOBALSINDEX);
idx2 := lua_gettop(L);
LuaCopyTable(L, idx2, idx, mt);
lua_setmetatable(L, idx);
lua_pop(L, 1);
result := 1;
end;
procedure RegisterClassTLuaObject(L : Plua_State);
var
classTable : Integer;
begin
lua_pushstring(L, LuaTLuaObjectClassName);
lua_newtable(L);
classTable := lua_gettop(L);
RegisterMethod(L, '__index', @index_TLuaObject, classTable);
RegisterMethod(L, '__newindex', @newindex_TLuaObject, classTable);
RegisterMethod(L, '__call', @new_TLuaObject, classTable);
RegisterMethod(L, '__gc', @gc_TLuaObject, classTable);
RegisterMethod(L, 'release', @gc_TLuaObject, classTable);
RegisterMethod(L, 'new', @new_TLuaObject, classTable);
lua_settable(L, LUA_GLOBALSINDEX);
end;
{ Global Management Methods }
procedure RegisterTLuaObject(L: Plua_State; ObjectName : AnsiString;
CreateFunc : lua_CFunction;
MethodsCallback: TLuaObjectRegisterMethodsCallback);
var
classTable : Integer;
begin
lua_pushstring(L, PChar(ObjectName));
lua_newtable(L);
classTable := lua_gettop(L);
RegisterMethod(L, '__index', @index_TLuaObject, classTable);
RegisterMethod(L, '__newindex', @newindex_TLuaObject, classTable);
RegisterMethod(L, '__call', CreateFunc, classTable);
RegisterMethod(L, '__gc', @gc_TLuaObject, classTable);
RegisterMethod(L, 'release', @gc_TLuaObject, classTable);
RegisterMethod(L, 'new', CreateFunc, classTable);
if Assigned(MethodsCallback) then
MethodsCallback(L, classTable);
lua_settable(L, LUA_GLOBALSINDEX);
end;
procedure ClearObjects;
begin
while LuaObjects.Count > 0 do
TLuaObject(LuaObjects[LuaObjects.Count-1]).Free;
end;
procedure RegisterLuaObject(L: Plua_State);
begin
RegisterClassTLuaObject(L);
end;
initialization
LuaObjects := TList.Create;
finalization
ClearObjects;
LuaObjects.Free;
end.