1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- using Jint.Collections;
- using Jint.Native.Function;
- using Jint.Native.Iterator;
- using Jint.Native.Object;
- using Jint.Native.Symbol;
- using Jint.Runtime;
- using Jint.Runtime.Descriptors;
- using Jint.Runtime.Interop;
- namespace Jint.Native.Map
- {
- public sealed class MapConstructor : FunctionInstance, IConstructor
- {
- private static readonly JsString _functionName = new JsString("Map");
- private MapConstructor(Engine engine)
- : base(engine, _functionName)
- {
- }
- public MapPrototype PrototypeObject { get; private set; }
- public static MapConstructor CreateMapConstructor(Engine engine)
- {
- var obj = new MapConstructor(engine)
- {
- _prototype = engine.Function.PrototypeObject
- };
- // The value of the [[Prototype]] internal property of the Map constructor is the Function prototype object
- obj.PrototypeObject = MapPrototype.CreatePrototypeObject(engine, obj);
- obj._length = new PropertyDescriptor(0, PropertyFlag.Configurable);
- // The initial value of Map.prototype is the Map 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 Map requires 'new'");
- }
- return Construct(arguments, thisObject);
- }
- public ObjectInstance Construct(JsValue[] arguments, JsValue newTarget)
- {
- var map = new MapInstance(Engine)
- {
- _prototype = PrototypeObject
- };
- if (arguments.Length > 0 && !arguments[0].IsNullOrUndefined())
- {
- var adder = map.Get("set");
- var iterator = arguments.At(0).GetIterator(_engine);
- IteratorProtocol.AddEntriesFromIterable(map, iterator, adder);
- }
- return map;
- }
- }
- }
|