예제 #1
0
        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

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

            // LuaDLL.lua_atlock(luaState, lockCallback = new KopiLua.LuaNativeFunction(LockCallback));
            // LuaDLL.lua_atunlock(luaState, unlockCallback = new KopiLua.LuaNativeFunction(UnlockCallback));
        }
예제 #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.LuaNativeFunction(this.collectObject);
     toStringFunction        = new KopiLua.LuaNativeFunction(this.toString);
     indexFunction           = new KopiLua.LuaNativeFunction(this.getMethod);
     newindexFunction        = new KopiLua.LuaNativeFunction(this.setFieldOrProperty);
     baseIndexFunction       = new KopiLua.LuaNativeFunction(this.getBaseMethod);
     callConstructorFunction = new KopiLua.LuaNativeFunction(this.callConstructor);
     classIndexFunction      = new KopiLua.LuaNativeFunction(this.getClassMethod);
     classNewindexFunction   = new KopiLua.LuaNativeFunction(this.setClassFieldOrProperty);
     execDelegateFunction    = new KopiLua.LuaNativeFunction(this.runFunctionDelegate);
 }
예제 #4
0
        /*
         * Constructs the wrapper for a known MethodBase instance
         */
        public LuaMethodWrapper(ObjectTranslator translator, object target, ProxyType targetType, MethodBase method)
        {
            invokeFunction = new LuaNativeFunction (this.Call);
            _Translator = translator;
            _Target = target;

            if (targetType != null)
                _ExtractTarget = translator.typeChecker.GetExtractor (targetType);

            _Method = method;
            _MethodName = method.Name;
            _IsStatic = method.IsStatic;
        }
예제 #5
0
        public ObjectTranslator(Lua interpreter, KopiLua.LuaState luaState)
        {
            this.interpreter = interpreter;
            typeChecker      = new CheckType(this);
            metaFunctions    = new MetaFunctions(this);
            assemblies       = new List <Assembly>();

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

            createLuaObjectList(luaState);
            createIndexingMetaFunction(luaState);
            createBaseClassMetatable(luaState);
            createClassMetatable(luaState);
            createFunctionMetatable(luaState);
            setGlobalFunctions(luaState);
        }
예제 #6
0
 /*
  * __call metafunction of CLR delegates, retrieves and calls the delegate.
  */
 private int runFunctionDelegate(KopiLua.LuaState luaState)
 {
     KopiLua.LuaNativeFunction func = (KopiLua.LuaNativeFunction)translator.getRawNetObject(luaState, 1);
     LuaDll.lua_remove(luaState, 1);
     return(func(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.LuaState 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.LuaNativeFunction)
            {
                translator.pushFunction(luaState, (KopiLua.LuaNativeFunction)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.LuaNativeFunction wrapper = new KopiLua.LuaNativeFunction((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
        /*
         * Constructs the wrapper for a known MethodBase instance
         */
        public LuaMethodWrapper(ObjectTranslator translator, object target, IReflect targetType, MethodBase method)
        {
            invokeFunction = new LuaNativeFunction (this.Call);
            _Translator = translator;
            _Target = target;

            if (targetType != null)
                _ExtractTarget = translator.typeChecker.GetExtractor (targetType);

            _Method = method;
            _MethodName = method.Name;

            if (method.IsStatic)
                _BindingType = BindingFlags.Static;
            else
                _BindingType = BindingFlags.Instance;
        }
예제 #9
0
        /*
         * Constructs the wrapper for a known method name
         */
        public LuaMethodWrapper(ObjectTranslator translator, IReflect targetType, string methodName, BindingFlags bindingType)
        {
            invokeFunction = new LuaNativeFunction (this.Call);

            _Translator = translator;
            _MethodName = methodName;

            if (targetType != null)
                _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*/);
        }
예제 #10
0
파일: LuaLib.cs 프로젝트: nobitagamer/NLua
 public static void LuaAtPanic(LuaState luaState, LuaNativeFunction panicf)
 {
     LuaCore.LuaAtPanic (luaState, (LuaNativeFunction)panicf);
 }
예제 #11
0
 /*
  * Pushes a delegate into the stack
  */
 internal void pushFunction(KopiLua.LuaState luaState, KopiLua.LuaNativeFunction func)
 {
     pushObject(luaState, func, "luaNet_function");
 }
예제 #12
0
        /*
         * Constructs the wrapper for a known method name
         */
        public LuaMethodWrapper(ObjectTranslator translator, ProxyType targetType, string methodName, BindingFlags bindingType)
        {
            invokeFunction = new LuaNativeFunction (this.Call);

            _Translator = translator;
            _MethodName = methodName;

            if (targetType != null)
                _ExtractTarget = translator.typeChecker.GetExtractor (targetType);

            _IsStatic = (bindingType & BindingFlags.Static) == BindingFlags.Static;
            _Members  = GetMethodsRecursively (targetType.UnderlyingSystemType, methodName, bindingType | BindingFlags.Public);
        }
예제 #13
0
 public LuaFunction(KopiLua.LuaNativeFunction function, Lua interpreter)
 {
     this.reference   = 0;
     this.function    = function;
     this.interpreter = interpreter;
 }
예제 #14
0
 public LuaFunction(int reference, Lua interpreter)
 {
     _Reference = reference;
     this.function = null;
     _Interpreter = interpreter;
 }
예제 #15
0
파일: LuaLib.cs 프로젝트: nobitagamer/NLua
 public static void LuaPushStdCallCFunction(LuaState luaState, LuaNativeFunction function)
 {
     LuaCore.LuaPushStdCallCFunction (luaState, function);
 }
예제 #16
0
 public LuaFunction(LuaNativeFunction function, Lua interpreter)
 {
     _Reference = 0;
     this.function = function;
     _Interpreter = interpreter;
 }
예제 #17
0
 internal void pushCSFunction(KopiLua.LuaNativeFunction function)
 {
     translator.pushFunction(luaState, function);
 }