luaL_getmetafield() public static method

public static luaL_getmetafield ( IntPtr luaState, int stackPos, string field ) : bool
luaState System.IntPtr
stackPos int
field string
return bool
示例#1
0
 public static int luaL_callmeta(IntPtr L, int stackPos, string field)
 {
     stackPos = LuaDLL.abs_index(L, stackPos);
     if (LuaDLL.luaL_getmetafield(L, stackPos, field) == 0)
     {
         return(0);
     }
     LuaDLL.lua_pushvalue(L, stackPos);
     if (LuaDLL.lua_pcall(L, 1, 1, 0) != 0)
     {
         string msg = LuaDLL.lua_tostring(L, -1);
         LuaDLL.lua_pop(L, 1);
         throw new LuaException(msg, null, 1);
     }
     return(1);
 }
示例#2
0
 public object getAsObject(IntPtr luaState, int stackPos)
 {
     if (LuaDLL.lua_type(luaState, stackPos) == LuaTypes.LUA_TTABLE && LuaDLL.luaL_getmetafield(luaState, stackPos, "__index") != LuaTypes.LUA_TNIL)
     {
         if (LuaDLL.luaL_checkmetatable(luaState, -1))
         {
             LuaDLL.lua_insert(luaState, stackPos);
             LuaDLL.lua_remove(luaState, stackPos + 1);
         }
         else
         {
             LuaDLL.lua_settop(luaState, -2);
         }
     }
     return(this.translator.getObject(luaState, stackPos));
 }
示例#3
0
        public object getAsObject(KopiLua.Lua.lua_State luaState, int stackPos)
        {
            if (LuaDLL.lua_type(luaState, stackPos) == LuaTypes.LUA_TTABLE)
            {
                if (LuaDLL.luaL_getmetafield(luaState, stackPos, "__index"))
                {
                    if (LuaDLL.luaL_checkmetatable(luaState, -1))
                    {
                        LuaDLL.lua_insert(luaState, stackPos);
                        LuaDLL.lua_remove(luaState, stackPos + 1);
                    }
                    else
                    {
                        LuaDLL.lua_settop(luaState, -2);
                    }
                }
            }
            object obj = translator.getObject(luaState, stackPos);

            return(obj);
        }
示例#4
0
        public object getAsNetObject(IntPtr luaState, int stackPos)
        {
            object obj = translator.getNetObject(luaState, stackPos);

            if (obj == null && LuaDLL.lua_type(luaState, stackPos) == LuaTypes.LUA_TTABLE)
            {
                if (LuaDLL.luaL_getmetafield(luaState, stackPos, "__index"))
                {
                    if (LuaDLL.luaL_checkmetatable(luaState, -1))
                    {
                        LuaDLL.lua_insert(luaState, stackPos);
                        LuaDLL.lua_remove(luaState, stackPos + 1);
                        obj = translator.getNetObject(luaState, stackPos);
                    }
                    else
                    {
                        LuaDLL.lua_settop(luaState, -2);
                    }
                }
            }
            return(obj);
        }
示例#5
0
        internal ExtractValue checkType(IntPtr luaState, int stackPos, Type paramType)
        {
            LuaTypes luatype = LuaDLL.lua_type(luaState, stackPos);

            if (paramType.IsByRef)
            {
                paramType = paramType.GetElementType();
            }

            Type underlyingType = Nullable.GetUnderlyingType(paramType);

            if (underlyingType != null)
            {
                paramType = underlyingType;     // Silently convert nullable types to their non null requics
            }

            long runtimeHandleValue = paramType.TypeHandle.Value.ToInt64();

            if (paramType.Equals(typeof(object)))
            {
                return(extractValues[runtimeHandleValue]);
            }

            //CP: Added support for generic parameters
            if (paramType.IsGenericParameter)
            {
                if (luatype == LuaTypes.LUA_TBOOLEAN)
                {
                    return(extractValues[typeof(bool).TypeHandle.Value.ToInt64()]);
                }
                else if (luatype == LuaTypes.LUA_TSTRING)
                {
                    return(extractValues[typeof(string).TypeHandle.Value.ToInt64()]);
                }
                else if (luatype == LuaTypes.LUA_TTABLE)
                {
                    return(extractValues[typeof(LuaTable).TypeHandle.Value.ToInt64()]);
                }
                else if (luatype == LuaTypes.LUA_TUSERDATA)
                {
                    return(extractValues[typeof(object).TypeHandle.Value.ToInt64()]);
                }
                else if (luatype == LuaTypes.LUA_TFUNCTION)
                {
                    return(extractValues[typeof(LuaFunction).TypeHandle.Value.ToInt64()]);
                }
                else if (luatype == LuaTypes.LUA_TNUMBER)
                {
                    return(extractValues[typeof(double).TypeHandle.Value.ToInt64()]);
                }
                //else // suppress CS0642
                ;    //an unsupported type was encountered
            }

            if (LuaDLL.lua_isnumber(luaState, stackPos))
            {
                return(extractValues[runtimeHandleValue]);
            }

            if (paramType == typeof(bool))
            {
                if (LuaDLL.lua_isboolean(luaState, stackPos))
                {
                    return(extractValues[runtimeHandleValue]);
                }
            }
            else if (paramType == typeof(string))
            {
                if (LuaDLL.lua_isstring(luaState, stackPos))
                {
                    return(extractValues[runtimeHandleValue]);
                }
                else if (luatype == LuaTypes.LUA_TNIL)
                {
                    return(extractNetObject); // kevinh - silently convert nil to a null string pointer
                }
            }
            else if (paramType == typeof(LuaTable))
            {
                if (luatype == LuaTypes.LUA_TTABLE)
                {
                    return(extractValues[runtimeHandleValue]);
                }
            }
            else if (paramType == typeof(LuaUserData))
            {
                if (luatype == LuaTypes.LUA_TUSERDATA)
                {
                    return(extractValues[runtimeHandleValue]);
                }
            }
            else if (paramType == typeof(LuaFunction))
            {
                if (luatype == LuaTypes.LUA_TFUNCTION)
                {
                    return(extractValues[runtimeHandleValue]);
                }
            }
            else if (typeof(Delegate).IsAssignableFrom(paramType) && luatype == LuaTypes.LUA_TFUNCTION)
            {
                return(new ExtractValue(new DelegateGenerator(translator, paramType).extractGenerated));
            }
            else if (paramType.IsInterface && luatype == LuaTypes.LUA_TTABLE)
            {
                return(new ExtractValue(new ClassGenerator(translator, paramType).extractGenerated));
            }
            else if ((paramType.IsInterface || paramType.IsClass) && luatype == LuaTypes.LUA_TNIL)
            {
                // kevinh - allow nil to be silently converted to null - extractNetObject will return null when the item ain't found
                return(extractNetObject);
            }
            else if (LuaDLL.lua_type(luaState, stackPos) == LuaTypes.LUA_TTABLE)
            {
                if (LuaDLL.luaL_getmetafield(luaState, stackPos, "__index"))
                {
                    object obj = translator.getNetObject(luaState, -1);
                    LuaDLL.lua_settop(luaState, -2);
                    if (obj != null && paramType.IsAssignableFrom(obj.GetType()))
                    {
                        return(extractNetObject);
                    }
                }
                else
                {
                    return(null);
                }
            }
            else
            {
                object obj = translator.getNetObject(luaState, stackPos);
                if (obj != null && paramType.IsAssignableFrom(obj.GetType()))
                {
                    return(extractNetObject);
                }
            }

            return(null);
        }
示例#6
0
文件: CheckType.cs 项目: qipa/tolua-2
        internal ExtractValue checkType(IntPtr luaState, int stackPos, Type paramType)
        {
            LuaTypes luatype = LuaDLL.lua_type(luaState, stackPos);

            if (paramType.IsByRef)
            {
                paramType = paramType.GetElementType();
            }

            Type underlyingType = Nullable.GetUnderlyingType(paramType);

            if (underlyingType != null)
            {
                paramType = underlyingType;     // Silently convert nullable types to their non null requics
            }

            long runtimeHandleValue = paramType.TypeHandle.Value.ToInt64();

            if (paramType.Equals(typeof(object)))
            {
                return(extractValues[runtimeHandleValue]);
            }

            //CP: Added support for generic parameters
            if (paramType.IsGenericParameter)
            {
                if (luatype == LuaTypes.LUA_TBOOLEAN)
                {
                    return(extractValues[typeof(bool).TypeHandle.Value.ToInt64()]);
                }
                else if (luatype == LuaTypes.LUA_TSTRING)
                {
                    return(extractValues[typeof(string).TypeHandle.Value.ToInt64()]);
                }
                else if (luatype == LuaTypes.LUA_TTABLE)
                {
                    return(extractValues[typeof(LuaTable).TypeHandle.Value.ToInt64()]);
                }
                else if (luatype == LuaTypes.LUA_TUSERDATA)
                {
                    return(extractValues[typeof(object).TypeHandle.Value.ToInt64()]);
                }
                else if (luatype == LuaTypes.LUA_TFUNCTION)
                {
                    return(extractValues[typeof(LuaFunction).TypeHandle.Value.ToInt64()]);
                }
                else if (luatype == LuaTypes.LUA_TNUMBER)
                {
                    return(extractValues[typeof(double).TypeHandle.Value.ToInt64()]);
                }
                //else // suppress CS0642
                ;    //an unsupported type was encountered
            }

            if (paramType.IsValueType && luatype == LuaTypes.LUA_TTABLE)
            {
                int          oldTop = LuaDLL.lua_gettop(luaState);
                ExtractValue ret    = null;
                LuaDLL.lua_pushvalue(luaState, stackPos);
                LuaDLL.lua_pushstring(luaState, "class");
                LuaDLL.lua_gettable(luaState, -2);

                if (!LuaDLL.lua_isnil(luaState, -1))
                {
                    string cls = LuaDLL.lua_tostring(luaState, -1);

                    if (cls == "Vector3" && paramType == typeof(Vector3))
                    {
                        ret = extractValues[typeof(Vector3).TypeHandle.Value.ToInt64()];
                    }
                    else if (cls == "Vector2" && paramType == typeof(Vector2))
                    {
                        ret = extractValues[typeof(Vector2).TypeHandle.Value.ToInt64()];
                    }
                    else if (cls == "Quaternion" && paramType == typeof(Quaternion))
                    {
                        ret = extractValues[typeof(Quaternion).TypeHandle.Value.ToInt64()];
                    }
                    else if (cls == "Color" && paramType == typeof(Color))
                    {
                        ret = extractValues[typeof(Color).TypeHandle.Value.ToInt64()];
                    }
                    else if (cls == "Vector4" && paramType == typeof(Vector4))
                    {
                        ret = extractValues[typeof(Vector4).TypeHandle.Value.ToInt64()];
                    }
                    else if (cls == "Ray" && paramType == typeof(Ray))
                    {
                        ret = extractValues[typeof(Ray).TypeHandle.Value.ToInt64()];
                    }
                    else
                    {
                        ret = null;
                    }
                }

                LuaDLL.lua_settop(luaState, oldTop);

                if (ret != null)
                {
                    return(ret);
                }
            }

            if (LuaDLL.lua_isnumber(luaState, stackPos))
            {
                return(extractValues[runtimeHandleValue]);
            }

            if (paramType == typeof(bool))
            {
                if (LuaDLL.lua_isboolean(luaState, stackPos))
                {
                    return(extractValues[runtimeHandleValue]);
                }
            }
            else if (paramType == typeof(string))
            {
                if (LuaDLL.lua_isstring(luaState, stackPos))
                {
                    return(extractValues[runtimeHandleValue]);
                }
                else if (luatype == LuaTypes.LUA_TNIL)
                {
                    return(extractNetObject); // kevinh - silently convert nil to a null string pointer
                }
            }
            else if (paramType == typeof(LuaTable))
            {
                if (luatype == LuaTypes.LUA_TTABLE)
                {
                    return(extractValues[runtimeHandleValue]);
                }
            }
            else if (paramType == typeof(LuaFunction))
            {
                if (luatype == LuaTypes.LUA_TFUNCTION)
                {
                    return(extractValues[runtimeHandleValue]);
                }
            }
            else if (typeof(Delegate).IsAssignableFrom(paramType) && luatype == LuaTypes.LUA_TFUNCTION)
            {
#if __NOGEN__
                translator.throwError(luaState, "Delegates not implemnented");
#else
                return(new ExtractValue(new DelegateGenerator(translator, paramType).extractGenerated));
#endif
            }
            else if (paramType.IsInterface && luatype == LuaTypes.LUA_TTABLE)
            {
#if __NOGEN__
                translator.throwError(luaState, "Interfaces not implemnented");
#else
                return(new ExtractValue(new ClassGenerator(translator, paramType).extractGenerated));
#endif
            }
            else if ((paramType.IsInterface || paramType.IsClass) && luatype == LuaTypes.LUA_TNIL)
            {
                // kevinh - allow nil to be silently converted to null - extractNetObject will return null when the item ain't found
                return(extractNetObject);
            }
            else if (LuaDLL.lua_type(luaState, stackPos) == LuaTypes.LUA_TTABLE)
            {
                if (LuaTypes.LUA_TNIL != LuaDLL.luaL_getmetafield(luaState, stackPos, "__index"))
                {
                    object obj = translator.getNetObject(luaState, -1);
                    LuaDLL.lua_settop(luaState, -2);
                    if (obj != null && paramType.IsAssignableFrom(obj.GetType()))
                    {
                        return(extractNetObject);
                    }
                }
            }
            else
            {
                //object obj = translator.getNetObject(luaState, stackPos);  //topameng 修改这里使支持注册到c#的lua类
                object obj = translator.getRawNetObject(luaState, stackPos);
                if (obj != null && paramType.IsAssignableFrom(obj.GetType()))
                {
                    return(extractNetObject);
                }
            }

            return(null);
        }
示例#7
0
        internal ExtractValue checkType(IntPtr luaState, int stackPos, Type paramType)
        {
            LuaTypes luaTypes = LuaDLL.lua_type(luaState, stackPos);

            if (paramType.IsByRef)
            {
                paramType = paramType.GetElementType();
            }
            Type underlyingType = Nullable.GetUnderlyingType(paramType);

            if (underlyingType != null)
            {
                paramType = underlyingType;
            }
            long key = paramType.TypeHandle.Value.ToInt64();

            if (paramType.Equals(typeof(object)))
            {
                return(this.extractValues[key]);
            }
            if (paramType.IsGenericParameter)
            {
                if (luaTypes == LuaTypes.LUA_TBOOLEAN)
                {
                    return(this.extractValues[typeof(bool).TypeHandle.Value.ToInt64()]);
                }
                if (luaTypes == LuaTypes.LUA_TSTRING)
                {
                    return(this.extractValues[typeof(string).TypeHandle.Value.ToInt64()]);
                }
                if (luaTypes == LuaTypes.LUA_TTABLE)
                {
                    return(this.extractValues[typeof(LuaTable).TypeHandle.Value.ToInt64()]);
                }
                if (luaTypes == LuaTypes.LUA_TUSERDATA)
                {
                    return(this.extractValues[typeof(object).TypeHandle.Value.ToInt64()]);
                }
                if (luaTypes == LuaTypes.LUA_TFUNCTION)
                {
                    return(this.extractValues[typeof(LuaFunction).TypeHandle.Value.ToInt64()]);
                }
                if (luaTypes == LuaTypes.LUA_TNUMBER)
                {
                    return(this.extractValues[typeof(double).TypeHandle.Value.ToInt64()]);
                }
            }
            if (paramType.IsValueType && luaTypes == LuaTypes.LUA_TTABLE)
            {
                int          newTop       = LuaDLL.lua_gettop(luaState);
                ExtractValue extractValue = null;
                LuaDLL.lua_pushvalue(luaState, stackPos);
                LuaDLL.lua_pushstring(luaState, "class");
                LuaDLL.lua_gettable(luaState, -2);
                if (!LuaDLL.lua_isnil(luaState, -1))
                {
                    string a = LuaDLL.lua_tostring(luaState, -1);
                    if (a == "Vector3" && paramType == typeof(Vector3))
                    {
                        extractValue = this.extractValues[typeof(Vector3).TypeHandle.Value.ToInt64()];
                    }
                    else if (a == "Vector2" && paramType == typeof(Vector2))
                    {
                        extractValue = this.extractValues[typeof(Vector2).TypeHandle.Value.ToInt64()];
                    }
                    else if (a == "Quaternion" && paramType == typeof(Quaternion))
                    {
                        extractValue = this.extractValues[typeof(Quaternion).TypeHandle.Value.ToInt64()];
                    }
                    else if (a == "Color" && paramType == typeof(Color))
                    {
                        extractValue = this.extractValues[typeof(Color).TypeHandle.Value.ToInt64()];
                    }
                    else if (a == "Vector4" && paramType == typeof(Vector4))
                    {
                        extractValue = this.extractValues[typeof(Vector4).TypeHandle.Value.ToInt64()];
                    }
                    else if (a == "Ray" && paramType == typeof(Ray))
                    {
                        extractValue = this.extractValues[typeof(Ray).TypeHandle.Value.ToInt64()];
                    }
                    else
                    {
                        extractValue = null;
                    }
                }
                LuaDLL.lua_settop(luaState, newTop);
                if (extractValue != null)
                {
                    return(extractValue);
                }
            }
            if (LuaDLL.lua_isnumber(luaState, stackPos))
            {
                return(this.extractValues[key]);
            }
            if (paramType == typeof(bool))
            {
                if (LuaDLL.lua_isboolean(luaState, stackPos))
                {
                    return(this.extractValues[key]);
                }
            }
            else if (paramType == typeof(string))
            {
                if (LuaDLL.lua_isstring(luaState, stackPos))
                {
                    return(this.extractValues[key]);
                }
                if (luaTypes == LuaTypes.LUA_TNIL)
                {
                    return(this.extractNetObject);
                }
            }
            else if (paramType == typeof(LuaTable))
            {
                if (luaTypes == LuaTypes.LUA_TTABLE)
                {
                    return(this.extractValues[key]);
                }
            }
            else if (paramType == typeof(LuaFunction))
            {
                if (luaTypes == LuaTypes.LUA_TFUNCTION)
                {
                    return(this.extractValues[key]);
                }
            }
            else if (typeof(Delegate).IsAssignableFrom(paramType) && luaTypes == LuaTypes.LUA_TFUNCTION)
            {
                this.translator.throwError(luaState, "Delegates not implemnented");
            }
            else if (paramType.IsInterface && luaTypes == LuaTypes.LUA_TTABLE)
            {
                this.translator.throwError(luaState, "Interfaces not implemnented");
            }
            else
            {
                if ((paramType.IsInterface || paramType.IsClass) && luaTypes == LuaTypes.LUA_TNIL)
                {
                    return(this.extractNetObject);
                }
                if (LuaDLL.lua_type(luaState, stackPos) == LuaTypes.LUA_TTABLE)
                {
                    if (LuaDLL.luaL_getmetafield(luaState, stackPos, "__index") != LuaTypes.LUA_TNIL)
                    {
                        object netObject = this.translator.getNetObject(luaState, -1);
                        LuaDLL.lua_settop(luaState, -2);
                        if (netObject != null && paramType.IsAssignableFrom(netObject.GetType()))
                        {
                            return(this.extractNetObject);
                        }
                    }
                }
                else
                {
                    object rawNetObject = this.translator.getRawNetObject(luaState, stackPos);
                    if (rawNetObject != null && paramType.IsAssignableFrom(rawNetObject.GetType()))
                    {
                        return(this.extractNetObject);
                    }
                }
            }
            return(null);
        }
示例#8
0
        internal ExtractValue checkType(IntPtr luaState, int stackPos, Type paramType)
        {
            LuaTypes luatype = LuaDLL.lua_type(luaState, stackPos);

            if (paramType.IsByRef)
            {
                paramType = paramType.GetElementType();
            }

            Type underlyingType = Nullable.GetUnderlyingType(paramType);

            if (underlyingType != null)
            {
                paramType = underlyingType;     // Silently convert nullable types to their non null requics
            }

            if (paramType.Equals(typeof(object)))
            {
                return(extractObject);
            }
            else if (paramType.Equals(typeof(sbyte)))
            {
                if (LuaDLL.lua_isnumber(luaState, stackPos))
                {
                    return(extractSbyte);
                }
            }
            else if (paramType.Equals(typeof(byte)))
            {
                if (LuaDLL.lua_isnumber(luaState, stackPos))
                {
                    return(extractByte);
                }
            }
            else if (paramType.Equals(typeof(short)))
            {
                if (LuaDLL.lua_isnumber(luaState, stackPos))
                {
                    return(extractShort);
                }
            }
            else if (paramType.Equals(typeof(ushort)))
            {
                if (LuaDLL.lua_isnumber(luaState, stackPos))
                {
                    return(extractUshort);
                }
            }
            else if (paramType.Equals(typeof(int)))
            {
                if (LuaDLL.lua_isnumber(luaState, stackPos))
                {
                    return(extractInt);
                }
            }
            else if (paramType.Equals(typeof(uint)))
            {
                if (LuaDLL.lua_isnumber(luaState, stackPos))
                {
                    return(extractUint);
                }
            }
            else if (paramType.Equals(typeof(long)))
            {
                if (LuaDLL.lua_isnumber(luaState, stackPos))
                {
                    return(extractLong);
                }
            }
            else if (paramType.Equals(typeof(ulong)))
            {
                if (LuaDLL.lua_isnumber(luaState, stackPos))
                {
                    return(extractUlong);
                }
            }
            else if (paramType.Equals(typeof(char)))
            {
                if (LuaDLL.lua_isnumber(luaState, stackPos))
                {
                    return(extractChar);
                }
            }
            else if (paramType.Equals(typeof(float)))
            {
                if (LuaDLL.lua_isnumber(luaState, stackPos))
                {
                    return(extractFloat);
                }
            }
            else if (paramType.Equals(typeof(decimal)))
            {
                if (LuaDLL.lua_isnumber(luaState, stackPos))
                {
                    return(extractDecimal);
                }
            }
            else if (paramType.Equals(typeof(double)))
            {
                if (LuaDLL.lua_isnumber(luaState, stackPos))
                {
                    return(extractDouble);
                }
            }
            else if (paramType.Equals(typeof(bool)))
            {
                if (LuaDLL.lua_isboolean(luaState, stackPos))
                {
                    return(extractBoolean);
                }
            }
            else if (paramType.Equals(typeof(string)))
            {
                if (LuaDLL.lua_isstring(luaState, stackPos))
                {
                    return(extractString);
                }
                else if (luatype == LuaTypes.LUA_TNIL)
                {
                    return(extractNetObject);            // kevinh - silently convert nil to a null string pointer
                }
            }
            else if (paramType.Equals(typeof(LuaTable)))
            {
                if (luatype == LuaTypes.LUA_TTABLE)
                {
                    return(extractTable);
                }
            }
            else if (paramType.Equals(typeof(LuaUserData)))
            {
                if (luatype == LuaTypes.LUA_TUSERDATA)
                {
                    return(extractUserdata);
                }
            }
            else if (paramType.Equals(typeof(LuaFunction)))
            {
                if (luatype == LuaTypes.LUA_TFUNCTION)
                {
                    return(extractFunction);
                }
            }
            else if (typeof(Delegate).IsAssignableFrom(paramType) &&
                     luatype == LuaTypes.LUA_TFUNCTION)
            {
                return(new ExtractValue(new DelegateGenerator(translator, paramType).extractGenerated));
            }
            else if (paramType.IsInterface &&
                     luatype == LuaTypes.LUA_TTABLE)
            {
                return(new ExtractValue(new ClassGenerator(translator, paramType).extractGenerated));
            }
            else if ((paramType.IsInterface || paramType.IsClass) &&
                     luatype == LuaTypes.LUA_TNIL)
            {
                // kevinh - allow nil to be silently converted to null - extractNetObject will return null when the item ain't found
                return(extractNetObject);
            }
            else if (LuaDLL.lua_type(luaState, stackPos) == LuaTypes.LUA_TTABLE)
            {
                if (LuaDLL.luaL_getmetafield(luaState, stackPos, "__index"))
                {
                    object obj = translator.getNetObject(luaState, -1);
                    LuaDLL.lua_settop(luaState, -2);
                    if (obj != null && paramType.IsAssignableFrom(obj.GetType()))
                    {
                        return(extractNetObject);
                    }
                }
                else
                {
                    return(null);
                }
            }
            else
            {
                object obj = translator.getNetObject(luaState, stackPos);
                if (obj != null && paramType.IsAssignableFrom(obj.GetType()))
                {
                    return(extractNetObject);
                }
            }
            return(null);
        }