Repository navigation
Conversation
|
It'll take a little while before this gets merged. |
|
Understandable, it is very big yes. Sorry about that :) The good news is that it is pretty well isolated to No rush to merge at all. And if you come to the conclusion this would better live in a fork, that's completely fine by me. I just figured this would put us at parity with GHC in terms of its JS / WASM backend features, and others could benefit as well. |
|
I definitely want mhs to have the functionality that this provides, so it will get merged eventually. |
|
To answer question 1: I would prefer if the 900 lines added to eval.c were in a separate file that is #included |
…argets A JavaScript FFI for MicroHs's emscripten target, in the style of GHC's WebAssembly backend, with the runtime changes that JavaScript callbacks and the browser need. Squash of the jsval branch onto upstream master. - foreign import javascript: the JavaScript code is inline, with the arguments as $1, $2, ...; each import becomes an EM_JS function, mhs_js_<name>, so emscripten's optimizer sees what the code uses and keeps it. Whether the code is an expression or statements is decided at compile time (jsIsStatements). safe and interruptible imports turn a JavaScript exception into a JSException; an interruptible import is async, and the calling thread waits for its Promise while the others run. - GHC.Wasm.Prim: JSVal (released when garbage collected, or with freeJSVal), JSString, and syncCallback / asyncCallback / "wrapper" imports, following GHC's API. - Callbacks run as Haskell threads of their own (run_callback). An uncaught Haskell exception in a callback is thrown to the JavaScript caller as an Error, and each callback keeps its own error. - The runtime returns to the JavaScript event loop when every Haskell thread is blocked, and is resumed by a callback, a timer or a settling Promise; no ASYNCIFY. - Targets browser, browser_js, emscripten_js and quickjs, and browser_rts: a runtime built once that fetches and runs a combinator program (app.comb), so a rebuild needs no C compiler. - JavaScript files embedded in the output: .js arguments and the jsbits of a package (README; the suffix detection itself is upstream, augustss#529). - A module that uses the JavaScript FFI still compiles for a native target (tests/JSNative.hs). - Tests: JSVal, JSCallback, ForImpJS, JSNative, BrowserRtsOther, run with the emscripten tests. Data.Text.Encoding is left as upstream has it: augustss#557 already stores NUL as C0 80 in decodeUtf8 and turns it back into 00 in encodeUtf8. The compiler is regenerated in two steps, since the runtime's js_exn_string (used by Control.Exception.Internal) is only in the new compiler's runtimeFFI list: upstream's compiler built the new one with upstream's Control.Exception.Internal, which then bootstrapped (stage2 equal to stage1). Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
- foreign import javascript: a $k in the code that is not an argument (in particular $0: the arguments are $1, $2, ... as with GHC's WebAssembly backend, but were $0, $1, ... before) is a compile-time error, instead of an undefined variable when the code runs. - Code that starts with 'function' or 'class' is a function or class expression unless more code follows it; it was taken for statements, which made the generated JavaScript a syntax error. 'return' as a property name ($1.return) no longer makes the code statements. - A foreign export called from JavaScript while the threads wait in the JavaScript event loop runs as a thread of its own, like a callback. It ran as a second main thread, which ended the program when it finished. - The runtime counts the live callbacks instead of setting a flag when the first one is made, so a deadlock is reported again once they have all been freed. Standard output is flushed when the runtime returns to the JavaScript event loop. - A callback that runs out of its reduction budget gets a new one, so yield() does not run on every reduction from then on. - Tests: JSDeadlock, JSExportBlocked. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
When every thread is blocked and one is in threadDelay, the runtime arms a JavaScript timer and returns to the event loop. If main then finished (woken by a callback or a Promise) with no live callback, main_finish called exit(), which under emscripten_exit_with_live_runtime only throws ExitStatus to the caller, and the timer stayed armed. When it fired, runThreads called check() outside any try, so the "runtime has stopped" Error was thrown into a bare setTimeout callback: in node an uncaught exception (exit status 1 instead of 0, after waiting out the delay), in a browser a console error. Stopping the runtime (stop, and the atexit hook mhs_js_exited) now also clears the timer, and runThreads does nothing once the runtime has stopped: a timer has no caller to throw to. Test: JSExitTimer, run with the emscripten tests. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
The parameters of the EM_JS function of an import are wasm i32s, which arrive
in JavaScript as signed numbers, and emscripten does no unsigned conversion.
So a Word argument >= 2^31 was negative in the JavaScript code ("$1" applied
to 0xFFFFFFFF gave -1). Convert it in place with >>> 0, as Bool and JSVal
arguments are converted. Results were already right: ToInt32 wraps them.
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
…Script js_fromUTF8 decoded with a default TextDecoder, which strips a leading byte order mark, so toJSString "\xFEFF..." lost its first character (miso worked around this in its own Data.JSString). Decode with ignoreBOM, and create the decoder once instead of on every conversion. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
It was never run, and a program using threadDelay hangs on it: the runtime returns to the event loop with a setTimeout, and qjs has none. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
The end of mhs_main became main_finish so that it can also run from mhs_js_run_threads. Defining it before mhs_main made the diff show the whole tail as moved; after it, the tail stays in place and only the emscripten branch in mhs_main is new. No change in behaviour. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
node colours a number given to console.log as a separate argument when FORCE_COLOR is set (the output then has escape codes), so the test failed on such a terminal. Concatenate instead. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Subsections (calling JavaScript, JavaScript values, callbacks, the event loop, targets, the browser runtime, embedded files), tables for the safety annotations and the targets, and the facts of the current branch: $0 is an error, a leading U+FEFF survives, browser_rts and rts-web.js, and that a foreign export called from JavaScript must not block. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Only entries through Module.mhsjs.enter (callbacks, timers, Promises) and an unsafe import's catch recorded that the runtime had stopped. A wasm trap while main itself was running, e.g. the evaluator exceeding the stack, went to emscripten's callMain instead: in the browser that is a console error, ABORT stays false, and the next DOM event ran its callback on a corrupted heap. main now runs inside a JavaScript frame (mhs_js_enter_main) that calls Module.mhsjs.stop for any exception except emscripten's 'unwind', then rethrows, so callMain still decides what an exit means. Test: JSTrap, an out of bounds read while main runs; a callback called 50 ms later is refused. It needs its own pre-js to keep node alive after the uncaught exception. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
…me's mkCallback fetched a value callback's result (getJSVal) inside enter, so a callback that returned a JSVal it had already freed made enter stop the runtime for good, although nothing had unwound: the C stack had returned normally. The result is now taken right after enter (still before any other Haskell thread runs), and an error there is thrown to the caller like an uncaught Haskell exception. Test: JSCallback 20. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
JavaScript such as 'x ??(y)' inside the generated C string literal forms a trigraph. Clang's default mode only warns, but a strict C99 build would rewrite the code. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
They were hand-written FFI adapters, known to the compiler through its runtimeFFI list, and js_exn_string was the only runtime entry typed IO String (building the Haskell string in C). A compiler without them in its list, i.e. upstream's or any older one, generated stubs for them and failed on the types: the library could only be built by a compiler already regenerated with this branch, in two steps. Now they are plain C functions with C types, declared in mhsffi.h: js_exn_string :: Ptr () -> IO CString the description, malloc()ed, UTF-8 js_free_jsval :: Ptr () -> IO () The JSVal is passed as the payload of its foreign pointer, which is the handle (primForeignPtrToPtr), so no new C type is needed, and the stubs are generated as for any other import. Control.Exception.Internal decodes the UTF-8 itself (it cannot import Foreign.C.String) with the peek_uint8 and free runtime entries, which every compiler has. Natively the two functions exist too, so a program using the JavaScript FFI still links. The regeneration takes one more stage than usual once: the current compiler emits no stub for js_exn_string, the one it builds does, and the one after that is the fixpoint (stage 2 equals stage 3). Test: JSVal checks that a non-ASCII message survives. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
The nested if in the let of peekUTF8 was an hlint suggestion, which the lint job treats as a failure. Regenerates base.pkg. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
emscripten, emscripten_js, browser, browser_js and browser_rts are for programs (with the JavaScript FFI and the event loop); emscripten_web is for running the compiler itself in a web page (web-mhs/). Say so where the targets are defined and described; nothing else changes. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Both are hlint suggestions, which the lint job treats as failures; the Tester one is in upstream's own recent change (upstream's lint job fails on it too). Regenerates tester.c and base.pkg. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
934c206 to
7635597
Compare
The emscripten code (the JavaScript side, JSVal handles, callbacks, interruptible imports, running the threads from the JavaScript event loop) moves from eval.c to src/runtime/jsffi.c, #included by eval.c like md5.c, since it needs the evaluator's statics. eval.c calls it through a few plain C functions declared in src/runtime/jsffi.h, included at the top of eval.c under __EMSCRIPTEN__ (an EM_JS function must be defined before its first use). What only the JavaScript FFI uses lives there too: the callback cases of throwto() and yield() (js_throwto, js_yield), the end of main under emscripten (js_main_done), the run time program of browser_rts (run_comb), and mhs_to_HsStablePtr. So for other targets throwto(), yield(), pause_exec(), MHS_INIT_ARGS and the end of mhs_main_body read as before. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
|
Okay cool, have gone ahead and moved most of the JS FFI logic out of
Claude identified additional things present in this PR that we could upstream separately, to make it easier to review. These aren't JS FFI specific; they fix bugs the JS FFI runs into (1 especially), but apply more generally. FFI bug fixes, with tests where possible.
I can split these off into 4 separate PRs, and then rebase against |
|
Yes, please split. I prefer unrelated items to go in their own PRs. |
|
Adding safe/unsafe to the AST would be a good thing to split out. |
Conflicts: - Parse.hs: take master's default safety (Safe, as in the Haskell report). README updated; the JSCallback test that relies on a fatal exception is now explicitly `unsafe`. - Desugar.hs: keep jsval's safety-aware ForImp/parseImpEnt, take master's foreign import C name (number first, then module). - tests/Makefile: keep both testjsnative and testffiname. - generated/mhs.c, generated/base.pkg: regenerated (mhs.c at a fixpoint). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Conflicts: - Desugar.hs: drop an explicit forall from a foreign import's type (master, augustss#574), then apply jsval's safety handling to that type. - tests/Makefile: keep both testforimpforall and testjsnative. - generated/mhs.c, generated/base.pkg: regenerated (mhs.c at a fixpoint). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Preface
This preface is mine. Everything after it was written by Claude.
I did this by giving Claude (Fable 5.1, and Opus 5.5 for the last round of fixes) the emscripten, MicroHs, GHC (WebAssembly backend and
rts/) and miso sources, and asking it to follow your README's instructions for adding primitives. I directed the work, reviewed the changes, and tested them with miso.The design follows GHC's WebAssembly backend. As there, a
JSValis an integer id shared between the two heaps, indexing a table of JavaScript values on the JavaScript side (GHC'sJSValManager); a callback keeps its Haskell function alive with aStablePtr; and a JavaScriptFinalizationRegistrycalls back into the runtime to free thatStablePtronce the JavaScript side has dropped the callback. The one difference: GHC ties the id's lifetime to aWeak#with a C finalizer, while this uses aForeignPtrwith a C finalizer.It works well for miso. miso's integration tests pass built with MicroHs, for both the WebAssembly (
browser) and the JavaScript-only (browser_js) target, in miso's CI on dmjio/miso#1694. And haskell-miso.org, which exercises most of miso, runs normally built entirely with MicroHs, prerendered pages included: https://mhs-latest.netlify.app. miso's FFI layer uses nearly all of the JavaScript FFI that GHC offers (synchronous and asynchronous callbacks, Promises, exceptions in both directions,JSVallifetimes), so getting its tests and examples to pass is a good sign.Size: the site's code is about 370 KB compressed with MicroHs, against 1.15 MB with GHC, a third; uncompressed, 1.8 MB against 5.5 MB. Performance is close on a desktop CPU (in-app navigation 158 ms against 125 ms, median), but startup is slower: the app is running after 0.89 s against 0.56 s, and about twice as long on a CPU throttled 4×. Details under Size and performance.
The PR also includes a browser runtime, the
browser_rtstarget with its small loadersrc/runtime/web/rts-web.js. The miso command line tool (https://github.com/haskell-miso/cli) uses it for hot reload inmiso dev: the runtime is built once with emcc, and after that every edit only recompiles the program to a.combfile (a few seconds, no C compiler), which the page fetches and runs. The tool builds a new runtime only when the program's foreign imports or its C and JavaScript files change, which it can tell from the foreign-import list thatmhswrites after the#####line of a.comb. It is close to the existingmhseval.js, with these differences:mhseval.js(already upstream)browser_rtsfopenof a.combin emscripten's in-memory file system, which the caller fillsfetchesapp.comb(or the script tag'sdata-comb); in node, readsMHS_COMB--embed-ffispackagesJSVal, callbacks,interruptibleimports,JSExceptionmain-O3-Oz -sSUPPORT_LONGJMP=wasm, 4M cell heapTwo related pieces of work are deliberately not part of this PR:
[js| ... |]with${expr}interpolation (Inline JavaScript:[js| ... |]with${expr}interpolation haskell-miso/MicroHs#2, branchjs-ffi-qq). The quasi-quoter needs something akin to ghcjs-base:ToJSVal/FromJSValtype classes to marshal the interpolated values. GHC's JavaScript backend has those; its WebAssembly backend does not, and this PR deliberately mirrors the WebAssembly backend. Including the quasi-quoter would mean adding those classes tobase(and so tobase.pkg) later.-sWASM=0, theemscripten_jsandbrowser_jstargets): splittingevaliso that V8 optimises it makes those builds 11 to 27 times faster (Make emscripten-sWASM=0builds 11-27x faster: split evali so V8 optimizes it haskell-miso/MicroHs#1, branchwasm0-speed). The WebAssembly targets do not need it.Claude's PR summary
Written by Claude.
foreign import javascript,JSVal, callbacks from JavaScript into Haskell, and a runtime that lives in the browser's event loop. This is what miso uses to compile browser applications with MicroHs; it has been in use there, and inmiso devof haskell-miso/cli, for a while on a fork, rebased onto currentmaster.Rebased onto current
master(9e1cd08). Excludinggenerated/(regenerated), the diff is 43 files, +2452 −117: about 1,500 lines of code, 700 of tests, 140 of README. The emscripten runtime is in a newsrc/runtime/jsffi.c(815 lines),#included bysrc/runtime/eval.c, with the declarationseval.cuses insrc/runtime/jsffi.h(37 lines);eval.citself is +207 −28, mostly under#if defined(__EMSCRIPTEN__).Contents
What it adds
Calling JavaScript. The code is inline, with the arguments as
$1,$2, ..., as in GHC's WebAssembly backend; an expression, or statements withreturn:Each import becomes an emscripten
EM_JSfunction in the generated C, so it is ordinary JavaScript in the output that emscripten's optimizer sees; nothing is compiled at run time. A$kthat is not an argument is a compile time error. Allowed types:Int,Word,Char,Bool,Double,Float,Ptr,JSVal, newtypes of these,()as a result.Holding JavaScript values.
JSValis aT_FORPTRwhose payload is an index into a table on the JavaScript side (Module.mhsjs), with a finalizer that drops the entry when the node is collected.freeJSValdrops it eagerly. Handle 0 isundefined; handles wrap around at 2^31.Callbacks.
foreign import javascript "wrapper sync"and"wrapper"turn a Haskell function of typeJSVal -> ... -> IO ()(orIO JSVal) into a JavaScript function;GHC.Wasm.Primwraps them assyncCallback/asyncCallback(0 to 3 arguments), the GHCJS names. A callback runs as a Haskell thread of its own. An uncaught Haskell exception in it is thrown to the JavaScript caller as anError.A program in the event loop. When every Haskell thread is blocked, the runtime returns to the JavaScript event loop; a callback, a timer (for
threadDelay) or a settling Promise resumes it. While callbacks exist the runtime stays alive aftermainreturns.threadDelay,MVarandforkIOwork unchanged.Promises. An
interruptibleimport is anasyncfunction. The calling thread waits for its Promise through anMVarthat the JavaScript side fills when the Promise settles; the other threads keep running. A rejected Promise is aJSException.Exceptions.
safeandinterruptibleimports turn a JavaScript exception into aJSException(new, inControl.Exception.Internal, raised by the runtime as theJSValnode itself;primIsIntreports it with a newrts_exncode, the same mechanism as the other RTS exceptions).unsafe, the default, treats one as fatal, as GHC's JavaScript backend does. The exception's description comes fromjs_exn_string, an ordinary C function (Ptr () -> IO CString) thatControl.Exception.Internalimports and decodes;js_free_jsvalis the same kind of function. Neither needs the compiler to know anything about the JavaScript runtime.Targets.
browser(WebAssembly,-Oz -sSUPPORT_LONGJMP=wasm, no file system),browser_js(-sWASM=0),emscripten_js, andbrowser_rts, all for programs, unlike the existingemscripten_web, which runs the compiler itself in a page and is untouched (mhs.conf.innow says which is which). Withbrowser_rtsthe runtime is built once, and the program is compiled to a.comband fetched at run time (src/runtime/web/rts-web.js, 40 lines). It is what makes a sub-second edit-reload loop possible withoutemcc; the.comb's#####foreign-import listing tells the tool when the runtime must be rebuilt.GHC.Wasm.Prim, 122 lines inbase:JSVal,JSString,JSException,toJSString/fromJSString,textToJSString/textFromJSString(UTF-8 with explicit lengths, so NUL and a leading U+FEFF survive),freeJSVal,jsNull/jsUndefined/isNull/isUndefined, and the callback functions. The name is GHC's so that code written for GHC's WebAssembly backend compiles; see the questions below.Design
No ASYNCIFY. A blocked Haskell thread keeps its state in the heap (
mt_root);cleanup()already clears the C stack on every reschedule. So "all threads blocked" is a state the runtime can simply return from:run_threads()returns tomhs_main, which callsemscripten_exit_with_live_runtime(), andmhs_js_run_threads()(called from a timer, after a callback, or when a Promise settles) continues them. The C stack ofmainis gone and nothing needs it.A callback is a thread, run to completion.
run_callback()creates a thread for the IO action, puts it at the head of the run queue, and evaluates it with a slice that is never exhausted (CALLBACK_SLICE), so no other thread runs under it. Called from the event loop (no Haskell thread on the C stack) it may block, in which case it is parked like any thread and JavaScript getsundefined. Called from inside the JavaScript of an import, or when it must return a value, it must not block: that is a fatal error, since a Haskell thread's C frames are below it.throwTofrom a callback cannot wait for delivery and is asynchronous there. The previous fork code ran callbacks on whatever stack was current with no current thread, which is whycatch,getMaskingState,forkIOandmyThreadIdread a null pointer in a callback; that is what this replaces.Why
setjmp-basedcatchforces-sSUPPORT_LONGJMP=wasmfor-Oz. With emscripten's JavaScriptlongjmp, every call out of a function containingsetjmpgoes through a JavaScript trampoline.-O3inlines most helpers intoevaliand pays a percent or two;-Ozdoes not inline and a miso application spent 84% of its time in trampolines. With WebAssembly exception handling (all browsers since 2022)-Ozmatches-O3and is about 12% smaller compressed.browser_jshas no wasm and stays-O3.Why
EM_JSand notEM_ASMor a--pre-js. The code of each import has to be visible to emscripten's optimizer so that definitions in embedded.jsfiles it calls are kept (#529made those files--pre-js; a directevalof a string would hide the uses).EM_JSgets the code into the output as a named function. The compiler passes the code to_EM_JSas a C string literal becauseEM_JSstringifies its body with#__VA_ARGS__, which requires valid C tokens; JavaScript with unbalanced quotes or#is not.Why the JSVal table and not the value in the heap. Only numbers cross the wasm boundary. This is also GHC's design (
JSValManager: aMapkeyed by integer, aFinalizationRegistryfor callbacks).A stopped runtime stays stopped. Every entry from JavaScript (callbacks, timers, settling Promises) goes through one wrapper that records an exception unwinding the C stack, and
mainitself runs inside such a frame (mhs_js_enter_main), so a wasm trap at any point marks the runtime as stopped; later calls into Haskell throw anErrorinstead of running on a corrupted heap. Anunsafeimport's JavaScript exception is fatal by the same route.What changes where
src/runtime/jsffi.c,jsffi.h#included byeval.c(it needs the evaluator's statics): the JavaScript side (mhs_js_init, 190 lines of JS in oneEM_JS), JSVal handles,run_callback,mhs_js_callback,mhs_js_settle,js_await(forT_IO_JSAWAIT),js_suspend(the event-loop return inrun_threads),mhs_js_run_threads,arm_timer,mhs_js_enter_main,js_exn_string/js_free_jsval(with native stand-ins),mhs_run_webandrun_comb,js_throwto/js_yield(throwTo and yield in a callback),js_main_done,mhs_to_HsStablePtr;jsffi.hdeclares whateval.ccallssrc/runtime/eval.cjsffi.c(each call site a line or two under#if;throwto,yield,pause_exec,MHS_INIT_ARGSand the end ofmhs_main_bodyare unchanged for other targets),T_IO_JSAWAIT/T_IO_JSAWAITRES,start_execsplit intorun_threads,main_finishsplit out ofmhs_mainso it can also run from the event loop,FP_JSVAL,exn_jsexception,rts_exn_msg/die_msgsplit out ofdie_exnsrc/runtime/mhsffi.hmhs_from_JSVal/mhs_to_JSVal/mhs_js_check_error,mhs_to_HsStablePtr,js_exn_string/js_free_jsvalsrc/MicroHs/FFI.hsEM_JSper import, the expression/statement decision,$kchecking, thewrapperimports,#if __EMSCRIPTEN__guards so native builds compilesrc/MicroHs/Desugar.hsjsKind, theinterruptiblewrapping (^f >>= IO.jsawait kind), module-qualified FFI numbersExpr.hs,Parse.hs,TypeCheck.hsSafetyis kept onForImplib/GHC/Wasm/Prim.hslib/Control/Exception/Internal.hsJSExceptionand its message (a small UTF-8 decoder overpeek_uint8);catchre-raises the translated exceptionlib/Primitives.hs,lib/base.cabalJSValmhs.conf.insrc/runtime/web/rts-web.jsbrowser_rtsloadertests/README.mdTwo changes are not emscripten-specific and are worth a look on their own:
mhs_<module>_<n>instead ofmhs_<n>), for every import whose entity is not a plain C identifier. The number is only unique within a module; two modules with inline-code imports collided. This changes generated C for existing programs with such imports.catchre-raises the translated exception (primRaise seinstead ofprimRaise einControl.Exception.Internal), so a handler of the wrong type passes on theSomeExceptionit was given, not the raw RTS value.What it deliberately does not do
mhs, and no way to run the JavaScript at compile time.JSVal;String/Textgo throughtoJSString, lists and records are a library's job..combof a program is not self-describing forbrowser_rts: the runtime's FFI table is fixed when it is built. A program with other foreign imports needs another runtime (--embed-ffis).Errorsaying the runtime has stopped, instead of running on a corrupted heap.Compatibility and cost for non-emscripten targets
jsffi.cis all under#if defined(__EMSCRIPTEN__)except the two native stand-ins. Ineval.ceverything is under#ifexcept: two newrts_exncodes andFP_JSVAL(enum values),main_finishbeing a function, themt_callbackflag onstruct mthread, andT_IO_JSAWAITerroring.gc_checkis unchanged for native code.foreign import javascriptcompiles and runs natively; the imports becomeT_BADDYNnodes and error only if called (tests/JSNative.hs).basegainsGHC.Wasm.PrimandJSException. Native programs that usecatchnow linkjs_exn_stringandjs_free_jsval, which exist as stand-ins in the native runtime, so nothing changes at run time.generated/mhs.candgenerated/base.pkgare regenerated; the compiler bootstraps to a fixpoint. The native test suite and the emscripten tests passed locally after the rebase; the later moves intojsffi.c/jsffi.hare checked by CI.Tests
JSValWordabove 2^31),JSStringround trips (NUL, U+FEFF), the three safety annotations,freeJSVal, GC of handlesJSCallback(19 cases)catch/mask/forkIO/myThreadIdin a callback; uncaught exceptions in sync and async callbacks;killThreadfrom a callback; a callback during anawait; use after free; value callbacks run once; the handle table stays bounded; parked callbacks; nested callbacks and their errors; anunsafeexception stopping the runtimeJSDeadlock,JSExportBlocked,JSExitTimer,JSTrapforeign exportwhilemainis blocked; exit with a thread still inthreadDelay; a wasm trap duringmainstops the runtimeBrowserRtsOtherbrowser_rtsForImpJS(extended),JSNative,JSFlag/JSBits(from #529, renumbered)A real application: haskell-miso.org built with this branch, the miso website built entirely by MicroHs: the client (
-tbrowser, oneapp.jswith the wasm embedded) and the 48 prerendered pages, written by the site's prerender program compiled with MicroHs for-temscriptenand run under node. The client hydrates the prerendered HTML; routing, the theme toggle, the CSS animations and the interactive demos work. Size and timings are under Size and performance.make runtestemscriptenruns them. The same tests also pass compiled for thebrowsertarget (-Oz, wasm SjLj) under node; the native core suite passes with the new library; and miso's integration tests pass built with MicroHs for bothbrowserandbrowser_js, in miso's CI (dmjio/miso#1694) and locally with emscripten 4.0.10 and 6.0.9.Size and performance
The same site, haskell-miso.org, built two ways: the production site (GHC 9.14 WebAssembly backend,
wasm-opt -O2, prerendered by GHC) and https://mhs-latest.netlify.app (this branch: client and prerendered pages both built by MicroHs). Both are prerendered HTML that the client hydrates.Size of the code the browser downloads:
app.wasm+ghc_wasm_jsffi.js+index.jsapp.js(wasm embedded)app.wasmalone)Performance, headless Chromium, cold cache, 10 loads per build and condition; navigation is 6 in-app clicks per load (
/docs/,/examples/,/blog/, twice), timed until the new page's heading is in the DOM and two frames have been drawn. The two sites are on different hosts (Netlify and production's), which affects first paint more than anything else here. In the plots,[==#==]is the middle half of the samples with#at the median, and the whiskers reach the minimum and maximum.Desktop CPU
Page errors: GHC 0, MicroHs 0.
CPU throttled 4× (about a mid-range phone)
Page errors: GHC 0, MicroHs 0.
In short: in-app navigation is within about 30 ms of GHC on a desktop CPU and about 1.4× on a slow one; startup, until the app is running, is about 1.6× on a desktop CPU and 2× on a slow one. The time goes into MicroHs evaluating a combinator graph where GHC runs compiled code; the FFI hot spots (string conversion, per-callback allocation, the
JSValtable) have not been optimised yet.Questions for review
These are the decisions I would rather have your view on than guess at:
It is 900 lines inDone, as suggested: it is now ineval.cunder#if.src/runtime/jsffi.c,#included fromeval.clikemd5.c(it needs the evaluator's statics, hence inclusion rather than a separate translation unit).eval.ccalls it only through a few plain C functions declared insrc/runtime/jsffi.h(js_init,js_enter_main,js_main_done,js_suspend,js_await,js_ffe_eval,js_throwto,js_yield,report_callback_exn), since anEM_JSfunction must be defined before its first use. Non-emscripten code paths ineval.cread as before.GHC.Wasm.Primis GHC's name and is what makes miso's FFI layer compile unchanged. If you would rather not haveGHC.*inlib/, anMhs.JS(or similar) withGHC.Wasm.Primre-exporting it works too.unsafe(the default) is synchronous and fatal on exception,safesynchronous withJSException,interruptibleasynchronous. GHC's WebAssembly backend makessafethe asynchronous one. The split here keeps "may this block" and "may this throw" independent, but it is a divergence from GHC that I want to be deliberate._EM_JS. The compiler uses emscripten's internal macro to pass the code as a string. It has been stable sinceEM_JSexists, but it is underscored. The alternative is a small header insrc/runtimedefining our ownMHS_EM_JSwith the same expansion, which pins the dependency in one place.Reviewing in parts
The runtime, the compiler and the targets can be read independently, in that order:
jsffi.c, theneval.candmhsffi.h): ineval.cthe hooks under__EMSCRIPTEN__, plusmain_finish,run_threads,mt_callback, the enums. Testable through the existingforeign export javascriptwithout the new syntax.FFI.hs,Desugar.hs,Expr.hs,Parse.hs,TypeCheck.hs,GHC.Wasm.Prim,Control.Exception.Internal).mhs.conf.in,rts-web.js, README).If splitting into three PRs is easier for you, the commits are already arranged so that each part builds on its own.
🤖 Generated with Claude Code