From 7c2e7c660804afca823d60e6496aa853f75db16c Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Thu, 10 Sep 2020 09:57:45 -0400 Subject: Use gotham-like state for ops (#7385) Provides a concrete state type that can be dynamically added. This is necessary for op crates. * renames BasicState to OpState * async ops take `Rc>` * sync ops take `&mut OpState` * removes `OpRegistry`, `OpRouter` traits * `get_error_class_fn` moved to OpState * ResourceTable moved to OpState --- cli/ops/process.rs | 46 ++++++++++++++++++++++------------------------ 1 file changed, 22 insertions(+), 24 deletions(-) (limited to 'cli/ops/process.rs') diff --git a/cli/ops/process.rs b/cli/ops/process.rs index fb8675db9..6a7e05ebf 100644 --- a/cli/ops/process.rs +++ b/cli/ops/process.rs @@ -2,28 +2,28 @@ use super::io::{std_file_resource, StreamResource, StreamResourceHolder}; use crate::signal::kill; -use crate::state::State; use deno_core::BufVec; use deno_core::ErrBox; -use deno_core::OpRegistry; +use deno_core::OpState; use deno_core::ZeroCopyBuf; use futures::future::poll_fn; use futures::future::FutureExt; use serde_derive::Deserialize; use serde_json::Value; +use std::cell::RefCell; use std::rc::Rc; use tokio::process::Command; #[cfg(unix)] use std::os::unix::process::ExitStatusExt; -pub fn init(s: &Rc) { - s.register_op_json_sync("op_run", op_run); - s.register_op_json_async("op_run_status", op_run_status); - s.register_op_json_sync("op_kill", op_kill); +pub fn init(rt: &mut deno_core::JsRuntime) { + super::reg_json_sync(rt, "op_run", op_run); + super::reg_json_async(rt, "op_run_status", op_run_status); + super::reg_json_sync(rt, "op_kill", op_kill); } -fn clone_file(state: &State, rid: u32) -> Result { +fn clone_file(state: &mut OpState, rid: u32) -> Result { std_file_resource(state, rid, move |r| match r { Ok(std_file) => std_file.try_clone().map_err(ErrBox::from), Err(_) => Err(ErrBox::bad_resource_id()), @@ -58,13 +58,12 @@ struct ChildResource { } fn op_run( - state: &State, + state: &mut OpState, args: Value, _zero_copy: &mut [ZeroCopyBuf], ) -> Result { let run_args: RunArgs = serde_json::from_value(args)?; - - state.check_run()?; + super::cli_state(state).check_run()?; let args = run_args.cmd; let env = run_args.env; @@ -111,7 +110,7 @@ fn op_run( let stdin_rid = match child.stdin.take() { Some(child_stdin) => { - let rid = state.resource_table.borrow_mut().add( + let rid = state.resource_table.add( "childStdin", Box::new(StreamResourceHolder::new(StreamResource::ChildStdin( child_stdin, @@ -124,7 +123,7 @@ fn op_run( let stdout_rid = match child.stdout.take() { Some(child_stdout) => { - let rid = state.resource_table.borrow_mut().add( + let rid = state.resource_table.add( "childStdout", Box::new(StreamResourceHolder::new(StreamResource::ChildStdout( child_stdout, @@ -137,7 +136,7 @@ fn op_run( let stderr_rid = match child.stderr.take() { Some(child_stderr) => { - let rid = state.resource_table.borrow_mut().add( + let rid = state.resource_table.add( "childStderr", Box::new(StreamResourceHolder::new(StreamResource::ChildStderr( child_stderr, @@ -149,10 +148,7 @@ fn op_run( }; let child_resource = ChildResource { child }; - let child_rid = state - .resource_table - .borrow_mut() - .add("child", Box::new(child_resource)); + let child_rid = state.resource_table.add("child", Box::new(child_resource)); Ok(json!({ "rid": child_rid, @@ -170,18 +166,19 @@ struct RunStatusArgs { } async fn op_run_status( - state: Rc, + state: Rc>, args: Value, _zero_copy: BufVec, ) -> Result { let args: RunStatusArgs = serde_json::from_value(args)?; let rid = args.rid as u32; - state.check_run()?; + super::cli_state2(&state).check_run()?; let run_status = poll_fn(|cx| { - let mut resource_table = state.resource_table.borrow_mut(); - let child_resource = resource_table + let mut state = state.borrow_mut(); + let child_resource = state + .resource_table .get_mut::(rid) .ok_or_else(ErrBox::bad_resource_id)?; let child = &mut child_resource.child; @@ -215,12 +212,13 @@ struct KillArgs { } fn op_kill( - state: &State, + state: &mut OpState, args: Value, _zero_copy: &mut [ZeroCopyBuf], ) -> Result { - state.check_unstable("Deno.kill"); - state.check_run()?; + let cli_state = super::cli_state(state); + cli_state.check_unstable("Deno.kill"); + cli_state.check_run()?; let args: KillArgs = serde_json::from_value(args)?; kill(args.pid, args.signo)?; -- cgit v1.2.3