-
-
Notifications
You must be signed in to change notification settings - Fork 400
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a path to Module (and expose it in Referrer) (#3783)
* Add an integration test for relative imports on SimpleModuleLoader * Add a path to Module (and expose it in Referrer) This allows SimpleModuleLoader to resolve relative to the current file (which this commit also does). Fixes #3782 * cargo clippy and fmt * prettier * Fix merge error
- Loading branch information
Showing
9 changed files
with
136 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { file1_2 } from "./file1_2.js"; | ||
|
||
export function file1_1() { | ||
return "file1_1" + "." + file1_2(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export function file1_2() { | ||
return "file1_2"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { file1_1 } from "./dir1/file1_1.js"; | ||
|
||
export function file1() { | ||
return "file1" + ".." + file1_1(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
#![allow(unused_crate_dependencies, missing_docs)] | ||
|
||
use std::path::PathBuf; | ||
use std::rc::Rc; | ||
|
||
use boa_engine::builtins::promise::PromiseState; | ||
use boa_engine::module::SimpleModuleLoader; | ||
use boa_engine::{js_string, Context, JsValue, Source}; | ||
|
||
/// Test that relative imports work with the simple module loader. | ||
#[test] | ||
fn subdirectories() { | ||
let assets_dir = | ||
PathBuf::from(std::env::var("CARGO_MANIFEST_DIR").unwrap()).join("tests/assets"); | ||
|
||
let loader = Rc::new(SimpleModuleLoader::new(assets_dir).unwrap()); | ||
let mut context = Context::builder() | ||
.module_loader(loader.clone()) | ||
.build() | ||
.unwrap(); | ||
|
||
let source = Source::from_bytes(b"export { file1 } from './file1.js';"); | ||
let module = boa_engine::Module::parse(source, None, &mut context).unwrap(); | ||
let result = module.load_link_evaluate(&mut context); | ||
|
||
context.run_jobs(); | ||
match result.state() { | ||
PromiseState::Pending => {} | ||
PromiseState::Fulfilled(v) => { | ||
assert!(v.is_undefined()); | ||
|
||
let foo_value = module | ||
.namespace(&mut context) | ||
.get(js_string!("file1"), &mut context) | ||
.unwrap() | ||
.as_callable() | ||
.unwrap() | ||
.call(&JsValue::undefined(), &[], &mut context) | ||
.unwrap(); | ||
|
||
assert_eq!( | ||
foo_value, | ||
JsValue::String(js_string!("file1..file1_1.file1_2")) | ||
); | ||
} | ||
PromiseState::Rejected(reason) => { | ||
panic!("Module failed to load: {}", reason.display()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters