Object.cpp 45 KB

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