PropertyDescriptor.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  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. var getProperty = obj.GetProperty(CommonProperties.Get);
  211. var hasGetProperty = getProperty != Undefined;
  212. var setProperty = obj.GetProperty(CommonProperties.Set);
  213. var hasSetProperty = setProperty != Undefined;
  214. if ((obj.HasProperty(CommonProperties.Value) || obj.HasProperty(CommonProperties.Writable)) &&
  215. (hasGetProperty || hasSetProperty))
  216. {
  217. ExceptionHelper.ThrowTypeError(realm);
  218. }
  219. var desc = hasGetProperty || hasSetProperty
  220. ? new GetSetPropertyDescriptor(null, null, PropertyFlag.None)
  221. : new PropertyDescriptor(PropertyFlag.None);
  222. var enumerableProperty = obj.GetProperty(CommonProperties.Enumerable);
  223. if (enumerableProperty != Undefined)
  224. {
  225. desc.Enumerable = TypeConverter.ToBoolean(obj.UnwrapJsValue(enumerableProperty));
  226. desc.EnumerableSet = true;
  227. }
  228. var configurableProperty = obj.GetProperty(CommonProperties.Configurable);
  229. if (configurableProperty != Undefined)
  230. {
  231. desc.Configurable = TypeConverter.ToBoolean(obj.UnwrapJsValue(configurableProperty));
  232. desc.ConfigurableSet = true;
  233. }
  234. var valueProperty = obj.GetProperty(CommonProperties.Value);
  235. if (valueProperty != Undefined)
  236. {
  237. desc.Value = obj.UnwrapJsValue(valueProperty);
  238. }
  239. var writableProperty = obj.GetProperty(CommonProperties.Writable);
  240. if (writableProperty != Undefined)
  241. {
  242. desc.Writable = TypeConverter.ToBoolean(obj.UnwrapJsValue(writableProperty));
  243. desc.WritableSet = true;
  244. }
  245. if (hasGetProperty)
  246. {
  247. var getter = obj.UnwrapJsValue(getProperty);
  248. if (!getter.IsUndefined() && getter.TryCast<ICallable>() == null)
  249. {
  250. ExceptionHelper.ThrowTypeError(realm);
  251. }
  252. ((GetSetPropertyDescriptor) desc).SetGet(getter);
  253. }
  254. if (hasSetProperty)
  255. {
  256. var setter = obj.UnwrapJsValue(setProperty);
  257. if (!setter.IsUndefined() && setter.TryCast<ICallable>() == null)
  258. {
  259. ExceptionHelper.ThrowTypeError(realm);
  260. }
  261. ((GetSetPropertyDescriptor) desc).SetSet(setter);
  262. }
  263. if (!ReferenceEquals(desc.Get, null))
  264. {
  265. if (!ReferenceEquals(desc.Value, null) || desc.WritableSet)
  266. {
  267. ExceptionHelper.ThrowTypeError(realm);
  268. }
  269. }
  270. return desc;
  271. }
  272. /// <summary>
  273. /// https://tc39.es/ecma262/#sec-frompropertydescriptor
  274. /// </summary>
  275. public static JsValue FromPropertyDescriptor(Engine engine, PropertyDescriptor desc)
  276. {
  277. if (ReferenceEquals(desc, Undefined))
  278. {
  279. return Native.Undefined.Instance;
  280. }
  281. var obj = engine.Realm.Intrinsics.Object.Construct(Arguments.Empty);
  282. var properties = new PropertyDictionary(4, checkExistingKeys: false);
  283. // TODO should not check for PropertyFlag.None, but needs a bigger cleanup
  284. // we should have possibility to leave out the properties in property descriptors as newer tests
  285. // also assert properties to be undefined
  286. if (desc.IsDataDescriptor())
  287. {
  288. properties["value"] = new PropertyDescriptor(desc.Value ?? Native.Undefined.Instance, PropertyFlag.ConfigurableEnumerableWritable);
  289. if (desc._flags != PropertyFlag.None || desc.WritableSet)
  290. {
  291. properties["writable"] = new PropertyDescriptor(desc.Writable, PropertyFlag.ConfigurableEnumerableWritable);
  292. }
  293. }
  294. else
  295. {
  296. properties["get"] = new PropertyDescriptor(desc.Get ?? Native.Undefined.Instance, PropertyFlag.ConfigurableEnumerableWritable);
  297. properties["set"] = new PropertyDescriptor(desc.Set ?? Native.Undefined.Instance, PropertyFlag.ConfigurableEnumerableWritable);
  298. }
  299. if (desc._flags != PropertyFlag.None || desc.EnumerableSet)
  300. {
  301. properties["enumerable"] = new PropertyDescriptor(desc.Enumerable, PropertyFlag.ConfigurableEnumerableWritable);
  302. }
  303. if (desc._flags != PropertyFlag.None || desc.ConfigurableSet)
  304. {
  305. properties["configurable"] = new PropertyDescriptor(desc.Configurable, PropertyFlag.ConfigurableEnumerableWritable);
  306. }
  307. obj.SetProperties(properties);
  308. return obj;
  309. }
  310. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  311. public bool IsAccessorDescriptor()
  312. {
  313. return !ReferenceEquals(Get, null) || !ReferenceEquals(Set, null);
  314. }
  315. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  316. public bool IsDataDescriptor()
  317. {
  318. return (_flags & (PropertyFlag.WritableSet | PropertyFlag.Writable)) != 0
  319. || (_flags & PropertyFlag.CustomJsValue) != 0 && !ReferenceEquals(CustomValue, null)
  320. || !ReferenceEquals(_value, null);
  321. }
  322. /// <summary>
  323. /// http://www.ecma-international.org/ecma-262/5.1/#sec-8.10.3
  324. /// </summary>
  325. /// <returns></returns>
  326. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  327. public bool IsGenericDescriptor()
  328. {
  329. return !IsDataDescriptor() && !IsAccessorDescriptor();
  330. }
  331. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  332. internal bool TryGetValue(ObjectInstance thisArg, out JsValue value)
  333. {
  334. value = JsValue.Undefined;
  335. // IsDataDescriptor logic inlined
  336. if ((_flags & (PropertyFlag.WritableSet | PropertyFlag.Writable)) != 0)
  337. {
  338. var val = (_flags & PropertyFlag.CustomJsValue) != 0
  339. ? CustomValue
  340. : _value;
  341. if (!ReferenceEquals(val, null))
  342. {
  343. value = val;
  344. return true;
  345. }
  346. }
  347. if (this == Undefined)
  348. {
  349. return false;
  350. }
  351. var getter = Get;
  352. if (!ReferenceEquals(getter, null) && !getter.IsUndefined())
  353. {
  354. // if getter is not undefined it must be ICallable
  355. var callable = getter.TryCast<ICallable>();
  356. value = callable.Call(thisArg, Arguments.Empty);
  357. }
  358. return true;
  359. }
  360. private sealed class UndefinedPropertyDescriptor : PropertyDescriptor
  361. {
  362. public UndefinedPropertyDescriptor() : base(PropertyFlag.None | PropertyFlag.CustomJsValue)
  363. {
  364. }
  365. protected internal override JsValue CustomValue
  366. {
  367. set => ExceptionHelper.ThrowInvalidOperationException("making changes to undefined property's descriptor is not allowed");
  368. }
  369. }
  370. internal sealed class AllForbiddenDescriptor : PropertyDescriptor
  371. {
  372. private static readonly PropertyDescriptor[] _cache;
  373. public static readonly AllForbiddenDescriptor NumberZero = new AllForbiddenDescriptor(JsNumber.Create(0));
  374. public static readonly AllForbiddenDescriptor NumberOne = new AllForbiddenDescriptor(JsNumber.Create(1));
  375. public static readonly AllForbiddenDescriptor BooleanFalse = new AllForbiddenDescriptor(JsBoolean.False);
  376. public static readonly AllForbiddenDescriptor BooleanTrue = new AllForbiddenDescriptor(JsBoolean.True);
  377. static AllForbiddenDescriptor()
  378. {
  379. _cache = new PropertyDescriptor[10];
  380. for (int i = 0; i < _cache.Length; ++i)
  381. {
  382. _cache[i] = new AllForbiddenDescriptor(JsNumber.Create(i));
  383. }
  384. }
  385. private AllForbiddenDescriptor(JsValue value)
  386. : base(PropertyFlag.AllForbidden)
  387. {
  388. _value = value;
  389. }
  390. public static PropertyDescriptor ForNumber(int number)
  391. {
  392. var temp = _cache;
  393. return (uint) number < temp.Length
  394. ? temp[number]
  395. : new PropertyDescriptor(number, PropertyFlag.AllForbidden);
  396. }
  397. }
  398. }
  399. }