Object.cpp 34 KB

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