void LUA_PushObjectProxy(lua_State* L, VTypedObject *pObj, VisTypedEngineObject_cl* pOwner)
{
  if (pObj==NULL)
  {
    lua_pushnil(L);                                           // ..., nil, TOP
    return;                                                   // 1 value left on the stack
  }

  lua_getfield(L, LUA_REGISTRYINDEX, VISION_WRAPPER_LOOKUP);  // ..., table, TOP
                                              
  //in case the wrapper lookup table does not exist, we create it
  if(lua_isnil(L, -1))
  {
    lua_pop(L,1);                                             // ..., TOP
    lua_newtable(L);                                          // ..., table, TOP
    lua_setfield(L, LUA_REGISTRYINDEX, VISION_WRAPPER_LOOKUP);// ..., TOP
    lua_getfield(L, LUA_REGISTRYINDEX, VISION_WRAPPER_LOOKUP);// ..., table, TOP

    VASSERT_MSG(!lua_isnil(L, -1), "Could not set Lua wrapper lookup table in Lua registry.");
  }

  //we are referencing the wrapper by the pointer address of the original object
  lua_pushfstring(L, "%p", pObj);                             // ..., table, address, TOP
  lua_rawget(L, -2);                                          // ..., table, wrapper-obj or nil, TOP

  //in case there is no stored wrapper we need to create a new wrapper
  if(lua_isnil(L, -1))
  {                                                           // ..., table, nil, TOP
    lua_pop(L, 1);                                            // ..., table, TOP
    LUA_CreateNewWrapper(L, pObj, pOwner);                    // ..., table, wrapper-obj, TOP
    lua_pushfstring(L, "%p", pObj);                           // ..., table, wrapper-obj, address, TOP
    lua_pushvalue(L, -2);                                     // ..., table, wrapper-obj, address, wrapper-obj, TOP
    lua_rawset(L, -4);                                        // ..., table, wrapper-obj, TOP
  }

  //remove the pointer table
  lua_remove(L, -2);                                          // ..., wrapper-obj, TOP
                                                              // 1 value left on the stack
}
Example #2
0
void LUA_PushObjectProxy(lua_State* L, VTypedObject *pObj, VisTypedEngineObject_cl* pOwner)
{
    if (pObj==NULL)
    {
        lua_pushnil(L);                                           // ..., nil, TOP
        return;                                                   // 1 value left on the stack
    }

    LUA_LookupObjectProxy(L, pObj);

    // In case there is no wrapper created yet, we create a new one.
    if (lua_isnil(L, -1))
    {
        lua_pop(L, 1);                                            // ..., TOP
        LUA_CreateNewWrapper(L, pObj, pOwner);                    // ..., wrapper-obj(value), TOP
        lua_pushlightuserdata(L, pObj);                           // ..., wrapper-obj(value), address(key), TOP
        lua_pushvalue(L, -2);                                     // ..., wrapper-obj(value), address(key), wrapper-obj, TOP
        lua_rawset(L, LUA_REGISTRYINDEX);                         // ..., wrapper-obj, TOP

        // Make sure callback is triggered when object gets deleted, so we can remove the object from the lookup table.
        pObj->SetObjectFlag(VObjectFlag_TriggerCallbackOnDelete);
    }                                                              // 1 value left on the stack
}