Exemple #1
0
 /// <summary>
 /// 把指定索引的值转成LuaFunction类型
 /// </summary>
 public static LuaFunction ToLuaFunction(this ILuaState self, int index)
 {
     if (self.IsFunction(index))
     {
         self.PushValue(index);
         return(new LuaFunction(self.L_Ref(LuaIndexes.LUA_REGISTRYINDEX), LuaEnv.Get(self).ls));
     }
     return(null);
 }
Exemple #2
0
    public static int __gc(ILuaState L)
    {
        int udata = LuaDLL.luanet_rawnetobj(L, 1);

        if (udata != -1)
        {
            var ls = LuaEnv.Get(L).ls;
            ls.translator.collectObject(udata);
        }

        return(0);
    }
Exemple #3
0
    public static int DoBuffer(this ILuaState self, byte[] nbytes, string name)
    {
        int oldTop = self.GetTop();

        self.PushCSharpFunction(LuaEnv.Get(self).ls.tracebackFunction);
        var b = oldTop + 1;

        if (self.L_LoadBuffer(nbytes, name) == LuaThreadStatus.LUA_OK &&
            self.PCall(0, -1, b) == LuaThreadStatus.LUA_OK)
        {
            self.Remove(b);
            return(self.GetTop() - oldTop);
        }

        ThrowExceptionFromError(self, oldTop);
        return(0);
    }
Exemple #4
0
    /// <summary>
    /// 将指定栈位置的数据转为它确切的类型
    /// 即:自动判断其类型,然后做转换;包括Unity内置类型。
    /// </summary>
    public static object ToAnyObject(this ILuaState self, int index)
    {
        var luaT = self.Type(index);

        switch (luaT)
        {
        case LuaTypes.LUA_TNUMBER:
            return((float)self.ToNumber(index));

        case LuaTypes.LUA_TSTRING:
            return(self.ToString(index));

        case LuaTypes.LUA_TUSERDATA:
        {
            var ls    = LuaEnv.Get(self).ls;
            int udata = LuaDLL.luanet_rawnetobj(self, index);

            if (udata != -1)
            {
                object obj = null;
                ls.translator.objects.TryGetValue(udata, out obj);
                return(obj);
            }
            else
            {
                return(null);
            }
        }

        case LuaTypes.LUA_TBOOLEAN:
            return(self.ToBoolean(index));

        case LuaTypes.LUA_TTABLE:
            return(self.ToAnyTable(index));

        case LuaTypes.LUA_TFUNCTION:
            return(self.ToLuaFunction(index));

        default:
            return(null);
        }
    }
Exemple #5
0
    /// <summary>
    /// If the value at the given acceptable index is a full userdata, returns its
    /// block address. If the value is a light userdata, returns its pointer.
    /// Otherwise, returns NULL.
    ///
    /// 这里只通过luanet来管理userdata
    /// </summary>
    public static object ToUserData(this ILuaState self, int index)
    {
        if (self.IsUserData(index))
        {
            int udata = LuaDLL.luanet_rawnetobj(self, index);

            if (udata != -1)
            {
                object obj = null;
                var    ls  = LuaEnv.Get(self).ls;
                ls.translator.objects.TryGetValue(udata, out obj);
                return(obj);
            }
        }
        else if (!self.IsNoneOrNil(index))
        {
            LogMgr.W("try to convert {0} to {1}", self.Type(index), LuaTypes.LUA_TUSERDATA);
        }
        return(null);
    }
Exemple #6
0
    /// <summary>
    /// Pushes a light userdata onto the stack.
    ///
    /// Userdata represent C values in Lua.A light userdata represents a pointer.
    /// It is a value (like a number): you do not create it, it has no individual metatable,
    /// and it is not collected(as it was never created).
    /// A light userdata is equal to "any" light userdata with the same C address.
    ///
    /// 这里只通过luanet来管理userdata
    /// ⚠部分Unity的值类型不是通过这个接口来压栈的!!
    /// </summary>
    public static void PushLightUserData(this ILuaState self, object value)
    {
        if (value != null)
        {
#if UNITY_ASSERTIONS
            if (value.GetType().IsValueType)
            {
                LogMgr.W("一个值类型被压栈:{0}", value);
            }
#endif
            var oldTop = self.GetTop();
            // 检查该类型是否已经Wrap,如果没有就自动Wrap
            var    type     = value.GetType();
            string metaName = self.BindRecursively(type);
            self.SetTop(oldTop);

            var ls = LuaEnv.Get(self).ls;
            ls.translator.pushObject(self, value, metaName);
        }
        else
        {
            self.PushNil();
        }
    }
    public static void PushUData(this ILuaState self, IEnumerator value)
    {
        var ls = LuaEnv.Get(self).ls;

        ls.translator.pushObject(self, value, s_meta);
    }
    public static void PushUData(this ILuaState self, System.Delegate value)
    {
        var ls = LuaEnv.Get(self).ls;

        ls.translator.pushObject(self, value, CLASS);
    }
    public static void PushUData(this ILuaState self, IList value)
    {
        var ls = LuaEnv.Get(self).ls;

        ls.translator.pushObject(self, value, META_TABLE);
    }
Exemple #10
0
    /// <summary>
    /// luanet: 值类型发生变化后,要重新映射
    /// </summary>
    public static void SetValue(this ILuaState self, int index, System.ValueType value)
    {
        var ls = LuaEnv.Get(self).ls;

        ls.translator.SetValueObject(self, index, value);
    }