2020-01-02 20:13:47 +00:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2019-11-04 15:38:52 +00:00
|
|
|
use crate::compilers::CompiledModule;
|
|
|
|
use crate::compilers::JsCompiler;
|
|
|
|
use crate::compilers::JsonCompiler;
|
2020-01-29 17:54:23 +00:00
|
|
|
use crate::compilers::TargetLib;
|
2019-11-04 15:38:52 +00:00
|
|
|
use crate::compilers::TsCompiler;
|
2019-11-14 13:31:39 +00:00
|
|
|
use crate::compilers::WasmCompiler;
|
2019-11-04 15:38:52 +00:00
|
|
|
use crate::deno_dir;
|
|
|
|
use crate::file_fetcher::SourceFileFetcher;
|
|
|
|
use crate::flags;
|
2020-02-19 13:17:13 +00:00
|
|
|
use crate::http_cache;
|
2019-11-04 15:38:52 +00:00
|
|
|
use crate::lockfile::Lockfile;
|
|
|
|
use crate::msg;
|
|
|
|
use crate::permissions::DenoPermissions;
|
2020-01-05 16:56:18 +00:00
|
|
|
use deno_core::ErrBox;
|
|
|
|
use deno_core::ModuleSpecifier;
|
2019-11-04 15:38:52 +00:00
|
|
|
use std;
|
|
|
|
use std::env;
|
|
|
|
use std::ops::Deref;
|
2020-02-11 16:23:40 +00:00
|
|
|
use std::sync::atomic::AtomicUsize;
|
2019-11-04 15:38:52 +00:00
|
|
|
use std::sync::Arc;
|
|
|
|
use std::sync::Mutex;
|
2020-02-07 02:24:51 +00:00
|
|
|
use tokio::sync::Mutex as AsyncMutex;
|
2019-11-04 15:38:52 +00:00
|
|
|
|
|
|
|
/// Holds state of the program and can be accessed by V8 isolate.
|
2020-02-07 02:24:51 +00:00
|
|
|
#[derive(Clone)]
|
2020-02-07 04:05:02 +00:00
|
|
|
pub struct GlobalState(Arc<GlobalStateInner>);
|
2019-11-04 15:38:52 +00:00
|
|
|
|
|
|
|
/// This structure represents state of single "deno" program.
|
|
|
|
///
|
|
|
|
/// It is shared by all created workers (thus V8 isolates).
|
2020-02-07 04:05:02 +00:00
|
|
|
pub struct GlobalStateInner {
|
2019-11-04 15:38:52 +00:00
|
|
|
/// Flags parsed from `argv` contents.
|
2020-02-26 10:52:15 +00:00
|
|
|
pub flags: flags::Flags,
|
2019-11-04 15:38:52 +00:00
|
|
|
/// Permissions parsed from `flags`.
|
|
|
|
pub permissions: DenoPermissions,
|
|
|
|
pub dir: deno_dir::DenoDir,
|
|
|
|
pub file_fetcher: SourceFileFetcher,
|
|
|
|
pub js_compiler: JsCompiler,
|
|
|
|
pub json_compiler: JsonCompiler,
|
|
|
|
pub ts_compiler: TsCompiler,
|
2019-11-14 13:31:39 +00:00
|
|
|
pub wasm_compiler: WasmCompiler,
|
2019-11-04 15:38:52 +00:00
|
|
|
pub lockfile: Option<Mutex<Lockfile>>,
|
2020-02-11 16:23:40 +00:00
|
|
|
pub compiler_starts: AtomicUsize,
|
2020-02-07 02:24:51 +00:00
|
|
|
compile_lock: AsyncMutex<()>,
|
2019-11-04 15:38:52 +00:00
|
|
|
}
|
|
|
|
|
2020-02-07 04:05:02 +00:00
|
|
|
impl Deref for GlobalState {
|
|
|
|
type Target = GlobalStateInner;
|
2019-11-04 15:38:52 +00:00
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-07 04:05:02 +00:00
|
|
|
impl GlobalState {
|
2020-02-26 10:52:15 +00:00
|
|
|
pub fn new(flags: flags::Flags) -> Result<Self, ErrBox> {
|
2019-11-04 15:38:52 +00:00
|
|
|
let custom_root = env::var("DENO_DIR").map(String::into).ok();
|
|
|
|
let dir = deno_dir::DenoDir::new(custom_root)?;
|
2020-02-19 13:17:13 +00:00
|
|
|
let deps_cache_location = dir.root.join("deps");
|
|
|
|
let http_cache = http_cache::HttpCache::new(&deps_cache_location)?;
|
2019-11-04 15:38:52 +00:00
|
|
|
|
|
|
|
let file_fetcher = SourceFileFetcher::new(
|
2020-02-19 13:17:13 +00:00
|
|
|
http_cache,
|
2019-11-04 15:38:52 +00:00
|
|
|
!flags.reload,
|
|
|
|
flags.cache_blacklist.clone(),
|
2019-12-03 22:48:53 +00:00
|
|
|
flags.no_remote,
|
|
|
|
flags.cached_only,
|
2020-02-17 16:59:51 +00:00
|
|
|
flags.ca_file.clone(),
|
2019-11-04 15:38:52 +00:00
|
|
|
)?;
|
|
|
|
|
|
|
|
let ts_compiler = TsCompiler::new(
|
|
|
|
file_fetcher.clone(),
|
|
|
|
dir.gen_cache.clone(),
|
|
|
|
!flags.reload,
|
|
|
|
flags.config_path.clone(),
|
|
|
|
)?;
|
|
|
|
|
|
|
|
// Note: reads lazily from disk on first call to lockfile.check()
|
|
|
|
let lockfile = if let Some(filename) = &flags.lock {
|
|
|
|
Some(Mutex::new(Lockfile::new(filename.to_string())))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
2020-02-07 04:05:02 +00:00
|
|
|
let inner = GlobalStateInner {
|
2019-11-04 15:38:52 +00:00
|
|
|
dir,
|
|
|
|
permissions: DenoPermissions::from_flags(&flags),
|
|
|
|
flags,
|
|
|
|
file_fetcher,
|
|
|
|
ts_compiler,
|
|
|
|
js_compiler: JsCompiler {},
|
|
|
|
json_compiler: JsonCompiler {},
|
2019-11-14 13:31:39 +00:00
|
|
|
wasm_compiler: WasmCompiler::default(),
|
2019-11-04 15:38:52 +00:00
|
|
|
lockfile,
|
2020-02-11 16:23:40 +00:00
|
|
|
compiler_starts: AtomicUsize::new(0),
|
2020-02-07 02:24:51 +00:00
|
|
|
compile_lock: AsyncMutex::new(()),
|
2019-11-04 15:38:52 +00:00
|
|
|
};
|
|
|
|
|
2020-02-07 04:05:02 +00:00
|
|
|
Ok(GlobalState(Arc::new(inner)))
|
2019-11-04 15:38:52 +00:00
|
|
|
}
|
|
|
|
|
2020-02-03 23:08:44 +00:00
|
|
|
pub async fn fetch_compiled_module(
|
2019-12-14 22:12:34 +00:00
|
|
|
&self,
|
2020-02-03 23:08:44 +00:00
|
|
|
module_specifier: ModuleSpecifier,
|
2019-11-25 14:33:23 +00:00
|
|
|
maybe_referrer: Option<ModuleSpecifier>,
|
2020-01-29 17:54:23 +00:00
|
|
|
target_lib: TargetLib,
|
2020-02-03 23:08:44 +00:00
|
|
|
) -> Result<CompiledModule, ErrBox> {
|
2019-11-04 15:38:52 +00:00
|
|
|
let state1 = self.clone();
|
|
|
|
let state2 = self.clone();
|
2020-02-03 23:08:44 +00:00
|
|
|
let module_specifier = module_specifier.clone();
|
2019-11-04 15:38:52 +00:00
|
|
|
|
2020-02-03 23:08:44 +00:00
|
|
|
let out = self
|
2019-11-04 15:38:52 +00:00
|
|
|
.file_fetcher
|
2020-02-25 19:42:00 +00:00
|
|
|
.fetch_source_file(&module_specifier, maybe_referrer)
|
2020-02-03 23:08:44 +00:00
|
|
|
.await?;
|
2020-02-07 02:24:51 +00:00
|
|
|
|
|
|
|
// 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 {
|
2020-02-25 19:42:00 +00:00
|
|
|
msg::MediaType::Unknown => state1.js_compiler.compile(out).await,
|
|
|
|
msg::MediaType::Json => state1.json_compiler.compile(&out).await,
|
2020-02-03 23:08:44 +00:00
|
|
|
msg::MediaType::Wasm => {
|
2020-02-25 19:42:00 +00:00
|
|
|
state1.wasm_compiler.compile(state1.clone(), &out).await
|
2020-02-03 23:08:44 +00:00
|
|
|
}
|
|
|
|
msg::MediaType::TypeScript
|
|
|
|
| msg::MediaType::TSX
|
|
|
|
| msg::MediaType::JSX => {
|
|
|
|
state1
|
|
|
|
.ts_compiler
|
2020-02-25 19:42:00 +00:00
|
|
|
.compile(state1.clone(), &out, target_lib)
|
2020-02-07 02:24:51 +00:00
|
|
|
.await
|
2020-02-03 23:08:44 +00:00
|
|
|
}
|
|
|
|
msg::MediaType::JavaScript => {
|
|
|
|
if state1.ts_compiler.compile_js {
|
2020-02-07 02:24:51 +00:00
|
|
|
state2
|
2020-01-29 17:54:23 +00:00
|
|
|
.ts_compiler
|
2020-02-25 19:42:00 +00:00
|
|
|
.compile(state1.clone(), &out, target_lib)
|
2020-02-07 02:24:51 +00:00
|
|
|
.await
|
2020-02-03 23:08:44 +00:00
|
|
|
} else {
|
2020-02-25 19:42:00 +00:00
|
|
|
state1.js_compiler.compile(out).await
|
2019-11-04 15:38:52 +00:00
|
|
|
}
|
2019-12-14 22:12:34 +00:00
|
|
|
}
|
2020-02-07 02:24:51 +00:00
|
|
|
}?;
|
|
|
|
drop(compile_lock);
|
2020-02-03 23:08:44 +00:00
|
|
|
|
|
|
|
if let Some(ref lockfile) = state2.lockfile {
|
|
|
|
let mut g = lockfile.lock().unwrap();
|
|
|
|
if state2.flags.lock_write {
|
|
|
|
g.insert(&compiled_module);
|
|
|
|
} else {
|
|
|
|
let check = match g.check(&compiled_module) {
|
|
|
|
Err(e) => return Err(ErrBox::from(e)),
|
|
|
|
Ok(v) => v,
|
|
|
|
};
|
|
|
|
if !check {
|
|
|
|
eprintln!(
|
|
|
|
"Subresource integrity check failed --lock={}\n{}",
|
|
|
|
g.filename, compiled_module.name
|
|
|
|
);
|
|
|
|
std::process::exit(10);
|
2019-11-04 15:38:52 +00:00
|
|
|
}
|
2019-12-14 22:12:34 +00:00
|
|
|
}
|
|
|
|
}
|
2020-02-03 23:08:44 +00:00
|
|
|
Ok(compiled_module)
|
2019-11-04 15:38:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
2020-02-07 04:05:02 +00:00
|
|
|
pub fn mock(argv: Vec<String>) -> GlobalState {
|
2020-02-26 10:52:15 +00:00
|
|
|
GlobalState::new(flags::Flags {
|
2020-02-07 04:05:02 +00:00
|
|
|
argv,
|
2020-02-26 10:52:15 +00:00
|
|
|
..flags::Flags::default()
|
2020-02-07 04:05:02 +00:00
|
|
|
})
|
2019-11-04 15:38:52 +00:00
|
|
|
.unwrap()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn thread_safe() {
|
|
|
|
fn f<S: Send + Sync>(_: S) {}
|
2020-02-25 19:42:00 +00:00
|
|
|
f(GlobalState::mock(vec![]));
|
2019-11-04 15:38:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn import_map_given_for_repl() {
|
2020-02-26 10:52:15 +00:00
|
|
|
let _result = GlobalState::new(flags::Flags {
|
2020-02-07 04:05:02 +00:00
|
|
|
import_map_path: Some("import_map.json".to_string()),
|
2020-02-26 10:52:15 +00:00
|
|
|
..flags::Flags::default()
|
2020-02-07 04:05:02 +00:00
|
|
|
});
|
2019-11-04 15:38:52 +00:00
|
|
|
}
|