From 3e16c3fe361d2db40a162e8037f5895ecc2fab3a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Tue, 7 Mar 2023 17:37:37 -0400 Subject: [PATCH] refactor(core): don't use Result in ExtensionBuilder::state (#18066) There's no point for this API to expect result. If something fails it should result in a panic during build time to signal to embedder that setup is wrong. --- cli/build.rs | 2 -- cli/lsp/tsc.rs | 1 - cli/ops/bench.rs | 1 - cli/ops/mod.rs | 1 - cli/ops/testing.rs | 1 - cli/tsc/mod.rs | 1 - core/examples/schedule_task.rs | 2 -- core/extensions.rs | 11 +++++------ core/runtime.rs | 6 ++---- ext/broadcast_channel/lib.rs | 1 - ext/cache/lib.rs | 1 - ext/crypto/lib.rs | 1 - ext/fetch/lib.rs | 1 - ext/ffi/lib.rs | 2 -- ext/flash/lib.rs | 1 - ext/fs/lib.rs | 1 - ext/io/lib.rs | 1 - ext/napi/lib.rs | 1 - ext/net/lib.rs | 1 - ext/net/ops.rs | 1 - ext/node/lib.rs | 1 - ext/web/benches/encoding.rs | 1 - ext/web/benches/timers_ops.rs | 1 - ext/web/lib.rs | 1 - ext/webgpu/lib.rs | 1 - ext/webgpu/surface.rs | 1 - ext/websocket/lib.rs | 1 - ext/webstorage/lib.rs | 1 - runtime/ops/os/mod.rs | 1 - runtime/ops/runtime.rs | 1 - runtime/ops/worker_host.rs | 2 -- runtime/web_worker.rs | 1 - runtime/worker.rs | 1 - 33 files changed, 7 insertions(+), 45 deletions(-) diff --git a/cli/build.rs b/cli/build.rs index c0a22ba128..21e702aed9 100644 --- a/cli/build.rs +++ b/cli/build.rs @@ -269,8 +269,6 @@ mod ts { state.put(op_crate_libs.clone()); state.put(build_libs.clone()); state.put(path_dts.clone()); - - Ok(()) }) .build(); diff --git a/cli/lsp/tsc.rs b/cli/lsp/tsc.rs index 2f66e2d2db..9a0c0dabe5 100644 --- a/cli/lsp/tsc.rs +++ b/cli/lsp/tsc.rs @@ -2826,7 +2826,6 @@ fn init_extension(performance: Arc) -> Extension { Arc::new(StateSnapshot::default()), performance.clone(), )); - Ok(()) }) .build() } diff --git a/cli/ops/bench.rs b/cli/ops/bench.rs index 2181a139c7..513ecc0d1f 100644 --- a/cli/ops/bench.rs +++ b/cli/ops/bench.rs @@ -38,7 +38,6 @@ pub fn init( .state(move |state| { state.put(sender.clone()); state.put(filter.clone()); - Ok(()) }) .build() } diff --git a/cli/ops/mod.rs b/cli/ops/mod.rs index c31a305316..53f74ad1f2 100644 --- a/cli/ops/mod.rs +++ b/cli/ops/mod.rs @@ -18,7 +18,6 @@ fn init_proc_state(ps: ProcState) -> Extension { .ops(vec![op_npm_process_state::decl()]) .state(move |state| { state.put(ps.clone()); - Ok(()) }) .build() } diff --git a/cli/ops/testing.rs b/cli/ops/testing.rs index e9d0acf41e..9170e9400d 100644 --- a/cli/ops/testing.rs +++ b/cli/ops/testing.rs @@ -44,7 +44,6 @@ pub fn init( state.put(sender.clone()); state.put(fail_fast_tracker.clone()); state.put(filter.clone()); - Ok(()) }) .build() } diff --git a/cli/tsc/mod.rs b/cli/tsc/mod.rs index bdd11fd52b..188bc05333 100644 --- a/cli/tsc/mod.rs +++ b/cli/tsc/mod.rs @@ -872,7 +872,6 @@ pub fn exec(request: Request) -> Result { root_map.clone(), remapped_specifiers.clone(), )); - Ok(()) }) .build()], ..Default::default() diff --git a/core/examples/schedule_task.rs b/core/examples/schedule_task.rs index 56c8f6dd66..42d00022d3 100644 --- a/core/examples/schedule_task.rs +++ b/core/examples/schedule_task.rs @@ -34,8 +34,6 @@ fn main() { let (tx, rx) = mpsc::unbounded::(); state.put(tx); state.put(rx); - - Ok(()) }) .build(); diff --git a/core/extensions.rs b/core/extensions.rs index ead1fa3546..beddce4c77 100644 --- a/core/extensions.rs +++ b/core/extensions.rs @@ -42,7 +42,7 @@ pub struct ExtensionFileSource { } pub type OpFnRef = v8::FunctionCallback; pub type OpMiddlewareFn = dyn Fn(OpDecl) -> OpDecl; -pub type OpStateFn = dyn Fn(&mut OpState) -> Result<(), Error>; +pub type OpStateFn = dyn Fn(&mut OpState); pub type OpEventLoopFn = dyn Fn(Rc>, &mut Context) -> bool; pub struct OpDecl { @@ -147,10 +147,9 @@ impl Extension { } /// Allows setting up the initial op-state of an isolate at startup. - pub fn init_state(&self, state: &mut OpState) -> Result<(), Error> { - match &self.opstate_fn { - Some(ofn) => ofn(state), - None => Ok(()), + pub fn init_state(&self, state: &mut OpState) { + if let Some(op_fn) = &self.opstate_fn { + op_fn(state); } } @@ -243,7 +242,7 @@ impl ExtensionBuilder { pub fn state(&mut self, opstate_fn: F) -> &mut Self where - F: Fn(&mut OpState) -> Result<(), Error> + 'static, + F: Fn(&mut OpState) + 'static, { self.state = Some(Box::new(opstate_fn)); self diff --git a/core/runtime.rs b/core/runtime.rs index 1d74c9ede0..92e23873a6 100644 --- a/core/runtime.rs +++ b/core/runtime.rs @@ -939,7 +939,7 @@ impl JsRuntime { // Setup state for e in extensions.iter_mut() { // ops are already registered during in bindings::initialize_context(); - e.init_state(&mut op_state.borrow_mut())?; + e.init_state(&mut op_state.borrow_mut()); // Setup event-loop middleware if let Some(middleware) = e.init_event_loop_middleware() { @@ -957,7 +957,7 @@ impl JsRuntime { // Setup state for e in extensions.iter_mut() { // ops are already registered during in bindings::initialize_context(); - e.init_state(&mut op_state.borrow_mut())?; + e.init_state(&mut op_state.borrow_mut()); // Setup event-loop middleware if let Some(middleware) = e.init_event_loop_middleware() { @@ -2887,7 +2887,6 @@ pub mod tests { mode, dispatch_count: dispatch_count2.clone(), }); - Ok(()) }) .build(); let mut runtime = JsRuntime::new(RuntimeOptions { @@ -4045,7 +4044,6 @@ assertEquals(1, notify_return_value); .ops(vec![op_async_borrow::decl()]) .state(|state| { state.put(InnerState(42)); - Ok(()) }) .build(); diff --git a/ext/broadcast_channel/lib.rs b/ext/broadcast_channel/lib.rs index 85994bf979..cb8d318908 100644 --- a/ext/broadcast_channel/lib.rs +++ b/ext/broadcast_channel/lib.rs @@ -122,7 +122,6 @@ pub fn init( .state(move |state| { state.put(bc.clone()); state.put(Unstable(unstable)); - Ok(()) }) .build() } diff --git a/ext/cache/lib.rs b/ext/cache/lib.rs index b0efbca552..463ade94f4 100644 --- a/ext/cache/lib.rs +++ b/ext/cache/lib.rs @@ -40,7 +40,6 @@ pub fn init( if let Some(create_cache) = maybe_create_cache.clone() { state.put(create_cache); } - Ok(()) }) .build() } diff --git a/ext/crypto/lib.rs b/ext/crypto/lib.rs index 9d6017f39f..6f456c9ee5 100644 --- a/ext/crypto/lib.rs +++ b/ext/crypto/lib.rs @@ -111,7 +111,6 @@ pub fn init(maybe_seed: Option) -> Extension { if let Some(seed) = maybe_seed { state.put(StdRng::seed_from_u64(seed)); } - Ok(()) }) .build() } diff --git a/ext/fetch/lib.rs b/ext/fetch/lib.rs index 766dabb620..f2a962539c 100644 --- a/ext/fetch/lib.rs +++ b/ext/fetch/lib.rs @@ -124,7 +124,6 @@ where ) .unwrap() }); - Ok(()) }) .build() } diff --git a/ext/ffi/lib.rs b/ext/ffi/lib.rs index 38a5a7eb2c..e9ce2ec2ed 100644 --- a/ext/ffi/lib.rs +++ b/ext/ffi/lib.rs @@ -151,8 +151,6 @@ pub fn init(unstable: bool) -> Extension { async_work_receiver, async_work_sender, }); - - Ok(()) }) .build() } diff --git a/ext/flash/lib.rs b/ext/flash/lib.rs index 4ae064173e..b50b7bb5c7 100644 --- a/ext/flash/lib.rs +++ b/ext/flash/lib.rs @@ -1567,7 +1567,6 @@ pub fn init(unstable: bool) -> Extension { join_handles: HashMap::default(), servers: HashMap::default(), }); - Ok(()) }) .build() } diff --git a/ext/fs/lib.rs b/ext/fs/lib.rs index 7551408a94..4b177a2bfb 100644 --- a/ext/fs/lib.rs +++ b/ext/fs/lib.rs @@ -122,7 +122,6 @@ pub fn init(unstable: bool) -> Extension { .esm(include_js_files!("30_fs.js",)) .state(move |state| { state.put(UnstableChecker { unstable }); - Ok(()) }) .ops(vec![ op_open_sync::decl::

(), diff --git a/ext/io/lib.rs b/ext/io/lib.rs index f8dde139f3..84d9d34a6b 100644 --- a/ext/io/lib.rs +++ b/ext/io/lib.rs @@ -132,7 +132,6 @@ pub fn init(stdio: Stdio) -> Extension { "stderr", )); assert_eq!(rid, 2, "stderr must have ResourceId 2"); - Ok(()) }) .build() } diff --git a/ext/napi/lib.rs b/ext/napi/lib.rs index 628ecd5a25..471ebfeabc 100644 --- a/ext/napi/lib.rs +++ b/ext/napi/lib.rs @@ -578,7 +578,6 @@ pub fn init() -> Extension { env_cleanup_hooks: Rc::new(RefCell::new(vec![])), tsfn_ref_counters: Arc::new(Mutex::new(vec![])), }); - Ok(()) }) .build() } diff --git a/ext/net/lib.rs b/ext/net/lib.rs index 0536f32334..5dc7823bcd 100644 --- a/ext/net/lib.rs +++ b/ext/net/lib.rs @@ -96,7 +96,6 @@ pub fn init( state.put(UnsafelyIgnoreCertificateErrors( unsafely_ignore_certificate_errors.clone(), )); - Ok(()) }) .build() } diff --git a/ext/net/ops.rs b/ext/net/ops.rs index 737a46d8c8..8ac08119ae 100644 --- a/ext/net/ops.rs +++ b/ext/net/ops.rs @@ -910,7 +910,6 @@ mod tests { .state(move |state| { state.put(TestPermission {}); state.put(UnstableChecker { unstable: true }); - Ok(()) }) .build(); diff --git a/ext/node/lib.rs b/ext/node/lib.rs index e1134bd035..411151f2fb 100644 --- a/ext/node/lib.rs +++ b/ext/node/lib.rs @@ -383,7 +383,6 @@ pub fn init( if let Some(npm_resolver) = maybe_npm_resolver.clone() { state.put(npm_resolver); } - Ok(()) }) .build() } diff --git a/ext/web/benches/encoding.rs b/ext/web/benches/encoding.rs index e9ef3a563e..ffea2617ee 100644 --- a/ext/web/benches/encoding.rs +++ b/ext/web/benches/encoding.rs @@ -40,7 +40,6 @@ fn setup() -> Vec { }]) .state(|state| { state.put(Permissions {}); - Ok(()) }) .build(), ] diff --git a/ext/web/benches/timers_ops.rs b/ext/web/benches/timers_ops.rs index f177f7d83d..8c700266ad 100644 --- a/ext/web/benches/timers_ops.rs +++ b/ext/web/benches/timers_ops.rs @@ -38,7 +38,6 @@ fn setup() -> Vec { ]) .state(|state| { state.put(Permissions{}); - Ok(()) }) .build() ] diff --git a/ext/web/lib.rs b/ext/web/lib.rs index 76095810ef..1af4cc29d7 100644 --- a/ext/web/lib.rs +++ b/ext/web/lib.rs @@ -121,7 +121,6 @@ pub fn init( state.put(Location(location)); } state.put(StartTime::now()); - Ok(()) }) .build() } diff --git a/ext/webgpu/lib.rs b/ext/webgpu/lib.rs index 68069c133d..8e13a80501 100644 --- a/ext/webgpu/lib.rs +++ b/ext/webgpu/lib.rs @@ -127,7 +127,6 @@ pub fn init(unstable: bool) -> Extension { // let unstable_checker = state.borrow::(); // let unstable = unstable_checker.unstable; state.put(Unstable(unstable)); - Ok(()) }) .build() } diff --git a/ext/webgpu/surface.rs b/ext/webgpu/surface.rs index 8304427b9b..d20d06bb15 100644 --- a/ext/webgpu/surface.rs +++ b/ext/webgpu/surface.rs @@ -30,7 +30,6 @@ pub fn init_surface(unstable: bool) -> Extension { // let unstable_checker = state.borrow::(); // let unstable = unstable_checker.unstable; state.put(super::Unstable(unstable)); - Ok(()) }) .build() } diff --git a/ext/websocket/lib.rs b/ext/websocket/lib.rs index 0ae2ada63f..4537c45e58 100644 --- a/ext/websocket/lib.rs +++ b/ext/websocket/lib.rs @@ -521,7 +521,6 @@ pub fn init( unsafely_ignore_certificate_errors.clone(), )); state.put::(WsRootStore(root_cert_store.clone())); - Ok(()) }) .build() } diff --git a/ext/webstorage/lib.rs b/ext/webstorage/lib.rs index 5e534a122d..98f1246d20 100644 --- a/ext/webstorage/lib.rs +++ b/ext/webstorage/lib.rs @@ -38,7 +38,6 @@ pub fn init(origin_storage_dir: Option) -> Extension { if let Some(origin_storage_dir) = &origin_storage_dir { state.put(OriginStorageDir(origin_storage_dir.clone())); } - Ok(()) }) .build() } diff --git a/runtime/ops/os/mod.rs b/runtime/ops/os/mod.rs index ac8ec541d0..83ad8164fe 100644 --- a/runtime/ops/os/mod.rs +++ b/runtime/ops/os/mod.rs @@ -45,7 +45,6 @@ pub fn init(exit_code: ExitCode) -> Extension { init_ops(&mut builder) .state(move |state| { state.put::(exit_code.clone()); - Ok(()) }) .build() } diff --git a/runtime/ops/runtime.rs b/runtime/ops/runtime.rs index 22a481e776..5ce9966e02 100644 --- a/runtime/ops/runtime.rs +++ b/runtime/ops/runtime.rs @@ -12,7 +12,6 @@ pub fn init(main_module: ModuleSpecifier) -> Extension { .ops(vec![op_main_module::decl()]) .state(move |state| { state.put::(main_module.clone()); - Ok(()) }) .build() } diff --git a/runtime/ops/worker_host.rs b/runtime/ops/worker_host.rs index 71009be8fc..6007a3260b 100644 --- a/runtime/ops/worker_host.rs +++ b/runtime/ops/worker_host.rs @@ -111,8 +111,6 @@ pub fn init( let format_js_error_fn_holder = FormatJsErrorFnHolder(format_js_error_fn.clone()); state.put::(format_js_error_fn_holder); - - Ok(()) }) .ops(vec![ op_create_worker::decl(), diff --git a/runtime/web_worker.rs b/runtime/web_worker.rs index cce69fabbe..f07ecd27f8 100644 --- a/runtime/web_worker.rs +++ b/runtime/web_worker.rs @@ -376,7 +376,6 @@ impl WebWorker { state.put::(permissions.clone()); state.put(ops::UnstableChecker { unstable }); state.put(ops::TestingFeaturesEnabled(enable_testing_features)); - Ok(()) }) .build(); let create_cache = options.cache_storage_dir.map(|storage_dir| { diff --git a/runtime/worker.rs b/runtime/worker.rs index 1a6b6f2fcb..c10f9f36eb 100644 --- a/runtime/worker.rs +++ b/runtime/worker.rs @@ -206,7 +206,6 @@ impl MainWorker { state.put::(permissions.clone()); state.put(ops::UnstableChecker { unstable }); state.put(ops::TestingFeaturesEnabled(enable_testing_features)); - Ok(()) }) .build(); let exit_code = ExitCode(Arc::new(AtomicI32::new(0)));