Skip to content
/ jz Public

Research: minimal JS subset compiling to WASM

License

Notifications You must be signed in to change notification settings

dy/jz

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

114 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

jz stability

Research on the topic: JS syntax that compiles to pure WASM. No runtime, no GC, no overhead.

import jz from 'jz'

// 3 lines of JS → 120 bytes of WASM, compiles in <1ms
const { exports } = await WebAssembly.instantiate(jz(`
  let { sin, PI } = Math
  export let sine = (out, freq, t) => {
    for (let i = 0; i < out.length; i++) out[i] = sin((t + i) * freq * PI * 2 / 44100)
  }
`))

// Real-time audio at native speed, zero GC pauses
exports.sine(audioBuffer, 440, sampleOffset)

Usage

import jz from 'jz'

const wasm = jz(`export const add = (a, b) => a + b`)
const { exports } = await WebAssembly.instantiate(wasm)

exports.add(2, 3)  // 5

CLI

# Install globally
npm install -g jz

# Evaluate expression (requires watr)
jz "1 + 2"
# Output: 3

# Compile to WAT (default)
jz compile program.js -o program.wat

# Compile to WASM binary (requires watr)
jz compile program.js -o program.wasm

# Run compiled program (requires watr)
jz run program.js

# Show help
jz --help

Reference

  • Numbers: 0.1, 1.2e+3, 0xabc, 0b101, 0o357
  • Strings: "abc", 'abc'
  • Values: true, false, null, NaN, Infinity, PI, E
  • Access: a.b, 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
  • Bitwise: ~a, a & b, a ^ b, a | b, a << b, a >> b, a >>> b
  • Logic: !a, a && b, a || b, a ?? b, a ? b : c
  • Assignment: a = b, a += b, a -= b, a *= b, a /= b, a %= b
  • Arrays: [a, b], arr[i], arr[i] = x, arr.length
  • TypedArrays: new Float32Array(n), buf[i], buf.length, buf.byteLength
  • Objects: {a: b}, {a, b}, obj.prop
  • Boxed primitives: Object.assign(42, {prop}), Object.assign("str", {prop}), Object.assign([arr], {prop})
  • Functions: (a, b) => c, a => b, () => c
  • Currying: add = x => y => x + y; add(5)(3)
  • Comments: // foo, /* bar */
  • Declarations: let, const, block scope
  • Strict equality: ===, !==
  • Closures: capture outer variables
  • Rest/spread: ...args, [...arr]
  • Destructuring params: ({ x }) => x
  • Regex: /pattern/.test(str) - compile-time regex, native WASM matching
  • More array/string methods

Built With

Similar

About

Research: minimal JS subset compiling to WASM

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published