Object.cpp 44 KB

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