Skip to content

Commit

Permalink
fix: dynamic import panic (#2792)
Browse files Browse the repository at this point in the history
  • Loading branch information
bartlomieju authored and ry committed Aug 21, 2019
1 parent 389763c commit b764d1b
Showing 1 changed file with 56 additions and 8 deletions.
64 changes: 56 additions & 8 deletions core/isolate.rs
Expand Up @@ -440,10 +440,7 @@ impl Isolate {
libdeno::deno_mod_new(self.libdeno_isolate, main, name_ptr, source_ptr)
};

self.check_last_exception().map(|_| id).map_err(|err| {
assert_eq!(id, 0);
err
})
self.check_last_exception().map(|_| id)
}

pub fn mod_get_imports(&self, id: deno_mod) -> Vec<String> {
Expand Down Expand Up @@ -503,10 +500,7 @@ impl Isolate {
err_str_ptr,
)
};
self.check_last_exception().map_err(|err| {
assert_eq!(id, 0);
err
})
self.check_last_exception()
}

fn poll_dyn_imports(&mut self) -> Poll<(), ErrBox> {
Expand Down Expand Up @@ -1012,6 +1006,60 @@ pub mod tests {
})
}

#[test]
fn dyn_import_err2() {
use std::convert::TryInto;
// Import multiple modules to demonstrate that after failed dynamic import
// another dynamic import can still be run
run_in_task(|| {
let count = Arc::new(AtomicUsize::new(0));
let count_ = count.clone();
let mut isolate = Isolate::new(StartupData::None, false);
isolate.set_dyn_import(move |_, specifier, referrer| {
let c = count_.fetch_add(1, Ordering::Relaxed);
match c {
0 => assert_eq!(specifier, "foo1.js"),
1 => assert_eq!(specifier, "foo2.js"),
2 => assert_eq!(specifier, "foo3.js"),
_ => unreachable!(),
}
assert_eq!(referrer, "dyn_import_error.js");

let source_code_info = SourceCodeInfo {
module_url_specified: specifier.to_owned(),
module_url_found: specifier.to_owned(),
code: "# not valid JS".to_owned(),
};
let stream = MockImportStream(vec![
Ok(RecursiveLoadEvent::Fetch(source_code_info)),
Ok(RecursiveLoadEvent::Instantiate(c.try_into().unwrap())),
]);
Box::new(stream)
});

js_check(isolate.execute(
"dyn_import_error.js",
r#"
(async () => {
await import("foo1.js");
})();
(async () => {
await import("foo2.js");
})();
(async () => {
await import("foo3.js");
})();
"#,
));

assert_eq!(count.load(Ordering::Relaxed), 3);
// Now each poll should return error
assert!(isolate.poll().is_err());
assert!(isolate.poll().is_err());
assert!(isolate.poll().is_err());
})
}

#[test]
fn dyn_import_ok() {
run_in_task(|| {
Expand Down

0 comments on commit b764d1b

Please sign in to comment.