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
|
namespace Luax
{
//--------------------------------------------------------------------------------//
///
/// ԲͬͣͨGetLuaClassName࣬GetClassNameᱻǣָluax_c_getupvalue
///
template<class TYPE, class BASE>
int LuaxNativeClass<TYPE, BASE>::_GetClassName(lua_State* L)
{
LUAX_SETUP(L, "*");
cc8* type = TYPE::GetLuaxClassName();
state.Push(type);
return 1;
}
//--------------------------------------------------------------------------------//
///
/// עṤ͵еԱ
///
template<class TYPE, class BASE>
void LuaxNativeClass<TYPE, BASE>::RegisterLuaxClassShared(LuaxState& state)
{
luaL_Reg regTable[] = {
{ "GetClass", _GetClass },
{ "GetClassName", _GetClassName },
{ NULL, NULL }
};
state.RegisterMethods(regTable);
}
///
/// ijԱעclass table
///
template<class TYPE, class BASE>
void LuaxNativeClass<TYPE, BASE>::RegisterLuaxFactoryClass(LuaxState& state)
{
luaL_Reg regTable[] = {
{ "GetRefTable", _GetRefTable },
{ NULL, NULL }
};
state.RegisterMethods(regTable);
}
///
/// ijԱעclass table
///
template<class TYPE, class BASE>
void LuaxNativeClass<TYPE, BASE>::RegisterLuaxSingletonClass(LuaxState& state)
{
luaL_Reg regTable[] = {
{ NULL, NULL }
};
state.RegisterMethods(regTable);
}
template<class TYPE, class BASE>
void LuaxNativeClass<TYPE, BASE>::PushLuaxClassTable(LuaxState& state)
{
assert(mClassTable);
mClassTable.PushRef(state);
}
template<class TYPE, class BASE>
void LuaxNativeClass<TYPE, BASE>::SetLuaxClassTableRef(LuaxState& state, int idx)
{
mClassTable.SetRef(state, idx);
}
template<class TYPE, class BASE>
LuaxNativeClass<TYPE, BASE>::LuaxNativeClass()
: mWatchDog()
#if LUAX_PROFILER
, mSafer(false)
#endif
{
}
template<class TYPE, class BASE>
LuaxNativeClass<TYPE, BASE>::~LuaxNativeClass()
{
}
#if LUAX_PROFILER
template<class TYPE, class BASE>
void LuaxNativeClass<TYPE, BASE>::operator delete(void* pdead, size_t size)
{
if (pdead == nullptr)
return;
// ϴʵʹReleaseͷš
TYPE* p = static_cast<TYPE*>(pdead);
assert(p->mSafer);
::operator delete(pdead, size);
}
#endif
template<class TYPE, class BASE>
void LuaxNativeClass<TYPE, BASE>::Retain()
{
++mWatchDog.mNativeRef;
}
template<class TYPE, class BASE>
void LuaxNativeClass<TYPE, BASE>::Release()
{
if (mWatchDog.mNativeRef > 0)
--mWatchDog.mNativeRef;
if (mWatchDog)
{
#if LUAX_PROFILER
mSafer = true;
#endif
delete this;
}
}
template<class TYPE, class BASE>
template<typename U>
void LuaxNativeClass<TYPE, BASE>::LuaxRetain(LuaxState& state, U* userdata)
{
if (PushLuaxRefTable(state))
{
if (userdata->PushLuaxUserdata(state))
{
lua_pushvalue(state, -1); // copy the userdata
lua_gettable(state, -3); // get the count (or nil)
u32 count = state.GetValue<u32>(-1, 0); // get the count (or 0)
lua_pop(state, 1); // pop the old count
lua_pushnumber(state, count + 1); // push the new count
lua_settable(state, -3); // save it in the table: reftable[userdata] = count
}
}
}
template<class TYPE, class BASE>
template<typename U>
void LuaxNativeClass<TYPE, BASE>::LuaxRelease(LuaxState& state, U* userdata)
{
if (PushLuaxRefTable(state))
{
if (userdata->PushLuaxUserdata(state))
{
lua_pushvalue(state, -1); // copy the userdata
lua_gettable(state, -3); // get the count (or nil)
u32 count = state.GetValue<u32>(-1, 0); // get the count (or 0)
lua_pop(state, 1); // pop the old count
// no such reference
if (count == 0)
{
state.Pop(2); // userdata, reftable
return; // nothing to do
}
if (count > 1) {
lua_pushnumber(state, count - 1); // push the new count
}
else {
lua_pushnil(state); // maybe cause gc
}
lua_settable(state, -3); // save it in the table
state.Pop(1); // reftable
return;
}
state.Pop(2); // nil, reftable
return;
}
}
template<class TYPE, class BASE>
bool LuaxNativeClass<TYPE, BASE>::PushLuaxUserdata(LuaxState& state)
{
assert(!TYPE::IsLuaxClassSingleton());
if (!mUserdata)
{
BindToLua(state);
return true;
}
return mUserdata.PushRef(state);
}
template<class TYPE, class BASE>
bool LuaxNativeClass<TYPE, BASE>::PushLuaxMemberTable(LuaxState& state)
{
int top = state.GetTop();
if (this->PushLuaxUserdata(state))
{
if (lua_getmetatable(state, -1)) // ref table
{
lua_replace(state, -2);
if (lua_getmetatable(state, -1)) // member table
{
lua_replace(state, -2);
return true;
}
}
}
lua_settop(state, top);
lua_pushnil(state);
return false;
}
template<class TYPE, class BASE>
bool LuaxNativeClass<TYPE, BASE>::PushLuaxRefTable(LuaxState& state)
{
// Singleton
if (TYPE::IsLuaxClassSingleton())
{
if (!this->mSingletonRefTable) {
lua_newtable(state);
this->mSingletonRefTable.SetRef(state, -1); // strong ref to member table won't be garbage collected
}
else {
this->mSingletonRefTable.PushRef(state);
}
return true;
}
// Factory
else
{
if (this->PushLuaxUserdata(state))
{
if (lua_getmetatable(state, -1))
{
lua_replace(state, -2);
return true;
}
}
}
return false;
}
///
/// userdataԴref tablemember tableclass table
/// ref table kvǿtableuserdataüͨuserdataΪkey
/// ΪvalueԼԱ
/// member table luaʵijԱ
/// class table б͵ʵеĺ
///
/// BindToLuaֻڵһעLuaʱá
///
template<class TYPE, class BASE>
void LuaxNativeClass<TYPE, BASE>::BindToLua(LuaxState& state)
{
// ܰuserdata
assert(!TYPE::IsLuaxClassSingleton());
assert(!mUserdata);
///
/// userdataջעַҪתΪTYPE*ֱthisܻᵼ¶ؼ̳еɥʧ̬
/// ֱӴthisȥڶؼ̳£òһͷ麯ġҪthis
/// תΪĵ͵ַõһ麯ͨһʵֶ̬
///
TYPE* p = static_cast<TYPE*>(this);
state.PushPtrUserdata(p);
lua_newtable(state); // ref tableluaʣC
lua_newtable(state); // member tableluaдĶԱ
PushLuaxClassTable(state); // class table
// stack:
// -1: class table
// -2: member table
// -3: ref table
// -4: userdata
int top = state.GetTop();
int memberTable = top - 1;
int refTable = top - 2;
// ref table ע __tostring __gc
lua_pushcfunction(state, __tostring);
lua_setfield(state, refTable, "__tostring");
lua_pushcfunction(state, __gc);
lua_setfield(state, refTable, "__gc");
// ref table __index __newindex Ϊ member table
lua_pushvalue(state, memberTable);
lua_setfield(state, refTable, "__index");
lua_pushvalue(state, memberTable);
lua_setfield(state, refTable, "__newindex");
// Ԫ
lua_setmetatable(state, -2); // class is meta of member
lua_setmetatable(state, -2); // member is meta of ref
lua_setmetatable(state, -2); // ref is meta of userdata
// һuserdataãͨPushLuaUserdatalua
mUserdata.SetRef(state, -1);
assert(mUserdata);
// һãGCʱ-1
++mWatchDog.mVMRef;
#if LUAX_PROFILER
mRefVMs.insert(state.GetVM());
#endif
}
///
/// Աù
///
template<class TYPE, class BASE>
void LuaxNativeClass<TYPE, BASE>::SetLuaxMemberRef(LuaxState& state, LuaxMemberRef& memRef, int idx)
{
ClearLuaxMemberRef(state, memRef);
if (!lua_isnil(state, idx))
{
idx = state.AbsIndex(idx);
if (PushLuaxRefTable(state))
{
lua_pushvalue(state, idx);
memRef.refID = luaL_ref(state, -2);
state.Pop(); // ref table
}
}
}
template<class TYPE, class BASE>
bool LuaxNativeClass<TYPE, BASE>::PushLuaxMemberRef(LuaxState& state, LuaxMemberRef& memRef)
{
if (memRef)
{
if (PushLuaxRefTable(state))
{
lua_rawgeti(state, -1, memRef.refID);
lua_replace(state, -2); // ref table
if (lua_isnil(state, -1))
goto failed;
return true;
}
}
lua_pushnil(state);
failed:
memRef.refID = LUA_NOREF;
return false;
}
template<class TYPE, class BASE>
bool LuaxNativeClass<TYPE, BASE>::PushLuaxMemberRef(LuaxState& state, int refID)
{
if (PushLuaxRefTable(state))
{
lua_rawgeti(state, -1, refID);
lua_replace(state, -2); // ref table
if (lua_isnil(state, -1))
goto failed;
return true;
}
lua_pushnil(state);
failed:
return false;
}
template<class TYPE, class BASE>
void LuaxNativeClass<TYPE, BASE>::ClearLuaxMemberRef(LuaxState& state, LuaxMemberRef& memRef)
{
if (memRef)
{
if (PushLuaxRefTable(state))
{
luaL_unref(state, -1, memRef.refID);
state.Pop(); // ref table
}
memRef.refID = LUA_NOREF;
}
}
//--------------------------------------------------------------------------------//
///
/// ͷŹʵ
///
template<class TYPE, class BASE>
int LuaxNativeClass<TYPE, BASE>::__gc(lua_State* L)
{
LUAX_STATE(L);
TYPE* self = state.GetUserdata<TYPE>(1);
assert(self);
#if LUAX_PROFILER
std::cout << "Luax: GC<" << TYPE::GetLuaxClassName() << ">\n";
#endif
if(self->mWatchDog.mVMRef > 0)
--self->mWatchDog.mVMRef;
self->Release();
return 0;
}
///
/// ʽ:
/// ַ
///
template<class TYPE, class BASE>
int LuaxNativeClass<TYPE, BASE>::__tostring(lua_State* L)
{
// params:
// 1: userdata
LUAX_STATE(L);
TYPE* self = state.GetUserdata<TYPE>(1);
if (self)
{
cc8* classname = "";
lua_getfield(state, 1, "GetClassName");
if (state.IsType(-1, LUA_TFUNCTION))
{
lua_pushvalue(L, 1); // userdata
state.Call(1, 1); // GetClassName
classname = state.GetValue<cc8*>(-1, "");
}
else
{
classname = TYPE::GetLuaxClassName();
}
lua_pushfstring(L, "%s: %p", classname, self);
return 1;
}
return 0;
}
#if LUAX_ENABLE_NATIVE_EXTEND
///
/// ࣬luaijԱΪƣDZ֤userdataͳһNative classṩ__init֧֣
/// nativeʵ崴ʹ__initгʼӵкͻһNewбnativeһ͡
///
template<class TYPE, class BASE>
int LuaxNativeClass<TYPE, BASE>::_ExtendFactory(lua_State* L)
{
// upvalues:
// 1: base class
// params:
// 1: class name
int baseClass = lua_upvalueindex(1);
lua_newtable(L); // class table
int inheritClass = lua_gettop(L);
// .GetClassName()
cc8* type = lua_tostring(L, 1);
lua_pushstring(L, type);
lua_pushcclosure(L, luax_c_getupvalue, 1);
lua_setfield(L, -2, "GetClassName");
// .GetClass()
lua_pushvalue(L, inheritClass);
lua_pushcclosure(L, luax_c_getupvalue, 1);
lua_setfield(L, -2, "GetClass");
// .Extend()
lua_pushvalue(L, inheritClass);
lua_pushcclosure(L, _ExtendFactory, 1);
lua_setfield(L, -2, "Extend");
// .New()
lua_pushvalue(L, inheritClass);
lua_getfield(L, baseClass, "New");
lua_pushcclosure(L, _New, 2);
lua_setfield(L, -2, "New");
// __base = baseClass
lua_pushvalue(L, baseClass);
lua_setfield(L, -2, "__base");
// __index = inheritClass
lua_pushvalue(L, inheritClass);
lua_setfield(L, -2, "__index");
// metatable is baseClass
lua_pushvalue(L, baseClass);
lua_setmetatable(L, inheritClass);
return 1;
}
template<class TYPE, class BASE>
int LuaxNativeClass<TYPE, BASE>::_ExtendSingleton(lua_State* L)
{
// upvalues:
// 1: base class
// params:
// 1: class name
int baseClass = lua_upvalueindex(1);
lua_newtable(L); // class name
int inheritClass = lua_gettop(L);
// .GetClassName()
cc8* type = lua_tostring(L, 1);
lua_pushstring(L, type);
lua_pushcclosure(L, luax_c_getupvalue, 1);
lua_setfield(L, -2, "GetClassName");
// .GetClass()
lua_pushvalue(L, inheritClass);
lua_pushcclosure(L, luax_c_getupvalue, 1);
lua_setfield(L, -2, "GetClass");
// .Extend()
lua_pushvalue(L, inheritClass);
lua_pushcclosure(L, _ExtendFactory, 1);
lua_setfield(L, -2, "Extend");
// __base = baseClass
lua_pushvalue(L, baseClass);
lua_setfield(L, -2, "__base");
// __index = inheritClass
lua_pushvalue(L, inheritClass);
lua_setfield(L, -2, "__index");
// metatable is baseClass
lua_pushvalue(L, baseClass);
lua_setmetatable(L, inheritClass);
return 1;
}
#endif /*LUAX_ENABLE_NATIVE_EXTEND*/
template<class TYPE, class BASE>
int LuaxNativeClass<TYPE, BASE>::_GetClass(lua_State* L)
{
LUAX_STATE(L);
if (!mClassTable)
lua_pushnil(L);
else
mClassTable.PushRef(state);
return 1;
}
template<class TYPE, class BASE>
int LuaxNativeClass<TYPE, BASE>::_GetRefTable(lua_State* L)
{
LUAX_STATE(L);
TYPE* self = state.GetUserdata<TYPE>(1);
bool success = self->PushLuaxRefTable(state);
if (!success)
lua_pushnil(L);
return 1;
}
template<class TYPE, class BASE>
int LuaxNativeClass<TYPE, BASE>::_New(lua_State* L)
{
LUAX_STATE(L);
// upvalues:
// 1: class table
// 2: original New()
// stack:
// -1~-n: args
int n = lua_gettop(L); // n args
lua_pushvalue(L, lua_upvalueindex(2));
if (state.IsType(-1, LUA_TFUNCTION))
{
// stack:
// -1: New
// -2~-1-n: args
state.PushValues(-1 - n, n);
// stack:
// -1~-n: args
// -n-1: New
// -n-2~-1-2n: args
state.Call(n, 1);
// stack:
// -1: userdata
// -2~-1-n: args
// reset member table's metatable to class table
if (state.IsType(-1, LUA_TUSERDATA))
{
if (lua_getmetatable(L, -1)) // ref table
{
if (lua_getmetatable(L, -1)) // member table
{
lua_pushvalue(L, lua_upvalueindex(1)); // class table
lua_setmetatable(L, -2);
state.Pop(); // member table
}
state.Pop(); // ref table
}
// stack:
// -1: userdata
// -2~-1-n: args
int args = state.AbsIndex(-1 - n);
// Ե__init
lua_getfield(L, lua_upvalueindex(1), "__init");
if (state.IsType(-1, LUA_TFUNCTION))
{
lua_pushvalue(L, -2); // userdata
state.PushValues(args, n);
state.Call(n + 1, 0);
}
else
state.Pop();
}
return 1;
}
return 0;
}
template<class TYPE, class BASE> LuaxStrongRef LuaxNativeClass<TYPE, BASE>::mClassTable; // class table
template<class TYPE, class BASE> LuaxStrongRef LuaxNativeClass<TYPE, BASE>::mSingletonRefTable; //
}
|