2020-01-02 15:13:47 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2020-01-29 12:54:23 -05:00
|
|
|
use crate::compilers::TargetLib;
|
2020-02-06 23:05:02 -05:00
|
|
|
use crate::global_state::GlobalState;
|
2019-03-14 19:17:52 -04:00
|
|
|
use crate::global_timer::GlobalTimer;
|
2019-06-09 09:08:20 -04:00
|
|
|
use crate::import_map::ImportMap;
|
2019-11-04 10:38:52 -05:00
|
|
|
use crate::metrics::Metrics;
|
2020-02-23 14:51:29 -05:00
|
|
|
use crate::op_error::OpError;
|
2019-10-01 18:51:05 -04:00
|
|
|
use crate::ops::JsonOp;
|
2019-11-14 12:10:25 -05:00
|
|
|
use crate::ops::MinimalOp;
|
2019-03-19 20:55:59 -04:00
|
|
|
use crate::permissions::DenoPermissions;
|
2020-04-09 18:15:17 -04:00
|
|
|
use crate::web_worker::WebWorkerHandle;
|
2020-01-05 11:56:18 -05:00
|
|
|
use deno_core::Buf;
|
|
|
|
use deno_core::CoreOp;
|
|
|
|
use deno_core::ErrBox;
|
2020-03-02 13:12:49 -05:00
|
|
|
use deno_core::ModuleLoader;
|
2020-01-05 11:56:18 -05:00
|
|
|
use deno_core::ModuleSpecifier;
|
|
|
|
use deno_core::Op;
|
|
|
|
use deno_core::ResourceTable;
|
2020-01-24 15:10:49 -05:00
|
|
|
use deno_core::ZeroCopyBuf;
|
2019-11-16 19:17:47 -05:00
|
|
|
use futures::future::FutureExt;
|
|
|
|
use futures::future::TryFutureExt;
|
2019-06-11 10:34:39 -04:00
|
|
|
use rand::rngs::StdRng;
|
|
|
|
use rand::SeedableRng;
|
2019-10-01 18:51:05 -04:00
|
|
|
use serde_json::Value;
|
2020-02-08 14:34:31 -05:00
|
|
|
use std::cell::RefCell;
|
2019-04-01 15:09:59 -04:00
|
|
|
use std::collections::HashMap;
|
2019-04-09 13:11:25 -04:00
|
|
|
use std::ops::Deref;
|
2020-01-20 09:45:44 -05:00
|
|
|
use std::path::Path;
|
2019-11-16 19:17:47 -05:00
|
|
|
use std::pin::Pin;
|
2020-02-08 14:34:31 -05:00
|
|
|
use std::rc::Rc;
|
2019-07-31 13:16:03 -04:00
|
|
|
use std::str;
|
2020-02-18 14:47:11 -05:00
|
|
|
use std::thread::JoinHandle;
|
2019-04-08 16:22:40 -04:00
|
|
|
use std::time::Instant;
|
2019-03-14 19:17:52 -04:00
|
|
|
|
2020-04-03 13:40:11 -04:00
|
|
|
#[derive(Copy, Clone, Eq, PartialEq)]
|
|
|
|
pub enum DebugType {
|
|
|
|
/// Can be debugged, will wait for debugger when --inspect-brk given.
|
|
|
|
Main,
|
|
|
|
/// Can be debugged, never waits for debugger.
|
|
|
|
Dependent,
|
|
|
|
/// No inspector instance is created.
|
|
|
|
Internal,
|
|
|
|
}
|
|
|
|
|
2020-02-08 14:34:31 -05:00
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct State(Rc<RefCell<StateInner>>);
|
|
|
|
|
|
|
|
impl Deref for State {
|
|
|
|
type Target = Rc<RefCell<StateInner>>;
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
2019-04-09 13:11:25 -04:00
|
|
|
|
2019-03-14 19:17:52 -04:00
|
|
|
#[cfg_attr(feature = "cargo-clippy", allow(stutter))]
|
2020-02-08 14:34:31 -05:00
|
|
|
pub struct StateInner {
|
2020-02-06 23:05:02 -05:00
|
|
|
pub global_state: GlobalState,
|
2020-02-08 14:34:31 -05:00
|
|
|
pub permissions: DenoPermissions,
|
2020-02-04 14:24:33 -05:00
|
|
|
pub main_module: ModuleSpecifier,
|
2019-06-09 09:08:20 -04:00
|
|
|
/// When flags contains a `.import_map_path` option, the content of the
|
|
|
|
/// import map file will be resolved and set.
|
|
|
|
pub import_map: Option<ImportMap>,
|
2019-03-14 19:17:52 -04:00
|
|
|
pub metrics: Metrics,
|
2020-02-08 14:34:31 -05:00
|
|
|
pub global_timer: GlobalTimer,
|
2020-04-09 18:15:17 -04:00
|
|
|
pub workers: HashMap<u32, (JoinHandle<()>, WebWorkerHandle)>,
|
2020-02-18 14:47:11 -05:00
|
|
|
pub next_worker_id: u32,
|
2019-04-08 16:22:40 -04:00
|
|
|
pub start_time: Instant,
|
2020-02-08 14:34:31 -05:00
|
|
|
pub seeded_rng: Option<StdRng>,
|
|
|
|
pub resource_table: ResourceTable,
|
2020-01-29 12:54:23 -05:00
|
|
|
pub target_lib: TargetLib,
|
2020-04-03 13:40:11 -04:00
|
|
|
pub debug_type: DebugType,
|
2019-03-14 19:17:52 -04:00
|
|
|
}
|
|
|
|
|
2020-02-08 14:34:31 -05:00
|
|
|
impl State {
|
2020-02-25 09:14:27 -05:00
|
|
|
pub fn stateful_json_op<D>(
|
|
|
|
&self,
|
|
|
|
dispatcher: D,
|
|
|
|
) -> impl Fn(&[u8], Option<ZeroCopyBuf>) -> CoreOp
|
|
|
|
where
|
|
|
|
D: Fn(&State, Value, Option<ZeroCopyBuf>) -> Result<JsonOp, OpError>,
|
|
|
|
{
|
|
|
|
use crate::ops::json_op;
|
|
|
|
self.core_op(json_op(self.stateful_op(dispatcher)))
|
|
|
|
}
|
|
|
|
|
2019-10-01 18:51:05 -04:00
|
|
|
/// Wrap core `OpDispatcher` to collect metrics.
|
2019-10-11 14:41:54 -04:00
|
|
|
pub fn core_op<D>(
|
2019-06-17 21:02:08 -04:00
|
|
|
&self,
|
2019-10-01 18:51:05 -04:00
|
|
|
dispatcher: D,
|
2020-01-24 15:10:49 -05:00
|
|
|
) -> impl Fn(&[u8], Option<ZeroCopyBuf>) -> CoreOp
|
2019-10-01 18:51:05 -04:00
|
|
|
where
|
2020-01-24 15:10:49 -05:00
|
|
|
D: Fn(&[u8], Option<ZeroCopyBuf>) -> CoreOp,
|
2019-10-01 18:51:05 -04:00
|
|
|
{
|
|
|
|
let state = self.clone();
|
|
|
|
|
2020-01-24 15:10:49 -05:00
|
|
|
move |control: &[u8], zero_copy: Option<ZeroCopyBuf>| -> CoreOp {
|
2020-02-11 11:23:40 -05:00
|
|
|
let bytes_sent_control = control.len() as u64;
|
2019-10-01 18:51:05 -04:00
|
|
|
let bytes_sent_zero_copy =
|
2020-02-11 11:23:40 -05:00
|
|
|
zero_copy.as_ref().map(|b| b.len()).unwrap_or(0) as u64;
|
2019-10-01 18:51:05 -04:00
|
|
|
|
|
|
|
let op = dispatcher(control, zero_copy);
|
|
|
|
|
|
|
|
match op {
|
|
|
|
Op::Sync(buf) => {
|
2020-03-02 13:13:36 -05:00
|
|
|
let mut state_ = state.borrow_mut();
|
|
|
|
state_.metrics.op_sync(
|
|
|
|
bytes_sent_control,
|
|
|
|
bytes_sent_zero_copy,
|
|
|
|
buf.len() as u64,
|
|
|
|
);
|
2019-10-01 18:51:05 -04:00
|
|
|
Op::Sync(buf)
|
|
|
|
}
|
|
|
|
Op::Async(fut) => {
|
2020-03-02 13:13:36 -05:00
|
|
|
let mut state_ = state.borrow_mut();
|
|
|
|
state_
|
|
|
|
.metrics
|
|
|
|
.op_dispatched_async(bytes_sent_control, bytes_sent_zero_copy);
|
2019-10-01 18:51:05 -04:00
|
|
|
let state = state.clone();
|
2019-11-16 19:17:47 -05:00
|
|
|
let result_fut = fut.map_ok(move |buf: Buf| {
|
2020-03-02 13:13:36 -05:00
|
|
|
let mut state_ = state.borrow_mut();
|
|
|
|
state_.metrics.op_completed_async(buf.len() as u64);
|
2019-10-01 18:51:05 -04:00
|
|
|
buf
|
2019-11-16 19:17:47 -05:00
|
|
|
});
|
2020-02-03 18:08:44 -05:00
|
|
|
Op::Async(result_fut.boxed_local())
|
2019-10-01 18:51:05 -04:00
|
|
|
}
|
2020-01-21 12:01:10 -05:00
|
|
|
Op::AsyncUnref(fut) => {
|
2020-03-02 13:13:36 -05:00
|
|
|
let mut state_ = state.borrow_mut();
|
|
|
|
state_.metrics.op_dispatched_async_unref(
|
|
|
|
bytes_sent_control,
|
|
|
|
bytes_sent_zero_copy,
|
|
|
|
);
|
2020-01-21 12:01:10 -05:00
|
|
|
let state = state.clone();
|
|
|
|
let result_fut = fut.map_ok(move |buf: Buf| {
|
2020-03-02 13:13:36 -05:00
|
|
|
let mut state_ = state.borrow_mut();
|
|
|
|
state_.metrics.op_completed_async_unref(buf.len() as u64);
|
2020-01-21 12:01:10 -05:00
|
|
|
buf
|
|
|
|
});
|
2020-02-03 18:08:44 -05:00
|
|
|
Op::AsyncUnref(result_fut.boxed_local())
|
2020-01-21 12:01:10 -05:00
|
|
|
}
|
2019-10-01 18:51:05 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-14 12:10:25 -05:00
|
|
|
/// This is a special function that provides `state` argument to dispatcher.
|
|
|
|
pub fn stateful_minimal_op<D>(
|
|
|
|
&self,
|
|
|
|
dispatcher: D,
|
2020-01-24 15:10:49 -05:00
|
|
|
) -> impl Fn(i32, Option<ZeroCopyBuf>) -> Pin<Box<MinimalOp>>
|
2019-11-14 12:10:25 -05:00
|
|
|
where
|
2020-02-08 14:34:31 -05:00
|
|
|
D: Fn(&State, i32, Option<ZeroCopyBuf>) -> Pin<Box<MinimalOp>>,
|
2019-11-14 12:10:25 -05:00
|
|
|
{
|
|
|
|
let state = self.clone();
|
|
|
|
|
2020-01-24 15:10:49 -05:00
|
|
|
move |rid: i32, zero_copy: Option<ZeroCopyBuf>| -> Pin<Box<MinimalOp>> {
|
2019-11-14 12:10:25 -05:00
|
|
|
dispatcher(&state, rid, zero_copy)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-01 18:51:05 -04:00
|
|
|
/// This is a special function that provides `state` argument to dispatcher.
|
|
|
|
///
|
|
|
|
/// NOTE: This only works with JSON dispatcher.
|
|
|
|
/// This is a band-aid for transition to `Isolate.register_op` API as most of our
|
|
|
|
/// ops require `state` argument.
|
|
|
|
pub fn stateful_op<D>(
|
|
|
|
&self,
|
|
|
|
dispatcher: D,
|
2020-02-23 14:51:29 -05:00
|
|
|
) -> impl Fn(Value, Option<ZeroCopyBuf>) -> Result<JsonOp, OpError>
|
2019-10-01 18:51:05 -04:00
|
|
|
where
|
2020-02-23 14:51:29 -05:00
|
|
|
D: Fn(&State, Value, Option<ZeroCopyBuf>) -> Result<JsonOp, OpError>,
|
2019-10-01 18:51:05 -04:00
|
|
|
{
|
|
|
|
let state = self.clone();
|
|
|
|
|
2020-01-24 15:10:49 -05:00
|
|
|
move |args: Value,
|
|
|
|
zero_copy: Option<ZeroCopyBuf>|
|
2020-02-23 14:51:29 -05:00
|
|
|
-> Result<JsonOp, OpError> { dispatcher(&state, args, zero_copy) }
|
2019-04-09 13:11:25 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-02 13:12:49 -05:00
|
|
|
impl ModuleLoader for State {
|
2019-06-09 09:08:20 -04:00
|
|
|
fn resolve(
|
|
|
|
&self,
|
|
|
|
specifier: &str,
|
|
|
|
referrer: &str,
|
2019-08-07 12:55:39 -04:00
|
|
|
is_main: bool,
|
2019-07-10 18:53:48 -04:00
|
|
|
) -> Result<ModuleSpecifier, ErrBox> {
|
2019-08-07 12:55:39 -04:00
|
|
|
if !is_main {
|
2020-02-08 14:34:31 -05:00
|
|
|
if let Some(import_map) = &self.borrow().import_map {
|
2019-06-12 15:00:08 -04:00
|
|
|
let result = import_map.resolve(specifier, referrer)?;
|
2019-10-03 09:16:06 -04:00
|
|
|
if let Some(r) = result {
|
|
|
|
return Ok(r);
|
2019-06-09 09:08:20 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-08-13 14:51:15 -04:00
|
|
|
let module_specifier =
|
|
|
|
ModuleSpecifier::resolve_import(specifier, referrer)?;
|
2019-06-09 09:08:20 -04:00
|
|
|
|
2019-08-13 14:51:15 -04:00
|
|
|
Ok(module_specifier)
|
2019-06-05 16:35:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Given an absolute url, load its source code.
|
2019-06-12 19:55:59 -04:00
|
|
|
fn load(
|
|
|
|
&self,
|
|
|
|
module_specifier: &ModuleSpecifier,
|
2019-11-25 09:33:23 -05:00
|
|
|
maybe_referrer: Option<ModuleSpecifier>,
|
2020-01-25 12:53:16 -05:00
|
|
|
is_dyn_import: bool,
|
2020-03-02 13:12:49 -05:00
|
|
|
) -> Pin<Box<deno_core::ModuleSourceFuture>> {
|
2020-02-03 18:08:44 -05:00
|
|
|
let module_specifier = module_specifier.clone();
|
2020-01-25 12:53:16 -05:00
|
|
|
if is_dyn_import {
|
|
|
|
if let Err(e) = self.check_dyn_import(&module_specifier) {
|
2020-02-23 14:51:29 -05:00
|
|
|
return async move { Err(e.into()) }.boxed_local();
|
2020-01-25 12:53:16 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-11 11:23:40 -05:00
|
|
|
let mut state = self.borrow_mut();
|
2020-01-25 12:53:16 -05:00
|
|
|
// TODO(bartlomieju): incrementing resolve_count here has no sense...
|
2020-02-11 11:23:40 -05:00
|
|
|
state.metrics.resolve_count += 1;
|
2019-08-07 12:55:39 -04:00
|
|
|
let module_url_specified = module_specifier.to_string();
|
2020-02-08 14:34:31 -05:00
|
|
|
let global_state = state.global_state.clone();
|
|
|
|
let target_lib = state.target_lib.clone();
|
2020-02-03 18:08:44 -05:00
|
|
|
let fut = async move {
|
|
|
|
let compiled_module = global_state
|
|
|
|
.fetch_compiled_module(module_specifier, maybe_referrer, target_lib)
|
|
|
|
.await?;
|
2020-03-02 13:12:49 -05:00
|
|
|
Ok(deno_core::ModuleSource {
|
2019-07-31 13:16:03 -04:00
|
|
|
// Real module name, might be different from initial specifier
|
|
|
|
// due to redirections.
|
|
|
|
code: compiled_module.code,
|
2019-08-07 12:55:39 -04:00
|
|
|
module_url_specified,
|
|
|
|
module_url_found: compiled_module.name,
|
2020-02-03 18:08:44 -05:00
|
|
|
})
|
|
|
|
};
|
2019-11-04 10:38:52 -05:00
|
|
|
|
2020-02-03 18:08:44 -05:00
|
|
|
fut.boxed_local()
|
2019-06-05 16:35:38 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-08 14:34:31 -05:00
|
|
|
impl State {
|
2020-01-29 12:54:23 -05:00
|
|
|
/// If `shared_permission` is None then permissions from globa state are used.
|
2019-11-09 15:07:14 -05:00
|
|
|
pub fn new(
|
2020-02-06 23:05:02 -05:00
|
|
|
global_state: GlobalState,
|
2020-02-08 14:34:31 -05:00
|
|
|
shared_permissions: Option<DenoPermissions>,
|
2020-02-04 14:24:33 -05:00
|
|
|
main_module: ModuleSpecifier,
|
2020-04-03 13:40:11 -04:00
|
|
|
debug_type: DebugType,
|
2019-11-09 15:07:14 -05:00
|
|
|
) -> Result<Self, ErrBox> {
|
2019-11-04 10:38:52 -05:00
|
|
|
let import_map: Option<ImportMap> =
|
|
|
|
match global_state.flags.import_map_path.as_ref() {
|
|
|
|
None => None,
|
|
|
|
Some(file_path) => Some(ImportMap::load(file_path)?),
|
|
|
|
};
|
2019-06-11 14:35:03 -04:00
|
|
|
|
2019-11-04 10:38:52 -05:00
|
|
|
let seeded_rng = match global_state.flags.seed {
|
2020-02-08 14:34:31 -05:00
|
|
|
Some(seed) => Some(StdRng::seed_from_u64(seed)),
|
2019-11-04 10:38:52 -05:00
|
|
|
None => None,
|
2019-11-03 10:39:27 -05:00
|
|
|
};
|
|
|
|
|
2019-11-24 10:42:30 -05:00
|
|
|
let permissions = if let Some(perm) = shared_permissions {
|
|
|
|
perm
|
|
|
|
} else {
|
2020-02-08 14:34:31 -05:00
|
|
|
global_state.permissions.clone()
|
2019-11-24 10:42:30 -05:00
|
|
|
};
|
2019-11-04 10:38:52 -05:00
|
|
|
|
2020-02-08 14:34:31 -05:00
|
|
|
let state = Rc::new(RefCell::new(StateInner {
|
2019-11-04 10:38:52 -05:00
|
|
|
global_state,
|
|
|
|
main_module,
|
|
|
|
permissions,
|
2019-06-09 09:08:20 -04:00
|
|
|
import_map,
|
2019-11-04 10:38:52 -05:00
|
|
|
metrics: Metrics::default(),
|
2020-02-08 14:34:31 -05:00
|
|
|
global_timer: GlobalTimer::new(),
|
|
|
|
workers: HashMap::new(),
|
2020-02-18 14:47:11 -05:00
|
|
|
next_worker_id: 0,
|
2019-04-08 16:22:40 -04:00
|
|
|
start_time: Instant::now(),
|
2019-06-11 10:34:39 -04:00
|
|
|
seeded_rng,
|
2020-02-08 14:34:31 -05:00
|
|
|
resource_table: ResourceTable::default(),
|
2020-01-29 12:54:23 -05:00
|
|
|
target_lib: TargetLib::Main,
|
2020-04-03 13:40:11 -04:00
|
|
|
debug_type,
|
2020-02-08 14:34:31 -05:00
|
|
|
}));
|
2020-01-29 12:54:23 -05:00
|
|
|
|
2020-02-08 14:34:31 -05:00
|
|
|
Ok(Self(state))
|
2020-01-29 12:54:23 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// If `shared_permission` is None then permissions from globa state are used.
|
|
|
|
pub fn new_for_worker(
|
2020-02-06 23:05:02 -05:00
|
|
|
global_state: GlobalState,
|
2020-02-08 14:34:31 -05:00
|
|
|
shared_permissions: Option<DenoPermissions>,
|
2020-02-04 14:24:33 -05:00
|
|
|
main_module: ModuleSpecifier,
|
2020-01-29 12:54:23 -05:00
|
|
|
) -> Result<Self, ErrBox> {
|
|
|
|
let seeded_rng = match global_state.flags.seed {
|
2020-02-08 14:34:31 -05:00
|
|
|
Some(seed) => Some(StdRng::seed_from_u64(seed)),
|
2020-01-29 12:54:23 -05:00
|
|
|
None => None,
|
|
|
|
};
|
|
|
|
|
|
|
|
let permissions = if let Some(perm) = shared_permissions {
|
|
|
|
perm
|
|
|
|
} else {
|
2020-02-08 14:34:31 -05:00
|
|
|
global_state.permissions.clone()
|
2020-01-29 12:54:23 -05:00
|
|
|
};
|
|
|
|
|
2020-02-08 14:34:31 -05:00
|
|
|
let state = Rc::new(RefCell::new(StateInner {
|
2020-01-29 12:54:23 -05:00
|
|
|
global_state,
|
|
|
|
main_module,
|
|
|
|
permissions,
|
|
|
|
import_map: None,
|
|
|
|
metrics: Metrics::default(),
|
2020-02-08 14:34:31 -05:00
|
|
|
global_timer: GlobalTimer::new(),
|
|
|
|
workers: HashMap::new(),
|
2020-02-18 14:47:11 -05:00
|
|
|
next_worker_id: 0,
|
2020-01-29 12:54:23 -05:00
|
|
|
start_time: Instant::now(),
|
|
|
|
seeded_rng,
|
2020-02-08 14:34:31 -05:00
|
|
|
resource_table: ResourceTable::default(),
|
2020-01-29 12:54:23 -05:00
|
|
|
target_lib: TargetLib::Worker,
|
2020-04-03 13:40:11 -04:00
|
|
|
debug_type: DebugType::Dependent,
|
2020-02-08 14:34:31 -05:00
|
|
|
}));
|
2019-07-31 07:58:41 -04:00
|
|
|
|
2020-02-08 14:34:31 -05:00
|
|
|
Ok(Self(state))
|
2019-03-14 19:17:52 -04:00
|
|
|
}
|
|
|
|
|
2019-03-19 20:55:59 -04:00
|
|
|
#[inline]
|
2020-02-23 14:51:29 -05:00
|
|
|
pub fn check_read(&self, path: &Path) -> Result<(), OpError> {
|
2020-02-08 14:34:31 -05:00
|
|
|
self.borrow().permissions.check_read(path)
|
2019-03-19 20:55:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2020-02-23 14:51:29 -05:00
|
|
|
pub fn check_write(&self, path: &Path) -> Result<(), OpError> {
|
2020-02-08 14:34:31 -05:00
|
|
|
self.borrow().permissions.check_write(path)
|
2019-03-19 20:55:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2020-02-23 14:51:29 -05:00
|
|
|
pub fn check_env(&self) -> Result<(), OpError> {
|
2020-02-08 14:34:31 -05:00
|
|
|
self.borrow().permissions.check_env()
|
2019-03-19 20:55:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2020-02-23 14:51:29 -05:00
|
|
|
pub fn check_net(&self, hostname: &str, port: u16) -> Result<(), OpError> {
|
2020-02-08 14:34:31 -05:00
|
|
|
self.borrow().permissions.check_net(hostname, port)
|
2019-05-08 19:20:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2020-02-23 14:51:29 -05:00
|
|
|
pub fn check_net_url(&self, url: &url::Url) -> Result<(), OpError> {
|
2020-02-08 14:34:31 -05:00
|
|
|
self.borrow().permissions.check_net_url(url)
|
2019-03-19 20:55:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2020-02-23 14:51:29 -05:00
|
|
|
pub fn check_run(&self) -> Result<(), OpError> {
|
2020-02-08 14:34:31 -05:00
|
|
|
self.borrow().permissions.check_run()
|
2019-03-19 20:55:59 -04:00
|
|
|
}
|
|
|
|
|
2019-12-05 15:30:20 -05:00
|
|
|
#[inline]
|
2020-02-23 14:51:29 -05:00
|
|
|
pub fn check_plugin(&self, filename: &Path) -> Result<(), OpError> {
|
2020-02-08 14:34:31 -05:00
|
|
|
self.borrow().permissions.check_plugin(filename)
|
2019-12-05 15:30:20 -05:00
|
|
|
}
|
|
|
|
|
2019-08-13 14:51:15 -04:00
|
|
|
pub fn check_dyn_import(
|
2020-01-04 05:20:52 -05:00
|
|
|
&self,
|
2019-08-13 14:51:15 -04:00
|
|
|
module_specifier: &ModuleSpecifier,
|
2020-02-23 14:51:29 -05:00
|
|
|
) -> Result<(), OpError> {
|
2019-08-13 14:51:15 -04:00
|
|
|
let u = module_specifier.as_url();
|
|
|
|
match u.scheme() {
|
|
|
|
"http" | "https" => {
|
|
|
|
self.check_net_url(u)?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
"file" => {
|
2020-01-20 09:45:44 -05:00
|
|
|
let path = u
|
2019-08-13 14:51:15 -04:00
|
|
|
.to_file_path()
|
|
|
|
.unwrap()
|
|
|
|
.into_os_string()
|
|
|
|
.into_string()
|
|
|
|
.unwrap();
|
2020-01-20 09:45:44 -05:00
|
|
|
self.check_read(Path::new(&path))?;
|
2019-08-13 14:51:15 -04:00
|
|
|
Ok(())
|
|
|
|
}
|
2020-02-23 14:51:29 -05:00
|
|
|
_ => unreachable!(),
|
2019-08-13 14:51:15 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-14 19:17:52 -04:00
|
|
|
#[cfg(test)]
|
2020-02-08 14:34:31 -05:00
|
|
|
pub fn mock(main_module: &str) -> State {
|
2020-02-04 14:24:33 -05:00
|
|
|
let module_specifier = ModuleSpecifier::resolve_url_or_path(main_module)
|
|
|
|
.expect("Invalid entry module");
|
2020-02-08 14:34:31 -05:00
|
|
|
State::new(
|
2020-02-06 23:05:02 -05:00
|
|
|
GlobalState::mock(vec!["deno".to_string()]),
|
2019-11-24 10:42:30 -05:00
|
|
|
None,
|
2019-11-04 10:38:52 -05:00
|
|
|
module_specifier,
|
2020-04-03 13:40:11 -04:00
|
|
|
DebugType::Main,
|
2019-07-31 17:11:37 -04:00
|
|
|
)
|
|
|
|
.unwrap()
|
2019-03-14 19:17:52 -04:00
|
|
|
}
|
|
|
|
}
|