Stream: git-wasmtime

Topic: wasmtime / issue #2868 Using a library that uses wasm-bin...


view this post on Zulip Wasmtime GitHub notifications bot (May 05 2022 at 16:35):

alexcrichton commented on issue #2868:

I'm going to close this since wasm-bindgen is not intended to work with Wasmtime.

view this post on Zulip Wasmtime GitHub notifications bot (May 05 2022 at 16:35):

alexcrichton closed issue #2868:

I managed to add websocket support (not merged upstream yet) to the popular sqlx crate so that it can be used from a browser app. I can now use this crate from a browser based wasm app that targets wasm32-unknown-unknown and querying works as expected. I want to now invoke APIs from this library using wasmtime.

So i created a 'hello-world' wasmtime project and added sqlx, with the websocket feature enabled, as a dependency. I am now trying to invoke APIs from my project like this:

[dependencies]
wasmtime = { version = "0.26.0" }
anyhow = { version = "1.0.40" }
sqlx = { path = "../sqlx", features = ["postgres", "runtime-wasm-bindgen"] }
use anyhow::Result;
use sqlx::Connection;
use sqlx::{Database, PgConnection, Postgres};
use std::future::Future;
use std::pin::Pin;
use std::task::{Context, Poll, RawWaker, RawWakerVTable, Waker};
use wasmtime::*;

const URL: &str = "postgresql://user:pass@ip_of_ws_tcp_proxy:port_of_ws_tcp_proxy/db";

pub async fn new() -> PgConnection {
    <Postgres as Database>::Connection::connect(URL)
        .await
        .unwrap()
}

fn main() -> Result<()> {
    let store = Store::new(&Engine::new(Config::new().async_support(true))?);
    let wat = r#"
        (module
            (import "" "" (func $async_hello))
            (func (export "")
                call $async_hello)
        )
    "#;

    let module = Module::new(store.engine(), wat)?;

    // host function
    let async_hello = Func::new_async(
        &store,
        FuncType::new(None, None),
        (),
        move |caller, state, params, results| {
            Box::new(async move {
                let mut conn = new().await;
                println!("connected!");
                Ok(())
            })
        },
    );

    run(async {
        let instance = Instance::new_async(&store, &module, &[async_hello.into()]).await?;
        let func = instance.get_func("").unwrap();
        func.call_async(&[]).await
    })
    .unwrap();

    Ok(())
}

fn run<F: Future>(future: F) -> F::Output {
    let mut f = Pin::from(Box::new(future));
    let waker = dummy_waker();
    let mut cx = Context::from_waker(&waker);
    loop {
        match f.as_mut().poll(&mut cx) {
            Poll::Ready(val) => break val,
            Poll::Pending => {}
        }
    }
}

fn dummy_waker() -> Waker {
    return unsafe { Waker::from_raw(clone(5 as *const _)) };

    unsafe fn clone(ptr: *const ()) -> RawWaker {
        assert_eq!(ptr as usize, 5);
        const VTABLE: RawWakerVTable = RawWakerVTable::new(clone, wake, wake_by_ref, drop);
        RawWaker::new(ptr, &VTABLE)
    }

    unsafe fn wake(ptr: *const ()) {
        assert_eq!(ptr as usize, 5);
    }

    unsafe fn wake_by_ref(ptr: *const ()) {
        assert_eq!(ptr as usize, 5);
    }

    unsafe fn drop(ptr: *const ()) {
        assert_eq!(ptr as usize, 5);
    }
}

I used the run and dummy_waker functions from the existing async test file. When i run this using $ cargo build --release && cargo run, I get this:

thread 'main' panicked at 'cannot call wasm-bindgen imported functions on non-wasm targets',
...

How can I make this work?


Last updated: Dec 23 2024 at 12:05 UTC