Skip to content

Give the GUI extension points instead of monkeypatching the VM - #40

Merged
Mistium merged 5 commits into
developfrom
vm-extension-points
Nov 1, 2026
Merged

Mistium merged 5 commits into
developfrom
vm-extension-points

Conversation

@Mistium

@Mistium Mistium commented Nov 1, 2026

Copy link
Copy Markdown
Member

Stacked on #39.

The GUI replaces VM methods at runtime because the VM offers no hooks for what it needs:

  • the debugger wraps runtime._step, greenFlag, startHats and _getMonitorThreadCount;
  • project replacement and the project fetcher wrap vm.loadProject;
  • mw-project-metadata.js wraps toJSON and deserializeProject;
  • the packager's pause and pointer-lock addons wrap the same step and hat methods.

This adds explicit extension points. The old methods stay ordinary writable prototype methods called through this., so third-party code that still patches them keeps working (tested).

runtime.addStepListener({before, after}) returns a remover. With no listeners, _step only checks two fields set in the constructor.

A real pause API: runtime.setPaused, isPaused, pauseThread(thread) and getPausedThreadState(thread), plus vm.setPaused/isPaused.

  • While paused, it stops the timer and holds every non-monitor thread, shifting thread timers forward on resume.
  • Only broadcast, clone and directly started edge-activated hats can start. Held threads count as stopped.
  • It emits RUNTIME_PAUSED/RUNTIME_UNPAUSED, which the VM forwards. stopAll and the green flag unpause.
  • A thread whose promise settles while paused keeps running after unpausing. The review caught this case, and it's fixed and tested.
  • Audio is not touched, so callers that want it suspended do that themselves.

vm.addLoadProjectInterceptor((input, options, next) => ...) returns a remover. The interceptor added last runs first, and a throw becomes a rejection.

vm.registerProjectMetadata(key, {serialize, deserialize}) returns a remover and covers extra fields in project.json meta. semver, vm, origin and platform are reserved, and errors are logged instead of failing a save or load. The VM has never carried unknown meta keys forward; today the GUI's own toJSON wrapper copies them across. So when the GUI moves to this registry, keys it doesn't register would stop surviving a round trip unless it keeps copying them.

None of this changes behaviour until the GUI adopts it, which is a follow-up PR. Each GUI patch site has a planned replacement:

  • the debugger and pointer lock move to addStepListener;
  • both pause implementations move to the pause API;
  • project replacement and the fetcher move to load interceptors;
  • the metadata wrapper moves to registerProjectMetadata.

Checks:

  • Lint and the conventions check pass, and snapshots are byte-identical.
  • Unit tap passes, apart from one interpreted timer fixture that failed once under heavy load and then passed three reruns.
  • Parity is identical to develop (2127/2143). Performance is 99.6% to 99.9% of develop across two separate runs.
  • An independent review checked how this interacts with the GUI's current patches; nothing collides before the GUI migrates.

There is no visible change.

The debugger and the packager pointer lock wrap runtime._step to run
code after every frame. addStepListener takes before and after
functions and returns a function that removes them. With no listeners
_step only checks two null fields.
The debugger and the packager both pause projects by wrapping
greenFlag, stopAll, startHats, sequencer.stepThreads and
_getMonitorThreadCount. runtime.setPaused now does the shared part:
it pauses the timer, holds every non-monitor thread until unpaused,
moves their timers forward by the paused time, does not start hats
other than broadcasts, clones and edge-activated hats called directly,
skips edge-activated hats in the step, counts held threads as stopped,
and emits RUNTIME_PAUSED and RUNTIME_UNPAUSED. stopAll and so the green
flag unpause. pauseThread and getPausedThreadState let the debugger hold
threads it starts while single stepping.
Project replacement, the project fetcher and collaboration replace
vm.loadProject to check, schedule or follow loads. An interceptor gets
the input, the options and a next function, can change either before
calling next, and can refuse a load by throwing. The last one added
runs first, and removing one does not depend on the order they were
added in. Without interceptors loadProject loads directly.
The GUI wraps toJSON and deserializeProject to stamp authorship and
dates into project.json, re-parsing the whole project on every save.
A registered key's serialize runs when a whole project is saved and its
result is written into meta before stringifying; deserialize runs on
every project load with that key's loaded value, or undefined. The
built-in keys semver, vm, origin and platform are reserved, and a
handler that throws is logged without failing the save or load.
A thread paused while waiting on a promise is held in PROMISE_WAIT. If the promise settled
and unpausing came before the next step, its saved PROMISE_WAIT was restored over the
RUNNING the promise had set, and nothing woke it again. Unpausing now restores the saved
status only for threads still held in PROMISE_WAIT.
@Mistium
Mistium force-pushed the vm-extension-points branch from adb7b23 to 5a41b1a Compare November 1, 2026 06:50
@Mistium
Mistium changed the base branch from vm-bug-fixes to develop November 1, 2026 06:50
@Mistium
Mistium merged commit d9873d7 into develop Nov 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant