DataViewPrototype.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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 ClrFunctionInstance(_engine, "get buffer", Buffer, 0, lengthFlags), Undefined, PropertyFlag.Configurable),
  33. ["byteLength"] = new GetSetPropertyDescriptor(new ClrFunctionInstance(_engine, "get byteLength", ByteLength, 0, lengthFlags), Undefined, PropertyFlag.Configurable),
  34. ["byteOffset"] = new GetSetPropertyDescriptor(new ClrFunctionInstance(Engine, "get byteOffset", ByteOffset, 0, lengthFlags), Undefined, PropertyFlag.Configurable),
  35. ["constructor"] = new PropertyDescriptor(_constructor, PropertyFlag.NonEnumerable),
  36. ["getBigInt64"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "getBigInt64", GetBigInt64, length: 1, lengthFlags), propertyFlags),
  37. ["getBigUint64"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "getBigUint64", GetBigUint64, length: 1, lengthFlags), propertyFlags),
  38. ["getFloat32"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "getFloat32", GetFloat32, length: 1, lengthFlags), propertyFlags),
  39. ["getFloat64"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "getFloat64", GetFloat64, length: 1, lengthFlags), propertyFlags),
  40. ["getInt8"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "getInt8", GetInt8, length: 1, lengthFlags), propertyFlags),
  41. ["getInt16"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "getInt16", GetInt16, length: 1, lengthFlags), propertyFlags),
  42. ["getInt32"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "getInt32", GetInt32, length: 1, lengthFlags), propertyFlags),
  43. ["getUint8"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "getUint8", GetUint8, length: 1, lengthFlags), propertyFlags),
  44. ["getUint16"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "getUint16", GetUint16, length: 1, lengthFlags), propertyFlags),
  45. ["getUint32"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "getUint32", GetUint32, length: 1, lengthFlags), propertyFlags),
  46. ["setBigInt64"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "setBigInt64", SetBigInt64, length: 2, lengthFlags), propertyFlags),
  47. ["setBigUint64"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "setBigUint64", SetBigUint64, length: 2, lengthFlags), propertyFlags),
  48. ["setFloat32"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "setFloat32", SetFloat32, length: 2, lengthFlags), propertyFlags),
  49. ["setFloat64"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "setFloat64", SetFloat64, length: 2, lengthFlags), propertyFlags),
  50. ["setInt8"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "setInt8", SetInt8, length: 2, lengthFlags), propertyFlags),
  51. ["setInt16"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "setInt16", SetInt16, length: 2, lengthFlags), propertyFlags),
  52. ["setInt32"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "setInt32", SetInt32, length: 2, lengthFlags), propertyFlags),
  53. ["setUint8"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "setUint8", SetUint8, length: 2, lengthFlags), propertyFlags),
  54. ["setUint16"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "setUint16", SetUint16, length: 2, lengthFlags), propertyFlags),
  55. ["setUint32"] = new PropertyDescriptor(new ClrFunctionInstance(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 buffer = o._viewedArrayBuffer!;
  87. buffer.AssertNotDetached();
  88. return JsNumber.Create(o._byteLength);
  89. }
  90. /// <summary>
  91. /// https://tc39.es/ecma262/#sec-get-dataview.prototype.byteoffset
  92. /// </summary>
  93. private JsValue ByteOffset(JsValue thisObject, JsValue[] arguments)
  94. {
  95. var o = thisObject as JsDataView;
  96. if (o is null)
  97. {
  98. ExceptionHelper.ThrowTypeError(_realm, "Method get DataView.prototype.byteOffset called on incompatible receiver " + thisObject);
  99. }
  100. var buffer = o._viewedArrayBuffer!;
  101. buffer.AssertNotDetached();
  102. return JsNumber.Create(o._byteOffset);
  103. }
  104. private JsValue GetBigInt64(JsValue thisObject, JsValue[] arguments)
  105. {
  106. return GetViewValue(thisObject, arguments.At(0), arguments.At(1), TypedArrayElementType.BigInt64);
  107. }
  108. private JsValue GetBigUint64(JsValue thisObject, JsValue[] arguments)
  109. {
  110. return GetViewValue(thisObject, arguments.At(0), arguments.At(1), TypedArrayElementType.BigUint64);
  111. }
  112. private JsValue GetFloat32(JsValue thisObject, JsValue[] arguments)
  113. {
  114. return GetViewValue(thisObject, arguments.At(0), arguments.At(1, JsBoolean.False), TypedArrayElementType.Float32);
  115. }
  116. private JsValue GetFloat64(JsValue thisObject, JsValue[] arguments)
  117. {
  118. return GetViewValue(thisObject, arguments.At(0), arguments.At(1, JsBoolean.False), TypedArrayElementType.Float64);
  119. }
  120. private JsValue GetInt8(JsValue thisObject, JsValue[] arguments)
  121. {
  122. return GetViewValue(thisObject, arguments.At(0), JsBoolean.True, TypedArrayElementType.Int8);
  123. }
  124. private JsValue GetInt16(JsValue thisObject, JsValue[] arguments)
  125. {
  126. return GetViewValue(thisObject, arguments.At(0), arguments.At(1, JsBoolean.False), TypedArrayElementType.Int16);
  127. }
  128. private JsValue GetInt32(JsValue thisObject, JsValue[] arguments)
  129. {
  130. return GetViewValue(thisObject, arguments.At(0), arguments.At(1, JsBoolean.False), TypedArrayElementType.Int32);
  131. }
  132. private JsValue GetUint8(JsValue thisObject, JsValue[] arguments)
  133. {
  134. return GetViewValue(thisObject, arguments.At(0), JsBoolean.True, TypedArrayElementType.Uint8);
  135. }
  136. private JsValue GetUint16(JsValue thisObject, JsValue[] arguments)
  137. {
  138. return GetViewValue(thisObject, arguments.At(0), arguments.At(1, JsBoolean.False), TypedArrayElementType.Uint16);
  139. }
  140. private JsValue GetUint32(JsValue thisObject, JsValue[] arguments)
  141. {
  142. return GetViewValue(thisObject, arguments.At(0), arguments.At(1, JsBoolean.False), TypedArrayElementType.Uint32);
  143. }
  144. private JsValue SetBigInt64(JsValue thisObject, JsValue[] arguments)
  145. {
  146. return SetViewValue(thisObject, arguments.At(0), arguments.At(2), TypedArrayElementType.BigInt64, arguments.At(1));
  147. }
  148. private JsValue SetBigUint64(JsValue thisObject, JsValue[] arguments)
  149. {
  150. return SetViewValue(thisObject, arguments.At(0), arguments.At(2), TypedArrayElementType.BigUint64, arguments.At(1));
  151. }
  152. private JsValue SetFloat32 (JsValue thisObject, JsValue[] arguments)
  153. {
  154. return SetViewValue(thisObject, arguments.At(0), arguments.At(2, JsBoolean.False), TypedArrayElementType.Float32, arguments.At(1));
  155. }
  156. private JsValue SetFloat64(JsValue thisObject, JsValue[] arguments)
  157. {
  158. return SetViewValue(thisObject, arguments.At(0), arguments.At(2, JsBoolean.False), TypedArrayElementType.Float64, arguments.At(1));
  159. }
  160. private JsValue SetInt8 (JsValue thisObject, JsValue[] arguments)
  161. {
  162. return SetViewValue(thisObject, arguments.At(0), JsBoolean.True, TypedArrayElementType.Int8, arguments.At(1));
  163. }
  164. private JsValue SetInt16(JsValue thisObject, JsValue[] arguments)
  165. {
  166. return SetViewValue(thisObject, arguments.At(0), arguments.At(2, JsBoolean.False), TypedArrayElementType.Int16, arguments.At(1));
  167. }
  168. private JsValue SetInt32(JsValue thisObject, JsValue[] arguments)
  169. {
  170. return SetViewValue(thisObject, arguments.At(0), arguments.At(2, JsBoolean.False), TypedArrayElementType.Int32, arguments.At(1));
  171. }
  172. private JsValue SetUint8(JsValue thisObject, JsValue[] arguments)
  173. {
  174. return SetViewValue(thisObject, arguments.At(0), JsBoolean.True, TypedArrayElementType.Uint8, arguments.At(1));
  175. }
  176. private JsValue SetUint16(JsValue thisObject, JsValue[] arguments)
  177. {
  178. return SetViewValue(thisObject, arguments.At(0), arguments.At(2, JsBoolean.False), TypedArrayElementType.Uint16, arguments.At(1));
  179. }
  180. private JsValue SetUint32(JsValue thisObject, JsValue[] arguments)
  181. {
  182. return SetViewValue(thisObject, arguments.At(0), arguments.At(2, JsBoolean.False), TypedArrayElementType.Uint32, arguments.At(1));
  183. }
  184. /// <summary>
  185. /// https://tc39.es/ecma262/#sec-getviewvalue
  186. /// </summary>
  187. private JsValue GetViewValue(
  188. JsValue view,
  189. JsValue requestIndex,
  190. JsValue isLittleEndian,
  191. TypedArrayElementType type)
  192. {
  193. var dataView = view as JsDataView;
  194. if (dataView is null)
  195. {
  196. ExceptionHelper.ThrowTypeError(_realm, "Method called on incompatible receiver " + view);
  197. }
  198. var getIndex = (int) TypeConverter.ToIndex(_realm, requestIndex);
  199. var isLittleEndianBoolean = TypeConverter.ToBoolean(isLittleEndian);
  200. var buffer = dataView._viewedArrayBuffer!;
  201. buffer.AssertNotDetached();
  202. var viewOffset = dataView._byteOffset;
  203. var viewSize = dataView._byteLength;
  204. var elementSize = type.GetElementSize();
  205. if (getIndex + elementSize > viewSize)
  206. {
  207. ExceptionHelper.ThrowRangeError(_realm, "Offset is outside the bounds of the DataView");
  208. }
  209. var bufferIndex = (int) (getIndex + viewOffset);
  210. return buffer.GetValueFromBuffer(bufferIndex, type, false, ArrayBufferOrder.Unordered, isLittleEndianBoolean).ToJsValue();
  211. }
  212. /// <summary>
  213. /// https://tc39.es/ecma262/#sec-setviewvalue
  214. /// </summary>
  215. private JsValue SetViewValue(
  216. JsValue view,
  217. JsValue requestIndex,
  218. JsValue isLittleEndian,
  219. TypedArrayElementType type,
  220. JsValue value)
  221. {
  222. var dataView = view as JsDataView;
  223. if (dataView is null)
  224. {
  225. ExceptionHelper.ThrowTypeError(_realm, "Method called on incompatible receiver " + view);
  226. }
  227. var getIndex = TypeConverter.ToIndex(_realm, requestIndex);
  228. TypedArrayValue numberValue;
  229. if (type.IsBigIntElementType())
  230. {
  231. numberValue = TypeConverter.ToBigInt(value);
  232. }
  233. else
  234. {
  235. numberValue = TypeConverter.ToNumber(value);
  236. }
  237. var isLittleEndianBoolean = TypeConverter.ToBoolean(isLittleEndian);
  238. var buffer = dataView._viewedArrayBuffer!;
  239. buffer.AssertNotDetached();
  240. var viewOffset = dataView._byteOffset;
  241. var viewSize = dataView._byteLength;
  242. var elementSize = type.GetElementSize();
  243. if (getIndex + elementSize > viewSize)
  244. {
  245. ExceptionHelper.ThrowRangeError(_realm, "Offset is outside the bounds of the DataView");
  246. }
  247. var bufferIndex = (int) (getIndex + viewOffset);
  248. buffer.SetValueInBuffer(bufferIndex, type, numberValue, false, ArrayBufferOrder.Unordered, isLittleEndianBoolean);
  249. return Undefined;
  250. }
  251. }
  252. }