PropertyDescriptorTests.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. using Jint.Native;
  2. using Jint.Native.Global;
  3. using Jint.Runtime.Descriptors;
  4. using Jint.Runtime.Descriptors.Specialized;
  5. using Jint.Runtime.Interop;
  6. using Jint.Tests.TestClasses;
  7. namespace Jint.Tests.Runtime;
  8. public class PropertyDescriptorTests
  9. {
  10. public class TestClass
  11. {
  12. public static readonly TestClass Instance = new TestClass();
  13. public string Method() => "Method";
  14. public class NestedType { }
  15. public readonly int fieldReadOnly = 8;
  16. public int field = 42;
  17. public string PropertyReadOnly => "PropertyReadOnly";
  18. public string PropertyWriteOnly { set { } }
  19. public string PropertyReadWrite { get; set; } = "PropertyReadWrite";
  20. public IndexedPropertyReadOnly<int, int> IndexerReadOnly { get; }
  21. = new((idx) => 42);
  22. public IndexedPropertyWriteOnly<int, int> IndexerWriteOnly { get; }
  23. = new((idx, v) => { });
  24. public IndexedProperty<int, int> IndexerReadWrite { get; }
  25. = new((idx) => 42, (idx, v) => { });
  26. }
  27. private readonly Engine _engine;
  28. private readonly bool checkType = true;
  29. public PropertyDescriptorTests()
  30. {
  31. _engine = new Engine(cfg => cfg.AllowClr(
  32. typeof(TestClass).Assembly,
  33. typeof(Console).Assembly,
  34. typeof(File).Assembly))
  35. .SetValue("log", new Action<object>(Console.WriteLine))
  36. .SetValue("assert", new Action<bool>(Assert.True))
  37. .SetValue("equal", new Action<object, object>(Assert.Equal))
  38. .SetValue("testClass", TestClass.Instance)
  39. ;
  40. }
  41. [Fact]
  42. public void PropertyDescriptorReadOnly()
  43. {
  44. var pd = _engine.Evaluate("""
  45. Object.defineProperty({}, 'value', {
  46. value: 42,
  47. writable: false
  48. })
  49. """).AsObject().GetOwnProperty("value");
  50. Assert.Equal(false, pd.IsAccessorDescriptor());
  51. Assert.Equal(true, pd.IsDataDescriptor());
  52. Assert.Equal(false, pd.Writable);
  53. Assert.Null(pd.Get);
  54. Assert.Null(pd.Set);
  55. }
  56. [Fact]
  57. public void PropertyDescriptorReadWrite()
  58. {
  59. var pd = _engine.Evaluate("""
  60. Object.defineProperty({}, 'value', {
  61. value: 42,
  62. writable: true
  63. })
  64. """).AsObject().GetOwnProperty("value");
  65. Assert.Equal(false, pd.IsAccessorDescriptor());
  66. Assert.Equal(true, pd.IsDataDescriptor());
  67. Assert.Equal(true, pd.Writable);
  68. Assert.Null(pd.Get);
  69. Assert.Null(pd.Set);
  70. }
  71. [Fact]
  72. public void UndefinedPropertyDescriptor()
  73. {
  74. var pd = PropertyDescriptor.Undefined;
  75. // PropertyDescriptor.UndefinedPropertyDescriptor is private
  76. //if (checkType) Assert.IsType<PropertyDescriptor.UndefinedPropertyDescriptor>(pd);
  77. Assert.Equal(false, pd.IsAccessorDescriptor());
  78. Assert.Equal(false, pd.IsDataDescriptor());
  79. }
  80. [Fact]
  81. public void AllForbiddenDescriptor()
  82. {
  83. var pd = _engine.Evaluate("Object.getPrototypeOf('s')").AsObject().GetOwnProperty("length");
  84. if (checkType) Assert.IsType<PropertyDescriptor.AllForbiddenDescriptor>(pd);
  85. Assert.Equal(false, pd.IsAccessorDescriptor());
  86. Assert.Equal(true, pd.IsDataDescriptor());
  87. }
  88. [Fact]
  89. public void LazyPropertyDescriptor()
  90. {
  91. var pd = _engine.Evaluate("globalThis").AsObject().GetOwnProperty("decodeURI");
  92. if (checkType)
  93. {
  94. Assert.IsType<LazyPropertyDescriptor<GlobalObject>>(pd);
  95. }
  96. Assert.Equal(false, pd.IsAccessorDescriptor());
  97. Assert.Equal(true, pd.IsDataDescriptor());
  98. }
  99. [Fact]
  100. public void ThrowerPropertyDescriptor()
  101. {
  102. var pd = _engine.Evaluate("Object.getPrototypeOf(function() {})").AsObject().GetOwnProperty("arguments");
  103. if (checkType) Assert.IsType<GetSetPropertyDescriptor.ThrowerPropertyDescriptor>(pd);
  104. Assert.Equal(true, pd.IsAccessorDescriptor());
  105. Assert.Equal(false, pd.IsDataDescriptor());
  106. }
  107. [Fact]
  108. public void GetSetPropertyDescriptorGetOnly()
  109. {
  110. var pd = _engine.Evaluate("""
  111. Object.defineProperty({}, 'value', {
  112. get() {}
  113. })
  114. """).AsObject().GetOwnProperty("value");
  115. if (checkType) Assert.IsType<GetSetPropertyDescriptor>(pd);
  116. Assert.Equal(true, pd.IsAccessorDescriptor());
  117. Assert.Equal(false, pd.IsDataDescriptor());
  118. Assert.NotNull(pd.Get);
  119. Assert.Null(pd.Set);
  120. }
  121. [Fact]
  122. public void GetSetPropertyDescriptorSetOnly()
  123. {
  124. var pd = _engine.Evaluate("""
  125. Object.defineProperty({}, 'value', {
  126. set() {}
  127. })
  128. """).AsObject().GetOwnProperty("value");
  129. if (checkType) Assert.IsType<GetSetPropertyDescriptor>(pd);
  130. Assert.Equal(true, pd.IsAccessorDescriptor());
  131. Assert.Equal(false, pd.IsDataDescriptor());
  132. Assert.Null(pd.Get);
  133. Assert.NotNull(pd.Set);
  134. }
  135. [Fact]
  136. public void GetSetPropertyDescriptorGetSet()
  137. {
  138. var pd = _engine.Evaluate("""
  139. Object.defineProperty({}, 'value', {
  140. get() {},
  141. set() {}
  142. })
  143. """).AsObject().GetOwnProperty("value");
  144. if (checkType) Assert.IsType<GetSetPropertyDescriptor>(pd);
  145. Assert.Equal(true, pd.IsAccessorDescriptor());
  146. Assert.Equal(false, pd.IsDataDescriptor());
  147. Assert.NotNull(pd.Get);
  148. Assert.NotNull(pd.Set);
  149. }
  150. [Fact]
  151. public void ClrAccessDescriptor()
  152. {
  153. JsValue ExtractClrAccessDescriptor(JsValue jsArugments)
  154. {
  155. var pd = ((JsArguments) jsArugments).ParameterMap.GetOwnProperty("0");
  156. return ObjectWrapper.Create(_engine, pd);
  157. }
  158. _engine.SetValue("ExtractClrAccessDescriptor", ExtractClrAccessDescriptor);
  159. var pdobj = _engine.Evaluate("""
  160. (function(a) {
  161. return ExtractClrAccessDescriptor(arguments);
  162. })(42)
  163. """);
  164. var pd = (PropertyDescriptor) ((ObjectWrapper) pdobj).Target;
  165. if (checkType) Assert.IsType<ClrAccessDescriptor>(pd);
  166. Assert.Equal(true, pd.IsAccessorDescriptor());
  167. Assert.Equal(false, pd.IsDataDescriptor());
  168. }
  169. [Fact]
  170. public void PropertyDescriptorMethod()
  171. {
  172. var pdMethod = _engine.Evaluate("Object.getOwnPropertyDescriptor(testClass, 'Method')");
  173. CheckPropertyDescriptor(pdMethod, false, false, false, true, false, false);
  174. var pd = _engine.Evaluate("testClass").AsObject().GetOwnProperty("Method");
  175. // use PropertyDescriptor to wrap method directly
  176. //if (checkType) Assert.IsType<PropertyDescriptor>(pd);
  177. Assert.Equal(false, pd.IsAccessorDescriptor());
  178. Assert.Equal(true, pd.IsDataDescriptor());
  179. }
  180. [Fact]
  181. public void PropertyDescriptorNestedType()
  182. {
  183. var pdMethod = _engine.Evaluate("Object.getOwnPropertyDescriptor(testClass, 'NestedType')");
  184. CheckPropertyDescriptor(pdMethod, false, false, false, true, false, false);
  185. var pd = _engine.Evaluate("testClass").AsObject().GetOwnProperty("NestedType");
  186. // use PropertyDescriptor to wrap nested type directly
  187. //if (checkType) Assert.IsType<PropertyDescriptor>(pd);
  188. Assert.Equal(false, pd.IsAccessorDescriptor());
  189. Assert.Equal(true, pd.IsDataDescriptor());
  190. }
  191. [Fact]
  192. public void ReflectionDescriptorFieldReadOnly()
  193. {
  194. var pdField = _engine.Evaluate("Object.getOwnPropertyDescriptor(testClass, 'fieldReadOnly')");
  195. CheckPropertyDescriptor(pdField, false, true, false, false, true, false);
  196. var pd = _engine.Evaluate("testClass").AsObject().GetOwnProperty("fieldReadOnly");
  197. if (checkType) Assert.IsType<ReflectionDescriptor>(pd);
  198. Assert.Equal(true, pd.IsAccessorDescriptor());
  199. Assert.Equal(false, pd.IsDataDescriptor());
  200. }
  201. [Fact]
  202. public void ReflectionDescriptorField()
  203. {
  204. var pdField = _engine.Evaluate("Object.getOwnPropertyDescriptor(testClass, 'field')");
  205. CheckPropertyDescriptor(pdField, false, true, true, false, true, true);
  206. var pd = _engine.Evaluate("testClass").AsObject().GetOwnProperty("field");
  207. if (checkType) Assert.IsType<ReflectionDescriptor>(pd);
  208. Assert.Equal(true, pd.IsAccessorDescriptor());
  209. Assert.Equal(false, pd.IsDataDescriptor());
  210. }
  211. [Fact]
  212. public void ReflectionDescriptorPropertyReadOnly()
  213. {
  214. var pdPropertyReadOnly = _engine.Evaluate("Object.getOwnPropertyDescriptor(testClass, 'PropertyReadOnly')");
  215. CheckPropertyDescriptor(pdPropertyReadOnly, false, true, false, false, true, false);
  216. var pd = _engine.Evaluate("testClass").AsObject().GetOwnProperty("PropertyReadOnly");
  217. if (checkType) Assert.IsType<ReflectionDescriptor>(pd);
  218. Assert.Equal(true, pd.IsAccessorDescriptor());
  219. Assert.Equal(false, pd.IsDataDescriptor());
  220. }
  221. [Fact]
  222. public void ReflectionDescriptorPropertyWriteOnly()
  223. {
  224. var pdPropertyWriteOnly = _engine.Evaluate("Object.getOwnPropertyDescriptor(testClass, 'PropertyWriteOnly')");
  225. CheckPropertyDescriptor(pdPropertyWriteOnly, false, true, true, false, false, true);
  226. var pd = _engine.Evaluate("testClass").AsObject().GetOwnProperty("PropertyWriteOnly");
  227. if (checkType) Assert.IsType<ReflectionDescriptor>(pd);
  228. Assert.Equal(true, pd.IsAccessorDescriptor());
  229. Assert.Equal(false, pd.IsDataDescriptor());
  230. }
  231. [Fact]
  232. public void ReflectionDescriptorPropertyReadWrite()
  233. {
  234. var pdPropertyReadWrite = _engine.Evaluate("Object.getOwnPropertyDescriptor(testClass, 'PropertyReadWrite')");
  235. CheckPropertyDescriptor(pdPropertyReadWrite, false, true, true, false, true, true);
  236. var pd = _engine.Evaluate("testClass").AsObject().GetOwnProperty("PropertyReadWrite");
  237. if (checkType) Assert.IsType<ReflectionDescriptor>(pd);
  238. Assert.Equal(true, pd.IsAccessorDescriptor());
  239. Assert.Equal(false, pd.IsDataDescriptor());
  240. }
  241. [Fact]
  242. public void ReflectionDescriptorIndexerReadOnly()
  243. {
  244. var pdIndexerReadOnly = _engine.Evaluate("Object.getOwnPropertyDescriptor(testClass.IndexerReadOnly, '1')");
  245. CheckPropertyDescriptor(pdIndexerReadOnly, false, true, false, false, true, false);
  246. var pd1 = _engine.Evaluate("testClass.IndexerReadOnly");
  247. var pd = pd1.AsObject().GetOwnProperty("1");
  248. if (checkType) Assert.IsType<ReflectionDescriptor>(pd);
  249. Assert.Equal(true, pd.IsAccessorDescriptor());
  250. Assert.Equal(false, pd.IsDataDescriptor());
  251. }
  252. [Fact]
  253. public void ReflectionDescriptorIndexerWriteOnly()
  254. {
  255. var pdIndexerWriteOnly = _engine.Evaluate("Object.getOwnPropertyDescriptor(testClass.IndexerWriteOnly, '1')");
  256. CheckPropertyDescriptor(pdIndexerWriteOnly, false, true, true, false, false, true);
  257. var pd = _engine.Evaluate("testClass.IndexerWriteOnly").AsObject().GetOwnProperty("1");
  258. if (checkType) Assert.IsType<ReflectionDescriptor>(pd);
  259. Assert.Equal(true, pd.IsAccessorDescriptor());
  260. Assert.Equal(false, pd.IsDataDescriptor());
  261. }
  262. [Fact]
  263. public void ReflectionDescriptorIndexerReadWrite()
  264. {
  265. var pdIndexerReadWrite = _engine.Evaluate("Object.getOwnPropertyDescriptor(testClass.IndexerReadWrite, 1)");
  266. CheckPropertyDescriptor(pdIndexerReadWrite, false, true, true, false, true, true);
  267. var pd = _engine.Evaluate("testClass.IndexerReadWrite").AsObject().GetOwnProperty("1");
  268. if (checkType) Assert.IsType<ReflectionDescriptor>(pd);
  269. Assert.Equal(true, pd.IsAccessorDescriptor());
  270. Assert.Equal(false, pd.IsDataDescriptor());
  271. }
  272. private void CheckPropertyDescriptor(
  273. JsValue jsPropertyDescriptor,
  274. bool configurable,
  275. bool enumerable,
  276. bool writable,
  277. bool hasValue,
  278. bool hasGet,
  279. bool hasSet
  280. )
  281. {
  282. var pd = jsPropertyDescriptor.AsObject();
  283. Assert.Equal(configurable, pd["configurable"].AsBoolean());
  284. Assert.Equal(enumerable, pd["enumerable"].AsBoolean());
  285. if (writable)
  286. {
  287. var writableActual = pd["writable"];
  288. if (!writableActual.IsUndefined())
  289. {
  290. Assert.True(writableActual.AsBoolean());
  291. }
  292. }
  293. Assert.Equal(hasValue, !pd["value"].IsUndefined());
  294. Assert.Equal(hasGet, !pd["get"].IsUndefined());
  295. Assert.Equal(hasSet, !pd["set"].IsUndefined());
  296. }
  297. [Fact]
  298. public void DefinePropertyFromAccesorToData()
  299. {
  300. var pd = _engine.Evaluate("""
  301. let o = {};
  302. Object.defineProperty(o, 'foo', {
  303. get() { return 1; },
  304. configurable: true
  305. });
  306. Object.defineProperty(o, 'foo', {
  307. value: 101
  308. });
  309. return Object.getOwnPropertyDescriptor(o, 'foo');
  310. """);
  311. Assert.Equal(101, pd.AsObject().Get("value").AsInteger());
  312. CheckPropertyDescriptor(pd, true, false, false, true, false, false);
  313. }
  314. }