ArrayBufferPrototype.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. /// <summary>
  10. /// https://tc39.es/ecma262/#sec-properties-of-the-arraybuffer-prototype-object
  11. /// </summary>
  12. internal sealed class ArrayBufferPrototype : Prototype
  13. {
  14. private readonly ArrayBufferConstructor _constructor;
  15. internal ArrayBufferPrototype(
  16. Engine engine,
  17. ArrayBufferConstructor constructor,
  18. ObjectPrototype objectPrototype) : base(engine, engine.Realm)
  19. {
  20. _prototype = objectPrototype;
  21. _constructor = constructor;
  22. }
  23. protected override void Initialize()
  24. {
  25. const PropertyFlag lengthFlags = PropertyFlag.Configurable;
  26. var properties = new PropertyDictionary(4, checkExistingKeys: false)
  27. {
  28. ["byteLength"] = new GetSetPropertyDescriptor(new ClrFunction(_engine, "get byteLength", ByteLength, 0, lengthFlags), Undefined, PropertyFlag.Configurable),
  29. [KnownKeys.Constructor] = new PropertyDescriptor(_constructor, PropertyFlag.NonEnumerable),
  30. ["detached"] = new GetSetPropertyDescriptor(new ClrFunction(_engine, "get detached", Detached, 0, lengthFlags), Undefined, PropertyFlag.Configurable),
  31. ["maxByteLength"] = new GetSetPropertyDescriptor(new ClrFunction(_engine, "get maxByteLength", MaxByteLength, 0, lengthFlags), Undefined, PropertyFlag.Configurable),
  32. ["resizable"] = new GetSetPropertyDescriptor(new ClrFunction(_engine, "get resizable", Resizable, 0, lengthFlags), Undefined, PropertyFlag.Configurable),
  33. ["resize"] = new PropertyDescriptor(new ClrFunction(_engine, "resize", Resize, 1, lengthFlags), PropertyFlag.NonEnumerable),
  34. ["slice"] = new PropertyDescriptor(new ClrFunction(_engine, "slice", Slice, 2, lengthFlags), PropertyFlag.NonEnumerable),
  35. ["transfer"] = new PropertyDescriptor(new ClrFunction(_engine, "transfer", Transfer, 0, lengthFlags), PropertyFlag.NonEnumerable),
  36. ["transferToFixedLength"] = new PropertyDescriptor(new ClrFunction(_engine, "transferToFixedLength", TransferToFixedLength, 0, lengthFlags), PropertyFlag.NonEnumerable),
  37. };
  38. SetProperties(properties);
  39. var symbols = new SymbolDictionary(1) { [GlobalSymbolRegistry.ToStringTag] = new PropertyDescriptor("ArrayBuffer", PropertyFlag.Configurable) };
  40. SetSymbols(symbols);
  41. }
  42. private JsValue Detached(JsValue thisObject, JsCallArguments arguments)
  43. {
  44. var o = thisObject as JsArrayBuffer;
  45. if (o is null || o.IsSharedArrayBuffer)
  46. {
  47. ExceptionHelper.ThrowTypeError(_realm, "Method ArrayBuffer.prototype.detached called on incompatible receiver " + thisObject);
  48. }
  49. return o.IsDetachedBuffer;
  50. }
  51. /// <summary>
  52. /// https://tc39.es/ecma262/#sec-get-arraybuffer.prototype.maxbytelength
  53. /// </summary>
  54. private JsValue MaxByteLength(JsValue thisObject, JsCallArguments arguments)
  55. {
  56. var o = thisObject as JsArrayBuffer;
  57. if (o is null || o.IsSharedArrayBuffer)
  58. {
  59. ExceptionHelper.ThrowTypeError(_realm, "Method ArrayBuffer.prototype.maxByteLength called on incompatible receiver " + thisObject);
  60. }
  61. if (o.IsDetachedBuffer)
  62. {
  63. return JsNumber.PositiveZero;
  64. }
  65. long length = o.IsFixedLengthArrayBuffer
  66. ? o.ArrayBufferByteLength
  67. : o._arrayBufferMaxByteLength.GetValueOrDefault();
  68. return length;
  69. }
  70. /// <summary>
  71. /// https://tc39.es/ecma262/#sec-get-arraybuffer.prototype.resizable
  72. /// </summary>
  73. private JsValue Resizable(JsValue thisObject, JsCallArguments arguments)
  74. {
  75. var o = thisObject as JsArrayBuffer;
  76. if (o is null || o.IsSharedArrayBuffer)
  77. {
  78. ExceptionHelper.ThrowTypeError(_realm, "Method ArrayBuffer.prototype.resizable called on incompatible receiver " + thisObject);
  79. }
  80. return !o.IsFixedLengthArrayBuffer;
  81. }
  82. /// <summary>
  83. /// https://tc39.es/ecma262/#sec-arraybuffer.prototype.resize
  84. /// </summary>
  85. private JsValue Resize(JsValue thisObject, JsCallArguments arguments)
  86. {
  87. var o = thisObject as JsArrayBuffer;
  88. if (o is null || o.IsSharedArrayBuffer)
  89. {
  90. ExceptionHelper.ThrowTypeError(_realm, "Method ArrayBuffer.prototype.resize called on incompatible receiver " + thisObject);
  91. }
  92. var newLength = arguments.At(0);
  93. var newByteLength = TypeConverter.ToIndex(_realm, newLength);
  94. o.AssertNotDetached();
  95. o.Resize(newByteLength);
  96. return Undefined;
  97. }
  98. /// <summary>
  99. /// https://tc39.es/ecma262/#sec-get-arraybuffer.prototype.bytelength
  100. /// </summary>
  101. private JsValue ByteLength(JsValue thisObject, JsCallArguments arguments)
  102. {
  103. var o = thisObject as JsArrayBuffer;
  104. if (o is null || o.IsSharedArrayBuffer)
  105. {
  106. ExceptionHelper.ThrowTypeError(_realm, "Method ArrayBuffer.prototype.byteLength called on incompatible receiver " + thisObject);
  107. }
  108. if (o.IsDetachedBuffer)
  109. {
  110. return JsNumber.PositiveZero;
  111. }
  112. return JsNumber.Create(o.ArrayBufferByteLength);
  113. }
  114. /// <summary>
  115. /// https://tc39.es/ecma262/#sec-arraybuffer.prototype.slice
  116. /// </summary>
  117. private JsValue Slice(JsValue thisObject, JsCallArguments arguments)
  118. {
  119. var o = thisObject as JsArrayBuffer;
  120. if (o is null || o.IsSharedArrayBuffer)
  121. {
  122. ExceptionHelper.ThrowTypeError(_realm, "Method ArrayBuffer.prototype.slice called on incompatible receiver " + thisObject);
  123. }
  124. o.AssertNotDetached();
  125. var start = arguments.At(0);
  126. var end = arguments.At(1);
  127. var len = o.ArrayBufferByteLength;
  128. var relativeStart = TypeConverter.ToIntegerOrInfinity(start);
  129. var first = relativeStart switch
  130. {
  131. double.NegativeInfinity => 0,
  132. < 0 => (int) System.Math.Max(len + relativeStart, 0),
  133. _ => (int) System.Math.Min(relativeStart, len)
  134. };
  135. double relativeEnd;
  136. if (end.IsUndefined())
  137. {
  138. relativeEnd = len;
  139. }
  140. else
  141. {
  142. relativeEnd = TypeConverter.ToIntegerOrInfinity(end);
  143. }
  144. var final = relativeEnd switch
  145. {
  146. double.NegativeInfinity => 0,
  147. < 0 => (int) System.Math.Max(len + relativeEnd, 0),
  148. _ => (int) System.Math.Min(relativeEnd, len)
  149. };
  150. var newLen = System.Math.Max(final - first, 0);
  151. var ctor = SpeciesConstructor(o, _realm.Intrinsics.ArrayBuffer);
  152. var bufferInstance = Construct(ctor, [JsNumber.Create(newLen)]) as JsArrayBuffer;
  153. if (bufferInstance is null)
  154. {
  155. ExceptionHelper.ThrowTypeError(_realm);
  156. }
  157. if (bufferInstance.IsSharedArrayBuffer)
  158. {
  159. ExceptionHelper.ThrowTypeError(_realm);
  160. }
  161. if (bufferInstance.IsDetachedBuffer)
  162. {
  163. ExceptionHelper.ThrowTypeError(_realm);
  164. }
  165. if (ReferenceEquals(bufferInstance, o))
  166. {
  167. ExceptionHelper.ThrowTypeError(_realm);
  168. }
  169. if (bufferInstance.ArrayBufferByteLength < newLen)
  170. {
  171. ExceptionHelper.ThrowTypeError(_realm);
  172. }
  173. // NOTE: Side-effects of the above steps may have detached O.
  174. if (bufferInstance.IsDetachedBuffer)
  175. {
  176. ExceptionHelper.ThrowTypeError(_realm);
  177. }
  178. var fromBuf = o.ArrayBufferData;
  179. var toBuf = bufferInstance.ArrayBufferData;
  180. System.Array.Copy(fromBuf!, first, toBuf!, 0, newLen);
  181. return bufferInstance;
  182. }
  183. /// <summary>
  184. /// https://tc39.es/proposal-arraybuffer-transfer/#sec-arraybuffer.prototype.transfer
  185. /// </summary>
  186. private JsValue Transfer(JsValue thisObject, JsCallArguments arguments)
  187. {
  188. return ArrayBufferCopyAndDetach(thisObject, arguments.At(0), PreserveResizability.PreserveResizability);
  189. }
  190. /// <summary>
  191. /// https://tc39.es/proposal-arraybuffer-transfer/#sec-arraybuffer.prototype.transfertofixedlength
  192. /// </summary>
  193. private JsValue TransferToFixedLength(JsValue thisObject, JsCallArguments arguments)
  194. {
  195. return ArrayBufferCopyAndDetach(thisObject, arguments.At(0), PreserveResizability.FixedLength);
  196. }
  197. private JsValue ArrayBufferCopyAndDetach(JsValue o, JsValue newLength, PreserveResizability preserveResizability)
  198. {
  199. if (o is not JsArrayBuffer arrayBuffer || arrayBuffer.IsSharedArrayBuffer)
  200. {
  201. ExceptionHelper.ThrowTypeError(_realm, "Method ArrayBuffer.prototype.ArrayBufferCopyAndDetach called on incompatible receiver " + o);
  202. return Undefined;
  203. }
  204. uint newByteLength;
  205. if (newLength.IsUndefined())
  206. {
  207. newByteLength = (uint) arrayBuffer.ArrayBufferByteLength;
  208. }
  209. else
  210. {
  211. newByteLength = TypeConverter.ToIndex(_realm, newLength);
  212. }
  213. arrayBuffer.AssertNotDetached();
  214. uint? newMaxByteLength = null;
  215. if (preserveResizability == PreserveResizability.PreserveResizability && arrayBuffer._arrayBufferMaxByteLength != null)
  216. {
  217. newMaxByteLength = (uint) arrayBuffer._arrayBufferMaxByteLength.Value;
  218. }
  219. if (!arrayBuffer._arrayBufferDetachKey.IsUndefined())
  220. {
  221. ExceptionHelper.ThrowTypeError(_realm);
  222. }
  223. var newBuffer = _engine.Realm.Intrinsics.ArrayBuffer.AllocateArrayBuffer(_engine.Realm.Intrinsics.ArrayBuffer, newByteLength, newMaxByteLength);
  224. var copyLength = System.Math.Min(newByteLength, arrayBuffer.ArrayBufferByteLength);
  225. var fromBlock = arrayBuffer.ArrayBufferData!;
  226. var toBlock = newBuffer.ArrayBufferData!;
  227. System.Array.Copy(fromBlock, 0, toBlock, 0, copyLength);
  228. // NOTE: Neither creation of the new Data Block nor copying from the old Data Block are observable. Implementations may implement this method as a zero-copy move or a realloc.
  229. arrayBuffer.DetachArrayBuffer();
  230. return newBuffer;
  231. }
  232. private enum PreserveResizability
  233. {
  234. PreserveResizability,
  235. FixedLength
  236. }
  237. }