2
0

PropertyDescriptor.cs 14 KB

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