Object.cpp 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2020-2021, Linus Groh <linusg@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/Debug.h>
  8. #include <AK/String.h>
  9. #include <AK/TemporaryChange.h>
  10. #include <LibJS/Heap/Heap.h>
  11. #include <LibJS/Interpreter.h>
  12. #include <LibJS/Runtime/Accessor.h>
  13. #include <LibJS/Runtime/Array.h>
  14. #include <LibJS/Runtime/Error.h>
  15. #include <LibJS/Runtime/GlobalObject.h>
  16. #include <LibJS/Runtime/NativeFunction.h>
  17. #include <LibJS/Runtime/NativeProperty.h>
  18. #include <LibJS/Runtime/Object.h>
  19. #include <LibJS/Runtime/ProxyObject.h>
  20. #include <LibJS/Runtime/Shape.h>
  21. #include <LibJS/Runtime/StringObject.h>
  22. #include <LibJS/Runtime/TemporaryClearException.h>
  23. #include <LibJS/Runtime/Value.h>
  24. namespace JS {
  25. PropertyDescriptor PropertyDescriptor::from_dictionary(VM& vm, const Object& object)
  26. {
  27. PropertyAttributes attributes;
  28. if (object.has_property(vm.names.configurable)) {
  29. attributes.set_has_configurable();
  30. if (object.get(vm.names.configurable).value_or(Value(false)).to_boolean())
  31. attributes.set_configurable();
  32. if (vm.exception())
  33. return {};
  34. }
  35. if (object.has_property(vm.names.enumerable)) {
  36. attributes.set_has_enumerable();
  37. if (object.get(vm.names.enumerable).value_or(Value(false)).to_boolean())
  38. attributes.set_enumerable();
  39. if (vm.exception())
  40. return {};
  41. }
  42. if (object.has_property(vm.names.writable)) {
  43. attributes.set_has_writable();
  44. if (object.get(vm.names.writable).value_or(Value(false)).to_boolean())
  45. attributes.set_writable();
  46. if (vm.exception())
  47. return {};
  48. }
  49. PropertyDescriptor descriptor { attributes, object.get(vm.names.value), nullptr, nullptr };
  50. if (vm.exception())
  51. return {};
  52. auto getter = object.get(vm.names.get);
  53. if (vm.exception())
  54. return {};
  55. if (getter.is_function())
  56. descriptor.getter = &getter.as_function();
  57. auto setter = object.get(vm.names.set);
  58. if (vm.exception())
  59. return {};
  60. if (setter.is_function())
  61. descriptor.setter = &setter.as_function();
  62. return descriptor;
  63. }
  64. Object* Object::create_empty(GlobalObject& global_object)
  65. {
  66. return global_object.heap().allocate<Object>(global_object, *global_object.new_object_shape());
  67. }
  68. Object::Object(GlobalObjectTag)
  69. {
  70. // This is the global object
  71. m_shape = heap().allocate_without_global_object<Shape>(*this);
  72. }
  73. Object::Object(ConstructWithoutPrototypeTag, GlobalObject& global_object)
  74. {
  75. m_shape = heap().allocate_without_global_object<Shape>(global_object);
  76. }
  77. Object::Object(Object& prototype)
  78. {
  79. m_shape = prototype.global_object().empty_object_shape();
  80. set_prototype(&prototype);
  81. }
  82. Object::Object(Shape& shape)
  83. : m_shape(&shape)
  84. {
  85. m_storage.resize(shape.property_count());
  86. }
  87. void Object::initialize(GlobalObject&)
  88. {
  89. }
  90. Object::~Object()
  91. {
  92. }
  93. Object* Object::prototype()
  94. {
  95. return shape().prototype();
  96. }
  97. const Object* Object::prototype() const
  98. {
  99. return shape().prototype();
  100. }
  101. // 10.1.2.1 OrdinarySetPrototypeOf, https://tc39.es/ecma262/#sec-ordinarysetprototypeof
  102. bool Object::set_prototype(Object* new_prototype)
  103. {
  104. if (prototype() == new_prototype)
  105. return true;
  106. if (!m_is_extensible)
  107. return false;
  108. auto* prototype = new_prototype;
  109. while (prototype) {
  110. if (prototype == this)
  111. return false;
  112. // NOTE: This is a best-effort implementation of the following step:
  113. // "If p.[[GetPrototypeOf]] is not the ordinary object internal method defined in 10.1.1,
  114. // set done to true."
  115. // We don't have a good way of detecting whether certain virtual Object methods have been
  116. // overridden by a given object, but as ProxyObject is the only one doing that, this check
  117. // does the trick.
  118. if (is<ProxyObject>(prototype))
  119. break;
  120. prototype = prototype->prototype();
  121. }
  122. auto& shape = this->shape();
  123. if (shape.is_unique())
  124. shape.set_prototype_without_transition(new_prototype);
  125. else
  126. m_shape = shape.create_prototype_transition(new_prototype);
  127. return true;
  128. }
  129. bool Object::has_prototype(const Object* prototype) const
  130. {
  131. for (auto* object = this->prototype(); object; object = object->prototype()) {
  132. if (vm().exception())
  133. return false;
  134. if (object == prototype)
  135. return true;
  136. }
  137. return false;
  138. }
  139. bool Object::prevent_extensions()
  140. {
  141. m_is_extensible = false;
  142. return true;
  143. }
  144. // 7.3.15 SetIntegrityLevel, https://tc39.es/ecma262/#sec-setintegritylevel
  145. bool Object::set_integrity_level(IntegrityLevel level)
  146. {
  147. // FIXME: This feels clunky and should get nicer abstractions.
  148. auto update_property = [this](auto& property_name, auto new_attributes) {
  149. if (property_name.is_number()) {
  150. auto value_and_attributes = m_indexed_properties.get(nullptr, property_name.as_number(), false).value();
  151. auto value = value_and_attributes.value;
  152. auto attributes = value_and_attributes.attributes.bits() & new_attributes;
  153. m_indexed_properties.put(nullptr, property_name.as_number(), value, attributes, false);
  154. } else {
  155. auto metadata = shape().lookup(property_name.to_string_or_symbol()).value();
  156. auto attributes = metadata.attributes.bits() & new_attributes;
  157. if (m_shape->is_unique())
  158. m_shape->reconfigure_property_in_unique_shape(property_name.to_string_or_symbol(), attributes);
  159. else
  160. set_shape(*m_shape->create_configure_transition(property_name.to_string_or_symbol(), attributes));
  161. }
  162. };
  163. auto& vm = this->vm();
  164. auto status = prevent_extensions();
  165. if (vm.exception())
  166. return false;
  167. if (!status)
  168. return false;
  169. auto keys = get_own_properties(PropertyKind::Key);
  170. if (vm.exception())
  171. return false;
  172. switch (level) {
  173. case IntegrityLevel::Sealed:
  174. for (auto& key : keys) {
  175. auto property_name = PropertyName::from_value(global_object(), key);
  176. if (property_name.is_string()) {
  177. i32 property_index = property_name.as_string().to_int().value_or(-1);
  178. if (property_index >= 0)
  179. property_name = property_index;
  180. }
  181. update_property(property_name, ~Attribute::Configurable);
  182. if (vm.exception())
  183. return {};
  184. }
  185. break;
  186. case IntegrityLevel::Frozen:
  187. for (auto& key : keys) {
  188. auto property_name = PropertyName::from_value(global_object(), key);
  189. if (property_name.is_string()) {
  190. i32 property_index = property_name.as_string().to_int().value_or(-1);
  191. if (property_index >= 0)
  192. property_name = property_index;
  193. }
  194. auto property_descriptor = get_own_property_descriptor(property_name);
  195. if (!property_descriptor.has_value())
  196. continue;
  197. u8 attributes = property_descriptor->is_accessor_descriptor()
  198. ? ~Attribute::Configurable
  199. : ~Attribute::Configurable & ~Attribute::Writable;
  200. update_property(property_name, attributes);
  201. if (vm.exception())
  202. return {};
  203. }
  204. break;
  205. default:
  206. VERIFY_NOT_REACHED();
  207. }
  208. return true;
  209. }
  210. // 7.3.16 TestIntegrityLevel, https://tc39.es/ecma262/#sec-testintegritylevel
  211. bool Object::test_integrity_level(IntegrityLevel level)
  212. {
  213. auto& vm = this->vm();
  214. auto extensible = is_extensible();
  215. if (vm.exception())
  216. return false;
  217. if (extensible)
  218. return false;
  219. auto keys = get_own_properties(PropertyKind::Key);
  220. if (vm.exception())
  221. return false;
  222. for (auto& key : keys) {
  223. auto property_name = PropertyName::from_value(global_object(), key);
  224. auto property_descriptor = get_own_property_descriptor(property_name);
  225. if (!property_descriptor.has_value())
  226. continue;
  227. if (property_descriptor->attributes.is_configurable())
  228. return false;
  229. if (level == IntegrityLevel::Frozen && property_descriptor->is_data_descriptor()) {
  230. if (property_descriptor->attributes.is_writable())
  231. return false;
  232. }
  233. }
  234. return true;
  235. }
  236. Value Object::get_own_property(const PropertyName& property_name, Value receiver, bool without_side_effects) const
  237. {
  238. VERIFY(property_name.is_valid());
  239. VERIFY(!receiver.is_empty());
  240. Value value_here;
  241. if (property_name.is_number()) {
  242. auto existing_property = m_indexed_properties.get(nullptr, property_name.as_number(), false);
  243. if (!existing_property.has_value())
  244. return {};
  245. value_here = existing_property.value().value.value_or(js_undefined());
  246. } else {
  247. auto metadata = shape().lookup(property_name.to_string_or_symbol());
  248. if (!metadata.has_value())
  249. return {};
  250. value_here = m_storage[metadata.value().offset].value_or(js_undefined());
  251. }
  252. VERIFY(!value_here.is_empty());
  253. if (!without_side_effects) {
  254. if (value_here.is_accessor())
  255. return value_here.as_accessor().call_getter(receiver);
  256. if (value_here.is_native_property())
  257. return call_native_property_getter(value_here.as_native_property(), receiver);
  258. }
  259. return value_here;
  260. }
  261. MarkedValueList Object::get_own_properties(PropertyKind kind, bool only_enumerable_properties, GetOwnPropertyReturnType return_type) const
  262. {
  263. MarkedValueList properties(heap());
  264. // FIXME: Support generic iterables
  265. if (is<StringObject>(*this)) {
  266. auto str = static_cast<const StringObject&>(*this).primitive_string().string();
  267. for (size_t i = 0; i < str.length(); ++i) {
  268. if (kind == PropertyKind::Key) {
  269. properties.append(js_string(vm(), String::number(i)));
  270. } else if (kind == PropertyKind::Value) {
  271. properties.append(js_string(vm(), String::formatted("{:c}", str[i])));
  272. } else {
  273. auto* entry_array = Array::create(global_object());
  274. entry_array->define_property(0, js_string(vm(), String::number(i)));
  275. entry_array->define_property(1, js_string(vm(), String::formatted("{:c}", str[i])));
  276. properties.append(entry_array);
  277. }
  278. if (vm().exception())
  279. return MarkedValueList { heap() };
  280. }
  281. return properties;
  282. }
  283. for (auto& entry : m_indexed_properties) {
  284. auto value_and_attributes = entry.value_and_attributes(const_cast<Object*>(this));
  285. if (only_enumerable_properties && !value_and_attributes.attributes.is_enumerable())
  286. continue;
  287. if (kind == PropertyKind::Key) {
  288. properties.append(js_string(vm(), String::number(entry.index())));
  289. } else if (kind == PropertyKind::Value) {
  290. properties.append(value_and_attributes.value);
  291. } else {
  292. auto* entry_array = Array::create(global_object());
  293. entry_array->define_property(0, js_string(vm(), String::number(entry.index())));
  294. entry_array->define_property(1, value_and_attributes.value);
  295. properties.append(entry_array);
  296. }
  297. if (vm().exception())
  298. return MarkedValueList { heap() };
  299. }
  300. auto add_property_to_results = [&](auto& property) {
  301. if (kind == PropertyKind::Key) {
  302. properties.append(property.key.to_value(vm()));
  303. } else if (kind == PropertyKind::Value) {
  304. properties.append(get(property.key));
  305. } else {
  306. auto* entry_array = Array::create(global_object());
  307. entry_array->define_property(0, property.key.to_value(vm()));
  308. entry_array->define_property(1, get(property.key));
  309. properties.append(entry_array);
  310. }
  311. };
  312. // NOTE: Most things including for..in/of and Object.{keys,values,entries}() use StringOnly, and in those
  313. // cases we won't be iterating the ordered property table twice. We can certainly improve this though.
  314. if (return_type == GetOwnPropertyReturnType::All || return_type == GetOwnPropertyReturnType::StringOnly) {
  315. for (auto& it : shape().property_table_ordered()) {
  316. if (only_enumerable_properties && !it.value.attributes.is_enumerable())
  317. continue;
  318. if (it.key.is_symbol())
  319. continue;
  320. add_property_to_results(it);
  321. if (vm().exception())
  322. return MarkedValueList { heap() };
  323. }
  324. }
  325. if (return_type == GetOwnPropertyReturnType::All || return_type == GetOwnPropertyReturnType::SymbolOnly) {
  326. for (auto& it : shape().property_table_ordered()) {
  327. if (only_enumerable_properties && !it.value.attributes.is_enumerable())
  328. continue;
  329. if (it.key.is_string())
  330. continue;
  331. add_property_to_results(it);
  332. if (vm().exception())
  333. return MarkedValueList { heap() };
  334. }
  335. }
  336. return properties;
  337. }
  338. // 7.3.23 EnumerableOwnPropertyNames, https://tc39.es/ecma262/#sec-enumerableownpropertynames
  339. MarkedValueList Object::get_enumerable_own_property_names(PropertyKind kind) const
  340. {
  341. return get_own_properties(kind, true, Object::GetOwnPropertyReturnType::StringOnly);
  342. }
  343. Optional<PropertyDescriptor> Object::get_own_property_descriptor(const PropertyName& property_name) const
  344. {
  345. VERIFY(property_name.is_valid());
  346. Value value;
  347. PropertyAttributes attributes;
  348. if (property_name.is_number()) {
  349. auto existing_value = m_indexed_properties.get(nullptr, property_name.as_number(), false);
  350. if (!existing_value.has_value())
  351. return {};
  352. value = existing_value.value().value;
  353. attributes = existing_value.value().attributes;
  354. } else {
  355. if (property_name.is_string()) {
  356. i32 property_index = property_name.as_string().to_int().value_or(-1);
  357. if (property_index >= 0)
  358. return get_own_property_descriptor(property_index);
  359. }
  360. auto metadata = shape().lookup(property_name.to_string_or_symbol());
  361. if (!metadata.has_value())
  362. return {};
  363. value = m_storage[metadata.value().offset];
  364. attributes = metadata.value().attributes;
  365. }
  366. PropertyDescriptor descriptor { attributes, {}, nullptr, nullptr };
  367. if (value.is_native_property()) {
  368. auto result = call_native_property_getter(value.as_native_property(), const_cast<Object*>(this));
  369. descriptor.value = result.value_or(js_undefined());
  370. } else if (value.is_accessor()) {
  371. auto& pair = value.as_accessor();
  372. if (pair.getter())
  373. descriptor.getter = pair.getter();
  374. if (pair.setter())
  375. descriptor.setter = pair.setter();
  376. } else {
  377. descriptor.value = value.value_or(js_undefined());
  378. }
  379. return descriptor;
  380. }
  381. // Equivalent to:
  382. // 6.2.5.4 FromPropertyDescriptor, https://tc39.es/ecma262/#sec-frompropertydescriptor
  383. Value Object::get_own_property_descriptor_object(const PropertyName& property_name) const
  384. {
  385. VERIFY(property_name.is_valid());
  386. auto& vm = this->vm();
  387. auto descriptor_opt = get_own_property_descriptor(property_name);
  388. if (!descriptor_opt.has_value())
  389. return js_undefined();
  390. auto descriptor = descriptor_opt.value();
  391. auto* descriptor_object = Object::create_empty(global_object());
  392. if (descriptor.is_data_descriptor()) {
  393. descriptor_object->define_property(vm.names.value, descriptor.value.value_or(js_undefined()));
  394. descriptor_object->define_property(vm.names.writable, Value(descriptor.attributes.is_writable()));
  395. } else {
  396. VERIFY(descriptor.is_accessor_descriptor());
  397. descriptor_object->define_property(vm.names.get, descriptor.getter ? Value(descriptor.getter) : js_undefined());
  398. descriptor_object->define_property(vm.names.set, descriptor.setter ? Value(descriptor.setter) : js_undefined());
  399. }
  400. descriptor_object->define_property(vm.names.enumerable, Value(descriptor.attributes.is_enumerable()));
  401. descriptor_object->define_property(vm.names.configurable, Value(descriptor.attributes.is_configurable()));
  402. return descriptor_object;
  403. }
  404. void Object::set_shape(Shape& new_shape)
  405. {
  406. m_storage.resize(new_shape.property_count());
  407. m_shape = &new_shape;
  408. }
  409. bool Object::define_property(const StringOrSymbol& property_name, const Object& descriptor, bool throw_exceptions)
  410. {
  411. auto& vm = this->vm();
  412. bool is_accessor_property = descriptor.has_property(vm.names.get) || descriptor.has_property(vm.names.set);
  413. PropertyAttributes attributes;
  414. if (descriptor.has_property(vm.names.configurable)) {
  415. attributes.set_has_configurable();
  416. if (descriptor.get(vm.names.configurable).value_or(Value(false)).to_boolean())
  417. attributes.set_configurable();
  418. if (vm.exception())
  419. return false;
  420. }
  421. if (descriptor.has_property(vm.names.enumerable)) {
  422. attributes.set_has_enumerable();
  423. if (descriptor.get(vm.names.enumerable).value_or(Value(false)).to_boolean())
  424. attributes.set_enumerable();
  425. if (vm.exception())
  426. return false;
  427. }
  428. if (is_accessor_property) {
  429. if (descriptor.has_property(vm.names.value) || descriptor.has_property(vm.names.writable)) {
  430. if (throw_exceptions)
  431. vm.throw_exception<TypeError>(global_object(), ErrorType::AccessorValueOrWritable);
  432. return false;
  433. }
  434. auto getter = descriptor.get(vm.names.get).value_or(js_undefined());
  435. if (vm.exception())
  436. return {};
  437. auto setter = descriptor.get(vm.names.set).value_or(js_undefined());
  438. if (vm.exception())
  439. return {};
  440. Function* getter_function { nullptr };
  441. Function* setter_function { nullptr };
  442. if (getter.is_function()) {
  443. getter_function = &getter.as_function();
  444. } else if (!getter.is_undefined()) {
  445. vm.throw_exception<TypeError>(global_object(), ErrorType::AccessorBadField, "get");
  446. return false;
  447. }
  448. if (setter.is_function()) {
  449. setter_function = &setter.as_function();
  450. } else if (!setter.is_undefined()) {
  451. vm.throw_exception<TypeError>(global_object(), ErrorType::AccessorBadField, "set");
  452. return false;
  453. }
  454. dbgln_if(OBJECT_DEBUG, "Defining new property {} with accessor descriptor {{ attributes={}, getter={}, setter={} }}", property_name.to_display_string(), attributes, getter, setter);
  455. return define_property(property_name, Accessor::create(vm, getter_function, setter_function), attributes, throw_exceptions);
  456. }
  457. auto value = descriptor.get(vm.names.value);
  458. if (vm.exception())
  459. return {};
  460. if (descriptor.has_property(vm.names.writable)) {
  461. attributes.set_has_writable();
  462. if (descriptor.get(vm.names.writable).value_or(Value(false)).to_boolean())
  463. attributes.set_writable();
  464. if (vm.exception())
  465. return false;
  466. }
  467. if (vm.exception())
  468. return {};
  469. dbgln_if(OBJECT_DEBUG, "Defining new property {} with data descriptor {{ attributes={}, value={} }}", property_name.to_display_string(), attributes, value);
  470. return define_property(property_name, value, attributes, throw_exceptions);
  471. }
  472. bool Object::define_property_without_transition(const PropertyName& property_name, Value value, PropertyAttributes attributes, bool throw_exceptions)
  473. {
  474. TemporaryChange change(m_transitions_enabled, false);
  475. return define_property(property_name, value, attributes, throw_exceptions);
  476. }
  477. bool Object::define_property(const PropertyName& property_name, Value value, PropertyAttributes attributes, bool throw_exceptions)
  478. {
  479. VERIFY(property_name.is_valid());
  480. if (property_name.is_number())
  481. return put_own_property_by_index(property_name.as_number(), value, attributes, PutOwnPropertyMode::DefineProperty, throw_exceptions);
  482. if (property_name.is_string()) {
  483. i32 property_index = property_name.as_string().to_int().value_or(-1);
  484. if (property_index >= 0)
  485. return put_own_property_by_index(property_index, value, attributes, PutOwnPropertyMode::DefineProperty, throw_exceptions);
  486. }
  487. return put_own_property(property_name.to_string_or_symbol(), value, attributes, PutOwnPropertyMode::DefineProperty, throw_exceptions);
  488. }
  489. bool Object::define_accessor(const PropertyName& property_name, Function* getter, Function* setter, PropertyAttributes attributes, bool throw_exceptions)
  490. {
  491. VERIFY(property_name.is_valid());
  492. Accessor* accessor { nullptr };
  493. auto property_metadata = shape().lookup(property_name.to_string_or_symbol());
  494. if (property_metadata.has_value()) {
  495. auto existing_property = get_direct(property_metadata.value().offset);
  496. if (existing_property.is_accessor())
  497. accessor = &existing_property.as_accessor();
  498. }
  499. if (!accessor) {
  500. accessor = Accessor::create(vm(), getter, setter);
  501. bool definition_success = define_property(property_name, accessor, attributes, throw_exceptions);
  502. if (vm().exception())
  503. return {};
  504. if (!definition_success)
  505. return false;
  506. } else {
  507. if (getter)
  508. accessor->set_getter(getter);
  509. if (setter)
  510. accessor->set_setter(setter);
  511. }
  512. return true;
  513. }
  514. bool Object::put_own_property(const StringOrSymbol& property_name, Value value, PropertyAttributes attributes, PutOwnPropertyMode mode, bool throw_exceptions)
  515. {
  516. VERIFY(!(mode == PutOwnPropertyMode::Put && value.is_accessor()));
  517. if (value.is_accessor()) {
  518. auto& accessor = value.as_accessor();
  519. if (accessor.getter())
  520. attributes.set_has_getter();
  521. if (accessor.setter())
  522. attributes.set_has_setter();
  523. }
  524. // NOTE: We disable transitions during initialize(), this makes building common runtime objects significantly faster.
  525. // Transitions are primarily interesting when scripts add properties to objects.
  526. if (!m_transitions_enabled && !m_shape->is_unique()) {
  527. m_shape->add_property_without_transition(property_name, attributes);
  528. m_storage.resize(m_shape->property_count());
  529. m_storage[m_shape->property_count() - 1] = value;
  530. return true;
  531. }
  532. auto metadata = shape().lookup(property_name);
  533. bool new_property = !metadata.has_value();
  534. if (!is_extensible() && new_property) {
  535. dbgln_if(OBJECT_DEBUG, "Disallow define_property of non-extensible object");
  536. if (throw_exceptions && vm().in_strict_mode())
  537. vm().throw_exception<TypeError>(global_object(), ErrorType::NonExtensibleDefine, property_name.to_display_string());
  538. return false;
  539. }
  540. if (new_property) {
  541. if (!m_shape->is_unique() && shape().property_count() > 100) {
  542. // If you add more than 100 properties to an object, let's stop doing
  543. // transitions to avoid filling up the heap with shapes.
  544. ensure_shape_is_unique();
  545. }
  546. if (m_shape->is_unique()) {
  547. m_shape->add_property_to_unique_shape(property_name, attributes);
  548. m_storage.resize(m_shape->property_count());
  549. } else if (m_transitions_enabled) {
  550. set_shape(*m_shape->create_put_transition(property_name, attributes));
  551. } else {
  552. m_shape->add_property_without_transition(property_name, attributes);
  553. m_storage.resize(m_shape->property_count());
  554. }
  555. metadata = shape().lookup(property_name);
  556. VERIFY(metadata.has_value());
  557. }
  558. if (!new_property && mode == PutOwnPropertyMode::DefineProperty && !metadata.value().attributes.is_configurable() && attributes != metadata.value().attributes) {
  559. dbgln_if(OBJECT_DEBUG, "Disallow reconfig of non-configurable property");
  560. if (throw_exceptions)
  561. vm().throw_exception<TypeError>(global_object(), ErrorType::DescChangeNonConfigurable, property_name.to_display_string());
  562. return false;
  563. }
  564. if (mode == PutOwnPropertyMode::DefineProperty && attributes != metadata.value().attributes) {
  565. if (m_shape->is_unique()) {
  566. m_shape->reconfigure_property_in_unique_shape(property_name, attributes);
  567. } else {
  568. set_shape(*m_shape->create_configure_transition(property_name, attributes));
  569. }
  570. metadata = shape().lookup(property_name);
  571. dbgln_if(OBJECT_DEBUG, "Reconfigured property {}, new shape says offset is {} and my storage capacity is {}", property_name.to_display_string(), metadata.value().offset, m_storage.size());
  572. }
  573. auto value_here = m_storage[metadata.value().offset];
  574. if (!new_property && mode == PutOwnPropertyMode::Put && !value_here.is_accessor() && !metadata.value().attributes.is_writable()) {
  575. dbgln_if(OBJECT_DEBUG, "Disallow write to non-writable property");
  576. if (throw_exceptions && vm().in_strict_mode())
  577. vm().throw_exception<TypeError>(global_object(), ErrorType::DescWriteNonWritable, property_name.to_display_string());
  578. return false;
  579. }
  580. if (value.is_empty())
  581. return true;
  582. if (value_here.is_native_property()) {
  583. call_native_property_setter(value_here.as_native_property(), this, value);
  584. } else {
  585. m_storage[metadata.value().offset] = value;
  586. }
  587. return true;
  588. }
  589. bool Object::put_own_property_by_index(u32 property_index, Value value, PropertyAttributes attributes, PutOwnPropertyMode mode, bool throw_exceptions)
  590. {
  591. VERIFY(!(mode == PutOwnPropertyMode::Put && value.is_accessor()));
  592. auto existing_property = m_indexed_properties.get(nullptr, property_index, false);
  593. auto new_property = !existing_property.has_value();
  594. if (!is_extensible() && new_property) {
  595. dbgln_if(OBJECT_DEBUG, "Disallow define_property of non-extensible object");
  596. if (throw_exceptions && vm().in_strict_mode())
  597. vm().throw_exception<TypeError>(global_object(), ErrorType::NonExtensibleDefine, property_index);
  598. return false;
  599. }
  600. if (value.is_accessor()) {
  601. auto& accessor = value.as_accessor();
  602. if (accessor.getter())
  603. attributes.set_has_getter();
  604. if (accessor.setter())
  605. attributes.set_has_setter();
  606. }
  607. PropertyAttributes existing_attributes = new_property ? 0 : existing_property.value().attributes;
  608. if (!new_property && mode == PutOwnPropertyMode::DefineProperty && !existing_attributes.is_configurable() && attributes != existing_attributes) {
  609. dbgln_if(OBJECT_DEBUG, "Disallow reconfig of non-configurable property");
  610. if (throw_exceptions)
  611. vm().throw_exception<TypeError>(global_object(), ErrorType::DescChangeNonConfigurable, property_index);
  612. return false;
  613. }
  614. auto value_here = new_property ? Value() : existing_property.value().value;
  615. if (!new_property && mode == PutOwnPropertyMode::Put && !value_here.is_accessor() && !existing_attributes.is_writable()) {
  616. dbgln_if(OBJECT_DEBUG, "Disallow write to non-writable property");
  617. return false;
  618. }
  619. if (value.is_empty())
  620. return true;
  621. if (value_here.is_native_property()) {
  622. call_native_property_setter(value_here.as_native_property(), this, value);
  623. } else {
  624. m_indexed_properties.put(this, property_index, value, attributes, mode == PutOwnPropertyMode::Put);
  625. }
  626. return true;
  627. }
  628. bool Object::delete_property(const PropertyName& property_name)
  629. {
  630. VERIFY(property_name.is_valid());
  631. if (property_name.is_number())
  632. return m_indexed_properties.remove(property_name.as_number());
  633. if (property_name.is_string()) {
  634. i32 property_index = property_name.as_string().to_int().value_or(-1);
  635. if (property_index >= 0)
  636. return m_indexed_properties.remove(property_index);
  637. }
  638. auto metadata = shape().lookup(property_name.to_string_or_symbol());
  639. if (!metadata.has_value())
  640. return true;
  641. if (!metadata.value().attributes.is_configurable())
  642. return false;
  643. size_t deleted_offset = metadata.value().offset;
  644. ensure_shape_is_unique();
  645. shape().remove_property_from_unique_shape(property_name.to_string_or_symbol(), deleted_offset);
  646. m_storage.remove(deleted_offset);
  647. return true;
  648. }
  649. void Object::ensure_shape_is_unique()
  650. {
  651. if (shape().is_unique())
  652. return;
  653. m_shape = m_shape->create_unique_clone();
  654. }
  655. Value Object::get_by_index(u32 property_index) const
  656. {
  657. const Object* object = this;
  658. while (object) {
  659. if (is<StringObject>(*object)) {
  660. auto& string = static_cast<const StringObject&>(*object).primitive_string().string();
  661. if (property_index < string.length())
  662. return js_string(heap(), string.substring(property_index, 1));
  663. } else if (static_cast<size_t>(property_index) < object->m_indexed_properties.array_like_size()) {
  664. auto result = object->m_indexed_properties.get(const_cast<Object*>(this), property_index);
  665. if (vm().exception())
  666. return {};
  667. if (result.has_value() && !result.value().value.is_empty())
  668. return result.value().value;
  669. }
  670. object = object->prototype();
  671. if (vm().exception())
  672. return {};
  673. }
  674. return {};
  675. }
  676. Value Object::get(const PropertyName& property_name, Value receiver, bool without_side_effects) const
  677. {
  678. VERIFY(property_name.is_valid());
  679. if (property_name.is_number())
  680. return get_by_index(property_name.as_number());
  681. if (property_name.is_string()) {
  682. auto& property_string = property_name.as_string();
  683. i32 property_index = property_string.to_int().value_or(-1);
  684. if (property_index >= 0)
  685. return get_by_index(property_index);
  686. }
  687. if (receiver.is_empty())
  688. receiver = Value(this);
  689. const Object* object = this;
  690. while (object) {
  691. auto value = object->get_own_property(property_name, receiver, without_side_effects);
  692. if (vm().exception())
  693. return {};
  694. if (!value.is_empty())
  695. return value;
  696. object = object->prototype();
  697. if (vm().exception())
  698. return {};
  699. }
  700. return {};
  701. }
  702. Value Object::get_without_side_effects(const PropertyName& property_name) const
  703. {
  704. TemporaryClearException clear_exception(vm());
  705. return get(property_name, {}, true);
  706. }
  707. bool Object::put_by_index(u32 property_index, Value value)
  708. {
  709. VERIFY(!value.is_empty());
  710. // If there's a setter in the prototype chain, we go to the setter.
  711. // Otherwise, it goes in the own property storage.
  712. Object* object = this;
  713. while (object) {
  714. auto existing_value = object->m_indexed_properties.get(nullptr, property_index, false);
  715. if (existing_value.has_value()) {
  716. auto value_here = existing_value.value();
  717. if (value_here.value.is_accessor()) {
  718. value_here.value.as_accessor().call_setter(object, value);
  719. return true;
  720. }
  721. if (value_here.value.is_native_property()) {
  722. // FIXME: Why doesn't put_by_index() receive the receiver value from put()?!
  723. auto receiver = this;
  724. call_native_property_setter(value_here.value.as_native_property(), receiver, value);
  725. return true;
  726. }
  727. }
  728. object = object->prototype();
  729. if (vm().exception())
  730. return {};
  731. }
  732. return put_own_property_by_index(property_index, value, default_attributes, PutOwnPropertyMode::Put);
  733. }
  734. bool Object::put(const PropertyName& property_name, Value value, Value receiver)
  735. {
  736. VERIFY(property_name.is_valid());
  737. if (property_name.is_number())
  738. return put_by_index(property_name.as_number(), value);
  739. VERIFY(!value.is_empty());
  740. if (property_name.is_string()) {
  741. auto& property_string = property_name.as_string();
  742. i32 property_index = property_string.to_int().value_or(-1);
  743. if (property_index >= 0)
  744. return put_by_index(property_index, value);
  745. }
  746. auto string_or_symbol = property_name.to_string_or_symbol();
  747. if (receiver.is_empty())
  748. receiver = Value(this);
  749. // If there's a setter in the prototype chain, we go to the setter.
  750. // Otherwise, it goes in the own property storage.
  751. Object* object = this;
  752. while (object) {
  753. auto metadata = object->shape().lookup(string_or_symbol);
  754. if (metadata.has_value()) {
  755. auto value_here = object->m_storage[metadata.value().offset];
  756. if (value_here.is_accessor()) {
  757. value_here.as_accessor().call_setter(receiver, value);
  758. return true;
  759. }
  760. if (value_here.is_native_property()) {
  761. call_native_property_setter(value_here.as_native_property(), receiver, value);
  762. return true;
  763. }
  764. }
  765. object = object->prototype();
  766. if (vm().exception())
  767. return false;
  768. }
  769. return put_own_property(string_or_symbol, value, default_attributes, PutOwnPropertyMode::Put);
  770. }
  771. bool Object::define_native_function(const StringOrSymbol& property_name, AK::Function<Value(VM&, GlobalObject&)> native_function, i32 length, PropertyAttributes attribute)
  772. {
  773. auto& vm = this->vm();
  774. String function_name;
  775. if (property_name.is_string()) {
  776. function_name = property_name.as_string();
  777. } else {
  778. function_name = String::formatted("[{}]", property_name.as_symbol()->description());
  779. }
  780. auto* function = NativeFunction::create(global_object(), function_name, move(native_function));
  781. function->define_property_without_transition(vm.names.length, Value(length), Attribute::Configurable);
  782. if (vm.exception())
  783. return {};
  784. function->define_property_without_transition(vm.names.name, js_string(vm.heap(), function_name), Attribute::Configurable);
  785. if (vm.exception())
  786. return {};
  787. return define_property(property_name, function, attribute);
  788. }
  789. bool Object::define_native_property(const StringOrSymbol& property_name, AK::Function<Value(VM&, GlobalObject&)> getter, AK::Function<void(VM&, GlobalObject&, Value)> setter, PropertyAttributes attribute)
  790. {
  791. return define_property(property_name, heap().allocate_without_global_object<NativeProperty>(move(getter), move(setter)), attribute);
  792. }
  793. // 20.1.2.3.1 ObjectDefineProperties, https://tc39.es/ecma262/#sec-objectdefineproperties
  794. void Object::define_properties(Value properties)
  795. {
  796. auto& vm = this->vm();
  797. auto* props = properties.to_object(global_object());
  798. if (!props)
  799. return;
  800. auto keys = props->get_own_properties(PropertyKind::Key);
  801. if (vm.exception())
  802. return;
  803. struct NameAndDescriptor {
  804. PropertyName name;
  805. PropertyDescriptor descriptor;
  806. };
  807. Vector<NameAndDescriptor> descriptors;
  808. for (auto& key : keys) {
  809. auto property_name = PropertyName::from_value(global_object(), key);
  810. auto property_descriptor = props->get_own_property_descriptor(property_name);
  811. if (property_descriptor.has_value() && property_descriptor->attributes.is_enumerable()) {
  812. auto descriptor_object = props->get(property_name);
  813. if (vm.exception())
  814. return;
  815. if (!descriptor_object.is_object()) {
  816. vm.throw_exception<TypeError>(global_object(), ErrorType::NotAnObject, descriptor_object.to_string_without_side_effects());
  817. return;
  818. }
  819. auto descriptor = PropertyDescriptor::from_dictionary(vm, descriptor_object.as_object());
  820. if (vm.exception())
  821. return;
  822. descriptors.append({ property_name, descriptor });
  823. }
  824. }
  825. for (auto& [name, descriptor] : descriptors) {
  826. // FIXME: The spec has both of this handled by DefinePropertyOrThrow(O, P, desc).
  827. // We should invest some time in improving object property handling, it not being
  828. // super close to the spec makes this and other things unnecessarily complicated.
  829. if (descriptor.is_accessor_descriptor())
  830. define_accessor(name, descriptor.getter, descriptor.setter, descriptor.attributes);
  831. else
  832. define_property(name, descriptor.value, descriptor.attributes);
  833. }
  834. }
  835. void Object::visit_edges(Cell::Visitor& visitor)
  836. {
  837. Cell::visit_edges(visitor);
  838. visitor.visit(m_shape);
  839. for (auto& value : m_storage)
  840. visitor.visit(value);
  841. m_indexed_properties.for_each_value([&visitor](auto& value) {
  842. visitor.visit(value);
  843. });
  844. }
  845. bool Object::has_property(const PropertyName& property_name) const
  846. {
  847. const Object* object = this;
  848. while (object) {
  849. if (object->has_own_property(property_name))
  850. return true;
  851. object = object->prototype();
  852. if (vm().exception())
  853. return false;
  854. }
  855. return false;
  856. }
  857. bool Object::has_own_property(const PropertyName& property_name) const
  858. {
  859. VERIFY(property_name.is_valid());
  860. auto has_indexed_property = [&](u32 index) -> bool {
  861. if (is<StringObject>(*this))
  862. return index < static_cast<const StringObject*>(this)->primitive_string().string().length();
  863. return m_indexed_properties.has_index(index);
  864. };
  865. if (property_name.is_number())
  866. return has_indexed_property(property_name.as_number());
  867. if (property_name.is_string()) {
  868. i32 property_index = property_name.as_string().to_int().value_or(-1);
  869. if (property_index >= 0)
  870. return has_indexed_property(property_index);
  871. }
  872. return shape().lookup(property_name.to_string_or_symbol()).has_value();
  873. }
  874. Value Object::ordinary_to_primitive(Value::PreferredType preferred_type) const
  875. {
  876. VERIFY(preferred_type == Value::PreferredType::String || preferred_type == Value::PreferredType::Number);
  877. auto& vm = this->vm();
  878. Vector<FlyString, 2> method_names;
  879. if (preferred_type == Value::PreferredType::String)
  880. method_names = { vm.names.toString, vm.names.valueOf };
  881. else
  882. method_names = { vm.names.valueOf, vm.names.toString };
  883. for (auto& method_name : method_names) {
  884. auto method = get(method_name);
  885. if (vm.exception())
  886. return {};
  887. if (method.is_function()) {
  888. auto result = vm.call(method.as_function(), const_cast<Object*>(this));
  889. if (!result.is_object())
  890. return result;
  891. }
  892. }
  893. vm.throw_exception<TypeError>(global_object(), ErrorType::Convert, "object", preferred_type == Value::PreferredType::String ? "string" : "number");
  894. return {};
  895. }
  896. Value Object::invoke_internal(const StringOrSymbol& property_name, Optional<MarkedValueList> arguments)
  897. {
  898. auto& vm = this->vm();
  899. auto property = get(property_name).value_or(js_undefined());
  900. if (vm.exception())
  901. return {};
  902. if (!property.is_function()) {
  903. vm.throw_exception<TypeError>(global_object(), ErrorType::NotAFunction, property.to_string_without_side_effects());
  904. return {};
  905. }
  906. return vm.call(property.as_function(), this, move(arguments));
  907. }
  908. Value Object::call_native_property_getter(NativeProperty& property, Value this_value) const
  909. {
  910. auto& vm = this->vm();
  911. CallFrame call_frame;
  912. if (auto* interpreter = vm.interpreter_if_exists())
  913. call_frame.current_node = interpreter->current_node();
  914. call_frame.is_strict_mode = vm.in_strict_mode();
  915. call_frame.this_value = this_value;
  916. vm.push_call_frame(call_frame, global_object());
  917. if (vm.exception())
  918. return {};
  919. auto result = property.get(vm, global_object());
  920. vm.pop_call_frame();
  921. return result;
  922. }
  923. void Object::call_native_property_setter(NativeProperty& property, Value this_value, Value setter_value) const
  924. {
  925. auto& vm = this->vm();
  926. CallFrame call_frame;
  927. if (auto* interpreter = vm.interpreter_if_exists())
  928. call_frame.current_node = interpreter->current_node();
  929. call_frame.is_strict_mode = vm.in_strict_mode();
  930. call_frame.this_value = this_value;
  931. vm.push_call_frame(call_frame, global_object());
  932. if (vm.exception())
  933. return;
  934. property.set(vm, global_object(), setter_value);
  935. vm.pop_call_frame();
  936. }
  937. }