Esempio n. 1
0
        public void Properties()
        {
            EcmaValue iterator = RegExp.Construct(".").Invoke(Symbol.MatchAll, "");
            EcmaValue proto    = Object.Invoke("getPrototypeOf", iterator);

            That(proto, Has.OwnProperty(WellKnownSymbol.ToStringTag, "RegExp String Iterator", EcmaPropertyAttributes.Configurable));
        }
Esempio n. 2
0
        public void Properties()
        {
            EcmaValue iterator = Map.Construct().Invoke(Symbol.Iterator);
            EcmaValue proto    = Object.Invoke("getPrototypeOf", iterator);

            That(proto, Has.OwnProperty(WellKnownSymbol.ToStringTag, "Map Iterator", EcmaPropertyAttributes.Configurable));
        }
Esempio n. 3
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);
            });
        }
Esempio n. 4
0
        public void Instance()
        {
            It("should support indexing notation to look up non numeric property names", () => {
                EcmaValue str = "hello world";
                That(str[0], Is.EqualTo("h"));
                That(str["foo"], Is.Undefined);
                That(str[-1], Is.Undefined);
                That(str[11], Is.Undefined);
                That(str[NaN], Is.Undefined);
                That(str[Infinity], Is.Undefined);

                EcmaValue strObj = String.Construct(str);
                strObj["foo"]    = 1;
                That(strObj["foo"], Is.EqualTo(1));

                EcmaValue strObj2 = String.Construct(str);
                That(strObj2["foo"], Is.Undefined);
            });

            It("should have the \"length\" property", () => {
                That(String.Construct(""), Has.OwnProperty("length", 0, EcmaPropertyAttributes.None));
                That(String.Construct(" "), Has.OwnProperty("length", 1, EcmaPropertyAttributes.None));
                That(String.Construct(" \b "), Has.OwnProperty("length", 3, EcmaPropertyAttributes.None));
                That(String.Construct("\ud834\udf06"), Has.OwnProperty("length", 2, EcmaPropertyAttributes.None));
            });

            It("should have property descriptor for numeric own properties of an exotic String object", () => {
                EcmaValue str = String.Construct("abc");
                That(str, Has.OwnProperty("0", "a", EcmaPropertyAttributes.Enumerable));
                That(str, Has.OwnProperty("1", "b", EcmaPropertyAttributes.Enumerable));
                That(str, Has.OwnProperty("2", "c", EcmaPropertyAttributes.Enumerable));
            });
        }
Esempio n. 5
0
 public void Properties()
 {
     That(BigInt, Has.OwnProperty("prototype", BigInt.Prototype, EcmaPropertyAttributes.None));
     That(BigInt.Prototype, Has.OwnProperty("constructor", BigInt, EcmaPropertyAttributes.Writable | EcmaPropertyAttributes.Configurable));
     That(BigInt.Prototype, Has.OwnProperty(WellKnownSymbol.ToStringTag, "BigInt", EcmaPropertyAttributes.Configurable));
     That(BigInt.Prototype.GetPrototypeOf(), Is.EqualTo(Object.Prototype));
 }
Esempio n. 6
0
 public void Properties()
 {
     That(Object.Invoke("getPrototypeOf", Reflect), Is.EqualTo(Object.Prototype));
     That(GlobalThis, Has.OwnProperty("Reflect", Reflect, EcmaPropertyAttributes.Writable | EcmaPropertyAttributes.Configurable));
     That(() => Reflect.Call(), Throws.TypeError);
     That(() => Reflect.Construct(), Throws.TypeError);
 }
Esempio n. 7
0
 public void Properties()
 {
     That(GlobalThis, Has.OwnProperty("JSON", Global.Json, EcmaPropertyAttributes.Writable | EcmaPropertyAttributes.Configurable));
     That(Global.Json, Has.OwnProperty(WellKnownSymbol.ToStringTag, "JSON", EcmaPropertyAttributes.Configurable));
     That(Global.Json, Is.TypeOf("object"));
     That(() => Global.Json.Call(This), Throws.TypeError);
     That(() => Global.Json.Construct(), Throws.TypeError);
 }
Esempio n. 8
0
 public void Properties()
 {
     That(SharedArrayBuffer, Has.OwnProperty("prototype", SharedArrayBuffer.Prototype, EcmaPropertyAttributes.None));
     That(SharedArrayBuffer.Prototype, Has.OwnProperty("constructor", SharedArrayBuffer, EcmaPropertyAttributes.Writable | EcmaPropertyAttributes.Configurable));
     That(SharedArrayBuffer.Prototype, Has.OwnProperty(WellKnownSymbol.ToStringTag, "SharedArrayBuffer", EcmaPropertyAttributes.Configurable));
     That(SharedArrayBuffer.Prototype.GetPrototypeOf(), Is.EqualTo(Object.Prototype));
     That(Object.Prototype.Get("toString").Call(SharedArrayBuffer.Prototype), Is.EqualTo("[object SharedArrayBuffer]"));
 }
Esempio n. 9
0
        public void Properties()
        {
            That(Symbol.Prototype, Has.OwnProperty("constructor", Symbol, EcmaPropertyAttributes.Writable | EcmaPropertyAttributes.Configurable));
            That(Symbol.Prototype.GetPrototypeOf(), Is.EqualTo(Object.Prototype));
            That(Object.Prototype.Get("toString").Call(Symbol.Prototype), Is.EqualTo("[object Symbol]"), "Symbol prototype object: its [[Class]] must be 'Symbol'");

            That(Symbol.Prototype, Has.OwnProperty(WellKnownSymbol.ToStringTag, "Symbol", EcmaPropertyAttributes.Configurable));
        }
Esempio n. 10
0
        public void Properties()
        {
            That(Promise, Has.OwnProperty("prototype", Promise.Prototype, EcmaPropertyAttributes.None));
            That(Promise.Prototype, Has.OwnProperty("constructor", Promise, EcmaPropertyAttributes.Writable | EcmaPropertyAttributes.Configurable));
            That(Promise.Prototype, Has.OwnProperty(WellKnownSymbol.ToStringTag, "Promise", EcmaPropertyAttributes.Configurable));
            That(Promise.Prototype.GetPrototypeOf(), Is.EqualTo(Object.Prototype));
            That(Object.Prototype.Get("toString").Call(Promise.Prototype), Is.EqualTo("[object Promise]"));

            That(() => Promise.Prototype["then"].Call(Promise.Prototype, Noop, Noop), Throws.TypeError, "Promise.prototype does not have a [[PromiseState]] internal slot");
        }
Esempio n. 11
0
        public void Properties()
        {
            EcmaValue AsyncFunction          = FunctionLiteral(async() => { })["constructor"];
            EcmaValue AsyncFunctionPrototype = AsyncFunction["prototype"];

            That(Object.Invoke("isExtensible", AsyncFunctionPrototype), Is.True, "The initial value of the [[Extensible]] internal slot of the AsyncFunction prototype object is true");
            That(Object.Invoke("getPrototypeOf", AsyncFunctionPrototype), Is.EqualTo(Function.Prototype));
            That(AsyncFunctionPrototype, Has.OwnProperty(Symbol.ToStringTag, "AsyncFunction", EcmaPropertyAttributes.Configurable));
            That(AsyncFunctionPrototype, Has.OwnProperty("constructor", AsyncFunction, EcmaPropertyAttributes.Configurable));
            That(AsyncFunction, Has.OwnProperty("prototype", AsyncFunctionPrototype, EcmaPropertyAttributes.None));
        }
Esempio n. 12
0
        public void Apply(RuntimeFunction apply)
        {
            IsUnconstructableFunctionWLength(apply, "apply", 3);
            That(Reflect, Has.OwnProperty("apply", apply, EcmaPropertyAttributes.DefaultMethodProperty));

            It("should return abrupt if argumentsList is not an ArrayLike object", () => {
                EcmaValue fn = Function.Construct();
                EcmaValue o  = CreateObject(("length", get: ThrowTest262Exception, set: null));
                Case((_, fn, 1, o), Throws.Test262);
                Case((_, fn, 1, 1), Throws.TypeError);
            });
Esempio n. 13
0
        public void Properties()
        {
            EcmaValue GeneratorFunctionPrototype = Object.Invoke("getPrototypeOf", new GeneratorFunction(EmptyGenerator));
            EcmaValue GeneratorFunction          = GeneratorFunctionPrototype["constructor"];

            That(Object.Invoke("isExtensible", GeneratorFunctionPrototype), Is.True, "The initial value of the [[Extensible]] internal slot of the GeneratorFunction prototype object is true");
            That(GeneratorFunctionPrototype, Has.OwnProperty(Symbol.ToStringTag, "GeneratorFunction", EcmaPropertyAttributes.Configurable));
            That(GeneratorFunctionPrototype, Has.OwnProperty("prototype", (RuntimeObject)WellKnownObject.GeneratorPrototype, EcmaPropertyAttributes.Configurable));
            That(GeneratorFunctionPrototype, Has.OwnProperty("constructor", GeneratorFunction, EcmaPropertyAttributes.Configurable));
            That(GeneratorFunction, Has.OwnProperty("prototype", GeneratorFunctionPrototype, EcmaPropertyAttributes.None));
        }
Esempio n. 14
0
        public void Properties()
        {
            RuntimeRealm realm   = new RuntimeRealm();
            EcmaValue    Symbol  = Global.Symbol;
            EcmaValue    OSymbol = realm.GetRuntimeObject(WellKnownObject.SymbolConstructor);

            foreach (var i in new[] { "asyncIterator", "hasInstance", "isConcatSpreadable", "iterator", "match", "matchAll", "replace", "search", "species", "split", "toPrimitive", "toStringTag", "unscopables" })
            {
                That(Symbol, Has.OwnProperty(i, EcmaPropertyAttributes.None), "Symbol.{0} descriptor", i);
                That(Symbol[i], Is.EqualTo(OSymbol[i]), "well-known symbols values are shared by all realms");
            }
        }
Esempio n. 15
0
        public void Species(RuntimeFunction species)
        {
            That(SharedArrayBuffer, Has.OwnProperty(WellKnownSymbol.Species, EcmaPropertyAttributes.Configurable));
            That(SharedArrayBuffer.GetOwnProperty(WellKnownSymbol.Species).Set, Is.Undefined);

            IsUnconstructableFunctionWLength(species, "get [Symbol.species]", 0);
            Case(SharedArrayBuffer, Is.EqualTo(SharedArrayBuffer));

            EcmaValue thisValue = new EcmaObject();

            Case(thisValue, Is.EqualTo(thisValue));
        }
Esempio n. 16
0
        public void Iterator(RuntimeFunction iterator)
        {
            IsUnconstructableFunctionWLength(iterator, "[Symbol.iterator]", 0);

            EcmaValue iterProto = Object.Invoke("getPrototypeOf", Object.Invoke("getPrototypeOf", Array.Construct().Invoke(Symbol.Iterator)));

            That(iterProto, Has.OwnProperty(Symbol.Iterator, iterator, EcmaPropertyAttributes.DefaultMethodProperty));

            EcmaValue thisValue = Object.Construct();

            Case(thisValue, thisValue);
        }
Esempio n. 17
0
        public void AsyncIterator(RuntimeFunction asyncIterator)
        {
            IsUnconstructableFunctionWLength(asyncIterator, "[Symbol.asyncIterator]", 0);

            AsyncGeneratorFunction generator = new AsyncGeneratorFunction(EmptyGenerator);
            EcmaValue iterProto = Object.Invoke("getPrototypeOf", Object.Invoke("getPrototypeOf", generator.Prototype));

            That(iterProto, Has.OwnProperty(Symbol.AsyncIterator, asyncIterator, EcmaPropertyAttributes.DefaultMethodProperty));

            EcmaValue thisValue = Object.Construct();

            Case(thisValue, thisValue);
        }
Esempio n. 18
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)));
                });
            }
        }
Esempio n. 19
0
        public void Constructor(RuntimeFunction ctor)
        {
            IsConstructorWLength(ctor, "Proxy", 2, null);
            That(GlobalThis, Has.OwnProperty("Proxy", ctor, EcmaPropertyAttributes.DefaultMethodProperty));

            That(TypeOf(ctor.Construct(Object.Construct(), Object.Construct())), Is.EqualTo("object"));
            That(() => ctor.Call(Object.Construct(), Object.Construct()), Throws.TypeError);

            EcmaValue revocable = Proxy.Invoke("revocable", Object.Construct(), Object.Construct());

            revocable.Invoke("revoke");

            That(() => ctor.Construct(Object.Construct(), revocable["proxy"]), Throws.TypeError);
            That(() => ctor.Construct(Object.Construct(), Undefined), Throws.TypeError);
            That(() => ctor.Construct(Object.Construct(), Null), Throws.TypeError);
            That(() => ctor.Construct(Object.Construct(), 0), Throws.TypeError);
            That(() => ctor.Construct(Object.Construct(), false), Throws.TypeError);
            That(() => ctor.Construct(Object.Construct(), ""), Throws.TypeError);
            That(() => ctor.Construct(Object.Construct(), new Symbol()), Throws.TypeError);

            That(() => ctor.Construct(revocable["proxy"], Object.Construct()), Throws.TypeError);
            That(() => ctor.Construct(Undefined, Object.Construct()), Throws.TypeError);
            That(() => ctor.Construct(Null, Object.Construct()), Throws.TypeError);
            That(() => ctor.Construct(0, Object.Construct()), Throws.TypeError);
            That(() => ctor.Construct(false, Object.Construct()), Throws.TypeError);
            That(() => ctor.Construct("", Object.Construct()), Throws.TypeError);
            That(() => ctor.Construct(new Symbol(), Object.Construct()), Throws.TypeError);

            // A Proxy exotic object is only callable if the given target is callable.
            EcmaValue p1 = Proxy.Construct(Object.Construct(), Object.Construct());

            That(() => p1.Call(_), Throws.TypeError);

            // A Proxy exotic object only accepts a constructor call if target is constructor.
            EcmaValue p2 = Proxy.Construct(GlobalThis["parseInt"], Object.Construct());

            That(() => p2.Call(_), Throws.Nothing);
            That(() => p2.Construct(_), Throws.TypeError);

            // The realm of a proxy exotic object is the realm of its target function
            RuntimeRealm realm = new RuntimeRealm();
            EcmaValue    C     = realm.GetRuntimeObject(WellKnownObject.FunctionConstructor).Construct();

            C["prototype"] = Null;
            EcmaValue P = Proxy.Construct(C, Object.Construct());

            That(Object.Invoke("getPrototypeOf", P.Construct()), Is.EqualTo(realm.GetRuntimeObject(WellKnownObject.ObjectPrototype)), "The realm of a proxy exotic object is the realm of its target function");

            P = Proxy.Construct(P, Object.Construct());
            That(Object.Invoke("getPrototypeOf", P.Construct()), Is.EqualTo(realm.GetRuntimeObject(WellKnownObject.ObjectPrototype)), "GetFunctionRealm is called recursively");
        }
Esempio n. 20
0
        public void ToPrimitive(RuntimeFunction toPrimitive)
        {
            IsUnconstructableFunctionWLength(toPrimitive, "[Symbol.toPrimitive]", 1);
            That(Symbol.Prototype, Has.OwnProperty(WellKnownSymbol.ToPrimitive, EcmaPropertyAttributes.Configurable));

            Case(Symbol["toPrimitive"], Symbol["toPrimitive"]);
            Case(Object.Call(Object, Symbol["toPrimitive"]), Symbol["toPrimitive"]);

            Case(Undefined, Throws.TypeError);
            Case(Null, Throws.TypeError);
            Case(123, Throws.TypeError);
            Case("test", Throws.TypeError);
            Case(true, Throws.TypeError);
            Case(Object.Construct(), Throws.TypeError);
        }
Esempio n. 21
0
        public void Properties()
        {
            That(Object, Has.OwnProperty("prototype", Object.Prototype, EcmaPropertyAttributes.None));

            That(Object.Prototype, Has.OwnProperty("constructor", Object, EcmaPropertyAttributes.Writable | EcmaPropertyAttributes.Configurable));
            That(Object.Invoke("getPrototypeOf", Object.Prototype), Is.EqualTo(Null));
            That(Object.Prototype.Get("toString").Call(Object.Prototype), Is.EqualTo("[object Object]"), "Boolean prototype object: its [[Class]] must be 'Object'");

            That(() => Object.Prototype.Call(), Throws.TypeError);
            That(() => Object.Prototype.Construct(), Throws.TypeError);

            That(Object.Construct()["constructor"], Is.EqualTo(Object));
            That(Object.Prototype.Invoke("isPrototypeOf", Object.Construct()), Is.EqualTo(true));
            That(Object.Invoke("isExtensible", Object.Prototype), Is.EqualTo(true));
        }
Esempio n. 22
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);
                }
            });
Esempio n. 23
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)));
            });
        }
Esempio n. 24
0
        public void Catch(RuntimeFunction @catch)
        {
            IsUnconstructableFunctionWLength(@catch, "catch", 1);
            IsAbruptedFromToObject(@catch);
            That(Promise.Prototype, Has.OwnProperty("catch", @catch, EcmaPropertyAttributes.DefaultMethodProperty));

            It("should invoke then method", () => {
                EcmaValue target      = new EcmaObject();
                EcmaValue returnValue = new EcmaObject();
                EcmaValue thisValue   = Null;
                EcmaValue args        = Null;
                target["then"]        = Intercept((a, b) => {
                    thisValue = This;
                    args      = Arguments;
                    return(returnValue);
                });

                Case((target, 1, 2, 3), returnValue, "Returns the result of the invocation of `then`");
                That(Logs.Count, Is.EqualTo(1), "Invokes `then` method exactly once");
                That(thisValue, Is.EqualTo(target), "Invokes `then` method with the instance as the `this` value");
                That(args, Is.EquivalentTo(new[] { Undefined, 1 }));

                using (TempProperty(Boolean.Prototype, "then", Intercept(_ => _))) {
                    Logs.Clear();
                    @catch.Call(true);
                    That(Logs.Count, Is.EqualTo(1));
                }
                using (TempProperty(Number.Prototype, "then", Intercept(_ => _))) {
                    Logs.Clear();
                    @catch.Call(34);
                    That(Logs.Count, Is.EqualTo(1));
                }
                using (TempProperty(String.Prototype, "then", Intercept(_ => _))) {
                    Logs.Clear();
                    @catch.Call("");
                    That(Logs.Count, Is.EqualTo(1));
                }
                using (TempProperty(Global.Symbol.Prototype, "then", Intercept(_ => _))) {
                    Logs.Clear();
                    @catch.Call(new Symbol());
                    That(Logs.Count, Is.EqualTo(1));
                }
            });
Esempio n. 25
0
        public void Constructor(RuntimeFunction ctor)
        {
            IsConstructorWLength(Symbol, "Symbol", 0, Symbol.Prototype);
            That(GlobalThis, Has.OwnProperty("Symbol", Symbol, EcmaPropertyAttributes.Writable | EcmaPropertyAttributes.Configurable));

            It("should return a unique value", () => {
                That(ctor.Call(_), Is.Not.EqualTo(ctor.Call(_)));
                That(ctor.Call(_, ""), Is.Not.EqualTo(ctor.Call(_, "")));
                That(ctor.Call(_, "x"), Is.Not.EqualTo(ctor.Call(_, "x")));
                That(ctor.Call(_, Null), Is.Not.EqualTo(ctor.Call(_, Null)));
            });

            It("should coerce first argument to a string value", () => {
                That(() => ctor.Call(_, ctor.Call(_, "1")), Throws.TypeError);

                Logs.Clear();
                ctor.Call(_, CreateObject(toString: Intercept(() => Object.Construct(), "toString"), valueOf: Intercept(() => Undefined, "valueOf")));
                That(Logs, Is.EquivalentTo(new[] { "toString", "valueOf" }));

                Logs.Clear();
                ctor.Call(_, CreateObject(toString: Intercept(() => Undefined, "toString"), valueOf: Intercept(() => Undefined, "valueOf")));
                That(Logs, Is.EquivalentTo(new[] { "toString" }));

                Logs.Clear();
                ctor.Call(_, CreateObject(new { toString = Null, valueOf = Intercept(() => Undefined, "valueOf") }));
                That(Logs, Is.EquivalentTo(new[] { "valueOf" }));

                Logs.Clear();
                That(() => ctor.Call(_, CreateObject(new { toString = Null, valueOf = Intercept(() => Object.Construct(), "valueOf") })), Throws.TypeError);
                That(Logs, Is.EquivalentTo(new[] { "valueOf" }));

                Logs.Clear();
                That(() => ctor.Call(_, CreateObject(toString: Intercept(() => Object.Construct(), "toString"), valueOf: Intercept(() => Object.Construct(), "valueOf"))), Throws.TypeError);
                That(Logs, Is.EquivalentTo(new[] { "toString", "valueOf" }));
            });

            It("should not be invoked with new", () => {
                That(() => ctor.Construct(), Throws.TypeError);
                That(() => ctor.Construct(""), Throws.TypeError);
            });
        }
Esempio n. 26
0
        public void Revocable(RuntimeFunction revocable)
        {
            IsUnconstructableFunctionWLength(revocable, "revocable", 2);
            That(Proxy, Has.OwnProperty("revocable", revocable, EcmaPropertyAttributes.DefaultMethodProperty));

            It("The returned object has a proxy property which is the created Proxy object built with the given target and handler given parameters", () => {
                EcmaValue target = CreateObject(new { attr = "foo" });
                EcmaValue r      = revocable.Call(_, target, CreateObject(new { get = FunctionLiteral((t, prop) => t[prop] + "!") }));
                That(r["proxy"]["attr"], Is.EqualTo("foo!"));
            });

            It("The property of Proxy Revocation functions", () => {
                EcmaValue revocationFunction = revocable.Call(_, Object.Construct(), Object.Construct())["revoke"];
                IsUnconstructableFunctionWLength(revocationFunction, null, 0);
                That(Object.Invoke("isExtensible", revocationFunction), Is.EqualTo(true));

                EcmaValue r = revocable.Call(_, Object.Construct(), Object.Construct());
                That(r.Invoke("revoke"), Is.Undefined);
                That(r.Invoke("revoke"), Is.Undefined);
            });
        }
Esempio n. 27
0
        public void Properties()
        {
            That(Error.Prototype, Has.OwnProperty("constructor", Error, EcmaPropertyAttributes.Writable | EcmaPropertyAttributes.Configurable));
            That(Error.Prototype.GetPrototypeOf(), Is.EqualTo(Object.Prototype));
            That(Object.Prototype.Get("toString").Call(Error.Prototype), Is.EqualTo("[object Object]"), "The value of the internal [[Class]] property of Error prototype object is 'Object'");

            That(Error.Prototype, Has.OwnProperty("name", "Error", EcmaPropertyAttributes.Writable | EcmaPropertyAttributes.Configurable));
            That(Error.Prototype, Has.OwnProperty("message", "", EcmaPropertyAttributes.Writable | EcmaPropertyAttributes.Configurable));

            foreach (string derived in new[] { "EvalError", "RangeError", "ReferenceError", "SyntaxError", "TypeError", "URIError" })
            {
                RuntimeFunction CDerived = (RuntimeFunction)GlobalThis[derived].ToObject();
                That(CDerived.Prototype, Has.OwnProperty("constructor", CDerived, EcmaPropertyAttributes.Writable | EcmaPropertyAttributes.Configurable));
                That(CDerived.Prototype.GetPrototypeOf(), Is.EqualTo(Error.Prototype));
                That(Object.Prototype.Get("toString").Call(CDerived.Prototype), Is.EqualTo("[object Object]"),
                     "Each NativeError prototype object is an ordinary object. It is not an Error instance and does not have an [[ErrorData]] internal slot.");

                That(CDerived.Prototype, Has.OwnProperty("name", derived, EcmaPropertyAttributes.Writable | EcmaPropertyAttributes.Configurable));
                That(CDerived.Prototype, Has.OwnProperty("message", "", EcmaPropertyAttributes.Writable | EcmaPropertyAttributes.Configurable));
            }
        }
Esempio n. 28
0
        public void Properties()
        {
            That(Math.GetPrototypeOf(), Is.EqualTo(Object.Prototype));
            That(() => Math.Call(), Throws.TypeError);
            That(() => Math.Construct(), Throws.TypeError);

            That(Math, Has.OwnProperty("E", EcmaPropertyAttributes.None));
            That(Math.Get("E"), Is.TypeOf("number"));
            That(Math.Get("E"), Is.Not.EqualTo(NaN));

            That(Math, Has.OwnProperty("LN10", EcmaPropertyAttributes.None));
            That(Math.Get("LN10"), Is.TypeOf("number"));
            That(Math.Get("LN10"), Is.Not.EqualTo(NaN));

            That(Math, Has.OwnProperty("LN2", EcmaPropertyAttributes.None));
            That(Math.Get("LN2"), Is.TypeOf("number"));
            That(Math.Get("LN2"), Is.Not.EqualTo(NaN));

            That(Math, Has.OwnProperty("LOG10E", EcmaPropertyAttributes.None));
            That(Math.Get("LOG10E"), Is.TypeOf("number"));
            That(Math.Get("LOG10E"), Is.Not.EqualTo(NaN));

            That(Math, Has.OwnProperty("LOG2E", EcmaPropertyAttributes.None));
            That(Math.Get("LOG2E"), Is.TypeOf("number"));
            That(Math.Get("LOG2E"), Is.Not.EqualTo(NaN));

            That(Math, Has.OwnProperty("PI", EcmaPropertyAttributes.None));
            That(Math.Get("PI"), Is.TypeOf("number"));
            That(Math.Get("PI"), Is.Not.EqualTo(NaN));

            That(Math, Has.OwnProperty("SQRT1_2", EcmaPropertyAttributes.None));
            That(Math.Get("SQRT1_2"), Is.TypeOf("number"));
            That(Math.Get("SQRT1_2"), Is.Not.EqualTo(NaN));

            That(Math, Has.OwnProperty("SQRT2", EcmaPropertyAttributes.None));
            That(Math.Get("SQRT2"), Is.TypeOf("number"));
            That(Math.Get("SQRT2"), Is.Not.EqualTo(NaN));

            That(Math, Has.OwnProperty(WellKnownSymbol.ToStringTag, "Math", EcmaPropertyAttributes.Configurable));
        }
Esempio n. 29
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);
            });
Esempio n. 30
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);
                }
            });