From 0260b488fbba9a43c64641428d3603b8761067a4 Mon Sep 17 00:00:00 2001 From: Aaron O'Mullan Date: Wed, 28 Apr 2021 18:41:50 +0200 Subject: core: introduce extensions (#9800) Extensions allow declarative extensions to "JsRuntime" (ops, state, JS or middleware). This allows for: - `op_crates` to be plug-and-play & self-contained, reducing complexity leaked to consumers - op middleware (like metrics_op) to be opt-in and for new middleware (unstable, tracing,...) - `MainWorker` and `WebWorker` to be composable, allowing users to extend workers with their ops whilst benefiting from the other infrastructure (inspector, etc...) In short extensions improve deno's modularity, reducing complexity and leaky abstractions for embedders and the internal codebase. --- op_crates/crypto/lib.rs | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) (limited to 'op_crates/crypto/lib.rs') diff --git a/op_crates/crypto/lib.rs b/op_crates/crypto/lib.rs index 8b4d34a7c..d5b0a30bc 100644 --- a/op_crates/crypto/lib.rs +++ b/op_crates/crypto/lib.rs @@ -2,25 +2,36 @@ use deno_core::error::null_opbuf; use deno_core::error::AnyError; -use deno_core::JsRuntime; +use deno_core::include_js_files; +use deno_core::op_sync; +use deno_core::Extension; use deno_core::OpState; use deno_core::ZeroCopyBuf; use rand::rngs::StdRng; use rand::thread_rng; use rand::Rng; +use rand::SeedableRng; use std::path::PathBuf; pub use rand; // Re-export rand -/// Execute this crates' JS source files. -pub fn init(isolate: &mut JsRuntime) { - let files = vec![( - "deno:op_crates/crypto/01_crypto.js", - include_str!("01_crypto.js"), - )]; - for (url, source_code) in files { - isolate.execute(url, source_code).unwrap(); - } +pub fn init(maybe_seed: Option) -> Extension { + Extension::with_ops( + include_js_files!( + prefix "deno:op_crates/crypto", + "01_crypto.js", + ), + vec![( + "op_crypto_get_random_values", + op_sync(op_crypto_get_random_values), + )], + Some(Box::new(move |state| { + if let Some(seed) = maybe_seed { + state.put(StdRng::seed_from_u64(seed)); + } + Ok(()) + })), + ) } pub fn op_crypto_get_random_values( -- cgit v1.2.3