Object.cpp 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172
  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(), receiver);
  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. }
  399. if (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. FunctionObject* getter_function { nullptr };
  444. FunctionObject* setter_function { nullptr };
  445. // We should only take previous getters for our own object not from any prototype
  446. auto existing_property = get_own_property(property_name, {}, AllowSideEffects::No).value_or(js_undefined());
  447. if (getter.is_function()) {
  448. getter_function = &getter.as_function();
  449. } else if (!getter.is_undefined()) {
  450. vm.throw_exception<TypeError>(global_object(), ErrorType::AccessorBadField, "get");
  451. return false;
  452. } else if (existing_property.is_accessor()) {
  453. // FIXME: This is a hack, since we store Accessor as a getter & setter tuple value, instead of as separate entries in the property
  454. getter_function = existing_property.as_accessor().getter();
  455. }
  456. if (setter.is_function()) {
  457. setter_function = &setter.as_function();
  458. } else if (!setter.is_undefined()) {
  459. vm.throw_exception<TypeError>(global_object(), ErrorType::AccessorBadField, "set");
  460. return false;
  461. } else if (existing_property.is_accessor()) {
  462. // FIXME: See above
  463. setter_function = existing_property.as_accessor().setter();
  464. }
  465. dbgln_if(OBJECT_DEBUG, "Defining new property {} with accessor descriptor {{ attributes={}, getter={}, setter={} }}", property_name.to_display_string(), attributes, getter, setter);
  466. return define_property(property_name, Accessor::create(vm, getter_function, setter_function), attributes, throw_exceptions);
  467. }
  468. auto value = descriptor.get(vm.names.value);
  469. if (vm.exception())
  470. return {};
  471. if (descriptor.has_property(vm.names.writable)) {
  472. attributes.set_has_writable();
  473. if (descriptor.get(vm.names.writable).value_or(Value(false)).to_boolean())
  474. attributes.set_writable();
  475. if (vm.exception())
  476. return false;
  477. }
  478. if (vm.exception())
  479. return {};
  480. dbgln_if(OBJECT_DEBUG, "Defining new property {} with data descriptor {{ attributes={}, value={} }}", property_name.to_display_string(), attributes, value);
  481. return define_property(property_name, value, attributes, throw_exceptions);
  482. }
  483. bool Object::define_property_without_transition(const PropertyName& property_name, Value value, PropertyAttributes attributes, bool throw_exceptions)
  484. {
  485. TemporaryChange change(m_transitions_enabled, false);
  486. return define_property(property_name, value, attributes, throw_exceptions);
  487. }
  488. bool Object::define_property(const PropertyName& property_name, Value value, PropertyAttributes attributes, bool throw_exceptions)
  489. {
  490. VERIFY(property_name.is_valid());
  491. if (property_name.is_number())
  492. return put_own_property_by_index(property_name.as_number(), value, attributes, PutOwnPropertyMode::DefineProperty, throw_exceptions);
  493. return put_own_property(property_name.to_string_or_symbol(), value, attributes, PutOwnPropertyMode::DefineProperty, throw_exceptions);
  494. }
  495. bool Object::define_native_accessor(PropertyName const& property_name, Function<Value(VM&, GlobalObject&)> getter, Function<Value(VM&, GlobalObject&)> setter, PropertyAttributes attribute)
  496. {
  497. auto& vm = this->vm();
  498. String formatted_property_name;
  499. if (property_name.is_string()) {
  500. formatted_property_name = property_name.as_string();
  501. } else {
  502. formatted_property_name = String::formatted("[{}]", property_name.as_symbol()->description());
  503. }
  504. FunctionObject* getter_function = nullptr;
  505. if (getter) {
  506. auto name = String::formatted("get {}", formatted_property_name);
  507. getter_function = NativeFunction::create(global_object(), name, move(getter));
  508. getter_function->define_property_without_transition(vm.names.length, Value(0), Attribute::Configurable);
  509. if (vm.exception())
  510. return {};
  511. getter_function->define_property_without_transition(vm.names.name, js_string(vm.heap(), name), Attribute::Configurable);
  512. if (vm.exception())
  513. return {};
  514. }
  515. FunctionObject* setter_function = nullptr;
  516. if (setter) {
  517. auto name = String::formatted("set {}", formatted_property_name);
  518. setter_function = NativeFunction::create(global_object(), name, move(setter));
  519. setter_function->define_property_without_transition(vm.names.length, Value(1), Attribute::Configurable);
  520. if (vm.exception())
  521. return {};
  522. setter_function->define_property_without_transition(vm.names.name, js_string(vm.heap(), name), Attribute::Configurable);
  523. if (vm.exception())
  524. return {};
  525. }
  526. return define_accessor(property_name, getter_function, setter_function, attribute);
  527. }
  528. bool Object::define_accessor(const PropertyName& property_name, FunctionObject* getter, FunctionObject* setter, PropertyAttributes attributes, bool throw_exceptions)
  529. {
  530. VERIFY(property_name.is_valid());
  531. auto existing_property = get_own_property(property_name, this, AllowSideEffects::No);
  532. auto* accessor = existing_property.is_accessor() ? &existing_property.as_accessor() : nullptr;
  533. if (!accessor) {
  534. accessor = Accessor::create(vm(), getter, setter);
  535. bool definition_success = define_property(property_name, accessor, attributes, throw_exceptions);
  536. if (vm().exception())
  537. return {};
  538. if (!definition_success)
  539. return false;
  540. } else {
  541. if (getter)
  542. accessor->set_getter(getter);
  543. if (setter)
  544. accessor->set_setter(setter);
  545. }
  546. return true;
  547. }
  548. bool Object::put_own_property(const StringOrSymbol& property_name, Value value, PropertyAttributes attributes, PutOwnPropertyMode mode, bool throw_exceptions)
  549. {
  550. VERIFY(!(mode == PutOwnPropertyMode::Put && value.is_accessor()));
  551. if (value.is_accessor()) {
  552. auto& accessor = value.as_accessor();
  553. if (accessor.getter())
  554. attributes.set_has_getter();
  555. if (accessor.setter())
  556. attributes.set_has_setter();
  557. }
  558. // NOTE: We disable transitions during initialize(), this makes building common runtime objects significantly faster.
  559. // Transitions are primarily interesting when scripts add properties to objects.
  560. if (!m_transitions_enabled && !m_shape->is_unique()) {
  561. m_shape->add_property_without_transition(property_name, attributes);
  562. m_storage.resize(m_shape->property_count());
  563. m_storage[m_shape->property_count() - 1] = value;
  564. return true;
  565. }
  566. auto metadata = shape().lookup(property_name);
  567. bool new_property = !metadata.has_value();
  568. if (!is_extensible() && new_property) {
  569. dbgln_if(OBJECT_DEBUG, "Disallow define_property of non-extensible object");
  570. if (throw_exceptions)
  571. vm().throw_exception<TypeError>(global_object(), ErrorType::NonExtensibleDefine, property_name.to_display_string());
  572. return false;
  573. }
  574. if (new_property) {
  575. if (!m_shape->is_unique() && shape().property_count() > 100) {
  576. // If you add more than 100 properties to an object, let's stop doing
  577. // transitions to avoid filling up the heap with shapes.
  578. ensure_shape_is_unique();
  579. }
  580. if (m_shape->is_unique()) {
  581. m_shape->add_property_to_unique_shape(property_name, attributes);
  582. m_storage.resize(m_shape->property_count());
  583. } else if (m_transitions_enabled) {
  584. set_shape(*m_shape->create_put_transition(property_name, attributes));
  585. } else {
  586. m_shape->add_property_without_transition(property_name, attributes);
  587. m_storage.resize(m_shape->property_count());
  588. }
  589. metadata = shape().lookup(property_name);
  590. VERIFY(metadata.has_value());
  591. }
  592. auto value_here = m_storage[metadata.value().offset];
  593. if (!new_property && mode == PutOwnPropertyMode::DefineProperty && !metadata.value().attributes.is_configurable()) {
  594. if ((attributes.has_configurable() && attributes.is_configurable()) || (attributes.has_enumerable() && attributes.is_enumerable() != metadata.value().attributes.is_enumerable())) {
  595. dbgln_if(OBJECT_DEBUG, "Disallow reconfig of non-configurable property");
  596. if (throw_exceptions)
  597. vm().throw_exception<TypeError>(global_object(), ErrorType::DescChangeNonConfigurable, property_name.to_display_string());
  598. return false;
  599. }
  600. if (value_here.is_accessor() != value.is_accessor()) {
  601. dbgln_if(OBJECT_DEBUG, "Disallow reconfig of non-configurable property");
  602. if (throw_exceptions)
  603. vm().throw_exception<TypeError>(global_object(), ErrorType::DescChangeNonConfigurable, property_name.to_display_string());
  604. return false;
  605. }
  606. if (!value_here.is_accessor() && !metadata.value().attributes.is_writable() && ((attributes.has_writable() && attributes.is_writable()) || (!value.is_empty() && !same_value(value, value_here)))) {
  607. dbgln_if(OBJECT_DEBUG, "Disallow reconfig of non-configurable property");
  608. if (throw_exceptions)
  609. vm().throw_exception<TypeError>(global_object(), ErrorType::DescChangeNonConfigurable, property_name.to_display_string());
  610. return false;
  611. }
  612. 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()))) {
  613. dbgln_if(OBJECT_DEBUG, "Disallow reconfig of non-configurable property");
  614. if (throw_exceptions)
  615. vm().throw_exception<TypeError>(global_object(), ErrorType::DescChangeNonConfigurable, property_name.to_display_string());
  616. return false;
  617. }
  618. }
  619. // FIXME: Instead of adding back existing attributes we should just stop overwriting them
  620. if (!attributes.has_configurable() && metadata.value().attributes.is_configurable()) {
  621. attributes.set_has_configurable();
  622. attributes.set_configurable();
  623. }
  624. if (!attributes.has_enumerable() && metadata.value().attributes.is_enumerable()) {
  625. attributes.set_has_enumerable();
  626. attributes.set_enumerable();
  627. }
  628. if (!value.is_accessor() && !attributes.has_writable() && metadata.value().attributes.is_writable()) {
  629. attributes.set_has_writable();
  630. attributes.set_writable();
  631. }
  632. if (mode == PutOwnPropertyMode::DefineProperty && attributes != metadata.value().attributes) {
  633. if (m_shape->is_unique()) {
  634. m_shape->reconfigure_property_in_unique_shape(property_name, attributes);
  635. } else {
  636. set_shape(*m_shape->create_configure_transition(property_name, attributes));
  637. }
  638. metadata = shape().lookup(property_name);
  639. 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());
  640. }
  641. if (!new_property && mode == PutOwnPropertyMode::Put && !value_here.is_accessor() && !metadata.value().attributes.is_writable()) {
  642. dbgln_if(OBJECT_DEBUG, "Disallow write to non-writable property");
  643. if (throw_exceptions && vm().in_strict_mode())
  644. vm().throw_exception<TypeError>(global_object(), ErrorType::DescWriteNonWritable, property_name.to_display_string());
  645. return false;
  646. }
  647. if (value.is_empty())
  648. return true;
  649. if (value_here.is_native_property()) {
  650. call_native_property_setter(value_here.as_native_property(), this, value);
  651. } else {
  652. m_storage[metadata.value().offset] = value;
  653. }
  654. return true;
  655. }
  656. bool Object::put_own_property_by_index(u32 property_index, Value value, PropertyAttributes attributes, PutOwnPropertyMode mode, bool throw_exceptions)
  657. {
  658. VERIFY(!(mode == PutOwnPropertyMode::Put && value.is_accessor()));
  659. auto existing_property = m_indexed_properties.get(nullptr, property_index, AllowSideEffects::No);
  660. auto new_property = !existing_property.has_value();
  661. if (!is_extensible() && new_property) {
  662. dbgln_if(OBJECT_DEBUG, "Disallow define_property of non-extensible object");
  663. if (throw_exceptions)
  664. vm().throw_exception<TypeError>(global_object(), ErrorType::NonExtensibleDefine, property_index);
  665. return false;
  666. }
  667. if (value.is_accessor()) {
  668. auto& accessor = value.as_accessor();
  669. if (accessor.getter())
  670. attributes.set_has_getter();
  671. if (accessor.setter())
  672. attributes.set_has_setter();
  673. }
  674. if (new_property) {
  675. if (!is_extensible()) {
  676. dbgln_if(OBJECT_DEBUG, "Disallow define_property of non-extensible object");
  677. if (throw_exceptions)
  678. vm().throw_exception<TypeError>(global_object(), ErrorType::NonExtensibleDefine, property_index);
  679. return false;
  680. }
  681. m_indexed_properties.put(this, property_index, value, attributes, mode == PutOwnPropertyMode::Put ? AllowSideEffects::Yes : AllowSideEffects::No);
  682. return true;
  683. }
  684. if (attributes == 0 && value.is_empty())
  685. return true;
  686. PropertyAttributes existing_attributes = existing_property.value().attributes;
  687. auto value_here = existing_property.value().value;
  688. if (mode == PutOwnPropertyMode::DefineProperty && !existing_attributes.is_configurable()) {
  689. if ((attributes.has_configurable() && attributes.is_configurable()) || (attributes.has_enumerable() && attributes.is_enumerable() != existing_attributes.is_enumerable())) {
  690. dbgln_if(OBJECT_DEBUG, "Disallow reconfig of non-configurable property");
  691. if (throw_exceptions)
  692. vm().throw_exception<TypeError>(global_object(), ErrorType::DescChangeNonConfigurable, property_index);
  693. return false;
  694. }
  695. if (value_here.is_accessor() != value.is_accessor()) {
  696. dbgln_if(OBJECT_DEBUG, "Disallow reconfig of non-configurable property");
  697. if (throw_exceptions)
  698. vm().throw_exception<TypeError>(global_object(), ErrorType::DescChangeNonConfigurable, property_index);
  699. return false;
  700. }
  701. if (!value_here.is_accessor() && !existing_attributes.is_writable() && ((attributes.has_writable() && attributes.is_writable()) || (!value.is_empty() && !same_value(value, value_here)))) {
  702. dbgln_if(OBJECT_DEBUG, "Disallow reconfig of non-configurable property");
  703. if (throw_exceptions)
  704. vm().throw_exception<TypeError>(global_object(), ErrorType::DescChangeNonConfigurable, property_index);
  705. return false;
  706. }
  707. 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()))) {
  708. dbgln_if(OBJECT_DEBUG, "Disallow reconfig of non-configurable property");
  709. if (throw_exceptions)
  710. vm().throw_exception<TypeError>(global_object(), ErrorType::DescChangeNonConfigurable, property_index);
  711. return false;
  712. }
  713. }
  714. if (mode == PutOwnPropertyMode::Put && !value_here.is_accessor() && !existing_attributes.is_writable()) {
  715. dbgln_if(OBJECT_DEBUG, "Disallow write to non-writable property");
  716. if (throw_exceptions)
  717. vm().throw_exception<TypeError>(global_object(), ErrorType::DescWriteNonWritable, property_index);
  718. return false;
  719. }
  720. PropertyAttributes combined_attributes = existing_attributes.overwrite(attributes);
  721. if (value.is_empty()) {
  722. if (combined_attributes == existing_attributes) {
  723. return true;
  724. }
  725. value = value_here.value_or(js_undefined());
  726. }
  727. attributes = combined_attributes;
  728. if (value_here.is_native_property()) {
  729. call_native_property_setter(value_here.as_native_property(), this, value);
  730. } else {
  731. m_indexed_properties.put(this, property_index, value, attributes, mode == PutOwnPropertyMode::Put ? AllowSideEffects::Yes : AllowSideEffects::No);
  732. }
  733. return true;
  734. }
  735. bool Object::delete_property(PropertyName const& property_name, bool force_throw_exception)
  736. {
  737. VERIFY(property_name.is_valid());
  738. if (property_name.is_number()) {
  739. if (!m_indexed_properties.remove(property_name.as_number())) {
  740. if (force_throw_exception || vm().in_strict_mode())
  741. vm().throw_exception<TypeError>(global_object(), ErrorType::DescChangeNonConfigurable, property_name.as_number());
  742. return false;
  743. }
  744. return true;
  745. }
  746. auto metadata = shape().lookup(property_name.to_string_or_symbol());
  747. if (!metadata.has_value())
  748. return true;
  749. if (!metadata.value().attributes.is_configurable()) {
  750. if (force_throw_exception || vm().in_strict_mode())
  751. vm().throw_exception<TypeError>(global_object(), ErrorType::DescChangeNonConfigurable, property_name.to_string_or_symbol().to_display_string());
  752. return false;
  753. }
  754. size_t deleted_offset = metadata.value().offset;
  755. ensure_shape_is_unique();
  756. shape().remove_property_from_unique_shape(property_name.to_string_or_symbol(), deleted_offset);
  757. m_storage.remove(deleted_offset);
  758. return true;
  759. }
  760. void Object::ensure_shape_is_unique()
  761. {
  762. if (shape().is_unique())
  763. return;
  764. m_shape = m_shape->create_unique_clone();
  765. }
  766. Value Object::get_by_index(u32 property_index, AllowSideEffects allow_side_effects) const
  767. {
  768. const Object* object = this;
  769. while (object) {
  770. if (is<StringObject>(*object)) {
  771. auto& string = static_cast<const StringObject&>(*object).primitive_string().string();
  772. if (property_index < string.length())
  773. return js_string(heap(), string.substring(property_index, 1));
  774. } else if (static_cast<size_t>(property_index) < object->m_indexed_properties.array_like_size()) {
  775. auto result = object->m_indexed_properties.get(const_cast<Object*>(this), property_index, allow_side_effects);
  776. if (vm().exception())
  777. return {};
  778. if (result.has_value() && !result.value().value.is_empty())
  779. return result.value().value;
  780. }
  781. object = object->prototype();
  782. if (vm().exception())
  783. return {};
  784. }
  785. return {};
  786. }
  787. Value Object::get(const PropertyName& property_name, Value receiver, AllowSideEffects allow_side_effects) const
  788. {
  789. VERIFY(property_name.is_valid());
  790. if (property_name.is_number())
  791. return get_by_index(property_name.as_number(), allow_side_effects);
  792. if (receiver.is_empty())
  793. receiver = Value(this);
  794. const Object* object = this;
  795. while (object) {
  796. auto value = object->get_own_property(property_name, receiver, allow_side_effects);
  797. if (vm().exception())
  798. return {};
  799. if (!value.is_empty())
  800. return value;
  801. object = object->prototype();
  802. if (vm().exception())
  803. return {};
  804. }
  805. return {};
  806. }
  807. Value Object::get_without_side_effects(const PropertyName& property_name) const
  808. {
  809. TemporaryClearException clear_exception(vm());
  810. return get(property_name, {}, AllowSideEffects::No);
  811. }
  812. bool Object::put_by_index(u32 property_index, Value value)
  813. {
  814. VERIFY(!value.is_empty());
  815. // If there's a setter in the prototype chain, we go to the setter.
  816. // Otherwise, it goes in the own property storage.
  817. Object* object = this;
  818. while (object) {
  819. auto existing_value = object->m_indexed_properties.get(nullptr, property_index, AllowSideEffects::No);
  820. if (existing_value.has_value()) {
  821. auto value_here = existing_value.value();
  822. if (value_here.value.is_accessor()) {
  823. value_here.value.as_accessor().call_setter(object, value);
  824. return true;
  825. }
  826. if (value_here.value.is_native_property()) {
  827. // FIXME: Why doesn't put_by_index() receive the receiver value from put()?!
  828. auto receiver = this;
  829. call_native_property_setter(value_here.value.as_native_property(), receiver, value);
  830. return true;
  831. }
  832. }
  833. object = object->prototype();
  834. if (vm().exception())
  835. return {};
  836. }
  837. return put_own_property_by_index(property_index, value, default_attributes, PutOwnPropertyMode::Put, vm().in_strict_mode());
  838. }
  839. bool Object::put(const PropertyName& property_name, Value value, Value receiver)
  840. {
  841. VERIFY(property_name.is_valid());
  842. if (property_name.is_number())
  843. return put_by_index(property_name.as_number(), value);
  844. VERIFY(!value.is_empty());
  845. auto string_or_symbol = property_name.to_string_or_symbol();
  846. if (receiver.is_empty())
  847. receiver = Value(this);
  848. // If there's a setter in the prototype chain, we go to the setter.
  849. // Otherwise, it goes in the own property storage.
  850. Object* object = this;
  851. while (object) {
  852. auto metadata = object->shape().lookup(string_or_symbol);
  853. if (metadata.has_value()) {
  854. auto value_here = object->m_storage[metadata.value().offset];
  855. if (value_here.is_accessor()) {
  856. value_here.as_accessor().call_setter(receiver, value);
  857. return true;
  858. }
  859. if (value_here.is_native_property()) {
  860. call_native_property_setter(value_here.as_native_property(), receiver, value);
  861. return true;
  862. }
  863. }
  864. object = object->prototype();
  865. if (vm().exception())
  866. return false;
  867. }
  868. return put_own_property(string_or_symbol, value, default_attributes, PutOwnPropertyMode::Put, vm().in_strict_mode());
  869. }
  870. bool Object::define_native_function(PropertyName const& property_name, Function<Value(VM&, GlobalObject&)> native_function, i32 length, PropertyAttributes attribute)
  871. {
  872. auto& vm = this->vm();
  873. String function_name;
  874. if (property_name.is_string()) {
  875. function_name = property_name.as_string();
  876. } else {
  877. function_name = String::formatted("[{}]", property_name.as_symbol()->description());
  878. }
  879. auto* function = NativeFunction::create(global_object(), function_name, move(native_function));
  880. function->define_property_without_transition(vm.names.length, Value(length), Attribute::Configurable);
  881. if (vm.exception())
  882. return {};
  883. function->define_property_without_transition(vm.names.name, js_string(vm.heap(), function_name), Attribute::Configurable);
  884. if (vm.exception())
  885. return {};
  886. return define_property(property_name, function, attribute);
  887. }
  888. bool Object::define_native_property(PropertyName const& property_name, Function<Value(VM&, GlobalObject&)> getter, Function<void(VM&, GlobalObject&, Value)> setter, PropertyAttributes attribute)
  889. {
  890. return define_property(property_name, heap().allocate_without_global_object<NativeProperty>(move(getter), move(setter)), attribute);
  891. }
  892. // 20.1.2.3.1 ObjectDefineProperties ( O, Properties ), https://tc39.es/ecma262/#sec-objectdefineproperties
  893. void Object::define_properties(Value properties)
  894. {
  895. auto& vm = this->vm();
  896. auto* props = properties.to_object(global_object());
  897. if (!props)
  898. return;
  899. auto keys = props->get_own_properties(PropertyKind::Key);
  900. if (vm.exception())
  901. return;
  902. struct NameAndDescriptor {
  903. PropertyName name;
  904. PropertyDescriptor descriptor;
  905. };
  906. Vector<NameAndDescriptor> descriptors;
  907. for (auto& key : keys) {
  908. auto property_name = PropertyName::from_value(global_object(), key);
  909. auto property_descriptor = props->get_own_property_descriptor(property_name);
  910. if (property_descriptor.has_value() && property_descriptor->attributes.is_enumerable()) {
  911. auto descriptor_object = props->get(property_name);
  912. if (vm.exception())
  913. return;
  914. if (!descriptor_object.is_object()) {
  915. vm.throw_exception<TypeError>(global_object(), ErrorType::NotAnObject, descriptor_object.to_string_without_side_effects());
  916. return;
  917. }
  918. auto descriptor = PropertyDescriptor::from_dictionary(vm, descriptor_object.as_object());
  919. if (vm.exception())
  920. return;
  921. descriptors.append({ property_name, descriptor });
  922. }
  923. }
  924. for (auto& [name, descriptor] : descriptors) {
  925. // FIXME: The spec has both of this handled by DefinePropertyOrThrow(O, P, desc).
  926. // We should invest some time in improving object property handling, it not being
  927. // super close to the spec makes this and other things unnecessarily complicated.
  928. if (descriptor.is_accessor_descriptor())
  929. define_accessor(name, descriptor.getter, descriptor.setter, descriptor.attributes);
  930. else
  931. define_property(name, descriptor.value, descriptor.attributes);
  932. }
  933. }
  934. void Object::visit_edges(Cell::Visitor& visitor)
  935. {
  936. Cell::visit_edges(visitor);
  937. visitor.visit(m_shape);
  938. for (auto& value : m_storage)
  939. visitor.visit(value);
  940. m_indexed_properties.for_each_value([&visitor](auto& value) {
  941. visitor.visit(value);
  942. });
  943. }
  944. bool Object::has_property(const PropertyName& property_name) const
  945. {
  946. const Object* object = this;
  947. while (object) {
  948. if (object->has_own_property(property_name))
  949. return true;
  950. object = object->prototype();
  951. if (vm().exception())
  952. return false;
  953. }
  954. return false;
  955. }
  956. bool Object::has_own_property(const PropertyName& property_name) const
  957. {
  958. VERIFY(property_name.is_valid());
  959. auto has_indexed_property = [&](u32 index) -> bool {
  960. if (is<StringObject>(*this))
  961. return index < static_cast<const StringObject*>(this)->primitive_string().string().length();
  962. return m_indexed_properties.has_index(index);
  963. };
  964. if (property_name.is_number())
  965. return has_indexed_property(property_name.as_number());
  966. return shape().lookup(property_name.to_string_or_symbol()).has_value();
  967. }
  968. Value Object::ordinary_to_primitive(Value::PreferredType preferred_type) const
  969. {
  970. VERIFY(preferred_type == Value::PreferredType::String || preferred_type == Value::PreferredType::Number);
  971. auto& vm = this->vm();
  972. Vector<FlyString, 2> method_names;
  973. if (preferred_type == Value::PreferredType::String)
  974. method_names = { vm.names.toString.as_string(), vm.names.valueOf.as_string() };
  975. else
  976. method_names = { vm.names.valueOf.as_string(), vm.names.toString.as_string() };
  977. for (auto& method_name : method_names) {
  978. auto method = get(method_name);
  979. if (vm.exception())
  980. return {};
  981. if (method.is_function()) {
  982. auto result = vm.call(method.as_function(), const_cast<Object*>(this));
  983. if (!result.is_object())
  984. return result;
  985. }
  986. }
  987. vm.throw_exception<TypeError>(global_object(), ErrorType::Convert, "object", preferred_type == Value::PreferredType::String ? "string" : "number");
  988. return {};
  989. }
  990. Value Object::invoke_internal(const StringOrSymbol& property_name, Optional<MarkedValueList> arguments)
  991. {
  992. auto& vm = this->vm();
  993. auto property = get(property_name).value_or(js_undefined());
  994. if (vm.exception())
  995. return {};
  996. if (!property.is_function()) {
  997. vm.throw_exception<TypeError>(global_object(), ErrorType::NotAFunction, property.to_string_without_side_effects());
  998. return {};
  999. }
  1000. return vm.call(property.as_function(), this, move(arguments));
  1001. }
  1002. Value Object::call_native_property_getter(NativeProperty& property, Value this_value) const
  1003. {
  1004. auto& vm = this->vm();
  1005. ExecutionContext execution_context;
  1006. if (auto* interpreter = vm.interpreter_if_exists())
  1007. execution_context.current_node = interpreter->current_node();
  1008. execution_context.is_strict_mode = vm.in_strict_mode();
  1009. execution_context.this_value = this_value;
  1010. vm.push_execution_context(execution_context, global_object());
  1011. if (vm.exception())
  1012. return {};
  1013. auto result = property.get(vm, global_object());
  1014. vm.pop_execution_context();
  1015. return result;
  1016. }
  1017. void Object::call_native_property_setter(NativeProperty& property, Value this_value, Value setter_value) const
  1018. {
  1019. auto& vm = this->vm();
  1020. ExecutionContext execution_context;
  1021. if (auto* interpreter = vm.interpreter_if_exists())
  1022. execution_context.current_node = interpreter->current_node();
  1023. execution_context.is_strict_mode = vm.in_strict_mode();
  1024. execution_context.this_value = this_value;
  1025. vm.push_execution_context(execution_context, global_object());
  1026. if (vm.exception())
  1027. return;
  1028. property.set(vm, global_object(), setter_value);
  1029. vm.pop_execution_context();
  1030. }
  1031. }