ObjectInstance.cs 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527
  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() => 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 uint index,
  900. out JsValue value,
  901. bool visitUnassigned,
  902. bool fromEnd = false)
  903. {
  904. long GetLength()
  905. {
  906. var descValue = Get(CommonProperties.Length);
  907. var len = TypeConverter.ToNumber(descValue);
  908. return (long) System.Math.Max(
  909. 0,
  910. System.Math.Min(len, ArrayOperations.MaxArrayLikeLength));
  911. }
  912. bool TryGetValue(uint 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. for (uint k = 0; k < length; k++)
  932. {
  933. if (TryGetValue(k, out var kvalue) || visitUnassigned)
  934. {
  935. args[0] = kvalue;
  936. args[1] = k;
  937. var testResult = callable.Call(thisArg, args);
  938. if (TypeConverter.ToBoolean(testResult))
  939. {
  940. index = k;
  941. value = kvalue;
  942. return true;
  943. }
  944. }
  945. }
  946. _engine._jsValueArrayPool.ReturnArray(args);
  947. index = 0;
  948. value = Undefined;
  949. return false;
  950. }
  951. internal ICallable GetCallable(JsValue source)
  952. {
  953. if (source is ICallable callable)
  954. {
  955. return callable;
  956. }
  957. ExceptionHelper.ThrowTypeError(_engine.Realm, "Argument must be callable");
  958. return null;
  959. }
  960. internal bool IsConcatSpreadable
  961. {
  962. get
  963. {
  964. var spreadable = Get(GlobalSymbolRegistry.IsConcatSpreadable);
  965. if (!spreadable.IsUndefined())
  966. {
  967. return TypeConverter.ToBoolean(spreadable);
  968. }
  969. return IsArray();
  970. }
  971. }
  972. public virtual bool IsArrayLike => TryGetValue(CommonProperties.Length, out var lengthValue)
  973. && lengthValue.IsNumber()
  974. && ((JsNumber) lengthValue)._value >= 0;
  975. // safe default
  976. internal virtual bool HasOriginalIterator => false;
  977. internal override bool IsIntegerIndexedArray => false;
  978. public virtual uint Length => (uint) TypeConverter.ToLength(Get(CommonProperties.Length));
  979. public virtual bool PreventExtensions()
  980. {
  981. Extensible = false;
  982. return true;
  983. }
  984. protected internal virtual ObjectInstance? GetPrototypeOf()
  985. {
  986. return _prototype;
  987. }
  988. /// <summary>
  989. /// https://tc39.es/ecma262/#sec-ordinarysetprototypeof
  990. /// </summary>
  991. public virtual bool SetPrototypeOf(JsValue value)
  992. {
  993. if (!value.IsObject() && !value.IsNull())
  994. {
  995. ExceptionHelper.ThrowArgumentException();
  996. }
  997. var current = _prototype ?? Null;
  998. if (ReferenceEquals(value, current))
  999. {
  1000. return true;
  1001. }
  1002. if (!Extensible)
  1003. {
  1004. return false;
  1005. }
  1006. if (value.IsNull())
  1007. {
  1008. _prototype = null;
  1009. return true;
  1010. }
  1011. // validate chain
  1012. var p = value as ObjectInstance;
  1013. bool done = false;
  1014. while (!done)
  1015. {
  1016. if (p is null)
  1017. {
  1018. done = true;
  1019. }
  1020. else if (ReferenceEquals(p, this))
  1021. {
  1022. return false;
  1023. }
  1024. else
  1025. {
  1026. p = p._prototype;
  1027. }
  1028. }
  1029. _prototype = value as ObjectInstance;
  1030. return true;
  1031. }
  1032. /// <summary>
  1033. /// https://tc39.es/ecma262/#sec-setfunctionname
  1034. /// </summary>
  1035. internal void SetFunctionName(JsValue name, string? prefix = null)
  1036. {
  1037. if (name is JsSymbol symbol)
  1038. {
  1039. name = symbol._value.IsUndefined()
  1040. ? JsString.Empty
  1041. : new JsString("[" + symbol._value + "]");
  1042. }
  1043. if (!string.IsNullOrWhiteSpace(prefix))
  1044. {
  1045. name = prefix + " " + name;
  1046. }
  1047. DefinePropertyOrThrow(CommonProperties.Name, new PropertyDescriptor(name, PropertyFlag.Configurable));
  1048. }
  1049. /// <summary>
  1050. /// https://tc39.es/ecma262/#sec-createmethodproperty
  1051. /// </summary>
  1052. internal virtual bool CreateMethodProperty(JsValue p, JsValue v)
  1053. {
  1054. var newDesc = new PropertyDescriptor(v, PropertyFlag.NonEnumerable);
  1055. return DefineOwnProperty(p, newDesc);
  1056. }
  1057. /// <summary>
  1058. /// https://tc39.es/ecma262/#sec-createdatapropertyorthrow
  1059. /// </summary>
  1060. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  1061. public bool CreateDataProperty(JsValue p, JsValue v)
  1062. {
  1063. return DefineOwnProperty(p, new PropertyDescriptor(v, PropertyFlag.ConfigurableEnumerableWritable));
  1064. }
  1065. /// <summary>
  1066. /// https://tc39.es/ecma262/#sec-createdataproperty
  1067. /// </summary>
  1068. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  1069. internal bool CreateDataPropertyOrThrow(JsValue p, JsValue v)
  1070. {
  1071. if (!CreateDataProperty(p, v))
  1072. {
  1073. ExceptionHelper.ThrowTypeError(_engine.Realm);
  1074. }
  1075. return true;
  1076. }
  1077. /// <summary>
  1078. /// https://tc39.es/ecma262/#sec-createnonenumerabledatapropertyorthrow
  1079. /// </summary>
  1080. internal void CreateNonEnumerableDataPropertyOrThrow(JsValue p, JsValue v)
  1081. {
  1082. var newDesc = new PropertyDescriptor(v, true, false, true);
  1083. DefinePropertyOrThrow(p, newDesc);
  1084. }
  1085. /// <summary>
  1086. /// https://tc39.es/ecma262/#sec-ordinaryobjectcreate
  1087. /// </summary>
  1088. internal static JsObject OrdinaryObjectCreate(Engine engine, ObjectInstance? proto)
  1089. {
  1090. var prototype = new JsObject(engine)
  1091. {
  1092. _prototype = proto
  1093. };
  1094. return prototype;
  1095. }
  1096. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  1097. internal ICallable? GetMethod(JsValue property)
  1098. {
  1099. return GetMethod(_engine.Realm, this, property);
  1100. }
  1101. internal static ICallable? GetMethod(Realm realm, JsValue v, JsValue p)
  1102. {
  1103. var jsValue = v.Get(p);
  1104. if (jsValue.IsNullOrUndefined())
  1105. {
  1106. return null;
  1107. }
  1108. var callable = jsValue as ICallable;
  1109. if (callable is null)
  1110. {
  1111. ExceptionHelper.ThrowTypeError(realm, "Value returned for property '" + p + "' of object is not a function");
  1112. }
  1113. return callable;
  1114. }
  1115. internal void CopyDataProperties(
  1116. ObjectInstance target,
  1117. HashSet<JsValue>? excludedItems)
  1118. {
  1119. var keys = GetOwnPropertyKeys();
  1120. for (var i = 0; i < keys.Count; i++)
  1121. {
  1122. var key = keys[i];
  1123. if (excludedItems == null || !excludedItems.Contains(key))
  1124. {
  1125. var desc = GetOwnProperty(key);
  1126. if (desc.Enumerable)
  1127. {
  1128. target.CreateDataProperty(key, UnwrapJsValue(desc, this));
  1129. }
  1130. }
  1131. }
  1132. }
  1133. internal JsArray EnumerableOwnPropertyNames(EnumerableOwnPropertyNamesKind kind)
  1134. {
  1135. var ownKeys = GetOwnPropertyKeys(Types.String);
  1136. var array = Engine.Realm.Intrinsics.Array.ArrayCreate((uint) ownKeys.Count);
  1137. uint index = 0;
  1138. for (var i = 0; i < ownKeys.Count; i++)
  1139. {
  1140. var property = ownKeys[i];
  1141. if (!property.IsString())
  1142. {
  1143. continue;
  1144. }
  1145. var desc = GetOwnProperty(property);
  1146. if (desc != PropertyDescriptor.Undefined && desc.Enumerable)
  1147. {
  1148. if (kind == EnumerableOwnPropertyNamesKind.Key)
  1149. {
  1150. array.SetIndexValue(index, property, updateLength: false);
  1151. }
  1152. else
  1153. {
  1154. var value = Get(property);
  1155. if (kind == EnumerableOwnPropertyNamesKind.Value)
  1156. {
  1157. array.SetIndexValue(index, value, updateLength: false);
  1158. }
  1159. else
  1160. {
  1161. var objectInstance = _engine.Realm.Intrinsics.Array.ArrayCreate(2);
  1162. objectInstance.SetIndexValue(0, property, updateLength: false);
  1163. objectInstance.SetIndexValue(1, value, updateLength: false);
  1164. array.SetIndexValue(index, objectInstance, updateLength: false);
  1165. }
  1166. }
  1167. index++;
  1168. }
  1169. }
  1170. array.SetLength(index);
  1171. return array;
  1172. }
  1173. internal enum EnumerableOwnPropertyNamesKind
  1174. {
  1175. Key,
  1176. Value,
  1177. KeyValue
  1178. }
  1179. internal ObjectInstance AssertThisIsObjectInstance(JsValue value, string methodName)
  1180. {
  1181. var instance = value as ObjectInstance;
  1182. if (instance is null)
  1183. {
  1184. ThrowIncompatibleReceiver(value, methodName);
  1185. }
  1186. return instance!;
  1187. }
  1188. [MethodImpl(MethodImplOptions.NoInlining)]
  1189. private void ThrowIncompatibleReceiver(JsValue value, string methodName)
  1190. {
  1191. ExceptionHelper.ThrowTypeError(_engine.Realm, $"Method {methodName} called on incompatible receiver {value}");
  1192. }
  1193. public override bool Equals(JsValue? obj)
  1194. {
  1195. return Equals(obj as ObjectInstance);
  1196. }
  1197. public bool Equals(ObjectInstance? other)
  1198. {
  1199. if (other is null)
  1200. {
  1201. return false;
  1202. }
  1203. if (ReferenceEquals(this, other))
  1204. {
  1205. return true;
  1206. }
  1207. return false;
  1208. }
  1209. internal IEnumerable<JsValue> GetKeys()
  1210. {
  1211. var visited = new HashSet<JsValue>();
  1212. foreach (var key in GetOwnPropertyKeys(Types.String))
  1213. {
  1214. var desc = GetOwnProperty(key);
  1215. if (desc != PropertyDescriptor.Undefined)
  1216. {
  1217. visited.Add(key);
  1218. if (desc.Enumerable)
  1219. {
  1220. yield return key;
  1221. }
  1222. }
  1223. }
  1224. if (Prototype is null)
  1225. {
  1226. yield break;
  1227. }
  1228. foreach (var protoKey in Prototype.GetKeys())
  1229. {
  1230. if (!visited.Contains(protoKey))
  1231. {
  1232. yield return protoKey;
  1233. }
  1234. }
  1235. }
  1236. public override string ToString()
  1237. {
  1238. return TypeConverter.ToString(this);
  1239. }
  1240. internal virtual ulong GetSmallestIndex(ulong length)
  1241. {
  1242. // there are some evil tests that iterate a lot with unshift..
  1243. if (Properties == null)
  1244. {
  1245. return 0;
  1246. }
  1247. var min = length;
  1248. foreach (var entry in Properties)
  1249. {
  1250. if (ulong.TryParse(entry.Key.ToString(), out var index))
  1251. {
  1252. min = System.Math.Min(index, min);
  1253. }
  1254. }
  1255. if (Prototype?.Properties != null)
  1256. {
  1257. foreach (var entry in Prototype.Properties)
  1258. {
  1259. if (ulong.TryParse(entry.Key.ToString(), out var index))
  1260. {
  1261. min = System.Math.Min(index, min);
  1262. }
  1263. }
  1264. }
  1265. return min;
  1266. }
  1267. /// <summary>
  1268. /// https://tc39.es/ecma262/#sec-setintegritylevel
  1269. /// </summary>
  1270. internal bool SetIntegrityLevel(IntegrityLevel level)
  1271. {
  1272. var status = PreventExtensions();
  1273. if (!status)
  1274. {
  1275. return false;
  1276. }
  1277. var keys = GetOwnPropertyKeys();
  1278. if (level == IntegrityLevel.Sealed)
  1279. {
  1280. for (var i = 0; i < keys.Count; i++)
  1281. {
  1282. var k = keys[i];
  1283. DefinePropertyOrThrow(k, new PropertyDescriptor { Configurable = false });
  1284. }
  1285. }
  1286. else
  1287. {
  1288. for (var i = 0; i < keys.Count; i++)
  1289. {
  1290. var k = keys[i];
  1291. var currentDesc = GetOwnProperty(k);
  1292. if (currentDesc != PropertyDescriptor.Undefined)
  1293. {
  1294. PropertyDescriptor desc;
  1295. if (currentDesc.IsAccessorDescriptor())
  1296. {
  1297. desc = new PropertyDescriptor { Configurable = false };
  1298. }
  1299. else
  1300. {
  1301. desc = new PropertyDescriptor { Configurable = false, Writable = false };
  1302. }
  1303. DefinePropertyOrThrow(k, desc);
  1304. }
  1305. }
  1306. }
  1307. return true;
  1308. }
  1309. internal enum IntegrityLevel
  1310. {
  1311. Sealed,
  1312. Frozen
  1313. }
  1314. }
  1315. }