示例#1
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)));
                });
            }
        }
示例#2
0
        public void ParseInt(RuntimeFunction parseInt)
        {
            IsUnconstructableFunctionWLength(parseInt, "parseInt", 2);
            IsAbruptedFromToPrimitive(parseInt.Bind(_));
            IsAbruptedFromToPrimitive(parseInt.Bind(_, ""));

            It("should coerce input argument by ToPrimitive(value, String)", () => {
                Case((_, Undefined), NaN);
                Case((_, Null), NaN);
                Case((_, ""), NaN);

                Case((_, true), NaN);
                Case((_, false), NaN);
                Case((_, Boolean.Construct(true)), NaN);
                Case((_, Boolean.Construct(false)), NaN);

                Case((_, -1), -1);
                Case((_, 0), 0);
                Case((_, -0d), 0);
                Case((_, 4.7, 10), 4);
                Case((_, 4.7 * 1e22, 10), 4);
                Case((_, 0.00000000000434, 10), 4);
                Case((_, Infinity), NaN);
                Case((_, NaN), NaN);
                Case((_, Number.Construct(Infinity)), NaN);
                Case((_, Number.Construct(NaN)), NaN);

                Case((_, "+"), NaN);
                Case((_, "-"), NaN);
                Case((_, String.Construct("-1")), -1);
                Case((_, String.Construct("Infinity")), NaN);
                Case((_, String.Construct("NaN")), NaN);
                Case((_, String.Construct("false")), NaN);

                Case((_, CreateObject(valueOf: () => 1)), NaN);
                Case((_, CreateObject(toString: () => 0, valueOf: () => 1)), 0);
                Case((_, CreateObject(toString: () => new EcmaObject(), valueOf: () => 1)), 1);
            });
示例#3
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)));
            });
        }
示例#4
0
        public void Exec(RuntimeFunction exec)
        {
            IsUnconstructableFunctionWLength(exec, "exec", 1);
            IsAbruptedFromToPrimitive(exec.Bind(RegExp.Construct()));
            RequireThisRegExpObject();

            It("should coerce its argument to string", () => {
                object undefined = Undefined;
                VerifyMatch(@"1|12", "123", new[] { "1" }, 0);
                VerifyMatch(@"1|12", 1.01, new[] { "1" }, 0);
                VerifyMatch(@"2|12", Number.Construct(1.012), new[] { "12" }, 3);
                VerifyMatch(@"\.14", CreateObject(toString: () => EcmaMath.PI), new[] { ".14" }, 1);
                VerifyMatch(@"t[a-b|q-s]", true, new[] { "tr" }, 0);
                VerifyMatch(@"AL|se", Boolean.Construct(), new[] { "se" }, 3);
                VerifyMatch(@"LS", "i", CreateObject(toString: () => false), new[] { "ls" }, 2);
                VerifyMatch(@"ll|l", Null, new[] { "ll" }, 2);
                VerifyMatch(@"nd|ne", Undefined, new[] { "nd" }, 1);
                VerifyMatch(@"((1)|(12))((3)|(23))", String.Construct("123"), new[] { "123", "1", "1", undefined, "23", undefined, "23" }, 0);
                VerifyMatch(@"a[a-z]{2,4}", Object.Construct("abcdefghi"), new[] { "abcde" }, 0);
                VerifyMatch(@"a[a-z]{2,4}?", CreateObject(toString: () => "abcdefghi"), new[] { "abc" }, 0);
                VerifyMatch(@"(aa|aabaac|ba|b|c)*", CreateObject(toString: () => new EcmaObject(), valueOf: () => "aabaac"), new[] { "aaba", "ba" }, 0);
            });

            It("should read and reset lastIndex to 0 when global is set and the match fails", () => {
                EcmaValue re       = RegExp.Construct("a", "g");
                int lastIndexReads = 0;

                re["lastIndex"] = CreateObject(valueOf: () => { lastIndexReads++; return(42); });
                Case((re, "abc"), Null);
                That(re["lastIndex"], Is.EqualTo(0));
                That(lastIndexReads, Is.EqualTo(1));

                lastIndexReads  = 0;
                re["lastIndex"] = CreateObject(valueOf: () => { lastIndexReads++; return(-1); });
                Case((re, "nbc"), Null);
                That(re["lastIndex"], Is.EqualTo(0));
                That(lastIndexReads, Is.EqualTo(1));
            });