Skip to content

Option to specify extra javascript imports required when instantiating WASM Module #1584

Open
@jrandall

Description

@jrandall

Motivation

The init function generated by wasm-bindgen adds an import bindings it generates, but there does not currently appear to be any way to specify additional imports that the WASM module may require. My immediate use case is to be able to provide a 'wasi_unstable': WASIPolyfill import in order to support a WASI library to which my wasm-bindgen project links, but this general facility could be used whenever a WASM module needs direct access to javascript in the browser that doesn't go through wasm-bindgen (i.e. it should make it easier for wasm-bindgen to interoperate with other mechanisms for binding between WASM and javascript).

Proposed Solution

I am not sure where is the best place would be to implement this option. The import needs to run before the wasm module is loaded, so it seems like it ought to be pure javascript, so it doesn't really make sense to annotate any rust code with the #[wasm_bindgen] macro to configure it.

For this reason it seems like the best option might be to have a crate level setting that just defines all extra imports as a javascript fragment that just gets dumped into the generated init function. However, there don't appear to be any other aspects of #[wasm_bindgen] that work that way, so I'm not sure exactly what to suggest.

Alternatives

Despite it being perhaps a bit awkward, perhaps It could be configured from the #[wasm_bindgen] macro using a self-contained annotation that would functions similar to an inline_js that indicates that it generates an import binding (can the macro annotate an empty code block or something like that?).

Perhaps something like:

#[wasm_bindgen(init_import_js = "function(imports) {imports.wasi_unstable = WASIPolyfill}")]

or

#[wasm_bindgen(init_import_js = "function() {return {"wasi_unstable": WASIPolyfill}}")]

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions