Exemple #1
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);
            });
        }
Exemple #2
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)));
                });
            }
        }
Exemple #3
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");
        }
Exemple #4
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)));
            });
        }