示例#1
0
        public static EcmaValue ResolvedOptions([This] EcmaValue thisValue)
        {
            PluralRules      rules      = thisValue.GetUnderlyingObject <PluralRules>();
            List <EcmaValue> categories = new List <EcmaValue>();

            foreach (KeyValuePair <string, PluralCategories> e in StringValueMap <PluralCategories> .Default.Forward)
            {
                if ((rules.PluralCategories & e.Value) != 0)
                {
                    categories.Add(e.Key);
                }
            }
            EcmaObject obj = new EcmaObject();

            obj.CreateDataPropertyOrThrow(PropertyKey.Locale, rules.Locale);
            obj.CreateDataPropertyOrThrow(PropertyKey.PluralCategories, new EcmaArray(categories));
            obj.CreateDataPropertyOrThrow(PropertyKey.Type, IntlProviderOptions.ToStringValue(rules.Type));
            if (rules.Digits.UseSignificantDigits)
            {
                obj.CreateDataPropertyOrThrow(PropertyKey.MinimumSignificantDigits, rules.Digits.MinimumSignificantDigits);
                obj.CreateDataPropertyOrThrow(PropertyKey.MaximumSignificantDigits, rules.Digits.MaximumSignificantDigits);
            }
            else
            {
                obj.CreateDataPropertyOrThrow(PropertyKey.MinimumIntegerDigits, rules.Digits.MinimumSignificantDigits);
                obj.CreateDataPropertyOrThrow(PropertyKey.MinimumFractionDigits, rules.Digits.MinimumFractionDigits);
                obj.CreateDataPropertyOrThrow(PropertyKey.MaximumFractionDigits, rules.Digits.MaximumFractionDigits);
            }
            return(obj);
        }
示例#2
0
        public void Constructor(RuntimeFunction ctor)
        {
            IsConstructorWLength(ctor, "RegExp", 2, RegExp.Prototype);

            It("should not throw TypeError when pattern has a [[RegExpMatcher]] internal slot", () => {
                EcmaValue re = RegExp.Construct(RegExp.Construct(), "g");
                That(re["global"], Is.EqualTo(true));
            });

            It("should return its input argument if ToBoolean(@@match) is true and constructor is RegExp", () => {
                EcmaValue obj      = new EcmaObject();
                obj["constructor"] = RegExp;

                obj[WellKnownSymbol.Match] = true;
                Case((RegExp, obj), obj);
                obj[WellKnownSymbol.Match] = "string";
                Case((RegExp, obj), obj);
                obj[WellKnownSymbol.Match] = new EcmaArray();
                Case((RegExp, obj), obj);
                obj[WellKnownSymbol.Match] = new Symbol();
                Case((RegExp, obj), obj);
                obj[WellKnownSymbol.Match] = 86;
                Case((RegExp, obj), obj);

                EcmaValue re1 = RegExp.Construct("(?:)");
                re1[WellKnownSymbol.Match] = false;
                Case((RegExp, re1), Is.Not.EqualTo(re1));

                EcmaValue re2      = RegExp.Construct("(?:)");
                re2["constructor"] = Null;
                Case((RegExp, re2), Is.Not.EqualTo(re2));
            });
示例#3
0
 private RuntimeObject EnsureObject(int index)
 {
     if (runtimeObjects[index] == null)
     {
         runtimeObjects[index] = new EcmaObject();
     }
     return(runtimeObjects[index]);
 }
示例#4
0
        public EcmaValue ToValue()
        {
            EcmaObject obj = new EcmaObject();

            obj.CreateDataPropertyOrThrow(PropertyKey.Type, IntlProviderOptions.ToStringValue(this.Type));
            obj.CreateDataPropertyOrThrow(PropertyKey.Value, this.Value);
            return(obj);
        }
示例#5
0
        public static EcmaValue CreateObject(object anonymousObject)
        {
            EcmaValue obj = new EcmaObject();

            foreach (PropertyInfo property in anonymousObject.GetType().GetProperties())
            {
                object value = property.GetValue(anonymousObject);
                obj[property.Name] = new EcmaValue(value);
            }
            return(obj);
        }
示例#6
0
        public static EcmaValue ResolvedOptions([This] EcmaValue thisValue)
        {
            RelativeTimeFormat formatter = thisValue.GetUnderlyingObject <RelativeTimeFormat>();
            EcmaObject         obj       = new EcmaObject();

            obj.CreateDataPropertyOrThrow(PropertyKey.Locale, formatter.Locale);
            obj.CreateDataPropertyOrThrow(PropertyKey.Style, IntlProviderOptions.ToStringValue(formatter.Style));
            obj.CreateDataPropertyOrThrow(PropertyKey.Numeric, IntlProviderOptions.ToStringValue(formatter.Numeric));
            obj.CreateDataPropertyOrThrow(PropertyKey.NumberingSystem, formatter.NumberingSystem);
            return(obj);
        }
示例#7
0
        public static EcmaValue Revocable(EcmaValue target, EcmaValue handler)
        {
            Guard.ArgumentIsObject(target, InternalString.Error.TargetOrHandlerNotObject);
            Guard.ArgumentIsObject(handler, InternalString.Error.TargetOrHandlerNotObject);
            RuntimeObjectProxy proxy  = new RuntimeObjectProxy(target.ToObject(), handler.ToObject());
            EcmaObject         result = new EcmaObject();

            result.CreateDataProperty("proxy", proxy);
            result.CreateDataProperty("revoke", new RuntimeObjectProxy.RevokeFunction(proxy));
            return(result);
        }
示例#8
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));
        }
示例#9
0
        public static EcmaValue ResolvedOptions([This] EcmaValue thisValue)
        {
            DateTimeFormat     formatter          = thisValue.GetUnderlyingObject <DateTimeFormat>();
            DateTimePartStyles dateTimePartStyles = formatter.DateTimePartStyles;
            EcmaObject         obj = new EcmaObject();

            obj.CreateDataPropertyOrThrow(PropertyKey.Locale, formatter.Locale);
            obj.CreateDataPropertyOrThrow(PropertyKey.Calendar, formatter.Calendar);
            obj.CreateDataPropertyOrThrow(PropertyKey.TimeZone, formatter.TimeZone);
            obj.CreateDataPropertyOrThrow(PropertyKey.Hour12, formatter.Hour12);
            obj.CreateDataPropertyOrThrow(PropertyKey.HourCycle, IntlProviderOptions.ToStringValue(formatter.HourCycle));
            obj.CreateDataPropertyOrThrow(PropertyKey.FormatMatcher, IntlProviderOptions.ToStringValue(formatter.FormatMatcher));
            if (dateTimePartStyles.Weekday != default)
            {
                obj.CreateDataPropertyOrThrow(PropertyKey.Weekday, IntlProviderOptions.ToStringValue(dateTimePartStyles.Weekday));
            }
            if (dateTimePartStyles.Era != default)
            {
                obj.CreateDataPropertyOrThrow(PropertyKey.Era, IntlProviderOptions.ToStringValue(dateTimePartStyles.Era));
            }
            if (dateTimePartStyles.Year != default)
            {
                obj.CreateDataPropertyOrThrow(PropertyKey.Year, IntlProviderOptions.ToStringValue(dateTimePartStyles.Year));
            }
            if (dateTimePartStyles.Month != default)
            {
                obj.CreateDataPropertyOrThrow(PropertyKey.Month, IntlProviderOptions.ToStringValue(dateTimePartStyles.Month));
            }
            if (dateTimePartStyles.Day != default)
            {
                obj.CreateDataPropertyOrThrow(PropertyKey.Day, IntlProviderOptions.ToStringValue(dateTimePartStyles.Day));
            }
            if (dateTimePartStyles.Hour != default)
            {
                obj.CreateDataPropertyOrThrow(PropertyKey.Hour, IntlProviderOptions.ToStringValue(dateTimePartStyles.Hour));
            }
            if (dateTimePartStyles.Minute != default)
            {
                obj.CreateDataPropertyOrThrow(PropertyKey.Minute, IntlProviderOptions.ToStringValue(dateTimePartStyles.Minute));
            }
            if (dateTimePartStyles.Second != default)
            {
                obj.CreateDataPropertyOrThrow(PropertyKey.Second, IntlProviderOptions.ToStringValue(dateTimePartStyles.Second));
            }
            if (dateTimePartStyles.TimeZoneName != default)
            {
                obj.CreateDataPropertyOrThrow(PropertyKey.TimeZoneName, IntlProviderOptions.ToStringValue(dateTimePartStyles.TimeZoneName));
            }
            return(obj);
        }
示例#10
0
        public static EcmaValue ResolvedOptions([This] EcmaValue thisValue)
        {
            Collator   collator = thisValue.GetUnderlyingObject <Collator>();
            EcmaObject obj      = new EcmaObject();

            obj.CreateDataPropertyOrThrow(PropertyKey.Locale, collator.Locale);
            obj.CreateDataPropertyOrThrow(PropertyKey.Usage, IntlProviderOptions.ToStringValue(collator.Usage));
            obj.CreateDataPropertyOrThrow(PropertyKey.Sensitivity, IntlProviderOptions.ToStringValue(collator.Sensitivity));
            obj.CreateDataPropertyOrThrow(PropertyKey.IgnorePunctuation, collator.IgnorePunctuation);
            obj.CreateDataPropertyOrThrow(PropertyKey.Collation, collator.Collation);
            obj.CreateDataPropertyOrThrow(PropertyKey.Numeric, collator.Numeric);
            obj.CreateDataPropertyOrThrow(PropertyKey.CaseFirst, IntlProviderOptions.ToStringValue(collator.CaseFirst));
            return(obj);
        }
示例#11
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));
                }
            });
示例#12
0
        private RuntimeObject GetSharedObject(int i, int j)
        {
            if (i == 0 && j == 0)
            {
                return(null);
            }
            if (i >= sharedObjects.Length)
            {
                throw new ArgumentOutOfRangeException("index");
            }
            if (i >= intrinsics.Length && intrinsics.Length != sharedObjects.Length)
            {
                Array.Resize(ref intrinsics, sharedObjects.Length);
            }
            RuntimeObject[] arr = intrinsics[i];
            if (arr == null)
            {
                arr           = new RuntimeObject[sharedObjects[i].Length];
                intrinsics[i] = arr;
            }
            if (j >= arr.Length)
            {
                throw new ArgumentOutOfRangeException("index");
            }
            RuntimeObject obj = arr[j];

            if (obj == null)
            {
                if (this != sharedRealm)
                {
                    obj = sharedObjects[i][j].CloneSlim(this);
                    objectHandles.Add(obj, new SharedObjectHandle(i, j));
                }
                else
                {
                    obj = new EcmaObject();
                }
                arr[j] = obj;
            }
            return(obj);
        }
示例#13
0
        public void Type()
        {
            It("should coerce native values to correct types", () => {
                EcmaValue undefined    = default;
                EcmaValue nullValue    = EcmaValue.Null;
                EcmaValue stringValue  = "abs";
                EcmaValue intValue     = 1;
                EcmaValue doubleValue  = 1d;
                EcmaValue obj          = new EcmaObject();
                EcmaValue symbol       = new EcmaValue(Symbol.Species);
                EcmaValue numberObject = intValue.ToObject();

                That(undefined.Type, Is.EqualTo(EcmaValueType.Undefined));
                That(nullValue.Type, Is.EqualTo(EcmaValueType.Null));
                That(stringValue.Type, Is.EqualTo(EcmaValueType.String));
                That(intValue.Type, Is.EqualTo(EcmaValueType.Number));
                That(doubleValue.Type, Is.EqualTo(EcmaValueType.Number));
                That(obj.Type, Is.EqualTo(EcmaValueType.Object));
                That(symbol.Type, Is.EqualTo(EcmaValueType.Symbol));
                That(numberObject.Type, Is.EqualTo(EcmaValueType.Object));
            });
        }
示例#14
0
        public static EcmaValue ResolvedOptions([This] EcmaValue thisValue)
        {
            NumberFormat formatter = thisValue.GetUnderlyingObject <NumberFormat>();
            EcmaObject   obj       = new EcmaObject();

            obj.CreateDataPropertyOrThrow(PropertyKey.Locale, formatter.Locale);
            obj.CreateDataPropertyOrThrow(PropertyKey.NumberingSystem, formatter.NumberingSystem);
            obj.CreateDataPropertyOrThrow(PropertyKey.Style, IntlProviderOptions.ToStringValue(formatter.Style));
            obj.CreateDataPropertyOrThrow(PropertyKey.Notation, IntlProviderOptions.ToStringValue(formatter.Notation));
            obj.CreateDataPropertyOrThrow(PropertyKey.CompactDisplay, IntlProviderOptions.ToStringValue(formatter.CompactDisplay));
            obj.CreateDataPropertyOrThrow(PropertyKey.SignDisplay, IntlProviderOptions.ToStringValue(formatter.SignDisplay));
            obj.CreateDataPropertyOrThrow(PropertyKey.UseGrouping, formatter.UseGrouping);
            switch (formatter.Style)
            {
            case NumberStyle.Currency:
                obj.CreateDataPropertyOrThrow(PropertyKey.Currency, formatter.Currency);
                obj.CreateDataPropertyOrThrow(PropertyKey.CurrencyDisplay, IntlProviderOptions.ToStringValue(formatter.CurrencyDisplay));
                obj.CreateDataPropertyOrThrow(PropertyKey.CurrencySign, IntlProviderOptions.ToStringValue(formatter.CurrencySign));
                break;

            case NumberStyle.Unit:
                obj.CreateDataPropertyOrThrow(PropertyKey.Unit, formatter.Unit);
                obj.CreateDataPropertyOrThrow(PropertyKey.UnitDisplay, IntlProviderOptions.ToStringValue(formatter.UnitDisplay));
                break;
            }
            if (formatter.Digits.UseSignificantDigits)
            {
                obj.CreateDataPropertyOrThrow(PropertyKey.MinimumSignificantDigits, formatter.Digits.MinimumSignificantDigits);
                obj.CreateDataPropertyOrThrow(PropertyKey.MaximumSignificantDigits, formatter.Digits.MaximumSignificantDigits);
            }
            else
            {
                obj.CreateDataPropertyOrThrow(PropertyKey.MinimumIntegerDigits, formatter.Digits.MinimumSignificantDigits);
                obj.CreateDataPropertyOrThrow(PropertyKey.MinimumFractionDigits, formatter.Digits.MinimumFractionDigits);
                obj.CreateDataPropertyOrThrow(PropertyKey.MaximumFractionDigits, formatter.Digits.MaximumFractionDigits);
            }
            return(obj);
        }
示例#15
0
        public void StructuredSerialize()
        {
            RuntimeRealm other = new RuntimeRealm();

            It("should clone object with own non-symbol properties", () => {
                EcmaValue obj     = new EcmaObject();
                obj["boolean"]    = true;
                obj["number"]     = 1;
                obj["string"]     = "";
                obj["circular"]   = obj;
                obj["booleanObj"] = Boolean.Construct(false);
                obj["numberObj"]  = Number.Construct(0);
                obj["stringObj"]  = String.Construct("");
                obj[new Symbol()] = "foo";
                Object.Invoke("defineProperty", obj, "foo", CreateObject(new { enumerable = true, value = 1 }));
                Object.Invoke("defineProperty", obj, "bar", CreateObject(new { enumerable = true, get = FunctionLiteral(() => 0) }));
                Object.Invoke("defineProperty", obj, "baz", CreateObject(new { enumerable = false, value = 1 }));

                other.Execute(() => {
                    EcmaValue clone = obj.ToObject().Clone(other);
                    That(clone != obj);
                    That(Object.Invoke("getPrototypeOf", clone), Is.EqualTo(Object.Prototype), "prototype is derived from target realm");

                    That(clone["boolean"], Is.EqualTo(true));
                    That(clone["number"], Is.EqualTo(1));
                    That(clone["string"], Is.EqualTo(""));

                    That(clone["booleanObj"], Is.InstanceOf(Boolean), "Boolean object is copied");
                    That(clone["numberObj"], Is.InstanceOf(Number), "Number object is copied");
                    That(clone["stringObj"], Is.InstanceOf(String), "String object is copied");

                    That(clone["circular"], Is.EqualTo(clone), "circular reference is preserved");
                    That(clone, Has.OwnProperty("foo", 1, EcmaPropertyAttributes.DefaultDataProperty), "non-writable and non-configurable flag is not copied");
                    That(clone, Has.OwnProperty("bar", 0, EcmaPropertyAttributes.DefaultDataProperty), "accessor is copied as data");
                    That(Object.Invoke("getOwnPropertyDescriptor", clone, "baz"), Is.Undefined, "non-enumerable property is not copied");
                    That(Object.Invoke("getOwnPropertySymbols", clone)["length"], Is.Zero, "symbol property is not copied");
                });
            });

            It("should not clone prototype chain", () => {
                EcmaValue obj         = new EcmaObject();
                EcmaValue customProto = Object.Invoke("create", obj);
                obj["foo"]            = "bar";

                Assume.That(Object.Invoke("getPrototypeOf", customProto), Is.EqualTo(obj));
                Assume.That(customProto["foo"], Is.EqualTo("bar"));

                other.Execute(() => {
                    EcmaValue clone = customProto.ToObject().Clone(other);
                    That(clone != customProto);
                    That(Object.Invoke("getPrototypeOf", clone), Is.EqualTo(Object.Prototype), "prototype chain is ignored");
                    That(Object.Invoke("getOwnPropertyNames", clone)["length"], Is.Zero, "inherited properties are not copied");
                    That(clone["foo"], Is.Undefined);
                });
            });

            It("should throw if property values are not cloneable", () => {
                EcmaValue poisoned = new EcmaObject();
                poisoned["foo"]    = new Symbol();
                other.Execute(() => {
                    That(() => poisoned.ToObject().Clone(other), Throws.Exception, "symbol value causes exception");
                });
            });

            It("should shallow clone primitive objects", () => {
                EcmaValue boolObj = Boolean.Construct(false);
                EcmaValue numObj  = Number.Construct(0);
                EcmaValue strObj  = String.Construct("");

                boolObj["foo"] = 1;
                numObj["foo"]  = 1;
                strObj["foo"]  = 1;

                Assume.That(Object.Invoke("getOwnPropertyNames", boolObj)["length"], Is.EqualTo(1));
                Assume.That(Object.Invoke("getOwnPropertyNames", numObj)["length"], Is.EqualTo(1));
                Assume.That(Object.Invoke("getOwnPropertyNames", strObj)["length"], Is.EqualTo(1));

                other.Execute(() => {
                    EcmaValue cboolObj = boolObj.ToObject().Clone(other);
                    That(cboolObj, Is.TypeOf("object"));
                    That(Object.Invoke("getOwnPropertyNames", cboolObj)["length"], Is.Zero);

                    EcmaValue cnumObj = numObj.ToObject().Clone(other);
                    That(cnumObj, Is.TypeOf("object"));
                    That(Object.Invoke("getOwnPropertyNames", cnumObj)["length"], Is.Zero);

                    EcmaValue cstrObj = strObj.ToObject().Clone(other);
                    That(cstrObj, Is.TypeOf("object"));
                    That(Object.Invoke("getOwnPropertyNames", cstrObj)["length"], Is.Zero);
                });
            });

            It("should shallow clone Date object", () => {
                EcmaValue date = Date.Construct(2020, 0, 1);
                EcmaValue ts   = +date;
                date["foo"]    = 1;

                other.Execute(() => {
                    EcmaValue clone = date.ToObject().Clone(other);
                    That(clone, Is.InstanceOf(Date));
                    That(+clone, Is.EqualTo(ts));
                    That(Object.Invoke("getOwnPropertyDescriptor", clone, "foo"), Is.Undefined);
                });
            });

            It("should deeply clone [[MapData]]", () => {
                EcmaValue map = Map.Construct();
                map.Invoke("set", map, map);
                map.Invoke("set", "bar", "baz");
                map["foo"] = 1;

                other.Execute(() => {
                    EcmaValue clone = map.ToObject().Clone(other);
                    That(clone, Is.InstanceOf(Map));
                    That(clone.Invoke("get", clone), Is.EqualTo(clone));
                    That(Object.Invoke("getOwnPropertyDescriptor", clone, "foo"), Is.Undefined, "own property is not copied");

                    clone.Invoke("set", map, 1);
                    clone.Invoke("delete", "bar");
                    clone.Invoke("set", "baz", true);
                });
                That(map.Invoke("get", map), Is.EqualTo(map), "original value is unchanged");
                That(map.Invoke("get", "bar"), Is.EqualTo("baz"), "original entry is not deleted");
                That(map.Invoke("has", "baz"), Is.False, "key added in clone is not added in original map");
            });

            It("should deeply clone [[SetData]]", () => {
                EcmaValue set = Set.Construct();
                set.Invoke("add", set);
                set.Invoke("add", true);
                set["foo"] = 1;

                other.Execute(() => {
                    EcmaValue clone = set.ToObject().Clone(other);
                    That(clone, Is.InstanceOf(Set));
                    That(clone.Invoke("has", clone), Is.True);
                    That(Object.Invoke("getOwnPropertyDescriptor", clone, "foo"), Is.Undefined, "own property is not copied");

                    clone.Invoke("delete", true);
                    clone.Invoke("add", false);
                });
                That(set.Invoke("has", true), Is.True);
                That(set.Invoke("has", false), Is.False);
            });

            It("should clone [[ArrayBufferData]] with same byte contents", () => {
                EcmaValue buffer = Global.ArrayBuffer.Construct(4);
                EcmaValue view   = Global.Uint8Array.Construct(buffer);
                view[1]          = 1;
                view[2]          = 2;
                view[3]          = 3;
                buffer["foo"]    = 1;

                other.Execute(() => {
                    EcmaValue clone     = buffer.ToObject().Clone(other);
                    EcmaValue cloneView = Global.Uint8Array.Construct(clone);
                    That(cloneView, Is.EquivalentTo(new[] { 0, 1, 2, 3 }));
                    That(Object.Invoke("getOwnPropertyDescriptor", clone, "foo"), Is.Undefined, "own property is not copied");
                    cloneView[0] = 42;
                });
                That(view, Is.EquivalentTo(new[] { 0, 1, 2, 3 }));
            });

            It("should clone [[ArrayBufferData]] with same data block if it is shared", () => {
                EcmaValue buffer = Global.SharedArrayBuffer.Construct(4);
                EcmaValue view   = Global.Uint8Array.Construct(buffer);
                view[1]          = 1;
                view[2]          = 2;
                view[3]          = 3;
                buffer["foo"]    = 1;

                other.Execute(() => {
                    EcmaValue clone     = buffer.ToObject().Clone(other);
                    EcmaValue cloneView = Global.Uint8Array.Construct(clone);
                    That(cloneView, Is.EquivalentTo(new[] { 0, 1, 2, 3 }));
                    That(Object.Invoke("getOwnPropertyDescriptor", clone, "foo"), Is.Undefined, "own property is not copied");
                    cloneView[0] = 42;
                });
                That(view, Is.EquivalentTo(new[] { 42, 1, 2, 3 }));
            });

            It("should clone [[ViewedArrayBuffer]] with same byte contents", () => {
                EcmaValue buffer = Global.ArrayBuffer.Construct(4);
                EcmaValue view   = Global.Uint8Array.Construct(buffer);
                view[1]          = 1;
                view[2]          = 2;
                view[3]          = 3;
                view["foo"]      = 1;

                other.Execute(() => {
                    EcmaValue cloneView = view.ToObject().Clone(other);
                    That(cloneView, Is.EquivalentTo(new[] { 0, 1, 2, 3 }));
                    That(Object.Invoke("getOwnPropertyDescriptor", cloneView, "foo"), Is.Undefined, "own property is not copied");
                    cloneView[0] = 42;
                });
                That(view, Is.EquivalentTo(new[] { 0, 1, 2, 3 }));
            });

            It("should clone [[ViewedArrayBuffer]] with same data block if it is shared", () => {
                EcmaValue buffer = Global.SharedArrayBuffer.Construct(4);
                EcmaValue view   = Global.Uint8Array.Construct(buffer);
                view[1]          = 1;
                view[2]          = 2;
                view[3]          = 3;
                view["foo"]      = 1;

                other.Execute(() => {
                    EcmaValue cloneView = view.ToObject().Clone(other);
                    That(cloneView, Is.EquivalentTo(new[] { 0, 1, 2, 3 }));
                    That(Object.Invoke("getOwnPropertyDescriptor", cloneView, "foo"), Is.Undefined, "own property is not copied");
                    cloneView[0] = 42;
                });
                That(view, Is.EquivalentTo(new[] { 42, 1, 2, 3 }));
            });

            It("should throw if object is callable", () => {
                EcmaValue fn = FunctionLiteral(Noop);
                That(() => fn.ToObject().Clone(other), Throws.Exception);
            });

            It("should throw if object has other internal slots", () => {
                EcmaValue symObj = Object.Construct(new Symbol());
                That(() => symObj.ToObject().Clone(other), Throws.Exception);
            });

            It("should detach array buffer if it is in the transfer list", () => {
                EcmaValue buffer = Global.ArrayBuffer.Construct(4);
                EcmaValue view   = Global.Uint8Array.Construct(buffer);
                view[1]          = 1;
                view[2]          = 2;
                view[3]          = 3;

                other.Execute(() => {
                    EcmaValue clone     = buffer.ToObject().Clone(other, buffer.ToObject());
                    EcmaValue cloneView = Global.Uint8Array.Construct(clone);
                    That(cloneView, Is.EquivalentTo(new[] { 0, 1, 2, 3 }));
                });
                That(() => view[1], Throws.TypeError);
            });

            It("should throw if object in transfer list is not transferable", () => {
                EcmaValue buffer = Global.ArrayBuffer.Construct(4);
                That(() => buffer.ToObject().Clone(other, new EcmaObject()), Throws.Exception);
            });

            It("should throw if array buffer in transfer list is detached", () => {
                EcmaValue buffer   = Global.ArrayBuffer.Construct(4);
                EcmaValue detached = Global.ArrayBuffer.Construct(4);
                DetachBuffer(detached);
                That(() => buffer.ToObject().Clone(other, detached.ToObject()), Throws.Exception);
            });
        }
示例#16
0
        public void HasOwnProperty(RuntimeFunction hasOwnProperty)
        {
            IsUnconstructableFunctionWLength(hasOwnProperty, "hasOwnProperty", 1);
            IsAbruptedFromToObject(hasOwnProperty);

            Case((new EcmaObject(), "foo"), false);
            Case((CreateObject(("foo", 42)), "foo"), true);
            Case((Object.Invoke("create", CreateObject(("foo", 42))), "foo"), false);

            Symbol    sym       = new Symbol();
            EcmaValue returnSym = FunctionLiteral(() => sym);

            Case((CreateObject((sym, 42)), sym), true);
            Case((CreateObject((sym, 42)), CreateObject((Symbol.ToPrimitive, returnSym))), true);
            Case((CreateObject((sym, 42)), CreateObject(("toString", returnSym), ("valueOf", Null))), true);
            Case((CreateObject((sym, 42)), CreateObject(("valueOf", returnSym), ("toString", Null))), true);

            EcmaValue o;

            o = FunctionLiteral(() => Void(This.ToObject()["foo"] = 42)).Construct();
            Case((o, "foo"), true);

            o = new EcmaObject();
            DefineProperty(o, "foo", value: 42);
            Case((o, "foo"), true);

            o = new EcmaObject();
            DefineProperty(o, "foo", value: 42, enumerable: true);
            Case((o, "foo"), true);
            Case((Object.Invoke("create", o), "foo"), false);

            o = new EcmaObject();
            DefineProperty(o, "foo", value: 42, configurable: true);
            Case((o, "foo"), true);
            Case((Object.Invoke("create", o), "foo"), false);

            o = new EcmaObject();
            DefineProperty(o, "foo", value: 42, writable: true);
            Case((o, "foo"), true);
            Case((Object.Invoke("create", o), "foo"), false);

            o = new EcmaObject();
            DefineProperty(o, "foo", value: 42, enumerable: true, configurable: true);
            Case((o, "foo"), true);
            Case((Object.Invoke("create", o), "foo"), false);

            o = new EcmaObject();
            DefineProperty(o, "foo", value: 42, enumerable: true, writable: true);
            Case((o, "foo"), true);
            Case((Object.Invoke("create", o), "foo"), false);

            o = new EcmaObject();
            DefineProperty(o, "foo", value: 42, configurable: true, writable: true);
            Case((o, "foo"), true);
            Case((Object.Invoke("create", o), "foo"), false);

            o = new EcmaObject();
            DefineProperty(o, "foo", value: 42, configurable: true, enumerable: true, writable: true);
            Case((o, "foo"), true);
            Case((Object.Invoke("create", o), "foo"), false);

            o = new EcmaObject();
            DefineProperty(o, "foo", get: () => 42);
            Case((o, "foo"), true);
            Case((Object.Invoke("create", o), "foo"), false);

            o = new EcmaObject();
            DefineProperty(o, "foo", set: _ => 42);
            Case((o, "foo"), true);
            Case((Object.Invoke("create", o), "foo"), false);

            o = new EcmaObject();
            DefineProperty(o, "foo", get: () => 42, set: _ => 42);
            Case((o, "foo"), true);
            Case((Object.Invoke("create", o), "foo"), false);

            o = new EcmaObject();
            DefineProperty(o, "foo", get: () => 42, enumerable: true);
            Case((o, "foo"), true);
            Case((Object.Invoke("create", o), "foo"), false);

            o = new EcmaObject();
            DefineProperty(o, "foo", set: _ => 42, enumerable: true);
            Case((o, "foo"), true);
            Case((Object.Invoke("create", o), "foo"), false);

            o = new EcmaObject();
            DefineProperty(o, "foo", get: () => 42, set: _ => 42, enumerable: true);
            Case((o, "foo"), true);
            Case((Object.Invoke("create", o), "foo"), false);

            o = new EcmaObject();
            DefineProperty(o, "foo", get: () => 42, configurable: true);
            Case((o, "foo"), true);
            Case((Object.Invoke("create", o), "foo"), false);

            o = new EcmaObject();
            DefineProperty(o, "foo", set: _ => 42, configurable: true);
            Case((o, "foo"), true);
            Case((Object.Invoke("create", o), "foo"), false);

            o = new EcmaObject();
            DefineProperty(o, "foo", get: () => 42, set: _ => 42, configurable: true);
            Case((o, "foo"), true);
            Case((Object.Invoke("create", o), "foo"), false);

            o = new EcmaObject();
            DefineProperty(o, "foo", get: () => 42, configurable: true, enumerable: true);
            Case((o, "foo"), true);
            Case((Object.Invoke("create", o), "foo"), false);

            o = new EcmaObject();
            DefineProperty(o, "foo", set: _ => 42, configurable: true, enumerable: true);
            Case((o, "foo"), true);
            Case((Object.Invoke("create", o), "foo"), false);

            o = new EcmaObject();
            DefineProperty(o, "foo", get: () => 42, set: _ => 42, configurable: true, enumerable: true);
            Case((o, "foo"), true);
            Case((Object.Invoke("create", o), "foo"), false);
        }
示例#17
0
 public static EcmaValue SetPrototypeOf([This] EcmaValue thisValue, EcmaValue proto)
 {
     return(EcmaObject.SetPrototypeOf(thisValue, proto));
 }
示例#18
0
 public static EcmaValue GetPrototypeOf([This] EcmaValue thisValue)
 {
     return(EcmaObject.GetPrototypeOf(thisValue));
 }
示例#19
0
        public static EcmaValue ResolvedOptions([This] EcmaValue thisValue)
        {
            ListFormat formatter = thisValue.GetUnderlyingObject <ListFormat>(); EcmaObject obj = new EcmaObject();

            obj.CreateDataPropertyOrThrow(PropertyKey.Locale, formatter.Locale);
            obj.CreateDataPropertyOrThrow(PropertyKey.Style, IntlProviderOptions.ToStringValue(formatter.Style));
            obj.CreateDataPropertyOrThrow(PropertyKey.Type, IntlProviderOptions.ToStringValue(formatter.Type));
            return(obj);
        }
示例#20
0
        public void ToString(RuntimeFunction toString)
        {
            IsUnconstructableFunctionWLength(toString, "toString", 0);

            EcmaValue obj = new EcmaObject();

            DefineProperty(obj, Symbol.ToStringTag, get: ThrowTest262Exception);
            Case(obj, Throws.Test262);

            EcmaValue args = default;

            FunctionLiteral(() => args = Arguments).Call();
            Case(args, "[object Arguments]");

            Case(new EcmaArray(), "[object Array]");

            Case(true, "[object Boolean]");
            Case(Boolean.Call(_, true), "[object Boolean]");
            Case(Boolean.Construct(true), "[object Boolean]");
            Case(Object.Call(_, true), "[object Boolean]");
            Case(Object.Call(_, Boolean.Call(true)), "[object Boolean]");
            Case(Object.Call(_, Boolean.Construct(true)), "[object Boolean]");

            Case(new EcmaDate(), "[object Date]");
            Case(Object.Call(_, new EcmaDate()), "[object Date]");

            Case(FunctionLiteral(() => Undefined), "[object Function]");

            Case(Null, "[object Null]");

            Case(42, "[object Number]");
            Case(Number.Call(_, 42), "[object Number]");
            Case(Number.Construct(42), "[object Number]");
            Case(Object.Call(_, 42), "[object Number]");
            Case(Object.Call(_, Number.Call(42)), "[object Number]");
            Case(Object.Call(_, Number.Construct(42)), "[object Number]");

            Case(Object.Prototype, "[object Object]");
            Case(new EcmaObject(), "[object Object]");

            Case(RegExp.Construct("."), "[object RegExp]");

            Case("", "[object String]");
            Case(String.Call(_, ""), "[object String]");
            Case(String.Construct(""), "[object String]");
            Case(Object.Call(_, ""), "[object String]");
            Case(Object.Call(_, String.Call("")), "[object String]");
            Case(Object.Call(_, String.Construct("")), "[object String]");

            Case(Undefined, "[object Undefined]");

            Case(Proxy.Construct(Array.Construct(), Object.Construct()), "[object Array]");

            // Non-string values of `@@toStringTag` property are ignored
            Case(CreateObject((Symbol.ToStringTag, Undefined)), "[object Object]");
            Case(CreateObject((Symbol.ToStringTag, Null)), "[object Object]");
            Case(CreateObject((Symbol.ToStringTag, Symbol.ToStringTag)), "[object Object]");
            Case(CreateObject((Symbol.ToStringTag, 42)), "[object Object]");
            Case(CreateObject((Symbol.ToStringTag, String.Construct(""))), "[object Object]");
            Case(CreateObject((Symbol.ToStringTag, new EcmaObject())), "[object Object]");
            Case(CreateObject((Symbol.ToStringTag, FunctionLiteral(() => ""))), "[object Object]");
        }