protected override void Initialize() { _prototype = Engine.Function.PrototypeObject; const PropertyFlag propertyFlags = PropertyFlag.Configurable | PropertyFlag.Writable; const PropertyFlag lengthFlags = PropertyFlag.Configurable; var properties = new PropertyDictionary(15, checkExistingKeys: false) { ["assign"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "assign", Assign, 2, lengthFlags), propertyFlags), ["entries"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "entries", Entries, 1, lengthFlags), propertyFlags), ["fromEntries"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "fromEntries", FromEntries, 1, lengthFlags), propertyFlags), ["getPrototypeOf"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "getPrototypeOf", GetPrototypeOf, 1), propertyFlags), ["getOwnPropertyDescriptor"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "getOwnPropertyDescriptor", GetOwnPropertyDescriptor, 2), propertyFlags), ["getOwnPropertyDescriptors"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "getOwnPropertyDescriptors", GetOwnPropertyDescriptors, 1, lengthFlags), propertyFlags), ["getOwnPropertyNames"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "getOwnPropertyNames", GetOwnPropertyNames, 1), propertyFlags), ["getOwnPropertySymbols"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "getOwnPropertySymbols", GetOwnPropertySymbols, 1, lengthFlags), propertyFlags), ["create"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "create", Create, 2), propertyFlags), ["defineProperty"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "defineProperty", DefineProperty, 3), propertyFlags), ["defineProperties"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "defineProperties", DefineProperties, 2), propertyFlags), ["is"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "is", Is, 2, lengthFlags), propertyFlags), ["seal"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "seal", Seal, 1), propertyFlags), ["freeze"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "freeze", Freeze, 1), propertyFlags), ["preventExtensions"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "preventExtensions", PreventExtensions, 1), propertyFlags), ["isSealed"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "isSealed", IsSealed, 1), propertyFlags), ["isFrozen"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "isFrozen", IsFrozen, 1), propertyFlags), ["isExtensible"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "isExtensible", IsExtensible, 1), propertyFlags), ["keys"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "keys", Keys, 1, lengthFlags), propertyFlags), ["values"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "values", Values, 1, lengthFlags), propertyFlags), ["setPrototypeOf"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "setPrototypeOf", SetPrototypeOf, 2, lengthFlags), propertyFlags) }; SetProperties(properties); }
/// <summary> /// http://www.ecma-international.org/ecma-262/5.1/#sec-13.2 /// </summary> /// <param name="engine"></param> /// <param name="functionDeclaration"></param> /// <param name="scope"></param> /// <param name="strict"></param> public ScriptFunctionInstance( Engine engine, IFunction functionDeclaration, LexicalEnvironment scope, bool strict) : base(engine, functionDeclaration.Id?.Name ?? "", GetParameterNames(functionDeclaration), scope, strict) { _functionDeclaration = functionDeclaration; Extensible = true; Prototype = _engine.Function.PrototypeObject; _length = new PropertyDescriptor(JsNumber.Create(_formalParameters.Length), PropertyFlag.AllForbidden); var proto = new ObjectInstanceWithConstructor(engine, this) { Extensible = true, Prototype = _engine.Object.PrototypeObject }; _prototype = new PropertyDescriptor(proto, PropertyFlag.OnlyWritable); if (_functionDeclaration.Id != null) { DefineOwnProperty("name", new PropertyDescriptor(_functionDeclaration.Id.Name, PropertyFlag.None), false); } if (strict) { var thrower = engine.Function.ThrowTypeError; const PropertyFlag flags = PropertyFlag.EnumerableSet | PropertyFlag.ConfigurableSet; DefineOwnProperty("caller", new GetSetPropertyDescriptor(thrower, thrower, flags), false); DefineOwnProperty("arguments", new GetSetPropertyDescriptor(thrower, thrower, flags), false); } }
protected override void Initialize() { const PropertyFlag propertyFlags = PropertyFlag.Configurable | PropertyFlag.Writable; var properties = new PropertyDictionary(12, checkExistingKeys: false) { ["length"] = new PropertyDescriptor(0, PropertyFlag.Configurable), ["constructor"] = new PropertyDescriptor(_mapConstructor, PropertyFlag.NonEnumerable), ["clear"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "clear", Clear, 0, PropertyFlag.Configurable), propertyFlags), ["delete"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "delete", Delete, 1, PropertyFlag.Configurable), propertyFlags), ["entries"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "entries", Entries, 0, PropertyFlag.Configurable), propertyFlags), ["forEach"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "forEach", ForEach, 1, PropertyFlag.Configurable), propertyFlags), ["get"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "get", Get, 1, PropertyFlag.Configurable), propertyFlags), ["has"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "has", Has, 1, PropertyFlag.Configurable), propertyFlags), ["keys"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "keys", Keys, 0, PropertyFlag.Configurable), propertyFlags), ["set"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "set", Set, 2, PropertyFlag.Configurable), propertyFlags), ["values"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "values", Values, 0, PropertyFlag.Configurable), propertyFlags), ["size"] = new GetSetPropertyDescriptor(get: new ClrFunctionInstance(Engine, "get size", Size, 0, PropertyFlag.Configurable), set: null, PropertyFlag.Configurable) }; SetProperties(properties); var symbols = new SymbolDictionary(2) { [GlobalSymbolRegistry.Iterator] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "iterator", Entries, 1, PropertyFlag.Configurable), propertyFlags), [GlobalSymbolRegistry.ToStringTag] = new PropertyDescriptor("Map", false, false, true), }; SetSymbols(symbols); }
protected override void Initialize() { const PropertyFlag lengthFlags = PropertyFlag.Configurable; const PropertyFlag propertyFlags = PropertyFlag.AllForbidden; var properties = new PropertyDictionary(15, checkExistingKeys: false) { ["for"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "for", For, 1, lengthFlags), PropertyFlag.Writable | PropertyFlag.Configurable), ["keyFor"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "keyFor", KeyFor, 1, lengthFlags), PropertyFlag.Writable | PropertyFlag.Configurable), ["hasInstance"] = new PropertyDescriptor(GlobalSymbolRegistry.HasInstance, propertyFlags), ["isConcatSpreadable"] = new PropertyDescriptor(GlobalSymbolRegistry.IsConcatSpreadable, propertyFlags), ["iterator"] = new PropertyDescriptor(GlobalSymbolRegistry.Iterator, propertyFlags), ["match"] = new PropertyDescriptor(GlobalSymbolRegistry.Match, propertyFlags), ["matchAll"] = new PropertyDescriptor(GlobalSymbolRegistry.MatchAll, propertyFlags), ["replace"] = new PropertyDescriptor(GlobalSymbolRegistry.Replace, propertyFlags), ["search"] = new PropertyDescriptor(GlobalSymbolRegistry.Search, propertyFlags), ["species"] = new PropertyDescriptor(GlobalSymbolRegistry.Species, propertyFlags), ["split"] = new PropertyDescriptor(GlobalSymbolRegistry.Split, propertyFlags), ["toPrimitive"] = new PropertyDescriptor(GlobalSymbolRegistry.ToPrimitive, propertyFlags), ["toStringTag"] = new PropertyDescriptor(GlobalSymbolRegistry.ToStringTag, propertyFlags), ["unscopables"] = new PropertyDescriptor(GlobalSymbolRegistry.Unscopables, propertyFlags), ["asyncIterator"] = new PropertyDescriptor(GlobalSymbolRegistry.AsyncIterator, propertyFlags) }; SetProperties(properties); }
protected internal PropertyDescriptor(JsValue value, PropertyFlag flags) : this(flags) { if ((_flags & PropertyFlag.CustomJsValue) != 0) { CustomValue = value; } _value = value; }
public PropertyDescriptor(JsValue value, PropertyFlag flags) : this(flags) { if ((_flags & PropertyFlag.CustomJsValue) != 0) { CustomValue = value; } _value = value; }
protected override void Initialize() { const PropertyFlag lengthFlags = PropertyFlag.Configurable; const PropertyFlag propertyFlags = lengthFlags | PropertyFlag.Writable; var trimStart = new PropertyDescriptor(new ClrFunctionInstance(Engine, "trimStart", TrimStart, 0, lengthFlags), propertyFlags); var trimEnd = new PropertyDescriptor(new ClrFunctionInstance(Engine, "trimEnd", TrimEnd, 0, lengthFlags), propertyFlags); var properties = new PropertyDictionary(35, checkExistingKeys: false) { ["constructor"] = new PropertyDescriptor(_constructor, PropertyFlag.NonEnumerable), ["toString"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "toString", ToStringString, 0, lengthFlags), propertyFlags), ["valueOf"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "valueOf", ValueOf, 0, lengthFlags), propertyFlags), ["charAt"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "charAt", CharAt, 1, lengthFlags), propertyFlags), ["charCodeAt"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "charCodeAt", CharCodeAt, 1, lengthFlags), propertyFlags), ["codePointAt"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "codePointAt", CodePointAt, 1, lengthFlags), propertyFlags), ["concat"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "concat", Concat, 1, lengthFlags), propertyFlags), ["indexOf"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "indexOf", IndexOf, 1, lengthFlags), propertyFlags), ["endsWith"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "endsWith", EndsWith, 1, lengthFlags), propertyFlags), ["startsWith"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "startsWith", StartsWith, 1, lengthFlags), propertyFlags), ["lastIndexOf"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "lastIndexOf", LastIndexOf, 1, lengthFlags), propertyFlags), ["localeCompare"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "localeCompare", LocaleCompare, 1, lengthFlags), propertyFlags), ["match"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "match", Match, 1, lengthFlags), propertyFlags), ["matchAll"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "matchAll", MatchAll, 1, lengthFlags), propertyFlags), ["replace"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "replace", Replace, 2, lengthFlags), propertyFlags), ["replaceAll"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "replaceAll", ReplaceAll, 2, lengthFlags), propertyFlags), ["search"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "search", Search, 1, lengthFlags), propertyFlags), ["slice"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "slice", Slice, 2, lengthFlags), propertyFlags), ["split"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "split", Split, 2, lengthFlags), propertyFlags), ["substr"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "substr", Substr, 2), propertyFlags), ["substring"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "substring", Substring, 2, lengthFlags), propertyFlags), ["toLowerCase"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "toLowerCase", ToLowerCase, 0, lengthFlags), propertyFlags), ["toLocaleLowerCase"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "toLocaleLowerCase", ToLocaleLowerCase, 0, lengthFlags), propertyFlags), ["toUpperCase"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "toUpperCase", ToUpperCase, 0, lengthFlags), propertyFlags), ["toLocaleUpperCase"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "toLocaleUpperCase", ToLocaleUpperCase, 0, lengthFlags), propertyFlags), ["trim"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "trim", Trim, 0, lengthFlags), propertyFlags), ["trimStart"] = trimStart, ["trimEnd"] = trimEnd, ["trimLeft"] = trimStart, ["trimRight"] = trimEnd, ["padStart"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "padStart", PadStart, 1, lengthFlags), propertyFlags), ["padEnd"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "padEnd", PadEnd, 1, lengthFlags), propertyFlags), ["includes"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "includes", Includes, 1, lengthFlags), propertyFlags), ["normalize"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "normalize", Normalize, 0, lengthFlags), propertyFlags), ["repeat"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "repeat", Repeat, 1, lengthFlags), propertyFlags), ["at"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "at", At, 1, lengthFlags), propertyFlags), }; SetProperties(properties); var symbols = new SymbolDictionary(1) { [GlobalSymbolRegistry.Iterator] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "[Symbol.iterator]", Iterator, 0, lengthFlags), propertyFlags) }; SetSymbols(symbols); }
protected override void Initialize() { const PropertyFlag lengthFlags = PropertyFlag.Configurable; GetSetPropertyDescriptor CreateGetAccessorDescriptor(string name, Func <RegExpInstance, JsValue> valueExtractor, JsValue protoValue = null) { return(new GetSetPropertyDescriptor( get: new ClrFunctionInstance(Engine, name, (thisObj, arguments) => { if (ReferenceEquals(thisObj, this)) { return protoValue ?? Undefined; } var r = thisObj as RegExpInstance; if (r is null) { ExceptionHelper.ThrowTypeError(_realm); } return valueExtractor(r); }, 0, lengthFlags), set: Undefined, flags: PropertyFlag.Configurable)); } const PropertyFlag propertyFlags = PropertyFlag.Configurable | PropertyFlag.Writable; var properties = new PropertyDictionary(12, checkExistingKeys: false) { ["constructor"] = new PropertyDescriptor(_constructor, propertyFlags), ["toString"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "toString", ToRegExpString, 0, lengthFlags), propertyFlags), ["exec"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "exec", _defaultExec, 1, lengthFlags), propertyFlags), ["test"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "test", Test, 1, lengthFlags), propertyFlags), ["dotAll"] = CreateGetAccessorDescriptor("get dotAll", r => r.DotAll), ["flags"] = new GetSetPropertyDescriptor(get: new ClrFunctionInstance(Engine, "get flags", Flags, 0, lengthFlags), set: Undefined, flags: PropertyFlag.Configurable), ["global"] = CreateGetAccessorDescriptor("get global", r => r.Global), ["ignoreCase"] = CreateGetAccessorDescriptor("get ignoreCase", r => r.IgnoreCase), ["multiline"] = CreateGetAccessorDescriptor("get multiline", r => r.Multiline), ["source"] = new GetSetPropertyDescriptor(get: new ClrFunctionInstance(Engine, "get source", Source, 0, lengthFlags), set: Undefined, flags: PropertyFlag.Configurable), ["sticky"] = CreateGetAccessorDescriptor("get sticky", r => r.Sticky), ["unicode"] = CreateGetAccessorDescriptor("get unicode", r => r.FullUnicode) }; SetProperties(properties); var symbols = new SymbolDictionary(5) { [GlobalSymbolRegistry.Match] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "[Symbol.match]", Match, 1, lengthFlags), propertyFlags), [GlobalSymbolRegistry.MatchAll] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "[Symbol.matchAll]", MatchAll, 1, lengthFlags), propertyFlags), [GlobalSymbolRegistry.Replace] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "[Symbol.replace]", Replace, 2, lengthFlags), propertyFlags), [GlobalSymbolRegistry.Search] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "[Symbol.search]", Search, 1, lengthFlags), propertyFlags), [GlobalSymbolRegistry.Split] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "[Symbol.split]", Split, 2, lengthFlags), propertyFlags) }; SetSymbols(symbols); }
private static int CreateMaskForRead(int i, PropertyFlag propertyFlag) { var mask = 0x55555555 << (int)propertyFlag; if (i == 0) { mask &= ~AdditionalStateMask; } return mask; }
protected override void EnsureInitialized() { if (_initialized) { return; } _initialized = true; var self = this; var len = _args.Length; self.SetOwnProperty("length", new PropertyDescriptor(len, PropertyFlag.NonEnumerable)); if (_args.Length > 0) { var map = Engine.Object.Construct(Arguments.Empty); var mappedNamed = _mappedNamed; mappedNamed.Clear(); for (var indx = 0; indx < len; indx++) { var indxStr = TypeConverter.ToString(indx); var val = _args[indx]; self.SetOwnProperty(indxStr, new PropertyDescriptor(val, PropertyFlag.ConfigurableEnumerableWritable)); if (indx < _names.Length) { var name = _names[indx]; if (!_strict && !mappedNamed.Contains(name)) { mappedNamed.Add(name); map.SetOwnProperty(indxStr, new ClrAccessDescriptor(_env, Engine, name)); } } } // step 12 if (mappedNamed.Count > 0) { self.ParameterMap = map; } } // step 13 if (!_strict) { self.SetOwnProperty("callee", new PropertyDescriptor(_func, PropertyFlag.NonEnumerable)); } // step 14 else { var thrower = Engine.Function.ThrowTypeError; const PropertyFlag flags = PropertyFlag.EnumerableSet | PropertyFlag.ConfigurableSet; self.DefineOwnProperty("caller", new GetSetPropertyDescriptor(get: thrower, set: thrower, flags: flags), false); self.DefineOwnProperty("callee", new GetSetPropertyDescriptor(get: thrower, set: thrower, flags: flags), false); } }
private static int CreateMaskForRead(int i, PropertyFlag propertyFlag) { var mask = PropertyFlagMask << (int)propertyFlag; if (i == 0) { mask &= ~AdditionalStateMask; } return(mask); }
public void FlagProperty(int propertyIndex, PropertyFlag propertyFlag, bool isFlagged) { propertyIndex = propertyIndex * BitsForPropertyFlags + (int)propertyFlag + BitsForAdditionalState; if (isFlagged) { _bits[propertyIndex / BitsPerInt] |= 1 << (propertyIndex % BitsPerInt); } else { _bits[propertyIndex / BitsPerInt] &= ~(1 << (propertyIndex % BitsPerInt)); } }
public void FlagProperty(int propertyIndex, PropertyFlag propertyFlag, bool isFlagged) { propertyIndex = propertyIndex * BitsForPropertyFlags + (int)propertyFlag + BitsForAdditionalState; if (isFlagged) { _bits[propertyIndex / BitsPerInt] |= 1 << propertyIndex % BitsPerInt; } else { _bits[propertyIndex / BitsPerInt] &= ~(1 << propertyIndex % BitsPerInt); } }
public ClrFunctionInstance( Engine engine, string name, Func <JsValue, JsValue[], JsValue> func, int length = 0, PropertyFlag lengthFlags = PropertyFlag.AllForbidden) : base(engine, name, null, null, false) { _func = func; Prototype = engine.Function.PrototypeObject; Extensible = true; _length = new PropertyDescriptor(length, lengthFlags); }
private int CreateMaskForWrite(int i, int propertyCount, PropertyFlag propertyFlag) { var mask = CreateMaskForRead(i, propertyFlag); if (i == _bits.Length - 1) { var overlay = PropertyFlagMask << (int)propertyFlag; var shift = (propertyCount * BitsForPropertyFlags + BitsForAdditionalState) % BitsPerInt; overlay = shift != 0 ? overlay << shift : 0; mask &= ~overlay; } return(mask); }
public bool AnyPropertiesFlagged(PropertyFlag propertyFlag) { // ReSharper disable once LoopCanBeConvertedToQuery for (var i = 0; i < _bits.Length; i++) { var bit = _bits[i]; if ((bit & CreateMaskForRead(i, propertyFlag)) != 0) { return(true); } } return(false); }
public void FlagAllProperties(int propertyCount, PropertyFlag propertyFlag, bool flagged) { for (var i = 0; i < _bits.Length; i++) { if (flagged) { _bits[i] |= CreateMaskForWrite(i, propertyCount, propertyFlag); } else { _bits[i] &= ~CreateMaskForWrite(i, propertyCount, propertyFlag); } } }
protected override void Initialize() { const PropertyFlag lengthFlags = PropertyFlag.Configurable; const PropertyFlag propertyFlags = PropertyFlag.Configurable | PropertyFlag.Writable; var properties = new PropertyDictionary(3, checkExistingKeys: false) { ["parse"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "parse", Parse, 1, lengthFlags), propertyFlags), ["UTC"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "UTC", Utc, 7, lengthFlags), propertyFlags), ["now"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "now", Now, 0, lengthFlags), propertyFlags) }; SetProperties(properties); }
public bool AnyPropertiesFlagged(PropertyFlag propertyFlag) { // ReSharper disable once LoopCanBeConvertedToQuery for (var i = 0; i < _bits.Length; i++) { var bit = _bits[i]; if ((bit & CreateMaskForRead(i, propertyFlag)) != 0) { return true; } } return false; }
protected override void Initialize() { const PropertyFlag lengthFlags = PropertyFlag.Configurable; const PropertyFlag propertyFlags = PropertyFlag.Configurable | PropertyFlag.Writable; var properties = new PropertyDictionary(40, checkExistingKeys: false) { ["Object"] = new PropertyDescriptor(Engine.Object, propertyFlags), ["Function"] = new PropertyDescriptor(Engine.Function, propertyFlags), ["Symbol"] = new PropertyDescriptor(Engine.Symbol, propertyFlags), ["Array"] = new PropertyDescriptor(Engine.Array, propertyFlags), ["Map"] = new PropertyDescriptor(Engine.Map, propertyFlags), ["Set"] = new PropertyDescriptor(Engine.Set, propertyFlags), ["String"] = new PropertyDescriptor(Engine.String, propertyFlags), ["RegExp"] = new PropertyDescriptor(Engine.RegExp, propertyFlags), ["Number"] = new PropertyDescriptor(Engine.Number, propertyFlags), ["Boolean"] = new PropertyDescriptor(Engine.Boolean, propertyFlags), ["Date"] = new PropertyDescriptor(Engine.Date, propertyFlags), ["Math"] = new PropertyDescriptor(Engine.Math, propertyFlags), ["JSON"] = new PropertyDescriptor(Engine.Json, propertyFlags), ["Error"] = new LazyPropertyDescriptor(() => Engine.Error, propertyFlags), ["EvalError"] = new LazyPropertyDescriptor(() => Engine.EvalError, propertyFlags), ["Proxy"] = new LazyPropertyDescriptor(() => Engine.Proxy, propertyFlags), ["RangeError"] = new LazyPropertyDescriptor(() => Engine.RangeError, propertyFlags), ["ReferenceError"] = new LazyPropertyDescriptor(() => Engine.ReferenceError, propertyFlags), ["Reflect"] = new LazyPropertyDescriptor(() => Engine.Reflect, propertyFlags), ["SyntaxError"] = new LazyPropertyDescriptor(() => Engine.SyntaxError, propertyFlags), ["TypeError"] = new LazyPropertyDescriptor(() => Engine.TypeError, propertyFlags), ["URIError"] = new LazyPropertyDescriptor(() => Engine.UriError, propertyFlags), ["NaN"] = new PropertyDescriptor(double.NaN, PropertyFlag.None), ["Infinity"] = new PropertyDescriptor(double.PositiveInfinity, PropertyFlag.None), ["undefined"] = new PropertyDescriptor(Undefined, PropertyFlag.None), ["parseInt"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "parseInt", ParseInt, 2, lengthFlags), propertyFlags), ["parseFloat"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "parseFloat", ParseFloat, 1, lengthFlags), propertyFlags), ["isNaN"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "isNaN", IsNaN, 1, lengthFlags), propertyFlags), ["isFinite"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "isFinite", IsFinite, 1, lengthFlags), propertyFlags), ["decodeURI"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "decodeURI", DecodeUri, 1, lengthFlags), propertyFlags), ["decodeURIComponent"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "decodeURIComponent", DecodeUriComponent, 1, lengthFlags), propertyFlags), ["encodeURI"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "encodeURI", EncodeUri, 1, lengthFlags), propertyFlags), ["encodeURIComponent"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "encodeURIComponent", EncodeUriComponent, 1, lengthFlags), propertyFlags), ["escape"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "escape", Escape, 1), propertyFlags), ["unescape"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "unescape", Unescape, 1), propertyFlags), ["globalThis"] = new PropertyDescriptor(this, propertyFlags), // toString is not mentioned or actually required in spec, but some tests rely on it ["toString"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "toString", ToStringString, 1), propertyFlags) }; SetProperties(properties); }
public ClrFunctionInstance( Engine engine, string name, Func <JsValue, JsValue[], JsValue> func, int length = 0, PropertyFlag lengthFlags = PropertyFlag.AllForbidden) : base(engine, !(string.IsNullOrEmpty(name) || name.Trim() == "") ? new JsString(name) : null) { _func = func; _prototype = engine.Function.PrototypeObject; _length = lengthFlags == PropertyFlag.AllForbidden ? PropertyDescriptor.AllForbiddenDescriptor.ForNumber(length) : new PropertyDescriptor(JsNumber.Create(length), lengthFlags); }
protected override void Initialize() { const PropertyFlag propertyFlags = PropertyFlag.Configurable | PropertyFlag.Writable; const PropertyFlag lengthFlags = PropertyFlag.Configurable; var properties = new PropertyDictionary(8, checkExistingKeys: false) { ["constructor"] = new PropertyDescriptor(_objectConstructor, propertyFlags), ["toString"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "toString", ToObjectString, 0, lengthFlags), propertyFlags), ["toLocaleString"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "toLocaleString", ToLocaleString, 0, lengthFlags), propertyFlags), ["valueOf"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "valueOf", ValueOf, 0, lengthFlags), propertyFlags), ["hasOwnProperty"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "hasOwnProperty", HasOwnProperty, 1, lengthFlags), propertyFlags), ["isPrototypeOf"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "isPrototypeOf", IsPrototypeOf, 1, lengthFlags), propertyFlags), ["propertyIsEnumerable"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "propertyIsEnumerable", PropertyIsEnumerable, 1, lengthFlags), propertyFlags) }; SetProperties(properties); }
public ClrFunctionInstance( Engine engine, string name, Func <JsValue, JsValue[], JsValue> func, int length = 0, PropertyFlag lengthFlags = PropertyFlag.AllForbidden) : base(engine, engine.Realm, name != null ? new JsString(name) : null) { _name = name; _func = func; _prototype = engine._originalIntrinsics.Function.PrototypeObject; _length = lengthFlags == PropertyFlag.AllForbidden ? PropertyDescriptor.AllForbiddenDescriptor.ForNumber(length) : new PropertyDescriptor(JsNumber.Create(length), lengthFlags); }
protected override void Initialize() { const PropertyFlag lengthFlags = PropertyFlag.Configurable; var properties = new PropertyDictionary(3, checkExistingKeys: false) { ["byteLength"] = new GetSetPropertyDescriptor(new ClrFunctionInstance(_engine, "get byteLength", ByteLength, 0, lengthFlags), Undefined, PropertyFlag.Configurable), ["constructor"] = new PropertyDescriptor(_constructor, PropertyFlag.NonEnumerable), ["slice"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "slice", Slice, 2, lengthFlags), PropertyFlag.Configurable | PropertyFlag.Writable) }; SetProperties(properties); var symbols = new SymbolDictionary(1) { [GlobalSymbolRegistry.ToStringTag] = new PropertyDescriptor("ArrayBuffer", PropertyFlag.Configurable) }; SetSymbols(symbols); }
protected override void Initialize() { const PropertyFlag lengthFlags = PropertyFlag.Configurable; const PropertyFlag propertyFlags = PropertyFlag.Configurable; SetProperties(new PropertyDictionary(5, checkExistingKeys: false) { ["length"] = new PropertyDescriptor(JsNumber.PositiveZero, propertyFlags), ["constructor"] = new PropertyDescriptor(_symbolConstructor, PropertyFlag.Configurable | PropertyFlag.Writable), ["description"] = new GetSetPropertyDescriptor(new ClrFunctionInstance(Engine, "description", Description, 0, lengthFlags), Undefined, propertyFlags), ["toString"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "toString", ToSymbolString, 0, lengthFlags), PropertyFlag.Configurable | PropertyFlag.Writable), ["valueOf"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "valueOf", ValueOf, 0, lengthFlags), PropertyFlag.Configurable | PropertyFlag.Writable) }); SetSymbols(new SymbolDictionary(1) { [GlobalSymbolRegistry.ToPrimitive] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "[Symbol.toPrimitive]", ToPrimitive, 1, lengthFlags), propertyFlags), [GlobalSymbolRegistry.ToStringTag] = new PropertyDescriptor(new JsString("Symbol"), propertyFlags) } ); }
protected override void Initialize() { const PropertyFlag lengthFlags = PropertyFlag.Configurable; const PropertyFlag propertyFlags = PropertyFlag.Configurable | PropertyFlag.Writable; var properties = new PropertyDictionary(24, checkExistingKeys: false) { ["buffer"] = new GetSetPropertyDescriptor(new ClrFunctionInstance(_engine, "get buffer", Buffer, 0, lengthFlags), Undefined, PropertyFlag.Configurable), ["byteLength"] = new GetSetPropertyDescriptor(new ClrFunctionInstance(_engine, "get byteLength", ByteLength, 0, lengthFlags), Undefined, PropertyFlag.Configurable), ["byteOffset"] = new GetSetPropertyDescriptor(new ClrFunctionInstance(Engine, "get byteOffset", ByteOffset, 0, lengthFlags), Undefined, PropertyFlag.Configurable), ["constructor"] = new PropertyDescriptor(_constructor, PropertyFlag.NonEnumerable), ["getBigInt64"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "getBigInt64", GetBigInt64, length: 1, lengthFlags), propertyFlags), ["getBigUint64"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "getBigUint64", GetBigUint64, length: 1, lengthFlags), propertyFlags), ["getFloat32"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "getFloat32", GetFloat32, length: 1, lengthFlags), propertyFlags), ["getFloat64"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "getFloat64", GetFloat64, length: 1, lengthFlags), propertyFlags), ["getInt8"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "getInt8", GetInt8, length: 1, lengthFlags), propertyFlags), ["getInt16"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "getInt16", GetInt16, length: 1, lengthFlags), propertyFlags), ["getInt32"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "getInt32", GetInt32, length: 1, lengthFlags), propertyFlags), ["getUint8"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "getUint8", GetUint8, length: 1, lengthFlags), propertyFlags), ["getUint16"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "getUint16", GetUint16, length: 1, lengthFlags), propertyFlags), ["getUint32"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "getUint32", GetUint32, length: 1, lengthFlags), propertyFlags), ["setBigInt64"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "setBigInt64", SetBigInt64, length: 2, lengthFlags), propertyFlags), ["setBigUint64"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "setBigUint64", SetBigUint64, length: 2, lengthFlags), propertyFlags), ["setFloat32"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "setFloat32", SetFloat32, length: 2, lengthFlags), propertyFlags), ["setFloat64"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "setFloat64", SetFloat64, length: 2, lengthFlags), propertyFlags), ["setInt8"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "setInt8", SetInt8, length: 2, lengthFlags), propertyFlags), ["setInt16"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "setInt16", SetInt16, length: 2, lengthFlags), propertyFlags), ["setInt32"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "setInt32", SetInt32, length: 2, lengthFlags), propertyFlags), ["setUint8"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "setUint8", SetUint8, length: 2, lengthFlags), propertyFlags), ["setUint16"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "setUint16", SetUint16, length: 2, lengthFlags), propertyFlags), ["setUint32"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "setUint32", SetUint32, length: 2, lengthFlags), propertyFlags) }; SetProperties(properties); var symbols = new SymbolDictionary(1) { [GlobalSymbolRegistry.ToStringTag] = new PropertyDescriptor("DataView", PropertyFlag.Configurable) }; SetSymbols(symbols); }
protected override void Initialize() { const PropertyFlag lengthFlags = PropertyFlag.Configurable; GetSetPropertyDescriptor CreateGetAccessorDescriptor(string name, Func <RegExpInstance, JsValue> valueExtractor, JsValue protoValue = null) { return(new GetSetPropertyDescriptor( get: new ClrFunctionInstance(Engine, name, (thisObj, arguments) => { if (ReferenceEquals(thisObj, this)) { return protoValue ?? Undefined; } if (!(thisObj is RegExpInstance r)) { return ExceptionHelper.ThrowTypeError <JsValue>(_engine); } return valueExtractor(r); }, 0, lengthFlags),
protected override void Initialize() { const PropertyFlag propertyFlags = PropertyFlag.Configurable | PropertyFlag.Writable; var properties = new PropertyDictionary(5, checkExistingKeys: false) { ["length"] = new PropertyDescriptor(0, PropertyFlag.Configurable), ["constructor"] = new PropertyDescriptor(_constructor, PropertyFlag.NonEnumerable), ["delete"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "delete", Delete, 1, PropertyFlag.Configurable), propertyFlags), ["add"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "add", Add, 1, PropertyFlag.Configurable), propertyFlags), ["has"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "has", Has, 1, PropertyFlag.Configurable), propertyFlags), }; SetProperties(properties); var symbols = new SymbolDictionary(1) { [GlobalSymbolRegistry.ToStringTag] = new PropertyDescriptor("WeakSet", false, false, true) }; SetSymbols(symbols); }
protected override void Initialize() { const PropertyFlag propertyFlags = PropertyFlag.Configurable | PropertyFlag.Writable; var properties = new PropertyDictionary(Props?.Count ?? 0, checkExistingKeys: false); if (Props != null) { foreach (var kp in Props) { if (kp.Value is CpuApiFunc f) { properties[kp.Key] = new PropertyDescriptor(new ClrFunctionInstance(Engine, kp.Key, (t, a) => _constructor.WrappedApi(t, a, f.Implementation), f.Arguments.Count, PropertyFlag.Configurable), propertyFlags); } else if (kp.Value is CpuApiValue v) { properties[kp.Key] = new PropertyDescriptor(v.Value, PropertyFlag.Configurable | PropertyFlag.Writable); } } } SetProperties(properties); }
private JsValue Bind(JsValue thisObj, JsValue[] arguments) { var target = thisObj.TryCast <ICallable>(x => { ExceptionHelper.ThrowTypeError(Engine); }); var thisArg = arguments.At(0); var f = new BindFunctionInstance(Engine) { Extensible = true }; f.TargetFunction = thisObj; f.BoundThis = thisArg; f.BoundArgs = arguments.Skip(1); f.Prototype = Engine.Function.PrototypeObject; var o = target as FunctionInstance; if (!ReferenceEquals(o, null)) { var l = TypeConverter.ToNumber(o.Get("length")) - (arguments.Length - 1); f.SetOwnProperty("length", new PropertyDescriptor(System.Math.Max(l, 0), PropertyFlag.AllForbidden)); } else { f.SetOwnProperty("length", new PropertyDescriptor(0, PropertyFlag.AllForbidden)); } var thrower = Engine.Function.ThrowTypeError; const PropertyFlag flags = PropertyFlag.EnumerableSet | PropertyFlag.ConfigurableSet; f.DefineOwnProperty("caller", new GetSetPropertyDescriptor(thrower, thrower, flags), false); f.DefineOwnProperty("arguments", new GetSetPropertyDescriptor(thrower, thrower, flags), false); return(f); }
protected override void Initialize() { const PropertyFlag propertyFlags = PropertyFlag.Configurable | PropertyFlag.Writable; const PropertyFlag lengthFlags = PropertyFlag.Configurable; var properties = new PropertyDictionary(7, checkExistingKeys: false) { ["constructor"] = new PropertyDescriptor(_realm.Intrinsics.Function, PropertyFlag.NonEnumerable), ["toString"] = new PropertyDescriptor(new ClrFunctionInstance(_engine, "toString", ToString, 0, lengthFlags), propertyFlags), ["apply"] = new PropertyDescriptor(new ClrFunctionInstance(_engine, "apply", Apply, 2, lengthFlags), propertyFlags), ["call"] = new PropertyDescriptor(new ClrFunctionInstance(_engine, "call", CallImpl, 1, lengthFlags), propertyFlags), ["bind"] = new PropertyDescriptor(new ClrFunctionInstance(_engine, "bind", Bind, 1, lengthFlags), propertyFlags), ["arguments"] = new GetSetPropertyDescriptor.ThrowerPropertyDescriptor(_engine, PropertyFlag.Configurable | PropertyFlag.CustomJsValue), ["caller"] = new GetSetPropertyDescriptor.ThrowerPropertyDescriptor(_engine, PropertyFlag.Configurable | PropertyFlag.CustomJsValue) }; SetProperties(properties); var symbols = new SymbolDictionary(1) { [GlobalSymbolRegistry.HasInstance] = new PropertyDescriptor(new ClrFunctionInstance(_engine, "[Symbol.hasInstance]", HasInstance, 1, PropertyFlag.Configurable), PropertyFlag.AllForbidden) }; SetSymbols(symbols); }
private int CreateMaskForWrite(int i, int propertyCount, PropertyFlag propertyFlag) { var mask = CreateMaskForRead(i, propertyFlag); if (i == _bits.Length - 1) { var overlay = 0x55555555 << (int)propertyFlag; var shift = (propertyCount * BitsForPropertyFlags + BitsForAdditionalState) % BitsPerInt; overlay = shift != 0 ? overlay << shift : 0; mask &= ~overlay; } return mask; }
public ThrowerPropertyDescriptor(Engine engine, PropertyFlag flags) : base(flags) { _engine = engine; }
internal GetSetPropertyDescriptor(JsValue get, JsValue set, PropertyFlag flags) : base(null, flags) { _get = get; _set = set; }
public bool IsPropertyFlagged(int propertyIndex, PropertyFlag propertyFlag) { propertyIndex = propertyIndex * BitsForPropertyFlags + (int)propertyFlag + BitsForAdditionalState; return (_bits[propertyIndex / BitsPerInt] & (1 << propertyIndex % BitsPerInt)) != 0; }
public bool AnyPropertiesFlagged(PropertyFlag propertyFlag) => _bits.Where((t, i) => (t & CreateMaskForRead(i, propertyFlag)) != 0).Any();
/// <summary> /// Creates this object and sets all its propertues /// </summary> /// <param name="id">The id of the property</param> /// <param name="type">The <see cref="PropertyType" /></param> /// <param name="flags">The <see cref="PropertyFlag" /></param> /// <param name="data">The property data</param> internal Property(ushort id, PropertyType type, PropertyFlag flags, byte[] data) { Id = id; Type = type; Flags = Convert.ToUInt32(flags); Data = data; }