ArrayBufferConstructor.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. internal 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. private 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. /// https://tc39.es/ecma262/#sec-arraybuffer.isview
  44. /// </summary>
  45. private static JsValue IsView(JsValue thisObject, JsValue[] arguments)
  46. {
  47. var arg = arguments.At(0);
  48. return arg is JsDataView or JsTypedArray;
  49. }
  50. /// <summary>
  51. /// https://tc39.es/ecma262/#sec-get-arraybuffer-@@species
  52. /// </summary>
  53. private static JsValue Species(JsValue thisObject, JsValue[] arguments)
  54. {
  55. return thisObject;
  56. }
  57. public override ObjectInstance Construct(JsValue[] arguments, JsValue newTarget)
  58. {
  59. if (newTarget.IsUndefined())
  60. {
  61. ExceptionHelper.ThrowTypeError(_realm);
  62. }
  63. var length = arguments.At(0);
  64. var options = arguments.At(1);
  65. var byteLength = TypeConverter.ToIndex(_realm, length);
  66. var requestedMaxByteLength = options.GetArrayBufferMaxByteLengthOption();
  67. return AllocateArrayBuffer(newTarget, byteLength, requestedMaxByteLength);
  68. }
  69. /// <summary>
  70. /// https://tc39.es/ecma262/#sec-allocatearraybuffer
  71. /// </summary>
  72. internal JsArrayBuffer AllocateArrayBuffer(JsValue constructor, ulong byteLength, uint? maxByteLength = null)
  73. {
  74. var allocatingResizableBuffer = maxByteLength != null;
  75. if (allocatingResizableBuffer && byteLength > maxByteLength)
  76. {
  77. ExceptionHelper.ThrowRangeError(_realm);
  78. }
  79. var obj = OrdinaryCreateFromConstructor(
  80. constructor,
  81. static intrinsics => intrinsics.ArrayBuffer.PrototypeObject,
  82. static (engine, _, state) => new JsArrayBuffer(engine, state!.Item1),
  83. new Tuple<uint?>(maxByteLength));
  84. var block = byteLength > 0 ? JsArrayBuffer.CreateByteDataBlock(_realm, byteLength) : System.Array.Empty<byte>();
  85. obj._arrayBufferData = block;
  86. return obj;
  87. }
  88. }