Beispiel #1
0
//#if !CLR2
        public override bool TryInvoke(InvokeBinder binder, object[] args, out object result)
        {
            var boxedArgs = args.Select(x => BoxedValue.Box(x)).ToArray();

            result = Call(Env.Globals, boxedArgs).UnboxObject();
            return(true);
        }
Beispiel #2
0
        /// <summary>
        /// Implements the binary `==` operator.
        /// </summary>
        public static bool eq(BoxedValue l, BoxedValue r)
        {
            if (Operators.same(l, r))
            {
                return(true);
            }

            if (l.IsNull && r.IsUndefined ||
                l.IsUndefined && r.IsNull)
            {
                return(true);
            }

            if (l.IsNumber && r.IsString)
            {
                return(l.Number == TypeConverter.ToNumber(r));
            }

            if (l.IsString && r.IsNumber)
            {
                return(TypeConverter.ToNumber(l) == r.Number);
            }

            if (l.Tag == TypeTags.Bool)
            {
                var newL = BoxedValue.Box(TypeConverter.ToNumber(l));
                return(Operators.eq(newL, r));
            }

            if (r.Tag == TypeTags.Bool)
            {
                var newR = BoxedValue.Box(TypeConverter.ToNumber(r));
                return(Operators.eq(l, newR));
            }

            if (l.Tag >= TypeTags.Object)
            {
                if (r.Tag == TypeTags.SuffixString || r.Tag == TypeTags.String || r.IsNumber)
                {
                    var newL = TypeConverter.ToPrimitive(l.Object, DefaultValueHint.None);
                    return(Operators.eq(newL, r));
                }

                return(false);
            }

            if (r.Tag >= TypeTags.Object)
            {
                if (l.Tag == TypeTags.SuffixString || l.Tag == TypeTags.String || l.IsNumber)
                {
                    var newR = TypeConverter.ToPrimitive(r.Object, DefaultValueHint.None);
                    return(Operators.eq(l, newR));
                }

                return(false);
            }

            return(false);
        }
Beispiel #3
0
        public static BoxedValue ToPrimitive(object c, DefaultValueHint hint)
        {
            if (c == null)
            {
                return(BoxedValue.Box(default(object)));
            }

            return(BoxedValue.Box(c.ToString()));
        }
Beispiel #4
0
        public T RaiseError <T>(CommonObject prototype, string message)
        {
            ErrorObject error = new ErrorObject(this)
            {
                Prototype = prototype
            };

            error.Put("message", message);
            throw new UserError(BoxedValue.Box((CommonObject)error), 0, 0);
        }
Beispiel #5
0
        public BoxedValue PickReturnObject(BoxedValue r, CommonObject o)
        {
            switch (r.Tag)
            {
            case TypeTags.Function: return(BoxedValue.Box(r.Func));

            case TypeTags.Object: return(BoxedValue.Box(r.Object));

            default: return(BoxedValue.Box(o));
            }
        }
Beispiel #6
0
        public override BoxedValue Get(uint i)
        {
            var a = (int)i;
            var s = this.Value.Value.String;

            if (this.Value.HasValue && a < s.Length)
            {
                return(BoxedValue.Box(s[a].ToString()));
            }

            return(Undefined.Boxed);
        }
Beispiel #7
0
        public override BoxedValue Get(string name)
        {
            uint index;

            if (uint.TryParse(name, out index))
            {
                return(this.Get(index));
            }

            if (string.Equals(name, "length"))
            {
                return(BoxedValue.Box(this.length));
            }

            return(base.Get(name));
        }
Beispiel #8
0
        /// <summary>
        /// Implements the binary `+` operator.
        /// </summary>
        public static BoxedValue add(BoxedValue l, BoxedValue r)
        {
            if (l.Tag == TypeTags.SuffixString)
            {
                var newString = SuffixString.Concat(
                    l.SuffixString,
                    TypeConverter.ToString(TypeConverter.ToPrimitive(r)));

                return(BoxedValue.Box(newString));
            }

            if (l.Tag == TypeTags.String ||
                r.Tag == TypeTags.String ||
                r.Tag == TypeTags.SuffixString)
            {
                var newString = SuffixString.Concat(
                    TypeConverter.ToString(TypeConverter.ToPrimitive(l)),
                    TypeConverter.ToString(TypeConverter.ToPrimitive(r)));

                return(BoxedValue.Box(newString));
            }

            var lPrim = TypeConverter.ToPrimitive(l);
            var rPrim = TypeConverter.ToPrimitive(r);

            if (lPrim.Tag == TypeTags.SuffixString)
            {
                var newString = SuffixString.Concat(
                    lPrim.SuffixString,
                    TypeConverter.ToString(rPrim));

                return(BoxedValue.Box(newString));
            }

            if (lPrim.Tag == TypeTags.String ||
                rPrim.Tag == TypeTags.String ||
                rPrim.Tag == TypeTags.SuffixString)
            {
                var newString = SuffixString.Concat(
                    TypeConverter.ToString(lPrim),
                    TypeConverter.ToString(rPrim));

                return(BoxedValue.Box(newString));
            }

            return(BoxedValue.Box(TypeConverter.ToNumber(lPrim) + TypeConverter.ToNumber(rPrim)));
        }
Beispiel #9
0
        public static BoxedValue ToPrimitive(BoxedValue v, DefaultValueHint hint)
        {
            switch (v.Tag)
            {
            case TypeTags.Clr:
                return(ToPrimitive(v.Clr, hint));

            case TypeTags.Object:
            case TypeTags.Function:
                return(v.Object.DefaultValue(hint));

            case TypeTags.SuffixString:
                return(BoxedValue.Box(v.Clr.ToString()));

            default:
                return(v);
            }
        }
Beispiel #10
0
        public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result)
        {
            var item = this.Find(binder.Name);

            if (item.HasValue)
            {
                var box = item.Value;
                if (box.IsFunction)
                {
                    var func      = box.Func;
                    var boxedArgs = args.Select(a => BoxedValue.Box(a)).ToArray();
                    var ret       = func.Call(this, boxedArgs);
                    result = ret.UnboxObject();
                    return(true);
                }
            }

            result = null;
            return(false);
        }
Beispiel #11
0
        public override void Put(string name, object value, uint tag)
        {
            var boxed = BoxedValue.Box(value, tag);

            if (name == "length")
            {
                this.PutLength(TypeConverter.ToNumber(boxed));
                this.SetAttrs("length", DescriptorAttrs.DontEnum); //TODO: Shouldn't `PutLength` keep the `DontEnum` flag?
                return;
            }

            uint index;

            if (TypeConverter.TryToIndex(name, out index))  //TODO: I changed this to use TryToIndex, but that forgoes checking that `index.ToString() == name`, which may be necessary.
            {
                this.Put(index, boxed);
                return;
            }

            base.Put(name, boxed);
        }
Beispiel #12
0
        public static BoxedValue JsBox(object o)
        {
            if (o is BoxedValue)
            {
                return((BoxedValue)o);
            }

            if (o == null)
            {
                return(Environment.BoxedNull);
            }

            var tag = TypeTag.OfType(o.GetType());

            switch (tag)
            {
            case TypeTags.Bool: return(BoxedValue.Box((bool)o));

            case TypeTags.Number: return(BoxedValue.Box((double)o));

            default: return(BoxedValue.Box(o, tag));
            }
        }
Beispiel #13
0
 public override void Put(uint index, object value, uint tag)
 {
     this.Put(index, BoxedValue.Box(value, tag));
 }
Beispiel #14
0
 static Undefined()
 {
     instance = new Undefined();
     boxed    = BoxedValue.Box(instance, TypeTags.Undefined);
 }
Beispiel #15
0
 public override void Put(uint index, double value)
 {
     this.Put(index, BoxedValue.Box(value));
 }
Beispiel #16
0
 public static BoxedValue ToBoxedValue(object c)
 {
     return(BoxedValue.Box(c));
 }
Beispiel #17
0
 public static BoxedValue ToBoxedValue(double d)
 {
     return(BoxedValue.Box(d));
 }
Beispiel #18
0
 public static BoxedValue ToBoxedValue(CommonObject o)
 {
     return(BoxedValue.Box(o));
 }
Beispiel #19
0
 public static BoxedValue ToBoxedValue(FunctionObject f)
 {
     return(BoxedValue.Box(f));
 }
Beispiel #20
0
 /// <summary>
 /// Implements the unary `-` operator.
 /// </summary>
 public static BoxedValue minus(BoxedValue o)
 {
     return(BoxedValue.Box((double)(TypeConverter.ToNumber(o) * -1.0)));
 }
Beispiel #21
0
 public static BoxedValue ToBoxedValue(bool b)
 {
     return(BoxedValue.Box(b));
 }
Beispiel #22
0
 public static BoxedValue ToPrimitive(bool b, DefaultValueHint hint)
 {
     return(BoxedValue.Box(b));
 }
Beispiel #23
0
 /// <summary>
 /// Implements the unary `+` operator.
 /// </summary>
 public static BoxedValue plus(BoxedValue value)
 {
     return(BoxedValue.Box(TypeConverter.ToNumber(value)));
 }
Beispiel #24
0
 public static BoxedValue ToPrimitive(double d, DefaultValueHint hint)
 {
     return(BoxedValue.Box(d));
 }
Beispiel #25
0
 public static BoxedValue ToPrimitive(string s, DefaultValueHint hint)
 {
     return(BoxedValue.Box(s));
 }
Beispiel #26
0
 public static BoxedValue ToBoxedValue(SuffixString s)
 {
     return(BoxedValue.Box(s));
 }