JsValueExtensions.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. using System;
  2. using System.Diagnostics.Contracts;
  3. using System.Runtime.CompilerServices;
  4. using Jint.Native;
  5. using Jint.Native.Array;
  6. using Jint.Native.Date;
  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 DateInstance;
  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 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. internal static bool IsInteger(this JsValue value)
  83. {
  84. return value._type == InternalTypes.Integer;
  85. }
  86. [Pure]
  87. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  88. public static bool IsBoolean(this JsValue value)
  89. {
  90. return value._type == InternalTypes.Boolean;
  91. }
  92. [Pure]
  93. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  94. public static bool IsNull(this JsValue value)
  95. {
  96. return value._type == InternalTypes.Null;
  97. }
  98. [Pure]
  99. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  100. public static bool IsSymbol(this JsValue value)
  101. {
  102. return value._type == InternalTypes.Symbol;
  103. }
  104. [Pure]
  105. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  106. public static DateInstance AsDate(this JsValue value)
  107. {
  108. if (!value.IsDate())
  109. {
  110. ExceptionHelper.ThrowArgumentException("The value is not a date");
  111. }
  112. return value as DateInstance;
  113. }
  114. [Pure]
  115. public static RegExpInstance AsRegExp(this JsValue value)
  116. {
  117. if (!value.IsRegExp())
  118. {
  119. ExceptionHelper.ThrowArgumentException("The value is not a regex");
  120. }
  121. return value as RegExpInstance;
  122. }
  123. [Pure]
  124. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  125. public static ObjectInstance AsObject(this JsValue value)
  126. {
  127. if (!value.IsObject())
  128. {
  129. ExceptionHelper.ThrowArgumentException("The value is not an object");
  130. }
  131. return value as ObjectInstance;
  132. }
  133. [Pure]
  134. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  135. public static TInstance AsInstance<TInstance>(this JsValue value) where TInstance : class
  136. {
  137. if (!value.IsObject())
  138. {
  139. ExceptionHelper.ThrowArgumentException("The value is not an object");
  140. }
  141. return value as TInstance;
  142. }
  143. [Pure]
  144. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  145. public static ArrayInstance AsArray(this JsValue value)
  146. {
  147. if (!value.IsArray())
  148. {
  149. ExceptionHelper.ThrowArgumentException("The value is not an array");
  150. }
  151. return value as ArrayInstance;
  152. }
  153. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  154. public static bool AsBoolean(this JsValue value)
  155. {
  156. if (value._type != InternalTypes.Boolean)
  157. {
  158. ThrowWrongTypeException(value, "boolean");
  159. }
  160. return ((JsBoolean) value)._value;
  161. }
  162. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  163. public static double AsNumber(this JsValue value)
  164. {
  165. if (!value.IsNumber())
  166. {
  167. ThrowWrongTypeException(value, "number");
  168. }
  169. return ((JsNumber) value)._value;
  170. }
  171. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  172. internal static int AsInteger(this JsValue value)
  173. {
  174. return (int) ((JsNumber) value)._value;
  175. }
  176. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  177. public static string AsString(this JsValue value)
  178. {
  179. if (!value.IsString())
  180. {
  181. ThrowWrongTypeException(value, "string");
  182. }
  183. return value.ToString();
  184. }
  185. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  186. public static bool IsUint8Array(this JsValue value)
  187. {
  188. return value is TypedArrayInstance { _arrayElementType: TypedArrayElementType.Uint8 };
  189. }
  190. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  191. public static byte[] AsUint8Array(this JsValue value)
  192. {
  193. if (!value.IsUint8Array())
  194. {
  195. ThrowWrongTypeException(value, "Uint8Array");
  196. }
  197. return ((TypedArrayInstance) value).ToNativeArray<byte>();
  198. }
  199. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  200. public static bool IsUint8ClampedArray(this JsValue value)
  201. {
  202. return value is TypedArrayInstance { _arrayElementType: TypedArrayElementType.Uint8C };
  203. }
  204. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  205. public static byte[] AsUint8ClampedArray(this JsValue value)
  206. {
  207. if (!value.IsUint8ClampedArray())
  208. {
  209. ThrowWrongTypeException(value, "Uint8ClampedArray");
  210. }
  211. return ((TypedArrayInstance) value).ToNativeArray<byte>();
  212. }
  213. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  214. public static bool IsInt8Array(this JsValue value)
  215. {
  216. return value is TypedArrayInstance { _arrayElementType: TypedArrayElementType.Int8 };
  217. }
  218. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  219. public static sbyte[] AsInt8Array(this JsValue value)
  220. {
  221. if (!value.IsInt8Array())
  222. {
  223. ThrowWrongTypeException(value, "Int8Array");
  224. }
  225. return ((TypedArrayInstance) value).ToNativeArray<sbyte>();
  226. }
  227. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  228. public static bool IsInt16Array(this JsValue value)
  229. {
  230. return value is TypedArrayInstance { _arrayElementType: TypedArrayElementType.Int16 };
  231. }
  232. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  233. public static short[] AsInt16Array(this JsValue value)
  234. {
  235. if (!value.IsInt16Array())
  236. {
  237. ThrowWrongTypeException(value, "Int16Array");
  238. }
  239. return ((TypedArrayInstance) value).ToNativeArray<short>();
  240. }
  241. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  242. public static bool IsUint16Array(this JsValue value)
  243. {
  244. return value is TypedArrayInstance { _arrayElementType: TypedArrayElementType.Uint16 };
  245. }
  246. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  247. public static ushort[] AsUint16Array(this JsValue value)
  248. {
  249. if (!value.IsUint16Array())
  250. {
  251. ThrowWrongTypeException(value, "Uint16Array");
  252. }
  253. return ((TypedArrayInstance) value).ToNativeArray<ushort>();
  254. }
  255. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  256. public static bool IsInt32Array(this JsValue value)
  257. {
  258. return value is TypedArrayInstance { _arrayElementType: TypedArrayElementType.Int32 };
  259. }
  260. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  261. public static int[] AsInt32Array(this JsValue value)
  262. {
  263. if (!value.IsInt32Array())
  264. {
  265. ThrowWrongTypeException(value, "Int32Array");
  266. }
  267. return ((TypedArrayInstance) value).ToNativeArray<int>();
  268. }
  269. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  270. public static bool IsUint32Array(this JsValue value)
  271. {
  272. return value is TypedArrayInstance { _arrayElementType: TypedArrayElementType.Uint32 };
  273. }
  274. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  275. public static uint[] AsUint32Array(this JsValue value)
  276. {
  277. if (!value.IsUint32Array())
  278. {
  279. ThrowWrongTypeException(value, "Uint32Array");
  280. }
  281. return ((TypedArrayInstance) value).ToNativeArray<uint>();
  282. }
  283. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  284. public static bool IsBigInt64Array(this JsValue value)
  285. {
  286. return value is TypedArrayInstance { _arrayElementType: TypedArrayElementType.BigInt64 };
  287. }
  288. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  289. public static long[] AsBigInt64Array(this JsValue value)
  290. {
  291. if (!value.IsUint32Array())
  292. {
  293. ThrowWrongTypeException(value, "BigInt64Array");
  294. }
  295. ExceptionHelper.ThrowNotImplementedException();
  296. return null;
  297. }
  298. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  299. public static bool IsBigUint64Array(this JsValue value)
  300. {
  301. return value is TypedArrayInstance { _arrayElementType: TypedArrayElementType.BigUint64 };
  302. }
  303. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  304. public static ulong[] AsBigUint64Array(this JsValue value)
  305. {
  306. if (!value.IsBigUint64Array())
  307. {
  308. ThrowWrongTypeException(value, "BigUint64Array");
  309. }
  310. ExceptionHelper.ThrowNotImplementedException();
  311. return null;
  312. }
  313. [Pure]
  314. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  315. public static T TryCast<T>(this JsValue value) where T : class
  316. {
  317. return value as T;
  318. }
  319. [Pure]
  320. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  321. public static T TryCast<T>(this JsValue value, Action<JsValue> fail) where T : class
  322. {
  323. if (value is T o)
  324. {
  325. return o;
  326. }
  327. fail.Invoke(value);
  328. return null;
  329. }
  330. [Pure]
  331. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  332. public static T As<T>(this JsValue value) where T : ObjectInstance
  333. {
  334. if (value.IsObject())
  335. {
  336. return value as T;
  337. }
  338. return null;
  339. }
  340. [Pure]
  341. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  342. public static bool IsCallable(this JsValue value)
  343. {
  344. return value.IsCallable;
  345. }
  346. [Pure]
  347. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  348. public static ICallable AsCallable(this JsValue value)
  349. {
  350. if (!value.IsCallable())
  351. {
  352. ThrowWrongTypeException(value, "Callable");
  353. }
  354. return value as ICallable;
  355. }
  356. [Pure]
  357. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  358. public static JsValue Call(this ICallable value, params JsValue[] arguments)
  359. {
  360. return value.Call(JsValue.Undefined, arguments);
  361. }
  362. /// <summary>
  363. /// If the value is a Promise
  364. /// 1. If "Fulfilled" returns the value it was fulfilled with
  365. /// 2. If "Rejected" throws "PromiseRejectedException" with the rejection reason
  366. /// 3. If "Pending" throws "InvalidOperationException". Should be called only in "Settled" state
  367. /// Else
  368. /// returns the value intact
  369. /// </summary>
  370. /// <param name="value">value to unwrap</param>
  371. /// <returns>inner value if Promise the value itself otherwise</returns>
  372. public static JsValue UnwrapIfPromise(this JsValue value)
  373. {
  374. if (value is PromiseInstance promise)
  375. {
  376. switch (promise.State)
  377. {
  378. case PromiseState.Pending:
  379. ExceptionHelper.ThrowInvalidOperationException("'UnwrapIfPromise' called before Promise was settled");
  380. return null;
  381. case PromiseState.Fulfilled:
  382. return promise.Value;
  383. case PromiseState.Rejected:
  384. ExceptionHelper.ThrowPromiseRejectedException(promise.Value);
  385. return null;
  386. default:
  387. ExceptionHelper.ThrowArgumentOutOfRangeException();
  388. return null;
  389. }
  390. }
  391. return value;
  392. }
  393. [MethodImpl(MethodImplOptions.NoInlining)]
  394. private static void ThrowWrongTypeException(JsValue value, string expectedType)
  395. {
  396. ExceptionHelper.ThrowArgumentException($"Expected {expectedType} but got {value._type}");
  397. }
  398. }
  399. }