← Contents

Chapter 31

The JavaScript Back End

Chapter 26 stated the principle the native generator is built on, and everything in it followed from that one sentence: the generator never reimplements the runtime. It emits C++ that calls the same functions the interpreter calls, which is why --exe and the interpreter produce byte-identical output, and why a feature implemented once works in both.

--target=js gives that up. It has to. There is no librakupp_rt.a in a browser, and no Value, and no applyArith — so a JavaScript program cannot call the runtime the other back ends share. Something has to play its part, and that something is a second runtime, written by hand, in JavaScript.

This chapter is about what happens to a code generator when the principle holding it together is unavailable, and what it costs to keep two runtimes honest instead of one.

The interface

One function, and the same refusal type main.cpp already catches:

// src/codegen/Js.h
std::string transpileToJs(Program& prog, const JsOptions& opt,
                          std::string* dts = nullptr,
                          std::string* map = nullptr,
                          std::vector<JsModuleUnit>* modules = nullptr);
std::string jsRuntimeSource();     // the runtime, baked into the binary
std::string jsRuntimeModule();     // …as the ES module `-o` writes beside it
std::string jsWasmWrapper(const std::string& src, const JsOptions& opt);

modules are the units the program uses, parsed and in dependency order. Each is compiled ahead of the program into a function scope of its own, so its my variables and private subs stay its own and its exports are bound where the program can see them. There is no loader in the output: a module is part of the one file.

transpileToJs throws CodegenError — the same struct Codegen.h declares — so the CLI reports a JavaScript refusal in exactly the shape it reports a C++ one. That is a small thing and it is the reason the second back end did not need a second error path through main.cpp.

The same program, a third way

Chapter 25 took demo.raku through four back ends. Here is the fifth:

# demo.raku
sub square($n) { $n * $n }
my $total = 0;
for 1..5 { $total += square($_) }
say $total;
// Generated by `rakupp --target=js` — rakupp 5.6.3, source b82ba795e0ab3338
// RAKUPP-EXE-MANIFEST {"rakupp":"5.6.3","mode":"js","source":"b82ba795…"}
import R from "./rakupp-rt.js";
R.main(() => {
    let v___0 = R.Any;
    function u_square(v_n) {
        let v___2_1 = R.Any;
        if (arguments.length !== 1) R.arityError("square", 1, arguments.length);
        if (v_n === R.Mu) R.notAny("$n");
        v_n = R.item(v_n);
        return R.mul(v_n, v_n);
    }
    let v_total = 0;
    {
        L3: for (let v___2_2 of R.rangeIter(1, 5, false, false)) {
            {
                (v_total = R.add(v_total, (u_square(v___2_2))));
            }
        }
    }
    R.sink(R.say(R.item(v_total)));
}, { mainExit: true });
//# sourceMappingURL=demo.js.map

The shape is deliberate and it is the same bargain the C++ back end makes: the program's subs are hoisted function declarations, its my variables are lets, its classes are R.defClass calls, so a reader recognises their own program. Every value operation is a call into R. Control flow is JavaScript's own wherever it is lexically local — that L3: is a real labelled loop, and last compiles to break L3 — and only where control has to cross a closure does the emitter fall back to throwing a control object.

Three lines are worth naming because they are what a signature costs: the arity check, the Mu check, and the itemisation. They are the price of not reimplementing the dispatcher, and they are also why a transpiled sub recurses about 8,900 deep where a plain JavaScript function manages 10,400 — the guards take stack frames too.

The runtime that had to be written

src/js-rt/ is thirteen fragments and 5,728 hand-written lines, concatenated in name order:

05-gb-tables.jsgrapheme-break data, so .chars counts clusters in a browser
10-core.jsthe value model, R.add and the rest of the arithmetic
20-str.js, 30-list.js, 40-objects.jsthe three container families
50-builtins.js, 60-methods.jswhat the emitter is allowed to call
70-host.js, 80-glue.jsstdout, exit status, the host boundary
85-js.jsthe use JS interop surface
86-async.js, 87-supply.jspromises and supplies over the event loop
90-regex.jsthe regex engine, again

tools/js/gen-rt-src.raku bakes that into src/JsRuntimeSrc.cpp, so a binary writes the exact runtime it was built with rather than whatever happens to be on disk — the same scheme the grammar shim uses. It goes in as an array of raw-string chunks under 15 KB each, because MSVC caps a single string literal at 16 KB and one literal of this size does not compile there at all.

That table is the honest cost of the chapter's opening. 90-regex.js is a second regex engine. 05-gb-tables.js is a second copy of the grapheme data. Every one of those files is a place where the two implementations can drift, and the reason the next section exists.

Three outputs, and a second tier

rakupp --target=js prog.raku -o prog.js           # + rakupp-rt.js beside it
rakupp --target=js --standalone prog.raku -o p.js # one file, runtime inlined
rakupp --target=js --module lib.raku -o lib.js    # an ES module + lib.d.ts
rakupp --target=js --runtime -o rakupp-rt.js      # the runtime alone

The sidecar form is 773 bytes of program against a 416 KB runtime; --standalone is that same runtime inlined, so the file is 416 KB whatever the program. For a web page that is one cached asset and many small programs, which is why the sidecar is the default. -o also writes prog.js.map, a source map from each generated line to the Raku line of its statement.

Anything outside the core is refused with the line and a reason:

$ rakupp --target=js prog.raku -o prog.js
note: concurrency/process types (Proc::Async) — P4 of the plan — outside the
      JavaScript core; --fallback=wasm runs it on the WebAssembly engine instead
exit 5, no file written

That last clause is the second tier. --fallback=wasm emits a 2 KB wrapper that loads the interpreter compiled to WebAssembly and runs the embedded source through it — --exe-info reads back "mode":"js-wasm". So there are two ways to reach a browser and they are complements rather than rivals: the transpiler is fast and refuses things, the WebAssembly engine accepts everything and carries the whole interpreter. Chapter 31 is about building the second one.

Keeping two runtimes honest

With one runtime, agreement is structural: --exe calls applyArith and so does the interpreter, so they cannot disagree. Here it has to be measured, and t/js/run.raku is what measures it.

Every program in t/regression/ and examples/ is transpiled with --standalone, run under a JavaScript host — Bun by default, Node or Deno through RAKUPP_JS — and compared with the same binary interpreting the same file — stdout, stderr and exit status, byte for byte. The interpreter is the oracle, not a golden file, which is the important choice: a golden records what the output was, and an oracle records what it should be. The report has three numbers — in core and agreeing, refused with a histogram of reasons, and disagreeing, which must be zero. The refusal histogram is the work queue.

The gate also checks two things that are not about any program: that the generated src/JsRuntimeSrc.cpp is current with src/js-rt/, and that every builtin the emitter is willing to call exists in the runtime. Four interop goldens under t/js/interop/ cover use JS, where the interpreter cannot be the oracle because there is no DOM on this side — they run against a stand-in and carry their own expected output.

Where it diverges

Three differences survive that discipline, and they are all the same shape: places where JavaScript's own semantics are visible through the runtime.

interpreterJavaScript
(0.1e0).Rat0.10.100000000000000006
(2e60).Intthe exact integer2e+60 — past 2⁵³ it stays a float
lt, leg, cmp, sort, hash .gistcodepoint orderUTF-16 code-unit order

The string ordering one is the least obvious and the most likely to bite: a character above the BMP is a surrogate pair in JavaScript, so ff sorts before 𝕏 here and after it there.

This table had six rows in September 2026. Tie rounding in printf("%.2f"), "a\r\nb".chars and say of an unhandled Failure now give the interpreter's answer, checked for this edition under Bun at v5.6.3.

Three claims about the core are also narrower than they look. Multi dispatch ranks by arity, type, literal and where — but not by sigil, so an untyped @ candidate does not beat an untyped $ one. A scalar's type constraint is not enforced at all: my Int $x = 'no' is accepted. And assigning Nil leaves Nil rather than the declared type's default.

Speed

Node starts in about 20 ms against the interpreter's 4, so a small program is slower under JavaScript. Whether a program that computes is faster depends on which interpreter it is compared with. In September 2026, on the benchmark machine with Node v24.20.0, wall clock including start-up:

kernelinterpreter--target=js under Node
fib(29)298 ms82 ms
streq (1M eq)233 ms69 ms
loopsum (1M)86 ms36 ms

Those were honest numbers and they flattered the wrong thing even then: what the JIT was beating was the tree-walker, not the native back end. A month later the tree-walker stopped walking these three. A sub or a loop over plain Ints and Strs runs as a kernel on machine values, and the benchmark sitting of 2026-10-21 (v5.5.0, an Apple M4, so not the same machine) has the interpreter at 11.7 ms for fib, 15.0 for streq and 5.2 for loopsum — each under Node's start-up alone. The JavaScript column has not been re-measured. The reason to reach for --target=js was never the table: it is that the program has to run somewhere there is no binary.

Honest limitations