Object.cpp 39 KB

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