LegacyPlatformObject.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /*
  2. * Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibJS/Runtime/GlobalObject.h>
  7. #include <LibWeb/Bindings/ExceptionOrUtils.h>
  8. #include <LibWeb/Bindings/LegacyPlatformObject.h>
  9. namespace Web::Bindings {
  10. LegacyPlatformObject::LegacyPlatformObject(JS::Object& prototype)
  11. : PlatformObject(prototype)
  12. {
  13. }
  14. LegacyPlatformObject::~LegacyPlatformObject() = default;
  15. JS::ThrowCompletionOr<bool> LegacyPlatformObject::is_named_property_exposed_on_object(JS::PropertyKey const& property_key) const
  16. {
  17. [[maybe_unused]] auto& vm = this->vm();
  18. // The spec doesn't say anything about the type of the property name here.
  19. // Numbers can be converted to a string, which is fine and what other engines do.
  20. // However, since a symbol cannot be converted to a string, it cannot be a supported property name. Return early if it's a symbol.
  21. if (property_key.is_symbol())
  22. return false;
  23. // 1. If P is not a supported property name of O, then return false.
  24. // NOTE: This is in it's own variable to enforce the type.
  25. // FIXME: Can this throw?
  26. Vector<String> supported_property_names = this->supported_property_names();
  27. auto property_key_string = property_key.to_string();
  28. if (!supported_property_names.contains_slow(property_key_string))
  29. return false;
  30. // 2. If O has an own property named P, then return false.
  31. // NOTE: This has to be done manually instead of using Object::has_own_property, as that would use the overridden internal_get_own_property.
  32. auto own_property_named_p = MUST(Object::internal_get_own_property(property_key));
  33. if (own_property_named_p.has_value())
  34. return false;
  35. // NOTE: Step 3 is not here as the interface doesn't have the LegacyOverrideBuiltIns extended attribute.
  36. // 4. Let prototype be O.[[GetPrototypeOf]]().
  37. auto* prototype = TRY(internal_get_prototype_of());
  38. // 5. While prototype is not null:
  39. while (prototype) {
  40. // FIXME: 1. If prototype is not a named properties object, and prototype has an own property named P, then return false.
  41. // (It currently does not check for named property objects)
  42. bool prototype_has_own_property_named_p = TRY(prototype->has_own_property(property_key));
  43. if (prototype_has_own_property_named_p)
  44. return false;
  45. // 2. Set prototype to prototype.[[GetPrototypeOf]]().
  46. prototype = TRY(prototype->internal_get_prototype_of());
  47. }
  48. // 6. Return true.
  49. return true;
  50. }
  51. JS::ThrowCompletionOr<Optional<JS::PropertyDescriptor>> LegacyPlatformObject::legacy_platform_object_get_own_property_for_get_own_property_slot(JS::PropertyKey const& property_name) const
  52. {
  53. auto& vm = this->vm();
  54. if (property_name.is_number()) {
  55. // 1. Let index be the result of calling ToUint32(P).
  56. u32 index = property_name.as_number();
  57. // 2. If index is a supported property index, then:
  58. // FIXME: Can this throw?
  59. if (is_supported_property_index(index)) {
  60. auto value = TRY(throw_dom_exception_if_needed(vm, [&] { return item_value(index); }));
  61. // 5. Let desc be a newly created Property Descriptor with no fields.
  62. JS::PropertyDescriptor descriptor;
  63. // 6. Set desc.[[Value]] to the result of converting value to an ECMAScript value.
  64. descriptor.value = value;
  65. descriptor.writable = false;
  66. // 8. Set desc.[[Enumerable]] and desc.[[Configurable]] to true.
  67. descriptor.enumerable = true;
  68. descriptor.configurable = true;
  69. // 9. Return desc.
  70. return descriptor;
  71. }
  72. // 3. Set ignoreNamedProps to true.
  73. return TRY(Object::internal_get_own_property(property_name));
  74. }
  75. // 1. If the result of running the named property visibility algorithm with property name P and object O is true, then:
  76. if (TRY(is_named_property_exposed_on_object(property_name))) {
  77. // FIXME: It's unfortunate that this is done twice, once in is_named_property_exposed_on_object and here.
  78. auto property_name_string = property_name.to_string();
  79. auto value = TRY(throw_dom_exception_if_needed(vm, [&] { return named_item_value(property_name_string); }));
  80. // 5. Let desc be a newly created Property Descriptor with no fields.
  81. JS::PropertyDescriptor descriptor;
  82. // 6. Set desc.[[Value]] to the result of converting value to an ECMAScript value.
  83. descriptor.value = value;
  84. descriptor.writable = false;
  85. descriptor.enumerable = false;
  86. // 9. Set desc.[[Configurable]] to true.
  87. descriptor.configurable = true;
  88. // 10. Return desc.
  89. return descriptor;
  90. }
  91. return TRY(Object::internal_get_own_property(property_name));
  92. }
  93. JS::ThrowCompletionOr<Optional<JS::PropertyDescriptor>> LegacyPlatformObject::legacy_platform_object_get_own_property_for_set_slot(JS::PropertyKey const& property_name) const
  94. {
  95. if (!property_name.is_number())
  96. return TRY(Object::internal_get_own_property(property_name));
  97. // 1. Let index be the result of calling ToUint32(P).
  98. u32 index = property_name.as_number();
  99. // 2. If index is a supported property index, then:
  100. // FIXME: Can this throw?
  101. if (is_supported_property_index(index)) {
  102. auto value = TRY(throw_dom_exception_if_needed(vm(), [&] { return item_value(index); }));
  103. // 5. Let desc be a newly created Property Descriptor with no fields.
  104. JS::PropertyDescriptor descriptor;
  105. // 6. Set desc.[[Value]] to the result of converting value to an ECMAScript value.
  106. descriptor.value = value;
  107. descriptor.writable = false;
  108. // 8. Set desc.[[Enumerable]] and desc.[[Configurable]] to true.
  109. descriptor.enumerable = true;
  110. descriptor.configurable = true;
  111. // 9. Return desc.
  112. return descriptor;
  113. }
  114. // 3. Set ignoreNamedProps to true.
  115. return TRY(Object::internal_get_own_property(property_name));
  116. }
  117. JS::ThrowCompletionOr<Optional<JS::PropertyDescriptor>> LegacyPlatformObject::internal_get_own_property(JS::PropertyKey const& property_name) const
  118. {
  119. // 1. Return LegacyPlatformObjectGetOwnProperty(O, P, false).
  120. return TRY(legacy_platform_object_get_own_property_for_get_own_property_slot(property_name));
  121. }
  122. JS::ThrowCompletionOr<bool> LegacyPlatformObject::internal_set(JS::PropertyKey const& property_name, JS::Value value, JS::Value receiver)
  123. {
  124. [[maybe_unused]] auto& global_object = this->global_object();
  125. // 2. Let ownDesc be LegacyPlatformObjectGetOwnProperty(O, P, true).
  126. auto own_descriptor = TRY(legacy_platform_object_get_own_property_for_set_slot(property_name));
  127. // 3. Perform ? OrdinarySetWithOwnDescriptor(O, P, V, Receiver, ownDesc).
  128. // NOTE: The spec says "perform" instead of "return", meaning nothing will be returned on this path according to the spec, which isn't possible to do.
  129. // Let's treat it as though it says "return" instead of "perform".
  130. return ordinary_set_with_own_descriptor(property_name, value, receiver, own_descriptor);
  131. }
  132. JS::ThrowCompletionOr<bool> LegacyPlatformObject::internal_define_own_property(JS::PropertyKey const& property_name, JS::PropertyDescriptor const& property_descriptor)
  133. {
  134. [[maybe_unused]] auto& vm = this->vm();
  135. [[maybe_unused]] auto& global_object = this->global_object();
  136. if (property_name.is_number()) {
  137. // 1. If the result of calling IsDataDescriptor(Desc) is false, then return false.
  138. if (!property_descriptor.is_data_descriptor())
  139. return false;
  140. return false;
  141. }
  142. if (property_name.is_string()) {
  143. auto& property_name_as_string = property_name.as_string();
  144. // 1. Let creating be true if P is not a supported property name, and false otherwise.
  145. // NOTE: This is in it's own variable to enforce the type.
  146. // FIXME: Can this throw?
  147. Vector<String> supported_property_names = this->supported_property_names();
  148. [[maybe_unused]] bool creating = !supported_property_names.contains_slow(property_name_as_string);
  149. // NOTE: This has to be done manually instead of using Object::has_own_property, as that would use the overridden internal_get_own_property.
  150. auto own_property_named_p = TRY(Object::internal_get_own_property(property_name));
  151. if (!own_property_named_p.has_value()) {
  152. if (!creating)
  153. return false;
  154. }
  155. }
  156. // property_descriptor is a const&, thus we need to create a copy here to set [[Configurable]]
  157. JS::PropertyDescriptor descriptor_copy(property_descriptor);
  158. descriptor_copy.configurable = true;
  159. // 4. Return OrdinaryDefineOwnProperty(O, P, Desc).
  160. return Object::internal_define_own_property(property_name, descriptor_copy);
  161. }
  162. JS::ThrowCompletionOr<bool> LegacyPlatformObject::internal_delete(JS::PropertyKey const& property_name)
  163. {
  164. [[maybe_unused]] auto& global_object = this->global_object();
  165. if (property_name.is_number()) {
  166. // 1. Let index be the result of calling ToUint32(P).
  167. u32 index = property_name.as_number();
  168. // 2. If index is not a supported property index, then return true.
  169. // FIXME: Can this throw?
  170. if (!is_supported_property_index(index))
  171. return true;
  172. // 3. Return false.
  173. return false;
  174. }
  175. if (TRY(is_named_property_exposed_on_object(property_name))) {
  176. return false;
  177. }
  178. // 3. If O has an own property with name P, then:
  179. auto own_property_named_p_descriptor = TRY(Object::internal_get_own_property(property_name));
  180. if (own_property_named_p_descriptor.has_value()) {
  181. // 1. If the property is not configurable, then return false.
  182. // 2. Otherwise, remove the property from O.
  183. if (*own_property_named_p_descriptor->configurable)
  184. storage_delete(property_name);
  185. else
  186. return false;
  187. }
  188. // 4. Return true.
  189. return true;
  190. }
  191. JS::ThrowCompletionOr<bool> LegacyPlatformObject::internal_prevent_extensions()
  192. {
  193. // 1. Return false.
  194. return false;
  195. }
  196. JS::ThrowCompletionOr<JS::MarkedVector<JS::Value>> LegacyPlatformObject::internal_own_property_keys() const
  197. {
  198. auto& vm = this->vm();
  199. // 1. Let keys be a new empty list of ECMAScript String and Symbol values.
  200. JS::MarkedVector<JS::Value> keys { heap() };
  201. for (u64 index = 0; index <= NumericLimits<u32>::max(); ++index) {
  202. if (is_supported_property_index(index))
  203. keys.append(js_string(vm, String::number(index)));
  204. else
  205. break;
  206. }
  207. for (auto& named_property : supported_property_names()) {
  208. if (TRY(is_named_property_exposed_on_object(named_property)))
  209. keys.append(js_string(vm, named_property));
  210. }
  211. // 4. For each P of O’s own property keys that is a String, in ascending chronological order of property creation, append P to keys.
  212. for (auto& it : shape().property_table_ordered()) {
  213. if (it.key.is_string())
  214. keys.append(it.key.to_value(vm));
  215. }
  216. // 5. For each P of O’s own property keys that is a Symbol, in ascending chronological order of property creation, append P to keys.
  217. for (auto& it : shape().property_table_ordered()) {
  218. if (it.key.is_symbol())
  219. keys.append(it.key.to_value(vm));
  220. }
  221. // FIXME: 6. Assert: keys has no duplicate items.
  222. // 7. Return keys.
  223. return { move(keys) };
  224. }
  225. JS::Value LegacyPlatformObject::item_value(size_t) const
  226. {
  227. return JS::js_undefined();
  228. }
  229. JS::Value LegacyPlatformObject::named_item_value(FlyString const&) const
  230. {
  231. return JS::js_undefined();
  232. }
  233. Vector<String> LegacyPlatformObject::supported_property_names() const
  234. {
  235. return {};
  236. }
  237. bool LegacyPlatformObject::is_supported_property_index(u32) const
  238. {
  239. return false;
  240. }
  241. }