summaryrefslogtreecommitdiff
path: root/core
diff options
context:
space:
mode:
Diffstat (limited to 'core')
-rw-r--r--core/examples/hello_world.rs39
-rw-r--r--core/examples/http_bench_json_ops.rs16
-rw-r--r--core/modules.rs10
-rw-r--r--core/ops_json.rs13
-rw-r--r--core/runtime.rs31
5 files changed, 71 insertions, 38 deletions
diff --git a/core/examples/hello_world.rs b/core/examples/hello_world.rs
index fd6cabf2b..aa822917e 100644
--- a/core/examples/hello_world.rs
+++ b/core/examples/hello_world.rs
@@ -3,25 +3,34 @@
//! JavaScript.
use deno_core::op_sync;
+use deno_core::Extension;
use deno_core::JsRuntime;
+use deno_core::RuntimeOptions;
fn main() {
- // Initialize a runtime instance
- let mut runtime = JsRuntime::new(Default::default());
+ // Build a deno_core::Extension providing custom ops
+ let ext = Extension::builder()
+ .ops(vec![
+ // An op for summing an array of numbers
+ (
+ "op_sum",
+ // The op-layer automatically deserializes inputs
+ // and serializes the returned Result & value
+ op_sync(|_state, nums: Vec<f64>, _: ()| {
+ // Sum inputs
+ let sum = nums.iter().fold(0.0, |a, v| a + v);
+ // return as a Result<f64, AnyError>
+ Ok(sum)
+ }),
+ ),
+ ])
+ .build();
- // Register an op for summing a number array.
- runtime.register_op(
- "op_sum",
- // The op-layer automatically deserializes inputs
- // and serializes the returned Result & value
- op_sync(|_state, nums: Vec<f64>, _: ()| {
- // Sum inputs
- let sum = nums.iter().fold(0.0, |a, v| a + v);
- // return as a Result<f64, AnyError>
- Ok(sum)
- }),
- );
- runtime.sync_ops_cache();
+ // Initialize a runtime instance
+ let mut runtime = JsRuntime::new(RuntimeOptions {
+ extensions: vec![ext],
+ ..Default::default()
+ });
// Now we see how to invoke the op we just defined. The runtime automatically
// contains a Deno.core object with several functions for interacting with it.
diff --git a/core/examples/http_bench_json_ops.rs b/core/examples/http_bench_json_ops.rs
index 641483b0b..f1dfb1039 100644
--- a/core/examples/http_bench_json_ops.rs
+++ b/core/examples/http_bench_json_ops.rs
@@ -118,11 +118,17 @@ impl From<tokio::net::TcpStream> for TcpStream {
}
fn create_js_runtime() -> JsRuntime {
- let mut runtime = JsRuntime::new(Default::default());
- runtime.register_op("listen", deno_core::op_sync(op_listen));
- runtime.register_op("accept", deno_core::op_async(op_accept));
- runtime.sync_ops_cache();
- runtime
+ let ext = deno_core::Extension::builder()
+ .ops(vec![
+ ("listen", deno_core::op_sync(op_listen)),
+ ("accept", deno_core::op_async(op_accept)),
+ ])
+ .build();
+
+ JsRuntime::new(deno_core::RuntimeOptions {
+ extensions: vec![ext],
+ ..Default::default()
+ })
}
fn op_listen(state: &mut OpState, _: (), _: ()) -> Result<ResourceId, Error> {
diff --git a/core/modules.rs b/core/modules.rs
index 45330dffd..fc505c31e 100644
--- a/core/modules.rs
+++ b/core/modules.rs
@@ -1075,6 +1075,7 @@ mod tests {
use super::*;
use crate::ops::OpCall;
use crate::serialize_op_result;
+ use crate::Extension;
use crate::JsRuntime;
use crate::Op;
use crate::OpPayload;
@@ -1403,7 +1404,7 @@ import "/a.js";
let dispatch_count = Arc::new(AtomicUsize::new(0));
let dispatch_count_ = dispatch_count.clone();
- let dispatcher = move |state, payload: OpPayload| -> Op {
+ let op_test = move |state, payload: OpPayload| -> Op {
dispatch_count_.fetch_add(1, Ordering::Relaxed);
let (control, _): (u8, ()) = payload.deserialize().unwrap();
assert_eq!(control, 42);
@@ -1411,12 +1412,15 @@ import "/a.js";
Op::Async(OpCall::ready(resp))
};
+ let ext = Extension::builder()
+ .ops(vec![("op_test", Box::new(op_test))])
+ .build();
+
let mut runtime = JsRuntime::new(RuntimeOptions {
+ extensions: vec![ext],
module_loader: Some(loader),
..Default::default()
});
- runtime.register_op("op_test", dispatcher);
- runtime.sync_ops_cache();
runtime
.execute_script(
diff --git a/core/ops_json.rs b/core/ops_json.rs
index ad4aeeb47..b7e833129 100644
--- a/core/ops_json.rs
+++ b/core/ops_json.rs
@@ -123,8 +123,6 @@ mod tests {
#[tokio::test]
async fn op_async_stack_trace() {
- let mut runtime = crate::JsRuntime::new(Default::default());
-
async fn op_throw(
_state: Rc<RefCell<OpState>>,
msg: Option<String>,
@@ -134,8 +132,15 @@ mod tests {
Err(crate::error::generic_error("foo"))
}
- runtime.register_op("op_throw", op_async(op_throw));
- runtime.sync_ops_cache();
+ let ext = crate::Extension::builder()
+ .ops(vec![("op_throw", op_async(op_throw))])
+ .build();
+
+ let mut runtime = crate::JsRuntime::new(crate::RuntimeOptions {
+ extensions: vec![ext],
+ ..Default::default()
+ });
+
runtime
.execute_script(
"<init>",
diff --git a/core/runtime.rs b/core/runtime.rs
index 643a4198d..d1b7db14c 100644
--- a/core/runtime.rs
+++ b/core/runtime.rs
@@ -1651,6 +1651,7 @@ pub mod tests {
futures::executor::block_on(lazy(move |cx| f(cx)));
}
+ #[derive(Copy, Clone)]
enum Mode {
Async,
AsyncZeroCopy(bool),
@@ -1661,7 +1662,7 @@ pub mod tests {
dispatch_count: Arc<AtomicUsize>,
}
- fn dispatch(rc_op_state: Rc<RefCell<OpState>>, payload: OpPayload) -> Op {
+ fn op_test(rc_op_state: Rc<RefCell<OpState>>, payload: OpPayload) -> Op {
let rc_op_state2 = rc_op_state.clone();
let op_state_ = rc_op_state2.borrow();
let test_state = op_state_.borrow::<TestState>();
@@ -1687,16 +1688,22 @@ pub mod tests {
fn setup(mode: Mode) -> (JsRuntime, Arc<AtomicUsize>) {
let dispatch_count = Arc::new(AtomicUsize::new(0));
- let mut runtime = JsRuntime::new(Default::default());
- let op_state = runtime.op_state();
- op_state.borrow_mut().put(TestState {
- mode,
- dispatch_count: dispatch_count.clone(),
+ let dispatch_count2 = dispatch_count.clone();
+ let ext = Extension::builder()
+ .ops(vec![("op_test", Box::new(op_test))])
+ .state(move |state| {
+ state.put(TestState {
+ mode,
+ dispatch_count: dispatch_count2.clone(),
+ });
+ Ok(())
+ })
+ .build();
+ let mut runtime = JsRuntime::new(RuntimeOptions {
+ extensions: vec![ext],
+ ..Default::default()
});
- runtime.register_op("op_test", dispatch);
- runtime.sync_ops_cache();
-
runtime
.execute_script(
"setup.js",
@@ -2029,12 +2036,14 @@ pub mod tests {
}
run_in_task(|cx| {
+ let ext = Extension::builder()
+ .ops(vec![("op_err", op_sync(op_err))])
+ .build();
let mut runtime = JsRuntime::new(RuntimeOptions {
+ extensions: vec![ext],
get_error_class_fn: Some(&get_error_class_name),
..Default::default()
});
- runtime.register_op("op_err", op_sync(op_err));
- runtime.sync_ops_cache();
runtime
.execute_script(
"error_builder_test.js",