Object.cpp 27 KB

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