addObject() private method

private addObject ( object obj ) : int
obj object
return int
Esempio n. 1
0
        public static int registerTable(IntPtr luaState)
        {
#if __NOGEN__
            throwError(luaState, "Tables as Objects not implemnented");
#else
            ObjectTranslator translator = ObjectTranslator.FromState(luaState);
            if (LuaDLL.lua_type(luaState, 1) == LuaTypes.LUA_TTABLE)
            {
                LuaTable luaTable       = translator.getTable(luaState, 1);
                string   superclassName = LuaDLL.lua_tostring(luaState, 2);
                if (superclassName != null)
                {
                    Type klass = translator.FindType(superclassName);
                    if (klass != null)
                    {
                        // Creates and pushes the object in the stack, setting
                        // it as the  metatable of the first argument
                        object obj = CodeGeneration.Instance.GetClassInstance(klass, luaTable);
                        translator.pushObject(luaState, obj, "luaNet_metatable");
                        LuaDLL.lua_newtable(luaState);
                        LuaDLL.lua_pushstring(luaState, "__index");
                        LuaDLL.lua_pushvalue(luaState, -3);
                        LuaDLL.lua_settable(luaState, -3);
                        LuaDLL.lua_pushstring(luaState, "__newindex");
                        LuaDLL.lua_pushvalue(luaState, -3);
                        LuaDLL.lua_settable(luaState, -3);
                        LuaDLL.lua_setmetatable(luaState, 1);
                        // Pushes the object again, this time as the base field
                        // of the table and with the luaNet_searchbase metatable
                        LuaDLL.lua_pushstring(luaState, "base");
                        int index = translator.addObject(obj);
                        translator.pushNewObject(luaState, obj, index, "luaNet_searchbase");
                        LuaDLL.lua_rawset(luaState, 1);
                    }
                    else
                    {
                        translator.throwError(luaState, "register_table: can not find superclass '" + superclassName + "'");
                    }
                }
                else
                {
                    translator.throwError(luaState, "register_table: superclass name can not be null");
                }
            }
            else
            {
                translator.throwError(luaState, "register_table: first arg is not a table");
            }
#endif
            return(0);
        }
        public static int registerTable(IntPtr luaState)
        {
            ObjectTranslator objectTranslator = ObjectTranslator.FromState(luaState);

            if (LuaDLL.lua_type(luaState, 1) == LuaTypes.LUA_TTABLE)
            {
                LuaTable table = objectTranslator.getTable(luaState, 1);
                string   text  = LuaDLL.lua_tostring(luaState, 2);
                if (text != null)
                {
                    Type type = objectTranslator.FindType(text);
                    if (type != null)
                    {
                        object classInstance = CodeGeneration.Instance.GetClassInstance(type, table);
                        objectTranslator.pushObject(luaState, classInstance, "luaNet_metatable");
                        LuaDLL.lua_newtable(luaState);
                        LuaDLL.lua_pushstring(luaState, "__index");
                        LuaDLL.lua_pushvalue(luaState, -3);
                        LuaDLL.lua_settable(luaState, -3);
                        LuaDLL.lua_pushstring(luaState, "__newindex");
                        LuaDLL.lua_pushvalue(luaState, -3);
                        LuaDLL.lua_settable(luaState, -3);
                        LuaDLL.lua_setmetatable(luaState, 1);
                        LuaDLL.lua_pushstring(luaState, "base");
                        int index = objectTranslator.addObject(classInstance);
                        objectTranslator.pushNewObject(luaState, classInstance, index, "luaNet_searchbase");
                        LuaDLL.lua_rawset(luaState, 1);
                    }
                    else
                    {
                        objectTranslator.throwError(luaState, "register_table: can not find superclass '" + text + "'");
                    }
                }
                else
                {
                    objectTranslator.throwError(luaState, "register_table: superclass name can not be null");
                }
            }
            else
            {
                objectTranslator.throwError(luaState, "register_table: first arg is not a table");
            }
            return(0);
        }