2020-01-02 15:13:47 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2020-05-07 09:57:10 -04:00
|
|
|
use crate::file_fetcher::SourceFileFetcher;
|
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;
|
2020-07-18 15:05:08 -04:00
|
|
|
use crate::http_util::create_http_client;
|
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-08-18 08:07:57 -04:00
|
|
|
use crate::ops::serialize_result;
|
2019-10-01 18:51:05 -04:00
|
|
|
use crate::ops::JsonOp;
|
2019-11-14 12:10:25 -05:00
|
|
|
use crate::ops::MinimalOp;
|
2020-05-04 14:10:59 -04:00
|
|
|
use crate::permissions::Permissions;
|
2020-05-08 10:18:00 -04:00
|
|
|
use crate::tsc::TargetLib;
|
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;
|
2020-08-18 08:07:57 -04:00
|
|
|
use deno_core::BufVec;
|
|
|
|
use deno_core::CoreIsolateState;
|
2020-01-05 11:56:18 -05:00
|
|
|
use deno_core::ErrBox;
|
2020-04-30 08:37:06 -04:00
|
|
|
use deno_core::ModuleLoadId;
|
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;
|
2020-08-18 08:07:57 -04:00
|
|
|
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;
|
2020-04-30 08:37:06 -04:00
|
|
|
use futures::Future;
|
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-08-18 12:30:13 -04:00
|
|
|
use std::cell::Cell;
|
2020-02-08 14:34:31 -05:00
|
|
|
use std::cell::RefCell;
|
2019-04-01 15:09:59 -04:00
|
|
|
use std::collections::HashMap;
|
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-08-18 12:30:13 -04:00
|
|
|
use std::sync::Arc;
|
2020-02-18 14:47:11 -05:00
|
|
|
use std::thread::JoinHandle;
|
2019-04-08 16:22:40 -04:00
|
|
|
use std::time::Instant;
|
2020-04-03 13:40:11 -04:00
|
|
|
|
2019-03-14 19:17:52 -04:00
|
|
|
#[cfg_attr(feature = "cargo-clippy", allow(stutter))]
|
2020-08-18 12:30:13 -04:00
|
|
|
pub struct State {
|
|
|
|
pub global_state: Arc<GlobalState>,
|
|
|
|
pub permissions: RefCell<Permissions>,
|
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>,
|
2020-08-18 12:30:13 -04:00
|
|
|
pub metrics: RefCell<Metrics>,
|
|
|
|
pub global_timer: RefCell<GlobalTimer>,
|
|
|
|
pub workers: RefCell<HashMap<u32, (JoinHandle<()>, WebWorkerHandle)>>,
|
|
|
|
pub next_worker_id: Cell<u32>,
|
2019-04-08 16:22:40 -04:00
|
|
|
pub start_time: Instant,
|
2020-08-18 12:30:13 -04:00
|
|
|
pub seeded_rng: Option<RefCell<StdRng>>,
|
2020-01-29 12:54:23 -05:00
|
|
|
pub target_lib: TargetLib,
|
2020-05-11 07:13:27 -04:00
|
|
|
pub is_main: bool,
|
2020-05-12 15:08:22 -04:00
|
|
|
pub is_internal: bool,
|
2020-08-18 12:30:13 -04:00
|
|
|
pub http_client: RefCell<reqwest::Client>,
|
2019-03-14 19:17:52 -04:00
|
|
|
}
|
|
|
|
|
2020-02-08 14:34:31 -05:00
|
|
|
impl State {
|
2020-08-12 10:44:58 -04:00
|
|
|
pub fn stateful_json_op<D>(
|
2020-08-18 12:30:13 -04:00
|
|
|
self: &Rc<Self>,
|
2020-08-12 10:44:58 -04:00
|
|
|
dispatcher: D,
|
|
|
|
) -> impl Fn(&mut deno_core::CoreIsolateState, &mut [ZeroCopyBuf]) -> Op
|
2020-02-25 09:14:27 -05:00
|
|
|
where
|
2020-08-25 18:22:15 -04:00
|
|
|
D: Fn(&Rc<State>, Value, &mut [ZeroCopyBuf]) -> Result<JsonOp, ErrBox>,
|
2020-02-25 09:14:27 -05:00
|
|
|
{
|
|
|
|
use crate::ops::json_op;
|
|
|
|
self.core_op(json_op(self.stateful_op(dispatcher)))
|
|
|
|
}
|
|
|
|
|
2020-08-18 08:07:57 -04:00
|
|
|
pub fn stateful_json_op_sync<D>(
|
2020-08-18 12:30:13 -04:00
|
|
|
self: &Rc<Self>,
|
2020-08-18 08:07:57 -04:00
|
|
|
resource_table: &Rc<RefCell<ResourceTable>>,
|
|
|
|
dispatcher: D,
|
|
|
|
) -> impl Fn(&mut deno_core::CoreIsolateState, &mut [ZeroCopyBuf]) -> Op
|
|
|
|
where
|
|
|
|
D: Fn(
|
|
|
|
&State,
|
|
|
|
&mut ResourceTable,
|
|
|
|
Value,
|
|
|
|
&mut [ZeroCopyBuf],
|
2020-08-25 18:22:15 -04:00
|
|
|
) -> Result<Value, ErrBox>,
|
2020-08-18 08:07:57 -04:00
|
|
|
{
|
|
|
|
let state = self.clone();
|
|
|
|
let resource_table = resource_table.clone();
|
|
|
|
|
2020-08-25 18:22:15 -04:00
|
|
|
move |isolate_state: &mut CoreIsolateState, bufs: &mut [ZeroCopyBuf]| {
|
2020-08-26 12:20:22 -04:00
|
|
|
let get_error_class_fn = isolate_state.get_error_class_fn;
|
|
|
|
|
2020-08-18 08:07:57 -04:00
|
|
|
// The first buffer should contain JSON encoded op arguments; parse them.
|
|
|
|
let args: Value = match serde_json::from_slice(&bufs[0]) {
|
|
|
|
Ok(v) => v,
|
|
|
|
Err(e) => {
|
2020-08-25 18:22:15 -04:00
|
|
|
return Op::Sync(serialize_result(
|
|
|
|
None,
|
|
|
|
Err(e.into()),
|
2020-08-26 12:20:22 -04:00
|
|
|
get_error_class_fn,
|
2020-08-25 18:22:15 -04:00
|
|
|
));
|
2020-08-18 08:07:57 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Make a slice containing all buffers except for the first one.
|
|
|
|
let zero_copy = &mut bufs[1..];
|
|
|
|
|
|
|
|
let result =
|
|
|
|
dispatcher(&state, &mut *resource_table.borrow_mut(), args, zero_copy);
|
|
|
|
|
|
|
|
// Convert to Op.
|
2020-08-26 12:20:22 -04:00
|
|
|
Op::Sync(serialize_result(None, result, get_error_class_fn))
|
2020-08-18 08:07:57 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn stateful_json_op_async<D, F>(
|
2020-08-18 12:30:13 -04:00
|
|
|
self: &Rc<Self>,
|
2020-08-18 08:07:57 -04:00
|
|
|
resource_table: &Rc<RefCell<ResourceTable>>,
|
|
|
|
dispatcher: D,
|
|
|
|
) -> impl Fn(&mut CoreIsolateState, &mut [ZeroCopyBuf]) -> Op
|
|
|
|
where
|
2020-08-18 12:30:13 -04:00
|
|
|
D:
|
|
|
|
FnOnce(Rc<State>, Rc<RefCell<ResourceTable>>, Value, BufVec) -> F + Clone,
|
2020-08-25 18:22:15 -04:00
|
|
|
F: Future<Output = Result<Value, ErrBox>> + 'static,
|
2020-08-18 08:07:57 -04:00
|
|
|
{
|
|
|
|
let state = self.clone();
|
|
|
|
let resource_table = resource_table.clone();
|
|
|
|
|
2020-08-25 18:22:15 -04:00
|
|
|
move |isolate_state: &mut CoreIsolateState, bufs: &mut [ZeroCopyBuf]| {
|
2020-08-26 12:20:22 -04:00
|
|
|
let get_error_class_fn = isolate_state.get_error_class_fn;
|
|
|
|
|
2020-08-18 08:07:57 -04:00
|
|
|
// The first buffer should contain JSON encoded op arguments; parse them.
|
|
|
|
let args: Value = match serde_json::from_slice(&bufs[0]) {
|
|
|
|
Ok(v) => v,
|
|
|
|
Err(e) => {
|
2020-08-25 18:22:15 -04:00
|
|
|
let e = e.into();
|
2020-08-26 12:20:22 -04:00
|
|
|
return Op::Sync(serialize_result(None, Err(e), get_error_class_fn));
|
2020-08-18 08:07:57 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// `args` should have a `promiseId` property with positive integer value.
|
|
|
|
let promise_id = match args.get("promiseId").and_then(|v| v.as_u64()) {
|
|
|
|
Some(i) => i,
|
|
|
|
None => {
|
2020-08-25 18:22:15 -04:00
|
|
|
let e = ErrBox::new("TypeError", "`promiseId` invalid/missing");
|
2020-08-26 12:20:22 -04:00
|
|
|
return Op::Sync(serialize_result(None, Err(e), get_error_class_fn));
|
2020-08-18 08:07:57 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Take ownership of all buffers after the first one.
|
|
|
|
let zero_copy: BufVec = bufs[1..].into();
|
|
|
|
|
|
|
|
// Call dispatcher to obtain op future.
|
|
|
|
let fut = (dispatcher.clone())(
|
|
|
|
state.clone(),
|
|
|
|
resource_table.clone(),
|
|
|
|
args,
|
|
|
|
zero_copy,
|
|
|
|
);
|
|
|
|
|
|
|
|
// Convert to Op.
|
|
|
|
Op::Async(
|
2020-08-25 18:22:15 -04:00
|
|
|
async move {
|
2020-08-26 12:20:22 -04:00
|
|
|
serialize_result(Some(promise_id), fut.await, get_error_class_fn)
|
2020-08-25 18:22:15 -04:00
|
|
|
}
|
|
|
|
.boxed_local(),
|
2020-08-18 08:07:57 -04:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-12 10:44:58 -04:00
|
|
|
pub fn stateful_json_op2<D>(
|
2020-08-18 12:30:13 -04:00
|
|
|
self: &Rc<Self>,
|
2020-08-12 10:44:58 -04:00
|
|
|
dispatcher: D,
|
|
|
|
) -> impl Fn(&mut deno_core::CoreIsolateState, &mut [ZeroCopyBuf]) -> Op
|
2020-04-21 09:48:44 -04:00
|
|
|
where
|
|
|
|
D: Fn(
|
2020-05-29 17:41:39 -04:00
|
|
|
&mut deno_core::CoreIsolateState,
|
2020-08-18 12:30:13 -04:00
|
|
|
&Rc<State>,
|
2020-04-21 09:48:44 -04:00
|
|
|
Value,
|
2020-06-01 14:20:47 -04:00
|
|
|
&mut [ZeroCopyBuf],
|
2020-08-25 18:22:15 -04:00
|
|
|
) -> Result<JsonOp, ErrBox>,
|
2020-04-21 09:48:44 -04:00
|
|
|
{
|
|
|
|
use crate::ops::json_op;
|
|
|
|
self.core_op(json_op(self.stateful_op2(dispatcher)))
|
|
|
|
}
|
|
|
|
|
2019-10-01 18:51:05 -04:00
|
|
|
/// Wrap core `OpDispatcher` to collect metrics.
|
2020-04-21 09:48:44 -04:00
|
|
|
// TODO(ry) this should be private. Is called by stateful_json_op or
|
|
|
|
// stateful_minimal_op
|
2020-08-12 10:44:58 -04:00
|
|
|
pub fn core_op<D>(
|
2020-08-18 12:30:13 -04:00
|
|
|
self: &Rc<Self>,
|
2020-08-12 10:44:58 -04:00
|
|
|
dispatcher: D,
|
|
|
|
) -> impl Fn(&mut deno_core::CoreIsolateState, &mut [ZeroCopyBuf]) -> Op
|
|
|
|
where
|
|
|
|
D: Fn(&mut deno_core::CoreIsolateState, &mut [ZeroCopyBuf]) -> Op,
|
|
|
|
{
|
2019-10-01 18:51:05 -04:00
|
|
|
let state = self.clone();
|
|
|
|
|
2020-05-29 17:41:39 -04:00
|
|
|
move |isolate_state: &mut deno_core::CoreIsolateState,
|
2020-06-01 14:20:47 -04:00
|
|
|
zero_copy: &mut [ZeroCopyBuf]|
|
2020-04-19 23:54:46 -04:00
|
|
|
-> Op {
|
2020-07-08 11:23:50 -04:00
|
|
|
let bytes_sent_control =
|
|
|
|
zero_copy.get(0).map(|s| s.len()).unwrap_or(0) as u64;
|
2019-10-01 18:51:05 -04:00
|
|
|
let bytes_sent_zero_copy =
|
2020-07-08 11:23:50 -04:00
|
|
|
zero_copy[1..].iter().map(|b| b.len()).sum::<usize>() as u64;
|
2019-10-01 18:51:05 -04:00
|
|
|
|
2020-08-12 10:44:58 -04:00
|
|
|
let op = dispatcher(isolate_state, zero_copy);
|
2019-10-01 18:51:05 -04:00
|
|
|
|
|
|
|
match op {
|
|
|
|
Op::Sync(buf) => {
|
2020-08-18 12:30:13 -04:00
|
|
|
state.metrics.borrow_mut().op_sync(
|
2020-03-02 13:13:36 -05:00
|
|
|
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-08-18 12:30:13 -04:00
|
|
|
state
|
2020-03-02 13:13:36 -05:00
|
|
|
.metrics
|
2020-08-18 12:30:13 -04:00
|
|
|
.borrow_mut()
|
2020-03-02 13:13:36 -05:00
|
|
|
.op_dispatched_async(bytes_sent_control, bytes_sent_zero_copy);
|
2019-10-01 18:51:05 -04:00
|
|
|
let state = state.clone();
|
2020-04-18 20:05:13 -04:00
|
|
|
let result_fut = fut.map(move |buf: Buf| {
|
2020-08-18 12:30:13 -04:00
|
|
|
state
|
|
|
|
.metrics
|
|
|
|
.borrow_mut()
|
|
|
|
.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-08-18 12:30:13 -04:00
|
|
|
state.metrics.borrow_mut().op_dispatched_async_unref(
|
2020-03-02 13:13:36 -05:00
|
|
|
bytes_sent_control,
|
|
|
|
bytes_sent_zero_copy,
|
|
|
|
);
|
2020-01-21 12:01:10 -05:00
|
|
|
let state = state.clone();
|
2020-04-18 20:05:13 -04:00
|
|
|
let result_fut = fut.map(move |buf: Buf| {
|
2020-08-18 12:30:13 -04:00
|
|
|
state
|
|
|
|
.metrics
|
|
|
|
.borrow_mut()
|
|
|
|
.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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-12 10:44:58 -04:00
|
|
|
pub fn stateful_minimal_op2<D>(
|
2020-08-18 12:30:13 -04:00
|
|
|
self: &Rc<Self>,
|
2020-08-12 10:44:58 -04:00
|
|
|
dispatcher: D,
|
|
|
|
) -> impl Fn(&mut deno_core::CoreIsolateState, &mut [ZeroCopyBuf]) -> Op
|
2019-11-14 12:10:25 -05:00
|
|
|
where
|
2020-04-21 09:48:44 -04:00
|
|
|
D: Fn(
|
2020-05-29 17:41:39 -04:00
|
|
|
&mut deno_core::CoreIsolateState,
|
2020-08-18 12:30:13 -04:00
|
|
|
&Rc<State>,
|
2020-04-21 09:48:44 -04:00
|
|
|
bool,
|
|
|
|
i32,
|
2020-06-01 14:20:47 -04:00
|
|
|
&mut [ZeroCopyBuf],
|
2020-04-21 09:48:44 -04:00
|
|
|
) -> MinimalOp,
|
2019-11-14 12:10:25 -05:00
|
|
|
{
|
|
|
|
let state = self.clone();
|
2020-04-21 09:48:44 -04:00
|
|
|
self.core_op(crate::ops::minimal_op(
|
2020-05-29 17:41:39 -04:00
|
|
|
move |isolate_state: &mut deno_core::CoreIsolateState,
|
2020-04-21 09:48:44 -04:00
|
|
|
is_sync: bool,
|
|
|
|
rid: i32,
|
2020-06-01 14:20:47 -04:00
|
|
|
zero_copy: &mut [ZeroCopyBuf]|
|
2020-04-21 09:48:44 -04:00
|
|
|
-> MinimalOp {
|
2020-05-29 17:41:39 -04:00
|
|
|
dispatcher(isolate_state, &state, is_sync, rid, zero_copy)
|
2020-04-21 09:48:44 -04:00
|
|
|
},
|
|
|
|
))
|
2019-11-14 12:10:25 -05:00
|
|
|
}
|
|
|
|
|
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.
|
2020-04-23 05:51:07 -04:00
|
|
|
/// This is a band-aid for transition to `CoreIsolate.register_op` API as most of our
|
2019-10-01 18:51:05 -04:00
|
|
|
/// ops require `state` argument.
|
2020-08-12 10:44:58 -04:00
|
|
|
pub fn stateful_op<D>(
|
2020-08-18 12:30:13 -04:00
|
|
|
self: &Rc<Self>,
|
2020-08-12 10:44:58 -04:00
|
|
|
dispatcher: D,
|
|
|
|
) -> impl Fn(
|
|
|
|
&mut deno_core::CoreIsolateState,
|
|
|
|
Value,
|
|
|
|
&mut [ZeroCopyBuf],
|
2020-08-25 18:22:15 -04:00
|
|
|
) -> Result<JsonOp, ErrBox>
|
2019-10-01 18:51:05 -04:00
|
|
|
where
|
2020-08-25 18:22:15 -04:00
|
|
|
D: Fn(&Rc<State>, Value, &mut [ZeroCopyBuf]) -> Result<JsonOp, ErrBox>,
|
2019-10-01 18:51:05 -04:00
|
|
|
{
|
|
|
|
let state = self.clone();
|
2020-05-29 17:41:39 -04:00
|
|
|
move |_isolate_state: &mut deno_core::CoreIsolateState,
|
2020-04-19 23:54:46 -04:00
|
|
|
args: Value,
|
2020-06-01 14:20:47 -04:00
|
|
|
zero_copy: &mut [ZeroCopyBuf]|
|
2020-08-25 18:22:15 -04:00
|
|
|
-> Result<JsonOp, ErrBox> { dispatcher(&state, args, zero_copy) }
|
2019-04-09 13:11:25 -04:00
|
|
|
}
|
2020-04-19 23:54:46 -04:00
|
|
|
|
2020-08-12 10:44:58 -04:00
|
|
|
pub fn stateful_op2<D>(
|
2020-08-18 12:30:13 -04:00
|
|
|
self: &Rc<Self>,
|
2020-08-12 10:44:58 -04:00
|
|
|
dispatcher: D,
|
|
|
|
) -> impl Fn(
|
|
|
|
&mut deno_core::CoreIsolateState,
|
|
|
|
Value,
|
|
|
|
&mut [ZeroCopyBuf],
|
2020-08-25 18:22:15 -04:00
|
|
|
) -> Result<JsonOp, ErrBox>
|
2020-04-19 23:54:46 -04:00
|
|
|
where
|
|
|
|
D: Fn(
|
2020-05-29 17:41:39 -04:00
|
|
|
&mut deno_core::CoreIsolateState,
|
2020-08-18 12:30:13 -04:00
|
|
|
&Rc<State>,
|
2020-04-19 23:54:46 -04:00
|
|
|
Value,
|
2020-06-01 14:20:47 -04:00
|
|
|
&mut [ZeroCopyBuf],
|
2020-08-25 18:22:15 -04:00
|
|
|
) -> Result<JsonOp, ErrBox>,
|
2020-04-19 23:54:46 -04:00
|
|
|
{
|
|
|
|
let state = self.clone();
|
2020-05-29 17:41:39 -04:00
|
|
|
move |isolate_state: &mut deno_core::CoreIsolateState,
|
2020-04-19 23:54:46 -04:00
|
|
|
args: Value,
|
2020-06-01 14:20:47 -04:00
|
|
|
zero_copy: &mut [ZeroCopyBuf]|
|
2020-08-25 18:22:15 -04:00
|
|
|
-> Result<JsonOp, ErrBox> {
|
2020-05-29 17:41:39 -04:00
|
|
|
dispatcher(isolate_state, &state, args, zero_copy)
|
2020-04-19 23:54:46 -04:00
|
|
|
}
|
|
|
|
}
|
2020-04-25 09:31:54 -04:00
|
|
|
|
|
|
|
/// Quits the process if the --unstable flag was not provided.
|
|
|
|
///
|
|
|
|
/// This is intentionally a non-recoverable check so that people cannot probe
|
|
|
|
/// for unstable APIs from stable programs.
|
2020-08-18 12:30:13 -04:00
|
|
|
pub fn check_unstable(self: &Rc<Self>, api_name: &str) {
|
2020-04-25 09:31:54 -04:00
|
|
|
// TODO(ry) Maybe use IsolateHandle::terminate_execution here to provide a
|
|
|
|
// stack trace in JS.
|
2020-08-18 12:30:13 -04:00
|
|
|
if !self.global_state.flags.unstable {
|
2020-04-27 19:12:38 -04:00
|
|
|
exit_unstable(api_name);
|
2020-04-25 09:31:54 -04:00
|
|
|
}
|
|
|
|
}
|
2019-04-09 13:11:25 -04:00
|
|
|
}
|
|
|
|
|
2020-05-18 06:59:29 -04:00
|
|
|
pub fn exit_unstable(api_name: &str) {
|
2020-04-27 19:12:38 -04:00
|
|
|
eprintln!(
|
|
|
|
"Unstable API '{}'. The --unstable flag must be provided.",
|
|
|
|
api_name
|
|
|
|
);
|
|
|
|
std::process::exit(70);
|
|
|
|
}
|
|
|
|
|
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-08-18 12:30:13 -04:00
|
|
|
if let Some(import_map) = &self.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
|
|
|
}
|
|
|
|
|
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-05-29 10:32:15 -04:00
|
|
|
_is_dyn_import: bool,
|
2020-03-02 13:12:49 -05:00
|
|
|
) -> Pin<Box<deno_core::ModuleSourceFuture>> {
|
2020-05-29 10:32:15 -04:00
|
|
|
let module_specifier = module_specifier.to_owned();
|
2020-01-25 12:53:16 -05:00
|
|
|
// TODO(bartlomieju): incrementing resolve_count here has no sense...
|
2020-08-18 12:30:13 -04:00
|
|
|
self.metrics.borrow_mut().resolve_count += 1;
|
2019-08-07 12:55:39 -04:00
|
|
|
let module_url_specified = module_specifier.to_string();
|
2020-08-18 12:30:13 -04:00
|
|
|
let global_state = self.global_state.clone();
|
2020-05-11 07:13:27 -04:00
|
|
|
|
2020-05-29 10:32:15 -04:00
|
|
|
// TODO(bartlomieju): `fetch_compiled_module` should take `load_id` param
|
2020-02-03 18:08:44 -05:00
|
|
|
let fut = async move {
|
|
|
|
let compiled_module = global_state
|
2020-05-29 10:32:15 -04:00
|
|
|
.fetch_compiled_module(module_specifier, maybe_referrer)
|
2020-02-03 18:08:44 -05:00
|
|
|
.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-04-30 08:37:06 -04:00
|
|
|
|
|
|
|
fn prepare_load(
|
|
|
|
&self,
|
|
|
|
_load_id: ModuleLoadId,
|
2020-05-29 10:32:15 -04:00
|
|
|
module_specifier: &ModuleSpecifier,
|
|
|
|
maybe_referrer: Option<String>,
|
|
|
|
is_dyn_import: bool,
|
2020-04-30 08:37:06 -04:00
|
|
|
) -> Pin<Box<dyn Future<Output = Result<(), ErrBox>>>> {
|
2020-05-29 10:32:15 -04:00
|
|
|
let module_specifier = module_specifier.clone();
|
2020-08-18 12:30:13 -04:00
|
|
|
let target_lib = self.target_lib.clone();
|
|
|
|
let maybe_import_map = self.import_map.clone();
|
2020-05-29 10:32:15 -04:00
|
|
|
// Only "main" module is loaded without permission check,
|
|
|
|
// ie. module that is associated with "is_main" state
|
|
|
|
// and is not a dynamic import.
|
2020-08-18 12:30:13 -04:00
|
|
|
let permissions = if self.is_main && !is_dyn_import {
|
2020-05-29 10:32:15 -04:00
|
|
|
Permissions::allow_all()
|
|
|
|
} else {
|
2020-08-18 12:30:13 -04:00
|
|
|
self.permissions.borrow().clone()
|
2020-05-29 10:32:15 -04:00
|
|
|
};
|
2020-08-18 12:30:13 -04:00
|
|
|
let global_state = self.global_state.clone();
|
2020-05-29 10:32:15 -04:00
|
|
|
// TODO(bartlomieju): I'm not sure if it's correct to ignore
|
|
|
|
// bad referrer - this is the case for `Deno.core.evalContext()` where
|
|
|
|
// `ref_str` is `<unknown>`.
|
|
|
|
let maybe_referrer = if let Some(ref_str) = maybe_referrer {
|
|
|
|
ModuleSpecifier::resolve_url(&ref_str).ok()
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
|
|
|
// TODO(bartlomieju): `prepare_module_load` should take `load_id` param
|
|
|
|
async move {
|
|
|
|
global_state
|
|
|
|
.prepare_module_load(
|
|
|
|
module_specifier,
|
|
|
|
maybe_referrer,
|
|
|
|
target_lib,
|
|
|
|
permissions,
|
|
|
|
is_dyn_import,
|
|
|
|
maybe_import_map,
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
.boxed_local()
|
2020-04-30 08:37:06 -04:00
|
|
|
}
|
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-08-18 12:30:13 -04:00
|
|
|
global_state: &Arc<GlobalState>,
|
2020-05-04 14:10:59 -04:00
|
|
|
shared_permissions: Option<Permissions>,
|
2020-02-04 14:24:33 -05:00
|
|
|
main_module: ModuleSpecifier,
|
2020-05-29 10:32:15 -04:00
|
|
|
maybe_import_map: Option<ImportMap>,
|
2020-05-12 15:08:22 -04:00
|
|
|
is_internal: bool,
|
2020-08-18 12:30:13 -04:00
|
|
|
) -> Result<Rc<Self>, ErrBox> {
|
|
|
|
let fl = &global_state.flags;
|
|
|
|
let state = State {
|
|
|
|
global_state: global_state.clone(),
|
2019-11-04 10:38:52 -05:00
|
|
|
main_module,
|
2020-08-18 12:30:13 -04:00
|
|
|
permissions: shared_permissions
|
|
|
|
.unwrap_or_else(|| global_state.permissions.clone())
|
|
|
|
.into(),
|
2020-05-29 10:32:15 -04:00
|
|
|
import_map: maybe_import_map,
|
2020-08-18 12:30:13 -04:00
|
|
|
metrics: Default::default(),
|
|
|
|
global_timer: Default::default(),
|
|
|
|
workers: Default::default(),
|
|
|
|
next_worker_id: Default::default(),
|
2019-04-08 16:22:40 -04:00
|
|
|
start_time: Instant::now(),
|
2020-08-18 12:30:13 -04:00
|
|
|
seeded_rng: fl.seed.map(|v| StdRng::seed_from_u64(v).into()),
|
2020-01-29 12:54:23 -05:00
|
|
|
target_lib: TargetLib::Main,
|
2020-05-11 07:13:27 -04:00
|
|
|
is_main: true,
|
2020-05-12 15:08:22 -04:00
|
|
|
is_internal,
|
2020-08-18 12:30:13 -04:00
|
|
|
http_client: create_http_client(fl.ca_file.as_deref())?.into(),
|
|
|
|
};
|
|
|
|
Ok(Rc::new(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-08-18 12:30:13 -04:00
|
|
|
global_state: &Arc<GlobalState>,
|
2020-05-04 14:10:59 -04:00
|
|
|
shared_permissions: Option<Permissions>,
|
2020-02-04 14:24:33 -05:00
|
|
|
main_module: ModuleSpecifier,
|
2020-08-18 12:30:13 -04:00
|
|
|
) -> Result<Rc<Self>, ErrBox> {
|
|
|
|
let fl = &global_state.flags;
|
|
|
|
let state = State {
|
|
|
|
global_state: global_state.clone(),
|
2020-01-29 12:54:23 -05:00
|
|
|
main_module,
|
2020-08-18 12:30:13 -04:00
|
|
|
permissions: shared_permissions
|
|
|
|
.unwrap_or_else(|| global_state.permissions.clone())
|
|
|
|
.into(),
|
2020-01-29 12:54:23 -05:00
|
|
|
import_map: None,
|
2020-08-18 12:30:13 -04:00
|
|
|
metrics: Default::default(),
|
|
|
|
global_timer: Default::default(),
|
|
|
|
workers: Default::default(),
|
|
|
|
next_worker_id: Default::default(),
|
2020-01-29 12:54:23 -05:00
|
|
|
start_time: Instant::now(),
|
2020-08-18 12:30:13 -04:00
|
|
|
seeded_rng: fl.seed.map(|v| StdRng::seed_from_u64(v).into()),
|
2020-01-29 12:54:23 -05:00
|
|
|
target_lib: TargetLib::Worker,
|
2020-05-11 07:13:27 -04:00
|
|
|
is_main: false,
|
2020-05-12 15:08:22 -04:00
|
|
|
is_internal: false,
|
2020-08-18 12:30:13 -04:00
|
|
|
http_client: create_http_client(fl.ca_file.as_deref())?.into(),
|
|
|
|
};
|
|
|
|
Ok(Rc::new(state))
|
2019-03-14 19:17:52 -04:00
|
|
|
}
|
|
|
|
|
2019-03-19 20:55:59 -04:00
|
|
|
#[inline]
|
2020-08-25 18:22:15 -04:00
|
|
|
pub fn check_read(&self, path: &Path) -> Result<(), ErrBox> {
|
2020-08-18 12:30:13 -04:00
|
|
|
self.permissions.borrow().check_read(path)
|
2019-03-19 20:55:59 -04:00
|
|
|
}
|
|
|
|
|
2020-05-29 11:27:43 -04:00
|
|
|
/// As `check_read()`, but permission error messages will anonymize the path
|
|
|
|
/// by replacing it with the given `display`.
|
|
|
|
#[inline]
|
|
|
|
pub fn check_read_blind(
|
|
|
|
&self,
|
|
|
|
path: &Path,
|
|
|
|
display: &str,
|
2020-08-25 18:22:15 -04:00
|
|
|
) -> Result<(), ErrBox> {
|
2020-08-18 12:30:13 -04:00
|
|
|
self.permissions.borrow().check_read_blind(path, display)
|
2020-05-29 11:27:43 -04:00
|
|
|
}
|
|
|
|
|
2019-03-19 20:55:59 -04:00
|
|
|
#[inline]
|
2020-08-25 18:22:15 -04:00
|
|
|
pub fn check_write(&self, path: &Path) -> Result<(), ErrBox> {
|
2020-08-18 12:30:13 -04:00
|
|
|
self.permissions.borrow().check_write(path)
|
2019-03-19 20:55:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2020-08-25 18:22:15 -04:00
|
|
|
pub fn check_env(&self) -> Result<(), ErrBox> {
|
2020-08-18 12:30:13 -04:00
|
|
|
self.permissions.borrow().check_env()
|
2019-03-19 20:55:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2020-08-25 18:22:15 -04:00
|
|
|
pub fn check_net(&self, hostname: &str, port: u16) -> Result<(), ErrBox> {
|
2020-08-18 12:30:13 -04:00
|
|
|
self.permissions.borrow().check_net(hostname, port)
|
2019-05-08 19:20:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2020-08-25 18:22:15 -04:00
|
|
|
pub fn check_net_url(&self, url: &url::Url) -> Result<(), ErrBox> {
|
2020-08-18 12:30:13 -04:00
|
|
|
self.permissions.borrow().check_net_url(url)
|
2019-03-19 20:55:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2020-08-25 18:22:15 -04:00
|
|
|
pub fn check_run(&self) -> Result<(), ErrBox> {
|
2020-08-18 12:30:13 -04:00
|
|
|
self.permissions.borrow().check_run()
|
2019-03-19 20:55:59 -04:00
|
|
|
}
|
|
|
|
|
2020-08-18 16:29:32 -04:00
|
|
|
#[inline]
|
2020-08-25 18:22:15 -04:00
|
|
|
pub fn check_hrtime(&self) -> Result<(), ErrBox> {
|
2020-08-18 12:30:13 -04:00
|
|
|
self.permissions.borrow().check_hrtime()
|
2020-08-18 16:29:32 -04:00
|
|
|
}
|
|
|
|
|
2019-12-05 15:30:20 -05:00
|
|
|
#[inline]
|
2020-08-25 18:22:15 -04:00
|
|
|
pub fn check_plugin(&self, filename: &Path) -> Result<(), ErrBox> {
|
2020-08-18 12:30:13 -04:00
|
|
|
self.permissions.borrow().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-08-25 18:22:15 -04:00
|
|
|
) -> Result<(), ErrBox> {
|
2019-08-13 14:51:15 -04:00
|
|
|
let u = module_specifier.as_url();
|
2020-05-07 09:57:10 -04:00
|
|
|
// TODO(bartlomieju): temporary fix to prevent hitting `unreachable`
|
|
|
|
// statement that is actually reachable...
|
|
|
|
SourceFileFetcher::check_if_supported_scheme(u)?;
|
|
|
|
|
2019-08-13 14:51:15 -04:00
|
|
|
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-08-18 12:30:13 -04:00
|
|
|
pub fn mock(main_module: &str) -> Rc<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-08-18 12:30:13 -04:00
|
|
|
&GlobalState::mock(vec!["deno".to_string()], None),
|
2019-11-24 10:42:30 -05:00
|
|
|
None,
|
2019-11-04 10:38:52 -05:00
|
|
|
module_specifier,
|
2020-05-29 10:32:15 -04:00
|
|
|
None,
|
2020-05-12 15:08:22 -04:00
|
|
|
false,
|
2019-07-31 17:11:37 -04:00
|
|
|
)
|
|
|
|
.unwrap()
|
2019-03-14 19:17:52 -04:00
|
|
|
}
|
|
|
|
}
|