PropertyDescriptor.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. using System.Diagnostics;
  2. using System.Runtime.CompilerServices;
  3. using Jint.Collections;
  4. using Jint.Native;
  5. using Jint.Native.Object;
  6. namespace Jint.Runtime.Descriptors;
  7. [DebuggerDisplay("Value: {Value}, Flags: {Flags}")]
  8. public class PropertyDescriptor
  9. {
  10. public static readonly PropertyDescriptor Undefined = new UndefinedPropertyDescriptor();
  11. internal PropertyFlag _flags;
  12. internal JsValue? _value;
  13. public PropertyDescriptor() : this(PropertyFlag.None)
  14. {
  15. }
  16. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  17. protected PropertyDescriptor(PropertyFlag flags)
  18. {
  19. _flags = flags & ~PropertyFlag.NonData;
  20. }
  21. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  22. protected internal PropertyDescriptor(JsValue? value, PropertyFlag flags) : this(flags)
  23. {
  24. if ((_flags & PropertyFlag.CustomJsValue) != PropertyFlag.None)
  25. {
  26. #pragma warning disable MA0056
  27. CustomValue = value;
  28. #pragma warning restore MA0056
  29. }
  30. _value = value;
  31. }
  32. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  33. public PropertyDescriptor(JsValue? value, bool? writable, bool? enumerable, bool? configurable)
  34. {
  35. if ((_flags & PropertyFlag.CustomJsValue) != PropertyFlag.None)
  36. {
  37. #pragma warning disable MA0056
  38. CustomValue = value;
  39. #pragma warning restore MA0056
  40. }
  41. _value = value;
  42. if (writable != null)
  43. {
  44. Writable = writable.Value;
  45. WritableSet = true;
  46. }
  47. if (enumerable != null)
  48. {
  49. Enumerable = enumerable.Value;
  50. EnumerableSet = true;
  51. }
  52. if (configurable != null)
  53. {
  54. Configurable = configurable.Value;
  55. ConfigurableSet = true;
  56. }
  57. }
  58. public PropertyDescriptor(PropertyDescriptor descriptor)
  59. {
  60. Value = descriptor.Value;
  61. Enumerable = descriptor.Enumerable;
  62. EnumerableSet = descriptor.EnumerableSet;
  63. Configurable = descriptor.Configurable;
  64. ConfigurableSet = descriptor.ConfigurableSet;
  65. Writable = descriptor.Writable;
  66. WritableSet = descriptor.WritableSet;
  67. }
  68. public virtual JsValue? Get => null;
  69. public virtual JsValue? Set => null;
  70. public bool Enumerable
  71. {
  72. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  73. get => (_flags & PropertyFlag.Enumerable) != PropertyFlag.None;
  74. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  75. set
  76. {
  77. _flags |= PropertyFlag.EnumerableSet;
  78. if (value)
  79. {
  80. _flags |= PropertyFlag.Enumerable;
  81. }
  82. else
  83. {
  84. _flags &= ~(PropertyFlag.Enumerable);
  85. }
  86. }
  87. }
  88. public bool EnumerableSet
  89. {
  90. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  91. get => (_flags & (PropertyFlag.EnumerableSet | PropertyFlag.Enumerable)) != PropertyFlag.None;
  92. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  93. private set
  94. {
  95. if (value)
  96. {
  97. _flags |= PropertyFlag.EnumerableSet;
  98. }
  99. else
  100. {
  101. _flags &= ~(PropertyFlag.EnumerableSet);
  102. }
  103. }
  104. }
  105. public bool Writable
  106. {
  107. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  108. get => (_flags & PropertyFlag.Writable) != PropertyFlag.None;
  109. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  110. set
  111. {
  112. _flags |= PropertyFlag.WritableSet;
  113. if (value)
  114. {
  115. _flags |= PropertyFlag.Writable;
  116. }
  117. else
  118. {
  119. _flags &= ~(PropertyFlag.Writable);
  120. }
  121. }
  122. }
  123. public bool WritableSet
  124. {
  125. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  126. get => (_flags & (PropertyFlag.WritableSet | PropertyFlag.Writable)) != PropertyFlag.None;
  127. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  128. private set
  129. {
  130. if (value)
  131. {
  132. _flags |= PropertyFlag.WritableSet;
  133. }
  134. else
  135. {
  136. _flags &= ~(PropertyFlag.WritableSet);
  137. }
  138. }
  139. }
  140. public bool Configurable
  141. {
  142. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  143. get => (_flags & PropertyFlag.Configurable) != PropertyFlag.None;
  144. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  145. set
  146. {
  147. _flags |= PropertyFlag.ConfigurableSet;
  148. if (value)
  149. {
  150. _flags |= PropertyFlag.Configurable;
  151. }
  152. else
  153. {
  154. _flags &= ~(PropertyFlag.Configurable);
  155. }
  156. }
  157. }
  158. public bool ConfigurableSet
  159. {
  160. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  161. get => (_flags & (PropertyFlag.ConfigurableSet | PropertyFlag.Configurable)) != PropertyFlag.None;
  162. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  163. private set
  164. {
  165. if (value)
  166. {
  167. _flags |= PropertyFlag.ConfigurableSet;
  168. }
  169. else
  170. {
  171. _flags &= ~(PropertyFlag.ConfigurableSet);
  172. }
  173. }
  174. }
  175. public JsValue Value
  176. {
  177. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  178. get
  179. {
  180. if ((_flags & PropertyFlag.CustomJsValue) != PropertyFlag.None)
  181. {
  182. return CustomValue!;
  183. }
  184. return _value!;
  185. }
  186. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  187. set
  188. {
  189. if ((_flags & PropertyFlag.CustomJsValue) != PropertyFlag.None)
  190. {
  191. CustomValue = value;
  192. }
  193. _value = value;
  194. }
  195. }
  196. protected internal virtual JsValue? CustomValue
  197. {
  198. get => null;
  199. set => ExceptionHelper.ThrowNotImplementedException();
  200. }
  201. internal PropertyFlag Flags
  202. {
  203. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  204. get => _flags;
  205. }
  206. /// <summary>
  207. /// https://tc39.es/ecma262/#sec-topropertydescriptor
  208. /// </summary>
  209. public static PropertyDescriptor ToPropertyDescriptor(Realm realm, JsValue o)
  210. {
  211. if (o is not ObjectInstance obj)
  212. {
  213. ExceptionHelper.ThrowTypeError(realm);
  214. return null;
  215. }
  216. bool? enumerable = null;
  217. var hasEnumerable = obj.HasProperty(CommonProperties.Enumerable);
  218. if (hasEnumerable)
  219. {
  220. enumerable = TypeConverter.ToBoolean(obj.Get(CommonProperties.Enumerable));
  221. }
  222. bool? configurable = null;
  223. var hasConfigurable = obj.HasProperty(CommonProperties.Configurable);
  224. if (hasConfigurable)
  225. {
  226. configurable = TypeConverter.ToBoolean(obj.Get(CommonProperties.Configurable));
  227. }
  228. JsValue? value = null;
  229. var hasValue = obj.HasProperty(CommonProperties.Value);
  230. if (hasValue)
  231. {
  232. value = obj.Get(CommonProperties.Value);
  233. }
  234. bool? writable = null;
  235. var hasWritable = obj.HasProperty(CommonProperties.Writable);
  236. if (hasWritable)
  237. {
  238. writable = TypeConverter.ToBoolean(obj.Get(CommonProperties.Writable));
  239. }
  240. JsValue? get = null;
  241. var hasGet = obj.HasProperty(CommonProperties.Get);
  242. if (hasGet)
  243. {
  244. get = obj.Get(CommonProperties.Get);
  245. }
  246. JsValue? set = null;
  247. var hasSet = obj.HasProperty(CommonProperties.Set);
  248. if (hasSet)
  249. {
  250. set = obj.Get(CommonProperties.Set);
  251. }
  252. if ((hasValue || hasWritable) && (hasGet || hasSet))
  253. {
  254. ExceptionHelper.ThrowTypeError(realm, "Invalid property descriptor. Cannot both specify accessors and a value or writable attribute");
  255. }
  256. var desc = hasGet || hasSet
  257. ? new GetSetPropertyDescriptor(null, null, PropertyFlag.None)
  258. : new PropertyDescriptor(PropertyFlag.None);
  259. if (hasEnumerable)
  260. {
  261. desc.Enumerable = enumerable!.Value;
  262. desc.EnumerableSet = true;
  263. }
  264. if (hasConfigurable)
  265. {
  266. desc.Configurable = configurable!.Value;
  267. desc.ConfigurableSet = true;
  268. }
  269. if (hasValue)
  270. {
  271. desc.Value = value!;
  272. }
  273. if (hasWritable)
  274. {
  275. desc.Writable = TypeConverter.ToBoolean(writable!.Value);
  276. desc.WritableSet = true;
  277. }
  278. if (hasGet)
  279. {
  280. if (!get!.IsUndefined() && get!.TryCast<ICallable>() == null)
  281. {
  282. ExceptionHelper.ThrowTypeError(realm);
  283. }
  284. ((GetSetPropertyDescriptor) desc).SetGet(get!);
  285. }
  286. if (hasSet)
  287. {
  288. if (!set!.IsUndefined() && set!.TryCast<ICallable>() is null)
  289. {
  290. ExceptionHelper.ThrowTypeError(realm);
  291. }
  292. ((GetSetPropertyDescriptor) desc).SetSet(set!);
  293. }
  294. if ((hasSet || hasGet) && (hasValue || hasWritable))
  295. {
  296. ExceptionHelper.ThrowTypeError(realm);
  297. }
  298. return desc;
  299. }
  300. /// <summary>
  301. /// https://tc39.es/ecma262/#sec-frompropertydescriptor
  302. /// </summary>
  303. public static JsValue FromPropertyDescriptor(Engine engine, PropertyDescriptor desc, bool strictUndefined = false)
  304. {
  305. if (ReferenceEquals(desc, Undefined))
  306. {
  307. return JsValue.Undefined;
  308. }
  309. var obj = engine.Realm.Intrinsics.Object.Construct(Arguments.Empty);
  310. var properties = new PropertyDictionary(4, checkExistingKeys: false);
  311. // TODO should not check for strictUndefined, but needs a bigger cleanup
  312. // we should have possibility to leave out the properties in property descriptors as newer tests
  313. // also assert properties to be undefined
  314. if (desc.IsDataDescriptor())
  315. {
  316. properties["value"] = new PropertyDescriptor(desc.Value ?? JsValue.Undefined, PropertyFlag.ConfigurableEnumerableWritable);
  317. if (desc._flags != PropertyFlag.None || desc.WritableSet)
  318. {
  319. properties["writable"] = new PropertyDescriptor(desc.Writable, PropertyFlag.ConfigurableEnumerableWritable);
  320. }
  321. }
  322. else
  323. {
  324. properties["get"] = new PropertyDescriptor(desc.Get ?? JsValue.Undefined, PropertyFlag.ConfigurableEnumerableWritable);
  325. properties["set"] = new PropertyDescriptor(desc.Set ?? JsValue.Undefined, PropertyFlag.ConfigurableEnumerableWritable);
  326. }
  327. if (!strictUndefined || desc.EnumerableSet)
  328. {
  329. properties["enumerable"] = new PropertyDescriptor(desc.Enumerable, PropertyFlag.ConfigurableEnumerableWritable);
  330. }
  331. if (!strictUndefined || desc.ConfigurableSet)
  332. {
  333. properties["configurable"] = new PropertyDescriptor(desc.Configurable, PropertyFlag.ConfigurableEnumerableWritable);
  334. }
  335. obj.SetProperties(properties);
  336. return obj;
  337. }
  338. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  339. public bool IsAccessorDescriptor()
  340. {
  341. return Get is not null || Set is not null;
  342. }
  343. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  344. public bool IsDataDescriptor()
  345. {
  346. if ((_flags & PropertyFlag.NonData) != PropertyFlag.None)
  347. {
  348. return false;
  349. }
  350. return (_flags & (PropertyFlag.WritableSet | PropertyFlag.Writable)) != PropertyFlag.None
  351. || (_flags & PropertyFlag.CustomJsValue) != PropertyFlag.None && CustomValue is not null
  352. || _value is not null;
  353. }
  354. /// <summary>
  355. /// http://www.ecma-international.org/ecma-262/5.1/#sec-8.10.3
  356. /// </summary>
  357. /// <returns></returns>
  358. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  359. public bool IsGenericDescriptor()
  360. {
  361. return !IsDataDescriptor() && !IsAccessorDescriptor();
  362. }
  363. private sealed class UndefinedPropertyDescriptor : PropertyDescriptor
  364. {
  365. public UndefinedPropertyDescriptor() : base(PropertyFlag.None | PropertyFlag.CustomJsValue)
  366. {
  367. }
  368. protected internal override JsValue? CustomValue
  369. {
  370. set => ExceptionHelper.ThrowInvalidOperationException("making changes to undefined property's descriptor is not allowed");
  371. }
  372. }
  373. internal sealed class AllForbiddenDescriptor : PropertyDescriptor
  374. {
  375. private static readonly PropertyDescriptor[] _cache;
  376. public static readonly AllForbiddenDescriptor NumberZero = new AllForbiddenDescriptor(JsNumber.Create(0));
  377. public static readonly AllForbiddenDescriptor NumberOne = new AllForbiddenDescriptor(JsNumber.Create(1));
  378. public static readonly AllForbiddenDescriptor BooleanFalse = new AllForbiddenDescriptor(JsBoolean.False);
  379. public static readonly AllForbiddenDescriptor BooleanTrue = new AllForbiddenDescriptor(JsBoolean.True);
  380. static AllForbiddenDescriptor()
  381. {
  382. _cache = new PropertyDescriptor[10];
  383. for (int i = 0; i < _cache.Length; ++i)
  384. {
  385. _cache[i] = new AllForbiddenDescriptor(JsNumber.Create(i));
  386. }
  387. }
  388. private AllForbiddenDescriptor(JsValue value)
  389. : base(PropertyFlag.AllForbidden)
  390. {
  391. _value = value;
  392. }
  393. public static PropertyDescriptor ForNumber(int number)
  394. {
  395. var temp = _cache;
  396. return (uint) number < temp.Length
  397. ? temp[number]
  398. : new PropertyDescriptor(number, PropertyFlag.AllForbidden);
  399. }
  400. }
  401. }