mirror of
https://github.com/denoland/deno.git
synced 2024-12-22 15:24:46 -05:00
Move resource_table from deno::State to deno_core::Isolate (#4834)
This commit is contained in:
parent
ef6ee25e09
commit
cc1720132a
20 changed files with 298 additions and 263 deletions
|
@ -69,6 +69,8 @@ target/debug/deno -A cli/js/tests/unit_test_runner.ts --worker --addr=127.0.0.1:
|
||||||
|
|
||||||
# Run specific tests
|
# Run specific tests
|
||||||
target/debug/deno --allow-net cli/js/tests/unit_test_runner.ts -- netTcpListenClose
|
target/debug/deno --allow-net cli/js/tests/unit_test_runner.ts -- netTcpListenClose
|
||||||
|
|
||||||
|
RUST_BACKTRACE=1 cargo run -- --allow-read --allow-write cli/js/tests/unit_test_runner.ts -- netUnixDialListen
|
||||||
```
|
```
|
||||||
|
|
||||||
### Http server
|
### Http server
|
||||||
|
|
17
cli/lib.rs
17
cli/lib.rs
|
@ -140,19 +140,20 @@ fn create_main_worker(
|
||||||
) -> Result<MainWorker, ErrBox> {
|
) -> Result<MainWorker, ErrBox> {
|
||||||
let state = State::new(global_state, None, main_module, DebugType::Main)?;
|
let state = State::new(global_state, None, main_module, DebugType::Main)?;
|
||||||
|
|
||||||
{
|
|
||||||
let mut s = state.borrow_mut();
|
|
||||||
let (stdin, stdout, stderr) = get_stdio();
|
|
||||||
s.resource_table.add("stdin", Box::new(stdin));
|
|
||||||
s.resource_table.add("stdout", Box::new(stdout));
|
|
||||||
s.resource_table.add("stderr", Box::new(stderr));
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut worker = MainWorker::new(
|
let mut worker = MainWorker::new(
|
||||||
"main".to_string(),
|
"main".to_string(),
|
||||||
startup_data::deno_isolate_init(),
|
startup_data::deno_isolate_init(),
|
||||||
state,
|
state,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
{
|
||||||
|
let (stdin, stdout, stderr) = get_stdio();
|
||||||
|
let mut t = worker.resource_table.borrow_mut();
|
||||||
|
t.add("stdin", Box::new(stdin));
|
||||||
|
t.add("stdout", Box::new(stdout));
|
||||||
|
t.add("stderr", Box::new(stderr));
|
||||||
|
}
|
||||||
|
|
||||||
worker.execute("bootstrapMainRuntime()")?;
|
worker.execute("bootstrapMainRuntime()")?;
|
||||||
Ok(worker)
|
Ok(worker)
|
||||||
}
|
}
|
||||||
|
|
|
@ -117,9 +117,9 @@ pub fn minimal_op<D>(
|
||||||
d: D,
|
d: D,
|
||||||
) -> impl Fn(&mut deno_core::Isolate, &[u8], Option<ZeroCopyBuf>) -> Op
|
) -> impl Fn(&mut deno_core::Isolate, &[u8], Option<ZeroCopyBuf>) -> Op
|
||||||
where
|
where
|
||||||
D: Fn(bool, i32, Option<ZeroCopyBuf>) -> MinimalOp,
|
D: Fn(&mut deno_core::Isolate, bool, i32, Option<ZeroCopyBuf>) -> MinimalOp,
|
||||||
{
|
{
|
||||||
move |_isolate: &mut deno_core::Isolate,
|
move |isolate: &mut deno_core::Isolate,
|
||||||
control: &[u8],
|
control: &[u8],
|
||||||
zero_copy: Option<ZeroCopyBuf>| {
|
zero_copy: Option<ZeroCopyBuf>| {
|
||||||
let mut record = match parse_min_record(control) {
|
let mut record = match parse_min_record(control) {
|
||||||
|
@ -137,7 +137,7 @@ where
|
||||||
};
|
};
|
||||||
let is_sync = record.promise_id == 0;
|
let is_sync = record.promise_id == 0;
|
||||||
let rid = record.arg;
|
let rid = record.arg;
|
||||||
let min_op = d(is_sync, rid, zero_copy);
|
let min_op = d(isolate, is_sync, rid, zero_copy);
|
||||||
|
|
||||||
match min_op {
|
match min_op {
|
||||||
MinimalOp::Sync(sync_result) => Op::Sync(match sync_result {
|
MinimalOp::Sync(sync_result) => Op::Sync(match sync_result {
|
||||||
|
|
|
@ -12,7 +12,7 @@ use http::Method;
|
||||||
use std::convert::From;
|
use std::convert::From;
|
||||||
|
|
||||||
pub fn init(i: &mut Isolate, s: &State) {
|
pub fn init(i: &mut Isolate, s: &State) {
|
||||||
i.register_op("op_fetch", s.stateful_json_op(op_fetch));
|
i.register_op("op_fetch", s.stateful_json_op2(op_fetch));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
|
@ -23,6 +23,7 @@ struct FetchArgs {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn op_fetch(
|
pub fn op_fetch(
|
||||||
|
isolate: &mut deno_core::Isolate,
|
||||||
state: &State,
|
state: &State,
|
||||||
args: Value,
|
args: Value,
|
||||||
data: Option<ZeroCopyBuf>,
|
data: Option<ZeroCopyBuf>,
|
||||||
|
@ -64,8 +65,8 @@ pub fn op_fetch(
|
||||||
request = request.header(name, v);
|
request = request.header(name, v);
|
||||||
}
|
}
|
||||||
debug!("Before fetch {}", url);
|
debug!("Before fetch {}", url);
|
||||||
let state_ = state.clone();
|
|
||||||
|
|
||||||
|
let resource_table = isolate.resource_table.clone();
|
||||||
let future = async move {
|
let future = async move {
|
||||||
let res = request.send().await?;
|
let res = request.send().await?;
|
||||||
debug!("Fetch response {}", url);
|
debug!("Fetch response {}", url);
|
||||||
|
@ -76,8 +77,8 @@ pub fn op_fetch(
|
||||||
}
|
}
|
||||||
|
|
||||||
let body = HttpBody::from(res);
|
let body = HttpBody::from(res);
|
||||||
let mut state = state_.borrow_mut();
|
let mut resource_table = resource_table.borrow_mut();
|
||||||
let rid = state.resource_table.add(
|
let rid = resource_table.add(
|
||||||
"httpBody",
|
"httpBody",
|
||||||
Box::new(StreamResourceHolder::new(StreamResource::HttpBody(
|
Box::new(StreamResourceHolder::new(StreamResource::HttpBody(
|
||||||
Box::new(body),
|
Box::new(body),
|
||||||
|
|
|
@ -18,8 +18,8 @@ use std::time::UNIX_EPOCH;
|
||||||
use rand::{thread_rng, Rng};
|
use rand::{thread_rng, Rng};
|
||||||
|
|
||||||
pub fn init(i: &mut Isolate, s: &State) {
|
pub fn init(i: &mut Isolate, s: &State) {
|
||||||
i.register_op("op_open", s.stateful_json_op(op_open));
|
i.register_op("op_open", s.stateful_json_op2(op_open));
|
||||||
i.register_op("op_seek", s.stateful_json_op(op_seek));
|
i.register_op("op_seek", s.stateful_json_op2(op_seek));
|
||||||
i.register_op("op_umask", s.stateful_json_op(op_umask));
|
i.register_op("op_umask", s.stateful_json_op(op_umask));
|
||||||
i.register_op("op_chdir", s.stateful_json_op(op_chdir));
|
i.register_op("op_chdir", s.stateful_json_op(op_chdir));
|
||||||
i.register_op("op_mkdir", s.stateful_json_op(op_mkdir));
|
i.register_op("op_mkdir", s.stateful_json_op(op_mkdir));
|
||||||
|
@ -68,13 +68,14 @@ struct OpenOptions {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn op_open(
|
fn op_open(
|
||||||
|
isolate: &mut deno_core::Isolate,
|
||||||
state: &State,
|
state: &State,
|
||||||
args: Value,
|
args: Value,
|
||||||
_zero_copy: Option<ZeroCopyBuf>,
|
_zero_copy: Option<ZeroCopyBuf>,
|
||||||
) -> Result<JsonOp, OpError> {
|
) -> Result<JsonOp, OpError> {
|
||||||
let args: OpenArgs = serde_json::from_value(args)?;
|
let args: OpenArgs = serde_json::from_value(args)?;
|
||||||
let path = resolve_from_cwd(Path::new(&args.path))?;
|
let path = resolve_from_cwd(Path::new(&args.path))?;
|
||||||
let state_ = state.clone();
|
let resource_table = isolate.resource_table.clone();
|
||||||
|
|
||||||
let mut open_options = std::fs::OpenOptions::new();
|
let mut open_options = std::fs::OpenOptions::new();
|
||||||
|
|
||||||
|
@ -166,8 +167,8 @@ fn op_open(
|
||||||
if is_sync {
|
if is_sync {
|
||||||
let std_file = open_options.open(path)?;
|
let std_file = open_options.open(path)?;
|
||||||
let tokio_file = tokio::fs::File::from_std(std_file);
|
let tokio_file = tokio::fs::File::from_std(std_file);
|
||||||
let mut state = state_.borrow_mut();
|
let mut resource_table = resource_table.borrow_mut();
|
||||||
let rid = state.resource_table.add(
|
let rid = resource_table.add(
|
||||||
"fsFile",
|
"fsFile",
|
||||||
Box::new(StreamResourceHolder::new(StreamResource::FsFile(Some((
|
Box::new(StreamResourceHolder::new(StreamResource::FsFile(Some((
|
||||||
tokio_file,
|
tokio_file,
|
||||||
|
@ -180,8 +181,8 @@ fn op_open(
|
||||||
let tokio_file = tokio::fs::OpenOptions::from(open_options)
|
let tokio_file = tokio::fs::OpenOptions::from(open_options)
|
||||||
.open(path)
|
.open(path)
|
||||||
.await?;
|
.await?;
|
||||||
let mut state = state_.borrow_mut();
|
let mut resource_table = resource_table.borrow_mut();
|
||||||
let rid = state.resource_table.add(
|
let rid = resource_table.add(
|
||||||
"fsFile",
|
"fsFile",
|
||||||
Box::new(StreamResourceHolder::new(StreamResource::FsFile(Some((
|
Box::new(StreamResourceHolder::new(StreamResource::FsFile(Some((
|
||||||
tokio_file,
|
tokio_file,
|
||||||
|
@ -204,7 +205,8 @@ struct SeekArgs {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn op_seek(
|
fn op_seek(
|
||||||
state: &State,
|
isolate: &mut deno_core::Isolate,
|
||||||
|
_state: &State,
|
||||||
args: Value,
|
args: Value,
|
||||||
_zero_copy: Option<ZeroCopyBuf>,
|
_zero_copy: Option<ZeroCopyBuf>,
|
||||||
) -> Result<JsonOp, OpError> {
|
) -> Result<JsonOp, OpError> {
|
||||||
|
@ -226,12 +228,12 @@ fn op_seek(
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let state = state.clone();
|
let resource_table = isolate.resource_table.clone();
|
||||||
let is_sync = args.promise_id.is_none();
|
let is_sync = args.promise_id.is_none();
|
||||||
|
|
||||||
if is_sync {
|
if is_sync {
|
||||||
let mut s = state.borrow_mut();
|
let mut resource_table = resource_table.borrow_mut();
|
||||||
let pos = std_file_resource(&mut s.resource_table, rid, |r| match r {
|
let pos = std_file_resource(&mut resource_table, rid, |r| match r {
|
||||||
Ok(std_file) => std_file.seek(seek_from).map_err(OpError::from),
|
Ok(std_file) => std_file.seek(seek_from).map_err(OpError::from),
|
||||||
Err(_) => Err(OpError::type_error(
|
Err(_) => Err(OpError::type_error(
|
||||||
"cannot seek on this type of resource".to_string(),
|
"cannot seek on this type of resource".to_string(),
|
||||||
|
@ -242,8 +244,8 @@ fn op_seek(
|
||||||
// TODO(ry) This is a fake async op. We need to use poll_fn,
|
// TODO(ry) This is a fake async op. We need to use poll_fn,
|
||||||
// tokio::fs::File::start_seek and tokio::fs::File::poll_complete
|
// tokio::fs::File::start_seek and tokio::fs::File::poll_complete
|
||||||
let fut = async move {
|
let fut = async move {
|
||||||
let mut s = state.borrow_mut();
|
let mut resource_table = resource_table.borrow_mut();
|
||||||
let pos = std_file_resource(&mut s.resource_table, rid, |r| match r {
|
let pos = std_file_resource(&mut resource_table, rid, |r| match r {
|
||||||
Ok(std_file) => std_file.seek(seek_from).map_err(OpError::from),
|
Ok(std_file) => std_file.seek(seek_from).map_err(OpError::from),
|
||||||
Err(_) => Err(OpError::type_error(
|
Err(_) => Err(OpError::type_error(
|
||||||
"cannot seek on this type of resource".to_string(),
|
"cannot seek on this type of resource".to_string(),
|
||||||
|
|
|
@ -17,8 +17,8 @@ use std::path::PathBuf;
|
||||||
use tokio::sync::mpsc;
|
use tokio::sync::mpsc;
|
||||||
|
|
||||||
pub fn init(i: &mut Isolate, s: &State) {
|
pub fn init(i: &mut Isolate, s: &State) {
|
||||||
i.register_op("op_fs_events_open", s.stateful_json_op(op_fs_events_open));
|
i.register_op("op_fs_events_open", s.stateful_json_op2(op_fs_events_open));
|
||||||
i.register_op("op_fs_events_poll", s.stateful_json_op(op_fs_events_poll));
|
i.register_op("op_fs_events_poll", s.stateful_json_op2(op_fs_events_poll));
|
||||||
}
|
}
|
||||||
|
|
||||||
struct FsEventsResource {
|
struct FsEventsResource {
|
||||||
|
@ -60,6 +60,7 @@ impl From<NotifyEvent> for FsEvent {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn op_fs_events_open(
|
pub fn op_fs_events_open(
|
||||||
|
isolate: &mut deno_core::Isolate,
|
||||||
state: &State,
|
state: &State,
|
||||||
args: Value,
|
args: Value,
|
||||||
_zero_copy: Option<ZeroCopyBuf>,
|
_zero_copy: Option<ZeroCopyBuf>,
|
||||||
|
@ -91,13 +92,14 @@ pub fn op_fs_events_open(
|
||||||
watcher.watch(path, recursive_mode).map_err(ErrBox::from)?;
|
watcher.watch(path, recursive_mode).map_err(ErrBox::from)?;
|
||||||
}
|
}
|
||||||
let resource = FsEventsResource { watcher, receiver };
|
let resource = FsEventsResource { watcher, receiver };
|
||||||
let table = &mut state.borrow_mut().resource_table;
|
let mut resource_table = isolate.resource_table.borrow_mut();
|
||||||
let rid = table.add("fsEvents", Box::new(resource));
|
let rid = resource_table.add("fsEvents", Box::new(resource));
|
||||||
Ok(JsonOp::Sync(json!(rid)))
|
Ok(JsonOp::Sync(json!(rid)))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn op_fs_events_poll(
|
pub fn op_fs_events_poll(
|
||||||
state: &State,
|
isolate: &mut deno_core::Isolate,
|
||||||
|
_state: &State,
|
||||||
args: Value,
|
args: Value,
|
||||||
_zero_copy: Option<ZeroCopyBuf>,
|
_zero_copy: Option<ZeroCopyBuf>,
|
||||||
) -> Result<JsonOp, OpError> {
|
) -> Result<JsonOp, OpError> {
|
||||||
|
@ -106,9 +108,9 @@ pub fn op_fs_events_poll(
|
||||||
rid: u32,
|
rid: u32,
|
||||||
}
|
}
|
||||||
let PollArgs { rid } = serde_json::from_value(args)?;
|
let PollArgs { rid } = serde_json::from_value(args)?;
|
||||||
let state = state.clone();
|
let resource_table = isolate.resource_table.clone();
|
||||||
let f = poll_fn(move |cx| {
|
let f = poll_fn(move |cx| {
|
||||||
let resource_table = &mut state.borrow_mut().resource_table;
|
let mut resource_table = resource_table.borrow_mut();
|
||||||
let watcher = resource_table
|
let watcher = resource_table
|
||||||
.get_mut::<FsEventsResource>(rid)
|
.get_mut::<FsEventsResource>(rid)
|
||||||
.ok_or_else(OpError::bad_resource_id)?;
|
.ok_or_else(OpError::bad_resource_id)?;
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
use super::dispatch_minimal::MinimalOp;
|
use super::dispatch_minimal::MinimalOp;
|
||||||
use crate::http_util::HttpBody;
|
use crate::http_util::HttpBody;
|
||||||
use crate::op_error::OpError;
|
use crate::op_error::OpError;
|
||||||
use crate::ops::minimal_op;
|
|
||||||
use crate::state::State;
|
use crate::state::State;
|
||||||
use deno_core::*;
|
use deno_core::*;
|
||||||
use futures::future::poll_fn;
|
use futures::future::poll_fn;
|
||||||
|
@ -60,14 +59,8 @@ lazy_static! {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn init(i: &mut Isolate, s: &State) {
|
pub fn init(i: &mut Isolate, s: &State) {
|
||||||
i.register_op(
|
i.register_op("op_read", s.stateful_minimal_op2(op_read));
|
||||||
"op_read",
|
i.register_op("op_write", s.stateful_minimal_op2(op_write));
|
||||||
s.core_op(minimal_op(s.stateful_minimal_op(op_read))),
|
|
||||||
);
|
|
||||||
i.register_op(
|
|
||||||
"op_write",
|
|
||||||
s.core_op(minimal_op(s.stateful_minimal_op(op_write))),
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_stdio() -> (
|
pub fn get_stdio() -> (
|
||||||
|
@ -211,7 +204,8 @@ impl DenoAsyncRead for StreamResource {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn op_read(
|
pub fn op_read(
|
||||||
state: &State,
|
isolate: &mut deno_core::Isolate,
|
||||||
|
_state: &State,
|
||||||
is_sync: bool,
|
is_sync: bool,
|
||||||
rid: i32,
|
rid: i32,
|
||||||
zero_copy: Option<ZeroCopyBuf>,
|
zero_copy: Option<ZeroCopyBuf>,
|
||||||
|
@ -220,15 +214,15 @@ pub fn op_read(
|
||||||
if zero_copy.is_none() {
|
if zero_copy.is_none() {
|
||||||
return MinimalOp::Sync(Err(no_buffer_specified()));
|
return MinimalOp::Sync(Err(no_buffer_specified()));
|
||||||
}
|
}
|
||||||
|
let resource_table = isolate.resource_table.clone();
|
||||||
|
|
||||||
let state = state.clone();
|
|
||||||
let mut buf = zero_copy.unwrap();
|
let mut buf = zero_copy.unwrap();
|
||||||
|
|
||||||
if is_sync {
|
if is_sync {
|
||||||
MinimalOp::Sync({
|
MinimalOp::Sync({
|
||||||
// First we look up the rid in the resource table.
|
// First we look up the rid in the resource table.
|
||||||
let resource_table = &mut state.borrow_mut().resource_table;
|
let mut resource_table = resource_table.borrow_mut();
|
||||||
std_file_resource(resource_table, rid as u32, move |r| match r {
|
std_file_resource(&mut resource_table, rid as u32, move |r| match r {
|
||||||
Ok(std_file) => {
|
Ok(std_file) => {
|
||||||
use std::io::Read;
|
use std::io::Read;
|
||||||
std_file
|
std_file
|
||||||
|
@ -244,7 +238,7 @@ pub fn op_read(
|
||||||
} else {
|
} else {
|
||||||
MinimalOp::Async(
|
MinimalOp::Async(
|
||||||
poll_fn(move |cx| {
|
poll_fn(move |cx| {
|
||||||
let resource_table = &mut state.borrow_mut().resource_table;
|
let mut resource_table = resource_table.borrow_mut();
|
||||||
let resource_holder = resource_table
|
let resource_holder = resource_table
|
||||||
.get_mut::<StreamResourceHolder>(rid as u32)
|
.get_mut::<StreamResourceHolder>(rid as u32)
|
||||||
.ok_or_else(OpError::bad_resource_id)?;
|
.ok_or_else(OpError::bad_resource_id)?;
|
||||||
|
@ -334,7 +328,8 @@ impl DenoAsyncWrite for StreamResource {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn op_write(
|
pub fn op_write(
|
||||||
state: &State,
|
isolate: &mut deno_core::Isolate,
|
||||||
|
_state: &State,
|
||||||
is_sync: bool,
|
is_sync: bool,
|
||||||
rid: i32,
|
rid: i32,
|
||||||
zero_copy: Option<ZeroCopyBuf>,
|
zero_copy: Option<ZeroCopyBuf>,
|
||||||
|
@ -344,14 +339,13 @@ pub fn op_write(
|
||||||
return MinimalOp::Sync(Err(no_buffer_specified()));
|
return MinimalOp::Sync(Err(no_buffer_specified()));
|
||||||
}
|
}
|
||||||
|
|
||||||
let state = state.clone();
|
|
||||||
let buf = zero_copy.unwrap();
|
let buf = zero_copy.unwrap();
|
||||||
|
|
||||||
if is_sync {
|
if is_sync {
|
||||||
MinimalOp::Sync({
|
MinimalOp::Sync({
|
||||||
// First we look up the rid in the resource table.
|
// First we look up the rid in the resource table.
|
||||||
let resource_table = &mut state.borrow_mut().resource_table;
|
let mut resource_table = isolate.resource_table.borrow_mut();
|
||||||
std_file_resource(resource_table, rid as u32, move |r| match r {
|
std_file_resource(&mut resource_table, rid as u32, move |r| match r {
|
||||||
Ok(std_file) => {
|
Ok(std_file) => {
|
||||||
use std::io::Write;
|
use std::io::Write;
|
||||||
std_file
|
std_file
|
||||||
|
@ -365,10 +359,11 @@ pub fn op_write(
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
|
let resource_table = isolate.resource_table.clone();
|
||||||
MinimalOp::Async(
|
MinimalOp::Async(
|
||||||
async move {
|
async move {
|
||||||
let nwritten = poll_fn(|cx| {
|
let nwritten = poll_fn(|cx| {
|
||||||
let resource_table = &mut state.borrow_mut().resource_table;
|
let mut resource_table = resource_table.borrow_mut();
|
||||||
let resource_holder = resource_table
|
let resource_holder = resource_table
|
||||||
.get_mut::<StreamResourceHolder>(rid as u32)
|
.get_mut::<StreamResourceHolder>(rid as u32)
|
||||||
.ok_or_else(OpError::bad_resource_id)?;
|
.ok_or_else(OpError::bad_resource_id)?;
|
||||||
|
@ -381,7 +376,7 @@ pub fn op_write(
|
||||||
// Figure out why it's needed and preferably remove it.
|
// Figure out why it's needed and preferably remove it.
|
||||||
// https://github.com/denoland/deno/issues/3565
|
// https://github.com/denoland/deno/issues/3565
|
||||||
poll_fn(|cx| {
|
poll_fn(|cx| {
|
||||||
let resource_table = &mut state.borrow_mut().resource_table;
|
let mut resource_table = resource_table.borrow_mut();
|
||||||
let resource_holder = resource_table
|
let resource_holder = resource_table
|
||||||
.get_mut::<StreamResourceHolder>(rid as u32)
|
.get_mut::<StreamResourceHolder>(rid as u32)
|
||||||
.ok_or_else(OpError::bad_resource_id)?;
|
.ok_or_else(OpError::bad_resource_id)?;
|
||||||
|
|
107
cli/ops/net.rs
107
cli/ops/net.rs
|
@ -20,12 +20,12 @@ use tokio::net::UdpSocket;
|
||||||
use super::net_unix;
|
use super::net_unix;
|
||||||
|
|
||||||
pub fn init(i: &mut Isolate, s: &State) {
|
pub fn init(i: &mut Isolate, s: &State) {
|
||||||
i.register_op("op_accept", s.stateful_json_op(op_accept));
|
i.register_op("op_accept", s.stateful_json_op2(op_accept));
|
||||||
i.register_op("op_connect", s.stateful_json_op(op_connect));
|
i.register_op("op_connect", s.stateful_json_op2(op_connect));
|
||||||
i.register_op("op_shutdown", s.stateful_json_op(op_shutdown));
|
i.register_op("op_shutdown", s.stateful_json_op2(op_shutdown));
|
||||||
i.register_op("op_listen", s.stateful_json_op(op_listen));
|
i.register_op("op_listen", s.stateful_json_op2(op_listen));
|
||||||
i.register_op("op_receive", s.stateful_json_op(op_receive));
|
i.register_op("op_receive", s.stateful_json_op2(op_receive));
|
||||||
i.register_op("op_send", s.stateful_json_op(op_send));
|
i.register_op("op_send", s.stateful_json_op2(op_send));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
|
@ -35,25 +35,16 @@ struct AcceptArgs {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn accept_tcp(
|
fn accept_tcp(
|
||||||
state: &State,
|
isolate: &mut deno_core::Isolate,
|
||||||
args: AcceptArgs,
|
args: AcceptArgs,
|
||||||
_zero_copy: Option<ZeroCopyBuf>,
|
_zero_copy: Option<ZeroCopyBuf>,
|
||||||
) -> Result<JsonOp, OpError> {
|
) -> Result<JsonOp, OpError> {
|
||||||
let rid = args.rid as u32;
|
let rid = args.rid as u32;
|
||||||
let state_ = state.clone();
|
let resource_table = isolate.resource_table.clone();
|
||||||
{
|
|
||||||
let state = state.borrow();
|
|
||||||
state
|
|
||||||
.resource_table
|
|
||||||
.get::<TcpListenerResource>(rid)
|
|
||||||
.ok_or_else(OpError::bad_resource_id)?;
|
|
||||||
}
|
|
||||||
|
|
||||||
let state = state.clone();
|
|
||||||
|
|
||||||
let op = async move {
|
let op = async move {
|
||||||
let accept_fut = poll_fn(|cx| {
|
let accept_fut = poll_fn(|cx| {
|
||||||
let resource_table = &mut state.borrow_mut().resource_table;
|
let mut resource_table = resource_table.borrow_mut();
|
||||||
let listener_resource = resource_table
|
let listener_resource = resource_table
|
||||||
.get_mut::<TcpListenerResource>(rid)
|
.get_mut::<TcpListenerResource>(rid)
|
||||||
.ok_or_else(|| {
|
.ok_or_else(|| {
|
||||||
|
@ -78,8 +69,8 @@ fn accept_tcp(
|
||||||
let (tcp_stream, _socket_addr) = accept_fut.await?;
|
let (tcp_stream, _socket_addr) = accept_fut.await?;
|
||||||
let local_addr = tcp_stream.local_addr()?;
|
let local_addr = tcp_stream.local_addr()?;
|
||||||
let remote_addr = tcp_stream.peer_addr()?;
|
let remote_addr = tcp_stream.peer_addr()?;
|
||||||
let mut state = state_.borrow_mut();
|
let mut resource_table = resource_table.borrow_mut();
|
||||||
let rid = state.resource_table.add(
|
let rid = resource_table.add(
|
||||||
"tcpStream",
|
"tcpStream",
|
||||||
Box::new(StreamResourceHolder::new(StreamResource::TcpStream(Some(
|
Box::new(StreamResourceHolder::new(StreamResource::TcpStream(Some(
|
||||||
tcp_stream,
|
tcp_stream,
|
||||||
|
@ -104,15 +95,16 @@ fn accept_tcp(
|
||||||
}
|
}
|
||||||
|
|
||||||
fn op_accept(
|
fn op_accept(
|
||||||
state: &State,
|
isolate: &mut deno_core::Isolate,
|
||||||
|
_state: &State,
|
||||||
args: Value,
|
args: Value,
|
||||||
zero_copy: Option<ZeroCopyBuf>,
|
zero_copy: Option<ZeroCopyBuf>,
|
||||||
) -> Result<JsonOp, OpError> {
|
) -> Result<JsonOp, OpError> {
|
||||||
let args: AcceptArgs = serde_json::from_value(args)?;
|
let args: AcceptArgs = serde_json::from_value(args)?;
|
||||||
match args.transport.as_str() {
|
match args.transport.as_str() {
|
||||||
"tcp" => accept_tcp(state, args, zero_copy),
|
"tcp" => accept_tcp(isolate, args, zero_copy),
|
||||||
#[cfg(unix)]
|
#[cfg(unix)]
|
||||||
"unix" => net_unix::accept_unix(state, args.rid as u32, zero_copy),
|
"unix" => net_unix::accept_unix(isolate, args.rid as u32, zero_copy),
|
||||||
_ => Err(OpError::other(format!(
|
_ => Err(OpError::other(format!(
|
||||||
"Unsupported transport protocol {}",
|
"Unsupported transport protocol {}",
|
||||||
args.transport
|
args.transport
|
||||||
|
@ -127,7 +119,8 @@ struct ReceiveArgs {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn receive_udp(
|
fn receive_udp(
|
||||||
state: &State,
|
isolate: &mut deno_core::Isolate,
|
||||||
|
_state: &State,
|
||||||
args: ReceiveArgs,
|
args: ReceiveArgs,
|
||||||
zero_copy: Option<ZeroCopyBuf>,
|
zero_copy: Option<ZeroCopyBuf>,
|
||||||
) -> Result<JsonOp, OpError> {
|
) -> Result<JsonOp, OpError> {
|
||||||
|
@ -135,11 +128,11 @@ fn receive_udp(
|
||||||
|
|
||||||
let rid = args.rid as u32;
|
let rid = args.rid as u32;
|
||||||
|
|
||||||
let state_ = state.clone();
|
let resource_table = isolate.resource_table.clone();
|
||||||
|
|
||||||
let op = async move {
|
let op = async move {
|
||||||
let receive_fut = poll_fn(|cx| {
|
let receive_fut = poll_fn(|cx| {
|
||||||
let resource_table = &mut state_.borrow_mut().resource_table;
|
let mut resource_table = resource_table.borrow_mut();
|
||||||
let resource = resource_table
|
let resource = resource_table
|
||||||
.get_mut::<UdpSocketResource>(rid)
|
.get_mut::<UdpSocketResource>(rid)
|
||||||
.ok_or_else(|| {
|
.ok_or_else(|| {
|
||||||
|
@ -163,6 +156,7 @@ fn receive_udp(
|
||||||
}
|
}
|
||||||
|
|
||||||
fn op_receive(
|
fn op_receive(
|
||||||
|
isolate: &mut deno_core::Isolate,
|
||||||
state: &State,
|
state: &State,
|
||||||
args: Value,
|
args: Value,
|
||||||
zero_copy: Option<ZeroCopyBuf>,
|
zero_copy: Option<ZeroCopyBuf>,
|
||||||
|
@ -170,10 +164,10 @@ fn op_receive(
|
||||||
assert!(zero_copy.is_some());
|
assert!(zero_copy.is_some());
|
||||||
let args: ReceiveArgs = serde_json::from_value(args)?;
|
let args: ReceiveArgs = serde_json::from_value(args)?;
|
||||||
match args.transport.as_str() {
|
match args.transport.as_str() {
|
||||||
"udp" => receive_udp(state, args, zero_copy),
|
"udp" => receive_udp(isolate, state, args, zero_copy),
|
||||||
#[cfg(unix)]
|
#[cfg(unix)]
|
||||||
"unixpacket" => {
|
"unixpacket" => {
|
||||||
net_unix::receive_unix_packet(state, args.rid as u32, zero_copy)
|
net_unix::receive_unix_packet(isolate, args.rid as u32, zero_copy)
|
||||||
}
|
}
|
||||||
_ => Err(OpError::other(format!(
|
_ => Err(OpError::other(format!(
|
||||||
"Unsupported transport protocol {}",
|
"Unsupported transport protocol {}",
|
||||||
|
@ -191,13 +185,14 @@ struct SendArgs {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn op_send(
|
fn op_send(
|
||||||
|
isolate: &mut deno_core::Isolate,
|
||||||
state: &State,
|
state: &State,
|
||||||
args: Value,
|
args: Value,
|
||||||
zero_copy: Option<ZeroCopyBuf>,
|
zero_copy: Option<ZeroCopyBuf>,
|
||||||
) -> Result<JsonOp, OpError> {
|
) -> Result<JsonOp, OpError> {
|
||||||
assert!(zero_copy.is_some());
|
assert!(zero_copy.is_some());
|
||||||
let buf = zero_copy.unwrap();
|
let buf = zero_copy.unwrap();
|
||||||
let state_ = state.clone();
|
let resource_table = isolate.resource_table.clone();
|
||||||
match serde_json::from_value(args)? {
|
match serde_json::from_value(args)? {
|
||||||
SendArgs {
|
SendArgs {
|
||||||
rid,
|
rid,
|
||||||
|
@ -207,9 +202,8 @@ fn op_send(
|
||||||
state.check_net(&args.hostname, args.port)?;
|
state.check_net(&args.hostname, args.port)?;
|
||||||
|
|
||||||
let op = async move {
|
let op = async move {
|
||||||
let mut state = state_.borrow_mut();
|
let mut resource_table = resource_table.borrow_mut();
|
||||||
let resource = state
|
let resource = resource_table
|
||||||
.resource_table
|
|
||||||
.get_mut::<UdpSocketResource>(rid as u32)
|
.get_mut::<UdpSocketResource>(rid as u32)
|
||||||
.ok_or_else(|| {
|
.ok_or_else(|| {
|
||||||
OpError::bad_resource("Socket has been closed".to_string())
|
OpError::bad_resource("Socket has been closed".to_string())
|
||||||
|
@ -231,9 +225,8 @@ fn op_send(
|
||||||
let address_path = net_unix::Path::new(&args.address);
|
let address_path = net_unix::Path::new(&args.address);
|
||||||
state.check_read(&address_path)?;
|
state.check_read(&address_path)?;
|
||||||
let op = async move {
|
let op = async move {
|
||||||
let mut state = state_.borrow_mut();
|
let mut resource_table = resource_table.borrow_mut();
|
||||||
let resource = state
|
let resource = resource_table
|
||||||
.resource_table
|
|
||||||
.get_mut::<net_unix::UnixDatagramResource>(rid as u32)
|
.get_mut::<net_unix::UnixDatagramResource>(rid as u32)
|
||||||
.ok_or_else(|| {
|
.ok_or_else(|| {
|
||||||
OpError::other("Socket has been closed".to_string())
|
OpError::other("Socket has been closed".to_string())
|
||||||
|
@ -261,24 +254,25 @@ struct ConnectArgs {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn op_connect(
|
fn op_connect(
|
||||||
|
isolate: &mut deno_core::Isolate,
|
||||||
state: &State,
|
state: &State,
|
||||||
args: Value,
|
args: Value,
|
||||||
_zero_copy: Option<ZeroCopyBuf>,
|
_zero_copy: Option<ZeroCopyBuf>,
|
||||||
) -> Result<JsonOp, OpError> {
|
) -> Result<JsonOp, OpError> {
|
||||||
|
let resource_table = isolate.resource_table.clone();
|
||||||
match serde_json::from_value(args)? {
|
match serde_json::from_value(args)? {
|
||||||
ConnectArgs {
|
ConnectArgs {
|
||||||
transport,
|
transport,
|
||||||
transport_args: ArgsEnum::Ip(args),
|
transport_args: ArgsEnum::Ip(args),
|
||||||
} if transport == "tcp" => {
|
} if transport == "tcp" => {
|
||||||
let state_ = state.clone();
|
|
||||||
state.check_net(&args.hostname, args.port)?;
|
state.check_net(&args.hostname, args.port)?;
|
||||||
let op = async move {
|
let op = async move {
|
||||||
let addr = resolve_addr(&args.hostname, args.port)?;
|
let addr = resolve_addr(&args.hostname, args.port)?;
|
||||||
let tcp_stream = TcpStream::connect(&addr).await?;
|
let tcp_stream = TcpStream::connect(&addr).await?;
|
||||||
let local_addr = tcp_stream.local_addr()?;
|
let local_addr = tcp_stream.local_addr()?;
|
||||||
let remote_addr = tcp_stream.peer_addr()?;
|
let remote_addr = tcp_stream.peer_addr()?;
|
||||||
let mut state = state_.borrow_mut();
|
let mut resource_table = resource_table.borrow_mut();
|
||||||
let rid = state.resource_table.add(
|
let rid = resource_table.add(
|
||||||
"tcpStream",
|
"tcpStream",
|
||||||
Box::new(StreamResourceHolder::new(StreamResource::TcpStream(Some(
|
Box::new(StreamResourceHolder::new(StreamResource::TcpStream(Some(
|
||||||
tcp_stream,
|
tcp_stream,
|
||||||
|
@ -306,7 +300,6 @@ fn op_connect(
|
||||||
transport_args: ArgsEnum::Unix(args),
|
transport_args: ArgsEnum::Unix(args),
|
||||||
} if transport == "unix" => {
|
} if transport == "unix" => {
|
||||||
let address_path = net_unix::Path::new(&args.address);
|
let address_path = net_unix::Path::new(&args.address);
|
||||||
let state_ = state.clone();
|
|
||||||
state.check_read(&address_path)?;
|
state.check_read(&address_path)?;
|
||||||
let op = async move {
|
let op = async move {
|
||||||
let address = args.address;
|
let address = args.address;
|
||||||
|
@ -314,8 +307,8 @@ fn op_connect(
|
||||||
net_unix::UnixStream::connect(net_unix::Path::new(&address)).await?;
|
net_unix::UnixStream::connect(net_unix::Path::new(&address)).await?;
|
||||||
let local_addr = unix_stream.local_addr()?;
|
let local_addr = unix_stream.local_addr()?;
|
||||||
let remote_addr = unix_stream.peer_addr()?;
|
let remote_addr = unix_stream.peer_addr()?;
|
||||||
let mut state = state_.borrow_mut();
|
let mut resource_table = resource_table.borrow_mut();
|
||||||
let rid = state.resource_table.add(
|
let rid = resource_table.add(
|
||||||
"unixStream",
|
"unixStream",
|
||||||
Box::new(StreamResourceHolder::new(StreamResource::UnixStream(
|
Box::new(StreamResourceHolder::new(StreamResource::UnixStream(
|
||||||
unix_stream,
|
unix_stream,
|
||||||
|
@ -346,7 +339,8 @@ struct ShutdownArgs {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn op_shutdown(
|
fn op_shutdown(
|
||||||
state: &State,
|
isolate: &mut deno_core::Isolate,
|
||||||
|
_state: &State,
|
||||||
args: Value,
|
args: Value,
|
||||||
_zero_copy: Option<ZeroCopyBuf>,
|
_zero_copy: Option<ZeroCopyBuf>,
|
||||||
) -> Result<JsonOp, OpError> {
|
) -> Result<JsonOp, OpError> {
|
||||||
|
@ -361,9 +355,8 @@ fn op_shutdown(
|
||||||
_ => unimplemented!(),
|
_ => unimplemented!(),
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut state = state.borrow_mut();
|
let mut resource_table = isolate.resource_table.borrow_mut();
|
||||||
let resource_holder = state
|
let resource_holder = resource_table
|
||||||
.resource_table
|
|
||||||
.get_mut::<StreamResourceHolder>(rid)
|
.get_mut::<StreamResourceHolder>(rid)
|
||||||
.ok_or_else(OpError::bad_resource_id)?;
|
.ok_or_else(OpError::bad_resource_id)?;
|
||||||
match resource_holder.resource {
|
match resource_holder.resource {
|
||||||
|
@ -456,10 +449,9 @@ struct ListenArgs {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn listen_tcp(
|
fn listen_tcp(
|
||||||
state: &State,
|
resource_table: &mut ResourceTable,
|
||||||
addr: SocketAddr,
|
addr: SocketAddr,
|
||||||
) -> Result<(u32, SocketAddr), OpError> {
|
) -> Result<(u32, SocketAddr), OpError> {
|
||||||
let mut state = state.borrow_mut();
|
|
||||||
let std_listener = std::net::TcpListener::bind(&addr)?;
|
let std_listener = std::net::TcpListener::bind(&addr)?;
|
||||||
let listener = TcpListener::from_std(std_listener)?;
|
let listener = TcpListener::from_std(std_listener)?;
|
||||||
let local_addr = listener.local_addr()?;
|
let local_addr = listener.local_addr()?;
|
||||||
|
@ -468,34 +460,31 @@ fn listen_tcp(
|
||||||
waker: None,
|
waker: None,
|
||||||
local_addr,
|
local_addr,
|
||||||
};
|
};
|
||||||
let rid = state
|
let rid = resource_table.add("tcpListener", Box::new(listener_resource));
|
||||||
.resource_table
|
|
||||||
.add("tcpListener", Box::new(listener_resource));
|
|
||||||
|
|
||||||
Ok((rid, local_addr))
|
Ok((rid, local_addr))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn listen_udp(
|
fn listen_udp(
|
||||||
state: &State,
|
resource_table: &mut ResourceTable,
|
||||||
addr: SocketAddr,
|
addr: SocketAddr,
|
||||||
) -> Result<(u32, SocketAddr), OpError> {
|
) -> Result<(u32, SocketAddr), OpError> {
|
||||||
let mut state = state.borrow_mut();
|
|
||||||
let std_socket = std::net::UdpSocket::bind(&addr)?;
|
let std_socket = std::net::UdpSocket::bind(&addr)?;
|
||||||
let socket = UdpSocket::from_std(std_socket)?;
|
let socket = UdpSocket::from_std(std_socket)?;
|
||||||
let local_addr = socket.local_addr()?;
|
let local_addr = socket.local_addr()?;
|
||||||
let socket_resource = UdpSocketResource { socket };
|
let socket_resource = UdpSocketResource { socket };
|
||||||
let rid = state
|
let rid = resource_table.add("udpSocket", Box::new(socket_resource));
|
||||||
.resource_table
|
|
||||||
.add("udpSocket", Box::new(socket_resource));
|
|
||||||
|
|
||||||
Ok((rid, local_addr))
|
Ok((rid, local_addr))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn op_listen(
|
fn op_listen(
|
||||||
|
isolate: &mut deno_core::Isolate,
|
||||||
state: &State,
|
state: &State,
|
||||||
args: Value,
|
args: Value,
|
||||||
_zero_copy: Option<ZeroCopyBuf>,
|
_zero_copy: Option<ZeroCopyBuf>,
|
||||||
) -> Result<JsonOp, OpError> {
|
) -> Result<JsonOp, OpError> {
|
||||||
|
let mut resource_table = isolate.resource_table.borrow_mut();
|
||||||
match serde_json::from_value(args)? {
|
match serde_json::from_value(args)? {
|
||||||
ListenArgs {
|
ListenArgs {
|
||||||
transport,
|
transport,
|
||||||
|
@ -504,9 +493,9 @@ fn op_listen(
|
||||||
state.check_net(&args.hostname, args.port)?;
|
state.check_net(&args.hostname, args.port)?;
|
||||||
let addr = resolve_addr(&args.hostname, args.port)?;
|
let addr = resolve_addr(&args.hostname, args.port)?;
|
||||||
let (rid, local_addr) = if transport == "tcp" {
|
let (rid, local_addr) = if transport == "tcp" {
|
||||||
listen_tcp(state, addr)?
|
listen_tcp(&mut resource_table, addr)?
|
||||||
} else {
|
} else {
|
||||||
listen_udp(state, addr)?
|
listen_udp(&mut resource_table, addr)?
|
||||||
};
|
};
|
||||||
debug!(
|
debug!(
|
||||||
"New listener {} {}:{}",
|
"New listener {} {}:{}",
|
||||||
|
@ -531,9 +520,9 @@ fn op_listen(
|
||||||
let address_path = net_unix::Path::new(&args.address);
|
let address_path = net_unix::Path::new(&args.address);
|
||||||
state.check_read(&address_path)?;
|
state.check_read(&address_path)?;
|
||||||
let (rid, local_addr) = if transport == "unix" {
|
let (rid, local_addr) = if transport == "unix" {
|
||||||
net_unix::listen_unix(state, &address_path)?
|
net_unix::listen_unix(&mut resource_table, &address_path)?
|
||||||
} else {
|
} else {
|
||||||
net_unix::listen_unix_packet(state, &address_path)?
|
net_unix::listen_unix_packet(&mut resource_table, &address_path)?
|
||||||
};
|
};
|
||||||
debug!(
|
debug!(
|
||||||
"New listener {} {}",
|
"New listener {} {}",
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
use super::dispatch_json::{Deserialize, JsonOp};
|
use super::dispatch_json::{Deserialize, JsonOp};
|
||||||
use super::io::{StreamResource, StreamResourceHolder};
|
use super::io::{StreamResource, StreamResourceHolder};
|
||||||
use crate::op_error::OpError;
|
use crate::op_error::OpError;
|
||||||
use crate::state::State;
|
|
||||||
use futures::future::FutureExt;
|
use futures::future::FutureExt;
|
||||||
|
|
||||||
use deno_core::*;
|
use deno_core::*;
|
||||||
|
@ -27,31 +26,35 @@ pub struct UnixListenArgs {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn accept_unix(
|
pub fn accept_unix(
|
||||||
state: &State,
|
isolate: &mut deno_core::Isolate,
|
||||||
rid: u32,
|
rid: u32,
|
||||||
_zero_copy: Option<ZeroCopyBuf>,
|
_zero_copy: Option<ZeroCopyBuf>,
|
||||||
) -> Result<JsonOp, OpError> {
|
) -> Result<JsonOp, OpError> {
|
||||||
let state_ = state.clone();
|
let resource_table = isolate.resource_table.clone();
|
||||||
{
|
{
|
||||||
let state = state.borrow();
|
let _ = resource_table
|
||||||
state
|
.borrow()
|
||||||
.resource_table
|
|
||||||
.get::<UnixListenerResource>(rid)
|
.get::<UnixListenerResource>(rid)
|
||||||
.ok_or_else(OpError::bad_resource_id)?;
|
.ok_or_else(OpError::bad_resource_id)?;
|
||||||
}
|
}
|
||||||
let op = async move {
|
let op = async move {
|
||||||
let mut state = state_.borrow_mut();
|
let mut resource_table_ = resource_table.borrow_mut();
|
||||||
let listener_resource = state
|
let listener_resource = {
|
||||||
.resource_table
|
resource_table_
|
||||||
.get_mut::<UnixListenerResource>(rid)
|
.get_mut::<UnixListenerResource>(rid)
|
||||||
.ok_or_else(|| {
|
.ok_or_else(|| {
|
||||||
OpError::bad_resource("Listener has been closed".to_string())
|
OpError::bad_resource("Listener has been closed".to_string())
|
||||||
})?;
|
})?
|
||||||
|
};
|
||||||
|
|
||||||
let (unix_stream, _socket_addr) =
|
let (unix_stream, _socket_addr) =
|
||||||
listener_resource.listener.accept().await?;
|
listener_resource.listener.accept().await?;
|
||||||
|
drop(resource_table_);
|
||||||
|
|
||||||
let local_addr = unix_stream.local_addr()?;
|
let local_addr = unix_stream.local_addr()?;
|
||||||
let remote_addr = unix_stream.peer_addr()?;
|
let remote_addr = unix_stream.peer_addr()?;
|
||||||
let rid = state.resource_table.add(
|
let mut resource_table_ = resource_table.borrow_mut();
|
||||||
|
let rid = resource_table_.add(
|
||||||
"unixStream",
|
"unixStream",
|
||||||
Box::new(StreamResourceHolder::new(StreamResource::UnixStream(
|
Box::new(StreamResourceHolder::new(StreamResource::UnixStream(
|
||||||
unix_stream,
|
unix_stream,
|
||||||
|
@ -74,17 +77,16 @@ pub fn accept_unix(
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn receive_unix_packet(
|
pub fn receive_unix_packet(
|
||||||
state: &State,
|
isolate: &mut deno_core::Isolate,
|
||||||
rid: u32,
|
rid: u32,
|
||||||
zero_copy: Option<ZeroCopyBuf>,
|
zero_copy: Option<ZeroCopyBuf>,
|
||||||
) -> Result<JsonOp, OpError> {
|
) -> Result<JsonOp, OpError> {
|
||||||
let mut buf = zero_copy.unwrap();
|
let mut buf = zero_copy.unwrap();
|
||||||
let state_ = state.clone();
|
let resource_table = isolate.resource_table.clone();
|
||||||
|
|
||||||
let op = async move {
|
let op = async move {
|
||||||
let mut state = state_.borrow_mut();
|
let mut resource_table_ = resource_table.borrow_mut();
|
||||||
let resource = state
|
let resource = resource_table_
|
||||||
.resource_table
|
|
||||||
.get_mut::<UnixDatagramResource>(rid)
|
.get_mut::<UnixDatagramResource>(rid)
|
||||||
.ok_or_else(|| {
|
.ok_or_else(|| {
|
||||||
OpError::bad_resource("Socket has been closed".to_string())
|
OpError::bad_resource("Socket has been closed".to_string())
|
||||||
|
@ -103,28 +105,24 @@ pub fn receive_unix_packet(
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn listen_unix(
|
pub fn listen_unix(
|
||||||
state: &State,
|
resource_table: &mut ResourceTable,
|
||||||
addr: &Path,
|
addr: &Path,
|
||||||
) -> Result<(u32, unix::net::SocketAddr), OpError> {
|
) -> Result<(u32, unix::net::SocketAddr), OpError> {
|
||||||
let mut state = state.borrow_mut();
|
|
||||||
if addr.exists() {
|
if addr.exists() {
|
||||||
remove_file(&addr).unwrap();
|
remove_file(&addr).unwrap();
|
||||||
}
|
}
|
||||||
let listener = UnixListener::bind(&addr)?;
|
let listener = UnixListener::bind(&addr)?;
|
||||||
let local_addr = listener.local_addr()?;
|
let local_addr = listener.local_addr()?;
|
||||||
let listener_resource = UnixListenerResource { listener };
|
let listener_resource = UnixListenerResource { listener };
|
||||||
let rid = state
|
let rid = resource_table.add("unixListener", Box::new(listener_resource));
|
||||||
.resource_table
|
|
||||||
.add("unixListener", Box::new(listener_resource));
|
|
||||||
|
|
||||||
Ok((rid, local_addr))
|
Ok((rid, local_addr))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn listen_unix_packet(
|
pub fn listen_unix_packet(
|
||||||
state: &State,
|
resource_table: &mut ResourceTable,
|
||||||
addr: &Path,
|
addr: &Path,
|
||||||
) -> Result<(u32, unix::net::SocketAddr), OpError> {
|
) -> Result<(u32, unix::net::SocketAddr), OpError> {
|
||||||
let mut state = state.borrow_mut();
|
|
||||||
if addr.exists() {
|
if addr.exists() {
|
||||||
remove_file(&addr).unwrap();
|
remove_file(&addr).unwrap();
|
||||||
}
|
}
|
||||||
|
@ -134,9 +132,7 @@ pub fn listen_unix_packet(
|
||||||
socket,
|
socket,
|
||||||
local_addr: local_addr.clone(),
|
local_addr: local_addr.clone(),
|
||||||
};
|
};
|
||||||
let rid = state
|
let rid = resource_table.add("unixDatagram", Box::new(datagram_resource));
|
||||||
.resource_table
|
|
||||||
.add("unixDatagram", Box::new(datagram_resource));
|
|
||||||
|
|
||||||
Ok((rid, local_addr))
|
Ok((rid, local_addr))
|
||||||
}
|
}
|
||||||
|
|
|
@ -46,14 +46,10 @@ pub fn op_open_plugin(
|
||||||
|
|
||||||
let lib = open_plugin(filename).unwrap();
|
let lib = open_plugin(filename).unwrap();
|
||||||
let plugin_resource = PluginResource { lib };
|
let plugin_resource = PluginResource { lib };
|
||||||
let mut state_ = state.borrow_mut();
|
|
||||||
let rid = state_
|
let mut resource_table = isolate.resource_table.borrow_mut();
|
||||||
.resource_table
|
let rid = resource_table.add("plugin", Box::new(plugin_resource));
|
||||||
.add("plugin", Box::new(plugin_resource));
|
let plugin_resource = resource_table.get::<PluginResource>(rid).unwrap();
|
||||||
let plugin_resource = state_
|
|
||||||
.resource_table
|
|
||||||
.get_mut::<PluginResource>(rid)
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
let deno_plugin_init = *unsafe {
|
let deno_plugin_init = *unsafe {
|
||||||
plugin_resource
|
plugin_resource
|
||||||
|
@ -61,6 +57,8 @@ pub fn op_open_plugin(
|
||||||
.symbol::<PluginInitFn>("deno_plugin_init")
|
.symbol::<PluginInitFn>("deno_plugin_init")
|
||||||
}
|
}
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
drop(resource_table);
|
||||||
|
|
||||||
deno_plugin_init(isolate);
|
deno_plugin_init(isolate);
|
||||||
|
|
||||||
Ok(JsonOp::Sync(json!(rid)))
|
Ok(JsonOp::Sync(json!(rid)))
|
||||||
|
|
|
@ -15,15 +15,16 @@ use tokio::process::Command;
|
||||||
use std::os::unix::process::ExitStatusExt;
|
use std::os::unix::process::ExitStatusExt;
|
||||||
|
|
||||||
pub fn init(i: &mut Isolate, s: &State) {
|
pub fn init(i: &mut Isolate, s: &State) {
|
||||||
i.register_op("op_run", s.stateful_json_op(op_run));
|
i.register_op("op_run", s.stateful_json_op2(op_run));
|
||||||
i.register_op("op_run_status", s.stateful_json_op(op_run_status));
|
i.register_op("op_run_status", s.stateful_json_op2(op_run_status));
|
||||||
i.register_op("op_kill", s.stateful_json_op(op_kill));
|
i.register_op("op_kill", s.stateful_json_op(op_kill));
|
||||||
}
|
}
|
||||||
|
|
||||||
fn clone_file(rid: u32, state: &State) -> Result<std::fs::File, OpError> {
|
fn clone_file(
|
||||||
let mut state = state.borrow_mut();
|
rid: u32,
|
||||||
|
resource_table: &mut ResourceTable,
|
||||||
std_file_resource(&mut state.resource_table, rid, move |r| match r {
|
) -> Result<std::fs::File, OpError> {
|
||||||
|
std_file_resource(resource_table, rid, move |r| match r {
|
||||||
Ok(std_file) => std_file.try_clone().map_err(OpError::from),
|
Ok(std_file) => std_file.try_clone().map_err(OpError::from),
|
||||||
Err(_) => Err(OpError::bad_resource_id()),
|
Err(_) => Err(OpError::bad_resource_id()),
|
||||||
})
|
})
|
||||||
|
@ -57,6 +58,7 @@ struct ChildResource {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn op_run(
|
fn op_run(
|
||||||
|
isolate: &mut deno_core::Isolate,
|
||||||
state: &State,
|
state: &State,
|
||||||
args: Value,
|
args: Value,
|
||||||
_zero_copy: Option<ZeroCopyBuf>,
|
_zero_copy: Option<ZeroCopyBuf>,
|
||||||
|
@ -64,7 +66,7 @@ fn op_run(
|
||||||
let run_args: RunArgs = serde_json::from_value(args)?;
|
let run_args: RunArgs = serde_json::from_value(args)?;
|
||||||
|
|
||||||
state.check_run()?;
|
state.check_run()?;
|
||||||
let state_ = state.clone();
|
let mut resource_table = isolate.resource_table.borrow_mut();
|
||||||
|
|
||||||
let args = run_args.cmd;
|
let args = run_args.cmd;
|
||||||
let env = run_args.env;
|
let env = run_args.env;
|
||||||
|
@ -83,7 +85,7 @@ fn op_run(
|
||||||
// TODO: make this work with other resources, eg. sockets
|
// TODO: make this work with other resources, eg. sockets
|
||||||
let stdin_rid = run_args.stdin_rid;
|
let stdin_rid = run_args.stdin_rid;
|
||||||
if stdin_rid > 0 {
|
if stdin_rid > 0 {
|
||||||
let file = clone_file(stdin_rid, &state_)?;
|
let file = clone_file(stdin_rid, &mut resource_table)?;
|
||||||
c.stdin(file);
|
c.stdin(file);
|
||||||
} else {
|
} else {
|
||||||
c.stdin(subprocess_stdio_map(run_args.stdin.as_ref()));
|
c.stdin(subprocess_stdio_map(run_args.stdin.as_ref()));
|
||||||
|
@ -91,7 +93,7 @@ fn op_run(
|
||||||
|
|
||||||
let stdout_rid = run_args.stdout_rid;
|
let stdout_rid = run_args.stdout_rid;
|
||||||
if stdout_rid > 0 {
|
if stdout_rid > 0 {
|
||||||
let file = clone_file(stdout_rid, &state_)?;
|
let file = clone_file(stdout_rid, &mut resource_table)?;
|
||||||
c.stdout(file);
|
c.stdout(file);
|
||||||
} else {
|
} else {
|
||||||
c.stdout(subprocess_stdio_map(run_args.stdout.as_ref()));
|
c.stdout(subprocess_stdio_map(run_args.stdout.as_ref()));
|
||||||
|
@ -99,7 +101,7 @@ fn op_run(
|
||||||
|
|
||||||
let stderr_rid = run_args.stderr_rid;
|
let stderr_rid = run_args.stderr_rid;
|
||||||
if stderr_rid > 0 {
|
if stderr_rid > 0 {
|
||||||
let file = clone_file(stderr_rid, &state_)?;
|
let file = clone_file(stderr_rid, &mut resource_table)?;
|
||||||
c.stderr(file);
|
c.stderr(file);
|
||||||
} else {
|
} else {
|
||||||
c.stderr(subprocess_stdio_map(run_args.stderr.as_ref()));
|
c.stderr(subprocess_stdio_map(run_args.stderr.as_ref()));
|
||||||
|
@ -112,12 +114,9 @@ fn op_run(
|
||||||
let mut child = c.spawn()?;
|
let mut child = c.spawn()?;
|
||||||
let pid = child.id();
|
let pid = child.id();
|
||||||
|
|
||||||
let mut state = state_.borrow_mut();
|
|
||||||
let table = &mut state.resource_table;
|
|
||||||
|
|
||||||
let stdin_rid = match child.stdin.take() {
|
let stdin_rid = match child.stdin.take() {
|
||||||
Some(child_stdin) => {
|
Some(child_stdin) => {
|
||||||
let rid = table.add(
|
let rid = resource_table.add(
|
||||||
"childStdin",
|
"childStdin",
|
||||||
Box::new(StreamResourceHolder::new(StreamResource::ChildStdin(
|
Box::new(StreamResourceHolder::new(StreamResource::ChildStdin(
|
||||||
child_stdin,
|
child_stdin,
|
||||||
|
@ -130,7 +129,7 @@ fn op_run(
|
||||||
|
|
||||||
let stdout_rid = match child.stdout.take() {
|
let stdout_rid = match child.stdout.take() {
|
||||||
Some(child_stdout) => {
|
Some(child_stdout) => {
|
||||||
let rid = table.add(
|
let rid = resource_table.add(
|
||||||
"childStdout",
|
"childStdout",
|
||||||
Box::new(StreamResourceHolder::new(StreamResource::ChildStdout(
|
Box::new(StreamResourceHolder::new(StreamResource::ChildStdout(
|
||||||
child_stdout,
|
child_stdout,
|
||||||
|
@ -143,7 +142,7 @@ fn op_run(
|
||||||
|
|
||||||
let stderr_rid = match child.stderr.take() {
|
let stderr_rid = match child.stderr.take() {
|
||||||
Some(child_stderr) => {
|
Some(child_stderr) => {
|
||||||
let rid = table.add(
|
let rid = resource_table.add(
|
||||||
"childStderr",
|
"childStderr",
|
||||||
Box::new(StreamResourceHolder::new(StreamResource::ChildStderr(
|
Box::new(StreamResourceHolder::new(StreamResource::ChildStderr(
|
||||||
child_stderr,
|
child_stderr,
|
||||||
|
@ -155,7 +154,7 @@ fn op_run(
|
||||||
};
|
};
|
||||||
|
|
||||||
let child_resource = ChildResource { child };
|
let child_resource = ChildResource { child };
|
||||||
let child_rid = table.add("child", Box::new(child_resource));
|
let child_rid = resource_table.add("child", Box::new(child_resource));
|
||||||
|
|
||||||
Ok(JsonOp::Sync(json!({
|
Ok(JsonOp::Sync(json!({
|
||||||
"rid": child_rid,
|
"rid": child_rid,
|
||||||
|
@ -173,6 +172,7 @@ struct RunStatusArgs {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn op_run_status(
|
fn op_run_status(
|
||||||
|
isolate: &mut deno_core::Isolate,
|
||||||
state: &State,
|
state: &State,
|
||||||
args: Value,
|
args: Value,
|
||||||
_zero_copy: Option<ZeroCopyBuf>,
|
_zero_copy: Option<ZeroCopyBuf>,
|
||||||
|
@ -181,11 +181,11 @@ fn op_run_status(
|
||||||
let rid = args.rid as u32;
|
let rid = args.rid as u32;
|
||||||
|
|
||||||
state.check_run()?;
|
state.check_run()?;
|
||||||
let state = state.clone();
|
let resource_table = isolate.resource_table.clone();
|
||||||
|
|
||||||
let future = async move {
|
let future = async move {
|
||||||
let run_status = poll_fn(|cx| {
|
let run_status = poll_fn(|cx| {
|
||||||
let resource_table = &mut state.borrow_mut().resource_table;
|
let mut resource_table = resource_table.borrow_mut();
|
||||||
let child_resource = resource_table
|
let child_resource = resource_table
|
||||||
.get_mut::<ChildResource>(rid)
|
.get_mut::<ChildResource>(rid)
|
||||||
.ok_or_else(OpError::bad_resource_id)?;
|
.ok_or_else(OpError::bad_resource_id)?;
|
||||||
|
|
|
@ -9,8 +9,8 @@ use std::sync::Arc;
|
||||||
use std::sync::Mutex;
|
use std::sync::Mutex;
|
||||||
|
|
||||||
pub fn init(i: &mut Isolate, s: &State) {
|
pub fn init(i: &mut Isolate, s: &State) {
|
||||||
i.register_op("op_repl_start", s.stateful_json_op(op_repl_start));
|
i.register_op("op_repl_start", s.stateful_json_op2(op_repl_start));
|
||||||
i.register_op("op_repl_readline", s.stateful_json_op(op_repl_readline));
|
i.register_op("op_repl_readline", s.stateful_json_op2(op_repl_readline));
|
||||||
}
|
}
|
||||||
|
|
||||||
struct ReplResource(Arc<Mutex<Repl>>);
|
struct ReplResource(Arc<Mutex<Repl>>);
|
||||||
|
@ -22,19 +22,19 @@ struct ReplStartArgs {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn op_repl_start(
|
fn op_repl_start(
|
||||||
|
isolate: &mut deno_core::Isolate,
|
||||||
state: &State,
|
state: &State,
|
||||||
args: Value,
|
args: Value,
|
||||||
_zero_copy: Option<ZeroCopyBuf>,
|
_zero_copy: Option<ZeroCopyBuf>,
|
||||||
) -> Result<JsonOp, OpError> {
|
) -> Result<JsonOp, OpError> {
|
||||||
let args: ReplStartArgs = serde_json::from_value(args)?;
|
let args: ReplStartArgs = serde_json::from_value(args)?;
|
||||||
|
|
||||||
debug!("op_repl_start {}", args.history_file);
|
debug!("op_repl_start {}", args.history_file);
|
||||||
let history_path =
|
let history_path =
|
||||||
repl::history_path(&state.borrow().global_state.dir, &args.history_file);
|
repl::history_path(&state.borrow().global_state.dir, &args.history_file);
|
||||||
let repl = repl::Repl::new(history_path);
|
let repl = repl::Repl::new(history_path);
|
||||||
let mut state = state.borrow_mut();
|
|
||||||
let resource = ReplResource(Arc::new(Mutex::new(repl)));
|
let resource = ReplResource(Arc::new(Mutex::new(repl)));
|
||||||
let rid = state.resource_table.add("repl", Box::new(resource));
|
let mut resource_table = isolate.resource_table.borrow_mut();
|
||||||
|
let rid = resource_table.add("repl", Box::new(resource));
|
||||||
Ok(JsonOp::Sync(json!(rid)))
|
Ok(JsonOp::Sync(json!(rid)))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -45,7 +45,8 @@ struct ReplReadlineArgs {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn op_repl_readline(
|
fn op_repl_readline(
|
||||||
state: &State,
|
isolate: &mut deno_core::Isolate,
|
||||||
|
_state: &State,
|
||||||
args: Value,
|
args: Value,
|
||||||
_zero_copy: Option<ZeroCopyBuf>,
|
_zero_copy: Option<ZeroCopyBuf>,
|
||||||
) -> Result<JsonOp, OpError> {
|
) -> Result<JsonOp, OpError> {
|
||||||
|
@ -53,9 +54,8 @@ fn op_repl_readline(
|
||||||
let rid = args.rid as u32;
|
let rid = args.rid as u32;
|
||||||
let prompt = args.prompt;
|
let prompt = args.prompt;
|
||||||
debug!("op_repl_readline {} {}", rid, prompt);
|
debug!("op_repl_readline {} {}", rid, prompt);
|
||||||
let state = state.borrow();
|
let resource_table = isolate.resource_table.borrow();
|
||||||
let resource = state
|
let resource = resource_table
|
||||||
.resource_table
|
|
||||||
.get::<ReplResource>(rid)
|
.get::<ReplResource>(rid)
|
||||||
.ok_or_else(OpError::bad_resource_id)?;
|
.ok_or_else(OpError::bad_resource_id)?;
|
||||||
let repl = resource.0.clone();
|
let repl = resource.0.clone();
|
||||||
|
|
|
@ -5,23 +5,24 @@ use crate::state::State;
|
||||||
use deno_core::*;
|
use deno_core::*;
|
||||||
|
|
||||||
pub fn init(i: &mut Isolate, s: &State) {
|
pub fn init(i: &mut Isolate, s: &State) {
|
||||||
i.register_op("op_resources", s.stateful_json_op(op_resources));
|
i.register_op("op_resources", s.stateful_json_op2(op_resources));
|
||||||
i.register_op("op_close", s.stateful_json_op(op_close));
|
i.register_op("op_close", s.stateful_json_op2(op_close));
|
||||||
}
|
}
|
||||||
|
|
||||||
fn op_resources(
|
fn op_resources(
|
||||||
state: &State,
|
isolate: &mut deno_core::Isolate,
|
||||||
|
_state: &State,
|
||||||
_args: Value,
|
_args: Value,
|
||||||
_zero_copy: Option<ZeroCopyBuf>,
|
_zero_copy: Option<ZeroCopyBuf>,
|
||||||
) -> Result<JsonOp, OpError> {
|
) -> Result<JsonOp, OpError> {
|
||||||
let state = state.borrow();
|
let serialized_resources = isolate.resource_table.borrow().entries();
|
||||||
let serialized_resources = state.resource_table.entries();
|
|
||||||
Ok(JsonOp::Sync(json!(serialized_resources)))
|
Ok(JsonOp::Sync(json!(serialized_resources)))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// op_close removes a resource from the resource table.
|
/// op_close removes a resource from the resource table.
|
||||||
fn op_close(
|
fn op_close(
|
||||||
state: &State,
|
isolate: &mut deno_core::Isolate,
|
||||||
|
_state: &State,
|
||||||
args: Value,
|
args: Value,
|
||||||
_zero_copy: Option<ZeroCopyBuf>,
|
_zero_copy: Option<ZeroCopyBuf>,
|
||||||
) -> Result<JsonOp, OpError> {
|
) -> Result<JsonOp, OpError> {
|
||||||
|
@ -30,9 +31,8 @@ fn op_close(
|
||||||
rid: i32,
|
rid: i32,
|
||||||
}
|
}
|
||||||
let args: CloseArgs = serde_json::from_value(args).unwrap();
|
let args: CloseArgs = serde_json::from_value(args).unwrap();
|
||||||
let mut state = state.borrow_mut();
|
let mut resource_table = isolate.resource_table.borrow_mut();
|
||||||
state
|
resource_table
|
||||||
.resource_table
|
|
||||||
.close(args.rid as u32)
|
.close(args.rid as u32)
|
||||||
.ok_or_else(OpError::bad_resource_id)?;
|
.ok_or_else(OpError::bad_resource_id)?;
|
||||||
Ok(JsonOp::Sync(json!({})))
|
Ok(JsonOp::Sync(json!({})))
|
||||||
|
|
|
@ -14,9 +14,9 @@ use std::task::Waker;
|
||||||
use tokio::signal::unix::{signal, Signal, SignalKind};
|
use tokio::signal::unix::{signal, Signal, SignalKind};
|
||||||
|
|
||||||
pub fn init(i: &mut Isolate, s: &State) {
|
pub fn init(i: &mut Isolate, s: &State) {
|
||||||
i.register_op("op_signal_bind", s.stateful_json_op(op_signal_bind));
|
i.register_op("op_signal_bind", s.stateful_json_op2(op_signal_bind));
|
||||||
i.register_op("op_signal_unbind", s.stateful_json_op(op_signal_unbind));
|
i.register_op("op_signal_unbind", s.stateful_json_op2(op_signal_unbind));
|
||||||
i.register_op("op_signal_poll", s.stateful_json_op(op_signal_poll));
|
i.register_op("op_signal_poll", s.stateful_json_op2(op_signal_poll));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(unix)]
|
#[cfg(unix)]
|
||||||
|
@ -38,13 +38,14 @@ struct SignalArgs {
|
||||||
|
|
||||||
#[cfg(unix)]
|
#[cfg(unix)]
|
||||||
fn op_signal_bind(
|
fn op_signal_bind(
|
||||||
state: &State,
|
isolate: &mut deno_core::Isolate,
|
||||||
|
_state: &State,
|
||||||
args: Value,
|
args: Value,
|
||||||
_zero_copy: Option<ZeroCopyBuf>,
|
_zero_copy: Option<ZeroCopyBuf>,
|
||||||
) -> Result<JsonOp, OpError> {
|
) -> Result<JsonOp, OpError> {
|
||||||
let args: BindSignalArgs = serde_json::from_value(args)?;
|
let args: BindSignalArgs = serde_json::from_value(args)?;
|
||||||
let mut state = state.borrow_mut();
|
let mut resource_table = isolate.resource_table.borrow_mut();
|
||||||
let rid = state.resource_table.add(
|
let rid = resource_table.add(
|
||||||
"signal",
|
"signal",
|
||||||
Box::new(SignalStreamResource(
|
Box::new(SignalStreamResource(
|
||||||
signal(SignalKind::from_raw(args.signo)).expect(""),
|
signal(SignalKind::from_raw(args.signo)).expect(""),
|
||||||
|
@ -58,18 +59,19 @@ fn op_signal_bind(
|
||||||
|
|
||||||
#[cfg(unix)]
|
#[cfg(unix)]
|
||||||
fn op_signal_poll(
|
fn op_signal_poll(
|
||||||
state: &State,
|
isolate: &mut deno_core::Isolate,
|
||||||
|
_state: &State,
|
||||||
args: Value,
|
args: Value,
|
||||||
_zero_copy: Option<ZeroCopyBuf>,
|
_zero_copy: Option<ZeroCopyBuf>,
|
||||||
) -> Result<JsonOp, OpError> {
|
) -> Result<JsonOp, OpError> {
|
||||||
let args: SignalArgs = serde_json::from_value(args)?;
|
let args: SignalArgs = serde_json::from_value(args)?;
|
||||||
let rid = args.rid as u32;
|
let rid = args.rid as u32;
|
||||||
let state_ = state.clone();
|
let resource_table = isolate.resource_table.clone();
|
||||||
|
|
||||||
let future = poll_fn(move |cx| {
|
let future = poll_fn(move |cx| {
|
||||||
let mut state = state_.borrow_mut();
|
let mut resource_table = resource_table.borrow_mut();
|
||||||
if let Some(mut signal) =
|
if let Some(mut signal) =
|
||||||
state.resource_table.get_mut::<SignalStreamResource>(rid)
|
resource_table.get_mut::<SignalStreamResource>(rid)
|
||||||
{
|
{
|
||||||
signal.1 = Some(cx.waker().clone());
|
signal.1 = Some(cx.waker().clone());
|
||||||
return signal.0.poll_recv(cx);
|
return signal.0.poll_recv(cx);
|
||||||
|
@ -83,14 +85,15 @@ fn op_signal_poll(
|
||||||
|
|
||||||
#[cfg(unix)]
|
#[cfg(unix)]
|
||||||
pub fn op_signal_unbind(
|
pub fn op_signal_unbind(
|
||||||
state: &State,
|
isolate: &mut deno_core::Isolate,
|
||||||
|
_state: &State,
|
||||||
args: Value,
|
args: Value,
|
||||||
_zero_copy: Option<ZeroCopyBuf>,
|
_zero_copy: Option<ZeroCopyBuf>,
|
||||||
) -> Result<JsonOp, OpError> {
|
) -> Result<JsonOp, OpError> {
|
||||||
let args: SignalArgs = serde_json::from_value(args)?;
|
let args: SignalArgs = serde_json::from_value(args)?;
|
||||||
let rid = args.rid as u32;
|
let rid = args.rid as u32;
|
||||||
let mut state = state.borrow_mut();
|
let mut resource_table = isolate.resource_table.borrow_mut();
|
||||||
let resource = state.resource_table.get::<SignalStreamResource>(rid);
|
let resource = resource_table.get::<SignalStreamResource>(rid);
|
||||||
if let Some(signal) = resource {
|
if let Some(signal) = resource {
|
||||||
if let Some(waker) = &signal.1 {
|
if let Some(waker) = &signal.1 {
|
||||||
// Wakes up the pending poll if exists.
|
// Wakes up the pending poll if exists.
|
||||||
|
@ -98,8 +101,7 @@ pub fn op_signal_unbind(
|
||||||
waker.clone().wake();
|
waker.clone().wake();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
state
|
resource_table
|
||||||
.resource_table
|
|
||||||
.close(rid)
|
.close(rid)
|
||||||
.ok_or_else(OpError::bad_resource_id)?;
|
.ok_or_else(OpError::bad_resource_id)?;
|
||||||
Ok(JsonOp::Sync(json!({})))
|
Ok(JsonOp::Sync(json!({})))
|
||||||
|
@ -107,6 +109,7 @@ pub fn op_signal_unbind(
|
||||||
|
|
||||||
#[cfg(not(unix))]
|
#[cfg(not(unix))]
|
||||||
pub fn op_signal_bind(
|
pub fn op_signal_bind(
|
||||||
|
_isolate: &mut deno_core::Isolate,
|
||||||
_state: &State,
|
_state: &State,
|
||||||
_args: Value,
|
_args: Value,
|
||||||
_zero_copy: Option<ZeroCopyBuf>,
|
_zero_copy: Option<ZeroCopyBuf>,
|
||||||
|
@ -116,6 +119,7 @@ pub fn op_signal_bind(
|
||||||
|
|
||||||
#[cfg(not(unix))]
|
#[cfg(not(unix))]
|
||||||
fn op_signal_unbind(
|
fn op_signal_unbind(
|
||||||
|
_isolate: &mut deno_core::Isolate,
|
||||||
_state: &State,
|
_state: &State,
|
||||||
_args: Value,
|
_args: Value,
|
||||||
_zero_copy: Option<ZeroCopyBuf>,
|
_zero_copy: Option<ZeroCopyBuf>,
|
||||||
|
@ -125,6 +129,7 @@ fn op_signal_unbind(
|
||||||
|
|
||||||
#[cfg(not(unix))]
|
#[cfg(not(unix))]
|
||||||
fn op_signal_poll(
|
fn op_signal_poll(
|
||||||
|
_isolate: &mut deno_core::Isolate,
|
||||||
_state: &State,
|
_state: &State,
|
||||||
_args: Value,
|
_args: Value,
|
||||||
_zero_copy: Option<ZeroCopyBuf>,
|
_zero_copy: Option<ZeroCopyBuf>,
|
||||||
|
|
|
@ -28,10 +28,10 @@ use tokio_rustls::{
|
||||||
use webpki::DNSNameRef;
|
use webpki::DNSNameRef;
|
||||||
|
|
||||||
pub fn init(i: &mut Isolate, s: &State) {
|
pub fn init(i: &mut Isolate, s: &State) {
|
||||||
i.register_op("op_start_tls", s.stateful_json_op(op_start_tls));
|
i.register_op("op_start_tls", s.stateful_json_op2(op_start_tls));
|
||||||
i.register_op("op_connect_tls", s.stateful_json_op(op_connect_tls));
|
i.register_op("op_connect_tls", s.stateful_json_op2(op_connect_tls));
|
||||||
i.register_op("op_listen_tls", s.stateful_json_op(op_listen_tls));
|
i.register_op("op_listen_tls", s.stateful_json_op2(op_listen_tls));
|
||||||
i.register_op("op_accept_tls", s.stateful_json_op(op_accept_tls));
|
i.register_op("op_accept_tls", s.stateful_json_op2(op_accept_tls));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
|
@ -52,14 +52,15 @@ struct StartTLSArgs {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn op_start_tls(
|
pub fn op_start_tls(
|
||||||
state: &State,
|
isolate: &mut deno_core::Isolate,
|
||||||
|
_state: &State,
|
||||||
args: Value,
|
args: Value,
|
||||||
_zero_copy: Option<ZeroCopyBuf>,
|
_zero_copy: Option<ZeroCopyBuf>,
|
||||||
) -> Result<JsonOp, OpError> {
|
) -> Result<JsonOp, OpError> {
|
||||||
let args: StartTLSArgs = serde_json::from_value(args)?;
|
let args: StartTLSArgs = serde_json::from_value(args)?;
|
||||||
let rid = args.rid as u32;
|
let rid = args.rid as u32;
|
||||||
let cert_file = args.cert_file.clone();
|
let cert_file = args.cert_file.clone();
|
||||||
let state_ = state.clone();
|
let resource_table = isolate.resource_table.clone();
|
||||||
|
|
||||||
let mut domain = args.hostname;
|
let mut domain = args.hostname;
|
||||||
if domain.is_empty() {
|
if domain.is_empty() {
|
||||||
|
@ -67,12 +68,12 @@ pub fn op_start_tls(
|
||||||
}
|
}
|
||||||
|
|
||||||
let op = async move {
|
let op = async move {
|
||||||
let mut state = state_.borrow_mut();
|
let mut resource_holder = {
|
||||||
|
let mut resource_table_ = resource_table.borrow_mut();
|
||||||
let mut resource_holder =
|
match resource_table_.remove::<StreamResourceHolder>(rid) {
|
||||||
match state.resource_table.remove::<StreamResourceHolder>(rid) {
|
|
||||||
Some(resource) => *resource,
|
Some(resource) => *resource,
|
||||||
None => return Err(OpError::bad_resource_id()),
|
None => return Err(OpError::bad_resource_id()),
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
if let StreamResource::TcpStream(ref mut tcp_stream) =
|
if let StreamResource::TcpStream(ref mut tcp_stream) =
|
||||||
|
@ -96,7 +97,8 @@ pub fn op_start_tls(
|
||||||
DNSNameRef::try_from_ascii_str(&domain).expect("Invalid DNS lookup");
|
DNSNameRef::try_from_ascii_str(&domain).expect("Invalid DNS lookup");
|
||||||
let tls_stream = tls_connector.connect(dnsname, tcp_stream).await?;
|
let tls_stream = tls_connector.connect(dnsname, tcp_stream).await?;
|
||||||
|
|
||||||
let rid = state.resource_table.add(
|
let mut resource_table_ = resource_table.borrow_mut();
|
||||||
|
let rid = resource_table_.add(
|
||||||
"clientTlsStream",
|
"clientTlsStream",
|
||||||
Box::new(StreamResourceHolder::new(StreamResource::ClientTlsStream(
|
Box::new(StreamResourceHolder::new(StreamResource::ClientTlsStream(
|
||||||
Box::new(tls_stream),
|
Box::new(tls_stream),
|
||||||
|
@ -123,13 +125,14 @@ pub fn op_start_tls(
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn op_connect_tls(
|
pub fn op_connect_tls(
|
||||||
|
isolate: &mut deno_core::Isolate,
|
||||||
state: &State,
|
state: &State,
|
||||||
args: Value,
|
args: Value,
|
||||||
_zero_copy: Option<ZeroCopyBuf>,
|
_zero_copy: Option<ZeroCopyBuf>,
|
||||||
) -> Result<JsonOp, OpError> {
|
) -> Result<JsonOp, OpError> {
|
||||||
let args: ConnectTLSArgs = serde_json::from_value(args)?;
|
let args: ConnectTLSArgs = serde_json::from_value(args)?;
|
||||||
let cert_file = args.cert_file.clone();
|
let cert_file = args.cert_file.clone();
|
||||||
let state_ = state.clone();
|
let resource_table = isolate.resource_table.clone();
|
||||||
state.check_net(&args.hostname, args.port)?;
|
state.check_net(&args.hostname, args.port)?;
|
||||||
if let Some(path) = cert_file.clone() {
|
if let Some(path) = cert_file.clone() {
|
||||||
state.check_read(Path::new(&path))?;
|
state.check_read(Path::new(&path))?;
|
||||||
|
@ -158,8 +161,8 @@ pub fn op_connect_tls(
|
||||||
let dnsname =
|
let dnsname =
|
||||||
DNSNameRef::try_from_ascii_str(&domain).expect("Invalid DNS lookup");
|
DNSNameRef::try_from_ascii_str(&domain).expect("Invalid DNS lookup");
|
||||||
let tls_stream = tls_connector.connect(dnsname, tcp_stream).await?;
|
let tls_stream = tls_connector.connect(dnsname, tcp_stream).await?;
|
||||||
let mut state = state_.borrow_mut();
|
let mut resource_table_ = resource_table.borrow_mut();
|
||||||
let rid = state.resource_table.add(
|
let rid = resource_table_.add(
|
||||||
"clientTlsStream",
|
"clientTlsStream",
|
||||||
Box::new(StreamResourceHolder::new(StreamResource::ClientTlsStream(
|
Box::new(StreamResourceHolder::new(StreamResource::ClientTlsStream(
|
||||||
Box::new(tls_stream),
|
Box::new(tls_stream),
|
||||||
|
@ -298,6 +301,7 @@ struct ListenTlsArgs {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn op_listen_tls(
|
fn op_listen_tls(
|
||||||
|
isolate: &mut deno_core::Isolate,
|
||||||
state: &State,
|
state: &State,
|
||||||
args: Value,
|
args: Value,
|
||||||
_zero_copy: Option<ZeroCopyBuf>,
|
_zero_copy: Option<ZeroCopyBuf>,
|
||||||
|
@ -327,10 +331,9 @@ fn op_listen_tls(
|
||||||
waker: None,
|
waker: None,
|
||||||
local_addr,
|
local_addr,
|
||||||
};
|
};
|
||||||
let mut state = state.borrow_mut();
|
|
||||||
let rid = state
|
let mut resource_table = isolate.resource_table.borrow_mut();
|
||||||
.resource_table
|
let rid = resource_table.add("tlsListener", Box::new(tls_listener_resource));
|
||||||
.add("tlsListener", Box::new(tls_listener_resource));
|
|
||||||
|
|
||||||
Ok(JsonOp::Sync(json!({
|
Ok(JsonOp::Sync(json!({
|
||||||
"rid": rid,
|
"rid": rid,
|
||||||
|
@ -348,16 +351,17 @@ struct AcceptTlsArgs {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn op_accept_tls(
|
fn op_accept_tls(
|
||||||
state: &State,
|
isolate: &mut deno_core::Isolate,
|
||||||
|
_state: &State,
|
||||||
args: Value,
|
args: Value,
|
||||||
_zero_copy: Option<ZeroCopyBuf>,
|
_zero_copy: Option<ZeroCopyBuf>,
|
||||||
) -> Result<JsonOp, OpError> {
|
) -> Result<JsonOp, OpError> {
|
||||||
let args: AcceptTlsArgs = serde_json::from_value(args)?;
|
let args: AcceptTlsArgs = serde_json::from_value(args)?;
|
||||||
let rid = args.rid as u32;
|
let rid = args.rid as u32;
|
||||||
let state = state.clone();
|
let resource_table = isolate.resource_table.clone();
|
||||||
let op = async move {
|
let op = async move {
|
||||||
let accept_fut = poll_fn(|cx| {
|
let accept_fut = poll_fn(|cx| {
|
||||||
let resource_table = &mut state.borrow_mut().resource_table;
|
let mut resource_table = resource_table.borrow_mut();
|
||||||
let listener_resource = resource_table
|
let listener_resource = resource_table
|
||||||
.get_mut::<TlsListenerResource>(rid)
|
.get_mut::<TlsListenerResource>(rid)
|
||||||
.ok_or_else(|| {
|
.ok_or_else(|| {
|
||||||
|
@ -383,9 +387,8 @@ fn op_accept_tls(
|
||||||
let local_addr = tcp_stream.local_addr()?;
|
let local_addr = tcp_stream.local_addr()?;
|
||||||
let remote_addr = tcp_stream.peer_addr()?;
|
let remote_addr = tcp_stream.peer_addr()?;
|
||||||
let tls_acceptor = {
|
let tls_acceptor = {
|
||||||
let state = state.borrow();
|
let resource_table = resource_table.borrow();
|
||||||
let resource = state
|
let resource = resource_table
|
||||||
.resource_table
|
|
||||||
.get::<TlsListenerResource>(rid)
|
.get::<TlsListenerResource>(rid)
|
||||||
.ok_or_else(OpError::bad_resource_id)
|
.ok_or_else(OpError::bad_resource_id)
|
||||||
.expect("Can't find tls listener");
|
.expect("Can't find tls listener");
|
||||||
|
@ -393,8 +396,8 @@ fn op_accept_tls(
|
||||||
};
|
};
|
||||||
let tls_stream = tls_acceptor.accept(tcp_stream).await?;
|
let tls_stream = tls_acceptor.accept(tcp_stream).await?;
|
||||||
let rid = {
|
let rid = {
|
||||||
let mut state = state.borrow_mut();
|
let mut resource_table = resource_table.borrow_mut();
|
||||||
state.resource_table.add(
|
resource_table.add(
|
||||||
"serverTlsStream",
|
"serverTlsStream",
|
||||||
Box::new(StreamResourceHolder::new(StreamResource::ServerTlsStream(
|
Box::new(StreamResourceHolder::new(StreamResource::ServerTlsStream(
|
||||||
Box::new(tls_stream),
|
Box::new(tls_stream),
|
||||||
|
|
|
@ -2,7 +2,6 @@ use super::dispatch_json::JsonOp;
|
||||||
use super::io::std_file_resource;
|
use super::io::std_file_resource;
|
||||||
use super::io::{StreamResource, StreamResourceHolder};
|
use super::io::{StreamResource, StreamResourceHolder};
|
||||||
use crate::op_error::OpError;
|
use crate::op_error::OpError;
|
||||||
use crate::ops::json_op;
|
|
||||||
use crate::state::State;
|
use crate::state::State;
|
||||||
use deno_core::*;
|
use deno_core::*;
|
||||||
#[cfg(unix)]
|
#[cfg(unix)]
|
||||||
|
@ -35,8 +34,8 @@ fn get_windows_handle(
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn init(i: &mut Isolate, s: &State) {
|
pub fn init(i: &mut Isolate, s: &State) {
|
||||||
i.register_op("op_set_raw", s.core_op(json_op(s.stateful_op(op_set_raw))));
|
i.register_op("op_set_raw", s.stateful_json_op2(op_set_raw));
|
||||||
i.register_op("op_isatty", s.core_op(json_op(s.stateful_op(op_isatty))));
|
i.register_op("op_isatty", s.stateful_json_op2(op_isatty));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
|
@ -46,7 +45,8 @@ struct SetRawArgs {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn op_set_raw(
|
pub fn op_set_raw(
|
||||||
state_: &State,
|
isolate: &mut deno_core::Isolate,
|
||||||
|
_state: &State,
|
||||||
args: Value,
|
args: Value,
|
||||||
_zero_copy: Option<ZeroCopyBuf>,
|
_zero_copy: Option<ZeroCopyBuf>,
|
||||||
) -> Result<JsonOp, OpError> {
|
) -> Result<JsonOp, OpError> {
|
||||||
|
@ -65,9 +65,8 @@ pub fn op_set_raw(
|
||||||
use winapi::shared::minwindef::FALSE;
|
use winapi::shared::minwindef::FALSE;
|
||||||
use winapi::um::{consoleapi, handleapi};
|
use winapi::um::{consoleapi, handleapi};
|
||||||
|
|
||||||
let mut state = state_.borrow_mut();
|
let mut resource_table = isolate.resource_table.borrow_mut();
|
||||||
let resource_holder =
|
let resource_holder = resource_table.get_mut::<StreamResourceHolder>(rid);
|
||||||
state.resource_table.get_mut::<StreamResourceHolder>(rid);
|
|
||||||
if resource_holder.is_none() {
|
if resource_holder.is_none() {
|
||||||
return Err(OpError::bad_resource_id());
|
return Err(OpError::bad_resource_id());
|
||||||
}
|
}
|
||||||
|
@ -132,9 +131,8 @@ pub fn op_set_raw(
|
||||||
{
|
{
|
||||||
use std::os::unix::io::AsRawFd;
|
use std::os::unix::io::AsRawFd;
|
||||||
|
|
||||||
let mut state = state_.borrow_mut();
|
let mut resource_table = isolate.resource_table.borrow_mut();
|
||||||
let resource_holder =
|
let resource_holder = resource_table.get_mut::<StreamResourceHolder>(rid);
|
||||||
state.resource_table.get_mut::<StreamResourceHolder>(rid);
|
|
||||||
if resource_holder.is_none() {
|
if resource_holder.is_none() {
|
||||||
return Err(OpError::bad_resource_id());
|
return Err(OpError::bad_resource_id());
|
||||||
}
|
}
|
||||||
|
@ -215,16 +213,17 @@ struct IsattyArgs {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn op_isatty(
|
pub fn op_isatty(
|
||||||
state: &State,
|
isolate: &mut deno_core::Isolate,
|
||||||
|
_state: &State,
|
||||||
args: Value,
|
args: Value,
|
||||||
_zero_copy: Option<ZeroCopyBuf>,
|
_zero_copy: Option<ZeroCopyBuf>,
|
||||||
) -> Result<JsonOp, OpError> {
|
) -> Result<JsonOp, OpError> {
|
||||||
let args: IsattyArgs = serde_json::from_value(args)?;
|
let args: IsattyArgs = serde_json::from_value(args)?;
|
||||||
let rid = args.rid;
|
let rid = args.rid;
|
||||||
|
|
||||||
let resource_table = &mut state.borrow_mut().resource_table;
|
let mut resource_table = isolate.resource_table.borrow_mut();
|
||||||
let isatty: bool =
|
let isatty: bool =
|
||||||
std_file_resource(resource_table, rid as u32, move |r| match r {
|
std_file_resource(&mut resource_table, rid as u32, move |r| match r {
|
||||||
Ok(std_file) => {
|
Ok(std_file) => {
|
||||||
#[cfg(windows)]
|
#[cfg(windows)]
|
||||||
{
|
{
|
||||||
|
|
|
@ -43,14 +43,6 @@ fn create_web_worker(
|
||||||
let state =
|
let state =
|
||||||
State::new_for_worker(global_state, Some(permissions), specifier)?;
|
State::new_for_worker(global_state, Some(permissions), specifier)?;
|
||||||
|
|
||||||
if has_deno_namespace {
|
|
||||||
let mut s = state.borrow_mut();
|
|
||||||
let (stdin, stdout, stderr) = get_stdio();
|
|
||||||
s.resource_table.add("stdin", Box::new(stdin));
|
|
||||||
s.resource_table.add("stdout", Box::new(stdout));
|
|
||||||
s.resource_table.add("stderr", Box::new(stderr));
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut worker = WebWorker::new(
|
let mut worker = WebWorker::new(
|
||||||
name.clone(),
|
name.clone(),
|
||||||
startup_data::deno_isolate_init(),
|
startup_data::deno_isolate_init(),
|
||||||
|
@ -58,6 +50,14 @@ fn create_web_worker(
|
||||||
has_deno_namespace,
|
has_deno_namespace,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
if has_deno_namespace {
|
||||||
|
let mut resource_table = worker.resource_table.borrow_mut();
|
||||||
|
let (stdin, stdout, stderr) = get_stdio();
|
||||||
|
resource_table.add("stdin", Box::new(stdin));
|
||||||
|
resource_table.add("stdout", Box::new(stdout));
|
||||||
|
resource_table.add("stderr", Box::new(stderr));
|
||||||
|
}
|
||||||
|
|
||||||
// Instead of using name for log we use `worker-${id}` because
|
// Instead of using name for log we use `worker-${id}` because
|
||||||
// WebWorkers can have empty string as name.
|
// WebWorkers can have empty string as name.
|
||||||
let script = format!(
|
let script = format!(
|
||||||
|
|
44
cli/state.rs
44
cli/state.rs
|
@ -14,7 +14,6 @@ use deno_core::ErrBox;
|
||||||
use deno_core::ModuleLoader;
|
use deno_core::ModuleLoader;
|
||||||
use deno_core::ModuleSpecifier;
|
use deno_core::ModuleSpecifier;
|
||||||
use deno_core::Op;
|
use deno_core::Op;
|
||||||
use deno_core::ResourceTable;
|
|
||||||
use deno_core::ZeroCopyBuf;
|
use deno_core::ZeroCopyBuf;
|
||||||
use futures::future::FutureExt;
|
use futures::future::FutureExt;
|
||||||
use rand::rngs::StdRng;
|
use rand::rngs::StdRng;
|
||||||
|
@ -64,7 +63,6 @@ pub struct StateInner {
|
||||||
pub next_worker_id: u32,
|
pub next_worker_id: u32,
|
||||||
pub start_time: Instant,
|
pub start_time: Instant,
|
||||||
pub seeded_rng: Option<StdRng>,
|
pub seeded_rng: Option<StdRng>,
|
||||||
pub resource_table: ResourceTable,
|
|
||||||
pub target_lib: TargetLib,
|
pub target_lib: TargetLib,
|
||||||
pub debug_type: DebugType,
|
pub debug_type: DebugType,
|
||||||
}
|
}
|
||||||
|
@ -81,7 +79,25 @@ impl State {
|
||||||
self.core_op(json_op(self.stateful_op(dispatcher)))
|
self.core_op(json_op(self.stateful_op(dispatcher)))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn stateful_json_op2<D>(
|
||||||
|
&self,
|
||||||
|
dispatcher: D,
|
||||||
|
) -> impl Fn(&mut deno_core::Isolate, &[u8], Option<ZeroCopyBuf>) -> Op
|
||||||
|
where
|
||||||
|
D: Fn(
|
||||||
|
&mut deno_core::Isolate,
|
||||||
|
&State,
|
||||||
|
Value,
|
||||||
|
Option<ZeroCopyBuf>,
|
||||||
|
) -> Result<JsonOp, OpError>,
|
||||||
|
{
|
||||||
|
use crate::ops::json_op;
|
||||||
|
self.core_op(json_op(self.stateful_op2(dispatcher)))
|
||||||
|
}
|
||||||
|
|
||||||
/// Wrap core `OpDispatcher` to collect metrics.
|
/// Wrap core `OpDispatcher` to collect metrics.
|
||||||
|
// TODO(ry) this should be private. Is called by stateful_json_op or
|
||||||
|
// stateful_minimal_op
|
||||||
pub fn core_op<D>(
|
pub fn core_op<D>(
|
||||||
&self,
|
&self,
|
||||||
dispatcher: D,
|
dispatcher: D,
|
||||||
|
@ -142,19 +158,29 @@ impl State {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// This is a special function that provides `state` argument to dispatcher.
|
pub fn stateful_minimal_op2<D>(
|
||||||
pub fn stateful_minimal_op<D>(
|
|
||||||
&self,
|
&self,
|
||||||
dispatcher: D,
|
dispatcher: D,
|
||||||
) -> impl Fn(bool, i32, Option<ZeroCopyBuf>) -> MinimalOp
|
) -> impl Fn(&mut deno_core::Isolate, &[u8], Option<ZeroCopyBuf>) -> Op
|
||||||
where
|
where
|
||||||
D: Fn(&State, bool, i32, Option<ZeroCopyBuf>) -> MinimalOp,
|
D: Fn(
|
||||||
|
&mut deno_core::Isolate,
|
||||||
|
&State,
|
||||||
|
bool,
|
||||||
|
i32,
|
||||||
|
Option<ZeroCopyBuf>,
|
||||||
|
) -> MinimalOp,
|
||||||
{
|
{
|
||||||
let state = self.clone();
|
let state = self.clone();
|
||||||
move |is_sync: bool,
|
self.core_op(crate::ops::minimal_op(
|
||||||
|
move |isolate: &mut deno_core::Isolate,
|
||||||
|
is_sync: bool,
|
||||||
rid: i32,
|
rid: i32,
|
||||||
zero_copy: Option<ZeroCopyBuf>|
|
zero_copy: Option<ZeroCopyBuf>|
|
||||||
-> MinimalOp { dispatcher(&state, is_sync, rid, zero_copy) }
|
-> MinimalOp {
|
||||||
|
dispatcher(isolate, &state, is_sync, rid, zero_copy)
|
||||||
|
},
|
||||||
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// This is a special function that provides `state` argument to dispatcher.
|
/// This is a special function that provides `state` argument to dispatcher.
|
||||||
|
@ -300,7 +326,6 @@ impl State {
|
||||||
next_worker_id: 0,
|
next_worker_id: 0,
|
||||||
start_time: Instant::now(),
|
start_time: Instant::now(),
|
||||||
seeded_rng,
|
seeded_rng,
|
||||||
resource_table: ResourceTable::default(),
|
|
||||||
target_lib: TargetLib::Main,
|
target_lib: TargetLib::Main,
|
||||||
debug_type,
|
debug_type,
|
||||||
}));
|
}));
|
||||||
|
@ -336,7 +361,6 @@ impl State {
|
||||||
next_worker_id: 0,
|
next_worker_id: 0,
|
||||||
start_time: Instant::now(),
|
start_time: Instant::now(),
|
||||||
seeded_rng,
|
seeded_rng,
|
||||||
resource_table: ResourceTable::default(),
|
|
||||||
target_lib: TargetLib::Worker,
|
target_lib: TargetLib::Worker,
|
||||||
debug_type: DebugType::Dependent,
|
debug_type: DebugType::Dependent,
|
||||||
}));
|
}));
|
||||||
|
|
|
@ -209,6 +209,19 @@ impl Future for Worker {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Deref for Worker {
|
||||||
|
type Target = deno_core::EsIsolate;
|
||||||
|
fn deref(&self) -> &Self::Target {
|
||||||
|
&self.isolate
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl DerefMut for Worker {
|
||||||
|
fn deref_mut(&mut self) -> &mut Self::Target {
|
||||||
|
&mut self.isolate
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// This worker is created and used by Deno executable.
|
/// This worker is created and used by Deno executable.
|
||||||
///
|
///
|
||||||
/// It provides ops available in the `Deno` namespace.
|
/// It provides ops available in the `Deno` namespace.
|
||||||
|
|
|
@ -13,6 +13,7 @@ use crate::js_errors::JSError;
|
||||||
use crate::ops::*;
|
use crate::ops::*;
|
||||||
use crate::shared_queue::SharedQueue;
|
use crate::shared_queue::SharedQueue;
|
||||||
use crate::shared_queue::RECOMMENDED_SIZE;
|
use crate::shared_queue::RECOMMENDED_SIZE;
|
||||||
|
use crate::ResourceTable;
|
||||||
use futures::future::FutureExt;
|
use futures::future::FutureExt;
|
||||||
use futures::stream::select;
|
use futures::stream::select;
|
||||||
use futures::stream::FuturesUnordered;
|
use futures::stream::FuturesUnordered;
|
||||||
|
@ -20,6 +21,7 @@ use futures::stream::StreamExt;
|
||||||
use futures::task::AtomicWaker;
|
use futures::task::AtomicWaker;
|
||||||
use futures::Future;
|
use futures::Future;
|
||||||
use libc::c_void;
|
use libc::c_void;
|
||||||
|
use std::cell::RefCell;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::convert::From;
|
use std::convert::From;
|
||||||
use std::error::Error;
|
use std::error::Error;
|
||||||
|
@ -28,6 +30,7 @@ use std::mem::forget;
|
||||||
use std::ops::{Deref, DerefMut};
|
use std::ops::{Deref, DerefMut};
|
||||||
use std::option::Option;
|
use std::option::Option;
|
||||||
use std::pin::Pin;
|
use std::pin::Pin;
|
||||||
|
use std::rc::Rc;
|
||||||
use std::sync::{Arc, Mutex, Once};
|
use std::sync::{Arc, Mutex, Once};
|
||||||
use std::task::Context;
|
use std::task::Context;
|
||||||
use std::task::Poll;
|
use std::task::Poll;
|
||||||
|
@ -163,6 +166,7 @@ pub struct Isolate {
|
||||||
snapshot_creator: Option<v8::SnapshotCreator>,
|
snapshot_creator: Option<v8::SnapshotCreator>,
|
||||||
has_snapshotted: bool,
|
has_snapshotted: bool,
|
||||||
snapshot: Option<SnapshotConfig>,
|
snapshot: Option<SnapshotConfig>,
|
||||||
|
pub resource_table: Rc<RefCell<ResourceTable>>,
|
||||||
pub global_context: v8::Global<v8::Context>,
|
pub global_context: v8::Global<v8::Context>,
|
||||||
pub(crate) shared_ab: v8::Global<v8::SharedArrayBuffer>,
|
pub(crate) shared_ab: v8::Global<v8::SharedArrayBuffer>,
|
||||||
pub(crate) js_recv_cb: v8::Global<v8::Function>,
|
pub(crate) js_recv_cb: v8::Global<v8::Function>,
|
||||||
|
@ -297,6 +301,7 @@ impl Isolate {
|
||||||
let core_isolate = Self {
|
let core_isolate = Self {
|
||||||
v8_isolate: None,
|
v8_isolate: None,
|
||||||
global_context,
|
global_context,
|
||||||
|
resource_table: Rc::new(RefCell::new(ResourceTable::default())),
|
||||||
pending_promise_exceptions: HashMap::new(),
|
pending_promise_exceptions: HashMap::new(),
|
||||||
shared_ab: v8::Global::<v8::SharedArrayBuffer>::new(),
|
shared_ab: v8::Global::<v8::SharedArrayBuffer>::new(),
|
||||||
js_recv_cb: v8::Global::<v8::Function>::new(),
|
js_recv_cb: v8::Global::<v8::Function>::new(),
|
||||||
|
|
Loading…
Reference in a new issue