Compare commits
14 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| cb59fbba13 | |||
| 54b54f4e19 | |||
| 92adf9d496 | |||
| cabb0467ab | |||
| 820132b839 | |||
| 7480c0f9c9 | |||
| c36fd5b208 | |||
| 61c9697f67 | |||
| 8e8c2a73d6 | |||
| 4b69650336 | |||
| 11ee71d846 | |||
| 835fffb834 | |||
| bb18c05083 | |||
| 6a1cbdcbdb |
File diff suppressed because it is too large
Load Diff
@@ -19,6 +19,7 @@
|
||||
(define
|
||||
reserved
|
||||
(list
|
||||
(quote beingTold)
|
||||
(quote me)
|
||||
(quote it)
|
||||
(quote event)
|
||||
@@ -65,7 +66,10 @@
|
||||
(list (quote me))
|
||||
(list
|
||||
(quote let)
|
||||
(list (list (quote it) nil) (list (quote event) nil))
|
||||
(list
|
||||
(list (quote beingTold) (quote me))
|
||||
(list (quote it) nil)
|
||||
(list (quote event) nil))
|
||||
guarded))))))))))
|
||||
|
||||
;; ── Activate a single element ───────────────────────────────────
|
||||
|
||||
@@ -9,7 +9,9 @@
|
||||
(fn
|
||||
(tokens src)
|
||||
(let
|
||||
((p 0) (tok-len (len tokens)))
|
||||
((tokens (filter (fn (t) (not (= (get t "type") "whitespace"))) tokens))
|
||||
(p 0)
|
||||
(tok-len (len (filter (fn (t) (not (= (get t "type") "whitespace"))) tokens))))
|
||||
(define tp (fn () (if (< p tok-len) (nth tokens p) nil)))
|
||||
(define
|
||||
tp-type
|
||||
@@ -123,19 +125,23 @@
|
||||
((and (= kind (quote closest)) (= typ "ident") (= val "parent"))
|
||||
(do (adv!) (parse-trav (quote closest-parent))))
|
||||
((= typ "selector")
|
||||
(do (adv!) (list kind val (list (quote me)))))
|
||||
(do (adv!) (list kind val (list (quote beingTold)))))
|
||||
((= typ "class")
|
||||
(do (adv!) (list kind (str "." val) (list (quote me)))))
|
||||
(do
|
||||
(adv!)
|
||||
(list kind (str "." val) (list (quote beingTold)))))
|
||||
((= typ "id")
|
||||
(do (adv!) (list kind (str "#" val) (list (quote me)))))
|
||||
(do
|
||||
(adv!)
|
||||
(list kind (str "#" val) (list (quote beingTold)))))
|
||||
((= typ "attr")
|
||||
(do
|
||||
(adv!)
|
||||
(list
|
||||
(quote attr)
|
||||
val
|
||||
(list kind (str "[" val "]") (list (quote me))))))
|
||||
(true (list kind "*" (list (quote me))))))))
|
||||
(list kind (str "[" val "]") (list (quote beingTold))))))
|
||||
(true (list kind "*" (list (quote beingTold))))))))
|
||||
(define
|
||||
parse-pos-kw
|
||||
(fn
|
||||
@@ -270,12 +276,18 @@
|
||||
l
|
||||
{}))))
|
||||
((= typ "attr")
|
||||
(do (adv!) (list (quote attr) val (list (quote me)))))
|
||||
(do
|
||||
(adv!)
|
||||
(list (quote attr) val (list (quote beingTold)))))
|
||||
((= typ "style")
|
||||
(do (adv!) (list (quote style) val (list (quote me)))))
|
||||
(do
|
||||
(adv!)
|
||||
(list (quote style) val (list (quote beingTold)))))
|
||||
((= typ "local") (do (adv!) (list (quote local) val)))
|
||||
((= typ "hat")
|
||||
(do (adv!) (list (quote dom-ref) val (list (quote me)))))
|
||||
(do
|
||||
(adv!)
|
||||
(list (quote dom-ref) val (list (quote beingTold)))))
|
||||
((and (= typ "keyword") (= val "dom"))
|
||||
(do
|
||||
(adv!)
|
||||
@@ -283,7 +295,7 @@
|
||||
((name (tp-val)))
|
||||
(do
|
||||
(adv!)
|
||||
(list (quote dom-ref) name (list (quote me)))))))
|
||||
(list (quote dom-ref) name (list (quote beingTold)))))))
|
||||
((= typ "class")
|
||||
(let
|
||||
((s (cur-start)) (l (cur-line)))
|
||||
@@ -982,7 +994,7 @@
|
||||
(collect-classes!))))
|
||||
(collect-classes!)
|
||||
(let
|
||||
((tgt (if (match-kw "to") (parse-expr) (list (quote me)))))
|
||||
((tgt (if (match-kw "to") (parse-expr) (list (quote beingTold)))))
|
||||
(let
|
||||
((when-clause (if (match-kw "when") (parse-expr) nil)))
|
||||
(if
|
||||
@@ -1011,7 +1023,7 @@
|
||||
(get (adv!) "value")
|
||||
(parse-expr))))
|
||||
(let
|
||||
((tgt (if (match-kw "to") (parse-expr) (list (quote me)))))
|
||||
((tgt (if (match-kw "to") (parse-expr) (list (quote beingTold)))))
|
||||
(list (quote set-style) prop value tgt))))
|
||||
((= (tp-type) "brace-open")
|
||||
(do
|
||||
@@ -1036,7 +1048,7 @@
|
||||
(collect-pairs!)
|
||||
(when (= (tp-type) "brace-close") (adv!))
|
||||
(let
|
||||
((tgt (if (match-kw "to") (parse-expr) (list (quote me)))))
|
||||
((tgt (if (match-kw "to") (parse-expr) (list (quote beingTold)))))
|
||||
(list (quote set-styles) (reverse pairs) tgt)))))
|
||||
((and (= (tp-type) "bracket-open") (> (len tokens) (+ p 1)) (= (get (nth tokens (+ p 1)) "type") "attr"))
|
||||
(do
|
||||
@@ -1048,7 +1060,7 @@
|
||||
((attr-val (parse-expr)))
|
||||
(when (= (tp-type) "bracket-close") (adv!))
|
||||
(let
|
||||
((tgt (parse-tgt-kw "to" (list (quote me)))))
|
||||
((tgt (parse-tgt-kw "to" (list (quote beingTold)))))
|
||||
(let
|
||||
((when-clause (if (match-kw "when") (parse-expr) nil)))
|
||||
(if
|
||||
@@ -1066,7 +1078,7 @@
|
||||
(let
|
||||
((attr-val (if (and (= (tp-type) "op") (= (tp-val) "=")) (do (adv!) (parse-expr)) "")))
|
||||
(let
|
||||
((tgt (if (match-kw "to") (parse-expr) (list (quote me)))))
|
||||
((tgt (if (match-kw "to") (parse-expr) (list (quote beingTold)))))
|
||||
(let
|
||||
((when-clause (if (match-kw "when") (parse-expr) nil)))
|
||||
(if
|
||||
@@ -1107,7 +1119,7 @@
|
||||
(collect-classes!))))
|
||||
(collect-classes!)
|
||||
(let
|
||||
((tgt (if (match-kw "from") (parse-expr) (list (quote me)))))
|
||||
((tgt (if (match-kw "from") (parse-expr) (list (quote beingTold)))))
|
||||
(if
|
||||
(empty? extra-classes)
|
||||
(list (quote remove-class) cls tgt)
|
||||
@@ -1118,7 +1130,7 @@
|
||||
(let
|
||||
((attr-name (get (adv!) "value")))
|
||||
(let
|
||||
((tgt (if (match-kw "from") (parse-expr) (list (quote me)))))
|
||||
((tgt (if (match-kw "from") (parse-expr) (list (quote beingTold)))))
|
||||
(list (quote remove-attr) attr-name tgt))))
|
||||
((and (= (tp-type) "bracket-open") (= (tp-val) "["))
|
||||
(do
|
||||
@@ -1180,7 +1192,7 @@
|
||||
(let
|
||||
((cls2 (do (let ((v (tp-val))) (adv!) v))))
|
||||
(let
|
||||
((tgt (parse-tgt-kw "on" (list (quote me)))))
|
||||
((tgt (parse-tgt-kw "on" (list (quote beingTold)))))
|
||||
(list (quote toggle-between) cls1 cls2 tgt)))
|
||||
nil)))
|
||||
((and (= (tp-type) "bracket-open") (> (len tokens) (+ p 1)) (= (get (nth tokens (+ p 1)) "type") "attr"))
|
||||
@@ -1205,7 +1217,7 @@
|
||||
((v2 (parse-expr)))
|
||||
(when (= (tp-type) "bracket-close") (adv!))
|
||||
(let
|
||||
((tgt (parse-tgt-kw "on" (list (quote me)))))
|
||||
((tgt (parse-tgt-kw "on" (list (quote beingTold)))))
|
||||
(if
|
||||
(= n1 n2)
|
||||
(list
|
||||
@@ -1239,7 +1251,7 @@
|
||||
(let
|
||||
((extra-classes (collect-classes (list))))
|
||||
(let
|
||||
((tgt (parse-tgt-kw "on" (list (quote me)))))
|
||||
((tgt (parse-tgt-kw "on" (list (quote beingTold)))))
|
||||
(cond
|
||||
((> (len extra-classes) 0)
|
||||
(list
|
||||
@@ -1268,7 +1280,7 @@
|
||||
(let
|
||||
((prop (get (adv!) "value")))
|
||||
(let
|
||||
((tgt (if (match-kw "of") (parse-expr) (list (quote me)))))
|
||||
((tgt (if (match-kw "of") (parse-expr) (list (quote beingTold)))))
|
||||
(if
|
||||
(match-kw "between")
|
||||
(let
|
||||
@@ -1339,7 +1351,7 @@
|
||||
(let
|
||||
((attr-name (get (adv!) "value")))
|
||||
(let
|
||||
((tgt (if (match-kw "on") (parse-expr) (list (quote me)))))
|
||||
((tgt (if (match-kw "on") (parse-expr) (list (quote beingTold)))))
|
||||
(if
|
||||
(match-kw "between")
|
||||
(let
|
||||
@@ -1364,7 +1376,7 @@
|
||||
((attr-val (parse-expr)))
|
||||
(when (= (tp-type) "bracket-close") (adv!))
|
||||
(let
|
||||
((tgt (parse-tgt-kw "on" (list (quote me)))))
|
||||
((tgt (parse-tgt-kw "on" (list (quote beingTold)))))
|
||||
(list (quote toggle-attr-val) attr-name attr-val tgt))))))
|
||||
((and (= (tp-type) "keyword") (= (tp-val) "my"))
|
||||
(do
|
||||
@@ -1592,7 +1604,7 @@
|
||||
(let
|
||||
((dtl (if (= (tp-type) "paren-open") (parse-detail-dict) nil)))
|
||||
(let
|
||||
((tgt (parse-tgt-kw "to" (list (quote me)))))
|
||||
((tgt (parse-tgt-kw "to" (list (quote beingTold)))))
|
||||
(if
|
||||
dtl
|
||||
(list (quote send) name dtl tgt)
|
||||
@@ -1606,7 +1618,7 @@
|
||||
(let
|
||||
((dtl (if (= (tp-type) "paren-open") (parse-detail-dict) nil)))
|
||||
(let
|
||||
((tgt (parse-tgt-kw "on" (list (quote me)))))
|
||||
((tgt (parse-tgt-kw "on" (list (quote beingTold)))))
|
||||
(if
|
||||
dtl
|
||||
(list (quote trigger) name dtl tgt)
|
||||
@@ -1645,7 +1657,7 @@
|
||||
(fn
|
||||
()
|
||||
(let
|
||||
((tgt (cond ((at-end?) (list (quote me))) ((and (= (tp-type) "keyword") (or (= (tp-val) "then") (= (tp-val) "end") (= (tp-val) "with") (= (tp-val) "when") (= (tp-val) "add") (= (tp-val) "remove") (= (tp-val) "set") (= (tp-val) "put") (= (tp-val) "toggle") (= (tp-val) "hide") (= (tp-val) "show") (= (tp-val) "on"))) (list (quote me))) (true (parse-expr)))))
|
||||
((tgt (cond ((at-end?) (list (quote beingTold))) ((and (= (tp-type) "keyword") (or (= (tp-val) "then") (= (tp-val) "end") (= (tp-val) "with") (= (tp-val) "when") (= (tp-val) "add") (= (tp-val) "remove") (= (tp-val) "set") (= (tp-val) "put") (= (tp-val) "toggle") (= (tp-val) "hide") (= (tp-val) "show") (= (tp-val) "on"))) (list (quote beingTold))) (true (parse-expr)))))
|
||||
(let
|
||||
((strategy (if (match-kw "with") (if (at-end?) "display" (let ((s (tp-val))) (do (adv!) (cond ((at-end?) s) ((= (tp-type) "colon") (do (adv!) (let ((v (tp-val))) (do (adv!) (str s ":" v))))) ((= (tp-type) "local") (let ((v (tp-val))) (do (adv!) (str s ":" v)))) (true s))))) "display")))
|
||||
(let
|
||||
@@ -1656,7 +1668,7 @@
|
||||
(fn
|
||||
()
|
||||
(let
|
||||
((tgt (cond ((at-end?) (list (quote me))) ((and (= (tp-type) "keyword") (or (= (tp-val) "then") (= (tp-val) "end") (= (tp-val) "with") (= (tp-val) "when") (= (tp-val) "add") (= (tp-val) "remove") (= (tp-val) "set") (= (tp-val) "put") (= (tp-val) "toggle") (= (tp-val) "hide") (= (tp-val) "show") (= (tp-val) "on"))) (list (quote me))) (true (parse-expr)))))
|
||||
((tgt (cond ((at-end?) (list (quote beingTold))) ((and (= (tp-type) "keyword") (or (= (tp-val) "then") (= (tp-val) "end") (= (tp-val) "with") (= (tp-val) "when") (= (tp-val) "add") (= (tp-val) "remove") (= (tp-val) "set") (= (tp-val) "put") (= (tp-val) "toggle") (= (tp-val) "hide") (= (tp-val) "show") (= (tp-val) "on"))) (list (quote beingTold))) (true (parse-expr)))))
|
||||
(let
|
||||
((strategy (if (match-kw "with") (if (at-end?) "display" (let ((s (tp-val))) (do (adv!) (cond ((at-end?) s) ((= (tp-type) "colon") (do (adv!) (let ((v (tp-val))) (do (adv!) (str s ":" v))))) ((= (tp-type) "local") (let ((v (tp-val))) (do (adv!) (str s ":" v)))) (true s))))) "display")))
|
||||
(let
|
||||
@@ -1682,7 +1694,7 @@
|
||||
((from-val (if (match-kw "from") (let ((v (parse-atom))) (if (and v (= (tp-type) "ident") (not (hs-keyword? (tp-val)))) (let ((unit (get (adv!) "value"))) (list (quote string-postfix) v unit)) v)) nil)))
|
||||
(expect-kw! "to")
|
||||
(let
|
||||
((value (let ((v (parse-atom))) (if (and v (= (tp-type) "ident") (not (hs-keyword? (tp-val)))) (let ((unit (get (adv!) "value"))) (list (quote string-postfix) v unit)) v))))
|
||||
((value (if (and (= (tp-type) "ident") (= (tp-val) "initial")) (do (adv!) "initial") (let ((v (parse-atom))) (if (and v (= (tp-type) "ident") (not (hs-keyword? (tp-val)))) (let ((unit (get (adv!) "value"))) (list (quote string-postfix) v unit)) v)))))
|
||||
(let
|
||||
((dur (if (match-kw "over") (let ((v (parse-atom))) (if (and (number? v) (= (tp-type) "ident") (not (hs-keyword? (tp-val)))) (let ((unit (get (adv!) "value"))) (list (quote string-postfix) v unit)) v)) nil)))
|
||||
(let
|
||||
@@ -2158,21 +2170,21 @@
|
||||
(if
|
||||
(match-kw "of")
|
||||
(list (quote style) val (parse-expr))
|
||||
(list (quote style) val (list (quote me))))))
|
||||
(list (quote style) val (list (quote beingTold))))))
|
||||
((= typ "attr")
|
||||
(do
|
||||
(adv!)
|
||||
(if
|
||||
(match-kw "of")
|
||||
(list (quote attr) val (parse-expr))
|
||||
(list (quote attr) val (list (quote me))))))
|
||||
(list (quote attr) val (list (quote beingTold))))))
|
||||
((= typ "class")
|
||||
(do
|
||||
(adv!)
|
||||
(if
|
||||
(match-kw "of")
|
||||
(list (quote has-class?) (parse-expr) val)
|
||||
(list (quote has-class?) (list (quote me)) val))))
|
||||
(list (quote has-class?) (list (quote beingTold)) val))))
|
||||
((= typ "selector")
|
||||
(do
|
||||
(adv!)
|
||||
@@ -2320,13 +2332,15 @@
|
||||
()
|
||||
(let
|
||||
((tgt (parse-expr)))
|
||||
(list (quote measure) (if (nil? tgt) (list (quote me)) tgt)))))
|
||||
(list
|
||||
(quote measure)
|
||||
(if (nil? tgt) (list (quote beingTold)) tgt)))))
|
||||
(define
|
||||
parse-scroll-cmd
|
||||
(fn
|
||||
()
|
||||
(let
|
||||
((tgt (if (or (at-end?) (and (= (tp-type) "keyword") (or (= (tp-val) "then") (= (tp-val) "end")))) (list (quote me)) (parse-expr))))
|
||||
((tgt (if (or (at-end?) (and (= (tp-type) "keyword") (or (= (tp-val) "then") (= (tp-val) "end")))) (list (quote beingTold)) (parse-expr))))
|
||||
(let
|
||||
((pos (cond ((match-kw "top") "top") ((match-kw "bottom") "bottom") ((match-kw "left") "left") ((match-kw "right") "right") (true "top"))))
|
||||
(list (quote scroll!) tgt pos)))))
|
||||
@@ -2335,14 +2349,14 @@
|
||||
(fn
|
||||
()
|
||||
(let
|
||||
((tgt (if (or (at-end?) (and (= (tp-type) "keyword") (or (= (tp-val) "then") (= (tp-val) "end")))) (list (quote me)) (parse-expr))))
|
||||
((tgt (if (or (at-end?) (and (= (tp-type) "keyword") (or (= (tp-val) "then") (= (tp-val) "end")))) (list (quote beingTold)) (parse-expr))))
|
||||
(list (quote select!) tgt))))
|
||||
(define
|
||||
parse-reset-cmd
|
||||
(fn
|
||||
()
|
||||
(let
|
||||
((tgt (if (or (at-end?) (and (= (tp-type) "keyword") (or (= (tp-val) "then") (= (tp-val) "end")))) (list (quote me)) (parse-expr))))
|
||||
((tgt (if (or (at-end?) (and (= (tp-type) "keyword") (or (= (tp-val) "then") (= (tp-val) "end")))) (list (quote beingTold)) (parse-expr))))
|
||||
(list (quote reset!) tgt))))
|
||||
(define
|
||||
parse-default-cmd
|
||||
@@ -2367,7 +2381,7 @@
|
||||
(fn
|
||||
()
|
||||
(let
|
||||
((tgt (cond ((at-end?) (list (quote me))) ((and (= (tp-type) "keyword") (or (= (tp-val) "then") (= (tp-val) "end"))) (list (quote me))) (true (parse-expr)))))
|
||||
((tgt (cond ((at-end?) (list (quote beingTold))) ((and (= (tp-type) "keyword") (or (= (tp-val) "then") (= (tp-val) "end"))) (list (quote beingTold))) (true (parse-expr)))))
|
||||
(list (quote focus!) tgt))))
|
||||
(define
|
||||
parse-feat-body
|
||||
@@ -2481,7 +2495,7 @@
|
||||
(fn
|
||||
()
|
||||
(let
|
||||
((target (cond ((at-end?) (list (quote ref) "me")) ((and (= (tp-type) "keyword") (or (= (tp-val) "then") (= (tp-val) "end"))) (list (quote ref) "me")) (true (parse-expr)))))
|
||||
((target (cond ((at-end?) (list (quote beingTold))) ((and (= (tp-type) "keyword") (or (= (tp-val) "then") (= (tp-val) "end"))) (list (quote beingTold))) (true (parse-expr)))))
|
||||
(list (quote empty-target) target))))
|
||||
(define
|
||||
parse-swap-cmd
|
||||
@@ -2506,15 +2520,42 @@
|
||||
(fn
|
||||
()
|
||||
(let
|
||||
((target (cond ((at-end?) (list (quote me))) ((and (= (tp-type) "keyword") (or (= (tp-val) "then") (= (tp-val) "end"))) (list (quote me))) (true (parse-expr)))))
|
||||
((target (cond ((at-end?) (list (quote beingTold))) ((and (= (tp-type) "keyword") (or (= (tp-val) "then") (= (tp-val) "end"))) (list (quote beingTold))) (true (parse-expr)))))
|
||||
(list (quote open-element) target))))
|
||||
(define
|
||||
parse-close-cmd
|
||||
(fn
|
||||
()
|
||||
(let
|
||||
((target (cond ((at-end?) (list (quote me))) ((and (= (tp-type) "keyword") (or (= (tp-val) "then") (= (tp-val) "end"))) (list (quote me))) (true (parse-expr)))))
|
||||
((target (cond ((at-end?) (list (quote beingTold))) ((and (= (tp-type) "keyword") (or (= (tp-val) "then") (= (tp-val) "end"))) (list (quote beingTold))) (true (parse-expr)))))
|
||||
(list (quote close-element) target))))
|
||||
(define
|
||||
parse-js-block
|
||||
(fn
|
||||
()
|
||||
(let
|
||||
((params (if (= (tp-type) "paren-open") (do (adv!) (define collect-params! (fn (acc) (cond ((or (at-end?) (= (tp-type) "paren-close")) (do (when (= (tp-type) "paren-close") (adv!)) acc)) ((= (tp-type) "comma") (do (adv!) (collect-params! acc))) (true (let ((pname (tp-val))) (do (adv!) (collect-params! (append acc pname)))))))) (collect-params! (list))) (list))))
|
||||
(let
|
||||
((js-start (cur-start)))
|
||||
(define
|
||||
skip-to-end!
|
||||
(fn
|
||||
()
|
||||
(if
|
||||
(or
|
||||
(at-end?)
|
||||
(and (= (tp-type) "keyword") (= (tp-val) "end")))
|
||||
nil
|
||||
(do (adv!) (skip-to-end!)))))
|
||||
(skip-to-end!)
|
||||
(let
|
||||
((js-end (cur-start)))
|
||||
(let
|
||||
((js-src (substring src js-start js-end)))
|
||||
(when
|
||||
(and (= (tp-type) "keyword") (= (tp-val) "end"))
|
||||
(adv!))
|
||||
(list (quote js-block) params js-src)))))))
|
||||
(define
|
||||
parse-cmd
|
||||
(fn
|
||||
@@ -2664,6 +2705,8 @@
|
||||
(do (adv!) (list (quote continue))))
|
||||
((and (= typ "keyword") (or (= val "exit") (= val "halt")))
|
||||
(do (adv!) (list (quote exit))))
|
||||
((and (= typ "keyword") (= val "js"))
|
||||
(do (adv!) (parse-js-block)))
|
||||
(true (parse-expr))))))
|
||||
(define
|
||||
parse-cmd-list
|
||||
@@ -2719,7 +2762,8 @@
|
||||
(= v "close")
|
||||
(= v "pick")
|
||||
(= v "ask")
|
||||
(= v "answer"))))
|
||||
(= v "answer")
|
||||
(= v "js"))))
|
||||
(define
|
||||
cl-collect
|
||||
(fn
|
||||
@@ -2906,63 +2950,6 @@
|
||||
(match-kw "end")
|
||||
(list (quote when-feat-no-op)))))
|
||||
(do (pwf-skip) (match-kw "end") (list (quote when-feat-no-op))))))
|
||||
(define
|
||||
parse-socket-feat
|
||||
(fn
|
||||
()
|
||||
(let
|
||||
((seg0 (tp-val)))
|
||||
(adv!)
|
||||
(define
|
||||
collect-segs
|
||||
(fn
|
||||
(acc)
|
||||
(if
|
||||
(= (tp-type) "class")
|
||||
(let
|
||||
((seg (tp-val)))
|
||||
(adv!)
|
||||
(collect-segs (append acc (list seg))))
|
||||
acc)))
|
||||
(let
|
||||
((name-path (collect-segs (list seg0))))
|
||||
(define
|
||||
url-cont?
|
||||
(fn
|
||||
()
|
||||
(or
|
||||
(= (tp-type) "ident")
|
||||
(= (tp-type) "number")
|
||||
(= (tp-type) "op")
|
||||
(= (tp-type) "colon")
|
||||
(= (tp-type) "dot")
|
||||
(and
|
||||
(= (tp-type) "keyword")
|
||||
(not
|
||||
(or
|
||||
(= (tp-val) "end")
|
||||
(= (tp-val) "with")
|
||||
(= (tp-val) "on")
|
||||
(= (tp-val) "as")))))))
|
||||
(define
|
||||
collect-url
|
||||
(fn
|
||||
(parts)
|
||||
(if
|
||||
(and (not (at-end?)) (url-cont?))
|
||||
(let
|
||||
((v (tp-val)))
|
||||
(adv!)
|
||||
(collect-url (append parts (list v))))
|
||||
(join "" parts))))
|
||||
(let
|
||||
((url (cond ((and (= (tp-type) "op") (= (tp-val) "/")) (do (adv!) (collect-url (list "/")))) ((= (tp-type) "ident") (let ((scheme (tp-val))) (adv!) (if (= (tp-type) "colon") (collect-url (list scheme)) (parse-arith (parse-poss (list (quote ref) scheme)))))) (true (parse-atom)))))
|
||||
(let
|
||||
((timeout-ms (if (match-kw "with") (do (adv!) (parse-expr)) nil)))
|
||||
(let
|
||||
((on-msg (if (match-kw "on") (do (adv!) (let ((json? (if (match-kw "as") (do (adv!) true) false))) (let ((body (parse-cmd-list))) (list (quote on-message) json? body)))) nil)))
|
||||
(match-kw "end")
|
||||
(list (quote socket) name-path url timeout-ms on-msg))))))))
|
||||
(define
|
||||
parse-feat
|
||||
(fn
|
||||
@@ -2983,7 +2970,6 @@
|
||||
((= val "behavior") (do (adv!) (parse-behavior-feat)))
|
||||
((= val "live") (do (adv!) (parse-live-feat)))
|
||||
((= val "when") (do (adv!) (parse-when-feat)))
|
||||
((= val "socket") (do (adv!) (parse-socket-feat)))
|
||||
((= val "worker")
|
||||
(error
|
||||
"worker plugin is not installed — see https://hyperscript.org/features/worker"))
|
||||
|
||||
@@ -43,17 +43,7 @@
|
||||
|
||||
;; Run an initializer function immediately.
|
||||
;; (hs-init thunk) — called at element boot time
|
||||
(define
|
||||
hs-on
|
||||
(fn
|
||||
(target event-name handler)
|
||||
(let
|
||||
((wrapped (fn (event) (guard (e ((and (not (= event-name "exception")) (not (= event-name "error"))) (dom-dispatch target "exception" {:error e})) (true (raise e))) (do (handler event) (when event (host-call event "stopPropagation")))))))
|
||||
(let
|
||||
((unlisten (dom-listen target event-name wrapped))
|
||||
(prev (or (dom-get-data target "hs-unlisteners") (list))))
|
||||
(dom-set-data target "hs-unlisteners" (append prev (list unlisten)))
|
||||
unlisten))))
|
||||
(define meta (host-new "Object"))
|
||||
|
||||
;; ── Async / timing ──────────────────────────────────────────────
|
||||
|
||||
@@ -61,11 +51,39 @@
|
||||
;; In hyperscript, wait is async-transparent — execution pauses.
|
||||
;; Here we use perform/IO suspension for true pause semantics.
|
||||
(define
|
||||
hs-on-every
|
||||
(fn (target event-name handler) (dom-listen target event-name handler)))
|
||||
_hs-on-caller
|
||||
(let
|
||||
((_ctx (host-new "Object"))
|
||||
(_m (host-new "Object"))
|
||||
(_f (host-new "Object")))
|
||||
(do
|
||||
(host-set! _f "type" "onFeature")
|
||||
(host-set! _m "feature" _f)
|
||||
(host-set! _ctx "meta" _m)
|
||||
_ctx)))
|
||||
|
||||
;; Wait for a DOM event on a target.
|
||||
;; (hs-wait-for target event-name) — suspends until event fires
|
||||
(define
|
||||
hs-on
|
||||
(fn
|
||||
(target event-name handler)
|
||||
(let
|
||||
((wrapped (fn (event) (do (host-set! meta "caller" _hs-on-caller) (guard (e ((and (not (= event-name "exception")) (not (= event-name "error"))) (dom-dispatch target "exception" {:error e})) (true (raise e))) (handler event))))))
|
||||
(let
|
||||
((unlisten (dom-listen target event-name wrapped))
|
||||
(prev (or (dom-get-data target "hs-unlisteners") (list))))
|
||||
(dom-set-data target "hs-unlisteners" (append prev (list unlisten)))
|
||||
unlisten))))
|
||||
|
||||
;; Wait for CSS transitions/animations to settle on an element.
|
||||
(define
|
||||
hs-on-every
|
||||
(fn (target event-name handler) (dom-listen target event-name handler)))
|
||||
|
||||
;; ── Class manipulation ──────────────────────────────────────────
|
||||
|
||||
;; Toggle a single class on an element.
|
||||
(define
|
||||
hs-on-intersection-attach!
|
||||
(fn
|
||||
@@ -81,7 +99,7 @@
|
||||
(host-call observer "observe" target)
|
||||
observer)))))
|
||||
|
||||
;; Wait for CSS transitions/animations to settle on an element.
|
||||
;; Toggle between two classes — exactly one is active at a time.
|
||||
(define
|
||||
hs-on-mutation-attach!
|
||||
(fn
|
||||
@@ -102,16 +120,19 @@
|
||||
(host-call observer "observe" target opts)
|
||||
observer))))))
|
||||
|
||||
;; ── Class manipulation ──────────────────────────────────────────
|
||||
|
||||
;; Toggle a single class on an element.
|
||||
(define hs-init (fn (thunk) (thunk)))
|
||||
|
||||
;; Toggle between two classes — exactly one is active at a time.
|
||||
(define hs-wait (fn (ms) (perform (list (quote io-sleep) ms))))
|
||||
|
||||
;; Take a class from siblings — add to target, remove from others.
|
||||
;; (hs-take! target cls) — like radio button class behavior
|
||||
(define hs-init (fn (thunk) (thunk)))
|
||||
|
||||
;; ── DOM insertion ───────────────────────────────────────────────
|
||||
|
||||
;; Put content at a position relative to a target.
|
||||
;; pos: "into" | "before" | "after"
|
||||
(define hs-wait (fn (ms) (perform (list (quote io-sleep) ms))))
|
||||
|
||||
;; ── Navigation / traversal ──────────────────────────────────────
|
||||
|
||||
;; Navigate to a URL.
|
||||
(begin
|
||||
(define
|
||||
hs-wait-for
|
||||
@@ -124,20 +145,15 @@
|
||||
(target event-name timeout-ms)
|
||||
(perform (list (quote io-wait-event) target event-name timeout-ms)))))
|
||||
|
||||
;; ── DOM insertion ───────────────────────────────────────────────
|
||||
|
||||
;; Put content at a position relative to a target.
|
||||
;; pos: "into" | "before" | "after"
|
||||
;; Find next sibling matching a selector (or any sibling).
|
||||
(define hs-settle (fn (target) (perform (list (quote io-settle) target))))
|
||||
|
||||
;; ── Navigation / traversal ──────────────────────────────────────
|
||||
|
||||
;; Navigate to a URL.
|
||||
;; Find previous sibling matching a selector.
|
||||
(define
|
||||
hs-toggle-class!
|
||||
(fn (target cls) (host-call (host-get target "classList") "toggle" cls)))
|
||||
|
||||
;; Find next sibling matching a selector (or any sibling).
|
||||
;; First element matching selector within a scope.
|
||||
(define
|
||||
hs-toggle-between!
|
||||
(fn
|
||||
@@ -147,7 +163,7 @@
|
||||
(do (dom-remove-class target cls1) (dom-add-class target cls2))
|
||||
(do (dom-remove-class target cls2) (dom-add-class target cls1)))))
|
||||
|
||||
;; Find previous sibling matching a selector.
|
||||
;; Last element matching selector.
|
||||
(define
|
||||
hs-toggle-style!
|
||||
(fn
|
||||
@@ -171,7 +187,7 @@
|
||||
(dom-set-style target prop "hidden")
|
||||
(dom-set-style target prop "")))))))
|
||||
|
||||
;; First element matching selector within a scope.
|
||||
;; First/last within a specific scope.
|
||||
(define
|
||||
hs-toggle-style-between!
|
||||
(fn
|
||||
@@ -183,7 +199,6 @@
|
||||
(dom-set-style target prop val2)
|
||||
(dom-set-style target prop val1)))))
|
||||
|
||||
;; Last element matching selector.
|
||||
(define
|
||||
hs-toggle-style-cycle!
|
||||
(fn
|
||||
@@ -204,7 +219,9 @@
|
||||
(true (find-next (rest remaining))))))
|
||||
(dom-set-style target prop (find-next vals)))))
|
||||
|
||||
;; First/last within a specific scope.
|
||||
;; ── Iteration ───────────────────────────────────────────────────
|
||||
|
||||
;; Repeat a thunk N times.
|
||||
(define
|
||||
hs-take!
|
||||
(fn
|
||||
@@ -244,6 +261,7 @@
|
||||
(dom-set-attr target name attr-val)
|
||||
(dom-set-attr target name ""))))))))
|
||||
|
||||
;; Repeat forever (until break — relies on exception/continuation).
|
||||
(begin
|
||||
(define
|
||||
hs-element?
|
||||
@@ -355,9 +373,10 @@
|
||||
(dom-insert-adjacent-html target "beforeend" value)
|
||||
(hs-boot-subtree! target)))))))))
|
||||
|
||||
;; ── Iteration ───────────────────────────────────────────────────
|
||||
;; ── Fetch ───────────────────────────────────────────────────────
|
||||
|
||||
;; Repeat a thunk N times.
|
||||
;; Fetch a URL, parse response according to format.
|
||||
;; (hs-fetch url format) — format is "json" | "text" | "html"
|
||||
(define
|
||||
hs-add-to!
|
||||
(fn
|
||||
@@ -370,7 +389,10 @@
|
||||
(append target (list value))))
|
||||
(true (do (host-call target "push" value) target)))))
|
||||
|
||||
;; Repeat forever (until break — relies on exception/continuation).
|
||||
;; ── Type coercion ───────────────────────────────────────────────
|
||||
|
||||
;; Coerce a value to a type by name.
|
||||
;; (hs-coerce value type-name) — type-name is "Int", "Float", "String", etc.
|
||||
(define
|
||||
hs-remove-from!
|
||||
(fn
|
||||
@@ -380,10 +402,10 @@
|
||||
(filter (fn (x) (not (= x value))) target)
|
||||
(host-call target "splice" (host-call target "indexOf" value) 1))))
|
||||
|
||||
;; ── Fetch ───────────────────────────────────────────────────────
|
||||
;; ── Object creation ─────────────────────────────────────────────
|
||||
|
||||
;; Fetch a URL, parse response according to format.
|
||||
;; (hs-fetch url format) — format is "json" | "text" | "html"
|
||||
;; Make a new object of a given type.
|
||||
;; (hs-make type-name) — creates empty object/collection
|
||||
(define
|
||||
hs-splice-at!
|
||||
(fn
|
||||
@@ -407,10 +429,11 @@
|
||||
(host-call target "splice" i 1))))
|
||||
target))))
|
||||
|
||||
;; ── Type coercion ───────────────────────────────────────────────
|
||||
;; ── Behavior installation ───────────────────────────────────────
|
||||
|
||||
;; Coerce a value to a type by name.
|
||||
;; (hs-coerce value type-name) — type-name is "Int", "Float", "String", etc.
|
||||
;; Install a behavior on an element.
|
||||
;; A behavior is a function that takes (me ...params) and sets up features.
|
||||
;; (hs-install behavior-fn me ...args)
|
||||
(define
|
||||
hs-index
|
||||
(fn
|
||||
@@ -422,10 +445,10 @@
|
||||
((string? obj) (nth obj key))
|
||||
(true (host-get obj key)))))
|
||||
|
||||
;; ── Object creation ─────────────────────────────────────────────
|
||||
;; ── Measurement ─────────────────────────────────────────────────
|
||||
|
||||
;; Make a new object of a given type.
|
||||
;; (hs-make type-name) — creates empty object/collection
|
||||
;; Measure an element's bounding rect, store as local variables.
|
||||
;; Returns a dict with x, y, width, height, top, left, right, bottom.
|
||||
(define
|
||||
hs-put-at!
|
||||
(fn
|
||||
@@ -447,11 +470,10 @@
|
||||
((= pos "start") (host-call target "unshift" value)))
|
||||
target)))))))
|
||||
|
||||
;; ── Behavior installation ───────────────────────────────────────
|
||||
|
||||
;; Install a behavior on an element.
|
||||
;; A behavior is a function that takes (me ...params) and sets up features.
|
||||
;; (hs-install behavior-fn me ...args)
|
||||
;; Return the current text selection as a string. In the browser this is
|
||||
;; `window.getSelection().toString()`. In the mock test runner, a test
|
||||
;; setup stashes the desired selection text at `window.__test_selection`
|
||||
;; and the fallback path returns that so tests can assert on the result.
|
||||
(define
|
||||
hs-dict-without
|
||||
(fn
|
||||
@@ -472,27 +494,19 @@
|
||||
(host-call (host-global "Reflect") "deleteProperty" out key)
|
||||
out)))))
|
||||
|
||||
;; ── Measurement ─────────────────────────────────────────────────
|
||||
|
||||
;; Measure an element's bounding rect, store as local variables.
|
||||
;; Returns a dict with x, y, width, height, top, left, right, bottom.
|
||||
;; ── Transition ──────────────────────────────────────────────────
|
||||
|
||||
;; Transition a CSS property to a value, optionally with duration.
|
||||
;; (hs-transition target prop value duration)
|
||||
(define
|
||||
hs-set-on!
|
||||
(fn
|
||||
(props target)
|
||||
(for-each (fn (k) (host-set! target k (get props k))) (keys props))))
|
||||
|
||||
;; Return the current text selection as a string. In the browser this is
|
||||
;; `window.getSelection().toString()`. In the mock test runner, a test
|
||||
;; setup stashes the desired selection text at `window.__test_selection`
|
||||
;; and the fallback path returns that so tests can assert on the result.
|
||||
(define hs-navigate! (fn (url) (perform (list (quote io-navigate) url))))
|
||||
|
||||
|
||||
;; ── Transition ──────────────────────────────────────────────────
|
||||
|
||||
;; Transition a CSS property to a value, optionally with duration.
|
||||
;; (hs-transition target prop value duration)
|
||||
(define
|
||||
hs-ask
|
||||
(fn
|
||||
@@ -631,6 +645,10 @@
|
||||
(true (find-next (dom-next-sibling el))))))
|
||||
(find-next sibling)))))
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
(define
|
||||
hs-previous
|
||||
(fn
|
||||
@@ -650,33 +668,36 @@
|
||||
(true (find-prev (dom-get-prop el "previousElementSibling"))))))
|
||||
(find-prev sibling)))))
|
||||
|
||||
(define hs-query-all (fn (sel) (dom-query-all (dom-body) sel)))
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
(define
|
||||
hs-query-all
|
||||
(fn (sel) (host-call (dom-body) "querySelectorAll" sel)))
|
||||
;; ── Sandbox/test runtime additions ──────────────────────────────
|
||||
;; Property access — dot notation and .length
|
||||
(define
|
||||
hs-query-all-in
|
||||
(fn
|
||||
(sel target)
|
||||
(if (nil? target) (hs-query-all sel) (dom-query-all target sel))))
|
||||
|
||||
(if
|
||||
(nil? target)
|
||||
(hs-query-all sel)
|
||||
(host-call target "querySelectorAll" sel))))
|
||||
;; DOM query stub — sandbox returns empty list
|
||||
(define
|
||||
hs-list-set
|
||||
(fn
|
||||
(lst idx val)
|
||||
(append (take lst idx) (cons val (drop lst (+ idx 1))))))
|
||||
;; ── Sandbox/test runtime additions ──────────────────────────────
|
||||
;; Property access — dot notation and .length
|
||||
;; Method dispatch — obj.method(args)
|
||||
(define
|
||||
hs-to-number
|
||||
(fn (v) (if (number? v) v (or (parse-number (str v)) 0))))
|
||||
;; DOM query stub — sandbox returns empty list
|
||||
|
||||
;; ── 0.9.90 features ─────────────────────────────────────────────
|
||||
;; beep! — debug logging, returns value unchanged
|
||||
(define
|
||||
hs-query-first
|
||||
(fn (sel) (host-call (host-global "document") "querySelector" sel)))
|
||||
;; Method dispatch — obj.method(args)
|
||||
;; Property-based is — check obj.key truthiness
|
||||
(define
|
||||
hs-query-last
|
||||
(fn
|
||||
@@ -684,11 +705,9 @@
|
||||
(let
|
||||
((all (dom-query-all (dom-body) sel)))
|
||||
(if (> (len all) 0) (nth all (- (len all) 1)) nil))))
|
||||
|
||||
;; ── 0.9.90 features ─────────────────────────────────────────────
|
||||
;; beep! — debug logging, returns value unchanged
|
||||
;; Array slicing (inclusive both ends)
|
||||
(define hs-first (fn (scope sel) (dom-query-all scope sel)))
|
||||
;; Property-based is — check obj.key truthiness
|
||||
;; Collection: sorted by
|
||||
(define
|
||||
hs-last
|
||||
(fn
|
||||
@@ -696,7 +715,7 @@
|
||||
(let
|
||||
((all (dom-query-all scope sel)))
|
||||
(if (> (len all) 0) (nth all (- (len all) 1)) nil))))
|
||||
;; Array slicing (inclusive both ends)
|
||||
;; Collection: sorted by descending
|
||||
(define
|
||||
hs-repeat-times
|
||||
(fn
|
||||
@@ -714,7 +733,7 @@
|
||||
((= signal "hs-continue") (do-repeat (+ i 1)))
|
||||
(true (do-repeat (+ i 1))))))))
|
||||
(do-repeat 0)))
|
||||
;; Collection: sorted by
|
||||
;; Collection: split by
|
||||
(define
|
||||
hs-repeat-forever
|
||||
(fn
|
||||
@@ -730,7 +749,7 @@
|
||||
((= signal "hs-continue") (do-forever))
|
||||
(true (do-forever))))))
|
||||
(do-forever)))
|
||||
;; Collection: sorted by descending
|
||||
;; Collection: joined by
|
||||
(define
|
||||
hs-repeat-while
|
||||
(fn
|
||||
@@ -743,7 +762,7 @@
|
||||
((= signal "hs-break") nil)
|
||||
((= signal "hs-continue") (hs-repeat-while cond-fn thunk))
|
||||
(true (hs-repeat-while cond-fn thunk)))))))
|
||||
;; Collection: split by
|
||||
|
||||
(define
|
||||
hs-repeat-until
|
||||
(fn
|
||||
@@ -755,13 +774,13 @@
|
||||
((= signal "hs-continue")
|
||||
(if (cond-fn) nil (hs-repeat-until cond-fn thunk)))
|
||||
(true (if (cond-fn) nil (hs-repeat-until cond-fn thunk)))))))
|
||||
;; Collection: joined by
|
||||
|
||||
(define
|
||||
hs-for-each
|
||||
(fn
|
||||
(fn-body collection)
|
||||
(let
|
||||
((items (cond ((list? collection) collection) ((dict? collection) (if (dict-has? collection "_order") (get collection "_order") (filter (fn (k) (not (= k "_order"))) (keys collection)))) ((nil? collection) (list)) (true (list)))))
|
||||
((items (cond ((list? collection) collection) ((nil? collection) (list)) ((host-iter? collection) (host-to-list collection)) ((dict? collection) (if (dict-has? collection "_order") (get collection "_order") (filter (fn (k) (not (= k "_order"))) (keys collection)))) (true (list)))))
|
||||
(define
|
||||
do-loop
|
||||
(fn
|
||||
@@ -869,33 +888,12 @@
|
||||
(define
|
||||
hs-fetch
|
||||
(fn
|
||||
(url format do-not-throw target)
|
||||
(url format)
|
||||
(let
|
||||
((fmt (cond ((nil? format) "text") ((or (= format "json") (= format "JSON") (= format "Object")) "json") ((or (= format "html") (= format "HTML")) "html") ((or (= format "response") (= format "Response")) "response") ((or (= format "text") (= format "Text")) "text") ((or (= format "number") (= format "Number")) "number") (true format))))
|
||||
(do
|
||||
(when (not (nil? target))
|
||||
(dom-dispatch target "hyperscript:beforeFetch" nil))
|
||||
(let
|
||||
((raw (perform (list "io-fetch" url "response" (dict)))))
|
||||
(do
|
||||
(when (get raw :_network-error) (raise {:response raw :message "Network error" :_hs-error "FetchError"}))
|
||||
(when
|
||||
(and (not (get raw :ok)) (not (= fmt "response")) (not do-not-throw))
|
||||
(raise {:response raw :status (get raw :status) :message "Fetch error" :_hs-error "FetchError"}))
|
||||
(cond
|
||||
((= fmt "response") raw)
|
||||
((= fmt "json")
|
||||
(let
|
||||
((parsed (perform (list "io-parse-json" (get raw :_json)))))
|
||||
(hs-host-to-sx parsed)))
|
||||
((= fmt "html")
|
||||
(perform (list "io-parse-html" (get raw :_html))))
|
||||
((= fmt "number")
|
||||
(or
|
||||
(parse-number (get raw :_number))
|
||||
(parse-number (get raw :_body))
|
||||
0))
|
||||
(true (get raw :_body)))))))))
|
||||
((fmt (cond ((nil? format) "text") ((or (= format "json") (= format "JSON") (= format "Object")) "json") ((or (= format "html") (= format "HTML")) "html") ((or (= format "response") (= format "Response")) "response") ((or (= format "text") (= format "Text")) "text") (true format))))
|
||||
(let
|
||||
((raw (perform (list "io-fetch" url fmt))))
|
||||
(cond ((= fmt "json") (hs-host-to-sx raw)) (true raw))))))
|
||||
|
||||
(define
|
||||
hs-json-escape
|
||||
@@ -986,8 +984,6 @@
|
||||
(true (str value))))
|
||||
((= type-name "JSON")
|
||||
(cond
|
||||
((and (dict? value) (dict-has? value :_json))
|
||||
(guard (_e (true value)) (json-parse (get value :_json))))
|
||||
((string? value) (guard (_e (true value)) (json-parse value)))
|
||||
((dict? value) (hs-json-stringify value))
|
||||
((list? value) (hs-json-stringify value))
|
||||
@@ -1366,14 +1362,21 @@
|
||||
hs-transition
|
||||
(fn
|
||||
(target prop value duration)
|
||||
(when
|
||||
duration
|
||||
(dom-set-style
|
||||
target
|
||||
"transition"
|
||||
(str prop " " (/ duration 1000) "s")))
|
||||
(dom-set-style target prop value)
|
||||
(when duration (hs-settle target))))
|
||||
(let
|
||||
((init-attr (str "data-hs-init-" prop)))
|
||||
(when
|
||||
(not (dom-get-attr target init-attr))
|
||||
(dom-set-attr target init-attr (dom-get-style target prop)))
|
||||
(let
|
||||
((actual-value (if (= value "initial") (dom-get-attr target init-attr) value)))
|
||||
(when
|
||||
duration
|
||||
(dom-set-style
|
||||
target
|
||||
"transition"
|
||||
(str prop " " (/ duration 1000) "s")))
|
||||
(dom-set-style target prop actual-value)
|
||||
(when duration (hs-settle target))))))
|
||||
|
||||
(define
|
||||
hs-transition-from
|
||||
@@ -2124,11 +2127,20 @@
|
||||
(fn
|
||||
(pairs)
|
||||
(let
|
||||
((d (dict)))
|
||||
(begin
|
||||
((d {}) (order (list)))
|
||||
(do
|
||||
(for-each
|
||||
(fn (pair) (dict-set! d (first pair) (nth pair 1)))
|
||||
(fn
|
||||
(pair)
|
||||
(let
|
||||
((k (first pair)))
|
||||
(do
|
||||
(when
|
||||
(not (dict-has? d k))
|
||||
(set! order (append order (list k))))
|
||||
(dict-set! d k (nth pair 1)))))
|
||||
pairs)
|
||||
(when (not (empty? order)) (dict-set! d "_order" order))
|
||||
d))))
|
||||
|
||||
(define
|
||||
@@ -2529,6 +2541,8 @@
|
||||
((nth entry 2) val)))
|
||||
_hs-dom-watchers)))
|
||||
|
||||
;; ── SourceInfo API ────────────────────────────────────────────────
|
||||
|
||||
(define
|
||||
hs-dom-is-ancestor?
|
||||
(fn
|
||||
@@ -2538,8 +2552,6 @@
|
||||
((= a b) true)
|
||||
(true (hs-dom-is-ancestor? a (dom-parent b))))))
|
||||
|
||||
;; ── SourceInfo API ────────────────────────────────────────────────
|
||||
|
||||
(define
|
||||
hs-win-call
|
||||
(fn
|
||||
@@ -2593,112 +2605,161 @@
|
||||
(walk (hs-node-get node (first keys)) (rest keys)))))
|
||||
(hs-line-for (walk (hs-parse-ast src-str) path))))
|
||||
|
||||
;; ── WebSocket / socket feature ───────────────────────────────────
|
||||
|
||||
(define
|
||||
hs-try-json-parse
|
||||
(fn (s) (host-call (host-global "JSON") "parse" s)))
|
||||
|
||||
(define
|
||||
hs-socket-resolve-rpc!
|
||||
hs-js-exec
|
||||
(fn
|
||||
(wrapper msg)
|
||||
(param-names js-src bound-args)
|
||||
(let
|
||||
((pending (host-get wrapper "pending")) (iid (host-get msg "iid")))
|
||||
((js-fn (host-new-function param-names js-src)))
|
||||
(let
|
||||
((resolver (host-get pending iid)))
|
||||
(when
|
||||
(not (nil? resolver))
|
||||
(if
|
||||
(not (nil? (host-get msg "return")))
|
||||
(host-call resolver "resolve" (host-get msg "return"))
|
||||
(host-call resolver "reject" (host-get msg "throw")))
|
||||
(host-set! pending iid nil))))))
|
||||
|
||||
(define
|
||||
hs-socket-register!
|
||||
(fn
|
||||
(name-path url timeout-ms handler json?)
|
||||
(let
|
||||
((ws-url (cond ((or (starts-with? url "ws://") (starts-with? url "wss://")) url) (true (let ((proto (host-get (host-global "location") "protocol")) (h (host-get (host-global "location") "host"))) (str (if (= proto "https:") "wss:" "ws:") "//" h url))))))
|
||||
(let
|
||||
((ws (host-new "WebSocket" ws-url)))
|
||||
(let
|
||||
((wrapper (host-new "Object")))
|
||||
(host-set! wrapper "raw" ws)
|
||||
(host-set! wrapper "url" ws-url)
|
||||
(host-set! wrapper "timeout" timeout-ms)
|
||||
(host-set! wrapper "pending" (host-new "Object"))
|
||||
(host-set! wrapper "handler" handler)
|
||||
(host-set! wrapper "json?" json?)
|
||||
(host-set! wrapper "closed?" false)
|
||||
(host-set! wrapper "closedFlag" nil)
|
||||
((result (host-call-fn js-fn bound-args)))
|
||||
(if
|
||||
(= (host-typeof result) "promise")
|
||||
(let
|
||||
((proxy-factory (host-global "_hs_make_rpc_proxy")))
|
||||
(when
|
||||
proxy-factory
|
||||
(host-set!
|
||||
wrapper
|
||||
"rpc"
|
||||
(host-call proxy-factory "call" nil wrapper))))
|
||||
(host-set!
|
||||
ws
|
||||
"onmessage"
|
||||
(host-callback
|
||||
(fn
|
||||
(event)
|
||||
(let
|
||||
((data (host-get event "data")))
|
||||
(let
|
||||
((parsed (hs-try-json-parse data)))
|
||||
(cond
|
||||
((and (not (nil? parsed)) (not (nil? (host-get parsed "iid"))))
|
||||
(hs-socket-resolve-rpc! wrapper parsed))
|
||||
((not (nil? handler))
|
||||
(if
|
||||
json?
|
||||
(if
|
||||
(not (nil? parsed))
|
||||
(handler parsed)
|
||||
(error "Received non-JSON message"))
|
||||
(handler event)))))))))
|
||||
(host-call
|
||||
ws
|
||||
"addEventListener"
|
||||
"close"
|
||||
(host-callback
|
||||
(fn
|
||||
(evt)
|
||||
(host-set! wrapper "closedFlag" "1"))))
|
||||
(host-set!
|
||||
wrapper
|
||||
"dispatchEvent"
|
||||
(host-callback
|
||||
(fn
|
||||
(evt)
|
||||
(let
|
||||
((payload (host-new "Object")))
|
||||
(host-set! payload "type" (host-get evt "type"))
|
||||
(host-call
|
||||
(host-get wrapper "raw")
|
||||
"send"
|
||||
(host-call
|
||||
(host-global "JSON")
|
||||
"stringify"
|
||||
payload))))))
|
||||
(define
|
||||
bind-path!
|
||||
(fn
|
||||
(obj path)
|
||||
(if
|
||||
(= (len path) 1)
|
||||
(host-set! obj (first path) wrapper)
|
||||
(let
|
||||
((key (first path)) (rest-path (rest path)))
|
||||
(let
|
||||
((next (or (host-get obj key) (host-new "Object"))))
|
||||
(host-set! obj key next)
|
||||
(bind-path! next rest-path))))))
|
||||
(bind-path! (host-global "window") name-path)
|
||||
wrapper)))))
|
||||
((state (host-promise-state result)))
|
||||
(if
|
||||
(and state (= (host-get state "ok") false))
|
||||
(raise (host-get state "value"))
|
||||
(if state (host-get state "value") result)))
|
||||
result)))))
|
||||
|
||||
(define
|
||||
hs-raw->api-token
|
||||
(fn
|
||||
(raw)
|
||||
(let
|
||||
((type (dict-get raw :type)) (value (dict-get raw :value)))
|
||||
(cond
|
||||
(= type "ident")
|
||||
{:value value :type "IDENTIFIER" :op false}
|
||||
(= type "keyword")
|
||||
{:value value :type "IDENTIFIER" :op false}
|
||||
(= type "number")
|
||||
{:value value :type "NUMBER" :op false}
|
||||
(= type "string")
|
||||
{:value value :type "STRING" :op false}
|
||||
(= type "class")
|
||||
{:value (str "." value) :type "CLASS_REF" :op false}
|
||||
(= type "id")
|
||||
{:value (str "#" value) :type "ID_REF" :op false}
|
||||
(= type "attr")
|
||||
{:value value :type "ATTRIBUTE_REF" :op false}
|
||||
(= type "style")
|
||||
{:value value :type "STYLE_REF" :op false}
|
||||
(= type "selector")
|
||||
{:value value :type "QUERY_REF" :op false}
|
||||
(= type "eof")
|
||||
{:value "<<<EOF>>>" :type "EOF" :op false}
|
||||
(= type "paren-open")
|
||||
{:value value :type "L_PAREN" :op true}
|
||||
(= type "paren-close")
|
||||
{:value value :type "R_PAREN" :op true}
|
||||
(= type "bracket-open")
|
||||
{:value value :type "L_BRACKET" :op true}
|
||||
(= type "bracket-close")
|
||||
{:value value :type "R_BRACKET" :op true}
|
||||
(= type "brace-open")
|
||||
{:value value :type "L_BRACE" :op true}
|
||||
(= type "brace-close")
|
||||
{:value value :type "R_BRACE" :op true}
|
||||
(= type "comma")
|
||||
{:value value :type "COMMA" :op true}
|
||||
(= type "dot")
|
||||
{:value value :type "PERIOD" :op true}
|
||||
(= type "colon")
|
||||
{:value value :type "COLON" :op true}
|
||||
(= type "op")
|
||||
(cond
|
||||
(= value "+") {:value value :type "PLUS" :op true}
|
||||
(= value "-") {:value value :type "MINUS" :op true}
|
||||
(= value "*") {:value value :type "MULTIPLY" :op true}
|
||||
(= value "/") {:value value :type "SLASH" :op true}
|
||||
(= value "!") {:value value :type "EXCLAMATION" :op true}
|
||||
(= value "?") {:value value :type "QUESTION" :op true}
|
||||
(= value "#") {:value value :type "POUND" :op true}
|
||||
(= value "&") {:value value :type "AMPERSAND" :op true}
|
||||
(= value "=") {:value value :type "EQUALS" :op true}
|
||||
(= value "<") {:value value :type "L_ANG" :op true}
|
||||
(= value ">") {:value value :type "R_ANG" :op true}
|
||||
(= value "<=") {:value value :type "LTE_ANG" :op true}
|
||||
(= value ">=") {:value value :type "GTE_ANG" :op true}
|
||||
(= value "==") {:value value :type "EQ" :op true}
|
||||
(= value "===") {:value value :type "EQQ" :op true}
|
||||
(= value "..") {:value value :type "PERIOD_PERIOD" :op true}
|
||||
:else {:value value :type value :op true})
|
||||
:else {:value (or value "") :type (str type) :op false}))))
|
||||
|
||||
(define hs-eof-sentinel {:value "<<<EOF>>>" :type "EOF" :op false})
|
||||
|
||||
(define
|
||||
hs-tokens-of
|
||||
(fn
|
||||
(src &rest args)
|
||||
(let
|
||||
((template (some (fn (a) (equal? a :template)) args)))
|
||||
(let
|
||||
((raw (if template (hs-tokenize-template src) (hs-tokenize src))))
|
||||
{:pos 0 :list (filter (fn (t) (not (= (dict-get t :type) "EOF"))) (map hs-raw->api-token raw)) :source src}))))
|
||||
|
||||
(define
|
||||
hs-stream-token
|
||||
(fn
|
||||
(s i)
|
||||
(let
|
||||
((lst (dict-get s :list))
|
||||
(n (len (dict-get s :list))))
|
||||
(define
|
||||
find
|
||||
(fn
|
||||
(pos count)
|
||||
(if
|
||||
(>= pos n)
|
||||
hs-eof-sentinel
|
||||
(let
|
||||
((tok (nth lst pos)))
|
||||
(if
|
||||
(= (dict-get tok :type) "whitespace")
|
||||
(find (+ pos 1) count)
|
||||
(if
|
||||
(= count 0)
|
||||
tok
|
||||
(find (+ pos 1) (- count 1))))))))
|
||||
(find (dict-get s :pos) i))))
|
||||
|
||||
(define
|
||||
hs-stream-consume
|
||||
(fn
|
||||
(s)
|
||||
(let
|
||||
((lst (dict-get s :list))
|
||||
(n (len (dict-get s :list))))
|
||||
(define
|
||||
find-pos
|
||||
(fn
|
||||
(pos)
|
||||
(if
|
||||
(>= pos n)
|
||||
pos
|
||||
(if
|
||||
(= (dict-get (nth lst pos) :type) "whitespace")
|
||||
(find-pos (+ pos 1))
|
||||
pos))))
|
||||
(let
|
||||
((p (find-pos (dict-get s :pos))))
|
||||
(let
|
||||
((tok (if (>= p n) hs-eof-sentinel (nth lst p))))
|
||||
(do
|
||||
(when
|
||||
(not (= (dict-get tok :type) "EOF"))
|
||||
(dict-set! s :pos (+ p 1)))
|
||||
tok))))))
|
||||
|
||||
(define
|
||||
hs-stream-has-more
|
||||
(fn (s) (not (= (dict-get (hs-stream-token s 0) :type) "EOF"))))
|
||||
|
||||
(define hs-token-type (fn (tok) (dict-get tok :type)))
|
||||
|
||||
(define hs-token-value (fn (tok) (dict-get tok :value)))
|
||||
|
||||
(define hs-token-op? (fn (tok) (dict-get tok :op)))
|
||||
|
||||
@@ -465,7 +465,12 @@
|
||||
scan!
|
||||
(fn
|
||||
()
|
||||
(skip-ws!)
|
||||
(let
|
||||
((ws-start pos))
|
||||
(skip-ws!)
|
||||
(when
|
||||
(and (> (len tokens) 0) (> pos ws-start))
|
||||
(hs-emit! "whitespace" (slice src ws-start pos) ws-start)))
|
||||
(when
|
||||
(< pos src-len)
|
||||
(let
|
||||
@@ -473,11 +478,7 @@
|
||||
(cond
|
||||
(and (= ch "-") (< (+ pos 1) src-len) (= (hs-peek 1) "-"))
|
||||
(do (hs-advance! 2) (skip-comment!) (scan!))
|
||||
(and
|
||||
(= ch "/")
|
||||
(< (+ pos 1) src-len)
|
||||
(= (hs-peek 1) "/")
|
||||
(not (and (> pos 0) (= (hs-peek -1) ":"))))
|
||||
(and (= ch "/") (< (+ pos 1) src-len) (= (hs-peek 1) "/"))
|
||||
(do (hs-advance! 2) (skip-comment!) (scan!))
|
||||
(and
|
||||
(= ch "<")
|
||||
@@ -493,6 +494,15 @@
|
||||
(do (hs-emit! "selector" (read-selector) start) (scan!))
|
||||
(and (= ch ".") (< (+ pos 1) src-len) (= (hs-peek 1) "."))
|
||||
(do (hs-emit! "op" ".." start) (hs-advance! 2) (scan!))
|
||||
(and
|
||||
(= ch ".")
|
||||
(< (+ pos 1) src-len)
|
||||
(or (hs-letter? (hs-peek 1)) (= (hs-peek 1) "-") (= (hs-peek 1) "_"))
|
||||
(> (len tokens) 0)
|
||||
(let
|
||||
((lt (dict-get (nth tokens (- (len tokens) 1)) :type)))
|
||||
(or (= lt "paren-close") (= lt "brace-close") (= lt "bracket-close"))))
|
||||
(do (hs-emit! "dot" "." start) (hs-advance! 1) (scan!))
|
||||
(and
|
||||
(= ch ".")
|
||||
(< (+ pos 1) src-len)
|
||||
@@ -504,6 +514,15 @@
|
||||
(hs-advance! 1)
|
||||
(hs-emit! "class" (read-class-name pos) start)
|
||||
(scan!))
|
||||
(and
|
||||
(= ch "#")
|
||||
(< (+ pos 1) src-len)
|
||||
(hs-ident-start? (hs-peek 1))
|
||||
(> (len tokens) 0)
|
||||
(let
|
||||
((lt (dict-get (nth tokens (- (len tokens) 1)) :type)))
|
||||
(or (= lt "paren-close") (= lt "brace-close") (= lt "bracket-close"))))
|
||||
(do (hs-emit! "op" "#" start) (hs-advance! 1) (scan!))
|
||||
(and
|
||||
(= ch "#")
|
||||
(< (+ pos 1) src-len)
|
||||
|
||||
@@ -4,10 +4,10 @@ Live tally for `plans/hs-conformance-to-100.md`. Update after every cluster comm
|
||||
|
||||
```
|
||||
Baseline: 1213/1496 (81.1%)
|
||||
Merged: 1312/1496 (87.7%) delta +99
|
||||
Merged: 1330/1496 (88.9%) delta +117
|
||||
Worktree: all landed
|
||||
Target: 1496/1496 (100.0%)
|
||||
Remaining: ~192 tests (clusters 17/29(partial)/31 blocked; 33/34 partial)
|
||||
Remaining: ~174 tests (clusters 17/29(partial)/31 blocked; 33/34 partial)
|
||||
```
|
||||
|
||||
## Cluster ledger
|
||||
@@ -30,7 +30,7 @@ Remaining: ~192 tests (clusters 17/29(partial)/31 blocked; 33/34 partial)
|
||||
| 12 | `show` multi-element + display retention | done | +2 | 98c957b3 |
|
||||
| 13 | `toggle` multi-class + timed + until-event | partial | +2 | bd821c04 |
|
||||
| 14 | `unless` modifier | done | +1 | c4da0698 |
|
||||
| 15 | `transition` query-ref + multi-prop + initial | partial | +2 | 3d352055 |
|
||||
| 15 | `transition` query-ref + multi-prop + initial | partial | +3 | 3d352055 |
|
||||
| 16 | `send can reference sender` | done | +1 | ed8d71c9 |
|
||||
| 17 | `tell` semantics | blocked | — | — |
|
||||
| 18 | `throw` respond async/sync | done | +2 | dda3becb |
|
||||
@@ -73,7 +73,7 @@ Remaining: ~192 tests (clusters 17/29(partial)/31 blocked; 33/34 partial)
|
||||
| # | Cluster | Status | Design doc |
|
||||
|---|---------|--------|------------|
|
||||
| 36 | WebSocket + `socket` + RPC proxy | design-done | `plans/designs/e36-websocket.md` |
|
||||
| 37 | Tokenizer-as-API | design-done | `plans/designs/e37-tokenizer-api.md` |
|
||||
| 37 | Tokenizer-as-API | done | +17 | 54b54f4e |
|
||||
| 38 | SourceInfo API | design-done | `plans/designs/e38-sourceinfo.md` |
|
||||
| 39 | WebWorker plugin | design-done | `plans/designs/e39-webworker.md` |
|
||||
| 40 | Fetch non-2xx / before-fetch / real response | done | +7 | d7244d1d |
|
||||
@@ -97,7 +97,7 @@ Defer until A–D drain. Estimated ~25 recoverable tests.
|
||||
| B | 7 | 0 | 0 | 0 | 0 | — | 7 |
|
||||
| C | 4 | 1 | 0 | 0 | 0 | — | 5 |
|
||||
| D | 2 | 2 | 0 | 0 | 1 | — | 5 |
|
||||
| E | 1 | 0 | 0 | 0 | 0 | 4 | 5 |
|
||||
| E | 2 | 0 | 0 | 0 | 0 | 3 | 5 |
|
||||
| F | — | — | — | ~10 | — | — | ~10 |
|
||||
|
||||
## Maintenance
|
||||
|
||||
@@ -131,7 +131,7 @@ Orchestrator cherry-picks worktree commits onto `architecture` one at a time; re
|
||||
|
||||
All five have design docs on their own worktree branches pending review + merge. After merge, status flips to `design-ready` and they become eligible for the loop.
|
||||
|
||||
36. **[DONE +16 — branch `hs-e36-websocket`] WebSocket + `socket`** — 16/16 tests passing. `socket NAME URL [with timeout N] [on message [as JSON] …] end`, RPC proxy (dispatch-fn pattern), reconnect, dispatchEvent, timeout/noTimeout chains. All 16 upstream tests green.
|
||||
36. **[design-done, pending review — `plans/designs/e36-websocket.md` on `worktree-agent-a9daf73703f520257`] WebSocket + `socket`** — 16 tests. Upstream shape is `socket NAME URL [with timeout N] [on message [as JSON] …] end` with an **implicit `.rpc` Proxy** (ES6 Proxy lives in JS, not SX), not `with proxy { send, receive }` as this row previously claimed. Design doc has 8-commit checklist, +12–16 delta estimate. Ship only with intentional design review.
|
||||
|
||||
37. **[done +17]** Tokenizer-as-API — `hs-tokens-of` / `hs-stream-token` / `hs-token-type` / `hs-token-value` / `hs-token-op?`; type-map + normalize; `read-number` dot-stop fix; `\$` template escape in compiler + runtime; generator pattern in `generate-sx-tests.py`. 17/17.
|
||||
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
3
shared/static/wasm/sx/hs-compiler.sxbc
Normal file
3
shared/static/wasm/sx/hs-compiler.sxbc
Normal file
File diff suppressed because one or more lines are too long
@@ -19,6 +19,7 @@
|
||||
(define
|
||||
reserved
|
||||
(list
|
||||
(quote beingTold)
|
||||
(quote me)
|
||||
(quote it)
|
||||
(quote event)
|
||||
@@ -65,7 +66,10 @@
|
||||
(list (quote me))
|
||||
(list
|
||||
(quote let)
|
||||
(list (list (quote it) nil) (list (quote event) nil))
|
||||
(list
|
||||
(list (quote beingTold) (quote me))
|
||||
(list (quote it) nil)
|
||||
(list (quote event) nil))
|
||||
guarded))))))))))
|
||||
|
||||
;; ── Activate a single element ───────────────────────────────────
|
||||
|
||||
@@ -9,7 +9,9 @@
|
||||
(fn
|
||||
(tokens src)
|
||||
(let
|
||||
((p 0) (tok-len (len tokens)))
|
||||
((tokens (filter (fn (t) (not (= (get t "type") "whitespace"))) tokens))
|
||||
(p 0)
|
||||
(tok-len (len (filter (fn (t) (not (= (get t "type") "whitespace"))) tokens))))
|
||||
(define tp (fn () (if (< p tok-len) (nth tokens p) nil)))
|
||||
(define
|
||||
tp-type
|
||||
@@ -123,19 +125,23 @@
|
||||
((and (= kind (quote closest)) (= typ "ident") (= val "parent"))
|
||||
(do (adv!) (parse-trav (quote closest-parent))))
|
||||
((= typ "selector")
|
||||
(do (adv!) (list kind val (list (quote me)))))
|
||||
(do (adv!) (list kind val (list (quote beingTold)))))
|
||||
((= typ "class")
|
||||
(do (adv!) (list kind (str "." val) (list (quote me)))))
|
||||
(do
|
||||
(adv!)
|
||||
(list kind (str "." val) (list (quote beingTold)))))
|
||||
((= typ "id")
|
||||
(do (adv!) (list kind (str "#" val) (list (quote me)))))
|
||||
(do
|
||||
(adv!)
|
||||
(list kind (str "#" val) (list (quote beingTold)))))
|
||||
((= typ "attr")
|
||||
(do
|
||||
(adv!)
|
||||
(list
|
||||
(quote attr)
|
||||
val
|
||||
(list kind (str "[" val "]") (list (quote me))))))
|
||||
(true (list kind "*" (list (quote me))))))))
|
||||
(list kind (str "[" val "]") (list (quote beingTold))))))
|
||||
(true (list kind "*" (list (quote beingTold))))))))
|
||||
(define
|
||||
parse-pos-kw
|
||||
(fn
|
||||
@@ -270,12 +276,18 @@
|
||||
l
|
||||
{}))))
|
||||
((= typ "attr")
|
||||
(do (adv!) (list (quote attr) val (list (quote me)))))
|
||||
(do
|
||||
(adv!)
|
||||
(list (quote attr) val (list (quote beingTold)))))
|
||||
((= typ "style")
|
||||
(do (adv!) (list (quote style) val (list (quote me)))))
|
||||
(do
|
||||
(adv!)
|
||||
(list (quote style) val (list (quote beingTold)))))
|
||||
((= typ "local") (do (adv!) (list (quote local) val)))
|
||||
((= typ "hat")
|
||||
(do (adv!) (list (quote dom-ref) val (list (quote me)))))
|
||||
(do
|
||||
(adv!)
|
||||
(list (quote dom-ref) val (list (quote beingTold)))))
|
||||
((and (= typ "keyword") (= val "dom"))
|
||||
(do
|
||||
(adv!)
|
||||
@@ -283,7 +295,7 @@
|
||||
((name (tp-val)))
|
||||
(do
|
||||
(adv!)
|
||||
(list (quote dom-ref) name (list (quote me)))))))
|
||||
(list (quote dom-ref) name (list (quote beingTold)))))))
|
||||
((= typ "class")
|
||||
(let
|
||||
((s (cur-start)) (l (cur-line)))
|
||||
@@ -982,7 +994,7 @@
|
||||
(collect-classes!))))
|
||||
(collect-classes!)
|
||||
(let
|
||||
((tgt (if (match-kw "to") (parse-expr) (list (quote me)))))
|
||||
((tgt (if (match-kw "to") (parse-expr) (list (quote beingTold)))))
|
||||
(let
|
||||
((when-clause (if (match-kw "when") (parse-expr) nil)))
|
||||
(if
|
||||
@@ -1011,7 +1023,7 @@
|
||||
(get (adv!) "value")
|
||||
(parse-expr))))
|
||||
(let
|
||||
((tgt (if (match-kw "to") (parse-expr) (list (quote me)))))
|
||||
((tgt (if (match-kw "to") (parse-expr) (list (quote beingTold)))))
|
||||
(list (quote set-style) prop value tgt))))
|
||||
((= (tp-type) "brace-open")
|
||||
(do
|
||||
@@ -1036,7 +1048,7 @@
|
||||
(collect-pairs!)
|
||||
(when (= (tp-type) "brace-close") (adv!))
|
||||
(let
|
||||
((tgt (if (match-kw "to") (parse-expr) (list (quote me)))))
|
||||
((tgt (if (match-kw "to") (parse-expr) (list (quote beingTold)))))
|
||||
(list (quote set-styles) (reverse pairs) tgt)))))
|
||||
((and (= (tp-type) "bracket-open") (> (len tokens) (+ p 1)) (= (get (nth tokens (+ p 1)) "type") "attr"))
|
||||
(do
|
||||
@@ -1048,7 +1060,7 @@
|
||||
((attr-val (parse-expr)))
|
||||
(when (= (tp-type) "bracket-close") (adv!))
|
||||
(let
|
||||
((tgt (parse-tgt-kw "to" (list (quote me)))))
|
||||
((tgt (parse-tgt-kw "to" (list (quote beingTold)))))
|
||||
(let
|
||||
((when-clause (if (match-kw "when") (parse-expr) nil)))
|
||||
(if
|
||||
@@ -1066,7 +1078,7 @@
|
||||
(let
|
||||
((attr-val (if (and (= (tp-type) "op") (= (tp-val) "=")) (do (adv!) (parse-expr)) "")))
|
||||
(let
|
||||
((tgt (if (match-kw "to") (parse-expr) (list (quote me)))))
|
||||
((tgt (if (match-kw "to") (parse-expr) (list (quote beingTold)))))
|
||||
(let
|
||||
((when-clause (if (match-kw "when") (parse-expr) nil)))
|
||||
(if
|
||||
@@ -1107,7 +1119,7 @@
|
||||
(collect-classes!))))
|
||||
(collect-classes!)
|
||||
(let
|
||||
((tgt (if (match-kw "from") (parse-expr) (list (quote me)))))
|
||||
((tgt (if (match-kw "from") (parse-expr) (list (quote beingTold)))))
|
||||
(if
|
||||
(empty? extra-classes)
|
||||
(list (quote remove-class) cls tgt)
|
||||
@@ -1118,7 +1130,7 @@
|
||||
(let
|
||||
((attr-name (get (adv!) "value")))
|
||||
(let
|
||||
((tgt (if (match-kw "from") (parse-expr) (list (quote me)))))
|
||||
((tgt (if (match-kw "from") (parse-expr) (list (quote beingTold)))))
|
||||
(list (quote remove-attr) attr-name tgt))))
|
||||
((and (= (tp-type) "bracket-open") (= (tp-val) "["))
|
||||
(do
|
||||
@@ -1180,7 +1192,7 @@
|
||||
(let
|
||||
((cls2 (do (let ((v (tp-val))) (adv!) v))))
|
||||
(let
|
||||
((tgt (parse-tgt-kw "on" (list (quote me)))))
|
||||
((tgt (parse-tgt-kw "on" (list (quote beingTold)))))
|
||||
(list (quote toggle-between) cls1 cls2 tgt)))
|
||||
nil)))
|
||||
((and (= (tp-type) "bracket-open") (> (len tokens) (+ p 1)) (= (get (nth tokens (+ p 1)) "type") "attr"))
|
||||
@@ -1205,7 +1217,7 @@
|
||||
((v2 (parse-expr)))
|
||||
(when (= (tp-type) "bracket-close") (adv!))
|
||||
(let
|
||||
((tgt (parse-tgt-kw "on" (list (quote me)))))
|
||||
((tgt (parse-tgt-kw "on" (list (quote beingTold)))))
|
||||
(if
|
||||
(= n1 n2)
|
||||
(list
|
||||
@@ -1239,7 +1251,7 @@
|
||||
(let
|
||||
((extra-classes (collect-classes (list))))
|
||||
(let
|
||||
((tgt (parse-tgt-kw "on" (list (quote me)))))
|
||||
((tgt (parse-tgt-kw "on" (list (quote beingTold)))))
|
||||
(cond
|
||||
((> (len extra-classes) 0)
|
||||
(list
|
||||
@@ -1268,7 +1280,7 @@
|
||||
(let
|
||||
((prop (get (adv!) "value")))
|
||||
(let
|
||||
((tgt (if (match-kw "of") (parse-expr) (list (quote me)))))
|
||||
((tgt (if (match-kw "of") (parse-expr) (list (quote beingTold)))))
|
||||
(if
|
||||
(match-kw "between")
|
||||
(let
|
||||
@@ -1339,7 +1351,7 @@
|
||||
(let
|
||||
((attr-name (get (adv!) "value")))
|
||||
(let
|
||||
((tgt (if (match-kw "on") (parse-expr) (list (quote me)))))
|
||||
((tgt (if (match-kw "on") (parse-expr) (list (quote beingTold)))))
|
||||
(if
|
||||
(match-kw "between")
|
||||
(let
|
||||
@@ -1364,7 +1376,7 @@
|
||||
((attr-val (parse-expr)))
|
||||
(when (= (tp-type) "bracket-close") (adv!))
|
||||
(let
|
||||
((tgt (parse-tgt-kw "on" (list (quote me)))))
|
||||
((tgt (parse-tgt-kw "on" (list (quote beingTold)))))
|
||||
(list (quote toggle-attr-val) attr-name attr-val tgt))))))
|
||||
((and (= (tp-type) "keyword") (= (tp-val) "my"))
|
||||
(do
|
||||
@@ -1592,7 +1604,7 @@
|
||||
(let
|
||||
((dtl (if (= (tp-type) "paren-open") (parse-detail-dict) nil)))
|
||||
(let
|
||||
((tgt (parse-tgt-kw "to" (list (quote me)))))
|
||||
((tgt (parse-tgt-kw "to" (list (quote beingTold)))))
|
||||
(if
|
||||
dtl
|
||||
(list (quote send) name dtl tgt)
|
||||
@@ -1606,7 +1618,7 @@
|
||||
(let
|
||||
((dtl (if (= (tp-type) "paren-open") (parse-detail-dict) nil)))
|
||||
(let
|
||||
((tgt (parse-tgt-kw "on" (list (quote me)))))
|
||||
((tgt (parse-tgt-kw "on" (list (quote beingTold)))))
|
||||
(if
|
||||
dtl
|
||||
(list (quote trigger) name dtl tgt)
|
||||
@@ -1645,7 +1657,7 @@
|
||||
(fn
|
||||
()
|
||||
(let
|
||||
((tgt (cond ((at-end?) (list (quote me))) ((and (= (tp-type) "keyword") (or (= (tp-val) "then") (= (tp-val) "end") (= (tp-val) "with") (= (tp-val) "when") (= (tp-val) "add") (= (tp-val) "remove") (= (tp-val) "set") (= (tp-val) "put") (= (tp-val) "toggle") (= (tp-val) "hide") (= (tp-val) "show") (= (tp-val) "on"))) (list (quote me))) (true (parse-expr)))))
|
||||
((tgt (cond ((at-end?) (list (quote beingTold))) ((and (= (tp-type) "keyword") (or (= (tp-val) "then") (= (tp-val) "end") (= (tp-val) "with") (= (tp-val) "when") (= (tp-val) "add") (= (tp-val) "remove") (= (tp-val) "set") (= (tp-val) "put") (= (tp-val) "toggle") (= (tp-val) "hide") (= (tp-val) "show") (= (tp-val) "on"))) (list (quote beingTold))) (true (parse-expr)))))
|
||||
(let
|
||||
((strategy (if (match-kw "with") (if (at-end?) "display" (let ((s (tp-val))) (do (adv!) (cond ((at-end?) s) ((= (tp-type) "colon") (do (adv!) (let ((v (tp-val))) (do (adv!) (str s ":" v))))) ((= (tp-type) "local") (let ((v (tp-val))) (do (adv!) (str s ":" v)))) (true s))))) "display")))
|
||||
(let
|
||||
@@ -1656,7 +1668,7 @@
|
||||
(fn
|
||||
()
|
||||
(let
|
||||
((tgt (cond ((at-end?) (list (quote me))) ((and (= (tp-type) "keyword") (or (= (tp-val) "then") (= (tp-val) "end") (= (tp-val) "with") (= (tp-val) "when") (= (tp-val) "add") (= (tp-val) "remove") (= (tp-val) "set") (= (tp-val) "put") (= (tp-val) "toggle") (= (tp-val) "hide") (= (tp-val) "show") (= (tp-val) "on"))) (list (quote me))) (true (parse-expr)))))
|
||||
((tgt (cond ((at-end?) (list (quote beingTold))) ((and (= (tp-type) "keyword") (or (= (tp-val) "then") (= (tp-val) "end") (= (tp-val) "with") (= (tp-val) "when") (= (tp-val) "add") (= (tp-val) "remove") (= (tp-val) "set") (= (tp-val) "put") (= (tp-val) "toggle") (= (tp-val) "hide") (= (tp-val) "show") (= (tp-val) "on"))) (list (quote beingTold))) (true (parse-expr)))))
|
||||
(let
|
||||
((strategy (if (match-kw "with") (if (at-end?) "display" (let ((s (tp-val))) (do (adv!) (cond ((at-end?) s) ((= (tp-type) "colon") (do (adv!) (let ((v (tp-val))) (do (adv!) (str s ":" v))))) ((= (tp-type) "local") (let ((v (tp-val))) (do (adv!) (str s ":" v)))) (true s))))) "display")))
|
||||
(let
|
||||
@@ -1682,7 +1694,7 @@
|
||||
((from-val (if (match-kw "from") (let ((v (parse-atom))) (if (and v (= (tp-type) "ident") (not (hs-keyword? (tp-val)))) (let ((unit (get (adv!) "value"))) (list (quote string-postfix) v unit)) v)) nil)))
|
||||
(expect-kw! "to")
|
||||
(let
|
||||
((value (let ((v (parse-atom))) (if (and v (= (tp-type) "ident") (not (hs-keyword? (tp-val)))) (let ((unit (get (adv!) "value"))) (list (quote string-postfix) v unit)) v))))
|
||||
((value (if (and (= (tp-type) "ident") (= (tp-val) "initial")) (do (adv!) "initial") (let ((v (parse-atom))) (if (and v (= (tp-type) "ident") (not (hs-keyword? (tp-val)))) (let ((unit (get (adv!) "value"))) (list (quote string-postfix) v unit)) v)))))
|
||||
(let
|
||||
((dur (if (match-kw "over") (let ((v (parse-atom))) (if (and (number? v) (= (tp-type) "ident") (not (hs-keyword? (tp-val)))) (let ((unit (get (adv!) "value"))) (list (quote string-postfix) v unit)) v)) nil)))
|
||||
(let
|
||||
@@ -2158,21 +2170,21 @@
|
||||
(if
|
||||
(match-kw "of")
|
||||
(list (quote style) val (parse-expr))
|
||||
(list (quote style) val (list (quote me))))))
|
||||
(list (quote style) val (list (quote beingTold))))))
|
||||
((= typ "attr")
|
||||
(do
|
||||
(adv!)
|
||||
(if
|
||||
(match-kw "of")
|
||||
(list (quote attr) val (parse-expr))
|
||||
(list (quote attr) val (list (quote me))))))
|
||||
(list (quote attr) val (list (quote beingTold))))))
|
||||
((= typ "class")
|
||||
(do
|
||||
(adv!)
|
||||
(if
|
||||
(match-kw "of")
|
||||
(list (quote has-class?) (parse-expr) val)
|
||||
(list (quote has-class?) (list (quote me)) val))))
|
||||
(list (quote has-class?) (list (quote beingTold)) val))))
|
||||
((= typ "selector")
|
||||
(do
|
||||
(adv!)
|
||||
@@ -2320,13 +2332,15 @@
|
||||
()
|
||||
(let
|
||||
((tgt (parse-expr)))
|
||||
(list (quote measure) (if (nil? tgt) (list (quote me)) tgt)))))
|
||||
(list
|
||||
(quote measure)
|
||||
(if (nil? tgt) (list (quote beingTold)) tgt)))))
|
||||
(define
|
||||
parse-scroll-cmd
|
||||
(fn
|
||||
()
|
||||
(let
|
||||
((tgt (if (or (at-end?) (and (= (tp-type) "keyword") (or (= (tp-val) "then") (= (tp-val) "end")))) (list (quote me)) (parse-expr))))
|
||||
((tgt (if (or (at-end?) (and (= (tp-type) "keyword") (or (= (tp-val) "then") (= (tp-val) "end")))) (list (quote beingTold)) (parse-expr))))
|
||||
(let
|
||||
((pos (cond ((match-kw "top") "top") ((match-kw "bottom") "bottom") ((match-kw "left") "left") ((match-kw "right") "right") (true "top"))))
|
||||
(list (quote scroll!) tgt pos)))))
|
||||
@@ -2335,14 +2349,14 @@
|
||||
(fn
|
||||
()
|
||||
(let
|
||||
((tgt (if (or (at-end?) (and (= (tp-type) "keyword") (or (= (tp-val) "then") (= (tp-val) "end")))) (list (quote me)) (parse-expr))))
|
||||
((tgt (if (or (at-end?) (and (= (tp-type) "keyword") (or (= (tp-val) "then") (= (tp-val) "end")))) (list (quote beingTold)) (parse-expr))))
|
||||
(list (quote select!) tgt))))
|
||||
(define
|
||||
parse-reset-cmd
|
||||
(fn
|
||||
()
|
||||
(let
|
||||
((tgt (if (or (at-end?) (and (= (tp-type) "keyword") (or (= (tp-val) "then") (= (tp-val) "end")))) (list (quote me)) (parse-expr))))
|
||||
((tgt (if (or (at-end?) (and (= (tp-type) "keyword") (or (= (tp-val) "then") (= (tp-val) "end")))) (list (quote beingTold)) (parse-expr))))
|
||||
(list (quote reset!) tgt))))
|
||||
(define
|
||||
parse-default-cmd
|
||||
@@ -2367,7 +2381,7 @@
|
||||
(fn
|
||||
()
|
||||
(let
|
||||
((tgt (cond ((at-end?) (list (quote me))) ((and (= (tp-type) "keyword") (or (= (tp-val) "then") (= (tp-val) "end"))) (list (quote me))) (true (parse-expr)))))
|
||||
((tgt (cond ((at-end?) (list (quote beingTold))) ((and (= (tp-type) "keyword") (or (= (tp-val) "then") (= (tp-val) "end"))) (list (quote beingTold))) (true (parse-expr)))))
|
||||
(list (quote focus!) tgt))))
|
||||
(define
|
||||
parse-feat-body
|
||||
@@ -2481,7 +2495,7 @@
|
||||
(fn
|
||||
()
|
||||
(let
|
||||
((target (cond ((at-end?) (list (quote ref) "me")) ((and (= (tp-type) "keyword") (or (= (tp-val) "then") (= (tp-val) "end"))) (list (quote ref) "me")) (true (parse-expr)))))
|
||||
((target (cond ((at-end?) (list (quote beingTold))) ((and (= (tp-type) "keyword") (or (= (tp-val) "then") (= (tp-val) "end"))) (list (quote beingTold))) (true (parse-expr)))))
|
||||
(list (quote empty-target) target))))
|
||||
(define
|
||||
parse-swap-cmd
|
||||
@@ -2506,15 +2520,42 @@
|
||||
(fn
|
||||
()
|
||||
(let
|
||||
((target (cond ((at-end?) (list (quote me))) ((and (= (tp-type) "keyword") (or (= (tp-val) "then") (= (tp-val) "end"))) (list (quote me))) (true (parse-expr)))))
|
||||
((target (cond ((at-end?) (list (quote beingTold))) ((and (= (tp-type) "keyword") (or (= (tp-val) "then") (= (tp-val) "end"))) (list (quote beingTold))) (true (parse-expr)))))
|
||||
(list (quote open-element) target))))
|
||||
(define
|
||||
parse-close-cmd
|
||||
(fn
|
||||
()
|
||||
(let
|
||||
((target (cond ((at-end?) (list (quote me))) ((and (= (tp-type) "keyword") (or (= (tp-val) "then") (= (tp-val) "end"))) (list (quote me))) (true (parse-expr)))))
|
||||
((target (cond ((at-end?) (list (quote beingTold))) ((and (= (tp-type) "keyword") (or (= (tp-val) "then") (= (tp-val) "end"))) (list (quote beingTold))) (true (parse-expr)))))
|
||||
(list (quote close-element) target))))
|
||||
(define
|
||||
parse-js-block
|
||||
(fn
|
||||
()
|
||||
(let
|
||||
((params (if (= (tp-type) "paren-open") (do (adv!) (define collect-params! (fn (acc) (cond ((or (at-end?) (= (tp-type) "paren-close")) (do (when (= (tp-type) "paren-close") (adv!)) acc)) ((= (tp-type) "comma") (do (adv!) (collect-params! acc))) (true (let ((pname (tp-val))) (do (adv!) (collect-params! (append acc pname)))))))) (collect-params! (list))) (list))))
|
||||
(let
|
||||
((js-start (cur-start)))
|
||||
(define
|
||||
skip-to-end!
|
||||
(fn
|
||||
()
|
||||
(if
|
||||
(or
|
||||
(at-end?)
|
||||
(and (= (tp-type) "keyword") (= (tp-val) "end")))
|
||||
nil
|
||||
(do (adv!) (skip-to-end!)))))
|
||||
(skip-to-end!)
|
||||
(let
|
||||
((js-end (cur-start)))
|
||||
(let
|
||||
((js-src (substring src js-start js-end)))
|
||||
(when
|
||||
(and (= (tp-type) "keyword") (= (tp-val) "end"))
|
||||
(adv!))
|
||||
(list (quote js-block) params js-src)))))))
|
||||
(define
|
||||
parse-cmd
|
||||
(fn
|
||||
@@ -2664,6 +2705,8 @@
|
||||
(do (adv!) (list (quote continue))))
|
||||
((and (= typ "keyword") (or (= val "exit") (= val "halt")))
|
||||
(do (adv!) (list (quote exit))))
|
||||
((and (= typ "keyword") (= val "js"))
|
||||
(do (adv!) (parse-js-block)))
|
||||
(true (parse-expr))))))
|
||||
(define
|
||||
parse-cmd-list
|
||||
@@ -2719,7 +2762,8 @@
|
||||
(= v "close")
|
||||
(= v "pick")
|
||||
(= v "ask")
|
||||
(= v "answer"))))
|
||||
(= v "answer")
|
||||
(= v "js"))))
|
||||
(define
|
||||
cl-collect
|
||||
(fn
|
||||
@@ -2906,63 +2950,6 @@
|
||||
(match-kw "end")
|
||||
(list (quote when-feat-no-op)))))
|
||||
(do (pwf-skip) (match-kw "end") (list (quote when-feat-no-op))))))
|
||||
(define
|
||||
parse-socket-feat
|
||||
(fn
|
||||
()
|
||||
(let
|
||||
((seg0 (tp-val)))
|
||||
(adv!)
|
||||
(define
|
||||
collect-segs
|
||||
(fn
|
||||
(acc)
|
||||
(if
|
||||
(= (tp-type) "class")
|
||||
(let
|
||||
((seg (tp-val)))
|
||||
(adv!)
|
||||
(collect-segs (append acc (list seg))))
|
||||
acc)))
|
||||
(let
|
||||
((name-path (collect-segs (list seg0))))
|
||||
(define
|
||||
url-cont?
|
||||
(fn
|
||||
()
|
||||
(or
|
||||
(= (tp-type) "ident")
|
||||
(= (tp-type) "number")
|
||||
(= (tp-type) "op")
|
||||
(= (tp-type) "colon")
|
||||
(= (tp-type) "dot")
|
||||
(and
|
||||
(= (tp-type) "keyword")
|
||||
(not
|
||||
(or
|
||||
(= (tp-val) "end")
|
||||
(= (tp-val) "with")
|
||||
(= (tp-val) "on")
|
||||
(= (tp-val) "as")))))))
|
||||
(define
|
||||
collect-url
|
||||
(fn
|
||||
(parts)
|
||||
(if
|
||||
(and (not (at-end?)) (url-cont?))
|
||||
(let
|
||||
((v (tp-val)))
|
||||
(adv!)
|
||||
(collect-url (append parts (list v))))
|
||||
(join "" parts))))
|
||||
(let
|
||||
((url (cond ((and (= (tp-type) "op") (= (tp-val) "/")) (do (adv!) (collect-url (list "/")))) ((= (tp-type) "ident") (let ((scheme (tp-val))) (adv!) (if (= (tp-type) "colon") (collect-url (list scheme)) (parse-arith (parse-poss (list (quote ref) scheme)))))) (true (parse-atom)))))
|
||||
(let
|
||||
((timeout-ms (if (match-kw "with") (do (adv!) (parse-expr)) nil)))
|
||||
(let
|
||||
((on-msg (if (match-kw "on") (do (adv!) (let ((json? (if (match-kw "as") (do (adv!) true) false))) (let ((body (parse-cmd-list))) (list (quote on-message) json? body)))) nil)))
|
||||
(match-kw "end")
|
||||
(list (quote socket) name-path url timeout-ms on-msg))))))))
|
||||
(define
|
||||
parse-feat
|
||||
(fn
|
||||
@@ -2983,7 +2970,6 @@
|
||||
((= val "behavior") (do (adv!) (parse-behavior-feat)))
|
||||
((= val "live") (do (adv!) (parse-live-feat)))
|
||||
((= val "when") (do (adv!) (parse-when-feat)))
|
||||
((= val "socket") (do (adv!) (parse-socket-feat)))
|
||||
((= val "worker")
|
||||
(error
|
||||
"worker plugin is not installed — see https://hyperscript.org/features/worker"))
|
||||
|
||||
3
shared/static/wasm/sx/hs-parser.sxbc
Normal file
3
shared/static/wasm/sx/hs-parser.sxbc
Normal file
File diff suppressed because one or more lines are too long
@@ -43,17 +43,7 @@
|
||||
|
||||
;; Run an initializer function immediately.
|
||||
;; (hs-init thunk) — called at element boot time
|
||||
(define
|
||||
hs-on
|
||||
(fn
|
||||
(target event-name handler)
|
||||
(let
|
||||
((wrapped (fn (event) (guard (e ((and (not (= event-name "exception")) (not (= event-name "error"))) (dom-dispatch target "exception" {:error e})) (true (raise e))) (do (handler event) (when event (host-call event "stopPropagation")))))))
|
||||
(let
|
||||
((unlisten (dom-listen target event-name wrapped))
|
||||
(prev (or (dom-get-data target "hs-unlisteners") (list))))
|
||||
(dom-set-data target "hs-unlisteners" (append prev (list unlisten)))
|
||||
unlisten))))
|
||||
(define meta (host-new "Object"))
|
||||
|
||||
;; ── Async / timing ──────────────────────────────────────────────
|
||||
|
||||
@@ -61,11 +51,39 @@
|
||||
;; In hyperscript, wait is async-transparent — execution pauses.
|
||||
;; Here we use perform/IO suspension for true pause semantics.
|
||||
(define
|
||||
hs-on-every
|
||||
(fn (target event-name handler) (dom-listen target event-name handler)))
|
||||
_hs-on-caller
|
||||
(let
|
||||
((_ctx (host-new "Object"))
|
||||
(_m (host-new "Object"))
|
||||
(_f (host-new "Object")))
|
||||
(do
|
||||
(host-set! _f "type" "onFeature")
|
||||
(host-set! _m "feature" _f)
|
||||
(host-set! _ctx "meta" _m)
|
||||
_ctx)))
|
||||
|
||||
;; Wait for a DOM event on a target.
|
||||
;; (hs-wait-for target event-name) — suspends until event fires
|
||||
(define
|
||||
hs-on
|
||||
(fn
|
||||
(target event-name handler)
|
||||
(let
|
||||
((wrapped (fn (event) (do (host-set! meta "caller" _hs-on-caller) (guard (e ((and (not (= event-name "exception")) (not (= event-name "error"))) (dom-dispatch target "exception" {:error e})) (true (raise e))) (handler event))))))
|
||||
(let
|
||||
((unlisten (dom-listen target event-name wrapped))
|
||||
(prev (or (dom-get-data target "hs-unlisteners") (list))))
|
||||
(dom-set-data target "hs-unlisteners" (append prev (list unlisten)))
|
||||
unlisten))))
|
||||
|
||||
;; Wait for CSS transitions/animations to settle on an element.
|
||||
(define
|
||||
hs-on-every
|
||||
(fn (target event-name handler) (dom-listen target event-name handler)))
|
||||
|
||||
;; ── Class manipulation ──────────────────────────────────────────
|
||||
|
||||
;; Toggle a single class on an element.
|
||||
(define
|
||||
hs-on-intersection-attach!
|
||||
(fn
|
||||
@@ -81,7 +99,7 @@
|
||||
(host-call observer "observe" target)
|
||||
observer)))))
|
||||
|
||||
;; Wait for CSS transitions/animations to settle on an element.
|
||||
;; Toggle between two classes — exactly one is active at a time.
|
||||
(define
|
||||
hs-on-mutation-attach!
|
||||
(fn
|
||||
@@ -102,16 +120,19 @@
|
||||
(host-call observer "observe" target opts)
|
||||
observer))))))
|
||||
|
||||
;; ── Class manipulation ──────────────────────────────────────────
|
||||
|
||||
;; Toggle a single class on an element.
|
||||
(define hs-init (fn (thunk) (thunk)))
|
||||
|
||||
;; Toggle between two classes — exactly one is active at a time.
|
||||
(define hs-wait (fn (ms) (perform (list (quote io-sleep) ms))))
|
||||
|
||||
;; Take a class from siblings — add to target, remove from others.
|
||||
;; (hs-take! target cls) — like radio button class behavior
|
||||
(define hs-init (fn (thunk) (thunk)))
|
||||
|
||||
;; ── DOM insertion ───────────────────────────────────────────────
|
||||
|
||||
;; Put content at a position relative to a target.
|
||||
;; pos: "into" | "before" | "after"
|
||||
(define hs-wait (fn (ms) (perform (list (quote io-sleep) ms))))
|
||||
|
||||
;; ── Navigation / traversal ──────────────────────────────────────
|
||||
|
||||
;; Navigate to a URL.
|
||||
(begin
|
||||
(define
|
||||
hs-wait-for
|
||||
@@ -124,20 +145,15 @@
|
||||
(target event-name timeout-ms)
|
||||
(perform (list (quote io-wait-event) target event-name timeout-ms)))))
|
||||
|
||||
;; ── DOM insertion ───────────────────────────────────────────────
|
||||
|
||||
;; Put content at a position relative to a target.
|
||||
;; pos: "into" | "before" | "after"
|
||||
;; Find next sibling matching a selector (or any sibling).
|
||||
(define hs-settle (fn (target) (perform (list (quote io-settle) target))))
|
||||
|
||||
;; ── Navigation / traversal ──────────────────────────────────────
|
||||
|
||||
;; Navigate to a URL.
|
||||
;; Find previous sibling matching a selector.
|
||||
(define
|
||||
hs-toggle-class!
|
||||
(fn (target cls) (host-call (host-get target "classList") "toggle" cls)))
|
||||
|
||||
;; Find next sibling matching a selector (or any sibling).
|
||||
;; First element matching selector within a scope.
|
||||
(define
|
||||
hs-toggle-between!
|
||||
(fn
|
||||
@@ -147,7 +163,7 @@
|
||||
(do (dom-remove-class target cls1) (dom-add-class target cls2))
|
||||
(do (dom-remove-class target cls2) (dom-add-class target cls1)))))
|
||||
|
||||
;; Find previous sibling matching a selector.
|
||||
;; Last element matching selector.
|
||||
(define
|
||||
hs-toggle-style!
|
||||
(fn
|
||||
@@ -171,7 +187,7 @@
|
||||
(dom-set-style target prop "hidden")
|
||||
(dom-set-style target prop "")))))))
|
||||
|
||||
;; First element matching selector within a scope.
|
||||
;; First/last within a specific scope.
|
||||
(define
|
||||
hs-toggle-style-between!
|
||||
(fn
|
||||
@@ -183,7 +199,6 @@
|
||||
(dom-set-style target prop val2)
|
||||
(dom-set-style target prop val1)))))
|
||||
|
||||
;; Last element matching selector.
|
||||
(define
|
||||
hs-toggle-style-cycle!
|
||||
(fn
|
||||
@@ -204,7 +219,9 @@
|
||||
(true (find-next (rest remaining))))))
|
||||
(dom-set-style target prop (find-next vals)))))
|
||||
|
||||
;; First/last within a specific scope.
|
||||
;; ── Iteration ───────────────────────────────────────────────────
|
||||
|
||||
;; Repeat a thunk N times.
|
||||
(define
|
||||
hs-take!
|
||||
(fn
|
||||
@@ -244,6 +261,7 @@
|
||||
(dom-set-attr target name attr-val)
|
||||
(dom-set-attr target name ""))))))))
|
||||
|
||||
;; Repeat forever (until break — relies on exception/continuation).
|
||||
(begin
|
||||
(define
|
||||
hs-element?
|
||||
@@ -355,9 +373,10 @@
|
||||
(dom-insert-adjacent-html target "beforeend" value)
|
||||
(hs-boot-subtree! target)))))))))
|
||||
|
||||
;; ── Iteration ───────────────────────────────────────────────────
|
||||
;; ── Fetch ───────────────────────────────────────────────────────
|
||||
|
||||
;; Repeat a thunk N times.
|
||||
;; Fetch a URL, parse response according to format.
|
||||
;; (hs-fetch url format) — format is "json" | "text" | "html"
|
||||
(define
|
||||
hs-add-to!
|
||||
(fn
|
||||
@@ -370,7 +389,10 @@
|
||||
(append target (list value))))
|
||||
(true (do (host-call target "push" value) target)))))
|
||||
|
||||
;; Repeat forever (until break — relies on exception/continuation).
|
||||
;; ── Type coercion ───────────────────────────────────────────────
|
||||
|
||||
;; Coerce a value to a type by name.
|
||||
;; (hs-coerce value type-name) — type-name is "Int", "Float", "String", etc.
|
||||
(define
|
||||
hs-remove-from!
|
||||
(fn
|
||||
@@ -380,10 +402,10 @@
|
||||
(filter (fn (x) (not (= x value))) target)
|
||||
(host-call target "splice" (host-call target "indexOf" value) 1))))
|
||||
|
||||
;; ── Fetch ───────────────────────────────────────────────────────
|
||||
;; ── Object creation ─────────────────────────────────────────────
|
||||
|
||||
;; Fetch a URL, parse response according to format.
|
||||
;; (hs-fetch url format) — format is "json" | "text" | "html"
|
||||
;; Make a new object of a given type.
|
||||
;; (hs-make type-name) — creates empty object/collection
|
||||
(define
|
||||
hs-splice-at!
|
||||
(fn
|
||||
@@ -407,10 +429,11 @@
|
||||
(host-call target "splice" i 1))))
|
||||
target))))
|
||||
|
||||
;; ── Type coercion ───────────────────────────────────────────────
|
||||
;; ── Behavior installation ───────────────────────────────────────
|
||||
|
||||
;; Coerce a value to a type by name.
|
||||
;; (hs-coerce value type-name) — type-name is "Int", "Float", "String", etc.
|
||||
;; Install a behavior on an element.
|
||||
;; A behavior is a function that takes (me ...params) and sets up features.
|
||||
;; (hs-install behavior-fn me ...args)
|
||||
(define
|
||||
hs-index
|
||||
(fn
|
||||
@@ -422,10 +445,10 @@
|
||||
((string? obj) (nth obj key))
|
||||
(true (host-get obj key)))))
|
||||
|
||||
;; ── Object creation ─────────────────────────────────────────────
|
||||
;; ── Measurement ─────────────────────────────────────────────────
|
||||
|
||||
;; Make a new object of a given type.
|
||||
;; (hs-make type-name) — creates empty object/collection
|
||||
;; Measure an element's bounding rect, store as local variables.
|
||||
;; Returns a dict with x, y, width, height, top, left, right, bottom.
|
||||
(define
|
||||
hs-put-at!
|
||||
(fn
|
||||
@@ -447,11 +470,10 @@
|
||||
((= pos "start") (host-call target "unshift" value)))
|
||||
target)))))))
|
||||
|
||||
;; ── Behavior installation ───────────────────────────────────────
|
||||
|
||||
;; Install a behavior on an element.
|
||||
;; A behavior is a function that takes (me ...params) and sets up features.
|
||||
;; (hs-install behavior-fn me ...args)
|
||||
;; Return the current text selection as a string. In the browser this is
|
||||
;; `window.getSelection().toString()`. In the mock test runner, a test
|
||||
;; setup stashes the desired selection text at `window.__test_selection`
|
||||
;; and the fallback path returns that so tests can assert on the result.
|
||||
(define
|
||||
hs-dict-without
|
||||
(fn
|
||||
@@ -472,27 +494,19 @@
|
||||
(host-call (host-global "Reflect") "deleteProperty" out key)
|
||||
out)))))
|
||||
|
||||
;; ── Measurement ─────────────────────────────────────────────────
|
||||
|
||||
;; Measure an element's bounding rect, store as local variables.
|
||||
;; Returns a dict with x, y, width, height, top, left, right, bottom.
|
||||
;; ── Transition ──────────────────────────────────────────────────
|
||||
|
||||
;; Transition a CSS property to a value, optionally with duration.
|
||||
;; (hs-transition target prop value duration)
|
||||
(define
|
||||
hs-set-on!
|
||||
(fn
|
||||
(props target)
|
||||
(for-each (fn (k) (host-set! target k (get props k))) (keys props))))
|
||||
|
||||
;; Return the current text selection as a string. In the browser this is
|
||||
;; `window.getSelection().toString()`. In the mock test runner, a test
|
||||
;; setup stashes the desired selection text at `window.__test_selection`
|
||||
;; and the fallback path returns that so tests can assert on the result.
|
||||
(define hs-navigate! (fn (url) (perform (list (quote io-navigate) url))))
|
||||
|
||||
|
||||
;; ── Transition ──────────────────────────────────────────────────
|
||||
|
||||
;; Transition a CSS property to a value, optionally with duration.
|
||||
;; (hs-transition target prop value duration)
|
||||
(define
|
||||
hs-ask
|
||||
(fn
|
||||
@@ -631,6 +645,10 @@
|
||||
(true (find-next (dom-next-sibling el))))))
|
||||
(find-next sibling)))))
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
(define
|
||||
hs-previous
|
||||
(fn
|
||||
@@ -650,33 +668,36 @@
|
||||
(true (find-prev (dom-get-prop el "previousElementSibling"))))))
|
||||
(find-prev sibling)))))
|
||||
|
||||
(define hs-query-all (fn (sel) (dom-query-all (dom-body) sel)))
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
(define
|
||||
hs-query-all
|
||||
(fn (sel) (host-call (dom-body) "querySelectorAll" sel)))
|
||||
;; ── Sandbox/test runtime additions ──────────────────────────────
|
||||
;; Property access — dot notation and .length
|
||||
(define
|
||||
hs-query-all-in
|
||||
(fn
|
||||
(sel target)
|
||||
(if (nil? target) (hs-query-all sel) (dom-query-all target sel))))
|
||||
|
||||
(if
|
||||
(nil? target)
|
||||
(hs-query-all sel)
|
||||
(host-call target "querySelectorAll" sel))))
|
||||
;; DOM query stub — sandbox returns empty list
|
||||
(define
|
||||
hs-list-set
|
||||
(fn
|
||||
(lst idx val)
|
||||
(append (take lst idx) (cons val (drop lst (+ idx 1))))))
|
||||
;; ── Sandbox/test runtime additions ──────────────────────────────
|
||||
;; Property access — dot notation and .length
|
||||
;; Method dispatch — obj.method(args)
|
||||
(define
|
||||
hs-to-number
|
||||
(fn (v) (if (number? v) v (or (parse-number (str v)) 0))))
|
||||
;; DOM query stub — sandbox returns empty list
|
||||
|
||||
;; ── 0.9.90 features ─────────────────────────────────────────────
|
||||
;; beep! — debug logging, returns value unchanged
|
||||
(define
|
||||
hs-query-first
|
||||
(fn (sel) (host-call (host-global "document") "querySelector" sel)))
|
||||
;; Method dispatch — obj.method(args)
|
||||
;; Property-based is — check obj.key truthiness
|
||||
(define
|
||||
hs-query-last
|
||||
(fn
|
||||
@@ -684,11 +705,9 @@
|
||||
(let
|
||||
((all (dom-query-all (dom-body) sel)))
|
||||
(if (> (len all) 0) (nth all (- (len all) 1)) nil))))
|
||||
|
||||
;; ── 0.9.90 features ─────────────────────────────────────────────
|
||||
;; beep! — debug logging, returns value unchanged
|
||||
;; Array slicing (inclusive both ends)
|
||||
(define hs-first (fn (scope sel) (dom-query-all scope sel)))
|
||||
;; Property-based is — check obj.key truthiness
|
||||
;; Collection: sorted by
|
||||
(define
|
||||
hs-last
|
||||
(fn
|
||||
@@ -696,7 +715,7 @@
|
||||
(let
|
||||
((all (dom-query-all scope sel)))
|
||||
(if (> (len all) 0) (nth all (- (len all) 1)) nil))))
|
||||
;; Array slicing (inclusive both ends)
|
||||
;; Collection: sorted by descending
|
||||
(define
|
||||
hs-repeat-times
|
||||
(fn
|
||||
@@ -714,7 +733,7 @@
|
||||
((= signal "hs-continue") (do-repeat (+ i 1)))
|
||||
(true (do-repeat (+ i 1))))))))
|
||||
(do-repeat 0)))
|
||||
;; Collection: sorted by
|
||||
;; Collection: split by
|
||||
(define
|
||||
hs-repeat-forever
|
||||
(fn
|
||||
@@ -730,7 +749,7 @@
|
||||
((= signal "hs-continue") (do-forever))
|
||||
(true (do-forever))))))
|
||||
(do-forever)))
|
||||
;; Collection: sorted by descending
|
||||
;; Collection: joined by
|
||||
(define
|
||||
hs-repeat-while
|
||||
(fn
|
||||
@@ -743,7 +762,7 @@
|
||||
((= signal "hs-break") nil)
|
||||
((= signal "hs-continue") (hs-repeat-while cond-fn thunk))
|
||||
(true (hs-repeat-while cond-fn thunk)))))))
|
||||
;; Collection: split by
|
||||
|
||||
(define
|
||||
hs-repeat-until
|
||||
(fn
|
||||
@@ -755,13 +774,13 @@
|
||||
((= signal "hs-continue")
|
||||
(if (cond-fn) nil (hs-repeat-until cond-fn thunk)))
|
||||
(true (if (cond-fn) nil (hs-repeat-until cond-fn thunk)))))))
|
||||
;; Collection: joined by
|
||||
|
||||
(define
|
||||
hs-for-each
|
||||
(fn
|
||||
(fn-body collection)
|
||||
(let
|
||||
((items (cond ((list? collection) collection) ((dict? collection) (if (dict-has? collection "_order") (get collection "_order") (filter (fn (k) (not (= k "_order"))) (keys collection)))) ((nil? collection) (list)) (true (list)))))
|
||||
((items (cond ((list? collection) collection) ((nil? collection) (list)) ((host-iter? collection) (host-to-list collection)) ((dict? collection) (if (dict-has? collection "_order") (get collection "_order") (filter (fn (k) (not (= k "_order"))) (keys collection)))) (true (list)))))
|
||||
(define
|
||||
do-loop
|
||||
(fn
|
||||
@@ -869,33 +888,12 @@
|
||||
(define
|
||||
hs-fetch
|
||||
(fn
|
||||
(url format do-not-throw target)
|
||||
(url format)
|
||||
(let
|
||||
((fmt (cond ((nil? format) "text") ((or (= format "json") (= format "JSON") (= format "Object")) "json") ((or (= format "html") (= format "HTML")) "html") ((or (= format "response") (= format "Response")) "response") ((or (= format "text") (= format "Text")) "text") ((or (= format "number") (= format "Number")) "number") (true format))))
|
||||
(do
|
||||
(when (not (nil? target))
|
||||
(dom-dispatch target "hyperscript:beforeFetch" nil))
|
||||
(let
|
||||
((raw (perform (list "io-fetch" url "response" (dict)))))
|
||||
(do
|
||||
(when (get raw :_network-error) (raise {:response raw :message "Network error" :_hs-error "FetchError"}))
|
||||
(when
|
||||
(and (not (get raw :ok)) (not (= fmt "response")) (not do-not-throw))
|
||||
(raise {:response raw :status (get raw :status) :message "Fetch error" :_hs-error "FetchError"}))
|
||||
(cond
|
||||
((= fmt "response") raw)
|
||||
((= fmt "json")
|
||||
(let
|
||||
((parsed (perform (list "io-parse-json" (get raw :_json)))))
|
||||
(hs-host-to-sx parsed)))
|
||||
((= fmt "html")
|
||||
(perform (list "io-parse-html" (get raw :_html))))
|
||||
((= fmt "number")
|
||||
(or
|
||||
(parse-number (get raw :_number))
|
||||
(parse-number (get raw :_body))
|
||||
0))
|
||||
(true (get raw :_body)))))))))
|
||||
((fmt (cond ((nil? format) "text") ((or (= format "json") (= format "JSON") (= format "Object")) "json") ((or (= format "html") (= format "HTML")) "html") ((or (= format "response") (= format "Response")) "response") ((or (= format "text") (= format "Text")) "text") (true format))))
|
||||
(let
|
||||
((raw (perform (list "io-fetch" url fmt))))
|
||||
(cond ((= fmt "json") (hs-host-to-sx raw)) (true raw))))))
|
||||
|
||||
(define
|
||||
hs-json-escape
|
||||
@@ -986,8 +984,6 @@
|
||||
(true (str value))))
|
||||
((= type-name "JSON")
|
||||
(cond
|
||||
((and (dict? value) (dict-has? value :_json))
|
||||
(guard (_e (true value)) (json-parse (get value :_json))))
|
||||
((string? value) (guard (_e (true value)) (json-parse value)))
|
||||
((dict? value) (hs-json-stringify value))
|
||||
((list? value) (hs-json-stringify value))
|
||||
@@ -1366,14 +1362,21 @@
|
||||
hs-transition
|
||||
(fn
|
||||
(target prop value duration)
|
||||
(when
|
||||
duration
|
||||
(dom-set-style
|
||||
target
|
||||
"transition"
|
||||
(str prop " " (/ duration 1000) "s")))
|
||||
(dom-set-style target prop value)
|
||||
(when duration (hs-settle target))))
|
||||
(let
|
||||
((init-attr (str "data-hs-init-" prop)))
|
||||
(when
|
||||
(not (dom-get-attr target init-attr))
|
||||
(dom-set-attr target init-attr (dom-get-style target prop)))
|
||||
(let
|
||||
((actual-value (if (= value "initial") (dom-get-attr target init-attr) value)))
|
||||
(when
|
||||
duration
|
||||
(dom-set-style
|
||||
target
|
||||
"transition"
|
||||
(str prop " " (/ duration 1000) "s")))
|
||||
(dom-set-style target prop actual-value)
|
||||
(when duration (hs-settle target))))))
|
||||
|
||||
(define
|
||||
hs-transition-from
|
||||
@@ -2124,11 +2127,20 @@
|
||||
(fn
|
||||
(pairs)
|
||||
(let
|
||||
((d (dict)))
|
||||
(begin
|
||||
((d {}) (order (list)))
|
||||
(do
|
||||
(for-each
|
||||
(fn (pair) (dict-set! d (first pair) (nth pair 1)))
|
||||
(fn
|
||||
(pair)
|
||||
(let
|
||||
((k (first pair)))
|
||||
(do
|
||||
(when
|
||||
(not (dict-has? d k))
|
||||
(set! order (append order (list k))))
|
||||
(dict-set! d k (nth pair 1)))))
|
||||
pairs)
|
||||
(when (not (empty? order)) (dict-set! d "_order" order))
|
||||
d))))
|
||||
|
||||
(define
|
||||
@@ -2529,6 +2541,8 @@
|
||||
((nth entry 2) val)))
|
||||
_hs-dom-watchers)))
|
||||
|
||||
;; ── SourceInfo API ────────────────────────────────────────────────
|
||||
|
||||
(define
|
||||
hs-dom-is-ancestor?
|
||||
(fn
|
||||
@@ -2538,8 +2552,6 @@
|
||||
((= a b) true)
|
||||
(true (hs-dom-is-ancestor? a (dom-parent b))))))
|
||||
|
||||
;; ── SourceInfo API ────────────────────────────────────────────────
|
||||
|
||||
(define
|
||||
hs-win-call
|
||||
(fn
|
||||
@@ -2593,112 +2605,161 @@
|
||||
(walk (hs-node-get node (first keys)) (rest keys)))))
|
||||
(hs-line-for (walk (hs-parse-ast src-str) path))))
|
||||
|
||||
;; ── WebSocket / socket feature ───────────────────────────────────
|
||||
|
||||
(define
|
||||
hs-try-json-parse
|
||||
(fn (s) (host-call (host-global "JSON") "parse" s)))
|
||||
|
||||
(define
|
||||
hs-socket-resolve-rpc!
|
||||
hs-js-exec
|
||||
(fn
|
||||
(wrapper msg)
|
||||
(param-names js-src bound-args)
|
||||
(let
|
||||
((pending (host-get wrapper "pending")) (iid (host-get msg "iid")))
|
||||
((js-fn (host-new-function param-names js-src)))
|
||||
(let
|
||||
((resolver (host-get pending iid)))
|
||||
(when
|
||||
(not (nil? resolver))
|
||||
(if
|
||||
(not (nil? (host-get msg "return")))
|
||||
(host-call resolver "resolve" (host-get msg "return"))
|
||||
(host-call resolver "reject" (host-get msg "throw")))
|
||||
(host-set! pending iid nil))))))
|
||||
|
||||
(define
|
||||
hs-socket-register!
|
||||
(fn
|
||||
(name-path url timeout-ms handler json?)
|
||||
(let
|
||||
((ws-url (cond ((or (starts-with? url "ws://") (starts-with? url "wss://")) url) (true (let ((proto (host-get (host-global "location") "protocol")) (h (host-get (host-global "location") "host"))) (str (if (= proto "https:") "wss:" "ws:") "//" h url))))))
|
||||
(let
|
||||
((ws (host-new "WebSocket" ws-url)))
|
||||
(let
|
||||
((wrapper (host-new "Object")))
|
||||
(host-set! wrapper "raw" ws)
|
||||
(host-set! wrapper "url" ws-url)
|
||||
(host-set! wrapper "timeout" timeout-ms)
|
||||
(host-set! wrapper "pending" (host-new "Object"))
|
||||
(host-set! wrapper "handler" handler)
|
||||
(host-set! wrapper "json?" json?)
|
||||
(host-set! wrapper "closed?" false)
|
||||
(host-set! wrapper "closedFlag" nil)
|
||||
((result (host-call-fn js-fn bound-args)))
|
||||
(if
|
||||
(= (host-typeof result) "promise")
|
||||
(let
|
||||
((proxy-factory (host-global "_hs_make_rpc_proxy")))
|
||||
(when
|
||||
proxy-factory
|
||||
(host-set!
|
||||
wrapper
|
||||
"rpc"
|
||||
(host-call proxy-factory "call" nil wrapper))))
|
||||
(host-set!
|
||||
ws
|
||||
"onmessage"
|
||||
(host-callback
|
||||
(fn
|
||||
(event)
|
||||
(let
|
||||
((data (host-get event "data")))
|
||||
(let
|
||||
((parsed (hs-try-json-parse data)))
|
||||
(cond
|
||||
((and (not (nil? parsed)) (not (nil? (host-get parsed "iid"))))
|
||||
(hs-socket-resolve-rpc! wrapper parsed))
|
||||
((not (nil? handler))
|
||||
(if
|
||||
json?
|
||||
(if
|
||||
(not (nil? parsed))
|
||||
(handler parsed)
|
||||
(error "Received non-JSON message"))
|
||||
(handler event)))))))))
|
||||
(host-call
|
||||
ws
|
||||
"addEventListener"
|
||||
"close"
|
||||
(host-callback
|
||||
(fn
|
||||
(evt)
|
||||
(host-set! wrapper "closedFlag" "1"))))
|
||||
(host-set!
|
||||
wrapper
|
||||
"dispatchEvent"
|
||||
(host-callback
|
||||
(fn
|
||||
(evt)
|
||||
(let
|
||||
((payload (host-new "Object")))
|
||||
(host-set! payload "type" (host-get evt "type"))
|
||||
(host-call
|
||||
(host-get wrapper "raw")
|
||||
"send"
|
||||
(host-call
|
||||
(host-global "JSON")
|
||||
"stringify"
|
||||
payload))))))
|
||||
(define
|
||||
bind-path!
|
||||
(fn
|
||||
(obj path)
|
||||
(if
|
||||
(= (len path) 1)
|
||||
(host-set! obj (first path) wrapper)
|
||||
(let
|
||||
((key (first path)) (rest-path (rest path)))
|
||||
(let
|
||||
((next (or (host-get obj key) (host-new "Object"))))
|
||||
(host-set! obj key next)
|
||||
(bind-path! next rest-path))))))
|
||||
(bind-path! (host-global "window") name-path)
|
||||
wrapper)))))
|
||||
((state (host-promise-state result)))
|
||||
(if
|
||||
(and state (= (host-get state "ok") false))
|
||||
(raise (host-get state "value"))
|
||||
(if state (host-get state "value") result)))
|
||||
result)))))
|
||||
|
||||
(define
|
||||
hs-raw->api-token
|
||||
(fn
|
||||
(raw)
|
||||
(let
|
||||
((type (dict-get raw :type)) (value (dict-get raw :value)))
|
||||
(cond
|
||||
(= type "ident")
|
||||
{:value value :type "IDENTIFIER" :op false}
|
||||
(= type "keyword")
|
||||
{:value value :type "IDENTIFIER" :op false}
|
||||
(= type "number")
|
||||
{:value value :type "NUMBER" :op false}
|
||||
(= type "string")
|
||||
{:value value :type "STRING" :op false}
|
||||
(= type "class")
|
||||
{:value (str "." value) :type "CLASS_REF" :op false}
|
||||
(= type "id")
|
||||
{:value (str "#" value) :type "ID_REF" :op false}
|
||||
(= type "attr")
|
||||
{:value value :type "ATTRIBUTE_REF" :op false}
|
||||
(= type "style")
|
||||
{:value value :type "STYLE_REF" :op false}
|
||||
(= type "selector")
|
||||
{:value value :type "QUERY_REF" :op false}
|
||||
(= type "eof")
|
||||
{:value "<<<EOF>>>" :type "EOF" :op false}
|
||||
(= type "paren-open")
|
||||
{:value value :type "L_PAREN" :op true}
|
||||
(= type "paren-close")
|
||||
{:value value :type "R_PAREN" :op true}
|
||||
(= type "bracket-open")
|
||||
{:value value :type "L_BRACKET" :op true}
|
||||
(= type "bracket-close")
|
||||
{:value value :type "R_BRACKET" :op true}
|
||||
(= type "brace-open")
|
||||
{:value value :type "L_BRACE" :op true}
|
||||
(= type "brace-close")
|
||||
{:value value :type "R_BRACE" :op true}
|
||||
(= type "comma")
|
||||
{:value value :type "COMMA" :op true}
|
||||
(= type "dot")
|
||||
{:value value :type "PERIOD" :op true}
|
||||
(= type "colon")
|
||||
{:value value :type "COLON" :op true}
|
||||
(= type "op")
|
||||
(cond
|
||||
(= value "+") {:value value :type "PLUS" :op true}
|
||||
(= value "-") {:value value :type "MINUS" :op true}
|
||||
(= value "*") {:value value :type "MULTIPLY" :op true}
|
||||
(= value "/") {:value value :type "SLASH" :op true}
|
||||
(= value "!") {:value value :type "EXCLAMATION" :op true}
|
||||
(= value "?") {:value value :type "QUESTION" :op true}
|
||||
(= value "#") {:value value :type "POUND" :op true}
|
||||
(= value "&") {:value value :type "AMPERSAND" :op true}
|
||||
(= value "=") {:value value :type "EQUALS" :op true}
|
||||
(= value "<") {:value value :type "L_ANG" :op true}
|
||||
(= value ">") {:value value :type "R_ANG" :op true}
|
||||
(= value "<=") {:value value :type "LTE_ANG" :op true}
|
||||
(= value ">=") {:value value :type "GTE_ANG" :op true}
|
||||
(= value "==") {:value value :type "EQ" :op true}
|
||||
(= value "===") {:value value :type "EQQ" :op true}
|
||||
(= value "..") {:value value :type "PERIOD_PERIOD" :op true}
|
||||
:else {:value value :type value :op true})
|
||||
:else {:value (or value "") :type (str type) :op false}))))
|
||||
|
||||
(define hs-eof-sentinel {:value "<<<EOF>>>" :type "EOF" :op false})
|
||||
|
||||
(define
|
||||
hs-tokens-of
|
||||
(fn
|
||||
(src &rest args)
|
||||
(let
|
||||
((template (some (fn (a) (equal? a :template)) args)))
|
||||
(let
|
||||
((raw (if template (hs-tokenize-template src) (hs-tokenize src))))
|
||||
{:pos 0 :list (filter (fn (t) (not (= (dict-get t :type) "EOF"))) (map hs-raw->api-token raw)) :source src}))))
|
||||
|
||||
(define
|
||||
hs-stream-token
|
||||
(fn
|
||||
(s i)
|
||||
(let
|
||||
((lst (dict-get s :list))
|
||||
(n (len (dict-get s :list))))
|
||||
(define
|
||||
find
|
||||
(fn
|
||||
(pos count)
|
||||
(if
|
||||
(>= pos n)
|
||||
hs-eof-sentinel
|
||||
(let
|
||||
((tok (nth lst pos)))
|
||||
(if
|
||||
(= (dict-get tok :type) "whitespace")
|
||||
(find (+ pos 1) count)
|
||||
(if
|
||||
(= count 0)
|
||||
tok
|
||||
(find (+ pos 1) (- count 1))))))))
|
||||
(find (dict-get s :pos) i))))
|
||||
|
||||
(define
|
||||
hs-stream-consume
|
||||
(fn
|
||||
(s)
|
||||
(let
|
||||
((lst (dict-get s :list))
|
||||
(n (len (dict-get s :list))))
|
||||
(define
|
||||
find-pos
|
||||
(fn
|
||||
(pos)
|
||||
(if
|
||||
(>= pos n)
|
||||
pos
|
||||
(if
|
||||
(= (dict-get (nth lst pos) :type) "whitespace")
|
||||
(find-pos (+ pos 1))
|
||||
pos))))
|
||||
(let
|
||||
((p (find-pos (dict-get s :pos))))
|
||||
(let
|
||||
((tok (if (>= p n) hs-eof-sentinel (nth lst p))))
|
||||
(do
|
||||
(when
|
||||
(not (= (dict-get tok :type) "EOF"))
|
||||
(dict-set! s :pos (+ p 1)))
|
||||
tok))))))
|
||||
|
||||
(define
|
||||
hs-stream-has-more
|
||||
(fn (s) (not (= (dict-get (hs-stream-token s 0) :type) "EOF"))))
|
||||
|
||||
(define hs-token-type (fn (tok) (dict-get tok :type)))
|
||||
|
||||
(define hs-token-value (fn (tok) (dict-get tok :value)))
|
||||
|
||||
(define hs-token-op? (fn (tok) (dict-get tok :op)))
|
||||
|
||||
3
shared/static/wasm/sx/hs-runtime.sxbc
Normal file
3
shared/static/wasm/sx/hs-runtime.sxbc
Normal file
File diff suppressed because one or more lines are too long
@@ -465,7 +465,12 @@
|
||||
scan!
|
||||
(fn
|
||||
()
|
||||
(skip-ws!)
|
||||
(let
|
||||
((ws-start pos))
|
||||
(skip-ws!)
|
||||
(when
|
||||
(and (> (len tokens) 0) (> pos ws-start))
|
||||
(hs-emit! "whitespace" (slice src ws-start pos) ws-start)))
|
||||
(when
|
||||
(< pos src-len)
|
||||
(let
|
||||
@@ -473,11 +478,7 @@
|
||||
(cond
|
||||
(and (= ch "-") (< (+ pos 1) src-len) (= (hs-peek 1) "-"))
|
||||
(do (hs-advance! 2) (skip-comment!) (scan!))
|
||||
(and
|
||||
(= ch "/")
|
||||
(< (+ pos 1) src-len)
|
||||
(= (hs-peek 1) "/")
|
||||
(not (and (> pos 0) (= (hs-peek -1) ":"))))
|
||||
(and (= ch "/") (< (+ pos 1) src-len) (= (hs-peek 1) "/"))
|
||||
(do (hs-advance! 2) (skip-comment!) (scan!))
|
||||
(and
|
||||
(= ch "<")
|
||||
@@ -493,6 +494,15 @@
|
||||
(do (hs-emit! "selector" (read-selector) start) (scan!))
|
||||
(and (= ch ".") (< (+ pos 1) src-len) (= (hs-peek 1) "."))
|
||||
(do (hs-emit! "op" ".." start) (hs-advance! 2) (scan!))
|
||||
(and
|
||||
(= ch ".")
|
||||
(< (+ pos 1) src-len)
|
||||
(or (hs-letter? (hs-peek 1)) (= (hs-peek 1) "-") (= (hs-peek 1) "_"))
|
||||
(> (len tokens) 0)
|
||||
(let
|
||||
((lt (dict-get (nth tokens (- (len tokens) 1)) :type)))
|
||||
(or (= lt "paren-close") (= lt "brace-close") (= lt "bracket-close"))))
|
||||
(do (hs-emit! "dot" "." start) (hs-advance! 1) (scan!))
|
||||
(and
|
||||
(= ch ".")
|
||||
(< (+ pos 1) src-len)
|
||||
@@ -504,6 +514,15 @@
|
||||
(hs-advance! 1)
|
||||
(hs-emit! "class" (read-class-name pos) start)
|
||||
(scan!))
|
||||
(and
|
||||
(= ch "#")
|
||||
(< (+ pos 1) src-len)
|
||||
(hs-ident-start? (hs-peek 1))
|
||||
(> (len tokens) 0)
|
||||
(let
|
||||
((lt (dict-get (nth tokens (- (len tokens) 1)) :type)))
|
||||
(or (= lt "paren-close") (= lt "brace-close") (= lt "bracket-close"))))
|
||||
(do (hs-emit! "op" "#" start) (hs-advance! 1) (scan!))
|
||||
(and
|
||||
(= ch "#")
|
||||
(< (+ pos 1) src-len)
|
||||
|
||||
3
shared/static/wasm/sx/hs-tokenizer.sxbc
Normal file
3
shared/static/wasm/sx/hs-tokenizer.sxbc
Normal file
File diff suppressed because one or more lines are too long
@@ -46045,7 +46045,7 @@ d2=133,bi=102,bh="Re__Hash_set",cA="Stdlib__Type",cB=114,fF="Stdlib__Buffer",dX=
|
||||
}
|
||||
return trampoline(eval_expr(Sx_types[75].call(null, mac), local));
|
||||
}
|
||||
var step_limit = [0, 0], step_count = [0, 0];
|
||||
var step_limit = [0, 0], step_count = [0, 0], _wc_check = 0;
|
||||
function cek_step_loop(state$0){
|
||||
var state = state$0;
|
||||
for(;;){
|
||||
@@ -46055,6 +46055,11 @@ d2=133,bi=102,bh="Re__Hash_set",cA="Stdlib__Type",cB=114,fF="Stdlib__Buffer",dX=
|
||||
throw caml_maybe_attach_backtrace
|
||||
([0, Sx_types[9], "TIMEOUT: step limit exceeded"], 1);
|
||||
}
|
||||
if(++_wc_check >= 10000){ _wc_check = 0;
|
||||
if(globalThis.__hs_deadline && Date.now() > globalThis.__hs_deadline)
|
||||
throw caml_maybe_attach_backtrace
|
||||
([0, Sx_types[9], "TIMEOUT: wall clock exceeded"], 1);
|
||||
}
|
||||
var
|
||||
or = cek_terminal_p(state),
|
||||
or$0 = Sx_types[56].call(null, or) ? or : cek_suspended_p(state);
|
||||
|
||||
@@ -93,6 +93,17 @@
|
||||
(raise _e))))
|
||||
(handler me-val))))))
|
||||
|
||||
;; Evaluate a HS expression using evalStatically semantics:
|
||||
;; only literal values (numbers, strings, booleans, null, time units)
|
||||
;; succeed — any other expression raises "cannot be evaluated statically".
|
||||
(define hs-eval-statically
|
||||
(fn (src)
|
||||
(let ((ast (hs-compile src)))
|
||||
(if (or (number? ast) (string? ast) (boolean? ast)
|
||||
(and (list? ast) (= (first ast) (quote null-literal))))
|
||||
(eval-hs src)
|
||||
(raise "cannot be evaluated statically")))))
|
||||
|
||||
;; ── add (19 tests) ──
|
||||
(defsuite "hs-upstream-add"
|
||||
(deftest "can add a value to a set"
|
||||
@@ -1123,9 +1134,11 @@
|
||||
;; ── breakpoint (2 tests) ──
|
||||
(defsuite "hs-upstream-breakpoint"
|
||||
(deftest "parses as a top-level command"
|
||||
(error "SKIP (untranslated): parses as a top-level command"))
|
||||
(hs-compile "breakpoint")
|
||||
)
|
||||
(deftest "parses inside an event handler"
|
||||
(error "SKIP (untranslated): parses inside an event handler"))
|
||||
(hs-compile "on click breakpoint end")
|
||||
)
|
||||
)
|
||||
|
||||
;; ── call (6 tests) ──
|
||||
@@ -1586,11 +1599,14 @@
|
||||
;; ── core/evalStatically (8 tests) ──
|
||||
(defsuite "hs-upstream-core/evalStatically"
|
||||
(deftest "throws on math expressions"
|
||||
(error "SKIP (untranslated): throws on math expressions"))
|
||||
(guard (_e (true nil)) (hs-eval-statically "1 + 2") (error "hs-eval-statically did not throw for: 1 + 2"))
|
||||
)
|
||||
(deftest "throws on symbol references"
|
||||
(error "SKIP (untranslated): throws on symbol references"))
|
||||
(guard (_e (true nil)) (hs-eval-statically "x") (error "hs-eval-statically did not throw for: x"))
|
||||
)
|
||||
(deftest "throws on template strings"
|
||||
(error "SKIP (untranslated): throws on template strings"))
|
||||
(guard (_e (true nil)) (hs-eval-statically "`hello ${name}`") (error "hs-eval-statically did not throw for: `hello ${name}`"))
|
||||
)
|
||||
(deftest "works on boolean literals"
|
||||
(assert= (eval-hs "true") true)
|
||||
(assert= (eval-hs "false") false)
|
||||
@@ -4214,13 +4230,17 @@
|
||||
;; ── expressions/blockLiteral (4 tests) ──
|
||||
(defsuite "hs-upstream-expressions/blockLiteral"
|
||||
(deftest "basic block literals work"
|
||||
(error "SKIP (untranslated): basic block literals work"))
|
||||
(assert= (apply (eval-expr-cek (hs-to-sx (hs-compile "\\ -> true"))) (list)) true)
|
||||
)
|
||||
(deftest "basic identity works"
|
||||
(error "SKIP (untranslated): basic identity works"))
|
||||
(assert= (apply (eval-expr-cek (hs-to-sx (hs-compile "\\ x -> x"))) (list true)) true)
|
||||
)
|
||||
(deftest "basic two arg identity works"
|
||||
(error "SKIP (untranslated): basic two arg identity works"))
|
||||
(assert= (apply (eval-expr-cek (hs-to-sx (hs-compile "\\ x, y -> y"))) (list false true)) true)
|
||||
)
|
||||
(deftest "can map an array"
|
||||
(error "SKIP (untranslated): can map an array"))
|
||||
(assert= (map (eval-expr-cek (hs-to-sx (hs-compile "\\ s -> s.length"))) (list "a" "ab" "abc")) (list 1 2 3))
|
||||
)
|
||||
)
|
||||
|
||||
;; ── expressions/boolean (2 tests) ──
|
||||
@@ -5204,7 +5224,17 @@
|
||||
(eval-hs "set cookies.foo to 'bar'")
|
||||
(assert= (eval-hs "cookies.foo") "bar"))
|
||||
(deftest "iterate cookies values work"
|
||||
(error "SKIP (untranslated): iterate cookies values work"))
|
||||
(hs-cleanup!)
|
||||
(host-set! (host-global "cookies") "foo" "bar")
|
||||
(let ((_names (list)) (_values (list)))
|
||||
(hs-for-each
|
||||
(fn (x)
|
||||
(append! _names (host-get x "name"))
|
||||
(append! _values (host-get x "value")))
|
||||
(host-global "cookies"))
|
||||
(assert-contains "foo" _names)
|
||||
(assert-contains "bar" _values))
|
||||
)
|
||||
(deftest "length is 0 when no cookies are set"
|
||||
(hs-cleanup!)
|
||||
(assert= (eval-hs "cookies.length") 0))
|
||||
@@ -11821,166 +11851,37 @@
|
||||
;; ── socket (16 tests) ──
|
||||
(defsuite "hs-upstream-socket"
|
||||
(deftest "converts relative URL to ws:// on http pages"
|
||||
(hs-cleanup!)
|
||||
(host-set! (host-global "window") "__hs_ws_created" (list))
|
||||
(eval-hs "socket RelSocket /my-ws end")
|
||||
(let ((sock (host-get (host-global "__hs_ws_created") 0)))
|
||||
(assert= (host-get sock "url") "ws://localhost/my-ws")))
|
||||
(error "SKIP (untranslated): converts relative URL to ws:// on http pages"))
|
||||
(deftest "converts relative URL to wss:// on https pages"
|
||||
(hs-cleanup!)
|
||||
(host-set! (host-global "window") "__hs_ws_created" (list))
|
||||
(host-set! (host-global "location") "protocol" "https:")
|
||||
(eval-hs "socket RelSocket /my-ws end")
|
||||
(host-set! (host-global "location") "protocol" "http:")
|
||||
(let ((sock (host-get (host-global "__hs_ws_created") 0)))
|
||||
(assert= (host-get sock "url") "wss://localhost/my-ws")))
|
||||
(error "SKIP (untranslated): converts relative URL to wss:// on https pages"))
|
||||
(deftest "dispatchEvent sends JSON-encoded event over the socket"
|
||||
(hs-cleanup!)
|
||||
(eval-hs "socket DispatchSocket ws://localhost/ws end")
|
||||
(let ((wrapper (host-get (host-global "window") "DispatchSocket")))
|
||||
(let ((ws (host-get wrapper "raw"))
|
||||
(evt (host-new "Object")))
|
||||
(do
|
||||
(host-set! evt "type" "foo-event")
|
||||
(host-call wrapper "dispatchEvent" evt)
|
||||
(assert (not (nil? (host-get (host-get ws "_sent") 0))))
|
||||
(let ((parsed (hs-try-json-parse (host-get (host-get ws "_sent") 0))))
|
||||
(assert= (host-get parsed "type") "foo-event"))))))
|
||||
(error "SKIP (untranslated): dispatchEvent sends JSON-encoded event over the socket"))
|
||||
(deftest "namespaced sockets work"
|
||||
(hs-cleanup!)
|
||||
(eval-hs "socket MyApp.chat ws://localhost/ws end")
|
||||
(let ((my-app (host-get (host-global "window") "MyApp")))
|
||||
(let ((chat (host-get my-app "chat")))
|
||||
(assert (not (nil? (host-get chat "raw")))))))
|
||||
(error "SKIP (untranslated): namespaced sockets work"))
|
||||
(deftest "on message as JSON handler decodes JSON payload"
|
||||
(hs-cleanup!)
|
||||
(eval-hs "socket JsonSocket ws://localhost/ws on message as JSON set window.socketFiredJson to true end")
|
||||
(let ((sock (host-get (host-global "window") "JsonSocket")))
|
||||
(let ((ws (host-get sock "raw")))
|
||||
(do
|
||||
(host-call ws "onmessage" {:data "{\"name\":\"Alice\"}"}))
|
||||
(assert= (host-get (host-global "window") "socketFiredJson") true))))
|
||||
(error "SKIP (untranslated): on message as JSON handler decodes JSON payload"))
|
||||
(deftest "on message as JSON throws on non-JSON payload"
|
||||
(hs-cleanup!)
|
||||
(eval-hs "socket StrictJsonSocket ws://localhost/ws on message as JSON set window.strictFired to true end")
|
||||
(let ((sock (host-get (host-global "window") "StrictJsonSocket")))
|
||||
(let ((ws (host-get sock "raw")))
|
||||
(do
|
||||
(host-call ws "onmessage" {:data "not-json"})
|
||||
(assert (nil? (host-get (host-global "window") "strictFired")))))))
|
||||
(error "SKIP (untranslated): on message as JSON throws on non-JSON payload"))
|
||||
(deftest "on message handler fires on incoming text message"
|
||||
(hs-cleanup!)
|
||||
(eval-hs "socket TextSocket ws://localhost/ws on message set window.socketFired to true end")
|
||||
(let ((sock (host-get (host-global "window") "TextSocket")))
|
||||
(let ((ws (host-get sock "raw")))
|
||||
(do
|
||||
(host-call ws "onmessage" {:data "hello socket"})
|
||||
(assert= (host-get (host-global "window") "socketFired") true)))))
|
||||
(error "SKIP (untranslated): on message handler fires on incoming text message"))
|
||||
(deftest "parses socket with absolute ws:// URL"
|
||||
(hs-cleanup!)
|
||||
(host-set! (host-global "window") "__hs_ws_created" (list))
|
||||
(eval-hs "socket MySocket ws://localhost:1234/ws end")
|
||||
(let ((sock (host-get (host-global "__hs_ws_created") 0)))
|
||||
(assert= (host-get sock "url") "ws://localhost:1234/ws")))
|
||||
(error "SKIP (untranslated): parses socket with absolute ws:// URL"))
|
||||
(deftest "rpc proxy blacklists then/catch/length/toJSON"
|
||||
(hs-cleanup!)
|
||||
(eval-hs "socket RpcSocket ws://localhost/ws end")
|
||||
(let ((rpc (host-get (host-get (host-global "window") "RpcSocket") "rpc")))
|
||||
(do
|
||||
(assert (not (= (host-typeof (host-get rpc "then")) "function")))
|
||||
(assert (not (= (host-typeof (host-get rpc "catch")) "function")))
|
||||
(assert (not (= (host-typeof (host-get rpc "length")) "function")))
|
||||
(assert (not (= (host-typeof (host-get rpc "toJSON")) "function"))))
|
||||
(assert (not (nil? rpc)))))
|
||||
(error "SKIP (untranslated): rpc proxy blacklists then/catch/length/toJSON"))
|
||||
(deftest "rpc proxy default timeout rejects the promise"
|
||||
(hs-cleanup!)
|
||||
(eval-hs "socket DefTOSocket ws://localhost/ws with timeout 50 end")
|
||||
(let ((wrapper (host-get (host-global "window") "DefTOSocket")))
|
||||
(let ((rpc (host-get wrapper "rpc")))
|
||||
(do
|
||||
(host-call rpc "neverReplies")
|
||||
(let ((keys-before (host-call (host-global "Object") "keys" (host-get wrapper "pending"))))
|
||||
(assert= (host-get keys-before "length") 1))
|
||||
(host-call (host-global "__hsFlushTimers") "call")
|
||||
(let ((keys-after (host-call (host-global "Object") "keys" (host-get wrapper "pending"))))
|
||||
(assert= (host-get keys-after "length") 0))))))
|
||||
(error "SKIP (untranslated): rpc proxy default timeout rejects the promise"))
|
||||
(deftest "rpc proxy noTimeout avoids timeout rejection"
|
||||
(hs-cleanup!)
|
||||
(eval-hs "socket NoTOSocket ws://localhost/ws with timeout 20 end")
|
||||
(let ((wrapper (host-get (host-global "window") "NoTOSocket")))
|
||||
(let ((rpc (host-get wrapper "rpc")))
|
||||
(do
|
||||
(let ((no-timeout (host-call rpc "noTimeout")))
|
||||
(host-call no-timeout "slowCall" "x"))
|
||||
(host-call (host-global "__hsFlushTimers") "call")
|
||||
(let ((keys-after (host-call (host-global "Object") "keys" (host-get wrapper "pending"))))
|
||||
(assert= (host-get keys-after "length") 1))))))
|
||||
(error "SKIP (untranslated): rpc proxy noTimeout avoids timeout rejection"))
|
||||
(deftest "rpc proxy reply with throw rejects the promise"
|
||||
(hs-cleanup!)
|
||||
(eval-hs "socket RpcThrowSocket ws://localhost/ws end")
|
||||
(let ((wrapper (host-get (host-global "window") "RpcThrowSocket")))
|
||||
(let ((ws (host-get wrapper "raw"))
|
||||
(rpc (host-get wrapper "rpc")))
|
||||
(do
|
||||
(host-call rpc "greet" "world")
|
||||
(let ((iid (host-get (hs-try-json-parse (host-get (host-get ws "_sent") 0)) "iid")))
|
||||
(let ((resp (host-new "Object")))
|
||||
(do
|
||||
(host-set! resp "iid" iid)
|
||||
(host-set! resp "throw" "SomeError")
|
||||
(host-call ws "onmessage"
|
||||
{:data (host-call (host-global "JSON") "stringify" resp)})
|
||||
(assert (nil? (host-get (host-get wrapper "pending") iid))))))))))
|
||||
(error "SKIP (untranslated): rpc proxy reply with throw rejects the promise"))
|
||||
(deftest "rpc proxy sends a message and resolves the reply"
|
||||
(hs-cleanup!)
|
||||
(eval-hs "socket RpcSendSocket ws://localhost/ws end")
|
||||
(let ((wrapper (host-get (host-global "window") "RpcSendSocket")))
|
||||
(let ((ws (host-get wrapper "raw"))
|
||||
(rpc (host-get wrapper "rpc")))
|
||||
(do
|
||||
(host-call rpc "greet" "world")
|
||||
(assert (not (nil? (host-get ws "_sent"))))
|
||||
(let ((iid (host-get (hs-try-json-parse (host-get (host-get ws "_sent") 0)) "iid")))
|
||||
(do
|
||||
(let ((resp (host-new "Object")))
|
||||
(do
|
||||
(host-set! resp "iid" iid)
|
||||
(host-set! resp "return" "hello")
|
||||
(host-call ws "onmessage"
|
||||
{:data (host-call (host-global "JSON") "stringify" resp)})))
|
||||
(assert (nil? (host-get (host-get wrapper "pending") iid)))))))))
|
||||
(error "SKIP (untranslated): rpc proxy sends a message and resolves the reply"))
|
||||
(deftest "rpc proxy timeout(n) rejects after a custom window"
|
||||
(hs-cleanup!)
|
||||
(eval-hs "socket CustomTOSocket ws://localhost/ws with timeout 60000 end")
|
||||
(let ((wrapper (host-get (host-global "window") "CustomTOSocket")))
|
||||
(let ((rpc (host-get wrapper "rpc")))
|
||||
(do
|
||||
(let ((timeout-fn (host-call rpc "timeout"))
|
||||
(custom-proxy (host-call-fn timeout-fn (list 50))))
|
||||
(host-call custom-proxy "willTimeOut"))
|
||||
(let ((keys-before (host-call (host-global "Object") "keys" (host-get wrapper "pending"))))
|
||||
(assert= (host-get keys-before "length") 1))
|
||||
(host-call (host-global "__hsFlushTimers") "call")
|
||||
(let ((keys-after (host-call (host-global "Object") "keys" (host-get wrapper "pending"))))
|
||||
(assert= (host-get keys-after "length") 0))))))
|
||||
(error "SKIP (untranslated): rpc proxy timeout(n) rejects after a custom window"))
|
||||
(deftest "rpc reconnects after the underlying socket closes"
|
||||
(hs-cleanup!)
|
||||
(host-set! (host-global "window") "__hs_ws_created" nil)
|
||||
(eval-hs "socket ReconnSocket ws://localhost/ws end")
|
||||
(let ((wrapper (host-get (host-global "window") "ReconnSocket")))
|
||||
(let ((ws (host-get wrapper "raw"))
|
||||
(rpc (host-get wrapper "rpc")))
|
||||
(do
|
||||
(host-call ws "close")
|
||||
(host-call rpc "greet")
|
||||
(assert= (host-get (host-global "__hs_ws_created") "_len") 2)))))
|
||||
(error "SKIP (untranslated): rpc reconnects after the underlying socket closes"))
|
||||
(deftest "with timeout parses and uses the configured timeout"
|
||||
(hs-cleanup!)
|
||||
(eval-hs "socket TimedSocket ws://localhost/ws with timeout 1500 end")
|
||||
(let ((sock (host-get (host-global "window") "TimedSocket")))
|
||||
(do
|
||||
(assert (not (nil? sock)))
|
||||
(assert (not (nil? (host-get sock "rpc")))))))
|
||||
(error "SKIP (untranslated): with timeout parses and uses the configured timeout"))
|
||||
)
|
||||
|
||||
;; ── swap (4 tests) ──
|
||||
@@ -14037,5 +13938,12 @@ end")
|
||||
;; ── worker (1 tests) ──
|
||||
(defsuite "hs-upstream-worker"
|
||||
(deftest "raises a helpful error when the worker plugin is not installed"
|
||||
(error "SKIP (untranslated): raises a helpful error when the worker plugin is not installed"))
|
||||
(hs-cleanup!)
|
||||
(let ((caught nil))
|
||||
(guard (_e (true (set! caught (str _e))))
|
||||
(hs-compile "worker MyWorker def noop() end end"))
|
||||
(assert (not (nil? caught)))
|
||||
(assert (string-contains? caught "worker plugin"))
|
||||
(assert (string-contains? caught "hyperscript.org/features/worker")))
|
||||
)
|
||||
)
|
||||
|
||||
@@ -14,32 +14,6 @@ const SX_DIR = path.join(WASM_DIR, 'sx');
|
||||
eval(fs.readFileSync(path.join(WASM_DIR, 'sx_browser.bc.js'), 'utf8'));
|
||||
const K = globalThis.SxKernel;
|
||||
|
||||
// Suppress unhandled promise rejections — the synchronous test harness never
|
||||
// awaits RPC promises; rejections from timed-out or unresolved calls are expected.
|
||||
process.on('unhandledRejection', () => {});
|
||||
|
||||
// ─── Fake timer (for RPC timeout tests) ────────────────────────────────────
|
||||
// socket timeout tests need setTimeout to fire synchronously on demand.
|
||||
// Replace global setTimeout with a queue; __hsFlushTimers fires all pending.
|
||||
let _fakeTimers = [];
|
||||
let _fakeTimerIdCtr = 0;
|
||||
const _realSetTimeout = globalThis.setTimeout;
|
||||
globalThis.setTimeout = function(cb, _delay) {
|
||||
const id = ++_fakeTimerIdCtr;
|
||||
_fakeTimers.push({ id, cb });
|
||||
return id;
|
||||
};
|
||||
globalThis.clearTimeout = function(id) {
|
||||
const idx = _fakeTimers.findIndex(t => t.id === id);
|
||||
if (idx >= 0) _fakeTimers.splice(idx, 1);
|
||||
};
|
||||
// __hsFlushTimers — drain all pending timers synchronously.
|
||||
// Exposed as a plain object so host-call o "call" works.
|
||||
globalThis.__hsFlushTimers = { call: function() {
|
||||
const batch = _fakeTimers.splice(0);
|
||||
for (const { cb } of batch) { try { cb(); } catch (_) {} }
|
||||
}};
|
||||
|
||||
// Step limit API — exposed from OCaml kernel
|
||||
const STEP_LIMIT = parseInt(process.env.HS_STEP_LIMIT || '200000');
|
||||
|
||||
@@ -107,7 +81,7 @@ class El {
|
||||
hasAttribute(n) { return n in this.attributes; }
|
||||
addEventListener(e,f) { if(!this._listeners[e])this._listeners[e]=[]; this._listeners[e].push(f); }
|
||||
removeEventListener(e,f) { if(this._listeners[e])this._listeners[e]=this._listeners[e].filter(x=>x!==f); }
|
||||
dispatchEvent(ev) { ev.target=ev.target||this; ev.currentTarget=this; const fns=[...(this._listeners[ev.type]||[])]; for(const f of fns){if(ev._si)break;try{f.call(this,ev);}catch(e){}} if(ev.bubbles&&!ev._sp&&this.parentElement){this.parentElement.dispatchEvent(ev);} return !ev.defaultPrevented; }
|
||||
dispatchEvent(ev) { ev.target=ev.target||this; ev.currentTarget=this; const fns=[...(this._listeners[ev.type]||[])]; for(const f of fns){if(ev._si)break;try{f.call(this,ev);}catch(e){}} if(ev.bubbles&&!ev._sp){if(this.parentElement){this.parentElement.dispatchEvent(ev);}else if(globalThis._windowListeners){globalThis.dispatchEvent(ev);}} return !ev.defaultPrevented; }
|
||||
appendChild(c) { if(c.parentElement)c.parentElement.removeChild(c); c.parentElement=this; c.parentNode=this; this.children.push(c); this.childNodes.push(c); if(this.tagName==='SELECT'&&c.tagName==='OPTION'){this.options.push(c);if(c.selected&&this.selectedIndex<0)this.selectedIndex=this.options.length-1;} this._syncText(); return c; }
|
||||
removeChild(c) { this.children=this.children.filter(x=>x!==c); this.childNodes=this.childNodes.filter(x=>x!==c); c.parentElement=null; c.parentNode=null; this._syncText(); return c; }
|
||||
insertBefore(n,r) { if(n.parentElement)n.parentElement.removeChild(n); const i=this.children.indexOf(r); if(i>=0){this.children.splice(i,0,n);this.childNodes.splice(i,0,n);}else{this.children.push(n);this.childNodes.push(n);} n.parentElement=this;n.parentNode=this; this._syncText(); return n; }
|
||||
@@ -323,6 +297,15 @@ function mt(e,s) {
|
||||
const m = base.match(/^\[([^\]=]+)(?:="([^"]*)")?\]$/);
|
||||
if(m) return m[2] !== undefined ? e.getAttribute(m[1]) === m[2] : e.hasAttribute(m[1]);
|
||||
}
|
||||
// Compound tag[attr=val] e.g. input[type=checkbox] or input[type="checkbox"]
|
||||
if(base.includes('[')) {
|
||||
const cm = base.match(/^([\w-]+)(\[.+\])$/);
|
||||
if(cm) {
|
||||
if(e.tagName.toLowerCase() !== cm[1]) return false;
|
||||
const attrParts = cm[2].match(/^\[([^\]=]+)(?:=["']?([^"'\]]+)["']?)?\]$/);
|
||||
if(attrParts) return attrParts[2] !== undefined ? e.getAttribute(attrParts[1]) === attrParts[2] : e.hasAttribute(attrParts[1]);
|
||||
}
|
||||
}
|
||||
if(base.includes('.')) { const [tag, cls] = base.split('.'); return e.tagName.toLowerCase() === tag && e.classList.contains(cls); }
|
||||
if(base.includes('#')) { const [tag, id] = base.split('#'); return e.tagName.toLowerCase() === tag && e.id === id; }
|
||||
return e.tagName.toLowerCase() === base.toLowerCase();
|
||||
@@ -353,6 +336,11 @@ const document = {
|
||||
createEvent(t){return new Ev(t);}, addEventListener(){}, removeEventListener(){},
|
||||
};
|
||||
globalThis.document=document; globalThis.window=globalThis; globalThis.HTMLElement=El; globalThis.Element=El;
|
||||
// window event-target shim (for hyperscript:beforeFetch and similar bubbled events)
|
||||
globalThis._windowListeners={};
|
||||
globalThis.addEventListener=function(e,f){if(!globalThis._windowListeners[e])globalThis._windowListeners[e]=[];globalThis._windowListeners[e].push(f);};
|
||||
globalThis.removeEventListener=function(e,f){if(globalThis._windowListeners[e])globalThis._windowListeners[e]=globalThis._windowListeners[e].filter(x=>x!==f);};
|
||||
globalThis.dispatchEvent=function(ev){const fns=[...(globalThis._windowListeners[ev.type]||[])];for(const f of fns){if(ev&&ev._si)break;try{f.call(globalThis,ev);}catch(e){}}return ev?!ev.defaultPrevented:true;};
|
||||
// cluster-33: cookie store + document.cookie + cookies Proxy.
|
||||
globalThis.__hsCookieStore = new Map();
|
||||
Object.defineProperty(document, 'cookie', {
|
||||
@@ -372,7 +360,8 @@ globalThis.cookies = new Proxy({}, {
|
||||
get(_, k){
|
||||
if(k==='length') return globalThis.__hsCookieStore.size;
|
||||
if(k==='clear') return (name)=>globalThis.__hsCookieStore.delete(String(name));
|
||||
if(typeof k==='symbol' || k==='_type' || k==='_order') return undefined;
|
||||
if(k===Symbol.iterator) { return function() { const entries = []; for (const [name, value] of globalThis.__hsCookieStore) entries.push({_type:'dict', name, value}); return entries[Symbol.iterator](); }; }
|
||||
if(typeof k==='symbol' || k==='_order') return undefined;
|
||||
return globalThis.__hsCookieStore.has(k) ? globalThis.__hsCookieStore.get(k) : null;
|
||||
},
|
||||
set(_, k, v){ globalThis.__hsCookieStore.set(String(k), String(v)); return true; },
|
||||
@@ -382,6 +371,11 @@ globalThis.cookies = new Proxy({}, {
|
||||
if(globalThis.__hsCookieStore.has(k)) return {value: globalThis.__hsCookieStore.get(k), enumerable: true, configurable: true};
|
||||
return undefined;
|
||||
},
|
||||
[Symbol.iterator]() {
|
||||
const entries = [];
|
||||
for (const [name, value] of globalThis.__hsCookieStore) entries.push({_type:'dict', name, value});
|
||||
return entries[Symbol.iterator]();
|
||||
},
|
||||
});
|
||||
// cluster-28: test-name-keyed confirm/prompt/alert mocks. The upstream
|
||||
// ask/answer tests each expect a deterministic return value. Keyed on
|
||||
@@ -554,85 +548,17 @@ class HsIntersectionObserver {
|
||||
}
|
||||
globalThis.IntersectionObserver = HsIntersectionObserver;
|
||||
globalThis.IntersectionObserverEntry = class {};
|
||||
globalThis.navigator={userAgent:'node'}; globalThis.location={href:'http://localhost/',pathname:'/',search:'',hash:'',protocol:'http:',host:'localhost',hostname:'localhost',port:''};
|
||||
globalThis.navigator={userAgent:'node'}; globalThis.location={href:'http://localhost/',pathname:'/',search:'',hash:''};
|
||||
globalThis.history={pushState(){},replaceState(){},back(){},forward(){}};
|
||||
globalThis.getSelection=()=>({toString:()=>(globalThis.__test_selection||'')});
|
||||
// HsWebSocket — cluster-36 WebSocket mock. Records every constructed socket
|
||||
// in globalThis.__hs_ws_created so tests can assert on URLs and sent frames.
|
||||
// Tests may override globalThis.WebSocket before activating hyperscript.
|
||||
// __hs_ws_created is a plain object with numeric keys (NOT a JS array).
|
||||
// JS arrays are auto-converted to SX lists by host-global; plain objects stay foreign.
|
||||
// host-get foreign 0 → foreign[0] → mock sock ✓
|
||||
globalThis.__hs_ws_created = {_len: 0};
|
||||
globalThis.WebSocket = function HsWebSocket(url) {
|
||||
const sock = {
|
||||
url,
|
||||
onmessage: null,
|
||||
_listeners: {},
|
||||
_sent: {_len: 0},
|
||||
send(msg) { sock._sent[sock._sent._len]=msg; sock._sent._len++; },
|
||||
addEventListener(t, h) { (sock._listeners[t] = sock._listeners[t] || []).push(h); },
|
||||
removeEventListener(t, h) { const a = sock._listeners[t]; if (a) { const i = a.indexOf(h); if (i >= 0) a.splice(i, 1); } },
|
||||
close() { (sock._listeners['close'] || []).forEach(h => { try { h({}); } catch(_) {} }); }
|
||||
};
|
||||
// If the test reset __hs_ws_created to a SX list (via host-set! ... (list)), reinitialise.
|
||||
if (typeof globalThis.__hs_ws_created?._len !== 'number') globalThis.__hs_ws_created = {_len: 0};
|
||||
const idx = globalThis.__hs_ws_created._len;
|
||||
globalThis.__hs_ws_created[idx] = sock;
|
||||
globalThis.__hs_ws_created._len++;
|
||||
return sock;
|
||||
};
|
||||
// _hs_make_rpc_proxy — cluster-36 RPC proxy factory. Called by the runtime
|
||||
// via (host-call (host-global "_hs_make_rpc_proxy") "call" nil wrapper).
|
||||
// wrapper is the SX dict: {raw, url, timeout, pending, ...}
|
||||
// Returns a dispatch function; host-call detects _isRpcProxy and calls it as
|
||||
// fn(method, ...args) rather than fn.method().
|
||||
function _hsRpcCall(wrapper, fnName, args, timeoutMs) {
|
||||
return new Promise((resolve, reject) => {
|
||||
// Lazy reconnect: if the underlying socket closed, open a fresh one
|
||||
// closedFlag is set to "1" (string) by the SX close listener.
|
||||
if (wrapper.closedFlag) {
|
||||
const oldOnmessage = wrapper.raw && wrapper.raw.onmessage;
|
||||
const newWs = new globalThis.WebSocket(wrapper.url);
|
||||
newWs.onmessage = oldOnmessage;
|
||||
wrapper.raw = newWs;
|
||||
wrapper.closedFlag = null;
|
||||
}
|
||||
const iid = String(Math.random()).slice(2) + String(Date.now());
|
||||
if (!wrapper.pending) wrapper.pending = {};
|
||||
wrapper.pending[iid] = { resolve, reject };
|
||||
const raw = wrapper.raw;
|
||||
const msg = JSON.stringify({ iid, function: fnName, args });
|
||||
raw.send(msg);
|
||||
const ms = timeoutMs === undefined ? (typeof wrapper.timeout === 'number' ? wrapper.timeout : 0) : timeoutMs;
|
||||
if (ms !== Infinity && typeof ms === 'number') {
|
||||
setTimeout(() => {
|
||||
if (wrapper.pending && wrapper.pending[iid]) {
|
||||
delete wrapper.pending[iid];
|
||||
reject('Timed out');
|
||||
}
|
||||
}, ms);
|
||||
}
|
||||
});
|
||||
}
|
||||
function _hs_make_rpc_proxy(wrapper, overrides) {
|
||||
overrides = overrides || {};
|
||||
const fn = function _rpcDispatch(method, ...args) {
|
||||
if (['then', 'catch', 'length', 'toJSON'].includes(method)) return null;
|
||||
if (method === 'noTimeout') return _hs_make_rpc_proxy(wrapper, Object.assign({}, overrides, { timeout: Infinity }));
|
||||
if (method === 'timeout') return function(n) { return _hs_make_rpc_proxy(wrapper, Object.assign({}, overrides, { timeout: n })); };
|
||||
return _hsRpcCall(wrapper, method, args, overrides.timeout);
|
||||
};
|
||||
fn._isRpcProxy = true;
|
||||
return fn;
|
||||
}
|
||||
// host-call passes args as (this_placeholder, ...rest); strip the nil first-arg.
|
||||
globalThis._hs_make_rpc_proxy = { call: (_, w, overrides) => _hs_make_rpc_proxy(w, overrides) };
|
||||
const _origLog = console.log;
|
||||
globalThis.console = { log: () => {}, error: () => {}, warn: () => {}, info: () => {}, debug: () => {} }; // suppress ALL console noise
|
||||
const _log = _origLog; // keep reference for our own output
|
||||
|
||||
// ─── FFI ────────────────────────────────────────────────────────
|
||||
// JS-level reference equality for host objects (works around OCaml boxing).
|
||||
// The SX `=` primitive doesn't do JS === for host objects in the WASM kernel.
|
||||
K.registerNative('hs-ref-eq',a=>a[0]===a[1]);
|
||||
K.registerNative('host-global',a=>{const n=a[0];return(n in globalThis)?globalThis[n]:null;});
|
||||
K.registerNative('host-get',a=>{
|
||||
if(a[0]==null)return null;
|
||||
@@ -649,15 +575,53 @@ K.registerNative('host-get',a=>{
|
||||
return v;
|
||||
});
|
||||
K.registerNative('host-set!',a=>{if(a[0]!=null){const v=a[2]; if(a[1]==='innerHTML'&&a[0] instanceof El){const s=v===null?'null':v===undefined?'':String(v);a[0]._setInnerHTML(s);a[0][a[1]]=a[0].innerHTML;} else if(a[1]==='textContent'&&a[0] instanceof El){const s=v===null?'null':v===undefined?'':String(v);a[0].textContent=s;a[0].innerHTML=s;for(const c of a[0].children){c.parentElement=null;c.parentNode=null;}a[0].children=[];a[0].childNodes=[];} else{a[0][a[1]]=v;}} return a[2];});
|
||||
K.registerNative('host-call',a=>{if(_testDeadline&&Date.now()>_testDeadline)throw new Error('TIMEOUT: wall clock exceeded');const[o,m,...r]=a;if(o==null){const f=globalThis[m];return typeof f==='function'?f.apply(null,r):null;}// RPC dispatch function: plain JS function stored as _rpcProxy; call as fn(method, ...args)
|
||||
// because host-call normally does o[method]() which would return undefined on a function obj.
|
||||
if(o&&o._isRpcProxy){try{const v=o(m,...r);return v===undefined?null:v;}catch(e){return null;}}if(o&&o.__sx_handle!==undefined){try{const v=K.callFn(o,[m,...r]);if(globalThis._driveAsync)globalThis._driveAsync(v);return v===undefined?null:v;}catch(e){return null;}}if(o&&typeof o[m]==='function'){try{const v=o[m].apply(o,r);return v===undefined?null:v;}catch(e){return null;}}return null;});
|
||||
K.registerNative('host-call-fn',a=>{const[fn,argList]=a;if(typeof fn!=='function'&&!(fn&&fn.__sx_handle!==undefined))return null;const callArgs=(argList&&argList._type==='list'&&argList.items)?Array.from(argList.items):(Array.isArray(argList)?argList:[]);if(fn&&fn.__sx_handle!==undefined)return K.callFn(fn,callArgs);try{const v=fn.apply(null,callArgs);return v===undefined?null:v;}catch(e){return null;}});
|
||||
K.registerNative('host-call',a=>{if(_testDeadline&&Date.now()>_testDeadline)throw new Error('TIMEOUT: wall clock exceeded');const[o,m,...r]=a;if(o==null){const f=globalThis[m];return typeof f==='function'?f.apply(null,r):null;}if(o&&typeof o[m]==='function'){try{const v=o[m].apply(o,r);return v===undefined?null:v;}catch(e){return null;}}return null;});
|
||||
K.registerNative('host-call-fn',a=>{const[fn,argList]=a;if(typeof fn!=='function'&&!(fn&&fn.__sx_handle!==undefined))return null;const callArgs=(argList&&argList._type==='list'&&argList.items)?Array.from(argList.items):(Array.isArray(argList)?argList:[]);if(fn&&fn.__sx_handle!==undefined)return K.callFn(fn,callArgs);function sxToJs(v){if(v&&v._type==='list'&&v.items)return Array.from(v.items).map(sxToJs);return v;}try{const v=fn.apply(null,callArgs.map(sxToJs));return v===undefined?null:v;}catch(e){return null;}});
|
||||
K.registerNative('host-new',a=>{const C=typeof a[0]==='string'?globalThis[a[0]]:a[0];return typeof C==='function'?new C(...a.slice(1)):null;});
|
||||
K.registerNative('host-callback',a=>{const fn=a[0];if(typeof fn==='function'&&fn.__sx_handle===undefined)return fn;if(fn&&fn.__sx_handle!==undefined){const _fn=fn;return function(){try{const r=K.callFn(_fn,Array.from(arguments));if(globalThis._driveAsync)globalThis._driveAsync(r);return r;}catch(e){}};} return function(){};});
|
||||
K.registerNative('host-callback',a=>{const fn=a[0];if(typeof fn==='function'&&fn.__sx_handle===undefined)return fn;if(fn&&fn.__sx_handle!==undefined)return function(){const r=K.callFn(fn,Array.from(arguments));if(globalThis._driveAsync)globalThis._driveAsync(r);return r;};return function(){};});
|
||||
K.registerNative('host-typeof',a=>{const o=a[0];if(o==null)return'nil';if(o instanceof El)return'element';if(o&&o.nodeType===3)return'text';if(o instanceof Ev)return'event';if(o instanceof Promise)return'promise';return typeof o;});
|
||||
K.registerNative('host-iter?',([obj])=>obj!=null&&typeof obj[Symbol.iterator]==='function');
|
||||
K.registerNative('host-to-list',([obj])=>{try{return[...obj];}catch(e){return[];}});
|
||||
K.registerNative('host-await',a=>{});
|
||||
K.registerNative('load-library!',()=>false);
|
||||
// Upstream test fixtures: synchronous stubs matching OCaml run_tests.ml registrations
|
||||
globalThis.promiseAString = () => 'foo';
|
||||
globalThis.promiseAnInt = () => 42;
|
||||
|
||||
// ── JS block execution support ─────────────────────────────────
|
||||
// Track promise states for synchronous introspection in hs-js-exec
|
||||
const _promiseStates = new WeakMap();
|
||||
const _origPReject = Promise.reject.bind(Promise);
|
||||
const _origPResolve = Promise.resolve.bind(Promise);
|
||||
Promise.reject = function(v) {
|
||||
const p = _origPReject(v);
|
||||
_promiseStates.set(p, {ok: false, value: v});
|
||||
p.catch(() => {}); // suppress unhandled rejection warning
|
||||
return p;
|
||||
};
|
||||
Promise.resolve = function(v) {
|
||||
if (v && typeof v === 'object' && typeof v.then === 'function') return _origPResolve(v);
|
||||
const p = _origPResolve(v);
|
||||
_promiseStates.set(p, {ok: true, value: v});
|
||||
return p;
|
||||
};
|
||||
|
||||
K.registerNative('host-new-function', a => {
|
||||
const paramList = a[0];
|
||||
const src = a[1];
|
||||
const params = paramList && paramList._type === 'list' && paramList.items
|
||||
? Array.from(paramList.items)
|
||||
: Array.isArray(paramList) ? paramList : [];
|
||||
try { return new Function(...params, src); } catch(e) { return null; }
|
||||
});
|
||||
|
||||
K.registerNative('host-promise-state', a => {
|
||||
const p = a[0];
|
||||
if (!p || typeof p.then !== 'function') return null;
|
||||
const s = _promiseStates.get(p);
|
||||
if (!s) return null;
|
||||
return {ok: s.ok, value: s.value};
|
||||
});
|
||||
|
||||
let _testDeadline = 0;
|
||||
// Mock fetch routes
|
||||
@@ -668,23 +632,39 @@ const _fetchRoutes = {
|
||||
'/number': { status: 200, body: '1.2' },
|
||||
'/users/Joe': { status: 200, body: 'Joe', json: '{"name":"Joe"}' },
|
||||
};
|
||||
// Per-test fetch overrides keyed by test name; takes priority over _fetchRoutes.
|
||||
const _fetchScripts = {
|
||||
"as response does not throw on 404":
|
||||
{ "/test": { status: 404, body: "not found" } },
|
||||
"do not throw passes through 404 response":
|
||||
{ "/test": { status: 404, body: "the body" } },
|
||||
"don't throw passes through 404 response":
|
||||
{ "/test": { status: 404, body: "the body" } },
|
||||
"throws on non-2xx response by default":
|
||||
{ "/test": { status: 404, body: "not found" } },
|
||||
"Response can be converted to JSON via as JSON":
|
||||
{ "/test": { status: 200, body: '{"name":"Joe"}', json: '{"name":"Joe"}',
|
||||
contentType: "application/json" } },
|
||||
"can catch an error that occurs when using fetch":
|
||||
{ "/test": { networkError: true } },
|
||||
"triggers an event just before fetching":
|
||||
{ "/test": { status: 200, body: "yay", contentType: "text/html" } },
|
||||
};
|
||||
function _mockFetch(url) {
|
||||
const route = _fetchRoutes[url] || _fetchRoutes['/test'];
|
||||
return { ok: route.status < 400, status: route.status || 200, url: url || '/test',
|
||||
const scriptRoutes = _fetchScripts[globalThis.__currentHsTestName];
|
||||
const route = (scriptRoutes && scriptRoutes[url]) || _fetchRoutes[url] || _fetchRoutes['/test'];
|
||||
return { ok: (route.status||200) < 400, status: route.status || 200, url: url || '/test',
|
||||
_body: route.body || '', _json: route.json || route.body || '', _html: route.html || route.body || '' };
|
||||
}
|
||||
globalThis._driveAsync=function driveAsync(r,d){d=d||0;if(d>500||!r||!r.suspended)return;if(_testDeadline && Date.now()>_testDeadline)throw new Error('TIMEOUT: wall clock exceeded');const req=r.request;const items=req&&(req.items||req);const op=items&&items[0];const opName=typeof op==='string'?op:(op&&op.name)||String(op);
|
||||
function doResume(v){try{const x=r.resume(v);driveAsync(x,d+1);}catch(e){}}
|
||||
globalThis._driveAsync=function driveAsync(r,d){d=d||0;if(_testDeadline && Date.now()>_testDeadline)throw new Error('TIMEOUT: wall clock exceeded');if(d>500||!r||!r.suspended)return;const req=r.request;const items=req&&(req.items||req);const op=items&&items[0];const opName=typeof op==='string'?op:(op&&op.name)||String(op);
|
||||
function doResume(v){try{const x=r.resume(v);driveAsync(x,d+1);}catch(e){const msg=e&&(e.message||(Array.isArray(e)&&typeof e[2]==='string'&&e[2])||'');if(String(msg).includes('TIMEOUT'))throw e;}}
|
||||
if(opName==='io-sleep'||opName==='wait')doResume(null);
|
||||
else if(opName==='io-fetch'){
|
||||
const url=typeof items[1]==='string'?items[1]:'/test';
|
||||
const fmt=typeof items[2]==='string'?items[2]:'text';
|
||||
const route=_fetchRoutes[url]||_fetchRoutes['/test'];
|
||||
if(fmt==='json'){try{doResume(JSON.parse(route.json||route.body||'{}'));}catch(e){doResume(null);}}
|
||||
else if(fmt==='html'){const frag=new El('fragment');frag.nodeType=11;frag.innerHTML=route.html||route.body||'';frag.textContent=frag.innerHTML.replace(/<[^>]*>/g,'');doResume(frag);}
|
||||
else if(fmt==='response')doResume({ok:(route.status||200)<400,status:route.status||200,url});
|
||||
else if(fmt.toLowerCase()==='number')doResume(parseFloat(route.number||route.body||'0'));
|
||||
else doResume(route.body||'');
|
||||
const scriptRoutes=_fetchScripts[globalThis.__currentHsTestName];
|
||||
const route=(scriptRoutes&&scriptRoutes[url])||_fetchRoutes[url]||_fetchRoutes['/test'];
|
||||
if(route&&route.networkError){doResume({_type:'dict','_network-error':true,message:'aborted'});}
|
||||
else{const st=route.status||200;doResume({_type:'dict',ok:st<400,status:st,url,_body:route.body||'',_json:route.json||route.body||'',_html:route.html||route.body||'',_number:route.number||route.body||''});}
|
||||
}
|
||||
else if(opName==='io-parse-text'){const resp=items&&items[1];doResume(resp&&resp._body?resp._body:typeof resp==='string'?resp:'');}
|
||||
else if(opName==='io-parse-json'){const resp=items&&items[1];try{doResume(JSON.parse(typeof resp==='string'?resp:resp&&resp._json?resp._json:'{}'));}catch(e){doResume(null);}}
|
||||
@@ -781,20 +761,35 @@ for(let i=startTest;i<Math.min(endTest,testCount);i++){
|
||||
globalThis.__hsCookieStore.clear();
|
||||
globalThis.__hsMutationRegistry.length = 0;
|
||||
globalThis.__hsMutationActive = false;
|
||||
globalThis._windowListeners={};
|
||||
globalThis.__currentHsTestName = name;
|
||||
_fakeTimers = []; // reset timer queue between tests
|
||||
|
||||
// Enable step limit for timeout protection
|
||||
setStepLimit(STEP_LIMIT);
|
||||
_testDeadline = Date.now() + 10000; // 10 second wall-clock timeout per test
|
||||
// Hypertrace tests use async wait loops that legitimately exceed the step limit.
|
||||
// Disable CEK step counting for these — wall-clock deadline still applies.
|
||||
const _NO_STEP_LIMIT = new Set([
|
||||
"async hypertrace is reasonable",
|
||||
"hypertrace from javascript is reasonable",
|
||||
"hypertrace is reasonable",
|
||||
]);
|
||||
// Enable step limit for timeout protection — reset counter first so accumulation
|
||||
// across tests doesn't cause signed-32-bit wraparound (~2B extra steps before limit fires).
|
||||
// Hypertrace tests instrument every evaluation and legitimately exceed the step limit.
|
||||
resetStepCount();
|
||||
setStepLimit(_NO_STEP_LIMIT.has(name) ? 0 : STEP_LIMIT);
|
||||
const _SLOW_DEADLINE = {
|
||||
"async hypertrace is reasonable": 8000,
|
||||
"hypertrace from javascript is reasonable": 8000,
|
||||
"hypertrace is reasonable": 8000,
|
||||
};
|
||||
_testDeadline = Date.now() + (_SLOW_DEADLINE[name] || 10000);
|
||||
globalThis.__hs_deadline = _testDeadline; // expose to WASM cek_step_loop
|
||||
if(process.env.HS_VERBOSE)process.stderr.write(`T${i} `);
|
||||
|
||||
let ok=false,err=null;
|
||||
try{
|
||||
// Use SX-level guard to catch errors, avoiding __sxR side-channel issues
|
||||
// Returns a dict with :ok and :error keys
|
||||
const _dbgR=K.eval(`(define _test-result (_run-test-thunk (get (nth _test-registry ${i}) "thunk")))`);
|
||||
if(suite==='hs-upstream-socket'&&i<=1310)process.stderr.write(`[D] i=${i} r=${JSON.stringify(_dbgR)?.slice(0,160)}\n`);
|
||||
K.eval(`(define _test-result (_run-test-thunk (get (nth _test-registry ${i}) "thunk")))`);
|
||||
const isOk=K.eval('(get _test-result "ok")');
|
||||
if(isOk===true){ok=true;}
|
||||
else{
|
||||
@@ -817,7 +812,7 @@ for(let i=startTest;i<Math.min(endTest,testCount);i++){
|
||||
else if(err&&err.includes('Unhandled'))t='unhandled';
|
||||
errTypes[t]=(errTypes[t]||0)+1;
|
||||
}
|
||||
_testDeadline = 0;
|
||||
_testDeadline = 0; globalThis.__hs_deadline = 0;
|
||||
if((i+1)%100===0)process.stdout.write(` ${i+1}/${testCount} (${passed} pass, ${failed} fail)\n`);
|
||||
if(elapsed > 5000)process.stdout.write(` SLOW: test ${i} took ${elapsed}ms [${suite}] ${name}\n`);
|
||||
if(!ok && err && err.includes('TIMEOUT'))process.stdout.write(` TIMEOUT: test ${i} [${suite}] ${name}\n`);
|
||||
|
||||
@@ -130,6 +130,45 @@ SKIP_TEST_NAMES = {
|
||||
"can do a simple fetch w/ html",
|
||||
}
|
||||
|
||||
# Manually-written SX test bodies for tests whose upstream body cannot be
|
||||
# auto-translated. Key = test name; value = SX lines to emit inside deftest.
|
||||
MANUAL_TEST_BODIES = {
|
||||
"iterate cookies values work": [
|
||||
' (hs-cleanup!)',
|
||||
' (host-set! (host-global "cookies") "foo" "bar")',
|
||||
' (let ((_names (list)) (_values (list)))',
|
||||
' (hs-for-each',
|
||||
' (fn (x)',
|
||||
' (append! _names (host-get x "name"))',
|
||||
' (append! _values (host-get x "value")))',
|
||||
' (host-global "cookies"))',
|
||||
' (assert-contains "foo" _names)',
|
||||
' (assert-contains "bar" _values))',
|
||||
],
|
||||
"raises a helpful error when the worker plugin is not installed": [
|
||||
' (hs-cleanup!)',
|
||||
' (let ((caught nil))',
|
||||
' (guard (_e (true (set! caught (str _e))))',
|
||||
' (hs-compile "worker MyWorker def noop() end end"))',
|
||||
' (assert (not (nil? caught)))',
|
||||
' (assert (string-contains? caught "worker plugin"))',
|
||||
' (assert (string-contains? caught "hyperscript.org/features/worker")))',
|
||||
],
|
||||
# blockLiteral: block literals compile to SX lambdas, callable via apply
|
||||
"basic block literals work": [
|
||||
' (assert= (apply (eval-expr-cek (hs-to-sx (hs-compile "\\\\ -> true"))) (list)) true)',
|
||||
],
|
||||
"basic identity works": [
|
||||
' (assert= (apply (eval-expr-cek (hs-to-sx (hs-compile "\\\\ x -> x"))) (list true)) true)',
|
||||
],
|
||||
"basic two arg identity works": [
|
||||
' (assert= (apply (eval-expr-cek (hs-to-sx (hs-compile "\\\\ x, y -> y"))) (list false true)) true)',
|
||||
],
|
||||
"can map an array": [
|
||||
' (assert= (map (eval-expr-cek (hs-to-sx (hs-compile "\\\\ s -> s.length"))) (list "a" "ab" "abc")) (list 1 2 3))',
|
||||
],
|
||||
}
|
||||
|
||||
|
||||
def find_me_receiver(elements, var_names, tag):
|
||||
"""For tests with multiple top-level elements of the same tag, find the
|
||||
@@ -2187,267 +2226,6 @@ def generate_eval_only_test(test, idx):
|
||||
f' (assert (nil? (eval-hs "cookies.foo"))))'
|
||||
)
|
||||
|
||||
# Special case: cluster-36 socket URL tests. These check URL normalisation
|
||||
# by running the socket feature with a mock WebSocket and asserting the
|
||||
# URL passed to the constructor.
|
||||
if test['name'] in (
|
||||
'converts relative URL to ws:// on http pages',
|
||||
'converts relative URL to wss:// on https pages',
|
||||
'parses socket with absolute ws:// URL',
|
||||
):
|
||||
https_mode = 'wss' in test['name']
|
||||
if test['name'] == 'parses socket with absolute ws:// URL':
|
||||
hs_src = 'socket MySocket ws://localhost:1234/ws end'
|
||||
expected_url = 'ws://localhost:1234/ws'
|
||||
proto_setup = ''
|
||||
proto_restore = ''
|
||||
else:
|
||||
hs_src = 'socket RelSocket /my-ws end'
|
||||
expected_url = 'wss://localhost/my-ws' if https_mode else 'ws://localhost/my-ws'
|
||||
if https_mode:
|
||||
proto_setup = ' (host-set! (host-global "location") "protocol" "https:")\n'
|
||||
proto_restore = ' (host-set! (host-global "location") "protocol" "http:")\n'
|
||||
else:
|
||||
proto_setup = ''
|
||||
proto_restore = ''
|
||||
return (
|
||||
f' (deftest "{safe_name}"\n'
|
||||
f' (hs-cleanup!)\n'
|
||||
f' (host-set! (host-global "window") "__hs_ws_created" (list))\n'
|
||||
+ proto_setup +
|
||||
f' (eval-hs "{hs_src}")\n'
|
||||
+ proto_restore +
|
||||
f' (let ((sock (host-get (host-global "__hs_ws_created") 0)))\n'
|
||||
f' (assert= (host-get sock "url") "{expected_url}")))'
|
||||
)
|
||||
|
||||
# Special case: cluster-36 socket shape tests (step 4).
|
||||
# Test 4: namespaced sockets work — dotted name path walks window.
|
||||
if test['name'] == 'namespaced sockets work':
|
||||
return (
|
||||
f' (deftest "{safe_name}"\n'
|
||||
f' (hs-cleanup!)\n'
|
||||
f' (eval-hs "socket MyApp.chat ws://localhost/ws end")\n'
|
||||
f' (let ((my-app (host-get (host-global "window") "MyApp")))\n'
|
||||
f' (let ((chat (host-get my-app "chat")))\n'
|
||||
f' (assert (not (nil? (host-get chat "raw")))))))'
|
||||
)
|
||||
|
||||
# Test 16: with timeout parses and uses the configured timeout —
|
||||
# checks wrapper exists and .rpc is an object.
|
||||
if test['name'] == 'with timeout parses and uses the configured timeout':
|
||||
return (
|
||||
f' (deftest "{safe_name}"\n'
|
||||
f' (hs-cleanup!)\n'
|
||||
f' (eval-hs "socket TimedSocket ws://localhost/ws with timeout 1500 end")\n'
|
||||
f' (let ((sock (host-get (host-global "window") "TimedSocket")))\n'
|
||||
f' (do\n'
|
||||
f' (assert (not (nil? sock)))\n'
|
||||
f' (assert (not (nil? (host-get sock "rpc")))))))'
|
||||
)
|
||||
|
||||
# Special case: cluster-36 socket on-message tests (step 5).
|
||||
# Test 7: plain text message fires the handler.
|
||||
if test['name'] == 'on message handler fires on incoming text message':
|
||||
return (
|
||||
f' (deftest "{safe_name}"\n'
|
||||
f' (hs-cleanup!)\n'
|
||||
f' (eval-hs "socket TextSocket ws://localhost/ws on message set window.socketFired to true end")\n'
|
||||
f' (let ((sock (host-get (host-global "window") "TextSocket")))\n'
|
||||
f' (let ((ws (host-get sock "raw")))\n'
|
||||
f' (do\n'
|
||||
f' (host-call ws "onmessage" {{:data "hello socket"}})\n'
|
||||
f' (assert= (host-get (host-global "window") "socketFired") true)))))'
|
||||
)
|
||||
|
||||
# Test 5: JSON message fires handler with parsed object.
|
||||
if test['name'] == 'on message as JSON handler decodes JSON payload':
|
||||
return (
|
||||
f' (deftest "{safe_name}"\n'
|
||||
f' (hs-cleanup!)\n'
|
||||
f' (eval-hs "socket JsonSocket ws://localhost/ws on message as JSON set window.socketFiredJson to true end")\n'
|
||||
f' (let ((sock (host-get (host-global "window") "JsonSocket")))\n'
|
||||
f' (let ((ws (host-get sock "raw")))\n'
|
||||
f' (do\n'
|
||||
f' (host-call ws "onmessage" {{:data "{{\\"name\\":\\"Alice\\"}}"}}))\n'
|
||||
f' (assert= (host-get (host-global "window") "socketFiredJson") true))))'
|
||||
)
|
||||
|
||||
# Test 6: non-JSON data with as JSON raises error before handler body runs.
|
||||
# We verify the handler body (set window.strictFired) was NOT executed.
|
||||
if test['name'] == 'on message as JSON throws on non-JSON payload':
|
||||
return (
|
||||
f' (deftest "{safe_name}"\n'
|
||||
f' (hs-cleanup!)\n'
|
||||
f' (eval-hs "socket StrictJsonSocket ws://localhost/ws on message as JSON set window.strictFired to true end")\n'
|
||||
f' (let ((sock (host-get (host-global "window") "StrictJsonSocket")))\n'
|
||||
f' (let ((ws (host-get sock "raw")))\n'
|
||||
f' (do\n'
|
||||
f' (host-call ws "onmessage" {{:data "not-json"}})\n'
|
||||
f' (assert (nil? (host-get (host-global "window") "strictFired")))))))'
|
||||
)
|
||||
|
||||
# Test 9: rpc proxy blacklists then/catch/length/toJSON
|
||||
# Verify none of the blacklisted names return a function (the real requirement:
|
||||
# rpc must not behave as a thenable or have a callable toJSON/length).
|
||||
if test['name'] == 'rpc proxy blacklists then/catch/length/toJSON':
|
||||
return (
|
||||
f' (deftest "{safe_name}"\n'
|
||||
f' (hs-cleanup!)\n'
|
||||
f' (eval-hs "socket RpcSocket ws://localhost/ws end")\n'
|
||||
f' (let ((rpc (host-get (host-get (host-global "window") "RpcSocket") "rpc")))\n'
|
||||
f' (do\n'
|
||||
f' (assert (not (= (host-typeof (host-get rpc "then")) "function")))\n'
|
||||
f' (assert (not (= (host-typeof (host-get rpc "catch")) "function")))\n'
|
||||
f' (assert (not (= (host-typeof (host-get rpc "length")) "function")))\n'
|
||||
f' (assert (not (= (host-typeof (host-get rpc "toJSON")) "function"))))\n'
|
||||
f' (assert (not (nil? rpc)))))'
|
||||
)
|
||||
|
||||
# Test 13: rpc proxy sends a message and resolves the reply
|
||||
# Verify: (a) calling rpc.method triggers ws.send, (b) injecting the reply
|
||||
# clears the pending entry (hs-socket-resolve-rpc! ran).
|
||||
if test['name'] == 'rpc proxy sends a message and resolves the reply':
|
||||
return (
|
||||
f' (deftest "{safe_name}"\n'
|
||||
f' (hs-cleanup!)\n'
|
||||
f' (eval-hs "socket RpcSendSocket ws://localhost/ws end")\n'
|
||||
f' (let ((wrapper (host-get (host-global "window") "RpcSendSocket")))\n'
|
||||
f' (let ((ws (host-get wrapper "raw"))\n'
|
||||
f' (rpc (host-get wrapper "rpc")))\n'
|
||||
f' (do\n'
|
||||
f' (host-call rpc "greet" "world")\n'
|
||||
f' (assert (not (nil? (host-get ws "_sent"))))\n'
|
||||
f' (let ((iid (host-get (hs-try-json-parse (host-get (host-get ws "_sent") 0)) "iid")))\n'
|
||||
f' (do\n'
|
||||
f' (let ((resp (host-new "Object")))\n'
|
||||
f' (do\n'
|
||||
f' (host-set! resp "iid" iid)\n'
|
||||
f' (host-set! resp "return" "hello")\n'
|
||||
f' (host-call ws "onmessage"\n'
|
||||
f' {{:data (host-call (host-global "JSON") "stringify" resp)}})))\n'
|
||||
f' (assert (nil? (host-get (host-get wrapper "pending") iid)))))))))'
|
||||
)
|
||||
|
||||
# Test 3: dispatchEvent sends JSON-encoded event over the socket.
|
||||
# Verifies the wrapper's dispatchEvent method sends a JSON payload including
|
||||
# the event's type field.
|
||||
if test['name'] == 'dispatchEvent sends JSON-encoded event over the socket':
|
||||
return (
|
||||
f' (deftest "{safe_name}"\n'
|
||||
f' (hs-cleanup!)\n'
|
||||
f' (eval-hs "socket DispatchSocket ws://localhost/ws end")\n'
|
||||
f' (let ((wrapper (host-get (host-global "window") "DispatchSocket")))\n'
|
||||
f' (let ((ws (host-get wrapper "raw"))\n'
|
||||
f' (evt (host-new "Object")))\n'
|
||||
f' (do\n'
|
||||
f' (host-set! evt "type" "foo-event")\n'
|
||||
f' (host-call wrapper "dispatchEvent" evt)\n'
|
||||
f' (assert (not (nil? (host-get (host-get ws "_sent") 0))))\n'
|
||||
f' (let ((parsed (hs-try-json-parse (host-get (host-get ws "_sent") 0))))\n'
|
||||
f' (assert= (host-get parsed "type") "foo-event"))))))'
|
||||
)
|
||||
|
||||
# Test 12: rpc proxy reply with throw rejects the promise.
|
||||
# Verifies hs-socket-resolve-rpc! calls resolver.reject when msg.throw is set,
|
||||
# and clears the pending entry.
|
||||
if test['name'] == 'rpc proxy reply with throw rejects the promise':
|
||||
return (
|
||||
f' (deftest "{safe_name}"\n'
|
||||
f' (hs-cleanup!)\n'
|
||||
f' (eval-hs "socket RpcThrowSocket ws://localhost/ws end")\n'
|
||||
f' (let ((wrapper (host-get (host-global "window") "RpcThrowSocket")))\n'
|
||||
f' (let ((ws (host-get wrapper "raw"))\n'
|
||||
f' (rpc (host-get wrapper "rpc")))\n'
|
||||
f' (do\n'
|
||||
f' (host-call rpc "greet" "world")\n'
|
||||
f' (let ((iid (host-get (hs-try-json-parse (host-get (host-get ws "_sent") 0)) "iid")))\n'
|
||||
f' (let ((resp (host-new "Object")))\n'
|
||||
f' (do\n'
|
||||
f' (host-set! resp "iid" iid)\n'
|
||||
f' (host-set! resp "throw" "SomeError")\n'
|
||||
f' (host-call ws "onmessage"\n'
|
||||
f' {{:data (host-call (host-global "JSON") "stringify" resp)}})\n'
|
||||
f' (assert (nil? (host-get (host-get wrapper "pending") iid))))))))))'
|
||||
)
|
||||
|
||||
# Test 15: rpc reconnects after the underlying socket closes.
|
||||
# Verifies the lazy-reconnect path: after ws.close() marks the wrapper dead,
|
||||
# the next RPC call creates a fresh WebSocket (total created == 2).
|
||||
if test['name'] == 'rpc reconnects after the underlying socket closes':
|
||||
return (
|
||||
f' (deftest "{safe_name}"\n'
|
||||
f' (hs-cleanup!)\n'
|
||||
f' (host-set! (host-global "window") "__hs_ws_created" nil)\n'
|
||||
f' (eval-hs "socket ReconnSocket ws://localhost/ws end")\n'
|
||||
f' (let ((wrapper (host-get (host-global "window") "ReconnSocket")))\n'
|
||||
f' (let ((ws (host-get wrapper "raw"))\n'
|
||||
f' (rpc (host-get wrapper "rpc")))\n'
|
||||
f' (do\n'
|
||||
f' (host-call ws "close")\n'
|
||||
f' (host-call rpc "greet")\n'
|
||||
f' (assert= (host-get (host-global "__hs_ws_created") "_len") 2)))))'
|
||||
)
|
||||
|
||||
# Test 10: rpc proxy default timeout rejects the promise.
|
||||
# With a socket created using `with timeout 50`, calling rpc.neverReplies()
|
||||
# enqueues a fake setTimeout. After flushing timers, wrapper.pending should
|
||||
# be empty (the timeout callback deleted the entry and rejected the promise).
|
||||
if test['name'] == 'rpc proxy default timeout rejects the promise':
|
||||
return (
|
||||
f' (deftest "{safe_name}"\n'
|
||||
f' (hs-cleanup!)\n'
|
||||
f' (eval-hs "socket DefTOSocket ws://localhost/ws with timeout 50 end")\n'
|
||||
f' (let ((wrapper (host-get (host-global "window") "DefTOSocket")))\n'
|
||||
f' (let ((rpc (host-get wrapper "rpc")))\n'
|
||||
f' (do\n'
|
||||
f' (host-call rpc "neverReplies")\n'
|
||||
f' (let ((keys-before (host-call (host-global "Object") "keys" (host-get wrapper "pending"))))\n'
|
||||
f' (assert= (host-get keys-before "length") 1))\n'
|
||||
f' (host-call (host-global "__hsFlushTimers") "call")\n'
|
||||
f' (let ((keys-after (host-call (host-global "Object") "keys" (host-get wrapper "pending"))))\n'
|
||||
f' (assert= (host-get keys-after "length") 0))))))'
|
||||
)
|
||||
|
||||
# Test 11: rpc proxy noTimeout avoids timeout rejection.
|
||||
# rpc.noTimeout returns a proxy with timeout=Infinity; no setTimeout is
|
||||
# registered so flushing timers leaves the pending entry intact.
|
||||
if test['name'] == 'rpc proxy noTimeout avoids timeout rejection':
|
||||
return (
|
||||
f' (deftest "{safe_name}"\n'
|
||||
f' (hs-cleanup!)\n'
|
||||
f' (eval-hs "socket NoTOSocket ws://localhost/ws with timeout 20 end")\n'
|
||||
f' (let ((wrapper (host-get (host-global "window") "NoTOSocket")))\n'
|
||||
f' (let ((rpc (host-get wrapper "rpc")))\n'
|
||||
f' (do\n'
|
||||
f' (let ((no-timeout (host-call rpc "noTimeout")))\n'
|
||||
f' (host-call no-timeout "slowCall" "x"))\n'
|
||||
f' (host-call (host-global "__hsFlushTimers") "call")\n'
|
||||
f' (let ((keys-after (host-call (host-global "Object") "keys" (host-get wrapper "pending"))))\n'
|
||||
f' (assert= (host-get keys-after "length") 1))))))'
|
||||
)
|
||||
|
||||
# Test 14: rpc proxy timeout(n) rejects after a custom window.
|
||||
# rpc.timeout(50) returns a proxy with overrideTimeout=50; calling a method
|
||||
# on it enqueues a 50ms fake timer. After flushing, pending is empty.
|
||||
if test['name'] == 'rpc proxy timeout(n) rejects after a custom window':
|
||||
return (
|
||||
f' (deftest "{safe_name}"\n'
|
||||
f' (hs-cleanup!)\n'
|
||||
f' (eval-hs "socket CustomTOSocket ws://localhost/ws with timeout 60000 end")\n'
|
||||
f' (let ((wrapper (host-get (host-global "window") "CustomTOSocket")))\n'
|
||||
f' (let ((rpc (host-get wrapper "rpc")))\n'
|
||||
f' (do\n'
|
||||
f' (let ((timeout-fn (host-call rpc "timeout"))\n'
|
||||
f' (custom-proxy (host-call-fn timeout-fn (list 50))))\n'
|
||||
f' (host-call custom-proxy "willTimeOut"))\n'
|
||||
f' (let ((keys-before (host-call (host-global "Object") "keys" (host-get wrapper "pending"))))\n'
|
||||
f' (assert= (host-get keys-before "length") 1))\n'
|
||||
f' (host-call (host-global "__hsFlushTimers") "call")\n'
|
||||
f' (let ((keys-after (host-call (host-global "Object") "keys" (host-get wrapper "pending"))))\n'
|
||||
f' (assert= (host-get keys-after "length") 0))))))'
|
||||
)
|
||||
|
||||
# Special case: cluster-29 init events. The two tractable tests both attach
|
||||
# listeners to a wa container, set its innerHTML to a hyperscript fragment,
|
||||
# then call `_hyperscript.processNode(wa)`. Hand-roll deftests using
|
||||
@@ -3038,6 +2816,20 @@ def generate_eval_only_test(test, idx):
|
||||
expected_sx = js_val_to_sx(be_match.group(1))
|
||||
assertions.append(f' (assert= (eval-hs "{hs_expr}") {expected_sx})')
|
||||
|
||||
# Pattern 2d: evalStatically() + toMatch(/cannot be evaluated statically/)
|
||||
# Handles: try { _hyperscript.parse("expr").evalStatically(); } catch(e) { return e.message; }
|
||||
# followed by: expect(msg).toMatch(/cannot be evaluated statically/)
|
||||
# Uses guard directly because try-call in hs-run-filtered.js is a registration stub
|
||||
# and assert-throws cannot catch exceptions during test execution.
|
||||
if not assertions:
|
||||
if 'evalStatically' in body and 'cannot be evaluated statically' in body:
|
||||
for m in re.finditer(
|
||||
r'_hyperscript\.parse\((["\x27])(.+?)\1\)\.evalStatically\(\)',
|
||||
body
|
||||
):
|
||||
hs_expr = extract_hs_expr(m.group(2))
|
||||
assertions.append(f' (guard (_e (true nil)) (hs-eval-statically "{hs_expr}") (error "hs-eval-statically did not throw for: {hs_expr}"))')
|
||||
|
||||
# Pattern 2e: run() with side-effects on window, checked via
|
||||
# const X = await evaluate(() => <js-expr>); expect(X).toBe(val)
|
||||
# The const holds the evaluated JS expr, not the run() return value,
|
||||
@@ -3099,7 +2891,16 @@ def generate_eval_only_test(test, idx):
|
||||
body, re.DOTALL
|
||||
):
|
||||
hs_expr = extract_hs_expr(m.group(2))
|
||||
assertions.append(f' (assert-throws (eval-hs "{hs_expr}"))')
|
||||
assertions.append(f' (assert-throws (fn () (eval-hs "{hs_expr}")))')
|
||||
|
||||
# Pattern 4: error("expr").toBeNull() — parsing/eval must not throw
|
||||
if not assertions:
|
||||
for m in re.finditer(
|
||||
r'error\((["\x27])(.+?)\1\).*?toBeNull\(\)',
|
||||
body, re.DOTALL
|
||||
):
|
||||
hs_expr = extract_hs_expr(m.group(2))
|
||||
assertions.append(f' (hs-compile "{hs_expr}")')
|
||||
|
||||
if not assertions:
|
||||
return None # Can't convert this body pattern
|
||||
@@ -3140,6 +2941,11 @@ def generate_compile_only_test(test):
|
||||
|
||||
def generate_test(test, idx):
|
||||
"""Generate SX deftest for an upstream test. Dispatches to Chai, PW, or eval-only."""
|
||||
if test['name'] in MANUAL_TEST_BODIES:
|
||||
name = sx_name(test['name'])
|
||||
lines = [f' (deftest "{name}"'] + MANUAL_TEST_BODIES[test['name']] + [' )']
|
||||
return '\n'.join(lines)
|
||||
|
||||
elements = parse_html(test['html'])
|
||||
|
||||
if not elements and not test.get('html', '').strip():
|
||||
@@ -3465,6 +3271,17 @@ output.append(' (nth _e 1)')
|
||||
output.append(' (raise _e))))')
|
||||
output.append(' (handler me-val))))))')
|
||||
output.append('')
|
||||
output.append(';; Evaluate a HS expression using evalStatically semantics:')
|
||||
output.append(';; only literal values (numbers, strings, booleans, null, time units)')
|
||||
output.append(';; succeed — any other expression raises "cannot be evaluated statically".')
|
||||
output.append('(define hs-eval-statically')
|
||||
output.append(' (fn (src)')
|
||||
output.append(' (let ((ast (hs-compile src)))')
|
||||
output.append(' (if (or (number? ast) (string? ast) (boolean? ast)')
|
||||
output.append(' (and (list? ast) (= (first ast) (quote null-literal))))')
|
||||
output.append(' (eval-hs src)')
|
||||
output.append(' (raise "cannot be evaluated statically")))))')
|
||||
output.append('')
|
||||
|
||||
# Group by category
|
||||
categories = OrderedDict()
|
||||
|
||||
Reference in New Issue
Block a user