Exemplo n.º 1
0
        public MetaFunctions(lua.State L, ObjectTranslator translator)
        {
            Debug.Assert(translator.interpreter.IsSameLua(L));
            this.translator  = translator;
            this.interpreter = translator.interpreter;

            // used by BuildObjectMetatable
            _toString = this.toString;
            _index    = this.index;
            _newindex = this.newIndex;

            luaL.checkstack(L, 2, "new MetaFunctions");
            StackAssert.Start(L);

            // Creates the metatable for superclasses (the base field of registered tables)
            bool didnt_exist = luaclr.newrefmeta(L, "luaNet_searchbase", 3);

            Debug.Assert(didnt_exist);
            lua.pushcfunction(L, _toString); lua.setfield(L, -2, "__tostring");
            lua.pushcfunction(L, _baseIndex = this.baseIndex); lua.setfield(L, -2, "__index");
            lua.pushcfunction(L, _newindex); lua.setfield(L, -2, "__newindex");
            lua.pop(L, 1);

            // Creates the metatable for type references
            didnt_exist = luaclr.newrefmeta(L, "luaNet_class", 4);
            Debug.Assert(didnt_exist);
            lua.pushcfunction(L, _toString); lua.setfield(L, -2, "__tostring");
            lua.pushcfunction(L, _classIndex    = this.classIndex); lua.setfield(L, -2, "__index");
            lua.pushcfunction(L, _classNewindex = this.classNewIndex); lua.setfield(L, -2, "__newindex");
            lua.pushcfunction(L, _classCall     = this.classCall); lua.setfield(L, -2, "__call");
            lua.pop(L, 1);

            StackAssert.End();
        }
        /// <summary>[-0, +1, m]</summary>
        static void _PushEnumeration <T>(lua.State L, Type type) where T : struct, IConvertible
        {
            Debug.Assert(typeof(T) == type);
            if (!type.IsEnum)
            {
                throw new ArgumentException("The type must be an enumeration.");
            }

            string[] names  = Enum.GetNames(type);
            var      values = (T[])Enum.GetValues(type);

            Debug.Assert(EzDelegate.Func(() => {
                if (values.Length == 0)
                {
                    return(true);
                }
                try { var x = values[0].ToDouble(null); return(true); }
                catch { return(false); }
            })(), "enum conversion to double isn't expected to throw exceptions");

            StackAssert.Start(L);
            luanet.checkstack(L, 3, "LuaRegistrationHelper._PushEnumeration");
            lua.createtable(L, 0, names.Length);
            for (int i = 0; i < names.Length; ++i)
            {
                lua.pushstring(L, names[i]);
                lua.pushnumber(L, values[i].ToDouble(null));
                lua.rawset(L, -3);
            }
            StackAssert.End(1);
        }
Exemplo n.º 3
0
        /// <summary>Sets a numeric field of a table ignoring its metatable, if it exists</summary>
        public void RawSet(int field, object value)
        {
            var L = Owner._L;

            luanet.checkstack(L, 2, "LuaTable.RawSet"); StackAssert.Start(L);
            push(L);
            Owner.translator.push(L, value);
            lua.rawseti(L, -2, field);
            lua.pop(L, 1);                               StackAssert.End();
        }
Exemplo n.º 4
0
        /// <summary>Sets a string field of a table ignoring its metatable, if it exists</summary>
        public void RawSet(string field, object value)
        {
            var L = Owner._L;

            luaclr.checkstack(L, 3, "LuaTable.RawSet"); StackAssert.Start(L);
            push(L);
            lua.pushstring(L, field);
            Owner.translator.push(L, value);
            lua.rawset(L, -3);
            lua.pop(L, 1);                               StackAssert.End();
        }
Exemplo n.º 5
0
        /// <summary><see cref="RawGet(int)"/> alternative that only sets plain Lua value types and strings.</summary>
        public void RawSetValue(int field, LuaValue value)
        {
            value.VerifySupport("value");
            var L = Owner._L;

            luanet.checkstack(L, 2, "LuaTable.RawSetValue"); StackAssert.Start(L);
            push(L);
            value.push(L);
            lua.rawseti(L, -2, field);
            lua.pop(L, 1);                                    StackAssert.End();
        }
Exemplo n.º 6
0
        /// <summary><see cref="RawGet(int)"/> alternative that only fetches plain Lua value types and strings.</summary>
        public LuaValue RawGetValue(int field)
        {
            var L = Owner._L;

            luanet.checkstack(L, 2, "LuaTable.RawGetValue"); StackAssert.Start(L);
            push(L);
            lua.rawgeti(L, -1, field);
            var obj = LuaValue.read(L, -1, false);

            lua.pop(L, 2);                                    StackAssert.End();
            return(obj);
        }
Exemplo n.º 7
0
        /// <summary><see cref="RawGet(object)"/> alternative that only sets plain Lua value types and strings.</summary>
        public void RawSetValue(LuaValue field, LuaValue value)
        {
            field.VerifySupport("field"); value.VerifySupport("value");
            var L = Owner._L;

            luanet.checkstack(L, 3, "LuaTable.RawSetValue"); StackAssert.Start(L);
            push(L);
            field.push(L);
            value.push(L);
            lua.rawset(L, -3);
            lua.pop(L, 1);                                    StackAssert.End();
        }
Exemplo n.º 8
0
        /// <summary>Gets a numeric field of a table ignoring its metatable, if it exists</summary>
        public object RawGet(int field)
        {
            var L = Owner._L;

            luanet.checkstack(L, 2, "LuaTable.RawGet"); StackAssert.Start(L);
            push(L);
            lua.rawgeti(L, -1, field);
            var obj = Owner.translator.getObject(L, -1);

            lua.pop(L, 2);                               StackAssert.End();
            return(obj);
        }
Exemplo n.º 9
0
        /// <summary>Indexer alternative that only sets plain Lua value types and strings.</summary>
        public void SetValue(LuaValue field, LuaValue value)
        {
            field.VerifySupport("field"); value.VerifySupport("value");
            var L = Owner._L;

            luaclr.checkstack(L, 3, "LuaBase.SetValue"); StackAssert.Start(L);
            rawpush(L);
            field.push(L);
            value.push(L);
            lua.settable(L, -3);
            lua.pop(L, 1);                                StackAssert.End();
        }
Exemplo n.º 10
0
        /// <summary>Looks up the field, ignoring metatables, and gets its Lua type.</summary>
        public LuaType RawFieldType(object field)
        {
            var L = Owner._L;

            luanet.checkstack(L, 2, "LuaTable.RawFieldType"); StackAssert.Start(L);
            push(L);
            Owner.translator.push(L, field);
            lua.rawget(L, -2);
            var type = lua.type(L, -1);

            lua.pop(L, 2);                                     StackAssert.End();
            return((LuaType)type);
        }
Exemplo n.º 11
0
        /// <summary>Looks up the field and gets its Lua type. The field's value is discarded without performing any Lua to CLR translation.</summary>
        public LuaType FieldType(object field)
        {
            var L = Owner._L;

            luaclr.checkstack(L, 2, "LuaBase.FieldType"); StackAssert.Start(L);
            push(L);
            Owner.translator.push(L, field);
            lua.gettable(L, -2);
            var type = lua.type(L, -1);

            lua.pop(L, 2);                                 StackAssert.End();
            return((LuaType)type);
        }
Exemplo n.º 12
0
        /// <summary><see cref="RawGet(object)"/> alternative that only fetches plain Lua value types and strings.</summary>
        public LuaValue RawGetValue(LuaValue field)
        {
            field.VerifySupport("field");
            var L = Owner._L;

            luanet.checkstack(L, 2, "LuaTable.RawGetValue"); StackAssert.Start(L);
            push(L);
            field.push(L);
            lua.rawget(L, -2);
            var obj = LuaValue.read(L, -1, false);

            lua.pop(L, 2);                                    StackAssert.End();
            return(obj);
        }
Exemplo n.º 13
0
        /// <summary>Indexer alternative that only fetches plain Lua value types and strings.</summary>
        public LuaValue GetValue(LuaValue field)
        {
            field.VerifySupport("field");
            var L = Owner._L;

            luaclr.checkstack(L, 2, "LuaBase.GetValue"); StackAssert.Start(L);
            rawpush(L);
            field.push(L);
            lua.gettable(L, -2);
            var obj = LuaValue.read(L, -1, false);

            lua.pop(L, 2);                                StackAssert.End();
            return(obj);
        }
Exemplo n.º 14
0
        /// <summary>Removes all entries from the table.</summary>
        public void Clear()
        {
            var L = Owner._L;

            luanet.checkstack(L, 4, "LuaTable.Clear"); StackAssert.Start(L);
            push(L);
            lua.pushnil(L);
            while (lua.next(L, -2))
            {
                lua.pop(L, 1);
                lua.pushvalue(L, -1);
                lua.pushnil(L);
                lua.rawset(L, -4);
            }
            lua.pop(L, 1);                              StackAssert.End();
        }
Exemplo n.º 15
0
        /// <summary>Counts the number of entries in the table.</summary>
        public ulong LongCount()
        {
            var L = Owner._L;

            luanet.checkstack(L, 3, "LuaTable.LongCount"); StackAssert.Start(L);
            ulong count = 0;

            push(L);
            lua.pushnil(L);
            while (lua.next(L, -2))
            {
                ++count;
                lua.pop(L, 1);
            }
            lua.pop(L, 1);                                  StackAssert.End();
            return(count);
        }
Exemplo n.º 16
0
        public ObjectTranslator(lua.State L, Lua interpreter)
        {
            Debug.Assert(interpreter.IsSameLua(L));
            luaclr.checkstack(L, 1, "new ObjectTranslator");
            this.interpreter = interpreter;
            typeChecker      = new CheckType(interpreter);
            metaFunctions    = new MetaFunctions(L, this);

            StackAssert.Start(L);
            lua.pushcfunction(L, _loadAssembly        = this.loadAssembly); lua.setglobal(L, "load_assembly");
            lua.pushcfunction(L, _importType          = this.importType); lua.setglobal(L, "import_type");
            lua.pushcfunction(L, _makeObject          = this.makeObject); lua.setglobal(L, "make_object");
            lua.pushcfunction(L, _freeObject          = this.freeObject); lua.setglobal(L, "free_object");
            lua.pushcfunction(L, _getMethodBysig      = this.getMethodBysig); lua.setglobal(L, "get_method_bysig");
            lua.pushcfunction(L, _getConstructorBysig = this.getConstructorBysig); lua.setglobal(L, "get_constructor_bysig");
            lua.pushcfunction(L, _ctype = this.ctype); lua.setglobal(L, "ctype");
            lua.pushcfunction(L, _enum  = this.@enum); lua.setglobal(L, "enum");
            StackAssert.End();
        }
Exemplo n.º 17
0
 /// <summary>Indexer for nested string fields of the Lua object.</summary>
 public object this[params string[] path]
 {
     get
     {
         var L = Owner._L;
         luanet.checkstack(L, 1, "LuaBase.get_Indexer(String[])"); StackAssert.Start(L);
         rawpush(L);
         var ret = Owner.getNestedObject(L, -1, path);
         lua.pop(L, 1);                                             StackAssert.End();
         return(ret);
     }
     set
     {
         var L = Owner._L;
         luanet.checkstack(L, 1, "LuaBase.set_Indexer(String[])"); StackAssert.Start(L);
         rawpush(L);
         Owner.setNestedObject(L, -1, path, value);
         lua.pop(L, 1);                                             StackAssert.End();
     }
 }
Exemplo n.º 18
0
        /// <summary>[-0, +1, m] Pushes a CLR object into the Lua stack as a userdata with the provided metatable</summary>
        void pushObject(lua.State L, object o)
        {
            Debug.Assert(interpreter.IsSameLua(L));
            if (o == null)
            {
                lua.pushnil(L);
                return;
            }
            luaL.checkstack(L, 3, "ObjectTranslator.pushObject");
            StackAssert.Start(L);

            luaclr.newref(L, o);

            // Gets or creates the metatable for the object's type
            if (luaclr.newrefmeta(L, o.GetType().AssemblyQualifiedName, 4))
            {
                metaFunctions.BuildObjectMetatable(L);
            }

            lua.setmetatable(L, -2);
            StackAssert.End(1);
        }
Exemplo n.º 19
0
 /// <summary>Indexer for string fields of the Lua object.</summary>
 public object this[string field]
 {
     get
     {
         var L = Owner._L;
         luanet.checkstack(L, 2, "LuaBase.get_Indexer(String)"); StackAssert.Start(L);
         rawpush(L);
         lua.getfield(L, -1, field);
         var obj = Owner.translator.getObject(L, -1);
         lua.pop(L, 2);                                           StackAssert.End();
         return(obj);
     }
     set
     {
         var L = Owner._L;
         luanet.checkstack(L, 2, "LuaBase.set_Indexer(String)"); StackAssert.Start(L);
         rawpush(L);
         Owner.translator.push(L, value);
         lua.setfield(L, -2, field);
         lua.pop(L, 1);                                           StackAssert.End();
     }
 }
Exemplo n.º 20
0
 /// <summary>Indexer for numeric fields of the Lua object.</summary>
 public object this[object field]
 {
     get
     {
         var L = Owner._L;
         luaclr.checkstack(L, 2, "LuaBase.get_Indexer(Object)"); StackAssert.Start(L);
         rawpush(L);
         Owner.translator.push(L, field);
         lua.gettable(L, -2);
         var obj = Owner.translator.getObject(L, -1);
         lua.pop(L, 2);                                           StackAssert.End();
         return(obj);
     }
     set
     {
         var L = Owner._L;
         luaclr.checkstack(L, 3, "LuaBase.set_Indexer(Object)"); StackAssert.Start(L);
         rawpush(L);
         Owner.translator.push(L, field);
         Owner.translator.push(L, value);
         lua.settable(L, -3);
         lua.pop(L, 1);                                           StackAssert.End();
     }
 }