diff options
author | Bartek IwaĆczuk <biwanczuk@gmail.com> | 2021-09-18 03:44:53 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-09-18 03:44:53 +0200 |
commit | f840906943849f5a09981e172d57e84301b77386 (patch) | |
tree | 8c1b5fcaf0e96a9edd1124d5b8048d28b63a2e27 /runtime/web_worker.rs | |
parent | 7c0173df27f57450508ad1400ab5599b8f7593f9 (diff) |
fix(core): prevent multiple main module loading (#12128)
This commit fixes a problem where loading and executing multiple
modules leads to all of the having "import.meta.main" set to true.
Following Rust APIs were deprecated:
- deno_core::JsRuntime::load_module
- deno_runtime::Worker::execute_module
- deno_runtime::WebWorker::execute_module
Following Rust APIs were added:
- deno_core::JsRuntime::load_main_module
- deno_core::JsRuntime::load_side_module
- deno_runtime::Worker::execute_main_module
- deno_runtime::Worker::execute_side_module
- deno_runtime::WebWorker::execute_main_module
Trying to load multiple "main" modules into the runtime now results in an
error. If user needs to load additional "non-main" modules they should use
APIs for "side" module.
Diffstat (limited to 'runtime/web_worker.rs')
-rw-r--r-- | runtime/web_worker.rs | 34 |
1 files changed, 28 insertions, 6 deletions
diff --git a/runtime/web_worker.rs b/runtime/web_worker.rs index ed7b95f5b..975028b16 100644 --- a/runtime/web_worker.rs +++ b/runtime/web_worker.rs @@ -458,21 +458,32 @@ impl WebWorker { Ok(()) } - /// Loads and instantiates specified JavaScript module. + /// Loads and instantiates specified JavaScript module + /// as "main" or "side" module. pub async fn preload_module( &mut self, module_specifier: &ModuleSpecifier, + main: bool, ) -> Result<ModuleId, AnyError> { - self.js_runtime.load_module(module_specifier, None).await + if main { + self + .js_runtime + .load_main_module(module_specifier, None) + .await + } else { + self + .js_runtime + .load_side_module(module_specifier, None) + .await + } } /// Loads, instantiates and executes specified JavaScript module. - pub async fn execute_module( + pub async fn execute_main_module( &mut self, module_specifier: &ModuleSpecifier, ) -> Result<(), AnyError> { - let id = self.preload_module(module_specifier).await?; - + let id = self.preload_module(module_specifier, true).await?; let mut receiver = self.js_runtime.mod_evaluate(id); tokio::select! { maybe_result = &mut receiver => { @@ -494,6 +505,17 @@ impl WebWorker { } } + #[deprecated( + since = "0.26.0", + note = "This method had a bug, marking multiple modules loaded as \"main\". Use `execute_main_module`." + )] + pub async fn execute_module( + &mut self, + module_specifier: &ModuleSpecifier, + ) -> Result<(), AnyError> { + self.execute_main_module(module_specifier).await + } + pub fn poll_event_loop( &mut self, cx: &mut Context, @@ -568,7 +590,7 @@ pub fn run_web_worker( } else { // TODO(bartlomieju): add "type": "classic", ie. ability to load // script instead of module - worker.execute_module(&specifier).await + worker.execute_main_module(&specifier).await }; let internal_handle = worker.internal_handle.clone(); |