JsValueExtensions.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751
  1. using System.Diagnostics.Contracts;
  2. using System.Numerics;
  3. using System.Runtime.CompilerServices;
  4. using Jint.Native;
  5. using Jint.Native.Function;
  6. using Jint.Native.Object;
  7. using Jint.Native.Promise;
  8. using Jint.Native.Symbol;
  9. using Jint.Native.TypedArray;
  10. using Jint.Runtime;
  11. namespace Jint;
  12. public static class JsValueExtensions
  13. {
  14. [Pure]
  15. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  16. public static bool IsPrimitive(this JsValue value)
  17. {
  18. return (value._type & (InternalTypes.Primitive | InternalTypes.Undefined | InternalTypes.Null)) != InternalTypes.Empty;
  19. }
  20. [Pure]
  21. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  22. public static bool IsUndefined(this JsValue value)
  23. {
  24. return value._type == InternalTypes.Undefined;
  25. }
  26. [Pure]
  27. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  28. public static bool IsArray(this JsValue value)
  29. {
  30. return value is JsArray;
  31. }
  32. [Pure]
  33. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  34. internal static bool IsNullOrUndefined(this JsValue value)
  35. {
  36. return value._type < InternalTypes.Boolean;
  37. }
  38. [Pure]
  39. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  40. public static bool IsDate(this JsValue value)
  41. {
  42. return value is JsDate;
  43. }
  44. [Pure]
  45. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  46. public static bool IsPromise(this JsValue value)
  47. {
  48. return value is JsPromise;
  49. }
  50. [Pure]
  51. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  52. public static bool IsPrivateName(this JsValue value) => value._type == InternalTypes.PrivateName;
  53. [Pure]
  54. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  55. public static bool IsRegExp(this JsValue value)
  56. {
  57. if (value is not ObjectInstance oi)
  58. {
  59. return false;
  60. }
  61. var matcher = oi.Get(GlobalSymbolRegistry.Match);
  62. if (!matcher.IsUndefined())
  63. {
  64. return TypeConverter.ToBoolean(matcher);
  65. }
  66. return value is JsRegExp;
  67. }
  68. [Pure]
  69. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  70. public static bool IsObject(this JsValue value)
  71. {
  72. return (value._type & InternalTypes.Object) != InternalTypes.Empty;
  73. }
  74. [Pure]
  75. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  76. public static bool IsString(this JsValue value)
  77. {
  78. return (value._type & InternalTypes.String) != InternalTypes.Empty;
  79. }
  80. [Pure]
  81. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  82. public static bool IsNumber(this JsValue value)
  83. {
  84. return (value._type & (InternalTypes.Number | InternalTypes.Integer)) != InternalTypes.Empty;
  85. }
  86. [Pure]
  87. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  88. public static bool IsBigInt(this JsValue value)
  89. {
  90. return (value._type & InternalTypes.BigInt) != InternalTypes.Empty;
  91. }
  92. [Pure]
  93. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  94. internal static bool IsInteger(this JsValue value)
  95. {
  96. return value._type == InternalTypes.Integer;
  97. }
  98. [Pure]
  99. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  100. public static bool IsBoolean(this JsValue value)
  101. {
  102. return value._type == InternalTypes.Boolean;
  103. }
  104. [Pure]
  105. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  106. public static bool IsNull(this JsValue value)
  107. {
  108. return value._type == InternalTypes.Null;
  109. }
  110. [Pure]
  111. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  112. public static bool IsSymbol(this JsValue value)
  113. {
  114. return value._type == InternalTypes.Symbol;
  115. }
  116. /// <summary>
  117. /// https://tc39.es/ecma262/#sec-canbeheldweakly
  118. /// </summary>
  119. [Pure]
  120. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  121. internal static bool CanBeHeldWeakly(this JsValue value, GlobalSymbolRegistry symbolRegistry)
  122. {
  123. return value.IsObject() || (value.IsSymbol() && symbolRegistry.KeyForSymbol(value).IsUndefined());
  124. }
  125. [Pure]
  126. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  127. public static JsDate AsDate(this JsValue value)
  128. {
  129. if (!value.IsDate())
  130. {
  131. ExceptionHelper.ThrowArgumentException("The value is not a date");
  132. }
  133. return (JsDate) value;
  134. }
  135. [Pure]
  136. public static JsRegExp AsRegExp(this JsValue value)
  137. {
  138. if (!value.IsRegExp())
  139. {
  140. ExceptionHelper.ThrowArgumentException("The value is not a regex");
  141. }
  142. return (JsRegExp) value;
  143. }
  144. [Pure]
  145. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  146. public static ObjectInstance AsObject(this JsValue value)
  147. {
  148. if (!value.IsObject())
  149. {
  150. ExceptionHelper.ThrowArgumentException("The value is not an object");
  151. }
  152. return (ObjectInstance) value;
  153. }
  154. [Pure]
  155. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  156. public static TInstance AsInstance<TInstance>(this JsValue value) where TInstance : class
  157. {
  158. if (!value.IsObject())
  159. {
  160. ExceptionHelper.ThrowArgumentException("The value is not an object");
  161. }
  162. return (value as TInstance)!;
  163. }
  164. [Pure]
  165. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  166. public static JsArray AsArray(this JsValue value)
  167. {
  168. if (!value.IsArray())
  169. {
  170. ExceptionHelper.ThrowArgumentException("The value is not an array");
  171. }
  172. return (JsArray) value;
  173. }
  174. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  175. public static bool AsBoolean(this JsValue value)
  176. {
  177. if (value._type != InternalTypes.Boolean)
  178. {
  179. ThrowWrongTypeException(value, "boolean");
  180. }
  181. return ((JsBoolean) value)._value;
  182. }
  183. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  184. public static double AsNumber(this JsValue value)
  185. {
  186. if (!value.IsNumber())
  187. {
  188. ThrowWrongTypeException(value, "number");
  189. }
  190. return ((JsNumber) value)._value;
  191. }
  192. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  193. internal static int AsInteger(this JsValue value)
  194. {
  195. return (int) ((JsNumber) value)._value;
  196. }
  197. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  198. internal static BigInteger AsBigInt(this JsValue value)
  199. {
  200. return ((JsBigInt) value)._value;
  201. }
  202. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  203. public static string AsString(this JsValue value)
  204. {
  205. if (!value.IsString())
  206. {
  207. ThrowWrongTypeException(value, "string");
  208. }
  209. return value.ToString();
  210. }
  211. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  212. public static bool IsArrayBuffer(this JsValue value)
  213. {
  214. return value is JsArrayBuffer;
  215. }
  216. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  217. public static byte[]? AsArrayBuffer(this JsValue value)
  218. {
  219. if (!value.IsArrayBuffer())
  220. {
  221. ThrowWrongTypeException(value, "ArrayBuffer");
  222. }
  223. return ((JsArrayBuffer) value)._arrayBufferData;
  224. }
  225. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  226. public static bool IsDataView(this JsValue value)
  227. {
  228. return value is JsDataView;
  229. }
  230. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  231. public static byte[]? AsDataView(this JsValue value)
  232. {
  233. if (!value.IsDataView())
  234. {
  235. ThrowWrongTypeException(value, "DataView");
  236. }
  237. var dataView = (JsDataView) value;
  238. if (dataView._viewedArrayBuffer?._arrayBufferData == null)
  239. {
  240. return null; // should not happen
  241. }
  242. // create view
  243. var res = new byte[dataView._byteLength];
  244. Array.Copy(dataView._viewedArrayBuffer._arrayBufferData!, dataView._byteOffset, res, 0, dataView._byteLength);
  245. return res;
  246. }
  247. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  248. public static bool IsUint8Array(this JsValue value)
  249. {
  250. return value is JsTypedArray { _arrayElementType: TypedArrayElementType.Uint8 };
  251. }
  252. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  253. public static byte[] AsUint8Array(this JsValue value)
  254. {
  255. if (!value.IsUint8Array())
  256. {
  257. ThrowWrongTypeException(value, "Uint8Array");
  258. }
  259. return ((JsTypedArray) value).ToNativeArray<byte>();
  260. }
  261. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  262. public static bool IsUint8ClampedArray(this JsValue value)
  263. {
  264. return value is JsTypedArray { _arrayElementType: TypedArrayElementType.Uint8C };
  265. }
  266. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  267. public static byte[] AsUint8ClampedArray(this JsValue value)
  268. {
  269. if (!value.IsUint8ClampedArray())
  270. {
  271. ThrowWrongTypeException(value, "Uint8ClampedArray");
  272. }
  273. return ((JsTypedArray) value).ToNativeArray<byte>();
  274. }
  275. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  276. public static bool IsInt8Array(this JsValue value)
  277. {
  278. return value is JsTypedArray { _arrayElementType: TypedArrayElementType.Int8 };
  279. }
  280. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  281. public static sbyte[] AsInt8Array(this JsValue value)
  282. {
  283. if (!value.IsInt8Array())
  284. {
  285. ThrowWrongTypeException(value, "Int8Array");
  286. }
  287. return ((JsTypedArray) value).ToNativeArray<sbyte>();
  288. }
  289. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  290. public static bool IsInt16Array(this JsValue value)
  291. {
  292. return value is JsTypedArray { _arrayElementType: TypedArrayElementType.Int16 };
  293. }
  294. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  295. public static short[] AsInt16Array(this JsValue value)
  296. {
  297. if (!value.IsInt16Array())
  298. {
  299. ThrowWrongTypeException(value, "Int16Array");
  300. }
  301. return ((JsTypedArray) value).ToNativeArray<short>();
  302. }
  303. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  304. public static bool IsUint16Array(this JsValue value)
  305. {
  306. return value is JsTypedArray { _arrayElementType: TypedArrayElementType.Uint16 };
  307. }
  308. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  309. public static ushort[] AsUint16Array(this JsValue value)
  310. {
  311. if (!value.IsUint16Array())
  312. {
  313. ThrowWrongTypeException(value, "Uint16Array");
  314. }
  315. return ((JsTypedArray) value).ToNativeArray<ushort>();
  316. }
  317. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  318. public static bool IsInt32Array(this JsValue value)
  319. {
  320. return value is JsTypedArray { _arrayElementType: TypedArrayElementType.Int32 };
  321. }
  322. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  323. public static int[] AsInt32Array(this JsValue value)
  324. {
  325. if (!value.IsInt32Array())
  326. {
  327. ThrowWrongTypeException(value, "Int32Array");
  328. }
  329. return ((JsTypedArray) value).ToNativeArray<int>();
  330. }
  331. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  332. public static bool IsUint32Array(this JsValue value)
  333. {
  334. return value is JsTypedArray { _arrayElementType: TypedArrayElementType.Uint32 };
  335. }
  336. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  337. public static uint[] AsUint32Array(this JsValue value)
  338. {
  339. if (!value.IsUint32Array())
  340. {
  341. ThrowWrongTypeException(value, "Uint32Array");
  342. }
  343. return ((JsTypedArray) value).ToNativeArray<uint>();
  344. }
  345. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  346. public static bool IsBigInt64Array(this JsValue value)
  347. {
  348. return value is JsTypedArray { _arrayElementType: TypedArrayElementType.BigInt64 };
  349. }
  350. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  351. public static long[] AsBigInt64Array(this JsValue value)
  352. {
  353. if (!value.IsBigInt64Array())
  354. {
  355. ThrowWrongTypeException(value, "BigInt64Array");
  356. }
  357. return ((JsTypedArray) value).ToNativeArray<long>();
  358. }
  359. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  360. public static bool IsBigUint64Array(this JsValue value)
  361. {
  362. return value is JsTypedArray { _arrayElementType: TypedArrayElementType.BigUint64 };
  363. }
  364. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  365. public static ulong[] AsBigUint64Array(this JsValue value)
  366. {
  367. if (!value.IsBigUint64Array())
  368. {
  369. ThrowWrongTypeException(value, "BigUint64Array");
  370. }
  371. return ((JsTypedArray) value).ToNativeArray<ulong>();
  372. }
  373. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  374. public static bool IsFloat16Array(this JsValue value)
  375. {
  376. return value is JsTypedArray { _arrayElementType: TypedArrayElementType.Float16 };
  377. }
  378. #if SUPPORTS_HALF
  379. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  380. public static Half[] AsFloat16Array(this JsValue value)
  381. {
  382. if (!value.IsFloat16Array())
  383. {
  384. ThrowWrongTypeException(value, "Float16Array");
  385. }
  386. return ((JsTypedArray) value).ToNativeArray<Half>();
  387. }
  388. #endif
  389. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  390. public static bool IsFloat32Array(this JsValue value)
  391. {
  392. return value is JsTypedArray { _arrayElementType: TypedArrayElementType.Float32 };
  393. }
  394. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  395. public static float[] AsFloat32Array(this JsValue value)
  396. {
  397. if (!value.IsFloat32Array())
  398. {
  399. ThrowWrongTypeException(value, "Float32Array");
  400. }
  401. return ((JsTypedArray) value).ToNativeArray<float>();
  402. }
  403. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  404. public static bool IsFloat64Array(this JsValue value)
  405. {
  406. return value is JsTypedArray { _arrayElementType: TypedArrayElementType.Float64 };
  407. }
  408. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  409. public static double[] AsFloat64Array(this JsValue value)
  410. {
  411. if (!value.IsFloat64Array())
  412. {
  413. ThrowWrongTypeException(value, "Float64Array");
  414. }
  415. return ((JsTypedArray) value).ToNativeArray<double>();
  416. }
  417. [Pure]
  418. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  419. public static T? TryCast<T>(this JsValue value) where T : class
  420. {
  421. return value as T;
  422. }
  423. [Pure]
  424. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  425. public static T? TryCast<T>(this JsValue value, Action<JsValue> fail) where T : class
  426. {
  427. if (value is T o)
  428. {
  429. return o;
  430. }
  431. fail.Invoke(value);
  432. return null;
  433. }
  434. [Pure]
  435. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  436. public static T? As<T>(this JsValue value) where T : ObjectInstance
  437. {
  438. if (value.IsObject())
  439. {
  440. return value as T;
  441. }
  442. return null;
  443. }
  444. [Pure]
  445. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  446. public static Function AsFunctionInstance(this JsValue value)
  447. {
  448. if (value is not Function instance)
  449. {
  450. ThrowWrongTypeException(value, "FunctionInstance");
  451. return null!;
  452. }
  453. return instance;
  454. }
  455. [Pure]
  456. public static JsValue Call(this JsValue value)
  457. {
  458. if (value is ObjectInstance objectInstance)
  459. {
  460. var engine = objectInstance.Engine;
  461. return engine.Call(value, Array.Empty<JsValue>());
  462. }
  463. return ThrowNotObject(value);
  464. }
  465. [Pure]
  466. public static JsValue Call(this JsValue value, JsValue arg1)
  467. {
  468. if (value is ObjectInstance objectInstance)
  469. {
  470. var engine = objectInstance.Engine;
  471. var arguments = engine._jsValueArrayPool.RentArray(1);
  472. arguments[0] = arg1;
  473. var result = engine.Call(value, arguments);
  474. engine._jsValueArrayPool.ReturnArray(arguments);
  475. return result;
  476. }
  477. return ThrowNotObject(value);
  478. }
  479. [Pure]
  480. public static JsValue Call(this JsValue value, JsValue arg1, JsValue arg2)
  481. {
  482. if (value is ObjectInstance objectInstance)
  483. {
  484. var engine = objectInstance.Engine;
  485. var arguments = engine._jsValueArrayPool.RentArray(2);
  486. arguments[0] = arg1;
  487. arguments[1] = arg2;
  488. var result = engine.Call(value, arguments);
  489. engine._jsValueArrayPool.ReturnArray(arguments);
  490. return result;
  491. }
  492. return ThrowNotObject(value);
  493. }
  494. [Pure]
  495. public static JsValue Call(this JsValue value, JsValue arg1, JsValue arg2, JsValue arg3)
  496. {
  497. if (value is ObjectInstance objectInstance)
  498. {
  499. var engine = objectInstance.Engine;
  500. var arguments = engine._jsValueArrayPool.RentArray(3);
  501. arguments[0] = arg1;
  502. arguments[1] = arg2;
  503. arguments[2] = arg3;
  504. var result = engine.Call(value, arguments);
  505. engine._jsValueArrayPool.ReturnArray(arguments);
  506. return result;
  507. }
  508. return ThrowNotObject(value);
  509. }
  510. [Pure]
  511. public static JsValue Call(this JsValue value, params JsCallArguments arguments)
  512. {
  513. if (value is ObjectInstance objectInstance)
  514. {
  515. return objectInstance.Engine.Call(value, arguments);
  516. }
  517. return ThrowNotObject(value);
  518. }
  519. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  520. public static JsValue Call(this JsValue value, JsValue thisObj, JsCallArguments arguments)
  521. {
  522. if (value is ObjectInstance objectInstance)
  523. {
  524. return objectInstance.Engine.Call(value, thisObj, arguments);
  525. }
  526. return ThrowNotObject(value);
  527. }
  528. [MethodImpl(MethodImplOptions.NoInlining)]
  529. private static JsValue ThrowNotObject(JsValue value)
  530. {
  531. ExceptionHelper.ThrowArgumentException(value + " is not object");
  532. return null;
  533. }
  534. /// <summary>
  535. /// If the value is a Promise
  536. /// 1. If "Fulfilled" returns the value it was fulfilled with
  537. /// 2. If "Rejected" throws "PromiseRejectedException" with the rejection reason
  538. /// 3. If "Pending" throws "InvalidOperationException". Should be called only in "Settled" state
  539. /// Else
  540. /// returns the value intact
  541. /// </summary>
  542. /// <param name="value">value to unwrap</param>
  543. /// <returns>inner value if Promise the value itself otherwise</returns>
  544. public static JsValue UnwrapIfPromise(this JsValue value) => UnwrapIfPromise(value, TimeSpan.FromSeconds(10));
  545. /// <summary>
  546. /// If the value is a Promise
  547. /// 1. If "Fulfilled" returns the value it was fulfilled with
  548. /// 2. If "Rejected" throws "PromiseRejectedException" with the rejection reason
  549. /// 3. If "Pending" throws "InvalidOperationException". Should be called only in "Settled" state
  550. /// Else
  551. /// returns the value intact
  552. /// </summary>
  553. /// <param name="value">value to unwrap</param>
  554. /// <param name="timeout">timeout to wait</param>
  555. /// <returns>inner value if Promise the value itself otherwise</returns>
  556. public static JsValue UnwrapIfPromise(this JsValue value, TimeSpan timeout)
  557. {
  558. if (value is JsPromise promise)
  559. {
  560. var engine = promise.Engine;
  561. var completedEvent = promise.CompletedEvent;
  562. engine.RunAvailableContinuations();
  563. if (!completedEvent.Wait(timeout))
  564. {
  565. ExceptionHelper.ThrowPromiseRejectedException($"Timeout of {timeout} reached");
  566. }
  567. switch (promise.State)
  568. {
  569. case PromiseState.Pending:
  570. ExceptionHelper.ThrowInvalidOperationException("'UnwrapIfPromise' called before Promise was settled");
  571. return null;
  572. case PromiseState.Fulfilled:
  573. return promise.Value;
  574. case PromiseState.Rejected:
  575. ExceptionHelper.ThrowPromiseRejectedException(promise.Value);
  576. return null;
  577. default:
  578. ExceptionHelper.ThrowArgumentOutOfRangeException();
  579. return null;
  580. }
  581. }
  582. return value;
  583. }
  584. [MethodImpl(MethodImplOptions.NoInlining)]
  585. private static void ThrowWrongTypeException(JsValue value, string expectedType)
  586. {
  587. ExceptionHelper.ThrowArgumentException($"Expected {expectedType} but got {value._type}");
  588. }
  589. internal static BigInteger ToBigInteger(this JsValue value, Engine engine)
  590. {
  591. try
  592. {
  593. return TypeConverter.ToBigInt(value);
  594. }
  595. catch (ParseErrorException ex)
  596. {
  597. ExceptionHelper.ThrowSyntaxError(engine.Realm, ex.Message);
  598. return default;
  599. }
  600. }
  601. internal static ICallable GetCallable(this JsValue source, Realm realm)
  602. {
  603. if (source is ICallable callable)
  604. {
  605. return callable;
  606. }
  607. ExceptionHelper.ThrowTypeError(realm, "Argument must be callable");
  608. return null;
  609. }
  610. /// <summary>
  611. /// https://tc39.es/ecma262/#sec-getarraybuffermaxbytelengthoption
  612. /// </summary>
  613. internal static uint? GetArrayBufferMaxByteLengthOption(this JsValue options)
  614. {
  615. if (options is not JsObject oi)
  616. {
  617. return null;
  618. }
  619. var maxByteLength = options.Get("maxByteLength");
  620. if (maxByteLength.IsUndefined())
  621. {
  622. return null;
  623. }
  624. return TypeConverter.ToIndex(oi.Engine.Realm, maxByteLength);
  625. }
  626. /// <summary>
  627. /// https://tc39.es/ecma262/#sec-canonicalize-keyed-collection-key
  628. /// </summary>
  629. internal static JsValue CanonicalizeKeyedCollectionKey(this JsValue key)
  630. {
  631. return key is JsNumber number && number.IsNegativeZero() ? JsNumber.PositiveZero : key;
  632. }
  633. }