ImportMapParseResult.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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/HTML/Scripting/ExceptionReporter.h>
  8. #include <LibWeb/HTML/Scripting/ImportMapParseResult.h>
  9. #include <LibWeb/HTML/Window.h>
  10. #include <LibWeb/WebIDL/DOMException.h>
  11. namespace Web::HTML {
  12. JS_DEFINE_ALLOCATOR(ImportMapParseResult);
  13. ImportMapParseResult::ImportMapParseResult() = default;
  14. ImportMapParseResult::~ImportMapParseResult() = default;
  15. // https://html.spec.whatwg.org/multipage/webappapis.html#create-an-import-map-parse-result
  16. JS::NonnullGCPtr<ImportMapParseResult> ImportMapParseResult::create(JS::Realm& realm, ByteString const& input, URL::URL base_url)
  17. {
  18. // 1. Let result be an import map parse result whose import map is null and whose error to rethrow is null.
  19. auto result = realm.heap().allocate<ImportMapParseResult>(realm);
  20. result->set_error_to_rethrow(JS::js_null());
  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. // FIXME: rethrow the original exception
  25. if (import_map.is_exception())
  26. result->set_error_to_rethrow(JS::Error::create(realm, "Failed to parse import map string"sv));
  27. // 2.2. Otherwise, set result's import map to the return value.
  28. else
  29. result->set_import_map(import_map.release_value());
  30. // 3. Return result.
  31. return result;
  32. }
  33. void ImportMapParseResult::visit_host_defined_self(JS::Cell::Visitor& visitor)
  34. {
  35. visitor.visit(*this);
  36. }
  37. void ImportMapParseResult::visit_edges(Visitor& visitor)
  38. {
  39. Base::visit_edges(visitor);
  40. visitor.visit(m_error_to_rethrow);
  41. }
  42. // https://html.spec.whatwg.org/multipage/webappapis.html#register-an-import-map
  43. void ImportMapParseResult::register_import_map(Window& global)
  44. {
  45. // 1. If result's error to rethrow is not null, then report the exception given by result's error to rethrow and return.
  46. if (!m_error_to_rethrow.is_null()) {
  47. HTML::report_exception(m_error_to_rethrow, global.realm());
  48. return;
  49. }
  50. // 2. Assert: global's import map is an empty import map.
  51. VERIFY(global.import_map().imports().is_empty() && global.import_map().scopes().is_empty());
  52. // 3. Set global's import map to result's import map.
  53. VERIFY(m_import_map.has_value());
  54. global.set_import_map(m_import_map.value());
  55. }
  56. }