JsValueExtensions.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  1. using System.Diagnostics.Contracts;
  2. using System.Numerics;
  3. using System.Runtime.CompilerServices;
  4. using Esprima;
  5. using Jint.Native;
  6. using Jint.Native.Function;
  7. using Jint.Native.Object;
  8. using Jint.Native.Promise;
  9. using Jint.Native.RegExp;
  10. using Jint.Native.Symbol;
  11. using Jint.Native.TypedArray;
  12. using Jint.Runtime;
  13. namespace Jint
  14. {
  15. public static class JsValueExtensions
  16. {
  17. [Pure]
  18. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  19. public static bool IsPrimitive(this JsValue value)
  20. {
  21. return (value._type & (InternalTypes.Primitive | InternalTypes.Undefined | InternalTypes.Null)) != 0;
  22. }
  23. [Pure]
  24. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  25. public static bool IsUndefined(this JsValue value)
  26. {
  27. return value._type == InternalTypes.Undefined;
  28. }
  29. [Pure]
  30. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  31. internal static bool IsNullOrUndefined(this JsValue value)
  32. {
  33. return value._type < InternalTypes.Boolean;
  34. }
  35. [Pure]
  36. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  37. public static bool IsDate(this JsValue value)
  38. {
  39. return value is JsDate;
  40. }
  41. [Pure]
  42. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  43. public static bool IsPromise(this JsValue value)
  44. {
  45. return value is PromiseInstance;
  46. }
  47. [Pure]
  48. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  49. public static bool IsRegExp(this JsValue value)
  50. {
  51. if (value is not ObjectInstance oi)
  52. {
  53. return false;
  54. }
  55. var matcher = oi.Get(GlobalSymbolRegistry.Match);
  56. if (!matcher.IsUndefined())
  57. {
  58. return TypeConverter.ToBoolean(matcher);
  59. }
  60. return value is RegExpInstance;
  61. }
  62. [Pure]
  63. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  64. public static bool IsObject(this JsValue value)
  65. {
  66. return (value._type & InternalTypes.Object) != 0;
  67. }
  68. [Pure]
  69. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  70. public static bool IsString(this JsValue value)
  71. {
  72. return (value._type & InternalTypes.String) != 0;
  73. }
  74. [Pure]
  75. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  76. public static bool IsNumber(this JsValue value)
  77. {
  78. return (value._type & (InternalTypes.Number | InternalTypes.Integer)) != 0;
  79. }
  80. [Pure]
  81. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  82. public static bool IsBigInt(this JsValue value)
  83. {
  84. return (value._type & InternalTypes.BigInt) != 0;
  85. }
  86. [Pure]
  87. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  88. internal static bool IsInteger(this JsValue value)
  89. {
  90. return value._type == InternalTypes.Integer;
  91. }
  92. [Pure]
  93. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  94. public static bool IsBoolean(this JsValue value)
  95. {
  96. return value._type == InternalTypes.Boolean;
  97. }
  98. [Pure]
  99. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  100. public static bool IsNull(this JsValue value)
  101. {
  102. return value._type == InternalTypes.Null;
  103. }
  104. [Pure]
  105. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  106. public static bool IsSymbol(this JsValue value)
  107. {
  108. return value._type == InternalTypes.Symbol;
  109. }
  110. [Pure]
  111. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  112. internal static bool CanBeHeldWeakly(this JsValue value, GlobalSymbolRegistry symbolRegistry)
  113. {
  114. return value.IsObject() || (value.IsSymbol() && !symbolRegistry.ContainsCustom(value));
  115. }
  116. [Pure]
  117. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  118. public static JsDate AsDate(this JsValue value)
  119. {
  120. if (!value.IsDate())
  121. {
  122. ExceptionHelper.ThrowArgumentException("The value is not a date");
  123. }
  124. return (JsDate) value;
  125. }
  126. [Pure]
  127. public static RegExpInstance AsRegExp(this JsValue value)
  128. {
  129. if (!value.IsRegExp())
  130. {
  131. ExceptionHelper.ThrowArgumentException("The value is not a regex");
  132. }
  133. return (RegExpInstance) value;
  134. }
  135. [Pure]
  136. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  137. public static ObjectInstance AsObject(this JsValue value)
  138. {
  139. if (!value.IsObject())
  140. {
  141. ExceptionHelper.ThrowArgumentException("The value is not an object");
  142. }
  143. return (ObjectInstance) value;
  144. }
  145. [Pure]
  146. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  147. public static TInstance AsInstance<TInstance>(this JsValue value) where TInstance : class
  148. {
  149. if (!value.IsObject())
  150. {
  151. ExceptionHelper.ThrowArgumentException("The value is not an object");
  152. }
  153. return (value as TInstance)!;
  154. }
  155. [Pure]
  156. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  157. public static JsArray AsArray(this JsValue value)
  158. {
  159. if (!value.IsArray())
  160. {
  161. ExceptionHelper.ThrowArgumentException("The value is not an array");
  162. }
  163. return (JsArray) value;
  164. }
  165. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  166. public static bool AsBoolean(this JsValue value)
  167. {
  168. if (value._type != InternalTypes.Boolean)
  169. {
  170. ThrowWrongTypeException(value, "boolean");
  171. }
  172. return ((JsBoolean) value)._value;
  173. }
  174. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  175. public static double AsNumber(this JsValue value)
  176. {
  177. if (!value.IsNumber())
  178. {
  179. ThrowWrongTypeException(value, "number");
  180. }
  181. return ((JsNumber) value)._value;
  182. }
  183. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  184. internal static int AsInteger(this JsValue value)
  185. {
  186. return (int) ((JsNumber) value)._value;
  187. }
  188. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  189. internal static BigInteger AsBigInt(this JsValue value)
  190. {
  191. return ((JsBigInt) value)._value;
  192. }
  193. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  194. public static string AsString(this JsValue value)
  195. {
  196. if (!value.IsString())
  197. {
  198. ThrowWrongTypeException(value, "string");
  199. }
  200. return value.ToString();
  201. }
  202. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  203. public static bool IsUint8Array(this JsValue value)
  204. {
  205. return value is TypedArrayInstance { _arrayElementType: TypedArrayElementType.Uint8 };
  206. }
  207. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  208. public static byte[] AsUint8Array(this JsValue value)
  209. {
  210. if (!value.IsUint8Array())
  211. {
  212. ThrowWrongTypeException(value, "Uint8Array");
  213. }
  214. return ((TypedArrayInstance) value).ToNativeArray<byte>();
  215. }
  216. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  217. public static bool IsUint8ClampedArray(this JsValue value)
  218. {
  219. return value is TypedArrayInstance { _arrayElementType: TypedArrayElementType.Uint8C };
  220. }
  221. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  222. public static byte[] AsUint8ClampedArray(this JsValue value)
  223. {
  224. if (!value.IsUint8ClampedArray())
  225. {
  226. ThrowWrongTypeException(value, "Uint8ClampedArray");
  227. }
  228. return ((TypedArrayInstance) value).ToNativeArray<byte>();
  229. }
  230. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  231. public static bool IsInt8Array(this JsValue value)
  232. {
  233. return value is TypedArrayInstance { _arrayElementType: TypedArrayElementType.Int8 };
  234. }
  235. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  236. public static sbyte[] AsInt8Array(this JsValue value)
  237. {
  238. if (!value.IsInt8Array())
  239. {
  240. ThrowWrongTypeException(value, "Int8Array");
  241. }
  242. return ((TypedArrayInstance) value).ToNativeArray<sbyte>();
  243. }
  244. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  245. public static bool IsInt16Array(this JsValue value)
  246. {
  247. return value is TypedArrayInstance { _arrayElementType: TypedArrayElementType.Int16 };
  248. }
  249. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  250. public static short[] AsInt16Array(this JsValue value)
  251. {
  252. if (!value.IsInt16Array())
  253. {
  254. ThrowWrongTypeException(value, "Int16Array");
  255. }
  256. return ((TypedArrayInstance) value).ToNativeArray<short>();
  257. }
  258. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  259. public static bool IsUint16Array(this JsValue value)
  260. {
  261. return value is TypedArrayInstance { _arrayElementType: TypedArrayElementType.Uint16 };
  262. }
  263. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  264. public static ushort[] AsUint16Array(this JsValue value)
  265. {
  266. if (!value.IsUint16Array())
  267. {
  268. ThrowWrongTypeException(value, "Uint16Array");
  269. }
  270. return ((TypedArrayInstance) value).ToNativeArray<ushort>();
  271. }
  272. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  273. public static bool IsInt32Array(this JsValue value)
  274. {
  275. return value is TypedArrayInstance { _arrayElementType: TypedArrayElementType.Int32 };
  276. }
  277. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  278. public static int[] AsInt32Array(this JsValue value)
  279. {
  280. if (!value.IsInt32Array())
  281. {
  282. ThrowWrongTypeException(value, "Int32Array");
  283. }
  284. return ((TypedArrayInstance) value).ToNativeArray<int>();
  285. }
  286. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  287. public static bool IsUint32Array(this JsValue value)
  288. {
  289. return value is TypedArrayInstance { _arrayElementType: TypedArrayElementType.Uint32 };
  290. }
  291. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  292. public static uint[] AsUint32Array(this JsValue value)
  293. {
  294. if (!value.IsUint32Array())
  295. {
  296. ThrowWrongTypeException(value, "Uint32Array");
  297. }
  298. return ((TypedArrayInstance) value).ToNativeArray<uint>();
  299. }
  300. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  301. public static bool IsBigInt64Array(this JsValue value)
  302. {
  303. return value is TypedArrayInstance { _arrayElementType: TypedArrayElementType.BigInt64 };
  304. }
  305. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  306. public static long[] AsBigInt64Array(this JsValue value)
  307. {
  308. if (!value.IsBigInt64Array())
  309. {
  310. ThrowWrongTypeException(value, "BigInt64Array");
  311. }
  312. return ((TypedArrayInstance) value).ToNativeArray<long>();
  313. }
  314. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  315. public static bool IsBigUint64Array(this JsValue value)
  316. {
  317. return value is TypedArrayInstance { _arrayElementType: TypedArrayElementType.BigUint64 };
  318. }
  319. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  320. public static ulong[] AsBigUint64Array(this JsValue value)
  321. {
  322. if (!value.IsBigUint64Array())
  323. {
  324. ThrowWrongTypeException(value, "BigUint64Array");
  325. }
  326. return ((TypedArrayInstance) value).ToNativeArray<ulong>();
  327. }
  328. [Pure]
  329. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  330. public static T? TryCast<T>(this JsValue value) where T : class
  331. {
  332. return value as T;
  333. }
  334. [Pure]
  335. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  336. public static T? TryCast<T>(this JsValue value, Action<JsValue> fail) where T : class
  337. {
  338. if (value is T o)
  339. {
  340. return o;
  341. }
  342. fail.Invoke(value);
  343. return null;
  344. }
  345. [Pure]
  346. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  347. public static T? As<T>(this JsValue value) where T : ObjectInstance
  348. {
  349. if (value.IsObject())
  350. {
  351. return value as T;
  352. }
  353. return null;
  354. }
  355. [Pure]
  356. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  357. public static FunctionInstance AsFunctionInstance(this JsValue value)
  358. {
  359. if (value is not FunctionInstance instance)
  360. {
  361. ThrowWrongTypeException(value, "FunctionInstance");
  362. return null!;
  363. }
  364. return instance;
  365. }
  366. [Pure]
  367. public static JsValue Call(this JsValue value)
  368. {
  369. if (value is ObjectInstance objectInstance)
  370. {
  371. var engine = objectInstance.Engine;
  372. return engine.Call(value, Array.Empty<JsValue>());
  373. }
  374. return ThrowNotObject(value);
  375. }
  376. [Pure]
  377. public static JsValue Call(this JsValue value, JsValue arg1)
  378. {
  379. if (value is ObjectInstance objectInstance)
  380. {
  381. var engine = objectInstance.Engine;
  382. var arguments = engine._jsValueArrayPool.RentArray(1);
  383. arguments[0] = arg1;
  384. var result = engine.Call(value, arguments);
  385. engine._jsValueArrayPool.ReturnArray(arguments);
  386. return result;
  387. }
  388. return ThrowNotObject(value);
  389. }
  390. [Pure]
  391. public static JsValue Call(this JsValue value, JsValue arg1, JsValue arg2)
  392. {
  393. if (value is ObjectInstance objectInstance)
  394. {
  395. var engine = objectInstance.Engine;
  396. var arguments = engine._jsValueArrayPool.RentArray(2);
  397. arguments[0] = arg1;
  398. arguments[1] = arg2;
  399. var result = engine.Call(value, arguments);
  400. engine._jsValueArrayPool.ReturnArray(arguments);
  401. return result;
  402. }
  403. return ThrowNotObject(value);
  404. }
  405. [Pure]
  406. public static JsValue Call(this JsValue value, JsValue arg1, JsValue arg2, JsValue arg3)
  407. {
  408. if (value is ObjectInstance objectInstance)
  409. {
  410. var engine = objectInstance.Engine;
  411. var arguments = engine._jsValueArrayPool.RentArray(3);
  412. arguments[0] = arg1;
  413. arguments[1] = arg2;
  414. arguments[2] = arg3;
  415. var result = engine.Call(value, arguments);
  416. engine._jsValueArrayPool.ReturnArray(arguments);
  417. return result;
  418. }
  419. return ThrowNotObject(value);
  420. }
  421. [Pure]
  422. public static JsValue Call(this JsValue value, params JsValue[] arguments)
  423. {
  424. if (value is ObjectInstance objectInstance)
  425. {
  426. return objectInstance.Engine.Call(value, arguments);
  427. }
  428. return ThrowNotObject(value);
  429. }
  430. [Pure]
  431. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  432. public static JsValue Call(this JsValue value, JsValue thisObj, JsValue[] arguments)
  433. {
  434. if (value is ObjectInstance objectInstance)
  435. {
  436. return objectInstance.Engine.Call(value, thisObj, arguments);
  437. }
  438. return ThrowNotObject(value);
  439. }
  440. [MethodImpl(MethodImplOptions.NoInlining)]
  441. private static JsValue ThrowNotObject(JsValue value)
  442. {
  443. ExceptionHelper.ThrowArgumentException(value + " is not object");
  444. return null;
  445. }
  446. /// <summary>
  447. /// If the value is a Promise
  448. /// 1. If "Fulfilled" returns the value it was fulfilled with
  449. /// 2. If "Rejected" throws "PromiseRejectedException" with the rejection reason
  450. /// 3. If "Pending" throws "InvalidOperationException". Should be called only in "Settled" state
  451. /// Else
  452. /// returns the value intact
  453. /// </summary>
  454. /// <param name="value">value to unwrap</param>
  455. /// <returns>inner value if Promise the value itself otherwise</returns>
  456. public static JsValue UnwrapIfPromise(this JsValue value)
  457. {
  458. if (value is PromiseInstance promise)
  459. {
  460. switch (promise.State)
  461. {
  462. case PromiseState.Pending:
  463. ExceptionHelper.ThrowInvalidOperationException("'UnwrapIfPromise' called before Promise was settled");
  464. return null;
  465. case PromiseState.Fulfilled:
  466. return promise.Value;
  467. case PromiseState.Rejected:
  468. ExceptionHelper.ThrowPromiseRejectedException(promise.Value);
  469. return null;
  470. default:
  471. ExceptionHelper.ThrowArgumentOutOfRangeException();
  472. return null;
  473. }
  474. }
  475. return value;
  476. }
  477. [MethodImpl(MethodImplOptions.NoInlining)]
  478. private static void ThrowWrongTypeException(JsValue value, string expectedType)
  479. {
  480. ExceptionHelper.ThrowArgumentException($"Expected {expectedType} but got {value._type}");
  481. }
  482. internal static BigInteger ToBigInteger(this JsValue value, Engine engine)
  483. {
  484. try
  485. {
  486. return TypeConverter.ToBigInt(value);
  487. }
  488. catch (ParserException ex)
  489. {
  490. ExceptionHelper.ThrowSyntaxError(engine.Realm, ex.Message);
  491. return default;
  492. }
  493. }
  494. }
  495. }