ArrayBufferConstructor.cs 4.6 KB

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