From 81ce6c0d7acd1acefaa011c6e32db1d14b9ffe86 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Fri, 27 Sep 2019 14:50:04 +0200 Subject: [PATCH] example with op_hello_js --- Cargo.lock | 9 ++++++++ Cargo.toml | 1 + op_hello_js/Cargo.toml | 11 +++++++++- op_hello_js/examples/hello.js | 1 + op_hello_js/examples/hello.rs | 41 +++++++++++++++++++++++++++++++++++ op_hello_js/hello.js | 4 ++-- op_hello_js/lib.rs | 4 ++-- 7 files changed, 66 insertions(+), 5 deletions(-) create mode 100644 op_hello_js/examples/hello.js create mode 100644 op_hello_js/examples/hello.rs diff --git a/Cargo.lock b/Cargo.lock index 20e94e0c9d0f4f..2057017972acc5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -805,6 +805,15 @@ dependencies = [ "libc 0.2.62 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "op_hello_js" +version = "0.1.0" +dependencies = [ + "deno 0.19.0", + "futures 0.1.29 (registry+https://github.com/rust-lang/crates.io-index)", + "tokio 0.1.22 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "os_pipe" version = "0.8.2" diff --git a/Cargo.toml b/Cargo.toml index 0f0b10de471024..f138a7cb9b8ad3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,6 +2,7 @@ members = [ "cli", "core", + "op_hello_js", "tools/hyper_hello", "deno_typescript", "js", diff --git a/op_hello_js/Cargo.toml b/op_hello_js/Cargo.toml index 5e6e44946ffa8b..66ff6d39c9dbe2 100644 --- a/op_hello_js/Cargo.toml +++ b/op_hello_js/Cargo.toml @@ -11,4 +11,13 @@ edition = "2018" path = "lib.rs" [dependencies] -deno = "0.19.0" +deno = { path = "../core", version = "0.19.0" } + +[[example]] +name = "op_hello_example" +path = "examples/hello.rs" + +# tokio is only used for op_hello_example +[dev_dependencies] +futures = "0.1.29" +tokio = "0.1.18" diff --git a/op_hello_js/examples/hello.js b/op_hello_js/examples/hello.js new file mode 100644 index 00000000000000..422b1c9659d6bd --- /dev/null +++ b/op_hello_js/examples/hello.js @@ -0,0 +1 @@ +hello(); diff --git a/op_hello_js/examples/hello.rs b/op_hello_js/examples/hello.rs new file mode 100644 index 00000000000000..5cb802f1a89dbf --- /dev/null +++ b/op_hello_js/examples/hello.rs @@ -0,0 +1,41 @@ +/// To run this benchmark: +/// +/// > DENO_BUILD_MODE=release ./tools/build.py && \ +/// ./target/release/deno_core_http_bench --multi-thread +extern crate deno; +extern crate futures; +extern crate tokio; + +use deno::*; +use futures::future::lazy; +use tokio::prelude::*; + + +fn main() { + let main_future = lazy(move || { + // TODO currently isolate.execute() must be run inside tokio, hence the + // lazy(). It would be nice to not have that contraint. Probably requires + // using v8::MicrotasksPolicy::kExplicit + + let js_source = include_str!("hello.js"); + + let mut isolate = deno::Isolate::new(StartupData::None, false); + let r = op_hello_js::init(&mut isolate); + eprintln!("result r {:?}", r); + let r = isolate.execute("hello.js", js_source); + eprintln!("result r {:?}", r); + + isolate.then(|r| { + js_check(r); + Ok(()) + }) + }); + + tokio::runtime::current_thread::run(main_future); +} + +fn js_check(r: Result<(), ErrBox>) { + if let Err(e) = r { + panic!(e.to_string()); + } +} diff --git a/op_hello_js/hello.js b/op_hello_js/hello.js index 7cfdaa82b871f3..b15ab404bd10c1 100644 --- a/op_hello_js/hello.js +++ b/op_hello_js/hello.js @@ -1,6 +1,6 @@ // TODO In the future maybe we can extract the op id in the top-level and use a // constant. But currently it's causing problems with snapshotting. -export function hello() { - Deno.core.send(Deno.ops["hello"]); +function hello() { + Deno.core.send(Deno.core.ops()["hello"]); } diff --git a/op_hello_js/lib.rs b/op_hello_js/lib.rs index ecd98b15a40694..333eed15415f40 100644 --- a/op_hello_js/lib.rs +++ b/op_hello_js/lib.rs @@ -1,9 +1,9 @@ extern crate deno; use deno::*; -pub fn init(&mut isolate: Isolate) -> Result<(), ErrBox> { +pub fn init(isolate: &mut Isolate) -> Result<(), ErrBox> { isolate.register_op("hello", op_hello); // register_op defined by #3002 - isolate.execute("hello.js")?; + isolate.execute("hello.js", include_str!("hello.js"))?; Ok(()) }