Object.cpp 40 KB

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