Esempio n. 1
0
        private object getAsDecimal(LuaCore.lua_State luaState, int stackPos)
        {
            decimal retVal = (decimal)LuaLib.lua_tonumber(luaState, stackPos);

            if (retVal == 0 && !LuaLib.lua_isnumber(luaState, stackPos))
            {
                return(null);
            }

            return(retVal);
        }
Esempio n. 2
0
        private object getAsFloat(LuaCore.lua_State luaState, int stackPos)
        {
            float retVal = (float)LuaLib.lua_tonumber(luaState, stackPos);

            if (retVal == 0 && !LuaLib.lua_isnumber(luaState, stackPos))
            {
                return(null);
            }

            return(retVal);
        }
Esempio n. 3
0
        private object getAsChar(LuaCore.lua_State luaState, int stackPos)
        {
            char retVal = (char)LuaLib.lua_tonumber(luaState, stackPos);

            if (retVal == 0 && !LuaLib.lua_isnumber(luaState, stackPos))
            {
                return(null);
            }

            return(retVal);
        }
Esempio n. 4
0
        private object getAsDouble(LuaCore.lua_State luaState, int stackPos)
        {
            double retVal = LuaLib.lua_tonumber(luaState, stackPos);

            if (retVal == 0 && !LuaLib.lua_isnumber(luaState, stackPos))
            {
                return(null);
            }

            return(retVal);
        }
Esempio n. 5
0
        private object getAsUlong(LuaCore.lua_State luaState, int stackPos)
        {
            ulong retVal = (ulong)LuaLib.lua_tonumber(luaState, stackPos);

            if (retVal == 0 && !LuaLib.lua_isnumber(luaState, stackPos))
            {
                return(null);
            }

            return(retVal);
        }
Esempio n. 6
0
        private object getAsByte(LuaCore.lua_State luaState, int stackPos)
        {
            byte retVal = (byte)LuaLib.lua_tonumber(luaState, stackPos);

            if (retVal == 0 && !LuaLib.lua_isnumber(luaState, stackPos))
            {
                return(null);
            }

            return(retVal);
        }
Esempio n. 7
0
        /*
         * __index metafunction of type references, works on static members.
         */
        private int getClassMethod(LuaCore.lua_State luaState)
        {
            IReflect klass;
            object   obj = translator.getRawNetObject(luaState, 1);

            if (obj.IsNull() || !(obj is IReflect))
            {
                translator.throwError(luaState, "trying to index an invalid type reference");
                LuaLib.lua_pushnil(luaState);
                return(1);
            }
            else
            {
                klass = (IReflect)obj;
            }

            if (LuaLib.lua_isnumber(luaState, 2))
            {
                int size = (int)LuaLib.lua_tonumber(luaState, 2);
                translator.push(luaState, Array.CreateInstance(klass.UnderlyingSystemType, size));
                return(1);
            }
            else
            {
                string methodName = LuaLib.lua_tostring(luaState, 2).ToString();

                if (methodName.IsNull())
                {
                    LuaLib.lua_pushnil(luaState);
                    return(1);
                }                 //CP: Ignore case
                else
                {
                    return(getMember(luaState, klass, null, methodName, BindingFlags.FlattenHierarchy | BindingFlags.Static | BindingFlags.IgnoreCase));
                }
            }
        }
Esempio n. 8
0
        /*
         * __newindex metafunction of CLR objects. Receives the object,
         * the member name and the value to be stored as arguments. Throws
         * and error if the assignment is invalid.
         */
        private int setFieldOrProperty(LuaCore.lua_State luaState)
        {
            object target = translator.getRawNetObject(luaState, 1);

            if (target.IsNull())
            {
                translator.throwError(luaState, "trying to index and invalid object reference");
                return(0);
            }

            var type = target.GetType();

            // First try to look up the parameter as a property name
            string detailMessage;
            bool   didMember = trySetMember(luaState, type, target, BindingFlags.Instance | BindingFlags.IgnoreCase, out detailMessage);

            if (didMember)
            {
                return(0);                         // Must have found the property name
            }
            // We didn't find a property name, now see if we can use a [] style this accessor to set array contents
            try
            {
                if (type.IsArray && LuaLib.lua_isnumber(luaState, 2))
                {
                    int    index = (int)LuaLib.lua_tonumber(luaState, 2);
                    var    arr   = (Array)target;
                    object val   = translator.getAsType(luaState, 3, arr.GetType().GetElementType());
                    arr.SetValue(val, index);
                }
                else
                {
                    // Try to see if we have a this[] accessor
                    var setter = type.GetMethod("set_Item");
                    if (!setter.IsNull())
                    {
                        var args      = setter.GetParameters();
                        var valueType = args[1].ParameterType;

                        // The new val ue the user specified
                        object val       = translator.getAsType(luaState, 3, valueType);
                        var    indexType = args[0].ParameterType;
                        object index     = translator.getAsType(luaState, 2, indexType);

                        object[] methodArgs = new object[2];

                        // Just call the indexer - if out of bounds an exception will happen
                        methodArgs[0] = index;
                        methodArgs[1] = val;
                        setter.Invoke(target, methodArgs);
                    }
                    else
                    {
                        translator.throwError(luaState, detailMessage);                         // Pass the original message from trySetMember because it is probably best
                    }
                }
            }
            catch (SEHException)
            {
                // If we are seeing a C++ exception - this must actually be for Lua's private use.  Let it handle it
                throw;
            }
            catch (Exception e)
            {
                ThrowError(luaState, e);
            }

            return(0);
        }
Esempio n. 9
0
        internal ExtractValue checkType(LuaCore.lua_State luaState, int stackPos, Type paramType)
        {
            var luatype = LuaLib.lua_type(luaState, stackPos);

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

            var underlyingType = Nullable.GetUnderlyingType(paramType);

            if (!underlyingType.IsNull())
            {
                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.Boolean)
                {
                    return(extractValues[typeof(bool).TypeHandle.Value.ToInt64()]);
                }
                else if (luatype == LuaTypes.String)
                {
                    return(extractValues[typeof(string).TypeHandle.Value.ToInt64()]);
                }
                else if (luatype == LuaTypes.Table)
                {
                    return(extractValues[typeof(LuaTable).TypeHandle.Value.ToInt64()]);
                }
                else if (luatype == LuaTypes.UserData)
                {
                    return(extractValues[typeof(object).TypeHandle.Value.ToInt64()]);
                }
                else if (luatype == LuaTypes.Function)
                {
                    return(extractValues[typeof(LuaFunction).TypeHandle.Value.ToInt64()]);
                }
                else if (luatype == LuaTypes.Number)
                {
                    return(extractValues[typeof(double).TypeHandle.Value.ToInt64()]);
                }
                //else
                //;//an unsupported type was encountered
            }

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

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

            return(null);
        }