From 97d8498d46bbfb7bb16eeb3a69565d2aeda1e5aa Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Fri, 11 Oct 2019 11:41:54 -0700 Subject: [PATCH] Add init methods for each op module (#3087) --- cli/ops/compiler.rs | 19 ++- cli/ops/errors.rs | 16 ++- cli/ops/fetch.rs | 5 + cli/ops/files.rs | 13 +- cli/ops/fs.rs | 56 ++++++--- cli/ops/io.rs | 10 +- cli/ops/metrics.rs | 21 ---- cli/ops/mod.rs | 2 - cli/ops/net.rs | 17 ++- cli/ops/os.rs | 31 +++-- cli/ops/performance.rs | 30 ----- cli/ops/permissions.rs | 12 ++ cli/ops/process.rs | 16 ++- cli/ops/random.rs | 10 +- cli/ops/repl.rs | 16 ++- cli/ops/resources.rs | 7 +- cli/ops/timers.rs | 43 ++++++- cli/ops/workers.rs | 59 ++++++++- cli/state.rs | 3 +- cli/worker.rs | 267 +++-------------------------------------- 20 files changed, 292 insertions(+), 361 deletions(-) delete mode 100644 cli/ops/metrics.rs delete mode 100644 cli/ops/performance.rs diff --git a/cli/ops/compiler.rs b/cli/ops/compiler.rs index 4228842dd89d3a..720eb65fc26eee 100644 --- a/cli/ops/compiler.rs +++ b/cli/ops/compiler.rs @@ -2,9 +2,22 @@ use super::dispatch_json::{Deserialize, JsonOp, Value}; use crate::futures::future::join_all; use crate::futures::Future; +use crate::ops::json_op; use crate::state::ThreadSafeState; use deno::*; +pub fn init(i: &mut Isolate, s: &ThreadSafeState) { + i.register_op("cache", s.core_op(json_op(s.stateful_op(op_cache)))); + i.register_op( + "fetch_source_files", + s.core_op(json_op(s.stateful_op(op_fetch_source_files))), + ); + i.register_op( + "fetch_asset", + s.core_op(json_op(s.stateful_op(op_fetch_asset))), + ); +} + #[derive(Deserialize)] #[serde(rename_all = "camelCase")] struct CacheArgs { @@ -13,7 +26,7 @@ struct CacheArgs { extension: String, } -pub fn op_cache( +fn op_cache( state: &ThreadSafeState, args: Value, _zero_copy: Option, @@ -38,7 +51,7 @@ struct FetchSourceFilesArgs { referrer: String, } -pub fn op_fetch_source_files( +fn op_fetch_source_files( state: &ThreadSafeState, args: Value, _data: Option, @@ -85,7 +98,7 @@ struct FetchAssetArgs { name: String, } -pub fn op_fetch_asset( +fn op_fetch_asset( _state: &ThreadSafeState, args: Value, _zero_copy: Option, diff --git a/cli/ops/errors.rs b/cli/ops/errors.rs index cd21a3880739a9..2d786b97de1a30 100644 --- a/cli/ops/errors.rs +++ b/cli/ops/errors.rs @@ -1,18 +1,30 @@ // Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. use super::dispatch_json::{Deserialize, JsonOp, Value}; use crate::fmt_errors::JSError; +use crate::ops::json_op; use crate::source_maps::get_orig_position; use crate::source_maps::CachedMaps; use crate::state::ThreadSafeState; use deno::*; use std::collections::HashMap; +pub fn init(i: &mut Isolate, s: &ThreadSafeState) { + i.register_op( + "apply_source_map", + s.core_op(json_op(s.stateful_op(op_apply_source_map))), + ); + i.register_op( + "format_error", + s.core_op(json_op(s.stateful_op(op_format_error))), + ); +} + #[derive(Deserialize)] struct FormatErrorArgs { error: String, } -pub fn op_format_error( +fn op_format_error( state: &ThreadSafeState, args: Value, _zero_copy: Option, @@ -32,7 +44,7 @@ struct ApplySourceMap { column: i32, } -pub fn op_apply_source_map( +fn op_apply_source_map( state: &ThreadSafeState, args: Value, _zero_copy: Option, diff --git a/cli/ops/fetch.rs b/cli/ops/fetch.rs index f69065f1d4e21c..1d330ce41f15d0 100644 --- a/cli/ops/fetch.rs +++ b/cli/ops/fetch.rs @@ -1,6 +1,7 @@ // Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. use super::dispatch_json::{Deserialize, JsonOp, Value}; use crate::http_util::get_client; +use crate::ops::json_op; use crate::resources; use crate::state::ThreadSafeState; use deno::*; @@ -12,6 +13,10 @@ use hyper::rt::Future; use std; use std::convert::From; +pub fn init(i: &mut Isolate, s: &ThreadSafeState) { + i.register_op("fetch", s.core_op(json_op(s.stateful_op(op_fetch)))); +} + #[derive(Deserialize)] struct FetchArgs { method: Option, diff --git a/cli/ops/files.rs b/cli/ops/files.rs index 01abff3a9a4d71..0432acd826325b 100644 --- a/cli/ops/files.rs +++ b/cli/ops/files.rs @@ -1,6 +1,7 @@ // Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. use super::dispatch_json::{Deserialize, JsonOp, Value}; use crate::fs as deno_fs; +use crate::ops::json_op; use crate::resources; use crate::state::ThreadSafeState; use deno::*; @@ -9,6 +10,12 @@ use std; use std::convert::From; use tokio; +pub fn init(i: &mut Isolate, s: &ThreadSafeState) { + i.register_op("open", s.core_op(json_op(s.stateful_op(op_open)))); + i.register_op("close", s.core_op(json_op(s.stateful_op(op_close)))); + i.register_op("seek", s.core_op(json_op(s.stateful_op(op_seek)))); +} + #[derive(Deserialize)] #[serde(rename_all = "camelCase")] struct OpenArgs { @@ -17,7 +24,7 @@ struct OpenArgs { mode: String, } -pub fn op_open( +fn op_open( state: &ThreadSafeState, args: Value, _zero_copy: Option, @@ -96,7 +103,7 @@ struct CloseArgs { rid: i32, } -pub fn op_close( +fn op_close( _state: &ThreadSafeState, args: Value, _zero_copy: Option, @@ -117,7 +124,7 @@ struct SeekArgs { whence: i32, } -pub fn op_seek( +fn op_seek( _state: &ThreadSafeState, args: Value, _zero_copy: Option, diff --git a/cli/ops/fs.rs b/cli/ops/fs.rs index c549bef3292eb6..4d54aaad6bdde7 100644 --- a/cli/ops/fs.rs +++ b/cli/ops/fs.rs @@ -1,10 +1,10 @@ // Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. // Some deserializer fields are only used on Unix and Windows build fails without it -#![allow(dead_code)] use super::dispatch_json::{blocking_json, Deserialize, JsonOp, Value}; use crate::deno_error::DenoError; use crate::deno_error::ErrorKind; use crate::fs as deno_fs; +use crate::ops::json_op; use crate::state::ThreadSafeState; use deno::*; use remove_dir_all::remove_dir_all; @@ -16,12 +16,34 @@ use std::time::UNIX_EPOCH; #[cfg(unix)] use std::os::unix::fs::PermissionsExt; +pub fn init(i: &mut Isolate, s: &ThreadSafeState) { + i.register_op("chdir", s.core_op(json_op(s.stateful_op(op_chdir)))); + i.register_op("mkdir", s.core_op(json_op(s.stateful_op(op_mkdir)))); + i.register_op("chmod", s.core_op(json_op(s.stateful_op(op_chmod)))); + i.register_op("chown", s.core_op(json_op(s.stateful_op(op_chown)))); + i.register_op("remove", s.core_op(json_op(s.stateful_op(op_remove)))); + i.register_op("copy_file", s.core_op(json_op(s.stateful_op(op_copy_file)))); + i.register_op("stat", s.core_op(json_op(s.stateful_op(op_stat)))); + i.register_op("read_dir", s.core_op(json_op(s.stateful_op(op_read_dir)))); + i.register_op("rename", s.core_op(json_op(s.stateful_op(op_rename)))); + i.register_op("link", s.core_op(json_op(s.stateful_op(op_link)))); + i.register_op("symlink", s.core_op(json_op(s.stateful_op(op_symlink)))); + i.register_op("read_link", s.core_op(json_op(s.stateful_op(op_read_link)))); + i.register_op("truncate", s.core_op(json_op(s.stateful_op(op_truncate)))); + i.register_op( + "make_temp_dir", + s.core_op(json_op(s.stateful_op(op_make_temp_dir))), + ); + i.register_op("cwd", s.core_op(json_op(s.stateful_op(op_cwd)))); + i.register_op("utime", s.core_op(json_op(s.stateful_op(op_utime)))); +} + #[derive(Deserialize)] struct ChdirArgs { directory: String, } -pub fn op_chdir( +fn op_chdir( _state: &ThreadSafeState, args: Value, _zero_copy: Option, @@ -40,7 +62,7 @@ struct MkdirArgs { mode: u32, } -pub fn op_mkdir( +fn op_mkdir( state: &ThreadSafeState, args: Value, _zero_copy: Option, @@ -66,7 +88,7 @@ struct ChmodArgs { mode: u32, } -pub fn op_chmod( +fn op_chmod( state: &ThreadSafeState, args: Value, _zero_copy: Option, @@ -100,7 +122,7 @@ struct ChownArgs { gid: u32, } -pub fn op_chown( +fn op_chown( state: &ThreadSafeState, args: Value, _zero_copy: Option, @@ -127,7 +149,7 @@ struct RemoveArgs { recursive: bool, } -pub fn op_remove( +fn op_remove( state: &ThreadSafeState, args: Value, _zero_copy: Option, @@ -161,7 +183,7 @@ struct CopyFileArgs { to: String, } -pub fn op_copy_file( +fn op_copy_file( state: &ThreadSafeState, args: Value, _zero_copy: Option, @@ -220,7 +242,7 @@ struct StatArgs { lstat: bool, } -pub fn op_stat( +fn op_stat( state: &ThreadSafeState, args: Value, _zero_copy: Option, @@ -262,7 +284,7 @@ struct ReadDirArgs { path: String, } -pub fn op_read_dir( +fn op_read_dir( state: &ThreadSafeState, args: Value, _zero_copy: Option, @@ -308,7 +330,7 @@ struct RenameArgs { newpath: String, } -pub fn op_rename( +fn op_rename( state: &ThreadSafeState, args: Value, _zero_copy: Option, @@ -338,7 +360,7 @@ struct LinkArgs { newname: String, } -pub fn op_link( +fn op_link( state: &ThreadSafeState, args: Value, _zero_copy: Option, @@ -367,7 +389,7 @@ struct SymlinkArgs { newname: String, } -pub fn op_symlink( +fn op_symlink( state: &ThreadSafeState, args: Value, _zero_copy: Option, @@ -400,7 +422,7 @@ struct ReadLinkArgs { name: String, } -pub fn op_read_link( +fn op_read_link( state: &ThreadSafeState, args: Value, _zero_copy: Option, @@ -429,7 +451,7 @@ struct TruncateArgs { len: u64, } -pub fn op_truncate( +fn op_truncate( state: &ThreadSafeState, args: Value, _zero_copy: Option, @@ -459,7 +481,7 @@ struct MakeTempDirArgs { suffix: Option, } -pub fn op_make_temp_dir( +fn op_make_temp_dir( state: &ThreadSafeState, args: Value, _zero_copy: Option, @@ -499,7 +521,7 @@ struct Utime { mtime: u64, } -pub fn op_utime( +fn op_utime( state: &ThreadSafeState, args: Value, _zero_copy: Option, @@ -514,7 +536,7 @@ pub fn op_utime( }) } -pub fn op_cwd( +fn op_cwd( _state: &ThreadSafeState, _args: Value, _zero_copy: Option, diff --git a/cli/ops/io.rs b/cli/ops/io.rs index 8b8520c355638f..98ac2f39522919 100644 --- a/cli/ops/io.rs +++ b/cli/ops/io.rs @@ -1,12 +1,18 @@ use super::dispatch_minimal::MinimalOp; use crate::deno_error; +use crate::ops::minimal_op; use crate::resources; +use crate::state::ThreadSafeState; use crate::tokio_read; use crate::tokio_write; -use deno::ErrBox; -use deno::PinnedBuf; +use deno::*; use futures::Future; +pub fn init(i: &mut Isolate, s: &ThreadSafeState) { + i.register_op("read", s.core_op(minimal_op(op_read))); + i.register_op("write", s.core_op(minimal_op(op_write))); +} + pub fn op_read(rid: i32, zero_copy: Option) -> Box { debug!("read rid={}", rid); let zero_copy = match zero_copy { diff --git a/cli/ops/metrics.rs b/cli/ops/metrics.rs deleted file mode 100644 index e1a23f6c817dbf..00000000000000 --- a/cli/ops/metrics.rs +++ /dev/null @@ -1,21 +0,0 @@ -// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. -use super::dispatch_json::{JsonOp, Value}; -use crate::state::ThreadSafeState; -use deno::*; -use std::sync::atomic::Ordering; - -pub fn op_metrics( - state: &ThreadSafeState, - _args: Value, - _zero_copy: Option, -) -> Result { - let m = &state.metrics; - - Ok(JsonOp::Sync(json!({ - "opsDispatched": m.ops_dispatched.load(Ordering::SeqCst) as u64, - "opsCompleted": m.ops_completed.load(Ordering::SeqCst) as u64, - "bytesSentControl": m.bytes_sent_control.load(Ordering::SeqCst) as u64, - "bytesSentData": m.bytes_sent_data.load(Ordering::SeqCst) as u64, - "bytesReceived": m.bytes_received.load(Ordering::SeqCst) as u64 - }))) -} diff --git a/cli/ops/mod.rs b/cli/ops/mod.rs index 0c07adcc93867d..4c7caf2ca45b05 100644 --- a/cli/ops/mod.rs +++ b/cli/ops/mod.rs @@ -12,10 +12,8 @@ pub mod fetch; pub mod files; pub mod fs; pub mod io; -pub mod metrics; pub mod net; pub mod os; -pub mod performance; pub mod permissions; pub mod process; pub mod random; diff --git a/cli/ops/net.rs b/cli/ops/net.rs index 507eff50482451..eac63354b92920 100644 --- a/cli/ops/net.rs +++ b/cli/ops/net.rs @@ -1,5 +1,6 @@ // Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. use super::dispatch_json::{Deserialize, JsonOp, Value}; +use crate::ops::json_op; use crate::resolve_addr::resolve_addr; use crate::resources; use crate::resources::Resource; @@ -14,12 +15,20 @@ use tokio; use tokio::net::TcpListener; use tokio::net::TcpStream; +pub fn init(i: &mut Isolate, s: &ThreadSafeState) { + i.register_op("accept", s.core_op(json_op(s.stateful_op(op_accept)))); + i.register_op("dial", s.core_op(json_op(s.stateful_op(op_dial)))); + i.register_op("dial_tls", s.core_op(json_op(s.stateful_op(op_dial)))); + i.register_op("shutdown", s.core_op(json_op(s.stateful_op(op_shutdown)))); + i.register_op("listen", s.core_op(json_op(s.stateful_op(op_listen)))); +} + #[derive(Deserialize)] struct AcceptArgs { rid: i32, } -pub fn op_accept( +fn op_accept( _state: &ThreadSafeState, args: Value, _zero_copy: Option, @@ -54,7 +63,7 @@ struct DialArgs { port: u16, } -pub fn op_dial( +fn op_dial( state: &ThreadSafeState, args: Value, _zero_copy: Option, @@ -96,7 +105,7 @@ struct ShutdownArgs { how: i32, } -pub fn op_shutdown( +fn op_shutdown( _state: &ThreadSafeState, args: Value, _zero_copy: Option, @@ -125,7 +134,7 @@ struct ListenArgs { port: u16, } -pub fn op_listen( +fn op_listen( state: &ThreadSafeState, args: Value, _zero_copy: Option, diff --git a/cli/ops/os.rs b/cli/ops/os.rs index 92f640afdc3b95..c50b8eedb69adf 100644 --- a/cli/ops/os.rs +++ b/cli/ops/os.rs @@ -2,6 +2,7 @@ use super::dispatch_json::{Deserialize, JsonOp, Value}; use crate::colors; use crate::fs as deno_fs; +use crate::ops::json_op; use crate::state::ThreadSafeState; use crate::version; use atty; @@ -22,7 +23,19 @@ static BUILD_OS: &str = "win"; #[cfg(target_arch = "x86_64")] static BUILD_ARCH: &str = "x64"; -pub fn op_start( +pub fn init(i: &mut Isolate, s: &ThreadSafeState) { + i.register_op("exit", s.core_op(json_op(s.stateful_op(op_exit)))); + i.register_op("is_tty", s.core_op(json_op(s.stateful_op(op_is_tty)))); + i.register_op("env", s.core_op(json_op(s.stateful_op(op_env)))); + i.register_op("exec_path", s.core_op(json_op(s.stateful_op(op_exec_path)))); + i.register_op("set_env", s.core_op(json_op(s.stateful_op(op_set_env)))); + i.register_op("get_env", s.core_op(json_op(s.stateful_op(op_get_env)))); + i.register_op("home_dir", s.core_op(json_op(s.stateful_op(op_home_dir)))); + i.register_op("hostname", s.core_op(json_op(s.stateful_op(op_hostname)))); + i.register_op("start", s.core_op(json_op(s.stateful_op(op_start)))); +} + +fn op_start( state: &ThreadSafeState, _args: Value, _zero_copy: Option, @@ -46,7 +59,7 @@ pub fn op_start( }))) } -pub fn op_home_dir( +fn op_home_dir( state: &ThreadSafeState, _args: Value, _zero_copy: Option, @@ -60,7 +73,7 @@ pub fn op_home_dir( Ok(JsonOp::Sync(json!(path))) } -pub fn op_exec_path( +fn op_exec_path( state: &ThreadSafeState, _args: Value, _zero_copy: Option, @@ -80,7 +93,7 @@ struct SetEnv { value: String, } -pub fn op_set_env( +fn op_set_env( state: &ThreadSafeState, args: Value, _zero_copy: Option, @@ -91,7 +104,7 @@ pub fn op_set_env( Ok(JsonOp::Sync(json!({}))) } -pub fn op_env( +fn op_env( state: &ThreadSafeState, _args: Value, _zero_copy: Option, @@ -106,7 +119,7 @@ struct GetEnv { key: String, } -pub fn op_get_env( +fn op_get_env( state: &ThreadSafeState, args: Value, _zero_copy: Option, @@ -125,7 +138,7 @@ struct Exit { code: i32, } -pub fn op_exit( +fn op_exit( _s: &ThreadSafeState, args: Value, _zero_copy: Option, @@ -134,7 +147,7 @@ pub fn op_exit( std::process::exit(args.code) } -pub fn op_is_tty( +fn op_is_tty( _s: &ThreadSafeState, _args: Value, _zero_copy: Option, @@ -146,7 +159,7 @@ pub fn op_is_tty( }))) } -pub fn op_hostname( +fn op_hostname( state: &ThreadSafeState, _args: Value, _zero_copy: Option, diff --git a/cli/ops/performance.rs b/cli/ops/performance.rs deleted file mode 100644 index 090fc332323947..00000000000000 --- a/cli/ops/performance.rs +++ /dev/null @@ -1,30 +0,0 @@ -// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. -use super::dispatch_json::{JsonOp, Value}; -use crate::state::ThreadSafeState; -use deno::*; - -// Returns a milliseconds and nanoseconds subsec -// since the start time of the deno runtime. -// If the High precision flag is not set, the -// nanoseconds are rounded on 2ms. -pub fn op_now( - state: &ThreadSafeState, - _args: Value, - _zero_copy: Option, -) -> Result { - let seconds = state.start_time.elapsed().as_secs(); - let mut subsec_nanos = state.start_time.elapsed().subsec_nanos(); - let reduced_time_precision = 2_000_000; // 2ms in nanoseconds - - // If the permission is not enabled - // Round the nano result on 2 milliseconds - // see: https://developer.mozilla.org/en-US/docs/Web/API/DOMHighResTimeStamp#Reduced_time_precision - if !state.permissions.allows_hrtime() { - subsec_nanos -= subsec_nanos % reduced_time_precision - } - - Ok(JsonOp::Sync(json!({ - "seconds": seconds, - "subsecNanos": subsec_nanos, - }))) -} diff --git a/cli/ops/permissions.rs b/cli/ops/permissions.rs index 5d14f39be51c10..8303aa9cf7ccc3 100644 --- a/cli/ops/permissions.rs +++ b/cli/ops/permissions.rs @@ -1,8 +1,20 @@ // Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. use super::dispatch_json::{Deserialize, JsonOp, Value}; +use crate::ops::json_op; use crate::state::ThreadSafeState; use deno::*; +pub fn init(i: &mut Isolate, s: &ThreadSafeState) { + i.register_op( + "permissions", + s.core_op(json_op(s.stateful_op(op_permissions))), + ); + i.register_op( + "revoke_permission", + s.core_op(json_op(s.stateful_op(op_revoke_permission))), + ); +} + pub fn op_permissions( state: &ThreadSafeState, _args: Value, diff --git a/cli/ops/process.rs b/cli/ops/process.rs index 8dff53c6e99113..5b59aa030df1cc 100644 --- a/cli/ops/process.rs +++ b/cli/ops/process.rs @@ -1,5 +1,6 @@ // Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. use super::dispatch_json::{Deserialize, JsonOp, Value}; +use crate::ops::json_op; use crate::resources; use crate::signal::kill; use crate::state::ThreadSafeState; @@ -14,6 +15,15 @@ use tokio_process::CommandExt; #[cfg(unix)] use std::os::unix::process::ExitStatusExt; +pub fn init(i: &mut Isolate, s: &ThreadSafeState) { + i.register_op("run", s.core_op(json_op(s.stateful_op(op_run)))); + i.register_op( + "run_status", + s.core_op(json_op(s.stateful_op(op_run_status))), + ); + i.register_op("kill", s.core_op(json_op(s.stateful_op(op_kill)))); +} + fn subprocess_stdio_map(s: &str) -> std::process::Stdio { match s { "inherit" => std::process::Stdio::inherit(), @@ -37,7 +47,7 @@ struct RunArgs { stderr_rid: u32, } -pub fn op_run( +fn op_run( state: &ThreadSafeState, args: Value, _zero_copy: Option, @@ -103,7 +113,7 @@ struct RunStatusArgs { rid: i32, } -pub fn op_run_status( +fn op_run_status( state: &ThreadSafeState, args: Value, _zero_copy: Option, @@ -144,7 +154,7 @@ struct KillArgs { signo: i32, } -pub fn op_kill( +fn op_kill( state: &ThreadSafeState, args: Value, _zero_copy: Option, diff --git a/cli/ops/random.rs b/cli/ops/random.rs index 7470eab402861b..614188d86ee99a 100644 --- a/cli/ops/random.rs +++ b/cli/ops/random.rs @@ -1,11 +1,19 @@ // Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. use super::dispatch_json::{JsonOp, Value}; +use crate::ops::json_op; use crate::state::ThreadSafeState; use deno::*; use rand::thread_rng; use rand::Rng; -pub fn op_get_random_values( +pub fn init(i: &mut Isolate, s: &ThreadSafeState) { + i.register_op( + "get_random_values", + s.core_op(json_op(s.stateful_op(op_get_random_values))), + ); +} + +fn op_get_random_values( state: &ThreadSafeState, _args: Value, zero_copy: Option, diff --git a/cli/ops/repl.rs b/cli/ops/repl.rs index 7ab7509dea8e48..886e5748359ebd 100644 --- a/cli/ops/repl.rs +++ b/cli/ops/repl.rs @@ -1,17 +1,29 @@ // Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. use super::dispatch_json::{blocking_json, Deserialize, JsonOp, Value}; +use crate::ops::json_op; use crate::repl; use crate::resources; use crate::state::ThreadSafeState; use deno::*; +pub fn init(i: &mut Isolate, s: &ThreadSafeState) { + i.register_op( + "repl_start", + s.core_op(json_op(s.stateful_op(op_repl_start))), + ); + i.register_op( + "repl_readline", + s.core_op(json_op(s.stateful_op(op_repl_readline))), + ); +} + #[derive(Deserialize)] #[serde(rename_all = "camelCase")] struct ReplStartArgs { history_file: String, } -pub fn op_repl_start( +fn op_repl_start( state: &ThreadSafeState, args: Value, _zero_copy: Option, @@ -32,7 +44,7 @@ struct ReplReadlineArgs { prompt: String, } -pub fn op_repl_readline( +fn op_repl_readline( _state: &ThreadSafeState, args: Value, _zero_copy: Option, diff --git a/cli/ops/resources.rs b/cli/ops/resources.rs index dafd01d08ce261..8397e9563e58f7 100644 --- a/cli/ops/resources.rs +++ b/cli/ops/resources.rs @@ -1,10 +1,15 @@ // Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. use super::dispatch_json::{JsonOp, Value}; +use crate::ops::json_op; use crate::resources::table_entries; use crate::state::ThreadSafeState; use deno::*; -pub fn op_resources( +pub fn init(i: &mut Isolate, s: &ThreadSafeState) { + i.register_op("resources", s.core_op(json_op(s.stateful_op(op_resources)))); +} + +fn op_resources( _state: &ThreadSafeState, _args: Value, _zero_copy: Option, diff --git a/cli/ops/timers.rs b/cli/ops/timers.rs index abcd5c1b382944..f366797b42ced5 100644 --- a/cli/ops/timers.rs +++ b/cli/ops/timers.rs @@ -1,5 +1,6 @@ // Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. use super::dispatch_json::{Deserialize, JsonOp, Value}; +use crate::ops::json_op; use crate::state::ThreadSafeState; use deno::*; use futures::Future; @@ -7,7 +8,19 @@ use std; use std::time::Duration; use std::time::Instant; -pub fn op_global_timer_stop( +pub fn init(i: &mut Isolate, s: &ThreadSafeState) { + i.register_op( + "global_timer_stop", + s.core_op(json_op(s.stateful_op(op_global_timer_stop))), + ); + i.register_op( + "global_timer", + s.core_op(json_op(s.stateful_op(op_global_timer))), + ); + i.register_op("now", s.core_op(json_op(s.stateful_op(op_now)))); +} + +fn op_global_timer_stop( state: &ThreadSafeState, _args: Value, _zero_copy: Option, @@ -23,7 +36,7 @@ struct GlobalTimerArgs { timeout: u64, } -pub fn op_global_timer( +fn op_global_timer( state: &ThreadSafeState, args: Value, _zero_copy: Option, @@ -40,3 +53,29 @@ pub fn op_global_timer( Ok(JsonOp::Async(Box::new(f))) } + +// Returns a milliseconds and nanoseconds subsec +// since the start time of the deno runtime. +// If the High precision flag is not set, the +// nanoseconds are rounded on 2ms. +fn op_now( + state: &ThreadSafeState, + _args: Value, + _zero_copy: Option, +) -> Result { + let seconds = state.start_time.elapsed().as_secs(); + let mut subsec_nanos = state.start_time.elapsed().subsec_nanos(); + let reduced_time_precision = 2_000_000; // 2ms in nanoseconds + + // If the permission is not enabled + // Round the nano result on 2 milliseconds + // see: https://developer.mozilla.org/en-US/docs/Web/API/DOMHighResTimeStamp#Reduced_time_precision + if !state.permissions.allows_hrtime() { + subsec_nanos -= subsec_nanos % reduced_time_precision + } + + Ok(JsonOp::Sync(json!({ + "seconds": seconds, + "subsecNanos": subsec_nanos, + }))) +} diff --git a/cli/ops/workers.rs b/cli/ops/workers.rs index 6950f25d61769d..c8c4252c3fb02e 100644 --- a/cli/ops/workers.rs +++ b/cli/ops/workers.rs @@ -3,6 +3,7 @@ use super::dispatch_json::{Deserialize, JsonOp, Value}; use crate::deno_error::js_check; use crate::deno_error::DenoError; use crate::deno_error::ErrorKind; +use crate::ops::json_op; use crate::resources; use crate::startup_data; use crate::state::ThreadSafeState; @@ -15,6 +16,36 @@ use futures::Sink; use futures::Stream; use std; use std::convert::From; +use std::sync::atomic::Ordering; + +pub fn init(i: &mut Isolate, s: &ThreadSafeState) { + i.register_op( + "create_worker", + s.core_op(json_op(s.stateful_op(op_create_worker))), + ); + i.register_op( + "host_get_worker_closed", + s.core_op(json_op(s.stateful_op(op_host_get_worker_closed))), + ); + i.register_op( + "host_post_message", + s.core_op(json_op(s.stateful_op(op_host_post_message))), + ); + i.register_op( + "host_get_message", + s.core_op(json_op(s.stateful_op(op_host_get_message))), + ); + // TODO: make sure these two ops are only accessible to appropriate Worker + i.register_op( + "worker_post_message", + s.core_op(json_op(s.stateful_op(op_worker_post_message))), + ); + i.register_op( + "worker_get_message", + s.core_op(json_op(s.stateful_op(op_worker_get_message))), + ); + i.register_op("metrics", s.core_op(json_op(s.stateful_op(op_metrics)))); +} struct GetMessageFuture { pub state: ThreadSafeState, @@ -33,7 +64,7 @@ impl Future for GetMessageFuture { } /// Get message from host as guest worker -pub fn op_worker_get_message( +fn op_worker_get_message( state: &ThreadSafeState, _args: Value, _data: Option, @@ -56,7 +87,7 @@ pub fn op_worker_get_message( } /// Post message to host as guest worker -pub fn op_worker_post_message( +fn op_worker_post_message( state: &ThreadSafeState, _args: Value, data: Option, @@ -84,7 +115,7 @@ struct CreateWorkerArgs { } /// Create worker as the host -pub fn op_create_worker( +fn op_create_worker( state: &ThreadSafeState, args: Value, _data: Option, @@ -154,7 +185,7 @@ struct HostGetWorkerClosedArgs { } /// Return when the worker closes -pub fn op_host_get_worker_closed( +fn op_host_get_worker_closed( state: &ThreadSafeState, args: Value, _data: Option, @@ -183,7 +214,7 @@ struct HostGetMessageArgs { } /// Get message from guest worker as host -pub fn op_host_get_message( +fn op_host_get_message( _state: &ThreadSafeState, args: Value, _data: Option, @@ -208,7 +239,7 @@ struct HostPostMessageArgs { } /// Post message to guest worker as host -pub fn op_host_post_message( +fn op_host_post_message( _state: &ThreadSafeState, args: Value, data: Option, @@ -225,3 +256,19 @@ pub fn op_host_post_message( Ok(JsonOp::Sync(json!({}))) } + +fn op_metrics( + state: &ThreadSafeState, + _args: Value, + _zero_copy: Option, +) -> Result { + let m = &state.metrics; + + Ok(JsonOp::Sync(json!({ + "opsDispatched": m.ops_dispatched.load(Ordering::SeqCst) as u64, + "opsCompleted": m.ops_completed.load(Ordering::SeqCst) as u64, + "bytesSentControl": m.bytes_sent_control.load(Ordering::SeqCst) as u64, + "bytesSentData": m.bytes_sent_data.load(Ordering::SeqCst) as u64, + "bytesReceived": m.bytes_received.load(Ordering::SeqCst) as u64 + }))) +} diff --git a/cli/state.rs b/cli/state.rs index 6f2c6db8f808b7..70845fb8b91ca1 100644 --- a/cli/state.rs +++ b/cli/state.rs @@ -104,9 +104,8 @@ impl Deref for ThreadSafeState { } impl ThreadSafeState { - // TODO: better name welcome /// Wrap core `OpDispatcher` to collect metrics. - pub fn cli_op( + pub fn core_op( &self, dispatcher: D, ) -> impl Fn(&[u8], Option) -> CoreOp diff --git a/cli/worker.rs b/cli/worker.rs index 41de7d1ed232a8..d932634332e358 100644 --- a/cli/worker.rs +++ b/cli/worker.rs @@ -1,8 +1,6 @@ // Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. use crate::fmt_errors::JSError; -use crate::ops::json_op; -use crate::ops::minimal_op; -use crate::ops::*; +use crate::ops; use crate::state::ThreadSafeState; use deno; use deno::ErrBox; @@ -33,255 +31,22 @@ impl Worker { let isolate = Arc::new(Mutex::new(deno::Isolate::new(startup_data, false))); { let mut i = isolate.lock().unwrap(); - let state_ = state.clone(); - - i.register_op("read", state_.cli_op(minimal_op(io::op_read))); - i.register_op("write", state_.cli_op(minimal_op(io::op_write))); - i.register_op( - "exit", - state_.cli_op(json_op(state_.stateful_op(os::op_exit))), - ); - i.register_op( - "is_tty", - state_.cli_op(json_op(state_.stateful_op(os::op_is_tty))), - ); - i.register_op( - "env", - state_.cli_op(json_op(state_.stateful_op(os::op_env))), - ); - i.register_op( - "exec_path", - state_.cli_op(json_op(state_.stateful_op(os::op_exec_path))), - ); - i.register_op( - "utime", - state_.cli_op(json_op(state_.stateful_op(fs::op_utime))), - ); - i.register_op( - "set_env", - state_.cli_op(json_op(state_.stateful_op(os::op_set_env))), - ); - i.register_op( - "get_env", - state_.cli_op(json_op(state_.stateful_op(os::op_get_env))), - ); - i.register_op( - "home_dir", - state_.cli_op(json_op(state_.stateful_op(os::op_home_dir))), - ); - i.register_op( - "start", - state_.cli_op(json_op(state_.stateful_op(os::op_start))), - ); - i.register_op( - "apply_source_map", - state_.cli_op(json_op(state_.stateful_op(errors::op_apply_source_map))), - ); - i.register_op( - "format_error", - state_.cli_op(json_op(state_.stateful_op(errors::op_format_error))), - ); - i.register_op( - "cache", - state_.cli_op(json_op(state_.stateful_op(compiler::op_cache))), - ); - i.register_op( - "fetch_source_files", - state_ - .cli_op(json_op(state_.stateful_op(compiler::op_fetch_source_files))), - ); - i.register_op( - "open", - state_.cli_op(json_op(state_.stateful_op(files::op_open))), - ); - i.register_op( - "close", - state_.cli_op(json_op(state_.stateful_op(files::op_close))), - ); - i.register_op( - "seek", - state_.cli_op(json_op(state_.stateful_op(files::op_seek))), - ); - i.register_op( - "fetch", - state_.cli_op(json_op(state_.stateful_op(fetch::op_fetch))), - ); - i.register_op( - "metrics", - state_.cli_op(json_op(state_.stateful_op(metrics::op_metrics))), - ); - i.register_op( - "repl_start", - state_.cli_op(json_op(state_.stateful_op(repl::op_repl_start))), - ); - i.register_op( - "repl_readline", - state_.cli_op(json_op(state_.stateful_op(repl::op_repl_readline))), - ); - i.register_op( - "accept", - state_.cli_op(json_op(state_.stateful_op(net::op_accept))), - ); - i.register_op( - "dial", - state_.cli_op(json_op(state_.stateful_op(net::op_dial))), - ); - i.register_op( - "dial_tls", - state_.cli_op(json_op(state_.stateful_op(net::op_dial))), - ); - i.register_op( - "shutdown", - state_.cli_op(json_op(state_.stateful_op(net::op_shutdown))), - ); - i.register_op( - "listen", - state_.cli_op(json_op(state_.stateful_op(net::op_listen))), - ); - i.register_op( - "resources", - state_.cli_op(json_op(state_.stateful_op(resources::op_resources))), - ); - i.register_op( - "get_random_values", - state_ - .cli_op(json_op(state_.stateful_op(random::op_get_random_values))), - ); - i.register_op( - "global_timer_stop", - state_ - .cli_op(json_op(state_.stateful_op(timers::op_global_timer_stop))), - ); - i.register_op( - "global_timer", - state_.cli_op(json_op(state_.stateful_op(timers::op_global_timer))), - ); - i.register_op( - "now", - state_.cli_op(json_op(state_.stateful_op(performance::op_now))), - ); - i.register_op( - "permissions", - state_.cli_op(json_op(state_.stateful_op(permissions::op_permissions))), - ); - i.register_op( - "revoke_permission", - state_.cli_op(json_op( - state_.stateful_op(permissions::op_revoke_permission), - )), - ); - i.register_op( - "create_worker", - state_.cli_op(json_op(state_.stateful_op(workers::op_create_worker))), - ); - i.register_op( - "host_get_worker_closed", - state_.cli_op(json_op( - state_.stateful_op(workers::op_host_get_worker_closed), - )), - ); - i.register_op( - "host_post_message", - state_ - .cli_op(json_op(state_.stateful_op(workers::op_host_post_message))), - ); - i.register_op( - "host_get_message", - state_ - .cli_op(json_op(state_.stateful_op(workers::op_host_get_message))), - ); - // TODO: make sure these two ops are only accessible to appropriate Worker - i.register_op( - "worker_post_message", - state_ - .cli_op(json_op(state_.stateful_op(workers::op_worker_post_message))), - ); - i.register_op( - "worker_get_message", - state_ - .cli_op(json_op(state_.stateful_op(workers::op_worker_get_message))), - ); - i.register_op( - "run", - state_.cli_op(json_op(state_.stateful_op(process::op_run))), - ); - i.register_op( - "run_status", - state_.cli_op(json_op(state_.stateful_op(process::op_run_status))), - ); - i.register_op( - "kill", - state_.cli_op(json_op(state_.stateful_op(process::op_kill))), - ); - i.register_op( - "chdir", - state_.cli_op(json_op(state_.stateful_op(fs::op_chdir))), - ); - i.register_op( - "mkdir", - state_.cli_op(json_op(state_.stateful_op(fs::op_mkdir))), - ); - i.register_op( - "chmod", - state_.cli_op(json_op(state_.stateful_op(fs::op_chmod))), - ); - i.register_op( - "chown", - state_.cli_op(json_op(state_.stateful_op(fs::op_chown))), - ); - i.register_op( - "remove", - state_.cli_op(json_op(state_.stateful_op(fs::op_remove))), - ); - i.register_op( - "copy_file", - state_.cli_op(json_op(state_.stateful_op(fs::op_copy_file))), - ); - i.register_op( - "stat", - state_.cli_op(json_op(state_.stateful_op(fs::op_stat))), - ); - i.register_op( - "read_dir", - state_.cli_op(json_op(state_.stateful_op(fs::op_read_dir))), - ); - i.register_op( - "rename", - state_.cli_op(json_op(state_.stateful_op(fs::op_rename))), - ); - i.register_op( - "link", - state_.cli_op(json_op(state_.stateful_op(fs::op_link))), - ); - i.register_op( - "symlink", - state_.cli_op(json_op(state_.stateful_op(fs::op_symlink))), - ); - i.register_op( - "read_link", - state_.cli_op(json_op(state_.stateful_op(fs::op_read_link))), - ); - i.register_op( - "truncate", - state_.cli_op(json_op(state_.stateful_op(fs::op_truncate))), - ); - i.register_op( - "make_temp_dir", - state_.cli_op(json_op(state_.stateful_op(fs::op_make_temp_dir))), - ); - i.register_op( - "cwd", - state_.cli_op(json_op(state_.stateful_op(fs::op_cwd))), - ); - i.register_op( - "fetch_asset", - state_.cli_op(json_op(state_.stateful_op(compiler::op_fetch_asset))), - ); - i.register_op( - "hostname", - state_.cli_op(json_op(state_.stateful_op(os::op_hostname))), - ); + ops::compiler::init(&mut i, &state); + ops::errors::init(&mut i, &state); + ops::fetch::init(&mut i, &state); + ops::files::init(&mut i, &state); + ops::fs::init(&mut i, &state); + ops::io::init(&mut i, &state); + ops::net::init(&mut i, &state); + ops::os::init(&mut i, &state); + ops::permissions::init(&mut i, &state); + ops::process::init(&mut i, &state); + ops::random::init(&mut i, &state); + ops::repl::init(&mut i, &state); + ops::resources::init(&mut i, &state); + ops::timers::init(&mut i, &state); + ops::workers::init(&mut i, &state); let state_ = state.clone(); i.set_dyn_import(move |id, specifier, referrer| {