Object.cpp 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2020-2021, Linus Groh <linusg@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/String.h>
  8. #include <AK/TemporaryChange.h>
  9. #include <LibJS/Heap/Heap.h>
  10. #include <LibJS/Interpreter.h>
  11. #include <LibJS/Runtime/AbstractOperations.h>
  12. #include <LibJS/Runtime/Accessor.h>
  13. #include <LibJS/Runtime/Array.h>
  14. #include <LibJS/Runtime/Error.h>
  15. #include <LibJS/Runtime/GlobalObject.h>
  16. #include <LibJS/Runtime/NativeFunction.h>
  17. #include <LibJS/Runtime/NativeProperty.h>
  18. #include <LibJS/Runtime/Object.h>
  19. #include <LibJS/Runtime/PropertyDescriptor.h>
  20. #include <LibJS/Runtime/ProxyObject.h>
  21. #include <LibJS/Runtime/Shape.h>
  22. #include <LibJS/Runtime/TemporaryClearException.h>
  23. #include <LibJS/Runtime/Value.h>
  24. namespace JS {
  25. // 10.1.12 OrdinaryObjectCreate ( proto [ , additionalInternalSlotsList ] ), https://tc39.es/ecma262/#sec-ordinaryobjectcreate
  26. Object* Object::create(GlobalObject& global_object, Object* prototype)
  27. {
  28. if (!prototype)
  29. return global_object.heap().allocate<Object>(global_object, *global_object.empty_object_shape());
  30. else if (prototype == global_object.object_prototype())
  31. return global_object.heap().allocate<Object>(global_object, *global_object.new_object_shape());
  32. else
  33. return global_object.heap().allocate<Object>(global_object, *prototype);
  34. }
  35. Object::Object(GlobalObjectTag)
  36. {
  37. // This is the global object
  38. m_shape = heap().allocate_without_global_object<Shape>(*this);
  39. }
  40. Object::Object(ConstructWithoutPrototypeTag, GlobalObject& global_object)
  41. {
  42. m_shape = heap().allocate_without_global_object<Shape>(global_object);
  43. }
  44. Object::Object(Object& prototype)
  45. {
  46. m_shape = prototype.global_object().empty_object_shape();
  47. auto success = internal_set_prototype_of(&prototype);
  48. VERIFY(success);
  49. }
  50. Object::Object(Shape& shape)
  51. : m_shape(&shape)
  52. {
  53. m_storage.resize(shape.property_count());
  54. }
  55. void Object::initialize(GlobalObject&)
  56. {
  57. }
  58. Object::~Object()
  59. {
  60. }
  61. // 7.2 Testing and Comparison Operations, https://tc39.es/ecma262/#sec-testing-and-comparison-operations
  62. // 7.2.5 IsExtensible ( O ), https://tc39.es/ecma262/#sec-isextensible-o
  63. bool Object::is_extensible() const
  64. {
  65. return internal_is_extensible();
  66. }
  67. // 7.3 Operations on Objects, https://tc39.es/ecma262/#sec-operations-on-objects
  68. // 7.3.2 Get ( O, P ), https://tc39.es/ecma262/#sec-get-o-p
  69. Value Object::get(PropertyName const& property_name) const
  70. {
  71. // 1. Assert: Type(O) is Object.
  72. // 2. Assert: IsPropertyKey(P) is true.
  73. VERIFY(property_name.is_valid());
  74. // 3. Return ? O.[[Get]](P, O).
  75. return internal_get(property_name, this);
  76. }
  77. // 7.3.3 GetV ( V, P ) is defined as Value::get().
  78. // 7.3.4 Set ( O, P, V, Throw ), https://tc39.es/ecma262/#sec-set-o-p-v-throw
  79. bool Object::set(PropertyName const& property_name, Value value, bool throw_exceptions)
  80. {
  81. VERIFY(!value.is_empty());
  82. auto& vm = this->vm();
  83. // 1. Assert: Type(O) is Object.
  84. // 2. Assert: IsPropertyKey(P) is true.
  85. VERIFY(property_name.is_valid());
  86. // 3. Assert: Type(Throw) is Boolean.
  87. // 4. Let success be ? O.[[Set]](P, V, O).
  88. auto success = internal_set(property_name, value, this);
  89. if (vm.exception())
  90. return {};
  91. // 5. If success is false and Throw is true, throw a TypeError exception.
  92. if (!success && throw_exceptions) {
  93. // FIXME: Improve/contextualize error message
  94. vm.throw_exception<TypeError>(global_object(), ErrorType::ObjectSetReturnedFalse);
  95. return {};
  96. }
  97. // 6. Return success.
  98. return success;
  99. }
  100. // 7.3.5 CreateDataProperty ( O, P, V ), https://tc39.es/ecma262/#sec-createdataproperty
  101. bool Object::create_data_property(PropertyName const& property_name, Value value)
  102. {
  103. // 1. Assert: Type(O) is Object.
  104. // 2. Assert: IsPropertyKey(P) is true.
  105. VERIFY(property_name.is_valid());
  106. // 3. Let newDesc be the PropertyDescriptor { [[Value]]: V, [[Writable]]: true, [[Enumerable]]: true, [[Configurable]]: true }.
  107. auto new_descriptor = PropertyDescriptor {
  108. .value = value,
  109. .writable = true,
  110. .enumerable = true,
  111. .configurable = true,
  112. };
  113. // 4. Return ? O.[[DefineOwnProperty]](P, newDesc).
  114. return internal_define_own_property(property_name, new_descriptor);
  115. }
  116. // 7.3.6 CreateMethodProperty ( O, P, V ), https://tc39.es/ecma262/#sec-createmethodproperty
  117. bool Object::create_method_property(PropertyName const& property_name, Value value)
  118. {
  119. VERIFY(!value.is_empty());
  120. // 1. Assert: Type(O) is Object.
  121. // 2. Assert: IsPropertyKey(P) is true.
  122. VERIFY(property_name.is_valid());
  123. // 3. Let newDesc be the PropertyDescriptor { [[Value]]: V, [[Writable]]: true, [[Enumerable]]: false, [[Configurable]]: true }.
  124. auto new_descriptor = PropertyDescriptor {
  125. .value = value,
  126. .writable = true,
  127. .enumerable = false,
  128. .configurable = true,
  129. };
  130. // 4. Return ? O.[[DefineOwnProperty]](P, newDesc).
  131. return internal_define_own_property(property_name, new_descriptor);
  132. }
  133. // 7.3.7 CreateDataPropertyOrThrow ( O, P, V ), https://tc39.es/ecma262/#sec-createdatapropertyorthrow
  134. bool Object::create_data_property_or_throw(PropertyName const& property_name, Value value)
  135. {
  136. VERIFY(!value.is_empty());
  137. auto& vm = this->vm();
  138. // 1. Assert: Type(O) is Object.
  139. // 2. Assert: IsPropertyKey(P) is true.
  140. VERIFY(property_name.is_valid());
  141. // 3. Let success be ? CreateDataProperty(O, P, V).
  142. auto success = create_data_property(property_name, value);
  143. if (vm.exception())
  144. return {};
  145. // 4. If success is false, throw a TypeError exception.
  146. if (!success) {
  147. // FIXME: Improve/contextualize error message
  148. vm.throw_exception<TypeError>(global_object(), ErrorType::ObjectDefineOwnPropertyReturnedFalse);
  149. return {};
  150. }
  151. // 5. Return success.
  152. return success;
  153. }
  154. // 7.3.8 DefinePropertyOrThrow ( O, P, desc ), https://tc39.es/ecma262/#sec-definepropertyorthrow
  155. bool Object::define_property_or_throw(PropertyName const& property_name, PropertyDescriptor const& property_descriptor)
  156. {
  157. auto& vm = this->vm();
  158. // 1. Assert: Type(O) is Object.
  159. // 2. Assert: IsPropertyKey(P) is true.
  160. VERIFY(property_name.is_valid());
  161. // 3. Let success be ? O.[[DefineOwnProperty]](P, desc).
  162. auto success = internal_define_own_property(property_name, property_descriptor);
  163. if (vm.exception())
  164. return {};
  165. // 4. If success is false, throw a TypeError exception.
  166. if (!success) {
  167. // FIXME: Improve/contextualize error message
  168. vm.throw_exception<TypeError>(global_object(), ErrorType::ObjectDefineOwnPropertyReturnedFalse);
  169. return {};
  170. }
  171. // 5. Return success.
  172. return success;
  173. }
  174. // 7.3.9 DeletePropertyOrThrow ( O, P ), https://tc39.es/ecma262/#sec-deletepropertyorthrow
  175. bool Object::delete_property_or_throw(PropertyName const& property_name)
  176. {
  177. auto& vm = this->vm();
  178. // 1. Assert: Type(O) is Object.
  179. // 2. Assert: IsPropertyKey(P) is true.
  180. VERIFY(property_name.is_valid());
  181. // 3. Let success be ? O.[[Delete]](P).
  182. auto success = internal_delete(property_name);
  183. if (vm.exception())
  184. return {};
  185. // 4. If success is false, throw a TypeError exception.
  186. if (!success) {
  187. // FIXME: Improve/contextualize error message
  188. vm.throw_exception<TypeError>(global_object(), ErrorType::ObjectDeleteReturnedFalse);
  189. return {};
  190. }
  191. // 5. Return success.
  192. return success;
  193. }
  194. // 7.3.11 HasProperty ( O, P ), https://tc39.es/ecma262/#sec-hasproperty
  195. bool Object::has_property(PropertyName const& property_name) const
  196. {
  197. // 1. Assert: Type(O) is Object.
  198. // 2. Assert: IsPropertyKey(P) is true.
  199. VERIFY(property_name.is_valid());
  200. // 3. Return ? O.[[HasProperty]](P).
  201. return internal_has_property(property_name);
  202. }
  203. // 7.3.12 HasOwnProperty ( O, P ), https://tc39.es/ecma262/#sec-hasownproperty
  204. bool Object::has_own_property(PropertyName const& property_name) const
  205. {
  206. auto& vm = this->vm();
  207. // 1. Assert: Type(O) is Object.
  208. // 2. Assert: IsPropertyKey(P) is true.
  209. VERIFY(property_name.is_valid());
  210. // 3. Let desc be ? O.[[GetOwnProperty]](P).
  211. auto descriptor = internal_get_own_property(property_name);
  212. if (vm.exception())
  213. return {};
  214. // 4. If desc is undefined, return false.
  215. if (!descriptor.has_value())
  216. return false;
  217. // 5. Return true.
  218. return true;
  219. }
  220. // 7.3.15 SetIntegrityLevel ( O, level ), https://tc39.es/ecma262/#sec-setintegritylevel
  221. bool Object::set_integrity_level(IntegrityLevel level)
  222. {
  223. auto& vm = this->vm();
  224. auto& global_object = this->global_object();
  225. // 1. Assert: Type(O) is Object.
  226. // 2. Assert: level is either sealed or frozen.
  227. VERIFY(level == IntegrityLevel::Sealed || level == IntegrityLevel::Frozen);
  228. // 3. Let status be ? O.[[PreventExtensions]]().
  229. auto status = internal_prevent_extensions();
  230. if (vm.exception())
  231. return {};
  232. // 4. If status is false, return false.
  233. if (!status)
  234. return false;
  235. // 5. Let keys be ? O.[[OwnPropertyKeys]]().
  236. auto keys = internal_own_property_keys();
  237. if (vm.exception())
  238. return {};
  239. // 6. If level is sealed, then
  240. if (level == IntegrityLevel::Sealed) {
  241. // a. For each element k of keys, do
  242. for (auto& key : keys) {
  243. auto property_name = PropertyName::from_value(global_object, key);
  244. // i. Perform ? DefinePropertyOrThrow(O, k, PropertyDescriptor { [[Configurable]]: false }).
  245. define_property_or_throw(property_name, { .configurable = false });
  246. if (vm.exception())
  247. return {};
  248. }
  249. }
  250. // 7. Else,
  251. else {
  252. // a. Assert: level is frozen.
  253. // b. For each element k of keys, do
  254. for (auto& key : keys) {
  255. auto property_name = PropertyName::from_value(global_object, key);
  256. // i. Let currentDesc be ? O.[[GetOwnProperty]](k).
  257. auto current_descriptor = internal_get_own_property(property_name);
  258. if (vm.exception())
  259. return {};
  260. // ii. If currentDesc is not undefined, then
  261. if (!current_descriptor.has_value())
  262. continue;
  263. PropertyDescriptor descriptor;
  264. // 1. If IsAccessorDescriptor(currentDesc) is true, then
  265. if (current_descriptor->is_accessor_descriptor()) {
  266. // a. Let desc be the PropertyDescriptor { [[Configurable]]: false }.
  267. descriptor = { .configurable = false };
  268. }
  269. // 2. Else,
  270. else {
  271. // a. Let desc be the PropertyDescriptor { [[Configurable]]: false, [[Writable]]: false }.
  272. descriptor = { .writable = false, .configurable = false };
  273. }
  274. // 3. Perform ? DefinePropertyOrThrow(O, k, desc).
  275. define_property_or_throw(property_name, descriptor);
  276. if (vm.exception())
  277. return {};
  278. }
  279. }
  280. // 8. Return true.
  281. return true;
  282. }
  283. // 7.3.16 TestIntegrityLevel ( O, level ), https://tc39.es/ecma262/#sec-testintegritylevel
  284. bool Object::test_integrity_level(IntegrityLevel level) const
  285. {
  286. auto& vm = this->vm();
  287. // 1. Assert: Type(O) is Object.
  288. // 2. Assert: level is either sealed or frozen.
  289. VERIFY(level == IntegrityLevel::Sealed || level == IntegrityLevel::Frozen);
  290. // 3. Let extensible be ? IsExtensible(O).
  291. auto extensible = is_extensible();
  292. if (vm.exception())
  293. return {};
  294. // 4. If extensible is true, return false.
  295. // 5. NOTE: If the object is extensible, none of its properties are examined.
  296. if (extensible)
  297. return false;
  298. // 6. Let keys be ? O.[[OwnPropertyKeys]]().
  299. auto keys = internal_own_property_keys();
  300. if (vm.exception())
  301. return {};
  302. // 7. For each element k of keys, do
  303. for (auto& key : keys) {
  304. auto property_name = PropertyName::from_value(global_object(), key);
  305. // a. Let currentDesc be ? O.[[GetOwnProperty]](k).
  306. auto current_descriptor = internal_get_own_property(property_name);
  307. if (vm.exception())
  308. return {};
  309. // b. If currentDesc is not undefined, then
  310. if (!current_descriptor.has_value())
  311. continue;
  312. // i. If currentDesc.[[Configurable]] is true, return false.
  313. if (*current_descriptor->configurable)
  314. return false;
  315. // ii. If level is frozen and IsDataDescriptor(currentDesc) is true, then
  316. if (level == IntegrityLevel::Frozen && current_descriptor->is_data_descriptor()) {
  317. // 1. If currentDesc.[[Writable]] is true, return false.
  318. if (*current_descriptor->writable)
  319. return false;
  320. }
  321. }
  322. // 8. Return true.
  323. return true;
  324. }
  325. // 7.3.23 EnumerableOwnPropertyNames ( O, kind ), https://tc39.es/ecma262/#sec-enumerableownpropertynames
  326. MarkedValueList Object::enumerable_own_property_names(PropertyKind kind) const
  327. {
  328. // NOTE: This has been flattened for readability, so some `else` branches in the
  329. // spec text have been replaced with `continue`s in the loop below.
  330. auto& vm = this->vm();
  331. auto& global_object = this->global_object();
  332. // 1. Assert: Type(O) is Object.
  333. // 2. Let ownKeys be ? O.[[OwnPropertyKeys]]().
  334. auto own_keys = internal_own_property_keys();
  335. if (vm.exception())
  336. return MarkedValueList { heap() };
  337. // 3. Let properties be a new empty List.
  338. auto properties = MarkedValueList { heap() };
  339. // 4. For each element key of ownKeys, do
  340. for (auto& key : own_keys) {
  341. // a. If Type(key) is String, then
  342. if (!key.is_string())
  343. continue;
  344. auto property_name = PropertyName::from_value(global_object, key);
  345. // i. Let desc be ? O.[[GetOwnProperty]](key).
  346. auto descriptor = internal_get_own_property(property_name);
  347. if (vm.exception())
  348. return MarkedValueList { heap() };
  349. // ii. If desc is not undefined and desc.[[Enumerable]] is true, then
  350. if (descriptor.has_value() && *descriptor->enumerable) {
  351. // 1. If kind is key, append key to properties.
  352. if (kind == PropertyKind::Key) {
  353. properties.append(key);
  354. continue;
  355. }
  356. // 2. Else,
  357. // a. Let value be ? Get(O, key).
  358. auto value = get(property_name);
  359. if (vm.exception())
  360. return MarkedValueList { heap() };
  361. // b. If kind is value, append value to properties.
  362. if (kind == PropertyKind::Value) {
  363. properties.append(value);
  364. continue;
  365. }
  366. // c. Else,
  367. // i. Assert: kind is key+value.
  368. VERIFY(kind == PropertyKind::KeyAndValue);
  369. // ii. Let entry be ! CreateArrayFromList(« key, value »).
  370. auto entry = Array::create_from(global_object, { key, value });
  371. // iii. Append entry to properties.
  372. properties.append(entry);
  373. }
  374. }
  375. // 5. Return properties.
  376. return properties;
  377. }
  378. // 10.1 Ordinary Object Internal Methods and Internal Slots, https://tc39.es/ecma262/#sec-ordinary-object-internal-methods-and-internal-slots
  379. // 10.1.1 [[GetPrototypeOf]] ( ), https://tc39.es/ecma262/#sec-ordinary-object-internal-methods-and-internal-slots-getprototypeof
  380. Object* Object::internal_get_prototype_of() const
  381. {
  382. // 1. Return O.[[Prototype]].
  383. return const_cast<Object*>(prototype());
  384. }
  385. // 10.1.2 [[SetPrototypeOf]] ( V ), https://tc39.es/ecma262/#sec-ordinary-object-internal-methods-and-internal-slots-setprototypeof-v
  386. bool Object::internal_set_prototype_of(Object* new_prototype)
  387. {
  388. // 1. Assert: Either Type(V) is Object or Type(V) is Null.
  389. // 2. Let current be O.[[Prototype]].
  390. // 3. If SameValue(V, current) is true, return true.
  391. if (prototype() == new_prototype)
  392. return true;
  393. // 4. Let extensible be O.[[Extensible]].
  394. // 5. If extensible is false, return false.
  395. if (!m_is_extensible)
  396. return false;
  397. // 6. Let p be V.
  398. auto* prototype = new_prototype;
  399. // 7. Let done be false.
  400. // 8. Repeat, while done is false,
  401. while (prototype) {
  402. // a. If p is null, set done to true.
  403. // b. Else if SameValue(p, O) is true, return false.
  404. if (prototype == this)
  405. return false;
  406. // c. Else,
  407. // i. If p.[[GetPrototypeOf]] is not the ordinary object internal method defined in 10.1.1, set done to true.
  408. // NOTE: This is a best-effort implementation; we don't have a good way of detecting whether certain virtual
  409. // Object methods have been overridden by a given object, but as ProxyObject is the only one doing that for
  410. // [[SetPrototypeOf]], this check does the trick.
  411. if (is<ProxyObject>(prototype))
  412. break;
  413. // ii. Else, set p to p.[[Prototype]].
  414. prototype = prototype->prototype();
  415. }
  416. // 9. Set O.[[Prototype]] to V.
  417. auto& shape = this->shape();
  418. if (shape.is_unique())
  419. shape.set_prototype_without_transition(new_prototype);
  420. else
  421. m_shape = shape.create_prototype_transition(new_prototype);
  422. // 10. Return true.
  423. return true;
  424. }
  425. // 10.1.3 [[IsExtensible]] ( ), https://tc39.es/ecma262/#sec-ordinary-object-internal-methods-and-internal-slots-isextensible
  426. bool Object::internal_is_extensible() const
  427. {
  428. // 1. Return O.[[Extensible]].
  429. return m_is_extensible;
  430. }
  431. // 10.1.4 [[PreventExtensions]] ( ), https://tc39.es/ecma262/#sec-ordinary-object-internal-methods-and-internal-slots-preventextensions
  432. bool Object::internal_prevent_extensions()
  433. {
  434. // 1. Set O.[[Extensible]] to false.
  435. m_is_extensible = false;
  436. // 2. Return true.
  437. return true;
  438. }
  439. // 10.1.5 [[GetOwnProperty]] ( P ), https://tc39.es/ecma262/#sec-ordinary-object-internal-methods-and-internal-slots-getownproperty-p
  440. Optional<PropertyDescriptor> Object::internal_get_own_property(PropertyName const& property_name) const
  441. {
  442. // 1. Assert: IsPropertyKey(P) is true.
  443. VERIFY(property_name.is_valid());
  444. // 2. If O does not have an own property with key P, return undefined.
  445. if (!storage_has(property_name))
  446. return {};
  447. // 3. Let D be a newly created Property Descriptor with no fields.
  448. PropertyDescriptor descriptor;
  449. // 4. Let X be O's own property whose key is P.
  450. auto [value, attributes] = *storage_get(property_name);
  451. // 5. If X is a data property, then
  452. if (!value.is_accessor()) {
  453. // a. Set D.[[Value]] to the value of X's [[Value]] attribute.
  454. descriptor.value = value.value_or(js_undefined());
  455. // b. Set D.[[Writable]] to the value of X's [[Writable]] attribute.
  456. descriptor.writable = attributes.is_writable();
  457. }
  458. // 6. Else,
  459. else {
  460. // a. Assert: X is an accessor property.
  461. // b. Set D.[[Get]] to the value of X's [[Get]] attribute.
  462. descriptor.get = value.as_accessor().getter();
  463. // c. Set D.[[Set]] to the value of X's [[Set]] attribute.
  464. descriptor.set = value.as_accessor().setter();
  465. }
  466. // 7. Set D.[[Enumerable]] to the value of X's [[Enumerable]] attribute.
  467. descriptor.enumerable = attributes.is_enumerable();
  468. // 8. Set D.[[Configurable]] to the value of X's [[Configurable]] attribute.
  469. descriptor.configurable = attributes.is_configurable();
  470. // 9. Return D.
  471. return descriptor;
  472. }
  473. // 10.1.6 [[DefineOwnProperty]] ( P, Desc ), https://tc39.es/ecma262/#sec-ordinary-object-internal-methods-and-internal-slots-defineownproperty-p-desc
  474. bool Object::internal_define_own_property(PropertyName const& property_name, PropertyDescriptor const& property_descriptor)
  475. {
  476. VERIFY(property_name.is_valid());
  477. auto& vm = this->vm();
  478. // 1. Let current be ? O.[[GetOwnProperty]](P).
  479. auto current = internal_get_own_property(property_name);
  480. if (vm.exception())
  481. return {};
  482. // 2. Let extensible be ? IsExtensible(O).
  483. auto extensible = is_extensible();
  484. if (vm.exception())
  485. return {};
  486. // 3. Return ValidateAndApplyPropertyDescriptor(O, P, extensible, Desc, current).
  487. return validate_and_apply_property_descriptor(this, property_name, extensible, property_descriptor, current);
  488. }
  489. // 10.1.7 [[HasProperty]] ( P ), https://tc39.es/ecma262/#sec-ordinary-object-internal-methods-and-internal-slots-hasproperty-p
  490. bool Object::internal_has_property(PropertyName const& property_name) const
  491. {
  492. auto& vm = this->vm();
  493. // 1. Assert: IsPropertyKey(P) is true.
  494. VERIFY(property_name.is_valid());
  495. // 2. Let hasOwn be ? O.[[GetOwnProperty]](P).
  496. auto has_own = internal_get_own_property(property_name);
  497. if (vm.exception())
  498. return {};
  499. // 3. If hasOwn is not undefined, return true.
  500. if (has_own.has_value())
  501. return true;
  502. // 4. Let parent be ? O.[[GetPrototypeOf]]().
  503. auto parent = internal_get_prototype_of();
  504. if (vm.exception())
  505. return {};
  506. // 5. If parent is not null, then
  507. if (parent) {
  508. // a. Return ? parent.[[HasProperty]](P).
  509. return parent->internal_has_property(property_name);
  510. }
  511. // 6. Return false.
  512. return false;
  513. }
  514. // 10.1.8 [[Get]] ( P, Receiver ), https://tc39.es/ecma262/#sec-ordinary-object-internal-methods-and-internal-slots-get-p-receiver
  515. Value Object::internal_get(PropertyName const& property_name, Value receiver) const
  516. {
  517. VERIFY(!receiver.is_empty());
  518. auto& vm = this->vm();
  519. // 1. Assert: IsPropertyKey(P) is true.
  520. VERIFY(property_name.is_valid());
  521. // 2. Let desc be ? O.[[GetOwnProperty]](P).
  522. auto descriptor = internal_get_own_property(property_name);
  523. if (vm.exception())
  524. return {};
  525. // 3. If desc is undefined, then
  526. if (!descriptor.has_value()) {
  527. // a. Let parent be ? O.[[GetPrototypeOf]]().
  528. auto parent = internal_get_prototype_of();
  529. if (vm.exception())
  530. return {};
  531. // b. If parent is null, return undefined.
  532. if (!parent)
  533. return js_undefined();
  534. // c. Return ? parent.[[Get]](P, Receiver).
  535. return parent->internal_get(property_name, receiver);
  536. }
  537. // 4. If IsDataDescriptor(desc) is true, return desc.[[Value]].
  538. if (descriptor->is_data_descriptor())
  539. return *descriptor->value;
  540. // 5. Assert: IsAccessorDescriptor(desc) is true.
  541. VERIFY(descriptor->is_accessor_descriptor());
  542. // 6. Let getter be desc.[[Get]].
  543. auto* getter = *descriptor->get;
  544. // 7. If getter is undefined, return undefined.
  545. if (!getter)
  546. return js_undefined();
  547. // 8. Return ? Call(getter, Receiver).
  548. return vm.call(*getter, receiver);
  549. }
  550. static bool ordinary_set_with_own_descriptor(Object&, PropertyName const&, Value, Value, Optional<PropertyDescriptor>);
  551. // 10.1.9 [[Set]] ( P, V, Receiver ), https://tc39.es/ecma262/#sec-ordinary-object-internal-methods-and-internal-slots-set-p-v-receiver
  552. bool Object::internal_set(PropertyName const& property_name, Value value, Value receiver)
  553. {
  554. VERIFY(!value.is_empty());
  555. VERIFY(!receiver.is_empty());
  556. auto& vm = this->vm();
  557. // 1. Assert: IsPropertyKey(P) is true.
  558. VERIFY(property_name.is_valid());
  559. // 2. Let ownDesc be ? O.[[GetOwnProperty]](P).
  560. auto own_descriptor = internal_get_own_property(property_name);
  561. if (vm.exception())
  562. return {};
  563. // 3. Return OrdinarySetWithOwnDescriptor(O, P, V, Receiver, ownDesc).
  564. return ordinary_set_with_own_descriptor(*this, property_name, value, receiver, own_descriptor);
  565. }
  566. // 10.1.9.2 OrdinarySetWithOwnDescriptor ( O, P, V, Receiver, ownDesc ), https://tc39.es/ecma262/#sec-ordinarysetwithowndescriptor
  567. bool ordinary_set_with_own_descriptor(Object& object, PropertyName const& property_name, Value value, Value receiver, Optional<PropertyDescriptor> own_descriptor)
  568. {
  569. auto& vm = object.vm();
  570. // 1. Assert: IsPropertyKey(P) is true.
  571. VERIFY(property_name.is_valid());
  572. // 2. If ownDesc is undefined, then
  573. if (!own_descriptor.has_value()) {
  574. // a. Let parent be ? O.[[GetPrototypeOf]]().
  575. auto parent = object.internal_get_prototype_of();
  576. if (vm.exception())
  577. return {};
  578. // b. If parent is not null, then
  579. if (parent) {
  580. // i. Return ? parent.[[Set]](P, V, Receiver).
  581. return parent->internal_set(property_name, value, receiver);
  582. }
  583. // c. Else,
  584. else {
  585. // i. Set ownDesc to the PropertyDescriptor { [[Value]]: undefined, [[Writable]]: true, [[Enumerable]]: true, [[Configurable]]: true }.
  586. own_descriptor = PropertyDescriptor {
  587. .value = js_undefined(),
  588. .writable = true,
  589. .enumerable = true,
  590. .configurable = true,
  591. };
  592. }
  593. }
  594. // 3. If IsDataDescriptor(ownDesc) is true, then
  595. if (own_descriptor->is_data_descriptor()) {
  596. // a. If ownDesc.[[Writable]] is false, return false.
  597. if (!*own_descriptor->writable)
  598. return false;
  599. // b. If Type(Receiver) is not Object, return false.
  600. if (!receiver.is_object())
  601. return false;
  602. // c. Let existingDescriptor be ? Receiver.[[GetOwnProperty]](P).
  603. auto existing_descriptor = receiver.as_object().internal_get_own_property(property_name);
  604. if (vm.exception())
  605. return {};
  606. // d. If existingDescriptor is not undefined, then
  607. if (existing_descriptor.has_value()) {
  608. // i. If IsAccessorDescriptor(existingDescriptor) is true, return false.
  609. if (existing_descriptor->is_accessor_descriptor())
  610. return false;
  611. // ii. If existingDescriptor.[[Writable]] is false, return false.
  612. if (!*existing_descriptor->writable)
  613. return false;
  614. // iii. Let valueDesc be the PropertyDescriptor { [[Value]]: V }.
  615. auto value_descriptor = PropertyDescriptor { .value = value };
  616. // iv. Return ? Receiver.[[DefineOwnProperty]](P, valueDesc).
  617. return receiver.as_object().internal_define_own_property(property_name, value_descriptor);
  618. }
  619. // e. Else,
  620. else {
  621. // i. Assert: Receiver does not currently have a property P.
  622. VERIFY(!receiver.as_object().storage_has(property_name));
  623. // ii. Return ? CreateDataProperty(Receiver, P, V).
  624. return receiver.as_object().create_data_property(property_name, value);
  625. }
  626. }
  627. // 4. Assert: IsAccessorDescriptor(ownDesc) is true.
  628. VERIFY(own_descriptor->is_accessor_descriptor());
  629. // 5. Let setter be ownDesc.[[Set]].
  630. auto* setter = *own_descriptor->set;
  631. // 6. If setter is undefined, return false.
  632. if (!setter)
  633. return false;
  634. // 7. Perform ? Call(setter, Receiver, « V »).
  635. (void)vm.call(*setter, receiver, value);
  636. // 8. Return true.
  637. return true;
  638. }
  639. // 10.1.10 [[Delete]] ( P ), https://tc39.es/ecma262/#sec-ordinary-object-internal-methods-and-internal-slots-delete-p
  640. bool Object::internal_delete(PropertyName const& property_name)
  641. {
  642. auto& vm = this->vm();
  643. // 1. Assert: IsPropertyKey(P) is true.
  644. VERIFY(property_name.is_valid());
  645. // 2. Let desc be ? O.[[GetOwnProperty]](P).
  646. auto descriptor = internal_get_own_property(property_name);
  647. if (vm.exception())
  648. return {};
  649. // 3. If desc is undefined, return true.
  650. if (!descriptor.has_value())
  651. return true;
  652. // 4. If desc.[[Configurable]] is true, then
  653. if (*descriptor->configurable) {
  654. // a. Remove the own property with name P from O.
  655. storage_delete(property_name);
  656. // b. Return true.
  657. return true;
  658. }
  659. // 5. Return false.
  660. return false;
  661. }
  662. // 10.1.11 [[OwnPropertyKeys]] ( ), https://tc39.es/ecma262/#sec-ordinary-object-internal-methods-and-internal-slots-ownpropertykeys
  663. MarkedValueList Object::internal_own_property_keys() const
  664. {
  665. auto& vm = this->vm();
  666. // 1. Let keys be a new empty List.
  667. MarkedValueList keys { heap() };
  668. // 2. For each own property key P of O such that P is an array index, in ascending numeric index order, do
  669. for (auto& entry : m_indexed_properties) {
  670. // a. Add P as the last element of keys.
  671. keys.append(js_string(vm, String::number(entry.index())));
  672. }
  673. // 3. For each own property key P of O such that Type(P) is String and P is not an array index, in ascending chronological order of property creation, do
  674. for (auto& it : shape().property_table_ordered()) {
  675. if (it.key.is_string()) {
  676. // a. Add P as the last element of keys.
  677. keys.append(it.key.to_value(vm));
  678. }
  679. }
  680. // 4. For each own property key P of O such that Type(P) is Symbol, in ascending chronological order of property creation, do
  681. for (auto& it : shape().property_table_ordered()) {
  682. if (it.key.is_symbol()) {
  683. // a. Add P as the last element of keys.
  684. keys.append(it.key.to_value(vm));
  685. }
  686. }
  687. // 5. Return keys.
  688. return keys;
  689. }
  690. Optional<ValueAndAttributes> Object::storage_get(PropertyName const& property_name, CallNativeProperty call_native_property) const
  691. {
  692. VERIFY(property_name.is_valid());
  693. Value value;
  694. PropertyAttributes attributes;
  695. if (property_name.is_number()) {
  696. auto value_and_attributes = m_indexed_properties.get(property_name.as_number());
  697. if (!value_and_attributes.has_value())
  698. return {};
  699. value = value_and_attributes->value;
  700. attributes = value_and_attributes->attributes;
  701. } else {
  702. auto metadata = shape().lookup(property_name.to_string_or_symbol());
  703. if (!metadata.has_value())
  704. return {};
  705. value = m_storage[metadata->offset];
  706. attributes = metadata->attributes;
  707. }
  708. if (value.is_native_property() && call_native_property == CallNativeProperty::Yes)
  709. value = call_native_property_getter(value.as_native_property(), this);
  710. return ValueAndAttributes { .value = value, .attributes = attributes };
  711. }
  712. bool Object::storage_has(PropertyName const& property_name) const
  713. {
  714. VERIFY(property_name.is_valid());
  715. if (property_name.is_number())
  716. return m_indexed_properties.has_index(property_name.as_number());
  717. return shape().lookup(property_name.to_string_or_symbol()).has_value();
  718. }
  719. void Object::storage_set(PropertyName const& property_name, ValueAndAttributes const& value_and_attributes)
  720. {
  721. VERIFY(property_name.is_valid());
  722. auto [value, attributes] = value_and_attributes;
  723. if (property_name.is_number()) {
  724. auto index = property_name.as_number();
  725. if (value.is_native_property()) {
  726. m_indexed_properties.put(index, value, attributes);
  727. } else {
  728. auto existing_value = m_indexed_properties.get(index);
  729. if (existing_value.has_value() && existing_value->value.is_native_property())
  730. call_native_property_setter(existing_value->value.as_native_property(), this, value);
  731. else
  732. m_indexed_properties.put(index, value, attributes);
  733. }
  734. return;
  735. }
  736. // NOTE: We disable transitions during initialize(), this makes building common runtime objects significantly faster.
  737. // Transitions are primarily interesting when scripts add properties to objects.
  738. if (!m_transitions_enabled && !m_shape->is_unique()) {
  739. m_shape->add_property_without_transition(property_name, attributes);
  740. m_storage.resize(m_shape->property_count());
  741. m_storage[m_shape->property_count() - 1] = value;
  742. return;
  743. }
  744. auto property_name_string_or_symbol = property_name.to_string_or_symbol();
  745. auto metadata = shape().lookup(property_name_string_or_symbol);
  746. if (!metadata.has_value()) {
  747. if (!m_shape->is_unique() && shape().property_count() > 100) {
  748. // If you add more than 100 properties to an object, let's stop doing
  749. // transitions to avoid filling up the heap with shapes.
  750. ensure_shape_is_unique();
  751. }
  752. if (m_shape->is_unique()) {
  753. m_shape->add_property_to_unique_shape(property_name_string_or_symbol, attributes);
  754. m_storage.resize(m_shape->property_count());
  755. } else if (m_transitions_enabled) {
  756. set_shape(*m_shape->create_put_transition(property_name_string_or_symbol, attributes));
  757. } else {
  758. m_shape->add_property_without_transition(property_name, attributes);
  759. m_storage.resize(m_shape->property_count());
  760. }
  761. metadata = shape().lookup(property_name_string_or_symbol);
  762. VERIFY(metadata.has_value());
  763. }
  764. if (attributes != metadata->attributes) {
  765. if (m_shape->is_unique()) {
  766. m_shape->reconfigure_property_in_unique_shape(property_name_string_or_symbol, attributes);
  767. } else {
  768. set_shape(*m_shape->create_configure_transition(property_name_string_or_symbol, attributes));
  769. }
  770. metadata = shape().lookup(property_name_string_or_symbol);
  771. VERIFY(metadata.has_value());
  772. }
  773. if (value.is_native_property()) {
  774. m_storage[metadata->offset] = value;
  775. } else {
  776. auto existing_value = m_storage[metadata->offset];
  777. if (existing_value.is_native_property())
  778. call_native_property_setter(existing_value.as_native_property(), this, value);
  779. else
  780. m_storage[metadata->offset] = value;
  781. }
  782. }
  783. void Object::storage_delete(PropertyName const& property_name)
  784. {
  785. VERIFY(property_name.is_valid());
  786. VERIFY(storage_has(property_name));
  787. if (property_name.is_number())
  788. return m_indexed_properties.remove(property_name.as_number());
  789. auto metadata = shape().lookup(property_name.to_string_or_symbol());
  790. VERIFY(metadata.has_value());
  791. ensure_shape_is_unique();
  792. shape().remove_property_from_unique_shape(property_name.to_string_or_symbol(), metadata->offset);
  793. m_storage.remove(metadata->offset);
  794. }
  795. void Object::set_shape(Shape& new_shape)
  796. {
  797. m_storage.resize(new_shape.property_count());
  798. m_shape = &new_shape;
  799. }
  800. bool Object::define_native_accessor(PropertyName const& property_name, Function<Value(VM&, GlobalObject&)> getter, Function<Value(VM&, GlobalObject&)> setter, PropertyAttributes attribute)
  801. {
  802. auto& vm = this->vm();
  803. String formatted_property_name;
  804. if (property_name.is_string()) {
  805. formatted_property_name = property_name.as_string();
  806. } else {
  807. formatted_property_name = String::formatted("[{}]", property_name.as_symbol()->description());
  808. }
  809. FunctionObject* getter_function = nullptr;
  810. if (getter) {
  811. auto name = String::formatted("get {}", formatted_property_name);
  812. getter_function = NativeFunction::create(global_object(), name, move(getter));
  813. getter_function->define_property_without_transition(vm.names.length, Value(0), Attribute::Configurable);
  814. if (vm.exception())
  815. return {};
  816. getter_function->define_property_without_transition(vm.names.name, js_string(vm.heap(), name), Attribute::Configurable);
  817. if (vm.exception())
  818. return {};
  819. }
  820. FunctionObject* setter_function = nullptr;
  821. if (setter) {
  822. auto name = String::formatted("set {}", formatted_property_name);
  823. setter_function = NativeFunction::create(global_object(), name, move(setter));
  824. setter_function->define_property_without_transition(vm.names.length, Value(1), Attribute::Configurable);
  825. if (vm.exception())
  826. return {};
  827. setter_function->define_property_without_transition(vm.names.name, js_string(vm.heap(), name), Attribute::Configurable);
  828. if (vm.exception())
  829. return {};
  830. }
  831. return define_accessor(property_name, getter_function, setter_function, attribute);
  832. }
  833. bool Object::define_property_without_transition(PropertyName const& property_name, Value value, PropertyAttributes attributes, bool throw_exceptions)
  834. {
  835. TemporaryChange change(m_transitions_enabled, false);
  836. return define_property(property_name, value, attributes, throw_exceptions);
  837. }
  838. bool Object::define_accessor(PropertyName const& property_name, FunctionObject* getter, FunctionObject* setter, PropertyAttributes attributes, bool throw_exceptions)
  839. {
  840. VERIFY(property_name.is_valid());
  841. auto existing_property = storage_get(property_name).value_or({}).value;
  842. auto* accessor = existing_property.is_accessor() ? &existing_property.as_accessor() : nullptr;
  843. if (!accessor) {
  844. accessor = Accessor::create(vm(), getter, setter);
  845. bool definition_success = define_property(property_name, accessor, attributes, throw_exceptions);
  846. if (vm().exception())
  847. return {};
  848. if (!definition_success)
  849. return false;
  850. } else {
  851. if (getter)
  852. accessor->set_getter(getter);
  853. if (setter)
  854. accessor->set_setter(setter);
  855. }
  856. return true;
  857. }
  858. void Object::ensure_shape_is_unique()
  859. {
  860. if (shape().is_unique())
  861. return;
  862. m_shape = m_shape->create_unique_clone();
  863. }
  864. // Simple side-effect free property lookup, following the prototype chain. Non-standard.
  865. Value Object::get_without_side_effects(const PropertyName& property_name) const
  866. {
  867. auto* object = this;
  868. while (object) {
  869. auto value_and_attributes = object->storage_get(property_name, CallNativeProperty::No);
  870. if (value_and_attributes.has_value())
  871. return value_and_attributes->value;
  872. object = object->prototype();
  873. }
  874. return {};
  875. }
  876. bool Object::define_native_function(PropertyName const& property_name, Function<Value(VM&, GlobalObject&)> native_function, i32 length, PropertyAttributes attribute)
  877. {
  878. auto& vm = this->vm();
  879. String function_name;
  880. if (property_name.is_string()) {
  881. function_name = property_name.as_string();
  882. } else {
  883. function_name = String::formatted("[{}]", property_name.as_symbol()->description());
  884. }
  885. auto* function = NativeFunction::create(global_object(), function_name, move(native_function));
  886. function->define_property_without_transition(vm.names.length, Value(length), Attribute::Configurable);
  887. if (vm.exception())
  888. return {};
  889. function->define_property_without_transition(vm.names.name, js_string(vm.heap(), function_name), Attribute::Configurable);
  890. if (vm.exception())
  891. return {};
  892. return define_property(property_name, function, attribute);
  893. }
  894. bool Object::define_native_property(PropertyName const& property_name, Function<Value(VM&, GlobalObject&)> getter, Function<void(VM&, GlobalObject&, Value)> setter, PropertyAttributes attribute)
  895. {
  896. return define_property(property_name, heap().allocate_without_global_object<NativeProperty>(move(getter), move(setter)), attribute);
  897. }
  898. // 20.1.2.3.1 ObjectDefineProperties ( O, Properties ), https://tc39.es/ecma262/#sec-objectdefineproperties
  899. Object* Object::define_properties(Value properties)
  900. {
  901. auto& vm = this->vm();
  902. auto& global_object = this->global_object();
  903. // 1. Assert: Type(O) is Object.
  904. // 2. Let props be ? ToObject(Properties).
  905. auto* props = properties.to_object(global_object);
  906. if (vm.exception())
  907. return {};
  908. // 3. Let keys be ? props.[[OwnPropertyKeys]]().
  909. auto keys = props->internal_own_property_keys();
  910. if (vm.exception())
  911. return {};
  912. struct NameAndDescriptor {
  913. PropertyName name;
  914. PropertyDescriptor descriptor;
  915. };
  916. // 4. Let descriptors be a new empty List.
  917. Vector<NameAndDescriptor> descriptors;
  918. // 5. For each element nextKey of keys, do
  919. for (auto& next_key : keys) {
  920. auto property_name = PropertyName::from_value(global_object, next_key);
  921. // a. Let propDesc be ? props.[[GetOwnProperty]](nextKey).
  922. auto property_descriptor = props->internal_get_own_property(property_name);
  923. if (vm.exception())
  924. return {};
  925. // b. If propDesc is not undefined and propDesc.[[Enumerable]] is true, then
  926. if (property_descriptor.has_value() && *property_descriptor->enumerable) {
  927. // i. Let descObj be ? Get(props, nextKey).
  928. auto descriptor_object = props->get(property_name);
  929. if (vm.exception())
  930. return {};
  931. // ii. Let desc be ? ToPropertyDescriptor(descObj).
  932. auto descriptor = to_property_descriptor(global_object, descriptor_object);
  933. if (vm.exception())
  934. return {};
  935. // iii. Append the pair (a two element List) consisting of nextKey and desc to the end of descriptors.
  936. descriptors.append({ property_name, descriptor });
  937. }
  938. }
  939. // 6. For each element pair of descriptors, do
  940. for (auto& [name, descriptor] : descriptors) {
  941. // a. Let P be the first element of pair.
  942. // b. Let desc be the second element of pair.
  943. // c. Perform ? DefinePropertyOrThrow(O, P, desc).
  944. define_property_or_throw(name, descriptor);
  945. if (vm.exception())
  946. return {};
  947. }
  948. // 7. Return O.
  949. return this;
  950. }
  951. void Object::visit_edges(Cell::Visitor& visitor)
  952. {
  953. Cell::visit_edges(visitor);
  954. visitor.visit(m_shape);
  955. for (auto& value : m_storage)
  956. visitor.visit(value);
  957. m_indexed_properties.for_each_value([&visitor](auto& value) {
  958. visitor.visit(value);
  959. });
  960. }
  961. // 7.1.1.1 OrdinaryToPrimitive ( O, hint ), https://tc39.es/ecma262/#sec-ordinarytoprimitive
  962. Value Object::ordinary_to_primitive(Value::PreferredType preferred_type) const
  963. {
  964. VERIFY(preferred_type == Value::PreferredType::String || preferred_type == Value::PreferredType::Number);
  965. auto& vm = this->vm();
  966. AK::Array<PropertyName, 2> method_names;
  967. if (preferred_type == Value::PreferredType::String)
  968. method_names = { vm.names.toString, vm.names.valueOf };
  969. else
  970. method_names = { vm.names.valueOf, vm.names.toString };
  971. for (auto& method_name : method_names) {
  972. auto method = get(method_name);
  973. if (vm.exception())
  974. return {};
  975. if (method.is_function()) {
  976. auto result = vm.call(method.as_function(), const_cast<Object*>(this));
  977. if (!result.is_object())
  978. return result;
  979. }
  980. }
  981. vm.throw_exception<TypeError>(global_object(), ErrorType::Convert, "object", preferred_type == Value::PreferredType::String ? "string" : "number");
  982. return {};
  983. }
  984. Value Object::invoke_internal(const StringOrSymbol& property_name, Optional<MarkedValueList> arguments)
  985. {
  986. auto& vm = this->vm();
  987. auto property = get(property_name);
  988. if (vm.exception())
  989. return {};
  990. if (!property.is_function()) {
  991. vm.throw_exception<TypeError>(global_object(), ErrorType::NotAFunction, property.to_string_without_side_effects());
  992. return {};
  993. }
  994. return vm.call(property.as_function(), this, move(arguments));
  995. }
  996. Value Object::call_native_property_getter(NativeProperty& property, Value this_value) const
  997. {
  998. auto& vm = this->vm();
  999. ExecutionContext execution_context;
  1000. if (auto* interpreter = vm.interpreter_if_exists())
  1001. execution_context.current_node = interpreter->current_node();
  1002. execution_context.is_strict_mode = vm.in_strict_mode();
  1003. execution_context.this_value = this_value;
  1004. vm.push_execution_context(execution_context, global_object());
  1005. if (vm.exception())
  1006. return {};
  1007. auto result = property.get(vm, global_object());
  1008. vm.pop_execution_context();
  1009. return result;
  1010. }
  1011. void Object::call_native_property_setter(NativeProperty& property, Value this_value, Value setter_value) const
  1012. {
  1013. auto& vm = this->vm();
  1014. ExecutionContext execution_context;
  1015. if (auto* interpreter = vm.interpreter_if_exists())
  1016. execution_context.current_node = interpreter->current_node();
  1017. execution_context.is_strict_mode = vm.in_strict_mode();
  1018. execution_context.this_value = this_value;
  1019. vm.push_execution_context(execution_context, global_object());
  1020. if (vm.exception())
  1021. return;
  1022. property.set(vm, global_object(), setter_value);
  1023. vm.pop_execution_context();
  1024. }
  1025. }