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

WebAssembly #1636

Closed
jamen opened this issue Sep 15, 2017 · 3 comments
Closed

WebAssembly #1636

jamen opened this issue Sep 15, 2017 · 3 comments

Comments

@jamen
Copy link

jamen commented Sep 15, 2017

Hey! I recently made rollup-plugin-wasm to try and support WebAssembly in Rollup. I wanted to share the results so far.

Firstly, the base plugin (wasm()) allows you to import plain .wasm files:

import createExample from './example.wasm'

// Initialize module
var example = createExample()

// Call WASM export
var foo = example.main()

This is fine if the compiler supports standalone binaries which you can import easily.

But, this plugin also allows you to require source files which have more complex conditions by using:

  • A compile method which turns source code into WebAssembly.
  • A load method for preparing WebAssembly exports (setting memory, tables, etc.)

By default, there is two configs implementing these that you can use:

Here is an example of rollup.config.js using Emscripten for C/C++ imports:

import wasm, { emscripten } from 'rollup-plugin-wasm'

export default {
  plugins: [ wasm(emscripten) ]
}

And an example of how you would write code:

int main (int a) {
  return a * 3
}
import { _main } from './example.cc'

// Calling C/C++ function
var foo = _main(3)

A good start! But it is still limited in terms of the libraries you can use, because the loader is simple.

Ideas for the future include:

  • Testing a bit more complex C/C++ code.
  • Support for Rust, Go, Elixir.
  • Support multiple lsource types. Like using JS, C++, and Rust together.
  • Build something cool with it
@Rich-Harris
Copy link
Contributor

Thanks, this is awesome! Have added it to the list of plugins. I'll close this issue as there isn't any action required for Rollup, but I'm excited to see where this goes.

@skandonkumar
Copy link

Hi, I was trying to build a JavaScript SDK over my C library. I converted my C library to WASM using EMScripten, which also provided me a wrapper JS code to call the C functions in JavaScript. I wrote my JavaScript SDK by making multiple calls to WASM and everything seemed to work. Now when I tried to build a bundle for production, I am encountering read errors. Can someone help me with my situation? Any suggestions would be appreciated. Thank You

@jamen
Copy link
Author

jamen commented Dec 20, 2019

@skandonkumar WebAssembly support in Rollup is not perfect, the only option right now is @rollup/plugin-wasm. There is interest in supporting WebAssembly in Rollup core, but it hasn't been championed yet. You could also try WebAssembly.instantiateStreaming(fetch('program.wasm'), imports) instead using WebAssembly through Rollup.

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

No branches or pull requests

3 participants