Object.cpp 33 KB

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