1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-01-03 04:48:52 -05:00

refactor: rewrite runtime/ ops to op2 (#20459)

This commit is contained in:
Bartek Iwańczuk 2023-09-12 00:10:43 +02:00 committed by GitHub
parent aaff69db3f
commit bdeb4bddbf
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 90 additions and 65 deletions

View file

@ -11,7 +11,7 @@ use deno_core::RcRef;
use deno_core::Resource; use deno_core::Resource;
use deno_core::ResourceId; use deno_core::ResourceId;
use deno_core::op; use deno_core::op2;
use notify::event::Event as NotifyEvent; use notify::event::Event as NotifyEvent;
use notify::Error as NotifyError; use notify::Error as NotifyError;
@ -92,10 +92,11 @@ pub struct OpenArgs {
paths: Vec<String>, paths: Vec<String>,
} }
#[op] #[op2]
#[smi]
fn op_fs_events_open( fn op_fs_events_open(
state: &mut OpState, state: &mut OpState,
args: OpenArgs, #[serde] args: OpenArgs,
) -> Result<ResourceId, AnyError> { ) -> Result<ResourceId, AnyError> {
let (sender, receiver) = mpsc::channel::<Result<FsEvent, AnyError>>(16); let (sender, receiver) = mpsc::channel::<Result<FsEvent, AnyError>>(16);
let sender = Mutex::new(sender); let sender = Mutex::new(sender);
@ -130,10 +131,11 @@ fn op_fs_events_open(
Ok(rid) Ok(rid)
} }
#[op] #[op2(async)]
#[serde]
async fn op_fs_events_poll( async fn op_fs_events_poll(
state: Rc<RefCell<OpState>>, state: Rc<RefCell<OpState>>,
rid: ResourceId, #[smi] rid: ResourceId,
) -> Result<Option<FsEvent>, AnyError> { ) -> Result<Option<FsEvent>, AnyError> {
let resource = state.borrow().resource_table.get::<FsEventsResource>(rid)?; let resource = state.borrow().resource_table.get::<FsEventsResource>(rid)?;
let mut receiver = RcRef::map(&resource, |r| &r.receiver).borrow_mut().await; let mut receiver = RcRef::map(&resource, |r| &r.receiver).borrow_mut().await;

View file

@ -6,6 +6,7 @@ use crate::worker::ExitCode;
use deno_core::error::type_error; use deno_core::error::type_error;
use deno_core::error::AnyError; use deno_core::error::AnyError;
use deno_core::op; use deno_core::op;
use deno_core::op2;
use deno_core::url::Url; use deno_core::url::Url;
use deno_core::v8; use deno_core::v8;
use deno_core::Op; use deno_core::Op;
@ -31,7 +32,6 @@ deno_core::ops!(
op_network_interfaces, op_network_interfaces,
op_os_release, op_os_release,
op_os_uptime, op_os_uptime,
op_node_unstable_os_uptime,
op_set_env, op_set_env,
op_set_exit_code, op_set_exit_code,
op_system_memory_info, op_system_memory_info,
@ -61,7 +61,8 @@ deno_core::extension!(
}, },
); );
#[op] #[op2]
#[string]
fn op_exec_path(state: &mut OpState) -> Result<String, AnyError> { fn op_exec_path(state: &mut OpState) -> Result<String, AnyError> {
let current_exe = env::current_exe().unwrap(); let current_exe = env::current_exe().unwrap();
state state
@ -75,11 +76,11 @@ fn op_exec_path(state: &mut OpState) -> Result<String, AnyError> {
into_string(path.into_os_string()) into_string(path.into_os_string())
} }
#[op] #[op2(fast)]
fn op_set_env( fn op_set_env(
state: &mut OpState, state: &mut OpState,
key: &str, #[string] key: &str,
value: &str, #[string] value: &str,
) -> Result<(), AnyError> { ) -> Result<(), AnyError> {
state.borrow_mut::<PermissionsContainer>().check_env(key)?; state.borrow_mut::<PermissionsContainer>().check_env(key)?;
if key.is_empty() { if key.is_empty() {
@ -99,16 +100,18 @@ fn op_set_env(
Ok(()) Ok(())
} }
#[op] #[op2]
#[serde]
fn op_env(state: &mut OpState) -> Result<HashMap<String, String>, AnyError> { fn op_env(state: &mut OpState) -> Result<HashMap<String, String>, AnyError> {
state.borrow_mut::<PermissionsContainer>().check_env_all()?; state.borrow_mut::<PermissionsContainer>().check_env_all()?;
Ok(env::vars().collect()) Ok(env::vars().collect())
} }
#[op] #[op2]
#[string]
fn op_get_env( fn op_get_env(
state: &mut OpState, state: &mut OpState,
key: String, #[string] key: String,
) -> Result<Option<String>, AnyError> { ) -> Result<Option<String>, AnyError> {
let skip_permission_check = NODE_ENV_VAR_ALLOWLIST.contains(&key); let skip_permission_check = NODE_ENV_VAR_ALLOWLIST.contains(&key);
@ -133,8 +136,11 @@ fn op_get_env(
Ok(r) Ok(r)
} }
#[op] #[op2(fast)]
fn op_delete_env(state: &mut OpState, key: String) -> Result<(), AnyError> { fn op_delete_env(
state: &mut OpState,
#[string] key: String,
) -> Result<(), AnyError> {
state.borrow_mut::<PermissionsContainer>().check_env(&key)?; state.borrow_mut::<PermissionsContainer>().check_env(&key)?;
if key.is_empty() || key.contains(&['=', '\0'] as &[char]) { if key.is_empty() || key.contains(&['=', '\0'] as &[char]) {
return Err(type_error("Key contains invalid characters.")); return Err(type_error("Key contains invalid characters."));
@ -143,18 +149,19 @@ fn op_delete_env(state: &mut OpState, key: String) -> Result<(), AnyError> {
Ok(()) Ok(())
} }
#[op] #[op2(fast)]
fn op_set_exit_code(state: &mut OpState, code: i32) { fn op_set_exit_code(state: &mut OpState, #[smi] code: i32) {
state.borrow_mut::<ExitCode>().set(code); state.borrow_mut::<ExitCode>().set(code);
} }
#[op] #[op2(fast)]
fn op_exit(state: &mut OpState) { fn op_exit(state: &mut OpState) {
let code = state.borrow::<ExitCode>().get(); let code = state.borrow::<ExitCode>().get();
std::process::exit(code) std::process::exit(code)
} }
#[op] #[op2]
#[serde]
fn op_loadavg(state: &mut OpState) -> Result<(f64, f64, f64), AnyError> { fn op_loadavg(state: &mut OpState) -> Result<(f64, f64, f64), AnyError> {
state state
.borrow_mut::<PermissionsContainer>() .borrow_mut::<PermissionsContainer>()
@ -162,7 +169,8 @@ fn op_loadavg(state: &mut OpState) -> Result<(f64, f64, f64), AnyError> {
Ok(sys_info::loadavg()) Ok(sys_info::loadavg())
} }
#[op] #[op2]
#[string]
fn op_hostname(state: &mut OpState) -> Result<String, AnyError> { fn op_hostname(state: &mut OpState) -> Result<String, AnyError> {
state state
.borrow_mut::<PermissionsContainer>() .borrow_mut::<PermissionsContainer>()
@ -170,7 +178,8 @@ fn op_hostname(state: &mut OpState) -> Result<String, AnyError> {
Ok(sys_info::hostname()) Ok(sys_info::hostname())
} }
#[op] #[op2]
#[string]
fn op_os_release(state: &mut OpState) -> Result<String, AnyError> { fn op_os_release(state: &mut OpState) -> Result<String, AnyError> {
state state
.borrow_mut::<PermissionsContainer>() .borrow_mut::<PermissionsContainer>()
@ -178,7 +187,8 @@ fn op_os_release(state: &mut OpState) -> Result<String, AnyError> {
Ok(sys_info::os_release()) Ok(sys_info::os_release())
} }
#[op] #[op2]
#[serde]
fn op_network_interfaces( fn op_network_interfaces(
state: &mut OpState, state: &mut OpState,
) -> Result<Vec<NetworkInterface>, AnyError> { ) -> Result<Vec<NetworkInterface>, AnyError> {
@ -229,7 +239,8 @@ impl From<netif::Interface> for NetworkInterface {
} }
} }
#[op] #[op2]
#[serde]
fn op_system_memory_info( fn op_system_memory_info(
state: &mut OpState, state: &mut OpState,
) -> Result<Option<sys_info::MemInfo>, AnyError> { ) -> Result<Option<sys_info::MemInfo>, AnyError> {
@ -239,6 +250,7 @@ fn op_system_memory_info(
Ok(sys_info::mem_info()) Ok(sys_info::mem_info())
} }
// TODO(bartlomieju): op2 doesn't support cfg attrs
#[cfg(not(windows))] #[cfg(not(windows))]
#[op] #[op]
fn op_gid(state: &mut OpState) -> Result<Option<u32>, AnyError> { fn op_gid(state: &mut OpState) -> Result<Option<u32>, AnyError> {
@ -252,6 +264,7 @@ fn op_gid(state: &mut OpState) -> Result<Option<u32>, AnyError> {
} }
} }
// TODO(bartlomieju): op2 doesn't support cfg attrs
#[cfg(windows)] #[cfg(windows)]
#[op] #[op]
fn op_gid(state: &mut OpState) -> Result<Option<u32>, AnyError> { fn op_gid(state: &mut OpState) -> Result<Option<u32>, AnyError> {
@ -261,6 +274,7 @@ fn op_gid(state: &mut OpState) -> Result<Option<u32>, AnyError> {
Ok(None) Ok(None)
} }
// TODO(bartlomieju): op2 doesn't support cfg attrs
#[cfg(not(windows))] #[cfg(not(windows))]
#[op] #[op]
fn op_uid(state: &mut OpState) -> Result<Option<u32>, AnyError> { fn op_uid(state: &mut OpState) -> Result<Option<u32>, AnyError> {
@ -274,6 +288,7 @@ fn op_uid(state: &mut OpState) -> Result<Option<u32>, AnyError> {
} }
} }
// TODO(bartlomieju): op2 doesn't support cfg attrs
#[cfg(windows)] #[cfg(windows)]
#[op] #[op]
fn op_uid(state: &mut OpState) -> Result<Option<u32>, AnyError> { fn op_uid(state: &mut OpState) -> Result<Option<u32>, AnyError> {
@ -293,7 +308,8 @@ struct MemoryUsage {
external: usize, external: usize,
} }
#[op(v8)] #[op2]
#[serde]
fn op_runtime_memory_usage(scope: &mut v8::HandleScope) -> MemoryUsage { fn op_runtime_memory_usage(scope: &mut v8::HandleScope) -> MemoryUsage {
let mut s = v8::HeapStatistics::default(); let mut s = v8::HeapStatistics::default();
scope.get_heap_statistics(&mut s); scope.get_heap_statistics(&mut s);
@ -464,8 +480,3 @@ fn os_uptime(state: &mut OpState) -> Result<u64, AnyError> {
fn op_os_uptime(state: &mut OpState) -> Result<u64, AnyError> { fn op_os_uptime(state: &mut OpState) -> Result<u64, AnyError> {
os_uptime(state) os_uptime(state)
} }
#[op]
fn op_node_unstable_os_uptime(state: &mut OpState) -> Result<u64, AnyError> {
os_uptime(state)
}

View file

@ -6,7 +6,7 @@ use crate::permissions::PermissionsContainer;
use deno_core::error::custom_error; use deno_core::error::custom_error;
use deno_core::error::uri_error; use deno_core::error::uri_error;
use deno_core::error::AnyError; use deno_core::error::AnyError;
use deno_core::op; use deno_core::op2;
use deno_core::url; use deno_core::url;
use deno_core::OpState; use deno_core::OpState;
use serde::Deserialize; use serde::Deserialize;
@ -51,10 +51,11 @@ impl From<PermissionState> for PermissionStatus {
} }
} }
#[op] #[op2]
#[serde]
pub fn op_query_permission( pub fn op_query_permission(
state: &mut OpState, state: &mut OpState,
args: PermissionArgs, #[serde] args: PermissionArgs,
) -> Result<PermissionStatus, AnyError> { ) -> Result<PermissionStatus, AnyError> {
let permissions = state.borrow::<PermissionsContainer>().0.lock(); let permissions = state.borrow::<PermissionsContainer>().0.lock();
let path = args.path.as_deref(); let path = args.path.as_deref();
@ -85,10 +86,11 @@ pub fn op_query_permission(
Ok(PermissionStatus::from(perm)) Ok(PermissionStatus::from(perm))
} }
#[op] #[op2]
#[serde]
pub fn op_revoke_permission( pub fn op_revoke_permission(
state: &mut OpState, state: &mut OpState,
args: PermissionArgs, #[serde] args: PermissionArgs,
) -> Result<PermissionStatus, AnyError> { ) -> Result<PermissionStatus, AnyError> {
let mut permissions = state.borrow_mut::<PermissionsContainer>().0.lock(); let mut permissions = state.borrow_mut::<PermissionsContainer>().0.lock();
let path = args.path.as_deref(); let path = args.path.as_deref();
@ -119,10 +121,11 @@ pub fn op_revoke_permission(
Ok(PermissionStatus::from(perm)) Ok(PermissionStatus::from(perm))
} }
#[op] #[op2]
#[serde]
pub fn op_request_permission( pub fn op_request_permission(
state: &mut OpState, state: &mut OpState,
args: PermissionArgs, #[serde] args: PermissionArgs,
) -> Result<PermissionStatus, AnyError> { ) -> Result<PermissionStatus, AnyError> {
let mut permissions = state.borrow_mut::<PermissionsContainer>().0.lock(); let mut permissions = state.borrow_mut::<PermissionsContainer>().0.lock();
let path = args.path.as_deref(); let path = args.path.as_deref();

View file

@ -6,6 +6,7 @@ use deno_core::anyhow::Context;
use deno_core::error::type_error; use deno_core::error::type_error;
use deno_core::error::AnyError; use deno_core::error::AnyError;
use deno_core::op; use deno_core::op;
use deno_core::op2;
use deno_core::serde_json; use deno_core::serde_json;
use deno_core::AsyncMutFuture; use deno_core::AsyncMutFuture;
use deno_core::AsyncRefCell; use deno_core::AsyncRefCell;
@ -322,16 +323,18 @@ fn spawn_child(
}) })
} }
#[op] #[op2]
#[serde]
fn op_spawn_child( fn op_spawn_child(
state: &mut OpState, state: &mut OpState,
args: SpawnArgs, #[serde] args: SpawnArgs,
api_name: String, #[string] api_name: String,
) -> Result<Child, AnyError> { ) -> Result<Child, AnyError> {
let command = create_command(state, args, &api_name)?; let command = create_command(state, args, &api_name)?;
spawn_child(state, command) spawn_child(state, command)
} }
// TODO(bartlomieju): op2 doesn't support clippy allows
#[op] #[op]
async fn op_spawn_wait( async fn op_spawn_wait(
state: Rc<RefCell<OpState>>, state: Rc<RefCell<OpState>>,
@ -351,10 +354,11 @@ async fn op_spawn_wait(
result result
} }
#[op] #[op2]
#[serde]
fn op_spawn_sync( fn op_spawn_sync(
state: &mut OpState, state: &mut OpState,
args: SpawnArgs, #[serde] args: SpawnArgs,
) -> Result<SpawnOutput, AnyError> { ) -> Result<SpawnOutput, AnyError> {
let stdout = matches!(args.stdio.stdout, Stdio::Piped); let stdout = matches!(args.stdio.stdout, Stdio::Piped);
let stderr = matches!(args.stdio.stderr, 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( fn op_spawn_kill(
state: &mut OpState, state: &mut OpState,
rid: ResourceId, #[smi] rid: ResourceId,
signal: String, #[string] signal: String,
) -> Result<(), AnyError> { ) -> Result<(), AnyError> {
if let Ok(child_resource) = state.resource_table.get::<ChildResource>(rid) { if let Ok(child_resource) = state.resource_table.get::<ChildResource>(rid) {
deprecated::kill(child_resource.1 as i32, &signal)?; deprecated::kill(child_resource.1 as i32, &signal)?;
@ -432,7 +436,7 @@ mod deprecated {
#[derive(Serialize)] #[derive(Serialize)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
// TODO(@AaronO): maybe find a more descriptive name or a convention for return structs // TODO(@AaronO): maybe find a more descriptive name or a convention for return structs
struct RunInfo { pub struct RunInfo {
rid: ResourceId, rid: ResourceId,
pid: Option<u32>, pid: Option<u32>,
stdin_rid: Option<ResourceId>, stdin_rid: Option<ResourceId>,
@ -440,10 +444,11 @@ mod deprecated {
stderr_rid: Option<ResourceId>, stderr_rid: Option<ResourceId>,
} }
#[op] #[op2]
fn op_run( #[serde]
pub fn op_run(
state: &mut OpState, state: &mut OpState,
run_args: RunArgs, #[serde] run_args: RunArgs,
) -> Result<RunInfo, AnyError> { ) -> Result<RunInfo, AnyError> {
let args = run_args.cmd; let args = run_args.cmd;
state state
@ -557,16 +562,17 @@ mod deprecated {
#[derive(Serialize)] #[derive(Serialize)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
struct ProcessStatus { pub struct ProcessStatus {
got_signal: bool, got_signal: bool,
exit_code: i32, exit_code: i32,
exit_signal: i32, exit_signal: i32,
} }
#[op] #[op2(async)]
async fn op_run_status( #[serde]
pub async fn op_run_status(
state: Rc<RefCell<OpState>>, state: Rc<RefCell<OpState>>,
rid: ResourceId, #[smi] rid: ResourceId,
) -> Result<ProcessStatus, AnyError> { ) -> Result<ProcessStatus, AnyError> {
let resource = state let resource = state
.borrow_mut() .borrow_mut()
@ -648,12 +654,12 @@ mod deprecated {
} }
} }
#[op] #[op2(fast)]
fn op_kill( pub fn op_kill(
state: &mut OpState, state: &mut OpState,
pid: i32, #[smi] pid: i32,
signal: String, #[string] signal: String,
api_name: String, #[string] api_name: String,
) -> Result<(), AnyError> { ) -> Result<(), AnyError> {
state state
.borrow_mut::<PermissionsContainer>() .borrow_mut::<PermissionsContainer>()

View file

@ -2,7 +2,7 @@
use crate::permissions::PermissionsContainer; use crate::permissions::PermissionsContainer;
use deno_core::error::AnyError; use deno_core::error::AnyError;
use deno_core::op; use deno_core::op2;
use deno_core::ModuleSpecifier; use deno_core::ModuleSpecifier;
use deno_core::OpState; use deno_core::OpState;
@ -15,7 +15,8 @@ deno_core::extension!(
}, },
); );
#[op] #[op2]
#[string]
fn op_main_module(state: &mut OpState) -> Result<String, AnyError> { fn op_main_module(state: &mut OpState) -> Result<String, AnyError> {
let main_url = state.borrow::<ModuleSpecifier>(); let main_url = state.borrow::<ModuleSpecifier>();
let main_path = main_url.to_string(); let main_path = main_url.to_string();
@ -30,7 +31,8 @@ fn op_main_module(state: &mut OpState) -> Result<String, AnyError> {
/// This is an op instead of being done at initialization time because /// This is an op instead of being done at initialization time because
/// it's expensive to retrieve the ppid on Windows. /// it's expensive to retrieve the ppid on Windows.
#[op] #[op2]
#[bigint]
pub fn op_ppid() -> i64 { pub fn op_ppid() -> i64 {
#[cfg(windows)] #[cfg(windows)]
{ {

View file

@ -2,6 +2,7 @@
use deno_core::error::type_error; use deno_core::error::type_error;
use deno_core::error::AnyError; use deno_core::error::AnyError;
use deno_core::op; use deno_core::op;
use deno_core::op2;
use deno_core::AsyncRefCell; use deno_core::AsyncRefCell;
use deno_core::CancelFuture; use deno_core::CancelFuture;
use deno_core::CancelHandle; use deno_core::CancelHandle;
@ -580,10 +581,10 @@ fn op_signal_bind(
Ok(rid) Ok(rid)
} }
#[op] #[op2(async)]
async fn op_signal_poll( async fn op_signal_poll(
state: Rc<RefCell<OpState>>, state: Rc<RefCell<OpState>>,
rid: ResourceId, #[smi] rid: ResourceId,
) -> Result<bool, AnyError> { ) -> Result<bool, AnyError> {
let resource = state let resource = state
.borrow_mut() .borrow_mut()
@ -599,10 +600,10 @@ async fn op_signal_poll(
} }
} }
#[op] #[op2(fast)]
pub fn op_signal_unbind( pub fn op_signal_unbind(
state: &mut OpState, state: &mut OpState,
rid: ResourceId, #[smi] rid: ResourceId,
) -> Result<(), AnyError> { ) -> Result<(), AnyError> {
state.resource_table.close(rid)?; state.resource_table.close(rid)?;
Ok(()) Ok(())

View file

@ -73,7 +73,7 @@ fn mode_raw_input_off(original_mode: DWORD) -> DWORD {
original_mode & !wincon::ENABLE_VIRTUAL_TERMINAL_INPUT | COOKED_MODE original_mode & !wincon::ENABLE_VIRTUAL_TERMINAL_INPUT | COOKED_MODE
} }
#[op(fast)] #[op2(fast)]
fn op_stdin_set_raw( fn op_stdin_set_raw(
state: &mut OpState, state: &mut OpState,
is_raw: bool, is_raw: bool,