DataViewPrototype.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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.ArrayBuffer;
  4. using Jint.Native.Object;
  5. using Jint.Native.Symbol;
  6. using Jint.Native.TypedArray;
  7. using Jint.Runtime;
  8. using Jint.Runtime.Descriptors;
  9. using Jint.Runtime.Interop;
  10. namespace Jint.Native.DataView
  11. {
  12. /// <summary>
  13. /// https://tc39.es/ecma262/#sec-properties-of-the-dataview-prototype-object
  14. /// </summary>
  15. internal sealed class DataViewPrototype : Prototype
  16. {
  17. private readonly DataViewConstructor _constructor;
  18. internal DataViewPrototype(
  19. Engine engine,
  20. DataViewConstructor constructor,
  21. ObjectPrototype objectPrototype) : base(engine, engine.Realm)
  22. {
  23. _prototype = objectPrototype;
  24. _constructor = constructor;
  25. }
  26. protected override void Initialize()
  27. {
  28. const PropertyFlag lengthFlags = PropertyFlag.Configurable;
  29. const PropertyFlag propertyFlags = PropertyFlag.Configurable | PropertyFlag.Writable;
  30. var properties = new PropertyDictionary(24, checkExistingKeys: false)
  31. {
  32. ["buffer"] = new GetSetPropertyDescriptor(new ClrFunction(_engine, "get buffer", Buffer, 0, lengthFlags), Undefined, PropertyFlag.Configurable),
  33. ["byteLength"] = new GetSetPropertyDescriptor(new ClrFunction(_engine, "get byteLength", ByteLength, 0, lengthFlags), Undefined, PropertyFlag.Configurable),
  34. ["byteOffset"] = new GetSetPropertyDescriptor(new ClrFunction(Engine, "get byteOffset", ByteOffset, 0, lengthFlags), Undefined, PropertyFlag.Configurable),
  35. ["constructor"] = new PropertyDescriptor(_constructor, PropertyFlag.NonEnumerable),
  36. ["getBigInt64"] = new PropertyDescriptor(new ClrFunction(Engine, "getBigInt64", GetBigInt64, length: 1, lengthFlags), propertyFlags),
  37. ["getBigUint64"] = new PropertyDescriptor(new ClrFunction(Engine, "getBigUint64", GetBigUint64, length: 1, lengthFlags), propertyFlags),
  38. ["getFloat32"] = new PropertyDescriptor(new ClrFunction(Engine, "getFloat32", GetFloat32, length: 1, lengthFlags), propertyFlags),
  39. ["getFloat64"] = new PropertyDescriptor(new ClrFunction(Engine, "getFloat64", GetFloat64, length: 1, lengthFlags), propertyFlags),
  40. ["getInt8"] = new PropertyDescriptor(new ClrFunction(Engine, "getInt8", GetInt8, length: 1, lengthFlags), propertyFlags),
  41. ["getInt16"] = new PropertyDescriptor(new ClrFunction(Engine, "getInt16", GetInt16, length: 1, lengthFlags), propertyFlags),
  42. ["getInt32"] = new PropertyDescriptor(new ClrFunction(Engine, "getInt32", GetInt32, length: 1, lengthFlags), propertyFlags),
  43. ["getUint8"] = new PropertyDescriptor(new ClrFunction(Engine, "getUint8", GetUint8, length: 1, lengthFlags), propertyFlags),
  44. ["getUint16"] = new PropertyDescriptor(new ClrFunction(Engine, "getUint16", GetUint16, length: 1, lengthFlags), propertyFlags),
  45. ["getUint32"] = new PropertyDescriptor(new ClrFunction(Engine, "getUint32", GetUint32, length: 1, lengthFlags), propertyFlags),
  46. ["setBigInt64"] = new PropertyDescriptor(new ClrFunction(Engine, "setBigInt64", SetBigInt64, length: 2, lengthFlags), propertyFlags),
  47. ["setBigUint64"] = new PropertyDescriptor(new ClrFunction(Engine, "setBigUint64", SetBigUint64, length: 2, lengthFlags), propertyFlags),
  48. ["setFloat32"] = new PropertyDescriptor(new ClrFunction(Engine, "setFloat32", SetFloat32, length: 2, lengthFlags), propertyFlags),
  49. ["setFloat64"] = new PropertyDescriptor(new ClrFunction(Engine, "setFloat64", SetFloat64, length: 2, lengthFlags), propertyFlags),
  50. ["setInt8"] = new PropertyDescriptor(new ClrFunction(Engine, "setInt8", SetInt8, length: 2, lengthFlags), propertyFlags),
  51. ["setInt16"] = new PropertyDescriptor(new ClrFunction(Engine, "setInt16", SetInt16, length: 2, lengthFlags), propertyFlags),
  52. ["setInt32"] = new PropertyDescriptor(new ClrFunction(Engine, "setInt32", SetInt32, length: 2, lengthFlags), propertyFlags),
  53. ["setUint8"] = new PropertyDescriptor(new ClrFunction(Engine, "setUint8", SetUint8, length: 2, lengthFlags), propertyFlags),
  54. ["setUint16"] = new PropertyDescriptor(new ClrFunction(Engine, "setUint16", SetUint16, length: 2, lengthFlags), propertyFlags),
  55. ["setUint32"] = new PropertyDescriptor(new ClrFunction(Engine, "setUint32", SetUint32, length: 2, lengthFlags), propertyFlags)
  56. };
  57. SetProperties(properties);
  58. var symbols = new SymbolDictionary(1)
  59. {
  60. [GlobalSymbolRegistry.ToStringTag] = new PropertyDescriptor("DataView", PropertyFlag.Configurable)
  61. };
  62. SetSymbols(symbols);
  63. }
  64. /// <summary>
  65. /// https://tc39.es/ecma262/#sec-get-dataview.prototype.buffer
  66. /// </summary>
  67. private JsValue Buffer(JsValue thisObject, JsValue[] arguments)
  68. {
  69. var o = thisObject as JsDataView;
  70. if (o is null)
  71. {
  72. ExceptionHelper.ThrowTypeError(_realm, "Method get DataView.prototype.buffer called on incompatible receiver " + thisObject);
  73. }
  74. return o._viewedArrayBuffer!;
  75. }
  76. /// <summary>
  77. /// https://tc39.es/ecma262/#sec-get-dataview.prototype.bytelength
  78. /// </summary>
  79. private JsValue ByteLength(JsValue thisObject, JsValue[] arguments)
  80. {
  81. var o = thisObject as JsDataView;
  82. if (o is null)
  83. {
  84. ExceptionHelper.ThrowTypeError(_realm, "Method get DataView.prototype.byteLength called on incompatible receiver " + thisObject);
  85. }
  86. var viewRecord = MakeDataViewWithBufferWitnessRecord(o, ArrayBufferOrder.SeqCst);
  87. if (viewRecord.IsViewOutOfBounds)
  88. {
  89. ExceptionHelper.ThrowTypeError(_realm, "Offset is outside the bounds of the DataView");
  90. }
  91. var buffer = o._viewedArrayBuffer!;
  92. buffer.AssertNotDetached();
  93. return JsNumber.Create(viewRecord.ViewByteLength);
  94. }
  95. /// <summary>
  96. /// https://tc39.es/ecma262/#sec-get-dataview.prototype.byteoffset
  97. /// </summary>
  98. private JsValue ByteOffset(JsValue thisObject, JsValue[] arguments)
  99. {
  100. var o = thisObject as JsDataView;
  101. if (o is null)
  102. {
  103. ExceptionHelper.ThrowTypeError(_realm, "Method get DataView.prototype.byteOffset called on incompatible receiver " + thisObject);
  104. }
  105. var viewRecord = MakeDataViewWithBufferWitnessRecord(o, ArrayBufferOrder.SeqCst);
  106. if (viewRecord.IsViewOutOfBounds)
  107. {
  108. ExceptionHelper.ThrowTypeError(_realm, "Offset is outside the bounds of the DataView");
  109. }
  110. var buffer = o._viewedArrayBuffer!;
  111. buffer.AssertNotDetached();
  112. return JsNumber.Create(o._byteOffset);
  113. }
  114. private JsValue GetBigInt64(JsValue thisObject, JsValue[] arguments)
  115. {
  116. return GetViewValue(thisObject, arguments.At(0), arguments.At(1), TypedArrayElementType.BigInt64);
  117. }
  118. private JsValue GetBigUint64(JsValue thisObject, JsValue[] arguments)
  119. {
  120. return GetViewValue(thisObject, arguments.At(0), arguments.At(1), TypedArrayElementType.BigUint64);
  121. }
  122. private JsValue GetFloat32(JsValue thisObject, JsValue[] arguments)
  123. {
  124. return GetViewValue(thisObject, arguments.At(0), arguments.At(1, JsBoolean.False), TypedArrayElementType.Float32);
  125. }
  126. private JsValue GetFloat64(JsValue thisObject, JsValue[] arguments)
  127. {
  128. return GetViewValue(thisObject, arguments.At(0), arguments.At(1, JsBoolean.False), TypedArrayElementType.Float64);
  129. }
  130. private JsValue GetInt8(JsValue thisObject, JsValue[] arguments)
  131. {
  132. return GetViewValue(thisObject, arguments.At(0), JsBoolean.True, TypedArrayElementType.Int8);
  133. }
  134. private JsValue GetInt16(JsValue thisObject, JsValue[] arguments)
  135. {
  136. return GetViewValue(thisObject, arguments.At(0), arguments.At(1, JsBoolean.False), TypedArrayElementType.Int16);
  137. }
  138. private JsValue GetInt32(JsValue thisObject, JsValue[] arguments)
  139. {
  140. return GetViewValue(thisObject, arguments.At(0), arguments.At(1, JsBoolean.False), TypedArrayElementType.Int32);
  141. }
  142. private JsValue GetUint8(JsValue thisObject, JsValue[] arguments)
  143. {
  144. return GetViewValue(thisObject, arguments.At(0), JsBoolean.True, TypedArrayElementType.Uint8);
  145. }
  146. private JsValue GetUint16(JsValue thisObject, JsValue[] arguments)
  147. {
  148. return GetViewValue(thisObject, arguments.At(0), arguments.At(1, JsBoolean.False), TypedArrayElementType.Uint16);
  149. }
  150. private JsValue GetUint32(JsValue thisObject, JsValue[] arguments)
  151. {
  152. return GetViewValue(thisObject, arguments.At(0), arguments.At(1, JsBoolean.False), TypedArrayElementType.Uint32);
  153. }
  154. private JsValue SetBigInt64(JsValue thisObject, JsValue[] arguments)
  155. {
  156. return SetViewValue(thisObject, arguments.At(0), arguments.At(2), TypedArrayElementType.BigInt64, arguments.At(1));
  157. }
  158. private JsValue SetBigUint64(JsValue thisObject, JsValue[] arguments)
  159. {
  160. return SetViewValue(thisObject, arguments.At(0), arguments.At(2), TypedArrayElementType.BigUint64, arguments.At(1));
  161. }
  162. private JsValue SetFloat32 (JsValue thisObject, JsValue[] arguments)
  163. {
  164. return SetViewValue(thisObject, arguments.At(0), arguments.At(2, JsBoolean.False), TypedArrayElementType.Float32, arguments.At(1));
  165. }
  166. private JsValue SetFloat64(JsValue thisObject, JsValue[] arguments)
  167. {
  168. return SetViewValue(thisObject, arguments.At(0), arguments.At(2, JsBoolean.False), TypedArrayElementType.Float64, arguments.At(1));
  169. }
  170. private JsValue SetInt8 (JsValue thisObject, JsValue[] arguments)
  171. {
  172. return SetViewValue(thisObject, arguments.At(0), JsBoolean.True, TypedArrayElementType.Int8, arguments.At(1));
  173. }
  174. private JsValue SetInt16(JsValue thisObject, JsValue[] arguments)
  175. {
  176. return SetViewValue(thisObject, arguments.At(0), arguments.At(2, JsBoolean.False), TypedArrayElementType.Int16, arguments.At(1));
  177. }
  178. private JsValue SetInt32(JsValue thisObject, JsValue[] arguments)
  179. {
  180. return SetViewValue(thisObject, arguments.At(0), arguments.At(2, JsBoolean.False), TypedArrayElementType.Int32, arguments.At(1));
  181. }
  182. private JsValue SetUint8(JsValue thisObject, JsValue[] arguments)
  183. {
  184. return SetViewValue(thisObject, arguments.At(0), JsBoolean.True, TypedArrayElementType.Uint8, arguments.At(1));
  185. }
  186. private JsValue SetUint16(JsValue thisObject, JsValue[] arguments)
  187. {
  188. return SetViewValue(thisObject, arguments.At(0), arguments.At(2, JsBoolean.False), TypedArrayElementType.Uint16, arguments.At(1));
  189. }
  190. private JsValue SetUint32(JsValue thisObject, JsValue[] arguments)
  191. {
  192. return SetViewValue(thisObject, arguments.At(0), arguments.At(2, JsBoolean.False), TypedArrayElementType.Uint32, arguments.At(1));
  193. }
  194. /// <summary>
  195. /// https://tc39.es/ecma262/#sec-getviewvalue
  196. /// </summary>
  197. private JsValue GetViewValue(
  198. JsValue view,
  199. JsValue requestIndex,
  200. JsValue isLittleEndian,
  201. TypedArrayElementType type)
  202. {
  203. if (view is not JsDataView dataView)
  204. {
  205. ExceptionHelper.ThrowTypeError(_realm, "Method called on incompatible receiver " + view);
  206. return Undefined;
  207. }
  208. var getIndex = (int) TypeConverter.ToIndex(_realm, requestIndex);
  209. var isLittleEndianBoolean = TypeConverter.ToBoolean(isLittleEndian);
  210. var buffer = dataView._viewedArrayBuffer!;
  211. buffer.AssertNotDetached();
  212. var viewOffset = dataView._byteOffset;
  213. var viewRecord = MakeDataViewWithBufferWitnessRecord(dataView, ArrayBufferOrder.Unordered);
  214. if (viewRecord.IsViewOutOfBounds)
  215. {
  216. ExceptionHelper.ThrowTypeError(_realm, "Offset is outside the bounds of the DataView");
  217. }
  218. var viewSize = viewRecord.ViewByteLength;
  219. var elementSize = type.GetElementSize();
  220. if (getIndex + elementSize > viewSize)
  221. {
  222. ExceptionHelper.ThrowRangeError(_realm, "Offset is outside the bounds of the DataView");
  223. }
  224. var bufferIndex = (int) (getIndex + viewOffset);
  225. return buffer.GetValueFromBuffer(bufferIndex, type, isTypedArray: false, ArrayBufferOrder.Unordered, isLittleEndianBoolean).ToJsValue();
  226. }
  227. internal readonly record struct DataViewWithBufferWitnessRecord(JsDataView Object, int CachedBufferByteLength)
  228. {
  229. /// <summary>
  230. /// https://tc39.es/ecma262/#sec-isviewoutofbounds
  231. /// </summary>
  232. public bool IsViewOutOfBounds
  233. {
  234. get
  235. {
  236. var view = Object;
  237. var bufferByteLength = CachedBufferByteLength;
  238. if (bufferByteLength == -1)
  239. {
  240. return true;
  241. }
  242. var byteOffsetStart = view._byteOffset;
  243. long byteOffsetEnd;
  244. if (view._byteLength == JsTypedArray.LengthAuto)
  245. {
  246. byteOffsetEnd = bufferByteLength;
  247. }
  248. else
  249. {
  250. byteOffsetEnd = byteOffsetStart + view._byteLength;
  251. }
  252. if (byteOffsetStart > bufferByteLength || byteOffsetEnd > bufferByteLength)
  253. {
  254. return true;
  255. }
  256. return false;
  257. }
  258. }
  259. /// <summary>
  260. /// https://tc39.es/ecma262/#sec-getviewbytelength
  261. /// </summary>
  262. public long ViewByteLength
  263. {
  264. get
  265. {
  266. var view = Object;
  267. if (view._byteLength != JsTypedArray.LengthAuto)
  268. {
  269. return view._byteLength;
  270. }
  271. var byteOffset = view._byteOffset;
  272. var byteLength = CachedBufferByteLength;
  273. return byteLength - byteOffset;
  274. }
  275. }
  276. }
  277. /// <summary>
  278. /// https://tc39.es/ecma262/#sec-makedataviewwithbufferwitnessrecord
  279. /// </summary>
  280. private static DataViewWithBufferWitnessRecord MakeDataViewWithBufferWitnessRecord(JsDataView obj, ArrayBufferOrder order)
  281. {
  282. var buffer = obj._viewedArrayBuffer;
  283. int byteLength;
  284. if (buffer?.IsDetachedBuffer == true)
  285. {
  286. byteLength = -1;
  287. }
  288. else
  289. {
  290. byteLength = IntrinsicTypedArrayPrototype.ArrayBufferByteLength(buffer!, order);
  291. }
  292. return new DataViewWithBufferWitnessRecord(obj, byteLength);
  293. }
  294. /// <summary>
  295. /// https://tc39.es/ecma262/#sec-setviewvalue
  296. /// </summary>
  297. private JsValue SetViewValue(
  298. JsValue view,
  299. JsValue requestIndex,
  300. JsValue isLittleEndian,
  301. TypedArrayElementType type,
  302. JsValue value)
  303. {
  304. var dataView = view as JsDataView;
  305. if (dataView is null)
  306. {
  307. ExceptionHelper.ThrowTypeError(_realm, "Method called on incompatible receiver " + view);
  308. }
  309. var getIndex = TypeConverter.ToIndex(_realm, requestIndex);
  310. TypedArrayValue numberValue;
  311. if (type.IsBigIntElementType())
  312. {
  313. numberValue = TypeConverter.ToBigInt(value);
  314. }
  315. else
  316. {
  317. numberValue = TypeConverter.ToNumber(value);
  318. }
  319. var isLittleEndianBoolean = TypeConverter.ToBoolean(isLittleEndian);
  320. var buffer = dataView._viewedArrayBuffer!;
  321. buffer.AssertNotDetached();
  322. var viewOffset = dataView._byteOffset;
  323. var viewRecord = MakeDataViewWithBufferWitnessRecord(dataView, ArrayBufferOrder.Unordered);
  324. if (viewRecord.IsViewOutOfBounds)
  325. {
  326. ExceptionHelper.ThrowTypeError(_realm, "Offset is outside the bounds of the DataView");
  327. }
  328. var viewSize = viewRecord.ViewByteLength;
  329. var elementSize = type.GetElementSize();
  330. if (getIndex + elementSize > viewSize)
  331. {
  332. ExceptionHelper.ThrowRangeError(_realm, "Offset is outside the bounds of the DataView");
  333. }
  334. var bufferIndex = (int) (getIndex + viewOffset);
  335. buffer.SetValueInBuffer(bufferIndex, type, numberValue, false, ArrayBufferOrder.Unordered, isLittleEndianBoolean);
  336. return Undefined;
  337. }
  338. }
  339. }