js-on-sx: Map/Set.prototype.forEach honour thisArg + pass (v,k,coll)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 40s

This commit is contained in:
2026-05-09 17:33:29 +00:00
parent 8ae7187c55
commit 87bf3711c4
2 changed files with 30 additions and 15 deletions

View File

@@ -5687,23 +5687,30 @@
(define
js-map-do-foreach
(fn
(m cb)
(m cb &rest opts)
(let
((ks (get m "__map_keys__")) (vs (get m "__map_vals__")))
((ks (get m "__map_keys__"))
(vs (get m "__map_vals__"))
(this-arg
(cond
((empty? opts) js-global-this)
((js-undefined? (nth opts 0)) js-global-this)
((= (nth opts 0) nil) js-global-this)
(else (nth opts 0)))))
(begin
(js-map-foreach-loop ks vs cb 0 (len ks))
(js-map-foreach-loop ks vs cb this-arg m 0 (len ks))
js-undefined))))
(define
js-map-foreach-loop
(fn
(ks vs cb i n)
(ks vs cb this-arg m i n)
(cond
((>= i n) nil)
(else
(begin
(js-call-with-this js-undefined cb (list (nth vs i) (nth ks i)))
(js-map-foreach-loop ks vs cb (+ i 1) n))))))
(js-call-with-this this-arg cb (list (nth vs i) (nth ks i) m))
(js-map-foreach-loop ks vs cb this-arg m (+ i 1) n))))))
(define
Map
@@ -5716,7 +5723,7 @@
:has (fn (k) (js-map-do-has (js-this) k))
:delete (fn (k) (js-map-do-delete (js-this) k))
:clear (fn () (js-map-do-clear (js-this)))
:forEach (fn (cb) (js-map-do-foreach (js-this) cb))
:forEach (fn (&rest args) (let ((cb (if (empty? args) :js-undefined (nth args 0))) (ta (if (>= (len args) 2) (nth args 1) :js-undefined))) (js-map-do-foreach (js-this) cb ta)))
:keys (fn () (let ((ks (get (js-this) "__map_keys__"))) (js-list-copy ks)))
:values (fn () (let ((vs (get (js-this) "__map_vals__"))) (js-list-copy vs)))
:entries
@@ -5834,26 +5841,32 @@
(define
js-set-do-foreach
(fn
(s cb)
(s cb &rest opts)
(let
((items (get s "__set_items__")))
((items (get s "__set_items__"))
(this-arg
(cond
((empty? opts) js-global-this)
((js-undefined? (nth opts 0)) js-global-this)
((= (nth opts 0) nil) js-global-this)
(else (nth opts 0)))))
(begin
(js-set-foreach-loop items cb 0 (len items))
(js-set-foreach-loop items cb this-arg s 0 (len items))
js-undefined))))
(define
js-set-foreach-loop
(fn
(items cb i n)
(items cb this-arg s i n)
(cond
((>= i n) nil)
(else
(begin
(js-call-with-this
js-undefined
this-arg
cb
(list (nth items i) (nth items i)))
(js-set-foreach-loop items cb (+ i 1) n))))))
(list (nth items i) (nth items i) s))
(js-set-foreach-loop items cb this-arg s (+ i 1) n))))))
(define
Set
@@ -5865,7 +5878,7 @@
:has (fn (v) (js-set-do-has (js-this) v))
:delete (fn (v) (js-set-do-delete (js-this) v))
:clear (fn () (js-set-do-clear (js-this)))
:forEach (fn (cb) (js-set-do-foreach (js-this) cb))
:forEach (fn (&rest args) (let ((cb (if (empty? args) :js-undefined (nth args 0))) (ta (if (>= (len args) 2) (nth args 1) :js-undefined))) (js-set-do-foreach (js-this) cb ta)))
:keys (fn () (js-list-copy (get (js-this) "__set_items__")))
:values (fn () (js-list-copy (get (js-this) "__set_items__")))
:entries