diff options
author | Bartek IwaĆczuk <biwanczuk@gmail.com> | 2021-12-15 19:22:36 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-12-15 19:22:36 +0100 |
commit | a1f0796fccfafee19b2fe06155efe746da2e9654 (patch) | |
tree | 01942b1bc202352418c88dbf8a1c447e72f6f976 /core/runtime.rs | |
parent | ec7d90666f68ae0bf6036a6915296622adc9b65c (diff) |
feat: Add support for import assertions and JSON modules (#12866)
This commit adds proper support for import assertions and JSON modules.
Implementation of "core/modules.rs" was changed to account for multiple possible
module types, instead of always assuming that the code is an "ES module". In
effect "ModuleMap" now has knowledge about each modules' type (stored via
"ModuleType" enum). Module loading pipeline now stores information about
expected module type for each request and validates that expected type matches
discovered module type based on file's "MediaType".
Relevant tests were added to "core/modules.rs" and integration tests,
additionally multiple WPT tests were enabled.
There are still some rough edges in the implementation and not all WPT were
enabled, due to:
a) unclear BOM handling in source code by "FileFetcher"
b) design limitation of Deno's "FileFetcher" that doesn't download the same
module multiple times in a single run
Co-authored-by: Kitson Kelly <me@kitsonkelly.com>
Diffstat (limited to 'core/runtime.rs')
-rw-r--r-- | core/runtime.rs | 27 |
1 files changed, 16 insertions, 11 deletions
diff --git a/core/runtime.rs b/core/runtime.rs index 15bb103ba..643a4198d 100644 --- a/core/runtime.rs +++ b/core/runtime.rs @@ -1259,12 +1259,15 @@ impl JsRuntime { if let Some(load_stream_result) = maybe_result { match load_stream_result { - Ok(info) => { + Ok((request, info)) => { // A module (not necessarily the one dynamically imported) has been // fetched. Create and register it, and if successful, poll for the // next recursive-load event related to this dynamic import. - let register_result = - load.register_and_recurse(&mut self.handle_scope(), &info); + let register_result = load.register_and_recurse( + &mut self.handle_scope(), + &request, + &info, + ); match register_result { Ok(()) => { @@ -1417,7 +1420,7 @@ impl JsRuntime { ) -> Result<ModuleId, Error> { let module_map_rc = Self::module_map(self.v8_isolate()); if let Some(code) = code { - module_map_rc.borrow_mut().new_module( + module_map_rc.borrow_mut().new_es_module( &mut self.handle_scope(), // main module true, @@ -1429,10 +1432,10 @@ impl JsRuntime { let mut load = ModuleMap::load_main(module_map_rc.clone(), specifier.as_str()).await?; - while let Some(info_result) = load.next().await { - let info = info_result?; + while let Some(load_result) = load.next().await { + let (request, info) = load_result?; let scope = &mut self.handle_scope(); - load.register_and_recurse(scope, &info)?; + load.register_and_recurse(scope, &request, &info)?; } let root_id = load.root_module_id.expect("Root module should be loaded"); @@ -1454,7 +1457,7 @@ impl JsRuntime { ) -> Result<ModuleId, Error> { let module_map_rc = Self::module_map(self.v8_isolate()); if let Some(code) = code { - module_map_rc.borrow_mut().new_module( + module_map_rc.borrow_mut().new_es_module( &mut self.handle_scope(), // not main module false, @@ -1466,10 +1469,10 @@ impl JsRuntime { let mut load = ModuleMap::load_side(module_map_rc.clone(), specifier.as_str()).await?; - while let Some(info_result) = load.next().await { - let info = info_result?; + while let Some(load_result) = load.next().await { + let (request, info) = load_result?; let scope = &mut self.handle_scope(); - load.register_and_recurse(scope, &info)?; + load.register_and_recurse(scope, &request, &info)?; } let root_id = load.root_module_id.expect("Root module should be loaded"); @@ -1630,6 +1633,7 @@ pub mod tests { use crate::error::custom_error; use crate::modules::ModuleSource; use crate::modules::ModuleSourceFuture; + use crate::modules::ModuleType; use crate::op_async; use crate::op_sync; use crate::ZeroCopyBuf; @@ -2642,6 +2646,7 @@ assertEquals(1, notify_return_value); code: "console.log('hello world');".to_string(), module_url_specified: "file:///main.js".to_string(), module_url_found: "file:///main.js".to_string(), + module_type: ModuleType::JavaScript, }) } .boxed_local() |