Skip to content
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

Closed
cretz opened this issue Apr 2, 2019 · 3 comments · Fixed by #1419
Closed

Invalid init Function Code for Passing in Arraybuffer #1418

cretz opened this issue Apr 2, 2019 · 3 comments · Fixed by #1419
Labels

Comments

@cretz
Copy link

cretz commented Apr 2, 2019

Please see the following code:

}} else {{
{init_memory1}
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;
{start}
return wasm;
}});

The result of instantiate according to MDN is ResultObject, not just Instance as is assumed here. I have to patch this to access instance.instance for it to work right.

@cretz cretz added the bug label Apr 2, 2019
@cretz cretz changed the title Invalid `iniCode for Passing in Arraybuffer Invalid init Function Code for Passing in Arraybuffer Apr 2, 2019
@cretz
Copy link
Author

cretz commented Apr 2, 2019

Actually this appears fixed there and is just invalid in my current wasm-pack. Disregard for now, thanks.

@cretz cretz closed this as completed Apr 2, 2019
@cretz
Copy link
Author

cretz commented Apr 2, 2019

Ok, reopening, having trouble with latest on Windows. For the simple wasm from a cargo build --target wasm32-unknown-unknown:

# C:\Users\chad.retz\AppData\Local\.wasm-pack\wasm-bindgen-9a795696ca119c40\wasm-bindgen.exe --version
wasm-bindgen 0.2.40 (7392df8a3)

C:\Users\chad.retz\AppData\Local\.wasm-pack\wasm-bindgen-9a795696ca119c40\wasm-bindgen.exe target\wasm32-unknown-unknown\debug\sample.wasm --out-dir pkg --web

(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 WebAssembly.instantiate to return a promise for instance but it in fact returns a promise for ResultObject of which instance is a member. I can't find this part of the init script in the codebase even on that version tag (just released)...so I wonder if the download for wasm-pack is acting funny.

@cretz cretz reopened this Apr 2, 2019
alexcrichton added a commit to alexcrichton/wasm-bindgen that referenced this issue Apr 2, 2019
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
alexcrichton added a commit to alexcrichton/wasm-bindgen that referenced this issue Apr 2, 2019
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
@alexcrichton
Copy link
Contributor

Thanks for the report! Definitely looks like a bug and should be fixed in #1419

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants