From 8f207c0f3f3a43d77e0c88cfdc840b4b742b9708 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Thu, 9 Mar 2023 10:56:19 -0400 Subject: refactor: Split extension registration for runtime and snapshotting (#18095) This commit splits "::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 --- ext/node/lib.rs | 91 ++++++++++++++++++++++++++++++++++----------------------- 1 file changed, 54 insertions(+), 37 deletions(-) (limited to 'ext/node/lib.rs') 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( - maybe_npm_resolver: Option>, -) -> 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( + ext: &mut ExtensionBuilder, + maybe_npm_resolver: Option>, +) -> &mut ExtensionBuilder { + ext .ops(vec![ ops::op_require_init_paths::decl(), ops::op_require_node_module_paths::decl::

(), @@ -395,9 +395,26 @@ pub fn init( state.put(npm_resolver); } }) +} + +pub fn init_ops_and_esm( + maybe_npm_resolver: Option>, +) -> Extension { + ops::

(&mut ext(), maybe_npm_resolver) + .esm(include_js_files!( + "01_node.js", + "02_require.js", + "module_es_shim.js", + )) .build() } +pub fn init_ops( + maybe_npm_resolver: Option>, +) -> Extension { + ops::

(&mut ext(), maybe_npm_resolver).build() +} + pub async fn initialize_runtime( js_runtime: &mut JsRuntime, uses_local_node_modules_dir: bool, -- cgit v1.2.3