示例#1
0
        // lockCallback, unlockCallback; used by debug code commented out for now

        public Lua()
        {
            luaState = LuaDLL.luaL_newstate();  // steffenj: Lua 5.1.1 API change (lua_open is gone)
            //LuaDLL.luaopen_base(luaState);	// steffenj: luaopen_* no longer used
            LuaDLL.luaL_openlibs(luaState);     // steffenj: Lua 5.1.1 API change (luaopen_base is gone, just open all libs right here)
            LuaDLL.lua_pushstring(luaState, "LUAINTERFACE LOADED");
            LuaDLL.lua_pushboolean(luaState, true);
            LuaDLL.lua_settable(luaState, (int)LuaIndexes.LUA_REGISTRYINDEX);
            LuaDLL.lua_newtable(luaState);
            LuaDLL.lua_setglobal(luaState, "luanet");
            LuaDLL.lua_pushvalue(luaState, (int)LuaIndexes.LUA_GLOBALSINDEX);
            LuaDLL.lua_getglobal(luaState, "luanet");
            LuaDLL.lua_pushstring(luaState, "getmetatable");
            LuaDLL.lua_getglobal(luaState, "getmetatable");
            LuaDLL.lua_settable(luaState, -3);
            LuaDLL.lua_replace(luaState, (int)LuaIndexes.LUA_GLOBALSINDEX);
            translator = new ObjectTranslator(this, luaState);
            LuaDLL.lua_replace(luaState, (int)LuaIndexes.LUA_GLOBALSINDEX);
            LuaDLL.luaL_dostring(luaState, Lua.init_luanet);    // steffenj: lua_dostring renamed to luaL_dostring

            tracebackFunction = new KopiLua.Lua.lua_CFunction(traceback);

            // We need to keep this in a managed reference so the delegate doesn't get garbage collected
            panicCallback = new KopiLua.Lua.lua_CFunction(PanicCallback);
            LuaDLL.lua_atpanic(luaState, panicCallback);
        }
示例#2
0
        public LuaFunction(int reference, Lua interpreter)
        {
            this.reference   = reference;
            this.function    = null;
            this.interpreter = interpreter;

            interpreter.AddLuaRef(reference, this);
        }
示例#3
0
 public MetaFunctions(ObjectTranslator translator)
 {
     this.translator = translator;
     gcFunction = new KopiLua.Lua.lua_CFunction(this.collectObject);
     toStringFunction = new KopiLua.Lua.lua_CFunction(this.toString);
     indexFunction = new KopiLua.Lua.lua_CFunction(this.getMethod);
     newindexFunction = new KopiLua.Lua.lua_CFunction(this.setFieldOrProperty);
     baseIndexFunction = new KopiLua.Lua.lua_CFunction(this.getBaseMethod);
     callConstructorFunction = new KopiLua.Lua.lua_CFunction(this.callConstructor);
     classIndexFunction = new KopiLua.Lua.lua_CFunction(this.getClassMethod);
     classNewindexFunction = new KopiLua.Lua.lua_CFunction(this.setClassFieldOrProperty);
     execDelegateFunction = new KopiLua.Lua.lua_CFunction(this.runFunctionDelegate);
 }
示例#4
0
 public MetaFunctions(ObjectTranslator translator)
 {
     this.translator         = translator;
     gcFunction              = new KopiLua.Lua.lua_CFunction(this.collectObject);
     toStringFunction        = new KopiLua.Lua.lua_CFunction(this.toString);
     indexFunction           = new KopiLua.Lua.lua_CFunction(this.getMethod);
     newindexFunction        = new KopiLua.Lua.lua_CFunction(this.setFieldOrProperty);
     baseIndexFunction       = new KopiLua.Lua.lua_CFunction(this.getBaseMethod);
     callConstructorFunction = new KopiLua.Lua.lua_CFunction(this.callConstructor);
     classIndexFunction      = new KopiLua.Lua.lua_CFunction(this.getClassMethod);
     classNewindexFunction   = new KopiLua.Lua.lua_CFunction(this.setClassFieldOrProperty);
     execDelegateFunction    = new KopiLua.Lua.lua_CFunction(this.runFunctionDelegate);
 }
示例#5
0
        public ObjectTranslator(Lua interpreter, KopiLua.Lua.lua_State luaState)
        {
            this.interpreter = interpreter;
            typeChecker      = new CheckType(this);
            metaFunctions    = new MetaFunctions(this);
            assemblies       = new List <Assembly>();

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

            createLuaObjectList(luaState);
            createIndexingMetaFunction(luaState);
            createBaseClassMetatable(luaState);
            createClassMetatable(luaState);
            createFunctionMetatable(luaState);
            setGlobalFunctions(luaState);
        }
        public ObjectTranslator(Lua interpreter,KopiLua.Lua.lua_State luaState)
        {
            this.interpreter=interpreter;
            typeChecker=new CheckType(this);
            metaFunctions=new MetaFunctions(this);
            assemblies=new List<Assembly>();

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

            createLuaObjectList(luaState);
            createIndexingMetaFunction(luaState);
            createBaseClassMetatable(luaState);
            createClassMetatable(luaState);
            createFunctionMetatable(luaState);
            setGlobalFunctions(luaState);
        }
示例#7
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(KopiLua.Lua.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 KopiLua.Lua.lua_CFunction)
            {
                translator.pushFunction(luaState, (KopiLua.Lua.lua_CFunction)cachedMember);
                translator.push(luaState, true);
                return(2);
            }
            else if (cachedMember != null)
            {
                member = (MemberInfo)cachedMember;
            }
            else
            {
                MemberInfo[] members = objType.GetMember(methodName, bindingType | BindingFlags.Public | 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
                    members = objType.GetMember(methodName, bindingType | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);

                    if (members.Length > 0)
                    {
                        member         = members[0];
                        implicitStatic = true;
                    }
                }
            }
            if (member != null)
            {
                if (member.MemberType == MemberTypes.Field)
                {
                    FieldInfo field = (FieldInfo)member;
                    if (cachedMember == null)
                    {
                        setMemberCache(memberCache, objType, methodName, member);
                    }
                    try
                    {
                        translator.push(luaState, field.GetValue(obj));
                    }
                    catch
                    {
                        LuaDLL.lua_pushnil(luaState);
                    }
                }
                else if (member.MemberType == MemberTypes.Property)
                {
                    PropertyInfo property = (PropertyInfo)member;
                    if (cachedMember == null)
                    {
                        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
                        {
                            LuaDLL.lua_pushnil(luaState);
                        }
                    }
                    catch (TargetInvocationException e)  // Convert this exception into a Lua error
                    {
                        ThrowError(luaState, e);
                        LuaDLL.lua_pushnil(luaState);
                    }
                }
                else if (member.MemberType == MemberTypes.Event)
                {
                    EventInfo eventInfo = (EventInfo)member;
                    if (cachedMember == null)
                    {
                        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 == null)
                        {
                            setMemberCache(memberCache, objType, methodName, member);
                        }

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

                        // Build a new long name and try to find the type by name
                        string longname   = dectype.FullName + "+" + name;
                        Type   nestedType = translator.FindType(longname);

                        translator.pushType(luaState, nestedType);
                    }
                    else
                    {
                        // Member type must be 'method'
                        KopiLua.Lua.lua_CFunction wrapper = new KopiLua.Lua.lua_CFunction((new LuaMethodWrapper(translator, objType, methodName, bindingType)).call);
                        if (cachedMember == null)
                        {
                            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);

                    LuaDLL.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);

                LuaDLL.lua_pushnil(luaState);
            }

            // push false because we are NOT returning a function (see luaIndexFunction)
            translator.push(luaState, false);
            return(2);
        }
示例#8
0
        // lockCallback, unlockCallback; used by debug code commented out for now
        public Lua()
        {
            luaState = LuaDLL.luaL_newstate();	// steffenj: Lua 5.1.1 API change (lua_open is gone)
            //LuaDLL.luaopen_base(luaState);	// steffenj: luaopen_* no longer used
            LuaDLL.luaL_openlibs(luaState);		// steffenj: Lua 5.1.1 API change (luaopen_base is gone, just open all libs right here)
            LuaDLL.lua_pushstring(luaState, "LUAINTERFACE LOADED");
            LuaDLL.lua_pushboolean(luaState, true);
            LuaDLL.lua_settable(luaState, (int) LuaIndexes.LUA_REGISTRYINDEX);
            LuaDLL.lua_newtable(luaState);
            LuaDLL.lua_setglobal(luaState, "luanet");
            LuaDLL.lua_pushvalue(luaState, (int)LuaIndexes.LUA_GLOBALSINDEX);
            LuaDLL.lua_getglobal(luaState, "luanet");
            LuaDLL.lua_pushstring(luaState, "getmetatable");
            LuaDLL.lua_getglobal(luaState, "getmetatable");
            LuaDLL.lua_settable(luaState, -3);
            LuaDLL.lua_replace(luaState, (int)LuaIndexes.LUA_GLOBALSINDEX);
            translator=new ObjectTranslator(this,luaState);
            LuaDLL.lua_replace(luaState, (int)LuaIndexes.LUA_GLOBALSINDEX);
            LuaDLL.luaL_dostring(luaState, Lua.init_luanet);	// steffenj: lua_dostring renamed to luaL_dostring

            tracebackFunction = new KopiLua.Lua.lua_CFunction(traceback);

            // We need to keep this in a managed reference so the delegate doesn't get garbage collected
            panicCallback = new KopiLua.Lua.lua_CFunction(PanicCallback);
            LuaDLL.lua_atpanic(luaState, panicCallback);
        }
 /*
  * Pushes a delegate into the stack
  */
 internal void pushFunction(KopiLua.Lua.lua_State luaState, KopiLua.Lua.lua_CFunction func)
 {
     pushObject(luaState, func, "luaNet_function");
 }
示例#10
0
 //[DllImport(LUADLL, CallingConvention = CallingConvention.Cdecl)]
 public static void lua_atpanic(KopiLua.Lua.lua_State luaState, KopiLua.Lua.lua_CFunction panicf)
 {
     KopiLua.Lua.lua_atpanic(luaState, panicf);
 }
示例#11
0
 //[DllImport(STUBDLL,CallingConvention=CallingConvention.Cdecl)]
 public static void lua_pushstdcallcfunction(KopiLua.Lua.lua_State luaState, KopiLua.Lua.lua_CFunction function)
 {
     KopiLua.Lua.lua_pushcfunction(luaState, function);
 }
示例#12
0
 public LuaFunction(KopiLua.Lua.lua_CFunction function, Lua interpreter)
 {
     _Reference = 0;
     this.function = function;
     _Interpreter = interpreter;
 }
示例#13
0
 //internal int reference;
 public LuaFunction(int reference, Lua interpreter)
 {
     _Reference = reference;
     this.function = null;
     _Interpreter = interpreter;
 }
示例#14
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(KopiLua.Lua.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 KopiLua.Lua.lua_CFunction)
            {
                translator.pushFunction(luaState, (KopiLua.Lua.lua_CFunction)cachedMember);
                translator.push(luaState, true);
                return 2;
            }
            else if (cachedMember != null)
            {
                member = (MemberInfo)cachedMember;
            }
            else
            {
                MemberInfo[] members = objType.GetMember(methodName, bindingType | BindingFlags.Public | 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
                    members = objType.GetMember(methodName, bindingType | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);

                    if (members.Length > 0)
                    {
                        member = members[0];
                        implicitStatic = true;
                    }
                }
            }
            if (member != null)
            {
                if (member.MemberType == MemberTypes.Field)
                {
                    FieldInfo field = (FieldInfo)member;
                    if (cachedMember == null) setMemberCache(memberCache, objType, methodName, member);
                    try
                    {
                        translator.push(luaState, field.GetValue(obj));
                    }
                    catch
                    {
                        LuaDll.lua_pushnil(luaState);
                    }
                }
                else if (member.MemberType == MemberTypes.Property)
                {
                    PropertyInfo property = (PropertyInfo)member;
                    if (cachedMember == null) 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
                            LuaDll.lua_pushnil(luaState);
                    }
                    catch(TargetInvocationException e)  // Convert this exception into a Lua error
                    {
                        ThrowError(luaState, e);
                        LuaDll.lua_pushnil(luaState);
                    }
                }
                else if (member.MemberType == MemberTypes.Event)
                {
                    EventInfo eventInfo = (EventInfo)member;
                    if (cachedMember == null) 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 == null) setMemberCache(memberCache, objType, methodName, member);

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

                        // Build a new long name and try to find the type by name
                        string longname = dectype.FullName + "+" + name;
                        Type nestedType = translator.FindType(longname);

                        translator.pushType(luaState, nestedType);
                    }
                    else
                    {
                        // Member type must be 'method'
                        KopiLua.Lua.lua_CFunction wrapper = new KopiLua.Lua.lua_CFunction((new LuaMethodWrapper(translator, objType, methodName, bindingType)).call);
                        if (cachedMember == null) 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);

                    LuaDll.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);

                LuaDll.lua_pushnil(luaState);
            }

            // push false because we are NOT returning a function (see luaIndexFunction)
            translator.push(luaState, false);
            return 2;
        }
示例#15
0
 public LuaFunction(KopiLua.Lua.lua_CFunction function, Lua interpreter)
 {
     this.reference   = 0;
     this.function    = function;
     this.interpreter = interpreter;
 }
示例#16
0
 internal void pushCSFunction(KopiLua.Lua.lua_CFunction function)
 {
     translator.pushFunction(luaState, function);
 }
示例#17
0
 /*
  * __call metafunction of CLR delegates, retrieves and calls the delegate.
  */
 private int runFunctionDelegate(KopiLua.Lua.lua_State luaState)
 {
     KopiLua.Lua.lua_CFunction func = (KopiLua.Lua.lua_CFunction)translator.getRawNetObject(luaState, 1);
     LuaDLL.lua_remove(luaState, 1);
     return(func(luaState));
 }
示例#18
0
 public LuaFunction(int reference, Lua interpreter)
 {
     this.reference   = reference;
     this.function    = null;
     this.interpreter = interpreter;
 }