Skip to content

Commit b3e42cc

Browse files
fix context and runtime handling
1 parent 1a72c3a commit b3e42cc

File tree

2 files changed

+25
-25
lines changed

2 files changed

+25
-25
lines changed

extism/host/src/main.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use extism::*;
22

3-
#[tokio::main]
4-
async fn main() -> anyhow::Result<()> {
3+
fn main() -> anyhow::Result<()> {
54
let tokio_rt = tokio::runtime::Builder::new_multi_thread()
65
.thread_name("main-runtime")
76
.worker_threads(2)
@@ -18,11 +17,10 @@ async fn main() -> anyhow::Result<()> {
1817
let file = Wasm::file("../plugin/target/wasm32-unknown-unknown/debug/plugin.wasm");
1918
let manifest = Manifest::new([file]);
2019

21-
let plugin = PluginBuilder::new(manifest)
22-
.with_wasi(true);
20+
let plugin = PluginBuilder::new(manifest).with_wasi(true);
2321

24-
let mut plugin = iroh_extism_host_functions::add_all_host_functions(rt, plugin, iroh)
25-
.build()?;
22+
let mut plugin =
23+
iroh_extism_host_functions::add_all_host_functions(rt, plugin, iroh).build()?;
2624

2725
let res = plugin
2826
.call::<&str, &str>("print_hai_and_get_ticket", "blobaaa54kekl2oa7yd5ro3u65kynwolq3h2msu65c3jfj44ja23fstmcajcnb2hi4dthixs65ltmuys2mjomrsxe4bonfzg62bonzsxi53pojvs4lydabefsibbyrlqbqfiirdmivybeyaeaqktotuaaaaaaaaaaaaqb7cvoah75ql4x2hvxaxxlgje2d7qy2avizyjb25pd4anc6c5ulvgpth5xq")
@@ -31,4 +29,4 @@ async fn main() -> anyhow::Result<()> {
3129
println!("{}", res);
3230

3331
Ok(())
34-
}
32+
}

extism/iroh-extism-host-functions/src/lib.rs

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -44,34 +44,33 @@ pub async fn create_iroh(path: PathBuf) -> Result<IrohNode> {
4444
// Ok(user_data.node_id().to_string())
4545
// });
4646

47-
host_fn!(iroh_blob_get_ticket(user_data: IrohNode; ticket: &str) -> Vec<u8> {
48-
let node = user_data.get()?;
49-
let node = node.lock().unwrap();
47+
struct Context {
48+
rt: tokio::runtime::Handle,
49+
iroh: IrohNode,
50+
}
51+
52+
host_fn!(iroh_blob_get_ticket(user_data: Context; ticket: &str) -> Vec<u8> {
53+
let ctx = user_data.get()?;
54+
let ctx = ctx.lock().unwrap();
5055

5156
let (node_addr, hash, format) = iroh::ticket::BlobTicket::from_str(ticket).map_err(|_| anyhow!("invalid ticket"))?.into_parts();
5257

5358
if format != iroh::rpc_protocol::BlobFormat::Raw {
5459
return Err(anyhow!("can only get raw bytes for now, not HashSequences (collections)"));
5560
}
56-
57-
// TODO(b5): this crashes execution because we don't have a handle to the original _tokio_ runtime :(
58-
let handle = tokio::runtime::Handle::current();
59-
let buf = handle.block_on(async move {
60-
let mut stream = node.client()
61-
.blobs
62-
.download(iroh::rpc_protocol::BlobDownloadRequest {
61+
let client = ctx.iroh.client();
62+
let buf = ctx.rt.block_on(async move {
63+
let mut stream = client.blobs.download(iroh::rpc_protocol::BlobDownloadRequest {
6364
hash,
6465
format,
6566
peer: node_addr,
6667
out: DownloadLocation::Internal,
6768
tag: SetTagOption::Auto,
68-
})
69-
.await?;
69+
}).await?;
7070
while stream.next().await.is_some() {}
7171

72-
let buffer = node.client().blobs.read(hash).await?.read_to_bytes().await.map_err(|e| anyhow!("read error: {}", e))?.to_vec();
73-
74-
anyhow::Ok(buffer)
72+
let buffer = client.blobs.read(hash).await?.read_to_bytes().await?;
73+
anyhow::Ok(buffer.to_vec())
7574
})?;
7675

7776
Ok(buf)
@@ -80,15 +79,18 @@ host_fn!(iroh_blob_get_ticket(user_data: IrohNode; ticket: &str) -> Vec<u8> {
8079
pub fn add_all_host_functions(
8180
rt: tokio::runtime::Handle,
8281
b: PluginBuilder,
83-
node: IrohNode,
82+
iroh: IrohNode,
8483
) -> PluginBuilder {
85-
let iroh = UserData::new(node);
84+
let ctx = UserData::new(Context {
85+
rt,
86+
iroh,
87+
});
8688

8789
b.with_function(
8890
"iroh_blob_get_ticket",
8991
[PTR],
9092
[PTR],
91-
iroh.clone(),
93+
ctx,
9294
iroh_blob_get_ticket,
9395
)
9496
}

0 commit comments

Comments
 (0)