js-on-sx: Array.prototype forEach/map/filter honour thisArg + pass (v,i,arr)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 39s

This commit is contained in:
2026-05-09 18:08:33 +00:00
parent 87bf3711c4
commit 1fef6ec94d
2 changed files with 49 additions and 13 deletions

View File

@@ -2222,13 +2222,40 @@
((sep (if (= (len args) 0) "," (js-to-string (nth args 0)))))
(js-list-join arr sep))))
((= name "concat") (fn (&rest args) (js-list-concat arr args)))
((= name "map") (fn (f) (js-list-map-loop f arr 0 (list))))
((= name "map")
(fn (&rest args)
(let
((f (if (empty? args) :js-undefined (nth args 0)))
(this-arg
(cond
((< (len args) 2) js-global-this)
((js-undefined? (nth args 1)) js-global-this)
((= (nth args 1) nil) js-global-this)
(else (nth args 1)))))
(js-list-map-loop f arr this-arg 0 (list)))))
((= name "filter")
(fn (f) (js-list-filter-loop f arr 0 (list))))
(fn (&rest args)
(let
((f (if (empty? args) :js-undefined (nth args 0)))
(this-arg
(cond
((< (len args) 2) js-global-this)
((js-undefined? (nth args 1)) js-global-this)
((= (nth args 1) nil) js-global-this)
(else (nth args 1)))))
(js-list-filter-loop f arr this-arg 0 (list)))))
((= name "forEach")
(fn
(f)
(begin (js-list-foreach-loop f arr 0) js-undefined)))
(&rest args)
(let
((f (if (empty? args) :js-undefined (nth args 0)))
(this-arg
(cond
((< (len args) 2) js-global-this)
((js-undefined? (nth args 1)) js-global-this)
((= (nth args 1) nil) js-global-this)
(else (nth args 1)))))
(begin (js-list-foreach-loop f arr this-arg 0) js-undefined))))
((= name "reduce")
(fn
(&rest args)
@@ -2337,7 +2364,7 @@
(fn
(f)
(let
((mapped (js-list-map-loop f arr 0 (list))))
((mapped (js-list-map-loop f arr js-global-this 0 (list))))
(js-list-flat-loop mapped 1 (list)))))
((= name "findLast")
(fn (f) (js-list-find-last-loop f arr (- (len arr) 1))))
@@ -2514,35 +2541,42 @@
(define
js-list-map-loop
(fn
(f arr i acc)
(f arr this-arg i acc)
(cond
((>= i (len arr)) acc)
(else
(do
(append! acc (f (nth arr i)))
(js-list-map-loop f arr (+ i 1) acc))))))
(append!
acc
(js-call-with-this this-arg f (list (nth arr i) i arr)))
(js-list-map-loop f arr this-arg (+ i 1) acc))))))
(define
js-list-filter-loop
(fn
(f arr i acc)
(f arr this-arg i acc)
(cond
((>= i (len arr)) acc)
(else
(do
(let
((v (nth arr i)))
(if (js-to-boolean (f v)) (append! acc v) nil))
(js-list-filter-loop f arr (+ i 1) acc))))))
(if
(js-to-boolean (js-call-with-this this-arg f (list v i arr)))
(append! acc v)
nil))
(js-list-filter-loop f arr this-arg (+ i 1) acc))))))
(define
js-list-foreach-loop
(fn
(f arr i)
(f arr this-arg i)
(cond
((>= i (len arr)) nil)
(else
(do (f (nth arr i)) (js-list-foreach-loop f arr (+ i 1)))))))
(do
(js-call-with-this this-arg f (list (nth arr i) i arr))
(js-list-foreach-loop f arr this-arg (+ i 1)))))))
(define
js-list-reduce-loop