Object.cpp 37 KB

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