Object.cpp 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182
  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/Debug.h>
  8. #include <AK/String.h>
  9. #include <AK/TemporaryChange.h>
  10. #include <LibJS/Heap/Heap.h>
  11. #include <LibJS/Interpreter.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/ProxyObject.h>
  20. #include <LibJS/Runtime/Shape.h>
  21. #include <LibJS/Runtime/StringObject.h>
  22. #include <LibJS/Runtime/TemporaryClearException.h>
  23. #include <LibJS/Runtime/Value.h>
  24. namespace JS {
  25. PropertyDescriptor PropertyDescriptor::from_dictionary(VM& vm, const Object& object)
  26. {
  27. PropertyAttributes attributes;
  28. if (object.has_property(vm.names.configurable)) {
  29. attributes.set_has_configurable();
  30. if (object.get(vm.names.configurable).value_or(Value(false)).to_boolean())
  31. attributes.set_configurable();
  32. if (vm.exception())
  33. return {};
  34. }
  35. if (object.has_property(vm.names.enumerable)) {
  36. attributes.set_has_enumerable();
  37. if (object.get(vm.names.enumerable).value_or(Value(false)).to_boolean())
  38. attributes.set_enumerable();
  39. if (vm.exception())
  40. return {};
  41. }
  42. if (object.has_property(vm.names.writable)) {
  43. attributes.set_has_writable();
  44. if (object.get(vm.names.writable).value_or(Value(false)).to_boolean())
  45. attributes.set_writable();
  46. if (vm.exception())
  47. return {};
  48. }
  49. PropertyDescriptor descriptor { attributes, object.get(vm.names.value), nullptr, nullptr };
  50. if (vm.exception())
  51. return {};
  52. auto getter = object.get(vm.names.get);
  53. if (vm.exception())
  54. return {};
  55. if (getter.is_function())
  56. descriptor.getter = &getter.as_function();
  57. if (!getter.is_empty())
  58. descriptor.attributes.set_has_getter();
  59. auto setter = object.get(vm.names.set);
  60. if (vm.exception())
  61. return {};
  62. if (setter.is_function())
  63. descriptor.setter = &setter.as_function();
  64. if (!setter.is_empty())
  65. descriptor.attributes.set_has_setter();
  66. return descriptor;
  67. }
  68. // 10.1.12 OrdinaryObjectCreate ( proto [ , additionalInternalSlotsList ] ), https://tc39.es/ecma262/#sec-ordinaryobjectcreate
  69. Object* Object::create(GlobalObject& global_object, Object* prototype)
  70. {
  71. if (!prototype)
  72. return global_object.heap().allocate<Object>(global_object, *global_object.empty_object_shape());
  73. else if (prototype == global_object.object_prototype())
  74. return global_object.heap().allocate<Object>(global_object, *global_object.new_object_shape());
  75. else
  76. return global_object.heap().allocate<Object>(global_object, *prototype);
  77. }
  78. Object::Object(GlobalObjectTag)
  79. {
  80. // This is the global object
  81. m_shape = heap().allocate_without_global_object<Shape>(*this);
  82. }
  83. Object::Object(ConstructWithoutPrototypeTag, GlobalObject& global_object)
  84. {
  85. m_shape = heap().allocate_without_global_object<Shape>(global_object);
  86. }
  87. Object::Object(Object& prototype)
  88. {
  89. m_shape = prototype.global_object().empty_object_shape();
  90. set_prototype(&prototype);
  91. }
  92. Object::Object(Shape& shape)
  93. : m_shape(&shape)
  94. {
  95. m_storage.resize(shape.property_count());
  96. }
  97. void Object::initialize(GlobalObject&)
  98. {
  99. }
  100. Object::~Object()
  101. {
  102. }
  103. Object* Object::prototype()
  104. {
  105. return shape().prototype();
  106. }
  107. const Object* Object::prototype() const
  108. {
  109. return shape().prototype();
  110. }
  111. // 10.1.2.1 OrdinarySetPrototypeOf ( O, V ), https://tc39.es/ecma262/#sec-ordinarysetprototypeof
  112. bool Object::set_prototype(Object* new_prototype)
  113. {
  114. if (prototype() == new_prototype)
  115. return true;
  116. if (!m_is_extensible)
  117. return false;
  118. auto* prototype = new_prototype;
  119. while (prototype) {
  120. if (prototype == this)
  121. return false;
  122. // NOTE: This is a best-effort implementation of the following step:
  123. // "If p.[[GetPrototypeOf]] is not the ordinary object internal method defined in 10.1.1,
  124. // set done to true."
  125. // We don't have a good way of detecting whether certain virtual Object methods have been
  126. // overridden by a given object, but as ProxyObject is the only one doing that, this check
  127. // does the trick.
  128. if (is<ProxyObject>(prototype))
  129. break;
  130. prototype = prototype->prototype();
  131. }
  132. auto& shape = this->shape();
  133. if (shape.is_unique())
  134. shape.set_prototype_without_transition(new_prototype);
  135. else
  136. m_shape = shape.create_prototype_transition(new_prototype);
  137. return true;
  138. }
  139. bool Object::has_prototype(const Object* prototype) const
  140. {
  141. for (auto* object = this->prototype(); object; object = object->prototype()) {
  142. if (vm().exception())
  143. return false;
  144. if (object == prototype)
  145. return true;
  146. }
  147. return false;
  148. }
  149. bool Object::prevent_extensions()
  150. {
  151. m_is_extensible = false;
  152. return true;
  153. }
  154. // 7.3.15 SetIntegrityLevel ( O, level ), https://tc39.es/ecma262/#sec-setintegritylevel
  155. bool Object::set_integrity_level(IntegrityLevel level)
  156. {
  157. // FIXME: This feels clunky and should get nicer abstractions.
  158. auto update_property = [this](auto& property_name, auto new_attributes) {
  159. if (property_name.is_number()) {
  160. auto value_and_attributes = m_indexed_properties.get(nullptr, property_name.as_number(), AllowSideEffects::No).value();
  161. auto value = value_and_attributes.value;
  162. auto attributes = value_and_attributes.attributes.bits() & new_attributes;
  163. m_indexed_properties.put(nullptr, property_name.as_number(), value, attributes, AllowSideEffects::No);
  164. } else {
  165. auto metadata = shape().lookup(property_name.to_string_or_symbol()).value();
  166. auto attributes = metadata.attributes.bits() & new_attributes;
  167. if (m_shape->is_unique())
  168. m_shape->reconfigure_property_in_unique_shape(property_name.to_string_or_symbol(), attributes);
  169. else
  170. set_shape(*m_shape->create_configure_transition(property_name.to_string_or_symbol(), attributes));
  171. }
  172. };
  173. auto& vm = this->vm();
  174. auto status = prevent_extensions();
  175. if (vm.exception())
  176. return false;
  177. if (!status)
  178. return false;
  179. auto keys = get_own_properties(PropertyKind::Key);
  180. if (vm.exception())
  181. return false;
  182. switch (level) {
  183. case IntegrityLevel::Sealed:
  184. for (auto& key : keys) {
  185. auto property_name = PropertyName::from_value(global_object(), key);
  186. update_property(property_name, ~Attribute::Configurable);
  187. if (vm.exception())
  188. return {};
  189. }
  190. break;
  191. case IntegrityLevel::Frozen:
  192. for (auto& key : keys) {
  193. auto property_name = PropertyName::from_value(global_object(), key);
  194. auto property_descriptor = get_own_property_descriptor(property_name);
  195. if (!property_descriptor.has_value())
  196. continue;
  197. u8 attributes = property_descriptor->is_accessor_descriptor()
  198. ? ~Attribute::Configurable
  199. : ~Attribute::Configurable & ~Attribute::Writable;
  200. update_property(property_name, attributes);
  201. if (vm.exception())
  202. return {};
  203. }
  204. break;
  205. default:
  206. VERIFY_NOT_REACHED();
  207. }
  208. return true;
  209. }
  210. // 7.3.16 TestIntegrityLevel ( O, level ), https://tc39.es/ecma262/#sec-testintegritylevel
  211. bool Object::test_integrity_level(IntegrityLevel level)
  212. {
  213. auto& vm = this->vm();
  214. auto extensible = is_extensible();
  215. if (vm.exception())
  216. return false;
  217. if (extensible)
  218. return false;
  219. auto keys = get_own_properties(PropertyKind::Key);
  220. if (vm.exception())
  221. return false;
  222. for (auto& key : keys) {
  223. auto property_name = PropertyName::from_value(global_object(), key);
  224. auto property_descriptor = get_own_property_descriptor(property_name);
  225. if (!property_descriptor.has_value())
  226. continue;
  227. if (property_descriptor->attributes.is_configurable())
  228. return false;
  229. if (level == IntegrityLevel::Frozen && property_descriptor->is_data_descriptor()) {
  230. if (property_descriptor->attributes.is_writable())
  231. return false;
  232. }
  233. }
  234. return true;
  235. }
  236. Value Object::get_own_property(const PropertyName& property_name, Value receiver, AllowSideEffects allow_side_effects) const
  237. {
  238. VERIFY(property_name.is_valid());
  239. Value value_here;
  240. if (property_name.is_number()) {
  241. auto existing_property = m_indexed_properties.get(nullptr, property_name.as_number(), AllowSideEffects::No);
  242. if (!existing_property.has_value())
  243. return {};
  244. value_here = existing_property.value().value.value_or(js_undefined());
  245. } else {
  246. auto metadata = shape().lookup(property_name.to_string_or_symbol());
  247. if (!metadata.has_value())
  248. return {};
  249. value_here = m_storage[metadata.value().offset].value_or(js_undefined());
  250. }
  251. VERIFY(!value_here.is_empty());
  252. if (allow_side_effects == AllowSideEffects::Yes) {
  253. VERIFY(!receiver.is_empty());
  254. if (value_here.is_accessor())
  255. return value_here.as_accessor().call_getter(receiver);
  256. if (value_here.is_native_property())
  257. return call_native_property_getter(value_here.as_native_property(), this);
  258. }
  259. return value_here;
  260. }
  261. MarkedValueList Object::get_own_properties(PropertyKind kind, bool only_enumerable_properties, GetOwnPropertyReturnType return_type) const
  262. {
  263. MarkedValueList properties(heap());
  264. // FIXME: Support generic iterables
  265. if (is<StringObject>(*this)) {
  266. auto str = static_cast<const StringObject&>(*this).primitive_string().string();
  267. for (size_t i = 0; i < str.length(); ++i) {
  268. if (kind == PropertyKind::Key) {
  269. properties.append(js_string(vm(), String::number(i)));
  270. } else if (kind == PropertyKind::Value) {
  271. properties.append(js_string(vm(), String::formatted("{:c}", str[i])));
  272. } else {
  273. auto* entry_array = Array::create(global_object());
  274. entry_array->define_property(0, js_string(vm(), String::number(i)));
  275. entry_array->define_property(1, js_string(vm(), String::formatted("{:c}", str[i])));
  276. properties.append(entry_array);
  277. }
  278. if (vm().exception())
  279. return MarkedValueList { heap() };
  280. }
  281. }
  282. if (return_type != GetOwnPropertyReturnType::SymbolOnly) {
  283. for (auto& entry : m_indexed_properties) {
  284. auto value_and_attributes = entry.value_and_attributes(const_cast<Object*>(this));
  285. if (only_enumerable_properties && !value_and_attributes.attributes.is_enumerable())
  286. continue;
  287. if (kind == PropertyKind::Key) {
  288. properties.append(js_string(vm(), String::number(entry.index())));
  289. } else if (kind == PropertyKind::Value) {
  290. properties.append(value_and_attributes.value);
  291. } else {
  292. auto* entry_array = Array::create(global_object());
  293. entry_array->define_property(0, js_string(vm(), String::number(entry.index())));
  294. entry_array->define_property(1, value_and_attributes.value);
  295. properties.append(entry_array);
  296. }
  297. if (vm().exception())
  298. return MarkedValueList { heap() };
  299. }
  300. }
  301. auto add_property_to_results = [&](auto& property) {
  302. if (kind == PropertyKind::Key) {
  303. properties.append(property.key.to_value(vm()));
  304. } else if (kind == PropertyKind::Value) {
  305. Value v = get(property.key);
  306. // Value may just have been deleted
  307. if (!v.is_empty())
  308. properties.append(v);
  309. } else {
  310. Value val = get(property.key);
  311. if (val.is_empty())
  312. return;
  313. auto* entry_array = Array::create(global_object());
  314. entry_array->define_property(0, property.key.to_value(vm()));
  315. entry_array->define_property(1, val);
  316. properties.append(entry_array);
  317. }
  318. };
  319. // NOTE: Most things including for..in/of and Object.{keys,values,entries}() use StringOnly, and in those
  320. // cases we won't be iterating the ordered property table twice. We can certainly improve this though.
  321. if (return_type == GetOwnPropertyReturnType::All || return_type == GetOwnPropertyReturnType::StringOnly) {
  322. for (auto& it : shape().property_table_ordered()) {
  323. if (only_enumerable_properties && !it.value.attributes.is_enumerable())
  324. continue;
  325. if (it.key.is_symbol())
  326. continue;
  327. add_property_to_results(it);
  328. if (vm().exception())
  329. return MarkedValueList { heap() };
  330. }
  331. }
  332. if (return_type == GetOwnPropertyReturnType::All || return_type == GetOwnPropertyReturnType::SymbolOnly) {
  333. for (auto& it : shape().property_table_ordered()) {
  334. if (only_enumerable_properties && !it.value.attributes.is_enumerable())
  335. continue;
  336. if (it.key.is_string())
  337. continue;
  338. add_property_to_results(it);
  339. if (vm().exception())
  340. return MarkedValueList { heap() };
  341. }
  342. }
  343. return properties;
  344. }
  345. // 7.3.23 EnumerableOwnPropertyNames ( O, kind ), https://tc39.es/ecma262/#sec-enumerableownpropertynames
  346. MarkedValueList Object::get_enumerable_own_property_names(PropertyKind kind) const
  347. {
  348. return get_own_properties(kind, true, Object::GetOwnPropertyReturnType::StringOnly);
  349. }
  350. Optional<PropertyDescriptor> Object::get_own_property_descriptor(const PropertyName& property_name) const
  351. {
  352. VERIFY(property_name.is_valid());
  353. Value value;
  354. PropertyAttributes attributes;
  355. if (property_name.is_number()) {
  356. auto existing_value = m_indexed_properties.get(nullptr, property_name.as_number(), AllowSideEffects::No);
  357. if (!existing_value.has_value())
  358. return {};
  359. value = existing_value.value().value;
  360. attributes = existing_value.value().attributes;
  361. } else {
  362. auto metadata = shape().lookup(property_name.to_string_or_symbol());
  363. if (!metadata.has_value())
  364. return {};
  365. value = m_storage[metadata.value().offset];
  366. attributes = metadata.value().attributes;
  367. }
  368. PropertyDescriptor descriptor { attributes, {}, nullptr, nullptr };
  369. if (value.is_native_property()) {
  370. auto result = call_native_property_getter(value.as_native_property(), const_cast<Object*>(this));
  371. descriptor.value = result.value_or(js_undefined());
  372. } else if (value.is_accessor()) {
  373. auto& pair = value.as_accessor();
  374. if (pair.getter())
  375. descriptor.getter = pair.getter();
  376. if (pair.setter())
  377. descriptor.setter = pair.setter();
  378. } else {
  379. descriptor.value = value.value_or(js_undefined());
  380. }
  381. return descriptor;
  382. }
  383. // Equivalent to:
  384. // 6.2.5.4 FromPropertyDescriptor ( Desc ), https://tc39.es/ecma262/#sec-frompropertydescriptor
  385. Value Object::get_own_property_descriptor_object(const PropertyName& property_name) const
  386. {
  387. VERIFY(property_name.is_valid());
  388. auto& vm = this->vm();
  389. auto& global_object = this->global_object();
  390. auto descriptor_opt = get_own_property_descriptor(property_name);
  391. if (!descriptor_opt.has_value())
  392. return js_undefined();
  393. auto descriptor = descriptor_opt.value();
  394. auto* descriptor_object = Object::create(global_object, global_object.object_prototype());
  395. if (descriptor.is_data_descriptor()) {
  396. descriptor_object->define_property(vm.names.value, descriptor.value.value_or(js_undefined()));
  397. descriptor_object->define_property(vm.names.writable, Value(descriptor.attributes.is_writable()));
  398. } else {
  399. VERIFY(descriptor.is_accessor_descriptor());
  400. descriptor_object->define_property(vm.names.get, descriptor.getter ? Value(descriptor.getter) : js_undefined());
  401. descriptor_object->define_property(vm.names.set, descriptor.setter ? Value(descriptor.setter) : js_undefined());
  402. }
  403. descriptor_object->define_property(vm.names.enumerable, Value(descriptor.attributes.is_enumerable()));
  404. descriptor_object->define_property(vm.names.configurable, Value(descriptor.attributes.is_configurable()));
  405. return descriptor_object;
  406. }
  407. void Object::set_shape(Shape& new_shape)
  408. {
  409. m_storage.resize(new_shape.property_count());
  410. m_shape = &new_shape;
  411. }
  412. bool Object::define_property(const StringOrSymbol& property_name, const Object& descriptor, bool throw_exceptions)
  413. {
  414. auto& vm = this->vm();
  415. bool is_accessor_property = descriptor.has_property(vm.names.get) || descriptor.has_property(vm.names.set);
  416. PropertyAttributes attributes;
  417. if (descriptor.has_property(vm.names.configurable)) {
  418. attributes.set_has_configurable();
  419. if (descriptor.get(vm.names.configurable).value_or(Value(false)).to_boolean())
  420. attributes.set_configurable();
  421. if (vm.exception())
  422. return false;
  423. }
  424. if (descriptor.has_property(vm.names.enumerable)) {
  425. attributes.set_has_enumerable();
  426. if (descriptor.get(vm.names.enumerable).value_or(Value(false)).to_boolean())
  427. attributes.set_enumerable();
  428. if (vm.exception())
  429. return false;
  430. }
  431. if (is_accessor_property) {
  432. if (descriptor.has_property(vm.names.value) || descriptor.has_property(vm.names.writable)) {
  433. if (throw_exceptions)
  434. vm.throw_exception<TypeError>(global_object(), ErrorType::AccessorValueOrWritable);
  435. return false;
  436. }
  437. auto getter = descriptor.get(vm.names.get).value_or(js_undefined());
  438. if (vm.exception())
  439. return {};
  440. auto setter = descriptor.get(vm.names.set).value_or(js_undefined());
  441. if (vm.exception())
  442. return {};
  443. Function* getter_function { nullptr };
  444. Function* setter_function { nullptr };
  445. auto existing_property = get_without_side_effects(property_name).value_or(js_undefined());
  446. if (getter.is_function()) {
  447. getter_function = &getter.as_function();
  448. } else if (!getter.is_undefined()) {
  449. vm.throw_exception<TypeError>(global_object(), ErrorType::AccessorBadField, "get");
  450. return false;
  451. } else if (existing_property.is_accessor()) {
  452. // FIXME: This is a hack, since we store Accessor as a getter & setter tuple value, instead of as separate entries in the property
  453. getter_function = existing_property.as_accessor().getter();
  454. }
  455. if (setter.is_function()) {
  456. setter_function = &setter.as_function();
  457. } else if (!setter.is_undefined()) {
  458. vm.throw_exception<TypeError>(global_object(), ErrorType::AccessorBadField, "set");
  459. return false;
  460. } else if (existing_property.is_accessor()) {
  461. // FIXME: See above
  462. setter_function = existing_property.as_accessor().setter();
  463. }
  464. dbgln_if(OBJECT_DEBUG, "Defining new property {} with accessor descriptor {{ attributes={}, getter={}, setter={} }}", property_name.to_display_string(), attributes, getter, setter);
  465. return define_property(property_name, Accessor::create(vm, getter_function, setter_function), attributes, throw_exceptions);
  466. }
  467. auto value = descriptor.get(vm.names.value);
  468. if (vm.exception())
  469. return {};
  470. if (descriptor.has_property(vm.names.writable)) {
  471. attributes.set_has_writable();
  472. if (descriptor.get(vm.names.writable).value_or(Value(false)).to_boolean())
  473. attributes.set_writable();
  474. if (vm.exception())
  475. return false;
  476. }
  477. if (vm.exception())
  478. return {};
  479. dbgln_if(OBJECT_DEBUG, "Defining new property {} with data descriptor {{ attributes={}, value={} }}", property_name.to_display_string(), attributes, value);
  480. return define_property(property_name, value, attributes, throw_exceptions);
  481. }
  482. bool Object::define_property_without_transition(const PropertyName& property_name, Value value, PropertyAttributes attributes, bool throw_exceptions)
  483. {
  484. TemporaryChange change(m_transitions_enabled, false);
  485. return define_property(property_name, value, attributes, throw_exceptions);
  486. }
  487. bool Object::define_property(const PropertyName& property_name, Value value, PropertyAttributes attributes, bool throw_exceptions)
  488. {
  489. VERIFY(property_name.is_valid());
  490. if (property_name.is_number())
  491. return put_own_property_by_index(property_name.as_number(), value, attributes, PutOwnPropertyMode::DefineProperty, throw_exceptions);
  492. return put_own_property(property_name.to_string_or_symbol(), value, attributes, PutOwnPropertyMode::DefineProperty, throw_exceptions);
  493. }
  494. bool Object::define_native_accessor(PropertyName const& property_name, AK::Function<Value(VM&, GlobalObject&)> getter, AK::Function<Value(VM&, GlobalObject&)> setter, PropertyAttributes attribute)
  495. {
  496. auto& vm = this->vm();
  497. String formatted_property_name;
  498. if (property_name.is_string()) {
  499. formatted_property_name = property_name.as_string();
  500. } else {
  501. formatted_property_name = String::formatted("[{}]", property_name.as_symbol()->description());
  502. }
  503. Function* getter_function = nullptr;
  504. if (getter) {
  505. auto name = String::formatted("get {}", formatted_property_name);
  506. getter_function = NativeFunction::create(global_object(), name, move(getter));
  507. getter_function->define_property_without_transition(vm.names.length, Value(0), Attribute::Configurable);
  508. if (vm.exception())
  509. return {};
  510. getter_function->define_property_without_transition(vm.names.name, js_string(vm.heap(), name), Attribute::Configurable);
  511. if (vm.exception())
  512. return {};
  513. }
  514. Function* setter_function = nullptr;
  515. if (setter) {
  516. auto name = String::formatted("set {}", formatted_property_name);
  517. setter_function = NativeFunction::create(global_object(), name, move(setter));
  518. setter_function->define_property_without_transition(vm.names.length, Value(1), Attribute::Configurable);
  519. if (vm.exception())
  520. return {};
  521. setter_function->define_property_without_transition(vm.names.name, js_string(vm.heap(), name), Attribute::Configurable);
  522. if (vm.exception())
  523. return {};
  524. }
  525. return define_accessor(property_name, getter_function, setter_function, attribute);
  526. }
  527. bool Object::define_accessor(const PropertyName& property_name, Function* getter, Function* setter, PropertyAttributes attributes, bool throw_exceptions)
  528. {
  529. VERIFY(property_name.is_valid());
  530. auto existing_property = get_own_property(property_name, this, AllowSideEffects::No);
  531. auto* accessor = existing_property.is_accessor() ? &existing_property.as_accessor() : nullptr;
  532. if (!accessor) {
  533. accessor = Accessor::create(vm(), getter, setter);
  534. bool definition_success = define_property(property_name, accessor, attributes, throw_exceptions);
  535. if (vm().exception())
  536. return {};
  537. if (!definition_success)
  538. return false;
  539. } else {
  540. if (getter)
  541. accessor->set_getter(getter);
  542. if (setter)
  543. accessor->set_setter(setter);
  544. }
  545. return true;
  546. }
  547. bool Object::put_own_property(const StringOrSymbol& property_name, Value value, PropertyAttributes attributes, PutOwnPropertyMode mode, bool throw_exceptions)
  548. {
  549. VERIFY(!(mode == PutOwnPropertyMode::Put && value.is_accessor()));
  550. if (value.is_accessor()) {
  551. auto& accessor = value.as_accessor();
  552. if (accessor.getter())
  553. attributes.set_has_getter();
  554. if (accessor.setter())
  555. attributes.set_has_setter();
  556. }
  557. // NOTE: We disable transitions during initialize(), this makes building common runtime objects significantly faster.
  558. // Transitions are primarily interesting when scripts add properties to objects.
  559. if (!m_transitions_enabled && !m_shape->is_unique()) {
  560. m_shape->add_property_without_transition(property_name, attributes);
  561. m_storage.resize(m_shape->property_count());
  562. m_storage[m_shape->property_count() - 1] = value;
  563. return true;
  564. }
  565. auto metadata = shape().lookup(property_name);
  566. bool new_property = !metadata.has_value();
  567. if (!is_extensible() && new_property) {
  568. dbgln_if(OBJECT_DEBUG, "Disallow define_property of non-extensible object");
  569. if (throw_exceptions)
  570. vm().throw_exception<TypeError>(global_object(), ErrorType::NonExtensibleDefine, property_name.to_display_string());
  571. return false;
  572. }
  573. if (new_property) {
  574. if (!m_shape->is_unique() && shape().property_count() > 100) {
  575. // If you add more than 100 properties to an object, let's stop doing
  576. // transitions to avoid filling up the heap with shapes.
  577. ensure_shape_is_unique();
  578. }
  579. if (m_shape->is_unique()) {
  580. m_shape->add_property_to_unique_shape(property_name, attributes);
  581. m_storage.resize(m_shape->property_count());
  582. } else if (m_transitions_enabled) {
  583. set_shape(*m_shape->create_put_transition(property_name, attributes));
  584. } else {
  585. m_shape->add_property_without_transition(property_name, attributes);
  586. m_storage.resize(m_shape->property_count());
  587. }
  588. metadata = shape().lookup(property_name);
  589. VERIFY(metadata.has_value());
  590. }
  591. auto value_here = m_storage[metadata.value().offset];
  592. if (!new_property && mode == PutOwnPropertyMode::DefineProperty && !metadata.value().attributes.is_configurable()) {
  593. if ((attributes.has_configurable() && attributes.is_configurable()) || (attributes.has_enumerable() && attributes.is_enumerable() != metadata.value().attributes.is_enumerable())) {
  594. dbgln_if(OBJECT_DEBUG, "Disallow reconfig of non-configurable property");
  595. if (throw_exceptions)
  596. vm().throw_exception<TypeError>(global_object(), ErrorType::DescChangeNonConfigurable, property_name.to_display_string());
  597. return false;
  598. }
  599. if (value_here.is_accessor() != value.is_accessor()) {
  600. dbgln_if(OBJECT_DEBUG, "Disallow reconfig of non-configurable property");
  601. if (throw_exceptions)
  602. vm().throw_exception<TypeError>(global_object(), ErrorType::DescChangeNonConfigurable, property_name.to_display_string());
  603. return false;
  604. }
  605. if (!value_here.is_accessor() && !metadata.value().attributes.is_writable() && ((attributes.has_writable() && attributes.is_writable()) || (!value.is_empty() && !same_value(value, value_here)))) {
  606. dbgln_if(OBJECT_DEBUG, "Disallow reconfig of non-configurable property");
  607. if (throw_exceptions)
  608. vm().throw_exception<TypeError>(global_object(), ErrorType::DescChangeNonConfigurable, property_name.to_display_string());
  609. return false;
  610. }
  611. if (value_here.is_accessor() && ((attributes.has_setter() && value.as_accessor().setter() != value_here.as_accessor().setter()) || (attributes.has_getter() && value.as_accessor().getter() != value_here.as_accessor().getter()))) {
  612. dbgln_if(OBJECT_DEBUG, "Disallow reconfig of non-configurable property");
  613. if (throw_exceptions)
  614. vm().throw_exception<TypeError>(global_object(), ErrorType::DescChangeNonConfigurable, property_name.to_display_string());
  615. return false;
  616. }
  617. }
  618. // FIXME: Instead of adding back existing attributes we should just stop overwriting them
  619. if (!attributes.has_configurable() && metadata.value().attributes.is_configurable()) {
  620. attributes.set_has_configurable();
  621. attributes.set_configurable();
  622. }
  623. if (!attributes.has_enumerable() && metadata.value().attributes.is_enumerable()) {
  624. attributes.set_has_enumerable();
  625. attributes.set_enumerable();
  626. }
  627. if (!value.is_accessor() && !attributes.has_writable() && metadata.value().attributes.is_writable()) {
  628. attributes.set_has_writable();
  629. attributes.set_writable();
  630. }
  631. if (mode == PutOwnPropertyMode::DefineProperty && attributes != metadata.value().attributes) {
  632. if (m_shape->is_unique()) {
  633. m_shape->reconfigure_property_in_unique_shape(property_name, attributes);
  634. } else {
  635. set_shape(*m_shape->create_configure_transition(property_name, attributes));
  636. }
  637. metadata = shape().lookup(property_name);
  638. dbgln_if(OBJECT_DEBUG, "Reconfigured property {}, new shape says offset is {} and my storage capacity is {}", property_name.to_display_string(), metadata.value().offset, m_storage.size());
  639. }
  640. if (!new_property && mode == PutOwnPropertyMode::Put && !value_here.is_accessor() && !metadata.value().attributes.is_writable()) {
  641. dbgln_if(OBJECT_DEBUG, "Disallow write to non-writable property");
  642. if (throw_exceptions && vm().in_strict_mode())
  643. vm().throw_exception<TypeError>(global_object(), ErrorType::DescWriteNonWritable, property_name.to_display_string());
  644. return false;
  645. }
  646. if (value.is_empty())
  647. return true;
  648. if (value_here.is_native_property()) {
  649. call_native_property_setter(value_here.as_native_property(), this, value);
  650. } else {
  651. m_storage[metadata.value().offset] = value;
  652. }
  653. return true;
  654. }
  655. bool Object::put_own_property_by_index(u32 property_index, Value value, PropertyAttributes attributes, PutOwnPropertyMode mode, bool throw_exceptions)
  656. {
  657. VERIFY(!(mode == PutOwnPropertyMode::Put && value.is_accessor()));
  658. auto existing_property = m_indexed_properties.get(nullptr, property_index, AllowSideEffects::No);
  659. auto new_property = !existing_property.has_value();
  660. if (!is_extensible() && new_property) {
  661. dbgln_if(OBJECT_DEBUG, "Disallow define_property of non-extensible object");
  662. if (throw_exceptions)
  663. vm().throw_exception<TypeError>(global_object(), ErrorType::NonExtensibleDefine, property_index);
  664. return false;
  665. }
  666. if (value.is_accessor()) {
  667. auto& accessor = value.as_accessor();
  668. if (accessor.getter())
  669. attributes.set_has_getter();
  670. if (accessor.setter())
  671. attributes.set_has_setter();
  672. }
  673. if (new_property) {
  674. if (!is_extensible()) {
  675. dbgln_if(OBJECT_DEBUG, "Disallow define_property of non-extensible object");
  676. if (throw_exceptions)
  677. vm().throw_exception<TypeError>(global_object(), ErrorType::NonExtensibleDefine, property_index);
  678. return false;
  679. }
  680. m_indexed_properties.put(this, property_index, value, attributes, mode == PutOwnPropertyMode::Put ? AllowSideEffects::Yes : AllowSideEffects::No);
  681. return true;
  682. }
  683. if (attributes == 0 && value.is_empty())
  684. return true;
  685. PropertyAttributes existing_attributes = existing_property.value().attributes;
  686. auto value_here = existing_property.value().value;
  687. if (mode == PutOwnPropertyMode::DefineProperty && !existing_attributes.is_configurable()) {
  688. if ((attributes.has_configurable() && attributes.is_configurable()) || (attributes.has_enumerable() && attributes.is_enumerable() != existing_attributes.is_enumerable())) {
  689. dbgln_if(OBJECT_DEBUG, "Disallow reconfig of non-configurable property");
  690. if (throw_exceptions)
  691. vm().throw_exception<TypeError>(global_object(), ErrorType::DescChangeNonConfigurable, property_index);
  692. return false;
  693. }
  694. if (value_here.is_accessor() != value.is_accessor()) {
  695. dbgln_if(OBJECT_DEBUG, "Disallow reconfig of non-configurable property");
  696. if (throw_exceptions)
  697. vm().throw_exception<TypeError>(global_object(), ErrorType::DescChangeNonConfigurable, property_index);
  698. return false;
  699. }
  700. if (!value_here.is_accessor() && !existing_attributes.is_writable() && ((attributes.has_writable() && attributes.is_writable()) || (!value.is_empty() && !same_value(value, value_here)))) {
  701. dbgln_if(OBJECT_DEBUG, "Disallow reconfig of non-configurable property");
  702. if (throw_exceptions)
  703. vm().throw_exception<TypeError>(global_object(), ErrorType::DescChangeNonConfigurable, property_index);
  704. return false;
  705. }
  706. if (value_here.is_accessor() && ((attributes.has_setter() && value.as_accessor().setter() != value_here.as_accessor().setter()) || (attributes.has_getter() && value.as_accessor().getter() != value_here.as_accessor().getter()))) {
  707. dbgln_if(OBJECT_DEBUG, "Disallow reconfig of non-configurable property");
  708. if (throw_exceptions)
  709. vm().throw_exception<TypeError>(global_object(), ErrorType::DescChangeNonConfigurable, property_index);
  710. return false;
  711. }
  712. }
  713. if (mode == PutOwnPropertyMode::Put && !value_here.is_accessor() && !existing_attributes.is_writable()) {
  714. dbgln_if(OBJECT_DEBUG, "Disallow write to non-writable property");
  715. if (throw_exceptions)
  716. vm().throw_exception<TypeError>(global_object(), ErrorType::DescWriteNonWritable, property_index);
  717. return false;
  718. }
  719. PropertyAttributes combined_attributes = existing_attributes.overwrite(attributes);
  720. if (value.is_empty()) {
  721. if (combined_attributes == existing_attributes) {
  722. return true;
  723. }
  724. value = value_here.value_or(js_undefined());
  725. }
  726. attributes = combined_attributes;
  727. if (value_here.is_native_property()) {
  728. call_native_property_setter(value_here.as_native_property(), this, value);
  729. } else {
  730. m_indexed_properties.put(this, property_index, value, attributes, mode == PutOwnPropertyMode::Put ? AllowSideEffects::Yes : AllowSideEffects::No);
  731. }
  732. return true;
  733. }
  734. bool Object::delete_property(PropertyName const& property_name, bool force_throw_exception)
  735. {
  736. VERIFY(property_name.is_valid());
  737. if (property_name.is_number()) {
  738. if (!m_indexed_properties.remove(property_name.as_number())) {
  739. if (force_throw_exception || vm().in_strict_mode())
  740. vm().throw_exception<TypeError>(global_object(), ErrorType::DescChangeNonConfigurable, property_name.as_number());
  741. return false;
  742. }
  743. return true;
  744. }
  745. auto metadata = shape().lookup(property_name.to_string_or_symbol());
  746. if (!metadata.has_value())
  747. return true;
  748. if (!metadata.value().attributes.is_configurable()) {
  749. if (force_throw_exception || vm().in_strict_mode())
  750. vm().throw_exception<TypeError>(global_object(), ErrorType::DescChangeNonConfigurable, property_name.to_string_or_symbol().to_display_string());
  751. return false;
  752. }
  753. size_t deleted_offset = metadata.value().offset;
  754. ensure_shape_is_unique();
  755. shape().remove_property_from_unique_shape(property_name.to_string_or_symbol(), deleted_offset);
  756. m_storage.remove(deleted_offset);
  757. return true;
  758. }
  759. void Object::ensure_shape_is_unique()
  760. {
  761. if (shape().is_unique())
  762. return;
  763. m_shape = m_shape->create_unique_clone();
  764. }
  765. Value Object::get_by_index(u32 property_index, AllowSideEffects allow_side_effects) const
  766. {
  767. const Object* object = this;
  768. while (object) {
  769. if (is<StringObject>(*object)) {
  770. auto& string = static_cast<const StringObject&>(*object).primitive_string().string();
  771. if (property_index < string.length())
  772. return js_string(heap(), string.substring(property_index, 1));
  773. } else if (static_cast<size_t>(property_index) < object->m_indexed_properties.array_like_size()) {
  774. auto result = object->m_indexed_properties.get(const_cast<Object*>(this), property_index, allow_side_effects);
  775. if (vm().exception())
  776. return {};
  777. if (result.has_value() && !result.value().value.is_empty())
  778. return result.value().value;
  779. }
  780. object = object->prototype();
  781. if (vm().exception())
  782. return {};
  783. }
  784. return {};
  785. }
  786. Value Object::get(const PropertyName& property_name, Value receiver, AllowSideEffects allow_side_effects) const
  787. {
  788. VERIFY(property_name.is_valid());
  789. if (property_name.is_number())
  790. return get_by_index(property_name.as_number(), allow_side_effects);
  791. if (receiver.is_empty())
  792. receiver = Value(this);
  793. const Object* object = this;
  794. while (object) {
  795. auto value = object->get_own_property(property_name, receiver, allow_side_effects);
  796. if (vm().exception())
  797. return {};
  798. if (!value.is_empty())
  799. return value;
  800. object = object->prototype();
  801. if (vm().exception())
  802. return {};
  803. }
  804. return {};
  805. }
  806. Value Object::get_without_side_effects(const PropertyName& property_name) const
  807. {
  808. TemporaryClearException clear_exception(vm());
  809. return get(property_name, {}, AllowSideEffects::No);
  810. }
  811. bool Object::put_by_index(u32 property_index, Value value)
  812. {
  813. VERIFY(!value.is_empty());
  814. // If there's a setter in the prototype chain, we go to the setter.
  815. // Otherwise, it goes in the own property storage.
  816. Object* object = this;
  817. while (object) {
  818. auto existing_value = object->m_indexed_properties.get(nullptr, property_index, AllowSideEffects::No);
  819. if (existing_value.has_value()) {
  820. auto value_here = existing_value.value();
  821. if (value_here.value.is_accessor()) {
  822. value_here.value.as_accessor().call_setter(object, value);
  823. return true;
  824. }
  825. if (value_here.value.is_native_property()) {
  826. call_native_property_setter(value_here.value.as_native_property(), this, value);
  827. return true;
  828. }
  829. }
  830. object = object->prototype();
  831. if (vm().exception())
  832. return {};
  833. }
  834. return put_own_property_by_index(property_index, value, default_attributes, PutOwnPropertyMode::Put, vm().in_strict_mode());
  835. }
  836. bool Object::put(const PropertyName& property_name, Value value, Value receiver)
  837. {
  838. VERIFY(property_name.is_valid());
  839. if (property_name.is_number())
  840. return put_by_index(property_name.as_number(), value);
  841. VERIFY(!value.is_empty());
  842. auto string_or_symbol = property_name.to_string_or_symbol();
  843. if (receiver.is_empty())
  844. receiver = Value(this);
  845. // If there's a setter in the prototype chain, we go to the setter.
  846. // Otherwise, it goes in the own property storage.
  847. Object* object = this;
  848. while (object) {
  849. auto metadata = object->shape().lookup(string_or_symbol);
  850. if (metadata.has_value()) {
  851. auto value_here = object->m_storage[metadata.value().offset];
  852. if (value_here.is_accessor()) {
  853. value_here.as_accessor().call_setter(receiver, value);
  854. return true;
  855. }
  856. if (value_here.is_native_property()) {
  857. call_native_property_setter(value_here.as_native_property(), this, value);
  858. return true;
  859. }
  860. }
  861. object = object->prototype();
  862. if (vm().exception())
  863. return false;
  864. }
  865. return put_own_property(string_or_symbol, value, default_attributes, PutOwnPropertyMode::Put, vm().in_strict_mode());
  866. }
  867. bool Object::define_native_function(PropertyName const& property_name, AK::Function<Value(VM&, GlobalObject&)> native_function, i32 length, PropertyAttributes attribute)
  868. {
  869. auto& vm = this->vm();
  870. String function_name;
  871. if (property_name.is_string()) {
  872. function_name = property_name.as_string();
  873. } else {
  874. function_name = String::formatted("[{}]", property_name.as_symbol()->description());
  875. }
  876. auto* function = NativeFunction::create(global_object(), function_name, move(native_function));
  877. function->define_property_without_transition(vm.names.length, Value(length), Attribute::Configurable);
  878. if (vm.exception())
  879. return {};
  880. function->define_property_without_transition(vm.names.name, js_string(vm.heap(), function_name), Attribute::Configurable);
  881. if (vm.exception())
  882. return {};
  883. return define_property(property_name, function, attribute);
  884. }
  885. bool Object::define_native_property(PropertyName const& property_name, AK::Function<Value(VM&, GlobalObject&)> getter, AK::Function<void(VM&, GlobalObject&, Value)> setter, PropertyAttributes attribute)
  886. {
  887. return define_property(property_name, heap().allocate_without_global_object<NativeProperty>(move(getter), move(setter)), attribute);
  888. }
  889. // 20.1.2.3.1 ObjectDefineProperties ( O, Properties ), https://tc39.es/ecma262/#sec-objectdefineproperties
  890. void Object::define_properties(Value properties)
  891. {
  892. auto& vm = this->vm();
  893. auto* props = properties.to_object(global_object());
  894. if (!props)
  895. return;
  896. auto keys = props->get_own_properties(PropertyKind::Key);
  897. if (vm.exception())
  898. return;
  899. struct NameAndDescriptor {
  900. PropertyName name;
  901. PropertyDescriptor descriptor;
  902. };
  903. Vector<NameAndDescriptor> descriptors;
  904. for (auto& key : keys) {
  905. auto property_name = PropertyName::from_value(global_object(), key);
  906. auto property_descriptor = props->get_own_property_descriptor(property_name);
  907. if (property_descriptor.has_value() && property_descriptor->attributes.is_enumerable()) {
  908. auto descriptor_object = props->get(property_name);
  909. if (vm.exception())
  910. return;
  911. if (!descriptor_object.is_object()) {
  912. vm.throw_exception<TypeError>(global_object(), ErrorType::NotAnObject, descriptor_object.to_string_without_side_effects());
  913. return;
  914. }
  915. auto descriptor = PropertyDescriptor::from_dictionary(vm, descriptor_object.as_object());
  916. if (vm.exception())
  917. return;
  918. descriptors.append({ property_name, descriptor });
  919. }
  920. }
  921. for (auto& [name, descriptor] : descriptors) {
  922. // FIXME: The spec has both of this handled by DefinePropertyOrThrow(O, P, desc).
  923. // We should invest some time in improving object property handling, it not being
  924. // super close to the spec makes this and other things unnecessarily complicated.
  925. if (descriptor.is_accessor_descriptor())
  926. define_accessor(name, descriptor.getter, descriptor.setter, descriptor.attributes);
  927. else
  928. define_property(name, descriptor.value, descriptor.attributes);
  929. }
  930. }
  931. void Object::visit_edges(Cell::Visitor& visitor)
  932. {
  933. Cell::visit_edges(visitor);
  934. visitor.visit(m_shape);
  935. for (auto& value : m_storage)
  936. visitor.visit(value);
  937. m_indexed_properties.for_each_value([&visitor](auto& value) {
  938. visitor.visit(value);
  939. });
  940. }
  941. bool Object::has_property(const PropertyName& property_name) const
  942. {
  943. const Object* object = this;
  944. while (object) {
  945. if (object->has_own_property(property_name))
  946. return true;
  947. object = object->prototype();
  948. if (vm().exception())
  949. return false;
  950. }
  951. return false;
  952. }
  953. bool Object::has_own_property(const PropertyName& property_name) const
  954. {
  955. VERIFY(property_name.is_valid());
  956. auto has_indexed_property = [&](u32 index) -> bool {
  957. if (is<StringObject>(*this))
  958. return index < static_cast<const StringObject*>(this)->primitive_string().string().length();
  959. return m_indexed_properties.has_index(index);
  960. };
  961. if (property_name.is_number())
  962. return has_indexed_property(property_name.as_number());
  963. return shape().lookup(property_name.to_string_or_symbol()).has_value();
  964. }
  965. Value Object::ordinary_to_primitive(Value::PreferredType preferred_type) const
  966. {
  967. VERIFY(preferred_type == Value::PreferredType::String || preferred_type == Value::PreferredType::Number);
  968. auto& vm = this->vm();
  969. Vector<FlyString, 2> method_names;
  970. if (preferred_type == Value::PreferredType::String)
  971. method_names = { vm.names.toString.as_string(), vm.names.valueOf.as_string() };
  972. else
  973. method_names = { vm.names.valueOf.as_string(), vm.names.toString.as_string() };
  974. for (auto& method_name : method_names) {
  975. auto method = get(method_name);
  976. if (vm.exception())
  977. return {};
  978. if (method.is_function()) {
  979. auto result = vm.call(method.as_function(), const_cast<Object*>(this));
  980. if (!result.is_object())
  981. return result;
  982. }
  983. }
  984. vm.throw_exception<TypeError>(global_object(), ErrorType::Convert, "object", preferred_type == Value::PreferredType::String ? "string" : "number");
  985. return {};
  986. }
  987. // 20.5.8.1 InstallErrorCause ( O, options ), https://tc39.es/proposal-error-cause/#sec-errorobjects-install-error-cause
  988. void Object::install_error_cause(Value options)
  989. {
  990. auto& vm = this->vm();
  991. if (!options.is_object())
  992. return;
  993. auto& options_object = options.as_object();
  994. if (!options_object.has_property(vm.names.cause))
  995. return;
  996. auto cause = options_object.get(vm.names.cause).value_or(js_undefined());
  997. if (vm.exception())
  998. return;
  999. define_property(vm.names.cause, cause, Attribute::Writable | Attribute::Configurable);
  1000. }
  1001. Value Object::invoke_internal(const StringOrSymbol& property_name, Optional<MarkedValueList> arguments)
  1002. {
  1003. auto& vm = this->vm();
  1004. auto property = get(property_name).value_or(js_undefined());
  1005. if (vm.exception())
  1006. return {};
  1007. if (!property.is_function()) {
  1008. vm.throw_exception<TypeError>(global_object(), ErrorType::NotAFunction, property.to_string_without_side_effects());
  1009. return {};
  1010. }
  1011. return vm.call(property.as_function(), this, move(arguments));
  1012. }
  1013. Value Object::call_native_property_getter(NativeProperty& property, Value this_value) const
  1014. {
  1015. auto& vm = this->vm();
  1016. CallFrame call_frame;
  1017. if (auto* interpreter = vm.interpreter_if_exists())
  1018. call_frame.current_node = interpreter->current_node();
  1019. call_frame.is_strict_mode = vm.in_strict_mode();
  1020. call_frame.this_value = this_value;
  1021. vm.push_call_frame(call_frame, global_object());
  1022. if (vm.exception())
  1023. return {};
  1024. auto result = property.get(vm, global_object());
  1025. vm.pop_call_frame();
  1026. return result;
  1027. }
  1028. void Object::call_native_property_setter(NativeProperty& property, Value this_value, Value setter_value) const
  1029. {
  1030. auto& vm = this->vm();
  1031. CallFrame call_frame;
  1032. if (auto* interpreter = vm.interpreter_if_exists())
  1033. call_frame.current_node = interpreter->current_node();
  1034. call_frame.is_strict_mode = vm.in_strict_mode();
  1035. call_frame.this_value = this_value;
  1036. vm.push_call_frame(call_frame, global_object());
  1037. if (vm.exception())
  1038. return;
  1039. property.set(vm, global_object(), setter_value);
  1040. vm.pop_call_frame();
  1041. }
  1042. }