123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- using Jint.Collections;
- using Jint.Native.Function;
- using Jint.Native.Object;
- using Jint.Native.Symbol;
- using Jint.Runtime;
- using Jint.Runtime.Descriptors;
- using Jint.Runtime.Descriptors.Specialized;
- using Jint.Runtime.Interop;
- namespace Jint.Native.Set
- {
- public sealed class SetConstructor : FunctionInstance, IConstructor
- {
- private static readonly JsString _functionName = new JsString("Set");
- private SetConstructor(Engine engine)
- : base(engine, _functionName, FunctionThisMode.Global)
- {
- }
- public SetPrototype PrototypeObject { get; private set; }
- public static SetConstructor CreateSetConstructor(Engine engine)
- {
- var obj = new SetConstructor(engine)
- {
- _prototype = engine.Function.PrototypeObject
- };
- // The value of the [[Prototype]] internal property of the Set constructor is the Function prototype object
- obj.PrototypeObject = SetPrototype.CreatePrototypeObject(engine, obj);
- obj._length = new PropertyDescriptor(0, PropertyFlag.Configurable);
- // The initial value of Set.prototype is the Set prototype object
- obj._prototypeDescriptor = new PropertyDescriptor(obj.PrototypeObject, PropertyFlag.AllForbidden);
- return obj;
- }
- protected override void Initialize()
- {
- var symbols = new SymbolDictionary(1)
- {
- [GlobalSymbolRegistry.Species] = new GetSetPropertyDescriptor(get: new ClrFunctionInstance(_engine, "get [Symbol.species]", Species, 0, PropertyFlag.Configurable), set: Undefined, PropertyFlag.Configurable)
- };
- SetSymbols(symbols);
- }
- private static JsValue Species(JsValue thisObject, JsValue[] arguments)
- {
- return thisObject;
- }
- public override JsValue Call(JsValue thisObject, JsValue[] arguments)
- {
- if (thisObject.IsUndefined())
- {
- ExceptionHelper.ThrowTypeError(_engine, "Constructor Set requires 'new'");
- }
- return Construct(arguments, thisObject);
- }
- public ObjectInstance Construct(JsValue[] arguments, JsValue newTarget)
- {
- var set = new SetInstance(Engine)
- {
- _prototype = PrototypeObject
- };
- if (arguments.Length > 0 && !arguments[0].IsNullOrUndefined())
- {
- var adderValue = set.Get("add");
- if (!(adderValue is ICallable adder))
- {
- return ExceptionHelper.ThrowTypeError<ObjectInstance>(_engine, "add must be callable");
- }
- var iterable = arguments.At(0).GetIterator(_engine);
- try
- {
- var args = new JsValue[1];
- do
- {
- if (!iterable.TryIteratorStep(out var next))
- {
- return set;
- }
- next.TryGetValue(CommonProperties.Value, out var nextValue);
- args[0] = nextValue;
- adder.Call(set, args);
- } while (true);
- }
- catch
- {
- iterable.Close(CompletionType.Throw);
- throw;
- }
- }
- return set;
- }
- }
- }
|