ObjectInstance.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  1. using System.Collections.Generic;
  2. using Jint.Native.Date;
  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.Value.Value;
  50. }
  51. var getter = desc.Get.Value;
  52. if (getter == Undefined.Instance || getter == null)
  53. {
  54. return Undefined.Instance;
  55. }
  56. // if getter is not undefined it must be ICallable
  57. var callable = (ICallable)getter;
  58. return callable.Call(this, Arguments.Empty);
  59. }
  60. public void Set(string name, object value)
  61. {
  62. if (!HasProperty(name))
  63. {
  64. DefineOwnProperty(name, new PropertyDescriptor(value, true, true, true), false);
  65. }
  66. else
  67. {
  68. Put(name, value, false);
  69. }
  70. }
  71. /// <summary>
  72. /// Returns the Property Descriptor of the named
  73. /// own property of this object, or undefined if
  74. /// absent.
  75. /// http://www.ecma-international.org/ecma-262/5.1/#sec-8.12.1
  76. /// </summary>
  77. /// <param name="propertyName"></param>
  78. /// <returns></returns>
  79. public virtual PropertyDescriptor GetOwnProperty(string propertyName)
  80. {
  81. PropertyDescriptor x;
  82. if (Properties.TryGetValue(propertyName, out x))
  83. {
  84. /* Spec implementation
  85. PropertyDescriptor d;
  86. if (x.IsDataDescriptor())
  87. {
  88. d = new PropertyDescriptor(x.As<DataDescriptor>());
  89. }
  90. else
  91. {
  92. d = new PropertyDescriptor(x.As<AccessorDescriptor>());
  93. }
  94. return d;
  95. */
  96. // optimmized implementation
  97. return x;
  98. }
  99. return PropertyDescriptor.Undefined;
  100. }
  101. /// <summary>
  102. /// http://www.ecma-international.org/ecma-262/5.1/#sec-8.12.2
  103. /// </summary>
  104. /// <param name="propertyName"></param>
  105. /// <returns></returns>
  106. public PropertyDescriptor GetProperty(string propertyName)
  107. {
  108. var prop = GetOwnProperty(propertyName);
  109. if (prop != PropertyDescriptor.Undefined)
  110. {
  111. return prop;
  112. }
  113. if(Prototype == null)
  114. {
  115. return PropertyDescriptor.Undefined;
  116. }
  117. return Prototype.GetProperty(propertyName);
  118. }
  119. /// <summary>
  120. /// Sets the specified named property to the value
  121. /// of the second parameter. The flag controls
  122. /// failure handling.
  123. /// </summary>
  124. /// <param name="propertyName"></param>
  125. /// <param name="value"></param>
  126. /// <param name="throwOnError"></param>
  127. public void Put(string propertyName, object value, bool throwOnError)
  128. {
  129. if (!CanPut(propertyName))
  130. {
  131. if (throwOnError)
  132. {
  133. throw new JavaScriptException(Engine.TypeError);
  134. }
  135. return;
  136. }
  137. var ownDesc = GetOwnProperty(propertyName);
  138. if (ownDesc.IsDataDescriptor())
  139. {
  140. var valueDesc = new PropertyDescriptor(value, null, null, null);
  141. DefineOwnProperty(propertyName, valueDesc, throwOnError);
  142. return;
  143. }
  144. // property is an accessor or inherited
  145. var desc = GetProperty(propertyName);
  146. if (desc.IsAccessorDescriptor())
  147. {
  148. var setter = (ICallable)desc.Set.Value;
  149. setter.Call(this, new [] {value});
  150. }
  151. else
  152. {
  153. var newDesc = new PropertyDescriptor(value, true, true, true);
  154. DefineOwnProperty(propertyName, newDesc, throwOnError);
  155. }
  156. }
  157. /// <summary>
  158. /// Returns a Boolean value indicating whether a
  159. /// [[Put]] operation with PropertyName can be
  160. /// performed.
  161. /// http://www.ecma-international.org/ecma-262/5.1/#sec-8.12.4
  162. /// </summary>
  163. /// <param name="propertyName"></param>
  164. /// <returns></returns>
  165. public bool CanPut(string propertyName)
  166. {
  167. var desc = GetOwnProperty(propertyName);
  168. if (desc != PropertyDescriptor.Undefined)
  169. {
  170. if (desc.IsAccessorDescriptor())
  171. {
  172. if (desc.Set.Value == Undefined.Instance)
  173. {
  174. return false;
  175. }
  176. return true;
  177. }
  178. return desc.Writable.Value;
  179. }
  180. if (Prototype == null)
  181. {
  182. return Extensible;
  183. }
  184. var inherited = Prototype.GetProperty(propertyName);
  185. if (inherited == PropertyDescriptor.Undefined)
  186. {
  187. return Extensible;
  188. }
  189. if (inherited.IsAccessorDescriptor())
  190. {
  191. if (inherited.Set.Value == Undefined.Instance)
  192. {
  193. return false;
  194. }
  195. return true;
  196. }
  197. if (!Extensible)
  198. {
  199. return false;
  200. }
  201. else
  202. {
  203. return inherited.Writable.Value;
  204. }
  205. }
  206. /// <summary>
  207. /// Returns a Boolean value indicating whether the
  208. /// object already has a property with the given
  209. /// name.
  210. /// </summary>
  211. /// <param name="propertyName"></param>
  212. /// <returns></returns>
  213. public bool HasProperty(string propertyName)
  214. {
  215. return GetProperty(propertyName) != PropertyDescriptor.Undefined;
  216. }
  217. /// <summary>
  218. /// Removes the specified named own property
  219. /// from the object. The flag controls failure
  220. /// handling.
  221. /// </summary>
  222. /// <param name="propertyName"></param>
  223. /// <param name="throwOnError"></param>
  224. /// <returns></returns>
  225. public virtual bool Delete(string propertyName, bool throwOnError)
  226. {
  227. var desc = GetOwnProperty(propertyName);
  228. if (desc == PropertyDescriptor.Undefined)
  229. {
  230. return true;
  231. }
  232. if (desc.Configurable.Value)
  233. {
  234. Properties.Remove(propertyName);
  235. return true;
  236. }
  237. else
  238. {
  239. if (throwOnError)
  240. {
  241. throw new JavaScriptException(Engine.TypeError);
  242. }
  243. return false;
  244. }
  245. }
  246. /// <summary>
  247. /// Hint is a String. Returns a default value for the
  248. /// object.
  249. /// </summary>
  250. /// <param name="hint"></param>
  251. /// <returns></returns>
  252. public object DefaultValue(Types hint)
  253. {
  254. if ((hint == Types.String) || (hint == Types.None && this is StringInstance) || this is DateInstance)
  255. {
  256. var toString = Get("toString");
  257. var callable = toString as ICallable;
  258. if (callable != null)
  259. {
  260. var str = callable.Call(this, Arguments.Empty);
  261. if (TypeConverter.IsPrimitiveValue(str))
  262. {
  263. return str;
  264. }
  265. }
  266. var valueOf = Get("valueOf");
  267. callable = valueOf as ICallable;
  268. if (callable != null)
  269. {
  270. var val = callable.Call(this, Arguments.Empty);
  271. if (TypeConverter.IsPrimitiveValue(val))
  272. {
  273. return val;
  274. }
  275. }
  276. throw new JavaScriptException(Engine.TypeError);
  277. }
  278. if ((hint == Types.Number) || (hint == Types.None))
  279. {
  280. var valueOf = Get("valueOf");
  281. var callable = valueOf as ICallable;
  282. if (callable != null)
  283. {
  284. var val = callable.Call(this, Arguments.Empty);
  285. if (TypeConverter.IsPrimitiveValue(val))
  286. {
  287. return val;
  288. }
  289. }
  290. var toString = Get("toString");
  291. callable = toString as ICallable;
  292. if (callable != null)
  293. {
  294. var str = callable.Call(this, Arguments.Empty);
  295. if (TypeConverter.IsPrimitiveValue(str))
  296. {
  297. return str;
  298. }
  299. }
  300. throw new JavaScriptException(Engine.TypeError);
  301. }
  302. return ToString();
  303. }
  304. /// <summary>
  305. /// Creates or alters the named own property to
  306. /// have the state described by a Property
  307. /// Descriptor. The flag controls failure handling.
  308. /// </summary>
  309. /// <param name="propertyName"></param>
  310. /// <param name="desc"></param>
  311. /// <param name="throwOnError"></param>
  312. /// <returns></returns>
  313. public virtual bool DefineOwnProperty(string propertyName, PropertyDescriptor desc, bool throwOnError)
  314. {
  315. var current = GetOwnProperty(propertyName);
  316. if (current == PropertyDescriptor.Undefined)
  317. {
  318. if (!Extensible)
  319. {
  320. if (throwOnError)
  321. {
  322. throw new JavaScriptException(Engine.TypeError);
  323. }
  324. return false;
  325. }
  326. else
  327. {
  328. if (desc.IsGenericDescriptor() || desc.IsDataDescriptor())
  329. {
  330. Properties[propertyName] = new PropertyDescriptor(desc);
  331. }
  332. else
  333. {
  334. Properties[propertyName] = new PropertyDescriptor(desc);
  335. }
  336. }
  337. return true;
  338. }
  339. // Step 5
  340. if(!current.Configurable.IsPresent && !current.Enumerable.IsPresent && !(current.IsDataDescriptor() && current.Writable.IsPresent))
  341. {
  342. if (!desc.IsDataDescriptor() || desc.Value.Value == null)
  343. {
  344. return true;
  345. }
  346. }
  347. // Step 6
  348. var configurableIsSame = current.Configurable.IsPresent
  349. ? desc.Configurable.IsPresent && (current.Configurable.Value == desc.Configurable.Value)
  350. : !desc.Configurable.IsPresent;
  351. var enumerableIsSame = current.Enumerable.IsPresent
  352. ? desc.Enumerable.IsPresent && (current.Enumerable.Value == desc.Enumerable.Value)
  353. : !desc.Enumerable.IsPresent;
  354. var writableIsSame = true;
  355. var valueIsSame = true;
  356. if (current.IsDataDescriptor() && desc.IsDataDescriptor())
  357. {
  358. var currentDataDescriptor = current;
  359. var descDataDescriptor = desc;
  360. writableIsSame = currentDataDescriptor.Writable.IsPresent
  361. ? descDataDescriptor.Writable.IsPresent && (currentDataDescriptor.Writable.Value == descDataDescriptor.Writable.Value)
  362. : !descDataDescriptor.Writable.IsPresent;
  363. valueIsSame = ExpressionInterpreter.SameValue(currentDataDescriptor.Value, descDataDescriptor.Value);
  364. }
  365. else if (current.IsAccessorDescriptor() && desc.IsAccessorDescriptor())
  366. {
  367. var currentAccessorDescriptor = current;
  368. var descAccessorDescriptor = desc;
  369. valueIsSame = ExpressionInterpreter.SameValue(currentAccessorDescriptor.Get, descAccessorDescriptor.Get)
  370. && ExpressionInterpreter.SameValue(currentAccessorDescriptor.Set, descAccessorDescriptor.Set);
  371. }
  372. else
  373. {
  374. valueIsSame = false;
  375. }
  376. if (configurableIsSame && enumerableIsSame && writableIsSame && valueIsSame)
  377. {
  378. return true;
  379. }
  380. if (current.Configurable.Value == false)
  381. {
  382. if (desc.Configurable.Value)
  383. {
  384. if (throwOnError)
  385. {
  386. throw new JavaScriptException(Engine.TypeError);
  387. }
  388. return false;
  389. }
  390. if (desc.Enumerable.IsPresent && desc.Enumerable.Value != current.Enumerable.Value)
  391. {
  392. if (throwOnError)
  393. {
  394. throw new JavaScriptException(Engine.TypeError);
  395. }
  396. return false;
  397. }
  398. }
  399. if (!desc.IsGenericDescriptor())
  400. {
  401. if (current.IsDataDescriptor() != desc.IsDataDescriptor())
  402. {
  403. if (!current.Configurable.Value)
  404. {
  405. if (throwOnError)
  406. {
  407. throw new JavaScriptException(Engine.TypeError);
  408. }
  409. return false;
  410. }
  411. if (current.IsDataDescriptor())
  412. {
  413. Properties[propertyName] = current = new PropertyDescriptor(
  414. get: Undefined.Instance,
  415. set: Undefined.Instance,
  416. enumerable: current.Enumerable.Value,
  417. configurable: current.Configurable.Value
  418. );
  419. }
  420. else
  421. {
  422. Properties[propertyName] = current = new PropertyDescriptor(
  423. value: Undefined.Instance,
  424. writable: null,
  425. enumerable: current.Enumerable.Value,
  426. configurable: current.Configurable.Value
  427. );
  428. }
  429. }
  430. else if (current.IsDataDescriptor() && desc.IsDataDescriptor())
  431. {
  432. if (current.Configurable.Value == false)
  433. {
  434. if (!current.Writable.Value && desc.Writable.Value)
  435. {
  436. if (throwOnError)
  437. {
  438. throw new JavaScriptException(Engine.TypeError);
  439. }
  440. return false;
  441. }
  442. if (!current.Writable.Value)
  443. {
  444. if (desc.Value.IsPresent && !valueIsSame)
  445. {
  446. if (throwOnError)
  447. {
  448. throw new JavaScriptException(Engine.TypeError);
  449. }
  450. return false;
  451. }
  452. }
  453. }
  454. if (desc.Writable.IsAbsent && current.Writable.IsPresent)
  455. {
  456. desc.Enumerable = current.Enumerable;
  457. }
  458. }
  459. else if (current.IsAccessorDescriptor() && desc.IsAccessorDescriptor())
  460. {
  461. if (current.Configurable.Value == false)
  462. {
  463. if ((desc.Set.Value != Undefined.Instance &&
  464. !ExpressionInterpreter.SameValue(desc.Set.Value, current.Set.Value))
  465. ||
  466. (desc.Get.Value != Undefined.Instance &&
  467. !ExpressionInterpreter.SameValue(desc.Get.Value, current.Get.Value)))
  468. {
  469. if (throwOnError)
  470. {
  471. throw new JavaScriptException(Engine.TypeError);
  472. }
  473. return false;
  474. }
  475. }
  476. }
  477. }
  478. if (desc.Value.IsPresent)
  479. {
  480. current.Value = desc.Value;
  481. }
  482. if (desc.Writable.IsPresent)
  483. {
  484. current.Writable = desc.Writable;
  485. }
  486. if (desc.Enumerable.IsPresent)
  487. {
  488. current.Enumerable = desc.Enumerable;
  489. }
  490. if (desc.Configurable.IsPresent)
  491. {
  492. current.Configurable = desc.Configurable;
  493. }
  494. if (desc.Get.IsPresent)
  495. {
  496. current.Get = desc.Get;
  497. }
  498. if (desc.Set.IsPresent)
  499. {
  500. current.Set = desc.Set;
  501. }
  502. return true;
  503. }
  504. /// <summary>
  505. /// Optimized version of [[Put]] when the property is known to be undeclared already
  506. /// </summary>
  507. /// <param name="name"></param>
  508. /// <param name="value"></param>
  509. /// <param name="writable"></param>
  510. /// <param name="configurable"></param>
  511. /// <param name="enumerable"></param>
  512. public void FastAddProperty(string name, object value, bool writable, bool enumerable, bool configurable)
  513. {
  514. Properties.Add(name, new PropertyDescriptor(value, writable, enumerable, configurable));
  515. }
  516. /// <summary>
  517. /// Optimized version of [[Put]] when the property is known to be already declared
  518. /// </summary>
  519. /// <param name="name"></param>
  520. /// <param name="value"></param>
  521. public void FastSetProperty(string name, PropertyDescriptor value)
  522. {
  523. Properties[name] = value;
  524. }
  525. public override string ToString()
  526. {
  527. return TypeConverter.ToString(this);
  528. }
  529. }
  530. }