DataViewConstructor.cs 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. using Jint.Native.ArrayBuffer;
  2. using Jint.Native.Function;
  3. using Jint.Native.Object;
  4. using Jint.Runtime;
  5. using Jint.Runtime.Descriptors;
  6. namespace Jint.Native.DataView
  7. {
  8. /// <summary>
  9. /// https://tc39.es/ecma262/#sec-dataview-constructor
  10. /// </summary>
  11. internal sealed class DataViewConstructor : Constructor
  12. {
  13. private static readonly JsString _functionName = new("DataView");
  14. internal DataViewConstructor(
  15. Engine engine,
  16. Realm realm,
  17. FunctionPrototype functionPrototype,
  18. ObjectPrototype objectPrototype)
  19. : base(engine, realm, _functionName)
  20. {
  21. _prototype = functionPrototype;
  22. PrototypeObject = new DataViewPrototype(engine, this, objectPrototype);
  23. _length = new PropertyDescriptor(1, PropertyFlag.Configurable);
  24. _prototypeDescriptor = new PropertyDescriptor(PrototypeObject, PropertyFlag.AllForbidden);
  25. }
  26. private DataViewPrototype PrototypeObject { get; }
  27. public override ObjectInstance Construct(JsValue[] arguments, JsValue newTarget)
  28. {
  29. if (newTarget.IsUndefined())
  30. {
  31. ExceptionHelper.ThrowTypeError(_realm);
  32. }
  33. var buffer = arguments.At(0) as JsArrayBuffer;
  34. var byteOffset = arguments.At(1);
  35. var byteLength = arguments.At(2);
  36. if (buffer is null)
  37. {
  38. ExceptionHelper.ThrowTypeError(_realm, "First argument to DataView constructor must be an ArrayBuffer");
  39. }
  40. var offset = TypeConverter.ToIndex(_realm, byteOffset);
  41. if (buffer.IsDetachedBuffer)
  42. {
  43. ExceptionHelper.ThrowTypeError(_realm);
  44. }
  45. var bufferByteLength = (uint) buffer.ArrayBufferByteLength;
  46. if (offset > bufferByteLength)
  47. {
  48. ExceptionHelper.ThrowRangeError(_realm, "Start offset " + offset + " is outside the bounds of the buffer");
  49. }
  50. uint viewByteLength;
  51. if (byteLength.IsUndefined())
  52. {
  53. viewByteLength = bufferByteLength - offset;
  54. }
  55. else
  56. {
  57. viewByteLength = TypeConverter.ToIndex(_realm, byteLength);
  58. if (offset + viewByteLength > bufferByteLength)
  59. {
  60. ExceptionHelper.ThrowRangeError(_realm, "Invalid DataView length");
  61. }
  62. }
  63. var o = OrdinaryCreateFromConstructor(
  64. newTarget,
  65. static intrinsics => intrinsics.DataView.PrototypeObject,
  66. static (Engine engine, Realm _, object? _) => new JsDataView(engine));
  67. if (buffer.IsDetachedBuffer)
  68. {
  69. ExceptionHelper.ThrowTypeError(_realm);
  70. }
  71. o._viewedArrayBuffer = buffer;
  72. o._byteLength = viewByteLength;
  73. o._byteOffset = offset;
  74. return o;
  75. }
  76. }
  77. }