diff options
| author | Aaron O'Mullan <aaron.omullan@gmail.com> | 2021-04-28 18:41:50 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-04-28 18:41:50 +0200 |
| commit | 0260b488fbba9a43c64641428d3603b8761067a4 (patch) | |
| tree | 66ce487f9241a3b91942dd048c7e43cb192bf9e8 /op_crates/url | |
| parent | b28f9445aae85dbf86033300cfcb55e404529a23 (diff) | |
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.
Diffstat (limited to 'op_crates/url')
| -rw-r--r-- | op_crates/url/benches/url_ops.rs | 22 | ||||
| -rw-r--r-- | op_crates/url/lib.rs | 33 |
2 files changed, 33 insertions, 22 deletions
diff --git a/op_crates/url/benches/url_ops.rs b/op_crates/url/benches/url_ops.rs index 0f584de90..8d5498540 100644 --- a/op_crates/url/benches/url_ops.rs +++ b/op_crates/url/benches/url_ops.rs @@ -1,21 +1,14 @@ use bencher::{benchmark_group, benchmark_main, Bencher}; -use deno_core::op_sync; use deno_core::v8; use deno_core::JsRuntime; +use deno_core::RuntimeOptions; fn create_js_runtime() -> JsRuntime { - let mut runtime = JsRuntime::new(Default::default()); - runtime.register_op("op_url_parse", op_sync(deno_url::op_url_parse)); - runtime.register_op( - "op_url_parse_search_params", - op_sync(deno_url::op_url_parse_search_params), - ); - runtime.register_op( - "op_url_stringify_search_params", - op_sync(deno_url::op_url_stringify_search_params), - ); - runtime.sync_ops_cache(); + let mut runtime = JsRuntime::new(RuntimeOptions { + extensions: vec![deno_url::init()], + ..Default::default() + }); runtime .execute( @@ -23,7 +16,10 @@ fn create_js_runtime() -> JsRuntime { "globalThis.__bootstrap = (globalThis.__bootstrap || {});", ) .unwrap(); - deno_url::init(&mut runtime); + + runtime.init_extension_js().unwrap(); + runtime.init_extension_ops().unwrap(); + runtime .execute("setup", "const { URL } = globalThis.__bootstrap.url;") .unwrap(); diff --git a/op_crates/url/lib.rs b/op_crates/url/lib.rs index 04663e411..49e34c47d 100644 --- a/op_crates/url/lib.rs +++ b/op_crates/url/lib.rs @@ -4,16 +4,39 @@ use deno_core::error::generic_error; use deno_core::error::type_error; use deno_core::error::uri_error; use deno_core::error::AnyError; +use deno_core::include_js_files; +use deno_core::op_sync; use deno_core::url::form_urlencoded; use deno_core::url::quirks; use deno_core::url::Url; -use deno_core::JsRuntime; +use deno_core::Extension; use deno_core::ZeroCopyBuf; use serde::Deserialize; use serde::Serialize; use std::panic::catch_unwind; use std::path::PathBuf; +pub fn init() -> Extension { + Extension::with_ops( + include_js_files!( + prefix "deno:op_crates/url", + "00_url.js", + ), + vec![ + ("op_url_parse", op_sync(op_url_parse)), + ( + "op_url_parse_search_params", + op_sync(op_url_parse_search_params), + ), + ( + "op_url_stringify_search_params", + op_sync(op_url_stringify_search_params), + ), + ], + None, + ) +} + #[derive(Deserialize)] #[serde(rename_all = "camelCase")] pub struct UrlParseArgs { @@ -146,14 +169,6 @@ pub fn op_url_stringify_search_params( Ok(search) } -/// Load and execute the javascript code. -pub fn init(isolate: &mut JsRuntime) { - let files = vec![("deno:op_crates/url/00_url.js", include_str!("00_url.js"))]; - for (url, source_code) in files { - isolate.execute(url, source_code).unwrap(); - } -} - pub fn get_declaration() -> PathBuf { PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("lib.deno_url.d.ts") } |
