ArrayBufferConstructor.cs 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. using Jint.Collections;
  2. using Jint.Native.DataView;
  3. using Jint.Native.Function;
  4. using Jint.Native.Object;
  5. using Jint.Native.Symbol;
  6. using Jint.Runtime;
  7. using Jint.Runtime.Descriptors;
  8. using Jint.Runtime.Interop;
  9. namespace Jint.Native.ArrayBuffer
  10. {
  11. /// <summary>
  12. /// https://tc39.es/ecma262/#sec-properties-of-the-arraybuffer-constructor
  13. /// </summary>
  14. public sealed class ArrayBufferConstructor : FunctionInstance, IConstructor
  15. {
  16. private static readonly JsString _functionName = new("ArrayBuffer");
  17. internal ArrayBufferConstructor(
  18. Engine engine,
  19. Realm realm,
  20. FunctionPrototype functionPrototype,
  21. ObjectPrototype objectPrototype)
  22. : base(engine, realm, _functionName)
  23. {
  24. _prototype = functionPrototype;
  25. PrototypeObject = new ArrayBufferPrototype(engine, realm, this, objectPrototype);
  26. _length = new PropertyDescriptor(1, PropertyFlag.Configurable);
  27. _prototypeDescriptor = new PropertyDescriptor(PrototypeObject, PropertyFlag.AllForbidden);
  28. }
  29. public ArrayBufferPrototype PrototypeObject { get; }
  30. protected override void Initialize()
  31. {
  32. const PropertyFlag lengthFlags = PropertyFlag.Configurable;
  33. var properties = new PropertyDictionary(1, checkExistingKeys: false)
  34. {
  35. ["isView"] = new PropertyDescriptor(new PropertyDescriptor(new ClrFunctionInstance(Engine, "isView", IsView, 1, lengthFlags), PropertyFlag.Configurable | PropertyFlag.Writable)),
  36. };
  37. SetProperties(properties);
  38. var symbols = new SymbolDictionary(1)
  39. {
  40. [GlobalSymbolRegistry.Species] = new GetSetPropertyDescriptor(get: new ClrFunctionInstance(Engine, "get [Symbol.species]", Species, 0, lengthFlags), set: Undefined,PropertyFlag.Configurable),
  41. };
  42. SetSymbols(symbols);
  43. }
  44. /// <summary>
  45. /// https://tc39.es/ecma262/#sec-arraybuffer.isview
  46. /// </summary>
  47. private static JsValue IsView(JsValue thisObject, JsValue[] arguments)
  48. {
  49. var arg = arguments.At(0);
  50. return arg is DataViewInstance;
  51. }
  52. /// <summary>
  53. /// https://tc39.es/ecma262/#sec-get-arraybuffer-@@species
  54. /// </summary>
  55. private static JsValue Species(JsValue thisObject, JsValue[] arguments)
  56. {
  57. return thisObject;
  58. }
  59. public override JsValue Call(JsValue thisObject, JsValue[] arguments)
  60. {
  61. ExceptionHelper.ThrowTypeError(_realm, "Constructor ArrayBuffer requires 'new'");
  62. return Undefined;
  63. }
  64. public ObjectInstance Construct(JsValue[] arguments, JsValue newTarget)
  65. {
  66. if (newTarget.IsUndefined())
  67. {
  68. ExceptionHelper.ThrowTypeError(_realm);
  69. }
  70. var byteLength = TypeConverter.ToIndex(_realm, arguments.At(0));
  71. return AllocateArrayBuffer(newTarget, byteLength);
  72. }
  73. internal ArrayBufferInstance AllocateArrayBuffer(JsValue constructor, uint byteLength)
  74. {
  75. var obj = OrdinaryCreateFromConstructor(
  76. constructor,
  77. static intrinsics => intrinsics.ArrayBuffer.PrototypeObject,
  78. static (engine, realm, state) => new ArrayBufferInstance(engine, (uint) ((JsNumber) state)._value),
  79. JsNumber.Create(byteLength));
  80. return obj;
  81. }
  82. }
  83. }