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
|
#include "LuaBindClass.hpp"
#include "LuaBindCFunctions.h"
#include "LuaBindVM.h"
namespace LuaBind
{
UIDGenerator gClassIDGenerator;
#if LUA_BIND_ENABLE_PLAIN_CLASS
int PlainClass::registry(lua_State* L)
{
LUA_BIND_STATE(L);
// params:
// 1: class name
cc8* type = state.GetValue<cc8*>(1, "");
lua_newtable(L); // class table
// GetClassName()
lua_pushstring(L, type);
lua_pushcclosure(L, luax_c_getupvalue, 1);
lua_setfield(L, -2, "GetClassName");
// GetClass()
lua_pushvalue(L, -1); // class table
lua_pushcclosure(L, luax_c_getupvalue, 1);
lua_setfield(L, -2, "GetClass");
// TypeOf()
lua_pushcfunction(L, _TypeOf);
lua_setfield(L, -2, "TypeOf");
// New()
lua_pushvalue(L, -1); // class table
lua_pushcclosure(L, _New, 1);
lua_setfield(L, -2, "New");
// Extend()
lua_pushvalue(L, -1); // class table
lua_pushcclosure(L, _Extend, 1);
lua_setfield(L, -2, "Extend");
lua_pushvalue(L, -1); // class table
lua_setfield(L, -2, "__index");
lua_pushstring(L, type);
lua_pushcclosure(L, __tostring, 1);
lua_setfield(L, -2, "__tostring");
return 1;
}
int PlainClass::__tostring(lua_State* L)
{
// upvalues:
// 1: class name
// params:
// 1: instance
if (!lua_istable(L, 1))
{
return luaL_typerror(L, 1, lua_typename(L, LUA_TTABLE));
}
cc8* type = lua_tostring(L, lua_upvalueindex(1));
lua_pushfstring(L, "%s: %p", type, lua_topointer(L, 1));
return 1;
}
//
// New函数接受n个参数,并尝试获取__init,将参数传给__init初始化实例。
//
int PlainClass::_New(lua_State* L)
{
LUA_BIND_STATE(L);
// upvalues:
// 1: class table
// params:
// n: params
int n = lua_gettop(L);
int classTable = lua_upvalueindex(1);
lua_newtable(L); // instance table
// instance 的 metatable 设置为 class
lua_pushvalue(L, classTable);
lua_setmetatable(L, -2);
// 找到构造函数,会触发metatable.__index,根据继承链向上找。
lua_getfield(L, classTable, "__init");
if (state.IsType(-1, LUA_TFUNCTION))
{
// stack:
// -1: __init()
// -2: instance
// -3~-n-2: params
lua_insert(L, -2 - n);
// stack:
// -1: instance
// -2~-n-1: params
// -n-2: __init()
lua_pushvalue(L, -1);
// stack:
// -1: instance
// -2: instance
// -3~-n-2: params
// -n-3: __init
lua_insert(L, -3 - n);
// stack:
// -1: instance
// -2~-n-1: params
// -n-2: __init()
// -n-3: instance
lua_insert(L, -1 - n);
// stack:
// -1~-n: params
// -n-1: instance
// -n-2: __init()
// -n-3: instance
lua_pcall(L, n + 1, 0, 0);
}
else
{
state.Pop();
}
return 1;
}
int PlainClass::_Extend(lua_State* L)
{
LUA_BIND_STATE(L);
// upvalues:
// 1: base class
// params:
// 1: class name
cc8* type = state.GetValue<cc8*>(1, "");
int baseClass = lua_upvalueindex(1);
lua_newtable(L); // class table
// GetClassName()
lua_pushstring(L, type);
lua_pushcclosure(L, luax_c_getupvalue, 1);
lua_setfield(L, -2, "GetClassName");
// GetClass()
lua_pushvalue(L, -1); // class table
lua_pushcclosure(L, luax_c_getupvalue, 1);
lua_setfield(L, -2, "GetClass");
// New()
lua_pushvalue(L, -1); // class table
lua_pushcclosure(L, _New, 1);
lua_setfield(L, -2, "New");
// Extend()
lua_pushvalue(L, -1); // class table
lua_pushcclosure(L, _Extend, 1);
lua_setfield(L, -2, "Extend");
// .__base 用来索引到基类
lua_pushvalue(L, baseClass); // base class
lua_setfield(L, -2, "__base");
lua_pushvalue(L, -1); // class table
lua_setfield(L, -2, "__index");
lua_pushstring(L, type);
lua_pushcclosure(L, __tostring, 1);
lua_setfield(L, -2, "__tostring");
// class的metatable设置为baseClass
lua_pushvalue(L, baseClass);
lua_setmetatable(L, -2);
return 1;
}
int PlainClass::_TypeOf(lua_State* L)
{
// params:
// 1: lua instance
// 2: type string
LUA_BIND_STATE(L);
cc8* type = state.GetValue<cc8*>(2, "");
if (!lua_istable(L, 1))
{
return luaL_typerror(L, 1, "Object");
}
lua_pushvalue(L, 1); // lua instance
while (lua_getmetatable(L, -1))
{
lua_getfield(L, -1, "GetClassName");
if (lua_isfunction(L, -1))
{
state.Call(0, 1);
cc8* name = state.GetValue<cc8*>(-1, "");
if (strcmp(name, type) == 0)
{
lua_pushboolean(L, true);
return 1;
}
else
{
state.Pop(); // name
}
}
else
{
state.Pop();
}
}
lua_pushboolean(L, false);
return 1;
}
#endif /*LUA_BIND_ENABLE_PLAIN_CLASS*/
}
|