Exemple #1
0
        public void Glue(string objectType, string tableName, Lua.luaL_Reg[] methods, Lua.lua_CFunction createFunction)
        {
            Lua.luaL_newmetatable(this, "GLua." + objectType);
            Lua.lua_pushstring(this, "__index");
            Lua.lua_pushvalue(this, -2);  /* pushes the metatable */
            Lua.lua_settable(this, -3);  /* metatable.__index = metatable */
            var functions = new List<Lua.luaL_Reg>();
            var ent = new Lua.luaL_Reg();
            ent.name = "new";
            ent.func = createFunction;
            functions.Add(ent);

            var gc = new Lua.luaL_Reg();
            gc.name = "__gc";
            gc.func = GcFunction;

            var mt = methods.ToList();
            mt.Add(gc); /* hack in our __gc automagic */
            methods = mt.ToArray();

            // Register the methods for referenced objects
            luaL_module(this, null, methods, 0);

            // Register the 'static' functions
            luaL_module(this, tableName, functions.ToArray(), 0);

            return;
        }
Exemple #2
0
        public void Glue(Type t, Object o, string internalType, string tableName)
        {
            if (t.IsEnum)
            {
                GlueEnum(t, internalType, tableName);
                return;
            }

            var staticFunctions = new List<Lua.luaL_Reg>();
            var objectFunctions = new List<Lua.luaL_Reg>();

            var functions = t.GetMethods().Where(m => m.IsStatic);

            var methods = t.GetMethods().Where(m => !m.IsStatic);

            GluedCallback lastCb = null;

            foreach (var mi in functions.OrderBy(f => f.Name))
            {
                var f = (o == null) ? GluedCallback.Wrap(mi) : GluedCallback.Wrap(mi, o);
                var e = new Lua.luaL_Reg {func = f.OnCall, name = mi.Name};

                CallbackReferences.Add(f);
                CallbackReferences2.Add(e);
                if (lastCb == null || lastCb.Method.Name != mi.Name)
                {
                    staticFunctions.Add(e);

                    lastCb = f;
                    if (mi.GetParameters().FirstOrDefault() != null)
                    {
                        if (mi.GetParameters().FirstOrDefault().ParameterType == t)
                        {
                            objectFunctions.Add(e);
                        }
                    }
                }else
                {
                    lastCb.Overloads.Add(f);
                }
            }

            lastCb = null;
            foreach (var mi in methods.OrderBy(f => f.Name))
            {
                var f = (o == null) ? GluedCallback.Wrap(mi) : GluedCallback.Wrap(mi, o);
                var e = new Lua.luaL_Reg { func = f.OnCall, name = mi.Name };

                CallbackReferences.Add(f);
                CallbackReferences2.Add(e);

                if (lastCb == null || lastCb.Method.Name != mi.Name)
                {
                    lastCb = f;
                    objectFunctions.Add(e);
                }else
                {

                    lastCb.Overloads.Add(f);
                }
            }

            Glue(internalType, tableName, objectFunctions.ToArray(), staticFunctions.ToArray());
        }