From bdeb4bddbf5cabd04abe906388f5ebfe64a84c53 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Tue, 12 Sep 2023 00:10:43 +0200 Subject: [PATCH] refactor: rewrite runtime/ ops to op2 (#20459) --- runtime/ops/fs_events.rs | 12 ++++---- runtime/ops/os/mod.rs | 59 ++++++++++++++++++++++---------------- runtime/ops/permissions.rs | 17 ++++++----- runtime/ops/process.rs | 48 +++++++++++++++++-------------- runtime/ops/runtime.rs | 8 ++++-- runtime/ops/signal.rs | 9 +++--- runtime/ops/tty.rs | 2 +- 7 files changed, 90 insertions(+), 65 deletions(-) diff --git a/runtime/ops/fs_events.rs b/runtime/ops/fs_events.rs index 2668431ebf..2d46f73ff1 100644 --- a/runtime/ops/fs_events.rs +++ b/runtime/ops/fs_events.rs @@ -11,7 +11,7 @@ use deno_core::RcRef; use deno_core::Resource; use deno_core::ResourceId; -use deno_core::op; +use deno_core::op2; use notify::event::Event as NotifyEvent; use notify::Error as NotifyError; @@ -92,10 +92,11 @@ pub struct OpenArgs { paths: Vec, } -#[op] +#[op2] +#[smi] fn op_fs_events_open( state: &mut OpState, - args: OpenArgs, + #[serde] args: OpenArgs, ) -> Result { let (sender, receiver) = mpsc::channel::>(16); let sender = Mutex::new(sender); @@ -130,10 +131,11 @@ fn op_fs_events_open( Ok(rid) } -#[op] +#[op2(async)] +#[serde] async fn op_fs_events_poll( state: Rc>, - rid: ResourceId, + #[smi] rid: ResourceId, ) -> Result, AnyError> { let resource = state.borrow().resource_table.get::(rid)?; let mut receiver = RcRef::map(&resource, |r| &r.receiver).borrow_mut().await; diff --git a/runtime/ops/os/mod.rs b/runtime/ops/os/mod.rs index cfd531a180..80f37514f3 100644 --- a/runtime/ops/os/mod.rs +++ b/runtime/ops/os/mod.rs @@ -6,6 +6,7 @@ use crate::worker::ExitCode; use deno_core::error::type_error; use deno_core::error::AnyError; use deno_core::op; +use deno_core::op2; use deno_core::url::Url; use deno_core::v8; use deno_core::Op; @@ -31,7 +32,6 @@ deno_core::ops!( op_network_interfaces, op_os_release, op_os_uptime, - op_node_unstable_os_uptime, op_set_env, op_set_exit_code, op_system_memory_info, @@ -61,7 +61,8 @@ deno_core::extension!( }, ); -#[op] +#[op2] +#[string] fn op_exec_path(state: &mut OpState) -> Result { let current_exe = env::current_exe().unwrap(); state @@ -75,11 +76,11 @@ fn op_exec_path(state: &mut OpState) -> Result { into_string(path.into_os_string()) } -#[op] +#[op2(fast)] fn op_set_env( state: &mut OpState, - key: &str, - value: &str, + #[string] key: &str, + #[string] value: &str, ) -> Result<(), AnyError> { state.borrow_mut::().check_env(key)?; if key.is_empty() { @@ -99,16 +100,18 @@ fn op_set_env( Ok(()) } -#[op] +#[op2] +#[serde] fn op_env(state: &mut OpState) -> Result, AnyError> { state.borrow_mut::().check_env_all()?; Ok(env::vars().collect()) } -#[op] +#[op2] +#[string] fn op_get_env( state: &mut OpState, - key: String, + #[string] key: String, ) -> Result, AnyError> { let skip_permission_check = NODE_ENV_VAR_ALLOWLIST.contains(&key); @@ -133,8 +136,11 @@ fn op_get_env( Ok(r) } -#[op] -fn op_delete_env(state: &mut OpState, key: String) -> Result<(), AnyError> { +#[op2(fast)] +fn op_delete_env( + state: &mut OpState, + #[string] key: String, +) -> Result<(), AnyError> { state.borrow_mut::().check_env(&key)?; if key.is_empty() || key.contains(&['=', '\0'] as &[char]) { return Err(type_error("Key contains invalid characters.")); @@ -143,18 +149,19 @@ fn op_delete_env(state: &mut OpState, key: String) -> Result<(), AnyError> { Ok(()) } -#[op] -fn op_set_exit_code(state: &mut OpState, code: i32) { +#[op2(fast)] +fn op_set_exit_code(state: &mut OpState, #[smi] code: i32) { state.borrow_mut::().set(code); } -#[op] +#[op2(fast)] fn op_exit(state: &mut OpState) { let code = state.borrow::().get(); std::process::exit(code) } -#[op] +#[op2] +#[serde] fn op_loadavg(state: &mut OpState) -> Result<(f64, f64, f64), AnyError> { state .borrow_mut::() @@ -162,7 +169,8 @@ fn op_loadavg(state: &mut OpState) -> Result<(f64, f64, f64), AnyError> { Ok(sys_info::loadavg()) } -#[op] +#[op2] +#[string] fn op_hostname(state: &mut OpState) -> Result { state .borrow_mut::() @@ -170,7 +178,8 @@ fn op_hostname(state: &mut OpState) -> Result { Ok(sys_info::hostname()) } -#[op] +#[op2] +#[string] fn op_os_release(state: &mut OpState) -> Result { state .borrow_mut::() @@ -178,7 +187,8 @@ fn op_os_release(state: &mut OpState) -> Result { Ok(sys_info::os_release()) } -#[op] +#[op2] +#[serde] fn op_network_interfaces( state: &mut OpState, ) -> Result, AnyError> { @@ -229,7 +239,8 @@ impl From for NetworkInterface { } } -#[op] +#[op2] +#[serde] fn op_system_memory_info( state: &mut OpState, ) -> Result, AnyError> { @@ -239,6 +250,7 @@ fn op_system_memory_info( Ok(sys_info::mem_info()) } +// TODO(bartlomieju): op2 doesn't support cfg attrs #[cfg(not(windows))] #[op] fn op_gid(state: &mut OpState) -> Result, AnyError> { @@ -252,6 +264,7 @@ fn op_gid(state: &mut OpState) -> Result, AnyError> { } } +// TODO(bartlomieju): op2 doesn't support cfg attrs #[cfg(windows)] #[op] fn op_gid(state: &mut OpState) -> Result, AnyError> { @@ -261,6 +274,7 @@ fn op_gid(state: &mut OpState) -> Result, AnyError> { Ok(None) } +// TODO(bartlomieju): op2 doesn't support cfg attrs #[cfg(not(windows))] #[op] fn op_uid(state: &mut OpState) -> Result, AnyError> { @@ -274,6 +288,7 @@ fn op_uid(state: &mut OpState) -> Result, AnyError> { } } +// TODO(bartlomieju): op2 doesn't support cfg attrs #[cfg(windows)] #[op] fn op_uid(state: &mut OpState) -> Result, AnyError> { @@ -293,7 +308,8 @@ struct MemoryUsage { external: usize, } -#[op(v8)] +#[op2] +#[serde] fn op_runtime_memory_usage(scope: &mut v8::HandleScope) -> MemoryUsage { let mut s = v8::HeapStatistics::default(); scope.get_heap_statistics(&mut s); @@ -464,8 +480,3 @@ fn os_uptime(state: &mut OpState) -> Result { fn op_os_uptime(state: &mut OpState) -> Result { os_uptime(state) } - -#[op] -fn op_node_unstable_os_uptime(state: &mut OpState) -> Result { - os_uptime(state) -} diff --git a/runtime/ops/permissions.rs b/runtime/ops/permissions.rs index e5c55076c5..8537a57870 100644 --- a/runtime/ops/permissions.rs +++ b/runtime/ops/permissions.rs @@ -6,7 +6,7 @@ use crate::permissions::PermissionsContainer; use deno_core::error::custom_error; use deno_core::error::uri_error; use deno_core::error::AnyError; -use deno_core::op; +use deno_core::op2; use deno_core::url; use deno_core::OpState; use serde::Deserialize; @@ -51,10 +51,11 @@ impl From for PermissionStatus { } } -#[op] +#[op2] +#[serde] pub fn op_query_permission( state: &mut OpState, - args: PermissionArgs, + #[serde] args: PermissionArgs, ) -> Result { let permissions = state.borrow::().0.lock(); let path = args.path.as_deref(); @@ -85,10 +86,11 @@ pub fn op_query_permission( Ok(PermissionStatus::from(perm)) } -#[op] +#[op2] +#[serde] pub fn op_revoke_permission( state: &mut OpState, - args: PermissionArgs, + #[serde] args: PermissionArgs, ) -> Result { let mut permissions = state.borrow_mut::().0.lock(); let path = args.path.as_deref(); @@ -119,10 +121,11 @@ pub fn op_revoke_permission( Ok(PermissionStatus::from(perm)) } -#[op] +#[op2] +#[serde] pub fn op_request_permission( state: &mut OpState, - args: PermissionArgs, + #[serde] args: PermissionArgs, ) -> Result { let mut permissions = state.borrow_mut::().0.lock(); let path = args.path.as_deref(); diff --git a/runtime/ops/process.rs b/runtime/ops/process.rs index 0b9a95c558..acfd0dc5c5 100644 --- a/runtime/ops/process.rs +++ b/runtime/ops/process.rs @@ -6,6 +6,7 @@ use deno_core::anyhow::Context; use deno_core::error::type_error; use deno_core::error::AnyError; use deno_core::op; +use deno_core::op2; use deno_core::serde_json; use deno_core::AsyncMutFuture; use deno_core::AsyncRefCell; @@ -322,16 +323,18 @@ fn spawn_child( }) } -#[op] +#[op2] +#[serde] fn op_spawn_child( state: &mut OpState, - args: SpawnArgs, - api_name: String, + #[serde] args: SpawnArgs, + #[string] api_name: String, ) -> Result { let command = create_command(state, args, &api_name)?; spawn_child(state, command) } +// TODO(bartlomieju): op2 doesn't support clippy allows #[op] async fn op_spawn_wait( state: Rc>, @@ -351,10 +354,11 @@ async fn op_spawn_wait( result } -#[op] +#[op2] +#[serde] fn op_spawn_sync( state: &mut OpState, - args: SpawnArgs, + #[serde] args: SpawnArgs, ) -> Result { let stdout = matches!(args.stdio.stdout, Stdio::Piped); let stderr = matches!(args.stdio.stderr, Stdio::Piped); @@ -381,11 +385,11 @@ fn op_spawn_sync( }) } -#[op] +#[op2(fast)] fn op_spawn_kill( state: &mut OpState, - rid: ResourceId, - signal: String, + #[smi] rid: ResourceId, + #[string] signal: String, ) -> Result<(), AnyError> { if let Ok(child_resource) = state.resource_table.get::(rid) { deprecated::kill(child_resource.1 as i32, &signal)?; @@ -432,7 +436,7 @@ mod deprecated { #[derive(Serialize)] #[serde(rename_all = "camelCase")] // TODO(@AaronO): maybe find a more descriptive name or a convention for return structs - struct RunInfo { + pub struct RunInfo { rid: ResourceId, pid: Option, stdin_rid: Option, @@ -440,10 +444,11 @@ mod deprecated { stderr_rid: Option, } - #[op] - fn op_run( + #[op2] + #[serde] + pub fn op_run( state: &mut OpState, - run_args: RunArgs, + #[serde] run_args: RunArgs, ) -> Result { let args = run_args.cmd; state @@ -557,16 +562,17 @@ mod deprecated { #[derive(Serialize)] #[serde(rename_all = "camelCase")] - struct ProcessStatus { + pub struct ProcessStatus { got_signal: bool, exit_code: i32, exit_signal: i32, } - #[op] - async fn op_run_status( + #[op2(async)] + #[serde] + pub async fn op_run_status( state: Rc>, - rid: ResourceId, + #[smi] rid: ResourceId, ) -> Result { let resource = state .borrow_mut() @@ -648,12 +654,12 @@ mod deprecated { } } - #[op] - fn op_kill( + #[op2(fast)] + pub fn op_kill( state: &mut OpState, - pid: i32, - signal: String, - api_name: String, + #[smi] pid: i32, + #[string] signal: String, + #[string] api_name: String, ) -> Result<(), AnyError> { state .borrow_mut::() diff --git a/runtime/ops/runtime.rs b/runtime/ops/runtime.rs index 175f185997..5042ba4867 100644 --- a/runtime/ops/runtime.rs +++ b/runtime/ops/runtime.rs @@ -2,7 +2,7 @@ use crate::permissions::PermissionsContainer; use deno_core::error::AnyError; -use deno_core::op; +use deno_core::op2; use deno_core::ModuleSpecifier; use deno_core::OpState; @@ -15,7 +15,8 @@ deno_core::extension!( }, ); -#[op] +#[op2] +#[string] fn op_main_module(state: &mut OpState) -> Result { let main_url = state.borrow::(); let main_path = main_url.to_string(); @@ -30,7 +31,8 @@ fn op_main_module(state: &mut OpState) -> Result { /// This is an op instead of being done at initialization time because /// it's expensive to retrieve the ppid on Windows. -#[op] +#[op2] +#[bigint] pub fn op_ppid() -> i64 { #[cfg(windows)] { diff --git a/runtime/ops/signal.rs b/runtime/ops/signal.rs index ca1f2211d6..8508aa56a5 100644 --- a/runtime/ops/signal.rs +++ b/runtime/ops/signal.rs @@ -2,6 +2,7 @@ use deno_core::error::type_error; use deno_core::error::AnyError; use deno_core::op; +use deno_core::op2; use deno_core::AsyncRefCell; use deno_core::CancelFuture; use deno_core::CancelHandle; @@ -580,10 +581,10 @@ fn op_signal_bind( Ok(rid) } -#[op] +#[op2(async)] async fn op_signal_poll( state: Rc>, - rid: ResourceId, + #[smi] rid: ResourceId, ) -> Result { let resource = state .borrow_mut() @@ -599,10 +600,10 @@ async fn op_signal_poll( } } -#[op] +#[op2(fast)] pub fn op_signal_unbind( state: &mut OpState, - rid: ResourceId, + #[smi] rid: ResourceId, ) -> Result<(), AnyError> { state.resource_table.close(rid)?; Ok(()) diff --git a/runtime/ops/tty.rs b/runtime/ops/tty.rs index 1d4c123a22..07a7a0b738 100644 --- a/runtime/ops/tty.rs +++ b/runtime/ops/tty.rs @@ -73,7 +73,7 @@ fn mode_raw_input_off(original_mode: DWORD) -> DWORD { original_mode & !wincon::ENABLE_VIRTUAL_TERMINAL_INPUT | COOKED_MODE } -#[op(fast)] +#[op2(fast)] fn op_stdin_set_raw( state: &mut OpState, is_raw: bool,