1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-25 15:29:32 -05:00

refactor(cli): Simplify choosing type lib in CliModuleLoader (#8637)

This commit is contained in:
Bartek Iwańczuk 2020-12-07 11:03:03 +01:00 committed by GitHub
parent c0ccbcdaee
commit c8e9b2654e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 33 additions and 34 deletions

View file

@ -22,23 +22,37 @@ pub struct CliModuleLoader {
/// import map file will be resolved and set.
pub import_map: Option<ImportMap>,
pub lib: TypeLib,
pub is_main: bool,
pub program_state: Arc<ProgramState>,
}
impl CliModuleLoader {
pub fn new(maybe_import_map: Option<ImportMap>) -> Rc<Self> {
pub fn new(program_state: Arc<ProgramState>) -> Rc<Self> {
let lib = if program_state.flags.unstable {
TypeLib::UnstableDenoWindow
} else {
TypeLib::DenoWindow
};
let import_map = program_state.maybe_import_map.clone();
Rc::new(CliModuleLoader {
import_map: maybe_import_map,
lib: TypeLib::DenoWindow,
is_main: true,
import_map,
lib,
program_state,
})
}
pub fn new_for_worker() -> Rc<Self> {
pub fn new_for_worker(program_state: Arc<ProgramState>) -> Rc<Self> {
let lib = if program_state.flags.unstable {
TypeLib::UnstableDenoWorker
} else {
TypeLib::DenoWorker
};
Rc::new(CliModuleLoader {
import_map: None,
lib: TypeLib::DenoWorker,
is_main: false,
lib,
program_state,
})
}
}
@ -46,18 +60,13 @@ impl CliModuleLoader {
impl ModuleLoader for CliModuleLoader {
fn resolve(
&self,
op_state: Rc<RefCell<OpState>>,
_op_state: Rc<RefCell<OpState>>,
specifier: &str,
referrer: &str,
is_main: bool,
) -> Result<ModuleSpecifier, AnyError> {
let program_state = {
let state = op_state.borrow();
state.borrow::<Arc<ProgramState>>().clone()
};
// FIXME(bartlomieju): hacky way to provide compatibility with repl
let referrer = if referrer.is_empty() && program_state.flags.repl {
let referrer = if referrer.is_empty() && self.program_state.flags.repl {
"<unknown>"
} else {
referrer
@ -80,19 +89,18 @@ impl ModuleLoader for CliModuleLoader {
fn load(
&self,
op_state: Rc<RefCell<OpState>>,
_op_state: Rc<RefCell<OpState>>,
module_specifier: &ModuleSpecifier,
maybe_referrer: Option<ModuleSpecifier>,
_is_dynamic: bool,
) -> Pin<Box<deno_core::ModuleSourceFuture>> {
let module_specifier = module_specifier.to_owned();
let module_url_specified = module_specifier.to_string();
let program_state = {
let state = op_state.borrow();
state.borrow::<Arc<ProgramState>>().clone()
};
let program_state = self.program_state.clone();
// TODO(@kitsonk) this shouldn't be async
// NOTE: this block is async only because of `deno_core`
// interface requirements; module was already loaded
// when constructing module graph during call to `prepare_load`.
let fut = async move {
let compiled_module = program_state
.fetch_compiled_module(module_specifier, maybe_referrer)?;
@ -117,21 +125,13 @@ impl ModuleLoader for CliModuleLoader {
is_dynamic: bool,
) -> Pin<Box<dyn Future<Output = Result<(), AnyError>>>> {
let specifier = specifier.clone();
let program_state = self.program_state.clone();
let maybe_import_map = self.import_map.clone();
let state = op_state.borrow();
// The permissions that should be applied to any dynamically imported module
let dynamic_permissions = state.borrow::<Permissions>().clone();
let program_state = state.borrow::<Arc<ProgramState>>().clone();
let lib = if program_state.flags.unstable {
if self.lib == TypeLib::DenoWindow {
TypeLib::UnstableDenoWindow
} else {
TypeLib::UnstableDenoWorker
}
} else {
self.lib.clone()
};
let lib = self.lib.clone();
drop(state);
// TODO(bartlomieju): `prepare_module_load` should take `load_id` param

View file

@ -137,7 +137,7 @@ impl WebWorker {
has_deno_namespace: bool,
worker_id: u32,
) -> Self {
let module_loader = CliModuleLoader::new_for_worker();
let module_loader = CliModuleLoader::new_for_worker(program_state.clone());
let global_state_ = program_state.clone();
let js_error_create_fn = Box::new(move |core_js_error| {

View file

@ -45,8 +45,7 @@ impl MainWorker {
main_module: ModuleSpecifier,
permissions: Permissions,
) -> Self {
let module_loader =
CliModuleLoader::new(program_state.maybe_import_map.clone());
let module_loader = CliModuleLoader::new(program_state.clone());
let global_state_ = program_state.clone();