OverloadResolution.cpp 22 KB

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