All Projects → peterc → webassembly-simplest-demo

peterc / webassembly-simplest-demo

Licence: other
A simple example of compiling C to WebAssembly and running it

Programming Languages

HTML
75241 projects
javascript
184084 projects - #8 most used programming language
Makefile
30231 projects
c
50402 projects - #5 most used programming language

Projects that are alternatives of or similar to webassembly-simplest-demo

Wasm Worker
Move a WebAssembly module into its own thread
Stars: ✭ 215 (+1553.85%)
Mutual labels:  webassembly
Gxb Core
GXChain Blockchain implementation
Stars: ✭ 226 (+1638.46%)
Mutual labels:  webassembly
Rust Yew Realworld Example App
Exemplary real world app built with Rust + Yew + WebAssembly
Stars: ✭ 249 (+1815.38%)
Mutual labels:  webassembly
Way Of Life
Conway's game of life implemented in JavaScript & WebAssembly, rendered to canvas.
Stars: ✭ 218 (+1576.92%)
Mutual labels:  webassembly
Wasm By Example
Wasm By Example is a website with a set of hands-on introduction examples and tutorials for WebAssembly (Wasm)
Stars: ✭ 226 (+1638.46%)
Mutual labels:  webassembly
Awesome Wasm Langs
😎 A curated list of languages that compile directly to or have their VMs in WebAssembly
Stars: ✭ 2,966 (+22715.38%)
Mutual labels:  webassembly
Customasm
💻 An assembler for custom, user-defined instruction sets! https://hlorenzi.github.io/customasm/web/
Stars: ✭ 211 (+1523.08%)
Mutual labels:  webassembly
rust-webassembly-serverless
An AWS lambda function written in Rust using WebAssembly
Stars: ✭ 29 (+123.08%)
Mutual labels:  webassembly
Wavelet
Write once, run forever. Deploy robust, scalable, decentralized WebAssembly applications on Wavelet.
Stars: ✭ 224 (+1623.08%)
Mutual labels:  webassembly
Wasmer Postgres
💽🕸 Postgres library to run WebAssembly binaries.
Stars: ✭ 245 (+1784.62%)
Mutual labels:  webassembly
Rusty Typescript
A TypeScript compiler written in Rust
Stars: ✭ 222 (+1607.69%)
Mutual labels:  webassembly
Timidity
Play MIDI files in the browser w/ Web Audio, WebAssembly, and libtimidity
Stars: ✭ 221 (+1600%)
Mutual labels:  webassembly
Wapm Cli
📦 WebAssembly Package Manager (CLI)
Stars: ✭ 236 (+1715.38%)
Mutual labels:  webassembly
Pont
An online board game in Rust and WebAssembly
Stars: ✭ 218 (+1576.92%)
Mutual labels:  webassembly
Prism
Build frontend web apps with Ruby and WebAssembly
Stars: ✭ 251 (+1830.77%)
Mutual labels:  webassembly
Porcupine
On-device wake word detection powered by deep learning.
Stars: ✭ 2,606 (+19946.15%)
Mutual labels:  webassembly
Electron Wasm Rust Example
A minimal Electron + WebAssembly (WASM) + 🦀 Rust example.
Stars: ✭ 227 (+1646.15%)
Mutual labels:  webassembly
vrcpu
Code, documentation, schematics, notes for my Ben Eater inspired breadboard computer and emulator
Stars: ✭ 98 (+653.85%)
Mutual labels:  webassembly
ugo-compiler-book
📚 µGo语言实现(从头开发一个迷你Go语言编译器)[Go版本+Rust版本]
Stars: ✭ 996 (+7561.54%)
Mutual labels:  webassembly
Wasmtime Go
Go WebAssembly runtime powered by Wasmtime
Stars: ✭ 239 (+1738.46%)
Mutual labels:  webassembly

A simple, raw WebAssembly demo for macOS-based developers

This is an attempt to show off the process of writing a simple function in C, compiling it to WebAssembly, and calling it from both Node.js and the Web browser, as of April 2019.

There's a lot of out of date guides out there, so the goal here is to keep things both as simple as possible and working using the norms of early 2019.

Note: This is aimed at macOS-based users. Everything will be generally fine for Linux users too, but installing a recent version of LLVM/clang (or even Emscripten) is a real minefield unless you know what you're doing.

Another note: Surma has written a fantastic tutorial which goes much deeper into the stuff covered in this repo. I strongly recommend it.

Write a function in C

Here's a basic Fibonacci sequence algorithm written in C:

int fib(int n) {
  int a = 0, b = 1, c = 0;

  for (int i = 1; i <= n; i++) {
    c = a + b;
    a = b;
    b = c;
  }

  return a;
}

Store that in a file called fib.c

How to Compile the C to WebAssembly

You need to be running the latest version of llvm/clang for the compilation to be relatively straightforward. The version of clang that comes with macOS/Xcode is NOT suitable.

On macOS with homebrew installed, run brew install llvm

You can also download pre-built binaries from here for most operating systems.

Once you have clang in play, this is how to compile fib.c to fib.wasm:

/usr/local/opt/llvm/bin/clang fib.c -o fib.wasm --target=wasm32 -nostdlib -fvisibility=default -Xlinker --no-entry -Xlinker -export-dynamic

You should now have a fib.wasm file to play with.

@julien has supplied a Makefile which also runs the above, though note you may need to prefix running make with a different PATH if you are using a separate build of clang like above. For example:

PATH=/usr/local/opt/llvm/bin:$PATH make

How to run the WebAssembly in Node.js

Using a recent version of Node (8+):

node node.js

Read the source code for the mechanism involved.

How to run the WebAssembly in the browser

Serve index.html over HTTP. The easiest way to do this on most systems is:

python -m SimpleHTTPServer 3000

Then navigate to http://localhost:3000/

Read the source code for the mechanism involved and some extra comments on its operation.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].