PropertyDescriptor.cs 15 KB

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