Implement the register-based JavaScript virtual machine that executes bytecode.
Scope#
Build the core execution engine: instruction dispatch loop, call stack management, and basic value representation.
Value Representation#
- Tagged value type: Number (f64), Boolean, String, Null, Undefined, Object (pointer)
- NaN-boxing or enum-based representation (choose based on performance)
- Value comparison and coercion rules (abstract equality, strict equality)
VM Core#
- Register file: fixed-size array of Values per call frame
- Instruction dispatch loop (match on opcode, execute, advance IP)
- Call frames: instruction pointer, register base, return register
- Call stack for nested function calls
- Global object for global variable storage
Operations#
- Arithmetic with type coercion (ToNumber)
- String concatenation (+ operator with string operands)
- Comparison with proper coercion rules
- Property access on objects (get/set)
- Function calls: push frame, execute, pop frame, return value
- typeof operator
- Exception handling: try/catch with exception stack
Error Handling#
- Runtime error types (TypeError, ReferenceError, RangeError)
- Stack traces (collect frame info for error reporting)
- Uncaught exception handling
Acceptance Criteria#
- Value type with all JS primitive types
- VM executes arithmetic expressions correctly
- VM handles variable load/store (local and global)
- VM handles control flow (jumps, conditionals, loops)
- VM handles function calls and returns
- VM handles string operations
- VM handles comparison with proper coercion
- Basic exception throwing and catching
- Integration test: compile and run simple JS programs end-to-end
- Unit tests for each instruction type
Phase 10 — JavaScript Engine (issue 4 of 15). Depends on: JS Bytecode.