Object.cpp 40 KB

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