Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core: introduce extensions #9800

Merged
merged 65 commits into from
Apr 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
65 commits
Select commit Hold shift + click to select a range
f9bf613
core: foundations of JsRuntimeModules
AaronO Mar 15, 2021
0b6e240
fmt
AaronO Mar 16, 2021
2fc18da
core: add include_js_files!() helper macro
AaronO Mar 16, 2021
adb5a1c
Move ops init code to respective op_crates
AaronO Mar 16, 2021
bbfbf25
Remove op_crate ops from runtime/ops/mod.rs
AaronO Mar 16, 2021
613fb8c
core: add declare_ops! macro
AaronO Mar 16, 2021
2366361
webgpu: simplify op declaration using declare_ops!
AaronO Mar 16, 2021
9c9df67
fmt
AaronO Mar 16, 2021
1ce3da4
test declare_ops! on small url op_crate
AaronO Mar 16, 2021
b336228
core/runtime_modules: add BasicModule
AaronO Mar 16, 2021
7555755
fmt
AaronO Mar 16, 2021
c541c59
core: add placeholder JsRuntime.init_mod_js() and .init_mod_ops()
AaronO Mar 16, 2021
2941be1
runtime: init JsRuntime with JsRuntimeModules
AaronO Mar 16, 2021
f7d1aaf
core: init state in JsRuntime.init_mod_ops()
AaronO Mar 16, 2021
b26f14b
runtime/web_worker: align with master's removal of deno namespace for…
AaronO Mar 17, 2021
49c8b88
Merge branch 'main' into core/js-runtime-modules
AaronO Mar 17, 2021
aae9ebc
fix typo
AaronO Mar 17, 2021
1192a85
typo...
AaronO Mar 17, 2021
c32c6e5
core: simplify JsRuntime.init_mod_js()
AaronO Mar 18, 2021
e2bac14
core: improve declare_ops! and include_js_files! macros
AaronO Mar 18, 2021
37a6c3f
Add unit test to check for op-metrics on op_crates
AaronO Mar 18, 2021
dd61b13
Convert metrics to a runtime extension
AaronO Mar 18, 2021
dcdd86c
Implement OpMiddleware and extension builder
AaronO Mar 18, 2021
dca4175
core: implement all trait methods for MultiModule
AaronO Mar 18, 2021
4934f9c
fmt
AaronO Mar 18, 2021
b7e3911
Use defaults user-agent and ca_data in op_create_http_client
AaronO Mar 18, 2021
d93c971
Revert "Use defaults user-agent and ca_data in op_create_http_client"
AaronO Mar 18, 2021
59d2a13
fmt
AaronO Mar 18, 2021
92736d6
Merge branch 'main' into core/js-runtime-modules
AaronO Mar 18, 2021
2d6d132
fmt
AaronO Mar 18, 2021
d9c3613
Merge branch 'main' into core/js-runtime-modules
AaronO Mar 18, 2021
63274b2
Merge branch 'main' into core/js-runtime-modules
AaronO Mar 18, 2021
7963691
Remove duplicate HttpClientDefaults
AaronO Mar 18, 2021
0efc263
Merge branch 'main' into core/js-runtime-modules
ry Mar 23, 2021
2120156
Exclude MultiModule for now
ry Mar 23, 2021
ce425ae
Rename JsRuntimeModule to Extension. Remove BasicModuleBuilder
ry Mar 23, 2021
60147a9
Remove example
ry Mar 23, 2021
ee50474
Downgrade Extension from a trait to a single concrete type
AaronO Mar 24, 2021
16bf35f
Remove declare_ops macro
ry Mar 24, 2021
afea31c
clean up
ry Mar 24, 2021
1a10dbb
fix
ry Mar 24, 2021
d2a9df8
Merge branch 'main' into core/js-runtime-modules
AaronO Apr 21, 2021
c3d5ebf
fmt
AaronO Apr 21, 2021
ab4129e
runtime/ops: delete files of op_crate ops
AaronO Apr 21, 2021
0fe2085
runtime/ops/runtime: drop duplicate op_metrics reg
AaronO Apr 21, 2021
7b4503a
runtime/workers: remove duplicate inits of some op_crates
AaronO Apr 21, 2021
8723251
core/runtime: remove duplicate OpState import
AaronO Apr 21, 2021
d95fffd
runtime/ops/mod: remove already converted op_crate mods
AaronO Apr 21, 2021
e5cfd88
lint
AaronO Apr 21, 2021
0bf6948
Fix url_ops bench
AaronO Apr 21, 2021
0d84bff
fmt
AaronO Apr 21, 2021
64e1380
Convert file op_crate to an Extension
AaronO Apr 21, 2021
f3afb7d
Convert timers op_crate to an Extension
AaronO Apr 21, 2021
badee09
typo
AaronO Apr 21, 2021
14be760
lint
AaronO Apr 21, 2021
6fd4098
fix metricsForOpCrates
AaronO Apr 21, 2021
761d08d
Merge branch 'main' into core/js-runtime-modules
AaronO Apr 22, 2021
2ae11fd
core/runtime: remove JsRuntime::execute_static() for now
AaronO Apr 22, 2021
5bb7857
Merge branch 'main' into core/js-runtime-modules
AaronO Apr 26, 2021
e18edd4
core/runtime: sync ops-cache after initing extension ops
AaronO Apr 26, 2021
05d830d
minor doc typo
AaronO Apr 26, 2021
3236445
reset CI
AaronO Apr 26, 2021
c8a11cb
core/extensions: panic when an extensions ops are inited twice
AaronO Apr 26, 2021
f051463
core: remove RcOpRegistrar type alias
AaronO Apr 26, 2021
2f129d0
cleanup: kill OpRegistrar trait in favour of 1st class op middleware
AaronO Apr 28, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions cli/tests/unit/metrics_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,12 @@ unitTest(
assert(metrics.opsDispatchedAsync === metrics.opsCompletedAsync);
},
);

// Test that ops from op_crates have metrics (via OpMiddleware)
unitTest(function metricsForOpCrates(): void {
const _ = new URL("https://deno.land");

const m1 = Deno.metrics().ops["op_url_parse"];
assert(m1.opsDispatched > 0);
assert(m1.opsCompleted > 0);
});
105 changes: 105 additions & 0 deletions core/extensions.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
use crate::error::AnyError;
use crate::{OpFn, OpState};

pub type SourcePair = (&'static str, &'static str);
pub type OpPair = (&'static str, Box<OpFn>);
pub type OpMiddlewareFn = dyn Fn(&'static str, Box<OpFn>) -> Box<OpFn>;
pub type OpStateFn = dyn Fn(&mut OpState) -> Result<(), AnyError>;

#[derive(Default)]
pub struct Extension {
js_files: Option<Vec<SourcePair>>,
ops: Option<Vec<OpPair>>,
opstate_fn: Option<Box<OpStateFn>>,
middleware_fn: Option<Box<OpMiddlewareFn>>,
initialized: bool,
}

impl Extension {
pub fn new(
js_files: Option<Vec<SourcePair>>,
ops: Option<Vec<OpPair>>,
AaronO marked this conversation as resolved.
Show resolved Hide resolved
opstate_fn: Option<Box<OpStateFn>>,
middleware_fn: Option<Box<OpMiddlewareFn>>,
) -> Self {
AaronO marked this conversation as resolved.
Show resolved Hide resolved
Self {
js_files,
ops,
opstate_fn,
middleware_fn,
initialized: false,
}
}

pub fn pure_js(js_files: Vec<SourcePair>) -> Self {
Self::new(Some(js_files), None, None, None)
}

pub fn with_ops(
js_files: Vec<SourcePair>,
ops: Vec<OpPair>,
opstate_fn: Option<Box<OpStateFn>>,
) -> Self {
Self::new(Some(js_files), Some(ops), opstate_fn, None)
}
}

// Note: this used to be a trait, but we "downgraded" it to a single concrete type
// for the initial iteration, it will likely become a trait in the future
impl Extension {
/// returns JS source code to be loaded into the isolate (either at snapshotting,
/// or at startup). as a vector of a tuple of the file name, and the source code.
pub(crate) fn init_js(&self) -> Vec<SourcePair> {
AaronO marked this conversation as resolved.
Show resolved Hide resolved
match &self.js_files {
Some(files) => files.clone(),
None => vec![],
}
}

/// Called at JsRuntime startup to initialize ops in the isolate.
pub(crate) fn init_ops(&mut self) -> Option<Vec<OpPair>> {
// TODO(@AaronO): maybe make op registration idempotent
if self.initialized {
panic!("init_ops called twice: not idempotent or correct");
}
self.initialized = true;

self.ops.take()
}

/// Allows setting up the initial op-state of an isolate at startup.
pub(crate) fn init_state(&self, state: &mut OpState) -> Result<(), AnyError> {
match &self.opstate_fn {
Some(ofn) => ofn(state),
None => Ok(()),
}
}

/// init_middleware lets us middleware op registrations, it's called before init_ops
pub(crate) fn init_middleware(&mut self) -> Option<Box<OpMiddlewareFn>> {
self.middleware_fn.take()
}
}

/// Helps embed JS files in an extension. Returns Vec<(&'static str, &'static str)>
/// representing the filename and source code.
///
/// Example:
/// ```ignore
/// include_js_files!(
/// prefix "deno:op_crates/hello",
/// "01_hello.js",
/// "02_goodbye.js",
/// )
/// ```
#[macro_export]
macro_rules! include_js_files {
(prefix $prefix:literal, $($file:literal,)+) => {
vec![
$((
concat!($prefix, "/", $file),
include_str!($file),
),)+
]
};
}
4 changes: 4 additions & 0 deletions core/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ mod async_cancel;
mod async_cell;
mod bindings;
pub mod error;
mod extensions;
mod flags;
mod gotham_state;
mod module_specifier;
Expand Down Expand Up @@ -75,6 +76,9 @@ pub use crate::runtime::JsErrorCreateFn;
pub use crate::runtime::JsRuntime;
pub use crate::runtime::RuntimeOptions;
pub use crate::runtime::Snapshot;
// pub use crate::runtime_modules::include_js_files!;
pub use crate::extensions::Extension;
pub use crate::extensions::OpMiddlewareFn;
pub use crate::zero_copy_buf::ZeroCopyBuf;

pub fn v8_version() -> &'static str {
Expand Down
59 changes: 59 additions & 0 deletions core/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ use crate::modules::NoopModuleLoader;
use crate::modules::PrepareLoadFuture;
use crate::modules::RecursiveModuleLoad;
use crate::ops::*;
use crate::Extension;
use crate::OpMiddlewareFn;
use crate::OpPayload;
use crate::OpResponse;
use crate::OpState;
Expand Down Expand Up @@ -83,6 +85,7 @@ pub struct JsRuntime {
snapshot_creator: Option<v8::SnapshotCreator>,
has_snapshotted: bool,
allocations: IsolateAllocations,
extensions: Vec<Extension>,
}

struct DynImportModEvaluate {
Expand Down Expand Up @@ -186,6 +189,10 @@ pub struct RuntimeOptions {
/// executed tries to load modules.
pub module_loader: Option<Rc<dyn ModuleLoader>>,

/// JsRuntime extensions, not to be confused with ES modules
/// these are sets of ops and other JS code to be initialized.
pub extensions: Vec<Extension>,

/// V8 snapshot that should be loaded on startup.
///
/// Currently can't be used with `will_snapshot`.
Expand Down Expand Up @@ -300,6 +307,7 @@ impl JsRuntime {
snapshot_creator: maybe_snapshot_creator,
has_snapshotted: false,
allocations: IsolateAllocations::default(),
extensions: options.extensions,
};

if !has_startup_snapshot {
Expand Down Expand Up @@ -349,6 +357,57 @@ impl JsRuntime {
.unwrap();
}

/// Initializes JS of provided Extensions
// NOTE: this will probably change when streamlining snapshot flow
AaronO marked this conversation as resolved.
Show resolved Hide resolved
pub fn init_extension_js(&mut self) -> Result<(), AnyError> {
// Take extensions to avoid double-borrow
let mut extensions: Vec<Extension> = std::mem::take(&mut self.extensions);
for m in extensions.iter_mut() {
let js_files = m.init_js();
for (filename, source) in js_files {
// TODO(@AaronO): use JsRuntime::execute_static() here to move src off heap
self.execute(filename, source)?;
}
}
// Restore extensions
self.extensions = extensions;

Ok(())
}

/// Initializes ops of provided Extensions
// NOTE: this will probably change when streamlining snapshot flow
AaronO marked this conversation as resolved.
Show resolved Hide resolved
pub fn init_extension_ops(&mut self) -> Result<(), AnyError> {
AaronO marked this conversation as resolved.
Show resolved Hide resolved
let op_state = self.op_state();
// Take extensions to avoid double-borrow
let mut extensions: Vec<Extension> = std::mem::take(&mut self.extensions);

// Middleware
let middleware: Vec<Box<OpMiddlewareFn>> = extensions
.iter_mut()
.filter_map(|e| e.init_middleware())
.collect();
// macroware wraps an opfn in all the middleware
let macroware =
move |name, opfn| middleware.iter().fold(opfn, |opfn, m| m(name, opfn));

// Register ops
for e in extensions.iter_mut() {
e.init_state(&mut op_state.borrow_mut())?;
// Register each op after middlewaring it
let mut ops = e.init_ops().unwrap_or_default();
for (name, opfn) in ops {
self.register_op(name, macroware(name, opfn));
}
}
// Sync ops cache
self.sync_ops_cache();
// Restore extensions
self.extensions = extensions;

Ok(())
}

/// Grabs a reference to core.js' handleAsyncMsgFromRust
fn init_recv_cb(&mut self) {
let context = self.global_context();
Expand Down
24 changes: 8 additions & 16 deletions op_crates/console/lib.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,15 @@
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.

use deno_core::JsRuntime;
use deno_core::include_js_files;
use deno_core::Extension;
use std::path::PathBuf;

/// Load and execute the javascript code.
pub fn init(isolate: &mut JsRuntime) {
let files = vec![
(
"deno:op_crates/console/01_colors.js",
include_str!("01_colors.js"),
),
(
"deno:op_crates/console/02_console.js",
include_str!("02_console.js"),
),
];
for (url, source_code) in files {
isolate.execute(url, source_code).unwrap();
}
pub fn init() -> Extension {
Extension::pure_js(include_js_files!(
prefix "deno:op_crates/console",
"01_colors.js",
"02_console.js",
))
}

pub fn get_declaration() -> PathBuf {
Expand Down
31 changes: 21 additions & 10 deletions op_crates/crypto/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,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<u64>) -> 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(
Expand Down
Loading