OverloadResolution.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. /*
  2. * Copyright (c) 2022, Sam Atkins <atkinssj@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibJS/Runtime/ArrayBuffer.h>
  7. #include <LibJS/Runtime/DataView.h>
  8. #include <LibJS/Runtime/FunctionObject.h>
  9. #include <LibJS/Runtime/TypedArray.h>
  10. #include <LibJS/Runtime/Value.h>
  11. #include <LibWeb/Bindings/PlatformObject.h>
  12. #include <LibWeb/WebIDL/OverloadResolution.h>
  13. namespace Web::WebIDL {
  14. // https://webidl.spec.whatwg.org/#dfn-convert-ecmascript-to-idl-value
  15. static JS::Value convert_ecmascript_type_to_idl_value(JS::Value value, IDL::Type const&)
  16. {
  17. // FIXME: We have this code already in the code generator, in `generate_to_cpp()`, but how do we use it here?
  18. return value;
  19. }
  20. template<typename Match>
  21. static bool has_overload_with_argument_type_or_subtype_matching(IDL::EffectiveOverloadSet& overloads, size_t argument_index, Match match)
  22. {
  23. // NOTE: This is to save some repetition.
  24. // Almost every sub-step of step 12 of the overload resolution algorithm matches overloads with an argument that is:
  25. // - One of several specific types.
  26. // - "an annotated type whose inner type is one of the above types"
  27. // - "a union type, nullable union type, or annotated union type that has one of the above types in its flattened member types"
  28. // So, this function lets you pass in the first check, and handles the others automatically.
  29. return overloads.has_overload_with_matching_argument_at_index(argument_index,
  30. [match](IDL::Type const& type, auto) {
  31. if (match(type))
  32. return true;
  33. // FIXME: - an annotated type whose inner type is one of the above types
  34. if (type.is_union()) {
  35. auto flattened_members = type.as_union().flattened_member_types();
  36. for (auto const& member : flattened_members) {
  37. if (match(member))
  38. return true;
  39. // FIXME: - an annotated type whose inner type is one of the above types
  40. }
  41. return false;
  42. }
  43. return false;
  44. });
  45. }
  46. // https://webidl.spec.whatwg.org/#es-overloads
  47. JS::ThrowCompletionOr<ResolvedOverload> resolve_overload(JS::VM& vm, IDL::EffectiveOverloadSet& overloads)
  48. {
  49. // FIXME: The vast majority of this algorithm can be (and must be, in order to resolve the dictionary
  50. // related FIXMEs below) evaluated at code-generation time.
  51. // 1. Let maxarg be the length of the longest type list of the entries in S.
  52. // 2. Let n be the size of args.
  53. // 3. Initialize argcount to be min(maxarg, n).
  54. // 4. Remove from S all entries whose type list is not of length argcount.
  55. // NOTE: The IDL-generated callers already only provide an overload set containing overloads with the correct number
  56. // of arguments. Therefore, we do not need to remove any entry from that set here. However, we do need to handle
  57. // when the number of user-provided arguments exceeds the overload set's argument count.
  58. int argument_count = min(vm.argument_count(), overloads.is_empty() ? 0 : overloads.items()[0].types.size());
  59. // 5. If S is empty, then throw a TypeError.
  60. if (overloads.is_empty())
  61. return vm.throw_completion<JS::TypeError>(JS::ErrorType::OverloadResolutionFailed);
  62. // 6. Initialize d to −1.
  63. auto distinguishing_argument_index = -1;
  64. // 7. Initialize method to undefined.
  65. Optional<JS::FunctionObject&> method;
  66. // 8. If there is more than one entry in S, then set d to be the distinguishing argument index for the entries of S.
  67. if (overloads.size() > 1)
  68. distinguishing_argument_index = overloads.distinguishing_argument_index();
  69. // 9. Initialize values to be an empty list, where each entry will be either an IDL value or the special value “missing”.
  70. Vector<ResolvedOverload::Argument> values;
  71. // 10. Initialize i to 0.
  72. auto i = 0;
  73. // 11. While i < d:
  74. while (i < distinguishing_argument_index) {
  75. // 1. Let V be args[i].
  76. auto const& value = vm.argument(i);
  77. auto const& item = overloads.items().first();
  78. // 2. Let type be the type at index i in the type list of any entry in S.
  79. auto const& type = item.types[i];
  80. // 3. Let optionality be the value at index i in the list of optionality values of any entry in S.
  81. auto const& optionality = item.optionality_values[i];
  82. // 4. If optionality is “optional” and V is undefined, then:
  83. if (optionality == IDL::Optionality::Optional && value.is_undefined()) {
  84. // FIXME: 1. If the argument at index i is declared with a default value, then append to values that default value.
  85. // 2. Otherwise, append to values the special value “missing”.
  86. values.empend(ResolvedOverload::Missing {});
  87. }
  88. // 5. Otherwise, append to values the result of converting V to IDL type type.
  89. values.empend(convert_ecmascript_type_to_idl_value(value, type));
  90. // 6. Set i to i + 1.
  91. ++i;
  92. }
  93. // 12. If i = d, then:
  94. if (i == distinguishing_argument_index) {
  95. // 1. Let V be args[i].
  96. auto const& value = vm.argument(i);
  97. // 2. If V is undefined, and there is an entry in S whose list of optionality values has “optional” at index i, then remove from S all other entries.
  98. if (value.is_undefined()
  99. && overloads.has_overload_with_matching_argument_at_index(i, [](auto&, IDL::Optionality const& optionality) { return optionality == IDL::Optionality::Optional; })) {
  100. overloads.remove_all_other_entries();
  101. }
  102. // 3. Otherwise: if V is null or undefined, and there is an entry in S that has one of the following types at position i of its type list,
  103. // - a nullable type
  104. // - a dictionary type
  105. // - an annotated type whose inner type is one of the above types
  106. // - a union type or annotated union type that includes a nullable type or that has a dictionary type in its flattened members
  107. // then remove from S all other entries.
  108. // NOTE: This is the one case we can't use `has_overload_with_argument_type_or_subtype_matching()` because we also need to look
  109. // for dictionary types in the flattened members.
  110. else if ((value.is_undefined() || value.is_null())
  111. && overloads.has_overload_with_matching_argument_at_index(i, [](IDL::Type const& type, auto) {
  112. if (type.is_nullable())
  113. return true;
  114. // FIXME: - a dictionary type
  115. // FIXME: - an annotated type whose inner type is one of the above types
  116. if (type.is_union()) {
  117. auto flattened_members = type.as_union().flattened_member_types();
  118. for (auto const& member : flattened_members) {
  119. if (member->is_nullable())
  120. return true;
  121. // FIXME: - a dictionary type
  122. // FIXME: - an annotated type whose inner type is one of the above types
  123. }
  124. return false;
  125. }
  126. return false;
  127. })) {
  128. overloads.remove_all_other_entries();
  129. }
  130. // 4. Otherwise: if V is a platform object, and there is an entry in S that has one of the following types at position i of its type list,
  131. // - an interface type that V implements
  132. // - object
  133. // - a nullable version of any of the above types
  134. // - an annotated type whose inner type is one of the above types
  135. // - a union type, nullable union type, or annotated union type that has one of the above types in its flattened member types
  136. // then remove from S all other entries.
  137. else if (value.is_object() && is<Bindings::PlatformObject>(value.as_object())
  138. && has_overload_with_argument_type_or_subtype_matching(overloads, i, [value](IDL::Type const& type) {
  139. // - an interface type that V implements
  140. if (static_cast<Bindings::PlatformObject const&>(value.as_object()).implements_interface(MUST(String::from_byte_string(type.name()))))
  141. return true;
  142. // - object
  143. if (type.is_object())
  144. return true;
  145. return false;
  146. })) {
  147. overloads.remove_all_other_entries();
  148. }
  149. // 5. Otherwise: if Type(V) is Object, V has an [[ArrayBufferData]] internal slot, and there is an entry in S that has one of the following types at position i of its type list,
  150. // - ArrayBuffer
  151. // - object
  152. // - a nullable version of either of the above types
  153. // - an annotated type whose inner type is one of the above types
  154. // - a union type, nullable union type, or annotated union type that has one of the above types in its flattened member types
  155. // then remove from S all other entries.
  156. else if (value.is_object() && is<JS::ArrayBuffer>(value.as_object())
  157. && has_overload_with_argument_type_or_subtype_matching(overloads, i, [](IDL::Type const& type) {
  158. if (type.is_plain() && (type.name() == "ArrayBuffer" || type.name() == "BufferSource"))
  159. return true;
  160. if (type.is_object())
  161. return true;
  162. return false;
  163. })) {
  164. overloads.remove_all_other_entries();
  165. }
  166. // 6. Otherwise: if Type(V) is Object, V has a [[DataView]] internal slot, and there is an entry in S that has one of the following types at position i of its type list,
  167. // - DataView
  168. // - object
  169. // - a nullable version of either of the above types
  170. // - an annotated type whose inner type is one of the above types
  171. // - a union type, nullable union type, or annotated union type that has one of the above types in its flattened member types
  172. // then remove from S all other entries.
  173. else if (value.is_object() && is<JS::DataView>(value.as_object())
  174. && has_overload_with_argument_type_or_subtype_matching(overloads, i, [](IDL::Type const& type) {
  175. if (type.is_plain() && (type.name() == "DataView" || type.name() == "BufferSource"))
  176. return true;
  177. if (type.is_object())
  178. return true;
  179. return false;
  180. })) {
  181. overloads.remove_all_other_entries();
  182. }
  183. // 7. Otherwise: if Type(V) is Object, V has a [[TypedArrayName]] internal slot, and there is an entry in S that has one of the following types at position i of its type list,
  184. // - a typed array type whose name is equal to the value of V’s [[TypedArrayName]] internal slot
  185. // - object
  186. // - a nullable version of either of the above types
  187. // - an annotated type whose inner type is one of the above types
  188. // - a union type, nullable union type, or annotated union type that has one of the above types in its flattened member types
  189. // then remove from S all other entries.
  190. else if (value.is_object() && value.as_object().is_typed_array()
  191. && has_overload_with_argument_type_or_subtype_matching(overloads, i, [&](IDL::Type const& type) {
  192. if (type.is_plain() && (type.name() == static_cast<JS::TypedArrayBase const&>(value.as_object()).element_name() || type.name() == "BufferSource"))
  193. return true;
  194. if (type.is_object())
  195. return true;
  196. return false;
  197. })) {
  198. overloads.remove_all_other_entries();
  199. }
  200. // 8. Otherwise: if IsCallable(V) is true, and there is an entry in S that has one of the following types at position i of its type list,
  201. // - a callback function type
  202. // - object
  203. // - a nullable version of any of the above types
  204. // - an annotated type whose inner type is one of the above types
  205. // - a union type, nullable union type, or annotated union type that has one of the above types in its flattened member types
  206. // then remove from S all other entries.
  207. else if (value.is_function()
  208. && has_overload_with_argument_type_or_subtype_matching(overloads, i, [](IDL::Type const& type) {
  209. // FIXME: - a callback function type
  210. if (type.is_object())
  211. return true;
  212. return false;
  213. })) {
  214. overloads.remove_all_other_entries();
  215. }
  216. // FIXME: 9. Otherwise: if Type(V) is Object and there is an entry in S that has one of the following types at position i of its type list,
  217. // - a sequence type
  218. // - a frozen array type
  219. // - a nullable version of any of the above types
  220. // - an annotated type whose inner type is one of the above types
  221. // - a union type, nullable union type, or annotated union type that has one of the above types in its flattened member types
  222. // and after performing the following steps,
  223. // {
  224. // 1. Let method be ? GetMethod(V, @@iterator).
  225. // }
  226. // method is not undefined, then remove from S all other entries.
  227. // 10. Otherwise: if Type(V) is Object and there is an entry in S that has one of the following types at position i of its type list,
  228. // - a callback interface type
  229. // - a dictionary type
  230. // - a record type
  231. // - object
  232. // - a nullable version of any of the above types
  233. // - an annotated type whose inner type is one of the above types
  234. // - a union type, nullable union type, or annotated union type that has one of the above types in its flattened member types
  235. // then remove from S all other entries.
  236. else if (value.is_object()
  237. && has_overload_with_argument_type_or_subtype_matching(overloads, i, [](IDL::Type const& type) {
  238. // FIXME: - a callback interface type
  239. // FIXME: - a dictionary type
  240. // FIXME: - a record type
  241. if (type.is_object())
  242. return true;
  243. return false;
  244. })) {
  245. overloads.remove_all_other_entries();
  246. }
  247. // 11. Otherwise: if Type(V) is Boolean and there is an entry in S that has one of the following types at position i of its type list,
  248. // - boolean
  249. // - a nullable boolean
  250. // - an annotated type whose inner type is one of the above types
  251. // - a union type, nullable union type, or annotated union type that has one of the above types in its flattened member types
  252. // then remove from S all other entries.
  253. else if (value.is_boolean()
  254. && has_overload_with_argument_type_or_subtype_matching(overloads, i, [](IDL::Type const& type) { return type.is_boolean(); })) {
  255. overloads.remove_all_other_entries();
  256. }
  257. // 12. Otherwise: if Type(V) is Number and there is an entry in S that has one of the following types at position i of its type list,
  258. // - a numeric type
  259. // - a nullable numeric type
  260. // - an annotated type whose inner type is one of the above types
  261. // - a union type, nullable union type, or annotated union type that has one of the above types in its flattened member types
  262. // then remove from S all other entries.
  263. else if (value.is_number()
  264. && has_overload_with_argument_type_or_subtype_matching(overloads, i, [](IDL::Type const& type) { return type.is_numeric(); })) {
  265. overloads.remove_all_other_entries();
  266. }
  267. // 13. Otherwise: if Type(V) is BigInt and there is an entry in S that has one of the following types at position i of its type list,
  268. // - bigint
  269. // - a nullable bigint
  270. // - an annotated type whose inner type is one of the above types
  271. // - a union type, nullable union type, or annotated union type that has one of the above types in its flattened member types
  272. // then remove from S all other entries.
  273. else if (value.is_bigint()
  274. && has_overload_with_argument_type_or_subtype_matching(overloads, i, [](IDL::Type const& type) { return type.is_bigint(); })) {
  275. overloads.remove_all_other_entries();
  276. }
  277. // 14. Otherwise: if there is an entry in S that has one of the following types at position i of its type list,
  278. // - a string type
  279. // - a nullable version of any of the above types
  280. // - an annotated type whose inner type is one of the above types
  281. // - a union type, nullable union type, or annotated union type that has one of the above types in its flattened member types
  282. // then remove from S all other entries.
  283. else if (has_overload_with_argument_type_or_subtype_matching(overloads, i, [](IDL::Type const& type) { return type.is_string(); })) {
  284. overloads.remove_all_other_entries();
  285. }
  286. // 15. Otherwise: if there is an entry in S that has one of the following types at position i of its type list,
  287. // - a numeric type
  288. // - a nullable numeric type
  289. // - an annotated type whose inner type is one of the above types
  290. // - a union type, nullable union type, or annotated union type that has one of the above types in its flattened member types
  291. // then remove from S all other entries.
  292. else if (has_overload_with_argument_type_or_subtype_matching(overloads, i, [](IDL::Type const& type) { return type.is_numeric(); })) {
  293. overloads.remove_all_other_entries();
  294. }
  295. // 16. Otherwise: if there is an entry in S that has one of the following types at position i of its type list,
  296. // - boolean
  297. // - a nullable boolean
  298. // - an annotated type whose inner type is one of the above types
  299. // - a union type, nullable union type, or annotated union type that has one of the above types in its flattened member types
  300. // then remove from S all other entries.
  301. else if (has_overload_with_argument_type_or_subtype_matching(overloads, i, [](IDL::Type const& type) { return type.is_boolean(); })) {
  302. overloads.remove_all_other_entries();
  303. }
  304. // 17. Otherwise: if there is an entry in S that has one of the following types at position i of its type list,
  305. // - bigint
  306. // - a nullable bigint
  307. // - an annotated type whose inner type is one of the above types
  308. // - a union type, nullable union type, or annotated union type that has one of the above types in its flattened member types
  309. // then remove from S all other entries.
  310. else if (has_overload_with_argument_type_or_subtype_matching(overloads, i, [](IDL::Type const& type) { return type.is_bigint(); })) {
  311. overloads.remove_all_other_entries();
  312. }
  313. // 18. Otherwise: if there is an entry in S that has any at position i of its type list, then remove from S all other entries.
  314. else if (overloads.has_overload_with_matching_argument_at_index(i, [](auto const& type, auto) { return type->is_any(); })) {
  315. overloads.remove_all_other_entries();
  316. }
  317. // 19. Otherwise: throw a TypeError.
  318. else {
  319. // FIXME: Remove this message once all the above sub-steps are implemented.
  320. dbgln("Failed to determine IDL overload. (Probably because of unimplemented steps.)");
  321. return vm.throw_completion<JS::TypeError>(JS::ErrorType::OverloadResolutionFailed);
  322. }
  323. }
  324. // 13. Let callable be the operation or extended attribute of the single entry in S.
  325. auto const& callable = overloads.only_item();
  326. // 14. If i = d and method is not undefined, then
  327. if (i == distinguishing_argument_index && method.has_value()) {
  328. // 1. Let V be args[i].
  329. auto const& value = vm.argument(i);
  330. // 2. Let T be the type at index i in the type list of the remaining entry in S.
  331. auto const& type = overloads.only_item().types[i];
  332. (void)value;
  333. (void)type;
  334. // FIXME: 3. If T is a sequence type, then append to values the result of creating a sequence of type T from V and method.
  335. // FIXME: 4. Otherwise, T is a frozen array type. Append to values the result of creating a frozen array of type T from V and method.
  336. // 5. Set i to i + 1.
  337. ++i;
  338. }
  339. // 15. While i < argcount:
  340. while (i < argument_count) {
  341. // 1. Let V be args[i].
  342. auto const& value = vm.argument(i);
  343. // 2. Let type be the type at index i in the type list of the remaining entry in S.
  344. auto const& entry = overloads.only_item();
  345. auto const& type = entry.types[i];
  346. // 3. Let optionality be the value at index i in the list of optionality values of the remaining entry in S.
  347. auto const& optionality = entry.optionality_values[i];
  348. // 4. If optionality is “optional” and V is undefined, then:
  349. if (optionality == IDL::Optionality::Optional && value.is_undefined()) {
  350. // FIXME: 1. If the argument at index i is declared with a default value, then append to values that default value.
  351. // 2. Otherwise, append to values the special value “missing”.
  352. values.empend(ResolvedOverload::Missing {});
  353. }
  354. // 5. Otherwise, append to values the result of converting V to IDL type type.
  355. else {
  356. values.append(convert_ecmascript_type_to_idl_value(value, type));
  357. }
  358. // 6. Set i to i + 1.
  359. ++i;
  360. }
  361. // 16. While i is less than the number of arguments callable is declared to take:
  362. while (i < static_cast<int>(callable.types.size())) {
  363. // FIXME: 1. If callable’s argument at index i is declared with a default value, then append to values that default value.
  364. if (false) {
  365. }
  366. // 2. Otherwise, if callable’s argument at index i is not variadic, then append to values the special value “missing”.
  367. else if (callable.optionality_values[i] != IDL::Optionality::Variadic) {
  368. values.empend(ResolvedOverload::Missing {});
  369. }
  370. // 3. Set i to i + 1.
  371. ++i;
  372. }
  373. // 17. Return the pair <callable, values>.
  374. return ResolvedOverload { callable.callable_id, move(values) };
  375. }
  376. }