ArrayBufferPrototype.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. #pragma warning disable CA1859 // Use concrete types when possible for improved performance -- most of prototype methods return JsValue
  2. using Jint.Collections;
  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. {
  10. /// <summary>
  11. /// https://tc39.es/ecma262/#sec-properties-of-the-arraybuffer-prototype-object
  12. /// </summary>
  13. internal sealed class ArrayBufferPrototype : Prototype
  14. {
  15. private readonly ArrayBufferConstructor _constructor;
  16. internal ArrayBufferPrototype(
  17. Engine engine,
  18. ArrayBufferConstructor constructor,
  19. ObjectPrototype objectPrototype) : base(engine, engine.Realm)
  20. {
  21. _prototype = objectPrototype;
  22. _constructor = constructor;
  23. }
  24. protected override void Initialize()
  25. {
  26. const PropertyFlag lengthFlags = PropertyFlag.Configurable;
  27. var properties = new PropertyDictionary(4, checkExistingKeys: false)
  28. {
  29. ["byteLength"] = new GetSetPropertyDescriptor(new ClrFunctionInstance(_engine, "get byteLength", ByteLength, 0, lengthFlags), Undefined, PropertyFlag.Configurable),
  30. ["constructor"] = new PropertyDescriptor(_constructor, PropertyFlag.NonEnumerable),
  31. ["detached"] = new GetSetPropertyDescriptor(new ClrFunctionInstance(_engine, "get detached", Detached, 0, lengthFlags), Undefined, PropertyFlag.Configurable),
  32. ["slice"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "slice", Slice, 2, lengthFlags), PropertyFlag.Configurable | PropertyFlag.Writable)
  33. };
  34. SetProperties(properties);
  35. var symbols = new SymbolDictionary(1)
  36. {
  37. [GlobalSymbolRegistry.ToStringTag] = new PropertyDescriptor("ArrayBuffer", PropertyFlag.Configurable)
  38. };
  39. SetSymbols(symbols);
  40. }
  41. private JsValue Detached(JsValue thisObject, JsValue[] arguments)
  42. {
  43. var o = thisObject as JsArrayBuffer;
  44. if (o is null)
  45. {
  46. ExceptionHelper.ThrowTypeError(_realm, "Method ArrayBuffer.prototype.detached called on incompatible receiver " + thisObject);
  47. }
  48. if (o.IsSharedArrayBuffer)
  49. {
  50. ExceptionHelper.ThrowTypeError(_realm);
  51. }
  52. return o.IsDetachedBuffer;
  53. }
  54. /// <summary>
  55. /// https://tc39.es/ecma262/#sec-get-arraybuffer.prototype.bytelength
  56. /// </summary>
  57. private JsValue ByteLength(JsValue thisObject, JsValue[] arguments)
  58. {
  59. var o = thisObject as JsArrayBuffer;
  60. if (o is null)
  61. {
  62. ExceptionHelper.ThrowTypeError(_realm, "Method ArrayBuffer.prototype.byteLength called on incompatible receiver " + thisObject);
  63. }
  64. if (o.IsSharedArrayBuffer)
  65. {
  66. ExceptionHelper.ThrowTypeError(_realm);
  67. }
  68. if (o.IsDetachedBuffer)
  69. {
  70. return JsNumber.PositiveZero;
  71. }
  72. return JsNumber.Create(o.ArrayBufferByteLength);
  73. }
  74. /// <summary>
  75. /// https://tc39.es/ecma262/#sec-arraybuffer.prototype.slice
  76. /// </summary>
  77. private JsValue Slice(JsValue thisObject, JsValue[] arguments)
  78. {
  79. var o = thisObject as JsArrayBuffer;
  80. if (o is null)
  81. {
  82. ExceptionHelper.ThrowTypeError(_realm, "Method ArrayBuffer.prototype.slice called on incompatible receiver " + thisObject);
  83. }
  84. if (o.IsSharedArrayBuffer)
  85. {
  86. ExceptionHelper.ThrowTypeError(_realm);
  87. }
  88. o.AssertNotDetached();
  89. var start = arguments.At(0);
  90. var end = arguments.At(1);
  91. var len = o.ArrayBufferByteLength;
  92. var relativeStart = TypeConverter.ToIntegerOrInfinity(start);
  93. var first = relativeStart switch
  94. {
  95. double.NegativeInfinity => 0,
  96. < 0 => (int) System.Math.Max(len + relativeStart, 0),
  97. _ => (int) System.Math.Min(relativeStart, len)
  98. };
  99. double relativeEnd;
  100. if (end.IsUndefined())
  101. {
  102. relativeEnd = len;
  103. }
  104. else
  105. {
  106. relativeEnd = TypeConverter.ToIntegerOrInfinity(end);
  107. }
  108. var final = relativeEnd switch
  109. {
  110. double.NegativeInfinity => 0,
  111. < 0 => (int) System.Math.Max(len + relativeEnd, 0),
  112. _ => (int) System.Math.Min(relativeEnd, len)
  113. };
  114. var newLen = System.Math.Max(final - first, 0);
  115. var ctor = SpeciesConstructor(o, _realm.Intrinsics.ArrayBuffer);
  116. var bufferInstance = Construct(ctor, new JsValue[] { JsNumber.Create(newLen) }) as JsArrayBuffer;
  117. if (bufferInstance is null)
  118. {
  119. ExceptionHelper.ThrowTypeError(_realm);
  120. }
  121. if (bufferInstance.IsSharedArrayBuffer)
  122. {
  123. ExceptionHelper.ThrowTypeError(_realm);
  124. }
  125. if (bufferInstance.IsDetachedBuffer)
  126. {
  127. ExceptionHelper.ThrowTypeError(_realm);
  128. }
  129. if (ReferenceEquals(bufferInstance, o))
  130. {
  131. ExceptionHelper.ThrowTypeError(_realm);
  132. }
  133. if (bufferInstance.ArrayBufferByteLength < newLen)
  134. {
  135. ExceptionHelper.ThrowTypeError(_realm);
  136. }
  137. // NOTE: Side-effects of the above steps may have detached O.
  138. if (bufferInstance.IsDetachedBuffer)
  139. {
  140. ExceptionHelper.ThrowTypeError(_realm);
  141. }
  142. var fromBuf = o.ArrayBufferData;
  143. var toBuf = bufferInstance.ArrayBufferData;
  144. System.Array.Copy(fromBuf!, first, toBuf!, 0, newLen);
  145. return bufferInstance;
  146. }
  147. }
  148. }