Object.cpp 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2020-2021, Linus Groh <mail@linusgroh.de>
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form must reproduce the above copyright notice,
  13. * this list of conditions and the following disclaimer in the documentation
  14. * and/or other materials provided with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  17. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  20. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  22. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  23. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  24. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  25. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include <AK/Debug.h>
  28. #include <AK/String.h>
  29. #include <AK/TemporaryChange.h>
  30. #include <LibJS/Heap/Heap.h>
  31. #include <LibJS/Interpreter.h>
  32. #include <LibJS/Runtime/Accessor.h>
  33. #include <LibJS/Runtime/Array.h>
  34. #include <LibJS/Runtime/Error.h>
  35. #include <LibJS/Runtime/GlobalObject.h>
  36. #include <LibJS/Runtime/NativeFunction.h>
  37. #include <LibJS/Runtime/NativeProperty.h>
  38. #include <LibJS/Runtime/Object.h>
  39. #include <LibJS/Runtime/Shape.h>
  40. #include <LibJS/Runtime/StringObject.h>
  41. #include <LibJS/Runtime/Value.h>
  42. namespace JS {
  43. PropertyDescriptor PropertyDescriptor::from_dictionary(VM& vm, const Object& object)
  44. {
  45. PropertyAttributes attributes;
  46. if (object.has_property(vm.names.configurable)) {
  47. attributes.set_has_configurable();
  48. if (object.get(vm.names.configurable).value_or(Value(false)).to_boolean())
  49. attributes.set_configurable();
  50. if (vm.exception())
  51. return {};
  52. }
  53. if (object.has_property(vm.names.enumerable)) {
  54. attributes.set_has_enumerable();
  55. if (object.get(vm.names.enumerable).value_or(Value(false)).to_boolean())
  56. attributes.set_enumerable();
  57. if (vm.exception())
  58. return {};
  59. }
  60. if (object.has_property(vm.names.writable)) {
  61. attributes.set_has_writable();
  62. if (object.get(vm.names.writable).value_or(Value(false)).to_boolean())
  63. attributes.set_writable();
  64. if (vm.exception())
  65. return {};
  66. }
  67. PropertyDescriptor descriptor { attributes, object.get(vm.names.value), nullptr, nullptr };
  68. if (vm.exception())
  69. return {};
  70. auto getter = object.get(vm.names.get);
  71. if (vm.exception())
  72. return {};
  73. if (getter.is_function())
  74. descriptor.getter = &getter.as_function();
  75. auto setter = object.get(vm.names.set);
  76. if (vm.exception())
  77. return {};
  78. if (setter.is_function())
  79. descriptor.setter = &setter.as_function();
  80. return descriptor;
  81. }
  82. Object* Object::create_empty(GlobalObject& global_object)
  83. {
  84. return global_object.heap().allocate<Object>(global_object, *global_object.new_object_shape());
  85. }
  86. Object::Object(GlobalObjectTag)
  87. {
  88. // This is the global object
  89. m_shape = heap().allocate_without_global_object<Shape>(*this);
  90. }
  91. Object::Object(ConstructWithoutPrototypeTag, GlobalObject& global_object)
  92. {
  93. m_shape = heap().allocate_without_global_object<Shape>(global_object);
  94. }
  95. Object::Object(Object& prototype)
  96. {
  97. m_shape = prototype.global_object().empty_object_shape();
  98. set_prototype(&prototype);
  99. }
  100. Object::Object(Shape& shape)
  101. : m_shape(&shape)
  102. {
  103. m_storage.resize(shape.property_count());
  104. }
  105. void Object::initialize(GlobalObject&)
  106. {
  107. }
  108. Object::~Object()
  109. {
  110. }
  111. Object* Object::prototype()
  112. {
  113. return shape().prototype();
  114. }
  115. const Object* Object::prototype() const
  116. {
  117. return shape().prototype();
  118. }
  119. bool Object::set_prototype(Object* new_prototype)
  120. {
  121. if (prototype() == new_prototype)
  122. return true;
  123. if (!m_is_extensible)
  124. return false;
  125. if (shape().is_unique()) {
  126. shape().set_prototype_without_transition(new_prototype);
  127. return true;
  128. }
  129. m_shape = m_shape->create_prototype_transition(new_prototype);
  130. return true;
  131. }
  132. bool Object::has_prototype(const Object* prototype) const
  133. {
  134. for (auto* object = this->prototype(); object; object = object->prototype()) {
  135. if (vm().exception())
  136. return false;
  137. if (object == prototype)
  138. return true;
  139. }
  140. return false;
  141. }
  142. bool Object::prevent_extensions()
  143. {
  144. m_is_extensible = false;
  145. return true;
  146. }
  147. Value Object::get_own_property(const PropertyName& property_name, Value receiver) const
  148. {
  149. VERIFY(property_name.is_valid());
  150. VERIFY(!receiver.is_empty());
  151. Value value_here;
  152. if (property_name.is_number()) {
  153. auto existing_property = m_indexed_properties.get(nullptr, property_name.as_number(), false);
  154. if (!existing_property.has_value())
  155. return {};
  156. value_here = existing_property.value().value.value_or(js_undefined());
  157. } else {
  158. auto metadata = shape().lookup(property_name.to_string_or_symbol());
  159. if (!metadata.has_value())
  160. return {};
  161. value_here = m_storage[metadata.value().offset].value_or(js_undefined());
  162. }
  163. VERIFY(!value_here.is_empty());
  164. if (value_here.is_accessor())
  165. return value_here.as_accessor().call_getter(receiver);
  166. if (value_here.is_native_property())
  167. return call_native_property_getter(value_here.as_native_property(), receiver);
  168. return value_here;
  169. }
  170. MarkedValueList Object::get_own_properties(PropertyKind kind, bool only_enumerable_properties, GetOwnPropertyReturnType return_type) const
  171. {
  172. MarkedValueList properties(heap());
  173. // FIXME: Support generic iterables
  174. if (is<StringObject>(*this)) {
  175. auto str = static_cast<const StringObject&>(*this).primitive_string().string();
  176. for (size_t i = 0; i < str.length(); ++i) {
  177. if (kind == PropertyKind::Key) {
  178. properties.append(js_string(vm(), String::number(i)));
  179. } else if (kind == PropertyKind::Value) {
  180. properties.append(js_string(vm(), String::formatted("{:c}", str[i])));
  181. } else {
  182. auto* entry_array = Array::create(global_object());
  183. entry_array->define_property(0, js_string(vm(), String::number(i)));
  184. entry_array->define_property(1, js_string(vm(), String::formatted("{:c}", str[i])));
  185. properties.append(entry_array);
  186. }
  187. if (vm().exception())
  188. return MarkedValueList { heap() };
  189. }
  190. return properties;
  191. }
  192. for (auto& entry : m_indexed_properties) {
  193. auto value_and_attributes = entry.value_and_attributes(const_cast<Object*>(this));
  194. if (only_enumerable_properties && !value_and_attributes.attributes.is_enumerable())
  195. continue;
  196. if (kind == PropertyKind::Key) {
  197. properties.append(js_string(vm(), String::number(entry.index())));
  198. } else if (kind == PropertyKind::Value) {
  199. properties.append(value_and_attributes.value);
  200. } else {
  201. auto* entry_array = Array::create(global_object());
  202. entry_array->define_property(0, js_string(vm(), String::number(entry.index())));
  203. entry_array->define_property(1, value_and_attributes.value);
  204. properties.append(entry_array);
  205. }
  206. if (vm().exception())
  207. return MarkedValueList { heap() };
  208. }
  209. auto add_property_to_results = [&](auto& property) {
  210. if (kind == PropertyKind::Key) {
  211. properties.append(property.key.to_value(vm()));
  212. } else if (kind == PropertyKind::Value) {
  213. properties.append(get(property.key));
  214. } else {
  215. auto* entry_array = Array::create(global_object());
  216. entry_array->define_property(0, property.key.to_value(vm()));
  217. entry_array->define_property(1, get(property.key));
  218. properties.append(entry_array);
  219. }
  220. };
  221. // NOTE: Most things including for..in/of and Object.{keys,values,entries}() use StringOnly, and in those
  222. // cases we won't be iterating the ordered property table twice. We can certainly improve this though.
  223. if (return_type == GetOwnPropertyReturnType::All || return_type == GetOwnPropertyReturnType::StringOnly) {
  224. for (auto& it : shape().property_table_ordered()) {
  225. if (only_enumerable_properties && !it.value.attributes.is_enumerable())
  226. continue;
  227. if (it.key.is_symbol())
  228. continue;
  229. add_property_to_results(it);
  230. if (vm().exception())
  231. return MarkedValueList { heap() };
  232. }
  233. }
  234. if (return_type == GetOwnPropertyReturnType::All || return_type == GetOwnPropertyReturnType::SymbolOnly) {
  235. for (auto& it : shape().property_table_ordered()) {
  236. if (only_enumerable_properties && !it.value.attributes.is_enumerable())
  237. continue;
  238. if (it.key.is_string())
  239. continue;
  240. add_property_to_results(it);
  241. if (vm().exception())
  242. return MarkedValueList { heap() };
  243. }
  244. }
  245. return properties;
  246. }
  247. // 7.3.23 EnumerableOwnPropertyNames, https://tc39.es/ecma262/#sec-enumerableownpropertynames
  248. MarkedValueList Object::get_enumerable_own_property_names(PropertyKind kind) const
  249. {
  250. return get_own_properties(kind, true, Object::GetOwnPropertyReturnType::StringOnly);
  251. }
  252. Optional<PropertyDescriptor> Object::get_own_property_descriptor(const PropertyName& property_name) const
  253. {
  254. VERIFY(property_name.is_valid());
  255. Value value;
  256. PropertyAttributes attributes;
  257. if (property_name.is_number()) {
  258. auto existing_value = m_indexed_properties.get(nullptr, property_name.as_number(), false);
  259. if (!existing_value.has_value())
  260. return {};
  261. value = existing_value.value().value;
  262. attributes = existing_value.value().attributes;
  263. attributes = default_attributes;
  264. } else {
  265. auto metadata = shape().lookup(property_name.to_string_or_symbol());
  266. if (!metadata.has_value())
  267. return {};
  268. value = m_storage[metadata.value().offset];
  269. if (vm().exception())
  270. return {};
  271. attributes = metadata.value().attributes;
  272. }
  273. PropertyDescriptor descriptor { attributes, {}, nullptr, nullptr };
  274. if (value.is_native_property()) {
  275. auto result = call_native_property_getter(value.as_native_property(), const_cast<Object*>(this));
  276. descriptor.value = result.value_or(js_undefined());
  277. } else if (value.is_accessor()) {
  278. auto& pair = value.as_accessor();
  279. if (pair.getter())
  280. descriptor.getter = pair.getter();
  281. if (pair.setter())
  282. descriptor.setter = pair.setter();
  283. } else {
  284. descriptor.value = value.value_or(js_undefined());
  285. }
  286. return descriptor;
  287. }
  288. Value Object::get_own_property_descriptor_object(const PropertyName& property_name) const
  289. {
  290. VERIFY(property_name.is_valid());
  291. auto& vm = this->vm();
  292. auto descriptor_opt = get_own_property_descriptor(property_name);
  293. if (!descriptor_opt.has_value())
  294. return js_undefined();
  295. auto descriptor = descriptor_opt.value();
  296. auto* descriptor_object = Object::create_empty(global_object());
  297. descriptor_object->define_property(vm.names.enumerable, Value(descriptor.attributes.is_enumerable()));
  298. if (vm.exception())
  299. return {};
  300. descriptor_object->define_property(vm.names.configurable, Value(descriptor.attributes.is_configurable()));
  301. if (vm.exception())
  302. return {};
  303. if (descriptor.is_data_descriptor()) {
  304. descriptor_object->define_property(vm.names.value, descriptor.value.value_or(js_undefined()));
  305. if (vm.exception())
  306. return {};
  307. descriptor_object->define_property(vm.names.writable, Value(descriptor.attributes.is_writable()));
  308. if (vm.exception())
  309. return {};
  310. } else if (descriptor.is_accessor_descriptor()) {
  311. if (descriptor.getter) {
  312. descriptor_object->define_property(vm.names.get, Value(descriptor.getter));
  313. if (vm.exception())
  314. return {};
  315. }
  316. if (descriptor.setter) {
  317. descriptor_object->define_property(vm.names.set, Value(descriptor.setter));
  318. if (vm.exception())
  319. return {};
  320. }
  321. }
  322. return descriptor_object;
  323. }
  324. void Object::set_shape(Shape& new_shape)
  325. {
  326. m_storage.resize(new_shape.property_count());
  327. m_shape = &new_shape;
  328. }
  329. bool Object::define_property(const StringOrSymbol& property_name, const Object& descriptor, bool throw_exceptions)
  330. {
  331. auto& vm = this->vm();
  332. bool is_accessor_property = descriptor.has_property(vm.names.get) || descriptor.has_property(vm.names.set);
  333. PropertyAttributes attributes;
  334. if (descriptor.has_property(vm.names.configurable)) {
  335. attributes.set_has_configurable();
  336. if (descriptor.get(vm.names.configurable).value_or(Value(false)).to_boolean())
  337. attributes.set_configurable();
  338. if (vm.exception())
  339. return false;
  340. }
  341. if (descriptor.has_property(vm.names.enumerable)) {
  342. attributes.set_has_enumerable();
  343. if (descriptor.get(vm.names.enumerable).value_or(Value(false)).to_boolean())
  344. attributes.set_enumerable();
  345. if (vm.exception())
  346. return false;
  347. }
  348. if (is_accessor_property) {
  349. if (descriptor.has_property(vm.names.value) || descriptor.has_property(vm.names.writable)) {
  350. if (throw_exceptions)
  351. vm.throw_exception<TypeError>(global_object(), ErrorType::AccessorValueOrWritable);
  352. return false;
  353. }
  354. auto getter = descriptor.get(vm.names.get).value_or(js_undefined());
  355. if (vm.exception())
  356. return {};
  357. auto setter = descriptor.get(vm.names.set).value_or(js_undefined());
  358. if (vm.exception())
  359. return {};
  360. Function* getter_function { nullptr };
  361. Function* setter_function { nullptr };
  362. if (getter.is_function()) {
  363. getter_function = &getter.as_function();
  364. } else if (!getter.is_undefined()) {
  365. vm.throw_exception<TypeError>(global_object(), ErrorType::AccessorBadField, "get");
  366. return false;
  367. }
  368. if (setter.is_function()) {
  369. setter_function = &setter.as_function();
  370. } else if (!setter.is_undefined()) {
  371. vm.throw_exception<TypeError>(global_object(), ErrorType::AccessorBadField, "set");
  372. return false;
  373. }
  374. #if OBJECT_DEBUG
  375. dbgln("Defining new property {} with accessor descriptor {{ attributes={}, getter={}, setter={} }}", property_name.to_display_string(), attributes, getter, setter);
  376. #endif
  377. return define_property(property_name, Accessor::create(vm, getter_function, setter_function), attributes, throw_exceptions);
  378. }
  379. auto value = descriptor.get(vm.names.value);
  380. if (vm.exception())
  381. return {};
  382. if (descriptor.has_property(vm.names.writable)) {
  383. attributes.set_has_writable();
  384. if (descriptor.get(vm.names.writable).value_or(Value(false)).to_boolean())
  385. attributes.set_writable();
  386. if (vm.exception())
  387. return false;
  388. }
  389. if (vm.exception())
  390. return {};
  391. #if OBJECT_DEBUG
  392. dbgln("Defining new property {} with data descriptor {{ attributes={}, value={} }}", property_name.to_display_string(), attributes, value);
  393. #endif
  394. return define_property(property_name, value, attributes, throw_exceptions);
  395. }
  396. bool Object::define_property_without_transition(const PropertyName& property_name, Value value, PropertyAttributes attributes, bool throw_exceptions)
  397. {
  398. TemporaryChange change(m_transitions_enabled, false);
  399. return define_property(property_name, value, attributes, throw_exceptions);
  400. }
  401. bool Object::define_property(const PropertyName& property_name, Value value, PropertyAttributes attributes, bool throw_exceptions)
  402. {
  403. VERIFY(property_name.is_valid());
  404. if (property_name.is_number())
  405. return put_own_property_by_index(property_name.as_number(), value, attributes, PutOwnPropertyMode::DefineProperty, throw_exceptions);
  406. if (property_name.is_string()) {
  407. i32 property_index = property_name.as_string().to_int().value_or(-1);
  408. if (property_index >= 0)
  409. return put_own_property_by_index(property_index, value, attributes, PutOwnPropertyMode::DefineProperty, throw_exceptions);
  410. }
  411. return put_own_property(property_name.to_string_or_symbol(), value, attributes, PutOwnPropertyMode::DefineProperty, throw_exceptions);
  412. }
  413. bool Object::define_accessor(const PropertyName& property_name, Function& getter_or_setter, bool is_getter, PropertyAttributes attributes, bool throw_exceptions)
  414. {
  415. VERIFY(property_name.is_valid());
  416. Accessor* accessor { nullptr };
  417. auto property_metadata = shape().lookup(property_name.to_string_or_symbol());
  418. if (property_metadata.has_value()) {
  419. auto existing_property = get_direct(property_metadata.value().offset);
  420. if (existing_property.is_accessor())
  421. accessor = &existing_property.as_accessor();
  422. }
  423. if (!accessor) {
  424. accessor = Accessor::create(vm(), nullptr, nullptr);
  425. bool definition_success = define_property(property_name, accessor, attributes, throw_exceptions);
  426. if (vm().exception())
  427. return {};
  428. if (!definition_success)
  429. return false;
  430. }
  431. if (is_getter)
  432. accessor->set_getter(&getter_or_setter);
  433. else
  434. accessor->set_setter(&getter_or_setter);
  435. return true;
  436. }
  437. bool Object::put_own_property(const StringOrSymbol& property_name, Value value, PropertyAttributes attributes, PutOwnPropertyMode mode, bool throw_exceptions)
  438. {
  439. VERIFY(!(mode == PutOwnPropertyMode::Put && value.is_accessor()));
  440. if (value.is_accessor()) {
  441. auto& accessor = value.as_accessor();
  442. if (accessor.getter())
  443. attributes.set_has_getter();
  444. if (accessor.setter())
  445. attributes.set_has_setter();
  446. }
  447. // NOTE: We disable transitions during initialize(), this makes building common runtime objects significantly faster.
  448. // Transitions are primarily interesting when scripts add properties to objects.
  449. if (!m_transitions_enabled && !m_shape->is_unique()) {
  450. m_shape->add_property_without_transition(property_name, attributes);
  451. m_storage.resize(m_shape->property_count());
  452. m_storage[m_shape->property_count() - 1] = value;
  453. return true;
  454. }
  455. auto metadata = shape().lookup(property_name);
  456. bool new_property = !metadata.has_value();
  457. if (!is_extensible() && new_property) {
  458. #if OBJECT_DEBUG
  459. dbgln("Disallow define_property of non-extensible object");
  460. #endif
  461. if (throw_exceptions && vm().in_strict_mode())
  462. vm().throw_exception<TypeError>(global_object(), ErrorType::NonExtensibleDefine, property_name.to_display_string());
  463. return false;
  464. }
  465. if (new_property) {
  466. if (!m_shape->is_unique() && shape().property_count() > 100) {
  467. // If you add more than 100 properties to an object, let's stop doing
  468. // transitions to avoid filling up the heap with shapes.
  469. ensure_shape_is_unique();
  470. }
  471. if (m_shape->is_unique()) {
  472. m_shape->add_property_to_unique_shape(property_name, attributes);
  473. m_storage.resize(m_shape->property_count());
  474. } else if (m_transitions_enabled) {
  475. set_shape(*m_shape->create_put_transition(property_name, attributes));
  476. } else {
  477. m_shape->add_property_without_transition(property_name, attributes);
  478. m_storage.resize(m_shape->property_count());
  479. }
  480. metadata = shape().lookup(property_name);
  481. VERIFY(metadata.has_value());
  482. }
  483. if (!new_property && mode == PutOwnPropertyMode::DefineProperty && !metadata.value().attributes.is_configurable() && attributes != metadata.value().attributes) {
  484. #if OBJECT_DEBUG
  485. dbgln("Disallow reconfig of non-configurable property");
  486. #endif
  487. if (throw_exceptions)
  488. vm().throw_exception<TypeError>(global_object(), ErrorType::DescChangeNonConfigurable, property_name.to_display_string());
  489. return false;
  490. }
  491. if (mode == PutOwnPropertyMode::DefineProperty && attributes != metadata.value().attributes) {
  492. if (m_shape->is_unique()) {
  493. m_shape->reconfigure_property_in_unique_shape(property_name, attributes);
  494. } else {
  495. set_shape(*m_shape->create_configure_transition(property_name, attributes));
  496. }
  497. metadata = shape().lookup(property_name);
  498. #if OBJECT_DEBUG
  499. dbgln("Reconfigured property {}, new shape says offset is {} and my storage capacity is {}", property_name.to_display_string(), metadata.value().offset, m_storage.size());
  500. #endif
  501. }
  502. auto value_here = m_storage[metadata.value().offset];
  503. if (!new_property && mode == PutOwnPropertyMode::Put && !value_here.is_accessor() && !metadata.value().attributes.is_writable()) {
  504. #if OBJECT_DEBUG
  505. dbgln("Disallow write to non-writable property");
  506. #endif
  507. return false;
  508. }
  509. if (value.is_empty())
  510. return true;
  511. if (value_here.is_native_property()) {
  512. call_native_property_setter(value_here.as_native_property(), this, value);
  513. } else {
  514. m_storage[metadata.value().offset] = value;
  515. }
  516. return true;
  517. }
  518. bool Object::put_own_property_by_index(u32 property_index, Value value, PropertyAttributes attributes, PutOwnPropertyMode mode, bool throw_exceptions)
  519. {
  520. VERIFY(!(mode == PutOwnPropertyMode::Put && value.is_accessor()));
  521. auto existing_property = m_indexed_properties.get(nullptr, property_index, false);
  522. auto new_property = !existing_property.has_value();
  523. if (!is_extensible() && new_property) {
  524. #if OBJECT_DEBUG
  525. dbgln("Disallow define_property of non-extensible object");
  526. #endif
  527. if (throw_exceptions && vm().in_strict_mode())
  528. vm().throw_exception<TypeError>(global_object(), ErrorType::NonExtensibleDefine, property_index);
  529. return false;
  530. }
  531. if (value.is_accessor()) {
  532. auto& accessor = value.as_accessor();
  533. if (accessor.getter())
  534. attributes.set_has_getter();
  535. if (accessor.setter())
  536. attributes.set_has_setter();
  537. }
  538. PropertyAttributes existing_attributes = new_property ? 0 : existing_property.value().attributes;
  539. if (!new_property && mode == PutOwnPropertyMode::DefineProperty && !existing_attributes.is_configurable() && attributes != existing_attributes) {
  540. #if OBJECT_DEBUG
  541. dbgln("Disallow reconfig of non-configurable property");
  542. #endif
  543. if (throw_exceptions)
  544. vm().throw_exception<TypeError>(global_object(), ErrorType::DescChangeNonConfigurable, property_index);
  545. return false;
  546. }
  547. auto value_here = new_property ? Value() : existing_property.value().value;
  548. if (!new_property && mode == PutOwnPropertyMode::Put && !value_here.is_accessor() && !existing_attributes.is_writable()) {
  549. #if OBJECT_DEBUG
  550. dbgln("Disallow write to non-writable property");
  551. #endif
  552. return false;
  553. }
  554. if (value.is_empty())
  555. return true;
  556. if (value_here.is_native_property()) {
  557. call_native_property_setter(value_here.as_native_property(), this, value);
  558. } else {
  559. m_indexed_properties.put(this, property_index, value, attributes, mode == PutOwnPropertyMode::Put);
  560. }
  561. return true;
  562. }
  563. Value Object::delete_property(const PropertyName& property_name)
  564. {
  565. VERIFY(property_name.is_valid());
  566. if (property_name.is_number())
  567. return Value(m_indexed_properties.remove(property_name.as_number()));
  568. if (property_name.is_string()) {
  569. i32 property_index = property_name.as_string().to_int().value_or(-1);
  570. if (property_index >= 0)
  571. return Value(m_indexed_properties.remove(property_index));
  572. }
  573. auto metadata = shape().lookup(property_name.to_string_or_symbol());
  574. if (!metadata.has_value())
  575. return Value(true);
  576. if (!metadata.value().attributes.is_configurable())
  577. return Value(false);
  578. size_t deleted_offset = metadata.value().offset;
  579. ensure_shape_is_unique();
  580. shape().remove_property_from_unique_shape(property_name.to_string_or_symbol(), deleted_offset);
  581. m_storage.remove(deleted_offset);
  582. return Value(true);
  583. }
  584. void Object::ensure_shape_is_unique()
  585. {
  586. if (shape().is_unique())
  587. return;
  588. m_shape = m_shape->create_unique_clone();
  589. }
  590. Value Object::get_by_index(u32 property_index) const
  591. {
  592. const Object* object = this;
  593. while (object) {
  594. if (is<StringObject>(*this)) {
  595. auto& string = static_cast<const StringObject*>(this)->primitive_string().string();
  596. if (property_index < string.length())
  597. return js_string(heap(), string.substring(property_index, 1));
  598. return js_undefined();
  599. }
  600. if (static_cast<size_t>(property_index) < object->m_indexed_properties.array_like_size()) {
  601. auto result = object->m_indexed_properties.get(const_cast<Object*>(this), property_index);
  602. if (vm().exception())
  603. return {};
  604. if (result.has_value() && !result.value().value.is_empty())
  605. return result.value().value;
  606. return {};
  607. }
  608. object = object->prototype();
  609. if (vm().exception())
  610. return {};
  611. }
  612. return {};
  613. }
  614. Value Object::get(const PropertyName& property_name, Value receiver) const
  615. {
  616. VERIFY(property_name.is_valid());
  617. if (property_name.is_number())
  618. return get_by_index(property_name.as_number());
  619. if (property_name.is_string()) {
  620. auto property_string = property_name.to_string();
  621. i32 property_index = property_string.to_int().value_or(-1);
  622. if (property_index >= 0)
  623. return get_by_index(property_index);
  624. }
  625. if (receiver.is_empty())
  626. receiver = Value(this);
  627. const Object* object = this;
  628. while (object) {
  629. auto value = object->get_own_property(property_name, receiver);
  630. if (vm().exception())
  631. return {};
  632. if (!value.is_empty())
  633. return value;
  634. object = object->prototype();
  635. if (vm().exception())
  636. return {};
  637. }
  638. return {};
  639. }
  640. bool Object::put_by_index(u32 property_index, Value value)
  641. {
  642. VERIFY(!value.is_empty());
  643. // If there's a setter in the prototype chain, we go to the setter.
  644. // Otherwise, it goes in the own property storage.
  645. Object* object = this;
  646. while (object) {
  647. auto existing_value = object->m_indexed_properties.get(nullptr, property_index, false);
  648. if (existing_value.has_value()) {
  649. auto value_here = existing_value.value();
  650. if (value_here.value.is_accessor()) {
  651. value_here.value.as_accessor().call_setter(object, value);
  652. return true;
  653. }
  654. if (value_here.value.is_native_property()) {
  655. // FIXME: Why doesn't put_by_index() receive the receiver value from put()?!
  656. auto receiver = this;
  657. call_native_property_setter(value_here.value.as_native_property(), receiver, value);
  658. return true;
  659. }
  660. }
  661. object = object->prototype();
  662. if (vm().exception())
  663. return {};
  664. }
  665. return put_own_property_by_index(property_index, value, default_attributes, PutOwnPropertyMode::Put);
  666. }
  667. bool Object::put(const PropertyName& property_name, Value value, Value receiver)
  668. {
  669. VERIFY(property_name.is_valid());
  670. if (property_name.is_number())
  671. return put_by_index(property_name.as_number(), value);
  672. VERIFY(!value.is_empty());
  673. if (property_name.is_string()) {
  674. auto& property_string = property_name.as_string();
  675. i32 property_index = property_string.to_int().value_or(-1);
  676. if (property_index >= 0)
  677. return put_by_index(property_index, value);
  678. }
  679. auto string_or_symbol = property_name.to_string_or_symbol();
  680. if (receiver.is_empty())
  681. receiver = Value(this);
  682. // If there's a setter in the prototype chain, we go to the setter.
  683. // Otherwise, it goes in the own property storage.
  684. Object* object = this;
  685. while (object) {
  686. auto metadata = object->shape().lookup(string_or_symbol);
  687. if (metadata.has_value()) {
  688. auto value_here = object->m_storage[metadata.value().offset];
  689. if (value_here.is_accessor()) {
  690. value_here.as_accessor().call_setter(receiver, value);
  691. return true;
  692. }
  693. if (value_here.is_native_property()) {
  694. call_native_property_setter(value_here.as_native_property(), receiver, value);
  695. return true;
  696. }
  697. }
  698. object = object->prototype();
  699. if (vm().exception())
  700. return false;
  701. }
  702. return put_own_property(string_or_symbol, value, default_attributes, PutOwnPropertyMode::Put);
  703. }
  704. bool Object::define_native_function(const StringOrSymbol& property_name, AK::Function<Value(VM&, GlobalObject&)> native_function, i32 length, PropertyAttributes attribute)
  705. {
  706. auto& vm = this->vm();
  707. String function_name;
  708. if (property_name.is_string()) {
  709. function_name = property_name.as_string();
  710. } else {
  711. function_name = String::formatted("[{}]", property_name.as_symbol()->description());
  712. }
  713. auto* function = NativeFunction::create(global_object(), function_name, move(native_function));
  714. function->define_property_without_transition(vm.names.length, Value(length), Attribute::Configurable);
  715. if (vm.exception())
  716. return {};
  717. function->define_property_without_transition(vm.names.name, js_string(vm.heap(), function_name), Attribute::Configurable);
  718. if (vm.exception())
  719. return {};
  720. return define_property(property_name, function, attribute);
  721. }
  722. bool Object::define_native_property(const StringOrSymbol& property_name, AK::Function<Value(VM&, GlobalObject&)> getter, AK::Function<void(VM&, GlobalObject&, Value)> setter, PropertyAttributes attribute)
  723. {
  724. return define_property(property_name, heap().allocate_without_global_object<NativeProperty>(move(getter), move(setter)), attribute);
  725. }
  726. void Object::visit_edges(Cell::Visitor& visitor)
  727. {
  728. Cell::visit_edges(visitor);
  729. visitor.visit(m_shape);
  730. for (auto& value : m_storage)
  731. visitor.visit(value);
  732. m_indexed_properties.for_each_value([&visitor](auto& value) {
  733. visitor.visit(value);
  734. });
  735. }
  736. bool Object::has_property(const PropertyName& property_name) const
  737. {
  738. const Object* object = this;
  739. while (object) {
  740. if (object->has_own_property(property_name))
  741. return true;
  742. object = object->prototype();
  743. if (vm().exception())
  744. return false;
  745. }
  746. return false;
  747. }
  748. bool Object::has_own_property(const PropertyName& property_name) const
  749. {
  750. VERIFY(property_name.is_valid());
  751. auto has_indexed_property = [&](u32 index) -> bool {
  752. if (is<StringObject>(*this))
  753. return index < static_cast<const StringObject*>(this)->primitive_string().string().length();
  754. return m_indexed_properties.has_index(index);
  755. };
  756. if (property_name.is_number())
  757. return has_indexed_property(property_name.as_number());
  758. if (property_name.is_string()) {
  759. i32 property_index = property_name.as_string().to_int().value_or(-1);
  760. if (property_index >= 0)
  761. return has_indexed_property(property_index);
  762. }
  763. return shape().lookup(property_name.to_string_or_symbol()).has_value();
  764. }
  765. Value Object::ordinary_to_primitive(Value::PreferredType preferred_type) const
  766. {
  767. VERIFY(preferred_type == Value::PreferredType::String || preferred_type == Value::PreferredType::Number);
  768. auto& vm = this->vm();
  769. Vector<FlyString, 2> method_names;
  770. if (preferred_type == Value::PreferredType::String)
  771. method_names = { vm.names.toString, vm.names.valueOf };
  772. else
  773. method_names = { vm.names.valueOf, vm.names.toString };
  774. for (auto& method_name : method_names) {
  775. auto method = get(method_name);
  776. if (vm.exception())
  777. return {};
  778. if (method.is_function()) {
  779. auto result = vm.call(method.as_function(), const_cast<Object*>(this));
  780. if (!result.is_object())
  781. return result;
  782. }
  783. }
  784. vm.throw_exception<TypeError>(global_object(), ErrorType::Convert, "object", preferred_type == Value::PreferredType::String ? "string" : "number");
  785. return {};
  786. }
  787. Value Object::invoke_internal(const StringOrSymbol& property_name, Optional<MarkedValueList> arguments)
  788. {
  789. auto& vm = this->vm();
  790. auto property = get(property_name).value_or(js_undefined());
  791. if (vm.exception())
  792. return {};
  793. if (!property.is_function()) {
  794. vm.throw_exception<TypeError>(global_object(), ErrorType::NotAFunction, property.to_string_without_side_effects());
  795. return {};
  796. }
  797. return vm.call(property.as_function(), this, move(arguments));
  798. }
  799. Value Object::call_native_property_getter(NativeProperty& property, Value this_value) const
  800. {
  801. auto& vm = this->vm();
  802. CallFrame call_frame;
  803. if (auto* interpreter = vm.interpreter_if_exists())
  804. call_frame.current_node = interpreter->current_node();
  805. call_frame.is_strict_mode = vm.in_strict_mode();
  806. call_frame.this_value = this_value;
  807. vm.push_call_frame(call_frame, global_object());
  808. if (vm.exception())
  809. return {};
  810. auto result = property.get(vm, global_object());
  811. vm.pop_call_frame();
  812. return result;
  813. }
  814. void Object::call_native_property_setter(NativeProperty& property, Value this_value, Value setter_value) const
  815. {
  816. auto& vm = this->vm();
  817. CallFrame call_frame;
  818. if (auto* interpreter = vm.interpreter_if_exists())
  819. call_frame.current_node = interpreter->current_node();
  820. call_frame.is_strict_mode = vm.in_strict_mode();
  821. call_frame.this_value = this_value;
  822. vm.push_call_frame(call_frame, global_object());
  823. if (vm.exception())
  824. return;
  825. property.set(vm, global_object(), setter_value);
  826. vm.pop_call_frame();
  827. }
  828. }