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

wasm: expose functions on instance.exports like rust and emscripten #31574

Closed
lucacasonato opened this issue Apr 19, 2019 · 2 comments
Closed
Labels
arch-wasm WebAssembly issues FeatureRequest FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@lucacasonato
Copy link

It would be nice to expose exported functions from go modules in the WASM instance.exports like Rust and emscripten does. This would allow for Go to work nicely with ES modules so it doesn't have to rely on assigning anything to the JS global object. This would also make the interface for calling WASM functions more standard and feel more natural in ES modules.

Example

// math.go

package math

import "syscall/js"

func Add(i []js.Value) js.Value {
    return js.ValueOf(i[0].Int()+i[1].Int())
}
<html>
    <head>
      <meta charset="utf-8">
      <script src="wasm_exec.js"></script>
      <script>
           async function init() {
                const go = new Go();
                let result = await WebAssembly.instantiateStreaming(fetch("main.wasm"), go.importObject)
                console.log(result.instance.exports.Add(1+1))
           }

           init();
       </script>
    </head>
    <body></body>
</html>

How does Rust do this

https://www.hellorust.com/demos/add/index.html

// add.rs

#[no_mangle]
pub fn add(a: i32, b: i32) -> i32 {
  return a + b
}
<html>
    <head>
      <meta charset="utf-8">
      <script>
            async function init() {
                let result = await WebAssembly.instantiateStreaming(fetch("main.wasm"))
                console.log(result.instance.exports.Add(1+1))
            }
            init();
       </script>
    </head>
    <body></body>
</html>

emscripten also has support for instance.exports (https://gist.github.com/kripken/59c67556dc03bb6d57052fedef1e61ab).

Here is some more info on exported functions in WASM: https://developer.mozilla.org/en-US/docs/WebAssembly/Exported_functions

@lucacasonato lucacasonato changed the title wasm: expose functions on instance.exports like rust wasm: expose functions on instance.exports like rust and emscripten Apr 19, 2019
@dmitshur dmitshur added arch-wasm WebAssembly issues FeatureRequest NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. labels Apr 20, 2019
@dmitshur dmitshur added this to the Unplanned milestone Apr 20, 2019
@dmitshur
Copy link
Contributor

/cc @neelance

@agnivade
Copy link
Contributor

Duplicate of #25612

@agnivade agnivade marked this as a duplicate of #25612 Apr 20, 2019
@golang golang locked and limited conversation to collaborators Apr 19, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
arch-wasm WebAssembly issues FeatureRequest FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

4 participants