summaryrefslogtreecommitdiff
path: root/core/examples
diff options
context:
space:
mode:
authorDivy Srivastava <dj.srivastava23@gmail.com>2022-03-14 23:14:15 +0530
committerGitHub <noreply@github.com>2022-03-14 18:44:15 +0100
commitb4e42953e1d243f2eda20e5be6b845d60b7bf688 (patch)
tree10b3bfff165f9c04f9174c7c399d44b9b724c3b3 /core/examples
parent4e3ed37037a2aa1edeac260dc3463a81d9cf9b88 (diff)
feat(core): codegen ops (#13861)
Co-authored-by: Aaron O'Mullan <aaron.omullan@gmail.com>
Diffstat (limited to 'core/examples')
-rw-r--r--core/examples/disable_ops.rs2
-rw-r--r--core/examples/hello_world.rs34
-rw-r--r--core/examples/http_bench_json_ops.js4
-rw-r--r--core/examples/http_bench_json_ops.rs13
-rw-r--r--core/examples/schedule_task.rs12
5 files changed, 42 insertions, 23 deletions
diff --git a/core/examples/disable_ops.rs b/core/examples/disable_ops.rs
index 0612845c8..9565d6e7f 100644
--- a/core/examples/disable_ops.rs
+++ b/core/examples/disable_ops.rs
@@ -9,7 +9,7 @@ use deno_core::RuntimeOptions;
fn main() {
let my_ext = Extension::builder()
.middleware(|name, opfn| match name {
- "op_print" => deno_core::void_op_sync(),
+ "op_print" => deno_core::void_op_sync::v8_cb(),
_ => opfn,
})
.build();
diff --git a/core/examples/hello_world.rs b/core/examples/hello_world.rs
index 42c9779f3..bfca5447c 100644
--- a/core/examples/hello_world.rs
+++ b/core/examples/hello_world.rs
@@ -2,27 +2,37 @@
//! This example shows you how to define ops in Rust and then call them from
//! JavaScript.
-use deno_core::op_sync;
+use deno_core::op;
use deno_core::Extension;
use deno_core::JsRuntime;
+use deno_core::OpState;
use deno_core::RuntimeOptions;
+// This is a hack to make the `#[op]` macro work with
+// deno_core examples.
+// You can remove this:
+use deno_core::*;
+
+#[op]
+fn op_sum(
+ _state: &mut OpState,
+ nums: Vec<f64>,
+ _: (),
+) -> Result<f64, deno_core::error::AnyError> {
+ // Sum inputs
+ let sum = nums.iter().fold(0.0, |a, v| a + v);
+ // return as a Result<f64, AnyError>
+ Ok(sum)
+}
+
fn main() {
// 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)
- }),
- ),
+ // The op-layer automatically deserializes inputs
+ // and serializes the returned Result & value
+ op_sum::decl(),
])
.build();
diff --git a/core/examples/http_bench_json_ops.js b/core/examples/http_bench_json_ops.js
index 22e1b468c..5b16776f4 100644
--- a/core/examples/http_bench_json_ops.js
+++ b/core/examples/http_bench_json_ops.js
@@ -11,12 +11,12 @@ const responseBuf = new Uint8Array(
/** Listens on 0.0.0.0:4500, returns rid. */
function listen() {
- return Deno.core.opSync("listen");
+ return Deno.core.opSync("op_listen");
}
/** Accepts a connection, returns rid. */
function accept(serverRid) {
- return Deno.core.opAsync("accept", serverRid);
+ return Deno.core.opAsync("op_accept", serverRid);
}
async function serve(rid) {
diff --git a/core/examples/http_bench_json_ops.rs b/core/examples/http_bench_json_ops.rs
index 9e60df4a4..3f608eeae 100644
--- a/core/examples/http_bench_json_ops.rs
+++ b/core/examples/http_bench_json_ops.rs
@@ -1,5 +1,6 @@
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
use deno_core::anyhow::Error;
+use deno_core::op;
use deno_core::AsyncRefCell;
use deno_core::AsyncResult;
use deno_core::CancelHandle;
@@ -17,6 +18,11 @@ use std::rc::Rc;
use tokio::io::AsyncReadExt;
use tokio::io::AsyncWriteExt;
+// This is a hack to make the `#[op]` macro work with
+// deno_core examples.
+// You can remove this:
+use deno_core::*;
+
struct Logger;
impl log::Log for Logger {
@@ -119,10 +125,7 @@ impl From<tokio::net::TcpStream> for TcpStream {
fn create_js_runtime() -> JsRuntime {
let ext = deno_core::Extension::builder()
- .ops(vec![
- ("listen", deno_core::op_sync(op_listen)),
- ("accept", deno_core::op_async(op_accept)),
- ])
+ .ops(vec![op_listen::decl(), op_accept::decl()])
.build();
JsRuntime::new(deno_core::RuntimeOptions {
@@ -131,6 +134,7 @@ fn create_js_runtime() -> JsRuntime {
})
}
+#[op]
fn op_listen(state: &mut OpState, _: (), _: ()) -> Result<ResourceId, Error> {
log::debug!("listen");
let addr = "127.0.0.1:4544".parse::<SocketAddr>().unwrap();
@@ -141,6 +145,7 @@ fn op_listen(state: &mut OpState, _: (), _: ()) -> Result<ResourceId, Error> {
Ok(rid)
}
+#[op]
async fn op_accept(
state: Rc<RefCell<OpState>>,
rid: ResourceId,
diff --git a/core/examples/schedule_task.rs b/core/examples/schedule_task.rs
index 2f4909b4f..3ada86417 100644
--- a/core/examples/schedule_task.rs
+++ b/core/examples/schedule_task.rs
@@ -1,6 +1,7 @@
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
use deno_core::anyhow::Error;
+use deno_core::op;
use deno_core::Extension;
use deno_core::JsRuntime;
use deno_core::OpState;
@@ -9,14 +10,16 @@ use futures::channel::mpsc;
use futures::stream::StreamExt;
use std::task::Poll;
+// This is a hack to make the `#[op]` macro work with
+// deno_core examples.
+// You can remove this:
+use deno_core::*;
+
type Task = Box<dyn FnOnce()>;
fn main() {
let my_ext = Extension::builder()
- .ops(vec![(
- "op_schedule_task",
- deno_core::op_sync(op_schedule_task),
- )])
+ .ops(vec![op_schedule_task::decl()])
.event_loop_middleware(|state, cx| {
let recv = state.borrow_mut::<mpsc::UnboundedReceiver<Task>>();
let mut ref_loop = false;
@@ -58,6 +61,7 @@ fn main() {
runtime.block_on(future).unwrap();
}
+#[op]
fn op_schedule_task(state: &mut OpState, i: u8, _: ()) -> Result<(), Error> {
let tx = state.borrow_mut::<mpsc::UnboundedSender<Task>>();
tx.unbounded_send(Box::new(move || println!("Hello, world! x{}", i)))