Object.cpp 42 KB

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