WynJS is a JavaScript runtime written entirely in Wyn — 2,700 lines that pass 217 tests covering ES6 classes, closures, template literals, try/catch, destructuring, and more.
Why build a JS runtime?
Dogfooding. The best way to find compiler bugs is to write something complex in your own language. WynJS found over 20 bugs in the Wyn compiler — from enum match codegen to closure capture to array sort corruption.
What it supports
- Variables, functions, closures, arrow functions
- ES6 classes with constructors, methods, inheritance
- Arrays with map, filter, reduce, push, pop, join
- Objects with dot access and bracket notation
- Template literals with
${expression}interpolation - try/catch/throw with Error objects
- for, for-of, for-in, while, do-while
- switch/case with break and fallthrough
- Ternary operator, spread operator, destructuring
- Math, JSON, console, typeof, instanceof
Architecture
WynJS uses a tagged-value system where every JS value is a string with a type prefix. Objects are stored in a HashMap-based registry. Functions are stored as source strings and re-parsed on each call — simple but effective.
wyn
// The core eval loop
fn eval_node(src: string, env: int) -> string {
var type = node_type(src)
match type {
"num" => return src
"str" => return src
"call" => return eval_call(src, env)
"bin" => return eval_binary(src, env)
_ => return "undefined"
}
}Results
- 217/217 tests passing
- 2,700 lines of Wyn
- 314KB compiled binary
- Compiles in ~1.2 seconds
The full source is at sample-apps/wynjs.