ObjectInstance.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866
  1. using System.Collections.Generic;
  2. using Jint.Runtime;
  3. using Jint.Runtime.Descriptors;
  4. using Jint.Runtime.Descriptors.Specialized;
  5. namespace Jint.Native.Object
  6. {
  7. public class ObjectInstance
  8. {
  9. private const string PropertyNamePrototype = "prototype";
  10. private const string PropertyNameConstructor = "constructor";
  11. private const string PropertyNameLength = "length";
  12. private JsValue _jsValue;
  13. private Dictionary<string, IPropertyDescriptor> _intrinsicProperties;
  14. private MruPropertyCache2<string, IPropertyDescriptor> _properties;
  15. private IPropertyDescriptor _prototype;
  16. private IPropertyDescriptor _constructor;
  17. private IPropertyDescriptor _length;
  18. public ObjectInstance(Engine engine)
  19. {
  20. Engine = engine;
  21. }
  22. public Engine Engine { get; set; }
  23. /// <summary>
  24. /// Caches the constructed JS.
  25. /// </summary>
  26. internal JsValue JsValue
  27. {
  28. get { return _jsValue = _jsValue ?? new JsValue(this); }
  29. }
  30. protected bool TryGetIntrinsicValue(JsSymbol symbol, out JsValue value)
  31. {
  32. IPropertyDescriptor descriptor;
  33. if (_intrinsicProperties != null && _intrinsicProperties.TryGetValue(symbol.AsSymbol(), out descriptor))
  34. {
  35. value = descriptor.Value;
  36. return true;
  37. }
  38. if (Prototype == null)
  39. {
  40. value = JsValue.Undefined;
  41. return false;
  42. }
  43. return Prototype.TryGetIntrinsicValue(symbol, out value);
  44. }
  45. public void SetIntrinsicValue(string name, JsValue value, bool writable, bool enumerable, bool configurable)
  46. {
  47. SetOwnProperty(name, new PropertyDescriptor(value, writable, enumerable, configurable));
  48. }
  49. protected void SetIntrinsicValue(JsSymbol symbol, JsValue value, bool writable, bool enumerable, bool configurable)
  50. {
  51. if (_intrinsicProperties == null)
  52. {
  53. _intrinsicProperties = new Dictionary<string, IPropertyDescriptor>();
  54. }
  55. _intrinsicProperties[symbol.AsSymbol()] = new PropertyDescriptor(value, writable, enumerable, configurable);
  56. }
  57. /// <summary>
  58. /// The prototype of this object.
  59. /// </summary>
  60. public ObjectInstance Prototype { get; set; }
  61. /// <summary>
  62. /// If true, own properties may be added to the
  63. /// object.
  64. /// </summary>
  65. public bool Extensible { get; set; }
  66. /// <summary>
  67. /// A String value indicating a specification defined
  68. /// classification of objects.
  69. /// </summary>
  70. public virtual string Class => "Object";
  71. public virtual IEnumerable<KeyValuePair<string, IPropertyDescriptor>> GetOwnProperties()
  72. {
  73. EnsureInitialized();
  74. if (_prototype != null)
  75. {
  76. yield return new KeyValuePair<string, IPropertyDescriptor>(PropertyNamePrototype, _prototype);
  77. }
  78. if (_constructor != null)
  79. {
  80. yield return new KeyValuePair<string, IPropertyDescriptor>(PropertyNameConstructor, _constructor);
  81. }
  82. if (_length != null)
  83. {
  84. yield return new KeyValuePair<string, IPropertyDescriptor>(PropertyNameLength, _length);
  85. }
  86. if (_properties != null)
  87. {
  88. foreach (var pair in _properties.GetEnumerator())
  89. {
  90. yield return pair;
  91. }
  92. }
  93. }
  94. protected void AddProperty(string propertyName, IPropertyDescriptor descriptor)
  95. {
  96. if (propertyName == PropertyNamePrototype)
  97. {
  98. _prototype = descriptor;
  99. return;
  100. }
  101. if (propertyName == PropertyNameConstructor)
  102. {
  103. _constructor = descriptor;
  104. return;
  105. }
  106. if (propertyName == PropertyNameLength)
  107. {
  108. _length = descriptor;
  109. return;
  110. }
  111. if (_properties == null)
  112. {
  113. _properties = new MruPropertyCache2<string, IPropertyDescriptor>();
  114. }
  115. _properties.Add(propertyName, descriptor);
  116. }
  117. protected bool TryGetProperty(string propertyName, out IPropertyDescriptor descriptor)
  118. {
  119. if (propertyName == PropertyNamePrototype)
  120. {
  121. descriptor = _prototype;
  122. return _prototype != null;
  123. }
  124. if (propertyName == PropertyNameConstructor)
  125. {
  126. descriptor = _constructor;
  127. return _constructor != null;
  128. }
  129. if (propertyName == PropertyNameLength)
  130. {
  131. descriptor = _length;
  132. return _length != null;
  133. }
  134. if (_properties == null)
  135. {
  136. descriptor = null;
  137. return false;
  138. }
  139. return _properties.TryGetValue(propertyName, out descriptor);
  140. }
  141. public virtual bool HasOwnProperty(string propertyName)
  142. {
  143. EnsureInitialized();
  144. if (propertyName == PropertyNamePrototype)
  145. {
  146. return _prototype != null;
  147. }
  148. if (propertyName == PropertyNameConstructor)
  149. {
  150. return _constructor != null;
  151. }
  152. if (propertyName == PropertyNameLength)
  153. {
  154. return _length != null;
  155. }
  156. return _properties?.ContainsKey(propertyName) ?? false;
  157. }
  158. public virtual void RemoveOwnProperty(string propertyName)
  159. {
  160. EnsureInitialized();
  161. if (propertyName == PropertyNamePrototype)
  162. {
  163. _prototype = null;
  164. }
  165. if (propertyName == PropertyNameConstructor)
  166. {
  167. _constructor = null;
  168. }
  169. if (propertyName == PropertyNameLength)
  170. {
  171. _length = null;
  172. }
  173. _properties?.Remove(propertyName);
  174. }
  175. /// <summary>
  176. /// Returns the value of the named property.
  177. /// http://www.ecma-international.org/ecma-262/5.1/#sec-8.12.3
  178. /// </summary>
  179. /// <param name="propertyName"></param>
  180. /// <returns></returns>
  181. public virtual JsValue Get(string propertyName)
  182. {
  183. var desc = GetProperty(propertyName);
  184. if (desc == PropertyDescriptor.Undefined)
  185. {
  186. return JsValue.Undefined;
  187. }
  188. if (desc.IsDataDescriptor())
  189. {
  190. var val = desc.Value;
  191. return val != null ? val : Undefined.Instance;
  192. }
  193. var getter = desc.Get != null ? desc.Get : Undefined.Instance;
  194. if (getter.IsUndefined())
  195. {
  196. return Undefined.Instance;
  197. }
  198. // if getter is not undefined it must be ICallable
  199. var callable = getter.TryCast<ICallable>();
  200. return callable.Call(this, Arguments.Empty);
  201. }
  202. /// <summary>
  203. /// Returns the Property Descriptor of the named
  204. /// own property of this object, or undefined if
  205. /// absent.
  206. /// http://www.ecma-international.org/ecma-262/5.1/#sec-8.12.1
  207. /// </summary>
  208. /// <param name="propertyName"></param>
  209. /// <returns></returns>
  210. public virtual IPropertyDescriptor GetOwnProperty(string propertyName)
  211. {
  212. EnsureInitialized();
  213. if (propertyName == PropertyNamePrototype)
  214. {
  215. return _prototype ?? PropertyDescriptor.Undefined;
  216. }
  217. if (propertyName == PropertyNameConstructor)
  218. {
  219. return _constructor ?? PropertyDescriptor.Undefined;
  220. }
  221. if (propertyName == PropertyNameLength)
  222. {
  223. return _length ?? PropertyDescriptor.Undefined;
  224. }
  225. if (_properties != null && _properties.TryGetValue(propertyName, out var x))
  226. {
  227. return x;
  228. }
  229. return PropertyDescriptor.Undefined;
  230. }
  231. protected internal virtual void SetOwnProperty(string propertyName, IPropertyDescriptor desc)
  232. {
  233. EnsureInitialized();
  234. if (propertyName == PropertyNamePrototype)
  235. {
  236. _prototype = desc;
  237. return;
  238. }
  239. if (propertyName == PropertyNameConstructor)
  240. {
  241. _constructor = desc;
  242. return;
  243. }
  244. if (propertyName == PropertyNameLength)
  245. {
  246. _length = desc;
  247. return;
  248. }
  249. if (_properties == null)
  250. {
  251. _properties = new MruPropertyCache2<string, IPropertyDescriptor>();
  252. }
  253. _properties[propertyName] = desc;
  254. }
  255. /// <summary>
  256. /// http://www.ecma-international.org/ecma-262/5.1/#sec-8.12.2
  257. /// </summary>
  258. /// <param name="propertyName"></param>
  259. /// <returns></returns>
  260. public IPropertyDescriptor GetProperty(string propertyName)
  261. {
  262. var prop = GetOwnProperty(propertyName);
  263. if (prop != PropertyDescriptor.Undefined)
  264. {
  265. return prop;
  266. }
  267. if (Prototype == null)
  268. {
  269. return PropertyDescriptor.Undefined;
  270. }
  271. return Prototype.GetProperty(propertyName);
  272. }
  273. public bool TryGetValue(string propertyName, out JsValue value)
  274. {
  275. value = JsValue.Undefined;
  276. var desc = GetOwnProperty(propertyName);
  277. if (desc != null && desc != PropertyDescriptor.Undefined)
  278. {
  279. if (desc == PropertyDescriptor.Undefined)
  280. {
  281. return false;
  282. }
  283. if (desc.IsDataDescriptor() && desc.Value != null)
  284. {
  285. value = desc.Value;
  286. return true;
  287. }
  288. var getter = desc.Get != null ? desc.Get : Undefined.Instance;
  289. if (getter.IsUndefined())
  290. {
  291. value = Undefined.Instance;
  292. return false;
  293. }
  294. // if getter is not undefined it must be ICallable
  295. var callable = getter.TryCast<ICallable>();
  296. value = callable.Call(this, Arguments.Empty);
  297. return true;
  298. }
  299. if (Prototype == null)
  300. {
  301. return false;
  302. }
  303. return Prototype.TryGetValue(propertyName, out value);
  304. }
  305. /// <summary>
  306. /// Sets the specified named property to the value
  307. /// of the second parameter. The flag controls
  308. /// failure handling.
  309. /// </summary>
  310. /// <param name="propertyName"></param>
  311. /// <param name="value"></param>
  312. /// <param name="throwOnError"></param>
  313. public virtual void Put(string propertyName, JsValue value, bool throwOnError)
  314. {
  315. if (!CanPut(propertyName))
  316. {
  317. if (throwOnError)
  318. {
  319. throw new JavaScriptException(Engine.TypeError);
  320. }
  321. return;
  322. }
  323. var ownDesc = GetOwnProperty(propertyName);
  324. if (ownDesc.IsDataDescriptor())
  325. {
  326. ownDesc.Value = value;
  327. return;
  328. // as per specification
  329. // var valueDesc = new PropertyDescriptor(value: value, writable: null, enumerable: null, configurable: null);
  330. // DefineOwnProperty(propertyName, valueDesc, throwOnError);
  331. // return;
  332. }
  333. // property is an accessor or inherited
  334. var desc = GetProperty(propertyName);
  335. if (desc.IsAccessorDescriptor())
  336. {
  337. var setter = desc.Set.TryCast<ICallable>();
  338. setter.Call(JsValue, new[] {value});
  339. }
  340. else
  341. {
  342. var newDesc = new ConfigurableEnumerableWritablePropertyDescriptor(value);
  343. DefineOwnProperty(propertyName, newDesc, throwOnError);
  344. }
  345. }
  346. /// <summary>
  347. /// Returns a Boolean value indicating whether a
  348. /// [[Put]] operation with PropertyName can be
  349. /// performed.
  350. /// http://www.ecma-international.org/ecma-262/5.1/#sec-8.12.4
  351. /// </summary>
  352. /// <param name="propertyName"></param>
  353. /// <returns></returns>
  354. public bool CanPut(string propertyName)
  355. {
  356. var desc = GetOwnProperty(propertyName);
  357. if (desc != PropertyDescriptor.Undefined)
  358. {
  359. if (desc.IsAccessorDescriptor())
  360. {
  361. if (desc.Set == null || desc.Set.IsUndefined())
  362. {
  363. return false;
  364. }
  365. return true;
  366. }
  367. return desc.Writable.HasValue && desc.Writable.Value;
  368. }
  369. if (Prototype == null)
  370. {
  371. return Extensible;
  372. }
  373. var inherited = Prototype.GetProperty(propertyName);
  374. if (inherited == PropertyDescriptor.Undefined)
  375. {
  376. return Extensible;
  377. }
  378. if (inherited.IsAccessorDescriptor())
  379. {
  380. if (inherited.Set == null || inherited.Set.IsUndefined())
  381. {
  382. return false;
  383. }
  384. return true;
  385. }
  386. if (!Extensible)
  387. {
  388. return false;
  389. }
  390. else
  391. {
  392. return inherited.Writable.HasValue && inherited.Writable.Value;
  393. }
  394. }
  395. /// <summary>
  396. /// Returns a Boolean value indicating whether the
  397. /// object already has a property with the given
  398. /// name.
  399. /// </summary>
  400. /// <param name="propertyName"></param>
  401. /// <returns></returns>
  402. public bool HasProperty(string propertyName)
  403. {
  404. return GetProperty(propertyName) != PropertyDescriptor.Undefined;
  405. }
  406. /// <summary>
  407. /// Removes the specified named own property
  408. /// from the object. The flag controls failure
  409. /// handling.
  410. /// </summary>
  411. /// <param name="propertyName"></param>
  412. /// <param name="throwOnError"></param>
  413. /// <returns></returns>
  414. public virtual bool Delete(string propertyName, bool throwOnError)
  415. {
  416. var desc = GetOwnProperty(propertyName);
  417. if (desc == PropertyDescriptor.Undefined)
  418. {
  419. return true;
  420. }
  421. if (desc.Configurable.HasValue && desc.Configurable.Value)
  422. {
  423. RemoveOwnProperty(propertyName);
  424. return true;
  425. }
  426. else
  427. {
  428. if (throwOnError)
  429. {
  430. throw new JavaScriptException(Engine.TypeError);
  431. }
  432. return false;
  433. }
  434. }
  435. /// <summary>
  436. /// Hint is a String. Returns a default value for the
  437. /// object.
  438. /// </summary>
  439. /// <param name="hint"></param>
  440. /// <returns></returns>
  441. public JsValue DefaultValue(Types hint)
  442. {
  443. EnsureInitialized();
  444. if (hint == Types.String || (hint == Types.None && Class == "Date"))
  445. {
  446. var toString = Get("toString").TryCast<ICallable>();
  447. if (toString != null)
  448. {
  449. var str = toString.Call(JsValue, Arguments.Empty);
  450. if (str.IsPrimitive())
  451. {
  452. return str;
  453. }
  454. }
  455. var valueOf = Get("valueOf").TryCast<ICallable>();
  456. if (valueOf != null)
  457. {
  458. var val = valueOf.Call(JsValue, Arguments.Empty);
  459. if (val.IsPrimitive())
  460. {
  461. return val;
  462. }
  463. }
  464. throw new JavaScriptException(Engine.TypeError);
  465. }
  466. if (hint == Types.Number || hint == Types.None)
  467. {
  468. var valueOf = Get("valueOf").TryCast<ICallable>();
  469. if (valueOf != null)
  470. {
  471. var val = valueOf.Call(JsValue, Arguments.Empty);
  472. if (val.IsPrimitive())
  473. {
  474. return val;
  475. }
  476. }
  477. var toString = Get("toString").TryCast<ICallable>();
  478. if (toString != null)
  479. {
  480. var str = toString.Call(JsValue, Arguments.Empty);
  481. if (str.IsPrimitive())
  482. {
  483. return str;
  484. }
  485. }
  486. throw new JavaScriptException(Engine.TypeError);
  487. }
  488. return ToString();
  489. }
  490. /// <summary>
  491. /// Creates or alters the named own property to
  492. /// have the state described by a Property
  493. /// Descriptor. The flag controls failure handling.
  494. /// </summary>
  495. /// <param name="propertyName"></param>
  496. /// <param name="desc"></param>
  497. /// <param name="throwOnError"></param>
  498. /// <returns></returns>
  499. public virtual bool DefineOwnProperty(string propertyName, IPropertyDescriptor desc, bool throwOnError)
  500. {
  501. var current = GetOwnProperty(propertyName);
  502. if (current == desc)
  503. {
  504. return true;
  505. }
  506. if (current == PropertyDescriptor.Undefined)
  507. {
  508. if (!Extensible)
  509. {
  510. if (throwOnError)
  511. {
  512. throw new JavaScriptException(Engine.TypeError);
  513. }
  514. return false;
  515. }
  516. else
  517. {
  518. if (desc.IsGenericDescriptor() || desc.IsDataDescriptor())
  519. {
  520. IPropertyDescriptor propertyDescriptor;
  521. if (desc.Configurable.GetValueOrDefault() && desc.Enumerable.GetValueOrDefault() && desc.Writable.GetValueOrDefault())
  522. {
  523. propertyDescriptor = new ConfigurableEnumerableWritablePropertyDescriptor(desc.Value != null ? desc.Value : JsValue.Undefined);
  524. }
  525. else if (!desc.Configurable.GetValueOrDefault() && !desc.Enumerable.GetValueOrDefault() && !desc.Writable.GetValueOrDefault())
  526. {
  527. propertyDescriptor = new AllForbiddenPropertyDescriptor(desc.Value != null ? desc.Value : JsValue.Undefined);
  528. }
  529. else
  530. {
  531. propertyDescriptor = new PropertyDescriptor(desc)
  532. {
  533. Value = desc.Value != null ? desc.Value : JsValue.Undefined,
  534. Writable = desc.Writable.HasValue ? desc.Writable.Value : false,
  535. Enumerable = desc.Enumerable.HasValue ? desc.Enumerable.Value : false,
  536. Configurable = desc.Configurable.HasValue ? desc.Configurable.Value : false
  537. };
  538. }
  539. SetOwnProperty(propertyName, propertyDescriptor);
  540. }
  541. else
  542. {
  543. SetOwnProperty(propertyName, new PropertyDescriptor(desc)
  544. {
  545. Get = desc.Get,
  546. Set = desc.Set,
  547. Enumerable = desc.Enumerable.HasValue ? desc.Enumerable : false,
  548. Configurable = desc.Configurable.HasValue ? desc.Configurable : false,
  549. });
  550. }
  551. }
  552. return true;
  553. }
  554. // Step 5
  555. if (!current.Configurable.HasValue &&
  556. !current.Enumerable.HasValue &&
  557. !current.Writable.HasValue &&
  558. current.Get == null &&
  559. current.Set == null &&
  560. current.Value == null)
  561. {
  562. return true;
  563. }
  564. // Step 6
  565. if (
  566. current.Configurable == desc.Configurable &&
  567. current.Writable == desc.Writable &&
  568. current.Enumerable == desc.Enumerable &&
  569. ((current.Get == null && desc.Get == null) || (current.Get != null && desc.Get != null && ExpressionInterpreter.SameValue(current.Get, desc.Get))) &&
  570. ((current.Set == null && desc.Set == null) || (current.Set != null && desc.Set != null && ExpressionInterpreter.SameValue(current.Set, desc.Set))) &&
  571. ((current.Value == null && desc.Value == null) || (current.Value != null && desc.Value != null && ExpressionInterpreter.StrictlyEqual(current.Value, desc.Value)))
  572. )
  573. {
  574. return true;
  575. }
  576. if (!current.Configurable.HasValue || !current.Configurable.Value)
  577. {
  578. if (desc.Configurable.HasValue && desc.Configurable.Value)
  579. {
  580. if (throwOnError)
  581. {
  582. throw new JavaScriptException(Engine.TypeError);
  583. }
  584. return false;
  585. }
  586. if (desc.Enumerable.HasValue && (!current.Enumerable.HasValue || desc.Enumerable.Value != current.Enumerable.Value))
  587. {
  588. if (throwOnError)
  589. {
  590. throw new JavaScriptException(Engine.TypeError);
  591. }
  592. return false;
  593. }
  594. }
  595. if (!desc.IsGenericDescriptor())
  596. {
  597. if (current.IsDataDescriptor() != desc.IsDataDescriptor())
  598. {
  599. if (!current.Configurable.HasValue || !current.Configurable.Value)
  600. {
  601. if (throwOnError)
  602. {
  603. throw new JavaScriptException(Engine.TypeError);
  604. }
  605. return false;
  606. }
  607. if (current.IsDataDescriptor())
  608. {
  609. SetOwnProperty(propertyName, current = new PropertyDescriptor(
  610. get: Undefined.Instance,
  611. set: Undefined.Instance,
  612. enumerable: current.Enumerable,
  613. configurable: current.Configurable
  614. ));
  615. }
  616. else
  617. {
  618. SetOwnProperty(propertyName, current = new PropertyDescriptor(
  619. value: Undefined.Instance,
  620. writable: null,
  621. enumerable: current.Enumerable,
  622. configurable: current.Configurable
  623. ));
  624. }
  625. }
  626. else if (current.IsDataDescriptor() && desc.IsDataDescriptor())
  627. {
  628. if (!current.Configurable.HasValue || current.Configurable.Value == false)
  629. {
  630. if (!current.Writable.HasValue || !current.Writable.Value && desc.Writable.HasValue && desc.Writable.Value)
  631. {
  632. if (throwOnError)
  633. {
  634. throw new JavaScriptException(Engine.TypeError);
  635. }
  636. return false;
  637. }
  638. if (!current.Writable.Value)
  639. {
  640. if (desc.Value != null && !ExpressionInterpreter.SameValue(desc.Value, current.Value))
  641. {
  642. if (throwOnError)
  643. {
  644. throw new JavaScriptException(Engine.TypeError);
  645. }
  646. return false;
  647. }
  648. }
  649. }
  650. }
  651. else if (current.IsAccessorDescriptor() && desc.IsAccessorDescriptor())
  652. {
  653. if (!current.Configurable.HasValue || !current.Configurable.Value)
  654. {
  655. if ((desc.Set != null && !ExpressionInterpreter.SameValue(desc.Set, current.Set != null ? current.Set : Undefined.Instance))
  656. ||
  657. (desc.Get != null && !ExpressionInterpreter.SameValue(desc.Get, current.Get != null ? current.Get : Undefined.Instance)))
  658. {
  659. if (throwOnError)
  660. {
  661. throw new JavaScriptException(Engine.TypeError);
  662. }
  663. return false;
  664. }
  665. }
  666. }
  667. }
  668. if (desc.Value != null)
  669. {
  670. current.Value = desc.Value;
  671. }
  672. PropertyDescriptor mutable = null;
  673. if (desc.Writable.HasValue)
  674. {
  675. current = mutable = current as PropertyDescriptor ?? new PropertyDescriptor(current);
  676. mutable.Writable = desc.Writable;
  677. }
  678. if (desc.Enumerable.HasValue)
  679. {
  680. current = mutable = current as PropertyDescriptor ?? new PropertyDescriptor(current);
  681. mutable.Enumerable = desc.Enumerable;
  682. }
  683. if (desc.Configurable.HasValue)
  684. {
  685. current = mutable = current as PropertyDescriptor ?? new PropertyDescriptor(current);
  686. mutable.Configurable = desc.Configurable;
  687. }
  688. if (desc.Get != null)
  689. {
  690. current = mutable = current as PropertyDescriptor ?? new PropertyDescriptor(current);
  691. mutable.Get = desc.Get;
  692. }
  693. if (desc.Set != null)
  694. {
  695. mutable = current as PropertyDescriptor ?? new PropertyDescriptor(current);
  696. mutable.Set = desc.Set;
  697. }
  698. if (mutable != null)
  699. {
  700. FastSetProperty(propertyName, mutable);
  701. }
  702. return true;
  703. }
  704. /// <summary>
  705. /// Optimized version of [[Put]] when the property is known to be undeclared already
  706. /// </summary>
  707. /// <param name="name"></param>
  708. /// <param name="value"></param>
  709. /// <param name="writable"></param>
  710. /// <param name="configurable"></param>
  711. /// <param name="enumerable"></param>
  712. public void FastAddProperty(string name, JsValue value, bool writable, bool enumerable, bool configurable)
  713. {
  714. SetOwnProperty(name, new PropertyDescriptor(value, writable, enumerable, configurable));
  715. }
  716. /// <summary>
  717. /// Optimized version of [[Put]] when the property is known to be already declared
  718. /// </summary>
  719. /// <param name="name"></param>
  720. /// <param name="value"></param>
  721. public void FastSetProperty(string name, IPropertyDescriptor value)
  722. {
  723. SetOwnProperty(name, value);
  724. }
  725. protected virtual void EnsureInitialized()
  726. {
  727. }
  728. public override string ToString()
  729. {
  730. return TypeConverter.ToString(this);
  731. }
  732. protected uint GetLengthValue() => TypeConverter.ToUint32(_length.Value);
  733. }
  734. }