https://github.com/dy/jz
Minimal JS subset compiling to WASM
https://github.com/dy/jz
Last synced: about 1 year ago
JSON representation
Minimal JS subset compiling to WASM
- Host: GitHub
- URL: https://github.com/dy/jz
- Owner: dy
- License: mit
- Created: 2024-11-13T14:13:53.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-02-28T01:14:39.000Z (over 1 year ago)
- Last Synced: 2025-04-06T00:32:54.114Z (about 1 year ago)
- Size: 28.3 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# jz  [](https://github.com/dy/piezo/actions/workflows/test.yml)
_JZ_ is minimal modern functional JS subset that compiles to WebAssembly (WASM).
Think of it as tiny _JavaScript to WASM compiler_.
## Reference
* Numbers: `0.1`, `1.2e+3`, `0xabc`, `0b101`, `0o357`
* Strings: `"abc"`, `'abc'`
* Values: `true`, `false`, `null`, `NaN`, `Infinity`, ~~`undefined`~~
* Access: `a.b`, `a[b]`, `a(b)`
* Arithmetic:`+a`, `-a`, `a + b`, `a - b`, `a * b`, `a / b`, `a % b`, `a ** b`
* Comparison: `a < b`, `a <= b`, `a > b`, `a >= b`, `a == b`, `a != b`, `a === b`, `a !== b`
* Bitwise: `~a`, `a & b`, `a ^ b`, `a | b`, `a << b`, `a >> b`, `a >>> b`
* Logic: `!a`, `a && b`, `a || b`, `a ? b : c`
* Increments: `a++`, `a--`, `++a`, `--a`
* Assignment: `a = b`, `a += b`, `a -= b`, `a *= b`, `a /= b`, `a %= b`, `a **= b`, `a <<= b`, `a >>= b`, `a >>>= b`
* Logical Assignment: `a ||= b`, `a &&= b`, `a ??= b`
* Arrays: `[a, b]`, `...a` (no objects yet)
* Declarations: `let a, b`, `const c` (no `var`)
* Functions: `(a, b) => c` (no `function` keyword)
* Comments: `// foo`, `/* bar */`
* Control Flow: `if (a) {...} else if (b) {...} else {}`, `for (a;b;c) {...}`, `while (a) {...}`
* Exceptions: `try {...} catch (e) {...}`
* Modules: `import`, `export`
## Usage
```js
import jz from 'jz'
// compile JS (function multiplying 2 numbers) - it returns WASM buffer
const buf = jz(`export x = (a, b) => a * b`)
// compile WASM module and create an instance
const mod = new WebAssembly.Module(buf)
const { exports: { x } } = new WebAssembly.Instance(mod, { ...imports })
// use exported WASM function
x(2,3) === 6
```
_Coming soon:_ CLI with jz a.js → a.wasm and batch compilation.
## Examples
_Coming soon_.
## Why?
JS has grown complex with legacy features (var, OOP) and niche additions (generators, async loops, etc).
JZ is inspired by floatbeats/bytebeats, it focuses on a minimal, modern, essential subset that ties to WebAssembly features.
* No classes/prototypes – use functions & closures.
* No old syntax – use modern ES5+.
* No [regrets](https://github.com/DavidBruant/ECMAScript-regrets) – drop `undefined`.
* No computed props - objects are structs.
* No autosemicolons - keep syntax ordered.
* No async – keep code plain & simple.
### Goals
* _lightweight_ – embed anywhere, from websites to microcontrollers.
* _fast_ – compiles to WASM faster than `eval` parses.
* _tiny WASM output_ – no runtime, no heap, no wrappers.
* _seamless JS integration_ – export / import, same func signatures.
### Why not [porf](https://github.com/CanadaHonk/porffor)?
Porffor is brilliant, but aligns to TC39 and hesitant on full WASM. JZ stays small and flexible.
### Why not [assemblyscript](https://github.com/AssemblyScript/assemblyscript)?
AssemblyScript is built on TypeScript, while JZ stays pure JS.
### Why not [piezo](https://github.com/dy/piezo)?
Piezo offers extra features like groups, pipes, units, ranges and extra operators.
It might become a solid niche language, but takes time for R&D.
JZ is possible first step for it.
### Why _jz_?
JZ stands for JavasSript Zero – a return to core, stripped to essentials. Also jazzy vibe.