Object.cpp 39 KB

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