-
Notifications
You must be signed in to change notification settings - Fork 106
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: zu1k <i@lgf.im>
- Loading branch information
zu1k
committed
Nov 20, 2021
1 parent
3cbe068
commit 354d733
Showing
5 changed files
with
53 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
good-mitm.lgf.im | ||
docs.mitm.plus |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
use deno_core::op_sync; | ||
use deno_core::JsRuntime; | ||
|
||
pub fn test_deno() { | ||
// Initialize a runtime instance | ||
let mut runtime = JsRuntime::new(Default::default()); | ||
|
||
// 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(); | ||
|
||
// 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. | ||
// You can find its definition in core.js. | ||
runtime | ||
.execute_script( | ||
"<usage>", | ||
r#" | ||
// Print helper function, calling Deno.core.print() | ||
function print(value) { | ||
Deno.core.print(value.toString()+"\n"); | ||
} | ||
const arr = [1, 2, 3]; | ||
print("The sum of"); | ||
print(arr); | ||
print("is"); | ||
print(Deno.core.opSync('op_sum', arr)); | ||
// And incorrect usage | ||
try { | ||
print(Deno.core.opSync('op_sum', 0)); | ||
} catch(e) { | ||
print('Exception:'); | ||
print(e); | ||
} | ||
"#, | ||
) | ||
.unwrap(); | ||
} |