Repository navigation
throwTo: do not return until the exception is raised in the target - #534
Merged
Merged
Conversation
GHC's throwTo (and so killThread) does not return until the exception has been raised in the target thread. MicroHs handed the exception over and returned at once, so the target stayed runnable with the exception pending and could still run, and consume shared state, before it was interrupted. For example, miso's tests killed the scheduler thread of one app, and the old scheduler could then take the redraw notification of the next app. Now throwTo waits on the read queue of the target's exception MVar until the target takes the exception (check_thrown), or finishes or dies. The wait is itself interruptible, since thread_intr searches that queue. A thread that throws to itself does not wait. Since throwTo waits for the target, killing a runnable thread could park the killer twice at the tail of the run queue, which made a loop of killThread quadratic. So throwTo moves a runnable target to the head of the run queue, and the woken thrower goes right after it. tests/ThrowToSync.hs: killing a runnable, a blocked and a masked thread, killing a dead thread, and throwTo to ourselves.
throwTo: do not return until the exception is raised in the target
…owTo Two problems found in review: A second throwTo to a thread waits for the thread's exception slot, which the first throwTo has filled. If the thread finished or died without taking the first exception, nothing released the second thrower, and it waited forever. Now, when a thread finishes or dies, every thread waiting to throw to it is released (release_throwto); each runs throwTo again, finds the thread gone, and returns. A throwTo that was interrupted while it waited still delivered its exception, because the exception stayed in the target's slot. In GHC an interrupted throwTo has no effect. Now a thread that takes an exception while it waits in throwTo takes its own exception back from the target, if the target has not taken it (revoke_throwto). ThrowToSync tests both.
augustss
reviewed
Oct 5, 2026
augustss
reviewed
Oct 5, 2026
augustss
reviewed
Oct 5, 2026
augustss
reviewed
Oct 5, 2026
Use them for the run queue insertions in wake_throwto and throwto, instead of doing the list manipulation inline.
dmjio
added a commit
to haskell-miso/MicroHs
that referenced
this pull request
Oct 5, 2026
Squash of the fork's work before the 2026-09-21 upstream merge, rebased
onto upstream master:
- Compile rts standalone, intro demo script.
- Add JSVal and a JavaScript FFI for the emscripten target
- Add browser and JavaScript-only emscripten targets
- Embed JavaScript files in the output: -js FILE, js target key, package jsbits
- Regenerate mcabal.c: query the mhs version without the package path flag
- Yield to the JavaScript event loop when all threads are blocked; more Data.Text functions
- Keep running Haskell threads after main or a callback finishes (emscripten)
- Browser targets: 4M cell heap; force a GC after many new JSVal handles
Run JavaScript callbacks as Haskell threads; fix exception and waiting issues
A callback from JavaScript used to run with ffe_eval on whatever stack was
current, so there was no current thread: catch, getMaskingState, forkIO and
myThreadId all read runq.mq_head, which is null while main is blocked in
pause_exec or awaiting a Promise (it only appeared to work because address 0
is readable in wasm). Callbacks now run as a thread of their own
(run_callback), first and without preemption; blocking in a callback is a
clean error instead of a longjmp out of a JavaScript frame.
An uncaught Haskell exception in a callback used to longjmp past the
JavaScript caller into the scheduler. It is now converted to a message and
thrown to the caller as a JavaScript Error (an asynchronous callback rejects
its Promise).
pause_exec waited by polling with emscripten_sleep(1) forever; it now waits
for the JavaScript event loop (mhs_js_wait_event) until a callback wakes it
or the earliest threadDelay expires, and says what to do when ASYNCIFY is
not enabled instead of reporting a deadlock.
Also: JSVal handles wrap around instead of overflowing the C int after 2^31
allocations; an interruptible import raises a JSException instead of an
emscripten assertion when it would nest inside another asynchronous
operation; safe/interruptible imports also catch exceptions from the
argument conversion (e.g., a freed JSVal).
tests/JSCallback.hs covers these: catch/mask/forkIO/myThreadId in a
callback, an uncaught exception in a synchronous and an asynchronous
callback, killThread of a delayed thread from a callback, a callback during
an await, use after freeJSVal, and the handle table staying bounded.
Remove lib/TAGS and rts-js-demo.sh, which should not have been committed,
and import Data.Text qualified in Data.Text.Encoding so that new Data.Text
exports cannot clash.
GHC.Wasm.Prim: no Eq/Show instances for JSString, like GHC; Data.Text.toTitle
GHC's GHC.Wasm.Prim defines no instances for JSString, so a module that
defines its own (e.g. miso's Data.JSString, which has Eq, Ord, Show,
Semigroup, Monoid and IsString) got duplicate instances with MicroHs.
The JSVal test converts with fromJSString before comparing or printing.
browser target: compile with -Oz -sSUPPORT_LONGJMP=wasm
The evaluator contains a setjmp (for catch), and with emscripten's default
JavaScript implementation of longjmp every call out of a function that uses
setjmp goes through a JavaScript trampoline. At -O3 most helpers are
inlined so this costs a percent or two, but at -Oz (which does not inline)
a miso app spent 84% of its time in those trampolines instead of 22%.
With WebAssembly exception handling for longjmp the -Oz build is as fast as
-O3 and about 12% smaller compressed. browser_js has no WebAssembly, so it
stays at -O3.
Fix the result of a JavaScript callback that returns a value
run_callback built the callback thread's root as ((performIO act) World),
which applies the result of the action to World, and it never wrote the
result back to the stack. A callback declared to return a JSVal therefore
got its value by evaluating the performIO application a second time, so its
action ran twice, and a callback returning () evaluated a bogus application.
The root is now what performIO builds, ((act World) K), and the result is
left at TOP(0) for the caller.
tests/JSCallback.hs checks that a value-returning callback runs its action
exactly once; miso's integration tests caught this as two components not
re-rendering.
Enable ASYNCIFY when the program creates JavaScript callbacks
ASYNCIFY was only added for an 'interruptible' import. But a Haskell
thread that blocks waiting for a callback (e.g. takeMVar filled by an event
handler) can only let the JavaScript event loop run by unwinding the stack,
which also needs ASYNCIFY. A program using syncCallback/asyncCallback with
no interruptible import therefore deadlocked:
waiting (async)
ERR: deadlock: waiting for a JavaScript callback needs ASYNCIFY
Both 'wrapper' and 'wrapper sync' imports now set the flag. It is not the
callback itself that needs ASYNCIFY (a synchronous callback runs on the
current stack, an asynchronous one defers with setTimeout), but waiting for
one; since that cannot be detected at compile time, any callback enables it.
'safe' does not need it: it is a synchronous call wrapped in try/catch.
throwTo from a JavaScript callback
throwTo waits until the target has taken the exception (augustss#534), but a
JavaScript callback's caller C stack is gone, so nothing there may block.
From a callback, throwTo hands the exception over and returns; if the
target already has one pending, a helper thread (async_throwto) waits to
hand it over. A callback thread that finishes or dies releases its
throwers with release_throwto, like any other thread.
catch: rethrow the converted exception when the handler does not match
A handler of the wrong type rethrew the raw RTS exception (an Int) instead
of the SomeException that rtsExn had made from it. For a JSException the
conversion takes the JavaScript value out of the runtime (js_take_exn), so
the next handler converted it again and got undefined. Rethrow the
converted exception, so the value survives any number of handlers.
Fix JavaScript callback and JSException bugs found in review
- A callback called by JavaScript after freeJSVal ran whichever callback had
been given its freed stable pointer since (slots are reused at once). It
now reads the stable pointer at call time and throws if it is gone.
- A callback called from the event loop returned a JSVal handle that nothing
on the Haskell side referenced, then ran the threads it woke; a GC in one
of them dropped the handle before JavaScript read it. JavaScript now takes
the result first, then runs the threads (_mhs_js_run_threads).
- Snippets were cached by the address of their source string only. The C
compiler merges identical literals, so a synchronous and an interruptible
import with the same code shared one compiled function, and the synchronous
one could get a Promise. Async functions are now cached under the negated
address.
- A JSException value that rtsExn never converted (its callback died of it,
or a newer JavaScript exception replaced it) stayed in the handle table
forever. It is now dropped.
- throwTo from a callback blocked, which is fatal there, when the target
already had an exception pending. A helper thread (async_throwto) now
hands it over.
- JavaScript timer delays are rounded up to whole milliseconds, so a timer
does not fire before the thread is due, and capped at the largest delay
setTimeout accepts.
tests/JSCallback.hs has a regression test for each; all of them fail with
the previous runtime.
Data.Text.chunksOf: no chunks for a non-positive size; update JSVal.ref
chunksOf n with n <= 0 on a non-empty Text returned an infinite list of empty
Texts; the text package returns [].
JSVal.ref follows 05fbaba, which runs asynchronous callbacks as microtasks
(queueMicrotask) instead of with setTimeout, so they now run before the
other callbacks queued with setTimeout.
run_callback: restore the stack when showing an uncaught exception fails
When a callback died of an exception whose show raised another one, control
came back to run_callback with the failed show's stack still on top, and
POP(1) removed only one entry. mhs_js_callback then returned to the thread
that had called JavaScript with its stack misaligned, and that thread went
wrong (in the test the program stopped silently after the first call).
Reset stack_ptr to its value before the callback instead.
Only pass the ASYNCIFY options to emscripten's C compiler
A program with a javascript wrapper or interruptible import got -sASYNCIFY
and -sASYNCIFY_STACK_SIZE whatever the target, so compiling it for a native
target failed: the JavaScript code is left out there, but cc rejects the
options. mainCompileC now adds them only when the target's C compiler is
emcc.
tests/JSNative (make testjsnative, part of alltest) builds and runs such a
program natively.
Regenerate mhs.c (bootstrapped: stage2 equals stage1).
Raise a JavaScript exception as its JSVal, not through a global slot
A JavaScript exception from a safe or interruptible import was raised as the
RTS exception number 10, with its value left in a global (mhs_js_exn_handle)
for rtsExn to take later (js_take_exn). If another JavaScript exception
happened before the handler converted the first one (another thread running
in between), the first thread's handler got the second value and the second
got undefined. Values that were never converted also had to be dropped by
hand (mhs_js_set_exn, mhs_js_drop_exn).
Now the exception raised is the JSVal node itself, an ordinary foreign
pointer. primIsInt reports such a node as 10, and rtsExn wraps it in a
JSException. The value belongs to its exception from the start and the GC
frees it with the exception, so the global, js_take_exn, mhs_js_set_exn,
mhs_js_drop_exn and the special case in rts_exn_msg are gone. die_exn and
the callback error path show a JSVal exception with js_exn_string's text.
tests/JSCallback: the leak test forces a GC before measuring, since the
values of dead exceptions are now freed by the GC.
Regenerate mhs.c (bootstrapped: stage2 equals stage1) and base.pkg.
JavaScript FFI: stop the runtime after a fatal error in a callback; fix Ptr in interruptible imports
Found in a review of the JSVal/callback implementation:
- An 'interruptible' import with a Ptr argument did not compile: the
generated EM_ASYNC_JS wrapper declared the parameter as int, and clang
rejects the pointer conversion. The parameter is now void *.
- A fatal error inside a callback did not end the program in the browser.
exit() with a live runtime (kept alive for the callbacks) only throws to
the JavaScript caller, and a JavaScript exception from an 'unsafe' import
unwinds the C stack in the middle of run_callback; either way the page
kept calling into a runtime whose state was gone. The JavaScript side
now records that the runtime has stopped (Module.mhsjs.stop, also from an
atexit hook, and from the unsafe import wrappers before the exception
propagates) and every later call into Haskell throws an Error saying so.
- yield in a callback was fatal when another thread existed (it tried to
reschedule, and a callback must run to completion). It is a no-op there.
- textToJSString/textFromJSString truncated text at a NUL character, since
they went through NUL-terminated C strings. They pass the lengths now.
Tests added to tests/JSVal.hs and tests/JSCallback.hs; bootstrapped
(stage2 equals stage1) and generated/base.pkg regenerated.
JavaScript FFI: drop emscripten's ASYNCIFY
ASYNCIFY instrumented the whole evaluator (the runtime calls FFI
functions through pointers), which made it 3.2x slower and the output
31% larger. It was used for two things, both replaced:
- Waiting for the JavaScript event loop while Haskell threads are blocked.
Haskell threads keep their state in the heap (cleanup() discards the C
stack), so run_threads() now simply returns to JavaScript whenever the
run queue is empty and something on the JavaScript side can wake a
thread (a callback, a threadDelay timer, a settling Promise), also while
the main thread is blocked. mhs_main() then keeps the runtime alive, and
main_finish() (the old tail of main) runs when the main thread finishes
later, from mhs_js_run_threads().
- 'interruptible' imports. The import now returns the Promise of its
async JavaScript code as a JSVal, and the desugarer wraps it with the
new IO.jsawait primitive: an MVar that the JavaScript side fills when
the Promise settles (Module.mhsjs.await -> mhs_js_settle), and a
takeMVar. Only the calling thread waits; a rejection raises the
JSException. Packages with interruptible imports must be recompiled.
A callback called from the JavaScript event loop may now block or await
(it is left waiting like any other thread; its caller gets undefined, or a
resolved Promise); an uncaught exception after that is reported like one
in a forkIO thread. Callbacks that return a value, or that are called
from the JavaScript code of an import, still must not block.
Also: every call into the runtime from JavaScript goes through
Module.mhsjs.enter (fail-stop, ExitStatus is not an error), the JSKind
table lives in the desugarer only, and the stale mhs_js_async_* prototypes
are gone. Bootstrapped (stage2 equals stage1), base.pkg regenerated;
miso's 290 browser tests pass.
Fix CI: lex (##) as the empty unboxed tuple again; hlint suggestions
The rule that lets '(#' start an operator such as (#>) also caught (##),
the empty unboxed tuple, which unordered-containers uses, so the hackage
build failed with "undefined value: ##". A '#' right after '(#' now
keeps the unboxed tuple. tests/Unboxed covers both (##) and (#>).
Also fix the hlint suggestions in Data.Text.concatMap, GHC.Wasm.Prim,
tests/JSCallback and tests/JSNative.
Regenerate mhs.c (bootstrapped: stage2 equals stage1) and base.pkg.
dmjio
added a commit
to haskell-miso/MicroHs
that referenced
this pull request
Oct 10, 2026
Squash of the fork's work before the 2026-09-21 upstream merge, rebased
onto upstream master:
- Compile rts standalone, intro demo script.
- Add JSVal and a JavaScript FFI for the emscripten target
- Add browser and JavaScript-only emscripten targets
- Embed JavaScript files in the output: -js FILE, js target key, package jsbits
- Regenerate mcabal.c: query the mhs version without the package path flag
- Yield to the JavaScript event loop when all threads are blocked; more Data.Text functions
- Keep running Haskell threads after main or a callback finishes (emscripten)
- Browser targets: 4M cell heap; force a GC after many new JSVal handles
Run JavaScript callbacks as Haskell threads; fix exception and waiting issues
A callback from JavaScript used to run with ffe_eval on whatever stack was
current, so there was no current thread: catch, getMaskingState, forkIO and
myThreadId all read runq.mq_head, which is null while main is blocked in
pause_exec or awaiting a Promise (it only appeared to work because address 0
is readable in wasm). Callbacks now run as a thread of their own
(run_callback), first and without preemption; blocking in a callback is a
clean error instead of a longjmp out of a JavaScript frame.
An uncaught Haskell exception in a callback used to longjmp past the
JavaScript caller into the scheduler. It is now converted to a message and
thrown to the caller as a JavaScript Error (an asynchronous callback rejects
its Promise).
pause_exec waited by polling with emscripten_sleep(1) forever; it now waits
for the JavaScript event loop (mhs_js_wait_event) until a callback wakes it
or the earliest threadDelay expires, and says what to do when ASYNCIFY is
not enabled instead of reporting a deadlock.
Also: JSVal handles wrap around instead of overflowing the C int after 2^31
allocations; an interruptible import raises a JSException instead of an
emscripten assertion when it would nest inside another asynchronous
operation; safe/interruptible imports also catch exceptions from the
argument conversion (e.g., a freed JSVal).
tests/JSCallback.hs covers these: catch/mask/forkIO/myThreadId in a
callback, an uncaught exception in a synchronous and an asynchronous
callback, killThread of a delayed thread from a callback, a callback during
an await, use after freeJSVal, and the handle table staying bounded.
Remove lib/TAGS and rts-js-demo.sh, which should not have been committed,
and import Data.Text qualified in Data.Text.Encoding so that new Data.Text
exports cannot clash.
GHC.Wasm.Prim: no Eq/Show instances for JSString, like GHC; Data.Text.toTitle
GHC's GHC.Wasm.Prim defines no instances for JSString, so a module that
defines its own (e.g. miso's Data.JSString, which has Eq, Ord, Show,
Semigroup, Monoid and IsString) got duplicate instances with MicroHs.
The JSVal test converts with fromJSString before comparing or printing.
browser target: compile with -Oz -sSUPPORT_LONGJMP=wasm
The evaluator contains a setjmp (for catch), and with emscripten's default
JavaScript implementation of longjmp every call out of a function that uses
setjmp goes through a JavaScript trampoline. At -O3 most helpers are
inlined so this costs a percent or two, but at -Oz (which does not inline)
a miso app spent 84% of its time in those trampolines instead of 22%.
With WebAssembly exception handling for longjmp the -Oz build is as fast as
-O3 and about 12% smaller compressed. browser_js has no WebAssembly, so it
stays at -O3.
Fix the result of a JavaScript callback that returns a value
run_callback built the callback thread's root as ((performIO act) World),
which applies the result of the action to World, and it never wrote the
result back to the stack. A callback declared to return a JSVal therefore
got its value by evaluating the performIO application a second time, so its
action ran twice, and a callback returning () evaluated a bogus application.
The root is now what performIO builds, ((act World) K), and the result is
left at TOP(0) for the caller.
tests/JSCallback.hs checks that a value-returning callback runs its action
exactly once; miso's integration tests caught this as two components not
re-rendering.
Enable ASYNCIFY when the program creates JavaScript callbacks
ASYNCIFY was only added for an 'interruptible' import. But a Haskell
thread that blocks waiting for a callback (e.g. takeMVar filled by an event
handler) can only let the JavaScript event loop run by unwinding the stack,
which also needs ASYNCIFY. A program using syncCallback/asyncCallback with
no interruptible import therefore deadlocked:
waiting (async)
ERR: deadlock: waiting for a JavaScript callback needs ASYNCIFY
Both 'wrapper' and 'wrapper sync' imports now set the flag. It is not the
callback itself that needs ASYNCIFY (a synchronous callback runs on the
current stack, an asynchronous one defers with setTimeout), but waiting for
one; since that cannot be detected at compile time, any callback enables it.
'safe' does not need it: it is a synchronous call wrapped in try/catch.
throwTo from a JavaScript callback
throwTo waits until the target has taken the exception (augustss#534), but a
JavaScript callback's caller C stack is gone, so nothing there may block.
From a callback, throwTo hands the exception over and returns; if the
target already has one pending, a helper thread (async_throwto) waits to
hand it over. A callback thread that finishes or dies releases its
throwers with release_throwto, like any other thread.
catch: rethrow the converted exception when the handler does not match
A handler of the wrong type rethrew the raw RTS exception (an Int) instead
of the SomeException that rtsExn had made from it. For a JSException the
conversion takes the JavaScript value out of the runtime (js_take_exn), so
the next handler converted it again and got undefined. Rethrow the
converted exception, so the value survives any number of handlers.
Fix JavaScript callback and JSException bugs found in review
- A callback called by JavaScript after freeJSVal ran whichever callback had
been given its freed stable pointer since (slots are reused at once). It
now reads the stable pointer at call time and throws if it is gone.
- A callback called from the event loop returned a JSVal handle that nothing
on the Haskell side referenced, then ran the threads it woke; a GC in one
of them dropped the handle before JavaScript read it. JavaScript now takes
the result first, then runs the threads (_mhs_js_run_threads).
- Snippets were cached by the address of their source string only. The C
compiler merges identical literals, so a synchronous and an interruptible
import with the same code shared one compiled function, and the synchronous
one could get a Promise. Async functions are now cached under the negated
address.
- A JSException value that rtsExn never converted (its callback died of it,
or a newer JavaScript exception replaced it) stayed in the handle table
forever. It is now dropped.
- throwTo from a callback blocked, which is fatal there, when the target
already had an exception pending. A helper thread (async_throwto) now
hands it over.
- JavaScript timer delays are rounded up to whole milliseconds, so a timer
does not fire before the thread is due, and capped at the largest delay
setTimeout accepts.
tests/JSCallback.hs has a regression test for each; all of them fail with
the previous runtime.
Data.Text.chunksOf: no chunks for a non-positive size; update JSVal.ref
chunksOf n with n <= 0 on a non-empty Text returned an infinite list of empty
Texts; the text package returns [].
JSVal.ref follows 05fbaba, which runs asynchronous callbacks as microtasks
(queueMicrotask) instead of with setTimeout, so they now run before the
other callbacks queued with setTimeout.
run_callback: restore the stack when showing an uncaught exception fails
When a callback died of an exception whose show raised another one, control
came back to run_callback with the failed show's stack still on top, and
POP(1) removed only one entry. mhs_js_callback then returned to the thread
that had called JavaScript with its stack misaligned, and that thread went
wrong (in the test the program stopped silently after the first call).
Reset stack_ptr to its value before the callback instead.
Only pass the ASYNCIFY options to emscripten's C compiler
A program with a javascript wrapper or interruptible import got -sASYNCIFY
and -sASYNCIFY_STACK_SIZE whatever the target, so compiling it for a native
target failed: the JavaScript code is left out there, but cc rejects the
options. mainCompileC now adds them only when the target's C compiler is
emcc.
tests/JSNative (make testjsnative, part of alltest) builds and runs such a
program natively.
Regenerate mhs.c (bootstrapped: stage2 equals stage1).
Raise a JavaScript exception as its JSVal, not through a global slot
A JavaScript exception from a safe or interruptible import was raised as the
RTS exception number 10, with its value left in a global (mhs_js_exn_handle)
for rtsExn to take later (js_take_exn). If another JavaScript exception
happened before the handler converted the first one (another thread running
in between), the first thread's handler got the second value and the second
got undefined. Values that were never converted also had to be dropped by
hand (mhs_js_set_exn, mhs_js_drop_exn).
Now the exception raised is the JSVal node itself, an ordinary foreign
pointer. primIsInt reports such a node as 10, and rtsExn wraps it in a
JSException. The value belongs to its exception from the start and the GC
frees it with the exception, so the global, js_take_exn, mhs_js_set_exn,
mhs_js_drop_exn and the special case in rts_exn_msg are gone. die_exn and
the callback error path show a JSVal exception with js_exn_string's text.
tests/JSCallback: the leak test forces a GC before measuring, since the
values of dead exceptions are now freed by the GC.
Regenerate mhs.c (bootstrapped: stage2 equals stage1) and base.pkg.
JavaScript FFI: stop the runtime after a fatal error in a callback; fix Ptr in interruptible imports
Found in a review of the JSVal/callback implementation:
- An 'interruptible' import with a Ptr argument did not compile: the
generated EM_ASYNC_JS wrapper declared the parameter as int, and clang
rejects the pointer conversion. The parameter is now void *.
- A fatal error inside a callback did not end the program in the browser.
exit() with a live runtime (kept alive for the callbacks) only throws to
the JavaScript caller, and a JavaScript exception from an 'unsafe' import
unwinds the C stack in the middle of run_callback; either way the page
kept calling into a runtime whose state was gone. The JavaScript side
now records that the runtime has stopped (Module.mhsjs.stop, also from an
atexit hook, and from the unsafe import wrappers before the exception
propagates) and every later call into Haskell throws an Error saying so.
- yield in a callback was fatal when another thread existed (it tried to
reschedule, and a callback must run to completion). It is a no-op there.
- textToJSString/textFromJSString truncated text at a NUL character, since
they went through NUL-terminated C strings. They pass the lengths now.
Tests added to tests/JSVal.hs and tests/JSCallback.hs; bootstrapped
(stage2 equals stage1) and generated/base.pkg regenerated.
JavaScript FFI: drop emscripten's ASYNCIFY
ASYNCIFY instrumented the whole evaluator (the runtime calls FFI
functions through pointers), which made it 3.2x slower and the output
31% larger. It was used for two things, both replaced:
- Waiting for the JavaScript event loop while Haskell threads are blocked.
Haskell threads keep their state in the heap (cleanup() discards the C
stack), so run_threads() now simply returns to JavaScript whenever the
run queue is empty and something on the JavaScript side can wake a
thread (a callback, a threadDelay timer, a settling Promise), also while
the main thread is blocked. mhs_main() then keeps the runtime alive, and
main_finish() (the old tail of main) runs when the main thread finishes
later, from mhs_js_run_threads().
- 'interruptible' imports. The import now returns the Promise of its
async JavaScript code as a JSVal, and the desugarer wraps it with the
new IO.jsawait primitive: an MVar that the JavaScript side fills when
the Promise settles (Module.mhsjs.await -> mhs_js_settle), and a
takeMVar. Only the calling thread waits; a rejection raises the
JSException. Packages with interruptible imports must be recompiled.
A callback called from the JavaScript event loop may now block or await
(it is left waiting like any other thread; its caller gets undefined, or a
resolved Promise); an uncaught exception after that is reported like one
in a forkIO thread. Callbacks that return a value, or that are called
from the JavaScript code of an import, still must not block.
Also: every call into the runtime from JavaScript goes through
Module.mhsjs.enter (fail-stop, ExitStatus is not an error), the JSKind
table lives in the desugarer only, and the stale mhs_js_async_* prototypes
are gone. Bootstrapped (stage2 equals stage1), base.pkg regenerated;
miso's 290 browser tests pass.
Fix CI: lex (##) as the empty unboxed tuple again; hlint suggestions
The rule that lets '(#' start an operator such as (#>) also caught (##),
the empty unboxed tuple, which unordered-containers uses, so the hackage
build failed with "undefined value: ##". A '#' right after '(#' now
keeps the unboxed tuple. tests/Unboxed covers both (##) and (#>).
Also fix the hlint suggestions in Data.Text.concatMap, GHC.Wasm.Prim,
tests/JSCallback and tests/JSNative.
Regenerate mhs.c (bootstrapped: stage2 equals stage1) and base.pkg.
dmjio
added a commit
to haskell-miso/MicroHs
that referenced
this pull request
Oct 14, 2026
Squash of the fork's work before the 2026-09-21 upstream merge, rebased
onto upstream master:
- Compile rts standalone, intro demo script.
- Add JSVal and a JavaScript FFI for the emscripten target
- Add browser and JavaScript-only emscripten targets
- Embed JavaScript files in the output: -js FILE, js target key, package jsbits
- Regenerate mcabal.c: query the mhs version without the package path flag
- Yield to the JavaScript event loop when all threads are blocked; more Data.Text functions
- Keep running Haskell threads after main or a callback finishes (emscripten)
- Browser targets: 4M cell heap; force a GC after many new JSVal handles
Run JavaScript callbacks as Haskell threads; fix exception and waiting issues
A callback from JavaScript used to run with ffe_eval on whatever stack was
current, so there was no current thread: catch, getMaskingState, forkIO and
myThreadId all read runq.mq_head, which is null while main is blocked in
pause_exec or awaiting a Promise (it only appeared to work because address 0
is readable in wasm). Callbacks now run as a thread of their own
(run_callback), first and without preemption; blocking in a callback is a
clean error instead of a longjmp out of a JavaScript frame.
An uncaught Haskell exception in a callback used to longjmp past the
JavaScript caller into the scheduler. It is now converted to a message and
thrown to the caller as a JavaScript Error (an asynchronous callback rejects
its Promise).
pause_exec waited by polling with emscripten_sleep(1) forever; it now waits
for the JavaScript event loop (mhs_js_wait_event) until a callback wakes it
or the earliest threadDelay expires, and says what to do when ASYNCIFY is
not enabled instead of reporting a deadlock.
Also: JSVal handles wrap around instead of overflowing the C int after 2^31
allocations; an interruptible import raises a JSException instead of an
emscripten assertion when it would nest inside another asynchronous
operation; safe/interruptible imports also catch exceptions from the
argument conversion (e.g., a freed JSVal).
tests/JSCallback.hs covers these: catch/mask/forkIO/myThreadId in a
callback, an uncaught exception in a synchronous and an asynchronous
callback, killThread of a delayed thread from a callback, a callback during
an await, use after freeJSVal, and the handle table staying bounded.
Remove lib/TAGS and rts-js-demo.sh, which should not have been committed,
and import Data.Text qualified in Data.Text.Encoding so that new Data.Text
exports cannot clash.
GHC.Wasm.Prim: no Eq/Show instances for JSString, like GHC; Data.Text.toTitle
GHC's GHC.Wasm.Prim defines no instances for JSString, so a module that
defines its own (e.g. miso's Data.JSString, which has Eq, Ord, Show,
Semigroup, Monoid and IsString) got duplicate instances with MicroHs.
The JSVal test converts with fromJSString before comparing or printing.
browser target: compile with -Oz -sSUPPORT_LONGJMP=wasm
The evaluator contains a setjmp (for catch), and with emscripten's default
JavaScript implementation of longjmp every call out of a function that uses
setjmp goes through a JavaScript trampoline. At -O3 most helpers are
inlined so this costs a percent or two, but at -Oz (which does not inline)
a miso app spent 84% of its time in those trampolines instead of 22%.
With WebAssembly exception handling for longjmp the -Oz build is as fast as
-O3 and about 12% smaller compressed. browser_js has no WebAssembly, so it
stays at -O3.
Fix the result of a JavaScript callback that returns a value
run_callback built the callback thread's root as ((performIO act) World),
which applies the result of the action to World, and it never wrote the
result back to the stack. A callback declared to return a JSVal therefore
got its value by evaluating the performIO application a second time, so its
action ran twice, and a callback returning () evaluated a bogus application.
The root is now what performIO builds, ((act World) K), and the result is
left at TOP(0) for the caller.
tests/JSCallback.hs checks that a value-returning callback runs its action
exactly once; miso's integration tests caught this as two components not
re-rendering.
Enable ASYNCIFY when the program creates JavaScript callbacks
ASYNCIFY was only added for an 'interruptible' import. But a Haskell
thread that blocks waiting for a callback (e.g. takeMVar filled by an event
handler) can only let the JavaScript event loop run by unwinding the stack,
which also needs ASYNCIFY. A program using syncCallback/asyncCallback with
no interruptible import therefore deadlocked:
waiting (async)
ERR: deadlock: waiting for a JavaScript callback needs ASYNCIFY
Both 'wrapper' and 'wrapper sync' imports now set the flag. It is not the
callback itself that needs ASYNCIFY (a synchronous callback runs on the
current stack, an asynchronous one defers with setTimeout), but waiting for
one; since that cannot be detected at compile time, any callback enables it.
'safe' does not need it: it is a synchronous call wrapped in try/catch.
throwTo from a JavaScript callback
throwTo waits until the target has taken the exception (augustss#534), but a
JavaScript callback's caller C stack is gone, so nothing there may block.
From a callback, throwTo hands the exception over and returns; if the
target already has one pending, a helper thread (async_throwto) waits to
hand it over. A callback thread that finishes or dies releases its
throwers with release_throwto, like any other thread.
catch: rethrow the converted exception when the handler does not match
A handler of the wrong type rethrew the raw RTS exception (an Int) instead
of the SomeException that rtsExn had made from it. For a JSException the
conversion takes the JavaScript value out of the runtime (js_take_exn), so
the next handler converted it again and got undefined. Rethrow the
converted exception, so the value survives any number of handlers.
Fix JavaScript callback and JSException bugs found in review
- A callback called by JavaScript after freeJSVal ran whichever callback had
been given its freed stable pointer since (slots are reused at once). It
now reads the stable pointer at call time and throws if it is gone.
- A callback called from the event loop returned a JSVal handle that nothing
on the Haskell side referenced, then ran the threads it woke; a GC in one
of them dropped the handle before JavaScript read it. JavaScript now takes
the result first, then runs the threads (_mhs_js_run_threads).
- Snippets were cached by the address of their source string only. The C
compiler merges identical literals, so a synchronous and an interruptible
import with the same code shared one compiled function, and the synchronous
one could get a Promise. Async functions are now cached under the negated
address.
- A JSException value that rtsExn never converted (its callback died of it,
or a newer JavaScript exception replaced it) stayed in the handle table
forever. It is now dropped.
- throwTo from a callback blocked, which is fatal there, when the target
already had an exception pending. A helper thread (async_throwto) now
hands it over.
- JavaScript timer delays are rounded up to whole milliseconds, so a timer
does not fire before the thread is due, and capped at the largest delay
setTimeout accepts.
tests/JSCallback.hs has a regression test for each; all of them fail with
the previous runtime.
Data.Text.chunksOf: no chunks for a non-positive size; update JSVal.ref
chunksOf n with n <= 0 on a non-empty Text returned an infinite list of empty
Texts; the text package returns [].
JSVal.ref follows 05fbaba, which runs asynchronous callbacks as microtasks
(queueMicrotask) instead of with setTimeout, so they now run before the
other callbacks queued with setTimeout.
run_callback: restore the stack when showing an uncaught exception fails
When a callback died of an exception whose show raised another one, control
came back to run_callback with the failed show's stack still on top, and
POP(1) removed only one entry. mhs_js_callback then returned to the thread
that had called JavaScript with its stack misaligned, and that thread went
wrong (in the test the program stopped silently after the first call).
Reset stack_ptr to its value before the callback instead.
Only pass the ASYNCIFY options to emscripten's C compiler
A program with a javascript wrapper or interruptible import got -sASYNCIFY
and -sASYNCIFY_STACK_SIZE whatever the target, so compiling it for a native
target failed: the JavaScript code is left out there, but cc rejects the
options. mainCompileC now adds them only when the target's C compiler is
emcc.
tests/JSNative (make testjsnative, part of alltest) builds and runs such a
program natively.
Regenerate mhs.c (bootstrapped: stage2 equals stage1).
Raise a JavaScript exception as its JSVal, not through a global slot
A JavaScript exception from a safe or interruptible import was raised as the
RTS exception number 10, with its value left in a global (mhs_js_exn_handle)
for rtsExn to take later (js_take_exn). If another JavaScript exception
happened before the handler converted the first one (another thread running
in between), the first thread's handler got the second value and the second
got undefined. Values that were never converted also had to be dropped by
hand (mhs_js_set_exn, mhs_js_drop_exn).
Now the exception raised is the JSVal node itself, an ordinary foreign
pointer. primIsInt reports such a node as 10, and rtsExn wraps it in a
JSException. The value belongs to its exception from the start and the GC
frees it with the exception, so the global, js_take_exn, mhs_js_set_exn,
mhs_js_drop_exn and the special case in rts_exn_msg are gone. die_exn and
the callback error path show a JSVal exception with js_exn_string's text.
tests/JSCallback: the leak test forces a GC before measuring, since the
values of dead exceptions are now freed by the GC.
Regenerate mhs.c (bootstrapped: stage2 equals stage1) and base.pkg.
JavaScript FFI: stop the runtime after a fatal error in a callback; fix Ptr in interruptible imports
Found in a review of the JSVal/callback implementation:
- An 'interruptible' import with a Ptr argument did not compile: the
generated EM_ASYNC_JS wrapper declared the parameter as int, and clang
rejects the pointer conversion. The parameter is now void *.
- A fatal error inside a callback did not end the program in the browser.
exit() with a live runtime (kept alive for the callbacks) only throws to
the JavaScript caller, and a JavaScript exception from an 'unsafe' import
unwinds the C stack in the middle of run_callback; either way the page
kept calling into a runtime whose state was gone. The JavaScript side
now records that the runtime has stopped (Module.mhsjs.stop, also from an
atexit hook, and from the unsafe import wrappers before the exception
propagates) and every later call into Haskell throws an Error saying so.
- yield in a callback was fatal when another thread existed (it tried to
reschedule, and a callback must run to completion). It is a no-op there.
- textToJSString/textFromJSString truncated text at a NUL character, since
they went through NUL-terminated C strings. They pass the lengths now.
Tests added to tests/JSVal.hs and tests/JSCallback.hs; bootstrapped
(stage2 equals stage1) and generated/base.pkg regenerated.
JavaScript FFI: drop emscripten's ASYNCIFY
ASYNCIFY instrumented the whole evaluator (the runtime calls FFI
functions through pointers), which made it 3.2x slower and the output
31% larger. It was used for two things, both replaced:
- Waiting for the JavaScript event loop while Haskell threads are blocked.
Haskell threads keep their state in the heap (cleanup() discards the C
stack), so run_threads() now simply returns to JavaScript whenever the
run queue is empty and something on the JavaScript side can wake a
thread (a callback, a threadDelay timer, a settling Promise), also while
the main thread is blocked. mhs_main() then keeps the runtime alive, and
main_finish() (the old tail of main) runs when the main thread finishes
later, from mhs_js_run_threads().
- 'interruptible' imports. The import now returns the Promise of its
async JavaScript code as a JSVal, and the desugarer wraps it with the
new IO.jsawait primitive: an MVar that the JavaScript side fills when
the Promise settles (Module.mhsjs.await -> mhs_js_settle), and a
takeMVar. Only the calling thread waits; a rejection raises the
JSException. Packages with interruptible imports must be recompiled.
A callback called from the JavaScript event loop may now block or await
(it is left waiting like any other thread; its caller gets undefined, or a
resolved Promise); an uncaught exception after that is reported like one
in a forkIO thread. Callbacks that return a value, or that are called
from the JavaScript code of an import, still must not block.
Also: every call into the runtime from JavaScript goes through
Module.mhsjs.enter (fail-stop, ExitStatus is not an error), the JSKind
table lives in the desugarer only, and the stale mhs_js_async_* prototypes
are gone. Bootstrapped (stage2 equals stage1), base.pkg regenerated;
miso's 290 browser tests pass.
Fix CI: lex (##) as the empty unboxed tuple again; hlint suggestions
The rule that lets '(#' start an operator such as (#>) also caught (##),
the empty unboxed tuple, which unordered-containers uses, so the hackage
build failed with "undefined value: ##". A '#' right after '(#' now
keeps the unboxed tuple. tests/Unboxed covers both (##) and (#>).
Also fix the hlint suggestions in Data.Text.concatMap, GHC.Wasm.Prim,
tests/JSCallback and tests/JSNative.
Regenerate mhs.c (bootstrapped: stage2 equals stage1) and base.pkg.
dmjio
added a commit
to haskell-miso/MicroHs
that referenced
this pull request
Oct 25, 2026
Squash of the fork's work before the 2026-09-21 upstream merge, rebased
onto upstream master:
- Compile rts standalone, intro demo script.
- Add JSVal and a JavaScript FFI for the emscripten target
- Add browser and JavaScript-only emscripten targets
- Embed JavaScript files in the output: -js FILE, js target key, package jsbits
- Regenerate mcabal.c: query the mhs version without the package path flag
- Yield to the JavaScript event loop when all threads are blocked; more Data.Text functions
- Keep running Haskell threads after main or a callback finishes (emscripten)
- Browser targets: 4M cell heap; force a GC after many new JSVal handles
Run JavaScript callbacks as Haskell threads; fix exception and waiting issues
A callback from JavaScript used to run with ffe_eval on whatever stack was
current, so there was no current thread: catch, getMaskingState, forkIO and
myThreadId all read runq.mq_head, which is null while main is blocked in
pause_exec or awaiting a Promise (it only appeared to work because address 0
is readable in wasm). Callbacks now run as a thread of their own
(run_callback), first and without preemption; blocking in a callback is a
clean error instead of a longjmp out of a JavaScript frame.
An uncaught Haskell exception in a callback used to longjmp past the
JavaScript caller into the scheduler. It is now converted to a message and
thrown to the caller as a JavaScript Error (an asynchronous callback rejects
its Promise).
pause_exec waited by polling with emscripten_sleep(1) forever; it now waits
for the JavaScript event loop (mhs_js_wait_event) until a callback wakes it
or the earliest threadDelay expires, and says what to do when ASYNCIFY is
not enabled instead of reporting a deadlock.
Also: JSVal handles wrap around instead of overflowing the C int after 2^31
allocations; an interruptible import raises a JSException instead of an
emscripten assertion when it would nest inside another asynchronous
operation; safe/interruptible imports also catch exceptions from the
argument conversion (e.g., a freed JSVal).
tests/JSCallback.hs covers these: catch/mask/forkIO/myThreadId in a
callback, an uncaught exception in a synchronous and an asynchronous
callback, killThread of a delayed thread from a callback, a callback during
an await, use after freeJSVal, and the handle table staying bounded.
Remove lib/TAGS and rts-js-demo.sh, which should not have been committed,
and import Data.Text qualified in Data.Text.Encoding so that new Data.Text
exports cannot clash.
GHC.Wasm.Prim: no Eq/Show instances for JSString, like GHC; Data.Text.toTitle
GHC's GHC.Wasm.Prim defines no instances for JSString, so a module that
defines its own (e.g. miso's Data.JSString, which has Eq, Ord, Show,
Semigroup, Monoid and IsString) got duplicate instances with MicroHs.
The JSVal test converts with fromJSString before comparing or printing.
browser target: compile with -Oz -sSUPPORT_LONGJMP=wasm
The evaluator contains a setjmp (for catch), and with emscripten's default
JavaScript implementation of longjmp every call out of a function that uses
setjmp goes through a JavaScript trampoline. At -O3 most helpers are
inlined so this costs a percent or two, but at -Oz (which does not inline)
a miso app spent 84% of its time in those trampolines instead of 22%.
With WebAssembly exception handling for longjmp the -Oz build is as fast as
-O3 and about 12% smaller compressed. browser_js has no WebAssembly, so it
stays at -O3.
Fix the result of a JavaScript callback that returns a value
run_callback built the callback thread's root as ((performIO act) World),
which applies the result of the action to World, and it never wrote the
result back to the stack. A callback declared to return a JSVal therefore
got its value by evaluating the performIO application a second time, so its
action ran twice, and a callback returning () evaluated a bogus application.
The root is now what performIO builds, ((act World) K), and the result is
left at TOP(0) for the caller.
tests/JSCallback.hs checks that a value-returning callback runs its action
exactly once; miso's integration tests caught this as two components not
re-rendering.
Enable ASYNCIFY when the program creates JavaScript callbacks
ASYNCIFY was only added for an 'interruptible' import. But a Haskell
thread that blocks waiting for a callback (e.g. takeMVar filled by an event
handler) can only let the JavaScript event loop run by unwinding the stack,
which also needs ASYNCIFY. A program using syncCallback/asyncCallback with
no interruptible import therefore deadlocked:
waiting (async)
ERR: deadlock: waiting for a JavaScript callback needs ASYNCIFY
Both 'wrapper' and 'wrapper sync' imports now set the flag. It is not the
callback itself that needs ASYNCIFY (a synchronous callback runs on the
current stack, an asynchronous one defers with setTimeout), but waiting for
one; since that cannot be detected at compile time, any callback enables it.
'safe' does not need it: it is a synchronous call wrapped in try/catch.
throwTo from a JavaScript callback
throwTo waits until the target has taken the exception (augustss#534), but a
JavaScript callback's caller C stack is gone, so nothing there may block.
From a callback, throwTo hands the exception over and returns; if the
target already has one pending, a helper thread (async_throwto) waits to
hand it over. A callback thread that finishes or dies releases its
throwers with release_throwto, like any other thread.
catch: rethrow the converted exception when the handler does not match
A handler of the wrong type rethrew the raw RTS exception (an Int) instead
of the SomeException that rtsExn had made from it. For a JSException the
conversion takes the JavaScript value out of the runtime (js_take_exn), so
the next handler converted it again and got undefined. Rethrow the
converted exception, so the value survives any number of handlers.
Fix JavaScript callback and JSException bugs found in review
- A callback called by JavaScript after freeJSVal ran whichever callback had
been given its freed stable pointer since (slots are reused at once). It
now reads the stable pointer at call time and throws if it is gone.
- A callback called from the event loop returned a JSVal handle that nothing
on the Haskell side referenced, then ran the threads it woke; a GC in one
of them dropped the handle before JavaScript read it. JavaScript now takes
the result first, then runs the threads (_mhs_js_run_threads).
- Snippets were cached by the address of their source string only. The C
compiler merges identical literals, so a synchronous and an interruptible
import with the same code shared one compiled function, and the synchronous
one could get a Promise. Async functions are now cached under the negated
address.
- A JSException value that rtsExn never converted (its callback died of it,
or a newer JavaScript exception replaced it) stayed in the handle table
forever. It is now dropped.
- throwTo from a callback blocked, which is fatal there, when the target
already had an exception pending. A helper thread (async_throwto) now
hands it over.
- JavaScript timer delays are rounded up to whole milliseconds, so a timer
does not fire before the thread is due, and capped at the largest delay
setTimeout accepts.
tests/JSCallback.hs has a regression test for each; all of them fail with
the previous runtime.
Data.Text.chunksOf: no chunks for a non-positive size; update JSVal.ref
chunksOf n with n <= 0 on a non-empty Text returned an infinite list of empty
Texts; the text package returns [].
JSVal.ref follows 05fbaba, which runs asynchronous callbacks as microtasks
(queueMicrotask) instead of with setTimeout, so they now run before the
other callbacks queued with setTimeout.
run_callback: restore the stack when showing an uncaught exception fails
When a callback died of an exception whose show raised another one, control
came back to run_callback with the failed show's stack still on top, and
POP(1) removed only one entry. mhs_js_callback then returned to the thread
that had called JavaScript with its stack misaligned, and that thread went
wrong (in the test the program stopped silently after the first call).
Reset stack_ptr to its value before the callback instead.
Only pass the ASYNCIFY options to emscripten's C compiler
A program with a javascript wrapper or interruptible import got -sASYNCIFY
and -sASYNCIFY_STACK_SIZE whatever the target, so compiling it for a native
target failed: the JavaScript code is left out there, but cc rejects the
options. mainCompileC now adds them only when the target's C compiler is
emcc.
tests/JSNative (make testjsnative, part of alltest) builds and runs such a
program natively.
Regenerate mhs.c (bootstrapped: stage2 equals stage1).
Raise a JavaScript exception as its JSVal, not through a global slot
A JavaScript exception from a safe or interruptible import was raised as the
RTS exception number 10, with its value left in a global (mhs_js_exn_handle)
for rtsExn to take later (js_take_exn). If another JavaScript exception
happened before the handler converted the first one (another thread running
in between), the first thread's handler got the second value and the second
got undefined. Values that were never converted also had to be dropped by
hand (mhs_js_set_exn, mhs_js_drop_exn).
Now the exception raised is the JSVal node itself, an ordinary foreign
pointer. primIsInt reports such a node as 10, and rtsExn wraps it in a
JSException. The value belongs to its exception from the start and the GC
frees it with the exception, so the global, js_take_exn, mhs_js_set_exn,
mhs_js_drop_exn and the special case in rts_exn_msg are gone. die_exn and
the callback error path show a JSVal exception with js_exn_string's text.
tests/JSCallback: the leak test forces a GC before measuring, since the
values of dead exceptions are now freed by the GC.
Regenerate mhs.c (bootstrapped: stage2 equals stage1) and base.pkg.
JavaScript FFI: stop the runtime after a fatal error in a callback; fix Ptr in interruptible imports
Found in a review of the JSVal/callback implementation:
- An 'interruptible' import with a Ptr argument did not compile: the
generated EM_ASYNC_JS wrapper declared the parameter as int, and clang
rejects the pointer conversion. The parameter is now void *.
- A fatal error inside a callback did not end the program in the browser.
exit() with a live runtime (kept alive for the callbacks) only throws to
the JavaScript caller, and a JavaScript exception from an 'unsafe' import
unwinds the C stack in the middle of run_callback; either way the page
kept calling into a runtime whose state was gone. The JavaScript side
now records that the runtime has stopped (Module.mhsjs.stop, also from an
atexit hook, and from the unsafe import wrappers before the exception
propagates) and every later call into Haskell throws an Error saying so.
- yield in a callback was fatal when another thread existed (it tried to
reschedule, and a callback must run to completion). It is a no-op there.
- textToJSString/textFromJSString truncated text at a NUL character, since
they went through NUL-terminated C strings. They pass the lengths now.
Tests added to tests/JSVal.hs and tests/JSCallback.hs; bootstrapped
(stage2 equals stage1) and generated/base.pkg regenerated.
JavaScript FFI: drop emscripten's ASYNCIFY
ASYNCIFY instrumented the whole evaluator (the runtime calls FFI
functions through pointers), which made it 3.2x slower and the output
31% larger. It was used for two things, both replaced:
- Waiting for the JavaScript event loop while Haskell threads are blocked.
Haskell threads keep their state in the heap (cleanup() discards the C
stack), so run_threads() now simply returns to JavaScript whenever the
run queue is empty and something on the JavaScript side can wake a
thread (a callback, a threadDelay timer, a settling Promise), also while
the main thread is blocked. mhs_main() then keeps the runtime alive, and
main_finish() (the old tail of main) runs when the main thread finishes
later, from mhs_js_run_threads().
- 'interruptible' imports. The import now returns the Promise of its
async JavaScript code as a JSVal, and the desugarer wraps it with the
new IO.jsawait primitive: an MVar that the JavaScript side fills when
the Promise settles (Module.mhsjs.await -> mhs_js_settle), and a
takeMVar. Only the calling thread waits; a rejection raises the
JSException. Packages with interruptible imports must be recompiled.
A callback called from the JavaScript event loop may now block or await
(it is left waiting like any other thread; its caller gets undefined, or a
resolved Promise); an uncaught exception after that is reported like one
in a forkIO thread. Callbacks that return a value, or that are called
from the JavaScript code of an import, still must not block.
Also: every call into the runtime from JavaScript goes through
Module.mhsjs.enter (fail-stop, ExitStatus is not an error), the JSKind
table lives in the desugarer only, and the stale mhs_js_async_* prototypes
are gone. Bootstrapped (stage2 equals stage1), base.pkg regenerated;
miso's 290 browser tests pass.
Fix CI: lex (##) as the empty unboxed tuple again; hlint suggestions
The rule that lets '(#' start an operator such as (#>) also caught (##),
the empty unboxed tuple, which unordered-containers uses, so the hackage
build failed with "undefined value: ##". A '#' right after '(#' now
keeps the unboxed tuple. tests/Unboxed covers both (##) and (#>).
Also fix the hlint suggestions in Data.Text.concatMap, GHC.Wasm.Prim,
tests/JSCallback and tests/JSNative.
Regenerate mhs.c (bootstrapped: stage2 equals stage1) and base.pkg.
dmjio
added a commit
to haskell-miso/MicroHs
that referenced
this pull request
Oct 26, 2026
Squash of the fork's work before the 2026-09-21 upstream merge, rebased
onto upstream master:
- Compile rts standalone, intro demo script.
- Add JSVal and a JavaScript FFI for the emscripten target
- Add browser and JavaScript-only emscripten targets
- Embed JavaScript files in the output: -js FILE, js target key, package jsbits
- Regenerate mcabal.c: query the mhs version without the package path flag
- Yield to the JavaScript event loop when all threads are blocked; more Data.Text functions
- Keep running Haskell threads after main or a callback finishes (emscripten)
- Browser targets: 4M cell heap; force a GC after many new JSVal handles
Run JavaScript callbacks as Haskell threads; fix exception and waiting issues
A callback from JavaScript used to run with ffe_eval on whatever stack was
current, so there was no current thread: catch, getMaskingState, forkIO and
myThreadId all read runq.mq_head, which is null while main is blocked in
pause_exec or awaiting a Promise (it only appeared to work because address 0
is readable in wasm). Callbacks now run as a thread of their own
(run_callback), first and without preemption; blocking in a callback is a
clean error instead of a longjmp out of a JavaScript frame.
An uncaught Haskell exception in a callback used to longjmp past the
JavaScript caller into the scheduler. It is now converted to a message and
thrown to the caller as a JavaScript Error (an asynchronous callback rejects
its Promise).
pause_exec waited by polling with emscripten_sleep(1) forever; it now waits
for the JavaScript event loop (mhs_js_wait_event) until a callback wakes it
or the earliest threadDelay expires, and says what to do when ASYNCIFY is
not enabled instead of reporting a deadlock.
Also: JSVal handles wrap around instead of overflowing the C int after 2^31
allocations; an interruptible import raises a JSException instead of an
emscripten assertion when it would nest inside another asynchronous
operation; safe/interruptible imports also catch exceptions from the
argument conversion (e.g., a freed JSVal).
tests/JSCallback.hs covers these: catch/mask/forkIO/myThreadId in a
callback, an uncaught exception in a synchronous and an asynchronous
callback, killThread of a delayed thread from a callback, a callback during
an await, use after freeJSVal, and the handle table staying bounded.
Remove lib/TAGS and rts-js-demo.sh, which should not have been committed,
and import Data.Text qualified in Data.Text.Encoding so that new Data.Text
exports cannot clash.
GHC.Wasm.Prim: no Eq/Show instances for JSString, like GHC; Data.Text.toTitle
GHC's GHC.Wasm.Prim defines no instances for JSString, so a module that
defines its own (e.g. miso's Data.JSString, which has Eq, Ord, Show,
Semigroup, Monoid and IsString) got duplicate instances with MicroHs.
The JSVal test converts with fromJSString before comparing or printing.
browser target: compile with -Oz -sSUPPORT_LONGJMP=wasm
The evaluator contains a setjmp (for catch), and with emscripten's default
JavaScript implementation of longjmp every call out of a function that uses
setjmp goes through a JavaScript trampoline. At -O3 most helpers are
inlined so this costs a percent or two, but at -Oz (which does not inline)
a miso app spent 84% of its time in those trampolines instead of 22%.
With WebAssembly exception handling for longjmp the -Oz build is as fast as
-O3 and about 12% smaller compressed. browser_js has no WebAssembly, so it
stays at -O3.
Fix the result of a JavaScript callback that returns a value
run_callback built the callback thread's root as ((performIO act) World),
which applies the result of the action to World, and it never wrote the
result back to the stack. A callback declared to return a JSVal therefore
got its value by evaluating the performIO application a second time, so its
action ran twice, and a callback returning () evaluated a bogus application.
The root is now what performIO builds, ((act World) K), and the result is
left at TOP(0) for the caller.
tests/JSCallback.hs checks that a value-returning callback runs its action
exactly once; miso's integration tests caught this as two components not
re-rendering.
Enable ASYNCIFY when the program creates JavaScript callbacks
ASYNCIFY was only added for an 'interruptible' import. But a Haskell
thread that blocks waiting for a callback (e.g. takeMVar filled by an event
handler) can only let the JavaScript event loop run by unwinding the stack,
which also needs ASYNCIFY. A program using syncCallback/asyncCallback with
no interruptible import therefore deadlocked:
waiting (async)
ERR: deadlock: waiting for a JavaScript callback needs ASYNCIFY
Both 'wrapper' and 'wrapper sync' imports now set the flag. It is not the
callback itself that needs ASYNCIFY (a synchronous callback runs on the
current stack, an asynchronous one defers with setTimeout), but waiting for
one; since that cannot be detected at compile time, any callback enables it.
'safe' does not need it: it is a synchronous call wrapped in try/catch.
throwTo from a JavaScript callback
throwTo waits until the target has taken the exception (augustss#534), but a
JavaScript callback's caller C stack is gone, so nothing there may block.
From a callback, throwTo hands the exception over and returns; if the
target already has one pending, a helper thread (async_throwto) waits to
hand it over. A callback thread that finishes or dies releases its
throwers with release_throwto, like any other thread.
catch: rethrow the converted exception when the handler does not match
A handler of the wrong type rethrew the raw RTS exception (an Int) instead
of the SomeException that rtsExn had made from it. For a JSException the
conversion takes the JavaScript value out of the runtime (js_take_exn), so
the next handler converted it again and got undefined. Rethrow the
converted exception, so the value survives any number of handlers.
Fix JavaScript callback and JSException bugs found in review
- A callback called by JavaScript after freeJSVal ran whichever callback had
been given its freed stable pointer since (slots are reused at once). It
now reads the stable pointer at call time and throws if it is gone.
- A callback called from the event loop returned a JSVal handle that nothing
on the Haskell side referenced, then ran the threads it woke; a GC in one
of them dropped the handle before JavaScript read it. JavaScript now takes
the result first, then runs the threads (_mhs_js_run_threads).
- Snippets were cached by the address of their source string only. The C
compiler merges identical literals, so a synchronous and an interruptible
import with the same code shared one compiled function, and the synchronous
one could get a Promise. Async functions are now cached under the negated
address.
- A JSException value that rtsExn never converted (its callback died of it,
or a newer JavaScript exception replaced it) stayed in the handle table
forever. It is now dropped.
- throwTo from a callback blocked, which is fatal there, when the target
already had an exception pending. A helper thread (async_throwto) now
hands it over.
- JavaScript timer delays are rounded up to whole milliseconds, so a timer
does not fire before the thread is due, and capped at the largest delay
setTimeout accepts.
tests/JSCallback.hs has a regression test for each; all of them fail with
the previous runtime.
Data.Text.chunksOf: no chunks for a non-positive size; update JSVal.ref
chunksOf n with n <= 0 on a non-empty Text returned an infinite list of empty
Texts; the text package returns [].
JSVal.ref follows 05fbaba, which runs asynchronous callbacks as microtasks
(queueMicrotask) instead of with setTimeout, so they now run before the
other callbacks queued with setTimeout.
run_callback: restore the stack when showing an uncaught exception fails
When a callback died of an exception whose show raised another one, control
came back to run_callback with the failed show's stack still on top, and
POP(1) removed only one entry. mhs_js_callback then returned to the thread
that had called JavaScript with its stack misaligned, and that thread went
wrong (in the test the program stopped silently after the first call).
Reset stack_ptr to its value before the callback instead.
Only pass the ASYNCIFY options to emscripten's C compiler
A program with a javascript wrapper or interruptible import got -sASYNCIFY
and -sASYNCIFY_STACK_SIZE whatever the target, so compiling it for a native
target failed: the JavaScript code is left out there, but cc rejects the
options. mainCompileC now adds them only when the target's C compiler is
emcc.
tests/JSNative (make testjsnative, part of alltest) builds and runs such a
program natively.
Regenerate mhs.c (bootstrapped: stage2 equals stage1).
Raise a JavaScript exception as its JSVal, not through a global slot
A JavaScript exception from a safe or interruptible import was raised as the
RTS exception number 10, with its value left in a global (mhs_js_exn_handle)
for rtsExn to take later (js_take_exn). If another JavaScript exception
happened before the handler converted the first one (another thread running
in between), the first thread's handler got the second value and the second
got undefined. Values that were never converted also had to be dropped by
hand (mhs_js_set_exn, mhs_js_drop_exn).
Now the exception raised is the JSVal node itself, an ordinary foreign
pointer. primIsInt reports such a node as 10, and rtsExn wraps it in a
JSException. The value belongs to its exception from the start and the GC
frees it with the exception, so the global, js_take_exn, mhs_js_set_exn,
mhs_js_drop_exn and the special case in rts_exn_msg are gone. die_exn and
the callback error path show a JSVal exception with js_exn_string's text.
tests/JSCallback: the leak test forces a GC before measuring, since the
values of dead exceptions are now freed by the GC.
Regenerate mhs.c (bootstrapped: stage2 equals stage1) and base.pkg.
JavaScript FFI: stop the runtime after a fatal error in a callback; fix Ptr in interruptible imports
Found in a review of the JSVal/callback implementation:
- An 'interruptible' import with a Ptr argument did not compile: the
generated EM_ASYNC_JS wrapper declared the parameter as int, and clang
rejects the pointer conversion. The parameter is now void *.
- A fatal error inside a callback did not end the program in the browser.
exit() with a live runtime (kept alive for the callbacks) only throws to
the JavaScript caller, and a JavaScript exception from an 'unsafe' import
unwinds the C stack in the middle of run_callback; either way the page
kept calling into a runtime whose state was gone. The JavaScript side
now records that the runtime has stopped (Module.mhsjs.stop, also from an
atexit hook, and from the unsafe import wrappers before the exception
propagates) and every later call into Haskell throws an Error saying so.
- yield in a callback was fatal when another thread existed (it tried to
reschedule, and a callback must run to completion). It is a no-op there.
- textToJSString/textFromJSString truncated text at a NUL character, since
they went through NUL-terminated C strings. They pass the lengths now.
Tests added to tests/JSVal.hs and tests/JSCallback.hs; bootstrapped
(stage2 equals stage1) and generated/base.pkg regenerated.
JavaScript FFI: drop emscripten's ASYNCIFY
ASYNCIFY instrumented the whole evaluator (the runtime calls FFI
functions through pointers), which made it 3.2x slower and the output
31% larger. It was used for two things, both replaced:
- Waiting for the JavaScript event loop while Haskell threads are blocked.
Haskell threads keep their state in the heap (cleanup() discards the C
stack), so run_threads() now simply returns to JavaScript whenever the
run queue is empty and something on the JavaScript side can wake a
thread (a callback, a threadDelay timer, a settling Promise), also while
the main thread is blocked. mhs_main() then keeps the runtime alive, and
main_finish() (the old tail of main) runs when the main thread finishes
later, from mhs_js_run_threads().
- 'interruptible' imports. The import now returns the Promise of its
async JavaScript code as a JSVal, and the desugarer wraps it with the
new IO.jsawait primitive: an MVar that the JavaScript side fills when
the Promise settles (Module.mhsjs.await -> mhs_js_settle), and a
takeMVar. Only the calling thread waits; a rejection raises the
JSException. Packages with interruptible imports must be recompiled.
A callback called from the JavaScript event loop may now block or await
(it is left waiting like any other thread; its caller gets undefined, or a
resolved Promise); an uncaught exception after that is reported like one
in a forkIO thread. Callbacks that return a value, or that are called
from the JavaScript code of an import, still must not block.
Also: every call into the runtime from JavaScript goes through
Module.mhsjs.enter (fail-stop, ExitStatus is not an error), the JSKind
table lives in the desugarer only, and the stale mhs_js_async_* prototypes
are gone. Bootstrapped (stage2 equals stage1), base.pkg regenerated;
miso's 290 browser tests pass.
Fix CI: lex (##) as the empty unboxed tuple again; hlint suggestions
The rule that lets '(#' start an operator such as (#>) also caught (##),
the empty unboxed tuple, which unordered-containers uses, so the hackage
build failed with "undefined value: ##". A '#' right after '(#' now
keeps the unboxed tuple. tests/Unboxed covers both (##) and (#>).
Also fix the hlint suggestions in Data.Text.concatMap, GHC.Wasm.Prim,
tests/JSCallback and tests/JSNative.
Regenerate mhs.c (bootstrapped: stage2 equals stage1) and base.pkg.
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.
Makes
throwTo, and sokillThread, wait until the exception has been raised in the target thread, as GHC does. One commit on top of master (6a961d30).This was found while getting miso's test suite to pass with MicroHs. The change is part of the
jsvalbranch of haskell-miso/MicroHs (the JavaScript FFI for the emscripten target), but it has nothing to do with JavaScript: it is a change to the scheduler that applies to every target. Sincejsvalis large, this is split out as an independent piece that can be reviewed and merged on its own.The problem
GHC documents that
throwTodoes not return until the exception has been raised in the target thread. MicroHs put the exception in the target's exception MVar and returned at once. The target stayed runnable with the exception pending, so afterkillThread treturned,tcould still run for a while, and take values from MVars or write shared state, before it was interrupted.This was found in miso. Its integration tests start and tear down many apps in one page. Teardown kills the app's scheduler thread, and the next app posts its first redraw notification right after. With the old
throwTothe killed scheduler could still take that notification, so the next app never redrew: two tests were flaky (dmjio/miso#1663). The same pattern is in miso's hot reload, and in component unmounting, which kills the component's subscription threads and then continues the teardown.Code written for GHC relies on this guarantee in general: killing a worker and then reusing what it held,
timeout, cancelling anasync.The change
All of it is in the scheduler in
src/runtime/eval.c, about 50 lines of code:throwTowaits. After handing over the exception it puts the calling thread on the read queue of the target's exception MVar, and records the target in a new fieldmt_throwto.wake_throwtoreleases it when the target takes the exception (check_thrown), finishes, or dies. WhenthrowToruns again it seesmt_throwtoand returns.thread_intralready searches the read queues of all MVars, so an exception thrown to a thread that is waiting inthrowTois delivered as before.mt_throwto.Keeping
killThreadin a loop linearWaiting for the target made killing many runnable threads quadratic. The killer parked at the tail of the run queue, the target took the exception only after every thread ahead of it had run a slice, and then the woken killer waited again for a slice of every other thread. So:
throwTomoves a runnable target to the head of the run queue before it waits;wake_throwtoputs the woken thrower right after the target (or at the head if the target is gone).Each kill is then a couple of context switches.
Tests
tests/ThrowToSync.hs(new, intest):throwToto ourselves does not deadlockuninterruptibleMask:killThreadwaits until it leaves the maskmake runtestmhsandmake runtestpass, on macOS x86-64.Cost
killThreadnow does a couple of context switches instead of returning at once. Killing n threads that spin inyield(3 runs each, macOS x86-64):It grows linearly. Without the run queue change above it was quadratic: on the
jsvalbranch, 5,000 threads took 35 s and 20,000 over 300 s.System.Timeout.timeoutkills its timer thread on every call, so it is the most common place where a program kills threads often.ncalls oftimeout 1000000 (return x), and killingnthreads blocked on an MVar (3 runs each):timeout, 1,000 callstimeout, 10,000 callsSo
timeoutis unchanged at 1,000 calls and about 35% slower at 10,000, roughly 0.1 to 0.15 ms more per call.The "before" kill timings flatter the old
throwTo. WhenkillThreadreturns without waiting, these benchmarks print and exit before the killed threads have run at all, so that work is skipped, not done faster. A program that keeps running pays most of it later.Neither column grows linearly for
timeout(10 times the calls take 7 to 10 times as long, before and after). This change does not cause that. A possible cause isthread_intr, which looks through all MVars to find the one a thread is blocked on (see theXXXcomment there), but I have not confirmed it.Like GHC's,
throwTonow blocks for as long as the target is in an uninterruptible mask.This trades a small, measurable cost in
killThreadfor the guarantee that a killed thread has stopped what it was doing whenkillThreadreturns. A program can't work around the missing guarantee, while the cost only shows when killing thousands of threads.Not included
No generated files are changed: nothing in the compiler or the libraries changed, and
makelinks the new runtime.generated/mhseval.jsandweb-mhs/contain the runtime compiled with emscripten and are stale until the next regeneration.Update: two fixes from review
A review of this PR found two problems, fixed in the second commit (
ThrowToSynchas a test for each):throwTocould hang. When two threads throw to the same thread, the second waits for the target's exception slot, which holds the first exception. If the target finished or died without taking it, nothing released the second thrower, and it waited forever (this also happened before this PR). Now, when a thread finishes or dies, every thread waiting to throw to it is released (release_throwto), runsthrowToagain, finds the target gone, and returns.throwTostill delivered its exception. If a thread waiting inthrowTowas itself interrupted (e.g. killed), its exception stayed in the target's slot and was raised there later. In GHC an interruptedthrowTohas no effect. Now the interrupted thread takes its exception back, if the target has not taken it yet (revoke_throwto).Checked against GHC 9.14.1 with small programs: killing a thread whose thrower is interrupted (the target now finishes normally, as in GHC), and two threads killing each other (only one dies, as GHC guarantees).
make runtestmhspasses.