Integration: - hs-collect-vars: scan compiled SX for set! targets, collect symbols - hs-handler: pre-declare collected variables in closure let-bindings so increment/decrement work on first use (variable persists across event handler calls via closure scope) Compiler: - Fix emit-inc/emit-dec: use expr (variable) not tgt-override (element) - Simplify to plain (set! x (+ x amount)) since vars are pre-declared Mock DOM: - Add mock console object to host-global - Add console handler (no-op) to host-call dispatch - Override console-log/debug/error as no-op primitives to avoid str hitting circular refs in mock DOM elements Fixes 4 log timeouts, 2+ increment/decrement failures. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
92 lines
3.0 KiB
Plaintext
92 lines
3.0 KiB
Plaintext
;; _hyperscript integration — wire _="..." attributes to compiled SX
|
|
;;
|
|
;; Entry points:
|
|
;; (hs-handler src) — compile source to callable (fn (me) ...)
|
|
;; (hs-activate! el) — activate hyperscript on a single element
|
|
;; (hs-boot!) — scan DOM, activate all _="..." elements
|
|
;; (hs-boot-subtree! root) — activate within a subtree (for HTMX swaps)
|
|
|
|
;; ── Compile source to a handler function ────────────────────────
|
|
;; Returns a function (fn (me) ...) that can be called with a DOM element.
|
|
;; Uses eval-expr-cek to turn the SX data structure into a live closure.
|
|
|
|
(begin
|
|
(define
|
|
hs-collect-vars
|
|
(fn
|
|
(sx)
|
|
(define vars (list))
|
|
(define
|
|
walk
|
|
(fn
|
|
(node)
|
|
(when
|
|
(list? node)
|
|
(when
|
|
(and
|
|
(> (len node) 1)
|
|
(= (first node) (quote set!))
|
|
(symbol? (nth node 1)))
|
|
(let
|
|
((name (nth node 1)))
|
|
(when
|
|
(not (some (fn (v) (= v name)) vars))
|
|
(set! vars (cons name vars)))))
|
|
(for-each walk node))))
|
|
(walk sx)
|
|
vars))
|
|
(define
|
|
hs-handler
|
|
(fn
|
|
(src)
|
|
(let
|
|
((sx (hs-to-sx-from-source src)))
|
|
(let
|
|
((extra-vars (hs-collect-vars sx)))
|
|
(let
|
|
((bindings (append (list (list (quote it) nil) (list (quote event) nil)) (map (fn (v) (list v nil)) extra-vars))))
|
|
(eval-expr-cek
|
|
(list
|
|
(quote fn)
|
|
(list (quote me))
|
|
(list (quote let) bindings sx)))))))))
|
|
|
|
;; ── Activate a single element ───────────────────────────────────
|
|
;; Reads the _="..." attribute, compiles, and executes with me=element.
|
|
;; Marks the element to avoid double-activation.
|
|
|
|
(define
|
|
hs-activate!
|
|
(fn
|
|
(el)
|
|
(let
|
|
((src (dom-get-attr el "_")))
|
|
(when
|
|
(and src (not (dom-get-data el "hs-active")))
|
|
(dom-set-data el "hs-active" true)
|
|
(let ((handler (hs-handler src))) (handler el))))))
|
|
|
|
;; ── Boot: scan entire document ──────────────────────────────────
|
|
;; Called once at page load. Finds all elements with _ attribute,
|
|
;; compiles their hyperscript, and activates them.
|
|
|
|
(define
|
|
hs-boot!
|
|
(fn
|
|
()
|
|
(let
|
|
((elements (dom-query-all (host-get (host-global "document") "body") "[_]")))
|
|
(for-each (fn (el) (hs-activate! el)) elements))))
|
|
|
|
;; ── Boot subtree: for dynamic content ───────────────────────────
|
|
;; Called after HTMX swaps or dynamic DOM insertion.
|
|
;; Only activates elements within the given root.
|
|
|
|
(define
|
|
hs-boot-subtree!
|
|
(fn
|
|
(root)
|
|
(let
|
|
((elements (dom-query-all root "[_]")))
|
|
(for-each (fn (el) (hs-activate! el)) elements))
|
|
(when (dom-get-attr root "_") (hs-activate! root)))) |