Пример #1
0
        public ObjectTranslator(Lua interpreter, LuaCore.lua_State luaState)
        {
            this.interpreter = interpreter;
            typeChecker = new CheckType(this);
            metaFunctions = new MetaFunctions(this);
            assemblies = new List<Assembly>();

            importTypeFunction = new LuaCore.lua_CFunction(this.importType);
            loadAssemblyFunction = new LuaCore.lua_CFunction(this.loadAssembly);
            registerTableFunction = new LuaCore.lua_CFunction(this.registerTable);
            unregisterTableFunction = new LuaCore.lua_CFunction(this.unregisterTable);
            getMethodSigFunction = new LuaCore.lua_CFunction(this.getMethodSignature);
            getConstructorSigFunction = new LuaCore.lua_CFunction(this.getConstructorSignature);

            createLuaObjectList(luaState);
            createIndexingMetaFunction(luaState);
            createBaseClassMetatable(luaState);
            createClassMetatable(luaState);
            createFunctionMetatable(luaState);
            setGlobalFunctions(luaState);
        }
Пример #2
0
 public LuaFunction(LuaCore.lua_CFunction function, Lua interpreter)
 {
     _Reference = 0;
     this.function = function;
     _Interpreter = interpreter;
 }
Пример #3
0
 public LuaFunction(int reference, Lua interpreter)
 {
     _Reference = reference;
     this.function = null;
     _Interpreter = interpreter;
 }
Пример #4
0
        public Lua()
        {
            luaState = LuaLib.luaL_newstate();	// steffenj: Lua 5.1.1 API change (lua_open is gone)
            //LuaLib.luaopen_base(luaState);	// steffenj: luaopen_* no longer used
            LuaLib.luaL_openlibs(luaState);		// steffenj: Lua 5.1.1 API change (luaopen_base is gone, just open all libs right here)
            LuaLib.lua_pushstring(luaState, "LUAINTERFACE LOADED");
            LuaLib.lua_pushboolean(luaState, true);
            LuaLib.lua_settable(luaState, (int)LuaIndexes.Registry);
            LuaLib.lua_newtable(luaState);
            LuaLib.lua_setglobal(luaState, "luanet");
            LuaLib.lua_pushvalue(luaState, (int)LuaIndexes.Globals);
            LuaLib.lua_getglobal(luaState, "luanet");
            LuaLib.lua_pushstring(luaState, "getmetatable");
            LuaLib.lua_getglobal(luaState, "getmetatable");
            LuaLib.lua_settable(luaState, -3);
            LuaLib.lua_replace(luaState, (int)LuaIndexes.Globals);
            translator = new ObjectTranslator(this, luaState);
            LuaLib.lua_replace(luaState, (int)LuaIndexes.Globals);
            LuaLib.luaL_dostring(luaState, Lua.init_luanet);	// steffenj: lua_dostring renamed to luaL_dostring

            // We need to keep this in a managed reference so the delegate doesn't get garbage collected
            panicCallback = new LuaCore.lua_CFunction(PanicCallback);
            LuaLib.lua_atpanic(luaState, panicCallback);

            //LuaLib.lua_atlock(luaState, lockCallback = new LuaCore.lua_CFunction(LockCallback));
            //LuaLib.lua_atunlock(luaState, unlockCallback = new LuaCore.lua_CFunction(UnlockCallback));
        }
Пример #5
0
 public MetaFunctions(ObjectTranslator translator)
 {
     this.translator = translator;
     gcFunction = new LuaCore.lua_CFunction(this.collectObject);
     toStringFunction = new LuaCore.lua_CFunction(this.toString);
     indexFunction = new LuaCore.lua_CFunction(this.getMethod);
     newindexFunction = new LuaCore.lua_CFunction(this.setFieldOrProperty);
     baseIndexFunction = new LuaCore.lua_CFunction(this.getBaseMethod);
     callConstructorFunction = new LuaCore.lua_CFunction(this.callConstructor);
     classIndexFunction = new LuaCore.lua_CFunction(this.getClassMethod);
     classNewindexFunction = new LuaCore.lua_CFunction(this.setClassFieldOrProperty);
     execDelegateFunction = new LuaCore.lua_CFunction(this.runFunctionDelegate);
 }
Пример #6
0
        /*
         * Pushes the value of a member or a delegate to call it, depending on the type of
         * the member. Works with static or instance members.
         * Uses reflection to find members, and stores the reflected MemberInfo object in
         * a cache (indexed by the type of the object and the name of the member).
         */
        private int getMember(LuaCore.lua_State luaState, IReflect objType, object obj, string methodName, BindingFlags bindingType)
        {
            bool implicitStatic = false;
            MemberInfo member = null;
            object cachedMember = checkMemberCache(memberCache, objType, methodName);
            //object cachedMember=null;

            if(cachedMember is LuaCore.lua_CFunction)
            {
                translator.pushFunction(luaState, (LuaCore.lua_CFunction)cachedMember);
                translator.push(luaState, true);
                return 2;
            }
            else if(!cachedMember.IsNull())
                member = (MemberInfo)cachedMember;
            else
            {
                //CP: Removed NonPublic binding search
                var members = objType.GetMember(methodName, bindingType | BindingFlags.Public | BindingFlags.IgnoreCase/*| BindingFlags.NonPublic*/);

                if(members.Length > 0)
                    member = members[0];
                else
                {
                    // If we can't find any suitable instance members, try to find them as statics - but we only want to allow implicit static
                    // lookups for fields/properties/events -kevinh
                    //CP: Removed NonPublic binding search and made case insensitive
                    members = objType.GetMember(methodName, bindingType | BindingFlags.Static | BindingFlags.Public | BindingFlags.IgnoreCase/*| BindingFlags.NonPublic*/);

                    if(members.Length > 0)
                    {
                        member = members[0];
                        implicitStatic = true;
                    }
                }
            }

            if(!member.IsNull())
            {
                if(member.MemberType == MemberTypes.Field)
                {
                    var field = (FieldInfo)member;

                    if(cachedMember.IsNull())
                        setMemberCache(memberCache, objType, methodName, member);

                    try
                    {
                        translator.push(luaState, field.GetValue(obj));
                    }
                    catch
                    {
                        LuaLib.lua_pushnil(luaState);
                    }
                }
                else if(member.MemberType == MemberTypes.Property)
                {
                    var property = (PropertyInfo)member;
                    if(cachedMember.IsNull())
                        setMemberCache(memberCache, objType, methodName, member);

                    try
                    {
                        object val = property.GetValue(obj, null);
                        translator.push(luaState, val);
                    }
                    catch(ArgumentException)
                    {
                        // If we can't find the getter in our class, recurse up to the base class and see
                        // if they can help.
                        if(objType is Type && !(((Type)objType) == typeof(object)))
                            return getMember(luaState, ((Type)objType).BaseType, obj, methodName, bindingType);
                        else
                            LuaLib.lua_pushnil(luaState);
                    }
                    catch(TargetInvocationException e)  // Convert this exception into a Lua error
                    {
                        ThrowError(luaState, e);
                        LuaLib.lua_pushnil(luaState);
                    }
                }
                else if(member.MemberType == MemberTypes.Event)
                {
                    var eventInfo = (EventInfo)member;
                    if(cachedMember.IsNull())
                        setMemberCache(memberCache, objType, methodName, member);

                    translator.push(luaState, new RegisterEventHandler(translator.pendingEvents, obj, eventInfo));
                }
                else if(!implicitStatic)
                {
                    if(member.MemberType == MemberTypes.NestedType)
                    {
                        // kevinh - added support for finding nested types
                        // cache us
                        if(cachedMember.IsNull())
                            setMemberCache(memberCache, objType, methodName, member);

                        // Find the name of our class
                        string name = member.Name;
                        var dectype = member.DeclaringType;

                        // Build a new long name and try to find the type by name
                        string longname = dectype.FullName + "+" + name;
                        var nestedType = translator.FindType(longname);
                        translator.pushType(luaState, nestedType);
                    }
                    else
                    {
                        // Member type must be 'method'
                        var wrapper = new LuaCore.lua_CFunction((new LuaMethodWrapper(translator, objType, methodName, bindingType)).call);

                        if(cachedMember.IsNull())
                            setMemberCache(memberCache, objType, methodName, wrapper);

                        translator.pushFunction(luaState, wrapper);
                        translator.push(luaState, true);
                        return 2;
                    }
                }
                else
                {
                    // If we reach this point we found a static method, but can't use it in this context because the user passed in an instance
                    translator.throwError(luaState, "can't pass instance to static method " + methodName);
                    LuaLib.lua_pushnil(luaState);
                }
            }
            else
            {
                // kevinh - we want to throw an exception because meerly returning 'nil' in this case
                // is not sufficient.  valid data members may return nil and therefore there must be some
                // way to know the member just doesn't exist.
                translator.throwError(luaState, "unknown member name " + methodName);
                LuaLib.lua_pushnil(luaState);
            }

            // push false because we are NOT returning a function (see luaIndexFunction)
            translator.push(luaState, false);
            return 2;
        }
Пример #7
0
        /*
         * Constructs the wrapper for a known MethodBase instance
         */
        public LuaMethodWrapper(ObjectTranslator translator, object target, IReflect targetType, MethodBase method)
        {
            invokeFunction = new LuaCore.lua_CFunction (this.call);
            _Translator = translator;
            _Target = target;

            if (!targetType.IsNull ())
                _ExtractTarget = translator.typeChecker.getExtractor (targetType);

            _Method = method;
            _MethodName = method.Name;

            if (method.IsStatic)
                _BindingType = BindingFlags.Static;
            else
                _BindingType = BindingFlags.Instance;
        }
Пример #8
0
        /*
         * Constructs the wrapper for a known method name
         */
        public LuaMethodWrapper(ObjectTranslator translator, IReflect targetType, string methodName, BindingFlags bindingType)
        {
            invokeFunction = new LuaCore.lua_CFunction (this.call);

            _Translator = translator;
            _MethodName = methodName;

            if (!targetType.IsNull ())
                _ExtractTarget = translator.typeChecker.getExtractor (targetType);

            _BindingType = bindingType;
            //CP: Removed NonPublic binding search and added IgnoreCase
            _Members = targetType.UnderlyingSystemType.GetMember (methodName, MemberTypes.Method, bindingType | BindingFlags.Public | BindingFlags.IgnoreCase/*|BindingFlags.NonPublic*/);
        }