JsValue.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Diagnostics.Contracts;
  5. using System.Threading;
  6. using Jint.Native.Array;
  7. using Jint.Native.Date;
  8. using Jint.Native.Object;
  9. using Jint.Native.RegExp;
  10. using Jint.Runtime;
  11. using Jint.Runtime.Interop;
  12. namespace Jint.Native
  13. {
  14. [DebuggerTypeProxy(typeof(JsValueDebugView))]
  15. public abstract class JsValue : IEquatable<JsValue>
  16. {
  17. public static readonly JsValue Undefined = new JsUndefined();
  18. public static readonly JsValue Null = new JsNull();
  19. [Pure]
  20. public bool IsPrimitive()
  21. {
  22. return Type != Types.Object && Type != Types.None;
  23. }
  24. [Pure]
  25. public bool IsUndefined()
  26. {
  27. return Type == Types.Undefined;
  28. }
  29. [Pure]
  30. public virtual bool IsArray()
  31. {
  32. return false;
  33. }
  34. [Pure]
  35. public virtual bool IsDate()
  36. {
  37. return false;
  38. }
  39. [Pure]
  40. public virtual bool IsRegExp()
  41. {
  42. return false;
  43. }
  44. [Pure]
  45. public bool IsObject()
  46. {
  47. return Type == Types.Object;
  48. }
  49. [Pure]
  50. public bool IsString()
  51. {
  52. return Type == Types.String;
  53. }
  54. [Pure]
  55. public bool IsNumber()
  56. {
  57. return Type == Types.Number;
  58. }
  59. [Pure]
  60. public bool IsBoolean()
  61. {
  62. return Type == Types.Boolean;
  63. }
  64. [Pure]
  65. public bool IsNull()
  66. {
  67. return Type == Types.Null;
  68. }
  69. [Pure]
  70. public bool IsCompletion()
  71. {
  72. return Type == Types.Completion;
  73. }
  74. [Pure]
  75. public bool IsSymbol()
  76. {
  77. return Type == Types.Symbol;
  78. }
  79. [Pure]
  80. public virtual ObjectInstance AsObject()
  81. {
  82. throw new ArgumentException("The value is not an object");
  83. }
  84. [Pure]
  85. public virtual TInstance AsInstance<TInstance>() where TInstance : class
  86. {
  87. throw new ArgumentException("The value is not an object");
  88. }
  89. [Pure]
  90. public virtual ArrayInstance AsArray()
  91. {
  92. throw new ArgumentException("The value is not an array");
  93. }
  94. [Pure]
  95. public virtual DateInstance AsDate()
  96. {
  97. throw new ArgumentException("The value is not a date");
  98. }
  99. [Pure]
  100. public virtual RegExpInstance AsRegExp()
  101. {
  102. throw new ArgumentException("The value is not a date");
  103. }
  104. [Pure]
  105. public virtual Completion AsCompletion()
  106. {
  107. if (Type != Types.Completion)
  108. {
  109. throw new ArgumentException("The value is not a completion record");
  110. }
  111. // TODO not implemented
  112. return null;
  113. }
  114. [Pure]
  115. public T TryCast<T>(Action<JsValue> fail = null) where T : class
  116. {
  117. if (IsObject())
  118. {
  119. var o = AsObject();
  120. var t = o as T;
  121. if (t != null)
  122. {
  123. return t;
  124. }
  125. }
  126. fail?.Invoke(this);
  127. return null;
  128. }
  129. public virtual bool Is<T>()
  130. {
  131. return false;
  132. }
  133. public virtual T As<T>() where T : ObjectInstance
  134. {
  135. return null;
  136. }
  137. [Pure]
  138. public virtual bool AsBoolean()
  139. {
  140. throw new ArgumentException("The value is not a boolean");
  141. }
  142. [Pure]
  143. public virtual string AsString()
  144. {
  145. throw new ArgumentException("The value is not a string");
  146. }
  147. [Pure]
  148. public virtual string AsSymbol()
  149. {
  150. throw new ArgumentException("The value is not a symbol");
  151. }
  152. [Pure]
  153. public virtual double AsNumber()
  154. {
  155. throw new ArgumentException("The value is not a number");
  156. }
  157. public abstract Types Type { get; }
  158. /// <summary>
  159. /// Creates a valid <see cref="JsValue"/> instance from any <see cref="Object"/> instance
  160. /// </summary>
  161. /// <param name="engine"></param>
  162. /// <param name="value"></param>
  163. /// <returns></returns>
  164. public static JsValue FromObject(Engine engine, object value)
  165. {
  166. if (value == null)
  167. {
  168. return Null;
  169. }
  170. if (value is JsValue jsValue)
  171. {
  172. return jsValue;
  173. }
  174. foreach (var converter in engine.Options._ObjectConverters)
  175. {
  176. if (converter.TryConvert(value, out var result))
  177. {
  178. return result;
  179. }
  180. }
  181. var valueType = value.GetType();
  182. var typeMappers = Engine.TypeMappers;
  183. if (typeMappers.TryGetValue(valueType, out var typeMapper))
  184. {
  185. return typeMapper(engine, value);
  186. }
  187. var type = value as Type;
  188. if (type != null)
  189. {
  190. var typeReference = TypeReference.CreateTypeReference(engine, type);
  191. return typeReference;
  192. }
  193. if (value is System.Array a)
  194. {
  195. JsValue Convert(Engine e, object v)
  196. {
  197. var array = (System.Array) v;
  198. var jsArray = engine.Array.Construct(a.Length);
  199. foreach (var item in array)
  200. {
  201. var jsItem = FromObject(engine, item);
  202. engine.Array.PrototypeObject.Push(jsArray, Arguments.From(jsItem));
  203. }
  204. return jsArray;
  205. }
  206. // racy, we don't care, worst case we'll catch up later
  207. Interlocked.CompareExchange(ref Engine.TypeMappers, new Dictionary<Type, Func<Engine, object, JsValue>>(typeMappers)
  208. {
  209. [valueType] = Convert
  210. }, typeMappers);
  211. return Convert(engine, a);
  212. }
  213. if (value is Delegate d)
  214. {
  215. return new DelegateWrapper(engine, d);
  216. }
  217. if (value.GetType().IsEnum())
  218. {
  219. return JsNumber.Create((int) value);
  220. }
  221. // if no known type could be guessed, wrap it as an ObjectInstance
  222. return new ObjectWrapper(engine, value);
  223. }
  224. /// <summary>
  225. /// Converts a <see cref="JsValue"/> to its underlying CLR value.
  226. /// </summary>
  227. /// <returns>The underlying CLR value of the <see cref="JsValue"/> instance.</returns>
  228. public abstract object ToObject();
  229. /// <summary>
  230. /// Invoke the current value as function.
  231. /// </summary>
  232. /// <param name="arguments">The arguments of the function call.</param>
  233. /// <returns>The value returned by the function call.</returns>
  234. public JsValue Invoke(params JsValue[] arguments)
  235. {
  236. return Invoke(Undefined, arguments);
  237. }
  238. /// <summary>
  239. /// Invoke the current value as function.
  240. /// </summary>
  241. /// <param name="thisObj">The this value inside the function call.</param>
  242. /// <param name="arguments">The arguments of the function call.</param>
  243. /// <returns>The value returned by the function call.</returns>
  244. public JsValue Invoke(JsValue thisObj, JsValue[] arguments)
  245. {
  246. var callable = TryCast<ICallable>();
  247. if (callable == null)
  248. {
  249. throw new ArgumentException("Can only invoke functions");
  250. }
  251. return callable.Call(thisObj, arguments);
  252. }
  253. public static bool ReturnOnAbruptCompletion(ref JsValue argument)
  254. {
  255. if (!argument.IsCompletion())
  256. {
  257. return false;
  258. }
  259. var completion = argument.AsCompletion();
  260. if (completion.IsAbrupt())
  261. {
  262. return true;
  263. }
  264. argument = completion.Value;
  265. return false;
  266. }
  267. public override string ToString()
  268. {
  269. return "None";
  270. }
  271. public static bool operator ==(JsValue a, JsValue b)
  272. {
  273. if ((object) a == null)
  274. {
  275. if ((object) b == null)
  276. {
  277. return true;
  278. }
  279. return false;
  280. }
  281. if ((object) b == null)
  282. {
  283. return false;
  284. }
  285. return a.Equals(b);
  286. }
  287. public static bool operator !=(JsValue a, JsValue b)
  288. {
  289. if ((object) a == null)
  290. {
  291. if ((object) b == null)
  292. {
  293. return false;
  294. }
  295. return true;
  296. }
  297. if ((object) b == null)
  298. {
  299. return true;
  300. }
  301. return !a.Equals(b);
  302. }
  303. static public implicit operator JsValue(char value)
  304. {
  305. return JsString.Create(value);
  306. }
  307. static public implicit operator JsValue(int value)
  308. {
  309. return JsNumber.Create(value);
  310. }
  311. static public implicit operator JsValue(uint value)
  312. {
  313. return JsNumber.Create(value);
  314. }
  315. static public implicit operator JsValue(double value)
  316. {
  317. return JsNumber.Create(value);
  318. }
  319. public static implicit operator JsValue(bool value)
  320. {
  321. return value ? JsBoolean.True : JsBoolean.False;
  322. }
  323. public static implicit operator JsValue(string value)
  324. {
  325. return JsString.Create(value);
  326. }
  327. public override bool Equals(object obj)
  328. {
  329. if (ReferenceEquals(null, obj))
  330. {
  331. return false;
  332. }
  333. if (ReferenceEquals(this, obj))
  334. {
  335. return true;
  336. }
  337. return obj is JsValue value && Equals(value);
  338. }
  339. public abstract bool Equals(JsValue other);
  340. public override int GetHashCode()
  341. {
  342. return Type.GetHashCode();
  343. }
  344. internal class JsValueDebugView
  345. {
  346. public string Value;
  347. public JsValueDebugView(JsValue value)
  348. {
  349. switch (value.Type)
  350. {
  351. case Types.None:
  352. Value = "None";
  353. break;
  354. case Types.Undefined:
  355. Value = "undefined";
  356. break;
  357. case Types.Null:
  358. Value = "null";
  359. break;
  360. case Types.Boolean:
  361. Value = value.AsBoolean() + " (bool)";
  362. break;
  363. case Types.String:
  364. Value = value.AsString() + " (string)";
  365. break;
  366. case Types.Number:
  367. Value = value.AsNumber() + " (number)";
  368. break;
  369. case Types.Object:
  370. Value = value.AsObject().GetType().Name;
  371. break;
  372. case Types.Symbol:
  373. Value = value.AsSymbol() + " (symbol)";
  374. break;
  375. default:
  376. Value = "Unknown";
  377. break;
  378. }
  379. }
  380. }
  381. }
  382. }