PropertyDescriptor.cs 13 KB

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