Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: module loading in EsIsolate #3615

Merged
merged 18 commits into from Jan 8, 2020
4 changes: 2 additions & 2 deletions cli/js.rs
Expand Up @@ -28,7 +28,7 @@ pub fn get_asset(name: &str) -> Option<&'static str> {

#[test]
fn cli_snapshot() {
let mut isolate = deno_core::EsIsolate::new(
let mut isolate = deno_core::Isolate::new(
deno_core::StartupData::Snapshot(CLI_SNAPSHOT),
false,
);
Expand All @@ -45,7 +45,7 @@ fn cli_snapshot() {

#[test]
fn compiler_snapshot() {
let mut isolate = deno_core::EsIsolate::new(
let mut isolate = deno_core::Isolate::new(
deno_core::StartupData::Snapshot(COMPILER_SNAPSHOT),
false,
);
Expand Down
4 changes: 2 additions & 2 deletions cli/lib.rs
Expand Up @@ -172,7 +172,6 @@ fn print_cache_info(worker: Worker) {

async fn print_file_info(worker: Worker, module_specifier: ModuleSpecifier) {
let global_state_ = &worker.state.global_state;
let state_ = &worker.state;

let maybe_source_file = global_state_
.file_fetcher
Expand Down Expand Up @@ -233,7 +232,8 @@ async fn print_file_info(worker: Worker, module_specifier: ModuleSpecifier) {
);
}

if let Some(deps) = state_.modules.lock().unwrap().deps(&compiled.name) {
let isolate = worker.isolate.try_lock().unwrap();
if let Some(deps) = isolate.modules.deps(&compiled.name) {
println!("{}{}", colors::bold("deps:\n".to_string()), deps.name);
if let Some(ref depsdeps) = deps.deps {
for d in depsdeps {
Expand Down
63 changes: 23 additions & 40 deletions cli/worker.rs
Expand Up @@ -6,7 +6,6 @@ use deno_core;
use deno_core::Buf;
use deno_core::ErrBox;
use deno_core::ModuleSpecifier;
use deno_core::RecursiveLoad;
use deno_core::StartupData;
use futures::channel::mpsc;
use futures::future::FutureExt;
Expand All @@ -20,6 +19,7 @@ use std::sync::Arc;
use std::sync::Mutex;
use std::task::Context;
use std::task::Poll;
use tokio::sync::Mutex as AsyncMutex;
use url::Url;

/// Wraps mpsc channels so they can be referenced
Expand All @@ -35,7 +35,7 @@ pub struct WorkerChannels {
#[derive(Clone)]
pub struct Worker {
pub name: String,
isolate: Arc<Mutex<Box<deno_core::EsIsolate>>>,
pub isolate: Arc<AsyncMutex<Box<deno_core::EsIsolate>>>,
pub state: ThreadSafeState,
external_channels: Arc<Mutex<WorkerChannels>>,
}
Expand All @@ -47,10 +47,13 @@ impl Worker {
state: ThreadSafeState,
external_channels: WorkerChannels,
) -> Self {
let isolate =
Arc::new(Mutex::new(deno_core::EsIsolate::new(startup_data, false)));
let isolate = Arc::new(AsyncMutex::new(deno_core::EsIsolate::new(
Box::new(state.clone()),
startup_data,
false,
)));
{
let mut i = isolate.lock().unwrap();
let mut i = isolate.try_lock().unwrap();
let op_registry = i.op_registry.clone();

ops::compiler::init(&mut i, &state);
Expand All @@ -71,18 +74,6 @@ impl Worker {
ops::timers::init(&mut i, &state);
ops::workers::init(&mut i, &state);

let state_ = state.clone();
i.set_dyn_import(move |id, specifier, referrer| {
let load_stream = RecursiveLoad::dynamic_import(
id,
specifier,
referrer,
state_.clone(),
state_.modules.clone(),
);
Box::new(load_stream)
});

let global_state_ = state.global_state.clone();
i.set_js_error_create(move |v8_exception| {
JSError::from_v8_exception(v8_exception, &global_state_.ts_compiler)
Expand All @@ -101,7 +92,7 @@ impl Worker {
&mut self,
handler: Box<dyn FnMut(ErrBox) -> Result<(), ErrBox>>,
) {
let mut i = self.isolate.lock().unwrap();
let mut i = self.isolate.try_lock().unwrap();
i.set_error_handler(handler);
}

Expand All @@ -119,39 +110,31 @@ impl Worker {
js_filename: &str,
js_source: &str,
) -> Result<(), ErrBox> {
let mut isolate = self.isolate.lock().unwrap();
let mut isolate = self.isolate.try_lock().unwrap();
isolate.execute(js_filename, js_source)
}

/// Executes the provided JavaScript module.
pub fn execute_mod_async(
///
/// Takes ownership of the isolate behind mutex.
pub async fn execute_mod_async(
&mut self,
module_specifier: &ModuleSpecifier,
maybe_code: Option<String>,
is_prefetch: bool,
) -> impl Future<Output = Result<(), ErrBox>> {
) -> Result<(), ErrBox> {
let specifier = module_specifier.to_string();
let worker = self.clone();
let loader = self.state.clone();
let isolate = self.isolate.clone();
let modules = self.state.modules.clone();
let recursive_load = RecursiveLoad::main(
&module_specifier.to_string(),
maybe_code,
loader,
modules,
);

async move {
let id = recursive_load.get_future(isolate).await?;
worker.state.global_state.progress.done();

if !is_prefetch {
let mut isolate = worker.isolate.lock().unwrap();
return isolate.mod_evaluate(id);
}
let mut isolate = self.isolate.lock().await;
let id = isolate.load_module(&specifier, maybe_code).await?;
worker.state.global_state.progress.done();

Ok(())
if !is_prefetch {
return isolate.mod_evaluate(id);
}

Ok(())
}

/// Post message to worker as a host.
Expand Down Expand Up @@ -183,7 +166,7 @@ impl Future for Worker {

fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
let inner = self.get_mut();
let mut isolate = inner.isolate.lock().unwrap();
let mut isolate = inner.isolate.try_lock().unwrap();
isolate.poll_unpin(cx)
}
}
Expand Down
20 changes: 10 additions & 10 deletions core/bindings.rs
@@ -1,7 +1,6 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.

use crate::es_isolate::EsIsolate;
use crate::es_isolate::ResolveContext;
use crate::isolate::DenoBuf;
use crate::isolate::Isolate;
use crate::isolate::PinnedBuf;
Expand Down Expand Up @@ -283,7 +282,7 @@ pub extern "C" fn host_initialize_import_meta_object_callback(
let id = module.get_identity_hash();
assert_ne!(id, 0);

let info = deno_isolate.get_module_info(id).expect("Module not found");
let info = deno_isolate.modules.get_info(id).expect("Module not found");

meta.create_data_property(
context,
Expand Down Expand Up @@ -713,9 +712,12 @@ pub fn module_resolve_callback(
let scope = hs.enter();

let referrer_id = referrer.get_identity_hash();
let referrer_info = deno_isolate
.get_module_info(referrer_id)
.expect("ModuleInfo not found");
let referrer_name = deno_isolate
.modules
.get_info(referrer_id)
.expect("ModuleInfo not found")
.name
.to_string();
let len_ = referrer.get_module_requests_length();

let specifier_str = specifier.to_rust_string_lossy(scope);
Expand All @@ -725,15 +727,13 @@ pub fn module_resolve_callback(
let req_str = req.to_rust_string_lossy(scope);

if req_str == specifier_str {
let ResolveContext { resolve_fn } =
unsafe { ResolveContext::from_raw_ptr(deno_isolate.resolve_context) };
let id = resolve_fn(&req_str, referrer_id);
let maybe_info = deno_isolate.get_module_info(id);
let id = deno_isolate.module_resolve_cb(&req_str, referrer_id);
let maybe_info = deno_isolate.modules.get_info(id);

if maybe_info.is_none() {
let msg = format!(
"Cannot resolve module \"{}\" from \"{}\"",
req_str, referrer_info.name
req_str, referrer_name
);
let msg = v8::String::new(scope, &msg).unwrap();
isolate.throw_exception(msg.into());
Expand Down