Object.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703
  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. Object* Object::create_empty(Interpreter&, GlobalObject& global_object)
  41. {
  42. return global_object.heap().allocate<Object>(global_object.object_prototype());
  43. }
  44. Object::Object(Object* prototype)
  45. {
  46. if (prototype) {
  47. m_shape = interpreter().global_object().empty_object_shape();
  48. set_prototype(prototype);
  49. } else {
  50. m_shape = interpreter().heap().allocate<Shape>();
  51. }
  52. }
  53. Object::~Object()
  54. {
  55. }
  56. Object* Object::prototype()
  57. {
  58. return shape().prototype();
  59. }
  60. const Object* Object::prototype() const
  61. {
  62. return shape().prototype();
  63. }
  64. bool Object::set_prototype(Object* new_prototype)
  65. {
  66. if (prototype() == new_prototype)
  67. return true;
  68. if (!m_is_extensible)
  69. return false;
  70. if (shape().is_unique()) {
  71. shape().set_prototype_without_transition(new_prototype);
  72. return true;
  73. }
  74. m_shape = m_shape->create_prototype_transition(new_prototype);
  75. return true;
  76. }
  77. bool Object::has_prototype(const Object* prototype) const
  78. {
  79. for (auto* object = this->prototype(); object; object = object->prototype()) {
  80. if (object == prototype)
  81. return true;
  82. }
  83. return false;
  84. }
  85. bool Object::prevent_extensions()
  86. {
  87. m_is_extensible = false;
  88. return true;
  89. }
  90. Value Object::get_own_property(const Object& this_object, PropertyName property_name) const
  91. {
  92. Value value_here;
  93. if (property_name.is_number()) {
  94. auto existing_property = m_indexed_properties.get(nullptr, property_name.as_number(), false);
  95. if (!existing_property.has_value())
  96. return {};
  97. value_here = existing_property.value().value;
  98. } else {
  99. auto metadata = shape().lookup(property_name.as_string());
  100. if (!metadata.has_value())
  101. return {};
  102. value_here = m_storage[metadata.value().offset];
  103. }
  104. ASSERT(!value_here.is_empty());
  105. if (value_here.is_accessor()) {
  106. return value_here.as_accessor().call_getter(Value(const_cast<Object*>(this)));
  107. }
  108. if (value_here.is_object() && value_here.as_object().is_native_property())
  109. return call_native_property_getter(const_cast<Object*>(&this_object), value_here);
  110. return value_here;
  111. }
  112. Value Object::get_own_properties(const Object& this_object, GetOwnPropertyMode kind, u8 attributes) const
  113. {
  114. auto* properties_array = Array::create(interpreter().global_object());
  115. // FIXME: Support generic iterables
  116. if (this_object.is_string_object()) {
  117. auto str = static_cast<const StringObject&>(this_object).primitive_string().string();
  118. for (size_t i = 0; i < str.length(); ++i) {
  119. if (kind == GetOwnPropertyMode::Key) {
  120. properties_array->define_property(i, js_string(interpreter(), String::number(i)));
  121. } else if (kind == GetOwnPropertyMode::Value) {
  122. properties_array->define_property(i, js_string(interpreter(), String::format("%c", str[i])));
  123. } else {
  124. auto* entry_array = Array::create(interpreter().global_object());
  125. entry_array->define_property(0, js_string(interpreter(), String::number(i)));
  126. entry_array->define_property(1, js_string(interpreter(), String::format("%c", str[i])));
  127. properties_array->define_property(i, entry_array);
  128. }
  129. }
  130. return properties_array;
  131. }
  132. size_t property_index = 0;
  133. for (auto& entry : m_indexed_properties) {
  134. if (kind == GetOwnPropertyMode::Key) {
  135. properties_array->define_property(property_index, js_string(interpreter(), String::number(entry.index())));
  136. } else if (kind == GetOwnPropertyMode::Value) {
  137. properties_array->define_property(property_index, entry.value_and_attributes(const_cast<Object*>(&this_object)).value);
  138. if (interpreter().exception())
  139. return {};
  140. } else {
  141. auto* entry_array = Array::create(interpreter().global_object());
  142. entry_array->define_property(0, js_string(interpreter(), String::number(entry.index())));
  143. entry_array->define_property(1, entry.value_and_attributes(const_cast<Object*>(&this_object)).value);
  144. if (interpreter().exception())
  145. return {};
  146. properties_array->define_property(property_index, entry_array);
  147. }
  148. ++property_index;
  149. }
  150. for (auto& it : this_object.shape().property_table_ordered()) {
  151. if (it.value.attributes & attributes) {
  152. size_t offset = it.value.offset + property_index;
  153. if (kind == GetOwnPropertyMode::Key) {
  154. properties_array->define_property(offset, js_string(interpreter(), it.key));
  155. } else if (kind == GetOwnPropertyMode::Value) {
  156. properties_array->define_property(offset, this_object.get(it.key));
  157. if (interpreter().exception())
  158. return {};
  159. } else {
  160. auto* entry_array = Array::create(interpreter().global_object());
  161. entry_array->define_property(0, js_string(interpreter(), it.key));
  162. entry_array->define_property(1, this_object.get(it.key));
  163. if (interpreter().exception())
  164. return {};
  165. properties_array->define_property(offset, entry_array);
  166. }
  167. }
  168. }
  169. return properties_array;
  170. }
  171. Value Object::get_own_property_descriptor(PropertyName property_name) const
  172. {
  173. Value value;
  174. u8 attributes;
  175. if (property_name.is_number()) {
  176. auto existing_value = m_indexed_properties.get(nullptr, property_name.as_number(), false);
  177. if (!existing_value.has_value())
  178. return js_undefined();
  179. value = existing_value.value().value;
  180. attributes = existing_value.value().attributes;
  181. attributes = default_attributes;
  182. } else {
  183. auto metadata = shape().lookup(property_name.as_string());
  184. if (!metadata.has_value())
  185. return js_undefined();
  186. value = m_storage[metadata.value().offset];
  187. if (interpreter().exception())
  188. return {};
  189. attributes = metadata.value().attributes;
  190. }
  191. auto* descriptor = Object::create_empty(interpreter(), interpreter().global_object());
  192. descriptor->define_property("enumerable", Value((attributes & Attribute::Enumerable) != 0));
  193. descriptor->define_property("configurable", Value((attributes & Attribute::Configurable) != 0));
  194. if (value.is_object() && value.as_object().is_native_property()) {
  195. auto result = call_native_property_getter(const_cast<Object*>(this), value);
  196. descriptor->define_property("value", result);
  197. descriptor->define_property("writable", Value((attributes & Attribute::Writable) != 0));
  198. } else if (value.is_accessor()) {
  199. auto& pair = value.as_accessor();
  200. if (pair.getter())
  201. descriptor->define_property("get", pair.getter());
  202. if (pair.setter())
  203. descriptor->define_property("set", pair.setter());
  204. } else {
  205. descriptor->define_property("value", value.value_or(js_undefined()));
  206. descriptor->define_property("writable", Value((attributes & Attribute::Writable) != 0));
  207. }
  208. return descriptor;
  209. }
  210. void Object::set_shape(Shape& new_shape)
  211. {
  212. m_storage.resize(new_shape.property_count());
  213. m_shape = &new_shape;
  214. }
  215. bool Object::define_property(const FlyString& property_name, const Object& descriptor, bool throw_exceptions)
  216. {
  217. bool is_accessor_property = descriptor.has_property("get") || descriptor.has_property("set");
  218. u8 configurable = descriptor.get("configurable").value_or(Value(false)).to_boolean() * Attribute::Configurable;
  219. if (interpreter().exception())
  220. return {};
  221. u8 enumerable = descriptor.get("enumerable").value_or(Value(false)).to_boolean() * Attribute::Enumerable;
  222. if (interpreter().exception())
  223. return {};
  224. u8 attributes = configurable | enumerable;
  225. if (is_accessor_property) {
  226. if (descriptor.has_property("value") || descriptor.has_property("writable")) {
  227. if (throw_exceptions)
  228. interpreter().throw_exception<TypeError>("Accessor property descriptors cannot specify a value or writable key");
  229. return false;
  230. }
  231. auto getter = descriptor.get("get").value_or(js_undefined());
  232. if (interpreter().exception())
  233. return {};
  234. auto setter = descriptor.get("set").value_or(js_undefined());
  235. if (interpreter().exception())
  236. return {};
  237. Function* getter_function { nullptr };
  238. Function* setter_function { nullptr };
  239. if (getter.is_function()) {
  240. getter_function = &getter.as_function();
  241. } else if (!getter.is_undefined()) {
  242. interpreter().throw_exception<TypeError>("Accessor descriptor's 'get' field must be a function or undefined");
  243. return false;
  244. }
  245. if (setter.is_function()) {
  246. setter_function = &setter.as_function();
  247. } else if (!setter.is_undefined()) {
  248. interpreter().throw_exception<TypeError>("Accessor descriptor's 'set' field must be a function or undefined");
  249. return false;
  250. }
  251. dbg() << "Defining new property " << property_name << " with accessor descriptor { attributes=" << attributes << ", "
  252. << "getter=" << getter.to_string_without_side_effects() << ", "
  253. << "setter=" << setter.to_string_without_side_effects() << "}";
  254. return define_property(property_name, Accessor::create(interpreter(), getter_function, setter_function), attributes, throw_exceptions);
  255. }
  256. auto value = descriptor.get("value");
  257. if (interpreter().exception())
  258. return {};
  259. u8 writable = descriptor.get("writable").value_or(Value(false)).to_boolean() * Attribute::Writable;
  260. if (interpreter().exception())
  261. return {};
  262. attributes |= writable;
  263. dbg() << "Defining new property " << property_name << " with data descriptor { attributes=" << attributes
  264. << ", value=" << (value.is_empty() ? "<empty>" : value.to_string_without_side_effects()) << " }";
  265. return define_property(property_name, value, attributes, throw_exceptions);
  266. }
  267. bool Object::define_property(PropertyName property_name, Value value, u8 attributes, bool throw_exceptions)
  268. {
  269. if (property_name.is_number())
  270. return put_own_property_by_index(*this, property_name.as_number(), value, attributes, PutOwnPropertyMode::DefineProperty, throw_exceptions);
  271. bool ok;
  272. i32 property_index = property_name.as_string().to_int(ok);
  273. if (ok && property_index >= 0)
  274. return put_own_property_by_index(*this, property_index, value, attributes, PutOwnPropertyMode::DefineProperty, throw_exceptions);
  275. return put_own_property(*this, property_name.as_string(), value, attributes, PutOwnPropertyMode::DefineProperty, throw_exceptions);
  276. }
  277. bool Object::put_own_property(Object& this_object, const FlyString& property_name, Value value, u8 attributes, PutOwnPropertyMode mode, bool throw_exceptions)
  278. {
  279. ASSERT(!(mode == PutOwnPropertyMode::Put && value.is_accessor()));
  280. if (!is_extensible()) {
  281. dbg() << "Disallow define_property of non-extensible object";
  282. if (throw_exceptions && interpreter().in_strict_mode())
  283. interpreter().throw_exception<TypeError>(String::format("Cannot define property %s on non-extensible object", property_name.characters()));
  284. return false;
  285. }
  286. if (value.is_accessor()) {
  287. auto& accessor = value.as_accessor();
  288. if (accessor.getter())
  289. attributes |= Attribute::HasGet;
  290. if (accessor.setter())
  291. attributes |= Attribute::HasSet;
  292. }
  293. auto metadata = shape().lookup(property_name);
  294. bool new_property = !metadata.has_value();
  295. if (new_property) {
  296. if (!m_shape->is_unique() && shape().property_count() > 100) {
  297. // If you add more than 100 properties to an object, let's stop doing
  298. // transitions to avoid filling up the heap with shapes.
  299. ensure_shape_is_unique();
  300. }
  301. if (m_shape->is_unique()) {
  302. m_shape->add_property_to_unique_shape(property_name, attributes);
  303. m_storage.resize(m_shape->property_count());
  304. } else {
  305. set_shape(*m_shape->create_put_transition(property_name, attributes));
  306. }
  307. metadata = shape().lookup(property_name);
  308. ASSERT(metadata.has_value());
  309. }
  310. if (!new_property && mode == PutOwnPropertyMode::DefineProperty && !(metadata.value().attributes & Attribute::Configurable) && attributes != metadata.value().attributes) {
  311. dbg() << "Disallow reconfig of non-configurable property";
  312. if (throw_exceptions)
  313. interpreter().throw_exception<TypeError>(String::format("Cannot change attributes of non-configurable property '%s'", property_name.characters()));
  314. return false;
  315. }
  316. if (mode == PutOwnPropertyMode::DefineProperty && attributes != metadata.value().attributes) {
  317. if (m_shape->is_unique()) {
  318. m_shape->reconfigure_property_in_unique_shape(property_name, attributes);
  319. } else {
  320. set_shape(*m_shape->create_configure_transition(property_name, attributes));
  321. }
  322. metadata = shape().lookup(property_name);
  323. dbg() << "Reconfigured property " << property_name << ", new shape says offset is " << metadata.value().offset << " and my storage capacity is " << m_storage.size();
  324. }
  325. auto value_here = m_storage[metadata.value().offset];
  326. if (!new_property && mode == PutOwnPropertyMode::Put && !value_here.is_accessor() && !(metadata.value().attributes & Attribute::Writable)) {
  327. dbg() << "Disallow write to non-writable property";
  328. return false;
  329. }
  330. if (value.is_empty())
  331. return true;
  332. if (value_here.is_object() && value_here.as_object().is_native_property()) {
  333. call_native_property_setter(const_cast<Object*>(&this_object), value_here, value);
  334. } else {
  335. m_storage[metadata.value().offset] = value;
  336. }
  337. return true;
  338. }
  339. bool Object::put_own_property_by_index(Object& this_object, u32 property_index, Value value, u8 attributes, PutOwnPropertyMode mode, bool throw_exceptions)
  340. {
  341. ASSERT(!(mode == PutOwnPropertyMode::Put && value.is_accessor()));
  342. if (!is_extensible()) {
  343. dbg() << "Disallow define_property of non-extensible object";
  344. if (throw_exceptions && interpreter().in_strict_mode())
  345. interpreter().throw_exception<TypeError>(String::format("Cannot define property %d on non-extensible object", property_index));
  346. return false;
  347. }
  348. if (value.is_accessor()) {
  349. auto& accessor = value.as_accessor();
  350. if (accessor.getter())
  351. attributes |= Attribute::HasGet;
  352. if (accessor.setter())
  353. attributes |= Attribute::HasSet;
  354. }
  355. auto existing_property = m_indexed_properties.get(nullptr, property_index, false);
  356. auto new_property = !existing_property.has_value();
  357. auto existing_attributes = new_property ? 0 : existing_property.value().attributes;
  358. if (!new_property && mode == PutOwnPropertyMode::DefineProperty && !(existing_attributes & Attribute::Configurable) && attributes != existing_attributes) {
  359. dbg() << "Disallow reconfig of non-configurable property";
  360. if (throw_exceptions)
  361. interpreter().throw_exception<TypeError>(String::format("Cannot change attributes of non-configurable property %d", property_index));
  362. return false;
  363. }
  364. auto value_here = new_property ? Value() : existing_property.value().value;
  365. if (!new_property && mode == PutOwnPropertyMode::Put && !value_here.is_accessor() && !(existing_attributes & Attribute::Writable)) {
  366. dbg() << "Disallow write to non-writable property";
  367. return false;
  368. }
  369. if (value.is_empty())
  370. return true;
  371. if (value_here.is_object() && value_here.as_object().is_native_property()) {
  372. call_native_property_setter(const_cast<Object*>(&this_object), value_here, value);
  373. } else {
  374. m_indexed_properties.put(&this_object, property_index, value, attributes, mode == PutOwnPropertyMode::Put);
  375. }
  376. return true;
  377. }
  378. Value Object::delete_property(PropertyName property_name)
  379. {
  380. ASSERT(property_name.is_valid());
  381. if (property_name.is_number())
  382. return Value(m_indexed_properties.remove(property_name.as_number()));
  383. auto metadata = shape().lookup(property_name.as_string());
  384. if (!metadata.has_value())
  385. return Value(true);
  386. if (!(metadata.value().attributes & Attribute::Configurable))
  387. return Value(false);
  388. size_t deleted_offset = metadata.value().offset;
  389. ensure_shape_is_unique();
  390. shape().remove_property_from_unique_shape(property_name.as_string(), deleted_offset);
  391. m_storage.remove(deleted_offset);
  392. return Value(true);
  393. }
  394. void Object::ensure_shape_is_unique()
  395. {
  396. if (shape().is_unique())
  397. return;
  398. m_shape = m_shape->create_unique_clone();
  399. }
  400. Value Object::get_by_index(u32 property_index) const
  401. {
  402. const Object* object = this;
  403. while (object) {
  404. if (is_string_object()) {
  405. auto& string = static_cast<const StringObject*>(this)->primitive_string().string();
  406. if (property_index < string.length())
  407. return js_string(heap(), string.substring(property_index, 1));
  408. return js_undefined();
  409. }
  410. if (static_cast<size_t>(property_index) < object->m_indexed_properties.array_like_size()) {
  411. auto result = object->m_indexed_properties.get(const_cast<Object*>(this), property_index);
  412. if (interpreter().exception())
  413. return {};
  414. if (result.has_value() && !result.value().value.is_empty())
  415. return result.value().value;
  416. return {};
  417. }
  418. object = object->prototype();
  419. }
  420. return {};
  421. }
  422. Value Object::get(PropertyName property_name) const
  423. {
  424. if (property_name.is_number())
  425. return get_by_index(property_name.as_number());
  426. auto property_string = property_name.to_string();
  427. bool ok;
  428. i32 property_index = property_string.to_int(ok);
  429. if (ok && property_index >= 0)
  430. return get_by_index(property_index);
  431. const Object* object = this;
  432. while (object) {
  433. auto value = object->get_own_property(*this, property_name);
  434. if (!value.is_empty())
  435. return value;
  436. object = object->prototype();
  437. }
  438. return {};
  439. }
  440. bool Object::put_by_index(u32 property_index, Value value)
  441. {
  442. ASSERT(!value.is_empty());
  443. // If there's a setter in the prototype chain, we go to the setter.
  444. // Otherwise, it goes in the own property storage.
  445. Object* object = this;
  446. while (object) {
  447. auto existing_value = object->m_indexed_properties.get(nullptr, property_index, false);
  448. if (existing_value.has_value()) {
  449. auto value_here = existing_value.value();
  450. if (value_here.value.is_accessor()) {
  451. value_here.value.as_accessor().call_setter(object, value);
  452. return true;
  453. }
  454. if (value_here.value.is_object() && value_here.value.as_object().is_native_property()) {
  455. call_native_property_setter(const_cast<Object*>(this), value_here.value, value);
  456. return true;
  457. }
  458. }
  459. object = object->prototype();
  460. }
  461. return put_own_property_by_index(*this, property_index, value, default_attributes, PutOwnPropertyMode::Put);
  462. }
  463. bool Object::put(PropertyName property_name, Value value)
  464. {
  465. if (property_name.is_number())
  466. return put_by_index(property_name.as_number(), value);
  467. ASSERT(!value.is_empty());
  468. auto property_string = property_name.to_string();
  469. bool ok;
  470. i32 property_index = property_string.to_int(ok);
  471. if (ok && property_index >= 0)
  472. return put_by_index(property_index, value);
  473. // If there's a setter in the prototype chain, we go to the setter.
  474. // Otherwise, it goes in the own property storage.
  475. Object* object = this;
  476. while (object) {
  477. auto metadata = object->shape().lookup(property_string);
  478. if (metadata.has_value()) {
  479. auto value_here = object->m_storage[metadata.value().offset];
  480. if (value_here.is_accessor()) {
  481. value_here.as_accessor().call_setter(Value(this), value);
  482. return true;
  483. }
  484. if (value_here.is_object() && value_here.as_object().is_native_property()) {
  485. call_native_property_setter(const_cast<Object*>(this), value_here, value);
  486. return true;
  487. }
  488. }
  489. object = object->prototype();
  490. }
  491. return put_own_property(*this, property_string, value, default_attributes, PutOwnPropertyMode::Put);
  492. }
  493. bool Object::define_native_function(const FlyString& property_name, AK::Function<Value(Interpreter&)> native_function, i32 length, u8 attribute)
  494. {
  495. auto* function = NativeFunction::create(interpreter(), interpreter().global_object(), property_name, move(native_function));
  496. function->define_property("length", Value(length), Attribute::Configurable);
  497. function->define_property("name", js_string(heap(), property_name), Attribute::Configurable);
  498. return define_property(property_name, function, attribute);
  499. }
  500. bool Object::define_native_property(const FlyString& property_name, AK::Function<Value(Interpreter&)> getter, AK::Function<void(Interpreter&, Value)> setter, u8 attribute)
  501. {
  502. return define_property(property_name, heap().allocate<NativeProperty>(move(getter), move(setter)), attribute);
  503. }
  504. void Object::visit_children(Cell::Visitor& visitor)
  505. {
  506. Cell::visit_children(visitor);
  507. visitor.visit(m_shape);
  508. for (auto& value : m_storage)
  509. visitor.visit(value);
  510. for (auto& value : m_indexed_properties.values_unordered())
  511. visitor.visit(value.value);
  512. }
  513. bool Object::has_property(PropertyName property_name) const
  514. {
  515. const Object* object = this;
  516. while (object) {
  517. if (object->has_own_property(property_name))
  518. return true;
  519. object = object->prototype();
  520. }
  521. return false;
  522. }
  523. bool Object::has_own_property(PropertyName property_name) const
  524. {
  525. auto has_indexed_property = [&](u32 index) -> bool {
  526. if (is_string_object())
  527. return index < static_cast<const StringObject*>(this)->primitive_string().string().length();
  528. return m_indexed_properties.has_index(index);
  529. };
  530. if (property_name.is_number())
  531. return has_indexed_property(property_name.as_number());
  532. bool ok;
  533. i32 property_index = property_name.as_string().to_int(ok);
  534. if (ok && property_index >= 0)
  535. return has_indexed_property(property_index);
  536. return shape().lookup(property_name.as_string()).has_value();
  537. }
  538. Value Object::to_primitive(Value::PreferredType preferred_type) const
  539. {
  540. Value result = js_undefined();
  541. switch (preferred_type) {
  542. case Value::PreferredType::Default:
  543. case Value::PreferredType::Number: {
  544. result = value_of();
  545. if (result.is_object()) {
  546. result = to_string();
  547. }
  548. break;
  549. }
  550. case Value::PreferredType::String: {
  551. result = to_string();
  552. if (result.is_object())
  553. result = value_of();
  554. break;
  555. }
  556. }
  557. ASSERT(!result.is_object());
  558. return result;
  559. }
  560. Value Object::to_string() const
  561. {
  562. auto to_string_property = get("toString");
  563. if (to_string_property.is_function()) {
  564. auto& to_string_function = to_string_property.as_function();
  565. auto& interpreter = const_cast<Object*>(this)->interpreter();
  566. auto to_string_result = interpreter.call(to_string_function, const_cast<Object*>(this));
  567. if (to_string_result.is_object())
  568. interpreter.throw_exception<TypeError>("Cannot convert object to string");
  569. if (interpreter.exception())
  570. return {};
  571. auto* string = to_string_result.to_primitive_string(interpreter);
  572. if (interpreter.exception())
  573. return {};
  574. return string;
  575. }
  576. return js_string(heap(), String::format("[object %s]", class_name()));
  577. }
  578. Value Object::invoke(const FlyString& property_name, Optional<MarkedValueList> arguments)
  579. {
  580. auto& interpreter = this->interpreter();
  581. auto property = get(property_name).value_or(js_undefined());
  582. if (interpreter.exception())
  583. return {};
  584. if (!property.is_function()) {
  585. interpreter.throw_exception<TypeError>(String::format("%s is not a function", property.to_string_without_side_effects().characters()));
  586. return {};
  587. }
  588. return interpreter.call(property.as_function(), this, move(arguments));
  589. }
  590. Value Object::call_native_property_getter(Object* this_object, Value property) const
  591. {
  592. ASSERT(property.is_object());
  593. ASSERT(property.as_object().is_native_property());
  594. auto& native_property = static_cast<NativeProperty&>(property.as_object());
  595. auto& call_frame = interpreter().push_call_frame();
  596. call_frame.this_value = this_object;
  597. auto result = native_property.get(interpreter());
  598. interpreter().pop_call_frame();
  599. return result;
  600. }
  601. void Object::call_native_property_setter(Object* this_object, Value property, Value value) const
  602. {
  603. ASSERT(property.is_object());
  604. ASSERT(property.as_object().is_native_property());
  605. auto& native_property = static_cast<NativeProperty&>(property.as_object());
  606. auto& call_frame = interpreter().push_call_frame();
  607. call_frame.this_value = this_object;
  608. native_property.set(interpreter(), value);
  609. interpreter().pop_call_frame();
  610. }
  611. }