Esempio n. 1
0
        public void OpenLib(RealStatePtr L)
        {
            if (0 != LuaAPI.xlua_getglobal(L, "xlua"))
            {
                throw new Exception("call xlua_getglobal fail!" + LuaAPI.lua_tostring(L, -1));
            }
            LuaAPI.xlua_pushasciistring(L, "import_type");
            LuaAPI.lua_pushstdcallcfunction(L, importTypeFunction);
            LuaAPI.lua_rawset(L, -3);
            LuaAPI.xlua_pushasciistring(L, "cast");
            LuaAPI.lua_pushstdcallcfunction(L, castFunction);
            LuaAPI.lua_rawset(L, -3);
            LuaAPI.xlua_pushasciistring(L, "load_assembly");
            LuaAPI.lua_pushstdcallcfunction(L, loadAssemblyFunction);
            LuaAPI.lua_rawset(L, -3);
            LuaAPI.xlua_pushasciistring(L, "access");
            LuaAPI.lua_pushstdcallcfunction(L, StaticLuaCallbacks.XLuaAccess);
            LuaAPI.lua_rawset(L, -3);
            LuaAPI.xlua_pushasciistring(L, "private_accessible");
            LuaAPI.lua_pushstdcallcfunction(L, StaticLuaCallbacks.XLuaPrivateAccessible);
            LuaAPI.lua_rawset(L, -3);
            LuaAPI.lua_pop(L, 1);

            LuaAPI.lua_createtable(L, 1, 4); // 4 for __gc, __tostring, __index, __newindex
            common_array_meta = LuaAPI.luaL_ref(L, LuaIndexes.LUA_REGISTRYINDEX);
            LuaAPI.lua_createtable(L, 1, 4); // 4 for __gc, __tostring, __index, __newindex
            common_delegate_meta = LuaAPI.luaL_ref(L, LuaIndexes.LUA_REGISTRYINDEX);
        }
Esempio n. 2
0
        public static void SetCSTable(RealStatePtr L, Type type, int cls_table)
        {
            int oldTop = LuaAPI.lua_gettop(L);

            cls_table = abs_idx(oldTop, cls_table);
            if (0 != LuaAPI.xlua_getglobal(L, "CS"))
            {
                throw new Exception("call xlua_getglobal fail!");
            }

            List <string> path = getPathOfType(type);

            for (int i = 0; i < path.Count - 1; ++i)
            {
                LuaAPI.xlua_pushasciistring(L, path[i]);
                if (0 != LuaAPI.xlua_pgettable(L, -2))
                {
                    LuaAPI.lua_settop(L, oldTop);
                    throw new Exception("SetCSTable for [" + type + "] error: " + LuaAPI.lua_tostring(L, -1));
                }
                if (LuaAPI.lua_isnil(L, -1))
                {
                    LuaAPI.lua_pop(L, 1);
                    LuaAPI.lua_createtable(L, 0, 0);
                    LuaAPI.xlua_pushasciistring(L, path[i]);
                    LuaAPI.lua_pushvalue(L, -2);
                    LuaAPI.lua_rawset(L, -4);
                }
                else if (!LuaAPI.lua_istable(L, -1))
                {
                    LuaAPI.lua_settop(L, oldTop);
                    throw new Exception("SetCSTable for [" + type + "] error: ancestors is not a table!");
                }
                LuaAPI.lua_remove(L, -2);
            }

            LuaAPI.xlua_pushasciistring(L, path[path.Count - 1]);
            LuaAPI.lua_pushvalue(L, cls_table);
            LuaAPI.lua_rawset(L, -3);
            LuaAPI.lua_pop(L, 1);
        }
Esempio n. 3
0
        public static void BeginClassRegister(Type type, RealStatePtr L, LuaCSFunction creator, int class_field_count,
                                              int static_getter_count, int static_setter_count)
        {
            LuaAPI.lua_createtable(L, 0, class_field_count);

            int cls_table = LuaAPI.lua_gettop(L);

            SetCSTable(L, type, cls_table);

            LuaAPI.lua_createtable(L, 0, 3);
            int meta_table = LuaAPI.lua_gettop(L);

            if (creator != null)
            {
                LuaAPI.xlua_pushasciistring(L, "__call");
                LuaAPI.lua_pushstdcallcfunction(L, creator);
                LuaAPI.lua_rawset(L, -3);
            }

            if (static_getter_count == 0)
            {
                LuaAPI.lua_pushnil(L);
            }
            else
            {
                LuaAPI.lua_createtable(L, 0, static_getter_count);
            }

            if (static_setter_count == 0)
            {
                LuaAPI.lua_pushnil(L);
            }
            else
            {
                LuaAPI.lua_createtable(L, 0, static_setter_count);
            }
            LuaAPI.lua_pushvalue(L, meta_table);
            LuaAPI.lua_setmetatable(L, cls_table);
        }
Esempio n. 4
0
        //meta: -4, method:-3, getter: -2, setter: -1
        public static void BeginObjectRegister(Type type, RealStatePtr L, ObjectTranslator translator, int meta_count, int method_count, int getter_count,
                                               int setter_count, int type_id = -1)
        {
            if (type == null)
            {
                if (type_id == -1)
                {
                    throw new Exception("Fatal: must provide a type of type_id");
                }
                LuaAPI.xlua_rawgeti(L, LuaIndexes.LUA_REGISTRYINDEX, type_id);
            }
            else
            {
                LuaAPI.luaL_getmetatable(L, type.FullName);
                if (LuaAPI.lua_isnil(L, -1))
                {
                    LuaAPI.lua_pop(L, 1);
                    LuaAPI.luaL_newmetatable(L, type.FullName);
                }
            }
            LuaAPI.lua_pushlightuserdata(L, LuaAPI.xlua_tag());
            LuaAPI.lua_pushnumber(L, 1);
            LuaAPI.lua_rawset(L, -3);

            if ((type == null || !translator.HasCustomOp(type)) && type != typeof(decimal))
            {
                LuaAPI.xlua_pushasciistring(L, "__gc");
                LuaAPI.lua_pushstdcallcfunction(L, translator.metaFunctions.GcMeta);
                LuaAPI.lua_rawset(L, -3);
            }

            LuaAPI.xlua_pushasciistring(L, "__tostring");
            LuaAPI.lua_pushstdcallcfunction(L, translator.metaFunctions.ToStringMeta);
            LuaAPI.lua_rawset(L, -3);

            if (method_count == 0)
            {
                LuaAPI.lua_pushnil(L);
            }
            else
            {
                LuaAPI.lua_createtable(L, 0, method_count);
            }

            if (getter_count == 0)
            {
                LuaAPI.lua_pushnil(L);
            }
            else
            {
                LuaAPI.lua_createtable(L, 0, getter_count);
            }

            if (setter_count == 0)
            {
                LuaAPI.lua_pushnil(L);
            }
            else
            {
                LuaAPI.lua_createtable(L, 0, setter_count);
            }
        }