Repository navigation
Conversation
10 of 15 tasks
dmjio
force-pushed
the
jsval
branch
3 times, most recently
from
October 14, 2026 20:08
d8bd2fd to
f2ec3f8
Compare
dmjio
force-pushed
the
jsval
branch
9 times, most recently
from
October 29, 2026 17:06
088de4d to
e358919
Compare
dmjio
force-pushed
the
jsval
branch
2 times, most recently
from
October 31, 2026 19:30
e079539 to
556acfc
Compare
dmjio
added a commit
to dmjio/miso
that referenced
this pull request
Nov 1, 2026
1a0ce4ba adds one commit to 556acfc4: two hlint suggestions fixed, one in upstream's Tools/Tester.hs and one in Control.Exception.Internal, with tester.c and base.pkg regenerated. It is the head that passed all 26 CI checks on haskell-miso/MicroHs#5, and that haskell-miso.org was rebuilt and checked with (miso 3f4956b). Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
dmjio
added a commit
to dmjio/miso
that referenced
this pull request
Nov 1, 2026
* MicroHs: bump to jsval 556acfc4 haskell-miso/MicroHs jsval was rebased onto upstream master (d756fa92) and gained, since 5fc13363: a trap while main runs marks the runtime as stopped; a value callback returning a freed JSVal is the caller's error, not a dead runtime; js_exn_string and js_free_jsval are ordinary C functions, so the library builds with any mhs; ? is escaped in the generated C string of a JavaScript import; the quickjs target is gone; and runtest no longer needs ghc. Nothing on the miso side changes. 5fc13363 is no longer on a branch after the rebase, hence the bump. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> * MicroHs: bump to jsval 1a0ce4ba 1a0ce4ba adds one commit to 556acfc4: two hlint suggestions fixed, one in upstream's Tools/Tester.hs and one in Control.Exception.Internal, with tester.c and base.pkg regenerated. It is the head that passed all 26 CI checks on haskell-miso/MicroHs#5, and that haskell-miso.org was rebuilt and checked with (miso 3f4956b). Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
Member
Author
dmjio
force-pushed
the
jsval
branch
3 times, most recently
from
November 8, 2026 21:51
7635597 to
4de3387
Compare
Make README.md a short introduction with installation instructions, an example, and a link to the full documentation. Add doc/MicroHs.md with the full documentation, assembled from the old README, the wiki, and the source: Language, Compiler (flags, interactive mode, targets), Packages, Implementation (compiler and runtime), Platforms, and Contributing. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
uname -m gives the kernel's architecture, so a 32 bit x86 program on an x86_64 kernel (e.g., the nightly's 32 bit Alpine job) got arch "x86_64" and the CPP symbol x86_64_HOST_ARCH, where GHC would say i386. With 32 bit words, x86_64, aarch64, and powerpc64 now become i386, arm, and powerpc. The compiler's CPP symbols only change after a regen; until then the CppHost test fails on such a system. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The runtime raises it as exception number 10 (exn_nontermination), which rtsExn turns into NonTermination. Control.Exception exports it, and nonTermination, as GHC does. Nothing raises it yet. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
One failing architecture cancelled the rest, so a failure on, e.g., riscv64 hid how the other architectures were doing. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
With -sWASM=0 (JavaScript only) binaryen inlines evalprimop and
evalprimret into evali, since each has a single caller, in spite of
NOINLINE. evali is then 233KB of JavaScript, which V8 refuses to optimize
("Function is too big to be optimized"), so the core of the evaluator runs
in V8's lower tiers.
Under emscripten, evali now calls them through volatile pointers, which
cannot be inlined. evali is then 25KB and V8 optimizes it. nfib 27 under
node takes 0.9s instead of 4.7s. The wasm build is as fast as before,
and other targets compile to the same code as before.
dmjio
force-pushed
the
jsval
branch
2 times, most recently
from
November 24, 2026 01:00
21470ec to
1e2622a
Compare
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Without it the order of the output could depend on where stdout goes. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A .combffi file now has two sections after the program: #####FFI, with the foreign imports (the separator was #####), and then #####Names, with the name of each definition after its label. Mhs.Reflect: * hReflect reads a combinator file, possibly compressed, from a handle. * The names of the definitions are used for the labels when the file has them. * The A d1 (A d2 (... e)) that ends a file written by mhs is reduced to e. * Operator names, e.g. Data.Function.., are printed in parentheses. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Consistent indentation, the code in commented sections, and no duplicated code (one resize listener, one function to save the file). Fixes: * New cleared the editor through window.editor, which is never set. * The sync conflict warning used term.writeFile, which does not exist. * stderr now goes to the terminal like stdout, as UTF-8 and with \r\n. * Run uses the qualified main of the module, since the earlier imports may have a main too, e.g. Demo.main. * A typo in line_edit. The trailing space of the prompt is written as \x20, so that it is not lost when trailing white space is removed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The handler of a catch restores the masking state. It was taken when CATCHR ran, but a rescheduled thread starts over from its root and runs CATCHR again, maybe inside a mask. Then the handler restored the wrong state, and the thread stayed masked. This happened in the web REPL when interrupting a canvhs animation: drawing uses bracket_, and the interrupt comes from another thread. After the first CTL-C the REPL was masked, so the second CTL-C was never delivered. Now CATCH x y z becomes CATCHR m (x z) y z, where m is the masking state. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
With -r the program runs in the same thread as the compiler, in the middle of a time slice, so its thread switches depended on how much of the slice the compiler had used, e.g., if the compilation cache was used. Yielding first gives the program a new time slice, as it gets when it runs on its own. So the MVar test no longer needs a yield. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…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. - foreign import javascript: the code is inline, with the arguments as $1, $2, ...; each import becomes an MHS_EM_JS function (mhsffi.h, the expansion of emscripten's _EM_JS), so emscripten's optimizer sees what the code uses. Whether the code is an expression or statements is decided at compile time (jsIsStatements); strings, comments and regular expression literals are skipped. - The safety annotations follow GHC's WebAssembly backend: an unsafe import is synchronous and a JavaScript exception in it is fatal; safe, interruptible and unannotated imports are asynchronous (the calling thread waits for the Promise while the others run), and a rejected Promise is a JSException. An asynchronous import must return IO. - 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. - 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. - The emscripten runtime is in src/runtime/jsffi.c, included by eval.c. The core of evali has no JavaScript hooks: IO.jsawait and IO.jsawaitres are in evalprimop. - Targets browser, browser_js, emscripten_js, and browser_rts: a runtime built once that fetches and runs a combinator program, so a rebuild needs no C compiler. - A module that uses the JavaScript FFI still compiles for a native target (tests/JSNative.hs). - Foreign import C names are module-qualified, since the import number is only unique within a module. - Tests: JSVal, JSCallback, JSDeadlock, JSExportBlocked, JSExitTimer, JSTrap, BrowserRtsOther, ForImpJS, JSNative, Error112. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds a JavaScript FFI to MicroHs for the emscripten target, in the style of GHC's WebAssembly backend, together with the runtime changes that JavaScript callbacks and the browser need. The goal is to compile browser applications (miso in particular) with MicroHs.
masteris at upstreamaugustss/MicroHsmaster (d756fa92, "More testing fixes") andjsvalis rebased onto it, so this PR is exactly the fork's 22 commits:0cbadda8JavaScript FFI for the emscripten target: JSVal, callbacks, browser targets622113d2JavaScript FFI: fixes from review3c131b43config.h: imath only without GMP20ea518druntest: depend on the compiler it runs22842268JavaScript FFI: drop the threadDelay timer when the runtime stops2733fd0aJavaScript FFI: pass Word arguments unsigned4bb6bdf7GHC.Wasm.Prim: keep a leading U+FEFF when converting a string to JavaScriptedbd0d40Regen589d412eDrop the quickjs target3829cfd4Define main_finish after mhs_main, where its body came from9b3b623etests/JSExportBlocked: print the result as one string5ea466a3README: structure the JavaScript FFI sectioneeaba3c1JavaScript FFI: a trap while main runs marks the runtime as stopped218ecd47JavaScript FFI: a freed result is the callback's error, not the runtime'se1b02c7bFFI: escape ? in the C string of a JavaScript import7aab5811JavaScript FFI: js_exn_string and js_free_jsval are ordinary C functions5459c2eaRegen40955527Control.Exception.Internal: guards in peekUTF8 (hlint)23a6ddbeREADME: traps stop the runtime too; a freed callback resultf36f60e1Regen556acfc4mhs.conf.in, README: the two families of emscripten targets1a0ce4bahlint: [prog] in Tester.parseTests, otherwise in peekUTF8Excluding
generated/mhs.candgenerated/base.pkg(regenerated, see Notes for merging), the diff against upstream is 42 files, +2358 −121; without the tests and the README it is 16 files, +1523 −106, of whichsrc/runtime/eval.cis +975 −30.Contents
GHC.Wasm.Primbrowser_rts)What it solves
Calling JavaScript from Haskell. Before this, a MicroHs program compiled with emscripten could only reach JavaScript through C. Now JavaScript code is written inline in a
foreign import javascript, with the arguments as$1,$2, ..., as in GHC's WebAssembly backend:Holding JavaScript values in Haskell. A
JSValis a first class Haskell value that refers to a JavaScript value (a DOM node, a function, a string, ...). The JavaScript value stays alive as long as the Haskell value does, and is released when theJSValis garbage collected, or right away withfreeJSVal.Calling Haskell from JavaScript. Haskell functions can be turned into JavaScript functions (
syncCallback,asyncCallback,"wrapper"imports), so they can be used as event handlers, timers, and Promise continuations.A program that lives in the event loop. A browser application does not end when
mainreturns, and it cannot block the browser's thread. The runtime returns to the JavaScript event loop whenever all Haskell threads are blocked, and is resumed by a callback, a timer, or a settling Promise.threadDelay,MVars andforkIOwork in that setting. No C stack has to be preserved for this (Haskell threads keep their state in the heap), so emscripten's ASYNCIFY is not needed.Awaiting Promises. An
interruptibleimport is anasyncfunction. The Haskell thread that calls it waits for the Promise while the other Haskell threads keep running, and a rejected Promise is a Haskell exception.Exceptions across the boundary, in both directions. A JavaScript exception in a
safeorinterruptibleimport is raised in Haskell as aJSException. An uncaught Haskell exception in a callback is thrown to the JavaScript caller as anError(an asynchronous callback rejects its Promise).Source compatibility with GHC. The module is called
GHC.Wasm.Primand follows GHC's API, with the GHCJS style callback functions added. The intent is that code written for GHC's WebAssembly or JavaScript backends, such as miso's FFI layer, needs few changes.JSStringhas noEq/Showinstances, like GHC's, so a library that defines its own does not get duplicate instances.Output for the browser. New targets produce a single file for the browser, with or without WebAssembly, and for node. The
browsertarget is compiled for size without losing speed (see Targets).A runtime that runs programs without a C compiler. The
browser_rtstarget builds the runtime once; a program is then compiled to a combinator file in seconds and fetched at run time (see The browser runtime). This is whatmiso devin haskell-miso/cli uses on every save.Code that also has to compile natively. A module that uses the JavaScript FFI still compiles for a native target: the JavaScript code is left out and nothing JavaScript specific reaches the C compiler (
tests/JSNative.hs). So one code base can have a native build, e.g. for a server or for tests.What it does not solve
It is emscripten only.
foreign import javascriptdoes something only with the emscripten targets. There is no JavaScript FFI for a compiler that itself runs natively and no way to run the JavaScript code at compile time. (On the2lttbranch aforeign import javascriptis for this reason the one thing that is object level only.)Only a few types cross the boundary. Arguments and results are
Int,Word,Char,Bool,Double,Float,Ptr,JSValand newtypes of these, with()as a result. There is no automatic conversion ofString,Text, lists, records,Maybe, or 64 bit integers. Strings go throughtoJSString/textToJSString, which copy via UTF-8. Everything else is the job of a library on top (in miso, itsToJSVal/FromJSValclasses).Callbacks take and return
JSValonly. A"wrapper"import wraps a function of typeJSVal -> ... -> IO ()orIO JSVal. The helper functions cover zero to three arguments.A callback cannot always block. A callback called from the JavaScript event loop may block (it then waits like any other thread, and its JavaScript caller gets
undefined, or an already resolved Promise for"wrapper"). But a callback that returns a value, one that is called from inside the JavaScript code of an import, and aforeign exportcalled from JavaScript, must not block; that is a fatal error. There is no way to check this at compile time.unsafeis the default, and it is unforgiving. Unlike the Haskell report, an import without a safety annotation isunsafe, and a JavaScript exception in anunsafeimport is fatal (as with GHC's JavaScript backend). Writesafeto get aJSExceptioninstead.A fatal error stops the Haskell side for good. After a fatal runtime error, or a JavaScript exception from an
unsafeimport inside a callback, the runtime is stopped. In the browser the page lives on, and every later call into Haskell throws anErrorsaying that the runtime has stopped. There is no restart.Memory is tied to the Haskell garbage collector. A JavaScript value referenced by a
JSValis only released when the Haskell GC finds theJSValdead, so JavaScript memory can stay around until the next Haskell collection. The browser targets use a 4M cell heap (about 60MB, instead of the default 50M cells) and force a collection after many new handles so that this happens regularly; a program that needs a bigger heap has to set-optc -DHEAP_CELLS=N. A callback is released when itsJSValis collected or freed.browser_rtsfixes the foreign imports when the runtime is built. A program whoseforeign imports are not in the runtime's FFI table needs a runtime built for it (--embed-ffis, or built from that program). The.combfile lists its imports after a#####line, so a tool can tell when that is.Not in this PR. Inline JavaScript with
[js| ... |]is PR #2, and the speedup for-sWASM=0builds is PR #1; both targetjsval. There is no DOM library here: this is the FFI that such a library is written with. Aquickjstarget was dropped: it was never run, and a program usingthreadDelayhangs on it, since the runtime waits in asetTimeoutandqjshas none.The
browsertarget needs WebAssembly exception handling, which browsers have had since 2022. Older browsers need thebrowser_jstarget.browser_jsandemscripten_jswere not run. All the testing here and in CI uses WebAssembly (emscriptenunder node,browserunder Chromium through miso's test suite, see below). The-sWASM=0targets were built on the fork before the rebase, not since.The language feature
EM_JSfunction,mhs_js_<module>_<n>, with the parameters$1,$2, ..., so it is ordinary JavaScript in the output, with nothing compiled at run time. It can be an expression, or statements that usereturn. The compiler decides which: statements if, outside brackets, strings and comments, the code has a;or the wordreturn(not as a property name), or starts with a statement keyword (if,let,throw, ...); otherwise an expression. Code that starts withfunctionorclassis an expression unless more code follows it.$kthat is not an argument is a compile time error. In particular$0: the arguments start at$1, as in GHC (they started at$0on the fork before).Moduleobject (e.g.Module.UTF8ToString($1)) andmhsjs, the table ofJSVals.JSValargument arrives as the JavaScript value it refers to; aJSValresult is entered in the table. AWordargument arrives as an unsigned number (a wasm i32 is signed by default).The safety annotation selects what happens on a JavaScript exception, and whether the code can wait:
unsafe(the default)safetry/catchJSExceptioninterruptibleasyncfunction, so it can useawaitJSException; so is a rejected PromiseWith
interruptiblethe calling Haskell thread waits for the Promise and the other Haskell threads keep running.safeandinterruptibleimports also catch exceptions from the conversion of the arguments (e.g. aJSValthat was freed).Two import strings make callbacks instead of calling JavaScript:
"wrapper sync"turns a Haskell function into a JavaScript function that calls it and returns when it is done."wrapper"does the same, but the JavaScript function runs the Haskell code later, from the event loop, and returns a Promise.In the compiler this touches the parser,
Expr.hs, the type checker (the allowed types),Desugar.hs, and mostlyFFI.hs, which generates oneEM_JSfunction per import into the C output. The code is passed to_EM_JS(what theEM_JSmacro expands to) as a C string, so it does not have to consist of valid C tokens.Primitives.hsgets theJSValtype. The C name of a foreign import is now qualified with its module (mhs_<module>_<n>), since the number alone is only unique within a module; this affects all foreign imports whose entity is not a plain C identifier, not only JavaScript ones.GHC.Wasm.PrimA new library module (122 lines), in
base:The string conversions go through UTF-8 with explicit lengths, so a NUL character in the text survives, and decode with a single
TextDecodercreated withignoreBOM, so a leading U+FEFF survives as well (a default decoder strips it).JSExceptionis defined inControl.Exception.Internal, since the runtime raises it. The two runtime functions the library needs,js_exn_string(the description of a JavaScript exception, a malloc()ed UTF-8 C string) andjs_free_jsval, are ordinary C functions with C types, declared inmhsffi.hand imported with plainforeign import ccall; theJSValis passed as the payload of its foreign pointer, i.e. the handle. So the library builds with anymhs, upstream's included, with no runtime-specific knowledge in the compiler.Callbacks and the scheduler
This is the largest part of the change:
src/runtime/eval.cgrows by about 900 lines.catch,getMaskingState,forkIOandmyThreadIdread a null pointer (which happens to be readable in wasm, so it appeared to work). A callback now runs as a thread of its own, first and without preemption; when it runs out of its reduction budget it gets a new one. Aforeign exportcalled from JavaScript while the threads wait in the event loop runs the same way (it used to run as a second main thread, which ended the program when it finished).mainincluded, e.g. intakeMVarwaiting for a callback, or in aninterruptibleimport), the runtime flushes stdout and returns to the JavaScript event loop, and waits there until a callback wakes it or the earliestthreadDelayexpires. It used to poll withemscripten_sleep(1).main. When a program has live callbacks the JavaScript runtime stays alive aftermainreturns, so the callbacks can still be invoked. Haskell threads keep running aftermainor a callback finishes. The program ends when themainthread finishes and no callback is left; the runtime counts the live callbacks, so a deadlock is reported again once they have all been freed. When the program exits, the timer armed for athreadDelayis dropped (it used to fire into the stopped runtime).JSValhandles wrap around instead of overflowing a Cintafter 2^31 allocations.Embedded JavaScript files
A
.jsfile on the command line, or in thejsbitsdirectory of a package, is embedded in the output with--pre-js(this part is upstream, augustss#529). Aforeign import javascriptcan call what such a file defines:At
-O3emscripten removes definitions in a--pre-jsfile that it sees no use of. Since the code of each import is anEM_JSfunction in the output, the optimizer sees that the import callsjsFlagAddand keeps it; the embedded files are still minified and what nothing uses is still removed.(Before the EM_JS change, the code was a string compiled at run time with a direct
eval. That needed a workaround,FFI.jsIdents, which mentioned each import's identifiers in a function that was never called so the optimizer would keep them. Both are gone.)The alternative, embedding with
--extern-pre-js, also works, but then the files are not optimized at all. It is still a matter of configuration: the option is thejskey of the target inmhs.conf.The tests
JSFlagandJSBitscame from upstream with augustss#529, where arguments are numbered from$0; on this branch they use$1,$2.Targets
New sections in
mhs.conf.in, next to the existingemscripten:emscriptenemscripten_js-sWASM=0), for nodebrowser-Oz -sSUPPORT_LONGJMP=wasm, 4M cell heapbrowser_rtsbrowser, but the program is fetched at run time-sINVOKE_RUN=0, see belowbrowser_js-O3, 4M cell heapThese are targets for programs. Upstream's existing
emscripten_webis a different thing, the compiler itself running in a web page (web-mhs/, ASYNCIFY for typed input); it is left as it is, andmhs.conf.innow says which is which.Why the
browsertarget is compiled the way it is: the evaluator contains asetjmp(forcatch). With emscripten's default JavaScript implementation oflongjmp, every call out of a function that usessetjmpgoes through a JavaScript trampoline. At-O3most helpers are inlined and this costs a percent or two; at-Oz, which does not inline, a miso application spent 84% of its time in those trampolines instead of 22%. With WebAssembly exception handling forlongjmpthe-Ozbuild is as fast as-O3and about 12% smaller compressed.browser_jshas no WebAssembly, so it stays at-O3. (These measurements were made on the fork before the rebase.)The standalone runtime compiled to JavaScript (
make rts.js, thennode rts.js prog.comb) is not in this PR; it is one commit on therts-jsbranch.The browser runtime (
browser_rts)The
browsertarget compiles the runtime and the program together, which means runningemcc(an-Ozlink takes a minute) for every change of the program.browser_rtsseparates the two:rts.jsis the runtime with-sINVOKE_RUN=0;src/runtime/web/rts-web.js(40 lines, installed with the runtime sources) fetchesapp.comb(orModule.combUrl, thedata-combattribute of the script tag, or in node theMHS_COMBenvironment variable) and hands it tomhs_run_web, which runs it instead of the compiled in program. The runtime's FFI table has the foreign imports of the program it was built with, plus those of the--embed-ffispackages. Upstream'smhseval.jsdoes the node half of this (a.combgiven as a file argument); this adds the fetch, the FFI table, and the keep-alive runtime of this PR, so callbacks and timers work in the fetched program.miso devin haskell-miso/cli uses this on every save of atarget: browserproject: it keys the runtime by the program's foreign imports (read from the#####section of the.comb) and its C and JavaScript sources, builds it once per key, and compiles onlyapp.combafterwards.The test
testbrowserrtsruns it under node (thereadFileSyncpath); the browserfetchpath has no test and is exercised bymiso dev.Fixes from review
622113d2, from the first round:$kin the code that is not an argument, in particular$0, is a compile time error instead of an undefined variable at run time.functionorclassis a function or class expression unless more code follows;returnas a property name no longer makes the code statements.foreign exportcalled while the threads wait in the event loop runs as a thread of its own.JSDeadlock,JSExportBlocked.22842268,2733fd0a,4bb6bdf7, from the second round (each with a test):threadDelaytimer fired after the runtime had exited. Ifmainfinished (woken by a callback or a Promise) with no live callback while another thread was inthreadDelay,exit()only threwExitStatusto the caller (the runtime is kept alive byemscripten_exit_with_live_runtime) and the armed timer stayed. When it fired, the "runtime has stopped"Errorwent into a baresetTimeoutcallback: an uncaught exception in node (exit status 1 after waiting out the delay), a console error in a browser. Stopping the runtime now clears the timer, andrunThreadsdoes nothing once the runtime is dead. TestJSExitTimer.Wordarguments at or above 2^31 arrived negative. EM_JS parameters are wasm i32s and emscripten does no unsigned conversion;$1on0xFFFFFFFFgave-1. They are converted with>>> 0, asBoolandJSValarguments are converted.toJSString.js_fromUTF8decoded with a defaultTextDecoder, which strips a byte order mark. It decodes withignoreBOM, and the decoder is created once instead of per call. (miso'sData.JSStringhad a workaround that re-inserted the mark; it is removed in MicroHs: bump to jsval 5fc13363 and drop the U+FEFF workaround dmjio/miso#1691, which also pins this branch.)589d412e,3829cfd4,9b3b623e,5ea466a3: thequickjstarget dropped (see above),main_finishdefined aftermhs_mainso the diff shows its body in place instead of moved,JSExportBlockedmade independent of node's console colouring, and the README's JavaScript FFI section restructured (subsections and tables, and the facts of this branch).eeaba3c1,218ecd47,e1b02c7b,7aab5811, from the third round (a review of the whole branch with upstreaming in mind):mainran left the runtime looking alive. Only entries throughModule.mhsjs.enterand anunsafeimport's catch recorded that the runtime had stopped; a trap duringmainitself (an out of bounds read, the evaluator exceeding the stack) went to emscripten'scallMain, so in the browser the next DOM event ran its callback on a corrupted heap.mainnow runs inside a JavaScript frame (mhs_js_enter_main) that marks the runtime stopped for any exception except emscripten'sunwind, then rethrows. TestJSTrap.mkCallbackfetched a value callback's result insideenter, so a callback returning aJSValit had freed madeenterstop the runtime for good although nothing had unwound. The result is taken right afterenter, and an error there is thrown to the caller. TestJSCallback20.?is escaped in the generated C string, so JavaScript such asx ??(y)cannot form a trigraph under a strict C mode.js_exn_stringandjs_free_jsvalare ordinary C functions (seeGHC.Wasm.Prim) instead of hand-written FFI adapters listed in the compiler'sruntimeFFI. Before, a compiler without them in its list, upstream's or any older one, generated stubs for them and failed onIO String; the library could only be built by a compiler already regenerated with this branch.Control.Exception.Internaldecodes the UTF-8 message itself with thepeek_uint8andfreeruntime entries.JSValtests a non-ASCII message.What is already upstream
Parts of the fork's work have been merged into
augustss/MicroHsand are therefore not in this diff any more:.jsfile on the command line, and thejsbitsdirectory of a package, are passed to the C compiler with the target'sjsoption (--pre-jsfor emscripten). This is Add.jsFILE suffix detection to embed JavaScript files in the output augustss/MicroHs#529.Data.Textadditions.lib/Data/Text.hson this branch is identical to upstream's.Data.Text.Encodingis left as upstream has it: Data.Text: UTF-8 primitives in the runtime augustss/MicroHs#557 already stores NUL as C0 80 indecodeUtf8and turns it back into 00 inencodeUtf8.throwTofixes (throwTo: do not return until the exception is raised in the target augustss/MicroHs#534):throwTodoes not return until the exception is raised in the target (tests/ThrowToSync.hs), and killing many runnable threads is no longer quadratic (5,000 threads: 35 s to 1.9 s).Tests and what was run
New tests:
JSValWordabove 2^31),JSString(including a leading U+FEFF and a NUL), the three safety annotations, a non-ASCII exception message,freeJSValJSCallbackcatch/mask/forkIO/myThreadIdin a callback; an uncaught exception in a synchronous and an asynchronous callback;killThreadof a delayed thread from a callback; a callback during anawait; use afterfreeJSVal; a value returning callback runs once; the handle table stays bounded; a callback left blocked; nested callbacks and their errors; a value callback returning a freedJSValJSDeadlockJSExportBlockedforeign exportcalled whilemainis blocked runs as its own thread; the program ends whenmaindoesJSExitTimermaindoes although a thread is still inthreadDelayJSTrapmainruns stops the runtime: a callback called later is refused (exit status 1)BrowserRtsOtherbrowser_rts+ nodeJSValrunsJSVal.comband a program it was not built withForImpJS(extended)JSNativeText,Unboxed(extended)Data.Text.toTitle;(##)and operators that start with#make runtestemscriptenrunstestforimpjs testjsval testbrowserrts testjscallback testjsdeadlock testjsexportblocked testjsexittimer testjstrapin addition to the existing tests. Before, CI ran no test of the JavaScript FFI itself.Run on the final commits:
54f863ec(12 build jobs plushlint, each for push and pull_request). The branch was then rebased onto upstreamd756fa92(four upstream commits, all test infrastructure, plus aWANT_GMP/WANT_IMATHguard ineval.cthat the branch'sconfig.halready satisfies) and regenerated; the native core suite,JSNative, and every JavaScript test target were rerun on the rebased tree.generated/mhs.candgenerated/base.pkgare from that bootstrap, at version 0.16.12.0. From the branch's previousgenerated/mhs.cthe regeneration took three stages once (that compiler emitted no stub forjs_exn_string, the one it builds does, and the next one is the fixpoint). The library no longer needs a compiler that knows about the JavaScript runtime: upstream'smhsbuilds it.tester all.tests) andJSNativepass with the new library and runtime, on Linux.testjsflagandtestjsbits. The review fixes were each shown to fail on the previous head and pass on this one.JSVal,JSCallback,JSExitTimer,JSExportBlockedandJSDeadlockalso pass compiled for thebrowsertarget (-Oz, wasm SjLj) and run under node.browsertarget, in a browser: miso's component test suite (tests/app, 343 tests) built with this MicroHs for-tbrowserand run headless in Chromium through miso'sts/playwright.ts, with emscripten 4.0.10 and with 6.0.9, with the miso side of the U+FEFF fix applied: 343 passed with both. That suite exercisesunsafe,safeandinterruptibleimports, callbacks from DOM events,await, and exceptions in both directions. The same was then built and run through miso's Nix expressions (nix-build -A miso-tests-mhs-bundle,playwright-mhs).make runtestmhs, and the JavaScript tests of that time.Not run: the whole test suite under emscripten locally (CI's emscripten job does that), and the
browser_jsandemscripten_jstargets since the rebase.Notes for merging
runtestno longer needs ghc (20ea518d): its recipe runs../bin/mhs(tests/Makefile's default) but it depended onbin/gmhs, somake everytestmhsfailed on a runner without ghc; upstream's nightlybuild-macosjob fails this way today (make: ghc: No such file or directory). This is an upstream fix, not fork-only.masterwill differ from upstream. Todaymasteris a copy ofaugustss/MicroHsmaster. After this merge, syncing with upstream is a merge or a rebase; on a conflict ingenerated/mhs.ctake either side and bootstrap again.MICROHS_REVto5fc13363inMakefile.mhsandnix/source.nix. After this rebase that commit is on no branch (its content isedbd0d40now); GitHub still serves it for the time being, but miso should be bumped to a commit on the currentjsval, or tomasteronce this merges.[js| ... |]with${expr}interpolation #2 targetjsval, which was force pushed for the rebase. They need a rebase onto the currentjsval, or a retarget tomasterafter this is merged.generated/mcabal.cis the fork's copy, which has thejs-sourcessupport. It was not regenerated (that needs the MicroCabal source) and it predates upstream's version bump.lib/base.cabalgetsGHC.Wasm.Prim.augustss/MicroHsis three PRs in dependency order: the runtime (callbacks as threads, the event loop,JSVal,jsawait), the compiler and library (foreign import javascript,GHC.Wasm.Prim), and the targets withbrowser_rts.