ImportMapParseResult.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. * Copyright (c) 2024, Jamie Mansfield <jmansfield@cadixdev.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibJS/Runtime/ModuleRequest.h>
  7. #include <LibWeb/Bindings/ExceptionOrUtils.h>
  8. #include <LibWeb/HTML/Scripting/ExceptionReporter.h>
  9. #include <LibWeb/HTML/Scripting/ImportMapParseResult.h>
  10. #include <LibWeb/HTML/Window.h>
  11. #include <LibWeb/WebIDL/DOMException.h>
  12. namespace Web::HTML {
  13. GC_DEFINE_ALLOCATOR(ImportMapParseResult);
  14. ImportMapParseResult::ImportMapParseResult() = default;
  15. ImportMapParseResult::~ImportMapParseResult() = default;
  16. // https://html.spec.whatwg.org/multipage/webappapis.html#create-an-import-map-parse-result
  17. GC::Ref<ImportMapParseResult> ImportMapParseResult::create(JS::Realm& realm, ByteString const& input, URL::URL base_url)
  18. {
  19. // 1. Let result be an import map parse result whose import map is null and whose error to rethrow is null.
  20. auto result = realm.create<ImportMapParseResult>();
  21. // 2. Parse an import map string given input and baseURL, catching any exceptions.
  22. auto import_map = parse_import_map_string(realm, input, base_url);
  23. // 2.1. If this threw an exception, then set result's error to rethrow to that exception.
  24. if (import_map.is_exception())
  25. result->set_error_to_rethrow(import_map.exception());
  26. // 2.2. Otherwise, set result's import map to the return value.
  27. else
  28. result->set_import_map(import_map.release_value());
  29. // 3. Return result.
  30. return result;
  31. }
  32. void ImportMapParseResult::visit_host_defined_self(Visitor& visitor)
  33. {
  34. visitor.visit(*this);
  35. }
  36. void ImportMapParseResult::visit_edges(Visitor& visitor)
  37. {
  38. Base::visit_edges(visitor);
  39. if (m_error_to_rethrow.has_value()) {
  40. m_error_to_rethrow.value().visit(
  41. [&](WebIDL::SimpleException const&) {
  42. // ignore
  43. },
  44. [&](GC::Ref<WebIDL::DOMException> exception) {
  45. visitor.visit(exception);
  46. },
  47. [&](JS::Completion const& completion) {
  48. if (completion.value().has_value())
  49. visitor.visit(completion.value().value());
  50. });
  51. }
  52. }
  53. // https://html.spec.whatwg.org/multipage/webappapis.html#register-an-import-map
  54. void ImportMapParseResult::register_import_map(Window& global)
  55. {
  56. // 1. If result's error to rethrow is not null, then report the exception given by result's error to rethrow and return.
  57. if (m_error_to_rethrow.has_value()) {
  58. auto completion = Web::Bindings::exception_to_throw_completion(global.vm(), m_error_to_rethrow.value());
  59. HTML::report_exception(completion, global.realm());
  60. return;
  61. }
  62. // 2. Merge existing and new import maps, given global and result's import map.
  63. VERIFY(m_import_map.has_value());
  64. merge_existing_and_new_import_maps(global, m_import_map.value());
  65. }
  66. }