hs: port loops/hs WebSocket runtime + test suite (replaces arch's underscore-prefixed API)
Adopts loops/hs's cleaner WebSocket API on top of arch's hyperscript: - Runtime: replace 5 arch socket functions (hs-try-json-parse, hs-socket-normalise-url, hs-socket-bind-name!, hs-socket-resolve-rpc!, hs-socket-register!) with loops/hs's versions. Wrapper fields now use external-style names (url, timeout, pending, handler, json?, closedFlag, dispatchEvent) instead of internal-style underscores (_url, _timeout, _pending, _hsSetupSocket). - Tests: replace arch's 257-line hs-upstream-socket suite (which probed _pending, _hsSetupSocket etc.) with loops/hs's 162-line suite that checks the new field names. Both suites cover the same 16 E36 behavioral cases. Parser/compiler unchanged: both branches emit (hs-socket-register! name-path url timeout handler json?) so the call signature is compatible with either runtime. Arch's parse-socket-feat / emit-socket are preserved. Local hs test.sh: 23/25 (the 2 failures are pre-existing hide/show cmd compiler issues, not socket-related). Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -3242,97 +3242,112 @@
|
||||
|
||||
(define hs-token-op? (fn (tok) (dict-get tok :op)))
|
||||
|
||||
;; ── WebSocket / socket feature ───────────────────────────────────
|
||||
|
||||
(define
|
||||
hs-try-json-parse
|
||||
(fn (data) (if (string? data) (guard (_e nil) (json-parse data)) nil)))
|
||||
|
||||
(define
|
||||
hs-socket-normalise-url
|
||||
(fn
|
||||
(url)
|
||||
(if
|
||||
(or (starts-with? url "ws://") (starts-with? url "wss://"))
|
||||
url
|
||||
(let
|
||||
((proto (host-get (host-global "location") "protocol"))
|
||||
(host-str (host-get (host-global "location") "host")))
|
||||
(let
|
||||
((scheme (if (= proto "https:") "wss://" "ws://")))
|
||||
(str scheme host-str url))))))
|
||||
|
||||
(define
|
||||
hs-socket-bind-name!
|
||||
(fn
|
||||
(name-path wrapper)
|
||||
(let
|
||||
((win (host-global "window")))
|
||||
(if
|
||||
(= (len name-path) 1)
|
||||
(host-set! win (first name-path) wrapper)
|
||||
(do
|
||||
(when
|
||||
(nil? (host-get win (first name-path)))
|
||||
(host-set! win (first name-path) (host-new "Object")))
|
||||
(host-set!
|
||||
(host-get win (first name-path))
|
||||
(nth name-path 1)
|
||||
wrapper))))))
|
||||
(fn (s) (host-call (host-global "JSON") "parse" s)))
|
||||
|
||||
(define
|
||||
hs-socket-resolve-rpc!
|
||||
(fn
|
||||
(wrapper data)
|
||||
(wrapper msg)
|
||||
(let
|
||||
((iid (host-get data "iid")))
|
||||
(when
|
||||
(not (nil? iid))
|
||||
(let
|
||||
((pending (host-get wrapper "_pending")))
|
||||
(when
|
||||
(not (nil? pending))
|
||||
(let
|
||||
((entry (host-get pending iid)))
|
||||
(when
|
||||
(not (nil? entry))
|
||||
(host-set! pending iid nil)
|
||||
(if
|
||||
(not (nil? (host-get data "throw")))
|
||||
(host-call-fn
|
||||
(host-get entry "reject")
|
||||
(list (host-get data "throw")))
|
||||
(host-call-fn
|
||||
(host-get entry "resolve")
|
||||
(list (host-get data "return"))))))))))))
|
||||
((pending (host-get wrapper "pending")) (iid (host-get msg "iid")))
|
||||
(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 on-message-handler json?)
|
||||
(name-path url timeout-ms handler json?)
|
||||
(let
|
||||
((norm-url (hs-socket-normalise-url url)))
|
||||
((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
|
||||
((wrapper (host-new "Object")))
|
||||
(do
|
||||
(host-set! wrapper "_url" norm-url)
|
||||
(host-set! wrapper "_timeout" (if (nil? timeout) 0 timeout))
|
||||
(host-set! wrapper "_pending" (host-new "Object"))
|
||||
(host-set! wrapper "_closed" false)
|
||||
((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)
|
||||
(let
|
||||
((ws (host-new "WebSocket" norm-url)))
|
||||
(do
|
||||
(host-set! wrapper "_ws" ws)
|
||||
(let
|
||||
((msg-handler (host-callback (fn (evt) (do (let ((parsed (hs-try-json-parse (host-get evt "data")))) (when (and (not (nil? parsed)) (not (nil? (host-get parsed "iid")))) (hs-socket-resolve-rpc! wrapper parsed))) (when (not (nil? on-message-handler)) (if json? (let ((data (hs-try-json-parse (host-get evt "data")))) (when (not (nil? data)) (on-message-handler data))) (on-message-handler evt))))))))
|
||||
(do
|
||||
(host-set! ws "onmessage" msg-handler)
|
||||
(host-set! wrapper "_onmessage_handler" msg-handler)
|
||||
(host-set!
|
||||
ws
|
||||
"onclose"
|
||||
(host-callback
|
||||
(fn (e) (host-set! wrapper "_closed" true))))
|
||||
(host-call-fn
|
||||
(host-global "_hsSetupSocket")
|
||||
(list wrapper))
|
||||
(hs-socket-bind-name! name-path wrapper)
|
||||
wrapper)))))))))
|
||||
((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)))))
|
||||
|
||||
|
||||
Reference in New Issue
Block a user