Object.cpp 32 KB

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