Object.cpp 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142
  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. auto existing_property = get_own_property(property_name, this, true);
  535. auto* accessor = existing_property.is_accessor() ? &existing_property.as_accessor() : nullptr;
  536. if (!accessor) {
  537. accessor = Accessor::create(vm(), getter, setter);
  538. bool definition_success = define_property(property_name, accessor, attributes, throw_exceptions);
  539. if (vm().exception())
  540. return {};
  541. if (!definition_success)
  542. return false;
  543. } else {
  544. if (getter)
  545. accessor->set_getter(getter);
  546. if (setter)
  547. accessor->set_setter(setter);
  548. }
  549. return true;
  550. }
  551. bool Object::put_own_property(const StringOrSymbol& property_name, Value value, PropertyAttributes attributes, PutOwnPropertyMode mode, bool throw_exceptions)
  552. {
  553. VERIFY(!(mode == PutOwnPropertyMode::Put && value.is_accessor()));
  554. if (value.is_accessor()) {
  555. auto& accessor = value.as_accessor();
  556. if (accessor.getter())
  557. attributes.set_has_getter();
  558. if (accessor.setter())
  559. attributes.set_has_setter();
  560. }
  561. // NOTE: We disable transitions during initialize(), this makes building common runtime objects significantly faster.
  562. // Transitions are primarily interesting when scripts add properties to objects.
  563. if (!m_transitions_enabled && !m_shape->is_unique()) {
  564. m_shape->add_property_without_transition(property_name, attributes);
  565. m_storage.resize(m_shape->property_count());
  566. m_storage[m_shape->property_count() - 1] = value;
  567. return true;
  568. }
  569. auto metadata = shape().lookup(property_name);
  570. bool new_property = !metadata.has_value();
  571. if (!is_extensible() && new_property) {
  572. dbgln_if(OBJECT_DEBUG, "Disallow define_property of non-extensible object");
  573. if (throw_exceptions && vm().in_strict_mode())
  574. vm().throw_exception<TypeError>(global_object(), ErrorType::NonExtensibleDefine, property_name.to_display_string());
  575. return false;
  576. }
  577. if (new_property) {
  578. if (!m_shape->is_unique() && shape().property_count() > 100) {
  579. // If you add more than 100 properties to an object, let's stop doing
  580. // transitions to avoid filling up the heap with shapes.
  581. ensure_shape_is_unique();
  582. }
  583. if (m_shape->is_unique()) {
  584. m_shape->add_property_to_unique_shape(property_name, attributes);
  585. m_storage.resize(m_shape->property_count());
  586. } else if (m_transitions_enabled) {
  587. set_shape(*m_shape->create_put_transition(property_name, attributes));
  588. } else {
  589. m_shape->add_property_without_transition(property_name, attributes);
  590. m_storage.resize(m_shape->property_count());
  591. }
  592. metadata = shape().lookup(property_name);
  593. VERIFY(metadata.has_value());
  594. }
  595. auto value_here = m_storage[metadata.value().offset];
  596. if (!new_property && mode == PutOwnPropertyMode::DefineProperty && !metadata.value().attributes.is_configurable()) {
  597. if ((attributes.has_configurable() && attributes.is_configurable()) || (attributes.has_enumerable() && attributes.is_enumerable() != metadata.value().attributes.is_enumerable())) {
  598. dbgln_if(OBJECT_DEBUG, "Disallow reconfig of non-configurable property");
  599. if (throw_exceptions)
  600. vm().throw_exception<TypeError>(global_object(), ErrorType::DescChangeNonConfigurable, property_name.to_display_string());
  601. return false;
  602. }
  603. if (value_here.is_accessor() != value.is_accessor()) {
  604. dbgln_if(OBJECT_DEBUG, "Disallow reconfig of non-configurable property");
  605. if (throw_exceptions)
  606. vm().throw_exception<TypeError>(global_object(), ErrorType::DescChangeNonConfigurable, property_name.to_display_string());
  607. return false;
  608. }
  609. if (!value_here.is_accessor() && !metadata.value().attributes.is_writable() && ((attributes.has_writable() && attributes.is_writable()) || (!value.is_empty() && !same_value(value, value_here)))) {
  610. dbgln_if(OBJECT_DEBUG, "Disallow reconfig of non-configurable property");
  611. if (throw_exceptions)
  612. vm().throw_exception<TypeError>(global_object(), ErrorType::DescChangeNonConfigurable, property_name.to_display_string());
  613. return false;
  614. }
  615. 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()))) {
  616. dbgln_if(OBJECT_DEBUG, "Disallow reconfig of non-configurable property");
  617. if (throw_exceptions)
  618. vm().throw_exception<TypeError>(global_object(), ErrorType::DescChangeNonConfigurable, property_name.to_display_string());
  619. return false;
  620. }
  621. }
  622. if (mode == PutOwnPropertyMode::DefineProperty && attributes != metadata.value().attributes) {
  623. if (m_shape->is_unique()) {
  624. m_shape->reconfigure_property_in_unique_shape(property_name, attributes);
  625. } else {
  626. set_shape(*m_shape->create_configure_transition(property_name, attributes));
  627. }
  628. metadata = shape().lookup(property_name);
  629. 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());
  630. }
  631. if (!new_property && mode == PutOwnPropertyMode::Put && !value_here.is_accessor() && !metadata.value().attributes.is_writable()) {
  632. dbgln_if(OBJECT_DEBUG, "Disallow write to non-writable property");
  633. if (throw_exceptions && vm().in_strict_mode())
  634. vm().throw_exception<TypeError>(global_object(), ErrorType::DescWriteNonWritable, property_name.to_display_string());
  635. return false;
  636. }
  637. if (value.is_empty())
  638. return true;
  639. if (value_here.is_native_property()) {
  640. call_native_property_setter(value_here.as_native_property(), this, value);
  641. } else {
  642. m_storage[metadata.value().offset] = value;
  643. }
  644. return true;
  645. }
  646. bool Object::put_own_property_by_index(u32 property_index, Value value, PropertyAttributes attributes, PutOwnPropertyMode mode, bool throw_exceptions)
  647. {
  648. VERIFY(!(mode == PutOwnPropertyMode::Put && value.is_accessor()));
  649. auto existing_property = m_indexed_properties.get(nullptr, property_index, false);
  650. auto new_property = !existing_property.has_value();
  651. if (!is_extensible() && new_property) {
  652. dbgln_if(OBJECT_DEBUG, "Disallow define_property of non-extensible object");
  653. if (throw_exceptions && vm().in_strict_mode())
  654. vm().throw_exception<TypeError>(global_object(), ErrorType::NonExtensibleDefine, property_index);
  655. return false;
  656. }
  657. if (value.is_accessor()) {
  658. auto& accessor = value.as_accessor();
  659. if (accessor.getter())
  660. attributes.set_has_getter();
  661. if (accessor.setter())
  662. attributes.set_has_setter();
  663. }
  664. PropertyAttributes existing_attributes = new_property ? 0 : existing_property.value().attributes;
  665. if (!new_property && mode == PutOwnPropertyMode::DefineProperty && !existing_attributes.is_configurable() && attributes != existing_attributes) {
  666. dbgln_if(OBJECT_DEBUG, "Disallow reconfig of non-configurable property");
  667. if (throw_exceptions)
  668. vm().throw_exception<TypeError>(global_object(), ErrorType::DescChangeNonConfigurable, property_index);
  669. return false;
  670. }
  671. auto value_here = new_property ? Value() : existing_property.value().value;
  672. if (!new_property && mode == PutOwnPropertyMode::Put && !value_here.is_accessor() && !existing_attributes.is_writable()) {
  673. dbgln_if(OBJECT_DEBUG, "Disallow write to non-writable property");
  674. return false;
  675. }
  676. if (value.is_empty())
  677. return true;
  678. if (value_here.is_native_property()) {
  679. call_native_property_setter(value_here.as_native_property(), this, value);
  680. } else {
  681. m_indexed_properties.put(this, property_index, value, attributes, mode == PutOwnPropertyMode::Put);
  682. }
  683. return true;
  684. }
  685. bool Object::delete_property(const PropertyName& property_name)
  686. {
  687. VERIFY(property_name.is_valid());
  688. if (property_name.is_number())
  689. return m_indexed_properties.remove(property_name.as_number());
  690. if (property_name.is_string() && property_name.string_may_be_number()) {
  691. i32 property_index = property_name.as_string().to_int().value_or(-1);
  692. if (property_index >= 0)
  693. return m_indexed_properties.remove(property_index);
  694. }
  695. auto metadata = shape().lookup(property_name.to_string_or_symbol());
  696. if (!metadata.has_value())
  697. return true;
  698. if (!metadata.value().attributes.is_configurable()) {
  699. if (vm().in_strict_mode())
  700. vm().throw_exception<TypeError>(global_object(), ErrorType::DescChangeNonConfigurable, property_name.to_string_or_symbol().to_display_string());
  701. return false;
  702. }
  703. size_t deleted_offset = metadata.value().offset;
  704. ensure_shape_is_unique();
  705. shape().remove_property_from_unique_shape(property_name.to_string_or_symbol(), deleted_offset);
  706. m_storage.remove(deleted_offset);
  707. return true;
  708. }
  709. void Object::ensure_shape_is_unique()
  710. {
  711. if (shape().is_unique())
  712. return;
  713. m_shape = m_shape->create_unique_clone();
  714. }
  715. Value Object::get_by_index(u32 property_index, bool without_side_effects) const
  716. {
  717. const Object* object = this;
  718. while (object) {
  719. if (is<StringObject>(*object)) {
  720. auto& string = static_cast<const StringObject&>(*object).primitive_string().string();
  721. if (property_index < string.length())
  722. return js_string(heap(), string.substring(property_index, 1));
  723. } else if (static_cast<size_t>(property_index) < object->m_indexed_properties.array_like_size()) {
  724. auto result = object->m_indexed_properties.get(const_cast<Object*>(this), property_index, !without_side_effects);
  725. if (vm().exception())
  726. return {};
  727. if (result.has_value() && !result.value().value.is_empty())
  728. return result.value().value;
  729. }
  730. object = object->prototype();
  731. if (vm().exception())
  732. return {};
  733. }
  734. return {};
  735. }
  736. Value Object::get(const PropertyName& property_name, Value receiver, bool without_side_effects) const
  737. {
  738. VERIFY(property_name.is_valid());
  739. if (property_name.is_number())
  740. return get_by_index(property_name.as_number(), without_side_effects);
  741. if (property_name.is_string() && property_name.string_may_be_number()) {
  742. auto& property_string = property_name.as_string();
  743. i32 property_index = property_string.to_int().value_or(-1);
  744. if (property_index >= 0)
  745. return get_by_index(property_index, without_side_effects);
  746. }
  747. if (receiver.is_empty())
  748. receiver = Value(this);
  749. const Object* object = this;
  750. while (object) {
  751. auto value = object->get_own_property(property_name, receiver, without_side_effects);
  752. if (vm().exception())
  753. return {};
  754. if (!value.is_empty())
  755. return value;
  756. object = object->prototype();
  757. if (vm().exception())
  758. return {};
  759. }
  760. return {};
  761. }
  762. Value Object::get_without_side_effects(const PropertyName& property_name) const
  763. {
  764. TemporaryClearException clear_exception(vm());
  765. return get(property_name, {}, true);
  766. }
  767. bool Object::put_by_index(u32 property_index, Value value)
  768. {
  769. VERIFY(!value.is_empty());
  770. // If there's a setter in the prototype chain, we go to the setter.
  771. // Otherwise, it goes in the own property storage.
  772. Object* object = this;
  773. while (object) {
  774. auto existing_value = object->m_indexed_properties.get(nullptr, property_index, false);
  775. if (existing_value.has_value()) {
  776. auto value_here = existing_value.value();
  777. if (value_here.value.is_accessor()) {
  778. value_here.value.as_accessor().call_setter(object, value);
  779. return true;
  780. }
  781. if (value_here.value.is_native_property()) {
  782. // FIXME: Why doesn't put_by_index() receive the receiver value from put()?!
  783. auto receiver = this;
  784. call_native_property_setter(value_here.value.as_native_property(), receiver, value);
  785. return true;
  786. }
  787. }
  788. object = object->prototype();
  789. if (vm().exception())
  790. return {};
  791. }
  792. return put_own_property_by_index(property_index, value, default_attributes, PutOwnPropertyMode::Put);
  793. }
  794. bool Object::put(const PropertyName& property_name, Value value, Value receiver)
  795. {
  796. VERIFY(property_name.is_valid());
  797. if (property_name.is_number())
  798. return put_by_index(property_name.as_number(), value);
  799. VERIFY(!value.is_empty());
  800. if (property_name.is_string() && property_name.string_may_be_number()) {
  801. auto& property_string = property_name.as_string();
  802. i32 property_index = property_string.to_int().value_or(-1);
  803. if (property_index >= 0)
  804. return put_by_index(property_index, value);
  805. }
  806. auto string_or_symbol = property_name.to_string_or_symbol();
  807. if (receiver.is_empty())
  808. receiver = Value(this);
  809. // If there's a setter in the prototype chain, we go to the setter.
  810. // Otherwise, it goes in the own property storage.
  811. Object* object = this;
  812. while (object) {
  813. auto metadata = object->shape().lookup(string_or_symbol);
  814. if (metadata.has_value()) {
  815. auto value_here = object->m_storage[metadata.value().offset];
  816. if (value_here.is_accessor()) {
  817. value_here.as_accessor().call_setter(receiver, value);
  818. return true;
  819. }
  820. if (value_here.is_native_property()) {
  821. call_native_property_setter(value_here.as_native_property(), receiver, value);
  822. return true;
  823. }
  824. }
  825. object = object->prototype();
  826. if (vm().exception())
  827. return false;
  828. }
  829. return put_own_property(string_or_symbol, value, default_attributes, PutOwnPropertyMode::Put);
  830. }
  831. bool Object::define_native_function(PropertyName const& property_name, AK::Function<Value(VM&, GlobalObject&)> native_function, i32 length, PropertyAttributes attribute)
  832. {
  833. auto& vm = this->vm();
  834. String function_name;
  835. if (property_name.is_string()) {
  836. function_name = property_name.as_string();
  837. } else {
  838. function_name = String::formatted("[{}]", property_name.as_symbol()->description());
  839. }
  840. auto* function = NativeFunction::create(global_object(), function_name, move(native_function));
  841. function->define_property_without_transition(vm.names.length, Value(length), Attribute::Configurable);
  842. if (vm.exception())
  843. return {};
  844. function->define_property_without_transition(vm.names.name, js_string(vm.heap(), function_name), Attribute::Configurable);
  845. if (vm.exception())
  846. return {};
  847. return define_property(property_name, function, attribute);
  848. }
  849. bool Object::define_native_property(PropertyName const& property_name, AK::Function<Value(VM&, GlobalObject&)> getter, AK::Function<void(VM&, GlobalObject&, Value)> setter, PropertyAttributes attribute)
  850. {
  851. return define_property(property_name, heap().allocate_without_global_object<NativeProperty>(move(getter), move(setter)), attribute);
  852. }
  853. // 20.1.2.3.1 ObjectDefineProperties ( O, Properties ), https://tc39.es/ecma262/#sec-objectdefineproperties
  854. void Object::define_properties(Value properties)
  855. {
  856. auto& vm = this->vm();
  857. auto* props = properties.to_object(global_object());
  858. if (!props)
  859. return;
  860. auto keys = props->get_own_properties(PropertyKind::Key);
  861. if (vm.exception())
  862. return;
  863. struct NameAndDescriptor {
  864. PropertyName name;
  865. PropertyDescriptor descriptor;
  866. };
  867. Vector<NameAndDescriptor> descriptors;
  868. for (auto& key : keys) {
  869. auto property_name = PropertyName::from_value(global_object(), key);
  870. auto property_descriptor = props->get_own_property_descriptor(property_name);
  871. if (property_descriptor.has_value() && property_descriptor->attributes.is_enumerable()) {
  872. auto descriptor_object = props->get(property_name);
  873. if (vm.exception())
  874. return;
  875. if (!descriptor_object.is_object()) {
  876. vm.throw_exception<TypeError>(global_object(), ErrorType::NotAnObject, descriptor_object.to_string_without_side_effects());
  877. return;
  878. }
  879. auto descriptor = PropertyDescriptor::from_dictionary(vm, descriptor_object.as_object());
  880. if (vm.exception())
  881. return;
  882. descriptors.append({ property_name, descriptor });
  883. }
  884. }
  885. for (auto& [name, descriptor] : descriptors) {
  886. // FIXME: The spec has both of this handled by DefinePropertyOrThrow(O, P, desc).
  887. // We should invest some time in improving object property handling, it not being
  888. // super close to the spec makes this and other things unnecessarily complicated.
  889. if (descriptor.is_accessor_descriptor())
  890. define_accessor(name, descriptor.getter, descriptor.setter, descriptor.attributes);
  891. else
  892. define_property(name, descriptor.value, descriptor.attributes);
  893. }
  894. }
  895. void Object::visit_edges(Cell::Visitor& visitor)
  896. {
  897. Cell::visit_edges(visitor);
  898. visitor.visit(m_shape);
  899. for (auto& value : m_storage)
  900. visitor.visit(value);
  901. m_indexed_properties.for_each_value([&visitor](auto& value) {
  902. visitor.visit(value);
  903. });
  904. }
  905. bool Object::has_property(const PropertyName& property_name) const
  906. {
  907. const Object* object = this;
  908. while (object) {
  909. if (object->has_own_property(property_name))
  910. return true;
  911. object = object->prototype();
  912. if (vm().exception())
  913. return false;
  914. }
  915. return false;
  916. }
  917. bool Object::has_own_property(const PropertyName& property_name) const
  918. {
  919. VERIFY(property_name.is_valid());
  920. auto has_indexed_property = [&](u32 index) -> bool {
  921. if (is<StringObject>(*this))
  922. return index < static_cast<const StringObject*>(this)->primitive_string().string().length();
  923. return m_indexed_properties.has_index(index);
  924. };
  925. if (property_name.is_number())
  926. return has_indexed_property(property_name.as_number());
  927. if (property_name.is_string() && property_name.string_may_be_number()) {
  928. i32 property_index = property_name.as_string().to_int().value_or(-1);
  929. if (property_index >= 0)
  930. return has_indexed_property(property_index);
  931. }
  932. return shape().lookup(property_name.to_string_or_symbol()).has_value();
  933. }
  934. Value Object::ordinary_to_primitive(Value::PreferredType preferred_type) const
  935. {
  936. VERIFY(preferred_type == Value::PreferredType::String || preferred_type == Value::PreferredType::Number);
  937. auto& vm = this->vm();
  938. Vector<FlyString, 2> method_names;
  939. if (preferred_type == Value::PreferredType::String)
  940. method_names = { vm.names.toString.as_string(), vm.names.valueOf.as_string() };
  941. else
  942. method_names = { vm.names.valueOf.as_string(), vm.names.toString.as_string() };
  943. for (auto& method_name : method_names) {
  944. auto method = get(method_name);
  945. if (vm.exception())
  946. return {};
  947. if (method.is_function()) {
  948. auto result = vm.call(method.as_function(), const_cast<Object*>(this));
  949. if (!result.is_object())
  950. return result;
  951. }
  952. }
  953. vm.throw_exception<TypeError>(global_object(), ErrorType::Convert, "object", preferred_type == Value::PreferredType::String ? "string" : "number");
  954. return {};
  955. }
  956. // 20.5.8.1 InstallErrorCause ( O, options ), https://tc39.es/proposal-error-cause/#sec-errorobjects-install-error-cause
  957. void Object::install_error_cause(Value options)
  958. {
  959. auto& vm = this->vm();
  960. if (!options.is_object())
  961. return;
  962. auto& options_object = options.as_object();
  963. if (!options_object.has_property(vm.names.cause))
  964. return;
  965. auto cause = options_object.get(vm.names.cause).value_or(js_undefined());
  966. if (vm.exception())
  967. return;
  968. define_property(vm.names.cause, cause, Attribute::Writable | Attribute::Configurable);
  969. }
  970. Value Object::invoke_internal(const StringOrSymbol& property_name, Optional<MarkedValueList> arguments)
  971. {
  972. auto& vm = this->vm();
  973. auto property = get(property_name).value_or(js_undefined());
  974. if (vm.exception())
  975. return {};
  976. if (!property.is_function()) {
  977. vm.throw_exception<TypeError>(global_object(), ErrorType::NotAFunction, property.to_string_without_side_effects());
  978. return {};
  979. }
  980. return vm.call(property.as_function(), this, move(arguments));
  981. }
  982. Value Object::call_native_property_getter(NativeProperty& property, Value this_value) const
  983. {
  984. auto& vm = this->vm();
  985. CallFrame call_frame;
  986. if (auto* interpreter = vm.interpreter_if_exists())
  987. call_frame.current_node = interpreter->current_node();
  988. call_frame.is_strict_mode = vm.in_strict_mode();
  989. call_frame.this_value = this_value;
  990. vm.push_call_frame(call_frame, global_object());
  991. if (vm.exception())
  992. return {};
  993. auto result = property.get(vm, global_object());
  994. vm.pop_call_frame();
  995. return result;
  996. }
  997. void Object::call_native_property_setter(NativeProperty& property, Value this_value, Value setter_value) const
  998. {
  999. auto& vm = this->vm();
  1000. CallFrame call_frame;
  1001. if (auto* interpreter = vm.interpreter_if_exists())
  1002. call_frame.current_node = interpreter->current_node();
  1003. call_frame.is_strict_mode = vm.in_strict_mode();
  1004. call_frame.this_value = this_value;
  1005. vm.push_call_frame(call_frame, global_object());
  1006. if (vm.exception())
  1007. return;
  1008. property.set(vm, global_object(), setter_value);
  1009. vm.pop_call_frame();
  1010. }
  1011. }