ArrayBufferConstructor.cs 3.3 KB

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