Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/bytecodealliance/javy
JS to WebAssembly toolchain
https://github.com/bytecodealliance/javy
Last synced: 4 days ago
JSON representation
JS to WebAssembly toolchain
- Host: GitHub
- URL: https://github.com/bytecodealliance/javy
- Owner: bytecodealliance
- License: apache-2.0
- Created: 2021-04-22T13:36:40.000Z (almost 4 years ago)
- Default Branch: main
- Last Pushed: 2025-01-08T21:25:06.000Z (17 days ago)
- Last Synced: 2025-01-14T14:08:27.651Z (11 days ago)
- Language: Rust
- Size: 101 MB
- Stars: 2,343
- Watchers: 248
- Forks: 115
- Open Issues: 28
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
- awesome-ccamel - bytecodealliance/javy - JS to WebAssembly toolchain (Rust)
README
## About this repo
**Introduction**: Run your JavaScript on WebAssembly. Javy takes your JavaScript
code, and executes it in a WebAssembly embedded JavaScript runtime. Javy can
create _very_ small Wasm modules in the 1 to 16 KB range with use of dynamic
linking. The default static linking produces modules that are at least 869 KB in
size.## Installation
Pre-compiled binaries of the Javy CLI can be found on [the releases
page](https://github.com/bytecodealliance/javy/releases).## Example
Define your JavaScript like:
```javascript
// Read input from stdin
const input = readInput();
// Call the function with the input
const result = foo(input);
// Write the result to stdout
writeOutput(result);// The main function.
function foo(input) {
return { foo: input.n + 1, newBar: input.bar + "!" };
}// Read input from stdin
function readInput() {
const chunkSize = 1024;
const inputChunks = [];
let totalBytes = 0;// Read all the available bytes
while (1) {
const buffer = new Uint8Array(chunkSize);
// Stdin file descriptor
const fd = 0;
const bytesRead = Javy.IO.readSync(fd, buffer);totalBytes += bytesRead;
if (bytesRead === 0) {
break;
}
inputChunks.push(buffer.subarray(0, bytesRead));
}// Assemble input into a single Uint8Array
const { finalBuffer } = inputChunks.reduce((context, chunk) => {
context.finalBuffer.set(chunk, context.bufferOffset);
context.bufferOffset += chunk.length;
return context;
}, { bufferOffset: 0, finalBuffer: new Uint8Array(totalBytes) });return JSON.parse(new TextDecoder().decode(finalBuffer));
}// Write output to stdout
function writeOutput(output) {
const encodedOutput = new TextEncoder().encode(JSON.stringify(output));
const buffer = new Uint8Array(encodedOutput);
// Stdout file descriptor
const fd = 1;
Javy.IO.writeSync(fd, buffer);
}
```Create a WebAssembly binary from your JavaScript by:
```bash
javy build index.js -o destination/index.wasm
```For more information on the commands you can run `javy --help`
You can then execute your WebAssembly binary using a WebAssembly engine:
```bash
$ echo '{ "n": 2, "bar": "baz" }' | wasmtime index.wasm
{"foo":3,"newBar":"baz!"}%
```## Documentation
Read the documentation [here](./docs/index.md)