Object.cpp 37 KB

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