diff options
author | Bartek IwaĆczuk <biwanczuk@gmail.com> | 2023-03-09 10:56:19 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-03-09 14:56:19 +0000 |
commit | 8f207c0f3f3a43d77e0c88cfdc840b4b742b9708 (patch) | |
tree | a8699bca5ffffff03a9d72f1bed9208a349338ba /ext/node/lib.rs | |
parent | 99da8a69e7260b72e55d7214ec96f6ac5e759f35 (diff) |
refactor: Split extension registration for runtime and snapshotting (#18095)
This commit splits "<ext_name>::init" functions into "init_ops" and
"init_ops_and_esm". That way we don't have to construct list of
ESM sources on each startup if we're running with a snapshot.
In a follow up commit "deno_core" will be changed to not have a split
between "extensions" and "extensions_with_js" - it will be embedders'
responsibility to pass appropriately configured extensions.
Prerequisite for https://github.com/denoland/deno/pull/18080
Diffstat (limited to 'ext/node/lib.rs')
-rw-r--r-- | ext/node/lib.rs | 91 |
1 files changed, 54 insertions, 37 deletions
diff --git a/ext/node/lib.rs b/ext/node/lib.rs index 899a1d30c..1c9d9e0aa 100644 --- a/ext/node/lib.rs +++ b/ext/node/lib.rs @@ -5,6 +5,7 @@ use deno_core::include_js_files; use deno_core::located_script_name; use deno_core::op; use deno_core::Extension; +use deno_core::ExtensionBuilder; use deno_core::JsRuntime; use once_cell::sync::Lazy; use std::collections::HashSet; @@ -95,16 +96,34 @@ fn op_node_build_os() -> String { .to_string() } +fn ext_polyfill() -> ExtensionBuilder { + Extension::builder_with_deps(env!("CARGO_PKG_NAME"), &["deno_io", "deno_fs"]) +} + +fn ops_polyfill(ext: &mut ExtensionBuilder) -> &mut ExtensionBuilder { + ext.ops(vec![ + crypto::op_node_create_hash::decl(), + crypto::op_node_hash_update::decl(), + crypto::op_node_hash_update_str::decl(), + crypto::op_node_hash_digest::decl(), + crypto::op_node_hash_digest_hex::decl(), + crypto::op_node_hash_clone::decl(), + crypto::op_node_private_encrypt::decl(), + crypto::op_node_private_decrypt::decl(), + crypto::op_node_public_encrypt::decl(), + winerror::op_node_sys_to_uv_error::decl(), + v8::op_v8_cached_data_version_tag::decl(), + v8::op_v8_get_heap_statistics::decl(), + idna::op_node_idna_domain_to_ascii::decl(), + idna::op_node_idna_domain_to_unicode::decl(), + idna::op_node_idna_punycode_decode::decl(), + idna::op_node_idna_punycode_encode::decl(), + op_node_build_os::decl(), + ]) +} + pub fn init_polyfill_ops() -> Extension { - Extension::builder(env!("CARGO_PKG_NAME")) - .ops(vec![ - crypto::op_node_create_hash::decl(), - crypto::op_node_hash_update::decl(), - crypto::op_node_hash_digest::decl(), - crypto::op_node_hash_clone::decl(), - op_node_build_os::decl(), - ]) - .build() + ops_polyfill(&mut ext_polyfill()).build() } pub fn init_polyfill_ops_and_esm() -> Extension { @@ -332,40 +351,21 @@ pub fn init_polyfill_ops_and_esm() -> Extension { "zlib.ts", ); - Extension::builder_with_deps(env!("CARGO_PKG_NAME"), &["deno_io", "deno_fs"]) + ops_polyfill(&mut ext_polyfill()) .esm(esm_files) .esm_entry_point("ext:deno_node/module_all.ts") - .ops(vec![ - crypto::op_node_create_hash::decl(), - crypto::op_node_hash_update::decl(), - crypto::op_node_hash_update_str::decl(), - crypto::op_node_hash_digest::decl(), - crypto::op_node_hash_digest_hex::decl(), - crypto::op_node_hash_clone::decl(), - crypto::op_node_private_encrypt::decl(), - crypto::op_node_private_decrypt::decl(), - crypto::op_node_public_encrypt::decl(), - winerror::op_node_sys_to_uv_error::decl(), - v8::op_v8_cached_data_version_tag::decl(), - v8::op_v8_get_heap_statistics::decl(), - idna::op_node_idna_domain_to_ascii::decl(), - idna::op_node_idna_domain_to_unicode::decl(), - idna::op_node_idna_punycode_decode::decl(), - idna::op_node_idna_punycode_encode::decl(), - op_node_build_os::decl(), - ]) .build() } -pub fn init<P: NodePermissions + 'static>( - maybe_npm_resolver: Option<Rc<dyn RequireNpmResolver>>, -) -> Extension { +fn ext() -> ExtensionBuilder { Extension::builder("deno_node_loading") - .esm(include_js_files!( - "01_node.js", - "02_require.js", - "module_es_shim.js", - )) +} + +fn ops<P: NodePermissions + 'static>( + ext: &mut ExtensionBuilder, + maybe_npm_resolver: Option<Rc<dyn RequireNpmResolver>>, +) -> &mut ExtensionBuilder { + ext .ops(vec![ ops::op_require_init_paths::decl(), ops::op_require_node_module_paths::decl::<P>(), @@ -395,9 +395,26 @@ pub fn init<P: NodePermissions + 'static>( state.put(npm_resolver); } }) +} + +pub fn init_ops_and_esm<P: NodePermissions + 'static>( + maybe_npm_resolver: Option<Rc<dyn RequireNpmResolver>>, +) -> Extension { + ops::<P>(&mut ext(), maybe_npm_resolver) + .esm(include_js_files!( + "01_node.js", + "02_require.js", + "module_es_shim.js", + )) .build() } +pub fn init_ops<P: NodePermissions + 'static>( + maybe_npm_resolver: Option<Rc<dyn RequireNpmResolver>>, +) -> Extension { + ops::<P>(&mut ext(), maybe_npm_resolver).build() +} + pub async fn initialize_runtime( js_runtime: &mut JsRuntime, uses_local_node_modules_dir: bool, |