From 29784df24eefcbe3ce015c8621c20b0bdbc54090 Mon Sep 17 00:00:00 2001 From: Matt Mastracci Date: Thu, 7 Sep 2023 13:19:20 -0600 Subject: [PATCH] chore(ext/fs): port some ops to op2 (#20402) Port as many of these ops as we can to `op2`. Waiting on a few `deno_core` updates to complete this file. --- ext/fs/ops.rs | 355 ++++++++++++++++++++++++++------------------------ 1 file changed, 188 insertions(+), 167 deletions(-) diff --git a/ext/fs/ops.rs b/ext/fs/ops.rs index da52318a45..c98b214c28 100644 --- a/ext/fs/ops.rs +++ b/ext/fs/ops.rs @@ -12,6 +12,7 @@ use deno_core::error::custom_error; use deno_core::error::type_error; use deno_core::error::AnyError; use deno_core::op; +use deno_core::op2; use deno_core::CancelFuture; use deno_core::CancelHandle; use deno_core::JsBuffer; @@ -34,7 +35,8 @@ use crate::interface::FsFileType; use crate::FsPermissions; use crate::OpenOptions; -#[op] +#[op2] +#[string] pub fn op_fs_cwd

(state: &mut OpState) -> Result where P: FsPermissions + 'static, @@ -48,8 +50,11 @@ where Ok(path_str) } -#[op] -fn op_fs_chdir

(state: &mut OpState, directory: &str) -> Result<(), AnyError> +#[op2(fast)] +pub fn op_fs_chdir

( + state: &mut OpState, + #[string] directory: &str, +) -> Result<(), AnyError> where P: FsPermissions + 'static, { @@ -61,8 +66,8 @@ where .context_path("chdir", &d) } -#[op] -fn op_fs_umask( +#[op2] +pub fn op_fs_umask( state: &mut OpState, mask: Option, ) -> Result @@ -72,11 +77,12 @@ where state.borrow::().umask(mask).context("umask") } -#[op] -fn op_fs_open_sync

( +#[op2] +#[smi] +pub fn op_fs_open_sync

( state: &mut OpState, - path: String, - options: Option, + #[string] path: String, + #[serde] options: Option, ) -> Result where P: FsPermissions + 'static, @@ -96,11 +102,12 @@ where Ok(rid) } -#[op] -async fn op_fs_open_async

( +#[op2(async)] +#[smi] +pub async fn op_fs_open_async

( state: Rc>, - path: String, - options: Option, + #[string] path: String, + #[serde] options: Option, ) -> Result where P: FsPermissions + 'static, @@ -126,10 +133,10 @@ where Ok(rid) } -#[op] -fn op_fs_mkdir_sync

( +#[op2] +pub fn op_fs_mkdir_sync

( state: &mut OpState, - path: String, + #[string] path: String, recursive: bool, mode: Option, ) -> Result<(), AnyError> @@ -151,10 +158,10 @@ where Ok(()) } -#[op] -async fn op_fs_mkdir_async

( +#[op2(async)] +pub async fn op_fs_mkdir_async

( state: Rc>, - path: String, + #[string] path: String, recursive: bool, mode: Option, ) -> Result<(), AnyError> @@ -178,10 +185,10 @@ where Ok(()) } -#[op] -fn op_fs_chmod_sync

( +#[op2(fast)] +pub fn op_fs_chmod_sync

( state: &mut OpState, - path: String, + #[string] path: String, mode: u32, ) -> Result<(), AnyError> where @@ -196,10 +203,10 @@ where Ok(()) } -#[op] -async fn op_fs_chmod_async

( +#[op2(async)] +pub async fn op_fs_chmod_async

( state: Rc>, - path: String, + #[string] path: String, mode: u32, ) -> Result<(), AnyError> where @@ -217,10 +224,10 @@ where Ok(()) } -#[op] -fn op_fs_chown_sync

( +#[op2] +pub fn op_fs_chown_sync

( state: &mut OpState, - path: String, + #[string] path: String, uid: Option, gid: Option, ) -> Result<(), AnyError> @@ -237,10 +244,10 @@ where Ok(()) } -#[op] -async fn op_fs_chown_async

( +#[op2(async)] +pub async fn op_fs_chown_async

( state: Rc>, - path: String, + #[string] path: String, uid: Option, gid: Option, ) -> Result<(), AnyError> @@ -259,10 +266,10 @@ where Ok(()) } -#[op] -fn op_fs_remove_sync

( +#[op2(fast)] +pub fn op_fs_remove_sync

( state: &mut OpState, - path: &str, + #[string] path: &str, recursive: bool, ) -> Result<(), AnyError> where @@ -281,10 +288,10 @@ where Ok(()) } -#[op] -async fn op_fs_remove_async

( +#[op2(async)] +pub async fn op_fs_remove_async

( state: Rc>, - path: String, + #[string] path: String, recursive: bool, ) -> Result<(), AnyError> where @@ -314,11 +321,11 @@ where Ok(()) } -#[op] -fn op_fs_copy_file_sync

( +#[op2(fast)] +pub fn op_fs_copy_file_sync

( state: &mut OpState, - from: &str, - to: &str, + #[string] from: &str, + #[string] to: &str, ) -> Result<(), AnyError> where P: FsPermissions + 'static, @@ -337,11 +344,11 @@ where Ok(()) } -#[op] -async fn op_fs_copy_file_async

( +#[op2(async)] +pub async fn op_fs_copy_file_async

( state: Rc>, - from: String, - to: String, + #[string] from: String, + #[string] to: String, ) -> Result<(), AnyError> where P: FsPermissions + 'static, @@ -365,7 +372,7 @@ where } #[op] -fn op_fs_stat_sync

( +pub fn op_fs_stat_sync

( state: &mut OpState, path: String, stat_out_buf: &mut [u32], @@ -384,10 +391,11 @@ where Ok(()) } -#[op] -async fn op_fs_stat_async

( +#[op2(async)] +#[serde] +pub async fn op_fs_stat_async

( state: Rc>, - path: String, + #[string] path: String, ) -> Result where P: FsPermissions + 'static, @@ -407,7 +415,7 @@ where } #[op] -fn op_fs_lstat_sync

( +pub fn op_fs_lstat_sync

( state: &mut OpState, path: String, stat_out_buf: &mut [u32], @@ -426,10 +434,11 @@ where Ok(()) } -#[op] -async fn op_fs_lstat_async

( +#[op2(async)] +#[serde] +pub async fn op_fs_lstat_async

( state: Rc>, - path: String, + #[string] path: String, ) -> Result where P: FsPermissions + 'static, @@ -448,10 +457,11 @@ where Ok(SerializableStat::from(stat)) } -#[op] -fn op_fs_realpath_sync

( +#[op2] +#[string] +pub fn op_fs_realpath_sync

( state: &mut OpState, - path: String, + #[string] path: String, ) -> Result where P: FsPermissions + 'static, @@ -472,10 +482,11 @@ where Ok(path_string) } -#[op] -async fn op_fs_realpath_async

( +#[op2(async)] +#[string] +pub async fn op_fs_realpath_async

( state: Rc>, - path: String, + #[string] path: String, ) -> Result where P: FsPermissions + 'static, @@ -501,10 +512,11 @@ where Ok(path_string) } -#[op] -fn op_fs_read_dir_sync

( +#[op2] +#[serde] +pub fn op_fs_read_dir_sync

( state: &mut OpState, - path: String, + #[string] path: String, ) -> Result, AnyError> where P: FsPermissions + 'static, @@ -521,10 +533,11 @@ where Ok(entries) } -#[op] -async fn op_fs_read_dir_async

( +#[op2(async)] +#[serde] +pub async fn op_fs_read_dir_async

( state: Rc>, - path: String, + #[string] path: String, ) -> Result, AnyError> where P: FsPermissions + 'static, @@ -547,11 +560,11 @@ where Ok(entries) } -#[op] -fn op_fs_rename_sync

( +#[op2(fast)] +pub fn op_fs_rename_sync

( state: &mut OpState, - oldpath: String, - newpath: String, + #[string] oldpath: String, + #[string] newpath: String, ) -> Result<(), AnyError> where P: FsPermissions + 'static, @@ -571,11 +584,11 @@ where Ok(()) } -#[op] -async fn op_fs_rename_async

( +#[op2(async)] +pub async fn op_fs_rename_async

( state: Rc>, - oldpath: String, - newpath: String, + #[string] oldpath: String, + #[string] newpath: String, ) -> Result<(), AnyError> where P: FsPermissions + 'static, @@ -599,11 +612,11 @@ where Ok(()) } -#[op] -fn op_fs_link_sync

( +#[op2(fast)] +pub fn op_fs_link_sync

( state: &mut OpState, - oldpath: &str, - newpath: &str, + #[string] oldpath: &str, + #[string] newpath: &str, ) -> Result<(), AnyError> where P: FsPermissions + 'static, @@ -624,11 +637,11 @@ where Ok(()) } -#[op] -async fn op_fs_link_async

( +#[op2(async)] +pub async fn op_fs_link_async

( state: Rc>, - oldpath: String, - newpath: String, + #[string] oldpath: String, + #[string] newpath: String, ) -> Result<(), AnyError> where P: FsPermissions + 'static, @@ -653,12 +666,12 @@ where Ok(()) } -#[op] -fn op_fs_symlink_sync

( +#[op2] +pub fn op_fs_symlink_sync

( state: &mut OpState, - oldpath: &str, - newpath: &str, - file_type: Option, + #[string] oldpath: &str, + #[string] newpath: &str, + #[serde] file_type: Option, ) -> Result<(), AnyError> where P: FsPermissions + 'static, @@ -677,12 +690,12 @@ where Ok(()) } -#[op] -async fn op_fs_symlink_async

( +#[op2(async)] +pub async fn op_fs_symlink_async

( state: Rc>, - oldpath: String, - newpath: String, - file_type: Option, + #[string] oldpath: String, + #[string] newpath: String, + #[serde] file_type: Option, ) -> Result<(), AnyError> where P: FsPermissions + 'static, @@ -705,10 +718,11 @@ where Ok(()) } -#[op] -fn op_fs_read_link_sync

( +#[op2] +#[string] +pub fn op_fs_read_link_sync

( state: &mut OpState, - path: String, + #[string] path: String, ) -> Result where P: FsPermissions + 'static, @@ -726,10 +740,11 @@ where Ok(target_string) } -#[op] -async fn op_fs_read_link_async

( +#[op2(async)] +#[string] +pub async fn op_fs_read_link_async

( state: Rc>, - path: String, + #[string] path: String, ) -> Result where P: FsPermissions + 'static, @@ -753,7 +768,7 @@ where } #[op] -fn op_fs_truncate_sync

( +pub fn op_fs_truncate_sync

( state: &mut OpState, path: &str, len: u64, @@ -775,7 +790,7 @@ where } #[op] -async fn op_fs_truncate_async

( +pub async fn op_fs_truncate_async

( state: Rc>, path: String, len: u64, @@ -801,7 +816,7 @@ where } #[op] -fn op_fs_utime_sync

( +pub fn op_fs_utime_sync

( state: &mut OpState, path: &str, atime_secs: i64, @@ -824,7 +839,7 @@ where } #[op] -async fn op_fs_utime_async

( +pub async fn op_fs_utime_async

( state: Rc>, path: String, atime_secs: i64, @@ -856,12 +871,13 @@ where Ok(()) } -#[op] -fn op_fs_make_temp_dir_sync

( +#[op2] +#[string] +pub fn op_fs_make_temp_dir_sync

( state: &mut OpState, - dir: Option, - prefix: Option, - suffix: Option, + #[string] dir: Option, + #[string] prefix: Option, + #[string] suffix: Option, ) -> Result where P: FsPermissions + 'static, @@ -889,12 +905,13 @@ where .context("tmpdir") } -#[op] -async fn op_fs_make_temp_dir_async

( +#[op2(async)] +#[string] +pub async fn op_fs_make_temp_dir_async

( state: Rc>, - dir: Option, - prefix: Option, - suffix: Option, + #[string] dir: Option, + #[string] prefix: Option, + #[string] suffix: Option, ) -> Result where P: FsPermissions + 'static, @@ -922,12 +939,13 @@ where .context("tmpdir") } -#[op] -fn op_fs_make_temp_file_sync

( +#[op2] +#[string] +pub fn op_fs_make_temp_file_sync

( state: &mut OpState, - dir: Option, - prefix: Option, - suffix: Option, + #[string] dir: Option, + #[string] prefix: Option, + #[string] suffix: Option, ) -> Result where P: FsPermissions + 'static, @@ -962,12 +980,13 @@ where .context("tmpfile") } -#[op] -async fn op_fs_make_temp_file_async

( +#[op2(async)] +#[string] +pub async fn op_fs_make_temp_file_async

( state: Rc>, - dir: Option, - prefix: Option, - suffix: Option, + #[string] dir: Option, + #[string] prefix: Option, + #[string] suffix: Option, ) -> Result where P: FsPermissions + 'static, @@ -1077,15 +1096,15 @@ fn tmp_name( Ok(path) } -#[op] -fn op_fs_write_file_sync

( +#[op2] +pub fn op_fs_write_file_sync

( state: &mut OpState, - path: String, + #[string] path: String, mode: Option, append: bool, create: bool, create_new: bool, - data: JsBuffer, + #[buffer] data: JsBuffer, ) -> Result<(), AnyError> where P: FsPermissions + 'static, @@ -1105,7 +1124,7 @@ where } #[op] -async fn op_fs_write_file_async

( +pub async fn op_fs_write_file_async

( state: Rc>, path: String, mode: Option, @@ -1148,10 +1167,11 @@ where Ok(()) } -#[op] -fn op_fs_read_file_sync

( +#[op2] +#[serde] +pub fn op_fs_read_file_sync

( state: &mut OpState, - path: String, + #[string] path: String, ) -> Result where P: FsPermissions + 'static, @@ -1168,7 +1188,7 @@ where } #[op] -async fn op_fs_read_file_async

( +pub async fn op_fs_read_file_async

( state: Rc>, path: String, cancel_rid: Option, @@ -1205,7 +1225,7 @@ where } #[op] -fn op_fs_read_file_text_sync

( +pub fn op_fs_read_file_text_sync

( state: &mut OpState, path: String, ) -> Result @@ -1224,7 +1244,7 @@ where } #[op] -async fn op_fs_read_file_text_async

( +pub async fn op_fs_read_file_text_async

( state: Rc>, path: String, cancel_rid: Option, @@ -1284,7 +1304,7 @@ fn to_seek_from(offset: i64, whence: i32) -> Result { } #[op] -fn op_fs_seek_sync( +pub fn op_fs_seek_sync( state: &mut OpState, rid: ResourceId, offset: i64, @@ -1297,7 +1317,7 @@ fn op_fs_seek_sync( } #[op] -async fn op_fs_seek_async( +pub async fn op_fs_seek_async( state: Rc>, rid: ResourceId, offset: i64, @@ -1309,40 +1329,40 @@ async fn op_fs_seek_async( Ok(cursor) } -#[op] -fn op_fs_fdatasync_sync( +#[op2(fast)] +pub fn op_fs_fdatasync_sync( state: &mut OpState, - rid: ResourceId, + #[smi] rid: ResourceId, ) -> Result<(), AnyError> { let file = FileResource::get_file(state, rid)?; file.datasync_sync()?; Ok(()) } -#[op] -async fn op_fs_fdatasync_async( +#[op2(async)] +pub async fn op_fs_fdatasync_async( state: Rc>, - rid: ResourceId, + #[smi] rid: ResourceId, ) -> Result<(), AnyError> { let file = FileResource::get_file(&state.borrow(), rid)?; file.datasync_async().await?; Ok(()) } -#[op] -fn op_fs_fsync_sync( +#[op2(fast)] +pub fn op_fs_fsync_sync( state: &mut OpState, - rid: ResourceId, + #[smi] rid: ResourceId, ) -> Result<(), AnyError> { let file = FileResource::get_file(state, rid)?; file.sync_sync()?; Ok(()) } -#[op] -async fn op_fs_fsync_async( +#[op2(async)] +pub async fn op_fs_fsync_async( state: Rc>, - rid: ResourceId, + #[smi] rid: ResourceId, ) -> Result<(), AnyError> { let file = FileResource::get_file(&state.borrow(), rid)?; file.sync_async().await?; @@ -1350,7 +1370,7 @@ async fn op_fs_fsync_async( } #[op] -fn op_fs_fstat_sync( +pub fn op_fs_fstat_sync( state: &mut OpState, rid: ResourceId, stat_out_buf: &mut [u32], @@ -1362,20 +1382,21 @@ fn op_fs_fstat_sync( Ok(()) } -#[op] -async fn op_fs_fstat_async( +#[op2(async)] +#[serde] +pub async fn op_fs_fstat_async( state: Rc>, - rid: ResourceId, + #[smi] rid: ResourceId, ) -> Result { let file = FileResource::get_file(&state.borrow(), rid)?; let stat = file.stat_async().await?; Ok(stat.into()) } -#[op] -fn op_fs_flock_sync( +#[op2(fast)] +pub fn op_fs_flock_sync( state: &mut OpState, - rid: ResourceId, + #[smi] rid: ResourceId, exclusive: bool, ) -> Result<(), AnyError> { check_unstable(state, "Deno.flockSync"); @@ -1384,10 +1405,10 @@ fn op_fs_flock_sync( Ok(()) } -#[op] -async fn op_fs_flock_async( +#[op2(async)] +pub async fn op_fs_flock_async( state: Rc>, - rid: ResourceId, + #[smi] rid: ResourceId, exclusive: bool, ) -> Result<(), AnyError> { check_unstable2(&state, "Deno.flock"); @@ -1396,10 +1417,10 @@ async fn op_fs_flock_async( Ok(()) } -#[op] -fn op_fs_funlock_sync( +#[op2(fast)] +pub fn op_fs_funlock_sync( state: &mut OpState, - rid: ResourceId, + #[smi] rid: ResourceId, ) -> Result<(), AnyError> { check_unstable(state, "Deno.funlockSync"); let file = FileResource::get_file(state, rid)?; @@ -1407,10 +1428,10 @@ fn op_fs_funlock_sync( Ok(()) } -#[op] -async fn op_fs_funlock_async( +#[op2(async)] +pub async fn op_fs_funlock_async( state: Rc>, - rid: ResourceId, + #[smi] rid: ResourceId, ) -> Result<(), AnyError> { check_unstable2(&state, "Deno.funlock"); let file = FileResource::get_file(&state.borrow(), rid)?; @@ -1419,7 +1440,7 @@ async fn op_fs_funlock_async( } #[op] -fn op_fs_ftruncate_sync( +pub fn op_fs_ftruncate_sync( state: &mut OpState, rid: ResourceId, len: u64, @@ -1430,7 +1451,7 @@ fn op_fs_ftruncate_sync( } #[op] -async fn op_fs_ftruncate_async( +pub async fn op_fs_ftruncate_async( state: Rc>, rid: ResourceId, len: u64, @@ -1441,7 +1462,7 @@ async fn op_fs_ftruncate_async( } #[op] -fn op_fs_futime_sync( +pub fn op_fs_futime_sync( state: &mut OpState, rid: ResourceId, atime_secs: i64, @@ -1455,7 +1476,7 @@ fn op_fs_futime_sync( } #[op] -async fn op_fs_futime_async( +pub async fn op_fs_futime_async( state: Rc>, rid: ResourceId, atime_secs: i64, @@ -1575,7 +1596,7 @@ macro_rules! create_struct_writer { #[derive(Serialize)] #[serde(rename_all = "camelCase")] - struct $name { + pub struct $name { $($field: $type),* } };