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
This commit is contained in:
Bartek Iwańczuk 2023-03-09 10:56:19 -04:00 committed by GitHub
parent 99da8a69e7
commit 8f207c0f3f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
26 changed files with 1015 additions and 496 deletions

View file

@ -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,