sx: step 11 — migrate prolog hook + add worker plugin

Move `hs-prolog-hook` / `hs-set-prolog-hook!` / `prolog` out of
`lib/hyperscript/runtime.sx` into a self-contained plugin file at
`lib/hyperscript/plugins/prolog.sx`. The API surface is preserved —
`lib/prolog/hs-bridge.sx::pl-install-hs-hook!` still calls
`hs-set-prolog-hook!` exactly as before, just resolved to the plugin
file's binding rather than runtime.sx's.

Move the E39 worker stub registration out of `lib/hyperscript/parser.sx`
into `lib/hyperscript/plugins/worker.sx`. The plugin calls
`(hs-register-feature! "worker" ...)` at file load time. Behaviour is
identical — `worker MyWorker ...` raises the same helpful "plugin not
installed" error, just routed through the registry from a separate
file. The pre-existing `behavioral` test for the helpful error
("raises a helpful error when the worker plugin is not installed")
still passes via the new path.

Wire-up:
- OCaml `bin/run_tests.ml`: load `plugins/worker.sx` and
  `plugins/prolog.sx` after `runtime.sx`, before `integration.sx`.
- JS `tests/hs-kernel-eval.js`: extend HS module list with
  `hs-worker` / `hs-prolog`; add `HS_PLUGINS` resolver branch so the
  `hs-` prefix maps to `lib/hyperscript/plugins/`.
- WASM `hosts/ocaml/browser/bundle.sh`: copy plugin files into
  `dist/sx/hs-<name>.sx`.
- WASM `hosts/ocaml/browser/compile-modules.js`: add `hs-worker` /
  `hs-prolog` to `FILES`, `HS_DEPS`, and `HS_LAZY` so the lazy loader
  resolves them on first reference.
- Worker plugin carries a sentinel `(define hs-worker-loaded? true)`
  so `extractDefines` indexes it in the module manifest (the lazy
  loader skips files with no defines).

Mirrors `shared/static/wasm/sx/hs-{parser,runtime}.sx` are byte-identical
to source; new mirrors `hs-{prolog,worker}.sx` written via sx_write_file.

OCaml: 4545 passed, 1339 failed — matches baseline.
JS: 2591 passed, 2465 failed — matches baseline.
Smoke tests: `(prolog ...)` raises "prolog hook not installed" cleanly,
`(hs-set-prolog-hook! ...)` then `(prolog ...)` returns the hook result,
`(hs-compile "worker MyWorker def noop() end end")` raises the worker
stub error via the registry path.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-05-07 01:20:32 +00:00
parent c08e217e2a
commit 6328b810bd
21 changed files with 492 additions and 106 deletions

View File

@@ -3373,9 +3373,6 @@
(hs-register-feature!
"when"
(fn (ctx) (begin ((dict-get ctx :adv!)) ((dict-get ctx :parse-when-feat)))))
(hs-register-feature!
"worker"
(fn (ctx) (error "worker plugin is not installed — see https://hyperscript.org/features/worker")))
(hs-register-feature!
"bind"
(fn (ctx) (begin ((dict-get ctx :adv!)) ((dict-get ctx :parse-bind-feat)))))

View File

@@ -0,0 +1,24 @@
;; lib/hyperscript/plugins/prolog.sx — Prolog plugin
;;
;; Provides the `prolog` HS-level function. Replaces the ad-hoc
;; hs-prolog-hook / hs-set-prolog-hook! slots that previously lived in
;; lib/hyperscript/runtime.sx (nodes 140142 of the plugin design doc).
;;
;; Two-step wiring preserves the original API:
;; 1. lib/prolog/runtime.sx loaded → defines pl-query-one
;; 2. lib/prolog/hs-bridge.sx (or this file's auto-wire) calls
;; (hs-set-prolog-hook! (fn (db goal) (not (= nil (pl-query-one db goal)))))
;; If neither is loaded, calling (prolog db goal) raises a clear error.
(define hs-prolog-hook nil)
(define hs-set-prolog-hook! (fn (f) (set! hs-prolog-hook f)))
(define
prolog
(fn
(db goal)
(if
(nil? hs-prolog-hook)
(raise "prolog hook not installed")
(hs-prolog-hook db goal))))

View File

@@ -0,0 +1,19 @@
;; lib/hyperscript/plugins/worker.sx — Worker plugin (stub)
;;
;; Phase 1 of the worker plugin: the registration formerly inlined in
;; lib/hyperscript/parser.sx (E39 stub) moves here. Behaviour is
;; identical — `worker MyWorker ...` raises a helpful error directing
;; users to the full plugin (not yet implemented).
;;
;; Phase 2 (future) replaces this stub with parse-worker-feat, a
;; compiler entry, hs-worker-define!, and the postMessage-based
;; method dispatch documented in plans/designs/hs-plugin-system.md §4a.
(define hs-worker-loaded? true)
(hs-register-feature!
"worker"
(fn
(ctx)
(error
"worker plugin is not installed — see https://hyperscript.org/features/worker")))

View File

@@ -2911,19 +2911,6 @@
((nth entry 2) val)))
_hs-dom-watchers)))
(define hs-prolog-hook nil)
(define hs-set-prolog-hook! (fn (f) (set! hs-prolog-hook f)))
(define
prolog
(fn
(db goal)
(if
(nil? hs-prolog-hook)
(raise "prolog hook not installed")
(hs-prolog-hook db goal))))
(define
hs-null-error!
(fn (selector) (raise (str "'" selector "' is null"))))

View File

@@ -4,7 +4,7 @@
;;
;; 1. Hook style — for `prolog(db, "goal(args)")` call syntax in Hyperscript:
;; (pl-install-hs-hook!) ;; call once at startup
;; Requires lib/hyperscript/runtime.sx (provides hs-set-prolog-hook!)
;; Requires lib/hyperscript/plugins/prolog.sx (provides hs-set-prolog-hook!)
;;
;; 2. Factory style — for named conditions like `when allowed(user, action)`:
;; (define allowed (pl-hs-predicate/2 pl-db "allowed"))