Ejemplo n.º 1
0
 public static void Deconstruct(this EcmaValue value, out EcmaValue item1, out EcmaValue item2, out EcmaValue rest)
 {
     EcmaValue[] arr = CreateListFromArrayLike(value);
     item1 = arr.ElementAtOrDefault(0);
     item2 = arr.ElementAtOrDefault(1);
     rest  = new EcmaArray(new List <EcmaValue>(arr.Skip(2)));
 }
Ejemplo n.º 2
0
        private void WritePropertyValue(TextWriter writer, Stack <RuntimeObject> stack, EcmaValue value)
        {
            switch (value.Type)
            {
            case EcmaValueType.Null:
                writer.Write("null");
                break;

            case EcmaValueType.Boolean:
                writer.Write(value.ToBoolean() ? "true" : "false");
                break;

            case EcmaValueType.Number:
                writer.Write(value.IsFinite ? value.ToString() : "null");
                break;

            case EcmaValueType.String:
                WriteString(writer, value.ToString());
                break;

            case EcmaValueType.Object:
                if (!value.IsCallable)
                {
                    if (EcmaArray.IsArray(value))
                    {
                        SerializeJsonArray(writer, stack, value.ToObject());
                    }
                    else
                    {
                        SerializeJsonObject(writer, stack, value.ToObject());
                    }
                }
                break;
            }
        }
Ejemplo n.º 3
0
 protected override PromiseState GetState(out EcmaValue value)
 {
     if (this.Count == 0 || this.All(v => v.State != PromiseState.Pending))
     {
         RuntimeObject    objectProto = RuntimeRealm.Current.GetRuntimeObject(WellKnownObject.ObjectPrototype);
         List <EcmaValue> values      = new List <EcmaValue>();
         foreach (PromiseAggregateHandler handler in this)
         {
             RuntimeObject obj = RuntimeObject.Create(objectProto);
             if (handler.State == PromiseState.Fulfilled)
             {
                 obj.CreateDataPropertyOrThrow(WellKnownProperty.Status, "fulfilled");
                 obj.CreateDataPropertyOrThrow(WellKnownProperty.Value, handler.Value);
             }
             else
             {
                 obj.CreateDataPropertyOrThrow(WellKnownProperty.Status, "rejected");
                 obj.CreateDataPropertyOrThrow(WellKnownProperty.Reason, handler.Value);
             }
             values.Add(obj);
         }
         value = new EcmaArray(values);
         return(PromiseState.Fulfilled);
     }
     return(base.GetState(out value));
 }
Ejemplo n.º 4
0
        public void ByteLength(RuntimeFunction byteLength)
        {
            IsUnconstructableFunctionWLength(byteLength, "get byteLength", 0);
            That(SharedArrayBuffer.Prototype, Has.OwnProperty("byteLength", EcmaPropertyAttributes.Configurable));
            That(SharedArrayBuffer.Prototype.GetOwnProperty("byteLength").Set, Is.Undefined);

            It("should throw a TypeError when this is not an object", () => {
                Case(Undefined, Throws.TypeError);
                Case(Null, Throws.TypeError);
                Case(false, Throws.TypeError);
                Case(0, Throws.TypeError);
                Case("", Throws.TypeError);
                Case(new Symbol(), Throws.TypeError);
            });

            It("should throw a TypeError exception when `this` does not have a [[ArrayBufferData]]", () => {
                Case(SharedArrayBuffer.Prototype, Throws.TypeError);
                Case(Object.Construct(), Throws.TypeError);
                Case(EcmaArray.Of(), Throws.TypeError);
                Case(Global.Int8Array.Construct(8), Throws.TypeError);
                Case(Global.DataView.Construct(SharedArrayBuffer.Construct(8), 0), Throws.TypeError);
            });

            It("should throw a TypeError if `this` is a ArrayBuffer", () => {
                Case(Global.ArrayBuffer.Construct(0), Throws.TypeError);
            });

            It("should return value from [[ByteLength]] internal slot", () => {
                Case(SharedArrayBuffer.Construct(0), 0);
                Case(SharedArrayBuffer.Construct(42), 42);
            });
        }
Ejemplo n.º 5
0
        protected override RuntimeObject CreateObject()
        {
            if (emptyOrSpread.Count == 0)
            {
                return(new EcmaArray(new List <EcmaValue>(values)));
            }
            EcmaArray target = new EcmaArray();

            for (int i = 0, j = 0, len = values.Count; i < len; i++)
            {
                if (!emptyOrSpread.TryGetValue(i, out object modifier))
                {
                    target[j++] = values[i];
                    continue;
                }
                if (modifier is ISpreadLiteral)
                {
                    foreach (EcmaValue w in values[i].ForOf())
                    {
                        target[j++] = w;
                    }
                }
                else
                {
                    j++;
                }
            }
            return(target);
        }
Ejemplo n.º 6
0
 protected override PromiseState GetState(out EcmaValue value)
 {
     if (this.Count == 0 || this.All(v => v.State == PromiseState.Fulfilled))
     {
         value = new EcmaArray(this.Select(v => v.Value).ToList());
         return(PromiseState.Fulfilled);
     }
     return(base.GetState(out value));
 }
Ejemplo n.º 7
0
        public void Clear(RuntimeFunction clear)
        {
            IsUnconstructableFunctionWLength(clear, "clear", 0);

            It("should throw a TypeError when this is not an object", () => {
                Case(Undefined, Throws.TypeError);
                Case(Null, Throws.TypeError);
                Case(false, Throws.TypeError);
                Case(0, Throws.TypeError);
                Case("", Throws.TypeError);
                Case(new Symbol(), Throws.TypeError);
            });

            It("should throw a TypeError when this object has no [[MapData]] internal slot", () => {
                Case(EcmaArray.Of(), Throws.TypeError);
                Case(Map.Prototype, Throws.TypeError);
                Case(Object.Construct(), Throws.TypeError);
                Case(Global.Set.Construct(), Throws.TypeError);
                Case(WeakSet.Construct(), Throws.TypeError);
            });

            It("should clear a Map object", () => {
                EcmaValue map = Map.Construct(EcmaArray.Of(
                                                  EcmaArray.Of("foo", "bar"),
                                                  EcmaArray.Of(1, 1)
                                                  ));
                clear.Call(map);
                That(map["size"], Is.EqualTo(0));

                map = Map.Construct();
                map.Invoke("set", "foo", "bar");
                map.Invoke("set", 1, 1);
                clear.Call(map);
                That(map["size"], Is.EqualTo(0));
            });

            It("should return undefined", () => {
                EcmaValue map = Map.Construct();
                Case(map, Undefined);
                Case(map, Undefined);
            });

            It("should not break iterator", () => {
                EcmaValue map = Map.Construct(EcmaArray.Of(
                                                  EcmaArray.Of("a", 1),
                                                  EcmaArray.Of("b", 2),
                                                  EcmaArray.Of("c", 3)
                                                  ));
                EcmaValue iterator = map.Invoke("entries");
                iterator.Invoke("next");
                clear.Call(map);
                VerifyIteratorResult(iterator.Invoke("next"), true);
            });
        }
Ejemplo n.º 8
0
 public static void Deconstruct(this EcmaValue value, out EcmaValue item1, out EcmaValue item2, out EcmaValue item3, out EcmaValue item4, out EcmaValue item5, out EcmaValue item6, out EcmaValue item7, out EcmaValue rest)
 {
     EcmaValue[] arr = CreateListFromArrayLike(value);
     item1 = arr.ElementAtOrDefault(0);
     item2 = arr.ElementAtOrDefault(1);
     item3 = arr.ElementAtOrDefault(2);
     item4 = arr.ElementAtOrDefault(3);
     item5 = arr.ElementAtOrDefault(4);
     item6 = arr.ElementAtOrDefault(5);
     item7 = arr.ElementAtOrDefault(6);
     rest  = new EcmaArray(new List <EcmaValue>(arr.Skip(7)));
 }
Ejemplo n.º 9
0
 public EcmaValue ToValue()
 {
     if (result.Success)
     {
         EcmaArray arr = new EcmaArray(Captures.ToArray());
         arr.CreateDataPropertyOrThrow(WellKnownProperty.Index, this.Index);
         arr.CreateDataPropertyOrThrow(WellKnownProperty.Input, this.Input);
         arr.CreateDataPropertyOrThrow(WellKnownProperty.Groups, CreateNamedGroupObject());
         return(arr);
     }
     return(EcmaValue.Null);
 }
Ejemplo n.º 10
0
        public void DerivedConstructor()
        {
            foreach (string derived in new[] { "EvalError", "RangeError", "ReferenceError", "SyntaxError", "TypeError", "URIError" })
            {
                That(GlobalThis[derived], Is.TypeOf("function"));

                RuntimeFunction ctor      = (RuntimeFunction)GlobalThis[derived].ToObject();
                WellKnownObject protoType = (WellKnownObject)System.Enum.Parse(typeof(WellKnownObject), derived + "Prototype", true);
                IsConstructorWLength(ctor, derived, 1, ctor.Realm.GetRuntimeObject(protoType), Error);
                IsAbruptedFromToPrimitive(ctor.Bind(_));

                That(ctor.Call(), Is.InstanceOf(ctor));
                That(Object.Prototype["toString"].Call(ctor.Construct()), Is.EqualTo("[object Error]"));

                It("should coerce first argument to string", () => {
                    That(ctor.Construct(Null)["message"], Is.EqualTo("null"));
                    That(ctor.Construct(0)["message"], Is.EqualTo("0"));
                    That(ctor.Construct(true)["message"], Is.EqualTo("true"));
                    That(ctor.Construct(Object.Construct())["message"], Is.EqualTo("[object Object]"));
                    That(ctor.Construct(CreateObject(toString: () => "foo"))["message"], Is.EqualTo("foo"));
                    That(ctor.Construct(CreateObject(toString: () => Object.Construct(), valueOf: () => 1))["message"], Is.EqualTo("1"));
                    That(ctor.Construct(CreateObject(toPrimitive: () => "foo"))["message"], Is.EqualTo("foo"));

                    That(() => ctor.Construct(new Symbol()), Throws.TypeError);
                    That(() => ctor.Construct(CreateObject(toString: () => Object.Construct(), valueOf: () => Object.Construct())), Throws.TypeError);
                });

                It("should define own message property if first argument is not undefined", () => {
                    That(ctor.Call(_, "msg1")["message"], Is.EqualTo("msg1"));
                    That(ctor.Construct("msg1")["message"], Is.EqualTo("msg1"));

                    That(ctor.Construct().HasOwnProperty("message"), Is.EqualTo(false));
                    That(ctor.Construct(Undefined).HasOwnProperty("message"), Is.EqualTo(false));
                    That(ctor.Construct(Null).HasOwnProperty("message"), Is.EqualTo(true));
                    That(ctor.Construct("").HasOwnProperty("message"), Is.EqualTo(true));
                });

                It("should define own stack property", () => {
                    That(ctor.Construct(), Has.OwnProperty("stack", EcmaPropertyAttributes.Writable | EcmaPropertyAttributes.Configurable));
                    That(ctor.Construct()["stack"], Is.Not.EqualTo(Undefined));
                });

                It("should derive [[Prototype]] value from realm of newTarget", () => {
                    RuntimeRealm realm = new RuntimeRealm();
                    EcmaValue fn       = realm.GetRuntimeObject(WellKnownObject.FunctionConstructor).Construct();
                    fn["prototype"]    = Null;
                    EcmaValue other    = Reflect.Invoke("construct", ctor, EcmaArray.Of(), fn);
                    That(Object.Invoke("getPrototypeOf", other), Is.EqualTo(realm.GetRuntimeObject(protoType)));
                });
            }
        }
Ejemplo n.º 11
0
        public static EcmaValue From([This] EcmaValue thisValue, EcmaValue arrayLike, EcmaValue mapFn, EcmaValue thisArg)
        {
            RuntimeObject items = arrayLike.ToObject();

            if (mapFn != default)
            {
                Guard.ArgumentIsCallable(mapFn);
            }
            bool          usingIterator = items.GetMethod(WellKnownSymbol.Iterator) != null;
            long          initialLen    = usingIterator ? 0 : arrayLike[WellKnownProperty.Length].ToLength();
            RuntimeObject arr;

            if (thisValue.IsCallable && thisValue.ToObject().IsConstructor)
            {
                arr = thisValue.Construct(usingIterator ? EcmaValue.EmptyArray : new EcmaValue[] { initialLen }).ToObject();
            }
            else
            {
                arr = new EcmaArray(initialLen);
            }

            if (usingIterator)
            {
                foreach (EcmaValue value in items.GetIterator())
                {
                    ArrayPrototype.ThrowIfLengthExceeded(initialLen + 1);
                    EcmaValue value1 = value;
                    if (mapFn != default)
                    {
                        value1 = mapFn.Call(thisArg, value, initialLen);
                    }
                    arr.CreateDataPropertyOrThrow(initialLen++, value1);
                }
            }
            else
            {
                for (long i = 0; i < initialLen; i++)
                {
                    EcmaValue value = items[i];
                    if (mapFn != default)
                    {
                        value = mapFn.Call(thisArg, value, i);
                    }
                    arr.CreateDataPropertyOrThrow(i, value);
                }
            }
            arr.SetOrThrow(WellKnownProperty.Length, initialLen);
            return(arr);
        }
Ejemplo n.º 12
0
                    public static ScriptData ReadData(Stream stream)
                    {
                        ScriptDataType scriptDataType = (ScriptDataType)stream.ReadByte();

                        if (!(stream.Position < stream.Length - 4))
                        {
                            return(null);
                        }
                        ScriptData scriptData;

                        switch (scriptDataType)
                        {
                        case ScriptDataType.Number:
                            scriptData = new Number(stream);
                            break;

                        case ScriptDataType.Boolean:
                            scriptData = new Boolean(stream);
                            break;

                        case ScriptDataType.String:
                            scriptData = new String(stream);
                            break;

                        case ScriptDataType.Object:
                            scriptData = new Object(stream);
                            break;

                        case ScriptDataType.Reference:
                            scriptData = new Reference(stream);
                            break;

                        case ScriptDataType.EcmaArray:
                            scriptData = new EcmaArray(stream);
                            break;

                        case ScriptDataType.ObjectEndMark:
                            scriptData = new ObjectEndMark(stream);
                            break;

                        case ScriptDataType.StrictArray:
                            scriptData = new StrictArray(stream);
                            break;

                        default:
                            throw new UnsupportedFormat(string.Format("Unsupported Script data type 0x{0}", scriptDataType.ToString("X2")));
                        }
                        return(scriptData);
                    }
Ejemplo n.º 13
0
        public void Constructor(RuntimeFunction ctor)
        {
            IsConstructorWLength(ctor, "WeakMap", 0, WeakMap.Prototype);
            That(GlobalThis, Has.OwnProperty("WeakMap", ctor, EcmaPropertyAttributes.Writable | EcmaPropertyAttributes.Configurable));

            It("must be called as constructor", () => {
                That(() => WeakMap.Call(This), Throws.TypeError);
                That(() => WeakMap.Call(This, EcmaArray.Of()), Throws.TypeError);
            });

            It("should return a new WeakMap object if iterable is undefined or null", () => {
                That(Object.Invoke("getPrototypeOf", WeakMap.Construct()), Is.EqualTo(WeakMap.Prototype));
                That(Object.Invoke("getPrototypeOf", WeakMap.Construct(Undefined)), Is.EqualTo(WeakMap.Prototype));
                That(Object.Invoke("getPrototypeOf", WeakMap.Construct(Null)), Is.EqualTo(WeakMap.Prototype));
            });

            It("should throw a TypeError if object is not iterable", () => {
                That(() => WeakMap.Construct(Object.Construct()), Throws.TypeError);
            });

            It("should throw a TypeError if iterable items are not Objects", () => {
                That(() => WeakMap.Construct(EcmaArray.Of(Undefined, 1)), Throws.TypeError);
                That(() => WeakMap.Construct(EcmaArray.Of(Null, 1)), Throws.TypeError);
                That(() => WeakMap.Construct(EcmaArray.Of(1, 1)), Throws.TypeError);
                That(() => WeakMap.Construct(EcmaArray.Of("", 1)), Throws.TypeError);
                That(() => WeakMap.Construct(EcmaArray.Of(true, 1)), Throws.TypeError);
                That(() => WeakMap.Construct(EcmaArray.Of(new Symbol(), 1)), Throws.TypeError);
                That(() => WeakMap.Construct(EcmaArray.Of(EcmaArray.Of("a", 1), 2)), Throws.TypeError);
            });

            It("should throw TypeError if set is not callable only when iterable is not undefined or null", () => {
                using (TempProperty(WeakMap.Prototype, "set", Null)) {
                    That(() => WeakMap.Construct(EcmaArray.Of()), Throws.TypeError);
                    That(() => WeakMap.Construct(), Throws.Nothing);
                    That(() => WeakMap.Construct(Undefined), Throws.Nothing);
                    That(() => WeakMap.Construct(Null), Throws.Nothing);
                }
            });

            It("should return abrupt from getting set mehod only when iterable is not undefined or null", () => {
                using (TempProperty(WeakMap.Prototype, "set", EcmaPropertyDescriptor.FromValue(CreateObject(("get", ThrowTest262Exception), ("configurable", true))))) {
                    That(() => WeakMap.Construct(EcmaArray.Of()), Throws.Test262);
                    That(() => WeakMap.Construct(), Throws.Nothing);
                    That(() => WeakMap.Construct(Undefined), Throws.Nothing);
                    That(() => WeakMap.Construct(Null), Throws.Nothing);
                }
            });
Ejemplo n.º 14
0
        public void Constructor(RuntimeFunction ctor)
        {
            IsConstructorWLength(ctor, "Error", 1, Error.Prototype);
            IsAbruptedFromToPrimitive(ctor.Bind(_));

            That(ctor.Call(), Is.InstanceOf(ctor));
            That(Object.Prototype["toString"].Call(ctor.Construct()), Is.EqualTo("[object Error]"));

            It("should coerce first argument to string", () => {
                That(ctor.Construct(Null)["message"], Is.EqualTo("null"));
                That(ctor.Construct(0)["message"], Is.EqualTo("0"));
                That(ctor.Construct(true)["message"], Is.EqualTo("true"));
                That(ctor.Construct(Object.Construct())["message"], Is.EqualTo("[object Object]"));
                That(ctor.Construct(CreateObject(toString: () => "foo"))["message"], Is.EqualTo("foo"));
                That(ctor.Construct(CreateObject(toString: () => Object.Construct(), valueOf: () => 1))["message"], Is.EqualTo("1"));
                That(ctor.Construct(CreateObject(toPrimitive: () => "foo"))["message"], Is.EqualTo("foo"));

                That(() => ctor.Construct(new Symbol()), Throws.TypeError);
                That(() => ctor.Construct(CreateObject(toString: () => Object.Construct(), valueOf: () => Object.Construct())), Throws.TypeError);
            });

            It("should define own message property if first argument is not undefined", () => {
                That(ctor.Call(_, "msg1"), Has.OwnProperty("message", "msg1", EcmaPropertyAttributes.Writable | EcmaPropertyAttributes.Configurable));
                That(ctor.Construct("msg1"), Has.OwnProperty("message", "msg1", EcmaPropertyAttributes.Writable | EcmaPropertyAttributes.Configurable));

                That(ctor.Construct().HasOwnProperty("message"), Is.EqualTo(false));
                That(ctor.Construct(Undefined).HasOwnProperty("message"), Is.EqualTo(false));
                That(ctor.Construct(Null).HasOwnProperty("message"), Is.EqualTo(true));
                That(ctor.Construct("").HasOwnProperty("message"), Is.EqualTo(true));
            });

            It("should define own stack property", () => {
                That(ctor.Construct(), Has.OwnProperty("stack", EcmaPropertyAttributes.Writable | EcmaPropertyAttributes.Configurable));
                That(ctor.Construct()["stack"], Is.Not.EqualTo(Undefined));
            });

            It("should derive [[Prototype]] value from realm of newTarget", () => {
                RuntimeRealm realm = new RuntimeRealm();
                EcmaValue fn       = realm.GetRuntimeObject(WellKnownObject.FunctionConstructor).Construct();
                fn["prototype"]    = Null;
                EcmaValue other    = Reflect.Invoke("construct", ctor, EcmaArray.Of(), fn);
                That(Object.Invoke("getPrototypeOf", other), Is.EqualTo(realm.GetRuntimeObject(WellKnownObject.ErrorPrototype)));
            });
        }
Ejemplo n.º 15
0
        public static EcmaValue Array([This] EcmaValue thisValue, params EcmaValue[] args)
        {
            EcmaArray array = thisValue.GetUnderlyingObject <EcmaArray>();

            if (args.Length == 1 && args[0].Type == EcmaValueType.Number)
            {
                uint length = args[0].ToUInt32();
                if (args[0] != length)
                {
                    throw new EcmaRangeErrorException(InternalString.Error.InvalidArrayLength);
                }
                array.Length = length;
            }
            else
            {
                array.Init(args);
            }
            return(thisValue);
        }
Ejemplo n.º 16
0
        public static EcmaValue Stringify(EcmaValue value, EcmaValue replacer, EcmaValue space)
        {
            string indentString;

            space = EcmaValueUtility.UnboxPrimitiveObject(space);
            if (space.Type == EcmaValueType.Number)
            {
                indentString = new String(' ', Math.Max(0, Math.Min(10, space.ToInt32())));
            }
            else if (space.Type == EcmaValueType.String)
            {
                indentString = space.ToStringOrThrow();
            }
            else
            {
                indentString = String.Empty;
            }
            string result;

            if (replacer.IsCallable)
            {
                result = new EcmaJsonWriter(indentString, replacer).Serialize(value);
            }
            else if (EcmaArray.IsArray(replacer))
            {
                HashSet <string> propertyList = new HashSet <string>();
                for (long i = 0, length = replacer[WellKnownProperty.Length].ToLength(); i < length; i++)
                {
                    EcmaValue item = EcmaValueUtility.UnboxPrimitiveObject(replacer[i]);
                    if (item.Type == EcmaValueType.String || item.Type == EcmaValueType.Number)
                    {
                        propertyList.Add(item.ToStringOrThrow());
                    }
                }
                result = new EcmaJsonWriter(indentString, propertyList).Serialize(value);
            }
            else
            {
                result = new EcmaJsonWriter(indentString).Serialize(value);
            }
            return(result ?? EcmaValue.Undefined);
        }
Ejemplo n.º 17
0
 protected virtual void WriteValueOrObjectTag(EcmaValue value)
 {
     if (value.IsCallable)
     {
         WriteToken(InspectorTokenType.Function, "function");
     }
     else if (value.Type != EcmaValueType.Object || value.IsRegExp || EcmaArray.IsArray(value))
     {
         WriteValue(value);
     }
     else
     {
         string objectTag = InspectorUtility.GetObjectTag(value.ToObject());
         if (String.IsNullOrEmpty(objectTag))
         {
             objectTag = "Object";
         }
         WriteToken(InspectorTokenType.ObjectTag, objectTag);
     }
 }
Ejemplo n.º 18
0
        private static EcmaValue InternalizeJsonProperty(RuntimeObject holder, EcmaPropertyKey property, EcmaValue reviver)
        {
            EcmaValue value = holder.Get(property);

            if (value.Type == EcmaValueType.Object)
            {
                RuntimeObject obj = value.ToObject();
                if (EcmaArray.IsArray(value))
                {
                    for (long i = 0, length = value[WellKnownProperty.Length].ToLength(); i < length; i++)
                    {
                        EcmaValue newValue = InternalizeJsonProperty(obj, i, reviver);
                        if (newValue.Type == EcmaValueType.Undefined)
                        {
                            obj.Delete(i);
                        }
                        else
                        {
                            obj.CreateDataProperty(i, newValue);
                        }
                    }
                }
                else
                {
                    foreach (EcmaPropertyKey key in obj.GetEnumerableOwnPropertyKeys().ToArray())
                    {
                        EcmaValue newValue = InternalizeJsonProperty(obj, key, reviver);
                        if (newValue.Type == EcmaValueType.Undefined)
                        {
                            obj.Delete(key);
                        }
                        else
                        {
                            obj.CreateDataProperty(key, newValue);
                        }
                    }
                }
            }
            return(reviver.Call(holder, property.ToValue(), value));
        }
Ejemplo n.º 19
0
        public void ToString(RuntimeFunction toString)
        {
            IsUnconstructableFunctionWLength(toString, "toString", 0);
            That(BigInt.Prototype, Has.OwnProperty("toString", toString, EcmaPropertyAttributes.DefaultMethodProperty));

            It("should throw a TypeError if the this value is not a BigInt", () => {
                Case(Undefined, Throws.TypeError);
                Case(Null, Throws.TypeError);
                Case(false, Throws.TypeError);
                Case(true, Throws.TypeError);
                Case("", Throws.TypeError);
                Case("1n", Throws.TypeError);
                Case(0, Throws.TypeError);
                Case(1, Throws.TypeError);
                Case(NaN, Throws.TypeError);
                Case(new Symbol(), Throws.TypeError);

                Case(EcmaArray.Of(BigIntLiteral(1)), Throws.TypeError);
                Case(CreateObject(new { x = BigIntLiteral(1) }), Throws.TypeError);
                Case(CreateObject(valueOf: ThrowTest262Exception, toString: ThrowTest262Exception, toPrimitive: ThrowTest262Exception), Throws.TypeError);
            });

            It("should throw a RangeError if radix is invalid", () => {
                Case((BigIntLiteral(0), 0), Throws.RangeError);
                Case((BigIntLiteral(-1), 0), Throws.RangeError);
                Case((BigIntLiteral(1), 0), Throws.RangeError);

                Case((BigIntLiteral(0), 1), Throws.RangeError);
                Case((BigIntLiteral(-1), 1), Throws.RangeError);
                Case((BigIntLiteral(1), 1), Throws.RangeError);

                Case((BigIntLiteral(0), 37), Throws.RangeError);
                Case((BigIntLiteral(-1), 37), Throws.RangeError);
                Case((BigIntLiteral(1), 37), Throws.RangeError);

                Case((BigIntLiteral(0), Null), Throws.RangeError);
                Case((BigIntLiteral(-1), Null), Throws.RangeError);
                Case((BigIntLiteral(1), Null), Throws.RangeError);
            });
Ejemplo n.º 20
0
        public void Constructor(RuntimeFunction ctor)
        {
            IsConstructorWLength(ctor, "Set", 0, Set.Prototype);
            That(GlobalThis, Has.OwnProperty("Set", ctor, EcmaPropertyAttributes.Writable | EcmaPropertyAttributes.Configurable));

            It("must be called as constructor", () => {
                That(() => Set.Call(This), Throws.TypeError);
                That(() => Set.Call(This, EcmaArray.Of()), Throws.TypeError);
            });

            It("should throw a TypeError if object is not iterable", () => {
                That(() => Set.Construct(Object.Construct()), Throws.TypeError);
            });

            It("should construct an empty set if iterable is undefined or null", () => {
                That(Set.Construct()["size"], Is.EqualTo(0));
                That(Set.Construct(Undefined)["size"], Is.EqualTo(0));
                That(Set.Construct(Null)["size"], Is.EqualTo(0));
            });

            It("should throw TypeError if add is not callable only when iterable is not undefined or null", () => {
                using (TempProperty(Set.Prototype, "add", Null)) {
                    That(() => Set.Construct(EcmaArray.Of()), Throws.TypeError);
                    That(() => Set.Construct(), Throws.Nothing);
                    That(() => Set.Construct(Undefined), Throws.Nothing);
                    That(() => Set.Construct(Null), Throws.Nothing);
                }
            });

            It("should return abrupt from getting add mehod only when iterable is not undefined or null", () => {
                using (TempProperty(Set.Prototype, "add", EcmaPropertyDescriptor.FromValue(CreateObject(("get", ThrowTest262Exception), ("configurable", true))))) {
                    That(() => Set.Construct(EcmaArray.Of()), Throws.Test262);
                    That(() => Set.Construct(), Throws.Nothing);
                    That(() => Set.Construct(Undefined), Throws.Nothing);
                    That(() => Set.Construct(Null), Throws.Nothing);
                }
            });
Ejemplo n.º 21
0
        public void Constructor(RuntimeFunction ctor)
        {
            IsConstructorWLength(ctor, "Date", 7, Date.Prototype);

            It("should get [[DateValue]] from Date objects without calling object's method", () => {
                EcmaValue date   = Date.Construct(1438560000000);
                date["toString"] = ThrowTest262Exception;
                date["valueOf"]  = ThrowTest262Exception;
                date["getTime"]  = ThrowTest262Exception;
                That(Date.Construct(date).Invoke("getTime"), Is.EqualTo(1438560000000));
            });

            It("should invoke @@toPrimitive and coerce returned value", () => {
                That(Date.Construct(CreateObject(toPrimitive: () => 8)).Invoke("getTime"), Is.EqualTo(8));
                That(Date.Construct(CreateObject(toPrimitive: () => Undefined)).Invoke("getTime"), Is.EqualTo(NaN));
                That(Date.Construct(CreateObject(toPrimitive: () => true)).Invoke("getTime"), Is.EqualTo(1));
                That(Date.Construct(CreateObject(toPrimitive: () => false)).Invoke("getTime"), Is.EqualTo(0));
                That(Date.Construct(CreateObject(toPrimitive: () => Null)).Invoke("getTime"), Is.EqualTo(0));
                That(Date.Construct(CreateObject(toPrimitive: () => "2016-06-05T18:40:00.000Z")).Invoke("getTime"), Is.EqualTo(1465152000000));

                That(() => Date.Construct(CreateObject(toPrimitive: () => new Symbol())), Throws.TypeError);
                That(() => Date.Construct(CreateObject(toPrimitive: () => new EcmaObject())), Throws.TypeError);
                That(() => Date.Construct(CreateObject(toPrimitive: () => new EcmaArray())), Throws.TypeError);
                That(() => Date.Construct(CreateObject(toPrimitive: ThrowTest262Exception)), Throws.Test262);
            });

            It("should coerce input value in the correct order", () => {
                Logs.Clear();
                Date.Construct(Undefined,
                               CreateObject(toString: Intercept(() => 0, "year")),
                               CreateObject(toString: Intercept(() => 0, "month")),
                               CreateObject(toString: Intercept(() => 0, "date")),
                               CreateObject(toString: Intercept(() => 0, "hours")),
                               CreateObject(toString: Intercept(() => 0, "minutes")),
                               CreateObject(toString: Intercept(() => 0, "seconds")),
                               CreateObject(toString: Intercept(() => 0, "ms")));
                CollectionAssert.AreEqual(new[] { "year", "month", "date", "hours", "minutes", "seconds", "ms" }, Logs);
            });

            It("should return a string representing the current time if Date is called as a function", () => {
                That(Date.Call(Date), Is.TypeOf("string"));
                That(Date.Call(Date, 1), Is.TypeOf("string"));
                That(Date.Call(Date, 1970, 1), Is.TypeOf("string"));
                That(Date.Call(Date, 1970, 1, 1), Is.TypeOf("string"));
                That(Date.Call(Date, 1970, 1, 1, 1), Is.TypeOf("string"));
                That(Date.Call(Date, 1970, 1, 1, 1, 0), Is.TypeOf("string"));
                That(Date.Call(Date, 1970, 1, 1, 1, 0, 0), Is.TypeOf("string"));
                That(Date.Call(Date, 1970, 1, 1, 1, 0, 0, 0), Is.TypeOf("string"));
                That(Date.Call(Date, NaN), Is.TypeOf("string"));
                That(Date.Call(Date, Infinity), Is.TypeOf("string"));
                That(Date.Call(Date, -Infinity), Is.TypeOf("string"));
                That(Date.Call(Date, Undefined), Is.TypeOf("string"));
                That(Date.Call(Date, Null), Is.TypeOf("string"));

                That(Date.Construct() - Date.Construct(Date.Call(Date)).Invoke("getTime"), Is.AtLeast(-1000).And.AtMost(1000));
                That(Date.Construct() - Date.Construct(Date.Call(Date, 1)).Invoke("getTime"), Is.AtLeast(-1000).And.AtMost(1000));
                That(Date.Construct() - Date.Construct(Date.Call(Date, 1970, 1)).Invoke("getTime"), Is.AtLeast(-1000).And.AtMost(1000));
                That(Date.Construct() - Date.Construct(Date.Call(Date, 1970, 1, 1)).Invoke("getTime"), Is.AtLeast(-1000).And.AtMost(1000));
                That(Date.Construct() - Date.Construct(Date.Call(Date, 1970, 1, 1, 1)).Invoke("getTime"), Is.AtLeast(-1000).And.AtMost(1000));
                That(Date.Construct() - Date.Construct(Date.Call(Date, 1970, 1, 1, 1, 0)).Invoke("getTime"), Is.AtLeast(-1000).And.AtMost(1000));
                That(Date.Construct() - Date.Construct(Date.Call(Date, 1970, 1, 1, 1, 0, 0)).Invoke("getTime"), Is.AtLeast(-1000).And.AtMost(1000));
                That(Date.Construct() - Date.Construct(Date.Call(Date, 1970, 1, 1, 1, 0, 0, 0)).Invoke("getTime"), Is.AtLeast(-1000).And.AtMost(1000));
                That(Date.Construct() - Date.Construct(Date.Call(Date, NaN)).Invoke("getTime"), Is.AtLeast(-1000).And.AtMost(1000));
                That(Date.Construct() - Date.Construct(Date.Call(Date, Infinity)).Invoke("getTime"), Is.AtLeast(-1000).And.AtMost(1000));
                That(Date.Construct() - Date.Construct(Date.Call(Date, -Infinity)).Invoke("getTime"), Is.AtLeast(-1000).And.AtMost(1000));
                That(Date.Construct() - Date.Construct(Date.Call(Date, Undefined)).Invoke("getTime"), Is.AtLeast(-1000).And.AtMost(1000));
                That(Date.Construct() - Date.Construct(Date.Call(Date, Null)).Invoke("getTime"), Is.AtLeast(-1000).And.AtMost(1000));
            });

            It("should derive [[Prototype]] value from realm of newTarget", () => {
                RuntimeRealm realm = new RuntimeRealm();
                EcmaValue fn       = realm.GetRuntimeObject(WellKnownObject.FunctionConstructor).Construct();
                fn["prototype"]    = Null;
                EcmaValue other    = Reflect.Invoke("construct", ctor, EcmaArray.Of(), fn);
                That(Object.Invoke("getPrototypeOf", other), Is.EqualTo(realm.GetRuntimeObject(WellKnownObject.DatePrototype)));
            });
        }
Ejemplo n.º 22
0
 public ScriptTag(string name, EcmaArray value) : base(new TagHeader(TagType.Script).HeaderBytes)
 {
     Name  = new String(name);
     Value = value;
 }
Ejemplo n.º 23
0
 public static EcmaValue IsArray(EcmaValue value)
 {
     return(EcmaArray.IsArray(value));
 }
Ejemplo n.º 24
0
        public void SparseArray()
        {
            EcmaArray arr = new EcmaArray();

            // create new chunks
            arr[1] = 1;
            arr[4] = 4;
            That(arr.ToValue(), Is.EquivalentTo(new[] { Undefined, 1, Undefined, Undefined, 4 }));
            That(!arr.HasProperty(0));
            That(!arr.HasProperty(2));
            That(!arr.HasProperty(3));

            // add item at the end of a chunk
            arr[5] = 5;
            That(arr.ToValue(), Is.EquivalentTo(new[] { Undefined, 1, Undefined, Undefined, 4, 5 }));
            That(arr.HasProperty(5));

            // add item at the beginning of a chunk
            arr[3] = 3;
            That(arr.ToValue(), Is.EquivalentTo(new[] { Undefined, 1, Undefined, 3, 4, 5 }));
            That(arr.HasProperty(3));

            // merge chunks
            arr[2] = 2;
            That(arr.ToValue(), Is.EquivalentTo(new[] { Undefined, 1, 2, 3, 4, 5 }));
            That(arr.HasProperty(2));

            // become a non-chunked array
            arr[0] = 0;
            That(arr.ToValue(), Is.EquivalentTo(new[] { 0, 1, 2, 3, 4, 5 }));
            That(arr.HasProperty(0));

            // becoma a chunked array
            arr.Delete(1);
            That(arr.ToValue(), Is.EquivalentTo(new[] { 0, Undefined, 2, 3, 4, 5 }));
            That(!arr.HasProperty(1));

            // delete item at the beginning of a chunk
            arr.Delete(2);
            That(arr.ToValue(), Is.EquivalentTo(new[] { 0, Undefined, Undefined, 3, 4, 5 }));
            That(!arr.HasProperty(2));

            // delete item at the end of a chunk
            arr.Delete(5);
            That(arr.ToValue(), Is.EquivalentTo(new[] { 0, Undefined, Undefined, 3, 4, Undefined }));
            That(!arr.HasProperty(5));

            // remove chunk
            arr.Delete(0);
            That(arr.ToValue(), Is.EquivalentTo(new[] { Undefined, Undefined, Undefined, 3, 4, Undefined }));
            That(!arr.HasProperty(0));

            // no chunk should be created
            arr    = new EcmaArray();
            arr[0] = 0;
            That(arr.ToValue(), Is.EquivalentTo(new[] { 0 }));
            arr.Delete(0);
            That(arr.ToValue(), Is.EquivalentTo(new[] { Undefined }));

            arr    = new EcmaArray(5);
            arr[0] = 0;
            arr[4] = 4;
        }
Ejemplo n.º 25
0
        public void SetLength()
        {
            EcmaArray arr = new EcmaArray();

            Assume.That(arr.Length, Is.EqualTo(0));
            arr.Length = 3;
            That(arr.ToValue(), Is.EquivalentTo(new[] { Undefined, Undefined, Undefined }));
            That(!arr.HasProperty(0));
            That(!arr.HasProperty(1));
            That(!arr.HasProperty(2));

            // should delete properties whose name is an array index and is larger than new length
            arr        = new EcmaArray(1, 2, 3);
            arr.Length = 2;
            That(arr.ToValue(), Is.EquivalentTo(new[] { 1, 2 }));
            That(!arr.HasProperty(2));

            // should have changed if a property is added whose name is an array index
            arr = new EcmaArray();
            Assume.That(arr.Length, Is.EqualTo(0));
            arr[0] = 1;
            That(arr.ToValue(), Is.EquivalentTo(new[] { 1 }));
            arr[1] = 2;
            That(arr.ToValue(), Is.EquivalentTo(new[] { 1, 2 }));

            arr     = new EcmaArray();
            arr[-1] = 1;
            That(arr.Length, Is.EqualTo(0));
            arr[true] = 1;
            That(arr.Length, Is.EqualTo(0));
            arr[CreateObject(valueOf: () => 3)] = 1;
            That(arr.Length, Is.EqualTo(0));

            arr["1"] = 1;
            That(arr.Length, Is.EqualTo(2));
            arr[Number.Construct(2)] = 2;
            That(arr.Length, Is.EqualTo(3));
            arr[String.Construct("3")] = 2;
            That(arr.Length, Is.EqualTo(4));

            arr[4294967294] = 4294967294;
            That(arr.Length, Is.EqualTo(4294967295));
            arr[4294967295] = 4294967295;
            That(arr.Length, Is.EqualTo(4294967295));

            arr = new EcmaArray();
            That(() => arr["length"] = -1, Throws.RangeError);
            That(() => arr["length"] = 4294967295, Throws.Nothing);
            That(() => arr["length"] = 4294967296, Throws.RangeError);
            That(() => arr["length"] = 4294967297, Throws.RangeError);
            That(() => arr["length"] = 1.5, Throws.RangeError);
            That(() => arr["length"] = NaN, Throws.RangeError);
            That(() => arr["length"] = Infinity, Throws.RangeError);
            That(() => arr["length"] = -Infinity, Throws.RangeError);
            That(() => arr["length"] = Undefined, Throws.RangeError);

            arr["length"] = true;
            That(arr.Length, Is.EqualTo(1));
            arr["length"] = Null;
            That(arr.Length, Is.EqualTo(0));
            arr["length"] = Boolean.Construct(false);
            That(arr.Length, Is.EqualTo(0));
            arr["length"] = Number.Construct(1);
            That(arr.Length, Is.EqualTo(1));
            arr["length"] = "1";
            That(arr.Length, Is.EqualTo(1));
            arr["length"] = String.Construct("1");
            That(arr.Length, Is.EqualTo(1));

            arr["length"] = CreateObject(valueOf: () => 2);
            That(arr.Length, Is.EqualTo(2));
            arr["length"] = CreateObject(valueOf: () => 2, toString: () => 1);
            That(arr.Length, Is.EqualTo(2));
            arr["length"] = CreateObject(valueOf: () => 2, toString: () => new EcmaObject());
            That(arr.Length, Is.EqualTo(2));
            arr["length"] = CreateObject(valueOf: () => 2, toString: ThrowTest262Exception);
            That(arr.Length, Is.EqualTo(2));

            arr["length"] = CreateObject(toString: () => 1);
            That(arr.Length, Is.EqualTo(1));
            arr["length"] = CreateObject(toString: () => 1, valueOf: () => new EcmaObject());
            That(arr.Length, Is.EqualTo(1));

            That(() => arr["length"] = CreateObject(valueOf: ThrowTest262Exception, toString: () => 1), Throws.Test262);
            That(() => arr["length"] = CreateObject(valueOf: () => new EcmaObject(), toString: () => new EcmaObject()), Throws.TypeError);
        }
Ejemplo n.º 26
0
        public void Constructor(RuntimeFunction ctor)
        {
            IsConstructorWLength(ctor, "DataView", 1, DataView.Prototype);
            That(GlobalThis, Has.OwnProperty("DataView", ctor, EcmaPropertyAttributes.Writable | EcmaPropertyAttributes.Configurable));

            It("should derive [[Prototype]] value from realm of newTarget", () => {
                RuntimeRealm realm = new RuntimeRealm();
                EcmaValue fn       = realm.GetRuntimeObject(WellKnownObject.FunctionConstructor).Construct();
                fn["prototype"]    = Null;
                EcmaValue other    = Reflect.Invoke("construct", ctor, EcmaArray.Of(ArrayBuffer.Construct(8)), fn);
                That(Object.Invoke("getPrototypeOf", other), Is.EqualTo(realm.GetRuntimeObject(WellKnownObject.DataViewPrototype)));
            });

            It("should throw a TypeError if ArrayBuffer is called as a function", () => {
                EcmaValue obj     = CreateObject(valueOf: ThrowTest262WithMessage("NewTarget should be verified before byteOffset"));
                EcmaValue buffer  = ArrayBuffer.Construct(8);
                EcmaValue sbuffer = SharedArrayBuffer.Construct(8);
                That(() => DataView.Call(Undefined, buffer, obj), Throws.TypeError);
                That(() => DataView.Call(Undefined, sbuffer, obj), Throws.TypeError);
            });

            It("should throw a TypeError if buffer is not Object", () => {
                EcmaValue obj = CreateObject(valueOf: ThrowTest262WithMessage("buffer should be verified before byteOffset"));
                That(() => DataView.Construct(0, obj), Throws.TypeError);
                That(() => DataView.Construct(1, obj), Throws.TypeError);
                That(() => DataView.Construct("", obj), Throws.TypeError);
                That(() => DataView.Construct("buffer", obj), Throws.TypeError);
                That(() => DataView.Construct(true, obj), Throws.TypeError);
                That(() => DataView.Construct(false, obj), Throws.TypeError);
                That(() => DataView.Construct(NaN, obj), Throws.TypeError);
                That(() => DataView.Construct(new Symbol(), obj), Throws.TypeError);
            });

            It("should throw a TypeError if buffer does not have [[ArrayBufferData]]", () => {
                EcmaValue obj = CreateObject(valueOf: ThrowTest262WithMessage("buffer should be verified before byteOffset"));
                That(() => DataView.Construct(Object.Construct(), obj), Throws.TypeError, "{}");
                That(() => DataView.Construct(EcmaArray.Of(), obj), Throws.TypeError, "[]");
                That(() => DataView.Construct(Global.Int8Array.Construct(), obj), Throws.TypeError, "typedArray instance");
                That(() => DataView.Construct(DataView.Construct(ArrayBuffer.Construct(0)), obj), Throws.TypeError, "dataView instance");
                That(() => DataView.Construct(DataView.Construct(SharedArrayBuffer.Construct(0)), obj), Throws.TypeError, "dataView instance");
            });

            It("should throw a TypeError if the buffer is detached", () => {
                EcmaValue obj    = CreateObject(valueOf: Intercept(() => 0));
                EcmaValue buffer = ArrayBuffer.Construct();
                DetachBuffer(buffer);

                That(() => DataView.Construct(buffer, obj), Throws.TypeError);
                That(Logs.Count, Is.EqualTo(1));
            });

            It("should reuse buffer argument instead of making a new clone", () => {
                EcmaValue buffer = ArrayBuffer.Construct(8);
                That(DataView.Construct(buffer, 0)["buffer"], Is.EqualTo(buffer));
                That(DataView.Construct(buffer, 0)["buffer"], Is.EqualTo(buffer));

                EcmaValue sbuffer = SharedArrayBuffer.Construct(8);
                That(DataView.Construct(sbuffer, 0)["buffer"], Is.EqualTo(sbuffer));
                That(DataView.Construct(sbuffer, 0)["buffer"], Is.EqualTo(sbuffer));
            });

            It("should throw a RangeError if ToInteger(byteOffset) < 0", () => {
                EcmaValue buffer = ArrayBuffer.Construct(8);
                That(() => DataView.Construct(buffer, -1), Throws.RangeError);
                That(() => DataView.Construct(buffer, -Infinity), Throws.RangeError);

                EcmaValue sbuffer = SharedArrayBuffer.Construct(8);
                That(() => DataView.Construct(sbuffer, -1), Throws.RangeError);
                That(() => DataView.Construct(sbuffer, -Infinity), Throws.RangeError);
            });

            It("should throw if buffer is detached during OrdinaryCreateFromConstructor", () => {
                EcmaValue buffer     = ArrayBuffer.Construct(8);
                EcmaValue called     = false;
                EcmaValue byteOffset = CreateObject(valueOf: () => Return(called = true, 0));
                EcmaValue newTarget  = FunctionLiteral(Noop).Invoke("bind", Null);
                Object.Invoke("defineProperty", newTarget, "prototype", CreateObject(new {
                    get = Intercept(() => {
                        DetachBuffer(buffer);
                        return(DataView.Prototype);
                    })
                }));

                That(() => Reflect.Invoke("construct", DataView, EcmaArray.Of(buffer, byteOffset), newTarget), Throws.TypeError);
                That(called, Is.EqualTo(true));
            });

            It("should return abrupt from newTarget's custom constructor prototype", () => {
                EcmaValue newTarget = FunctionLiteral(Noop).Invoke("bind", Null);
                Object.Invoke("defineProperty", newTarget, "prototype", CreateObject(new { get = ThrowTest262Exception }));

                EcmaValue buffer = ArrayBuffer.Construct(8);
                That(() => Reflect.Invoke("construct", DataView, EcmaArray.Of(buffer, 0), newTarget), Throws.Test262);

                EcmaValue sbuffer = SharedArrayBuffer.Construct(8);
                That(() => Reflect.Invoke("construct", DataView, EcmaArray.Of(sbuffer, 0), newTarget), Throws.Test262);
            });

            It("should use DataView.Prototype if newTarget's prototype is not an Object", () => {
                EcmaValue newTarget    = FunctionLiteral(Noop);
                newTarget["prototype"] = Null;

                EcmaValue buffer = ArrayBuffer.Construct(8);
                EcmaValue result = Reflect.Invoke("construct", DataView, EcmaArray.Of(buffer, 0), newTarget);
                That(result["constructor"], Is.EqualTo(DataView));
                That(Object.Invoke("getPrototypeOf", result), Is.EqualTo(DataView.Prototype));

                EcmaValue sbuffer = SharedArrayBuffer.Construct(8);
                EcmaValue sresult = Reflect.Invoke("construct", DataView, EcmaArray.Of(sbuffer, 0), newTarget);
                That(sresult["constructor"], Is.EqualTo(DataView));
                That(Object.Invoke("getPrototypeOf", sresult), Is.EqualTo(DataView.Prototype));
            });

            It("should use newTarget's custom constructor prototype if Object", () => {
                EcmaValue newTarget    = FunctionLiteral(Noop);
                EcmaValue proto        = Object.Construct();
                newTarget["prototype"] = proto;

                EcmaValue buffer = ArrayBuffer.Construct(8);
                EcmaValue result = Reflect.Invoke("construct", DataView, EcmaArray.Of(buffer, 0), newTarget);
                That(result["constructor"], Is.EqualTo(Object));
                That(Object.Invoke("getPrototypeOf", result), Is.EqualTo(proto));

                EcmaValue sbuffer = SharedArrayBuffer.Construct(8);
                EcmaValue sresult = Reflect.Invoke("construct", DataView, EcmaArray.Of(sbuffer, 0), newTarget);
                That(sresult["constructor"], Is.EqualTo(Object));
                That(Object.Invoke("getPrototypeOf", sresult), Is.EqualTo(proto));
            });

            It("should return new instance from defined offset", () => {
                EcmaValue sample;
                EcmaValue buffer = ArrayBuffer.Construct(4);

                sample = DataView.Construct(buffer, 0);
                That(sample["byteLength"], Is.EqualTo(4), "sample.byteLength");
                That(sample["byteOffset"], Is.EqualTo(0), "sample.byteOffset");
                That(sample["buffer"], Is.EqualTo(buffer), "sample.buffer");
                That(sample["constructor"], Is.EqualTo(DataView));
                That(Object.Invoke("getPrototypeOf", sample), Is.EqualTo(DataView.Prototype));

                sample = DataView.Construct(buffer, 1);
                That(sample["byteLength"], Is.EqualTo(3), "sample.byteLength");
                That(sample["byteOffset"], Is.EqualTo(1), "sample.byteOffset");
                That(sample["buffer"], Is.EqualTo(buffer), "sample.buffer");
                That(sample["constructor"], Is.EqualTo(DataView));
                That(Object.Invoke("getPrototypeOf", sample), Is.EqualTo(DataView.Prototype));

                sample = DataView.Construct(buffer, 2);
                That(sample["byteLength"], Is.EqualTo(2), "sample.byteLength");
                That(sample["byteOffset"], Is.EqualTo(2), "sample.byteOffset");
                That(sample["buffer"], Is.EqualTo(buffer), "sample.buffer");
                That(sample["constructor"], Is.EqualTo(DataView));
                That(Object.Invoke("getPrototypeOf", sample), Is.EqualTo(DataView.Prototype));

                sample = DataView.Construct(buffer, 3);
                That(sample["byteLength"], Is.EqualTo(1), "sample.byteLength");
                That(sample["byteOffset"], Is.EqualTo(3), "sample.byteOffset");
                That(sample["buffer"], Is.EqualTo(buffer), "sample.buffer");
                That(sample["constructor"], Is.EqualTo(DataView));
                That(Object.Invoke("getPrototypeOf", sample), Is.EqualTo(DataView.Prototype));

                sample = DataView.Construct(buffer, 4);
                That(sample["byteLength"], Is.EqualTo(0), "sample.byteLength");
                That(sample["byteOffset"], Is.EqualTo(4), "sample.byteOffset");
                That(sample["buffer"], Is.EqualTo(buffer), "sample.buffer");
                That(sample["constructor"], Is.EqualTo(DataView));
                That(Object.Invoke("getPrototypeOf", sample), Is.EqualTo(DataView.Prototype));

                sample = DataView.Construct(buffer, 0, Undefined);
                That(sample["byteLength"], Is.EqualTo(4), "sample.byteLength");
                That(sample["byteOffset"], Is.EqualTo(0), "sample.byteOffset");
                That(sample["buffer"], Is.EqualTo(buffer), "sample.buffer");
                That(sample["constructor"], Is.EqualTo(DataView));
                That(Object.Invoke("getPrototypeOf", sample), Is.EqualTo(DataView.Prototype));

                sample = DataView.Construct(buffer, 1, Undefined);
                That(sample["byteLength"], Is.EqualTo(3), "sample.byteLength");
                That(sample["byteOffset"], Is.EqualTo(1), "sample.byteOffset");
                That(sample["buffer"], Is.EqualTo(buffer), "sample.buffer");
                That(sample["constructor"], Is.EqualTo(DataView));
                That(Object.Invoke("getPrototypeOf", sample), Is.EqualTo(DataView.Prototype));

                sample = DataView.Construct(buffer, 2, Undefined);
                That(sample["byteLength"], Is.EqualTo(2), "sample.byteLength");
                That(sample["byteOffset"], Is.EqualTo(2), "sample.byteOffset");
                That(sample["buffer"], Is.EqualTo(buffer), "sample.buffer");
                That(sample["constructor"], Is.EqualTo(DataView));
                That(Object.Invoke("getPrototypeOf", sample), Is.EqualTo(DataView.Prototype));

                sample = DataView.Construct(buffer, 3, Undefined);
                That(sample["byteLength"], Is.EqualTo(1), "sample.byteLength");
                That(sample["byteOffset"], Is.EqualTo(3), "sample.byteOffset");
                That(sample["buffer"], Is.EqualTo(buffer), "sample.buffer");
                That(sample["constructor"], Is.EqualTo(DataView));
                That(Object.Invoke("getPrototypeOf", sample), Is.EqualTo(DataView.Prototype));

                sample = DataView.Construct(buffer, 4, Undefined);
                That(sample["byteLength"], Is.EqualTo(0), "sample.byteLength");
                That(sample["byteOffset"], Is.EqualTo(4), "sample.byteOffset");
                That(sample["buffer"], Is.EqualTo(buffer), "sample.buffer");
                That(sample["constructor"], Is.EqualTo(DataView));
                That(Object.Invoke("getPrototypeOf", sample), Is.EqualTo(DataView.Prototype));

                buffer = SharedArrayBuffer.Construct(4);
                sample = DataView.Construct(buffer, 0);
                That(sample["byteLength"], Is.EqualTo(4), "sample.byteLength");
                That(sample["byteOffset"], Is.EqualTo(0), "sample.byteOffset");
                That(sample["buffer"], Is.EqualTo(buffer), "sample.buffer");
                That(sample["constructor"], Is.EqualTo(DataView));
                That(Object.Invoke("getPrototypeOf", sample), Is.EqualTo(DataView.Prototype));

                sample = DataView.Construct(buffer, 1);
                That(sample["byteLength"], Is.EqualTo(3), "sample.byteLength");
                That(sample["byteOffset"], Is.EqualTo(1), "sample.byteOffset");
                That(sample["buffer"], Is.EqualTo(buffer), "sample.buffer");
                That(sample["constructor"], Is.EqualTo(DataView));
                That(Object.Invoke("getPrototypeOf", sample), Is.EqualTo(DataView.Prototype));

                sample = DataView.Construct(buffer, 2);
                That(sample["byteLength"], Is.EqualTo(2), "sample.byteLength");
                That(sample["byteOffset"], Is.EqualTo(2), "sample.byteOffset");
                That(sample["buffer"], Is.EqualTo(buffer), "sample.buffer");
                That(sample["constructor"], Is.EqualTo(DataView));
                That(Object.Invoke("getPrototypeOf", sample), Is.EqualTo(DataView.Prototype));

                sample = DataView.Construct(buffer, 3);
                That(sample["byteLength"], Is.EqualTo(1), "sample.byteLength");
                That(sample["byteOffset"], Is.EqualTo(3), "sample.byteOffset");
                That(sample["buffer"], Is.EqualTo(buffer), "sample.buffer");
                That(sample["constructor"], Is.EqualTo(DataView));
                That(Object.Invoke("getPrototypeOf", sample), Is.EqualTo(DataView.Prototype));

                sample = DataView.Construct(buffer, 4);
                That(sample["byteLength"], Is.EqualTo(0), "sample.byteLength");
                That(sample["byteOffset"], Is.EqualTo(4), "sample.byteOffset");
                That(sample["buffer"], Is.EqualTo(buffer), "sample.buffer");
                That(sample["constructor"], Is.EqualTo(DataView));
                That(Object.Invoke("getPrototypeOf", sample), Is.EqualTo(DataView.Prototype));

                sample = DataView.Construct(buffer, 0, Undefined);
                That(sample["byteLength"], Is.EqualTo(4), "sample.byteLength");
                That(sample["byteOffset"], Is.EqualTo(0), "sample.byteOffset");
                That(sample["buffer"], Is.EqualTo(buffer), "sample.buffer");
                That(sample["constructor"], Is.EqualTo(DataView));
                That(Object.Invoke("getPrototypeOf", sample), Is.EqualTo(DataView.Prototype));

                sample = DataView.Construct(buffer, 1, Undefined);
                That(sample["byteLength"], Is.EqualTo(3), "sample.byteLength");
                That(sample["byteOffset"], Is.EqualTo(1), "sample.byteOffset");
                That(sample["buffer"], Is.EqualTo(buffer), "sample.buffer");
                That(sample["constructor"], Is.EqualTo(DataView));
                That(Object.Invoke("getPrototypeOf", sample), Is.EqualTo(DataView.Prototype));

                sample = DataView.Construct(buffer, 2, Undefined);
                That(sample["byteLength"], Is.EqualTo(2), "sample.byteLength");
                That(sample["byteOffset"], Is.EqualTo(2), "sample.byteOffset");
                That(sample["buffer"], Is.EqualTo(buffer), "sample.buffer");
                That(sample["constructor"], Is.EqualTo(DataView));
                That(Object.Invoke("getPrototypeOf", sample), Is.EqualTo(DataView.Prototype));

                sample = DataView.Construct(buffer, 3, Undefined);
                That(sample["byteLength"], Is.EqualTo(1), "sample.byteLength");
                That(sample["byteOffset"], Is.EqualTo(3), "sample.byteOffset");
                That(sample["buffer"], Is.EqualTo(buffer), "sample.buffer");
                That(sample["constructor"], Is.EqualTo(DataView));
                That(Object.Invoke("getPrototypeOf", sample), Is.EqualTo(DataView.Prototype));

                sample = DataView.Construct(buffer, 4, Undefined);
                That(sample["byteLength"], Is.EqualTo(0), "sample.byteLength");
                That(sample["byteOffset"], Is.EqualTo(4), "sample.byteOffset");
                That(sample["buffer"], Is.EqualTo(buffer), "sample.buffer");
                That(sample["constructor"], Is.EqualTo(DataView));
                That(Object.Invoke("getPrototypeOf", sample), Is.EqualTo(DataView.Prototype));
            });

            It("should return new instance from defined length and offset", () => {
                EcmaValue sample;
                EcmaValue buffer = ArrayBuffer.Construct(3);

                sample = DataView.Construct(buffer, 1, 2);
                That(sample["byteLength"], Is.EqualTo(2), "sample.byteLength");
                That(sample["byteOffset"], Is.EqualTo(1), "sample.byteOffset");
                That(sample["buffer"], Is.EqualTo(buffer), "sample.buffer");
                That(sample["constructor"], Is.EqualTo(DataView));
                That(Object.Invoke("getPrototypeOf", sample), Is.EqualTo(DataView.Prototype));

                sample = DataView.Construct(buffer, 1, 0);
                That(sample["byteLength"], Is.EqualTo(0), "sample.byteLength");
                That(sample["byteOffset"], Is.EqualTo(1), "sample.byteOffset");
                That(sample["buffer"], Is.EqualTo(buffer), "sample.buffer");
                That(sample["constructor"], Is.EqualTo(DataView));
                That(Object.Invoke("getPrototypeOf", sample), Is.EqualTo(DataView.Prototype));

                sample = DataView.Construct(buffer, 0, 3);
                That(sample["byteLength"], Is.EqualTo(3), "sample.byteLength");
                That(sample["byteOffset"], Is.EqualTo(0), "sample.byteOffset");
                That(sample["buffer"], Is.EqualTo(buffer), "sample.buffer");
                That(sample["constructor"], Is.EqualTo(DataView));
                That(Object.Invoke("getPrototypeOf", sample), Is.EqualTo(DataView.Prototype));

                sample = DataView.Construct(buffer, 3, 0);
                That(sample["byteLength"], Is.EqualTo(0), "sample.byteLength");
                That(sample["byteOffset"], Is.EqualTo(3), "sample.byteOffset");
                That(sample["buffer"], Is.EqualTo(buffer), "sample.buffer");
                That(sample["constructor"], Is.EqualTo(DataView));
                That(Object.Invoke("getPrototypeOf", sample), Is.EqualTo(DataView.Prototype));

                sample = DataView.Construct(buffer, 0, 1);
                That(sample["byteLength"], Is.EqualTo(1), "sample.byteLength");
                That(sample["byteOffset"], Is.EqualTo(0), "sample.byteOffset");
                That(sample["buffer"], Is.EqualTo(buffer), "sample.buffer");
                That(sample["constructor"], Is.EqualTo(DataView));
                That(Object.Invoke("getPrototypeOf", sample), Is.EqualTo(DataView.Prototype));

                sample = DataView.Construct(buffer, 0, 2);
                That(sample["byteLength"], Is.EqualTo(2), "sample.byteLength");
                That(sample["byteOffset"], Is.EqualTo(0), "sample.byteOffset");
                That(sample["buffer"], Is.EqualTo(buffer), "sample.buffer");
                That(sample["constructor"], Is.EqualTo(DataView));
                That(Object.Invoke("getPrototypeOf", sample), Is.EqualTo(DataView.Prototype));

                buffer = SharedArrayBuffer.Construct(3);
                sample = DataView.Construct(buffer, 1, 2);
                That(sample["byteLength"], Is.EqualTo(2), "sample.byteLength");
                That(sample["byteOffset"], Is.EqualTo(1), "sample.byteOffset");
                That(sample["buffer"], Is.EqualTo(buffer), "sample.buffer");
                That(sample["constructor"], Is.EqualTo(DataView));
                That(Object.Invoke("getPrototypeOf", sample), Is.EqualTo(DataView.Prototype));

                sample = DataView.Construct(buffer, 1, 0);
                That(sample["byteLength"], Is.EqualTo(0), "sample.byteLength");
                That(sample["byteOffset"], Is.EqualTo(1), "sample.byteOffset");
                That(sample["buffer"], Is.EqualTo(buffer), "sample.buffer");
                That(sample["constructor"], Is.EqualTo(DataView));
                That(Object.Invoke("getPrototypeOf", sample), Is.EqualTo(DataView.Prototype));

                sample = DataView.Construct(buffer, 0, 3);
                That(sample["byteLength"], Is.EqualTo(3), "sample.byteLength");
                That(sample["byteOffset"], Is.EqualTo(0), "sample.byteOffset");
                That(sample["buffer"], Is.EqualTo(buffer), "sample.buffer");
                That(sample["constructor"], Is.EqualTo(DataView));
                That(Object.Invoke("getPrototypeOf", sample), Is.EqualTo(DataView.Prototype));

                sample = DataView.Construct(buffer, 3, 0);
                That(sample["byteLength"], Is.EqualTo(0), "sample.byteLength");
                That(sample["byteOffset"], Is.EqualTo(3), "sample.byteOffset");
                That(sample["buffer"], Is.EqualTo(buffer), "sample.buffer");
                That(sample["constructor"], Is.EqualTo(DataView));
                That(Object.Invoke("getPrototypeOf", sample), Is.EqualTo(DataView.Prototype));

                sample = DataView.Construct(buffer, 0, 1);
                That(sample["byteLength"], Is.EqualTo(1), "sample.byteLength");
                That(sample["byteOffset"], Is.EqualTo(0), "sample.byteOffset");
                That(sample["buffer"], Is.EqualTo(buffer), "sample.buffer");
                That(sample["constructor"], Is.EqualTo(DataView));
                That(Object.Invoke("getPrototypeOf", sample), Is.EqualTo(DataView.Prototype));

                sample = DataView.Construct(buffer, 0, 2);
                That(sample["byteLength"], Is.EqualTo(2), "sample.byteLength");
                That(sample["byteOffset"], Is.EqualTo(0), "sample.byteOffset");
                That(sample["buffer"], Is.EqualTo(buffer), "sample.buffer");
                That(sample["constructor"], Is.EqualTo(DataView));
                That(Object.Invoke("getPrototypeOf", sample), Is.EqualTo(DataView.Prototype));
            });

            It("should throw a RangeError if offset > bufferByteLength", () => {
                EcmaValue buffer = ArrayBuffer.Construct(1);
                That(() => DataView.Construct(buffer, 2), Throws.RangeError);
                That(() => DataView.Construct(buffer, Infinity), Throws.RangeError);

                EcmaValue sbuffer = SharedArrayBuffer.Construct(1);
                That(() => DataView.Construct(sbuffer, 2), Throws.RangeError);
                That(() => DataView.Construct(sbuffer, Infinity), Throws.RangeError);
            });

            It("should throw a RangeError if offset + viewByteLength > bufferByteLength", () => {
                EcmaValue buffer = ArrayBuffer.Construct(3);
                That(() => DataView.Construct(buffer, 0, 4), Throws.RangeError);
                That(() => DataView.Construct(buffer, 1, 3), Throws.RangeError);
                That(() => DataView.Construct(buffer, 2, 2), Throws.RangeError);
                That(() => DataView.Construct(buffer, 3, 1), Throws.RangeError);
                That(() => DataView.Construct(buffer, 4, 0), Throws.RangeError);
                That(() => DataView.Construct(buffer, 4, -1), Throws.RangeError);
                That(() => DataView.Construct(buffer, 4, -Infinity), Throws.RangeError);
                That(() => DataView.Construct(buffer, 0, Infinity), Throws.RangeError);

                EcmaValue sbuffer = SharedArrayBuffer.Construct(3);
                That(() => DataView.Construct(sbuffer, 0, 4), Throws.RangeError);
                That(() => DataView.Construct(sbuffer, 1, 3), Throws.RangeError);
                That(() => DataView.Construct(sbuffer, 2, 2), Throws.RangeError);
                That(() => DataView.Construct(sbuffer, 3, 1), Throws.RangeError);
                That(() => DataView.Construct(sbuffer, 4, 0), Throws.RangeError);
                That(() => DataView.Construct(sbuffer, 4, -1), Throws.RangeError);
                That(() => DataView.Construct(sbuffer, 4, -Infinity), Throws.RangeError);
                That(() => DataView.Construct(sbuffer, 0, Infinity), Throws.RangeError);
            });

            It("should throw a RangeError if ToInteger(byteOffset) < 0", () => {
                EcmaValue buffer = ArrayBuffer.Construct(2);
                That(() => DataView.Construct(buffer, -1), Throws.RangeError);
                That(() => DataView.Construct(buffer, -Infinity), Throws.RangeError);

                EcmaValue sbuffer = SharedArrayBuffer.Construct(2);
                That(() => DataView.Construct(sbuffer, -1), Throws.RangeError);
                That(() => DataView.Construct(sbuffer, -Infinity), Throws.RangeError);
            });

            It("should throw a RangeError if ToInteger(byteLength) < 0", () => {
                EcmaValue buffer = ArrayBuffer.Construct(2);
                That(() => DataView.Construct(buffer, 0, -1), Throws.RangeError);
                That(() => DataView.Construct(buffer, 0, -Infinity), Throws.RangeError);
                That(() => DataView.Construct(buffer, 1, -1), Throws.RangeError);
                That(() => DataView.Construct(buffer, 2, -Infinity), Throws.RangeError);

                EcmaValue sbuffer = SharedArrayBuffer.Construct(2);
                That(() => DataView.Construct(sbuffer, -1), Throws.RangeError);
                That(() => DataView.Construct(sbuffer, -Infinity), Throws.RangeError);
                That(() => DataView.Construct(sbuffer, 1, -1), Throws.RangeError);
                That(() => DataView.Construct(sbuffer, 2, -Infinity), Throws.RangeError);
            });

            It("should perform ToIndex conversions on byteOffset", () => {
                EcmaValue obj1 = CreateObject(valueOf: () => 3);
                EcmaValue obj2 = CreateObject(valueOf: () => 4);
                EcmaValue ab   = ArrayBuffer.Construct(42);
                EcmaValue sample;

                sample = DataView.Construct(ab, -0);
                That(sample["byteOffset"], Is.EqualTo(0), "-0");

                sample = DataView.Construct(ab, obj1);
                That(sample["byteOffset"], Is.EqualTo(3), "object's valueOf");

                sample = DataView.Construct(ab, obj2);
                That(sample["byteOffset"], Is.EqualTo(4), "object's toString");

                sample = DataView.Construct(ab, "");
                That(sample["byteOffset"], Is.EqualTo(0), "the Empty string");

                sample = DataView.Construct(ab, "0");
                That(sample["byteOffset"], Is.EqualTo(0), "string '0'");

                sample = DataView.Construct(ab, "1");
                That(sample["byteOffset"], Is.EqualTo(1), "string '1'");

                sample = DataView.Construct(ab, true);
                That(sample["byteOffset"], Is.EqualTo(1), "true");

                sample = DataView.Construct(ab, false);
                That(sample["byteOffset"], Is.EqualTo(0), "false");

                sample = DataView.Construct(ab, NaN);
                That(sample["byteOffset"], Is.EqualTo(0), "NaN");

                sample = DataView.Construct(ab, Null);
                That(sample["byteOffset"], Is.EqualTo(0), "null");

                sample = DataView.Construct(ab, Undefined);
                That(sample["byteOffset"], Is.EqualTo(0), "undefined");

                sample = DataView.Construct(ab, 0.1);
                That(sample["byteOffset"], Is.EqualTo(0), "0.1");

                sample = DataView.Construct(ab, 0.9);
                That(sample["byteOffset"], Is.EqualTo(0), "0.9");

                sample = DataView.Construct(ab, 1.1);
                That(sample["byteOffset"], Is.EqualTo(1), "1.1");

                sample = DataView.Construct(ab, 1.9);
                That(sample["byteOffset"], Is.EqualTo(1), "1.9");

                sample = DataView.Construct(ab, -0.1);
                That(sample["byteOffset"], Is.EqualTo(0), "-0.1");

                sample = DataView.Construct(ab, -0.99999);
                That(sample["byteOffset"], Is.EqualTo(0), "-0.99999");
            });

            It("should perform ToIndex conversions on byteLength", () => {
                EcmaValue obj1 = CreateObject(valueOf: () => 3);
                EcmaValue obj2 = CreateObject(valueOf: () => 4);
                EcmaValue ab   = ArrayBuffer.Construct(42);
                EcmaValue sample;

                sample = DataView.Construct(ab, 0, -0);
                That(sample["byteLength"], Is.EqualTo(0), "-0");

                sample = DataView.Construct(ab, 0, obj1);
                That(sample["byteLength"], Is.EqualTo(3), "object's valueOf");

                sample = DataView.Construct(ab, 0, obj2);
                That(sample["byteLength"], Is.EqualTo(4), "object's toString");

                sample = DataView.Construct(ab, 0, "");
                That(sample["byteLength"], Is.EqualTo(0), "the Empty string");

                sample = DataView.Construct(ab, 0, "0");
                That(sample["byteLength"], Is.EqualTo(0), "string '0'");

                sample = DataView.Construct(ab, 0, "1");
                That(sample["byteLength"], Is.EqualTo(1), "string '1'");

                sample = DataView.Construct(ab, 0, true);
                That(sample["byteLength"], Is.EqualTo(1), "true");

                sample = DataView.Construct(ab, 0, false);
                That(sample["byteLength"], Is.EqualTo(0), "false");

                sample = DataView.Construct(ab, 0, NaN);
                That(sample["byteLength"], Is.EqualTo(0), "NaN");

                sample = DataView.Construct(ab, 0, Null);
                That(sample["byteLength"], Is.EqualTo(0), "null");

                sample = DataView.Construct(ab, 0, 0.1);
                That(sample["byteLength"], Is.EqualTo(0), "0.1");

                sample = DataView.Construct(ab, 0, 0.9);
                That(sample["byteLength"], Is.EqualTo(0), "0.9");

                sample = DataView.Construct(ab, 0, 1.1);
                That(sample["byteLength"], Is.EqualTo(1), "1.1");

                sample = DataView.Construct(ab, 0, 1.9);
                That(sample["byteLength"], Is.EqualTo(1), "1.9");

                sample = DataView.Construct(ab, 0, -0.1);
                That(sample["byteLength"], Is.EqualTo(0), "-0.1");

                sample = DataView.Construct(ab, 0, -0.99999);
                That(sample["byteLength"], Is.EqualTo(0), "-0.99999");
            });

            It("should return abrupt from ToNumber(byteOffset)", () => {
                EcmaValue obj = CreateObject(valueOf: ThrowTest262Exception);
                EcmaValue ab  = ArrayBuffer.Construct(0);
                That(() => DataView.Construct(ab, obj), Throws.Test262);
                That(() => DataView.Construct(ab, new Symbol()), Throws.TypeError);
            });

            It("should return abrupt from ToNumber(byteLength)", () => {
                EcmaValue obj1 = CreateObject(valueOf: ThrowTest262Exception);
                EcmaValue obj2 = CreateObject(toString: ThrowTest262Exception);
                EcmaValue ab   = ArrayBuffer.Construct(0);
                That(() => DataView.Construct(ab, 0, obj1), Throws.Test262);
                That(() => DataView.Construct(ab, 0, obj2), Throws.Test262);
                That(() => DataView.Construct(ab, 0, new Symbol()), Throws.TypeError);
            });
        }
Ejemplo n.º 27
0
        public void Next(RuntimeFunction next)
        {
            IsUnconstructableFunctionWLength(next, "next", 0);

            It("should throw a TypeError if this value does not have all of the internal slots", () => {
                EcmaValue iterator = RegExp.Construct(".").Invoke(Symbol.MatchAll, "");
                That(() => iterator["next"].Call(Undefined), Throws.TypeError);
                That(() => iterator["next"].Call(Null), Throws.TypeError);
                That(() => iterator["next"].Call(1), Throws.TypeError);
                That(() => iterator["next"].Call(false), Throws.TypeError);
                That(() => iterator["next"].Call(""), Throws.TypeError);
                That(() => iterator["next"].Call(new Symbol()), Throws.TypeError);
                That(() => Object.Invoke("create", iterator).Invoke("next"), Throws.TypeError);
            });

            It("should iterate over matches", () => {
                EcmaValue iterator = RegExp.Construct("\\w").Invoke(Symbol.MatchAll, "*a*b*");
                VerifyIteratorResult(iterator.Invoke("next"), false, v => VerifyMatchObject(v, new[] { "a" }, 1, "*a*b*"));
                VerifyIteratorResult(iterator.Invoke("next"), true);

                iterator = RegExp.Construct("\\w", "g").Invoke(Symbol.MatchAll, "*a*b*");
                VerifyIteratorResult(iterator.Invoke("next"), false, v => VerifyMatchObject(v, new[] { "a" }, 1, "*a*b*"));
                VerifyIteratorResult(iterator.Invoke("next"), false, v => VerifyMatchObject(v, new[] { "b" }, 3, "*a*b*"));
                VerifyIteratorResult(iterator.Invoke("next"), true);
            });

            It("should not throw if exec is not callable", () => {
                foreach (EcmaValue value in new[] { Undefined, Null, 4, true, new Symbol() })
                {
                    using (TempProperty(RegExp.Prototype, "exec", value)) {
                        EcmaValue iterator = RegExp.Construct("\\w", "g").Invoke(Symbol.MatchAll, "*a*b*");
                        VerifyIteratorResult(iterator.Invoke("next"), false, v => VerifyMatchObject(v, new[] { "a" }, 1, "*a*b*"));
                        VerifyIteratorResult(iterator.Invoke("next"), false, v => VerifyMatchObject(v, new[] { "b" }, 3, "*a*b*"));
                        VerifyIteratorResult(iterator.Invoke("next"), true);
                    }
                }
            });

            It("should re-throw errors thrown coercing RegExp's lastIndex to a length", () => {
                EcmaValue iterator = RegExp.Construct(".", "g").Invoke(Symbol.MatchAll, "");
                using (TempProperty(RegExp.Prototype, "exec", FunctionLiteral(() =>
                                                                              Return(This.ToObject()["lastIndex"] = CreateObject(valueOf: ThrowTest262Exception), EcmaArray.Of(""))))) {
                    Case(iterator, Throws.Test262);
                }
            });

            It("should return abrupt from calling exec", () => {
                EcmaValue iterator = RegExp.Construct(".").Invoke(Symbol.MatchAll, "");
                using (TempProperty(RegExp.Prototype, "exec", ThrowTest262Exception)) {
                    Case(iterator, Throws.Test262);
                }
            });

            It("should return abrupt from getting exec", () => {
                EcmaValue iterator = RegExp.Construct(".").Invoke(Symbol.MatchAll, "");
                using (TempProperty(RegExp.Prototype, "exec", new EcmaPropertyDescriptor(ThrowTest262Exception, Null))) {
                    Case(iterator, Throws.Test262);
                }
            });

            It("should return abrupt from accessing the first match", () => {
                EcmaValue iterator = RegExp.Construct(".").Invoke(Symbol.MatchAll, "");
                using (TempProperty(RegExp.Prototype, "exec", FunctionLiteral(() => CreateObject((0, get: ThrowTest262Exception, set: null))))) {
                    Case(iterator, Throws.Test262);
                }
            });
Ejemplo n.º 28
0
        public void Constructor(RuntimeFunction ctor)
        {
            IsConstructorWLength(ctor, "SharedArrayBuffer", 1, SharedArrayBuffer.Prototype);
            That(GlobalThis, Has.OwnProperty("SharedArrayBuffer", ctor, EcmaPropertyAttributes.Writable | EcmaPropertyAttributes.Configurable));

            It("should derive [[Prototype]] value from realm of newTarget", () => {
                RuntimeRealm realm = new RuntimeRealm();
                EcmaValue fn       = realm.GetRuntimeObject(WellKnownObject.FunctionConstructor).Construct();
                fn["prototype"]    = Null;
                EcmaValue other    = Reflect.Invoke("construct", ctor, EcmaArray.Of(Noop), fn);
                That(Object.Invoke("getPrototypeOf", other), Is.EqualTo(realm.GetRuntimeObject(WellKnownObject.SharedArrayBufferPrototype)));
            });

            It("should derive [[Prototype]] internal slot from NewTarget", () => {
                EcmaValue arrayBuffer = Reflect.Invoke("construct", SharedArrayBuffer, EcmaArray.Of(8), Object);
                That(Object.Invoke("getPrototypeOf", arrayBuffer), Is.EqualTo(Object.Prototype));

                EcmaValue newTarget = FunctionLiteral(Noop).Invoke("bind", Null);
                Object.Invoke("defineProperty", newTarget, "prototype", CreateObject(new { get = FunctionLiteral(() => Array.Prototype) }));
                arrayBuffer = Reflect.Invoke("construct", SharedArrayBuffer, EcmaArray.Of(16), newTarget);
                That(Object.Invoke("getPrototypeOf", arrayBuffer), Is.EqualTo(Array.Prototype));
            });

            It("should use %SharedArrayBufferPrototype% if NewTarget.prototype is not an object", () => {
                EcmaValue arrayBuffer;
                EcmaValue newTarget    = FunctionLiteral(Noop);
                newTarget["prototype"] = Undefined;
                arrayBuffer            = Reflect.Invoke("construct", SharedArrayBuffer, EcmaArray.Of(1), newTarget);
                That(Object.Invoke("getPrototypeOf", arrayBuffer), Is.EqualTo(SharedArrayBuffer.Prototype), "newTarget.prototype is undefined");

                newTarget["prototype"] = Null;
                arrayBuffer            = Reflect.Invoke("construct", SharedArrayBuffer, EcmaArray.Of(2), newTarget);
                That(Object.Invoke("getPrototypeOf", arrayBuffer), Is.EqualTo(SharedArrayBuffer.Prototype), "newTarget.prototype is null");

                newTarget["prototype"] = true;
                arrayBuffer            = Reflect.Invoke("construct", SharedArrayBuffer, EcmaArray.Of(3), newTarget);
                That(Object.Invoke("getPrototypeOf", arrayBuffer), Is.EqualTo(SharedArrayBuffer.Prototype), "newTarget.prototype is a Boolean");

                newTarget["prototype"] = "";
                arrayBuffer            = Reflect.Invoke("construct", SharedArrayBuffer, EcmaArray.Of(4), newTarget);
                That(Object.Invoke("getPrototypeOf", arrayBuffer), Is.EqualTo(SharedArrayBuffer.Prototype), "newTarget.prototype is a String");

                newTarget["prototype"] = new Symbol();
                arrayBuffer            = Reflect.Invoke("construct", SharedArrayBuffer, EcmaArray.Of(5), newTarget);
                That(Object.Invoke("getPrototypeOf", arrayBuffer), Is.EqualTo(SharedArrayBuffer.Prototype), "newTarget.prototype is a Symbol");

                newTarget["prototype"] = 1;
                arrayBuffer            = Reflect.Invoke("construct", SharedArrayBuffer, EcmaArray.Of(6), newTarget);
                That(Object.Invoke("getPrototypeOf", arrayBuffer), Is.EqualTo(SharedArrayBuffer.Prototype), "newTarget.prototype is a Number");
            });

            It("should throw a TypeError if ArrayBuffer is called as a function", () => {
                That(() => SharedArrayBuffer.Call(Undefined), Throws.TypeError);
                That(() => SharedArrayBuffer.Call(Undefined, 42), Throws.TypeError);
            });

            It("should convert the `length` parameter to a value numeric index value", () => {
                EcmaValue obj1 = CreateObject(valueOf: () => 42);
                EcmaValue obj2 = CreateObject(toString: () => 42);
                That(SharedArrayBuffer.Construct(obj1)["byteLength"], Is.EqualTo(42), "object's valueOf");
                That(SharedArrayBuffer.Construct(obj2)["byteLength"], Is.EqualTo(42), "object's toString");
                That(SharedArrayBuffer.Construct("")["byteLength"], Is.EqualTo(0), "the Empty string");
                That(SharedArrayBuffer.Construct("0")["byteLength"], Is.EqualTo(0), "string '0'");
                That(SharedArrayBuffer.Construct("1")["byteLength"], Is.EqualTo(1), "string '1'");
                That(SharedArrayBuffer.Construct(true)["byteLength"], Is.EqualTo(1), "true");
                That(SharedArrayBuffer.Construct(false)["byteLength"], Is.EqualTo(0), "false");
                That(SharedArrayBuffer.Construct(NaN)["byteLength"], Is.EqualTo(0), "NaN");
                That(SharedArrayBuffer.Construct(Null)["byteLength"], Is.EqualTo(0), "null");
                That(SharedArrayBuffer.Construct(Undefined)["byteLength"], Is.EqualTo(0), "undefined");
                That(SharedArrayBuffer.Construct(0.1)["byteLength"], Is.EqualTo(0), "0.1");
                That(SharedArrayBuffer.Construct(0.9)["byteLength"], Is.EqualTo(0), "0.9");
                That(SharedArrayBuffer.Construct(1.1)["byteLength"], Is.EqualTo(1), "1.1");
                That(SharedArrayBuffer.Construct(1.9)["byteLength"], Is.EqualTo(1), "1.9");
                That(SharedArrayBuffer.Construct(-0.1)["byteLength"], Is.EqualTo(0), "-0.1");
                That(SharedArrayBuffer.Construct(-0.99999)["byteLength"], Is.EqualTo(0), "-0.99999");
            });

            It("should return an empty instance if length is absent", () => {
                That(SharedArrayBuffer.Construct()["byteLength"], Is.EqualTo(0));
            });

            It("should accept zero as the `length` parameter", () => {
                That(SharedArrayBuffer.Construct(0)["byteLength"], Is.EqualTo(0));
                That(SharedArrayBuffer.Construct(-0d)["byteLength"], Is.EqualTo(0));
            });

            It("should throw a RangeError if length represents an integer < 0", () => {
                That(() => SharedArrayBuffer.Construct(-1), Throws.RangeError);
                That(() => SharedArrayBuffer.Construct(-1.1), Throws.RangeError);
                That(() => SharedArrayBuffer.Construct(-Infinity), Throws.RangeError);
            });

            It("should throw a RangeError if requested Data Block is too large", () => {
                That(() => SharedArrayBuffer.Construct(9007199254740992), Throws.RangeError);
                That(() => SharedArrayBuffer.Construct(Infinity), Throws.RangeError);
            });

            It("should return abrupt from ToIndex(length)", () => {
                That(() => SharedArrayBuffer.Construct(new Symbol()), Throws.TypeError);
                That(() => SharedArrayBuffer.Construct(CreateObject(valueOf: ThrowTest262Exception)), Throws.Test262);
            });

            It("should create the new ArrayBuffer instance prior to allocating the Data Block", () => {
                EcmaValue newTarget = FunctionLiteral(Noop).Invoke("bind", Null);
                Object.Invoke("defineProperty", newTarget, "prototype", CreateObject(new { get = ThrowTest262Exception }));
                That(() => Reflect.Invoke("construct", SharedArrayBuffer, EcmaArray.Of(9007199254740992), newTarget), Throws.Test262);
            });

            It("should initialize all bytes to zero", () => {
                EcmaValue view = DataView.Construct(SharedArrayBuffer.Construct(9));
                That(view.Invoke("getUint8", 0), Is.EqualTo(0));
                That(view.Invoke("getUint8", 1), Is.EqualTo(0));
                That(view.Invoke("getUint8", 2), Is.EqualTo(0));
                That(view.Invoke("getUint8", 3), Is.EqualTo(0));
                That(view.Invoke("getUint8", 4), Is.EqualTo(0));
                That(view.Invoke("getUint8", 5), Is.EqualTo(0));
                That(view.Invoke("getUint8", 6), Is.EqualTo(0));
                That(view.Invoke("getUint8", 7), Is.EqualTo(0));
                That(view.Invoke("getUint8", 8), Is.EqualTo(0));
            });
        }
Ejemplo n.º 29
0
        public void Slice(RuntimeFunction slice)
        {
            IsUnconstructableFunctionWLength(slice, "slice", 2);
            That(SharedArrayBuffer.Prototype, Has.OwnProperty("slice", slice, EcmaPropertyAttributes.DefaultMethodProperty));

            It("should throw a TypeError when this is not an object", () => {
                Case(Undefined, Throws.TypeError);
                Case(Null, Throws.TypeError);
                Case(false, Throws.TypeError);
                Case(0, Throws.TypeError);
                Case("", Throws.TypeError);
                Case(new Symbol(), Throws.TypeError);
            });

            It("should throw a TypeError exception when `this` does not have a [[ArrayBufferData]]", () => {
                Case(SharedArrayBuffer.Prototype, Throws.TypeError);
                Case(Object.Construct(), Throws.TypeError);
                Case(EcmaArray.Of(), Throws.TypeError);
                Case(Global.Int8Array.Construct(8), Throws.TypeError);
                Case(Global.DataView.Construct(SharedArrayBuffer.Construct(8), 0), Throws.TypeError);
            });

            It("should throw a TypeError if `this` is a ArrayBuffer", () => {
                Case(Global.ArrayBuffer.Construct(0), Throws.TypeError);
            });

            It("should default the `start` index to 0 if absent", () => {
                EcmaValue arrayBuffer = SharedArrayBuffer.Construct(8);
                That(slice.Call(arrayBuffer)["byteLength"], Is.EqualTo(8));
                That(slice.Call(arrayBuffer, Undefined, 6)["byteLength"], Is.EqualTo(6));
            });

            It("should return zero-length buffer if `start` index exceeds `end` index", () => {
                EcmaValue arrayBuffer = SharedArrayBuffer.Construct(8);
                That(slice.Call(arrayBuffer, 5, 4)["byteLength"], Is.EqualTo(0));
            });

            It("should clamp large `start` index  to [[ArrayBufferByteLength]]", () => {
                EcmaValue arrayBuffer = SharedArrayBuffer.Construct(8);
                That(slice.Call(arrayBuffer, 10, 8)["byteLength"], Is.EqualTo(0));
                That(slice.Call(arrayBuffer, 0x100000000, 7)["byteLength"], Is.EqualTo(0));
                That(slice.Call(arrayBuffer, Infinity, 6)["byteLength"], Is.EqualTo(0));
            });

            It("should normalize negative `start` index relative to [[ArrayBufferByteLength]]", () => {
                EcmaValue arrayBuffer = SharedArrayBuffer.Construct(8);
                That(slice.Call(arrayBuffer, -5, 6)["byteLength"], Is.EqualTo(3));
                That(slice.Call(arrayBuffer, -12, 6)["byteLength"], Is.EqualTo(6));
                That(slice.Call(arrayBuffer, -Infinity, 6)["byteLength"], Is.EqualTo(6));
            });

            It("should convert the `start` index parameter to an integral numeric value", () => {
                EcmaValue arrayBuffer = SharedArrayBuffer.Construct(8);
                That(slice.Call(arrayBuffer, 4.5, 8)["byteLength"], Is.EqualTo(4));
                That(slice.Call(arrayBuffer, NaN, 8)["byteLength"], Is.EqualTo(8));
            });

            It("should default the `end` index to [[ArrayBufferByteLength]] if absent", () => {
                EcmaValue arrayBuffer = SharedArrayBuffer.Construct(8);
                That(slice.Call(arrayBuffer, 6)["byteLength"], Is.EqualTo(2));
                That(slice.Call(arrayBuffer, 6, Undefined)["byteLength"], Is.EqualTo(2));
            });

            It("should clamp large `end` index  to [[ArrayBufferByteLength]]", () => {
                EcmaValue arrayBuffer = SharedArrayBuffer.Construct(8);
                That(slice.Call(arrayBuffer, 1, 12)["byteLength"], Is.EqualTo(7));
                That(slice.Call(arrayBuffer, 2, 0x100000000)["byteLength"], Is.EqualTo(6));
                That(slice.Call(arrayBuffer, 3, Infinity)["byteLength"], Is.EqualTo(5));
            });

            It("should normalize negative `end` index relative to [[ArrayBufferByteLength]]", () => {
                EcmaValue arrayBuffer = SharedArrayBuffer.Construct(8);
                That(slice.Call(arrayBuffer, 2, -4)["byteLength"], Is.EqualTo(2));
                That(slice.Call(arrayBuffer, 2, -10)["byteLength"], Is.EqualTo(0));
                That(slice.Call(arrayBuffer, 2, -Infinity)["byteLength"], Is.EqualTo(0));
            });

            It("should convert the `end` index parameter to an integral numeric value", () => {
                EcmaValue arrayBuffer = SharedArrayBuffer.Construct(8);
                That(slice.Call(arrayBuffer, 0, 4.5)["byteLength"], Is.EqualTo(4));
                That(slice.Call(arrayBuffer, 0, NaN)["byteLength"], Is.EqualTo(0));
            });

            It("should convert start and end index in correct order", () => {
                slice.Call(SharedArrayBuffer.Construct(8),
                           CreateObject(new { valueOf = Intercept(() => 0, "start") }),
                           CreateObject(new { valueOf = Intercept(() => 0, "end") }));
                That(Logs, Is.EquivalentTo(new[] { "start", "end" }));
            });

            It("should throw a TypeError if `constructor` property is not an object", () => {
                EcmaValue arrayBuffer = SharedArrayBuffer.Construct(8);
                Case(Object.Invoke("assign", arrayBuffer, CreateObject(new { constructor = Null })), Throws.TypeError);
                Case(Object.Invoke("assign", arrayBuffer, CreateObject(new { constructor = true })), Throws.TypeError);
                Case(Object.Invoke("assign", arrayBuffer, CreateObject(new { constructor = "" })), Throws.TypeError);
                Case(Object.Invoke("assign", arrayBuffer, CreateObject(new { constructor = 1 })), Throws.TypeError);
                Case(Object.Invoke("assign", arrayBuffer, CreateObject(new { constructor = new Symbol() })), Throws.TypeError);
            });

            It("should throw a TypeError if species constructor is not a constructor", () => {
                EcmaValue arrayBuffer        = SharedArrayBuffer.Construct(8);
                EcmaValue speciesConstructor = Object.Construct();
                arrayBuffer["constructor"]   = speciesConstructor;

                speciesConstructor[Symbol.Species] = Object.Construct();
                Case(arrayBuffer, Throws.TypeError);
                speciesConstructor[Symbol.Species] = Function.Prototype;
                Case(arrayBuffer, Throws.TypeError);
            });

            It("should throw a TypeError if species constructor is not an object", () => {
                EcmaValue arrayBuffer        = SharedArrayBuffer.Construct(8);
                EcmaValue speciesConstructor = Object.Construct();
                arrayBuffer["constructor"]   = speciesConstructor;

                speciesConstructor[Symbol.Species] = true;
                Case(arrayBuffer, Throws.TypeError);
                speciesConstructor[Symbol.Species] = "";
                Case(arrayBuffer, Throws.TypeError);
                speciesConstructor[Symbol.Species] = 1;
                Case(arrayBuffer, Throws.TypeError);
                speciesConstructor[Symbol.Species] = new Symbol();
                Case(arrayBuffer, Throws.TypeError);
            });

            It("should use default constructor is `constructor` property is undefined", () => {
                EcmaValue arrayBuffer      = SharedArrayBuffer.Construct(8);
                arrayBuffer["constructor"] = Undefined;
                That(Object.Invoke("getPrototypeOf", slice.Call(arrayBuffer)), Is.EqualTo(SharedArrayBuffer.Prototype));
            });

            It("should use default constructor if species constructor is null or undefined", () => {
                EcmaValue arrayBuffer        = SharedArrayBuffer.Construct(8);
                EcmaValue speciesConstructor = Object.Construct();
                arrayBuffer["constructor"]   = speciesConstructor;

                speciesConstructor[Symbol.Species] = Null;
                That(Object.Invoke("getPrototypeOf", slice.Call(arrayBuffer)), Is.EqualTo(SharedArrayBuffer.Prototype));
                speciesConstructor[Symbol.Species] = Undefined;
                That(Object.Invoke("getPrototypeOf", slice.Call(arrayBuffer)), Is.EqualTo(SharedArrayBuffer.Prototype));
            });

            It("does not throw TypeError if new SharedArrayBuffer is too large", () => {
                EcmaValue speciesConstructor       = Object.Construct();
                speciesConstructor[Symbol.Species] = FunctionLiteral((length) => {
                    return(SharedArrayBuffer.Construct(10));
                });
                EcmaValue arrayBuffer      = SharedArrayBuffer.Construct(8);
                arrayBuffer["constructor"] = speciesConstructor;
                That(slice.Call(arrayBuffer)["byteLength"], Is.EqualTo(10));
            });

            It("should throw a TypeError if new SharedArrayBuffer is too small", () => {
                EcmaValue speciesConstructor       = Object.Construct();
                speciesConstructor[Symbol.Species] = FunctionLiteral((length) => {
                    return(SharedArrayBuffer.Construct(4));
                });
                EcmaValue arrayBuffer      = SharedArrayBuffer.Construct(8);
                arrayBuffer["constructor"] = speciesConstructor;
                Case(arrayBuffer, Throws.TypeError);
            });

            It("should throw a TypeError if new object is not an SharedArrayBuffer instance", () => {
                EcmaValue speciesConstructor       = Object.Construct();
                speciesConstructor[Symbol.Species] = FunctionLiteral((length) => {
                    return(Object.Construct());
                });
                EcmaValue arrayBuffer      = SharedArrayBuffer.Construct(8);
                arrayBuffer["constructor"] = speciesConstructor;
                Case(arrayBuffer, Throws.TypeError);
            });

            It("should throw a TypeError if species constructor returns `this` value", () => {
                EcmaValue arrayBuffer              = SharedArrayBuffer.Construct(8);
                EcmaValue speciesConstructor       = Object.Construct();
                speciesConstructor[Symbol.Species] = FunctionLiteral((length) => {
                    return(arrayBuffer);
                });
                arrayBuffer["constructor"] = speciesConstructor;
                Case(arrayBuffer, Throws.TypeError);
            });

            It("should create new SharedArrayBuffer instance from SpeciesConstructor", () => {
                EcmaValue resultBuffer             = default;
                EcmaValue speciesConstructor       = Object.Construct();
                speciesConstructor[Symbol.Species] = FunctionLiteral((length) => {
                    return(resultBuffer = SharedArrayBuffer.Construct(length));
                });
                EcmaValue arrayBuffer      = SharedArrayBuffer.Construct(8);
                arrayBuffer["constructor"] = speciesConstructor;
                EcmaValue result           = slice.Call(arrayBuffer);
                That(result, Is.EqualTo(resultBuffer));
            });
        }
Ejemplo n.º 30
0
 public EcmaValue ToValue()
 {
     return(EcmaArray.Of(this.Key.ToValue(), this.Value));
 }