2
0

ArrayBufferConstructor.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. using Jint.Collections;
  2. using Jint.Native.Function;
  3. using Jint.Native.Object;
  4. using Jint.Native.Symbol;
  5. using Jint.Runtime;
  6. using Jint.Runtime.Descriptors;
  7. using Jint.Runtime.Interop;
  8. namespace Jint.Native.ArrayBuffer;
  9. /// <summary>
  10. /// https://tc39.es/ecma262/#sec-properties-of-the-arraybuffer-constructor
  11. /// </summary>
  12. public sealed class ArrayBufferConstructor : Constructor
  13. {
  14. private static readonly JsString _functionName = new("ArrayBuffer");
  15. internal ArrayBufferConstructor(
  16. Engine engine,
  17. Realm realm,
  18. FunctionPrototype functionPrototype,
  19. ObjectPrototype objectPrototype)
  20. : base(engine, realm, _functionName)
  21. {
  22. _prototype = functionPrototype;
  23. PrototypeObject = new ArrayBufferPrototype(engine, this, objectPrototype);
  24. _length = new PropertyDescriptor(1, PropertyFlag.Configurable);
  25. _prototypeDescriptor = new PropertyDescriptor(PrototypeObject, PropertyFlag.AllForbidden);
  26. }
  27. internal ArrayBufferPrototype PrototypeObject { get; }
  28. protected override void Initialize()
  29. {
  30. const PropertyFlag lengthFlags = PropertyFlag.Configurable;
  31. var properties = new PropertyDictionary(1, checkExistingKeys: false)
  32. {
  33. ["isView"] = new PropertyDescriptor(new PropertyDescriptor(new ClrFunction(Engine, "isView", IsView, 1, lengthFlags), PropertyFlag.Configurable | PropertyFlag.Writable)),
  34. };
  35. SetProperties(properties);
  36. var symbols = new SymbolDictionary(1)
  37. {
  38. [GlobalSymbolRegistry.Species] = new GetSetPropertyDescriptor(get: new ClrFunction(Engine, "get [Symbol.species]", Species, 0, lengthFlags), set: Undefined,PropertyFlag.Configurable),
  39. };
  40. SetSymbols(symbols);
  41. }
  42. /// <summary>
  43. /// Constructs a new JsArrayBuffer instance and takes ownership of the given byte array and uses it as backing store.
  44. /// </summary>
  45. public JsArrayBuffer Construct(byte[] data)
  46. {
  47. return CreateJsArrayBuffer(this, data, byteLength: (ulong) data.Length, maxByteLength: null);
  48. }
  49. /// <summary>
  50. /// Constructs a new JsArrayBuffer with given byte length and optional max byte length.
  51. /// </summary>
  52. public JsArrayBuffer Construct(ulong byteLength, uint? maxByteLength = null)
  53. {
  54. return AllocateArrayBuffer(this, byteLength, maxByteLength);
  55. }
  56. public override ObjectInstance Construct(JsValue[] arguments, JsValue newTarget)
  57. {
  58. if (newTarget.IsUndefined())
  59. {
  60. ExceptionHelper.ThrowTypeError(_realm);
  61. }
  62. var length = arguments.At(0);
  63. var options = arguments.At(1);
  64. var byteLength = TypeConverter.ToIndex(_realm, length);
  65. var requestedMaxByteLength = options.GetArrayBufferMaxByteLengthOption();
  66. return AllocateArrayBuffer(newTarget, byteLength, requestedMaxByteLength);
  67. }
  68. /// <summary>
  69. /// https://tc39.es/ecma262/#sec-get-arraybuffer-@@species
  70. /// </summary>
  71. private static JsValue Species(JsValue thisObject, JsValue[] arguments)
  72. {
  73. return thisObject;
  74. }
  75. /// <summary>
  76. /// https://tc39.es/ecma262/#sec-arraybuffer.isview
  77. /// </summary>
  78. private static JsValue IsView(JsValue thisObject, JsValue[] arguments)
  79. {
  80. var arg = arguments.At(0);
  81. return arg is JsDataView or JsTypedArray;
  82. }
  83. /// <summary>
  84. /// https://tc39.es/ecma262/#sec-allocatearraybuffer
  85. /// </summary>
  86. internal JsArrayBuffer AllocateArrayBuffer(JsValue constructor, ulong byteLength, uint? maxByteLength = null)
  87. {
  88. var allocatingResizableBuffer = maxByteLength != null;
  89. if (allocatingResizableBuffer && byteLength > maxByteLength)
  90. {
  91. ExceptionHelper.ThrowRangeError(_realm);
  92. }
  93. return CreateJsArrayBuffer(constructor, block: null, byteLength, maxByteLength);
  94. }
  95. private JsArrayBuffer CreateJsArrayBuffer(JsValue constructor, byte[]? block, ulong byteLength, uint? maxByteLength)
  96. {
  97. var obj = OrdinaryCreateFromConstructor(
  98. constructor,
  99. static intrinsics => intrinsics.ArrayBuffer.PrototypeObject,
  100. static (engine, _, state) =>
  101. {
  102. var buffer = new JsArrayBuffer(engine, [], state.MaxByteLength)
  103. {
  104. _arrayBufferData = state.Block ?? (state.ByteLength > 0 ? JsArrayBuffer.CreateByteDataBlock(engine.Realm, state.ByteLength) : []),
  105. };
  106. return buffer;
  107. },
  108. new ConstructState(block, byteLength, maxByteLength));
  109. return obj;
  110. }
  111. private readonly record struct ConstructState(byte[]? Block, ulong ByteLength, uint? MaxByteLength);
  112. }