Luna_LWF_Button.Register C# (CSharp) Method

Register() public static method

public static Register ( Lua L ) : void
L Lua
return void
    public static void Register(Lua.lua_State L)
    {
        objects.Add(L, new Dictionary<int, LWF.Button>());
        objectIdentifiers.Add(L, new Dictionary<LWF.Button, int>());
        int methods;
        Lua.lua_newtable(L);
        methods = Lua.lua_gettop(L);
        // use a single table
        // sometimes more convenient
        int metatable=methods;

        Lua.luaL_dostring(L, "if not __luna then __luna={} end");

        Lua.lua_pushstring(L, "__luna");
        Lua.lua_gettable(L, Lua.LUA_GLOBALSINDEX);
        // unlike original luna class, this class uses the same table for methods and metatable
        // store methods table in __luna global table so that
        // scripts can add functions written in Lua.
        Lua.lua_pushstring(L, LunaTraits_LWF_Button.className);
        Lua.lua_pushvalue(L, methods);
        Lua.lua_settable(L, -3); // __luna[className]=methods

        Lua.lua_pushliteral(L, "__index");
        Lua.lua_pushvalue(L, methods);
        Lua.lua_settable(L, metatable); // metatable.__index=methods

        /* Lua.lua_pushliteral(L, "__tostring"); */
        /* Lua.lua_pushcfunction(L, tostring_T); */
        /* Lua.lua_settable(L, metatable);// metatable.__tostring=tostring_T */

        Lua.lua_pushliteral(L, "__gc");
        Lua.lua_pushcfunction(L, gc_T);
        Lua.lua_settable(L, metatable);

        /*if (false)
        {
            // ctor supports only classname:new
            Lua.lua_pushliteral(L, "new");
            Lua.lua_pushcfunction(L, new_T);
            Lua.lua_settable(L, methods);       // add new_T to metatable table
        }
        else
        */
        {
            // ctor supports both classname:new(...) and classname(...)
            // very slight memory and performance overhead, so
            // no reason to support only one
            Lua.lua_newtable(L);                // mt for method table
            {
                Lua.lua_pushcfunction(L, new_T);
                Lua.lua_pushvalue(L, -1);           // dup new_T function
                set(L, methods, "new");         // add new_T to method table
            }
            set(L, -3, "__call");           // mt.__call = new_T
            Lua.lua_setmetatable(L, methods);
        }

        // fill method table with metatable from class T
        for (int i = 0;; i++)
        {
            LunaTraits_LWF_Button.RegType l = LunaTraits_LWF_Button.methods[i];
            if (l.name == null) break;
            Lua.lua_pushstring(L, l.name);
            Lua.lua_pushcclosure(L, l.mfunc, 0);
            Lua.lua_settable(L, methods);
        }

        Lua.lua_pop(L, 2);  // drop methods and __luna
    }

Usage Example

Beispiel #1
0
 public static void open(Lua.lua_State L)
 {
     Luna.dostring(L, "if __luna==nil then __luna={} end");
     Luna.dostring(L, "    if __luna.copyMethodsFrom==nil then\n        function __luna.copyMethodsFrom(methodsChild, methodsParent)\n            for k,v in pairs(methodsParent) do\n                if k~='__index' and k~='__newindex' and methodsChild[k]==nil then\n                    methodsChild[k]=v\n                end\n            end\n        end\n        function __luna.overwriteMethodsFrom(methodsChild, methodsParent)\n            for k,v in pairs(methodsParent) do\n                if k~='__index' and k~='__newindex' then\n                    if verbose then print('registering', k, methodsChild[k]) end\n                    methodsChild[k]=v\n                end\n            end\n        end\n    end\n    ");
     impl_LunaTraits_LWF_LWF.luna_init_hashmap();
     impl_LunaTraits_LWF_LWF.luna_init_write_hashmap();
     Luna_LWF_LWF.Register(L);
     Luna.dostring(L, "if not LWF then LWF={} end LWF.LWF=__luna.LWF_LWF");
     Luna.dostring(L, "                __luna.LWF_LWF.luna_class='.LWF'");
     impl_LunaTraits_LWF_Button.luna_init_hashmap();
     impl_LunaTraits_LWF_Button.luna_init_write_hashmap();
     Luna_LWF_Button.Register(L);
     Luna.dostring(L, "if not LWF then LWF={} end LWF.Button=__luna.LWF_Button");
     Luna.dostring(L, "                __luna.LWF_Button.luna_class='.Button'");
     impl_LunaTraits_LWF_Movie.luna_init_hashmap();
     impl_LunaTraits_LWF_Movie.luna_init_write_hashmap();
     Luna_LWF_Movie.Register(L);
     Luna.dostring(L, "if not LWF then LWF={} end LWF.Movie=__luna.LWF_Movie");
     Luna.dostring(L, "                __luna.LWF_Movie.luna_class='.Movie'");
     impl_LunaTraits_LWF_Point.luna_init_hashmap();
     impl_LunaTraits_LWF_Point.luna_init_write_hashmap();
     Luna_LWF_Point.Register(L);
     Luna.dostring(L, "if not LWF then LWF={} end LWF.Point=__luna.LWF_Point");
     Luna.dostring(L, "                __luna.LWF_Point.luna_class='.Point'");
 }