Beispiel #1
0
        public ObjectTranslator(LuaEnv luaenv, RealStatePtr L)
        {
            assemblies = new List <Assembly>();

            foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
            {
                assemblies.Add(assembly);
            }

            this.luaEnv      = luaenv;
            objectCasters    = new ObjectCasters(this);
            objectCheckers   = new ObjectCheckers(this);
            methodWrapsCache = new MethodWrapsCache(this, objectCheckers, objectCasters);
            metaFunctions    = new StaticLuaCallbacks();

            importTypeFunction   = new LuaCSFunction(StaticLuaCallbacks.ImportType);
            loadAssemblyFunction = new LuaCSFunction(StaticLuaCallbacks.LoadAssembly);
            castFunction         = new LuaCSFunction(StaticLuaCallbacks.Cast);

            LuaAPI.lua_newtable(L);
            LuaAPI.lua_newtable(L);
            LuaAPI.xlua_pushasciistring(L, "__mode");
            LuaAPI.xlua_pushasciistring(L, "v");
            LuaAPI.lua_rawset(L, -3);
            LuaAPI.lua_setmetatable(L, -2);
            cacheRef = LuaAPI.luaL_ref(L, LuaIndexes.LUA_REGISTRYINDEX);

            initCSharpCallLua();
        }
Beispiel #2
0
        static int __CreateInstance(RealStatePtr L)
        {
            try {
                ObjectTranslator translator = ObjectTranslatorPool.Instance.Find(L);
                if (LuaAPI.lua_gettop(L) == 2 && translator.Assignable <XLua.ObjectTranslator>(L, 2))
                {
                    XLua.ObjectTranslator _translator = (XLua.ObjectTranslator)translator.GetObject(L, 2, typeof(XLua.ObjectTranslator));

                    XLua.ObjectCasters gen_ret = new XLua.ObjectCasters(_translator);
                    translator.Push(L, gen_ret);

                    return(1);
                }
            }
            catch (System.Exception gen_e) {
                return(LuaAPI.luaL_error(L, "c# exception:" + gen_e));
            }
            return(LuaAPI.luaL_error(L, "invalid arguments to XLua.ObjectCasters constructor!"));
        }
        static int __CreateInstance(RealStatePtr L)
        {
            try {
                ObjectTranslator translator = ObjectTranslatorPool.Instance.Find(L);
                if (LuaAPI.lua_gettop(L) == 4 && translator.Assignable <XLua.ObjectTranslator>(L, 2) && translator.Assignable <XLua.ObjectCheckers>(L, 3) && translator.Assignable <XLua.ObjectCasters>(L, 4))
                {
                    XLua.ObjectTranslator _translator  = (XLua.ObjectTranslator)translator.GetObject(L, 2, typeof(XLua.ObjectTranslator));
                    XLua.ObjectCheckers   _objCheckers = (XLua.ObjectCheckers)translator.GetObject(L, 3, typeof(XLua.ObjectCheckers));
                    XLua.ObjectCasters    _objCasters  = (XLua.ObjectCasters)translator.GetObject(L, 4, typeof(XLua.ObjectCasters));

                    XLua.MethodWrapsCache gen_ret = new XLua.MethodWrapsCache(_translator, _objCheckers, _objCasters);
                    translator.Push(L, gen_ret);

                    return(1);
                }
            }
            catch (System.Exception gen_e) {
                return(LuaAPI.luaL_error(L, "c# exception:" + gen_e));
            }
            return(LuaAPI.luaL_error(L, "invalid arguments to XLua.MethodWrapsCache constructor!"));
        }
Beispiel #4
0
        public ObjectTranslator(LuaEnv luaenv, RealStatePtr L)
        {
#if XLUA_GENERAL
            var dumb_field = typeof(ObjectTranslator).GetField("s_gen_reg_dumb_obj", BindingFlags.Static | BindingFlags.DeclaredOnly | BindingFlags.NonPublic);
            if (dumb_field != null)
            {
                dumb_field.GetValue(null);
            }
#endif
            assemblies = new List <Assembly>();

            foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
            {
                assemblies.Add(assembly);
            }

            this.luaEnv      = luaenv;
            objectCasters    = new ObjectCasters(this);
            objectCheckers   = new ObjectCheckers(this);
            methodWrapsCache = new MethodWrapsCache(this, objectCheckers, objectCasters);
            metaFunctions    = new StaticLuaCallbacks();

            importTypeFunction   = new LuaCSFunction(StaticLuaCallbacks.ImportType);
            loadAssemblyFunction = new LuaCSFunction(StaticLuaCallbacks.LoadAssembly);
            castFunction         = new LuaCSFunction(StaticLuaCallbacks.Cast);
            cTypeFunction        = new LuaCSFunction(StaticLuaCallbacks.Ctype);

            LuaAPI.lua_newtable(L);
            LuaAPI.lua_newtable(L);
            LuaAPI.xlua_pushasciistring(L, "__mode");
            LuaAPI.xlua_pushasciistring(L, "v");
            LuaAPI.lua_rawset(L, -3);
            LuaAPI.lua_setmetatable(L, -2);
            cacheRef = LuaAPI.luaL_ref(L, LuaIndexes.LUA_REGISTRYINDEX);

            initCSharpCallLua();
        }
Beispiel #5
0
        static int _m_GetCaster(RealStatePtr L)
        {
            try {
                ObjectTranslator translator = ObjectTranslatorPool.Instance.Find(L);


                XLua.ObjectCasters gen_to_be_invoked = (XLua.ObjectCasters)translator.FastGetCSObj(L, 1);



                {
                    System.Type _type = (System.Type)translator.GetObject(L, 2, typeof(System.Type));

                    XLua.ObjectCast gen_ret = gen_to_be_invoked.GetCaster(_type);
                    translator.Push(L, gen_ret);



                    return(1);
                }
            } catch (System.Exception gen_e) {
                return(LuaAPI.luaL_error(L, "c# exception:" + gen_e));
            }
        }
Beispiel #6
0
        static int _m_AddCaster(RealStatePtr L)
        {
            try {
                ObjectTranslator translator = ObjectTranslatorPool.Instance.Find(L);


                XLua.ObjectCasters gen_to_be_invoked = (XLua.ObjectCasters)translator.FastGetCSObj(L, 1);



                {
                    System.Type     _type = (System.Type)translator.GetObject(L, 2, typeof(System.Type));
                    XLua.ObjectCast _oc   = translator.GetDelegate <XLua.ObjectCast>(L, 3);

                    gen_to_be_invoked.AddCaster(_type, _oc);



                    return(0);
                }
            } catch (System.Exception gen_e) {
                return(LuaAPI.luaL_error(L, "c# exception:" + gen_e));
            }
        }
        static int _m_Init(RealStatePtr L)
        {
            try {
                ObjectTranslator translator = ObjectTranslatorPool.Instance.Find(L);


                XLua.OverloadMethodWrap gen_to_be_invoked = (XLua.OverloadMethodWrap)translator.FastGetCSObj(L, 1);



                {
                    XLua.ObjectCheckers _objCheckers = (XLua.ObjectCheckers)translator.GetObject(L, 2, typeof(XLua.ObjectCheckers));
                    XLua.ObjectCasters  _objCasters  = (XLua.ObjectCasters)translator.GetObject(L, 3, typeof(XLua.ObjectCasters));

                    gen_to_be_invoked.Init(_objCheckers, _objCasters);



                    return(0);
                }
            } catch (System.Exception gen_e) {
                return(LuaAPI.luaL_error(L, "c# exception:" + gen_e));
            }
        }
Beispiel #8
0
 public MethodWrapsCache(ObjectTranslator translator, ObjectCheckers objCheckers, ObjectCasters objCasters)
 {
     this.translator = translator;
     this.objCheckers = objCheckers;
     this.objCasters = objCasters;
 }
Beispiel #9
0
        public void Init(ObjectCheckers objCheckers, ObjectCasters objCasters)
        {
            if (typeof(Delegate).IsAssignableFrom(targetType) || !method.IsStatic || method.IsConstructor)
            {
                luaStackPosStart = 2;
                if (!method.IsConstructor)
                {
                    targetNeeded = true;
                }
            }

            var paramInfos = method.GetParameters();
            refPos = new int[paramInfos.Length];

            List<int> inPosList = new List<int>();
            List<int> outPosList = new List<int>();

            List<ObjectCheck> paramsChecks = new List<ObjectCheck>();
            List<ObjectCast> paramsCasts = new List<ObjectCast>();
            List<bool> isOptionalList = new List<bool>();
            List<object> defaultValueList = new List<object>();

            for(int i = 0; i < paramInfos.Length; i++)
            {
                refPos[i] = -1;
                if (!paramInfos[i].IsIn && paramInfos[i].IsOut)  // out parameter
                {
                    outPosList.Add(i);
                }
                else
                {
                    if(paramInfos[i].ParameterType.IsByRef)
                    {
                        var ttype = paramInfos[i].ParameterType.GetElementType();
                        if(CopyByValue.IsStruct(ttype) && ttype != typeof(decimal))
                        {
                            refPos[i] = inPosList.Count;
                        }
                        outPosList.Add(i);
                    }

                    inPosList.Add(i);
                    var paramType = paramInfos[i].IsDefined(typeof(ParamArrayAttribute), false) || (!paramInfos[i].ParameterType.IsArray && paramInfos[i].ParameterType.IsByRef ) ?
                        paramInfos[i].ParameterType.GetElementType() : paramInfos[i].ParameterType;
                    paramsChecks.Add (objCheckers.GetChecker(paramType));
                    paramsCasts.Add (objCasters.GetCaster(paramType));
                    isOptionalList.Add(paramInfos[i].IsOptional);
                    var defalutValue = paramInfos[i].DefaultValue;
                    if (paramInfos[i].IsOptional)
                    {
                        if (defalutValue != null && defalutValue.GetType() != paramInfos[i].ParameterType)
                        {
                            defalutValue = defalutValue.GetType() == typeof(Missing) ? Activator.CreateInstance(paramInfos[i].ParameterType) : Convert.ChangeType(defalutValue, paramInfos[i].ParameterType);
                        }
                        HasDefalutValue = true;
                    }
                    defaultValueList.Add(paramInfos[i].IsOptional ? defalutValue : null);
                }
            }
            checkArray = paramsChecks.ToArray();
            castArray = paramsCasts.ToArray();
            inPosArray = inPosList.ToArray();
            outPosArray = outPosList.ToArray();
            isOptionalArray = isOptionalList.ToArray();
            defaultValueArray = defaultValueList.ToArray();

            if (paramInfos.Length > 0 && paramInfos[paramInfos.Length - 1].IsDefined(typeof(ParamArrayAttribute), false))
            {
                paramsType = paramInfos[paramInfos.Length - 1].ParameterType.GetElementType();
            }

            args = new object[paramInfos.Length];

            if (method is MethodInfo) //constructor is not MethodInfo?
            {
                isVoid = (method as MethodInfo).ReturnType == typeof(void);
            }
            else if(method is ConstructorInfo)
            {
                isVoid = false;
            }
        }
        public void Init(ObjectCheckers objCheckers, ObjectCasters objCasters)
        {
            if (typeof(Delegate).IsAssignableFrom(targetType) || !method.IsStatic || method.IsConstructor)
            {
                luaStackPosStart = 2;
                if (!method.IsConstructor)
                {
                    targetNeeded = true;
                }
            }

            var paramInfos = method.GetParameters();

            refPos = new int[paramInfos.Length];

            List <int> inPosList  = new List <int>();
            List <int> outPosList = new List <int>();

            List <ObjectCheck> paramsChecks     = new List <ObjectCheck>();
            List <ObjectCast>  paramsCasts      = new List <ObjectCast>();
            List <bool>        isOptionalList   = new List <bool>();
            List <object>      defaultValueList = new List <object>();

            for (int i = 0; i < paramInfos.Length; i++)
            {
                refPos[i] = -1;
                if (!paramInfos[i].IsIn && paramInfos[i].IsOut)  // out parameter
                {
                    outPosList.Add(i);
                }
                else
                {
                    if (paramInfos[i].ParameterType.IsByRef)
                    {
                        var ttype = paramInfos[i].ParameterType.GetElementType();
                        if (CopyByValue.IsStruct(ttype) && ttype != typeof(decimal))
                        {
                            refPos[i] = inPosList.Count;
                        }
                        outPosList.Add(i);
                    }

                    inPosList.Add(i);
                    var paramType = paramInfos[i].IsDefined(typeof(ParamArrayAttribute), false) || (!paramInfos[i].ParameterType.IsArray && paramInfos[i].ParameterType.IsByRef) ?
                                    paramInfos[i].ParameterType.GetElementType() : paramInfos[i].ParameterType;
                    paramsChecks.Add(objCheckers.GetChecker(paramType));
                    paramsCasts.Add(objCasters.GetCaster(paramType));
                    isOptionalList.Add(paramInfos[i].IsOptional);
                    var defalutValue = paramInfos[i].DefaultValue;
                    if (paramInfos[i].IsOptional)
                    {
                        if (defalutValue != null && defalutValue.GetType() != paramInfos[i].ParameterType)
                        {
                            defalutValue = defalutValue.GetType() == typeof(Missing) ? Activator.CreateInstance(paramInfos[i].ParameterType) : Convert.ChangeType(defalutValue, paramInfos[i].ParameterType);
                        }
                        HasDefalutValue = true;
                    }
                    defaultValueList.Add(paramInfos[i].IsOptional ? defalutValue : null);
                }
            }
            checkArray        = paramsChecks.ToArray();
            castArray         = paramsCasts.ToArray();
            inPosArray        = inPosList.ToArray();
            outPosArray       = outPosList.ToArray();
            isOptionalArray   = isOptionalList.ToArray();
            defaultValueArray = defaultValueList.ToArray();

            if (paramInfos.Length > 0 && paramInfos[paramInfos.Length - 1].IsDefined(typeof(ParamArrayAttribute), false))
            {
                paramsType = paramInfos[paramInfos.Length - 1].ParameterType.GetElementType();
            }

            args = new object[paramInfos.Length];

            if (method is MethodInfo) //constructor is not MethodInfo?
            {
                isVoid = (method as MethodInfo).ReturnType == typeof(void);
            }
            else if (method is ConstructorInfo)
            {
                isVoid = false;
            }
        }
 public MethodWrapsCache(ObjectTranslator translator, ObjectCheckers objCheckers, ObjectCasters objCasters)
 {
     this.translator  = translator;
     this.objCheckers = objCheckers;
     this.objCasters  = objCasters;
 }