public JsValue Call(JsValue thisObject, JsValue[] arguments)
        {
            // direct calls on a NamespaceReference constructor object is creating a generic type 
            var genericTypes = new Type[arguments.Length];
            for (int i = 0; i < arguments.Length; i++)
            {
                var genericTypeReference = arguments.At(i);
                if (genericTypeReference == Undefined.Instance || !genericTypeReference.IsObject() || genericTypeReference.AsObject().Class != "TypeReference")
                {
                    throw new JavaScriptException(Engine.TypeError, "Invalid generic type parameter");
                }

                genericTypes[i] = arguments.At(0).As<TypeReference>().Type;
            }

            var typeReference = GetPath(_path + "`" + arguments.Length.ToString(CultureInfo.InvariantCulture)).As<TypeReference>();

            if (typeReference == null)
            {
                return Undefined.Instance;
            }

            var genericType = typeReference.Type.MakeGenericType(genericTypes);

            return TypeReference.CreateTypeReference(Engine, genericType);
        }
Esempio n. 2
0
        public override JsValue Call(JsValue thisObject, JsValue[] arguments)
        {
            var pattern = arguments.At(0);
            var flags = arguments.At(1);

            if (pattern != Undefined.Instance && flags == Undefined.Instance && TypeConverter.ToObject(Engine, pattern).Class == "Regex")
            {
                return pattern;
            }

            return Construct(arguments);
        }
Esempio n. 3
0
        public ObjectInstance Construct(JsValue[] arguments)
        {
            var instance = new ErrorInstance(Engine, _name);
            instance.Prototype = PrototypeObject;
            instance.Extensible = true;

            if (arguments.At(0) != Undefined.Instance)
            {
                instance.Put("message", TypeConverter.ToString(arguments.At(0)), false);
            }

            return instance;
        }
Esempio n. 4
0
        private JsValue Bind(JsValue thisObj, JsValue[] arguments)
        {
            var target = thisObj.TryCast<ICallable>(x =>
            {
                throw new JavaScriptException(Engine.TypeError);
            });
            
            var thisArg = arguments.At(0);
            var f = new BindFunctionInstance(Engine) {Extensible = true};
            f.TargetFunction = thisObj;
            f.BoundThis = thisArg;
            f.BoundArgs = arguments.Skip(1).ToArray();
            f.Prototype = Engine.Function.PrototypeObject;

            var o = target as FunctionInstance;
            if (o != null)
            {
                var l = TypeConverter.ToNumber(o.Get("length")) - (arguments.Length - 1);
                f.FastAddProperty("length", System.Math.Max(l, 0), false, false, false); 
            }
            else
            {
                f.FastAddProperty("length", 0, false, false, false); 
            }
            

            var thrower = Engine.Function.ThrowTypeError;
            f.DefineOwnProperty("caller", new PropertyDescriptor(thrower, thrower, false, false), false);
            f.DefineOwnProperty("arguments", new PropertyDescriptor(thrower, thrower, false, false), false);


            return f;
        }
Esempio n. 5
0
        private JsValue Parse(JsValue thisObj, JsValue[] arguments)
        {
            DateTime result;
            var date = TypeConverter.ToString(arguments.At(0));

            if (!DateTime.TryParseExact(date, new[]
            {
                "yyyy-MM-ddTHH:mm:ss.FFF",
                "yyyy-MM-ddTHH:mm:ss",
                "yyyy-MM-ddTHH:mm",
                "yyyy-MM-dd",
                "yyyy-MM",
                "yyyy"
            }, CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal | DateTimeStyles.AssumeUniversal, out result))
            {
                if (!DateTime.TryParseExact(date, new[]
                {
                    // Formats used in DatePrototype toString methods
                    "ddd MMM dd yyyy HH:mm:ss 'GMT'K",
                    "ddd MMM dd yyyy",
                    "HH:mm:ss 'GMT'K",

                    // standard formats
                    "yyyy-M-dTH:m:s.FFFK",
                    "yyyy/M/dTH:m:s.FFFK",
                    "yyyy-M-dTH:m:sK",
                    "yyyy/M/dTH:m:sK",
                    "yyyy-M-dTH:mK",
                    "yyyy/M/dTH:mK",
                    "yyyy-M-d H:m:s.FFFK",
                    "yyyy/M/d H:m:s.FFFK",
                    "yyyy-M-d H:m:sK",
                    "yyyy/M/d H:m:sK",
                    "yyyy-M-d H:mK",
                    "yyyy/M/d H:mK",
                    "yyyy-M-dK",
                    "yyyy/M/dK",
                    "yyyy-MK",
                    "yyyy/MK",
                    "yyyyK",
                    "THH:mm:ss.FFFK",
                    "THH:mm:ssK",
                    "THH:mmK",
                    "THHK"
                }, CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal, out result))
                {
                    if (!DateTime.TryParse(date, Engine.Options.GetCulture(), DateTimeStyles.AdjustToUniversal, out result))
                    {
                        if (!DateTime.TryParse(date, CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal, out result))
                        {
                            // unrecognized dates should return NaN (15.9.4.2)
                            return double.NaN;
                        }
                    }
                }
            }

            return FromDateTime(result);
        }
Esempio n. 6
0
        /// <summary>
        /// http://www.ecma-international.org/ecma-262/5.1/#sec-15.1.2.2
        /// </summary>
        public static JsValue ParseInt(JsValue thisObject, JsValue[] arguments)
        {
            string inputString = TypeConverter.ToString(arguments.At(0));
            var s = inputString.Trim();

            var sign = 1;
            if (!System.String.IsNullOrEmpty(s))
            {
                if (s[0] == '-')
                {
                    sign = -1;
                }
                
                if (s[0] == '-' || s[0] == '+')
                {
                    s = s.Substring(1);
                }
            }

            var stripPrefix = true;

            int radix = arguments.Length > 1 ? TypeConverter.ToInt32(arguments[1]) : 0;

            if (radix == 0)
            {
                if (s.Length >= 2 && s.StartsWith("0x") || s.StartsWith("0X"))
                {
                    radix = 16;
                }
                else
                {
                    radix = 10;
                }
            }
            else if (radix < 2 || radix > 36)
            {
                return double.NaN;
            }
            else if(radix != 16)
            {
                stripPrefix = false;
            }

            if (stripPrefix && s.Length >= 2 && s.StartsWith("0x") || s.StartsWith("0X"))
            {
                s = s.Substring(2);
            }

            try
            {
                return sign * Parse(s, radix).AsNumber();
            }
            catch
            {
                return double.NaN;
            }

        }
Esempio n. 7
0
        private JsValue IsArray(JsValue thisObj, JsValue[] arguments)
        {
            if (arguments.Length == 0)
            {
                return false;
            }

            var o = arguments.At(0);

            return o.IsObject() && o.AsObject().Class == "Array";
        }
Esempio n. 8
0
        public ObjectInstance Construct(JsValue[] arguments)
        {
            var instance = new ArrayInstance(Engine);
            instance.Prototype = PrototypeObject;
            instance.Extensible = true;

            if (arguments.Length == 1 && arguments.At(0).IsNumber())
            {
                var length = TypeConverter.ToUint32(arguments.At(0));
                if (!TypeConverter.ToNumber(arguments[0]).Equals(length))
                {
                    throw new JavaScriptException(Engine.RangeError, "Invalid array length");
                }

                instance.FastAddProperty("length", length, true, false, false);
            }
            else if (arguments.Length == 1 && arguments.At(0).IsObject() && arguments.At(0).As<ObjectWrapper>() != null)
            {
                var enumerable = arguments.At(0).As<ObjectWrapper>().Target as IEnumerable;

                if (enumerable != null)
                {
                    var jsArray = Engine.Array.Construct(Arguments.Empty);
                    foreach (var item in enumerable)
                    {
                        var jsItem = JsValue.FromObject(Engine, item);
                        Engine.Array.PrototypeObject.Push(jsArray, Arguments.From(jsItem));
                    }

                    return jsArray;
                }
            }
            else
            {
                instance.FastAddProperty("length", 0, true, false, false);
                PrototypeObject.Push(instance, arguments);
            }

            return instance;
        }
Esempio n. 9
0
        private JsValue Parse(JsValue thisObj, JsValue[] arguments)
        {
            DateTime result;
            var date = TypeConverter.ToString(arguments.At(0));

            if (!DateTime.TryParseExact(date, new[]
            {
                "yyyy-MM-ddTHH:mm:ss.FFF",
                "yyyy-MM-ddTHH:mm:ss",
                "yyyy-MM-ddTHH:mm",
                "yyyy-MM-dd",
                "yyyy-MM",
                "yyyy"
            }, CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal | DateTimeStyles.AssumeUniversal, out result))
            {
                if (!DateTime.TryParseExact(date, new[]
                {
                    "yyyy-M-dTH:m:s.FFFK",
                    "yyyy/M/dTH:m:s.FFFK",
                    "yyyy-M-dTH:m:sK",
                    "yyyy/M/dTH:m:sK",
                    "yyyy-M-dTH:mK",
                    "yyyy/M/dTH:mK",
                    "yyyy-M-d H:m:s.FFFK",
                    "yyyy/M/d H:m:s.FFFK",
                    "yyyy-M-d H:m:sK",
                    "yyyy/M/d H:m:sK",
                    "yyyy-M-d H:mK",
                    "yyyy/M/d H:mK",
                    "yyyy-M-dK",
                    "yyyy/M/dK",
                    "yyyy-MK",
                    "yyyy/MK",
                    "yyyyK",
                    "THH:mm:ss.FFFK",
                    "THH:mm:ssK",
                    "THH:mmK",
                    "THHK"
                }, CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal, out result))
                {
                    if (!DateTime.TryParse(date, CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal,out result))
                    {
                        // unrecognized dates should return NaN (15.9.4.2)
                        return double.NaN;
                    }
                }
            }

            return FromDateTime(result);
        }
Esempio n. 10
0
        private JsValue Parse(JsValue thisObj, JsValue[] arguments)
        {
            DateTime result;
            var date = TypeConverter.ToString(arguments.At(0));

            if (!DateTime.TryParseExact(date, new[]
            {
                "yyyy-MM-ddTHH:mm:ss.FFF",
                "yyyy-MM-ddTHH:mm:ss",
                "yyyy-MM-ddTHH:mm",
                "yyyy-MM-dd",
                "yyyy-MM",
                "yyyy"
            }, CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal, out result))
            {
                if (!DateTime.TryParseExact(date, new[]
                {
                    "yyyy-M-dTH:m:s.FFFK",
                    "yyyy/M/dTH:m:s.FFFK",
                    "yyyy-M-dTH:m:sK",
                    "yyyy/M/dTH:m:sK",
                    "yyyy-M-dTH:mK",
                    "yyyy/M/dTH:mK",
                    "yyyy-M-d H:m:s.FFFK",
                    "yyyy/M/d H:m:s.FFFK",
                    "yyyy-M-d H:m:sK",
                    "yyyy/M/d H:m:sK",
                    "yyyy-M-d H:mK",
                    "yyyy/M/d H:mK",
                    "yyyy-M-dK",
                    "yyyy/M/dK",
                    "yyyy-MK",
                    "yyyy/MK",
                    "yyyyK",
                    "THH:mm:ss.FFFK",
                    "THH:mm:ssK",
                    "THH:mmK",
                    "THHK"
                }, CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal, out result))
                {
                    if (!DateTime.TryParse(date, CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal,out result))
                    {
                        throw new JavaScriptException(Engine.SyntaxError, "Invalid date");
                    }
                }
            }

            return FromDateTime(result);
        }
Esempio n. 11
0
        private JsValue LastIndexOf(JsValue thisObj, JsValue[] arguments)
        {
            var o = TypeConverter.ToObject(Engine, thisObj);
            var lenValue = o.Get("length");
            var len = TypeConverter.ToUint32(lenValue);
            if (len == 0)
            {
                return -1;
            }

            var n = arguments.Length > 1 ? TypeConverter.ToInteger(arguments[1]) : len - 1;
            double k;
            if (n >= 0)
            {
                k = System.Math.Min(n, len - 1); // min
            }
            else
            {
                k = len - System.Math.Abs(n);
            }
            var searchElement = arguments.At(0);
            for (; k >= 0; k--)
            {
                var kString = TypeConverter.ToString(k);
                var kPresent = o.HasProperty(kString);
                if (kPresent)
                {
                    var elementK = o.Get(kString);
                    var same = ExpressionInterpreter.StrictlyEqual(elementK, searchElement);
                    if (same)
                    {
                        return k;
                    }
                }
            }
            return -1;
        }
Esempio n. 12
0
        private JsValue Splice(JsValue thisObj, JsValue[] arguments)
        {
            var start = arguments.At(0);
            var deleteCount = arguments.At(1);

            var o = TypeConverter.ToObject(Engine, thisObj);
            var a = Engine.Array.Construct(Arguments.Empty);
            var lenVal = o.Get("length");
            var len = TypeConverter.ToUint32(lenVal);
            var relativeStart = TypeConverter.ToInteger(start);

            uint actualStart;
            if (relativeStart < 0)
            {
                actualStart = (uint)System.Math.Max(len + relativeStart, 0);
            }
            else
            {
                actualStart = (uint)System.Math.Min(relativeStart, len);
            }

            var actualDeleteCount = System.Math.Min(System.Math.Max(TypeConverter.ToInteger(deleteCount), 0), len - actualStart);
            for (var k = 0; k < actualDeleteCount; k++)
            {
                var from = (actualStart + k).ToString();
                var fromPresent = o.HasProperty(from);
                if (fromPresent)
                {
                    var fromValue = o.Get(from); 
                    a.DefineOwnProperty(k.ToString(), new PropertyDescriptor(fromValue, true, true, true), false);
                }
            }
            
            var items = arguments.Skip(2).ToArray();
            if (items.Length < actualDeleteCount)
            {
                for (var k = actualStart; k < len - actualDeleteCount; k++)
                {
                    var from = (k + actualDeleteCount).ToString();
                    var to = (k + items.Length).ToString();
                    var fromPresent = o.HasProperty(from);
                    if (fromPresent)
                    {
                        var fromValue = o.Get(from);
                        o.Put(to, fromValue, true);
                    }
                    else
                    {
                        o.Delete(to, true);
                    }
                }
                for (var k = len; k > len - actualDeleteCount + items.Length; k-- )
                {
                    o.Delete((k - 1).ToString(), true);
                }
            }
            else if (items.Length > actualDeleteCount)
            {
                for (var k = len - actualDeleteCount; k > actualStart; k--)
                {
                    var from = (k + actualDeleteCount - 1).ToString();
                    var to = (k + items.Length - 1).ToString();
                    var fromPresent = o.HasProperty(from);
                    if (fromPresent)
                    {
                        var fromValue = o.Get(from);
                        o.Put(to, fromValue, true);
                    }
                    else
                    {
                        o.Delete(to, true);
                    }
                }
            }

            for(var k = 0; k< items.Length; k++)
            {
                var e = items[k];
                o.Put((k+actualStart).ToString(), e, true);
            }

            o.Put("length", len - actualDeleteCount + items.Length, true);
            return a;
        }
Esempio n. 13
0
        private JsValue Sort(JsValue thisObj, JsValue[] arguments)
        {
            if (!thisObj.IsObject())
            {
                throw new JavaScriptException(Engine.TypeError, "Array.prorotype.sort can only be applied on objects");  
            }

            var obj = thisObj.AsObject();

            var len = obj.Get("length");
            var lenVal = TypeConverter.ToInt32(len);
            if (lenVal <= 1)
            {
                return obj;
            }

            var compareArg = arguments.At(0);
            ICallable compareFn = null;
            if (compareArg != Undefined.Instance)
            {
                compareFn = compareArg.TryCast<ICallable>(x =>
                {
                    throw new JavaScriptException(Engine.TypeError, "The sort argument must be a function");
                });
            }

            Comparison<JsValue> comparer = (x, y) =>
                {
                    if (x == Undefined.Instance && y == Undefined.Instance)
                    {
                        return 0;
                    }

                    if (x == Undefined.Instance)
                    {
                        return 1;
                    }

                    if (y == Undefined.Instance)
                    {
                        return -1;
                    }

                    if (compareFn != null)
                    {
                        var s = (int) TypeConverter.ToUint32(compareFn.Call(Undefined.Instance, new[] {x, y}));
                        return s;
                    }

                    var xString = TypeConverter.ToString(x);
                    var yString = TypeConverter.ToString(y);

                    var r = System.String.CompareOrdinal(xString, yString);
                    return r;
                };

            var array = Enumerable.Range(0, lenVal).Select(i => obj.Get(i.ToString())).ToArray();
            
            // don't eat inner exceptions
            try
            {
                System.Array.Sort(array, comparer);
            }
            catch (InvalidOperationException e)
            {
                throw e.InnerException;
            }

            foreach (var i in Enumerable.Range(0, lenVal))
            {
                obj.Put(i.ToString(), array[i], false);
            }

            return obj;
        }
Esempio n. 14
0
        private JsValue Map(JsValue thisObj, JsValue[] arguments)
        {
            var callbackfn = arguments.At(0);
            var thisArg = arguments.At(1);

            var o = TypeConverter.ToObject(Engine, thisObj);
            var lenValue = o.Get("length");
            var len = TypeConverter.ToUint32(lenValue);

            var callable = callbackfn.TryCast<ICallable>(x =>
            {
                throw new JavaScriptException(Engine.TypeError, "Argument must be callable");
            });

            var a = Engine.Array.Construct(new JsValue[] {len});

            for (var k = 0; k < len; k++)
            {
                var pk = k.ToString();
                var kpresent = o.HasProperty(pk);
                if (kpresent)
                {
                    var kvalue = o.Get(pk);
                    var mappedValue = callable.Call(thisArg, new [] { kvalue, k, o });
                    a.DefineOwnProperty(pk, new PropertyDescriptor(mappedValue, true, true, true), false);
                }
            }

            return a;
        }
Esempio n. 15
0
        private JsValue Every(JsValue thisObj, JsValue[] arguments)
        {
            var callbackfn = arguments.At(0);
            var thisArg = arguments.At(1);

            var o = TypeConverter.ToObject(Engine, thisObj);
            var lenValue = o.Get("length");
            var len = TypeConverter.ToUint32(lenValue);

            var callable = callbackfn.TryCast<ICallable>(x =>
            {
                throw new JavaScriptException(Engine.TypeError, "Argument must be callable");
            });

            for (var k = 0; k < len; k++)
            {
                var pk = k.ToString();
                var kpresent = o.HasProperty(pk);
                if (kpresent)
                {
                    var kvalue = o.Get(pk);
                    var testResult = callable.Call(thisArg, new [] { kvalue, k, o });
                    if (false == TypeConverter.ToBoolean(testResult))
                    {
                        return JsValue.False;
                    }
                }
            }

            return JsValue.True;
        }
Esempio n. 16
0
 private JsValue SetUTCFullYear(JsValue thisObj, JsValue[] arguments)
 {
     var thisTime = EnsureDateInstance(thisObj).PrimitiveValue;
     var t = double.IsNaN(thisTime) ? +0 : thisTime;
     var y = TypeConverter.ToNumber(arguments.At(0));
     var m = arguments.Length <= 1 ? MonthFromTime(t) : TypeConverter.ToNumber(arguments.At(1));
     var dt = arguments.Length <= 2 ? DateFromTime(t) : TypeConverter.ToNumber(arguments.At(2));
     var newDate = MakeDate(MakeDay(y, m, dt), TimeWithinDay(t));
     var u = TimeClip(newDate);
     thisObj.As<DateInstance>().PrimitiveValue = u;
     return u;
 }
Esempio n. 17
0
        private JsValue Filter(JsValue thisObj, JsValue[] arguments)
        {
            var callbackfn = arguments.At(0);
            var thisArg = arguments.At(1);

            var o = TypeConverter.ToObject(Engine, thisObj);
            var lenValue = o.Get("length");
            var len = TypeConverter.ToUint32(lenValue);

            var callable = callbackfn.TryCast<ICallable>(x =>
            {
                throw new JavaScriptException(Engine.TypeError,
                    "Argument must be callable");
            });

            var a = (ArrayInstance)Engine.Array.Construct(Arguments.Empty);

            var to = 0;
            for (var k = 0; k < len; k++)
            {
                var pk = k.ToString();
                var kpresent = o.HasProperty(pk);
                if (kpresent)
                {
                    var kvalue = o.Get(pk);
                    var selected = callable.Call(thisArg, new [] { kvalue, k, o });
                    if (TypeConverter.ToBoolean(selected))
                    {
                        a.DefineOwnProperty(to.ToString(), new PropertyDescriptor(kvalue, true, true, true), false);
                        to++;
                    }
                }
            }

            return a;
        }
Esempio n. 18
0
 private JsValue SetUTCMonth(JsValue thisObj, JsValue[] arguments)
 {
     var t = EnsureDateInstance(thisObj).PrimitiveValue;
     var m = TypeConverter.ToNumber(arguments.At(0));
     var dt = arguments.Length <= 1 ? DateFromTime(t) : TypeConverter.ToNumber(arguments.At(1));
     var newDate = MakeDate(MakeDay(YearFromTime(t), m, dt), TimeWithinDay(t));
     var u = TimeClip(newDate);
     thisObj.As<DateInstance>().PrimitiveValue = u;
     return u;
 }
        public JsValue Keys(JsValue thisObject, JsValue[] arguments)
        {
            var oArg = arguments.At(0);
            var o = oArg.TryCast<ObjectInstance>();
            if (o == null)
            {
                throw new JavaScriptException(Engine.TypeError);
            }

            var enumerableProperties = o.Properties
                .Where(x => x.Value.Enumerable.HasValue && x.Value.Enumerable.Value.AsBoolean())
                .ToArray();
            var n = enumerableProperties.Length;
            var array = Engine.Array.Construct(new JsValue[] {n});
            var index = 0;
            foreach (var prop in enumerableProperties)
            {
                var p = prop.Key;
                array.DefineOwnProperty(
                    TypeConverter.ToString(index), 
                    new PropertyDescriptor(p, true, true, true), 
                    false);
                index++;
            }
            return array;
        }
Esempio n. 20
0
 private JsValue SetUTCHours(JsValue thisObj, JsValue[] arguments)
 {
     var t = EnsureDateInstance(thisObj).PrimitiveValue;
     var h = TypeConverter.ToNumber(arguments.At(0));
     var m = arguments.Length <= 1 ? MinFromTime(t) : TypeConverter.ToNumber(arguments.At(1));
     var s = arguments.Length <= 2 ? SecFromTime(t) : TypeConverter.ToNumber(arguments.At(2));
     var milli = arguments.Length <= 3 ? MsFromTime(t) : TypeConverter.ToNumber(arguments.At(3));
     var date = MakeDate(Day(t), MakeTime(h, m, s, milli));
     var u = TimeClip(date);
     thisObj.As<DateInstance>().PrimitiveValue = u;
     return u;
 }
Esempio n. 21
0
 private JsValue SetDate(JsValue thisObj, JsValue[] arguments)
 {
     var t = LocalTime(EnsureDateInstance(thisObj).PrimitiveValue);
     var dt = TypeConverter.ToNumber(arguments.At(0));
     var newDate = MakeDate(MakeDay(YearFromTime(t), MonthFromTime(t), dt), TimeWithinDay(t));
     var u = TimeClip(Utc(newDate));
     thisObj.As<DateInstance>().PrimitiveValue = u;
     return u;
 }
        public JsValue IsFrozen(JsValue thisObject, JsValue[] arguments)
        {
            var oArg = arguments.At(0);
            var o = oArg.TryCast<ObjectInstance>();
            if (o == null)
            {
                throw new JavaScriptException(Engine.TypeError);
            }

            foreach (var p in o.Properties.Keys)
            {
                var desc = o.GetOwnProperty(p);
                if (desc.IsDataDescriptor())
                {
                    if (desc.Writable.HasValue && desc.Writable.Value.AsBoolean())
                    {
                        return false;
                    }
                }
                if (desc.Configurable.HasValue && desc.Configurable.Value.AsBoolean())
                {
                    return false;
                }
            }

            if (o.Extensible == false)
            {
                return true;
            }

            return false;
        }
Esempio n. 23
0
        private JsValue Join(JsValue thisObj, JsValue[] arguments)
        {
            var separator = arguments.At(0);
            var o = TypeConverter.ToObject(Engine, thisObj);
            var lenVal = o.Get("length");
            var len = TypeConverter.ToUint32(lenVal);
            if (separator == Undefined.Instance)
            {
                separator = ",";
            }
            var sep = TypeConverter.ToString(separator);
            
            // as per the spec, this has to be called after ToString(separator)
            if (len == 0)
            {
                return "";
            }

            var element0 = o.Get("0");
            string r = element0 == Undefined.Instance || element0 == Null.Instance
                                  ? ""
                                  : TypeConverter.ToString(element0);
            for (var k = 1; k < len; k++)
            {
                var s = r + sep;
                var element = o.Get(k.ToString());
                string next = element == Undefined.Instance || element == Null.Instance
                                  ? ""
                                  : TypeConverter.ToString(element);
                r = s + next;
            }
            return r;
        }
Esempio n. 24
0
 private JsValue SetTime(JsValue thisObj, JsValue[] arguments)
 {
     return EnsureDateInstance(thisObj).PrimitiveValue = TimeClip(TypeConverter.ToNumber(arguments.At(0)));
 }
Esempio n. 25
0
        private JsValue Slice(JsValue thisObj, JsValue[] arguments)
        {
            var start = arguments.At(0);
            var end = arguments.At(1);

            var o = TypeConverter.ToObject(Engine, thisObj);
            var a = Engine.Array.Construct(Arguments.Empty);
            var lenVal = o.Get("length");
            var len = TypeConverter.ToUint32(lenVal);

            var relativeStart = TypeConverter.ToInteger(start);
            uint k;
            if (relativeStart < 0)
            {
                k = (uint)System.Math.Max(len + relativeStart, 0);
            }
            else
            {
                k = (uint)System.Math.Min(TypeConverter.ToInteger(start), len);
            }

            uint final;
            if (end == Undefined.Instance)
            {
                final = TypeConverter.ToUint32(len);
            }
            else
            {
                double relativeEnd = TypeConverter.ToInteger(end);
                if (relativeEnd < 0)
                {
                    final = (uint)System.Math.Max(len + relativeEnd, 0);
                }
                else
                {
                    final = (uint)System.Math.Min(TypeConverter.ToInteger(relativeEnd), len);
                }
            }

            var n = 0;
            for (; k < final; k++)
            {
                var pk = TypeConverter.ToString(k);
                var kPresent = o.HasProperty(pk);
                if (kPresent)
                {
                    var kValue = o.Get(pk);
                    a.DefineOwnProperty(TypeConverter.ToString(n),
                                        new PropertyDescriptor(kValue, true, true, true), false);
                }
                n++;
            }

            return a;
        }
Esempio n. 26
0
        private JsValue SetYear(JsValue thisObj, JsValue[] arguments)
        {
            var thisTime = EnsureDateInstance(thisObj).PrimitiveValue;
            var t = double.IsNaN(thisTime) ? +0 : LocalTime(thisTime);
            var y = TypeConverter.ToNumber(arguments.At(0));
            if (double.IsNaN(y))
            {
                EnsureDateInstance(thisObj).PrimitiveValue = double.NaN;
                return double.NaN;
            }

            var fy = TypeConverter.ToInteger(y);
            if (y >= 0 && y <= 99)
            {
                fy = fy + 1900;
            }

            var newDate = MakeDay(fy, MonthFromTime(t), DateFromTime(t));
            var u = Utc(MakeDate(newDate, TimeWithinDay(t)));
            EnsureDateInstance(thisObj).PrimitiveValue = TimeClip(u);
            return u;
        }
Esempio n. 27
0
 private JsValue SetUTCMilliseconds(JsValue thisObj, JsValue[] arguments)
 {
     var t = EnsureDateInstance(thisObj).PrimitiveValue;
     var time = MakeTime(HourFromTime(t), MinFromTime(t), SecFromTime(t), TypeConverter.ToNumber(arguments.At(0)));
     var u = TimeClip(MakeDate(Day(t), time));
     thisObj.As<DateInstance>().PrimitiveValue = u;
     return u;
 }
        public JsValue IsExtensible(JsValue thisObject, JsValue[] arguments)
        {
            var oArg = arguments.At(0);
            var o = oArg.TryCast<ObjectInstance>();
            if (o == null)
            {
                throw new JavaScriptException(Engine.TypeError);
            }

            return o.Extensible;
        }
Esempio n. 29
0
        private JsValue Reduce(JsValue thisObj, JsValue[] arguments)
        {
            var callbackfn = arguments.At(0);
            var initialValue = arguments.At(1);

            var o = TypeConverter.ToObject(Engine, thisObj);
            var lenValue = o.Get("length");
            var len = TypeConverter.ToUint32(lenValue);

            var callable = callbackfn.TryCast<ICallable>(x =>
            {
                throw new JavaScriptException(Engine.TypeError, "Argument must be callable");
            });

            if (len == 0 && arguments.Length < 2)
            {
                throw new JavaScriptException(Engine.TypeError);
            }

            var k = 0;
            JsValue accumulator = Undefined.Instance;
            if (arguments.Length > 1)
            {
                accumulator = initialValue;
            }
            else
            {
                var kPresent = false;
                while (kPresent == false && k < len)
                {
                    var pk = k.ToString();
                    kPresent = o.HasProperty(pk);
                    if (kPresent)
                    {
                        accumulator = o.Get(pk);
                    }
                    k++;
                }
                if (kPresent == false)
                {
                    throw new JavaScriptException(Engine.TypeError);
                }
            }

            while(k < len)
            {
                var pk = k.ToString();
                var kPresent = o.HasProperty(pk);
                if (kPresent)
                {
                    var kvalue = o.Get(pk);
                    accumulator = callable.Call(Undefined.Instance, new [] { accumulator, kvalue, k, o });
                }
                k++;
            }

            return accumulator;
        }
Esempio n. 30
0
 private JsValue SetMinutes(JsValue thisObj, JsValue[] arguments)
 {
     var t = LocalTime(EnsureDateInstance(thisObj).PrimitiveValue);
     var m = TypeConverter.ToNumber(arguments.At(0));
     var s = arguments.Length <= 1 ? SecFromTime(t) : TypeConverter.ToNumber(arguments.At(1));
     var milli = arguments.Length <= 2 ? MsFromTime(t) : TypeConverter.ToNumber(arguments.At(2));
     var date = MakeDate(Day(t), MakeTime(HourFromTime(t), m, s, milli));
     var u = TimeClip(Utc(date));
     thisObj.As<DateInstance>().PrimitiveValue = u;
     return u;
 }