MCPcopy Create free account
hub / github.com/Tencent/UnLua / Bind

Method Bind

Plugins/UnLua/Source/UnLua/Private/Registries/ObjectRegistry.cpp:106–156  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

104 }
105
106 int FObjectRegistry::Bind(UObject* Object)
107 {
108 if (const auto Exists = ObjectRefs.Find(Object))
109 {
110 if (*Exists != LUA_NOREF)
111 return *Exists;
112 }
113
114 const auto L = Env->GetMainState();
115
116 int OldTop = lua_gettop(L);
117
118 lua_getfield(L, LUA_REGISTRYINDEX, REGISTRY_KEY);
119 lua_pushlightuserdata(L, Object);
120 lua_newtable(L); // create a Lua table ('INSTANCE')
121 PushObjectCore(L, Object); // push UObject ('RAW_UOBJECT')
122 lua_pushstring(L, "Object");
123 lua_pushvalue(L, -2);
124 lua_rawset(L, -4); // INSTANCE.Object = RAW_UOBJECT
125
126 // in some case may occur module or object metatable can
127 // not be found problem
128 const auto Class = Object->IsA<UClass>() ? static_cast<UClass*>(Object) : Object->GetClass();
129 const auto ClassBoundRef = Env->GetManager()->GetBoundRef(Class);
130 int32 TypeModule = lua_rawgeti(L, LUA_REGISTRYINDEX, ClassBoundRef); // push the required module/table ('REQUIRED_MODULE') to the top of the stack
131 int32 TypeMetatable = lua_getmetatable(L, -2); // get the metatable ('METATABLE_UOBJECT') of 'RAW_UOBJECT'
132 if (TypeModule != LUA_TTABLE || TypeMetatable == LUA_TNIL)
133 {
134 lua_pop(L, lua_gettop(L) - OldTop);
135 return LUA_REFNIL;
136 }
137
138#if ENABLE_CALL_OVERRIDDEN_FUNCTION
139 lua_pushstring(L, "Overridden");
140 lua_pushvalue(L, -2);
141 lua_rawset(L, -4);
142#endif
143 lua_setmetatable(L, -2); // REQUIRED_MODULE.metatable = METATABLE_UOBJECT
144 lua_setmetatable(L, -3); // INSTANCE.metatable = REQUIRED_MODULE
145 lua_pop(L, 1);
146
147 lua_pushvalue(L, -1);
148 const auto Ret = luaL_ref(L, LUA_REGISTRYINDEX);
149 ObjectRefs.Add(Object, Ret);
150
151 FUnLuaDelegates::OnObjectBinded.Broadcast(Object); // 'INSTANCE' is on the top of stack now
152
153 lua_rawset(L, -3);
154 lua_pop(L, 1);
155 return Ret;
156 }
157
158 bool FObjectRegistry::IsBound(const UObject* Object) const
159 {

Callers 3

FScriptDelegate_BindFunction · 0.45
SetValueInternalMethod · 0.45
SetValueInternalMethod · 0.45

Calls 15

lua_gettopFunction · 0.85
lua_getfieldFunction · 0.85
lua_pushlightuserdataFunction · 0.85
PushObjectCoreFunction · 0.85
lua_pushstringFunction · 0.85
lua_pushvalueFunction · 0.85
lua_rawsetFunction · 0.85
lua_rawgetiFunction · 0.85
lua_getmetatableFunction · 0.85
lua_setmetatableFunction · 0.85
luaL_refFunction · 0.85
GetManagerMethod · 0.80

Tested by

no test coverage detected