mirror of https://github.com/boa-dev/boa.git
Browse Source
* 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 errorpull/3787/head
Hans Larsen
8 months ago
committed by
GitHub
9 changed files with 136 additions and 16 deletions
@ -0,0 +1,5 @@ |
|||||||
|
import { file1_2 } from "./file1_2.js"; |
||||||
|
|
||||||
|
export function file1_1() { |
||||||
|
return "file1_1" + "." + file1_2(); |
||||||
|
} |
@ -0,0 +1,3 @@ |
|||||||
|
export function file1_2() { |
||||||
|
return "file1_2"; |
||||||
|
} |
@ -0,0 +1,5 @@ |
|||||||
|
import { file1_1 } from "./dir1/file1_1.js"; |
||||||
|
|
||||||
|
export function file1() { |
||||||
|
return "file1" + ".." + file1_1(); |
||||||
|
} |
@ -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()); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue