summaryrefslogtreecommitdiff
path: root/cli/ops/os.rs
diff options
context:
space:
mode:
authorBartek IwaƄczuk <biwanczuk@gmail.com>2020-09-20 01:17:35 +0200
committerGitHub <noreply@github.com>2020-09-20 01:17:35 +0200
commitb657d743a22802b8232fbf558f2f00bf2942096f (patch)
treee3a3047e78b6bd9c9e3f551f99f5e80e32de079d /cli/ops/os.rs
parentaaa5e6613a739f8e2ff7579b69c2504bcdc37d4f (diff)
refactor: remove CliState, use OpState, add CliModuleLoader (#7588)
- remove "CliState.workers" and "CliState.next_worker_id", instead store them on "OpState" using type aliases. - remove "CliState.global_timer" and "CliState.start_time", instead store them on "OpState" using type aliases. - remove "CliState.is_internal", instead pass it to Worker::new - move "CliState::permissions" to "OpState" - move "CliState::main_module" to "OpState" - move "CliState::global_state" to "OpState" - move "CliState::check_unstable()" to "GlobalState" - change "cli_state()" to "global_state()" - change "deno_core::ModuleLoader" trait to pass "OpState" to callbacks - rename "CliState" to "CliModuleLoader"
Diffstat (limited to 'cli/ops/os.rs')
-rw-r--r--cli/ops/os.rs34
1 files changed, 16 insertions, 18 deletions
diff --git a/cli/ops/os.rs b/cli/ops/os.rs
index d47d126ab..42d09da86 100644
--- a/cli/ops/os.rs
+++ b/cli/ops/os.rs
@@ -1,5 +1,6 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
+use crate::permissions::Permissions;
use deno_core::error::AnyError;
use deno_core::url::Url;
use deno_core::OpState;
@@ -28,8 +29,9 @@ fn op_exec_path(
_zero_copy: &mut [ZeroCopyBuf],
) -> Result<Value, AnyError> {
let current_exe = env::current_exe().unwrap();
- let cli_state = super::cli_state(state);
- cli_state.check_read_blind(&current_exe, "exec_path")?;
+ state
+ .borrow::<Permissions>()
+ .check_read_blind(&current_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();
@@ -49,8 +51,7 @@ fn op_set_env(
_zero_copy: &mut [ZeroCopyBuf],
) -> Result<Value, AnyError> {
let args: SetEnv = serde_json::from_value(args)?;
- let cli_state = super::cli_state(state);
- cli_state.check_env()?;
+ state.borrow::<Permissions>().check_env()?;
env::set_var(args.key, args.value);
Ok(json!({}))
}
@@ -60,8 +61,7 @@ fn op_env(
_args: Value,
_zero_copy: &mut [ZeroCopyBuf],
) -> Result<Value, AnyError> {
- let cli_state = super::cli_state(state);
- cli_state.check_env()?;
+ state.borrow::<Permissions>().check_env()?;
let v = env::vars().collect::<HashMap<String, String>>();
Ok(json!(v))
}
@@ -77,8 +77,7 @@ fn op_get_env(
_zero_copy: &mut [ZeroCopyBuf],
) -> Result<Value, AnyError> {
let args: GetEnv = serde_json::from_value(args)?;
- let cli_state = super::cli_state(state);
- cli_state.check_env()?;
+ state.borrow::<Permissions>().check_env()?;
let r = match env::var(args.key) {
Err(env::VarError::NotPresent) => json!([]),
v => json!([v?]),
@@ -97,8 +96,7 @@ fn op_delete_env(
_zero_copy: &mut [ZeroCopyBuf],
) -> Result<Value, AnyError> {
let args: DeleteEnv = serde_json::from_value(args)?;
- let cli_state = super::cli_state(state);
- cli_state.check_env()?;
+ state.borrow::<Permissions>().check_env()?;
env::remove_var(args.key);
Ok(json!({}))
}
@@ -122,9 +120,9 @@ fn op_loadavg(
_args: Value,
_zero_copy: &mut [ZeroCopyBuf],
) -> Result<Value, AnyError> {
- let cli_state = super::cli_state(state);
+ let cli_state = super::global_state(state);
cli_state.check_unstable("Deno.loadavg");
- cli_state.check_env()?;
+ state.borrow::<Permissions>().check_env()?;
match sys_info::loadavg() {
Ok(loadavg) => Ok(json!([loadavg.one, loadavg.five, loadavg.fifteen])),
Err(_) => Ok(json!([0f64, 0f64, 0f64])),
@@ -136,9 +134,9 @@ fn op_hostname(
_args: Value,
_zero_copy: &mut [ZeroCopyBuf],
) -> Result<Value, AnyError> {
- let cli_state = super::cli_state(state);
+ let cli_state = super::global_state(state);
cli_state.check_unstable("Deno.hostname");
- cli_state.check_env()?;
+ state.borrow::<Permissions>().check_env()?;
let hostname = sys_info::hostname().unwrap_or_else(|_| "".to_string());
Ok(json!(hostname))
}
@@ -148,9 +146,9 @@ fn op_os_release(
_args: Value,
_zero_copy: &mut [ZeroCopyBuf],
) -> Result<Value, AnyError> {
- let cli_state = super::cli_state(state);
+ let cli_state = super::global_state(state);
cli_state.check_unstable("Deno.osRelease");
- cli_state.check_env()?;
+ state.borrow::<Permissions>().check_env()?;
let release = sys_info::os_release().unwrap_or_else(|_| "".to_string());
Ok(json!(release))
}
@@ -160,9 +158,9 @@ fn op_system_memory_info(
_args: Value,
_zero_copy: &mut [ZeroCopyBuf],
) -> Result<Value, AnyError> {
- let cli_state = super::cli_state(state);
+ let cli_state = super::global_state(state);
cli_state.check_unstable("Deno.systemMemoryInfo");
- cli_state.check_env()?;
+ state.borrow::<Permissions>().check_env()?;
match sys_info::mem_info() {
Ok(info) => Ok(json!({
"total": info.total,