-
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
Invalid init
Function Code for Passing in Arraybuffer
#1418
Comments
init
Function Code for Passing in Arraybuffer
Actually this appears fixed there and is just invalid in my current wasm-pack. Disregard for now, thanks. |
Ok, reopening, having trouble with latest on Windows. For the simple wasm from a
(I need pkg web because I need to init the WASM array buffer myself). Here is the generated init function: function init(module_or_path, maybe_memory) {
let result;
const imports = { './imedge_manip': __exports };
if (module_or_path instanceof URL || typeof module_or_path === 'string' || module_or_path instanceof Request) {
const response = fetch(module_or_path);
if (typeof WebAssembly.instantiateStreaming === 'function') {
result = WebAssembly.instantiateStreaming(response, imports)
.catch(e => {
console.warn("`WebAssembly.instantiateStreaming` failed. Assuming this is because your server does not serve wasm with `application/wasm` MIME type. Falling back to `WebAssembly.instantiate` which is slower. Original error:\n", e);
return response
.then(r => r.arrayBuffer())
.then(bytes => WebAssembly.instantiate(bytes, imports));
});
} else {
result = response
.then(r => r.arrayBuffer())
.then(bytes => WebAssembly.instantiate(bytes, imports));
}
} else {
result = WebAssembly.instantiate(module_or_path, imports)
.then(instance => {
return { instance, module: module_or_path };
});
}
return result.then(({instance, module}) => {
wasm = instance.exports;
init.__wbindgen_wasm_module = module;
return wasm;
});
} Note, this code expects |
This commit fixes the `init` function when passed a `WebAssembly.Module`. Upon closer reading of the [spec] we see there's two possible return values from `WebAssembly.instantiate`. If passed a `Module`, it will return only the `Instance`. If passed a buffer source, though, it'll return an object with the module/instance. The fix here is to check the result value and add in the module if it looks like it wasn't already present. This will hopefully continue to address the CloudFlare use case where `instance of WebAssembly.Module` doens't work, but it'll also ensure that it detects when the module is present and uses the result object if it can. Closes rustwasm#1418 [spec]: http://webassembly.github.io/spec/js-api/index.html#webassembly-namespace
This commit fixes the `init` function when passed a `WebAssembly.Module`. Upon closer reading of the [spec] we see there's two possible return values from `WebAssembly.instantiate`. If passed a `Module`, it will return only the `Instance`. If passed a buffer source, though, it'll return an object with the module/instance. The fix here is to check the result value is an `Instance`, and if so assume the input must have been a module so it's paired up in the output. Closes rustwasm#1418 [spec]: http://webassembly.github.io/spec/js-api/index.html#webassembly-namespace
Thanks for the report! Definitely looks like a bug and should be fixed in #1419 |
Please see the following code:
wasm-bindgen/crates/cli-support/src/js/mod.rs
Lines 891 to 903 in a1c457b
The result of
instantiate
according to MDN isResultObject
, not justInstance
as is assumed here. I have to patch this to accessinstance.instance
for it to work right.The text was updated successfully, but these errors were encountered: