Applications build for WebAssembly with FreePascal
Date: 2026-12-02
Area: build, engine, apps
Supersedes: ADR 0188's consequence
that a Wasm build starts from G2 once Lakon compiles programs; its other decisions stand.
Related: ADR 0146
Context
ADR 0188 expected the first WebAssembly build of G2 to come from Lakon, which compiles no program yet. FreePascal
3.3.1, the compiler G2 already needs, targets wasm32-wasip1 with its own assembler and linker, and fpc-toolchain
ships that target's units. The playground runs each script by starting an engine binary on the server; a module the
browser runs would take that work off the server.
WASI preview 1 has no threads, no sockets, no shared libraries and no C math library. WebAssembly also keeps calls on the host engine's own stack, outside linear memory, so the address of a local no longer says how deep native code has recursed: the native stack guard saw nothing, and a recursion through built-ins overflowed V8's stack, which ends the whole module.
Decision
- The applications build for
wasm32-wasip1with FreePascal 3.3.1.FPC_TARGET_CPU=wasm32 FPC_TARGET_OS=wasip1 ./build.pas runner testrunnerbuilds modules that import onlywasi_snapshot_preview1. - What a host lacks is named once.
source/shared/HostPlatform.inc, whichShared.incand the engine'sGoccia.Checks.incboth include, definesGOCCIA_NO_SOCKETS,GOCCIA_NO_DYNAMIC_LIBRARIES,GOCCIA_NO_THREADS,GOCCIA_NO_LIBMandGOCCIA_HIDDEN_CALL_STACKfor WASI and for Lakon. Code that works around what a host lacks branches on these symbols; a call of a WASI API (its clocks, its file descriptors) branches onWASI. - Without threads, the work a thread did runs on the caller's thread.
ThreadLaunchruns a file's thread, and a fetch or provider request's, when it starts, and the join finds it finished. What needs a second thread running beside the first is refused: a nested sandbox run, whose engine would share its caller's thread, and test262's agents.Atomics.waitcan only time out, at its timeout or the run's deadline. - The deadline has no timer thread. The instruction counter runs whenever a deadline is set, hands out its budget
in windows of 4096 instructions and looks at the clock between them; the waits look at it through
LimitDeadlinePassed, as they already did, and native loops that pollInterrupt, such as the regular expression matcher and BigInt arithmetic, throughInterruptPending. A wait sleeps, as no other thread can end it. - Without a C math library, the engine uses fdlibm.
FdLibmports fdlibm 5.3, the library V8's and SpiderMonkey'sMathcome from, withlog2andlog10from FreeBSD's msun.**goes throughGoccia.NumberExponentiation, as on macOS, and%throughGoccia.NumberRemainder. - On a hidden call stack, the engine counts nesting.
CallValue,ConstructandGeneratorResumecount how deeply they nest: the native stack guard refuses past 300 nested calls (NATIVE_DEPTH_MAX), and the re-entry floor past 250. At V8's default WebAssembly stack of 984 KB, the deepest recursion through these entries overflowed past 450. Some recursions outside them that inputs drive get lower limits there: 256 levels of source nesting and trees 1,536 tall, JSON 1,000 deep,flat2,500 and proxies forwarding 800 deep. - The clocks are WASI's (
clock_time_get), to the nanosecond. - What the host cannot do fails as it does elsewhere. A network request fails before it connects, the libraries ICU and FFI load from are not found, and an OS error has no code, since FreePascal's WASI runtime keeps none.
Consequences
- Intl has no ICU on WebAssembly yet, and
fetchfails there until an embedder answers it through its transport (ADR 0146). - A recursion through built-ins ends in a
RangeErrorafter 300 nested calls on WebAssembly (250 through callbacks), where a native build allows far more. Native recursion with no limit of its own can still overflow the host's stack, which ends the module. - A run with a timeout counts every instruction on WebAssembly and takes no fast path, about 1.8 times as long on a hot loop.
wasm32-wasip1threadswould give the applications their threads back, but FreePascal 3.3.1 stops with an internal error on a threadvar array indexed by a variable, which the engine has.- The
LAKONconditionals that remain stay where they are;HostPlatform.incdefines its symbols for Lakon too.