ObjectInstance.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  1. using System;
  2. using System.Collections.Generic;
  3. using Jint.Native.String;
  4. using Jint.Runtime;
  5. using Jint.Runtime.Descriptors;
  6. namespace Jint.Native.Object
  7. {
  8. public class ObjectInstance
  9. {
  10. public ObjectInstance(Engine engine)
  11. {
  12. Engine = engine;
  13. Properties = new Dictionary<string, PropertyDescriptor>();
  14. }
  15. public Engine Engine { get; set; }
  16. public IDictionary<string, PropertyDescriptor> Properties { get; private set; }
  17. /// <summary>
  18. /// The prototype of this object.
  19. /// </summary>
  20. public ObjectInstance Prototype { get; set; }
  21. /// <summary>
  22. /// If true, own properties may be added to the
  23. /// object.
  24. /// </summary>
  25. public bool Extensible { get; set; }
  26. /// <summary>
  27. /// A String value indicating a specification defined
  28. /// classification of objects.
  29. /// </summary>
  30. public virtual string Class
  31. {
  32. get { return "Object"; }
  33. }
  34. /// <summary>
  35. /// Returns the value of the named property.
  36. /// http://www.ecma-international.org/ecma-262/5.1/#sec-8.12.3
  37. /// </summary>
  38. /// <param name="propertyName"></param>
  39. /// <returns></returns>
  40. public virtual object Get(string propertyName)
  41. {
  42. var desc = GetProperty(propertyName);
  43. if (desc == PropertyDescriptor.Undefined)
  44. {
  45. return Undefined.Instance;
  46. }
  47. if (desc.IsDataDescriptor())
  48. {
  49. return desc.As<DataDescriptor>().Value;
  50. }
  51. var getter = desc.As<AccessorDescriptor>().Get;
  52. if (getter == null)
  53. {
  54. return Undefined.Instance;
  55. }
  56. return getter.Call(this, Arguments.Empty);
  57. }
  58. public void Set(string name, object value)
  59. {
  60. if (!HasProperty(name))
  61. {
  62. DefineOwnProperty(name, new DataDescriptor(value) { Configurable = true, Enumerable = true, Writable = true }, false);
  63. }
  64. else
  65. {
  66. Put(name, value, false);
  67. }
  68. }
  69. /// <summary>
  70. /// Returns the Property Descriptor of the named
  71. /// own property of this object, or undefined if
  72. /// absent.
  73. /// http://www.ecma-international.org/ecma-262/5.1/#sec-8.12.1
  74. /// </summary>
  75. /// <param name="propertyName"></param>
  76. /// <returns></returns>
  77. public virtual PropertyDescriptor GetOwnProperty(string propertyName)
  78. {
  79. PropertyDescriptor x;
  80. if (Properties.TryGetValue(propertyName, out x))
  81. {
  82. /* Spec implementation
  83. PropertyDescriptor d;
  84. if (x.IsDataDescriptor())
  85. {
  86. d = new DataDescriptor(x.As<DataDescriptor>());
  87. }
  88. else
  89. {
  90. d = new AccessorDescriptor(x.As<AccessorDescriptor>());
  91. }
  92. return d;
  93. */
  94. // optimmized implementation
  95. return x;
  96. }
  97. return PropertyDescriptor.Undefined;
  98. }
  99. /// <summary>
  100. /// http://www.ecma-international.org/ecma-262/5.1/#sec-8.12.2
  101. /// </summary>
  102. /// <param name="propertyName"></param>
  103. /// <returns></returns>
  104. public PropertyDescriptor GetProperty(string propertyName)
  105. {
  106. var prop = GetOwnProperty(propertyName);
  107. if (prop != PropertyDescriptor.Undefined)
  108. {
  109. return prop;
  110. }
  111. if(Prototype == null)
  112. {
  113. return PropertyDescriptor.Undefined;
  114. }
  115. return Prototype.GetProperty(propertyName);
  116. }
  117. /// <summary>
  118. /// Sets the specified named property to the value
  119. /// of the second parameter. The flag controls
  120. /// failure handling.
  121. /// </summary>
  122. /// <param name="propertyName"></param>
  123. /// <param name="value"></param>
  124. /// <param name="throwOnError"></param>
  125. public void Put(string propertyName, object value, bool throwOnError)
  126. {
  127. if (!CanPut(propertyName))
  128. {
  129. if (throwOnError)
  130. {
  131. throw new JavaScriptException(Engine.TypeError);
  132. }
  133. return;
  134. }
  135. var ownDesc = GetOwnProperty(propertyName);
  136. if (ownDesc.IsDataDescriptor())
  137. {
  138. var valueDesc = new DataDescriptor(value);
  139. DefineOwnProperty(propertyName, valueDesc, throwOnError);
  140. return;
  141. }
  142. // property is an accessor or inherited
  143. var desc = GetProperty(propertyName);
  144. if (desc.IsAccessorDescriptor())
  145. {
  146. var setter = desc.As<AccessorDescriptor>().Set;
  147. setter.Call(this, new [] {value});
  148. }
  149. else
  150. {
  151. var newDesc = new DataDescriptor(value) {Writable = true, Enumerable = true, Configurable = true};
  152. DefineOwnProperty(propertyName, newDesc, throwOnError);
  153. }
  154. }
  155. /// <summary>
  156. /// Returns a Boolean value indicating whether a
  157. /// [[Put]] operation with PropertyName can be
  158. /// performed.
  159. /// http://www.ecma-international.org/ecma-262/5.1/#sec-8.12.4
  160. /// </summary>
  161. /// <param name="propertyName"></param>
  162. /// <returns></returns>
  163. public bool CanPut(string propertyName)
  164. {
  165. var desc = GetOwnProperty(propertyName);
  166. if (desc != PropertyDescriptor.Undefined)
  167. {
  168. if (desc.IsAccessorDescriptor())
  169. {
  170. if (desc.As<AccessorDescriptor>().Set == null)
  171. {
  172. return false;
  173. }
  174. return true;
  175. }
  176. return desc.As<DataDescriptor>().WritableIsSet;
  177. }
  178. if (Prototype == null)
  179. {
  180. return Extensible;
  181. }
  182. var inherited = Prototype.GetProperty(propertyName);
  183. if (inherited == PropertyDescriptor.Undefined)
  184. {
  185. return Extensible;
  186. }
  187. if (inherited.IsAccessorDescriptor())
  188. {
  189. if (inherited.As<AccessorDescriptor>().Set == null)
  190. {
  191. return false;
  192. }
  193. return true;
  194. }
  195. if (!Extensible)
  196. {
  197. return false;
  198. }
  199. else
  200. {
  201. return inherited.As<DataDescriptor>().WritableIsSet;
  202. }
  203. }
  204. /// <summary>
  205. /// Returns a Boolean value indicating whether the
  206. /// object already has a property with the given
  207. /// name.
  208. /// </summary>
  209. /// <param name="propertyName"></param>
  210. /// <returns></returns>
  211. public bool HasProperty(string propertyName)
  212. {
  213. return GetProperty(propertyName) != PropertyDescriptor.Undefined;
  214. }
  215. /// <summary>
  216. /// Removes the specified named own property
  217. /// from the object. The flag controls failure
  218. /// handling.
  219. /// </summary>
  220. /// <param name="propertyName"></param>
  221. /// <param name="throwOnError"></param>
  222. /// <returns></returns>
  223. public virtual bool Delete(string propertyName, bool throwOnError)
  224. {
  225. var desc = GetOwnProperty(propertyName);
  226. if (desc == PropertyDescriptor.Undefined)
  227. {
  228. return true;
  229. }
  230. if (desc.ConfigurableIsSetToTrue)
  231. {
  232. Properties.Remove(propertyName);
  233. return true;
  234. }
  235. else
  236. {
  237. if (throwOnError)
  238. {
  239. throw new JavaScriptException(Engine.TypeError);
  240. }
  241. return false;
  242. }
  243. }
  244. /// <summary>
  245. /// Hint is a String. Returns a default value for the
  246. /// object.
  247. /// </summary>
  248. /// <param name="hint"></param>
  249. /// <returns></returns>
  250. public object DefaultValue(Types hint)
  251. {
  252. if ((hint == Types.String) || (hint == Types.None && this is StringInstance))
  253. {
  254. var toString = Get("toString");
  255. var callable = toString as ICallable;
  256. if (callable != null)
  257. {
  258. var str = callable.Call(this, Arguments.Empty);
  259. if (TypeConverter.IsPrimitiveValue(str))
  260. {
  261. return str;
  262. }
  263. }
  264. var valueOf = Get("valueOf");
  265. callable = valueOf as ICallable;
  266. if (callable != null)
  267. {
  268. var val = callable.Call(this, Arguments.Empty);
  269. if (TypeConverter.IsPrimitiveValue(val))
  270. {
  271. return val;
  272. }
  273. }
  274. throw new JavaScriptException(Engine.TypeError);
  275. }
  276. if ((hint == Types.Number) || (hint == Types.None))
  277. {
  278. var valueOf = Get("valueOf");
  279. var callable = valueOf as ICallable;
  280. if (callable != null)
  281. {
  282. var val = callable.Call(this, Arguments.Empty);
  283. if (TypeConverter.IsPrimitiveValue(val))
  284. {
  285. return val;
  286. }
  287. }
  288. var toString = Get("toString");
  289. callable = toString as ICallable;
  290. if (callable != null)
  291. {
  292. var str = callable.Call(this, Arguments.Empty);
  293. if (TypeConverter.IsPrimitiveValue(str))
  294. {
  295. return str;
  296. }
  297. }
  298. throw new JavaScriptException(Engine.TypeError);
  299. }
  300. return ToString();
  301. }
  302. /// <summary>
  303. /// Creates or alters the named own property to
  304. /// have the state described by a Property
  305. /// Descriptor. The flag controls failure handling.
  306. /// </summary>
  307. /// <param name="propertyName"></param>
  308. /// <param name="desc"></param>
  309. /// <param name="throwOnError"></param>
  310. /// <returns></returns>
  311. public virtual bool DefineOwnProperty(string propertyName, PropertyDescriptor desc, bool throwOnError)
  312. {
  313. var current = GetOwnProperty(propertyName);
  314. if (current == PropertyDescriptor.Undefined)
  315. {
  316. if (!Extensible)
  317. {
  318. if (throwOnError)
  319. {
  320. throw new JavaScriptException(Engine.TypeError);
  321. }
  322. return false;
  323. }
  324. else
  325. {
  326. if (desc.IsGenericDescriptor() || desc.IsDataDescriptor())
  327. {
  328. Properties[propertyName] = new DataDescriptor(desc.As<DataDescriptor>());
  329. }
  330. else
  331. {
  332. Properties[propertyName] = new AccessorDescriptor(desc.As<AccessorDescriptor>());
  333. }
  334. }
  335. return true;
  336. }
  337. // todo: if desc and current are the same, return true
  338. if (current.ConfigurableIsSetToFalse)
  339. {
  340. if (desc.ConfigurableIsSetToTrue)
  341. {
  342. if (throwOnError)
  343. {
  344. throw new JavaScriptException(Engine.TypeError);
  345. }
  346. return false;
  347. }
  348. if (desc.Enumerable.HasValue && desc.Enumerable.Value != current.Enumerable.Value)
  349. {
  350. if (throwOnError)
  351. {
  352. throw new JavaScriptException(Engine.TypeError);
  353. }
  354. return false;
  355. }
  356. }
  357. if (desc.IsGenericDescriptor())
  358. {
  359. // ????
  360. }
  361. if (current.IsDataDescriptor() != desc.IsDataDescriptor())
  362. {
  363. if (!current.ConfigurableIsSetToTrue)
  364. {
  365. if (throwOnError)
  366. {
  367. throw new JavaScriptException(Engine.TypeError);
  368. }
  369. return false;
  370. }
  371. if (current.IsDataDescriptor())
  372. {
  373. // todo: convert to accessor
  374. }
  375. }
  376. else if (current.IsDataDescriptor() && desc.IsDataDescriptor())
  377. {
  378. var cd = current.As<DataDescriptor>();
  379. var dd = current.As<DataDescriptor>();
  380. if (!current.ConfigurableIsSetToTrue)
  381. {
  382. if (!cd.WritableIsSet && dd.WritableIsSet)
  383. {
  384. if (throwOnError)
  385. {
  386. throw new JavaScriptException(Engine.TypeError);
  387. }
  388. return false;
  389. }
  390. }
  391. if (!dd.Writable.HasValue && cd.Writable.HasValue)
  392. {
  393. dd.Enumerable = cd.Enumerable;
  394. }
  395. }
  396. else if (current.IsAccessorDescriptor() && desc.IsAccessorDescriptor())
  397. {
  398. var ca = current.As<AccessorDescriptor>();
  399. var da = desc.As<AccessorDescriptor>();
  400. if (!current.ConfigurableIsSetToTrue)
  401. {
  402. if ( (da.Set != null && da.Set != ca.Set)
  403. || (da.Get != null && da.Get != ca.Get))
  404. {
  405. if (throwOnError)
  406. {
  407. throw new JavaScriptException(Engine.TypeError);
  408. }
  409. return false;
  410. }
  411. }
  412. }
  413. Properties[propertyName] = desc;
  414. if (!desc.Configurable.HasValue && current.Configurable.HasValue)
  415. {
  416. desc.Configurable = current.Configurable;
  417. }
  418. if (!desc.Enumerable.HasValue && current.Enumerable.HasValue)
  419. {
  420. desc.Enumerable = current.Enumerable;
  421. }
  422. return true;
  423. }
  424. /// <summary>
  425. /// Optimized version of [[Put]] when the property is known to be undeclared already
  426. /// </summary>
  427. /// <param name="name"></param>
  428. /// <param name="value"></param>
  429. /// <param name="writable"></param>
  430. /// <param name="configurable"></param>
  431. /// <param name="enumerable"></param>
  432. public void FastAddProperty(string name, object value, bool writable, bool enumerable, bool configurable)
  433. {
  434. Properties.Add(name, new DataDescriptor(value) { Writable = writable, Enumerable = enumerable, Configurable = configurable });
  435. }
  436. /// <summary>
  437. /// Optimized version of [[Put]] when the property is known to be already declared
  438. /// </summary>
  439. /// <param name="name"></param>
  440. /// <param name="value"></param>
  441. public void FastSetProperty(string name, PropertyDescriptor value)
  442. {
  443. Properties[name] = value;
  444. }
  445. public override string ToString()
  446. {
  447. return TypeConverter.ToString(this);
  448. }
  449. }
  450. }