-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Import JS function provided at runtime #3659
Comments
It's my idea. declare #[wasm_bindgen]
extern "C" {
pub type NS;
#[wasm_bindgen(method)]
pub fn foo(this: &NS) -> u32;
}
#[wasm_bindgen]
pub fn set_foo_ns(ns: &NS) -> u32 {
ns.foo()
} import wasmInit, { set_foo_ns } from '../pkg';
wasmInit().then( ()=> { console.log(set_foo_ns( { foo: () => 1 } )); } ); It will print |
An alternative would be to define a wrapper that lives in a .ts/.js file and calls the Vue function itself, and then import the wrapper into WASM. |
I believe the simplest way is
The documentation could definitely be improved. |
@stefnotch I think this is a good way. I'm just commenting an example I created when adding dynamic import to another project. (as it wasn't obvious to me on how to fully complete it) #[wasm_bindgen]
extern "C" {
#[wasm_bindgen]
pub type HelloWorldModule;
#[wasm_bindgen(method, js_name = "helloWorld")]
pub fn hello_world(this: &HelloWorldModule);
}
pub async fn load_dynamic_hello_world() -> HelloWorldModule {
let module_as_str = r#"export function helloWorld() { console.log("Hello World!"); }"#;
let from_data = Array::new();
from_data.push(&module_as_str.into());
let mut type_set: BlobPropertyBag = BlobPropertyBag::new();
type_set.type_("application/javascript");
let blob = Blob::new_with_str_sequence_and_options(&from_data, &type_set).unwrap();
let module_address = web_sys::Url::create_object_url_with_blob(&blob).unwrap();
let module_promise: Promise = js_sys::eval(&format!(r#"import ("{}")"#, module_address))
.unwrap()
.into();
let module = JsFuture::from(module_promise).await.unwrap();
let as_hello_world: HelloWorldModule = module.into();
as_hello_world.hello_world();
as_hello_world
} I like it better than |
Motivation
I'm embedding a Bevy game in a Vue app. i'd like for them to be able to communicate. since there's no apparent way for a rust function called from JS to interact with the ECS (or, i haven't found it) i'd like to be able to call a JS function from rust which returns a queue of recent events.
The function I want to call will be in a .vue file (and, even if i put it in a .ts file, i can't figure out how to get
#[wasm_bindgen(module=...)]
to play nice with Vite) which makes it impossible to import. So i'd like to be able to provide it at runtime, presumably while calling__wbg_init
as a bonus, this would make it always possible to link to a function with any bundler.
Proposed Solution
modify __wbg_init to something like
Alternatives
If this is already possible i couldn't find it in the documentation.
The text was updated successfully, but these errors were encountered: