ArrayBufferPrototype.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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 ClrFunctionInstance(_engine, "get byteLength", ByteLength, 0, lengthFlags), Undefined, PropertyFlag.Configurable),
  29. [KnownKeys.Constructor] = new PropertyDescriptor(_constructor, PropertyFlag.NonEnumerable),
  30. ["detached"] = new GetSetPropertyDescriptor(new ClrFunctionInstance(_engine, "get detached", Detached, 0, lengthFlags), Undefined, PropertyFlag.Configurable),
  31. ["maxByteLength"] = new GetSetPropertyDescriptor(new ClrFunctionInstance(_engine, "get maxByteLength", MaxByteLength, 0, lengthFlags), Undefined, PropertyFlag.Configurable),
  32. ["resizable"] = new GetSetPropertyDescriptor(new ClrFunctionInstance(_engine, "get resizable", Resizable, 0, lengthFlags), Undefined, PropertyFlag.Configurable),
  33. ["resize"] = new PropertyDescriptor(new ClrFunctionInstance(_engine, "resize", Resize, 1, lengthFlags), PropertyFlag.NonEnumerable),
  34. ["slice"] = new PropertyDescriptor(new ClrFunctionInstance(_engine, "slice", Slice, 2, lengthFlags), PropertyFlag.NonEnumerable)
  35. };
  36. SetProperties(properties);
  37. var symbols = new SymbolDictionary(1) { [GlobalSymbolRegistry.ToStringTag] = new PropertyDescriptor("ArrayBuffer", PropertyFlag.Configurable) };
  38. SetSymbols(symbols);
  39. }
  40. private JsValue Detached(JsValue thisObject, JsValue[] arguments)
  41. {
  42. var o = thisObject as JsArrayBuffer;
  43. if (o is null)
  44. {
  45. ExceptionHelper.ThrowTypeError(_realm, "Method ArrayBuffer.prototype.detached called on incompatible receiver " + thisObject);
  46. }
  47. if (o.IsSharedArrayBuffer)
  48. {
  49. ExceptionHelper.ThrowTypeError(_realm);
  50. }
  51. return o.IsDetachedBuffer;
  52. }
  53. /// <summary>
  54. /// https://tc39.es/ecma262/#sec-get-arraybuffer.prototype.maxbytelength
  55. /// </summary>
  56. private JsValue MaxByteLength(JsValue thisObject, JsValue[] arguments)
  57. {
  58. var o = thisObject as JsArrayBuffer;
  59. if (o is null)
  60. {
  61. ExceptionHelper.ThrowTypeError(_realm, "Method ArrayBuffer.prototype.maxByteLength called on incompatible receiver " + thisObject);
  62. }
  63. if (o.IsSharedArrayBuffer)
  64. {
  65. ExceptionHelper.ThrowTypeError(_realm);
  66. }
  67. if (o.IsDetachedBuffer)
  68. {
  69. return JsNumber.PositiveZero;
  70. }
  71. long length = o.IsFixedLengthArrayBuffer
  72. ? o.ArrayBufferByteLength
  73. : o._arrayBufferMaxByteLength.GetValueOrDefault();
  74. return length;
  75. }
  76. /// <summary>
  77. /// https://tc39.es/ecma262/#sec-get-arraybuffer.prototype.resizable
  78. /// </summary>
  79. private JsValue Resizable(JsValue thisObject, JsValue[] arguments)
  80. {
  81. var o = thisObject as JsArrayBuffer;
  82. if (o is null)
  83. {
  84. ExceptionHelper.ThrowTypeError(_realm, "Method ArrayBuffer.prototype.resizable called on incompatible receiver " + thisObject);
  85. }
  86. if (o.IsSharedArrayBuffer)
  87. {
  88. ExceptionHelper.ThrowTypeError(_realm);
  89. }
  90. return !o.IsFixedLengthArrayBuffer;
  91. }
  92. /// <summary>
  93. /// https://tc39.es/ecma262/#sec-arraybuffer.prototype.resize
  94. /// </summary>
  95. private JsValue Resize(JsValue thisObject, JsValue[] arguments)
  96. {
  97. var o = thisObject as JsArrayBuffer;
  98. if (o is null)
  99. {
  100. ExceptionHelper.ThrowTypeError(_realm, "Method ArrayBuffer.prototype.resize called on incompatible receiver " + thisObject);
  101. }
  102. if (o.IsSharedArrayBuffer)
  103. {
  104. ExceptionHelper.ThrowTypeError(_realm);
  105. }
  106. var newLength = arguments.At(0);
  107. var newByteLength = TypeConverter.ToIndex(_realm, newLength);
  108. o.AssertNotDetached();
  109. o.Resize(newByteLength);
  110. return Undefined;
  111. }
  112. /// <summary>
  113. /// https://tc39.es/ecma262/#sec-get-arraybuffer.prototype.bytelength
  114. /// </summary>
  115. private JsValue ByteLength(JsValue thisObject, JsValue[] arguments)
  116. {
  117. var o = thisObject as JsArrayBuffer;
  118. if (o is null)
  119. {
  120. ExceptionHelper.ThrowTypeError(_realm, "Method ArrayBuffer.prototype.byteLength called on incompatible receiver " + thisObject);
  121. }
  122. if (o.IsSharedArrayBuffer)
  123. {
  124. ExceptionHelper.ThrowTypeError(_realm);
  125. }
  126. if (o.IsDetachedBuffer)
  127. {
  128. return JsNumber.PositiveZero;
  129. }
  130. return JsNumber.Create(o.ArrayBufferByteLength);
  131. }
  132. /// <summary>
  133. /// https://tc39.es/ecma262/#sec-arraybuffer.prototype.slice
  134. /// </summary>
  135. private JsValue Slice(JsValue thisObject, JsValue[] arguments)
  136. {
  137. var o = thisObject as JsArrayBuffer;
  138. if (o is null)
  139. {
  140. ExceptionHelper.ThrowTypeError(_realm, "Method ArrayBuffer.prototype.slice called on incompatible receiver " + thisObject);
  141. }
  142. if (o.IsSharedArrayBuffer)
  143. {
  144. ExceptionHelper.ThrowTypeError(_realm);
  145. }
  146. o.AssertNotDetached();
  147. var start = arguments.At(0);
  148. var end = arguments.At(1);
  149. var len = o.ArrayBufferByteLength;
  150. var relativeStart = TypeConverter.ToIntegerOrInfinity(start);
  151. var first = relativeStart switch
  152. {
  153. double.NegativeInfinity => 0,
  154. < 0 => (int) System.Math.Max(len + relativeStart, 0),
  155. _ => (int) System.Math.Min(relativeStart, len)
  156. };
  157. double relativeEnd;
  158. if (end.IsUndefined())
  159. {
  160. relativeEnd = len;
  161. }
  162. else
  163. {
  164. relativeEnd = TypeConverter.ToIntegerOrInfinity(end);
  165. }
  166. var final = relativeEnd switch
  167. {
  168. double.NegativeInfinity => 0,
  169. < 0 => (int) System.Math.Max(len + relativeEnd, 0),
  170. _ => (int) System.Math.Min(relativeEnd, len)
  171. };
  172. var newLen = System.Math.Max(final - first, 0);
  173. var ctor = SpeciesConstructor(o, _realm.Intrinsics.ArrayBuffer);
  174. var bufferInstance = Construct(ctor, new JsValue[] { JsNumber.Create(newLen) }) as JsArrayBuffer;
  175. if (bufferInstance is null)
  176. {
  177. ExceptionHelper.ThrowTypeError(_realm);
  178. }
  179. if (bufferInstance.IsSharedArrayBuffer)
  180. {
  181. ExceptionHelper.ThrowTypeError(_realm);
  182. }
  183. if (bufferInstance.IsDetachedBuffer)
  184. {
  185. ExceptionHelper.ThrowTypeError(_realm);
  186. }
  187. if (ReferenceEquals(bufferInstance, o))
  188. {
  189. ExceptionHelper.ThrowTypeError(_realm);
  190. }
  191. if (bufferInstance.ArrayBufferByteLength < newLen)
  192. {
  193. ExceptionHelper.ThrowTypeError(_realm);
  194. }
  195. // NOTE: Side-effects of the above steps may have detached O.
  196. if (bufferInstance.IsDetachedBuffer)
  197. {
  198. ExceptionHelper.ThrowTypeError(_realm);
  199. }
  200. var fromBuf = o.ArrayBufferData;
  201. var toBuf = bufferInstance.ArrayBufferData;
  202. System.Array.Copy(fromBuf!, first, toBuf!, 0, newLen);
  203. return bufferInstance;
  204. }
  205. }