ArrayBufferPrototype.cs 10 KB

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