Metatable
要理解lua的class,首先要先理解Metatable
的作用和__index
以及lua调用table里面的函数的时候搜索函数的逻辑:
1、直接当前表里面搜索函数 如果存在,直接调用,不存在继续
2、如果表里面不存在调用的函数,会查找表的Metatable的__index
a、如果__index是一个表,则在该表里面查找,回到第一步
b、如果__index是一个函数,则传递要查找的表、函数名字给__index这个函数,并返回函数的返回结果。如果函数返回一个函数则执行该函数,或者直接提示找不到函数,或者返回另外一个表,则又回到第一步在这个表里面查找
伪代码如下:
"index"@H_404_18@: The indexing access table@H_404_18@[key].
function@H_404_18@ gettable_event@H_404_18@ (table,key)@H_404_18@@H_404_18@
local@H_404_18@ h
if@H_404_18@ type@H_404_18@(table@H_404_18@) == "table"@H_404_18@ then@H_404_18@
local@H_404_18@ v = rawget@H_404_18@(table@H_404_18@,key)
if@H_404_18@ v ~= nil@H_404_18@ then@H_404_18@ return@H_404_18@ v end@H_404_18@
h = Metatable(table@H_404_18@).__index
if@H_404_18@ h == nil@H_404_18@ then@H_404_18@ return@H_404_18@ nil@H_404_18@ end@H_404_18@
else@H_404_18@
h = Metatable(table@H_404_18@).__index
if@H_404_18@ h == nil@H_404_18@ then@H_404_18@
error@H_404_18@(···)
end@H_404_18@
end@H_404_18@
if@H_404_18@ type@H_404_18@(h) == "function"@H_404_18@ then@H_404_18@
return@H_404_18@ (h(table@H_404_18@,key)) -- call the handler@H_404_18@
else@H_404_18@ return@H_404_18@ h[key] -- or repeat operation on it@H_404_18@
end@H_404_18@
end@H_404_18@
cocos2dx-lua
的class实现
下面来看看cocos2dx
的lua的class的实现方式
class的声明
function@H_404_18@ class@H_404_18@(classname,...)@H_404_18@@H_404_18@
local@H_404_18@ cls = {__cname = classname} --设置类的名字@H_404_18@
local@H_404_18@ supers = {...}
for@H_404_18@ _,super in@H_404_18@ ipairs@H_404_18@(supers) do@H_404_18@ --遍历第二个参数@H_404_18@
local@H_404_18@ superType = type@H_404_18@(super)
assert@H_404_18@(superType == "nil"@H_404_18@ or@H_404_18@ superType == "table"@H_404_18@ or@H_404_18@ superType == "function"@H_404_18@,string@H_404_18@.format("class() - create class \"%s\" with invalid super class type \"%s\""@H_404_18@,classname,superType))
if@H_404_18@ superType == "function” then --如果参数是一个函数 则将这个函数设置为类的__create函数 assert(cls.__create == nil,string.format("@H_404_18@class() - create class \"%s\" with more than one creating function"@H_404_18@,classname));
-- if super is function,set it to __create@H_404_18@
cls.__create = super
elseif@H_404_18@ superType == "table” then --如果是一个表 if super["@H_404_18@.isclass"] then --如果是一个c++的对象 -- super is native class assert(cls.__create == nil,string.format("@H_404_18@class() - create class \"%s\" with more than one creating function or native class"@H_404_18@,classname));
cls.__create = function@H_404_18@()@H_404_18@@H_404_18@ return@H_404_18@ super:create() end@H_404_18@ --__create函数就等于调用c++类的create函数@H_404_18@
else@H_404_18@
-- super is pure lua class@H_404_18@
cls.__supers = cls.__supers or@H_404_18@ {} --设置@H_404_18@
cls.__supers[#cls.__supers + 1@H_404_18@] = super
if@H_404_18@ not@H_404_18@ cls.super then@H_404_18@
-- set first super pure lua class as class.super@H_404_18@
cls.super = super
end@H_404_18@
end@H_404_18@
else@H_404_18@
error@H_404_18@(string@H_404_18@.format("class() - create class \"%s\" with invalid super type"@H_404_18@,classname),0@H_404_18@)
end@H_404_18@
end@H_404_18@
cls.__index = cls
if@H_404_18@ not@H_404_18@ cls.__supers or@H_404_18@ #cls.__supers == 1@H_404_18@ then@H_404_18@ --如果只有一个父类 则设置Metatable为一个表@H_404_18@
setMetatable@H_404_18@(cls,{__index = cls.super})
else@H_404_18@
setMetatable@H_404_18@(cls,{__index = function@H_404_18@(_,key)@H_404_18@@H_404_18@ --如果有多个父类,则设置Metatable为一个函数,通过函数查找对应的函数@H_404_18@
local@H_404_18@ supers = cls.__supers
for@H_404_18@ i = 1@H_404_18@,#supers do@H_404_18@
local@H_404_18@ super = supers[i]
if@H_404_18@ super[key] then@H_404_18@ return@H_404_18@ super[key] end@H_404_18@
end@H_404_18@
end@H_404_18@})
end@H_404_18@
if@H_404_18@ not@H_404_18@ cls.ctor then@H_404_18@
-- add default constructor@H_404_18@
cls.ctor = function@H_404_18@()@H_404_18@@H_404_18@ end@H_404_18@
end@H_404_18@
cls.new = function@H_404_18@(...)@H_404_18@@H_404_18@
local@H_404_18@ instance
if@H_404_18@ cls.__create then@H_404_18@
instance = cls.__create(...)
else@H_404_18@
instance = {}
end@H_404_18@
setMetatableindex(instance,cls)
instance.class = cls
instance:ctor(...)
return@H_404_18@ instance
end@H_404_18@
cls.create = function@H_404_18@(_,...)@H_404_18@@H_404_18@
return@H_404_18@ cls.new(...)
end@H_404_18@
return@H_404_18@ cls
end@H_404_18@
local@H_404_18@ setMetatableindex_
setMetatableindex_ = function@H_404_18@(t,index)@H_404_18@@H_404_18@
if@H_404_18@ type@H_404_18@(t) == "userdata"@H_404_18@ then@H_404_18@
local@H_404_18@ peer = tolua.getpeer(t)
if@H_404_18@ not@H_404_18@ peer then@H_404_18@
peer = {}
tolua.setpeer(t,peer)
end@H_404_18@
setMetatableindex_(peer,index)
else@H_404_18@
local@H_404_18@ mt = getMetatable@H_404_18@(t)
if@H_404_18@ not@H_404_18@ mt then@H_404_18@ mt = {} end@H_404_18@
if@H_404_18@ not@H_404_18@ mt.__index then@H_404_18@
mt.__index = index
setMetatable@H_404_18@(t,mt)
elseif@H_404_18@ mt.__index ~= index then@H_404_18@
setMetatableindex_(mt,index) --另外总觉得这一句有问题,不是应该是setMetatableindex(mt.__index,index)吗,如果__index是函数,也应该扩展让其会搜索其Metatable或者自身之后,再使用setMetatableindex(mt,index),才会生效,请大神指点迷津@H_404_18@
end@H_404_18@
end@H_404_18@
end@H_404_18@
setMetatableindex = setMetatableindex_
这里有两种情况:
对于从C++对象派生的情况,new出来的实际上并不是一个
table
而是一个userdata
这个时候其函数从两个地方来:而对于从纯lua对象派生的类,new出来的实例也是一个table
关于C++类搜索key的猜测和测试
对于a的问题,目前猜测是这样,因为Metatable的__index是一个函数,所以我猜测是这个函数做了一些特殊操作,因此进行了如下实验:
因为我们知道当在一个表或者userdata中找不到某个域的时候,回去__index中查找,如果还是__index是一个函数,则传入__index的参数是这个userdata或者表本身和要查找的域的名字,所以我们考虑设置__index函数为如下的函数:
function@H_404_18@ myindex@H_404_18@(t,key)@H_404_18@@H_404_18@
return@H_404_18@ getMetatable@H_404_18@(t)[key]
end@H_404_18@
这样就告诉表或者userdata,如果找不到某个函数,就将自己和key传递个__index然后从自己的Metatable中查找。实验如下:
local@H_404_18@ Class1 = {}
function@H_404_18@ Class1:test@H_404_18@()@H_404_18@@H_404_18@
print@H_404_18@('test'@H_404_18@)
end@H_404_18@
local@H_404_18@ Class2 = {}
local@H_404_18@ myIndex = function@H_404_18@(t,key)@H_404_18@@H_404_18@
return@H_404_18@ getMetatable@H_404_18@(t)[key]
end@H_404_18@
Class1.__index = myIndex
function@H_404_18@ Class2:new@H_404_18@()@H_404_18@@H_404_18@
local@H_404_18@ o = {}
local@H_404_18@ t = {__index = myIndex}
setMetatable@H_404_18@(o,t)
local@H_404_18@ t1 = {__index = myIndex}
setMetatable@H_404_18@(t,t1)
setMetatable@H_404_18@(t1,Class1)
return@H_404_18@ o
end@H_404_18@
local@H_404_18@ ta = Class2.new()
ta:test()
这段代码输出为test,也就是说可以调用到Class1的test函数
上面的代码Class1就只是o
的Metatable
的Metatable
的Metatable
,和我们cocos2dx返回的userdata的结构类似了
我们来看看这段代码是如何做到的:
来看调用关系
- 在ta中查找,找不到,继续
- ta中没有通过ta的Metatable(t)的__index查找,__index为函数,则将ta和’test’作为参数传递给该函数,该函数通过getMetatable(ta)即(t)查找,t中也没有,继续
- t中没有,通过t的Metatable(t1)的__index查找,也为函数,将t和’test’作为参数传递给函数,通过getMetatable(t)即(t1)中查找,t1中也没有,继续
- t1中没有,通过t1的Metatable(Class1)的__index查找,为函数,将t1和’test’作为参数传递给函数,通过getMetatable(t1)即(Class1)查找,找到,所以返回Class1的test函数
说明这种实现方式是可行的,那么再来看看cocos2dx是否是这样实现的。
通过创建一个派生自cocos2dx
的一个userdata
test
,然后尝试输出下面的函数
print@H_404_18@(getMetatable@H_404_18@(test).__index(test,"visit”))@H_404_18@
输出为函数,说明找到了函数,然后我们尝试重载一个函数来看看。
local@H_404_18@ tmpMetatablefunc = getMetatable@H_404_18@
getMetatable@H_404_18@ = function@H_404_18@(ta)@H_404_18@@H_404_18@
print@H_404_18@(ta)
return@H_404_18@ tmpMetatablefunc(ta)
end@H_404_18@
function@H_404_18@ ViewBase:setPosition@H_404_18@(x,y)@H_404_18@@H_404_18@
print@H_404_18@("--------"@H_404_18@)
local@H_404_18@ tmpindex = getMetatable@H_404_18@(self).__index
getMetatable@H_404_18@(self).__index = function@H_404_18@(t1,key)@H_404_18@@H_404_18@ print@H_404_18@(t1,key) return@H_404_18@ tmpindex(t1,key) end@H_404_18@
local@H_404_18@ a = getMetatable@H_404_18@(self).__index(self,'setPosition'@H_404_18@)
print@H_404_18@(a)
local@H_404_18@ b = getMetatable@H_404_18@(self)['setPosition'@H_404_18@]
print@H_404_18@("ttttttttttt"@H_404_18@)
print@H_404_18@(a == b)
print@H_404_18@("callfunction"@H_404_18@)
a(self,x,y)
--getMetatable(self)['setPosition'](self,y)@H_404_18@
print@H_404_18@("viewbase.setPosition"@H_404_18@)
end@H_404_18@
通过__index来获取setPosition函数
当ViewBase重新定一个setPosition函数的时候,如果用这种方式来重载的时候,我发现出现了递归调用,通过上面的测试代码发现是因为通过__index查找到的函数就是我们在这里定义的ViewBase:setPosition本身,而不是我们想调用的其基类的setPosition。由此还可以推断出另外一个事实,那就是tolua++中__index并不是仅仅搜索Metatable,还是对tolua.getpeer进行搜索,因为通过上面的Class的定义我们知道ViewBase是放在tolua.getpeer的表的Metatale的__index中的。但是这里还没有证明__index会搜索getMetatable的key。
local@H_404_18@ tmpMetatablefunc = getMetatable@H_404_18@
getMetatable@H_404_18@ = function@H_404_18@(ta)@H_404_18@@H_404_18@
print@H_404_18@(ta)
return@H_404_18@ tmpMetatablefunc(ta)
end@H_404_18@
function@H_404_18@ ViewBase:setPosition1@H_404_18@(x,y)@H_404_18@
print@H_404_18@("viewbase.setPosition"@H_404_18@)
end@H_404_18@
我们重新定义的函数叫setPosition1,这样搜索的时候就不会在tolua.getpeer中找到setPosition了。然后调用setPosition1来设置位置,发现成功了,那就证明
__index函数确实会搜索Metatable的域了。
并且综合上面的分析,还可以知道其搜索顺序是先搜索tolua.getpeer中的函数,再搜索Metatable,这样就可以做到优先使用我们重新定义的函数而不是积累的函数,做到重定义父类函数的功能。
这里分析了这么多,其实还不如直接去看tolua++的源代码,不过因为时间有限,对lua的c接口还不熟悉,所以仅先通过在lua这边看到的现象进行一些分析知其然,以后再仇视时间区看看tolua++代码和lua代码,之其所以然吧。
对于C++类的派生,创建的时候其实是先从C++创建一个原始的userdata,然后通过设置其getpeer的Metatable来扩展其成员函数
其搜索顺序应该是先b后a
如果访问的是C++的原生函数,则是从getMetatable获取到,如果是派生出来的函数,则通过tolua.getpeer的表来得到其类或者父类的域
这个时候其函数和成员变量都来自于
instance[funcname] ——————>instance自身以及其Metatable的__index域(递归)
这里是直接创建一个表,然后将其Metatable指向其类,然后在访问instance的域的时候就会找到其类或者父类的域了
而如果要调用父类的函数而不是调用自己重载的函数,可以使用如下函数:
function@H_404_18@ getBaseFunc@H_404_18@(data,name)@H_404_18@@H_404_18@
local@H_404_18@ a
if@H_404_18@ data.super ~= nil@H_404_18@ then@H_404_18@
a = data.super[a]
end@H_404_18@
if@H_404_18@ a == nil@H_404_18@ then@H_404_18@
a = getMetatable@H_404_18@(data)[a]
end@H_404_18@
return@H_404_18@ a
end@H_404_18@
如果调用指定级数的函数
可以直接使用ClassName.func(self,param)
cocos2dx搜索key的实现
/* Register@H_404_18@ a usertype
* It@H_404_18@ creates the correspoding Metatable in@H_404_18@ the registry,for both 'type@H_404_18@' and 'const type@H_404_18@'.@H_404_18@
* It@H_404_18@ maps 'const type@H_404_18@' as being also a 'type@H_404_18@'@H_404_18@
*/
TOLUA_API@H_404_18@ void tolua_usertype (lua_State* L@H_404_18@,const char* type@H_404_18@)@H_404_18@
{
char ctype[128@H_404_18@] = "const "@H_404_18@;
strncat(ctype,type@H_404_18@,120);@H_404_18@
/* create both Metatables */
if@H_404_18@ (tolua_newMetatable(L@H_404_18@,ctype) && tolua_newMetatable(L@H_404_18@,type@H_404_18@))@H_404_18@
mapsuper(L@H_404_18@,ctype); /* 'type@H_404_18@' is also a 'const type@H_404_18@' */@H_404_18@
}
在这个函数中调用了tolua_newMetatable
函数
2. tolua_newMetatable 创建Metatable
/* Create Metatable * Create and register new Metatable */@H_404_18@
static@H_404_18@ int@H_404_18@ tolua_newMetatable (lua_State* L,const@H_404_18@ char@H_404_18@* name)
{
int@H_404_18@ r = luaL_newMetatable(L,name);
#ifdef LUA_VERSION_NUM /* only lua 5.1 */@H_404_18@
if@H_404_18@ (r) {
lua_pushvalue(L,-1@H_404_18@);
lua_pushstring(L,name);
lua_settable(L,LUA_REGISTRYINDEX); /* reg[mt] = type_name */@H_404_18@
};
#endif@H_404_18@@H_404_18@
if@H_404_18@ (r)
tolua_classevents(L); /* set Meta events */@H_404_18@
lua_pop(L,1@H_404_18@);
return@H_404_18@ r;
}
这个函数中就调用了tolua_classevent来设置Metatable的__index函数等Metafunction
3. tolua_classevents 设置么他function,其中就包括__index函数
/* Register class events
* It expects the Metatable on the top of the stack
*/
TOLUA_API@H_404_18@ void tolua_classevents@H_404_18@ (lua_State@H_404_18@* L)
{
lua_pushstring@H_404_18@(L,"__index"@H_404_18@);@H_404_18@
lua_pushcfunction@H_404_18@(L,class_index_event);@H_404_18@
lua_rawset@H_404_18@(L,-3@H_404_18@);@H_404_18@
lua_pushstring@H_404_18@(L,"__newindex"@H_404_18@);@H_404_18@
lua_pushcfunction@H_404_18@(L,class_newindex_event);@H_404_18@
lua_rawset@H_404_18@(L,-3@H_404_18@);@H_404_18@
lua_pushstring@H_404_18@(L,"__add"@H_404_18@);@H_404_18@
lua_pushcfunction@H_404_18@(L,class_add_event);@H_404_18@
lua_rawset@H_404_18@(L,"__sub"@H_404_18@);@H_404_18@
lua_pushcfunction@H_404_18@(L,class_sub_event);@H_404_18@
lua_rawset@H_404_18@(L,"__mul"@H_404_18@);@H_404_18@
lua_pushcfunction@H_404_18@(L,class_mul_event);@H_404_18@
lua_rawset@H_404_18@(L,"__div"@H_404_18@);@H_404_18@
lua_pushcfunction@H_404_18@(L,class_div_event);@H_404_18@
lua_rawset@H_404_18@(L,"__lt"@H_404_18@);@H_404_18@
lua_pushcfunction@H_404_18@(L,class_lt_event);@H_404_18@
lua_rawset@H_404_18@(L,"__le"@H_404_18@);@H_404_18@
lua_pushcfunction@H_404_18@(L,class_le_event);@H_404_18@
lua_rawset@H_404_18@(L,"__eq"@H_404_18@);@H_404_18@
lua_pushcfunction@H_404_18@(L,class_eq_event);@H_404_18@
lua_rawset@H_404_18@(L,"__call"@H_404_18@);@H_404_18@
lua_pushcfunction@H_404_18@(L,class_call_event);@H_404_18@
lua_rawset@H_404_18@(L,"__gc"@H_404_18@);@H_404_18@
lua_pushstring@H_404_18@(L,"tolua_gc_event"@H_404_18@);@H_404_18@
lua_rawget@H_404_18@(L,LUA_REGISTRYINDEX@H_404_18@);@H_404_18@
/*lua_pushcfunction@H_404_18@(L,class_gc_event);*/@H_404_18@
lua_rawset@H_404_18@(L,-3@H_404_18@);@H_404_18@
}
- class_index_event即为真正的搜索key的逻辑了
/* Class index function
* If@H_404_18@ the object is a@H_404_18@ userdata (ie,an object),it searches the field in
* the alternative table stored in the corresponding "uBox"@H_404_18@ table.
*/
static int class_index_event (lua_State@H_404_18@* L)
{
int t = lua_type@H_404_18@(L,1@H_404_18@);@H_404_18@
if@H_404_18@ (t == LUA_TUSERDATA@H_404_18@) //如果是userdata
{
/* Access alternative table */
#ifdef LUA_VERSION@H_404_18@_NUM /* new macro on version 5.1@H_404_18@ */ //从peer里面搜索key
lua_getfenv@H_404_18@(L,1@H_404_18@);@H_404_18@
if@H_404_18@ (!lua_rawequal@H_404_18@(L,-1@H_404_18@,TOLUA_NOPEER@H_404_18@)) {
lua_pushvalue@H_404_18@(L,2@H_404_18@); /* key */@H_404_18@
lua_gettable@H_404_18@(L,-2@H_404_18@); /* on lua 5.1,we trade the "tolua_peers" lookup for a gettable call */@H_404_18@
if@H_404_18@ (!lua_isnil@H_404_18@(L,-1@H_404_18@)) //如果搜索到key,则直接返回
return@H_404_18@ 1@H_404_18@;@H_404_18@
};@H_404_18@
#else@H_404_18@
lua_pushstring@H_404_18@(L,"tolua_peers"@H_404_18@);@H_404_18@
lua_rawget@H_404_18@(L,LUA_REGISTRYINDEX@H_404_18@); /* stack: obj key uBox */@H_404_18@
lua_pushvalue@H_404_18@(L,1@H_404_18@);@H_404_18@
lua_rawget@H_404_18@(L,-2@H_404_18@); /* stack: obj key uBox uBox[u] */@H_404_18@
if@H_404_18@ (lua_istable@H_404_18@(L,-1@H_404_18@))
{
lua_pushvalue@H_404_18@(L,2@H_404_18@); /* key */@H_404_18@
lua_rawget@H_404_18@(L,-2@H_404_18@); /* stack: obj key uBox uBox[u] value */@H_404_18@
if@H_404_18@ (!lua_isnil@H_404_18@(L,-1@H_404_18@))
return@H_404_18@ 1@H_404_18@;@H_404_18@
}
#endif
lua_settop@H_404_18@(L,2@H_404_18@); /* stack: obj key */@H_404_18@
/* Try Metatables */
lua_pushvalue@H_404_18@(L,1@H_404_18@); /* stack: obj key obj */@H_404_18@
while@H_404_18@ (lua_getMetatable@H_404_18@(L,-1@H_404_18@))
{ /* stack:@H_404_18@ obj key obj mt */
lua_remove@H_404_18@(L,-2@H_404_18@); /* stack: obj key mt */@H_404_18@
if@H_404_18@ (lua_isnumber@H_404_18@(L,2@H_404_18@)) /* check if@H_404_18@ key is a@H_404_18@ numeric value */
{
/* try operator[] */
lua_pushstring@H_404_18@(L,".geti"@H_404_18@);@H_404_18@
lua_rawget@H_404_18@(L,-2@H_404_18@); /* stack: obj key mt func */@H_404_18@
if@H_404_18@ (lua_isfunction@H_404_18@(L,-1@H_404_18@))
{
lua_pushvalue@H_404_18@(L,1@H_404_18@);@H_404_18@
lua_pushvalue@H_404_18@(L,2@H_404_18@);@H_404_18@
lua_call@H_404_18@(L,2@H_404_18@,1@H_404_18@);@H_404_18@
return@H_404_18@ 1@H_404_18@;@H_404_18@
}
}
else@H_404_18@
{ //在Metatable中搜索
lua_pushvalue@H_404_18@(L,2@H_404_18@); /* stack: obj key mt key */@H_404_18@
lua_rawget@H_404_18@(L,-2@H_404_18@); /* stack: obj key mt value */@H_404_18@
if@H_404_18@ (!lua_isnil@H_404_18@(L,-1@H_404_18@))
return@H_404_18@ 1@H_404_18@;@H_404_18@
else@H_404_18@
lua_pop@H_404_18@(L,1@H_404_18@);@H_404_18@
/* try C/C++ variable */
lua_pushstring@H_404_18@(L,".get"@H_404_18@); //c++的变量保存在".get"中@H_404_18@
lua_rawget@H_404_18@(L,-2@H_404_18@); /* stack: obj key mt tget */@H_404_18@
if@H_404_18@ (lua_istable@H_404_18@(L,2@H_404_18@);@H_404_18@
lua_rawget@H_404_18@(L,-2@H_404_18@); /* stack: obj key mt value */@H_404_18@
if@H_404_18@ (lua_iscfunction@H_404_18@(L,-1@H_404_18@))
{
lua_pushvalue@H_404_18@(L,1@H_404_18@);@H_404_18@
lua_pushvalue@H_404_18@(L,2@H_404_18@);@H_404_18@
lua_call@H_404_18@(L,1@H_404_18@);@H_404_18@
return@H_404_18@ 1@H_404_18@;@H_404_18@
}
else@H_404_18@ if@H_404_18@ (lua_istable@H_404_18@(L,-1@H_404_18@))
{
/* deal with array:@H_404_18@ create table to be returned and@H_404_18@ cache it in uBox */
void* u = *((void**)lua_touserdata@H_404_18@(L,1@H_404_18@));@H_404_18@
lua_newtable@H_404_18@(L); /* stack: obj key mt value table */@H_404_18@
lua_pushstring@H_404_18@(L,".self"@H_404_18@);@H_404_18@
lua_pushlightuserdata@H_404_18@(L,u);@H_404_18@
lua_rawset@H_404_18@(L,-3@H_404_18@); /* store usertype in ".self" */@H_404_18@
lua_insert@H_404_18@(L,-2@H_404_18@); /* stack: obj key mt table value */@H_404_18@
lua_setMetatable@H_404_18@(L,-2@H_404_18@); /* set stored value as Metatable */@H_404_18@
lua_pushvalue@H_404_18@(L,-1@H_404_18@); /* stack: obj key met table table */@H_404_18@
lua_pushvalue@H_404_18@(L,2@H_404_18@); /* stack: obj key mt table table key */@H_404_18@
lua_insert@H_404_18@(L,-2@H_404_18@); /* stack: obj key mt table key table */@H_404_18@
storeatuBox(L,1@H_404_18@); /* stack: obj key mt table */@H_404_18@
return@H_404_18@ 1@H_404_18@;@H_404_18@
}
}
}
lua_settop@H_404_18@(L,3@H_404_18@);@H_404_18@
}
lua_pushnil@H_404_18@(L);@H_404_18@
return@H_404_18@ 1@H_404_18@;@H_404_18@
}
else@H_404_18@ if@H_404_18@ (t== LUA_TTABLE@H_404_18@) //如果是表
{
lua_pushvalue@H_404_18@(L,1@H_404_18@);@H_404_18@
class_table_get_index(L);@H_404_18@
return@H_404_18@ 1@H_404_18@;@H_404_18@
}
lua_pushnil@H_404_18@(L);@H_404_18@
return@H_404_18@ 1@H_404_18@;@H_404_18@
}
class_table_get_index 函数
static int class_table_get_index (lua_State* L)
{
// stack: obj key ...@H_404_18@ obj
while@H_404_18@ (lua_getMetatable(L,-1@H_404_18@)) { /* stack: obj key obj mt */
lua_remove(L,-2@H_404_18@); /* stack: ...@H_404_18@ mt */
lua_pushvalue(L,2@H_404_18@); /* stack: ...@H_404_18@ mt key */
lua_rawget(L,-2@H_404_18@); /* stack: ...@H_404_18@ mt value */
if@H_404_18@ (!lua_isnil(L,-1@H_404_18@)) {
return@H_404_18@ 1@H_404_18@;
} else@H_404_18@ {
lua_pop(L,1@H_404_18@);
}
/* try@H_404_18@ C/C++ variable */
lua_pushstring(L,".get"@H_404_18@);
lua_rawget(L,-2@H_404_18@); /* stack: obj key ...@H_404_18@ mt tget */
if@H_404_18@ (lua_istable(L,-1@H_404_18@)) {
lua_pushvalue(L,2@H_404_18@); /* stack: obj key ...@H_404_18@ mt tget key */
lua_rawget(L,-2@H_404_18@); /* stack: obj key ...@H_404_18@ mt tget value */
if@H_404_18@ (lua_iscfunction(L,-1@H_404_18@)) {
lua_call(L,0@H_404_18@,1@H_404_18@);
return@H_404_18@ 1@H_404_18@;
} else@H_404_18@ if@H_404_18@ (lua_istable(L,-1@H_404_18@)) {
return@H_404_18@ 1@H_404_18@;
}
lua_pop(L,2@H_404_18@);
}
}
lua_pushnil(L);
return@H_404_18@ 1@H_404_18@;
}