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

Should we register modules directly? #4

Open
yoshuawuyts opened this issue Nov 27, 2017 · 0 comments
Open

Should we register modules directly? #4

yoshuawuyts opened this issue Nov 27, 2017 · 0 comments

Comments

@yoshuawuyts
Copy link
Member

yoshuawuyts commented Nov 27, 2017

I was thinking: perhaps it would make sense to register modules directly?

E.g. turn this:

var rust = require('rustify')

var wasm = rust`
  #[no_mangle]
  pub fn add_one(x: i32) -> i32 {
    x + 1
  }
`

WebAssembly.instantiate(wasm, {})
  .then(function (res) {
    var addOne = res.instance.exports.add_one
    console.log(addOne(41))
    console.log(addOne(68))
  }).catch(function (e) {
    console.error('Creating WASM module failed', e)
  })

Into this:

var rust = require('rustify')

var wasm = rust`
  #[no_mangle]
  pub fn add_one(x: i32) -> i32 {
    x + 1
  }
`

wasm.then((mod) => {
  console.log(mod.add_one(41))
  console.log(mod.add_one(68))
}).catch((e) => console.error('WASM error', e))

The benefit is that there's way less code to remember (ugh, res.instance.exports). The downside is that we're no long just exposing a Uint8Array, so doing things like registering a function twice is not going to be possible.

Would this be worth it?

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

No branches or pull requests

1 participant