Skip to content

Commit

Permalink
example with op_hello_js
Browse files Browse the repository at this point in the history
  • Loading branch information
bartlomieju committed Oct 1, 2019
1 parent 10c7f6b commit 81ce6c0
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 5 deletions.
9 changes: 9 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
members = [
"cli",
"core",
"op_hello_js",
"tools/hyper_hello",
"deno_typescript",
"js",
Expand Down
11 changes: 10 additions & 1 deletion op_hello_js/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
1 change: 1 addition & 0 deletions op_hello_js/examples/hello.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hello();
41 changes: 41 additions & 0 deletions op_hello_js/examples/hello.rs
Original file line number Diff line number Diff line change
@@ -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());
}
}
4 changes: 2 additions & 2 deletions op_hello_js/hello.js
Original file line number Diff line number Diff line change
@@ -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"]);
}
4 changes: 2 additions & 2 deletions op_hello_js/lib.rs
Original file line number Diff line number Diff line change
@@ -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(())
}

Expand Down

0 comments on commit 81ce6c0

Please sign in to comment.