2020-01-02 18:41:59 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2019-03-26 11:56:34 -04:00
|
|
|
|
2020-01-08 09:06:04 -05:00
|
|
|
use rusty_v8 as v8;
|
2019-03-26 11:56:34 -04:00
|
|
|
|
2019-07-10 18:53:48 -04:00
|
|
|
use crate::any_error::ErrBox;
|
2020-01-07 06:45:44 -05:00
|
|
|
use crate::es_isolate::DynImportId;
|
|
|
|
use crate::es_isolate::ModuleId;
|
|
|
|
use crate::es_isolate::SourceCodeInfo;
|
2019-06-12 19:55:59 -04:00
|
|
|
use crate::module_specifier::ModuleSpecifier;
|
2019-11-16 19:17:47 -05:00
|
|
|
use futures::future::FutureExt;
|
2019-08-07 12:55:39 -04:00
|
|
|
use futures::stream::FuturesUnordered;
|
|
|
|
use futures::stream::Stream;
|
2019-11-16 19:17:47 -05:00
|
|
|
use futures::stream::TryStreamExt;
|
2019-03-26 11:56:34 -04:00
|
|
|
use std::collections::HashMap;
|
2019-04-16 15:13:42 -04:00
|
|
|
use std::collections::HashSet;
|
|
|
|
use std::fmt;
|
2019-11-16 19:17:47 -05:00
|
|
|
use std::future::Future;
|
|
|
|
use std::pin::Pin;
|
2019-06-05 16:35:38 -04:00
|
|
|
use std::sync::Arc;
|
2019-11-16 19:17:47 -05:00
|
|
|
use std::task::Context;
|
|
|
|
use std::task::Poll;
|
2019-03-26 11:56:34 -04:00
|
|
|
|
2019-07-10 18:53:48 -04:00
|
|
|
pub type SourceCodeInfoFuture =
|
2019-11-16 19:17:47 -05:00
|
|
|
dyn Future<Output = Result<SourceCodeInfo, ErrBox>> + Send;
|
2019-03-26 11:56:34 -04:00
|
|
|
|
2019-06-05 16:35:38 -04:00
|
|
|
pub trait Loader: Send + Sync {
|
2019-03-26 11:56:34 -04:00
|
|
|
/// Returns an absolute URL.
|
|
|
|
/// When implementing an spec-complaint VM, this should be exactly the
|
|
|
|
/// algorithm described here:
|
|
|
|
/// https://html.spec.whatwg.org/multipage/webappapis.html#resolve-a-module-specifier
|
2020-01-25 12:53:16 -05:00
|
|
|
///
|
|
|
|
/// `is_main` can be used to resolve from current working directory or
|
|
|
|
/// apply import map for child imports.
|
2019-06-09 09:08:20 -04:00
|
|
|
fn resolve(
|
|
|
|
&self,
|
|
|
|
specifier: &str,
|
|
|
|
referrer: &str,
|
2019-08-07 12:55:39 -04:00
|
|
|
is_main: bool,
|
2019-07-10 18:53:48 -04:00
|
|
|
) -> Result<ModuleSpecifier, ErrBox>;
|
2019-03-26 11:56:34 -04:00
|
|
|
|
2019-06-12 19:55:59 -04:00
|
|
|
/// Given ModuleSpecifier, load its source code.
|
2020-01-25 12:53:16 -05:00
|
|
|
///
|
|
|
|
/// `is_dyn_import` can be used to check permissions or deny
|
|
|
|
/// dynamic imports altogether.
|
2019-06-12 19:55:59 -04:00
|
|
|
fn load(
|
|
|
|
&self,
|
|
|
|
module_specifier: &ModuleSpecifier,
|
2019-11-25 09:33:23 -05:00
|
|
|
maybe_referrer: Option<ModuleSpecifier>,
|
2020-01-25 12:53:16 -05:00
|
|
|
is_dyn_import: bool,
|
2019-11-16 19:17:47 -05:00
|
|
|
) -> Pin<Box<SourceCodeInfoFuture>>;
|
2019-03-26 11:56:34 -04:00
|
|
|
}
|
|
|
|
|
2019-08-07 12:55:39 -04:00
|
|
|
#[derive(Debug, Eq, PartialEq)]
|
|
|
|
enum Kind {
|
|
|
|
Main,
|
2020-01-08 09:06:04 -05:00
|
|
|
DynamicImport,
|
2019-08-07 12:55:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Eq, PartialEq)]
|
2020-01-08 09:06:04 -05:00
|
|
|
pub enum LoadState {
|
|
|
|
ResolveMain(String, Option<String>),
|
|
|
|
ResolveImport(String, String),
|
2019-08-07 12:55:39 -04:00
|
|
|
LoadingRoot,
|
2020-01-08 09:06:04 -05:00
|
|
|
LoadingImports,
|
|
|
|
Done,
|
2019-03-26 11:56:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// This future is used to implement parallel async module loading without
|
2020-01-08 09:06:04 -05:00
|
|
|
/// that is consumed by the isolate.
|
|
|
|
pub struct RecursiveModuleLoad {
|
2019-08-07 12:55:39 -04:00
|
|
|
kind: Kind,
|
2020-01-08 09:06:04 -05:00
|
|
|
// Kind::Main
|
|
|
|
pub root_module_id: Option<ModuleId>,
|
|
|
|
// Kind::Main
|
|
|
|
pub dyn_import_id: Option<DynImportId>,
|
|
|
|
pub state: LoadState,
|
|
|
|
pub loader: Arc<Box<dyn Loader + Unpin>>,
|
|
|
|
pub pending: FuturesUnordered<Pin<Box<SourceCodeInfoFuture>>>,
|
|
|
|
pub is_pending: HashSet<ModuleSpecifier>,
|
2019-03-26 11:56:34 -04:00
|
|
|
}
|
|
|
|
|
2020-01-08 09:06:04 -05:00
|
|
|
impl RecursiveModuleLoad {
|
2019-08-07 12:55:39 -04:00
|
|
|
/// Starts a new parallel load of the given URL of the main module.
|
|
|
|
pub fn main(
|
|
|
|
specifier: &str,
|
2019-10-19 17:19:19 -04:00
|
|
|
code: Option<String>,
|
2020-01-08 09:06:04 -05:00
|
|
|
loader: Arc<Box<dyn Loader + Unpin>>,
|
2019-08-07 12:55:39 -04:00
|
|
|
) -> Self {
|
|
|
|
let kind = Kind::Main;
|
2020-01-08 09:06:04 -05:00
|
|
|
let state = LoadState::ResolveMain(specifier.to_owned(), code);
|
|
|
|
Self::new(kind, state, loader, None)
|
2019-08-07 12:55:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn dynamic_import(
|
2020-01-06 14:07:35 -05:00
|
|
|
id: DynImportId,
|
2019-08-07 12:55:39 -04:00
|
|
|
specifier: &str,
|
|
|
|
referrer: &str,
|
2020-01-08 09:06:04 -05:00
|
|
|
loader: Arc<Box<dyn Loader + Unpin>>,
|
2019-08-07 12:55:39 -04:00
|
|
|
) -> Self {
|
2020-01-08 09:06:04 -05:00
|
|
|
let kind = Kind::DynamicImport;
|
|
|
|
let state =
|
|
|
|
LoadState::ResolveImport(specifier.to_owned(), referrer.to_owned());
|
|
|
|
Self::new(kind, state, loader, Some(id))
|
2019-08-07 12:55:39 -04:00
|
|
|
}
|
|
|
|
|
2020-01-08 09:06:04 -05:00
|
|
|
pub fn is_dynamic_import(&self) -> bool {
|
|
|
|
self.kind != Kind::Main
|
2019-08-07 12:55:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
fn new(
|
|
|
|
kind: Kind,
|
2020-01-08 09:06:04 -05:00
|
|
|
state: LoadState,
|
|
|
|
loader: Arc<Box<dyn Loader + Unpin>>,
|
|
|
|
dyn_import_id: Option<DynImportId>,
|
2019-06-05 16:35:38 -04:00
|
|
|
) -> Self {
|
2019-04-16 15:13:42 -04:00
|
|
|
Self {
|
2020-01-08 09:06:04 -05:00
|
|
|
root_module_id: None,
|
|
|
|
dyn_import_id,
|
2019-08-07 12:55:39 -04:00
|
|
|
kind,
|
|
|
|
state,
|
2019-06-05 16:35:38 -04:00
|
|
|
loader,
|
2019-08-07 12:55:39 -04:00
|
|
|
pending: FuturesUnordered::new(),
|
2019-04-16 15:13:42 -04:00
|
|
|
is_pending: HashSet::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-07 12:55:39 -04:00
|
|
|
fn add_root(&mut self) -> Result<(), ErrBox> {
|
|
|
|
let module_specifier = match self.state {
|
2020-01-08 09:06:04 -05:00
|
|
|
LoadState::ResolveMain(ref specifier, _) => {
|
2020-01-25 12:53:16 -05:00
|
|
|
self.loader.resolve(specifier, ".", true)?
|
2019-11-16 19:17:47 -05:00
|
|
|
}
|
2020-01-08 09:06:04 -05:00
|
|
|
LoadState::ResolveImport(ref specifier, ref referrer) => {
|
2020-01-25 12:53:16 -05:00
|
|
|
self.loader.resolve(specifier, referrer, false)?
|
2019-08-07 12:55:39 -04:00
|
|
|
}
|
2019-03-26 11:56:34 -04:00
|
|
|
|
2020-01-08 09:06:04 -05:00
|
|
|
_ => unreachable!(),
|
2019-08-07 12:55:39 -04:00
|
|
|
};
|
|
|
|
|
2020-01-08 09:06:04 -05:00
|
|
|
let load_fut = match &self.state {
|
|
|
|
LoadState::ResolveMain(_, Some(code)) => {
|
|
|
|
futures::future::ok(SourceCodeInfo {
|
|
|
|
code: code.to_owned(),
|
|
|
|
module_url_specified: module_specifier.to_string(),
|
|
|
|
module_url_found: module_specifier.to_string(),
|
|
|
|
})
|
|
|
|
.boxed()
|
|
|
|
}
|
2020-01-25 12:53:16 -05:00
|
|
|
_ => self
|
|
|
|
.loader
|
|
|
|
.load(&module_specifier, None, self.is_dynamic_import())
|
|
|
|
.boxed(),
|
2019-08-07 12:55:39 -04:00
|
|
|
};
|
2019-04-18 21:33:50 -04:00
|
|
|
|
2020-01-08 09:06:04 -05:00
|
|
|
self.pending.push(load_fut);
|
2019-08-07 12:55:39 -04:00
|
|
|
|
2020-01-08 09:06:04 -05:00
|
|
|
self.state = LoadState::LoadingRoot;
|
|
|
|
Ok(())
|
|
|
|
}
|
2019-03-26 11:56:34 -04:00
|
|
|
|
2020-01-08 09:06:04 -05:00
|
|
|
pub fn add_import(
|
|
|
|
&mut self,
|
|
|
|
specifier: ModuleSpecifier,
|
|
|
|
referrer: ModuleSpecifier,
|
|
|
|
) {
|
|
|
|
if !self.is_pending.contains(&specifier) {
|
2020-01-25 12:53:16 -05:00
|
|
|
let fut =
|
|
|
|
self
|
|
|
|
.loader
|
|
|
|
.load(&specifier, Some(referrer), self.is_dynamic_import());
|
2020-01-08 09:06:04 -05:00
|
|
|
self.pending.push(fut.boxed());
|
|
|
|
self.is_pending.insert(specifier);
|
2019-03-26 11:56:34 -04:00
|
|
|
}
|
2019-08-07 12:55:39 -04:00
|
|
|
}
|
|
|
|
}
|
2019-04-16 15:13:42 -04:00
|
|
|
|
2020-01-08 09:06:04 -05:00
|
|
|
impl Stream for RecursiveModuleLoad {
|
|
|
|
type Item = Result<SourceCodeInfo, ErrBox>;
|
2019-03-26 11:56:34 -04:00
|
|
|
|
2019-11-16 19:17:47 -05:00
|
|
|
fn poll_next(
|
|
|
|
self: Pin<&mut Self>,
|
|
|
|
cx: &mut Context,
|
|
|
|
) -> Poll<Option<Self::Item>> {
|
|
|
|
let inner = self.get_mut();
|
|
|
|
match inner.state {
|
2020-01-08 09:06:04 -05:00
|
|
|
LoadState::ResolveMain(..) | LoadState::ResolveImport(..) => {
|
2019-11-16 19:17:47 -05:00
|
|
|
if let Err(e) = inner.add_root() {
|
|
|
|
return Poll::Ready(Some(Err(e)));
|
|
|
|
}
|
|
|
|
inner.try_poll_next_unpin(cx)
|
2019-08-07 12:55:39 -04:00
|
|
|
}
|
2020-01-08 09:06:04 -05:00
|
|
|
LoadState::LoadingRoot | LoadState::LoadingImports => {
|
2019-11-16 19:17:47 -05:00
|
|
|
match inner.pending.try_poll_next_unpin(cx)? {
|
|
|
|
Poll::Ready(None) => unreachable!(),
|
2020-01-08 09:06:04 -05:00
|
|
|
Poll::Ready(Some(info)) => Poll::Ready(Some(Ok(info))),
|
2019-11-16 19:17:47 -05:00
|
|
|
Poll::Pending => Poll::Pending,
|
2019-08-07 12:55:39 -04:00
|
|
|
}
|
|
|
|
}
|
2020-01-08 09:06:04 -05:00
|
|
|
LoadState::Done => Poll::Ready(None),
|
2019-11-16 19:17:47 -05:00
|
|
|
}
|
2019-03-26 11:56:34 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-08 09:06:04 -05:00
|
|
|
pub struct ModuleInfo {
|
|
|
|
pub main: bool,
|
|
|
|
pub name: String,
|
|
|
|
pub handle: v8::Global<v8::Module>,
|
2020-01-25 12:53:16 -05:00
|
|
|
pub import_specifiers: Vec<ModuleSpecifier>,
|
2019-03-26 11:56:34 -04:00
|
|
|
}
|
|
|
|
|
2019-04-18 21:33:50 -04:00
|
|
|
/// A symbolic module entity.
|
2019-05-09 16:44:30 -04:00
|
|
|
enum SymbolicModule {
|
2019-04-18 21:33:50 -04:00
|
|
|
/// This module is an alias to another module.
|
|
|
|
/// This is useful such that multiple names could point to
|
|
|
|
/// the same underlying module (particularly due to redirects).
|
|
|
|
Alias(String),
|
|
|
|
/// This module associates with a V8 module by id.
|
2020-01-06 14:07:35 -05:00
|
|
|
Mod(ModuleId),
|
2019-04-18 21:33:50 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Default)]
|
|
|
|
/// Alias-able module name map
|
2019-05-09 16:44:30 -04:00
|
|
|
struct ModuleNameMap {
|
2019-04-18 21:33:50 -04:00
|
|
|
inner: HashMap<String, SymbolicModule>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ModuleNameMap {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
ModuleNameMap {
|
|
|
|
inner: HashMap::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get the id of a module.
|
|
|
|
/// If this module is internally represented as an alias,
|
|
|
|
/// follow the alias chain to get the final module id.
|
2020-01-06 14:07:35 -05:00
|
|
|
pub fn get(&self, name: &str) -> Option<ModuleId> {
|
2019-04-18 21:33:50 -04:00
|
|
|
let mut mod_name = name;
|
|
|
|
loop {
|
|
|
|
let cond = self.inner.get(mod_name);
|
|
|
|
match cond {
|
|
|
|
Some(SymbolicModule::Alias(target)) => {
|
|
|
|
mod_name = target;
|
|
|
|
}
|
|
|
|
Some(SymbolicModule::Mod(mod_id)) => {
|
|
|
|
return Some(*mod_id);
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Insert a name assocated module id.
|
2020-01-06 14:07:35 -05:00
|
|
|
pub fn insert(&mut self, name: String, id: ModuleId) {
|
2019-04-18 21:33:50 -04:00
|
|
|
self.inner.insert(name, SymbolicModule::Mod(id));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Create an alias to another module.
|
|
|
|
pub fn alias(&mut self, name: String, target: String) {
|
|
|
|
self.inner.insert(name, SymbolicModule::Alias(target));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Check if a name is an alias to another module.
|
|
|
|
pub fn is_alias(&self, name: &str) -> bool {
|
|
|
|
let cond = self.inner.get(name);
|
|
|
|
match cond {
|
|
|
|
Some(SymbolicModule::Alias(_)) => true,
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-26 11:56:34 -04:00
|
|
|
/// A collection of JS modules.
|
|
|
|
#[derive(Default)]
|
|
|
|
pub struct Modules {
|
2020-01-08 09:06:04 -05:00
|
|
|
pub(crate) info: HashMap<ModuleId, ModuleInfo>,
|
2019-04-18 21:33:50 -04:00
|
|
|
by_name: ModuleNameMap,
|
2019-03-26 11:56:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Modules {
|
|
|
|
pub fn new() -> Modules {
|
|
|
|
Self {
|
|
|
|
info: HashMap::new(),
|
2019-04-18 21:33:50 -04:00
|
|
|
by_name: ModuleNameMap::new(),
|
2019-03-26 11:56:34 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-06 14:07:35 -05:00
|
|
|
pub fn get_id(&self, name: &str) -> Option<ModuleId> {
|
2019-04-18 21:33:50 -04:00
|
|
|
self.by_name.get(name)
|
2019-03-26 11:56:34 -04:00
|
|
|
}
|
|
|
|
|
2020-01-25 12:53:16 -05:00
|
|
|
pub fn get_children(&self, id: ModuleId) -> Option<&Vec<ModuleSpecifier>> {
|
2020-01-08 09:06:04 -05:00
|
|
|
self.info.get(&id).map(|i| &i.import_specifiers)
|
2019-03-26 11:56:34 -04:00
|
|
|
}
|
|
|
|
|
2020-01-06 14:07:35 -05:00
|
|
|
pub fn get_name(&self, id: ModuleId) -> Option<&String> {
|
2019-04-16 15:13:42 -04:00
|
|
|
self.info.get(&id).map(|i| &i.name)
|
2019-03-26 11:56:34 -04:00
|
|
|
}
|
|
|
|
|
2020-01-25 12:53:16 -05:00
|
|
|
pub fn is_registered(&self, specifier: &ModuleSpecifier) -> bool {
|
|
|
|
self.by_name.get(&specifier.to_string()).is_some()
|
2019-03-26 11:56:34 -04:00
|
|
|
}
|
|
|
|
|
2020-01-08 09:06:04 -05:00
|
|
|
pub fn register(
|
|
|
|
&mut self,
|
|
|
|
id: ModuleId,
|
|
|
|
name: &str,
|
|
|
|
main: bool,
|
|
|
|
handle: v8::Global<v8::Module>,
|
2020-01-25 12:53:16 -05:00
|
|
|
import_specifiers: Vec<ModuleSpecifier>,
|
2020-01-08 09:06:04 -05:00
|
|
|
) {
|
2019-03-26 11:56:34 -04:00
|
|
|
let name = String::from(name);
|
|
|
|
debug!("register_complete {}", name);
|
|
|
|
|
2019-05-02 20:37:02 -04:00
|
|
|
self.by_name.insert(name.clone(), id);
|
2019-03-26 11:56:34 -04:00
|
|
|
self.info.insert(
|
|
|
|
id,
|
|
|
|
ModuleInfo {
|
2020-01-08 09:06:04 -05:00
|
|
|
main,
|
2019-03-26 11:56:34 -04:00
|
|
|
name,
|
2020-01-08 09:06:04 -05:00
|
|
|
import_specifiers,
|
|
|
|
handle,
|
2019-03-26 11:56:34 -04:00
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
2019-04-16 15:13:42 -04:00
|
|
|
|
2019-04-18 21:33:50 -04:00
|
|
|
pub fn alias(&mut self, name: &str, target: &str) {
|
|
|
|
self.by_name.alias(name.to_owned(), target.to_owned());
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_alias(&self, name: &str) -> bool {
|
|
|
|
self.by_name.is_alias(name)
|
|
|
|
}
|
|
|
|
|
2020-01-08 09:06:04 -05:00
|
|
|
pub fn get_info(&self, id: ModuleId) -> Option<&ModuleInfo> {
|
|
|
|
if id == 0 {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
self.info.get(&id)
|
|
|
|
}
|
|
|
|
|
2020-01-25 12:53:16 -05:00
|
|
|
pub fn deps(&self, module_specifier: &ModuleSpecifier) -> Option<Deps> {
|
|
|
|
Deps::new(self, module_specifier)
|
2019-04-16 15:13:42 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-19 11:18:46 -04:00
|
|
|
/// This is a tree structure representing the dependencies of a given module.
|
|
|
|
/// Use Modules::deps to construct it. The 'deps' member is None if this module
|
2020-01-02 07:48:46 -05:00
|
|
|
/// was already seen elsewhere in the tree.
|
2019-04-19 11:18:46 -04:00
|
|
|
#[derive(Debug, PartialEq)]
|
2019-04-16 15:13:42 -04:00
|
|
|
pub struct Deps {
|
|
|
|
pub name: String,
|
|
|
|
pub deps: Option<Vec<Deps>>,
|
|
|
|
prefix: String,
|
|
|
|
is_last: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Deps {
|
2020-01-25 12:53:16 -05:00
|
|
|
fn new(
|
|
|
|
modules: &Modules,
|
|
|
|
module_specifier: &ModuleSpecifier,
|
|
|
|
) -> Option<Deps> {
|
2019-04-16 15:13:42 -04:00
|
|
|
let mut seen = HashSet::new();
|
2020-01-25 12:53:16 -05:00
|
|
|
Self::helper(&mut seen, "".to_string(), true, modules, module_specifier)
|
2019-04-16 15:13:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
fn helper(
|
|
|
|
seen: &mut HashSet<String>,
|
|
|
|
prefix: String,
|
|
|
|
is_last: bool,
|
|
|
|
modules: &Modules,
|
2020-01-25 12:53:16 -05:00
|
|
|
module_specifier: &ModuleSpecifier,
|
2019-04-19 11:18:46 -04:00
|
|
|
) -> Option<Deps> {
|
2020-01-25 12:53:16 -05:00
|
|
|
let name = module_specifier.to_string();
|
|
|
|
if seen.contains(&name) {
|
2019-04-19 11:18:46 -04:00
|
|
|
Some(Deps {
|
2020-01-25 12:53:16 -05:00
|
|
|
name,
|
2019-04-16 15:13:42 -04:00
|
|
|
prefix,
|
|
|
|
deps: None,
|
|
|
|
is_last,
|
2019-04-19 11:18:46 -04:00
|
|
|
})
|
2019-04-16 15:13:42 -04:00
|
|
|
} else {
|
2020-01-25 12:53:16 -05:00
|
|
|
let mod_id = modules.get_id(&name)?;
|
|
|
|
let children = modules.get_children(mod_id).unwrap();
|
2019-04-16 15:13:42 -04:00
|
|
|
seen.insert(name.to_string());
|
2019-04-19 11:18:46 -04:00
|
|
|
let child_count = children.len();
|
|
|
|
let deps: Vec<Deps> = children
|
2019-04-16 15:13:42 -04:00
|
|
|
.iter()
|
|
|
|
.enumerate()
|
2020-01-25 12:53:16 -05:00
|
|
|
.map(|(index, dep_specifier)| {
|
2019-04-16 15:13:42 -04:00
|
|
|
let new_is_last = index == child_count - 1;
|
|
|
|
let mut new_prefix = prefix.clone();
|
|
|
|
new_prefix.push(if is_last { ' ' } else { '│' });
|
|
|
|
new_prefix.push(' ');
|
|
|
|
|
2020-01-25 12:53:16 -05:00
|
|
|
Self::helper(seen, new_prefix, new_is_last, modules, dep_specifier)
|
2019-04-19 11:18:46 -04:00
|
|
|
})
|
|
|
|
// If any of the children are missing, return None.
|
|
|
|
.collect::<Option<_>>()?;
|
|
|
|
|
|
|
|
Some(Deps {
|
2020-01-25 12:53:16 -05:00
|
|
|
name,
|
2019-04-16 15:13:42 -04:00
|
|
|
prefix,
|
|
|
|
deps: Some(deps),
|
|
|
|
is_last,
|
2019-04-19 11:18:46 -04:00
|
|
|
})
|
2019-04-16 15:13:42 -04:00
|
|
|
}
|
|
|
|
}
|
2019-04-27 13:04:09 -04:00
|
|
|
|
|
|
|
pub fn to_json(&self) -> String {
|
|
|
|
let mut children = "[".to_string();
|
|
|
|
|
|
|
|
if let Some(ref deps) = self.deps {
|
|
|
|
for d in deps {
|
|
|
|
children.push_str(&d.to_json());
|
|
|
|
if !d.is_last {
|
|
|
|
children.push_str(",");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
children.push_str("]");
|
|
|
|
|
|
|
|
format!("[\"{}\",{}]", self.name, children)
|
|
|
|
}
|
2019-04-16 15:13:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for Deps {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
let mut has_children = false;
|
|
|
|
if let Some(ref deps) = self.deps {
|
|
|
|
has_children = !deps.is_empty();
|
|
|
|
}
|
|
|
|
write!(
|
|
|
|
f,
|
|
|
|
"{}{}─{} {}",
|
|
|
|
self.prefix,
|
|
|
|
if self.is_last { "└" } else { "├" },
|
|
|
|
if has_children { "┬" } else { "─" },
|
|
|
|
self.name
|
|
|
|
)?;
|
|
|
|
|
|
|
|
if let Some(ref deps) = self.deps {
|
|
|
|
for d in deps {
|
|
|
|
write!(f, "\n{}", d)?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
2019-03-26 11:56:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2020-01-08 09:06:04 -05:00
|
|
|
use crate::es_isolate::EsIsolate;
|
2019-03-26 11:56:34 -04:00
|
|
|
use crate::isolate::js_check;
|
2019-11-16 19:17:47 -05:00
|
|
|
use futures::future::FutureExt;
|
2019-07-10 18:53:48 -04:00
|
|
|
use std::error::Error;
|
2019-04-16 15:13:42 -04:00
|
|
|
use std::fmt;
|
2019-11-16 19:17:47 -05:00
|
|
|
use std::future::Future;
|
2020-01-08 09:06:04 -05:00
|
|
|
use std::sync::Mutex;
|
2019-03-26 11:56:34 -04:00
|
|
|
|
|
|
|
struct MockLoader {
|
2019-06-05 16:35:38 -04:00
|
|
|
pub loads: Arc<Mutex<Vec<String>>>,
|
2019-03-26 11:56:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl MockLoader {
|
|
|
|
fn new() -> Self {
|
|
|
|
Self {
|
2019-06-05 16:35:38 -04:00
|
|
|
loads: Arc::new(Mutex::new(Vec::new())),
|
2019-03-26 11:56:34 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-18 21:33:50 -04:00
|
|
|
fn mock_source_code(url: &str) -> Option<(&'static str, &'static str)> {
|
|
|
|
// (code, real_module_name)
|
2019-06-12 19:55:59 -04:00
|
|
|
let spec: Vec<&str> = url.split("file://").collect();
|
|
|
|
match spec[1] {
|
|
|
|
"/a.js" => Some((A_SRC, "file:///a.js")),
|
|
|
|
"/b.js" => Some((B_SRC, "file:///b.js")),
|
|
|
|
"/c.js" => Some((C_SRC, "file:///c.js")),
|
|
|
|
"/d.js" => Some((D_SRC, "file:///d.js")),
|
|
|
|
"/circular1.js" => Some((CIRCULAR1_SRC, "file:///circular1.js")),
|
|
|
|
"/circular2.js" => Some((CIRCULAR2_SRC, "file:///circular2.js")),
|
|
|
|
"/circular3.js" => Some((CIRCULAR3_SRC, "file:///circular3.js")),
|
|
|
|
"/redirect1.js" => Some((REDIRECT1_SRC, "file:///redirect1.js")),
|
|
|
|
// pretend redirect - real module name is different than one requested
|
|
|
|
"/redirect2.js" => Some((REDIRECT2_SRC, "file:///dir/redirect2.js")),
|
|
|
|
"/dir/redirect3.js" => Some((REDIRECT3_SRC, "file:///redirect3.js")),
|
|
|
|
"/slow.js" => Some((SLOW_SRC, "file:///slow.js")),
|
|
|
|
"/never_ready.js" => {
|
2019-08-07 12:55:39 -04:00
|
|
|
Some(("should never be Ready", "file:///never_ready.js"))
|
2019-06-12 19:55:59 -04:00
|
|
|
}
|
|
|
|
"/main.js" => Some((MAIN_SRC, "file:///main.js")),
|
|
|
|
"/bad_import.js" => Some((BAD_IMPORT_SRC, "file:///bad_import.js")),
|
2019-10-19 17:19:19 -04:00
|
|
|
// deliberately empty code.
|
|
|
|
"/main_with_code.js" => Some(("", "file:///main_with_code.js")),
|
2019-04-16 15:13:42 -04:00
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, PartialEq)]
|
|
|
|
enum MockError {
|
|
|
|
ResolveErr,
|
|
|
|
LoadErr,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for MockError {
|
|
|
|
fn fmt(&self, _f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
unimplemented!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Error for MockError {
|
2019-06-18 09:28:56 -04:00
|
|
|
fn cause(&self) -> Option<&dyn Error> {
|
2019-04-16 15:13:42 -04:00
|
|
|
unimplemented!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct DelayedSourceCodeFuture {
|
|
|
|
url: String,
|
|
|
|
counter: u32,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Future for DelayedSourceCodeFuture {
|
2019-11-16 19:17:47 -05:00
|
|
|
type Output = Result<SourceCodeInfo, ErrBox>;
|
2019-04-16 15:13:42 -04:00
|
|
|
|
2019-11-16 19:17:47 -05:00
|
|
|
fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
|
|
|
|
let inner = self.get_mut();
|
|
|
|
inner.counter += 1;
|
|
|
|
if inner.url == "file:///never_ready.js" {
|
|
|
|
return Poll::Pending;
|
2019-08-07 12:55:39 -04:00
|
|
|
}
|
2019-11-16 19:17:47 -05:00
|
|
|
if inner.url == "file:///slow.js" && inner.counter < 2 {
|
2019-08-07 12:55:39 -04:00
|
|
|
// TODO(ry) Hopefully in the future we can remove current task
|
|
|
|
// notification. See comment above run_in_task.
|
2019-11-16 19:17:47 -05:00
|
|
|
cx.waker().wake_by_ref();
|
|
|
|
return Poll::Pending;
|
2019-04-16 15:13:42 -04:00
|
|
|
}
|
2019-11-16 19:17:47 -05:00
|
|
|
match mock_source_code(&inner.url) {
|
|
|
|
Some(src) => Poll::Ready(Ok(SourceCodeInfo {
|
2019-04-18 21:33:50 -04:00
|
|
|
code: src.0.to_owned(),
|
2019-11-16 19:17:47 -05:00
|
|
|
module_url_specified: inner.url.clone(),
|
2019-08-07 12:55:39 -04:00
|
|
|
module_url_found: src.1.to_owned(),
|
2019-04-18 21:33:50 -04:00
|
|
|
})),
|
2019-11-16 19:17:47 -05:00
|
|
|
None => Poll::Ready(Err(MockError::LoadErr.into())),
|
2019-04-16 15:13:42 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-01 20:51:44 -04:00
|
|
|
impl Loader for MockLoader {
|
2019-06-09 09:08:20 -04:00
|
|
|
fn resolve(
|
|
|
|
&self,
|
|
|
|
specifier: &str,
|
|
|
|
referrer: &str,
|
|
|
|
_is_root: bool,
|
2019-07-10 18:53:48 -04:00
|
|
|
) -> Result<ModuleSpecifier, ErrBox> {
|
2019-06-12 19:55:59 -04:00
|
|
|
let referrer = if referrer == "." {
|
|
|
|
"file:///"
|
|
|
|
} else {
|
|
|
|
referrer
|
|
|
|
};
|
|
|
|
|
2019-04-18 21:33:50 -04:00
|
|
|
eprintln!(">> RESOLVING, S: {}, R: {}", specifier, referrer);
|
|
|
|
|
core: clearly define when module lookup is path-based vs URL-based
The rules are now as follows:
* In `import` statements, as mandated by the WHATWG specification,
the import specifier is always treated as a URL.
If it is a relative URL, it must start with either / or ./ or ../
* A script name passed to deno as a command line argument may be either
an absolute URL or a local path.
- If the name starts with a valid URI scheme followed by a colon, e.g.
'http:', 'https:', 'file:', 'foo+bar:', it always interpreted as a
URL (even if Deno doesn't support the indicated protocol).
- Otherwise, the script name is interpreted as a local path. The local
path may be relative, and operating system semantics determine how
it is resolved. Prefixing a relative path with ./ is not required.
2019-07-08 03:55:24 -04:00
|
|
|
let output_specifier =
|
|
|
|
match ModuleSpecifier::resolve_import(specifier, referrer) {
|
|
|
|
Ok(specifier) => specifier,
|
2019-07-10 18:53:48 -04:00
|
|
|
Err(..) => return Err(MockError::ResolveErr.into()),
|
core: clearly define when module lookup is path-based vs URL-based
The rules are now as follows:
* In `import` statements, as mandated by the WHATWG specification,
the import specifier is always treated as a URL.
If it is a relative URL, it must start with either / or ./ or ../
* A script name passed to deno as a command line argument may be either
an absolute URL or a local path.
- If the name starts with a valid URI scheme followed by a colon, e.g.
'http:', 'https:', 'file:', 'foo+bar:', it always interpreted as a
URL (even if Deno doesn't support the indicated protocol).
- Otherwise, the script name is interpreted as a local path. The local
path may be relative, and operating system semantics determine how
it is resolved. Prefixing a relative path with ./ is not required.
2019-07-08 03:55:24 -04:00
|
|
|
};
|
2019-06-12 19:55:59 -04:00
|
|
|
|
|
|
|
if mock_source_code(&output_specifier.to_string()).is_some() {
|
2019-04-18 21:33:50 -04:00
|
|
|
Ok(output_specifier)
|
2019-04-16 15:13:42 -04:00
|
|
|
} else {
|
2019-07-10 18:53:48 -04:00
|
|
|
Err(MockError::ResolveErr.into())
|
2019-04-16 15:13:42 -04:00
|
|
|
}
|
2019-03-26 11:56:34 -04:00
|
|
|
}
|
|
|
|
|
2019-06-12 19:55:59 -04:00
|
|
|
fn load(
|
|
|
|
&self,
|
|
|
|
module_specifier: &ModuleSpecifier,
|
2019-11-25 09:33:23 -05:00
|
|
|
_maybe_referrer: Option<ModuleSpecifier>,
|
2020-01-25 12:53:16 -05:00
|
|
|
_is_dyn_import: bool,
|
2019-11-16 19:17:47 -05:00
|
|
|
) -> Pin<Box<SourceCodeInfoFuture>> {
|
2019-06-05 16:35:38 -04:00
|
|
|
let mut loads = self.loads.lock().unwrap();
|
2019-06-12 19:55:59 -04:00
|
|
|
loads.push(module_specifier.to_string());
|
|
|
|
let url = module_specifier.to_string();
|
2019-11-16 19:17:47 -05:00
|
|
|
DelayedSourceCodeFuture { url, counter: 0 }.boxed()
|
2019-03-26 11:56:34 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const A_SRC: &str = r#"
|
2019-06-12 19:55:59 -04:00
|
|
|
import { b } from "/b.js";
|
|
|
|
import { c } from "/c.js";
|
2019-03-26 11:56:34 -04:00
|
|
|
if (b() != 'b') throw Error();
|
|
|
|
if (c() != 'c') throw Error();
|
|
|
|
if (!import.meta.main) throw Error();
|
2019-06-12 19:55:59 -04:00
|
|
|
if (import.meta.url != 'file:///a.js') throw Error();
|
2019-03-26 11:56:34 -04:00
|
|
|
"#;
|
|
|
|
|
|
|
|
const B_SRC: &str = r#"
|
2019-06-12 19:55:59 -04:00
|
|
|
import { c } from "/c.js";
|
2019-03-26 11:56:34 -04:00
|
|
|
if (c() != 'c') throw Error();
|
|
|
|
export function b() { return 'b'; }
|
|
|
|
if (import.meta.main) throw Error();
|
2019-06-12 19:55:59 -04:00
|
|
|
if (import.meta.url != 'file:///b.js') throw Error();
|
2019-03-26 11:56:34 -04:00
|
|
|
"#;
|
|
|
|
|
|
|
|
const C_SRC: &str = r#"
|
2019-06-12 19:55:59 -04:00
|
|
|
import { d } from "/d.js";
|
2019-03-26 11:56:34 -04:00
|
|
|
export function c() { return 'c'; }
|
|
|
|
if (d() != 'd') throw Error();
|
|
|
|
if (import.meta.main) throw Error();
|
2019-06-12 19:55:59 -04:00
|
|
|
if (import.meta.url != 'file:///c.js') throw Error();
|
2019-03-26 11:56:34 -04:00
|
|
|
"#;
|
|
|
|
|
|
|
|
const D_SRC: &str = r#"
|
|
|
|
export function d() { return 'd'; }
|
|
|
|
if (import.meta.main) throw Error();
|
2019-06-12 19:55:59 -04:00
|
|
|
if (import.meta.url != 'file:///d.js') throw Error();
|
2019-03-26 11:56:34 -04:00
|
|
|
"#;
|
|
|
|
|
2019-08-07 12:55:39 -04:00
|
|
|
// TODO(ry) Sadly FuturesUnordered requires the current task to be set. So
|
|
|
|
// even though we are only using poll() in these tests and not Tokio, we must
|
|
|
|
// nevertheless run it in the tokio executor. Ideally run_in_task can be
|
|
|
|
// removed in the future.
|
|
|
|
use crate::isolate::tests::run_in_task;
|
2020-01-08 09:06:04 -05:00
|
|
|
use crate::isolate::StartupData;
|
2019-08-07 12:55:39 -04:00
|
|
|
|
2019-03-26 11:56:34 -04:00
|
|
|
#[test]
|
|
|
|
fn test_recursive_load() {
|
2020-01-08 09:06:04 -05:00
|
|
|
let loader = MockLoader::new();
|
|
|
|
let loads = loader.loads.clone();
|
|
|
|
let mut isolate =
|
|
|
|
EsIsolate::new(Box::new(loader), StartupData::None, false);
|
|
|
|
let a_id_fut = isolate.load_module("/a.js", None);
|
|
|
|
let a_id = futures::executor::block_on(a_id_fut).expect("Failed to load");
|
|
|
|
|
|
|
|
js_check(isolate.mod_evaluate(a_id));
|
|
|
|
let l = loads.lock().unwrap();
|
|
|
|
assert_eq!(
|
|
|
|
l.to_vec(),
|
|
|
|
vec![
|
|
|
|
"file:///a.js",
|
|
|
|
"file:///b.js",
|
|
|
|
"file:///c.js",
|
|
|
|
"file:///d.js"
|
|
|
|
]
|
|
|
|
);
|
2019-03-26 11:56:34 -04:00
|
|
|
|
2020-01-08 09:06:04 -05:00
|
|
|
let modules = &isolate.modules;
|
|
|
|
assert_eq!(modules.get_id("file:///a.js"), Some(a_id));
|
|
|
|
let b_id = modules.get_id("file:///b.js").unwrap();
|
|
|
|
let c_id = modules.get_id("file:///c.js").unwrap();
|
|
|
|
let d_id = modules.get_id("file:///d.js").unwrap();
|
|
|
|
assert_eq!(
|
|
|
|
modules.get_children(a_id),
|
2020-01-25 12:53:16 -05:00
|
|
|
Some(&vec![
|
|
|
|
ModuleSpecifier::resolve_url("file:///b.js").unwrap(),
|
|
|
|
ModuleSpecifier::resolve_url("file:///c.js").unwrap()
|
|
|
|
])
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
modules.get_children(b_id),
|
|
|
|
Some(&vec![ModuleSpecifier::resolve_url("file:///c.js").unwrap()])
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
modules.get_children(c_id),
|
|
|
|
Some(&vec![ModuleSpecifier::resolve_url("file:///d.js").unwrap()])
|
2020-01-08 09:06:04 -05:00
|
|
|
);
|
|
|
|
assert_eq!(modules.get_children(d_id), Some(&vec![]));
|
2019-03-26 11:56:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
const CIRCULAR1_SRC: &str = r#"
|
2019-06-12 19:55:59 -04:00
|
|
|
import "/circular2.js";
|
2019-03-26 11:56:34 -04:00
|
|
|
Deno.core.print("circular1");
|
|
|
|
"#;
|
|
|
|
|
|
|
|
const CIRCULAR2_SRC: &str = r#"
|
2019-06-12 19:55:59 -04:00
|
|
|
import "/circular3.js";
|
2019-03-26 11:56:34 -04:00
|
|
|
Deno.core.print("circular2");
|
|
|
|
"#;
|
|
|
|
|
2019-04-16 15:13:42 -04:00
|
|
|
const CIRCULAR3_SRC: &str = r#"
|
2019-06-12 19:55:59 -04:00
|
|
|
import "/circular1.js";
|
|
|
|
import "/circular2.js";
|
2019-04-16 15:13:42 -04:00
|
|
|
Deno.core.print("circular3");
|
|
|
|
"#;
|
|
|
|
|
2019-03-26 11:56:34 -04:00
|
|
|
#[test]
|
|
|
|
fn test_circular_load() {
|
2020-01-08 09:06:04 -05:00
|
|
|
let loader = MockLoader::new();
|
|
|
|
let loads = loader.loads.clone();
|
|
|
|
let mut isolate =
|
|
|
|
EsIsolate::new(Box::new(loader), StartupData::None, false);
|
2019-08-07 12:55:39 -04:00
|
|
|
|
2020-01-08 09:06:04 -05:00
|
|
|
let fut = async move {
|
|
|
|
let result = isolate.load_module("/circular1.js", None).await;
|
|
|
|
assert!(result.is_ok());
|
|
|
|
let circular1_id = result.unwrap();
|
|
|
|
js_check(isolate.mod_evaluate(circular1_id));
|
2019-08-07 12:55:39 -04:00
|
|
|
|
2020-01-08 09:06:04 -05:00
|
|
|
let l = loads.lock().unwrap();
|
|
|
|
assert_eq!(
|
|
|
|
l.to_vec(),
|
|
|
|
vec![
|
|
|
|
"file:///circular1.js",
|
|
|
|
"file:///circular2.js",
|
|
|
|
"file:///circular3.js"
|
|
|
|
]
|
|
|
|
);
|
2019-08-07 12:55:39 -04:00
|
|
|
|
2020-01-08 09:06:04 -05:00
|
|
|
let modules = &isolate.modules;
|
2019-08-07 12:55:39 -04:00
|
|
|
|
2020-01-08 09:06:04 -05:00
|
|
|
assert_eq!(modules.get_id("file:///circular1.js"), Some(circular1_id));
|
|
|
|
let circular2_id = modules.get_id("file:///circular2.js").unwrap();
|
2019-08-07 12:55:39 -04:00
|
|
|
|
2020-01-08 09:06:04 -05:00
|
|
|
assert_eq!(
|
|
|
|
modules.get_children(circular1_id),
|
2020-01-25 12:53:16 -05:00
|
|
|
Some(&vec![
|
|
|
|
ModuleSpecifier::resolve_url("file:///circular2.js").unwrap()
|
|
|
|
])
|
2020-01-08 09:06:04 -05:00
|
|
|
);
|
2019-08-07 12:55:39 -04:00
|
|
|
|
2020-01-08 09:06:04 -05:00
|
|
|
assert_eq!(
|
|
|
|
modules.get_children(circular2_id),
|
2020-01-25 12:53:16 -05:00
|
|
|
Some(&vec![
|
|
|
|
ModuleSpecifier::resolve_url("file:///circular3.js").unwrap()
|
|
|
|
])
|
2020-01-08 09:06:04 -05:00
|
|
|
);
|
|
|
|
|
|
|
|
assert!(modules.get_id("file:///circular3.js").is_some());
|
|
|
|
let circular3_id = modules.get_id("file:///circular3.js").unwrap();
|
|
|
|
assert_eq!(
|
|
|
|
modules.get_children(circular3_id),
|
|
|
|
Some(&vec![
|
2020-01-25 12:53:16 -05:00
|
|
|
ModuleSpecifier::resolve_url("file:///circular1.js").unwrap(),
|
|
|
|
ModuleSpecifier::resolve_url("file:///circular2.js").unwrap()
|
2020-01-08 09:06:04 -05:00
|
|
|
])
|
|
|
|
);
|
|
|
|
}
|
|
|
|
.boxed();
|
|
|
|
|
|
|
|
futures::executor::block_on(fut);
|
2019-04-18 21:33:50 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
const REDIRECT1_SRC: &str = r#"
|
|
|
|
import "./redirect2.js";
|
|
|
|
Deno.core.print("redirect1");
|
|
|
|
"#;
|
|
|
|
|
|
|
|
const REDIRECT2_SRC: &str = r#"
|
|
|
|
import "./redirect3.js";
|
|
|
|
Deno.core.print("redirect2");
|
|
|
|
"#;
|
|
|
|
|
|
|
|
const REDIRECT3_SRC: &str = r#"
|
|
|
|
Deno.core.print("redirect3");
|
|
|
|
"#;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_redirect_load() {
|
2020-01-08 09:06:04 -05:00
|
|
|
let loader = MockLoader::new();
|
|
|
|
let loads = loader.loads.clone();
|
|
|
|
let mut isolate =
|
|
|
|
EsIsolate::new(Box::new(loader), StartupData::None, false);
|
|
|
|
|
|
|
|
let fut = async move {
|
|
|
|
let result = isolate.load_module("/redirect1.js", None).await;
|
2019-08-07 12:55:39 -04:00
|
|
|
println!(">> result {:?}", result);
|
2020-01-08 09:06:04 -05:00
|
|
|
assert!(result.is_ok());
|
|
|
|
let redirect1_id = result.unwrap();
|
|
|
|
js_check(isolate.mod_evaluate(redirect1_id));
|
|
|
|
let l = loads.lock().unwrap();
|
|
|
|
assert_eq!(
|
|
|
|
l.to_vec(),
|
|
|
|
vec![
|
|
|
|
"file:///redirect1.js",
|
|
|
|
"file:///redirect2.js",
|
|
|
|
"file:///dir/redirect3.js"
|
|
|
|
]
|
|
|
|
);
|
2019-08-07 12:55:39 -04:00
|
|
|
|
2020-01-08 09:06:04 -05:00
|
|
|
let modules = &isolate.modules;
|
2019-08-07 12:55:39 -04:00
|
|
|
|
2020-01-08 09:06:04 -05:00
|
|
|
assert_eq!(modules.get_id("file:///redirect1.js"), Some(redirect1_id));
|
2019-08-07 12:55:39 -04:00
|
|
|
|
2020-01-08 09:06:04 -05:00
|
|
|
let redirect2_id = modules.get_id("file:///dir/redirect2.js").unwrap();
|
|
|
|
assert!(modules.is_alias("file:///redirect2.js"));
|
|
|
|
assert!(!modules.is_alias("file:///dir/redirect2.js"));
|
|
|
|
assert_eq!(modules.get_id("file:///redirect2.js"), Some(redirect2_id));
|
2019-08-07 12:55:39 -04:00
|
|
|
|
2020-01-08 09:06:04 -05:00
|
|
|
let redirect3_id = modules.get_id("file:///redirect3.js").unwrap();
|
|
|
|
assert!(modules.is_alias("file:///dir/redirect3.js"));
|
|
|
|
assert!(!modules.is_alias("file:///redirect3.js"));
|
|
|
|
assert_eq!(
|
|
|
|
modules.get_id("file:///dir/redirect3.js"),
|
|
|
|
Some(redirect3_id)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
.boxed();
|
|
|
|
|
|
|
|
futures::executor::block_on(fut);
|
2019-03-26 11:56:34 -04:00
|
|
|
}
|
2019-04-16 15:13:42 -04:00
|
|
|
|
|
|
|
// main.js
|
|
|
|
const MAIN_SRC: &str = r#"
|
|
|
|
// never_ready.js never loads.
|
2019-06-12 19:55:59 -04:00
|
|
|
import "/never_ready.js";
|
2019-04-16 15:13:42 -04:00
|
|
|
// slow.js resolves after one tick.
|
2019-06-12 19:55:59 -04:00
|
|
|
import "/slow.js";
|
2019-04-16 15:13:42 -04:00
|
|
|
"#;
|
|
|
|
|
|
|
|
// slow.js
|
|
|
|
const SLOW_SRC: &str = r#"
|
|
|
|
// Circular import of never_ready.js
|
|
|
|
// Does this trigger two Loader calls? It shouldn't.
|
2019-06-12 19:55:59 -04:00
|
|
|
import "/never_ready.js";
|
|
|
|
import "/a.js";
|
2019-04-16 15:13:42 -04:00
|
|
|
"#;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn slow_never_ready_modules() {
|
2019-11-16 19:17:47 -05:00
|
|
|
run_in_task(|mut cx| {
|
2019-08-07 12:55:39 -04:00
|
|
|
let loader = MockLoader::new();
|
|
|
|
let loads = loader.loads.clone();
|
2020-01-08 09:06:04 -05:00
|
|
|
let mut isolate =
|
|
|
|
EsIsolate::new(Box::new(loader), StartupData::None, false);
|
|
|
|
let mut recursive_load = isolate.load_module("/main.js", None).boxed();
|
2019-04-16 15:13:42 -04:00
|
|
|
|
2019-11-16 19:17:47 -05:00
|
|
|
let result = recursive_load.poll_unpin(&mut cx);
|
|
|
|
assert!(result.is_pending());
|
2019-08-07 12:55:39 -04:00
|
|
|
|
|
|
|
// TODO(ry) Arguably the first time we poll only the following modules
|
|
|
|
// should be loaded:
|
|
|
|
// "file:///main.js",
|
|
|
|
// "file:///never_ready.js",
|
|
|
|
// "file:///slow.js"
|
|
|
|
// But due to current task notification in DelayedSourceCodeFuture they
|
|
|
|
// all get loaded in a single poll. Also see the comment above
|
|
|
|
// run_in_task.
|
|
|
|
|
|
|
|
for _ in 0..10 {
|
2019-11-16 19:17:47 -05:00
|
|
|
let result = recursive_load.poll_unpin(&mut cx);
|
|
|
|
assert!(result.is_pending());
|
2019-10-23 10:19:27 -04:00
|
|
|
let l = loads.lock().unwrap();
|
2019-08-07 12:55:39 -04:00
|
|
|
assert_eq!(
|
|
|
|
l.to_vec(),
|
|
|
|
vec![
|
|
|
|
"file:///main.js",
|
|
|
|
"file:///never_ready.js",
|
|
|
|
"file:///slow.js",
|
|
|
|
"file:///a.js",
|
|
|
|
"file:///b.js",
|
|
|
|
"file:///c.js",
|
|
|
|
"file:///d.js"
|
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
})
|
2019-04-16 15:13:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// bad_import.js
|
|
|
|
const BAD_IMPORT_SRC: &str = r#"
|
|
|
|
import "foo";
|
|
|
|
"#;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn loader_disappears_after_error() {
|
2019-11-16 19:17:47 -05:00
|
|
|
run_in_task(|mut cx| {
|
2019-08-07 12:55:39 -04:00
|
|
|
let loader = MockLoader::new();
|
2020-01-08 09:06:04 -05:00
|
|
|
let mut isolate =
|
|
|
|
EsIsolate::new(Box::new(loader), StartupData::None, false);
|
|
|
|
let mut load_fut = isolate.load_module("/bad_import.js", None).boxed();
|
2019-11-16 19:17:47 -05:00
|
|
|
let result = load_fut.poll_unpin(&mut cx);
|
|
|
|
if let Poll::Ready(Err(err)) = result {
|
|
|
|
assert_eq!(
|
|
|
|
err.downcast_ref::<MockError>().unwrap(),
|
|
|
|
&MockError::ResolveErr
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
unreachable!();
|
|
|
|
}
|
2019-08-07 12:55:39 -04:00
|
|
|
})
|
2019-04-16 15:13:42 -04:00
|
|
|
}
|
2019-04-19 11:18:46 -04:00
|
|
|
|
2019-10-19 17:19:19 -04:00
|
|
|
const MAIN_WITH_CODE_SRC: &str = r#"
|
|
|
|
import { b } from "/b.js";
|
|
|
|
import { c } from "/c.js";
|
|
|
|
if (b() != 'b') throw Error();
|
|
|
|
if (c() != 'c') throw Error();
|
|
|
|
if (!import.meta.main) throw Error();
|
|
|
|
if (import.meta.url != 'file:///main_with_code.js') throw Error();
|
|
|
|
"#;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn recursive_load_main_with_code() {
|
2020-01-08 09:06:04 -05:00
|
|
|
let loader = MockLoader::new();
|
|
|
|
let loads = loader.loads.clone();
|
|
|
|
let mut isolate =
|
|
|
|
EsIsolate::new(Box::new(loader), StartupData::None, false);
|
|
|
|
// In default resolution code should be empty.
|
|
|
|
// Instead we explicitly pass in our own code.
|
|
|
|
// The behavior should be very similar to /a.js.
|
|
|
|
let main_id_fut = isolate
|
|
|
|
.load_module("/main_with_code.js", Some(MAIN_WITH_CODE_SRC.to_owned()))
|
|
|
|
.boxed();
|
|
|
|
let main_id =
|
|
|
|
futures::executor::block_on(main_id_fut).expect("Failed to load");
|
|
|
|
|
|
|
|
js_check(isolate.mod_evaluate(main_id));
|
|
|
|
|
|
|
|
let l = loads.lock().unwrap();
|
|
|
|
assert_eq!(
|
|
|
|
l.to_vec(),
|
|
|
|
vec!["file:///b.js", "file:///c.js", "file:///d.js"]
|
|
|
|
);
|
2019-10-19 17:19:19 -04:00
|
|
|
|
2020-01-08 09:06:04 -05:00
|
|
|
let modules = &isolate.modules;
|
2019-10-19 17:19:19 -04:00
|
|
|
|
2020-01-08 09:06:04 -05:00
|
|
|
assert_eq!(modules.get_id("file:///main_with_code.js"), Some(main_id));
|
|
|
|
let b_id = modules.get_id("file:///b.js").unwrap();
|
|
|
|
let c_id = modules.get_id("file:///c.js").unwrap();
|
|
|
|
let d_id = modules.get_id("file:///d.js").unwrap();
|
2019-10-19 17:19:19 -04:00
|
|
|
|
2020-01-08 09:06:04 -05:00
|
|
|
assert_eq!(
|
|
|
|
modules.get_children(main_id),
|
2020-01-25 12:53:16 -05:00
|
|
|
Some(&vec![
|
|
|
|
ModuleSpecifier::resolve_url("file:///b.js").unwrap(),
|
|
|
|
ModuleSpecifier::resolve_url("file:///c.js").unwrap()
|
|
|
|
])
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
modules.get_children(b_id),
|
|
|
|
Some(&vec![ModuleSpecifier::resolve_url("file:///c.js").unwrap()])
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
modules.get_children(c_id),
|
|
|
|
Some(&vec![ModuleSpecifier::resolve_url("file:///d.js").unwrap()])
|
2020-01-08 09:06:04 -05:00
|
|
|
);
|
|
|
|
assert_eq!(modules.get_children(d_id), Some(&vec![]));
|
2019-10-19 17:19:19 -04:00
|
|
|
}
|
|
|
|
|
2019-04-19 11:18:46 -04:00
|
|
|
#[test]
|
|
|
|
fn empty_deps() {
|
|
|
|
let modules = Modules::new();
|
2020-01-25 12:53:16 -05:00
|
|
|
let specifier = ModuleSpecifier::resolve_url("file:///foo").unwrap();
|
|
|
|
assert!(modules.deps(&specifier).is_none());
|
2019-04-19 11:18:46 -04:00
|
|
|
}
|
|
|
|
|
2020-01-08 09:06:04 -05:00
|
|
|
/* TODO(bartlomieju): reenable
|
2019-04-19 11:18:46 -04:00
|
|
|
#[test]
|
|
|
|
fn deps() {
|
|
|
|
// "foo" -> "bar"
|
|
|
|
let mut modules = Modules::new();
|
|
|
|
modules.register(1, "foo");
|
|
|
|
modules.register(2, "bar");
|
|
|
|
modules.add_child(1, "bar");
|
|
|
|
let maybe_deps = modules.deps("foo");
|
|
|
|
assert!(maybe_deps.is_some());
|
|
|
|
let mut foo_deps = maybe_deps.unwrap();
|
|
|
|
assert_eq!(foo_deps.name, "foo");
|
|
|
|
assert!(foo_deps.deps.is_some());
|
|
|
|
let foo_children = foo_deps.deps.take().unwrap();
|
|
|
|
assert_eq!(foo_children.len(), 1);
|
|
|
|
let bar_deps = &foo_children[0];
|
|
|
|
assert_eq!(bar_deps.name, "bar");
|
|
|
|
assert_eq!(bar_deps.deps, Some(vec![]));
|
|
|
|
}
|
2019-04-27 13:04:09 -04:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_deps_to_json() {
|
|
|
|
let mut modules = Modules::new();
|
|
|
|
modules.register(1, "foo");
|
|
|
|
modules.register(2, "bar");
|
|
|
|
modules.register(3, "baz");
|
|
|
|
modules.register(4, "zuh");
|
|
|
|
modules.add_child(1, "bar");
|
|
|
|
modules.add_child(1, "baz");
|
|
|
|
modules.add_child(3, "zuh");
|
|
|
|
let maybe_deps = modules.deps("foo");
|
|
|
|
assert!(maybe_deps.is_some());
|
|
|
|
assert_eq!(
|
|
|
|
"[\"foo\",[[\"bar\",[]],[\"baz\",[[\"zuh\",[]]]]]]",
|
|
|
|
maybe_deps.unwrap().to_json()
|
|
|
|
);
|
|
|
|
}
|
2020-01-08 09:06:04 -05:00
|
|
|
*/
|
2019-03-26 11:56:34 -04:00
|
|
|
}
|