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:
parent
aaff69db3f
commit
bdeb4bddbf
7 changed files with 90 additions and 65 deletions
|
@ -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<String>,
|
||||
}
|
||||
|
||||
#[op]
|
||||
#[op2]
|
||||
#[smi]
|
||||
fn op_fs_events_open(
|
||||
state: &mut OpState,
|
||||
args: OpenArgs,
|
||||
#[serde] args: OpenArgs,
|
||||
) -> Result<ResourceId, AnyError> {
|
||||
let (sender, receiver) = mpsc::channel::<Result<FsEvent, AnyError>>(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<RefCell<OpState>>,
|
||||
rid: ResourceId,
|
||||
#[smi] rid: ResourceId,
|
||||
) -> Result<Option<FsEvent>, AnyError> {
|
||||
let resource = state.borrow().resource_table.get::<FsEventsResource>(rid)?;
|
||||
let mut receiver = RcRef::map(&resource, |r| &r.receiver).borrow_mut().await;
|
||||
|
|
|
@ -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<String, AnyError> {
|
||||
let current_exe = env::current_exe().unwrap();
|
||||
state
|
||||
|
@ -75,11 +76,11 @@ fn op_exec_path(state: &mut OpState) -> Result<String, AnyError> {
|
|||
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::<PermissionsContainer>().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<HashMap<String, String>, AnyError> {
|
||||
state.borrow_mut::<PermissionsContainer>().check_env_all()?;
|
||||
Ok(env::vars().collect())
|
||||
}
|
||||
|
||||
#[op]
|
||||
#[op2]
|
||||
#[string]
|
||||
fn op_get_env(
|
||||
state: &mut OpState,
|
||||
key: String,
|
||||
#[string] key: String,
|
||||
) -> Result<Option<String>, 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::<PermissionsContainer>().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::<ExitCode>().set(code);
|
||||
}
|
||||
|
||||
#[op]
|
||||
#[op2(fast)]
|
||||
fn op_exit(state: &mut OpState) {
|
||||
let code = state.borrow::<ExitCode>().get();
|
||||
std::process::exit(code)
|
||||
}
|
||||
|
||||
#[op]
|
||||
#[op2]
|
||||
#[serde]
|
||||
fn op_loadavg(state: &mut OpState) -> Result<(f64, f64, f64), AnyError> {
|
||||
state
|
||||
.borrow_mut::<PermissionsContainer>()
|
||||
|
@ -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<String, AnyError> {
|
||||
state
|
||||
.borrow_mut::<PermissionsContainer>()
|
||||
|
@ -170,7 +178,8 @@ fn op_hostname(state: &mut OpState) -> Result<String, AnyError> {
|
|||
Ok(sys_info::hostname())
|
||||
}
|
||||
|
||||
#[op]
|
||||
#[op2]
|
||||
#[string]
|
||||
fn op_os_release(state: &mut OpState) -> Result<String, AnyError> {
|
||||
state
|
||||
.borrow_mut::<PermissionsContainer>()
|
||||
|
@ -178,7 +187,8 @@ fn op_os_release(state: &mut OpState) -> Result<String, AnyError> {
|
|||
Ok(sys_info::os_release())
|
||||
}
|
||||
|
||||
#[op]
|
||||
#[op2]
|
||||
#[serde]
|
||||
fn op_network_interfaces(
|
||||
state: &mut OpState,
|
||||
) -> Result<Vec<NetworkInterface>, AnyError> {
|
||||
|
@ -229,7 +239,8 @@ impl From<netif::Interface> for NetworkInterface {
|
|||
}
|
||||
}
|
||||
|
||||
#[op]
|
||||
#[op2]
|
||||
#[serde]
|
||||
fn op_system_memory_info(
|
||||
state: &mut OpState,
|
||||
) -> Result<Option<sys_info::MemInfo>, 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<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)]
|
||||
#[op]
|
||||
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)
|
||||
}
|
||||
|
||||
// TODO(bartlomieju): op2 doesn't support cfg attrs
|
||||
#[cfg(not(windows))]
|
||||
#[op]
|
||||
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)]
|
||||
#[op]
|
||||
fn op_uid(state: &mut OpState) -> Result<Option<u32>, 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<u64, AnyError> {
|
|||
fn op_os_uptime(state: &mut OpState) -> Result<u64, AnyError> {
|
||||
os_uptime(state)
|
||||
}
|
||||
|
||||
#[op]
|
||||
fn op_node_unstable_os_uptime(state: &mut OpState) -> Result<u64, AnyError> {
|
||||
os_uptime(state)
|
||||
}
|
||||
|
|
|
@ -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<PermissionState> for PermissionStatus {
|
|||
}
|
||||
}
|
||||
|
||||
#[op]
|
||||
#[op2]
|
||||
#[serde]
|
||||
pub fn op_query_permission(
|
||||
state: &mut OpState,
|
||||
args: PermissionArgs,
|
||||
#[serde] args: PermissionArgs,
|
||||
) -> Result<PermissionStatus, AnyError> {
|
||||
let permissions = state.borrow::<PermissionsContainer>().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<PermissionStatus, AnyError> {
|
||||
let mut permissions = state.borrow_mut::<PermissionsContainer>().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<PermissionStatus, AnyError> {
|
||||
let mut permissions = state.borrow_mut::<PermissionsContainer>().0.lock();
|
||||
let path = args.path.as_deref();
|
||||
|
|
|
@ -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<Child, AnyError> {
|
||||
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<RefCell<OpState>>,
|
||||
|
@ -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<SpawnOutput, AnyError> {
|
||||
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::<ChildResource>(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<u32>,
|
||||
stdin_rid: Option<ResourceId>,
|
||||
|
@ -440,10 +444,11 @@ mod deprecated {
|
|||
stderr_rid: Option<ResourceId>,
|
||||
}
|
||||
|
||||
#[op]
|
||||
fn op_run(
|
||||
#[op2]
|
||||
#[serde]
|
||||
pub fn op_run(
|
||||
state: &mut OpState,
|
||||
run_args: RunArgs,
|
||||
#[serde] run_args: RunArgs,
|
||||
) -> Result<RunInfo, AnyError> {
|
||||
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<RefCell<OpState>>,
|
||||
rid: ResourceId,
|
||||
#[smi] rid: ResourceId,
|
||||
) -> Result<ProcessStatus, AnyError> {
|
||||
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::<PermissionsContainer>()
|
||||
|
|
|
@ -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<String, AnyError> {
|
||||
let main_url = state.borrow::<ModuleSpecifier>();
|
||||
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
|
||||
/// it's expensive to retrieve the ppid on Windows.
|
||||
#[op]
|
||||
#[op2]
|
||||
#[bigint]
|
||||
pub fn op_ppid() -> i64 {
|
||||
#[cfg(windows)]
|
||||
{
|
||||
|
|
|
@ -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<RefCell<OpState>>,
|
||||
rid: ResourceId,
|
||||
#[smi] rid: ResourceId,
|
||||
) -> Result<bool, AnyError> {
|
||||
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(())
|
||||
|
|
|
@ -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,
|
||||
|
|
Loading…
Reference in a new issue