Object.cpp 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113
  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. if (return_type != GetOwnPropertyReturnType::SymbolOnly) {
  284. for (auto& entry : m_indexed_properties) {
  285. auto value_and_attributes = entry.value_and_attributes(const_cast<Object*>(this));
  286. if (only_enumerable_properties && !value_and_attributes.attributes.is_enumerable())
  287. continue;
  288. if (kind == PropertyKind::Key) {
  289. properties.append(js_string(vm(), String::number(entry.index())));
  290. } else if (kind == PropertyKind::Value) {
  291. properties.append(value_and_attributes.value);
  292. } else {
  293. auto* entry_array = Array::create(global_object());
  294. entry_array->define_property(0, js_string(vm(), String::number(entry.index())));
  295. entry_array->define_property(1, value_and_attributes.value);
  296. properties.append(entry_array);
  297. }
  298. if (vm().exception())
  299. return MarkedValueList { heap() };
  300. }
  301. }
  302. auto add_property_to_results = [&](auto& property) {
  303. if (kind == PropertyKind::Key) {
  304. properties.append(property.key.to_value(vm()));
  305. } else if (kind == PropertyKind::Value) {
  306. properties.append(get(property.key));
  307. } else {
  308. auto* entry_array = Array::create(global_object());
  309. entry_array->define_property(0, property.key.to_value(vm()));
  310. entry_array->define_property(1, get(property.key));
  311. properties.append(entry_array);
  312. }
  313. };
  314. // NOTE: Most things including for..in/of and Object.{keys,values,entries}() use StringOnly, and in those
  315. // cases we won't be iterating the ordered property table twice. We can certainly improve this though.
  316. if (return_type == GetOwnPropertyReturnType::All || return_type == GetOwnPropertyReturnType::StringOnly) {
  317. for (auto& it : shape().property_table_ordered()) {
  318. if (only_enumerable_properties && !it.value.attributes.is_enumerable())
  319. continue;
  320. if (it.key.is_symbol())
  321. continue;
  322. add_property_to_results(it);
  323. if (vm().exception())
  324. return MarkedValueList { heap() };
  325. }
  326. }
  327. if (return_type == GetOwnPropertyReturnType::All || return_type == GetOwnPropertyReturnType::SymbolOnly) {
  328. for (auto& it : shape().property_table_ordered()) {
  329. if (only_enumerable_properties && !it.value.attributes.is_enumerable())
  330. continue;
  331. if (it.key.is_string())
  332. continue;
  333. add_property_to_results(it);
  334. if (vm().exception())
  335. return MarkedValueList { heap() };
  336. }
  337. }
  338. return properties;
  339. }
  340. // 7.3.23 EnumerableOwnPropertyNames, https://tc39.es/ecma262/#sec-enumerableownpropertynames
  341. MarkedValueList Object::get_enumerable_own_property_names(PropertyKind kind) const
  342. {
  343. return get_own_properties(kind, true, Object::GetOwnPropertyReturnType::StringOnly);
  344. }
  345. Optional<PropertyDescriptor> Object::get_own_property_descriptor(const PropertyName& property_name) const
  346. {
  347. VERIFY(property_name.is_valid());
  348. Value value;
  349. PropertyAttributes attributes;
  350. if (property_name.is_number()) {
  351. auto existing_value = m_indexed_properties.get(nullptr, property_name.as_number(), false);
  352. if (!existing_value.has_value())
  353. return {};
  354. value = existing_value.value().value;
  355. attributes = existing_value.value().attributes;
  356. } else {
  357. if (property_name.is_string()) {
  358. i32 property_index = property_name.as_string().to_int().value_or(-1);
  359. if (property_index >= 0)
  360. return get_own_property_descriptor(property_index);
  361. }
  362. auto metadata = shape().lookup(property_name.to_string_or_symbol());
  363. if (!metadata.has_value())
  364. return {};
  365. value = m_storage[metadata.value().offset];
  366. attributes = metadata.value().attributes;
  367. }
  368. PropertyDescriptor descriptor { attributes, {}, nullptr, nullptr };
  369. if (value.is_native_property()) {
  370. auto result = call_native_property_getter(value.as_native_property(), const_cast<Object*>(this));
  371. descriptor.value = result.value_or(js_undefined());
  372. } else if (value.is_accessor()) {
  373. auto& pair = value.as_accessor();
  374. if (pair.getter())
  375. descriptor.getter = pair.getter();
  376. if (pair.setter())
  377. descriptor.setter = pair.setter();
  378. } else {
  379. descriptor.value = value.value_or(js_undefined());
  380. }
  381. return descriptor;
  382. }
  383. // Equivalent to:
  384. // 6.2.5.4 FromPropertyDescriptor, https://tc39.es/ecma262/#sec-frompropertydescriptor
  385. Value Object::get_own_property_descriptor_object(const PropertyName& property_name) const
  386. {
  387. VERIFY(property_name.is_valid());
  388. auto& vm = this->vm();
  389. auto descriptor_opt = get_own_property_descriptor(property_name);
  390. if (!descriptor_opt.has_value())
  391. return js_undefined();
  392. auto descriptor = descriptor_opt.value();
  393. auto* descriptor_object = Object::create_empty(global_object());
  394. if (descriptor.is_data_descriptor()) {
  395. descriptor_object->define_property(vm.names.value, descriptor.value.value_or(js_undefined()));
  396. descriptor_object->define_property(vm.names.writable, Value(descriptor.attributes.is_writable()));
  397. } else {
  398. VERIFY(descriptor.is_accessor_descriptor());
  399. descriptor_object->define_property(vm.names.get, descriptor.getter ? Value(descriptor.getter) : js_undefined());
  400. descriptor_object->define_property(vm.names.set, descriptor.setter ? Value(descriptor.setter) : js_undefined());
  401. }
  402. descriptor_object->define_property(vm.names.enumerable, Value(descriptor.attributes.is_enumerable()));
  403. descriptor_object->define_property(vm.names.configurable, Value(descriptor.attributes.is_configurable()));
  404. return descriptor_object;
  405. }
  406. void Object::set_shape(Shape& new_shape)
  407. {
  408. m_storage.resize(new_shape.property_count());
  409. m_shape = &new_shape;
  410. }
  411. bool Object::define_property(const StringOrSymbol& property_name, const Object& descriptor, bool throw_exceptions)
  412. {
  413. auto& vm = this->vm();
  414. bool is_accessor_property = descriptor.has_property(vm.names.get) || descriptor.has_property(vm.names.set);
  415. PropertyAttributes attributes;
  416. if (descriptor.has_property(vm.names.configurable)) {
  417. attributes.set_has_configurable();
  418. if (descriptor.get(vm.names.configurable).value_or(Value(false)).to_boolean())
  419. attributes.set_configurable();
  420. if (vm.exception())
  421. return false;
  422. }
  423. if (descriptor.has_property(vm.names.enumerable)) {
  424. attributes.set_has_enumerable();
  425. if (descriptor.get(vm.names.enumerable).value_or(Value(false)).to_boolean())
  426. attributes.set_enumerable();
  427. if (vm.exception())
  428. return false;
  429. }
  430. if (is_accessor_property) {
  431. if (descriptor.has_property(vm.names.value) || descriptor.has_property(vm.names.writable)) {
  432. if (throw_exceptions)
  433. vm.throw_exception<TypeError>(global_object(), ErrorType::AccessorValueOrWritable);
  434. return false;
  435. }
  436. auto getter = descriptor.get(vm.names.get).value_or(js_undefined());
  437. if (vm.exception())
  438. return {};
  439. auto setter = descriptor.get(vm.names.set).value_or(js_undefined());
  440. if (vm.exception())
  441. return {};
  442. Function* getter_function { nullptr };
  443. Function* setter_function { nullptr };
  444. if (getter.is_function()) {
  445. getter_function = &getter.as_function();
  446. } else if (!getter.is_undefined()) {
  447. vm.throw_exception<TypeError>(global_object(), ErrorType::AccessorBadField, "get");
  448. return false;
  449. }
  450. if (setter.is_function()) {
  451. setter_function = &setter.as_function();
  452. } else if (!setter.is_undefined()) {
  453. vm.throw_exception<TypeError>(global_object(), ErrorType::AccessorBadField, "set");
  454. return false;
  455. }
  456. dbgln_if(OBJECT_DEBUG, "Defining new property {} with accessor descriptor {{ attributes={}, getter={}, setter={} }}", property_name.to_display_string(), attributes, getter, setter);
  457. return define_property(property_name, Accessor::create(vm, getter_function, setter_function), attributes, throw_exceptions);
  458. }
  459. auto value = descriptor.get(vm.names.value);
  460. if (vm.exception())
  461. return {};
  462. if (descriptor.has_property(vm.names.writable)) {
  463. attributes.set_has_writable();
  464. if (descriptor.get(vm.names.writable).value_or(Value(false)).to_boolean())
  465. attributes.set_writable();
  466. if (vm.exception())
  467. return false;
  468. }
  469. if (vm.exception())
  470. return {};
  471. dbgln_if(OBJECT_DEBUG, "Defining new property {} with data descriptor {{ attributes={}, value={} }}", property_name.to_display_string(), attributes, value);
  472. return define_property(property_name, value, attributes, throw_exceptions);
  473. }
  474. bool Object::define_property_without_transition(const PropertyName& property_name, Value value, PropertyAttributes attributes, bool throw_exceptions)
  475. {
  476. TemporaryChange change(m_transitions_enabled, false);
  477. return define_property(property_name, value, attributes, throw_exceptions);
  478. }
  479. bool Object::define_property(const PropertyName& property_name, Value value, PropertyAttributes attributes, bool throw_exceptions)
  480. {
  481. VERIFY(property_name.is_valid());
  482. if (property_name.is_number())
  483. return put_own_property_by_index(property_name.as_number(), value, attributes, PutOwnPropertyMode::DefineProperty, throw_exceptions);
  484. if (property_name.is_string()) {
  485. i32 property_index = property_name.as_string().to_int().value_or(-1);
  486. if (property_index >= 0)
  487. return put_own_property_by_index(property_index, value, attributes, PutOwnPropertyMode::DefineProperty, throw_exceptions);
  488. }
  489. return put_own_property(property_name.to_string_or_symbol(), value, attributes, PutOwnPropertyMode::DefineProperty, throw_exceptions);
  490. }
  491. bool Object::define_native_accessor(const StringOrSymbol& property_name, AK::Function<Value(VM&, GlobalObject&)> getter, AK::Function<Value(VM&, GlobalObject&)> setter, PropertyAttributes attribute)
  492. {
  493. auto& vm = this->vm();
  494. String formatted_property_name;
  495. if (property_name.is_string()) {
  496. formatted_property_name = property_name.as_string();
  497. } else {
  498. formatted_property_name = String::formatted("[{}]", property_name.as_symbol()->description());
  499. }
  500. Function* getter_function = nullptr;
  501. if (getter) {
  502. auto name = String::formatted("get {}", formatted_property_name);
  503. getter_function = NativeFunction::create(global_object(), name, move(getter));
  504. getter_function->define_property_without_transition(vm.names.length, Value(0), Attribute::Configurable);
  505. if (vm.exception())
  506. return {};
  507. getter_function->define_property_without_transition(vm.names.name, js_string(vm.heap(), name), Attribute::Configurable);
  508. if (vm.exception())
  509. return {};
  510. }
  511. Function* setter_function = nullptr;
  512. if (setter) {
  513. auto name = String::formatted("set {}", formatted_property_name);
  514. setter_function = NativeFunction::create(global_object(), name, move(setter));
  515. setter_function->define_property_without_transition(vm.names.length, Value(1), Attribute::Configurable);
  516. if (vm.exception())
  517. return {};
  518. setter_function->define_property_without_transition(vm.names.name, js_string(vm.heap(), name), Attribute::Configurable);
  519. if (vm.exception())
  520. return {};
  521. }
  522. return define_accessor(property_name, getter_function, setter_function, attribute);
  523. }
  524. bool Object::define_accessor(const PropertyName& property_name, Function* getter, Function* setter, PropertyAttributes attributes, bool throw_exceptions)
  525. {
  526. VERIFY(property_name.is_valid());
  527. Accessor* accessor { nullptr };
  528. auto property_metadata = shape().lookup(property_name.to_string_or_symbol());
  529. if (property_metadata.has_value()) {
  530. auto existing_property = get_direct(property_metadata.value().offset);
  531. if (existing_property.is_accessor())
  532. accessor = &existing_property.as_accessor();
  533. }
  534. if (!accessor) {
  535. accessor = Accessor::create(vm(), getter, setter);
  536. bool definition_success = define_property(property_name, accessor, attributes, throw_exceptions);
  537. if (vm().exception())
  538. return {};
  539. if (!definition_success)
  540. return false;
  541. } else {
  542. if (getter)
  543. accessor->set_getter(getter);
  544. if (setter)
  545. accessor->set_setter(setter);
  546. }
  547. return true;
  548. }
  549. bool Object::put_own_property(const StringOrSymbol& property_name, Value value, PropertyAttributes attributes, PutOwnPropertyMode mode, bool throw_exceptions)
  550. {
  551. VERIFY(!(mode == PutOwnPropertyMode::Put && value.is_accessor()));
  552. if (value.is_accessor()) {
  553. auto& accessor = value.as_accessor();
  554. if (accessor.getter())
  555. attributes.set_has_getter();
  556. if (accessor.setter())
  557. attributes.set_has_setter();
  558. }
  559. // NOTE: We disable transitions during initialize(), this makes building common runtime objects significantly faster.
  560. // Transitions are primarily interesting when scripts add properties to objects.
  561. if (!m_transitions_enabled && !m_shape->is_unique()) {
  562. m_shape->add_property_without_transition(property_name, attributes);
  563. m_storage.resize(m_shape->property_count());
  564. m_storage[m_shape->property_count() - 1] = value;
  565. return true;
  566. }
  567. auto metadata = shape().lookup(property_name);
  568. bool new_property = !metadata.has_value();
  569. if (!is_extensible() && new_property) {
  570. dbgln_if(OBJECT_DEBUG, "Disallow define_property of non-extensible object");
  571. if (throw_exceptions && vm().in_strict_mode())
  572. vm().throw_exception<TypeError>(global_object(), ErrorType::NonExtensibleDefine, property_name.to_display_string());
  573. return false;
  574. }
  575. if (new_property) {
  576. if (!m_shape->is_unique() && shape().property_count() > 100) {
  577. // If you add more than 100 properties to an object, let's stop doing
  578. // transitions to avoid filling up the heap with shapes.
  579. ensure_shape_is_unique();
  580. }
  581. if (m_shape->is_unique()) {
  582. m_shape->add_property_to_unique_shape(property_name, attributes);
  583. m_storage.resize(m_shape->property_count());
  584. } else if (m_transitions_enabled) {
  585. set_shape(*m_shape->create_put_transition(property_name, attributes));
  586. } else {
  587. m_shape->add_property_without_transition(property_name, attributes);
  588. m_storage.resize(m_shape->property_count());
  589. }
  590. metadata = shape().lookup(property_name);
  591. VERIFY(metadata.has_value());
  592. }
  593. if (!new_property && mode == PutOwnPropertyMode::DefineProperty && !metadata.value().attributes.is_configurable() && attributes != metadata.value().attributes) {
  594. dbgln_if(OBJECT_DEBUG, "Disallow reconfig of non-configurable property");
  595. if (throw_exceptions)
  596. vm().throw_exception<TypeError>(global_object(), ErrorType::DescChangeNonConfigurable, property_name.to_display_string());
  597. return false;
  598. }
  599. if (mode == PutOwnPropertyMode::DefineProperty && attributes != metadata.value().attributes) {
  600. if (m_shape->is_unique()) {
  601. m_shape->reconfigure_property_in_unique_shape(property_name, attributes);
  602. } else {
  603. set_shape(*m_shape->create_configure_transition(property_name, attributes));
  604. }
  605. metadata = shape().lookup(property_name);
  606. 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());
  607. }
  608. auto value_here = m_storage[metadata.value().offset];
  609. if (!new_property && mode == PutOwnPropertyMode::Put && !value_here.is_accessor() && !metadata.value().attributes.is_writable()) {
  610. dbgln_if(OBJECT_DEBUG, "Disallow write to non-writable property");
  611. if (throw_exceptions && vm().in_strict_mode())
  612. vm().throw_exception<TypeError>(global_object(), ErrorType::DescWriteNonWritable, property_name.to_display_string());
  613. return false;
  614. }
  615. if (value.is_empty())
  616. return true;
  617. if (value_here.is_native_property()) {
  618. call_native_property_setter(value_here.as_native_property(), this, value);
  619. } else {
  620. m_storage[metadata.value().offset] = value;
  621. }
  622. return true;
  623. }
  624. bool Object::put_own_property_by_index(u32 property_index, Value value, PropertyAttributes attributes, PutOwnPropertyMode mode, bool throw_exceptions)
  625. {
  626. VERIFY(!(mode == PutOwnPropertyMode::Put && value.is_accessor()));
  627. auto existing_property = m_indexed_properties.get(nullptr, property_index, false);
  628. auto new_property = !existing_property.has_value();
  629. if (!is_extensible() && new_property) {
  630. dbgln_if(OBJECT_DEBUG, "Disallow define_property of non-extensible object");
  631. if (throw_exceptions && vm().in_strict_mode())
  632. vm().throw_exception<TypeError>(global_object(), ErrorType::NonExtensibleDefine, property_index);
  633. return false;
  634. }
  635. if (value.is_accessor()) {
  636. auto& accessor = value.as_accessor();
  637. if (accessor.getter())
  638. attributes.set_has_getter();
  639. if (accessor.setter())
  640. attributes.set_has_setter();
  641. }
  642. PropertyAttributes existing_attributes = new_property ? 0 : existing_property.value().attributes;
  643. if (!new_property && mode == PutOwnPropertyMode::DefineProperty && !existing_attributes.is_configurable() && attributes != existing_attributes) {
  644. dbgln_if(OBJECT_DEBUG, "Disallow reconfig of non-configurable property");
  645. if (throw_exceptions)
  646. vm().throw_exception<TypeError>(global_object(), ErrorType::DescChangeNonConfigurable, property_index);
  647. return false;
  648. }
  649. auto value_here = new_property ? Value() : existing_property.value().value;
  650. if (!new_property && mode == PutOwnPropertyMode::Put && !value_here.is_accessor() && !existing_attributes.is_writable()) {
  651. dbgln_if(OBJECT_DEBUG, "Disallow write to non-writable property");
  652. return false;
  653. }
  654. if (value.is_empty())
  655. return true;
  656. if (value_here.is_native_property()) {
  657. call_native_property_setter(value_here.as_native_property(), this, value);
  658. } else {
  659. m_indexed_properties.put(this, property_index, value, attributes, mode == PutOwnPropertyMode::Put);
  660. }
  661. return true;
  662. }
  663. bool Object::delete_property(const PropertyName& property_name)
  664. {
  665. VERIFY(property_name.is_valid());
  666. if (property_name.is_number())
  667. return m_indexed_properties.remove(property_name.as_number());
  668. if (property_name.is_string()) {
  669. i32 property_index = property_name.as_string().to_int().value_or(-1);
  670. if (property_index >= 0)
  671. return m_indexed_properties.remove(property_index);
  672. }
  673. auto metadata = shape().lookup(property_name.to_string_or_symbol());
  674. if (!metadata.has_value())
  675. return true;
  676. if (!metadata.value().attributes.is_configurable())
  677. return false;
  678. size_t deleted_offset = metadata.value().offset;
  679. ensure_shape_is_unique();
  680. shape().remove_property_from_unique_shape(property_name.to_string_or_symbol(), deleted_offset);
  681. m_storage.remove(deleted_offset);
  682. return true;
  683. }
  684. void Object::ensure_shape_is_unique()
  685. {
  686. if (shape().is_unique())
  687. return;
  688. m_shape = m_shape->create_unique_clone();
  689. }
  690. Value Object::get_by_index(u32 property_index) const
  691. {
  692. const Object* object = this;
  693. while (object) {
  694. if (is<StringObject>(*object)) {
  695. auto& string = static_cast<const StringObject&>(*object).primitive_string().string();
  696. if (property_index < string.length())
  697. return js_string(heap(), string.substring(property_index, 1));
  698. } else if (static_cast<size_t>(property_index) < object->m_indexed_properties.array_like_size()) {
  699. auto result = object->m_indexed_properties.get(const_cast<Object*>(this), property_index);
  700. if (vm().exception())
  701. return {};
  702. if (result.has_value() && !result.value().value.is_empty())
  703. return result.value().value;
  704. }
  705. object = object->prototype();
  706. if (vm().exception())
  707. return {};
  708. }
  709. return {};
  710. }
  711. Value Object::get(const PropertyName& property_name, Value receiver, bool without_side_effects) const
  712. {
  713. VERIFY(property_name.is_valid());
  714. if (property_name.is_number())
  715. return get_by_index(property_name.as_number());
  716. if (property_name.is_string()) {
  717. auto& property_string = property_name.as_string();
  718. i32 property_index = property_string.to_int().value_or(-1);
  719. if (property_index >= 0)
  720. return get_by_index(property_index);
  721. }
  722. if (receiver.is_empty())
  723. receiver = Value(this);
  724. const Object* object = this;
  725. while (object) {
  726. auto value = object->get_own_property(property_name, receiver, without_side_effects);
  727. if (vm().exception())
  728. return {};
  729. if (!value.is_empty())
  730. return value;
  731. object = object->prototype();
  732. if (vm().exception())
  733. return {};
  734. }
  735. return {};
  736. }
  737. Value Object::get_without_side_effects(const PropertyName& property_name) const
  738. {
  739. TemporaryClearException clear_exception(vm());
  740. return get(property_name, {}, true);
  741. }
  742. bool Object::put_by_index(u32 property_index, Value value)
  743. {
  744. VERIFY(!value.is_empty());
  745. // If there's a setter in the prototype chain, we go to the setter.
  746. // Otherwise, it goes in the own property storage.
  747. Object* object = this;
  748. while (object) {
  749. auto existing_value = object->m_indexed_properties.get(nullptr, property_index, false);
  750. if (existing_value.has_value()) {
  751. auto value_here = existing_value.value();
  752. if (value_here.value.is_accessor()) {
  753. value_here.value.as_accessor().call_setter(object, value);
  754. return true;
  755. }
  756. if (value_here.value.is_native_property()) {
  757. // FIXME: Why doesn't put_by_index() receive the receiver value from put()?!
  758. auto receiver = this;
  759. call_native_property_setter(value_here.value.as_native_property(), receiver, value);
  760. return true;
  761. }
  762. }
  763. object = object->prototype();
  764. if (vm().exception())
  765. return {};
  766. }
  767. return put_own_property_by_index(property_index, value, default_attributes, PutOwnPropertyMode::Put);
  768. }
  769. bool Object::put(const PropertyName& property_name, Value value, Value receiver)
  770. {
  771. VERIFY(property_name.is_valid());
  772. if (property_name.is_number())
  773. return put_by_index(property_name.as_number(), value);
  774. VERIFY(!value.is_empty());
  775. if (property_name.is_string()) {
  776. auto& property_string = property_name.as_string();
  777. i32 property_index = property_string.to_int().value_or(-1);
  778. if (property_index >= 0)
  779. return put_by_index(property_index, value);
  780. }
  781. auto string_or_symbol = property_name.to_string_or_symbol();
  782. if (receiver.is_empty())
  783. receiver = Value(this);
  784. // If there's a setter in the prototype chain, we go to the setter.
  785. // Otherwise, it goes in the own property storage.
  786. Object* object = this;
  787. while (object) {
  788. auto metadata = object->shape().lookup(string_or_symbol);
  789. if (metadata.has_value()) {
  790. auto value_here = object->m_storage[metadata.value().offset];
  791. if (value_here.is_accessor()) {
  792. value_here.as_accessor().call_setter(receiver, value);
  793. return true;
  794. }
  795. if (value_here.is_native_property()) {
  796. call_native_property_setter(value_here.as_native_property(), receiver, value);
  797. return true;
  798. }
  799. }
  800. object = object->prototype();
  801. if (vm().exception())
  802. return false;
  803. }
  804. return put_own_property(string_or_symbol, value, default_attributes, PutOwnPropertyMode::Put);
  805. }
  806. bool Object::define_native_function(const StringOrSymbol& property_name, AK::Function<Value(VM&, GlobalObject&)> native_function, i32 length, PropertyAttributes attribute)
  807. {
  808. auto& vm = this->vm();
  809. String function_name;
  810. if (property_name.is_string()) {
  811. function_name = property_name.as_string();
  812. } else {
  813. function_name = String::formatted("[{}]", property_name.as_symbol()->description());
  814. }
  815. auto* function = NativeFunction::create(global_object(), function_name, move(native_function));
  816. function->define_property_without_transition(vm.names.length, Value(length), Attribute::Configurable);
  817. if (vm.exception())
  818. return {};
  819. function->define_property_without_transition(vm.names.name, js_string(vm.heap(), function_name), Attribute::Configurable);
  820. if (vm.exception())
  821. return {};
  822. return define_property(property_name, function, attribute);
  823. }
  824. bool Object::define_native_property(const StringOrSymbol& property_name, AK::Function<Value(VM&, GlobalObject&)> getter, AK::Function<void(VM&, GlobalObject&, Value)> setter, PropertyAttributes attribute)
  825. {
  826. return define_property(property_name, heap().allocate_without_global_object<NativeProperty>(move(getter), move(setter)), attribute);
  827. }
  828. // 20.1.2.3.1 ObjectDefineProperties, https://tc39.es/ecma262/#sec-objectdefineproperties
  829. void Object::define_properties(Value properties)
  830. {
  831. auto& vm = this->vm();
  832. auto* props = properties.to_object(global_object());
  833. if (!props)
  834. return;
  835. auto keys = props->get_own_properties(PropertyKind::Key);
  836. if (vm.exception())
  837. return;
  838. struct NameAndDescriptor {
  839. PropertyName name;
  840. PropertyDescriptor descriptor;
  841. };
  842. Vector<NameAndDescriptor> descriptors;
  843. for (auto& key : keys) {
  844. auto property_name = PropertyName::from_value(global_object(), key);
  845. auto property_descriptor = props->get_own_property_descriptor(property_name);
  846. if (property_descriptor.has_value() && property_descriptor->attributes.is_enumerable()) {
  847. auto descriptor_object = props->get(property_name);
  848. if (vm.exception())
  849. return;
  850. if (!descriptor_object.is_object()) {
  851. vm.throw_exception<TypeError>(global_object(), ErrorType::NotAnObject, descriptor_object.to_string_without_side_effects());
  852. return;
  853. }
  854. auto descriptor = PropertyDescriptor::from_dictionary(vm, descriptor_object.as_object());
  855. if (vm.exception())
  856. return;
  857. descriptors.append({ property_name, descriptor });
  858. }
  859. }
  860. for (auto& [name, descriptor] : descriptors) {
  861. // FIXME: The spec has both of this handled by DefinePropertyOrThrow(O, P, desc).
  862. // We should invest some time in improving object property handling, it not being
  863. // super close to the spec makes this and other things unnecessarily complicated.
  864. if (descriptor.is_accessor_descriptor())
  865. define_accessor(name, descriptor.getter, descriptor.setter, descriptor.attributes);
  866. else
  867. define_property(name, descriptor.value, descriptor.attributes);
  868. }
  869. }
  870. void Object::visit_edges(Cell::Visitor& visitor)
  871. {
  872. Cell::visit_edges(visitor);
  873. visitor.visit(m_shape);
  874. for (auto& value : m_storage)
  875. visitor.visit(value);
  876. m_indexed_properties.for_each_value([&visitor](auto& value) {
  877. visitor.visit(value);
  878. });
  879. }
  880. bool Object::has_property(const PropertyName& property_name) const
  881. {
  882. const Object* object = this;
  883. while (object) {
  884. if (object->has_own_property(property_name))
  885. return true;
  886. object = object->prototype();
  887. if (vm().exception())
  888. return false;
  889. }
  890. return false;
  891. }
  892. bool Object::has_own_property(const PropertyName& property_name) const
  893. {
  894. VERIFY(property_name.is_valid());
  895. auto has_indexed_property = [&](u32 index) -> bool {
  896. if (is<StringObject>(*this))
  897. return index < static_cast<const StringObject*>(this)->primitive_string().string().length();
  898. return m_indexed_properties.has_index(index);
  899. };
  900. if (property_name.is_number())
  901. return has_indexed_property(property_name.as_number());
  902. if (property_name.is_string()) {
  903. i32 property_index = property_name.as_string().to_int().value_or(-1);
  904. if (property_index >= 0)
  905. return has_indexed_property(property_index);
  906. }
  907. return shape().lookup(property_name.to_string_or_symbol()).has_value();
  908. }
  909. Value Object::ordinary_to_primitive(Value::PreferredType preferred_type) const
  910. {
  911. VERIFY(preferred_type == Value::PreferredType::String || preferred_type == Value::PreferredType::Number);
  912. auto& vm = this->vm();
  913. Vector<FlyString, 2> method_names;
  914. if (preferred_type == Value::PreferredType::String)
  915. method_names = { vm.names.toString, vm.names.valueOf };
  916. else
  917. method_names = { vm.names.valueOf, vm.names.toString };
  918. for (auto& method_name : method_names) {
  919. auto method = get(method_name);
  920. if (vm.exception())
  921. return {};
  922. if (method.is_function()) {
  923. auto result = vm.call(method.as_function(), const_cast<Object*>(this));
  924. if (!result.is_object())
  925. return result;
  926. }
  927. }
  928. vm.throw_exception<TypeError>(global_object(), ErrorType::Convert, "object", preferred_type == Value::PreferredType::String ? "string" : "number");
  929. return {};
  930. }
  931. // 20.5.8.1 InstallErrorCause, https://tc39.es/proposal-error-cause/#sec-errorobjects-install-error-cause
  932. void Object::install_error_cause(Value options)
  933. {
  934. auto& vm = this->vm();
  935. if (!options.is_object())
  936. return;
  937. auto& options_object = options.as_object();
  938. if (!options_object.has_property(vm.names.cause))
  939. return;
  940. auto cause = options_object.get(vm.names.cause).value_or(js_undefined());
  941. if (vm.exception())
  942. return;
  943. define_property(vm.names.cause, cause, Attribute::Writable | Attribute::Configurable);
  944. }
  945. Value Object::invoke_internal(const StringOrSymbol& property_name, Optional<MarkedValueList> arguments)
  946. {
  947. auto& vm = this->vm();
  948. auto property = get(property_name).value_or(js_undefined());
  949. if (vm.exception())
  950. return {};
  951. if (!property.is_function()) {
  952. vm.throw_exception<TypeError>(global_object(), ErrorType::NotAFunction, property.to_string_without_side_effects());
  953. return {};
  954. }
  955. return vm.call(property.as_function(), this, move(arguments));
  956. }
  957. Value Object::call_native_property_getter(NativeProperty& property, Value this_value) const
  958. {
  959. auto& vm = this->vm();
  960. CallFrame call_frame;
  961. if (auto* interpreter = vm.interpreter_if_exists())
  962. call_frame.current_node = interpreter->current_node();
  963. call_frame.is_strict_mode = vm.in_strict_mode();
  964. call_frame.this_value = this_value;
  965. vm.push_call_frame(call_frame, global_object());
  966. if (vm.exception())
  967. return {};
  968. auto result = property.get(vm, global_object());
  969. vm.pop_call_frame();
  970. return result;
  971. }
  972. void Object::call_native_property_setter(NativeProperty& property, Value this_value, Value setter_value) const
  973. {
  974. auto& vm = this->vm();
  975. CallFrame call_frame;
  976. if (auto* interpreter = vm.interpreter_if_exists())
  977. call_frame.current_node = interpreter->current_node();
  978. call_frame.is_strict_mode = vm.in_strict_mode();
  979. call_frame.this_value = this_value;
  980. vm.push_call_frame(call_frame, global_object());
  981. if (vm.exception())
  982. return;
  983. property.set(vm, global_object(), setter_value);
  984. vm.pop_call_frame();
  985. }
  986. }