mirror of
https://github.com/denoland/deno.git
synced 2024-12-23 07:44:48 -05:00
cleanup(core/example/hello_world): use Deno.core.print instead of new op (#10645)
This commit is contained in:
parent
d059f9b06e
commit
9f9a50a3e8
1 changed files with 9 additions and 57 deletions
|
@ -4,54 +4,16 @@
|
||||||
|
|
||||||
use deno_core::op_sync;
|
use deno_core::op_sync;
|
||||||
use deno_core::JsRuntime;
|
use deno_core::JsRuntime;
|
||||||
use deno_core::ZeroCopyBuf;
|
|
||||||
use std::io::Write;
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
// Initialize a runtime instance
|
// Initialize a runtime instance
|
||||||
let mut runtime = JsRuntime::new(Default::default());
|
let mut runtime = JsRuntime::new(Default::default());
|
||||||
|
|
||||||
// The first thing we do is define two ops. They will be used to show how to
|
// Register an op for summing a number array.
|
||||||
// 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.
|
|
||||||
|
|
||||||
// Register the op for outputting a string to stdout.
|
|
||||||
// It can be invoked with Deno.core.opcall and the id this method returns
|
|
||||||
// or Deno.core.opSync and the name provided.
|
|
||||||
runtime.register_op(
|
|
||||||
"op_print",
|
|
||||||
// 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
|
|
||||||
op_sync(
|
|
||||||
|_state, msg: Option<String>, zero_copy: Option<ZeroCopyBuf>| {
|
|
||||||
let mut out = std::io::stdout();
|
|
||||||
|
|
||||||
// Write msg to stdout
|
|
||||||
if let Some(msg) = msg {
|
|
||||||
out.write_all(msg.as_bytes()).unwrap();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Write the contents of every buffer to stdout
|
|
||||||
if let Some(buf) = zero_copy {
|
|
||||||
out.write_all(&buf).unwrap();
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(()) // No meaningful result
|
|
||||||
},
|
|
||||||
),
|
|
||||||
);
|
|
||||||
|
|
||||||
// Register the JSON op for summing a number array.
|
|
||||||
runtime.register_op(
|
runtime.register_op(
|
||||||
"op_sum",
|
"op_sum",
|
||||||
// The op_sync function automatically deserializes
|
// The op-layer automatically deserializes inputs
|
||||||
// the first ZeroCopyBuf and serializes the return value
|
// and serializes the returned Result & value
|
||||||
// to reduce boilerplate
|
|
||||||
op_sync(|_state, nums: Vec<f64>, _: ()| {
|
op_sync(|_state, nums: Vec<f64>, _: ()| {
|
||||||
// Sum inputs
|
// Sum inputs
|
||||||
let sum = nums.iter().fold(0.0, |a, v| a + v);
|
let sum = nums.iter().fold(0.0, |a, v| a + v);
|
||||||
|
@ -61,28 +23,18 @@ fn main() {
|
||||||
);
|
);
|
||||||
runtime.sync_ops_cache();
|
runtime.sync_ops_cache();
|
||||||
|
|
||||||
// Now we see how to invoke the ops we just defined. The runtime automatically
|
// 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.
|
// contains a Deno.core object with several functions for interacting with it.
|
||||||
// You can find its definition in core.js.
|
// You can find its definition in core.js.
|
||||||
runtime
|
|
||||||
.execute(
|
|
||||||
"<init>",
|
|
||||||
r#"
|
|
||||||
// Define a print function that uses
|
|
||||||
// our op_print op to display the stringified argument.
|
|
||||||
const _newline = new Uint8Array([10]);
|
|
||||||
function print(value) {
|
|
||||||
Deno.core.opSync('op_print', value.toString(), _newline);
|
|
||||||
}
|
|
||||||
"#,
|
|
||||||
)
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
// Now we can finally use this in an example.
|
|
||||||
runtime
|
runtime
|
||||||
.execute(
|
.execute(
|
||||||
"<usage>",
|
"<usage>",
|
||||||
r#"
|
r#"
|
||||||
|
// Print helper function, calling Deno.core.print()
|
||||||
|
function print(value) {
|
||||||
|
Deno.core.print(value.toString()+"\n");
|
||||||
|
}
|
||||||
|
|
||||||
const arr = [1, 2, 3];
|
const arr = [1, 2, 3];
|
||||||
print("The sum of");
|
print("The sum of");
|
||||||
print(arr);
|
print(arr);
|
||||||
|
|
Loading…
Reference in a new issue