Object.cpp 34 KB

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