2021-01-10 21:59:07 -05:00
|
|
|
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
2020-09-23 10:56:36 -04:00
|
|
|
//! This example shows you how to define ops in Rust and then call them from
|
|
|
|
//! JavaScript.
|
|
|
|
|
2021-04-12 15:55:05 -04:00
|
|
|
use deno_core::op_sync;
|
2020-09-23 10:56:36 -04:00
|
|
|
use deno_core::JsRuntime;
|
|
|
|
use std::io::Write;
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
// Initialize a runtime instance
|
|
|
|
let mut runtime = JsRuntime::new(Default::default());
|
|
|
|
|
|
|
|
// The first thing we do is define two ops. They will be used to show how to
|
|
|
|
// pass data to Rust and back to JavaScript.
|
|
|
|
//
|
|
|
|
// The first one is used to print data to stdout, because by default the
|
|
|
|
// JavaScript console functions are just stubs (they don't do anything).
|
|
|
|
//
|
|
|
|
// The second one just transforms some input and returns it to JavaScript.
|
|
|
|
|
2021-03-31 10:37:38 -04:00
|
|
|
// Register the op for outputting a string to stdout.
|
2021-04-23 11:50:45 -04:00
|
|
|
// It can be invoked with Deno.core.opcall and the id this method returns
|
|
|
|
// or Deno.core.opSync and the name provided.
|
2020-09-23 10:56:36 -04:00
|
|
|
runtime.register_op(
|
|
|
|
"op_print",
|
2021-03-31 10:37:38 -04:00
|
|
|
// The op_fn callback takes a state object OpState,
|
|
|
|
// a structured arg of type `T` and an optional ZeroCopyBuf,
|
|
|
|
// a mutable reference to a JavaScript ArrayBuffer
|
2021-04-12 15:55:05 -04:00
|
|
|
op_sync(|_state, msg: Option<String>, zero_copy| {
|
2020-09-23 10:56:36 -04:00
|
|
|
let mut out = std::io::stdout();
|
|
|
|
|
2021-03-31 10:37:38 -04:00
|
|
|
// Write msg to stdout
|
|
|
|
if let Some(msg) = msg {
|
|
|
|
out.write_all(msg.as_bytes()).unwrap();
|
|
|
|
}
|
|
|
|
|
2020-09-23 10:56:36 -04:00
|
|
|
// Write the contents of every buffer to stdout
|
2021-04-02 09:47:57 -04:00
|
|
|
if let Some(buf) = zero_copy {
|
2020-09-23 10:56:36 -04:00
|
|
|
out.write_all(&buf).unwrap();
|
|
|
|
}
|
|
|
|
|
2021-03-31 10:37:38 -04:00
|
|
|
Ok(()) // No meaningful result
|
|
|
|
}),
|
2020-09-23 10:56:36 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
// Register the JSON op for summing a number array.
|
|
|
|
runtime.register_op(
|
|
|
|
"op_sum",
|
2021-04-12 15:55:05 -04:00
|
|
|
// The op_sync function automatically deserializes
|
2020-09-23 10:56:36 -04:00
|
|
|
// the first ZeroCopyBuf and serializes the return value
|
|
|
|
// to reduce boilerplate
|
2021-04-12 15:55:05 -04:00
|
|
|
op_sync(|_state, nums: Vec<f64>, _| {
|
2021-03-31 10:37:38 -04:00
|
|
|
// Sum inputs
|
|
|
|
let sum = nums.iter().fold(0.0, |a, v| a + v);
|
|
|
|
// return as a Result<f64, AnyError>
|
|
|
|
Ok(sum)
|
2020-09-23 10:56:36 -04:00
|
|
|
}),
|
|
|
|
);
|
2021-04-25 16:00:05 -04:00
|
|
|
runtime.sync_ops_cache();
|
2020-09-23 10:56:36 -04:00
|
|
|
|
|
|
|
// Now we see how to invoke the ops we just defined. The runtime automatically
|
|
|
|
// contains a Deno.core object with several functions for interacting with it.
|
|
|
|
// You can find its definition in core.js.
|
2021-03-31 10:37:38 -04:00
|
|
|
runtime
|
|
|
|
.execute(
|
|
|
|
"<init>",
|
|
|
|
r#"
|
2021-04-25 16:00:05 -04:00
|
|
|
// Define a print function that uses
|
2020-09-23 10:56:36 -04:00
|
|
|
// our op_print op to display the stringified argument.
|
|
|
|
const _newline = new Uint8Array([10]);
|
|
|
|
function print(value) {
|
2021-04-23 11:50:45 -04:00
|
|
|
Deno.core.opSync('op_print', value.toString(), _newline);
|
2020-09-23 10:56:36 -04:00
|
|
|
}
|
|
|
|
"#,
|
2021-03-31 10:37:38 -04:00
|
|
|
)
|
|
|
|
.unwrap();
|
2020-09-23 10:56:36 -04:00
|
|
|
|
|
|
|
// Now we can finally use this in an example.
|
|
|
|
runtime
|
|
|
|
.execute(
|
|
|
|
"<usage>",
|
|
|
|
r#"
|
|
|
|
const arr = [1, 2, 3];
|
|
|
|
print("The sum of");
|
|
|
|
print(arr);
|
|
|
|
print("is");
|
2021-04-12 15:55:05 -04:00
|
|
|
print(Deno.core.opSync('op_sum', arr));
|
2020-09-23 10:56:36 -04:00
|
|
|
|
|
|
|
// And incorrect usage
|
|
|
|
try {
|
2021-04-12 15:55:05 -04:00
|
|
|
print(Deno.core.opSync('op_sum', 0));
|
2020-09-23 10:56:36 -04:00
|
|
|
} catch(e) {
|
|
|
|
print('Exception:');
|
|
|
|
print(e);
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
}
|