Ejemplo n.º 1
0
        /*
         * Pushes a new object into the Lua stack with the provided
         * metatable
         */
        private void PushNewObject(LuaState luaState, object o, int index, string metatable)
        {
            if (metatable == "luaNet_metatable")
            {
                // Gets or creates the metatable for the object's type
                luaState.GetMetaTable(o.GetType().AssemblyQualifiedName);

                if (luaState.IsNil(-1))
                {
                    luaState.SetTop(-2);
                    luaState.NewMetaTable(o.GetType().AssemblyQualifiedName);
                    luaState.PushString("cache");
                    luaState.NewTable();
                    luaState.RawSet(-3);
                    luaState.PushLightUserData(_tagPtr);
                    luaState.PushNumber(1);
                    luaState.RawSet(-3);
                    luaState.PushString("__index");
                    luaState.PushString("luaNet_indexfunction");
                    luaState.RawGet(LuaRegistry.Index);
                    luaState.RawSet(-3);
                    luaState.PushString("__gc");
                    luaState.PushCFunction(MetaFunctions.GcFunction);
                    luaState.RawSet(-3);
                    luaState.PushString("__tostring");
                    luaState.PushCFunction(MetaFunctions.ToStringFunction);
                    luaState.RawSet(-3);
                    luaState.PushString("__concat");
                    luaState.PushCFunction(MetaFunctions.ConcatFunction);
                    luaState.RawSet(-3);
                    luaState.PushString("__newindex");
                    luaState.PushCFunction(MetaFunctions.NewIndexFunction);
                    luaState.RawSet(-3);
                    // Bind C# operator with Lua metamethods (__add, __sub, __mul)
                    RegisterOperatorsFunctions(luaState, o.GetType());
                    RegisterCallMethodForDelegate(luaState, o);
                }
            }
            else
            {
                luaState.GetMetaTable(metatable);
            }

            // Stores the object index in the Lua list and pushes the
            // index into the Lua stack
            luaState.GetMetaTable("luaNet_objects");
            luaState.NewUData(index);
            luaState.PushCopy(-3);
            luaState.Remove(-4);
            luaState.SetMetaTable(-2);
            luaState.PushCopy(-1);
            luaState.RawSetInteger(-3, index);
            luaState.Remove(-2);
        }
Ejemplo n.º 2
0
        public static bool CheckMetaTable(this LuaState state, int index, IntPtr tag)
        {
            if (!state.GetMetaTable(index))
            {
                return(false);
            }

            state.PushLightUserData(tag);
            state.RawGet(-2);
            bool isNotNil = !state.IsNil(-1);

            state.SetTop(-3);
            return(isNotNil);
        }