示例#1
0
        /// <summary>
        /// Performs a binary arithmetic operation and returns the result.
        /// </summary>
        /// <param name="type">The type of operation to perform.</param>
        /// <param name="other">The other value to use.</param>
        /// <returns>The result of the operation.</returns>
        /// <exception cref="System.InvalidOperationException">
        /// If the operation cannot be performed with the given values.
        /// </exception>
        /// <exception cref="System.InvalidArgumentException">
        /// If the argument is an invalid value.
        /// </exception>
        ILuaValue ILuaValue.Arithmetic(BinaryOperationType type, ILuaValue other)
        {
            // Attempt to use a meta-method.
            var ret = LuaValueBase.AttempMetamethod(type, this, other);

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

            // Do some default operations.
            ret = DefaultArithmetic(type, other);
            if (ret != null)
            {
                return(ret);
            }

            // If the other is not a visitor, throw.
            if (!(other is ILuaValueVisitor))
            {
                throw new InvalidOperationException(Errors.CannotArithmetic(LuaValueType.Function));
            }
            else
            {
                return(((ILuaValueVisitor)other).Arithmetic(type, this));
            }
        }
示例#2
0
        /// <summary>
        /// Performs a binary arithmetic operation and returns the result.
        /// </summary>
        /// <param name="type">The type of operation to perform.</param>
        /// <param name="self">The first value to use.</param>
        /// <returns>The result of the operation.</returns>
        /// <exception cref="System.InvalidOperationException">
        /// If the operation cannot be performed with the given values.
        /// </exception>
        /// <exception cref="System.InvalidArgumentException">
        /// If the argument is an invalid value.
        /// </exception>
        ILuaValue ILuaValueVisitor.Arithmetic(BinaryOperationType type, LuaValues.LuaThread self)
        {
            var ret = LuaValueBase.AttempMetamethod(type, self, this);

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

            throw new InvalidOperationException(Errors.CannotArithmetic(LuaValueType.Thread));
        }
示例#3
0
        protected override ILuaMultiValue _invokeInternal(ILuaValue self, bool methodCall, int overload,
                                                          ILuaMultiValue args)
        {
            MethodInfo method;
            object     target;

            if (overload < 0)
            {
                if (!Helpers.GetCompatibleMethod(_methods, args, out method, out target))
                {
                    throw new ArgumentException(
                              $"No overload of method '{Name}' could be found with specified parameters.");
                }
            }
            else
            {
                Tuple <MethodInfo, object> temp;
                if (!_methods.TryGetIndex(overload, out temp))
                {
                    throw new InvalidOperationException(
                              $"There is not an overload for '{Name}' with the index of '{overload}'.");
                }

                if (!Helpers.GetCompatibleMethod(new[] { temp }, args, out method, out target))
                {
                    throw new ArgumentException(
                              $"No overload of method '{Name}' could be found with specified parameters.");
                }
            }

            // Invoke the selected method
            object retObj;

            object[] r_args = Helpers.ConvertForArgs(args, method);
            retObj = Helpers.DynamicInvoke(method, target, r_args);

            // Restore by-reference variables.
            var min = Math.Min(method.GetParameters().Length, args.Count);

            for (int i = 0; i < min; i++)
            {
                args[i] = LuaValueBase.CreateValue(r_args[i]);
            }

            if (retObj is ILuaMultiValue ret)
            {
                return(ret);
            }

            // Convert the return type and return
            Type returnType = method.ReturnType;

            if (method.GetCustomAttributes(typeof(MultipleReturnAttribute), true).Length > 0)
            {
                if (typeof(IEnumerable).IsAssignableFrom(returnType))
                {
                    // TODO: Support restricted variables.
                    IEnumerable tempE = (IEnumerable)retObj;
                    return(LuaMultiValue.CreateMultiValueFromObj(tempE.Cast <object>().ToArray()));
                }
                else
                {
                    throw new InvalidOperationException(
                              "Methods marked with MultipleReturnAttribute must return a type compatible with " +
                              "IEnumerable.");
                }
            }
            else
            {
                return(LuaMultiValue.CreateMultiValueFromObj(retObj));
            }
        }