Object.cpp 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926
  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::formatted("{: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::formatted("{: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. dbgln("Defining new property {} with accessor descriptor {{ attributes={}, getter={}, setter={} }}", property_name.to_display_string(), attributes, getter, setter);
  356. #endif
  357. return define_property(property_name, Accessor::create(vm, getter_function, setter_function), attributes, throw_exceptions);
  358. }
  359. auto value = descriptor.get(vm.names.value);
  360. if (vm.exception())
  361. return {};
  362. if (descriptor.has_property(vm.names.writable)) {
  363. attributes.set_has_writable();
  364. if (descriptor.get(vm.names.writable).value_or(Value(false)).to_boolean())
  365. attributes.set_writable();
  366. if (vm.exception())
  367. return false;
  368. }
  369. if (vm.exception())
  370. return {};
  371. #ifdef OBJECT_DEBUG
  372. dbgln("Defining new property {} with data descriptor {{ attributes={}, value={} }}", property_name.to_display_string(), attributes, value);
  373. #endif
  374. return define_property(property_name, value, attributes, throw_exceptions);
  375. }
  376. bool Object::define_property_without_transition(const PropertyName& property_name, Value value, PropertyAttributes attributes, bool throw_exceptions)
  377. {
  378. TemporaryChange change(m_transitions_enabled, false);
  379. return define_property(property_name, value, attributes, throw_exceptions);
  380. }
  381. bool Object::define_property(const PropertyName& property_name, Value value, PropertyAttributes attributes, bool throw_exceptions)
  382. {
  383. ASSERT(property_name.is_valid());
  384. if (property_name.is_number())
  385. return put_own_property_by_index(*this, property_name.as_number(), value, attributes, PutOwnPropertyMode::DefineProperty, throw_exceptions);
  386. if (property_name.is_string()) {
  387. i32 property_index = property_name.as_string().to_int().value_or(-1);
  388. if (property_index >= 0)
  389. return put_own_property_by_index(*this, property_index, value, attributes, PutOwnPropertyMode::DefineProperty, throw_exceptions);
  390. }
  391. return put_own_property(*this, property_name.to_string_or_symbol(), value, attributes, PutOwnPropertyMode::DefineProperty, throw_exceptions);
  392. }
  393. bool Object::define_accessor(const PropertyName& property_name, Function& getter_or_setter, bool is_getter, PropertyAttributes attributes, bool throw_exceptions)
  394. {
  395. ASSERT(property_name.is_valid());
  396. Accessor* accessor { nullptr };
  397. auto property_metadata = shape().lookup(property_name.to_string_or_symbol());
  398. if (property_metadata.has_value()) {
  399. auto existing_property = get_direct(property_metadata.value().offset);
  400. if (existing_property.is_accessor())
  401. accessor = &existing_property.as_accessor();
  402. }
  403. if (!accessor) {
  404. accessor = Accessor::create(vm(), nullptr, nullptr);
  405. bool definition_success = define_property(property_name, accessor, attributes, throw_exceptions);
  406. if (vm().exception())
  407. return {};
  408. if (!definition_success)
  409. return false;
  410. }
  411. if (is_getter)
  412. accessor->set_getter(&getter_or_setter);
  413. else
  414. accessor->set_setter(&getter_or_setter);
  415. return true;
  416. }
  417. bool Object::put_own_property(Object& this_object, const StringOrSymbol& property_name, Value value, PropertyAttributes attributes, PutOwnPropertyMode mode, bool throw_exceptions)
  418. {
  419. ASSERT(!(mode == PutOwnPropertyMode::Put && value.is_accessor()));
  420. if (value.is_accessor()) {
  421. auto& accessor = value.as_accessor();
  422. if (accessor.getter())
  423. attributes.set_has_getter();
  424. if (accessor.setter())
  425. attributes.set_has_setter();
  426. }
  427. // NOTE: We disable transitions during initialize(), this makes building common runtime objects significantly faster.
  428. // Transitions are primarily interesting when scripts add properties to objects.
  429. if (!m_transitions_enabled && !m_shape->is_unique()) {
  430. m_shape->add_property_without_transition(property_name, attributes);
  431. m_storage.resize(m_shape->property_count());
  432. m_storage[m_shape->property_count() - 1] = value;
  433. return true;
  434. }
  435. auto metadata = shape().lookup(property_name);
  436. bool new_property = !metadata.has_value();
  437. if (!is_extensible() && new_property) {
  438. #ifdef OBJECT_DEBUG
  439. dbgln("Disallow define_property of non-extensible object");
  440. #endif
  441. if (throw_exceptions && vm().in_strict_mode())
  442. vm().throw_exception<TypeError>(global_object(), ErrorType::NonExtensibleDefine, property_name.to_display_string());
  443. return false;
  444. }
  445. if (new_property) {
  446. if (!m_shape->is_unique() && shape().property_count() > 100) {
  447. // If you add more than 100 properties to an object, let's stop doing
  448. // transitions to avoid filling up the heap with shapes.
  449. ensure_shape_is_unique();
  450. }
  451. if (m_shape->is_unique()) {
  452. m_shape->add_property_to_unique_shape(property_name, attributes);
  453. m_storage.resize(m_shape->property_count());
  454. } else if (m_transitions_enabled) {
  455. set_shape(*m_shape->create_put_transition(property_name, attributes));
  456. } else {
  457. m_shape->add_property_without_transition(property_name, attributes);
  458. m_storage.resize(m_shape->property_count());
  459. }
  460. metadata = shape().lookup(property_name);
  461. ASSERT(metadata.has_value());
  462. }
  463. if (!new_property && mode == PutOwnPropertyMode::DefineProperty && !metadata.value().attributes.is_configurable() && attributes != metadata.value().attributes) {
  464. #ifdef OBJECT_DEBUG
  465. dbgln("Disallow reconfig of non-configurable property");
  466. #endif
  467. if (throw_exceptions)
  468. vm().throw_exception<TypeError>(global_object(), ErrorType::DescChangeNonConfigurable, property_name.to_display_string());
  469. return false;
  470. }
  471. if (mode == PutOwnPropertyMode::DefineProperty && attributes != metadata.value().attributes) {
  472. if (m_shape->is_unique()) {
  473. m_shape->reconfigure_property_in_unique_shape(property_name, attributes);
  474. } else {
  475. set_shape(*m_shape->create_configure_transition(property_name, attributes));
  476. }
  477. metadata = shape().lookup(property_name);
  478. #ifdef OBJECT_DEBUG
  479. dbgln("Reconfigured property {}, new shape says offset is {} and my storage capacity is {}", property_name.to_display_string(), metadata.value().offset, m_storage.size());
  480. #endif
  481. }
  482. auto value_here = m_storage[metadata.value().offset];
  483. if (!new_property && mode == PutOwnPropertyMode::Put && !value_here.is_accessor() && !metadata.value().attributes.is_writable()) {
  484. #ifdef OBJECT_DEBUG
  485. dbgln("Disallow write to non-writable property");
  486. #endif
  487. return false;
  488. }
  489. if (value.is_empty())
  490. return true;
  491. if (value_here.is_native_property()) {
  492. call_native_property_setter(value_here.as_native_property(), &this_object, value);
  493. } else {
  494. m_storage[metadata.value().offset] = value;
  495. }
  496. return true;
  497. }
  498. bool Object::put_own_property_by_index(Object& this_object, u32 property_index, Value value, PropertyAttributes attributes, PutOwnPropertyMode mode, bool throw_exceptions)
  499. {
  500. ASSERT(!(mode == PutOwnPropertyMode::Put && value.is_accessor()));
  501. auto existing_property = m_indexed_properties.get(nullptr, property_index, false);
  502. auto new_property = !existing_property.has_value();
  503. if (!is_extensible() && new_property) {
  504. #ifdef OBJECT_DEBUG
  505. dbgln("Disallow define_property of non-extensible object");
  506. #endif
  507. if (throw_exceptions && vm().in_strict_mode())
  508. vm().throw_exception<TypeError>(global_object(), ErrorType::NonExtensibleDefine, property_index);
  509. return false;
  510. }
  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. PropertyAttributes existing_attributes = new_property ? 0 : existing_property.value().attributes;
  519. if (!new_property && mode == PutOwnPropertyMode::DefineProperty && !existing_attributes.is_configurable() && attributes != existing_attributes) {
  520. #ifdef OBJECT_DEBUG
  521. dbgln("Disallow reconfig of non-configurable property");
  522. #endif
  523. if (throw_exceptions)
  524. vm().throw_exception<TypeError>(global_object(), ErrorType::DescChangeNonConfigurable, property_index);
  525. return false;
  526. }
  527. auto value_here = new_property ? Value() : existing_property.value().value;
  528. if (!new_property && mode == PutOwnPropertyMode::Put && !value_here.is_accessor() && !existing_attributes.is_writable()) {
  529. #ifdef OBJECT_DEBUG
  530. dbgln("Disallow write to non-writable property");
  531. #endif
  532. return false;
  533. }
  534. if (value.is_empty())
  535. return true;
  536. if (value_here.is_native_property()) {
  537. call_native_property_setter(value_here.as_native_property(), &this_object, value);
  538. } else {
  539. m_indexed_properties.put(&this_object, property_index, value, attributes, mode == PutOwnPropertyMode::Put);
  540. }
  541. return true;
  542. }
  543. Value Object::delete_property(const PropertyName& property_name)
  544. {
  545. ASSERT(property_name.is_valid());
  546. if (property_name.is_number())
  547. return Value(m_indexed_properties.remove(property_name.as_number()));
  548. if (property_name.is_string()) {
  549. i32 property_index = property_name.as_string().to_int().value_or(-1);
  550. if (property_index >= 0)
  551. return Value(m_indexed_properties.remove(property_index));
  552. }
  553. auto metadata = shape().lookup(property_name.to_string_or_symbol());
  554. if (!metadata.has_value())
  555. return Value(true);
  556. if (!metadata.value().attributes.is_configurable())
  557. return Value(false);
  558. size_t deleted_offset = metadata.value().offset;
  559. ensure_shape_is_unique();
  560. shape().remove_property_from_unique_shape(property_name.to_string_or_symbol(), deleted_offset);
  561. m_storage.remove(deleted_offset);
  562. return Value(true);
  563. }
  564. void Object::ensure_shape_is_unique()
  565. {
  566. if (shape().is_unique())
  567. return;
  568. m_shape = m_shape->create_unique_clone();
  569. }
  570. Value Object::get_by_index(u32 property_index) const
  571. {
  572. const Object* object = this;
  573. while (object) {
  574. if (is_string_object()) {
  575. auto& string = static_cast<const StringObject*>(this)->primitive_string().string();
  576. if (property_index < string.length())
  577. return js_string(heap(), string.substring(property_index, 1));
  578. return js_undefined();
  579. }
  580. if (static_cast<size_t>(property_index) < object->m_indexed_properties.array_like_size()) {
  581. auto result = object->m_indexed_properties.get(const_cast<Object*>(this), property_index);
  582. if (vm().exception())
  583. return {};
  584. if (result.has_value() && !result.value().value.is_empty())
  585. return result.value().value;
  586. return {};
  587. }
  588. object = object->prototype();
  589. if (vm().exception())
  590. return {};
  591. }
  592. return {};
  593. }
  594. Value Object::get(const PropertyName& property_name, Value receiver) const
  595. {
  596. ASSERT(property_name.is_valid());
  597. if (property_name.is_number())
  598. return get_by_index(property_name.as_number());
  599. if (property_name.is_string()) {
  600. auto property_string = property_name.to_string();
  601. i32 property_index = property_string.to_int().value_or(-1);
  602. if (property_index >= 0)
  603. return get_by_index(property_index);
  604. }
  605. const Object* object = this;
  606. while (object) {
  607. if (receiver.is_empty())
  608. receiver = Value(const_cast<Object*>(this));
  609. auto value = object->get_own_property(property_name, receiver);
  610. if (vm().exception())
  611. return {};
  612. if (!value.is_empty())
  613. return value;
  614. object = object->prototype();
  615. if (vm().exception())
  616. return {};
  617. }
  618. return {};
  619. }
  620. bool Object::put_by_index(u32 property_index, Value value)
  621. {
  622. ASSERT(!value.is_empty());
  623. // If there's a setter in the prototype chain, we go to the setter.
  624. // Otherwise, it goes in the own property storage.
  625. Object* object = this;
  626. while (object) {
  627. auto existing_value = object->m_indexed_properties.get(nullptr, property_index, false);
  628. if (existing_value.has_value()) {
  629. auto value_here = existing_value.value();
  630. if (value_here.value.is_accessor()) {
  631. value_here.value.as_accessor().call_setter(object, value);
  632. return true;
  633. }
  634. if (value_here.value.is_native_property()) {
  635. // FIXME: Why doesn't put_by_index() receive the receiver value from put()?!
  636. auto receiver = this;
  637. call_native_property_setter(value_here.value.as_native_property(), receiver, 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 (receiver.is_empty())
  661. receiver = Value(this);
  662. // If there's a setter in the prototype chain, we go to the setter.
  663. // Otherwise, it goes in the own property storage.
  664. Object* object = this;
  665. while (object) {
  666. auto metadata = object->shape().lookup(string_or_symbol);
  667. if (metadata.has_value()) {
  668. auto value_here = object->m_storage[metadata.value().offset];
  669. if (value_here.is_accessor()) {
  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(value_here.as_native_property(), receiver, 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_edges(Cell::Visitor& visitor)
  707. {
  708. Cell::visit_edges(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(NativeProperty& property, Value this_value) 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_value;
  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(NativeProperty& property, Value this_value, Value setter_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_value;
  797. vm.push_call_frame(call_frame, global_object());
  798. if (vm.exception())
  799. return;
  800. property.set(vm, global_object(), setter_value);
  801. vm.pop_call_frame();
  802. }
  803. }