Ejemplo n.º 1
0
        public void PushCSObject(object o)
        {
            if (o == null)
            {
                LuaLib.lua_pushnil(L);
                return;
            }

            int refId;

            if (!_objectUserdataRefIds.TryGetValue(o, out refId))
            {
                LuaBindMetatable metatable = Bindings.GetMetatable(o.GetType(), LuaBindMetatableType.Instance);

                int objectUserdataValue = _newObjectUserdataValue++;
                LuaLib.wutlua_newuserdata(L, objectUserdataValue);                              // |ud
                metatable.Push();                                                               // |ud|mt
                LuaLib.lua_setmetatable(L, -2);                                                 // |ud		// ud.metatable = mt

                refId = LuaLib.luaL_ref(L, LuaIndices.LUA_REGISTRYINDEX);                       // |

                _objects[objectUserdataValue] = o;
                _objectUserdataRefIds[o]      = refId;
            }
            LuaLib.lua_rawgeti(L, LuaIndices.LUA_REGISTRYINDEX, refId);                 // |ud
        }
Ejemplo n.º 2
0
        static int _GetBaseMetatable(IntPtr L)
        {
            LuaState luaState = LuaState.Get(L);

            int refId;

            LuaLib.lua_pushstring(L, "__refId");                                // |mt|s
            LuaLib.lua_rawget(L, 1);                                            // |mt|v	// v = mt.s
            refId = (int)LuaLib.lua_tonumber(L, -1);
            LuaLib.lua_pop(L, 1);                                               // |mt

            LuaBindMetatable self     = luaState.Bindings.GetMetatableByRefId(refId);
            Type             baseType = self._type.BaseType;

            if (baseType == null)
            {
                // No base type
                LuaLib.lua_pushstring(L, "__base");                             // |mt|s
                LuaLib.lua_pushboolean(L, false);                               // |mt|s|b
                LuaLib.lua_rawset(L, -3);                                       // |mt		// mt.__base = false
            }
            else
            {
                LuaBindMetatable baseMetatable = luaState.Bindings.GetMetatable(baseType, self._metatableType);

                LuaLib.lua_pushstring(L, "__base");                             // |mt|s
                baseMetatable.Push();                                           // |mt|s|basemt
                LuaLib.lua_rawset(L, -3);                                       // |mt		// mt.__base = basemt
            }

            return(0);
        }
Ejemplo n.º 3
0
        public LuaBindMetatable GetMetatable(Type type, LuaBindMetatableType metatableType)
        {
            LuaBindMetatable metatable;

            var metatables = _metatables[(int)metatableType];

            if (!metatables.TryGetValue(type, out metatable))
            {
                metatable        = new LuaBindMetatable(_luaState, type, metatableType);
                metatables[type] = metatable;
            }

            return(metatable);
        }
Ejemplo n.º 4
0
        public void RegisterType(Type type)
        {
            UnityEngine.Debug.Assert(!_typeTables.ContainsKey(type));

            IntPtr L = _luaState.L;

            LuaTable typeTable = new LuaTable(_luaState);

            typeTable.Push();                                                   // |t
            LuaLib.lua_pushstring(L, "__typeName");                             // |t|sk
            LuaLib.lua_pushstring(L, type.ToString());                          // |t|sk|sv
            LuaLib.lua_rawset(L, -3);                                           // |t		// t.__typeName = typeName

            LuaBindMetatable metatable = GetMetatable(type, LuaBindMetatableType.Type);

            metatable.Push();                                                           // |t|mt
            LuaLib.lua_setmetatable(L, -2);                                             // |t		// t.metatable = mt

            LuaLib.lua_pop(L, 1);                                                       // |

            _typeNames.Add(type.ToString(), type);
            _typeTables[type] = typeTable;
        }
Ejemplo n.º 5
0
 public void RegisterMetatableRefId(int refId, LuaBindMetatable metatable)
 {
     _metatableRefIds.Add(refId, metatable);
 }