ObjectInstance.cs 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550
  1. using System.Diagnostics;
  2. using System.Diagnostics.CodeAnalysis;
  3. using System.Runtime.CompilerServices;
  4. using Jint.Collections;
  5. using Jint.Native.Array;
  6. using Jint.Native.BigInt;
  7. using Jint.Native.Boolean;
  8. using Jint.Native.Function;
  9. using Jint.Native.Number;
  10. using Jint.Native.RegExp;
  11. using Jint.Native.String;
  12. using Jint.Native.Symbol;
  13. using Jint.Runtime;
  14. using Jint.Runtime.Descriptors;
  15. using Jint.Runtime.Interop;
  16. namespace Jint.Native.Object
  17. {
  18. public partial class ObjectInstance : JsValue, IEquatable<ObjectInstance>
  19. {
  20. private bool _initialized;
  21. private readonly ObjectClass _class;
  22. internal PropertyDictionary? _properties;
  23. internal SymbolDictionary? _symbols;
  24. internal ObjectInstance? _prototype;
  25. protected readonly Engine _engine;
  26. protected ObjectInstance(Engine engine) : this(engine, ObjectClass.Object)
  27. {
  28. }
  29. internal ObjectInstance(
  30. Engine engine,
  31. ObjectClass objectClass = ObjectClass.Object,
  32. InternalTypes type = InternalTypes.Object)
  33. : base(type)
  34. {
  35. _engine = engine;
  36. _class = objectClass;
  37. // if engine is ready, we can take default prototype for object
  38. _prototype = engine.Realm.Intrinsics?.Object?.PrototypeObject;
  39. Extensible = true;
  40. }
  41. public Engine Engine
  42. {
  43. [DebuggerStepThrough]
  44. get => _engine;
  45. }
  46. /// <summary>
  47. /// The prototype of this object.
  48. /// </summary>
  49. public ObjectInstance? Prototype
  50. {
  51. [DebuggerStepThrough]
  52. get => GetPrototypeOf();
  53. }
  54. /// <summary>
  55. /// If true, own properties may be added to the
  56. /// object.
  57. /// </summary>
  58. public virtual bool Extensible { get; private set; }
  59. internal PropertyDictionary? Properties
  60. {
  61. [DebuggerStepThrough]
  62. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  63. get => _properties;
  64. }
  65. /// <summary>
  66. /// A value indicating a specification defined classification of objects.
  67. /// </summary>
  68. internal ObjectClass Class
  69. {
  70. [DebuggerStepThrough]
  71. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  72. get => _class;
  73. }
  74. public JsValue this[JsValue property]
  75. {
  76. get => Get(property);
  77. set => Set(property, value);
  78. }
  79. /// <summary>
  80. /// https://tc39.es/ecma262/#sec-construct
  81. /// </summary>
  82. internal static ObjectInstance Construct(IConstructor f, JsValue[]? argumentsList = null, IConstructor? newTarget = null)
  83. {
  84. newTarget ??= f;
  85. argumentsList ??= System.Array.Empty<JsValue>();
  86. return f.Construct(argumentsList, (JsValue) newTarget);
  87. }
  88. /// <summary>
  89. /// https://tc39.es/ecma262/#sec-speciesconstructor
  90. /// </summary>
  91. internal static IConstructor SpeciesConstructor(ObjectInstance o, IConstructor defaultConstructor)
  92. {
  93. var c = o.Get(CommonProperties.Constructor);
  94. if (c.IsUndefined())
  95. {
  96. return defaultConstructor;
  97. }
  98. var oi = c as ObjectInstance;
  99. if (oi is null)
  100. {
  101. ExceptionHelper.ThrowTypeError(o._engine.Realm);
  102. }
  103. var s = oi.Get(GlobalSymbolRegistry.Species);
  104. if (s.IsNullOrUndefined())
  105. {
  106. return defaultConstructor;
  107. }
  108. if (s.IsConstructor)
  109. {
  110. return (IConstructor) s;
  111. }
  112. ExceptionHelper.ThrowTypeError(o._engine.Realm);
  113. return null;
  114. }
  115. internal void SetProperties(PropertyDictionary? properties)
  116. {
  117. if (properties != null)
  118. {
  119. properties.CheckExistingKeys = true;
  120. }
  121. _properties = properties;
  122. }
  123. internal void SetSymbols(SymbolDictionary? symbols)
  124. {
  125. _symbols = symbols;
  126. }
  127. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  128. internal void SetProperty(JsValue property, PropertyDescriptor value)
  129. {
  130. if (property is JsString jsString)
  131. {
  132. SetProperty(jsString.ToString(), value);
  133. }
  134. else
  135. {
  136. SetPropertyUnlikely(property, value);
  137. }
  138. }
  139. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  140. internal void SetProperty(string property, PropertyDescriptor value)
  141. {
  142. Key key = property;
  143. SetProperty(key, value);
  144. }
  145. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  146. internal void SetProperty(Key property, PropertyDescriptor value)
  147. {
  148. _properties ??= new PropertyDictionary();
  149. _properties[property] = value;
  150. }
  151. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  152. internal void SetDataProperty(string property, JsValue value)
  153. {
  154. _properties ??= new PropertyDictionary();
  155. _properties[property] = new PropertyDescriptor(value, PropertyFlag.ConfigurableEnumerableWritable);
  156. }
  157. [MethodImpl(MethodImplOptions.NoInlining)]
  158. private void SetPropertyUnlikely(JsValue property, PropertyDescriptor value)
  159. {
  160. var propertyKey = TypeConverter.ToPropertyKey(property);
  161. if (!property.IsSymbol())
  162. {
  163. _properties ??= new PropertyDictionary();
  164. _properties[TypeConverter.ToString(propertyKey)] = value;
  165. }
  166. else
  167. {
  168. _symbols ??= new SymbolDictionary();
  169. _symbols[(JsSymbol) propertyKey] = value;
  170. }
  171. }
  172. internal void ClearProperties()
  173. {
  174. _properties?.Clear();
  175. _symbols?.Clear();
  176. }
  177. public virtual IEnumerable<KeyValuePair<JsValue, PropertyDescriptor>> GetOwnProperties()
  178. {
  179. EnsureInitialized();
  180. if (_properties != null)
  181. {
  182. foreach (var pair in _properties)
  183. {
  184. yield return new KeyValuePair<JsValue, PropertyDescriptor>(new JsString(pair.Key), pair.Value);
  185. }
  186. }
  187. if (_symbols != null)
  188. {
  189. foreach (var pair in _symbols)
  190. {
  191. yield return new KeyValuePair<JsValue, PropertyDescriptor>(pair.Key, pair.Value);
  192. }
  193. }
  194. }
  195. public virtual List<JsValue> GetOwnPropertyKeys(Types types = Types.String | Types.Symbol)
  196. {
  197. EnsureInitialized();
  198. var propertyKeys = new List<JsValue>();
  199. if ((types & Types.String) != 0)
  200. {
  201. propertyKeys.AddRange(GetInitialOwnStringPropertyKeys());
  202. }
  203. var keys = new List<JsValue>(_properties?.Count ?? 0 + _symbols?.Count ?? 0 + propertyKeys.Count);
  204. List<JsValue>? symbolKeys = null;
  205. if ((types & Types.String) != 0 && _properties != null)
  206. {
  207. foreach (var pair in _properties)
  208. {
  209. var propertyName = pair.Key.Name;
  210. var arrayIndex = ArrayInstance.ParseArrayIndex(propertyName);
  211. if (arrayIndex < ArrayOperations.MaxArrayLength)
  212. {
  213. keys.Add(JsString.Create(arrayIndex));
  214. }
  215. else
  216. {
  217. propertyKeys.Add(new JsString(propertyName));
  218. }
  219. }
  220. }
  221. keys.Sort((v1, v2) => TypeConverter.ToNumber(v1).CompareTo(TypeConverter.ToNumber(v2)));
  222. keys.AddRange(propertyKeys);
  223. if ((types & Types.Symbol) != 0 && _symbols != null)
  224. {
  225. foreach (var pair in _symbols)
  226. {
  227. symbolKeys ??= new List<JsValue>();
  228. symbolKeys.Add(pair.Key);
  229. }
  230. }
  231. if (symbolKeys != null)
  232. {
  233. keys.AddRange(symbolKeys);
  234. }
  235. return keys;
  236. }
  237. internal virtual IEnumerable<JsValue> GetInitialOwnStringPropertyKeys() => System.Linq.Enumerable.Empty<JsValue>();
  238. protected virtual void AddProperty(JsValue property, PropertyDescriptor descriptor)
  239. {
  240. SetProperty(property, descriptor);
  241. }
  242. protected virtual bool TryGetProperty(JsValue property, [NotNullWhen(true)] out PropertyDescriptor? descriptor)
  243. {
  244. descriptor = null;
  245. var key = TypeConverter.ToPropertyKey(property);
  246. if (!key.IsSymbol())
  247. {
  248. return _properties?.TryGetValue(TypeConverter.ToString(key), out descriptor) == true;
  249. }
  250. return _symbols?.TryGetValue((JsSymbol) key, out descriptor) == true;
  251. }
  252. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  253. public bool HasOwnProperty(JsValue property)
  254. {
  255. return !ReferenceEquals(GetOwnProperty(property), PropertyDescriptor.Undefined);
  256. }
  257. public virtual void RemoveOwnProperty(JsValue property)
  258. {
  259. EnsureInitialized();
  260. var key = TypeConverter.ToPropertyKey(property);
  261. if (!key.IsSymbol())
  262. {
  263. _properties?.Remove(TypeConverter.ToString(key));
  264. return;
  265. }
  266. _symbols?.Remove((JsSymbol) key);
  267. }
  268. public override JsValue Get(JsValue property, JsValue receiver)
  269. {
  270. if ((_type & InternalTypes.PlainObject) != 0 && ReferenceEquals(this, receiver) && property is JsString jsString)
  271. {
  272. EnsureInitialized();
  273. if (_properties?.TryGetValue(jsString.ToString(), out var ownDesc) == true)
  274. {
  275. return UnwrapJsValue(ownDesc, receiver);
  276. }
  277. }
  278. else
  279. {
  280. var desc = GetOwnProperty(property);
  281. if (desc != PropertyDescriptor.Undefined)
  282. {
  283. return UnwrapJsValue(desc, receiver);
  284. }
  285. }
  286. return Prototype?.Get(property, receiver) ?? Undefined;
  287. }
  288. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  289. internal JsValue UnwrapJsValue(PropertyDescriptor desc)
  290. {
  291. return UnwrapJsValue(desc, this);
  292. }
  293. internal static JsValue UnwrapJsValue(PropertyDescriptor desc, JsValue thisObject)
  294. {
  295. var value = (desc._flags & PropertyFlag.CustomJsValue) != 0
  296. ? desc.CustomValue
  297. : desc._value;
  298. // IsDataDescriptor inlined
  299. if ((desc._flags & (PropertyFlag.WritableSet | PropertyFlag.Writable)) != 0 || value is not null)
  300. {
  301. return value ?? Undefined;
  302. }
  303. return UnwrapFromGetter(desc, thisObject);
  304. }
  305. /// <summary>
  306. /// A rarer case.
  307. /// </summary>
  308. [MethodImpl(MethodImplOptions.NoInlining)]
  309. private static JsValue UnwrapFromGetter(PropertyDescriptor desc, JsValue thisObject)
  310. {
  311. var getter = desc.Get ?? Undefined;
  312. if (getter.IsUndefined())
  313. {
  314. return Undefined;
  315. }
  316. var functionInstance = (FunctionInstance) getter;
  317. return functionInstance._engine.Call(functionInstance, thisObject, Arguments.Empty, expression: null);
  318. }
  319. /// <summary>
  320. /// Returns the Property Descriptor of the named
  321. /// own property of this object, or undefined if
  322. /// absent.
  323. /// http://www.ecma-international.org/ecma-262/5.1/#sec-8.12.1
  324. /// </summary>
  325. public virtual PropertyDescriptor GetOwnProperty(JsValue property)
  326. {
  327. EnsureInitialized();
  328. PropertyDescriptor? descriptor = null;
  329. var key = TypeConverter.ToPropertyKey(property);
  330. if (!key.IsSymbol())
  331. {
  332. _properties?.TryGetValue(TypeConverter.ToString(key), out descriptor);
  333. }
  334. else
  335. {
  336. _symbols?.TryGetValue((JsSymbol) key, out descriptor);
  337. }
  338. return descriptor ?? PropertyDescriptor.Undefined;
  339. }
  340. protected internal virtual void SetOwnProperty(JsValue property, PropertyDescriptor desc)
  341. {
  342. EnsureInitialized();
  343. SetProperty(property, desc);
  344. }
  345. /// <summary>
  346. /// http://www.ecma-international.org/ecma-262/5.1/#sec-8.12.2
  347. /// </summary>
  348. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  349. public PropertyDescriptor GetProperty(JsValue property)
  350. {
  351. var prop = GetOwnProperty(property);
  352. if (prop != PropertyDescriptor.Undefined)
  353. {
  354. return prop;
  355. }
  356. return Prototype?.GetProperty(property) ?? PropertyDescriptor.Undefined;
  357. }
  358. public bool TryGetValue(JsValue property, out JsValue value)
  359. {
  360. value = Undefined;
  361. var desc = GetOwnProperty(property);
  362. if (desc != null && desc != PropertyDescriptor.Undefined)
  363. {
  364. if (desc == PropertyDescriptor.Undefined)
  365. {
  366. return false;
  367. }
  368. var descValue = desc.Value;
  369. if (desc.WritableSet && !ReferenceEquals(descValue, null))
  370. {
  371. value = descValue;
  372. return true;
  373. }
  374. var getter = desc.Get ?? Undefined;
  375. if (getter.IsUndefined())
  376. {
  377. value = Undefined;
  378. return false;
  379. }
  380. // if getter is not undefined it must be ICallable
  381. var callable = (ICallable) getter;
  382. value = callable.Call(this, Arguments.Empty);
  383. return true;
  384. }
  385. if (ReferenceEquals(Prototype, null))
  386. {
  387. return false;
  388. }
  389. return Prototype.TryGetValue(property, out value);
  390. }
  391. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  392. public bool Set(JsValue p, JsValue v, bool throwOnError)
  393. {
  394. if (!Set(p, v) && throwOnError)
  395. {
  396. ExceptionHelper.ThrowTypeError(_engine.Realm);
  397. }
  398. return true;
  399. }
  400. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  401. public bool Set(JsValue property, JsValue value)
  402. {
  403. if ((_type & InternalTypes.PlainObject) != 0 && property is JsString jsString)
  404. {
  405. var key = (Key) jsString.ToString();
  406. if (_properties?.TryGetValue(key, out var ownDesc) == true)
  407. {
  408. if ((ownDesc._flags & PropertyFlag.Writable) != 0)
  409. {
  410. ownDesc._value = value;
  411. return true;
  412. }
  413. }
  414. }
  415. return Set(property, value, this);
  416. }
  417. private static readonly PropertyDescriptor _marker = new(Undefined, PropertyFlag.ConfigurableEnumerableWritable);
  418. /// <summary>
  419. /// https://tc39.es/ecma262/#sec-ordinarysetwithowndescriptor
  420. /// </summary>
  421. public override bool Set(JsValue property, JsValue value, JsValue receiver)
  422. {
  423. if ((_type & InternalTypes.PlainObject) != 0 && ReferenceEquals(this, receiver) && property is JsString jsString)
  424. {
  425. var key = (Key) jsString.ToString();
  426. if (_properties?.TryGetValue(key, out var ownDesc) == true)
  427. {
  428. if ((ownDesc._flags & PropertyFlag.Writable) != 0)
  429. {
  430. ownDesc._value = value;
  431. return true;
  432. }
  433. }
  434. }
  435. return SetUnlikely(property, value, receiver);
  436. }
  437. private bool SetUnlikely(JsValue property, JsValue value, JsValue receiver)
  438. {
  439. var ownDesc = GetOwnProperty(property);
  440. if (ownDesc == PropertyDescriptor.Undefined)
  441. {
  442. var parent = GetPrototypeOf();
  443. if (parent is not null)
  444. {
  445. return parent.Set(property, value, receiver);
  446. }
  447. ownDesc = _marker;
  448. }
  449. if (ownDesc.IsDataDescriptor())
  450. {
  451. if (!ownDesc.Writable)
  452. {
  453. return false;
  454. }
  455. if (receiver is not ObjectInstance oi)
  456. {
  457. return false;
  458. }
  459. var existingDescriptor = oi.GetOwnProperty(property);
  460. if (existingDescriptor != PropertyDescriptor.Undefined)
  461. {
  462. if (existingDescriptor.IsAccessorDescriptor())
  463. {
  464. return false;
  465. }
  466. if (!existingDescriptor.Writable)
  467. {
  468. return false;
  469. }
  470. var valueDesc = new PropertyDescriptor(value, PropertyFlag.None);
  471. return oi.DefineOwnProperty(property, valueDesc);
  472. }
  473. else
  474. {
  475. return oi.CreateDataProperty(property, value);
  476. }
  477. }
  478. if (ownDesc.Set is not FunctionInstance setter)
  479. {
  480. return false;
  481. }
  482. _engine.Call(setter, receiver, new[]
  483. {
  484. value
  485. }, expression: null);
  486. return true;
  487. }
  488. /// <summary>
  489. /// Returns a Boolean value indicating whether a
  490. /// [[Put]] operation with PropertyName can be
  491. /// performed.
  492. /// http://www.ecma-international.org/ecma-262/5.1/#sec-8.12.4
  493. /// </summary>
  494. public bool CanPut(JsValue property)
  495. {
  496. var desc = GetOwnProperty(property);
  497. if (desc != PropertyDescriptor.Undefined)
  498. {
  499. if (desc.IsAccessorDescriptor())
  500. {
  501. var set = desc.Set;
  502. if (ReferenceEquals(set, null) || set.IsUndefined())
  503. {
  504. return false;
  505. }
  506. return true;
  507. }
  508. return desc.Writable;
  509. }
  510. if (ReferenceEquals(Prototype, null))
  511. {
  512. return Extensible;
  513. }
  514. var inherited = Prototype.GetProperty(property);
  515. if (inherited == PropertyDescriptor.Undefined)
  516. {
  517. return Extensible;
  518. }
  519. if (inherited.IsAccessorDescriptor())
  520. {
  521. var set = inherited.Set;
  522. if (ReferenceEquals(set, null) || set.IsUndefined())
  523. {
  524. return false;
  525. }
  526. return true;
  527. }
  528. if (!Extensible)
  529. {
  530. return false;
  531. }
  532. return inherited.Writable;
  533. }
  534. /// <summary>
  535. /// https://tc39.es/ecma262/#sec-ordinary-object-internal-methods-and-internal-slots-hasproperty-p
  536. /// </summary>
  537. public virtual bool HasProperty(JsValue property)
  538. {
  539. var key = TypeConverter.ToPropertyKey(property);
  540. var hasOwn = GetOwnProperty(key);
  541. if (hasOwn != PropertyDescriptor.Undefined)
  542. {
  543. return true;
  544. }
  545. var parent = GetPrototypeOf();
  546. if (parent is not null)
  547. {
  548. return parent.HasProperty(key);
  549. }
  550. return false;
  551. }
  552. /// <summary>
  553. /// https://tc39.es/ecma262/#sec-deletepropertyorthrow
  554. /// </summary>
  555. public bool DeletePropertyOrThrow(JsValue property)
  556. {
  557. if (!Delete(property))
  558. {
  559. ExceptionHelper.ThrowTypeError(_engine.Realm);
  560. }
  561. return true;
  562. }
  563. /// <summary>
  564. /// Removes the specified named own property
  565. /// from the object. The flag controls failure
  566. /// handling.
  567. /// </summary>
  568. public virtual bool Delete(JsValue property)
  569. {
  570. var desc = GetOwnProperty(property);
  571. if (desc == PropertyDescriptor.Undefined)
  572. {
  573. return true;
  574. }
  575. if (desc.Configurable)
  576. {
  577. RemoveOwnProperty(property);
  578. return true;
  579. }
  580. return false;
  581. }
  582. public bool DefinePropertyOrThrow(JsValue property, PropertyDescriptor desc)
  583. {
  584. if (!DefineOwnProperty(property, desc))
  585. {
  586. ExceptionHelper.ThrowTypeError(_engine.Realm, "Cannot redefine property: " + property);
  587. }
  588. return true;
  589. }
  590. /// <summary>
  591. /// Creates or alters the named own property to have the state described by a PropertyDescriptor.
  592. /// </summary>
  593. public virtual bool DefineOwnProperty(JsValue property, PropertyDescriptor desc)
  594. {
  595. var current = GetOwnProperty(property);
  596. if (current == desc)
  597. {
  598. return true;
  599. }
  600. return ValidateAndApplyPropertyDescriptor(this, property, Extensible, desc, current);
  601. }
  602. /// <summary>
  603. /// https://tc39.es/ecma262/#sec-validateandapplypropertydescriptor
  604. /// </summary>
  605. protected static bool ValidateAndApplyPropertyDescriptor(ObjectInstance? o, JsValue property, bool extensible, PropertyDescriptor desc, PropertyDescriptor current)
  606. {
  607. var descValue = desc.Value;
  608. if (current == PropertyDescriptor.Undefined)
  609. {
  610. if (!extensible)
  611. {
  612. return false;
  613. }
  614. if (o is not null)
  615. {
  616. if (desc.IsGenericDescriptor() || desc.IsDataDescriptor())
  617. {
  618. PropertyDescriptor propertyDescriptor;
  619. if ((desc._flags & PropertyFlag.ConfigurableEnumerableWritable) == PropertyFlag.ConfigurableEnumerableWritable)
  620. {
  621. propertyDescriptor = new PropertyDescriptor(descValue ?? Undefined, PropertyFlag.ConfigurableEnumerableWritable);
  622. }
  623. else if ((desc._flags & PropertyFlag.ConfigurableEnumerableWritable) == 0)
  624. {
  625. propertyDescriptor = new PropertyDescriptor(descValue ?? Undefined, PropertyFlag.AllForbidden);
  626. }
  627. else
  628. {
  629. propertyDescriptor = new PropertyDescriptor(desc)
  630. {
  631. Value = descValue ?? Undefined
  632. };
  633. }
  634. o.SetOwnProperty(property, propertyDescriptor);
  635. }
  636. else
  637. {
  638. var descriptor = new GetSetPropertyDescriptor(desc.Get, desc.Set, PropertyFlag.None)
  639. {
  640. Enumerable = desc.Enumerable,
  641. Configurable = desc.Configurable
  642. };
  643. o.SetOwnProperty(property, descriptor);
  644. }
  645. }
  646. return true;
  647. }
  648. // Step 3
  649. var currentGet = current.Get;
  650. var currentSet = current.Set;
  651. var currentValue = current.Value;
  652. // 4. If every field in Desc is absent, return true.
  653. if ((current._flags & (PropertyFlag.ConfigurableSet | PropertyFlag.EnumerableSet | PropertyFlag.WritableSet)) == 0 &&
  654. ReferenceEquals(currentGet, null) &&
  655. ReferenceEquals(currentSet, null) &&
  656. ReferenceEquals(currentValue, null))
  657. {
  658. return true;
  659. }
  660. // Step 6
  661. var descGet = desc.Get;
  662. var descSet = desc.Set;
  663. if (
  664. current.Configurable == desc.Configurable && current.ConfigurableSet == desc.ConfigurableSet &&
  665. current.Writable == desc.Writable && current.WritableSet == desc.WritableSet &&
  666. current.Enumerable == desc.Enumerable && current.EnumerableSet == desc.EnumerableSet &&
  667. ((ReferenceEquals(currentGet, null) && ReferenceEquals(descGet, null)) || (!ReferenceEquals(currentGet, null) && !ReferenceEquals(descGet, null) && SameValue(currentGet, descGet))) &&
  668. ((ReferenceEquals(currentSet, null) && ReferenceEquals(descSet, null)) || (!ReferenceEquals(currentSet, null) && !ReferenceEquals(descSet, null) && SameValue(currentSet, descSet))) &&
  669. ((ReferenceEquals(currentValue, null) && ReferenceEquals(descValue, null)) || (!ReferenceEquals(currentValue, null) && !ReferenceEquals(descValue, null) && currentValue == descValue))
  670. )
  671. {
  672. return true;
  673. }
  674. if (!current.Configurable)
  675. {
  676. if (desc.Configurable)
  677. {
  678. return false;
  679. }
  680. if (desc.EnumerableSet && (desc.Enumerable != current.Enumerable))
  681. {
  682. return false;
  683. }
  684. }
  685. if (!desc.IsGenericDescriptor())
  686. {
  687. if (current.IsDataDescriptor() != desc.IsDataDescriptor())
  688. {
  689. if (!current.Configurable)
  690. {
  691. return false;
  692. }
  693. if (o is not null)
  694. {
  695. var flags = current.Flags & ~(PropertyFlag.Writable | PropertyFlag.WritableSet | PropertyFlag.CustomJsValue);
  696. if (current.IsDataDescriptor())
  697. {
  698. o.SetOwnProperty(property, current = new GetSetPropertyDescriptor(
  699. get: Undefined,
  700. set: Undefined,
  701. flags
  702. ));
  703. }
  704. else
  705. {
  706. o.SetOwnProperty(property, current = new PropertyDescriptor(
  707. value: Undefined,
  708. flags
  709. ));
  710. }
  711. }
  712. }
  713. else if (current.IsDataDescriptor() && desc.IsDataDescriptor())
  714. {
  715. if (!current.Configurable)
  716. {
  717. if (!current.Writable && desc.Writable)
  718. {
  719. return false;
  720. }
  721. if (!current.Writable)
  722. {
  723. if (!ReferenceEquals(descValue, null) && !SameValue(descValue, currentValue!))
  724. {
  725. return false;
  726. }
  727. }
  728. }
  729. }
  730. else if (current.IsAccessorDescriptor() && desc.IsAccessorDescriptor())
  731. {
  732. if (!current.Configurable)
  733. {
  734. if ((!ReferenceEquals(descSet, null) && !SameValue(descSet, currentSet ?? Undefined))
  735. ||
  736. (!ReferenceEquals(descGet, null) && !SameValue(descGet, currentGet ?? Undefined)))
  737. {
  738. return false;
  739. }
  740. }
  741. }
  742. }
  743. if (o is not null)
  744. {
  745. if (!ReferenceEquals(descValue, null))
  746. {
  747. current.Value = descValue;
  748. }
  749. if (desc.WritableSet)
  750. {
  751. current.Writable = desc.Writable;
  752. }
  753. if (desc.EnumerableSet)
  754. {
  755. current.Enumerable = desc.Enumerable;
  756. }
  757. if (desc.ConfigurableSet)
  758. {
  759. current.Configurable = desc.Configurable;
  760. }
  761. PropertyDescriptor? mutable = null;
  762. if (!ReferenceEquals(descGet, null))
  763. {
  764. mutable = new GetSetPropertyDescriptor(mutable ?? current);
  765. ((GetSetPropertyDescriptor) mutable).SetGet(descGet);
  766. }
  767. if (!ReferenceEquals(descSet, null))
  768. {
  769. mutable = new GetSetPropertyDescriptor(mutable ?? current);
  770. ((GetSetPropertyDescriptor) mutable).SetSet(descSet);
  771. }
  772. if (mutable != null)
  773. {
  774. // replace old with new type that supports get and set
  775. o.SetOwnProperty(property, mutable);
  776. }
  777. }
  778. return true;
  779. }
  780. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  781. protected internal void EnsureInitialized()
  782. {
  783. if (_initialized)
  784. {
  785. return;
  786. }
  787. // we need to set flag eagerly to prevent wrong recursion
  788. _initialized = true;
  789. Initialize();
  790. }
  791. protected virtual void Initialize()
  792. {
  793. }
  794. public override object ToObject()
  795. {
  796. return ToObject(new ObjectTraverseStack(_engine));
  797. }
  798. private object ToObject(ObjectTraverseStack stack)
  799. {
  800. if (this is IObjectWrapper wrapper)
  801. {
  802. return wrapper.Target;
  803. }
  804. stack.Enter(this);
  805. object? converted = null;
  806. switch (Class)
  807. {
  808. case ObjectClass.String:
  809. if (this is StringInstance stringInstance)
  810. {
  811. converted = stringInstance.StringData.ToString();
  812. }
  813. break;
  814. case ObjectClass.Date:
  815. if (this is JsDate dateInstance)
  816. {
  817. converted = dateInstance.ToDateTime();
  818. }
  819. break;
  820. case ObjectClass.Boolean:
  821. if (this is BooleanInstance booleanInstance)
  822. {
  823. converted = ((JsBoolean) booleanInstance.BooleanData)._value
  824. ? JsBoolean.BoxedTrue
  825. : JsBoolean.BoxedFalse;
  826. }
  827. break;
  828. case ObjectClass.Function:
  829. if (this is ICallable function)
  830. {
  831. converted = (Func<JsValue, JsValue[], JsValue>) function.Call;
  832. }
  833. break;
  834. case ObjectClass.Number:
  835. if (this is NumberInstance numberInstance)
  836. {
  837. converted = numberInstance.NumberData._value;
  838. }
  839. break;
  840. case ObjectClass.RegExp:
  841. if (this is RegExpInstance regeExpInstance)
  842. {
  843. converted = regeExpInstance.Value;
  844. }
  845. break;
  846. case ObjectClass.Arguments:
  847. case ObjectClass.Object:
  848. if (this is JsArray arrayInstance)
  849. {
  850. var result = new object?[arrayInstance.Length];
  851. for (uint i = 0; i < result.Length; i++)
  852. {
  853. var value = arrayInstance[i];
  854. object? valueToSet = null;
  855. if (!value.IsUndefined())
  856. {
  857. valueToSet = value is ObjectInstance oi
  858. ? oi.ToObject(stack)
  859. : value.ToObject();
  860. }
  861. result[i] = valueToSet;
  862. }
  863. converted = result;
  864. break;
  865. }
  866. if (this is BigIntInstance bigIntInstance)
  867. {
  868. converted = bigIntInstance.BigIntData._value;
  869. break;
  870. }
  871. var o = _engine.Options.Interop.CreateClrObject(this);
  872. foreach (var p in GetOwnProperties())
  873. {
  874. if (!p.Value.Enumerable)
  875. {
  876. continue;
  877. }
  878. var key = p.Key.ToString();
  879. var propertyValue = Get(p.Key);
  880. var value = propertyValue is ObjectInstance oi
  881. ? oi.ToObject(stack)
  882. : propertyValue.ToObject();
  883. o.Add(key, value);
  884. }
  885. converted = o;
  886. break;
  887. default:
  888. converted = this;
  889. break;
  890. }
  891. stack.Exit();
  892. return converted!;
  893. }
  894. /// <summary>
  895. /// Handles the generic find of (callback[, thisArg])
  896. /// </summary>
  897. internal virtual bool FindWithCallback(
  898. JsValue[] arguments,
  899. out ulong index,
  900. out JsValue value,
  901. bool visitUnassigned,
  902. bool fromEnd = false)
  903. {
  904. ulong GetLength()
  905. {
  906. var descValue = Get(CommonProperties.Length);
  907. var len = TypeConverter.ToNumber(descValue);
  908. return (ulong) System.Math.Max(
  909. 0,
  910. System.Math.Min(len, ArrayOperations.MaxArrayLikeLength));
  911. }
  912. bool TryGetValue(ulong idx, out JsValue jsValue)
  913. {
  914. var property = JsString.Create(idx);
  915. var kPresent = HasProperty(property);
  916. jsValue = kPresent ? Get(property) : Undefined;
  917. return kPresent;
  918. }
  919. var length = GetLength();
  920. if (length == 0)
  921. {
  922. index = 0;
  923. value = Undefined;
  924. return false;
  925. }
  926. var callbackfn = arguments.At(0);
  927. var thisArg = arguments.At(1);
  928. var callable = GetCallable(callbackfn);
  929. var args = _engine._jsValueArrayPool.RentArray(3);
  930. args[2] = this;
  931. if (!fromEnd)
  932. {
  933. for (ulong k = 0; k < length; k++)
  934. {
  935. if (TryGetValue(k, out var kvalue) || visitUnassigned)
  936. {
  937. args[0] = kvalue;
  938. args[1] = k;
  939. var testResult = callable.Call(thisArg, args);
  940. if (TypeConverter.ToBoolean(testResult))
  941. {
  942. index = k;
  943. value = kvalue;
  944. return true;
  945. }
  946. }
  947. }
  948. }
  949. else
  950. {
  951. for (ulong k = length - 1; k >= 0; k--)
  952. {
  953. if (TryGetValue(k, out var kvalue) || visitUnassigned)
  954. {
  955. kvalue ??= Undefined;
  956. args[0] = kvalue;
  957. args[1] = k;
  958. var testResult = callable.Call(thisArg, args);
  959. if (TypeConverter.ToBoolean(testResult))
  960. {
  961. index = k;
  962. value = kvalue;
  963. return true;
  964. }
  965. }
  966. }
  967. }
  968. _engine._jsValueArrayPool.ReturnArray(args);
  969. index = 0;
  970. value = Undefined;
  971. return false;
  972. }
  973. internal ICallable GetCallable(JsValue source)
  974. {
  975. if (source is ICallable callable)
  976. {
  977. return callable;
  978. }
  979. ExceptionHelper.ThrowTypeError(_engine.Realm, "Argument must be callable");
  980. return null;
  981. }
  982. internal bool IsConcatSpreadable
  983. {
  984. get
  985. {
  986. var spreadable = Get(GlobalSymbolRegistry.IsConcatSpreadable);
  987. if (!spreadable.IsUndefined())
  988. {
  989. return TypeConverter.ToBoolean(spreadable);
  990. }
  991. return IsArray();
  992. }
  993. }
  994. public virtual bool IsArrayLike => TryGetValue(CommonProperties.Length, out var lengthValue)
  995. && lengthValue.IsNumber()
  996. && ((JsNumber) lengthValue)._value >= 0;
  997. // safe default
  998. internal virtual bool HasOriginalIterator => false;
  999. internal override bool IsIntegerIndexedArray => false;
  1000. public virtual uint Length => (uint) TypeConverter.ToLength(Get(CommonProperties.Length));
  1001. public virtual bool PreventExtensions()
  1002. {
  1003. Extensible = false;
  1004. return true;
  1005. }
  1006. protected internal virtual ObjectInstance? GetPrototypeOf()
  1007. {
  1008. return _prototype;
  1009. }
  1010. /// <summary>
  1011. /// https://tc39.es/ecma262/#sec-ordinarysetprototypeof
  1012. /// </summary>
  1013. public virtual bool SetPrototypeOf(JsValue value)
  1014. {
  1015. if (!value.IsObject() && !value.IsNull())
  1016. {
  1017. ExceptionHelper.ThrowArgumentException();
  1018. }
  1019. var current = _prototype ?? Null;
  1020. if (ReferenceEquals(value, current))
  1021. {
  1022. return true;
  1023. }
  1024. if (!Extensible)
  1025. {
  1026. return false;
  1027. }
  1028. if (value.IsNull())
  1029. {
  1030. _prototype = null;
  1031. return true;
  1032. }
  1033. // validate chain
  1034. var p = value as ObjectInstance;
  1035. bool done = false;
  1036. while (!done)
  1037. {
  1038. if (p is null)
  1039. {
  1040. done = true;
  1041. }
  1042. else if (ReferenceEquals(p, this))
  1043. {
  1044. return false;
  1045. }
  1046. else
  1047. {
  1048. p = p._prototype;
  1049. }
  1050. }
  1051. _prototype = value as ObjectInstance;
  1052. return true;
  1053. }
  1054. /// <summary>
  1055. /// https://tc39.es/ecma262/#sec-setfunctionname
  1056. /// </summary>
  1057. internal void SetFunctionName(JsValue name, string? prefix = null)
  1058. {
  1059. if (name is JsSymbol symbol)
  1060. {
  1061. name = symbol._value.IsUndefined()
  1062. ? JsString.Empty
  1063. : new JsString("[" + symbol._value + "]");
  1064. }
  1065. if (!string.IsNullOrWhiteSpace(prefix))
  1066. {
  1067. name = prefix + " " + name;
  1068. }
  1069. DefinePropertyOrThrow(CommonProperties.Name, new PropertyDescriptor(name, PropertyFlag.Configurable));
  1070. }
  1071. /// <summary>
  1072. /// https://tc39.es/ecma262/#sec-createmethodproperty
  1073. /// </summary>
  1074. internal virtual bool CreateMethodProperty(JsValue p, JsValue v)
  1075. {
  1076. var newDesc = new PropertyDescriptor(v, PropertyFlag.NonEnumerable);
  1077. return DefineOwnProperty(p, newDesc);
  1078. }
  1079. /// <summary>
  1080. /// https://tc39.es/ecma262/#sec-createdatapropertyorthrow
  1081. /// </summary>
  1082. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  1083. public bool CreateDataProperty(JsValue p, JsValue v)
  1084. {
  1085. return DefineOwnProperty(p, new PropertyDescriptor(v, PropertyFlag.ConfigurableEnumerableWritable));
  1086. }
  1087. /// <summary>
  1088. /// https://tc39.es/ecma262/#sec-createdataproperty
  1089. /// </summary>
  1090. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  1091. internal bool CreateDataPropertyOrThrow(JsValue p, JsValue v)
  1092. {
  1093. if (!CreateDataProperty(p, v))
  1094. {
  1095. ExceptionHelper.ThrowTypeError(_engine.Realm);
  1096. }
  1097. return true;
  1098. }
  1099. /// <summary>
  1100. /// https://tc39.es/ecma262/#sec-createnonenumerabledatapropertyorthrow
  1101. /// </summary>
  1102. internal void CreateNonEnumerableDataPropertyOrThrow(JsValue p, JsValue v)
  1103. {
  1104. var newDesc = new PropertyDescriptor(v, true, false, true);
  1105. DefinePropertyOrThrow(p, newDesc);
  1106. }
  1107. /// <summary>
  1108. /// https://tc39.es/ecma262/#sec-ordinaryobjectcreate
  1109. /// </summary>
  1110. internal static JsObject OrdinaryObjectCreate(Engine engine, ObjectInstance? proto)
  1111. {
  1112. var prototype = new JsObject(engine)
  1113. {
  1114. _prototype = proto
  1115. };
  1116. return prototype;
  1117. }
  1118. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  1119. internal ICallable? GetMethod(JsValue property)
  1120. {
  1121. return GetMethod(_engine.Realm, this, property);
  1122. }
  1123. internal static ICallable? GetMethod(Realm realm, JsValue v, JsValue p)
  1124. {
  1125. var jsValue = v.Get(p);
  1126. if (jsValue.IsNullOrUndefined())
  1127. {
  1128. return null;
  1129. }
  1130. var callable = jsValue as ICallable;
  1131. if (callable is null)
  1132. {
  1133. ExceptionHelper.ThrowTypeError(realm, "Value returned for property '" + p + "' of object is not a function");
  1134. }
  1135. return callable;
  1136. }
  1137. internal void CopyDataProperties(
  1138. ObjectInstance target,
  1139. HashSet<JsValue>? excludedItems)
  1140. {
  1141. var keys = GetOwnPropertyKeys();
  1142. for (var i = 0; i < keys.Count; i++)
  1143. {
  1144. var key = keys[i];
  1145. if (excludedItems == null || !excludedItems.Contains(key))
  1146. {
  1147. var desc = GetOwnProperty(key);
  1148. if (desc.Enumerable)
  1149. {
  1150. target.CreateDataProperty(key, UnwrapJsValue(desc, this));
  1151. }
  1152. }
  1153. }
  1154. }
  1155. internal JsArray EnumerableOwnPropertyNames(EnumerableOwnPropertyNamesKind kind)
  1156. {
  1157. var ownKeys = GetOwnPropertyKeys(Types.String);
  1158. var array = Engine.Realm.Intrinsics.Array.ArrayCreate((uint) ownKeys.Count);
  1159. uint index = 0;
  1160. for (var i = 0; i < ownKeys.Count; i++)
  1161. {
  1162. var property = ownKeys[i];
  1163. if (!property.IsString())
  1164. {
  1165. continue;
  1166. }
  1167. var desc = GetOwnProperty(property);
  1168. if (desc != PropertyDescriptor.Undefined && desc.Enumerable)
  1169. {
  1170. if (kind == EnumerableOwnPropertyNamesKind.Key)
  1171. {
  1172. array.SetIndexValue(index, property, updateLength: false);
  1173. }
  1174. else
  1175. {
  1176. var value = Get(property);
  1177. if (kind == EnumerableOwnPropertyNamesKind.Value)
  1178. {
  1179. array.SetIndexValue(index, value, updateLength: false);
  1180. }
  1181. else
  1182. {
  1183. var objectInstance = _engine.Realm.Intrinsics.Array.ArrayCreate(2);
  1184. objectInstance.SetIndexValue(0, property, updateLength: false);
  1185. objectInstance.SetIndexValue(1, value, updateLength: false);
  1186. array.SetIndexValue(index, objectInstance, updateLength: false);
  1187. }
  1188. }
  1189. index++;
  1190. }
  1191. }
  1192. array.SetLength(index);
  1193. return array;
  1194. }
  1195. internal enum EnumerableOwnPropertyNamesKind
  1196. {
  1197. Key,
  1198. Value,
  1199. KeyValue
  1200. }
  1201. internal ObjectInstance AssertThisIsObjectInstance(JsValue value, string methodName)
  1202. {
  1203. var instance = value as ObjectInstance;
  1204. if (instance is null)
  1205. {
  1206. ThrowIncompatibleReceiver(value, methodName);
  1207. }
  1208. return instance!;
  1209. }
  1210. [MethodImpl(MethodImplOptions.NoInlining)]
  1211. private void ThrowIncompatibleReceiver(JsValue value, string methodName)
  1212. {
  1213. ExceptionHelper.ThrowTypeError(_engine.Realm, $"Method {methodName} called on incompatible receiver {value}");
  1214. }
  1215. public override bool Equals(JsValue? obj)
  1216. {
  1217. return Equals(obj as ObjectInstance);
  1218. }
  1219. public bool Equals(ObjectInstance? other)
  1220. {
  1221. if (other is null)
  1222. {
  1223. return false;
  1224. }
  1225. if (ReferenceEquals(this, other))
  1226. {
  1227. return true;
  1228. }
  1229. return false;
  1230. }
  1231. internal IEnumerable<JsValue> GetKeys()
  1232. {
  1233. var visited = new HashSet<JsValue>();
  1234. foreach (var key in GetOwnPropertyKeys(Types.String))
  1235. {
  1236. var desc = GetOwnProperty(key);
  1237. if (desc != PropertyDescriptor.Undefined)
  1238. {
  1239. visited.Add(key);
  1240. if (desc.Enumerable)
  1241. {
  1242. yield return key;
  1243. }
  1244. }
  1245. }
  1246. if (Prototype is null)
  1247. {
  1248. yield break;
  1249. }
  1250. foreach (var protoKey in Prototype.GetKeys())
  1251. {
  1252. if (!visited.Contains(protoKey))
  1253. {
  1254. yield return protoKey;
  1255. }
  1256. }
  1257. }
  1258. public override string ToString()
  1259. {
  1260. return TypeConverter.ToString(this);
  1261. }
  1262. internal virtual ulong GetSmallestIndex(ulong length)
  1263. {
  1264. // there are some evil tests that iterate a lot with unshift..
  1265. if (Properties == null)
  1266. {
  1267. return 0;
  1268. }
  1269. var min = length;
  1270. foreach (var entry in Properties)
  1271. {
  1272. if (ulong.TryParse(entry.Key.ToString(), out var index))
  1273. {
  1274. min = System.Math.Min(index, min);
  1275. }
  1276. }
  1277. if (Prototype?.Properties != null)
  1278. {
  1279. foreach (var entry in Prototype.Properties)
  1280. {
  1281. if (ulong.TryParse(entry.Key.ToString(), out var index))
  1282. {
  1283. min = System.Math.Min(index, min);
  1284. }
  1285. }
  1286. }
  1287. return min;
  1288. }
  1289. /// <summary>
  1290. /// https://tc39.es/ecma262/#sec-setintegritylevel
  1291. /// </summary>
  1292. internal bool SetIntegrityLevel(IntegrityLevel level)
  1293. {
  1294. var status = PreventExtensions();
  1295. if (!status)
  1296. {
  1297. return false;
  1298. }
  1299. var keys = GetOwnPropertyKeys();
  1300. if (level == IntegrityLevel.Sealed)
  1301. {
  1302. for (var i = 0; i < keys.Count; i++)
  1303. {
  1304. var k = keys[i];
  1305. DefinePropertyOrThrow(k, new PropertyDescriptor { Configurable = false });
  1306. }
  1307. }
  1308. else
  1309. {
  1310. for (var i = 0; i < keys.Count; i++)
  1311. {
  1312. var k = keys[i];
  1313. var currentDesc = GetOwnProperty(k);
  1314. if (currentDesc != PropertyDescriptor.Undefined)
  1315. {
  1316. PropertyDescriptor desc;
  1317. if (currentDesc.IsAccessorDescriptor())
  1318. {
  1319. desc = new PropertyDescriptor { Configurable = false };
  1320. }
  1321. else
  1322. {
  1323. desc = new PropertyDescriptor { Configurable = false, Writable = false };
  1324. }
  1325. DefinePropertyOrThrow(k, desc);
  1326. }
  1327. }
  1328. }
  1329. return true;
  1330. }
  1331. internal enum IntegrityLevel
  1332. {
  1333. Sealed,
  1334. Frozen
  1335. }
  1336. }
  1337. }