Skip to content

Commit ef978c9

Browse files
committed
added test for JsExternal
1 parent 69cfc1f commit ef978c9

5 files changed

Lines changed: 74 additions & 10 deletions

File tree

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { executeExample } from '../../test-utils/run_test.ts'
2+
import { assertEquals } from "jsr:@std/assert@^1"
3+
4+
Deno.test("background_tasks", async () => {
5+
const result = await executeExample("background_tasks")
6+
assertEquals(result, [
7+
`Task [rs]: Started`,
8+
`Task [js]: Message`,
9+
`Task [rs]: Ended`,
10+
].join('\n'))
11+
});

examples/src/background_tasks/mod.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,34 +25,34 @@ pub fn main() -> anyhow::Result<()> {
2525
let ctx = worker.create_context()?;
2626

2727
// Execute some JavaScript in the context
28-
ctx.exec(|env| {
28+
ctx.exec_blocking(|env| {
29+
// Tell the runtime that it must remain alive
30+
// Safety: This must be manually incremented and decremented
31+
// if the ref count is greater than 0, the runtime
32+
// will remain alive until it's 0
2933
env.inc_ref();
3034

3135
env.spawn_background({
3236
let env = env.as_async();
3337
async move {
34-
println!("Background Task Started");
38+
println!("Task [rs]: Started");
3539
tokio::time::sleep(Duration::from_secs(1)).await;
36-
println!("Background Task Ended");
3740

3841
env.exec_async(|env| {
39-
println!("hi");
42+
println!("Task [js]: Message");
43+
// Tell the runtime that this task will no longer require keeping it alive
4044
env.dec_ref();
4145
Ok(())
4246
})
4347
.await?;
48+
49+
println!("Task [rs]: Ended");
4450
Ok(())
4551
}
4652
})?;
4753

4854
Ok(())
4955
})?;
5056

51-
println!("Context Dropping");
52-
drop(ctx);
53-
drop(worker);
54-
55-
println!("Context Dropped");
56-
5757
Ok(())
5858
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { executeExample } from '../../test-utils/run_test.ts'
2+
import { assertEquals } from "jsr:@std/assert@^1"
3+
4+
Deno.test("external_value", async () => {
5+
const result = await executeExample("external_value")
6+
assertEquals(result, "42")
7+
});

examples/src/external_value/mod.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
use ion::*;
2+
3+
pub fn main() -> anyhow::Result<()> {
4+
let runtime = JsRuntime::initialize_once(JsRuntimeOptions {
5+
extensions: vec![
6+
ion::extensions::console(),
7+
ion::extensions::set_interval(),
8+
ion::extensions::set_timeout(),
9+
],
10+
transformers: vec![
11+
ion::transformers::json(),
12+
ion::transformers::ts(),
13+
ion::transformers::tsx(),
14+
],
15+
..Default::default()
16+
})?;
17+
18+
let worker = runtime.spawn_worker(JsWorkerOptions::default())?;
19+
let ctx = worker.create_context()?;
20+
21+
// Set External Value
22+
ctx.exec_blocking(|env| {
23+
let external = 42;
24+
let external_js = JsExternal::new(env, external)?;
25+
env.global_this()?
26+
.set_named_property("external", external_js)?;
27+
Ok(())
28+
})?;
29+
30+
// Use External Value
31+
ctx.exec_blocking(|env| {
32+
let Some(external_js) = env
33+
.global_this()?
34+
.get_named_property::<JsExternal<i32>>("external")?
35+
else {
36+
panic!("Could not revive external")
37+
};
38+
let external = external_js.as_inner()?;
39+
println!("{}", external);
40+
Ok(())
41+
})?;
42+
43+
Ok(())
44+
}

examples/src/main.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ mod custom_extension;
77
mod custom_resolver;
88
mod deferred;
99
mod eval;
10+
mod external_value;
1011
mod http_server;
1112
mod memory_usage_context;
1213
mod memory_usage_module;
@@ -53,6 +54,7 @@ fn main() -> anyhow::Result<()> {
5354
"memory_usage_worker" => memory_usage_worker::main(),
5455
"memory_usage_module" => memory_usage_module::main(),
5556
"memory_usage_value" => memory_usage_value::main(),
57+
"external_value" => external_value::main(),
5658
_ => Err(anyhow::anyhow!("No example for: \"{}\"", example)),
5759
}
5860
}

0 commit comments

Comments
 (0)