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/io/lib.rs | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) (limited to 'ext/io/lib.rs') diff --git a/ext/io/lib.rs b/ext/io/lib.rs index 8eebc93ae..92f681f1e 100644 --- a/ext/io/lib.rs +++ b/ext/io/lib.rs @@ -13,6 +13,7 @@ use deno_core::BufView; use deno_core::CancelHandle; use deno_core::CancelTryFuture; use deno_core::Extension; +use deno_core::ExtensionBuilder; use deno_core::OpState; use deno_core::RcRef; use deno_core::Resource; @@ -78,13 +79,16 @@ pub static STDERR_HANDLE: Lazy = Lazy::new(|| { unsafe { StdFile::from_raw_handle(GetStdHandle(winbase::STD_ERROR_HANDLE)) } }); -pub fn init(stdio: Stdio) -> Extension { - // todo(dsheret): don't do this? Taking out the writers was necessary to prevent invalid handle panics - let stdio = Rc::new(RefCell::new(Some(stdio))); - +fn ext() -> ExtensionBuilder { Extension::builder_with_deps("deno_io", &["deno_web"]) +} + +fn ops( + ext: &mut ExtensionBuilder, + stdio: Rc>>, +) -> &mut ExtensionBuilder { + ext .ops(vec![op_read_sync::decl(), op_write_sync::decl()]) - .esm(include_js_files!("12_io.js",)) .middleware(|op| match op.name { "op_print" => op_print::decl(), _ => op, @@ -132,9 +136,24 @@ pub fn init(stdio: Stdio) -> Extension { )); assert_eq!(rid, 2, "stderr must have ResourceId 2"); }) +} + +pub fn init_ops_and_esm(stdio: Stdio) -> Extension { + // todo(dsheret): don't do this? Taking out the writers was necessary to prevent invalid handle panics + let stdio = Rc::new(RefCell::new(Some(stdio))); + + ops(&mut ext(), stdio) + .esm(include_js_files!("12_io.js",)) .build() } +pub fn init_ops(stdio: Stdio) -> Extension { + // todo(dsheret): don't do this? Taking out the writers was necessary to prevent invalid handle panics + let stdio = Rc::new(RefCell::new(Some(stdio))); + + ops(&mut ext(), stdio).build() +} + pub enum StdioPipe { Inherit, File(StdFile), -- cgit v1.2.3