DataViewPrototype.cs 17 KB

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