DataViewPrototype.cs 18 KB

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