forked from malcome/Lua4Lazarus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathl4l_object.pas
347 lines (312 loc) · 8.35 KB
/
l4l_object.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
{
Lua4Lazarus
TLuaObject
License: New BSD
Copyright(c)2010- Malcome@Japan All rights reserved.
Version History:
1.54.200703 by Malcome Japan.
- Test Lazarus 2.0.8 (FPC 3.0.4)
- for Lua 5.4
1.53.150205 by Malcome Japan.
- Test Lazarus 1.4RC1 (FPC 2.6.4), Lazarus 1.5 (FPC 3.0.1)
- for Lua 5.3
1.52.0 by Malcome Japan.
- Test Lazarus 1.3 (FPC 2.6.2)
- for Lua 5.2
1.0.0 by Malcome@Japan.
- Test Lazarus 0.9.29 (FPC 2.4.1)
- for Lua 5.1
License for Lua 5.0 and later versions:
Copyright(c)1994-2015 Lua.org, PUC-Rio.
}
unit l4l_object;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, lua54;
type
{ TLuaObject }
TLuaObject = class(TPersistent)
private
FLS: Plua_State;
protected
property LS: Plua_State read FLS;
function Iterator({%H-}index: integer): integer; virtual;
public
constructor Create(L : Plua_State); virtual;
published
end;
procedure l4l_SetLuaObject(obj: TLuaObject);
procedure l4l_PushLuaObject(obj: TLuaObject);
function l4l_isobject(L : Plua_State; n: Integer): boolean;
function l4l_isobject(L : Plua_State; n: Integer; c: TClass): boolean;
function l4l_toobject(L : Plua_State; n: Integer): TLuaObject;
implementation
uses
typinfo;
const
FIELD_OBJ = '___l4lObject___';
FIELD_FN = '___l4lFuncName___';
FIELD_IC = '___l4lIteCount___';
PROP_HEAD = 'l4l_';
function gc(L : Plua_State) : Integer; cdecl;
var
p: PPointer;
begin
p:= lua_touserdata(L, 1);
try TLuaObject(p^).Free; except end;
Result:= 0;
end;
function call(L : Plua_State) : Integer; cdecl;
var
p: PPointer;
obj: TLuaObject;
method: function:integer of object;
begin
lua_getfield(L, 1, FIELD_OBJ);
p:= lua_touserdata(L, -1);
lua_remove(L, -1);
obj:= TLuaObject(p^);
lua_getfield(L, 1, FIELD_FN);
lua_remove(L, 1);
p:= lua_touserdata(L, -1);
lua_remove(L, -1);
TMethod(method).Data := obj;
TMethod(method).Code := p^;
try
Result := method();
except
on E: Exception do luaL_error(L, PChar(E.Message));
end;
end;
function Index(L : Plua_State) : Integer; cdecl;
var
p: PPointer;
key: string;
obj: TLuaObject;
pi: PPropInfo;
o: TObject;
begin
Result := 0;
lua_getfield(L, 1, FIELD_OBJ);
p:= lua_touserdata(L, -1);
obj:= TLuaObject(p^);
key := lua_tostring(L, 2);
if Assigned(obj.MethodAddress(PROP_HEAD + key)) then begin
lua_getfield(L, 1, PChar(LowerCase(key)));
end else begin
try
pi := FindPropInfo(obj, PROP_HEAD + LowerCase(key));
except
luaL_error(L, 'Unknown property: "%s".', PChar(key));
end;
try
case pi^.PropType^.Kind of
tkInteger, tkQWord:
lua_pushinteger(L, GetOrdProp(obj, pi));
tkInt64: lua_pushinteger(L, GetInt64Prop(obj, pi));
tkFloat: lua_pushnumber(L, GetFloatProp(obj, pi));
tkBool: begin
if GetOrdProp(obj, pi) = 0 then
lua_pushboolean(L, False)
else
lua_pushboolean(L, True);
end;
tkClass: begin
o:= GetObjectProp(obj, pi);
if o is TLuaObject then begin
l4l_PushLuaObject(o as TLuaObject);
end else begin
lua_pushnil(L);
end;
end;
else begin
lua_pushstring(L, PChar(GetStrProp(obj, pi)));
end;
end;
except
on E: Exception do luaL_error(L, PChar(E.Message));
end;
end;
Result := 1;
end;
function NewIndex(L : Plua_State) : Integer; cdecl;
var
p: PPointer;
key: string;
obj: TLuaObject;
pi: PPropInfo;
begin
Result:=0;
lua_getfield(L, 1, FIELD_OBJ);
p:= lua_touserdata(L, -1);
obj:= TLuaObject(p^);
key := lua_tostring(L, 2);
try
pi := FindPropInfo(obj, PROP_HEAD + LowerCase(key));
except
luaL_error(L, 'Unknown property: "%s".', PChar(key));
end;
try
case pi^.PropType^.Kind of
tkInteger, tkInt64, tkQWord:
SetOrdProp(obj, pi, lua_tointeger(L, 3));
tkFloat: SetFloatProp(obj, pi, lua_tonumber(L, 3));
tkBool: SetOrdProp(obj, pi, Ord(lua_toboolean(L, 3)));
tkClass: begin
lua_getfield(L, 3, FIELD_OBJ);
p:= lua_touserdata(L, -1);
if Assigned(p) and Assigned(p^) then begin
SetObjectProp(obj, pi, TObject(p^));
end else
SetObjectProp(obj, pi, TObject(nil));
end;
else begin
SetStrProp(obj, pi, lua_tostring(L, 3));
end;
end;
except
on E: Exception do luaL_error(L, PChar(E.Message));
end;
end;
function iterator(L : Plua_State) : Integer; cdecl;
var
i: integer;
p: PPointer;
obj: TLuaObject;
begin
Result:= 0;
lua_getfield(L, 1, FIELD_OBJ);
p:= lua_touserdata(L, -1);
obj:= TLuaObject(p^);
if lua_isnil(L, 3) then begin
i := 0;
end else begin
lua_pushstring(L, FIELD_IC);
lua_rawget(L, 1);
i:= lua_tointeger(L, -1) + 1;
end;
lua_pushstring(L, FIELD_IC);
lua_pushinteger(L, i);
lua_rawset(L, 1);
try
Result := obj.Iterator(i);
except
on E: Exception do luaL_error(L, PChar(E.Message));
end;
end;
procedure l4l_SetLuaObject(obj: TLuaObject);
type
TMethodRec = packed record
name : pshortstring;
addr : pointer;
end;
TMethodTable = packed record
count : dword;
entries : packed array[0..0] of TMethodRec;
end;
PMethodTable = ^TMethodTable;
var
p: PPointer;
i, t: integer;
mt: PMethodTable;
s:string;
cl: TClass;
begin
t:= lua_gettop(obj.LS);
lua_pushstring(obj.LS, FIELD_OBJ);
p:= lua_newuserdata(obj.LS, SizeOf(Pointer));
p^:=obj;
if lua_getmetatable(obj.LS, -1) = 0 then lua_newtable(obj.LS);
lua_pushstring(obj.LS, '__gc');
lua_pushcfunction(obj.LS, @gc);
lua_settable(obj.LS, -3);
lua_setmetatable(obj.LS, -2);
lua_settable(obj.LS, -3);
cl:= obj.ClassType;
while Assigned(cl) do begin
p := Pointer(PAnsiChar(cl) + vmtMethodtable);
mt := p^;
if Assigned(mt) then begin
for i:=0 to mt^.count-1 do begin
s:= LowerCase(mt^.entries[i].name^);
if Copy(s, 1, 4) <> PROP_HEAD then continue;
Delete(s, 1, 4);
lua_pushstring(obj.LS, PChar(s));
lua_newtable(obj.LS);
lua_pushstring(obj.LS, FIELD_FN);
p:= lua_newuserdata(obj.LS, SizeOf(Pointer));
p^:= mt^.entries[i].addr;
lua_settable(obj.LS, -3);
lua_newtable(obj.LS);
lua_pushstring(obj.LS, '__call');
lua_pushcfunction(obj.LS, @call);
lua_settable(obj.LS, -3);
lua_pushstring(obj.LS, '__index');
lua_pushvalue(obj.LS, t); // SuperClass
lua_settable(obj.LS, -3);
lua_setmetatable(obj.LS, -2);
lua_settable(obj.LS, -3);
end;
end;
cl := cl.ClassParent;
end;
lua_newtable(obj.LS);
lua_pushstring(obj.LS, '__newindex');
lua_pushcfunction(obj.LS, @NewIndex);
lua_settable(obj.LS, -3);
lua_pushstring(obj.LS, '__index');
lua_pushcfunction(obj.LS, @Index);
lua_settable(obj.LS, -3);
lua_pushstring(obj.LS, '__call');
lua_pushcfunction(obj.LS, @iterator);
lua_settable(obj.LS, -3);
lua_setmetatable(obj.LS, -2);
end;
procedure l4l_PushLuaObject(obj: TLuaObject);
begin
lua_newtable(obj.LS);
l4l_SetLuaObject(obj);
end;
function l4l_isobject(L : Plua_State; n: Integer): boolean;
begin
Result:= l4l_isobject(L, n, TLuaObject);
end;
function l4l_isobject(L : Plua_State; n: Integer; c: TClass): boolean;
var
p: PPointer;
begin
Result:= False;
if lua_istable(L, n) then begin
lua_getfield(L, n, FIELD_OBJ);
if not lua_isnil(L, -1) then begin
p:= lua_touserdata(L, -1);
Result := TObject(p^) is c;
end;
lua_remove(L, -1);
end;
end;
function l4l_toobject(L: Plua_State; n: Integer): TLuaObject;
var
p: PPointer;
begin
Result:= nil;
if lua_istable(L, n) then begin
lua_getfield(L, n, FIELD_OBJ);
if not lua_isnil(L, -1) then begin
p:= lua_touserdata(L, -1);
Result := TLuaObject(p^);
end;
lua_remove(L, -1);
end;
end;
{ TLuaObject }
function TLuaObject.Iterator(index: integer): integer;
begin
Result := 0;
end;
constructor TLuaObject.Create(L: Plua_State);
begin
FLS := L;
end;
end.