1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- using Jint.Collections;
- using Jint.Native.Object;
- using Jint.Native.Symbol;
- using Jint.Runtime;
- using Jint.Runtime.Descriptors;
- using Jint.Runtime.Interop;
- namespace Jint.Native.WeakSet
- {
- /// <summary>
- /// https://tc39.es/ecma262/#sec-weakset-objects
- /// </summary>
- internal sealed class WeakSetPrototype : Prototype
- {
- private readonly WeakSetConstructor _constructor;
- internal ClrFunctionInstance _originalAddFunction = null!;
- internal WeakSetPrototype(
- Engine engine,
- Realm realm,
- WeakSetConstructor constructor,
- ObjectPrototype prototype) : base(engine, realm)
- {
- _prototype = prototype;
- _constructor = constructor;
- }
- protected override void Initialize()
- {
- _originalAddFunction = new ClrFunctionInstance(Engine, "add", Add, 1, PropertyFlag.Configurable);
- const PropertyFlag PropertyFlags = PropertyFlag.Configurable | PropertyFlag.Writable;
- var properties = new PropertyDictionary(5, checkExistingKeys: false)
- {
- ["length"] = new(0, PropertyFlag.Configurable),
- ["constructor"] = new(_constructor, PropertyFlag.NonEnumerable),
- ["delete"] = new(new ClrFunctionInstance(Engine, "delete", Delete, 1, PropertyFlag.Configurable), PropertyFlags),
- ["add"] = new(_originalAddFunction, PropertyFlags),
- ["has"] = new(new ClrFunctionInstance(Engine, "has", Has, 1, PropertyFlag.Configurable), PropertyFlags),
- };
- SetProperties(properties);
- var symbols = new SymbolDictionary(1)
- {
- [GlobalSymbolRegistry.ToStringTag] = new("WeakSet", false, false, true)
- };
- SetSymbols(symbols);
- }
- private JsValue Add(JsValue thisObj, JsValue[] arguments)
- {
- var set = AssertWeakSetInstance(thisObj);
- set.WeakSetAdd(arguments.At(0));
- return thisObj;
- }
- private JsValue Delete(JsValue thisObj, JsValue[] arguments)
- {
- var set = AssertWeakSetInstance(thisObj);
- return set.WeakSetDelete(arguments.At(0)) ? JsBoolean.True : JsBoolean.False;
- }
- private JsValue Has(JsValue thisObj, JsValue[] arguments)
- {
- var set = AssertWeakSetInstance(thisObj);
- return set.WeakSetHas(arguments.At(0)) ? JsBoolean.True : JsBoolean.False;
- }
- private WeakSetInstance AssertWeakSetInstance(JsValue thisObj)
- {
- var set = thisObj as WeakSetInstance;
- if (set is null)
- {
- ExceptionHelper.ThrowTypeError(_realm, "object must be a WeakSet");
- }
- return set;
- }
- }
- }
|