Object.cpp 43 KB

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