Object.cpp 40 KB

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