diff options
Diffstat (limited to 'cli/global_state.rs')
| -rw-r--r-- | cli/global_state.rs | 36 |
1 files changed, 22 insertions, 14 deletions
diff --git a/cli/global_state.rs b/cli/global_state.rs index 80f7d6e7a..ca040c40c 100644 --- a/cli/global_state.rs +++ b/cli/global_state.rs @@ -23,8 +23,10 @@ use std::path::Path; use std::str; use std::sync::Arc; use std::sync::Mutex; +use tokio::sync::Mutex as AsyncMutex; /// Holds state of the program and can be accessed by V8 isolate. +#[derive(Clone)] pub struct ThreadSafeGlobalState(Arc<GlobalState>); /// This structure represents state of single "deno" program. @@ -45,12 +47,7 @@ pub struct GlobalState { pub ts_compiler: TsCompiler, pub wasm_compiler: WasmCompiler, pub lockfile: Option<Mutex<Lockfile>>, -} - -impl Clone for ThreadSafeGlobalState { - fn clone(&self) -> Self { - ThreadSafeGlobalState(self.0.clone()) - } + compile_lock: AsyncMutex<()>, } impl Deref for ThreadSafeGlobalState { @@ -103,6 +100,7 @@ impl ThreadSafeGlobalState { json_compiler: JsonCompiler {}, wasm_compiler: WasmCompiler::default(), lockfile, + compile_lock: AsyncMutex::new(()), }; Ok(ThreadSafeGlobalState(Arc::new(state))) @@ -122,11 +120,19 @@ impl ThreadSafeGlobalState { .file_fetcher .fetch_source_file_async(&module_specifier, maybe_referrer) .await?; - let compiled_module_fut = match out.media_type { - msg::MediaType::Unknown => state1.js_compiler.compile_async(out), - msg::MediaType::Json => state1.json_compiler.compile_async(&out), + + // TODO(ry) Try to lift compile_lock as high up in the call stack for + // sanity. + let compile_lock = self.compile_lock.lock().await; + + let compiled_module = match out.media_type { + msg::MediaType::Unknown => state1.js_compiler.compile_async(out).await, + msg::MediaType::Json => state1.json_compiler.compile_async(&out).await, msg::MediaType::Wasm => { - state1.wasm_compiler.compile_async(state1.clone(), &out) + state1 + .wasm_compiler + .compile_async(state1.clone(), &out) + .await } msg::MediaType::TypeScript | msg::MediaType::TSX @@ -134,18 +140,20 @@ impl ThreadSafeGlobalState { state1 .ts_compiler .compile_async(state1.clone(), &out, target_lib) + .await } msg::MediaType::JavaScript => { if state1.ts_compiler.compile_js { - state1 + state2 .ts_compiler .compile_async(state1.clone(), &out, target_lib) + .await } else { - state1.js_compiler.compile_async(out) + state1.js_compiler.compile_async(out).await } } - }; - let compiled_module = compiled_module_fut.await?; + }?; + drop(compile_lock); if let Some(ref lockfile) = state2.lockfile { let mut g = lockfile.lock().unwrap(); |
