ObjectInstance.cs 16 KB

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