PropertyDescriptor.cs 16 KB

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