https://github.com/xtuc/inline-wast
Inline WebAssembly in your JavaScript
https://github.com/xtuc/inline-wast
Last synced: over 1 year ago
JSON representation
Inline WebAssembly in your JavaScript
- Host: GitHub
- URL: https://github.com/xtuc/inline-wast
- Owner: xtuc
- License: gpl-2.0
- Created: 2018-01-06T16:51:33.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2018-01-16T13:58:53.000Z (over 8 years ago)
- Last Synced: 2025-04-30T12:25:25.968Z (over 1 year ago)
- Language: JavaScript
- Homepage:
- Size: 321 KB
- Stars: 11
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# inline-wast
> Inline WebAssembly in your JavaScript
## Motivations
The idea is (almost) the same than the built-in `asm` (or `__asm__`) function in C.
Express your computation using the WebAssembly backend or an [interpreter](https://github.com/xtuc/js-webassembly-interpreter).
WAST is a superset of WATF (`.wat`) and is not part of the WebAssembly specification but we use it for convenience.
## Example
### Instructions
```js
const {wastInstructions} = require('inline-wast/lib/interpreter');
function add(a, b) {
const fn = wastInstructions`
(i32.const ${a})
(i32.const ${b})
(i32.add)
`;
return fn();
}
console.log(add(1, 1)); // 2
```
### Function declaration
```js
const {wast} = require('inline-wast/lib/interpreter');
function add(a, b) {
const exports = wast(`
(func (export "add") (param $l i32) (param $r i32) (result i32)
(get_local $l)
(get_local $r)
(i32.add)
)
`);
return exports.add(a, b);
}
console.log(add(1, 1)); // 2
```
### Native
If you want to use the native WebAssembly backend the usage remains the same, but you need to use:
```js
const {wastInstructions, wast} = require('inline-wast/lib/native');
```
It's not recommended for now, the WAST to WASM conversion needs to be refactored.