1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-29 16:30:56 -05:00

Replace mutex by atomics (#1238)

This commit is contained in:
F001 2018-11-30 11:03:00 +08:00 committed by Ryan Dahl
parent 286e76d8c1
commit b6fda735ee
7 changed files with 151 additions and 148 deletions

View file

@ -41,16 +41,13 @@ impl DenoDir {
// https://github.com/denoland/deno/blob/golang/deno_dir.go#L99-L111 // https://github.com/denoland/deno/blob/golang/deno_dir.go#L99-L111
pub fn new( pub fn new(
reload: bool, reload: bool,
custom_root: Option<&Path>, custom_root: Option<PathBuf>,
) -> std::io::Result<Self> { ) -> std::io::Result<Self> {
// Only setup once. // Only setup once.
let home_dir = dirs::home_dir().expect("Could not get home directory."); let home_dir = dirs::home_dir().expect("Could not get home directory.");
let default = home_dir.join(".deno"); let default = home_dir.join(".deno");
let root: PathBuf = match custom_root { let root: PathBuf = custom_root.unwrap_or(default);
None => default,
Some(path) => path.to_path_buf(),
};
let gen = root.as_path().join("gen"); let gen = root.as_path().join("gen");
let deps = root.as_path().join("deps"); let deps = root.as_path().join("deps");
let deps_http = deps.join("http"); let deps_http = deps.join("http");
@ -390,8 +387,8 @@ pub struct CodeFetchOutput {
#[cfg(test)] #[cfg(test)]
pub fn test_setup() -> (TempDir, DenoDir) { pub fn test_setup() -> (TempDir, DenoDir) {
let temp_dir = TempDir::new().expect("tempdir fail"); let temp_dir = TempDir::new().expect("tempdir fail");
let deno_dir = let deno_dir = DenoDir::new(false, Some(temp_dir.path().to_path_buf()))
DenoDir::new(false, Some(temp_dir.path())).expect("setup fail"); .expect("setup fail");
(temp_dir, deno_dir) (temp_dir, deno_dir)
} }

View file

@ -17,10 +17,9 @@ use std;
use std::env; use std::env;
use std::ffi::CStr; use std::ffi::CStr;
use std::ffi::CString; use std::ffi::CString;
use std::path::Path; use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::mpsc; use std::sync::mpsc;
use std::sync::Arc; use std::sync::Arc;
use std::sync::Mutex;
use std::time::Duration; use std::time::Duration;
use std::time::Instant; use std::time::Instant;
use tokio; use tokio;
@ -47,130 +46,113 @@ pub struct Isolate {
libdeno_isolate: *const libdeno::isolate, libdeno_isolate: *const libdeno::isolate,
dispatch: Dispatch, dispatch: Dispatch,
rx: mpsc::Receiver<(i32, Buf)>, rx: mpsc::Receiver<(i32, Buf)>,
tx: mpsc::Sender<(i32, Buf)>,
ntasks: i32, ntasks: i32,
pub timeout_due: Option<Instant>, pub timeout_due: Option<Instant>,
pub state: Arc<IsolateState>, pub state: Arc<IsolateState>,
} }
// Isolate cannot be passed between threads but IsolateState can. So any state that // Isolate cannot be passed between threads but IsolateState can.
// needs to be accessed outside the main V8 thread should be inside IsolateState. // IsolateState satisfies Send and Sync.
// So any state that needs to be accessed outside the main V8 thread should be
// inside IsolateState.
#[cfg_attr(feature = "cargo-clippy", allow(stutter))] #[cfg_attr(feature = "cargo-clippy", allow(stutter))]
pub struct IsolateState { pub struct IsolateState {
pub dir: deno_dir::DenoDir, pub dir: deno_dir::DenoDir,
pub argv: Vec<String>, pub argv: Vec<String>,
pub permissions: Mutex<DenoPermissions>, pub permissions: DenoPermissions,
pub flags: flags::DenoFlags, pub flags: flags::DenoFlags,
tx: Mutex<Option<mpsc::Sender<(i32, Buf)>>>, pub metrics: Metrics,
pub metrics: Mutex<Metrics>,
} }
impl IsolateState { impl IsolateState {
// Thread safe. pub fn new(flags: flags::DenoFlags, argv_rest: Vec<String>) -> Self {
fn send_to_js(&self, req_id: i32, buf: Buf) { let custom_root = env::var("DENO_DIR").map(|s| s.into()).ok();
let mut g = self.tx.lock().unwrap(); IsolateState {
let maybe_tx = g.as_mut(); dir: deno_dir::DenoDir::new(flags.reload, custom_root).unwrap(),
assert!(maybe_tx.is_some(), "Expected tx to not be deleted."); argv: argv_rest,
let tx = maybe_tx.unwrap(); permissions: DenoPermissions::new(&flags),
tx.send((req_id, buf)).expect("tx.send error"); flags,
metrics: Metrics::default(),
}
} }
pub fn check_write(&self, filename: &str) -> DenoResult<()> { pub fn check_write(&self, filename: &str) -> DenoResult<()> {
let mut perm = self.permissions.lock().unwrap(); self.permissions.check_write(filename)
perm.check_write(filename)
} }
pub fn check_env(&self) -> DenoResult<()> { pub fn check_env(&self) -> DenoResult<()> {
let mut perm = self.permissions.lock().unwrap(); self.permissions.check_env()
perm.check_env()
} }
pub fn check_net(&self, filename: &str) -> DenoResult<()> { pub fn check_net(&self, filename: &str) -> DenoResult<()> {
let mut perm = self.permissions.lock().unwrap(); self.permissions.check_net(filename)
perm.check_net(filename)
} }
pub fn check_run(&self) -> DenoResult<()> { pub fn check_run(&self) -> DenoResult<()> {
let mut perm = self.permissions.lock().unwrap(); self.permissions.check_run()
perm.check_run()
} }
fn metrics_op_dispatched( fn metrics_op_dispatched(
&self, &self,
bytes_sent_control: u64, bytes_sent_control: usize,
bytes_sent_data: u64, bytes_sent_data: usize,
) { ) {
let mut metrics = self.metrics.lock().unwrap(); self.metrics.ops_dispatched.fetch_add(1, Ordering::SeqCst);
metrics.ops_dispatched += 1; self
metrics.bytes_sent_control += bytes_sent_control; .metrics
metrics.bytes_sent_data += bytes_sent_data; .bytes_sent_control
.fetch_add(bytes_sent_control, Ordering::SeqCst);
self
.metrics
.bytes_sent_data
.fetch_add(bytes_sent_data, Ordering::SeqCst);
} }
fn metrics_op_completed(&self, bytes_received: u64) { fn metrics_op_completed(&self, bytes_received: usize) {
let mut metrics = self.metrics.lock().unwrap(); self.metrics.ops_completed.fetch_add(1, Ordering::SeqCst);
metrics.ops_completed += 1; self
metrics.bytes_received += bytes_received; .metrics
.bytes_received
.fetch_add(bytes_received, Ordering::SeqCst);
} }
} }
// AtomicU64 is currently unstable
#[derive(Default)] #[derive(Default)]
pub struct Metrics { pub struct Metrics {
pub ops_dispatched: u64, pub ops_dispatched: AtomicUsize,
pub ops_completed: u64, pub ops_completed: AtomicUsize,
pub bytes_sent_control: u64, pub bytes_sent_control: AtomicUsize,
pub bytes_sent_data: u64, pub bytes_sent_data: AtomicUsize,
pub bytes_received: u64, pub bytes_received: AtomicUsize,
} }
static DENO_INIT: std::sync::Once = std::sync::ONCE_INIT; static DENO_INIT: std::sync::Once = std::sync::ONCE_INIT;
fn empty() -> libdeno::deno_buf {
libdeno::deno_buf {
alloc_ptr: std::ptr::null_mut(),
alloc_len: 0,
data_ptr: std::ptr::null_mut(),
data_len: 0,
}
}
impl Isolate { impl Isolate {
pub fn new( pub fn new(
snapshot: libdeno::deno_buf, snapshot: libdeno::deno_buf,
flags: flags::DenoFlags, state: Arc<IsolateState>,
argv_rest: Vec<String>,
dispatch: Dispatch, dispatch: Dispatch,
) -> Self { ) -> Self {
DENO_INIT.call_once(|| { DENO_INIT.call_once(|| {
unsafe { libdeno::deno_init() }; unsafe { libdeno::deno_init() };
}); });
let shared = empty(); // TODO Use shared for message passing. let shared = libdeno::deno_buf::empty(); // TODO Use shared for message passing.
let libdeno_isolate = let libdeno_isolate =
unsafe { libdeno::deno_new(snapshot, shared, pre_dispatch) }; unsafe { libdeno::deno_new(snapshot, shared, pre_dispatch) };
// This channel handles sending async messages back to the runtime. // This channel handles sending async messages back to the runtime.
let (tx, rx) = mpsc::channel::<(i32, Buf)>(); let (tx, rx) = mpsc::channel::<(i32, Buf)>();
let custom_root_path;
let custom_root = match env::var("DENO_DIR") {
Ok(path) => {
custom_root_path = path;
Some(Path::new(custom_root_path.as_str()))
}
Err(_e) => None,
};
Self { Self {
libdeno_isolate, libdeno_isolate,
dispatch, dispatch,
rx, rx,
tx,
ntasks: 0, ntasks: 0,
timeout_due: None, timeout_due: None,
state: Arc::new(IsolateState { state: state,
dir: deno_dir::DenoDir::new(flags.reload, custom_root).unwrap(),
argv: argv_rest,
permissions: Mutex::new(DenoPermissions::new(&flags)),
flags,
tx: Mutex::new(Some(tx)),
metrics: Mutex::new(Metrics::default()),
}),
} }
} }
@ -207,7 +189,7 @@ impl Isolate {
} }
pub fn respond(&mut self, req_id: i32, buf: Buf) { pub fn respond(&mut self, req_id: i32, buf: Buf) {
self.state.metrics_op_completed(buf.len() as u64); self.state.metrics_op_completed(buf.len());
// TODO(zero-copy) Use Buf::leak(buf) to leak the heap allocated buf. And // TODO(zero-copy) Use Buf::leak(buf) to leak the heap allocated buf. And
// don't do the memcpy in ImportBuf() (in libdeno/binding.cc) // don't do the memcpy in ImportBuf() (in libdeno/binding.cc)
@ -311,8 +293,8 @@ extern "C" fn pre_dispatch(
data_buf: libdeno::deno_buf, data_buf: libdeno::deno_buf,
) { ) {
// for metrics // for metrics
let bytes_sent_control = control_buf.data_len as u64; let bytes_sent_control = control_buf.data_len;
let bytes_sent_data = data_buf.data_len as u64; let bytes_sent_data = data_buf.data_len;
// control_buf is only valid for the lifetime of this call, thus is // control_buf is only valid for the lifetime of this call, thus is
// interpretted as a slice. // interpretted as a slice.
@ -344,14 +326,14 @@ extern "C" fn pre_dispatch(
if buf_size == 0 { if buf_size == 0 {
// FIXME // FIXME
isolate.state.metrics_op_completed(buf.len() as u64); isolate.state.metrics_op_completed(buf.len());
} else { } else {
// Set the synchronous response, the value returned from isolate.send(). // Set the synchronous response, the value returned from isolate.send().
isolate.respond(req_id, buf); isolate.respond(req_id, buf);
} }
} else { } else {
// Execute op asynchronously. // Execute op asynchronously.
let state = Arc::clone(&isolate.state); let tx = isolate.tx.clone();
// TODO Ideally Tokio would could tell us how many tasks are executing, but // TODO Ideally Tokio would could tell us how many tasks are executing, but
// it cannot currently. Therefore we track top-level promises/tasks // it cannot currently. Therefore we track top-level promises/tasks
@ -360,7 +342,8 @@ extern "C" fn pre_dispatch(
let task = op let task = op
.and_then(move |buf| { .and_then(move |buf| {
state.send_to_js(req_id, buf); let sender = tx; // tx is moved to new thread
sender.send((req_id, buf)).expect("tx.send error");
Ok(()) Ok(())
}).map_err(|_| ()); }).map_err(|_| ());
tokio::spawn(task); tokio::spawn(task);
@ -398,7 +381,10 @@ mod tests {
fn test_dispatch_sync() { fn test_dispatch_sync() {
let argv = vec![String::from("./deno"), String::from("hello.js")]; let argv = vec![String::from("./deno"), String::from("hello.js")];
let (flags, rest_argv, _) = flags::set_flags(argv).unwrap(); let (flags, rest_argv, _) = flags::set_flags(argv).unwrap();
let mut isolate = Isolate::new(empty(), flags, rest_argv, dispatch_sync);
let state = Arc::new(IsolateState::new(flags, rest_argv));
let snapshot = libdeno::deno_buf::empty();
let mut isolate = Isolate::new(snapshot, state, dispatch_sync);
tokio_util::init(|| { tokio_util::init(|| {
isolate isolate
.execute( .execute(
@ -438,17 +424,18 @@ mod tests {
fn test_metrics_sync() { fn test_metrics_sync() {
let argv = vec![String::from("./deno"), String::from("hello.js")]; let argv = vec![String::from("./deno"), String::from("hello.js")];
let (flags, rest_argv, _) = flags::set_flags(argv).unwrap(); let (flags, rest_argv, _) = flags::set_flags(argv).unwrap();
let mut isolate = let state = Arc::new(IsolateState::new(flags, rest_argv));
Isolate::new(empty(), flags, rest_argv, metrics_dispatch_sync); let snapshot = libdeno::deno_buf::empty();
let mut isolate = Isolate::new(snapshot, state, metrics_dispatch_sync);
tokio_util::init(|| { tokio_util::init(|| {
// Verify that metrics have been properly initialized. // Verify that metrics have been properly initialized.
{ {
let metrics = isolate.state.metrics.lock().unwrap(); let metrics = &isolate.state.metrics;
assert_eq!(metrics.ops_dispatched, 0); assert_eq!(metrics.ops_dispatched.load(Ordering::SeqCst), 0);
assert_eq!(metrics.ops_completed, 0); assert_eq!(metrics.ops_completed.load(Ordering::SeqCst), 0);
assert_eq!(metrics.bytes_sent_control, 0); assert_eq!(metrics.bytes_sent_control.load(Ordering::SeqCst), 0);
assert_eq!(metrics.bytes_sent_data, 0); assert_eq!(metrics.bytes_sent_data.load(Ordering::SeqCst), 0);
assert_eq!(metrics.bytes_received, 0); assert_eq!(metrics.bytes_received.load(Ordering::SeqCst), 0);
} }
isolate isolate
@ -461,12 +448,12 @@ mod tests {
"#, "#,
).expect("execute error"); ).expect("execute error");
isolate.event_loop(); isolate.event_loop();
let metrics = isolate.state.metrics.lock().unwrap(); let metrics = &isolate.state.metrics;
assert_eq!(metrics.ops_dispatched, 1); assert_eq!(metrics.ops_dispatched.load(Ordering::SeqCst), 1);
assert_eq!(metrics.ops_completed, 1); assert_eq!(metrics.ops_completed.load(Ordering::SeqCst), 1);
assert_eq!(metrics.bytes_sent_control, 3); assert_eq!(metrics.bytes_sent_control.load(Ordering::SeqCst), 3);
assert_eq!(metrics.bytes_sent_data, 5); assert_eq!(metrics.bytes_sent_data.load(Ordering::SeqCst), 5);
assert_eq!(metrics.bytes_received, 4); assert_eq!(metrics.bytes_received.load(Ordering::SeqCst), 4);
}); });
} }
@ -474,17 +461,18 @@ mod tests {
fn test_metrics_async() { fn test_metrics_async() {
let argv = vec![String::from("./deno"), String::from("hello.js")]; let argv = vec![String::from("./deno"), String::from("hello.js")];
let (flags, rest_argv, _) = flags::set_flags(argv).unwrap(); let (flags, rest_argv, _) = flags::set_flags(argv).unwrap();
let mut isolate = let state = Arc::new(IsolateState::new(flags, rest_argv));
Isolate::new(empty(), flags, rest_argv, metrics_dispatch_async); let snapshot = libdeno::deno_buf::empty();
let mut isolate = Isolate::new(snapshot, state, metrics_dispatch_async);
tokio_util::init(|| { tokio_util::init(|| {
// Verify that metrics have been properly initialized. // Verify that metrics have been properly initialized.
{ {
let metrics = isolate.state.metrics.lock().unwrap(); let metrics = &isolate.state.metrics;
assert_eq!(metrics.ops_dispatched, 0); assert_eq!(metrics.ops_dispatched.load(Ordering::SeqCst), 0);
assert_eq!(metrics.ops_completed, 0); assert_eq!(metrics.ops_completed.load(Ordering::SeqCst), 0);
assert_eq!(metrics.bytes_sent_control, 0); assert_eq!(metrics.bytes_sent_control.load(Ordering::SeqCst), 0);
assert_eq!(metrics.bytes_sent_data, 0); assert_eq!(metrics.bytes_sent_data.load(Ordering::SeqCst), 0);
assert_eq!(metrics.bytes_received, 0); assert_eq!(metrics.bytes_received.load(Ordering::SeqCst), 0);
} }
isolate isolate
@ -500,10 +488,10 @@ mod tests {
// Make sure relevant metrics are updated before task is executed. // Make sure relevant metrics are updated before task is executed.
{ {
let metrics = isolate.state.metrics.lock().unwrap(); let metrics = &isolate.state.metrics;
assert_eq!(metrics.ops_dispatched, 1); assert_eq!(metrics.ops_dispatched.load(Ordering::SeqCst), 1);
assert_eq!(metrics.bytes_sent_control, 3); assert_eq!(metrics.bytes_sent_control.load(Ordering::SeqCst), 3);
assert_eq!(metrics.bytes_sent_data, 5); assert_eq!(metrics.bytes_sent_data.load(Ordering::SeqCst), 5);
// Note we cannot check ops_completed nor bytes_received because that // Note we cannot check ops_completed nor bytes_received because that
// would be a race condition. It might be nice to have use a oneshot // would be a race condition. It might be nice to have use a oneshot
// with metrics_dispatch_async() to properly validate them. // with metrics_dispatch_async() to properly validate them.
@ -513,12 +501,12 @@ mod tests {
// Make sure relevant metrics are updated after task is executed. // Make sure relevant metrics are updated after task is executed.
{ {
let metrics = isolate.state.metrics.lock().unwrap(); let metrics = &isolate.state.metrics;
assert_eq!(metrics.ops_dispatched, 1); assert_eq!(metrics.ops_dispatched.load(Ordering::SeqCst), 1);
assert_eq!(metrics.ops_completed, 1); assert_eq!(metrics.ops_completed.load(Ordering::SeqCst), 1);
assert_eq!(metrics.bytes_sent_control, 3); assert_eq!(metrics.bytes_sent_control.load(Ordering::SeqCst), 3);
assert_eq!(metrics.bytes_sent_data, 5); assert_eq!(metrics.bytes_sent_data.load(Ordering::SeqCst), 5);
assert_eq!(metrics.bytes_received, 4); assert_eq!(metrics.bytes_received.load(Ordering::SeqCst), 4);
} }
}); });
} }

View file

@ -2,6 +2,7 @@
use libc::c_char; use libc::c_char;
use libc::c_int; use libc::c_int;
use libc::c_void; use libc::c_void;
use std::ptr::null_mut;
#[repr(C)] #[repr(C)]
pub struct isolate { pub struct isolate {
@ -17,6 +18,17 @@ pub struct deno_buf {
pub data_len: usize, pub data_len: usize,
} }
impl deno_buf {
pub fn empty() -> Self {
deno_buf {
alloc_ptr: null_mut(),
alloc_len: 0,
data_ptr: null_mut(),
data_len: 0,
}
}
}
type DenoRecvCb = unsafe extern "C" fn( type DenoRecvCb = unsafe extern "C" fn(
user_data: *mut c_void, user_data: *mut c_void,
req_id: i32, req_id: i32,

View file

@ -49,6 +49,7 @@ pub mod version;
mod eager_unix; mod eager_unix;
use std::env; use std::env;
use std::sync::Arc;
static LOGGER: Logger = Logger; static LOGGER: Logger = Logger;
@ -95,12 +96,9 @@ fn main() {
log::LevelFilter::Info log::LevelFilter::Info
}); });
let mut isolate = isolate::Isolate::new( let state = Arc::new(isolate::IsolateState::new(flags, rest_argv));
unsafe { snapshot::deno_snapshot.clone() }, let snapshot = unsafe { snapshot::deno_snapshot.clone() };
flags, let mut isolate = isolate::Isolate::new(snapshot, state, ops::dispatch);
rest_argv,
ops::dispatch,
);
tokio_util::init(|| { tokio_util::init(|| {
isolate isolate
.execute("deno_main.js", "denoMain();") .execute("deno_main.js", "denoMain();")

View file

@ -2,6 +2,20 @@
#![allow(dead_code)] #![allow(dead_code)]
#![cfg_attr(feature = "cargo-clippy", allow(clippy, pedantic))] #![cfg_attr(feature = "cargo-clippy", allow(clippy, pedantic))]
use flatbuffers; use flatbuffers;
use std::sync::atomic::Ordering;
// GN_OUT_DIR is set either by build.rs (for the Cargo build), or by // GN_OUT_DIR is set either by build.rs (for the Cargo build), or by
// build_extra/rust/run.py (for the GN+Ninja build). // build_extra/rust/run.py (for the GN+Ninja build).
include!(concat!(env!("GN_OUT_DIR"), "/gen/msg_generated.rs")); include!(concat!(env!("GN_OUT_DIR"), "/gen/msg_generated.rs"));
impl<'a> From<&'a super::isolate::Metrics> for MetricsResArgs {
fn from(m: &'a super::isolate::Metrics) -> Self {
MetricsResArgs {
ops_dispatched: m.ops_dispatched.load(Ordering::SeqCst) as u64,
ops_completed: m.ops_completed.load(Ordering::SeqCst) as u64,
bytes_sent_control: m.bytes_sent_control.load(Ordering::SeqCst) as u64,
bytes_sent_data: m.bytes_sent_data.load(Ordering::SeqCst) as u64,
bytes_received: m.bytes_received.load(Ordering::SeqCst) as u64,
}
}
}

View file

@ -23,6 +23,7 @@ use remove_dir_all::remove_dir_all;
use repl; use repl;
use resources::table_entries; use resources::table_entries;
use std; use std;
use std::convert::From;
use std::fs; use std::fs;
use std::net::{Shutdown, SocketAddr}; use std::net::{Shutdown, SocketAddr};
#[cfg(unix)] #[cfg(unix)]
@ -1291,18 +1292,10 @@ fn op_metrics(
assert_eq!(data.len(), 0); assert_eq!(data.len(), 0);
let cmd_id = base.cmd_id(); let cmd_id = base.cmd_id();
let metrics = state.metrics.lock().unwrap();
let builder = &mut FlatBufferBuilder::new(); let builder = &mut FlatBufferBuilder::new();
let inner = msg::MetricsRes::create( let inner = msg::MetricsRes::create(
builder, builder,
&msg::MetricsResArgs { &msg::MetricsResArgs::from(&state.metrics),
ops_dispatched: metrics.ops_dispatched,
ops_completed: metrics.ops_completed,
bytes_sent_control: metrics.bytes_sent_control,
bytes_sent_data: metrics.bytes_sent_data,
bytes_received: metrics.bytes_received,
},
); );
ok_future(serialize_response( ok_future(serialize_response(
cmd_id, cmd_id,

View file

@ -5,40 +5,41 @@ use flags::DenoFlags;
use errors::permission_denied; use errors::permission_denied;
use errors::DenoResult; use errors::DenoResult;
use std::io; use std::io;
use std::sync::atomic::{AtomicBool, Ordering};
#[cfg_attr(feature = "cargo-clippy", allow(stutter))] #[cfg_attr(feature = "cargo-clippy", allow(stutter))]
#[derive(Debug, Default, PartialEq)] #[derive(Debug, Default)]
pub struct DenoPermissions { pub struct DenoPermissions {
pub allow_write: bool, pub allow_write: AtomicBool,
pub allow_net: bool, pub allow_net: AtomicBool,
pub allow_env: bool, pub allow_env: AtomicBool,
pub allow_run: bool, pub allow_run: AtomicBool,
} }
impl DenoPermissions { impl DenoPermissions {
pub fn new(flags: &DenoFlags) -> Self { pub fn new(flags: &DenoFlags) -> Self {
Self { Self {
allow_write: flags.allow_write, allow_write: AtomicBool::new(flags.allow_write),
allow_env: flags.allow_env, allow_env: AtomicBool::new(flags.allow_env),
allow_net: flags.allow_net, allow_net: AtomicBool::new(flags.allow_net),
allow_run: flags.allow_run, allow_run: AtomicBool::new(flags.allow_run),
} }
} }
pub fn check_run(&mut self) -> DenoResult<()> { pub fn check_run(&self) -> DenoResult<()> {
if self.allow_run { if self.allow_run.load(Ordering::SeqCst) {
return Ok(()); return Ok(());
}; };
// TODO get location (where access occurred) // TODO get location (where access occurred)
let r = permission_prompt("Deno requests access to run a subprocess."); let r = permission_prompt("Deno requests access to run a subprocess.");
if r.is_ok() { if r.is_ok() {
self.allow_run = true; self.allow_run.store(true, Ordering::SeqCst);
} }
r r
} }
pub fn check_write(&mut self, filename: &str) -> DenoResult<()> { pub fn check_write(&self, filename: &str) -> DenoResult<()> {
if self.allow_write { if self.allow_write.load(Ordering::SeqCst) {
return Ok(()); return Ok(());
}; };
// TODO get location (where access occurred) // TODO get location (where access occurred)
@ -47,13 +48,13 @@ impl DenoPermissions {
filename filename
));; ));;
if r.is_ok() { if r.is_ok() {
self.allow_write = true; self.allow_write.store(true, Ordering::SeqCst);
} }
r r
} }
pub fn check_net(&mut self, domain_name: &str) -> DenoResult<()> { pub fn check_net(&self, domain_name: &str) -> DenoResult<()> {
if self.allow_net { if self.allow_net.load(Ordering::SeqCst) {
return Ok(()); return Ok(());
}; };
// TODO get location (where access occurred) // TODO get location (where access occurred)
@ -62,20 +63,20 @@ impl DenoPermissions {
domain_name domain_name
)); ));
if r.is_ok() { if r.is_ok() {
self.allow_net = true; self.allow_net.store(true, Ordering::SeqCst);
} }
r r
} }
pub fn check_env(&mut self) -> DenoResult<()> { pub fn check_env(&self) -> DenoResult<()> {
if self.allow_env { if self.allow_env.load(Ordering::SeqCst) {
return Ok(()); return Ok(());
}; };
// TODO get location (where access occurred) // TODO get location (where access occurred)
let r = let r =
permission_prompt(&"Deno requests access to environment variables."); permission_prompt(&"Deno requests access to environment variables.");
if r.is_ok() { if r.is_ok() {
self.allow_env = true; self.allow_env.store(true, Ordering::SeqCst);
} }
r r
} }