PropertyDescriptor.cs 16 KB

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