HS: custom conversion API + asExpression tests (+2)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 39s

Add _hs-custom-conversions dict and _hs-dynamic-converters list to
runtime.sx. hs-set-conversion!/hs-clear-conversion!/hs-add-dynamic-converter!/
hs-pop-dynamic-converter!/hs-clear-converters! helpers expose the API.
hs-coerce fallback now checks static dict then dynamic resolvers before
returning value unchanged.

Hand-roll MANUAL_TEST_BODIES for "can accept custom conversions" and
"can accept custom dynamic conversions" — previously SKIP (untranslated).

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-05-05 22:35:42 +00:00
parent 2f8abb18a3
commit 0753982a02
4 changed files with 183 additions and 34 deletions

View File

@@ -1156,6 +1156,41 @@
"}")))
(true (hs-json-escape (str v))))))
(begin
(define _hs-custom-conversions {})
(define _hs-dynamic-converters (list))
(define
hs-set-conversion!
(fn (name conv-fn) (dict-set! _hs-custom-conversions name conv-fn)))
(define
hs-clear-conversion!
(fn (name) (dict-set! _hs-custom-conversions name nil)))
(define
hs-add-dynamic-converter!
(fn
(conv-fn)
(set!
_hs-dynamic-converters
(append _hs-dynamic-converters (list conv-fn)))))
(define
hs-pop-dynamic-converter!
(fn
()
(let
((n (len _hs-dynamic-converters)))
(when
(> n 0)
(set!
_hs-dynamic-converters
(slice _hs-dynamic-converters 0 (- n 1)))))))
(define
hs-clear-converters!
(fn
()
(do
(set! _hs-custom-conversions {})
(set! _hs-dynamic-converters (list))))))
(define
hs-coerce
(fn
@@ -1280,7 +1315,15 @@
(filter (fn (k) (not (= k "_order"))) (keys value))))
m)))
((= type-name "Date") (host-new "Date" value))
(true value))))
(true
(let
((static-fn (get _hs-custom-conversions type-name)))
(if
(not (nil? static-fn))
(static-fn value)
(let
((dynamic-result (reduce (fn (acc resolver) (if (not (nil? acc)) acc (resolver type-name value))) nil _hs-dynamic-converters)))
(if (not (nil? dynamic-result)) dynamic-result value))))))))
(define
hs-gather-form-nodes
@@ -2722,6 +2765,8 @@
((store (host-get el "__hs_vars")))
(if (nil? store) nil (host-get store name)))))
;; ── SourceInfo API ────────────────────────────────────────────────
(define
hs-dom-set-var-raw!
(fn
@@ -2735,8 +2780,6 @@
(host-set! (host-get el "__hs_vars") name val)
(when changed (hs-dom-fire-watchers! el name val))))))
;; ── SourceInfo API ────────────────────────────────────────────────
(define
hs-dom-resolve-start
(fn