jadeallencook / wasm

Easily export functions written in C code and import them into JavaScript.

Home Page:https://jadeallencook.github.io/wasm/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

WASM

Easily export functions written in C and import them into JavaScript.

Getting Started

Let's first create a simple C program that exports an "add" function:

#include <webassembly.h>

export int add(int a, int b) {
  return a + b;
}

Next, include WASM into the head then use WASM to import our function:

<head>
  <script src="wasm.js"></script>
</head>
<body>
  <script>
    wasm('program.wasm').then(({ add } ) => {
      console.log(add(1, 2)); // 3
    });
  </script>
</body>

Now convert the C to WASM and launch a local dev server by running:

$ npm start

Launch your browser (localhost:8080) and open your console!

NPM Start

When you run "npm start" you're actually running these two commands:

$ npx wa compile -o program.wasm program.c
$ php -S localhost:8080

The first one will compile "program.c" into "program.wasm".

The second command will launch a PHP server on port 8080.

I'm currently working on adding a Node server with live reload.

About

Easily export functions written in C code and import them into JavaScript.

https://jadeallencook.github.io/wasm/


Languages

Language:HTML 58.5%Language:CSS 23.9%Language:JavaScript 15.6%Language:C 2.0%