Stream: git-wasmtime

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


view this post on Zulip Wasmtime GitHub notifications bot (May 02 2021 at 09:32):

abhijeetbhagat opened 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?

view this post on Zulip Wasmtime GitHub notifications bot (May 02 2021 at 09:50):

bjorn3 commented on Issue #2868:

You are adding sqlx to the program that uses wasmtime to run a webassembly module. You aren't adding it to the wasm module itself. Note that wasm-bindgen doesn't have support for generating wasmtime compatible glue code, or any glue code that isn't javascript for that matter, so you will have to manually implement glue code for wasmtime. Also you say that sqlx uses websockets when using wasm-bindgen. That makes me think that you actually want to run your code in a browser. Wasmtime is made for running wasm modules outside of the browser, not in it.

view this post on Zulip Wasmtime GitHub notifications bot (May 03 2021 at 16:00):

alexcrichton commented on Issue #2868:

Thanks for the report! I think, though, that this error is coming from wasm-bindgen and not wasmtime.

In general wasm-bindgen is designed to run on the web with a JS runtime. The wasm-bindgen project was not designed with a use case like wasmtime in mind, and consequently it's unlikely to work very well in wasmtime.

Do you have a specific issue with wasmtime though where you think something is going wrong?


Last updated: Dec 23 2024 at 12:05 UTC