diff options
author | Ryan Dahl <ry@tinyclouds.org> | 2020-09-10 09:57:45 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-09-10 09:57:45 -0400 |
commit | 7c2e7c660804afca823d60e6496aa853f75db16c (patch) | |
tree | b7746b181c1564c6b1abd2e906662f9e6b008417 /cli/ops/os.rs | |
parent | 6f70e6e72ba2d5c1de7495adac37c1e4f4e86b24 (diff) |
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<RefCell<OpState>>`
* sync ops take `&mut OpState`
* removes `OpRegistry`, `OpRouter` traits
* `get_error_class_fn` moved to OpState
* ResourceTable moved to OpState
Diffstat (limited to 'cli/ops/os.rs')
-rw-r--r-- | cli/ops/os.rs | 81 |
1 files changed, 44 insertions, 37 deletions
diff --git a/cli/ops/os.rs b/cli/ops/os.rs index a38b5b08a..9860018a0 100644 --- a/cli/ops/os.rs +++ b/cli/ops/os.rs @@ -1,36 +1,35 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. -use crate::state::State; use deno_core::ErrBox; -use deno_core::OpRegistry; +use deno_core::OpState; use deno_core::ZeroCopyBuf; use serde_derive::Deserialize; use serde_json::Value; use std::collections::HashMap; use std::env; -use std::rc::Rc; use url::Url; -pub fn init(s: &Rc<State>) { - s.register_op_json_sync("op_exit", op_exit); - s.register_op_json_sync("op_env", op_env); - s.register_op_json_sync("op_exec_path", op_exec_path); - s.register_op_json_sync("op_set_env", op_set_env); - s.register_op_json_sync("op_get_env", op_get_env); - s.register_op_json_sync("op_delete_env", op_delete_env); - s.register_op_json_sync("op_hostname", op_hostname); - s.register_op_json_sync("op_loadavg", op_loadavg); - s.register_op_json_sync("op_os_release", op_os_release); - s.register_op_json_sync("op_system_memory_info", op_system_memory_info); +pub fn init(rt: &mut deno_core::JsRuntime) { + super::reg_json_sync(rt, "op_exit", op_exit); + super::reg_json_sync(rt, "op_env", op_env); + super::reg_json_sync(rt, "op_exec_path", op_exec_path); + super::reg_json_sync(rt, "op_set_env", op_set_env); + super::reg_json_sync(rt, "op_get_env", op_get_env); + super::reg_json_sync(rt, "op_delete_env", op_delete_env); + super::reg_json_sync(rt, "op_hostname", op_hostname); + super::reg_json_sync(rt, "op_loadavg", op_loadavg); + super::reg_json_sync(rt, "op_os_release", op_os_release); + super::reg_json_sync(rt, "op_system_memory_info", op_system_memory_info); } fn op_exec_path( - state: &State, + state: &mut OpState, _args: Value, _zero_copy: &mut [ZeroCopyBuf], ) -> Result<Value, ErrBox> { let current_exe = env::current_exe().unwrap(); - state.check_read_blind(¤t_exe, "exec_path")?; + let cli_state = super::cli_state(state); + cli_state.check_read_blind(¤t_exe, "exec_path")?; // Now apply URL parser to current exe to get fully resolved path, otherwise // we might get `./` and `../` bits in `exec_path` let exe_url = Url::from_file_path(current_exe).unwrap(); @@ -45,22 +44,24 @@ struct SetEnv { } fn op_set_env( - state: &State, + state: &mut OpState, args: Value, _zero_copy: &mut [ZeroCopyBuf], ) -> Result<Value, ErrBox> { let args: SetEnv = serde_json::from_value(args)?; - state.check_env()?; + let cli_state = super::cli_state(state); + cli_state.check_env()?; env::set_var(args.key, args.value); Ok(json!({})) } fn op_env( - state: &State, + state: &mut OpState, _args: Value, _zero_copy: &mut [ZeroCopyBuf], ) -> Result<Value, ErrBox> { - state.check_env()?; + let cli_state = super::cli_state(state); + cli_state.check_env()?; let v = env::vars().collect::<HashMap<String, String>>(); Ok(json!(v)) } @@ -71,12 +72,13 @@ struct GetEnv { } fn op_get_env( - state: &State, + state: &mut OpState, args: Value, _zero_copy: &mut [ZeroCopyBuf], ) -> Result<Value, ErrBox> { let args: GetEnv = serde_json::from_value(args)?; - state.check_env()?; + let cli_state = super::cli_state(state); + cli_state.check_env()?; let r = match env::var(args.key) { Err(env::VarError::NotPresent) => json!([]), v => json!([v?]), @@ -90,12 +92,13 @@ struct DeleteEnv { } fn op_delete_env( - state: &State, + state: &mut OpState, args: Value, _zero_copy: &mut [ZeroCopyBuf], ) -> Result<Value, ErrBox> { let args: DeleteEnv = serde_json::from_value(args)?; - state.check_env()?; + let cli_state = super::cli_state(state); + cli_state.check_env()?; env::remove_var(args.key); Ok(json!({})) } @@ -106,7 +109,7 @@ struct Exit { } fn op_exit( - _state: &State, + _state: &mut OpState, args: Value, _zero_copy: &mut [ZeroCopyBuf], ) -> Result<Value, ErrBox> { @@ -115,12 +118,13 @@ fn op_exit( } fn op_loadavg( - state: &State, + state: &mut OpState, _args: Value, _zero_copy: &mut [ZeroCopyBuf], ) -> Result<Value, ErrBox> { - state.check_unstable("Deno.loadavg"); - state.check_env()?; + let cli_state = super::cli_state(state); + cli_state.check_unstable("Deno.loadavg"); + cli_state.check_env()?; match sys_info::loadavg() { Ok(loadavg) => Ok(json!([loadavg.one, loadavg.five, loadavg.fifteen])), Err(_) => Ok(json!([0f64, 0f64, 0f64])), @@ -128,34 +132,37 @@ fn op_loadavg( } fn op_hostname( - state: &State, + state: &mut OpState, _args: Value, _zero_copy: &mut [ZeroCopyBuf], ) -> Result<Value, ErrBox> { - state.check_unstable("Deno.hostname"); - state.check_env()?; + let cli_state = super::cli_state(state); + cli_state.check_unstable("Deno.hostname"); + cli_state.check_env()?; let hostname = sys_info::hostname().unwrap_or_else(|_| "".to_string()); Ok(json!(hostname)) } fn op_os_release( - state: &State, + state: &mut OpState, _args: Value, _zero_copy: &mut [ZeroCopyBuf], ) -> Result<Value, ErrBox> { - state.check_unstable("Deno.osRelease"); - state.check_env()?; + let cli_state = super::cli_state(state); + cli_state.check_unstable("Deno.osRelease"); + cli_state.check_env()?; let release = sys_info::os_release().unwrap_or_else(|_| "".to_string()); Ok(json!(release)) } fn op_system_memory_info( - state: &State, + state: &mut OpState, _args: Value, _zero_copy: &mut [ZeroCopyBuf], ) -> Result<Value, ErrBox> { - state.check_unstable("Deno.systemMemoryInfo"); - state.check_env()?; + let cli_state = super::cli_state(state); + cli_state.check_unstable("Deno.systemMemoryInfo"); + cli_state.check_env()?; match sys_info::mem_info() { Ok(info) => Ok(json!({ "total": info.total, |