Sign in

    js_engine

    JavaScript interpreter in MoonBit

    javascript
    interpreter
    Download zip
    Author
    Version
    0.1.0
    License
    Apache-2.0
    Last updated
    8 months ago
    Downloads
    5K

    No dependencies

    #js_engine

    A minimal JavaScript tree-walking interpreter written in MoonBit.

    Self-hosting is already achieved: the engine compiles to JavaScript via MoonBit's JS target and runs on Node.js. See docs/SELF_HOST_JS_RESEARCH.md and ROADMAP.md for details.

    #Features

    • Numeric literals (IEEE 754 doubles), strings, booleans, null, undefined
    • Variable declarations: let, const, var
    • Arithmetic: +, -, *, /, %
    • Comparison: <, >, <=, >=
    • Equality: ==, ===, !=, !==
    • Logical: &&, ||, !
    • Control flow: if/else, while, for, break, continue, return
    • Functions: declarations, expressions, closures, recursion
    • Ternary operator: ? :
    • typeof operator
    • console.log() output
    • String concatenation (including mixed types)
    • Single-line (//) and multi-line (/* */) comments

    #Package Structure

    token/ Token types and source locations lexer/ Tokenizer ast/ AST node definitions parser/ Recursive descent parser with Pratt precedence interpreter/ Tree-walking evaluator with closures and scope chains cmd/main/ CLI entry point

    #Usage

    #CLI

    moon run cmd/main -- 'console.log(1 + 2)' # 3

    moon run cmd/main -- ' function fib(n) { if (n <= 1) { return n; } return fib(n - 1) + fib(n - 2); } console.log(fib(10)); ' # 55

    #As a Library

    let (output, result) = @js_engine.run("console.log(1 + 2)")
    // output = ["3"], result = "undefined"

    #Development

    moon check # Type check moon fmt # Format code moon info # Update .mbti interface files moon test # Run tests moon test --update # Update snapshot tests moon build # Build

    #Documentation

    • ROADMAP.md — Current test262 status, failure breakdown, future targets, architecture overview
    • docs/PHASE_HISTORY.md — Archived implementation notes for completed phases (1-6)
    • AGENTS.md — MoonBit coding conventions and tooling guide for AI agents

    #License

    Apache-2.0

    fib

    fn fib(n : Int) -> Int64

    has_pending_microtasks

    fn has_pending_microtasks(interp :
    Interpreter
    ) -> Bool

    Check if there are pending microtasks in the queue

    has_pending_timers

    fn has_pending_timers(interp :
    Interpreter
    ) -> Bool

    Check if there are pending timers in the queue

    run

    fn run(source : String) -> (Array[String], String) raise

    run_microtask_checkpoint

    fn run_microtask_checkpoint(interp :
    Interpreter
    ) -> Bool raise

    Run a single microtask checkpoint Returns true if there are more microtasks to process

    run_timer_checkpoint

    fn run_timer_checkpoint(interp :
    Interpreter
    ) -> Unit raise

    Run all pending timers with microtask draining between each

    run_with_event_loop

    Run JavaScript source with event loop support This version allows the host to control microtask and timer execution timing

    sum

    fn sum(data~ : Array[Int], start? : Int, length? : Int) -> Int

    data is a labelled argument without default value having type Array[Int] start is an optional labelled argument with default value 0 having type Int length is an optional labelled argument without default value having type Option[Int]

    Source Files