Ejemplo n.º 1
0
        /// <summary>Native method for Grace ||</summary>
        /// <param name="ctx">Current interpreter</param>
        /// <param name="other">Argument to the method</param>
        public GraceObject OrOr(EvaluationContext ctx, GraceObject other)
        {
            GraceBoolean oth = other as GraceBoolean;

            if (oth != null)
            {
                return(GraceBoolean.Create(this.Boolean || oth.Boolean));
            }
            GraceObjectProxy op = other as GraceObjectProxy;

            if (op != null)
            {
                return(GraceBoolean.Create(this.Boolean || (dynamic)op.Object));
            }
            ErrorReporting.RaiseError(ctx, "R2001",
                                      new Dictionary <string, string>()
            {
                { "method", "||" },
                { "index", "1" },
                { "part", "||" },
                { "required", "Boolean" }
            },
                                      "ArgumentTypeError: || requires a Boolean argument"
                                      );
            return(GraceBoolean.False);
        }
Ejemplo n.º 2
0
        /// <inheritsdoc/>
        /// <remarks>Uses reflection to access the method, or the
        /// dynamic type to access operators.</remarks>
        public override GraceObject Request(EvaluationContext ctx, MethodRequest req)
        {
            string name = req.Name;

            switch (name)
            {
            case "isNull":
                if (obj == null)
                {
                    return(GraceBoolean.True);
                }
                return(GraceBoolean.False);

            case "+(_)":
                return(GraceObjectProxy.Create((dynamic)obj + (dynamic)viewAsNative(req[0].Arguments[0])));

            case "-(_)":
                return(GraceObjectProxy.Create((dynamic)obj - (dynamic)viewAsNative(req[0].Arguments[0])));

            case "*(_)":
                return(GraceObjectProxy.Create((dynamic)obj * (dynamic)viewAsNative(req[0].Arguments[0])));

            case "/(_)":
                return(GraceObjectProxy.Create((dynamic)obj / (dynamic)viewAsNative(req[0].Arguments[0])));

            case "<(_)":
                return(GraceObjectProxy.Create((dynamic)obj < (dynamic)viewAsNative(req[0].Arguments[0])));

            case "<=(_)":
                return(GraceObjectProxy.Create((dynamic)obj <= (dynamic)viewAsNative(req[0].Arguments[0])));

            case ">(_)":
                return(GraceObjectProxy.Create((dynamic)obj > (dynamic)viewAsNative(req[0].Arguments[0])));

            case ">=(_)":
                return(GraceObjectProxy.Create((dynamic)obj >= (dynamic)viewAsNative(req[0].Arguments[0])));

            case "==(_)":
                return(GraceObjectProxy.Create((dynamic)obj == (dynamic)viewAsNative(req[0].Arguments[0])));

            case "!=(_)":
                return(GraceObjectProxy.Create((dynamic)obj != (dynamic)viewAsNative(req[0].Arguments[0])));

            case "%(_)":
                return(GraceObjectProxy.Create((dynamic)obj % (dynamic)viewAsNative(req[0].Arguments[0])));

            case "^(_)":
                return(GraceObjectProxy.Create(Math.Pow((dynamic)obj, (dynamic)viewAsNative(req[0].Arguments[0]))));

            case "asString":
                if (obj == null)
                {
                    return(GraceString.Create("(null)"));
                }
                return(GraceString.Create(obj.ToString()));

            case "prefix!":
                return(GraceObjectProxy.Create(!(dynamic)obj));

            case "at(_)":
                if (Interpreter.JSIL)
                {
                    // Calling get_Item directly is iffy, but
                    // works on JSIL where accessing Item fails,
                    // and [] uses native (the wrong) [].
                    return(GraceObjectProxy.Create(
                               ((dynamic)obj)
                               .get_Item(
                                   (dynamic)viewAsNative(req[0].Arguments[0]))
                               ));
                }
                return(GraceObjectProxy.Create(
                           ((dynamic)obj)[
                               (dynamic)viewAsNative(req[0].Arguments[0])]));
            }
            object[] args = new object[req[0].Arguments.Count];
            for (int i = 0; i < req[0].Arguments.Count; i++)
            {
                args[i] = viewAsNative(req[0].Arguments[i]);
            }
            Type[] types = new Type[args.Length];
            for (int i = 0; i < types.Length; i++)
            {
                types[i] = args[i].GetType();
            }
            MethodInfo meth = type.GetMethod(name, types);

            if (meth == null)
            {
                ErrorReporting.RaiseError(ctx, "R2000",
                                          new Dictionary <string, string>()
                {
                    { "method", req.Name },
                    { "receiver", "Native Proxy" }
                },
                                          "LookupError: Native proxy failed to find method «${method}»"
                                          );
            }
            return(GraceObjectProxy.Create(meth.Invoke(obj, args)));
        }