2019-03-14 19:17:52 -04:00
|
|
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
|
|
|
use crate::deno_dir;
|
2019-03-19 20:55:59 -04:00
|
|
|
use crate::errors::DenoResult;
|
2019-03-14 19:17:52 -04:00
|
|
|
use crate::flags;
|
|
|
|
use crate::global_timer::GlobalTimer;
|
|
|
|
use crate::modules::Modules;
|
2019-04-09 13:11:25 -04:00
|
|
|
use crate::ops;
|
2019-03-19 20:55:59 -04:00
|
|
|
use crate::permissions::DenoPermissions;
|
2019-04-08 17:10:00 -04:00
|
|
|
use crate::resources;
|
2019-04-01 15:09:59 -04:00
|
|
|
use crate::resources::ResourceId;
|
2019-04-08 17:10:00 -04:00
|
|
|
use crate::worker::Worker;
|
2019-04-09 13:11:25 -04:00
|
|
|
use deno::deno_buf;
|
2019-03-30 19:30:40 -04:00
|
|
|
use deno::Buf;
|
2019-04-09 13:11:25 -04:00
|
|
|
use deno::Dispatch;
|
|
|
|
use deno::Op;
|
2019-04-01 15:09:59 -04:00
|
|
|
use futures::future::Shared;
|
2019-03-14 19:17:52 -04:00
|
|
|
use std;
|
2019-04-01 15:09:59 -04:00
|
|
|
use std::collections::HashMap;
|
2019-03-14 19:17:52 -04:00
|
|
|
use std::env;
|
2019-04-09 13:11:25 -04:00
|
|
|
use std::ops::Deref;
|
2019-03-14 19:17:52 -04:00
|
|
|
use std::sync::atomic::{AtomicUsize, Ordering};
|
2019-04-09 13:11:25 -04:00
|
|
|
use std::sync::Arc;
|
2019-03-14 19:17:52 -04:00
|
|
|
use std::sync::Mutex;
|
2019-04-08 16:22:40 -04:00
|
|
|
use std::time::Instant;
|
2019-04-08 17:10:00 -04:00
|
|
|
use tokio::sync::mpsc as async_mpsc;
|
2019-03-14 19:17:52 -04:00
|
|
|
|
|
|
|
pub type WorkerSender = async_mpsc::Sender<Buf>;
|
|
|
|
pub type WorkerReceiver = async_mpsc::Receiver<Buf>;
|
|
|
|
pub type WorkerChannels = (WorkerSender, WorkerReceiver);
|
2019-04-08 17:10:00 -04:00
|
|
|
pub type UserWorkerTable = HashMap<ResourceId, Shared<Worker>>;
|
2019-03-14 19:17:52 -04:00
|
|
|
|
|
|
|
// AtomicU64 is currently unstable
|
|
|
|
#[derive(Default)]
|
|
|
|
pub struct Metrics {
|
|
|
|
pub ops_dispatched: AtomicUsize,
|
|
|
|
pub ops_completed: AtomicUsize,
|
|
|
|
pub bytes_sent_control: AtomicUsize,
|
|
|
|
pub bytes_sent_data: AtomicUsize,
|
|
|
|
pub bytes_received: AtomicUsize,
|
|
|
|
pub resolve_count: AtomicUsize,
|
|
|
|
}
|
|
|
|
|
2019-04-09 13:11:25 -04:00
|
|
|
// Wrap State so that it can implement Dispatch.
|
|
|
|
pub struct ThreadSafeState(Arc<State>);
|
|
|
|
|
|
|
|
// Isolate cannot be passed between threads but ThreadSafeState can.
|
|
|
|
// ThreadSafeState satisfies Send and Sync.
|
2019-03-14 19:17:52 -04:00
|
|
|
// So any state that needs to be accessed outside the main V8 thread should be
|
2019-04-09 13:11:25 -04:00
|
|
|
// inside ThreadSafeState.
|
2019-03-14 19:17:52 -04:00
|
|
|
#[cfg_attr(feature = "cargo-clippy", allow(stutter))]
|
2019-04-09 13:11:25 -04:00
|
|
|
pub struct State {
|
2019-03-14 19:17:52 -04:00
|
|
|
pub dir: deno_dir::DenoDir,
|
|
|
|
pub argv: Vec<String>,
|
2019-03-19 20:55:59 -04:00
|
|
|
pub permissions: DenoPermissions,
|
2019-03-14 19:17:52 -04:00
|
|
|
pub flags: flags::DenoFlags,
|
|
|
|
pub metrics: Metrics,
|
|
|
|
pub modules: Mutex<Modules>,
|
2019-04-08 17:10:00 -04:00
|
|
|
pub worker_channels: Mutex<WorkerChannels>,
|
2019-03-14 19:17:52 -04:00
|
|
|
pub global_timer: Mutex<GlobalTimer>,
|
2019-04-01 15:09:59 -04:00
|
|
|
pub workers: Mutex<UserWorkerTable>,
|
2019-04-08 16:22:40 -04:00
|
|
|
pub start_time: Instant,
|
2019-04-08 17:10:00 -04:00
|
|
|
pub resource: resources::Resource,
|
2019-04-11 10:58:31 -04:00
|
|
|
pub dispatch_selector: ops::OpSelector,
|
2019-03-14 19:17:52 -04:00
|
|
|
}
|
|
|
|
|
2019-04-09 13:11:25 -04:00
|
|
|
impl Clone for ThreadSafeState {
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
ThreadSafeState(self.0.clone())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Deref for ThreadSafeState {
|
|
|
|
type Target = Arc<State>;
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Dispatch for ThreadSafeState {
|
|
|
|
fn dispatch(
|
|
|
|
&mut self,
|
|
|
|
control: &[u8],
|
|
|
|
zero_copy: deno_buf,
|
|
|
|
) -> (bool, Box<Op>) {
|
2019-04-11 10:58:31 -04:00
|
|
|
ops::dispatch_all(self, control, zero_copy, self.dispatch_selector)
|
2019-04-09 13:11:25 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ThreadSafeState {
|
2019-04-11 10:58:31 -04:00
|
|
|
pub fn new(
|
|
|
|
flags: flags::DenoFlags,
|
|
|
|
argv_rest: Vec<String>,
|
|
|
|
dispatch_selector: ops::OpSelector,
|
|
|
|
) -> Self {
|
2019-03-14 19:17:52 -04:00
|
|
|
let custom_root = env::var("DENO_DIR").map(|s| s.into()).ok();
|
|
|
|
|
2019-04-08 17:10:00 -04:00
|
|
|
let (worker_in_tx, worker_in_rx) = async_mpsc::channel::<Buf>(1);
|
|
|
|
let (worker_out_tx, worker_out_rx) = async_mpsc::channel::<Buf>(1);
|
|
|
|
let internal_channels = (worker_out_tx, worker_in_rx);
|
|
|
|
let external_channels = (worker_in_tx, worker_out_rx);
|
|
|
|
let resource = resources::add_worker(external_channels);
|
|
|
|
|
2019-04-09 13:11:25 -04:00
|
|
|
ThreadSafeState(Arc::new(State {
|
2019-03-28 16:05:41 -04:00
|
|
|
dir: deno_dir::DenoDir::new(custom_root).unwrap(),
|
2019-03-14 19:17:52 -04:00
|
|
|
argv: argv_rest,
|
2019-03-19 20:55:59 -04:00
|
|
|
permissions: DenoPermissions::from_flags(&flags),
|
2019-03-14 19:17:52 -04:00
|
|
|
flags,
|
|
|
|
metrics: Metrics::default(),
|
|
|
|
modules: Mutex::new(Modules::new()),
|
2019-04-08 17:10:00 -04:00
|
|
|
worker_channels: Mutex::new(internal_channels),
|
2019-03-14 19:17:52 -04:00
|
|
|
global_timer: Mutex::new(GlobalTimer::new()),
|
2019-04-01 15:09:59 -04:00
|
|
|
workers: Mutex::new(UserWorkerTable::new()),
|
2019-04-08 16:22:40 -04:00
|
|
|
start_time: Instant::now(),
|
2019-04-08 17:10:00 -04:00
|
|
|
resource,
|
2019-04-11 10:58:31 -04:00
|
|
|
dispatch_selector,
|
2019-04-09 13:11:25 -04:00
|
|
|
}))
|
2019-03-14 19:17:52 -04:00
|
|
|
}
|
|
|
|
|
2019-04-01 15:09:59 -04:00
|
|
|
/// Read main module from argv
|
2019-03-14 19:17:52 -04:00
|
|
|
pub fn main_module(&self) -> Option<String> {
|
|
|
|
if self.argv.len() <= 1 {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
let specifier = self.argv[1].clone();
|
|
|
|
let referrer = ".";
|
|
|
|
match self.dir.resolve_module_url(&specifier, referrer) {
|
|
|
|
Ok(url) => Some(url.to_string()),
|
|
|
|
Err(e) => {
|
|
|
|
debug!("Potentially swallowed error {}", e);
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-19 20:55:59 -04:00
|
|
|
#[inline]
|
|
|
|
pub fn check_read(&self, filename: &str) -> DenoResult<()> {
|
|
|
|
self.permissions.check_read(filename)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn check_write(&self, filename: &str) -> DenoResult<()> {
|
|
|
|
self.permissions.check_write(filename)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn check_env(&self) -> DenoResult<()> {
|
|
|
|
self.permissions.check_env()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn check_net(&self, filename: &str) -> DenoResult<()> {
|
|
|
|
self.permissions.check_net(filename)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn check_run(&self) -> DenoResult<()> {
|
|
|
|
self.permissions.check_run()
|
|
|
|
}
|
|
|
|
|
2019-03-14 19:17:52 -04:00
|
|
|
#[cfg(test)]
|
2019-04-09 13:11:25 -04:00
|
|
|
pub fn mock() -> ThreadSafeState {
|
2019-03-14 19:17:52 -04:00
|
|
|
let argv = vec![String::from("./deno"), String::from("hello.js")];
|
|
|
|
// For debugging: argv.push_back(String::from("-D"));
|
2019-04-06 18:13:06 -04:00
|
|
|
let (flags, rest_argv) = flags::set_flags(argv).unwrap();
|
2019-04-11 10:58:31 -04:00
|
|
|
ThreadSafeState::new(flags, rest_argv, ops::op_selector_std)
|
2019-03-14 19:17:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn metrics_op_dispatched(
|
|
|
|
&self,
|
|
|
|
bytes_sent_control: usize,
|
|
|
|
bytes_sent_data: usize,
|
|
|
|
) {
|
|
|
|
self.metrics.ops_dispatched.fetch_add(1, Ordering::SeqCst);
|
|
|
|
self
|
|
|
|
.metrics
|
|
|
|
.bytes_sent_control
|
|
|
|
.fetch_add(bytes_sent_control, Ordering::SeqCst);
|
|
|
|
self
|
|
|
|
.metrics
|
|
|
|
.bytes_sent_data
|
|
|
|
.fetch_add(bytes_sent_data, Ordering::SeqCst);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn metrics_op_completed(&self, bytes_received: usize) {
|
|
|
|
self.metrics.ops_completed.fetch_add(1, Ordering::SeqCst);
|
|
|
|
self
|
|
|
|
.metrics
|
|
|
|
.bytes_received
|
|
|
|
.fetch_add(bytes_received, Ordering::SeqCst);
|
|
|
|
}
|
|
|
|
}
|
2019-04-09 13:11:25 -04:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn thread_safe() {
|
|
|
|
fn f<S: Send + Sync>(_: S) {}
|
|
|
|
f(ThreadSafeState::mock());
|
|
|
|
}
|