Skip to content

Instantly share code, notes, and snippets.

@mattdesl
Last active July 22, 2019 06:31
Show Gist options
  • Save mattdesl/cdf046e660ecbede86f62a9fba5c667c to your computer and use it in GitHub Desktop.
Save mattdesl/cdf046e660ecbede86f62a9fba5c667c to your computer and use it in GitHub Desktop.
wasm + budo/browserify

Fork of @surma's work here: https://gist.github.com/surma/b2705b6cca29357ebea1c9e6e15684cc

Using browserify + budo.

Build instructions:

  • Install emscripten and ensure it is in your PATH, available at emcc
  • Clone this gist
  • npm install
  • npm start
  • Open http://localhost:9966
  • Look at console

Note: This doesn't emit a file like with Webpack, so if you aren't running your web server in the same directory as your wasm file, you will be forced to output your emcc build to /public/ (for example) so that the wasm file can be found by the browser.

/**
* Copyright 2018 Google Inc. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <emscripten.h>
EMSCRIPTEN_KEEPALIVE
int fib(int n) {
int i, t, a = 0, b = 1;
for (i = 0; i < n; i++) {
t = a + b;
a = b;
b = t;
}
return b;
}
<html>
<body>
<script src="/bundle.js"></script>
</body>
</html>
const fibonacci = require('./fibonacci.js');
fibonacci().then(Module => {
console.log(Module._fib(12));
});
{
"name": "lol",
"scripts": {
"build:codec": "emcc -O3 -s WASM=1 -s EXTRA_EXPORTED_RUNTIME_METHODS='[\"cwrap\"]' -s ALLOW_MEMORY_GROWTH=1 -s MODULARIZE=1 -s 'EXPORT_NAME=\"fibonacci\"' -o ./fibonacci.js fibonacci.c",
"build:js": "browserify index.js > bundle.js",
"build": "npm run build:codec && npm run build:js",
"test": "npm run build && budo",
"start": "budo index.js:bundle.js --live"
},
"devDependencies": {
"browserify": "^16.2.2",
"budo": "^11.2.2"
},
"dependencies": {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment