2
0

SharedArrayBufferConstructor.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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.SharedArrayBuffer;
  9. /// <summary>
  10. /// https://tc39.es/ecma262/#sec-sharedarraybuffer-constructor
  11. /// </summary>
  12. internal sealed class SharedArrayBufferConstructor : Constructor
  13. {
  14. private static readonly JsString _functionName = new("SharedArrayBuffer");
  15. internal SharedArrayBufferConstructor(
  16. Engine engine,
  17. Realm realm,
  18. FunctionPrototype functionPrototype,
  19. ObjectPrototype objectPrototype)
  20. : base(engine, realm, _functionName)
  21. {
  22. _prototype = functionPrototype;
  23. PrototypeObject = new SharedArrayBufferPrototype(engine, this, objectPrototype);
  24. _length = new PropertyDescriptor(1, PropertyFlag.Configurable);
  25. _prototypeDescriptor = new PropertyDescriptor(PrototypeObject, PropertyFlag.AllForbidden);
  26. }
  27. private SharedArrayBufferPrototype 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, JsCallArguments 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, JsCallArguments arguments)
  54. {
  55. return thisObject;
  56. }
  57. protected internal override JsValue Call(JsValue thisObject, JsCallArguments arguments)
  58. {
  59. ExceptionHelper.ThrowTypeError(_realm, "Constructor SharedArrayBuffer requires 'new'");
  60. return Undefined;
  61. }
  62. public override ObjectInstance Construct(JsCallArguments arguments, JsValue newTarget)
  63. {
  64. if (newTarget.IsUndefined())
  65. {
  66. ExceptionHelper.ThrowTypeError(_realm);
  67. }
  68. var length = arguments.At(0);
  69. var options = arguments.At(1);
  70. var byteLength = TypeConverter.ToIndex(_realm, length);
  71. var requestedMaxByteLength = options.GetArrayBufferMaxByteLengthOption();
  72. return AllocateSharedArrayBuffer(newTarget, byteLength, requestedMaxByteLength);
  73. }
  74. private JsSharedArrayBuffer AllocateSharedArrayBuffer(JsValue constructor, uint byteLength, uint? maxByteLength = null)
  75. {
  76. var allocatingGrowableBuffer = maxByteLength != null;
  77. if (allocatingGrowableBuffer && byteLength > maxByteLength)
  78. {
  79. ExceptionHelper.ThrowRangeError(_realm);
  80. }
  81. var allocLength = maxByteLength.GetValueOrDefault(byteLength);
  82. var obj = OrdinaryCreateFromConstructor(
  83. constructor,
  84. static intrinsics => intrinsics.SharedArrayBuffer.PrototypeObject,
  85. static (engine, _, state) =>
  86. {
  87. var buffer = new JsSharedArrayBuffer(engine, [], state.MaxByteLength, state.ArrayBufferByteLengthData)
  88. {
  89. _arrayBufferData = state.Block ?? (state.ByteLength > 0 ? JsSharedArrayBuffer.CreateSharedByteDataBlock(engine.Realm, state.ByteLength) : []),
  90. };
  91. return buffer;
  92. },
  93. new ConstructState(Block: null, allocLength, maxByteLength, byteLength));
  94. return obj;
  95. }
  96. private readonly record struct ConstructState(byte[]? Block, uint ByteLength, uint? MaxByteLength, uint ArrayBufferByteLengthData);
  97. }