Fix WASM browser: broken links (&rest bytecode) + broken reactive counter (ListRef mutation)
Two bugs fixed: 1. Links: bytecode compiler doesn't handle &rest params — treats them as positional, so (first rest) gets a raw string instead of a list. Replaced &rest with explicit optional params in all bytecode-compiled web SX files (dom-query, dom-add-listener, browser-push-state, etc.). The VM already pads missing args with Nil. 2. Reactive counter: signal-remove-sub! used (filter ...) which returns immutable List, but signal-add-sub! uses (append!) which only mutates ListRef. Subscribers silently vanished after first effect re-run. Fixed by adding remove! primitive that mutates ListRef in-place. Also: - Added evalVM API to WASM kernel (compile + run through bytecode VM) - Added scope tracing (scope-push!/pop!/peek/context instrumentation) - Added Playwright reactive mode for debugging island signal/DOM state - Replaced cek-call with direct calls in core-signals.sx effect/computed - Recompiled all 23 bytecode modules Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -1,21 +1,110 @@
|
||||
(define assert-signal-value :effects () (fn ((sig :as any) expected) (let ((actual (deref sig))) (assert= actual expected (str "Expected signal value " expected ", got " actual)))))
|
||||
(define
|
||||
assert-signal-value
|
||||
:effects ()
|
||||
(fn
|
||||
((sig :as any) expected)
|
||||
(let
|
||||
((actual (deref sig)))
|
||||
(assert=
|
||||
actual
|
||||
expected
|
||||
(str "Expected signal value " expected ", got " actual)))))
|
||||
|
||||
(define assert-signal-has-subscribers :effects () (fn ((sig :as any)) (assert (> (len (signal-subscribers sig)) 0) "Expected signal to have subscribers")))
|
||||
(define
|
||||
assert-signal-has-subscribers
|
||||
:effects ()
|
||||
(fn
|
||||
((sig :as any))
|
||||
(assert
|
||||
(> (len (signal-subscribers sig)) 0)
|
||||
"Expected signal to have subscribers")))
|
||||
|
||||
(define assert-signal-no-subscribers :effects () (fn ((sig :as any)) (assert (= (len (signal-subscribers sig)) 0) "Expected signal to have no subscribers")))
|
||||
(define
|
||||
assert-signal-no-subscribers
|
||||
:effects ()
|
||||
(fn
|
||||
((sig :as any))
|
||||
(assert
|
||||
(= (len (signal-subscribers sig)) 0)
|
||||
"Expected signal to have no subscribers")))
|
||||
|
||||
(define assert-signal-subscriber-count :effects () (fn ((sig :as any) (n :as number)) (let ((actual (len (signal-subscribers sig)))) (assert= actual n (str "Expected " n " subscribers, got " actual)))))
|
||||
(define
|
||||
assert-signal-subscriber-count
|
||||
:effects ()
|
||||
(fn
|
||||
((sig :as any) (n :as number))
|
||||
(let
|
||||
((actual (len (signal-subscribers sig))))
|
||||
(assert= actual n (str "Expected " n " subscribers, got " actual)))))
|
||||
|
||||
(define simulate-signal-set! :effects (mutation) (fn ((sig :as any) value) (reset! sig value)))
|
||||
(define
|
||||
simulate-signal-set!
|
||||
:effects (mutation)
|
||||
(fn ((sig :as any) value) (reset! sig value)))
|
||||
|
||||
(define simulate-signal-swap! :effects (mutation) (fn ((sig :as any) (f :as lambda) &rest args) (apply swap! (cons sig (cons f args)))))
|
||||
(define
|
||||
simulate-signal-swap!
|
||||
:effects (mutation)
|
||||
(fn ((sig :as any) (f :as lambda)) (swap! sig f)))
|
||||
|
||||
(define assert-computed-dep-count :effects () (fn ((sig :as any) (n :as number)) (let ((actual (len (signal-deps sig)))) (assert= actual n (str "Expected " n " deps, got " actual)))))
|
||||
(define
|
||||
assert-computed-dep-count
|
||||
:effects ()
|
||||
(fn
|
||||
((sig :as any) (n :as number))
|
||||
(let
|
||||
((actual (len (signal-deps sig))))
|
||||
(assert= actual n (str "Expected " n " deps, got " actual)))))
|
||||
|
||||
(define assert-computed-depends-on :effects () (fn ((computed-sig :as any) (dep-sig :as any)) (assert (contains? (signal-deps computed-sig) dep-sig) "Expected computed to depend on the given signal")))
|
||||
(define
|
||||
assert-computed-depends-on
|
||||
:effects ()
|
||||
(fn
|
||||
((computed-sig :as any) (dep-sig :as any))
|
||||
(assert
|
||||
(contains? (signal-deps computed-sig) dep-sig)
|
||||
"Expected computed to depend on the given signal")))
|
||||
|
||||
(define count-effect-runs :effects (mutation) (fn ((thunk :as lambda)) (let ((count (signal 0))) (effect (fn () (deref count))) (let ((run-count 0) (tracker (effect (fn () (set! run-count (+ run-count 1)) (cek-call thunk nil))))) run-count))))
|
||||
(define
|
||||
count-effect-runs
|
||||
:effects (mutation)
|
||||
(fn
|
||||
((thunk :as lambda))
|
||||
(let
|
||||
((count (signal 0)))
|
||||
(effect (fn () (deref count)))
|
||||
(let
|
||||
((run-count 0)
|
||||
(tracker
|
||||
(effect
|
||||
(fn () (set! run-count (+ run-count 1)) (cek-call thunk nil)))))
|
||||
run-count))))
|
||||
|
||||
(define make-test-signal :effects (mutation) (fn (initial-value) (let ((sig (signal initial-value)) (history (list))) (effect (fn () (append! history (deref sig)))) {:signal sig :history history})))
|
||||
(define
|
||||
make-test-signal
|
||||
:effects (mutation)
|
||||
(fn
|
||||
(initial-value)
|
||||
(let
|
||||
((sig (signal initial-value)) (history (list)))
|
||||
(effect (fn () (append! history (deref sig))))
|
||||
{:signal sig :history history})))
|
||||
|
||||
(define assert-batch-coalesces :effects (mutation) (fn ((thunk :as lambda) (expected-notify-count :as number)) (let ((notify-count 0) (sig (signal 0))) (effect (fn () (deref sig) (set! notify-count (+ notify-count 1)))) (set! notify-count 0) (batch thunk) (assert= notify-count expected-notify-count (str "Expected " expected-notify-count " notifications, got " notify-count)))))
|
||||
(define
|
||||
assert-batch-coalesces
|
||||
:effects (mutation)
|
||||
(fn
|
||||
((thunk :as lambda) (expected-notify-count :as number))
|
||||
(let
|
||||
((notify-count 0) (sig (signal 0)))
|
||||
(effect (fn () (deref sig) (set! notify-count (+ notify-count 1))))
|
||||
(set! notify-count 0)
|
||||
(batch thunk)
|
||||
(assert=
|
||||
notify-count
|
||||
expected-notify-count
|
||||
(str
|
||||
"Expected "
|
||||
expected-notify-count
|
||||
" notifications, got "
|
||||
notify-count)))))
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,227 +1,221 @@
|
||||
;; ==========================================================================
|
||||
;; browser.sx — Browser API library functions
|
||||
;;
|
||||
;; Location, history, storage, cookies, timers, fetch — all expressed
|
||||
;; using the host FFI primitives. Library functions, not primitives.
|
||||
;; ==========================================================================
|
||||
(define
|
||||
browser-location-href
|
||||
(fn () (host-get (host-get (dom-window) "location") "href")))
|
||||
|
||||
(define
|
||||
browser-location-pathname
|
||||
(fn () (host-get (host-get (dom-window) "location") "pathname")))
|
||||
|
||||
;; --------------------------------------------------------------------------
|
||||
;; Location & navigation
|
||||
;; --------------------------------------------------------------------------
|
||||
(define
|
||||
browser-location-origin
|
||||
(fn () (host-get (host-get (dom-window) "location") "origin")))
|
||||
|
||||
(define browser-location-href
|
||||
(fn ()
|
||||
(host-get (host-get (dom-window) "location") "href")))
|
||||
(define
|
||||
browser-same-origin?
|
||||
(fn (url) (starts-with? url (browser-location-origin))))
|
||||
|
||||
(define browser-location-pathname
|
||||
(fn ()
|
||||
(host-get (host-get (dom-window) "location") "pathname")))
|
||||
|
||||
(define browser-location-origin
|
||||
(fn ()
|
||||
(host-get (host-get (dom-window) "location") "origin")))
|
||||
|
||||
(define browser-same-origin?
|
||||
(fn (url)
|
||||
(starts-with? url (browser-location-origin))))
|
||||
|
||||
;; Extract pathname from a URL string using the URL API
|
||||
(define url-pathname
|
||||
(fn (url)
|
||||
(define
|
||||
url-pathname
|
||||
(fn
|
||||
(url)
|
||||
(host-get (host-new "URL" url (browser-location-origin)) "pathname")))
|
||||
|
||||
(define browser-push-state
|
||||
(fn (url-or-state &rest rest)
|
||||
(if (empty? rest)
|
||||
;; Single arg: just URL
|
||||
(host-call (host-get (dom-window) "history") "pushState" nil "" url-or-state)
|
||||
;; Three args: state, title, url
|
||||
(host-call (host-get (dom-window) "history") "pushState" url-or-state (first rest) (nth rest 1)))))
|
||||
(define
|
||||
browser-push-state
|
||||
(fn
|
||||
(url-or-state title url)
|
||||
(if
|
||||
(nil? title)
|
||||
(host-call
|
||||
(host-get (dom-window) "history")
|
||||
"pushState"
|
||||
nil
|
||||
""
|
||||
url-or-state)
|
||||
(host-call
|
||||
(host-get (dom-window) "history")
|
||||
"pushState"
|
||||
url-or-state
|
||||
title
|
||||
url))))
|
||||
|
||||
(define browser-replace-state
|
||||
(fn (url-or-state &rest rest)
|
||||
(if (empty? rest)
|
||||
(host-call (host-get (dom-window) "history") "replaceState" nil "" url-or-state)
|
||||
(host-call (host-get (dom-window) "history") "replaceState" url-or-state (first rest) (nth rest 1)))))
|
||||
(define
|
||||
browser-replace-state
|
||||
(fn
|
||||
(url-or-state title url)
|
||||
(if
|
||||
(nil? title)
|
||||
(host-call
|
||||
(host-get (dom-window) "history")
|
||||
"replaceState"
|
||||
nil
|
||||
""
|
||||
url-or-state)
|
||||
(host-call
|
||||
(host-get (dom-window) "history")
|
||||
"replaceState"
|
||||
url-or-state
|
||||
title
|
||||
url))))
|
||||
|
||||
(define browser-reload
|
||||
(fn ()
|
||||
(host-call (host-get (dom-window) "location") "reload")))
|
||||
(define
|
||||
browser-reload
|
||||
(fn () (host-call (host-get (dom-window) "location") "reload")))
|
||||
|
||||
(define browser-navigate
|
||||
(fn (url)
|
||||
(host-set! (host-get (dom-window) "location") "href" url)))
|
||||
(define
|
||||
browser-navigate
|
||||
(fn (url) (host-set! (host-get (dom-window) "location") "href" url)))
|
||||
|
||||
|
||||
;; --------------------------------------------------------------------------
|
||||
;; Storage
|
||||
;; --------------------------------------------------------------------------
|
||||
|
||||
(define local-storage-get
|
||||
(fn (key)
|
||||
(define
|
||||
local-storage-get
|
||||
(fn
|
||||
(key)
|
||||
(host-call (host-get (dom-window) "localStorage") "getItem" key)))
|
||||
|
||||
(define local-storage-set
|
||||
(fn (key val)
|
||||
(define
|
||||
local-storage-set
|
||||
(fn
|
||||
(key val)
|
||||
(host-call (host-get (dom-window) "localStorage") "setItem" key val)))
|
||||
|
||||
(define local-storage-remove
|
||||
(fn (key)
|
||||
(define
|
||||
local-storage-remove
|
||||
(fn
|
||||
(key)
|
||||
(host-call (host-get (dom-window) "localStorage") "removeItem" key)))
|
||||
|
||||
|
||||
;; --------------------------------------------------------------------------
|
||||
;; Timers
|
||||
;; --------------------------------------------------------------------------
|
||||
|
||||
(define set-timeout
|
||||
(fn (fn-val ms)
|
||||
(define
|
||||
set-timeout
|
||||
(fn
|
||||
(fn-val ms)
|
||||
(host-call (dom-window) "setTimeout" (host-callback fn-val) ms)))
|
||||
|
||||
(define set-interval
|
||||
(fn (fn-val ms)
|
||||
(define
|
||||
set-interval
|
||||
(fn
|
||||
(fn-val ms)
|
||||
(host-call (dom-window) "setInterval" (host-callback fn-val) ms)))
|
||||
|
||||
(define clear-timeout
|
||||
(fn (id)
|
||||
(host-call (dom-window) "clearTimeout" id)))
|
||||
(define clear-timeout (fn (id) (host-call (dom-window) "clearTimeout" id)))
|
||||
|
||||
(define clear-interval
|
||||
(fn (id)
|
||||
(host-call (dom-window) "clearInterval" id)))
|
||||
(define
|
||||
clear-interval
|
||||
(fn (id) (host-call (dom-window) "clearInterval" id)))
|
||||
|
||||
(define request-animation-frame
|
||||
(fn (fn-val)
|
||||
(define
|
||||
request-animation-frame
|
||||
(fn
|
||||
(fn-val)
|
||||
(host-call (dom-window) "requestAnimationFrame" (host-callback fn-val))))
|
||||
|
||||
(define
|
||||
fetch-request
|
||||
(fn (url opts) (host-call (dom-window) "fetch" url opts)))
|
||||
|
||||
;; --------------------------------------------------------------------------
|
||||
;; Fetch
|
||||
;; --------------------------------------------------------------------------
|
||||
(define new-abort-controller (fn () (host-new "AbortController")))
|
||||
|
||||
(define fetch-request
|
||||
(fn (url opts)
|
||||
(host-call (dom-window) "fetch" url opts)))
|
||||
(define controller-signal (fn (controller) (host-get controller "signal")))
|
||||
|
||||
(define new-abort-controller
|
||||
(fn ()
|
||||
(host-new "AbortController")))
|
||||
(define controller-abort (fn (controller) (host-call controller "abort")))
|
||||
|
||||
(define controller-signal
|
||||
(fn (controller)
|
||||
(host-get controller "signal")))
|
||||
|
||||
(define controller-abort
|
||||
(fn (controller)
|
||||
(host-call controller "abort")))
|
||||
|
||||
|
||||
;; --------------------------------------------------------------------------
|
||||
;; Promises
|
||||
;; --------------------------------------------------------------------------
|
||||
|
||||
(define promise-then
|
||||
(fn (p on-resolve on-reject)
|
||||
(let ((cb-resolve (host-callback on-resolve))
|
||||
(cb-reject (if on-reject (host-callback on-reject) nil)))
|
||||
(if cb-reject
|
||||
(define
|
||||
promise-then
|
||||
(fn
|
||||
(p on-resolve on-reject)
|
||||
(let
|
||||
((cb-resolve (host-callback on-resolve))
|
||||
(cb-reject (if on-reject (host-callback on-reject) nil)))
|
||||
(if
|
||||
cb-reject
|
||||
(host-call (host-call p "then" cb-resolve) "catch" cb-reject)
|
||||
(host-call p "then" cb-resolve)))))
|
||||
|
||||
(define promise-resolve
|
||||
(fn (val)
|
||||
(host-call (host-global "Promise") "resolve" val)))
|
||||
(define
|
||||
promise-resolve
|
||||
(fn (val) (host-call (host-global "Promise") "resolve" val)))
|
||||
|
||||
(define promise-delayed
|
||||
(fn (ms val)
|
||||
(host-new "Promise" (host-callback
|
||||
(fn (resolve)
|
||||
(set-timeout (fn () (host-call resolve "call" nil val)) ms))))))
|
||||
(define
|
||||
promise-delayed
|
||||
(fn
|
||||
(ms val)
|
||||
(host-new
|
||||
"Promise"
|
||||
(host-callback
|
||||
(fn
|
||||
(resolve)
|
||||
(set-timeout (fn () (host-call resolve "call" nil val)) ms))))))
|
||||
|
||||
(define browser-confirm (fn (msg) (host-call (dom-window) "confirm" msg)))
|
||||
|
||||
;; --------------------------------------------------------------------------
|
||||
;; Dialogs & media
|
||||
;; --------------------------------------------------------------------------
|
||||
(define
|
||||
browser-prompt
|
||||
(fn (msg default) (host-call (dom-window) "prompt" msg default)))
|
||||
|
||||
(define browser-confirm
|
||||
(fn (msg) (host-call (dom-window) "confirm" msg)))
|
||||
|
||||
(define browser-prompt
|
||||
(fn (msg default)
|
||||
(host-call (dom-window) "prompt" msg default)))
|
||||
|
||||
(define browser-media-matches?
|
||||
(fn (query)
|
||||
(define
|
||||
browser-media-matches?
|
||||
(fn
|
||||
(query)
|
||||
(host-get (host-call (dom-window) "matchMedia" query) "matches")))
|
||||
|
||||
(define json-parse (fn (s) (host-call (host-global "JSON") "parse" s)))
|
||||
|
||||
;; --------------------------------------------------------------------------
|
||||
;; JSON
|
||||
;; --------------------------------------------------------------------------
|
||||
(define
|
||||
log-info
|
||||
(fn (msg) (host-call (host-global "console") "log" (str "[sx] " msg))))
|
||||
|
||||
(define json-parse
|
||||
(fn (s)
|
||||
(host-call (host-global "JSON") "parse" s)))
|
||||
(define
|
||||
log-warn
|
||||
(fn (msg) (host-call (host-global "console") "warn" (str "[sx] " msg))))
|
||||
|
||||
(define
|
||||
console-log
|
||||
(fn (msg) (host-call (host-global "console") "log" (str "[sx] " msg))))
|
||||
|
||||
;; --------------------------------------------------------------------------
|
||||
;; Console
|
||||
;; --------------------------------------------------------------------------
|
||||
(define now-ms (fn () (host-call (host-global "Date") "now")))
|
||||
|
||||
(define log-info
|
||||
(fn (msg)
|
||||
(host-call (host-global "console") "log" (str "[sx] " msg))))
|
||||
|
||||
(define log-warn
|
||||
(fn (msg)
|
||||
(host-call (host-global "console") "warn" (str "[sx] " msg))))
|
||||
|
||||
(define console-log
|
||||
(fn (&rest args)
|
||||
(host-call (host-global "console") "log"
|
||||
(join " " (cons "[sx]" (map str args))))))
|
||||
|
||||
(define now-ms
|
||||
(fn ()
|
||||
(host-call (host-global "Date") "now")))
|
||||
|
||||
|
||||
;; --------------------------------------------------------------------------
|
||||
;; Scheduling
|
||||
;; --------------------------------------------------------------------------
|
||||
|
||||
(define schedule-idle
|
||||
(fn (f)
|
||||
(let ((cb (host-callback (fn (_deadline) (f)))))
|
||||
(if (host-get (dom-window) "requestIdleCallback")
|
||||
(define
|
||||
schedule-idle
|
||||
(fn
|
||||
(f)
|
||||
(let
|
||||
((cb (host-callback (fn (_deadline) (f)))))
|
||||
(if
|
||||
(host-get (dom-window) "requestIdleCallback")
|
||||
(host-call (dom-window) "requestIdleCallback" cb)
|
||||
(set-timeout cb 0)))))
|
||||
|
||||
(define
|
||||
set-cookie
|
||||
(fn
|
||||
(name value days)
|
||||
(let
|
||||
((d (or days 365))
|
||||
(expires
|
||||
(host-call
|
||||
(host-new
|
||||
"Date"
|
||||
(+ (host-call (host-global "Date") "now") (* d 86400000)))
|
||||
"toUTCString")))
|
||||
(host-set!
|
||||
(dom-document)
|
||||
"cookie"
|
||||
(str
|
||||
name
|
||||
"="
|
||||
(host-call nil "encodeURIComponent" value)
|
||||
";expires="
|
||||
expires
|
||||
";path=/;SameSite=Lax")))))
|
||||
|
||||
;; --------------------------------------------------------------------------
|
||||
;; Cookies
|
||||
;; --------------------------------------------------------------------------
|
||||
|
||||
(define set-cookie
|
||||
(fn (name value days)
|
||||
(let ((d (or days 365))
|
||||
(expires (host-call
|
||||
(host-new "Date"
|
||||
(+ (host-call (host-global "Date") "now")
|
||||
(* d 864e5)))
|
||||
"toUTCString")))
|
||||
(host-set! (dom-document) "cookie"
|
||||
(str name "="
|
||||
(host-call nil "encodeURIComponent" value)
|
||||
";expires=" expires ";path=/;SameSite=Lax")))))
|
||||
|
||||
(define get-cookie
|
||||
(fn (name)
|
||||
(let ((cookies (host-get (dom-document) "cookie"))
|
||||
(match (host-call cookies "match"
|
||||
(host-new "RegExp"
|
||||
(str "(?:^|;\\s*)" name "=([^;]*)")))))
|
||||
(if match
|
||||
(host-call nil "decodeURIComponent" (host-get match 1))
|
||||
nil))))
|
||||
(define
|
||||
get-cookie
|
||||
(fn
|
||||
(name)
|
||||
(let
|
||||
((cookies (host-get (dom-document) "cookie"))
|
||||
(match
|
||||
(host-call
|
||||
cookies
|
||||
"match"
|
||||
(host-new "RegExp" (str "(?:^|;\\s*)" name "=([^;]*)")))))
|
||||
(if match (host-call nil "decodeURIComponent" (host-get match 1)) nil))))
|
||||
|
||||
@@ -9,13 +9,11 @@
|
||||
(define
|
||||
dom-create-element
|
||||
(fn
|
||||
(tag &rest ns-arg)
|
||||
(let
|
||||
((ns (if (and ns-arg (not (empty? ns-arg))) (first ns-arg) nil)))
|
||||
(if
|
||||
ns
|
||||
(host-call (dom-document) "createElementNS" ns tag)
|
||||
(host-call (dom-document) "createElement" tag)))))
|
||||
(tag ns)
|
||||
(if
|
||||
ns
|
||||
(host-call (dom-document) "createElementNS" ns tag)
|
||||
(host-call (dom-document) "createElement" tag))))
|
||||
|
||||
(define
|
||||
create-text-node
|
||||
@@ -128,11 +126,11 @@
|
||||
(define
|
||||
dom-query
|
||||
(fn
|
||||
(root-or-sel &rest rest)
|
||||
(root-or-sel sel)
|
||||
(if
|
||||
(empty? rest)
|
||||
(nil? sel)
|
||||
(host-call (dom-document) "querySelector" root-or-sel)
|
||||
(host-call root-or-sel "querySelector" (first rest)))))
|
||||
(host-call root-or-sel "querySelector" sel))))
|
||||
|
||||
(define
|
||||
dom-query-all
|
||||
@@ -342,12 +340,12 @@
|
||||
(define
|
||||
dom-add-listener
|
||||
(fn
|
||||
(el event-name handler &rest opts)
|
||||
(el event-name handler opts)
|
||||
(let
|
||||
((cb (host-callback handler)))
|
||||
(if
|
||||
(and opts (not (empty? opts)))
|
||||
(host-call el "addEventListener" event-name cb (first opts))
|
||||
opts
|
||||
(host-call el "addEventListener" event-name cb opts)
|
||||
(host-call el "addEventListener" event-name cb))
|
||||
(fn () (host-call el "removeEventListener" event-name cb)))))
|
||||
|
||||
|
||||
Reference in New Issue
Block a user