Ejemplo n.º 1
0
        public override ILuaMultiValue Invoke(ILuaValue self, bool memberCall, int overload, ILuaMultiValue args)
        {
            if (overload >= 0)
            {
                throw new NotSupportedException(string.Format(Resources.CannotUseOverload, "LuaType"));
            }

            if (args == null)
            {
                args = new LuaMultiValue();
            }

            Type t = Type;

            if (Helpers.GetCompatibleMethod(
                    t.GetConstructors()
                    .Where(c => c.GetCustomAttributes(typeof(LuaIgnoreAttribute), true).Length == 0)
                    .Select(c => Tuple.Create(c, (object)null)),
                    args, out ConstructorInfo method, out _))
            {
                object value = method.Invoke(Helpers.ConvertForArgs(args, method));
                return(LuaMultiValue.CreateMultiValueFromObj(value));
            }

            throw new InvalidOperationException(string.Format(Resources.CannotCall, "LuaType"));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Provides the implementation for operations that invoke an object. Classes
        ///     derived from the System.Dynamic.DynamicObject class can override this method
        ///     to specify dynamic behavior for operations such as invoking an object or
        ///     a delegate.
        /// </summary>
        /// <param name="binder">Provides information about the invoke operation.</param>
        /// <param name="args">The arguments that are passed to the object during the invoke operation.
        ///     For example, for the sampleObject(100) operation, where sampleObject is derived
        ///     from the System.Dynamic.DynamicObject class, args[0] is equal to 100.</param>
        /// <param name="result">The result of the object invocation.</param>
        /// <returns>true if the operation is successful; otherwise, false. If this method returns
        ///     false, the run-time binder of the language determines the behavior. (In most
        ///     cases, a language-specific run-time exception is thrown.</returns>
        public override bool TryInvoke(InvokeBinder binder, object[] args, out object result)
        {
            var ret = InvokeInternal(LuaNil.Nil, false, -1,
                                     LuaMultiValue.CreateMultiValueFromObj(args));

            result = ret.GetValue();
            return(true);
        }
Ejemplo n.º 3
0
    protected override ILuaMultiValue _invokeInternal(ILuaValue target, bool methodCall,
                                                     int overload, ILuaMultiValue args) {
      if (methodCall) {
        args = new LuaMultiValue(new[] { target }.Concat(args).ToArray());
      }

      return _invokeInternal(args);
    }
Ejemplo n.º 4
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));
            }
        }