Object.cpp 44 KB

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