From e55b448730160a6e4df9815a268d4049ac89deab Mon Sep 17 00:00:00 2001 From: Matt Mastracci Date: Fri, 17 Mar 2023 12:22:15 -0600 Subject: feat(core) deno_core::extension! macro to simplify extension registration (#18210) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This implements two macros to simplify extension registration and centralize a lot of the boilerplate as a base for future improvements: * `deno_core::ops!` registers a block of `#[op]`s, optionally with type parameters, useful for places where we share lists of ops * `deno_core::extension!` is used to register an extension, and creates two methods that can be used at runtime/snapshot generation time: `init_ops` and `init_ops_and_esm`. --------- Co-authored-by: Bartek IwaƄczuk --- ext/fs/lib.rs | 161 ++++++++++++++++++++++++++-------------------------------- 1 file changed, 72 insertions(+), 89 deletions(-) (limited to 'ext/fs/lib.rs') diff --git a/ext/fs/lib.rs b/ext/fs/lib.rs index 746d40dff..48b0e3495 100644 --- a/ext/fs/lib.rs +++ b/ext/fs/lib.rs @@ -4,12 +4,9 @@ use deno_core::error::custom_error; use deno_core::error::type_error; use deno_core::error::AnyError; -use deno_core::include_js_files; use deno_core::op; use deno_core::CancelFuture; use deno_core::CancelHandle; -use deno_core::Extension; -use deno_core::ExtensionBuilder; use deno_core::OpState; use deno_core::ResourceId; use deno_core::ZeroCopyBuf; @@ -118,92 +115,78 @@ use deno_core::error::generic_error; #[cfg(not(unix))] use deno_core::error::not_supported; -fn ext() -> ExtensionBuilder { - Extension::builder("deno_fs") -} - -fn ops( - ext: &mut ExtensionBuilder, - unstable: bool, -) -> &mut ExtensionBuilder { - ext - .state(move |state| { - state.put(UnstableChecker { unstable }); - }) - .ops(vec![ - op_open_sync::decl::

(), - op_open_async::decl::

(), - op_write_file_sync::decl::

(), - op_write_file_async::decl::

(), - op_seek_sync::decl(), - op_seek_async::decl(), - op_fdatasync_sync::decl(), - op_fdatasync_async::decl(), - op_fsync_sync::decl(), - op_fsync_async::decl(), - op_fstat_sync::decl(), - op_fstat_async::decl(), - op_flock_sync::decl(), - op_flock_async::decl(), - op_funlock_sync::decl(), - op_funlock_async::decl(), - op_umask::decl(), - op_chdir::decl::

(), - op_mkdir_sync::decl::

(), - op_mkdir_async::decl::

(), - op_chmod_sync::decl::

(), - op_chmod_async::decl::

(), - op_chown_sync::decl::

(), - op_chown_async::decl::

(), - op_remove_sync::decl::

(), - op_remove_async::decl::

(), - op_copy_file_sync::decl::

(), - op_copy_file_async::decl::

(), - op_stat_sync::decl::

(), - op_stat_async::decl::

(), - op_realpath_sync::decl::

(), - op_realpath_async::decl::

(), - op_read_dir_sync::decl::

(), - op_read_dir_async::decl::

(), - op_rename_sync::decl::

(), - op_rename_async::decl::

(), - op_link_sync::decl::

(), - op_link_async::decl::

(), - op_symlink_sync::decl::

(), - op_symlink_async::decl::

(), - op_read_link_sync::decl::

(), - op_read_link_async::decl::

(), - op_ftruncate_sync::decl(), - op_ftruncate_async::decl(), - op_truncate_sync::decl::

(), - op_truncate_async::decl::

(), - op_make_temp_dir_sync::decl::

(), - op_make_temp_dir_async::decl::

(), - op_make_temp_file_sync::decl::

(), - op_make_temp_file_async::decl::

(), - op_cwd::decl::

(), - op_futime_sync::decl(), - op_futime_async::decl(), - op_utime_sync::decl::

(), - op_utime_async::decl::

(), - op_readfile_sync::decl::

(), - op_readfile_text_sync::decl::

(), - op_readfile_async::decl::

(), - op_readfile_text_async::decl::

(), - ]) -} - -pub fn init_ops_and_esm( - unstable: bool, -) -> Extension { - ops::

(&mut ext(), unstable) - .esm(include_js_files!("30_fs.js",)) - .build() -} - -pub fn init_ops(unstable: bool) -> Extension { - ops::

(&mut ext(), unstable).build() -} +deno_core::extension!(deno_fs, + deps = [ deno_web, deno_io ], + parameters = [P: FsPermissions], + ops = [ + op_open_sync

, + op_open_async

, + op_write_file_sync

, + op_write_file_async

, + op_seek_sync, + op_seek_async, + op_fdatasync_sync, + op_fdatasync_async, + op_fsync_sync, + op_fsync_async, + op_fstat_sync, + op_fstat_async, + op_flock_sync, + op_flock_async, + op_funlock_sync, + op_funlock_async, + op_umask, + op_chdir

, + op_mkdir_sync

, + op_mkdir_async

, + op_chmod_sync

, + op_chmod_async

, + op_chown_sync

, + op_chown_async

, + op_remove_sync

, + op_remove_async

, + op_copy_file_sync

, + op_copy_file_async

, + op_stat_sync

, + op_stat_async

, + op_realpath_sync

, + op_realpath_async

, + op_read_dir_sync

, + op_read_dir_async

, + op_rename_sync

, + op_rename_async

, + op_link_sync

, + op_link_async

, + op_symlink_sync

, + op_symlink_async

, + op_read_link_sync

, + op_read_link_async

, + op_ftruncate_sync, + op_ftruncate_async, + op_truncate_sync

, + op_truncate_async

, + op_make_temp_dir_sync

, + op_make_temp_dir_async

, + op_make_temp_file_sync

, + op_make_temp_file_async

, + op_cwd

, + op_futime_sync, + op_futime_async, + op_utime_sync

, + op_utime_async

, + op_readfile_sync

, + op_readfile_text_sync

, + op_readfile_async

, + op_readfile_text_async

, + ], + esm = [ "30_fs.js" ], + config = { + unstable: bool + }, + state = |state, unstable| { + state.put(UnstableChecker { unstable }); + }, +); fn default_err_mapper(err: Error, desc: String) -> Error { Error::new(err.kind(), format!("{err}, {desc}")) -- cgit v1.2.3