Object.cpp 44 KB

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