Object.cpp 33 KB

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