| 
									
										
										
										
											2022-10-03 21:01:59 +02:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Copyright (c) 2022, networkException <networkexception@serenityos.org> | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-23 14:18:38 +01:00
										 |  |  | #include <LibJS/Runtime/ModuleRequest.h>
 | 
					
						
							| 
									
										
										
										
											2022-10-03 21:01:59 +02:00
										 |  |  | #include <LibWeb/HTML/Scripting/Environments.h>
 | 
					
						
							|  |  |  | #include <LibWeb/HTML/Scripting/Fetching.h>
 | 
					
						
							|  |  |  | #include <LibWeb/HTML/Scripting/ModuleScript.h>
 | 
					
						
							|  |  |  | #include <LibWeb/WebIDL/DOMException.h>
 | 
					
						
							| 
									
										
										
										
											2022-10-23 04:10:01 +02:00
										 |  |  | #include <LibWeb/WebIDL/ExceptionOr.h>
 | 
					
						
							| 
									
										
										
										
											2022-10-03 21:01:59 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace Web::HTML { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-19 19:47:52 +01:00
										 |  |  | JS_DEFINE_ALLOCATOR(JavaScriptModuleScript); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-03 21:01:59 +02:00
										 |  |  | ModuleScript::~ModuleScript() = default; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-18 16:22:27 +13:00
										 |  |  | ModuleScript::ModuleScript(URL::URL base_url, ByteString filename, EnvironmentSettingsObject& environment_settings_object) | 
					
						
							| 
									
										
										
										
											2022-10-03 21:01:59 +02:00
										 |  |  |     : Script(move(base_url), move(filename), environment_settings_object) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | JavaScriptModuleScript::~JavaScriptModuleScript() = default; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-18 16:22:27 +13:00
										 |  |  | JavaScriptModuleScript::JavaScriptModuleScript(URL::URL base_url, ByteString filename, EnvironmentSettingsObject& environment_settings_object) | 
					
						
							| 
									
										
										
										
											2022-10-03 21:01:59 +02:00
										 |  |  |     : ModuleScript(move(base_url), move(filename), environment_settings_object) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // https://html.spec.whatwg.org/multipage/webappapis.html#creating-a-javascript-module-script
 | 
					
						
							| 
									
										
										
										
											2024-03-18 16:22:27 +13:00
										 |  |  | WebIDL::ExceptionOr<JS::GCPtr<JavaScriptModuleScript>> JavaScriptModuleScript::create(ByteString const& filename, StringView source, EnvironmentSettingsObject& settings_object, URL::URL base_url) | 
					
						
							| 
									
										
										
										
											2022-10-03 21:01:59 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     // 1. If scripting is disabled for settings, then set source to the empty string.
 | 
					
						
							|  |  |  |     if (settings_object.is_scripting_disabled()) | 
					
						
							|  |  |  |         source = ""sv; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     auto& realm = settings_object.realm(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // 2. Let script be a new module script that this algorithm will subsequently initialize.
 | 
					
						
							| 
									
										
										
										
											2023-08-13 13:05:26 +02:00
										 |  |  |     auto script = realm.heap().allocate<JavaScriptModuleScript>(realm, move(base_url), filename, settings_object); | 
					
						
							| 
									
										
										
										
											2022-10-03 21:01:59 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-23 04:10:01 +02:00
										 |  |  |     // 3. Set script's settings object to settings.
 | 
					
						
							|  |  |  |     // NOTE: This was already done when constructing.
 | 
					
						
							| 
									
										
										
										
											2022-10-03 21:01:59 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-23 04:10:01 +02:00
										 |  |  |     // 4. Set script's base URL to baseURL.
 | 
					
						
							|  |  |  |     // NOTE: This was already done when constructing.
 | 
					
						
							| 
									
										
										
										
											2022-10-03 21:01:59 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // FIXME: 5. Set script's fetch options to options.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // 6. Set script's parse error and error to rethrow to null.
 | 
					
						
							| 
									
										
										
										
											2023-05-18 19:05:56 +02:00
										 |  |  |     script->set_parse_error(JS::js_null()); | 
					
						
							|  |  |  |     script->set_error_to_rethrow(JS::js_null()); | 
					
						
							| 
									
										
										
										
											2022-10-03 21:01:59 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // 7. Let result be ParseModule(source, settings's Realm, script).
 | 
					
						
							|  |  |  |     auto result = JS::SourceTextModule::parse(source, settings_object.realm(), filename.view(), script); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // 8. If result is a list of errors, then:
 | 
					
						
							|  |  |  |     if (result.is_error()) { | 
					
						
							|  |  |  |         auto& parse_error = result.error().first(); | 
					
						
							| 
									
										
										
										
											2024-04-05 09:26:03 +02:00
										 |  |  |         dbgln("JavaScriptModuleScript: Failed to parse: {}", parse_error.to_string()); | 
					
						
							| 
									
										
										
										
											2022-10-03 21:01:59 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-18 19:05:56 +02:00
										 |  |  |         // 1. Set script's parse error to result[0].
 | 
					
						
							| 
									
										
										
										
											2024-04-05 00:39:53 +02:00
										 |  |  |         script->set_parse_error(JS::SyntaxError::create(settings_object.realm(), parse_error.to_string())); | 
					
						
							| 
									
										
										
										
											2022-10-03 21:01:59 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         // 2. Return script.
 | 
					
						
							|  |  |  |         return script; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-02 16:20:01 +01:00
										 |  |  |     // 9. For each ModuleRequest record requested of result.[[RequestedModules]]:
 | 
					
						
							| 
									
										
										
										
											2022-10-03 21:01:59 +02:00
										 |  |  |     for (auto const& requested : result.value()->requested_modules()) { | 
					
						
							|  |  |  |         // FIXME: Clarify if this should be checked for all requested before running the steps below.
 | 
					
						
							| 
									
										
										
										
											2023-12-02 16:20:01 +01:00
										 |  |  |         // 1. If requested.[[Attributes]] contains a Record entry such that entry.[[Key]] is not "type", then:
 | 
					
						
							|  |  |  |         for (auto const& attribute : requested.attributes) { | 
					
						
							|  |  |  |             if (attribute.key != "type"sv) { | 
					
						
							|  |  |  |                 // 1. Let error be a new SyntaxError exception.
 | 
					
						
							|  |  |  |                 auto error = JS::SyntaxError::create(settings_object.realm(), "Module request attributes must only contain a type attribute"_fly_string); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 // 2. Set script's parse error to error.
 | 
					
						
							|  |  |  |                 script->set_parse_error(error); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 // 3. Return script.
 | 
					
						
							|  |  |  |                 return script; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-10-03 21:01:59 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-02 16:20:01 +01:00
										 |  |  |         // 2. Let url be the result of resolving a module specifier given script and requested.[[Specifier]], catching any exceptions.
 | 
					
						
							| 
									
										
										
										
											2022-10-23 04:10:01 +02:00
										 |  |  |         auto url = resolve_module_specifier(*script, requested.module_specifier); | 
					
						
							| 
									
										
										
										
											2022-10-03 21:01:59 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-02 16:20:01 +01:00
										 |  |  |         // 3. If the previous step threw an exception, then:
 | 
					
						
							| 
									
										
										
										
											2022-10-23 04:10:01 +02:00
										 |  |  |         if (url.is_exception()) { | 
					
						
							|  |  |  |             // FIXME: 1. Set script's parse error to that exception.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             // 2. Return script.
 | 
					
						
							|  |  |  |             return script; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-02 16:20:01 +01:00
										 |  |  |         // 4. Let moduleType be the result of running the module type from module request steps given requested.
 | 
					
						
							| 
									
										
										
										
											2022-10-03 21:01:59 +02:00
										 |  |  |         auto module_type = module_type_from_module_request(requested); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-02 16:20:01 +01:00
										 |  |  |         // 5. If the result of running the module type allowed steps given moduleType and settings is false, then:
 | 
					
						
							| 
									
										
										
										
											2022-10-23 04:10:01 +02:00
										 |  |  |         if (!settings_object.module_type_allowed(module_type)) { | 
					
						
							| 
									
										
										
										
											2022-10-03 21:01:59 +02:00
										 |  |  |             // FIXME: 1. Let error be a new TypeError exception.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             // FIXME: 2. Set script's parse error to error.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-23 04:10:01 +02:00
										 |  |  |             // 3. Return script.
 | 
					
						
							|  |  |  |             return script; | 
					
						
							| 
									
										
										
										
											2022-10-03 21:01:59 +02:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-02 16:20:01 +01:00
										 |  |  |     // 10. Set script's record to result.
 | 
					
						
							| 
									
										
										
										
											2022-10-03 21:01:59 +02:00
										 |  |  |     script->m_record = result.value(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-02 16:20:01 +01:00
										 |  |  |     // 11. Return script.
 | 
					
						
							| 
									
										
										
										
											2022-10-03 21:01:59 +02:00
										 |  |  |     return script; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // https://html.spec.whatwg.org/multipage/webappapis.html#run-a-module-script
 | 
					
						
							|  |  |  | JS::Promise* JavaScriptModuleScript::run(PreventErrorReporting) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     // 1. Let settings be the settings object of script.
 | 
					
						
							|  |  |  |     auto& settings = settings_object(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // 2. Check if we can run script with settings. If this returns "do not run", then return a promise resolved with undefined.
 | 
					
						
							|  |  |  |     if (settings.can_run_script() == RunScriptDecision::DoNotRun) { | 
					
						
							| 
									
										
										
										
											2022-12-13 20:49:50 +00:00
										 |  |  |         auto promise = JS::Promise::create(settings.realm()); | 
					
						
							| 
									
										
										
										
											2022-10-03 21:01:59 +02:00
										 |  |  |         promise->fulfill(JS::js_undefined()); | 
					
						
							|  |  |  |         return promise; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // 3. Prepare to run script given settings.
 | 
					
						
							|  |  |  |     settings.prepare_to_run_script(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // 4. Let evaluationPromise be null.
 | 
					
						
							|  |  |  |     JS::Promise* evaluation_promise = nullptr; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-18 19:05:56 +02:00
										 |  |  |     // 5. If script's error to rethrow is not null, then set evaluationPromise to a promise rejected with script's error to rethrow.
 | 
					
						
							|  |  |  |     if (!error_to_rethrow().is_null()) { | 
					
						
							|  |  |  |         evaluation_promise = JS::Promise::create(settings.realm()); | 
					
						
							|  |  |  |         evaluation_promise->reject(error_to_rethrow()); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-10-03 21:01:59 +02:00
										 |  |  |     // 6. Otherwise:
 | 
					
						
							| 
									
										
										
										
											2023-05-18 19:05:56 +02:00
										 |  |  |     else { | 
					
						
							| 
									
										
										
										
											2022-10-03 21:01:59 +02:00
										 |  |  |         // 1. Let record be script's record.
 | 
					
						
							|  |  |  |         auto record = m_record; | 
					
						
							| 
									
										
										
										
											2023-05-18 19:05:56 +02:00
										 |  |  |         VERIFY(record); | 
					
						
							| 
									
										
										
										
											2022-10-03 21:01:59 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-06 11:57:47 +01:00
										 |  |  |         // NON-STANDARD: To ensure that LibJS can find the module on the stack, we push a new execution context.
 | 
					
						
							| 
									
										
										
										
											2024-05-31 15:12:33 +02:00
										 |  |  |         auto module_execution_context = JS::ExecutionContext::create(); | 
					
						
							| 
									
										
										
										
											2023-12-06 11:57:47 +01:00
										 |  |  |         module_execution_context->realm = &settings.realm(); | 
					
						
							|  |  |  |         module_execution_context->script_or_module = JS::NonnullGCPtr<JS::Module> { *record }; | 
					
						
							|  |  |  |         vm().push_execution_context(*module_execution_context); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-03 21:01:59 +02:00
										 |  |  |         // 2. Set evaluationPromise to record.Evaluate().
 | 
					
						
							|  |  |  |         auto elevation_promise_or_error = record->evaluate(vm()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // NOTE: This step will recursively evaluate all of the module's dependencies.
 | 
					
						
							|  |  |  |         // If Evaluate fails to complete as a result of the user agent aborting the running script,
 | 
					
						
							|  |  |  |         // then set evaluationPromise to a promise rejected with a new "QuotaExceededError" DOMException.
 | 
					
						
							|  |  |  |         if (elevation_promise_or_error.is_error()) { | 
					
						
							| 
									
										
										
										
											2022-12-13 20:49:50 +00:00
										 |  |  |             auto promise = JS::Promise::create(settings_object().realm()); | 
					
						
							| 
									
										
										
										
											2023-09-06 16:03:01 +12:00
										 |  |  |             promise->reject(WebIDL::QuotaExceededError::create(settings_object().realm(), "Failed to evaluate module script"_fly_string).ptr()); | 
					
						
							| 
									
										
										
										
											2022-10-03 21:01:59 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |             evaluation_promise = promise; | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             evaluation_promise = elevation_promise_or_error.value(); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-12-06 11:57:47 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         // NON-STANDARD: Pop the execution context mentioned above.
 | 
					
						
							|  |  |  |         vm().pop_execution_context(); | 
					
						
							| 
									
										
										
										
											2022-10-03 21:01:59 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // FIXME: 7. If preventErrorReporting is false, then upon rejection of evaluationPromise with reason, report the exception given by reason for script.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // 8. Clean up after running script with settings.
 | 
					
						
							|  |  |  |     settings.clean_up_after_running_script(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // 9. Return evaluationPromise.
 | 
					
						
							|  |  |  |     return evaluation_promise; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void JavaScriptModuleScript::visit_edges(Cell::Visitor& visitor) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     Base::visit_edges(visitor); | 
					
						
							|  |  |  |     visitor.visit(m_record); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |