From ee422f3d15e2f25fd748e34d4bc668eca02c9086 Mon Sep 17 00:00:00 2001 From: giles Date: Fri, 8 May 2026 16:02:14 +0000 Subject: [PATCH] js-on-sx: Function constructor compiles + evaluates JS source MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Was unconditionally throwing "Function constructor not supported". Now js-function-ctor joins param strings with commas, wraps the body in (function(){}), and runs it through js-eval. Now Function('a', 'b', 'return a + b')(3,4) === 7. built-ins/Function: 0/14 → 4/14. conformance.sh: 148/148. --- lib/js/runtime.sx | 53 ++++++++++++++++++++++++++++- lib/js/test262-scoreboard.json | 62 +++++++++++++++------------------- lib/js/test262-scoreboard.md | 22 ++++++------ plans/js-on-sx.md | 2 ++ 4 files changed, 91 insertions(+), 48 deletions(-) diff --git a/lib/js/runtime.sx b/lib/js/runtime.sx index b952bb7d..bae53331 100644 --- a/lib/js/runtime.sx +++ b/lib/js/runtime.sx @@ -23,7 +23,58 @@ ;; ── Boolean coercion (ToBoolean) ────────────────────────────────── -(define js-function-global {:__callable__ (fn (&rest args) (error "TypeError: Function constructor not supported")) :prototype {:call (fn (&rest args) :js-undefined) :length 0 :bind (fn (&rest args) (fn () :js-undefined)) :toString (fn () "function () { [native code] }") :apply (fn (&rest args) :js-undefined) :name ""}}) +(define js-function-global {:__callable__ (fn (&rest args) (js-function-ctor args)) :prototype {:call (fn (&rest args) :js-undefined) :length 0 :bind (fn (&rest args) (fn () :js-undefined)) :toString (fn () "function () { [native code] }") :apply (fn (&rest args) :js-undefined) :name ""}}) + +(define + js-function-ctor + (fn + (args) + (cond + ((empty? args) (js-eval "(function(){})")) + (else + (let + ((all-strs (js-fn-args-to-strs args)) + (n (len args))) + (let + ((param-strs (js-fn-take-init all-strs)) + (body-str (js-fn-take-last all-strs))) + (js-eval + (str "(function(" (js-fn-join-commas param-strs) "){" body-str "})")))))))) + +(define + js-fn-args-to-strs + (fn + (args) + (cond + ((empty? args) (list)) + (else (cons (js-to-string (first args)) (js-fn-args-to-strs (rest args))))))) + +(define + js-fn-take-init + (fn + (lst) + (cond + ((empty? lst) (list)) + ((empty? (rest lst)) (list)) + (else (cons (first lst) (js-fn-take-init (rest lst))))))) + +(define + js-fn-take-last + (fn + (lst) + (cond + ((empty? lst) "") + ((empty? (rest lst)) (first lst)) + (else (js-fn-take-last (rest lst)))))) + +(define + js-fn-join-commas + (fn + (lst) + (cond + ((empty? lst) "") + ((empty? (rest lst)) (first lst)) + (else (str (first lst) "," (js-fn-join-commas (rest lst))))))) ;; ── Numeric coercion (ToNumber) ─────────────────────────────────── diff --git a/lib/js/test262-scoreboard.json b/lib/js/test262-scoreboard.json index dd60cde0..83a7afcc 100644 --- a/lib/js/test262-scoreboard.json +++ b/lib/js/test262-scoreboard.json @@ -1,61 +1,53 @@ { "totals": { - "pass": 23, - "fail": 21, - "skip": 5, - "timeout": 1, - "total": 50, - "runnable": 45, - "pass_rate": 51.1 + "pass": 4, + "fail": 10, + "skip": 16, + "timeout": 0, + "total": 30, + "runnable": 14, + "pass_rate": 28.6 }, "categories": [ { - "category": "built-ins/Array", - "total": 50, - "pass": 23, - "fail": 21, - "skip": 5, - "timeout": 1, - "pass_rate": 51.1, + "category": "built-ins/Function", + "total": 30, + "pass": 4, + "fail": 10, + "skip": 16, + "timeout": 0, + "pass_rate": 28.6, "top_failures": [ [ - "Test262Error (assertion failed)", - 18 + "SyntaxError (parse/unsupported syntax)", + 4 ], [ - "TypeError: not a function", - 2 + "ReferenceError (undefined symbol)", + 3 ], [ - "Timeout", - 1 - ], - [ - "Unhandled: Not callable: {:2 43 :1 42 :0 41 :length 3}\\", - 1 + "TypeError (other)", + 3 ] ] } ], "top_failure_modes": [ [ - "Test262Error (assertion failed)", - 18 + "SyntaxError (parse/unsupported syntax)", + 4 ], [ - "TypeError: not a function", - 2 + "ReferenceError (undefined symbol)", + 3 ], [ - "Timeout", - 1 - ], - [ - "Unhandled: Not callable: {:2 43 :1 42 :0 41 :length 3}\\", - 1 + "TypeError (other)", + 3 ] ], "pinned_commit": "d5e73fc8d2c663554fb72e2380a8c2bc1a318a33", - "elapsed_seconds": 141.9, + "elapsed_seconds": 11.2, "workers": 1 } \ No newline at end of file diff --git a/lib/js/test262-scoreboard.md b/lib/js/test262-scoreboard.md index 3e7cbec8..22bc7a81 100644 --- a/lib/js/test262-scoreboard.md +++ b/lib/js/test262-scoreboard.md @@ -1,28 +1,26 @@ # test262 scoreboard Pinned commit: `d5e73fc8d2c663554fb72e2380a8c2bc1a318a33` -Wall time: 141.9s +Wall time: 11.2s -**Total:** 23/45 runnable passed (51.1%). Raw: pass=23 fail=21 skip=5 timeout=1 total=50. +**Total:** 4/14 runnable passed (28.6%). Raw: pass=4 fail=10 skip=16 timeout=0 total=30. ## Top failure modes -- **18x** Test262Error (assertion failed) -- **2x** TypeError: not a function -- **1x** Timeout -- **1x** Unhandled: Not callable: {:2 43 :1 42 :0 41 :length 3}\ +- **4x** SyntaxError (parse/unsupported syntax) +- **3x** ReferenceError (undefined symbol) +- **3x** TypeError (other) ## Categories (worst pass-rate first, min 10 runnable) | Category | Pass | Fail | Skip | Timeout | Total | Pass % | |---|---:|---:|---:|---:|---:|---:| -| built-ins/Array | 23 | 21 | 5 | 1 | 50 | 51.1% | +| built-ins/Function | 4 | 10 | 16 | 0 | 30 | 28.6% | ## Per-category top failures (min 10 runnable, worst first) -### built-ins/Array (23/45 — 51.1%) +### built-ins/Function (4/14 — 28.6%) -- **18x** Test262Error (assertion failed) -- **2x** TypeError: not a function -- **1x** Timeout -- **1x** Unhandled: Not callable: {:2 43 :1 42 :0 41 :length 3}\ +- **4x** SyntaxError (parse/unsupported syntax) +- **3x** ReferenceError (undefined symbol) +- **3x** TypeError (other) diff --git a/plans/js-on-sx.md b/plans/js-on-sx.md index 8d712ae9..cd5f0763 100644 --- a/plans/js-on-sx.md +++ b/plans/js-on-sx.md @@ -158,6 +158,8 @@ Each item: implement → tests → update progress. Mark `[x]` when tests green. Append-only record of completed iterations. Loop writes one line per iteration: date, what was done, test count delta. +- 2026-05-08 — **`Function(arg1, arg2, ..., body)` constructor compiles + evaluates JS source.** Was unconditionally throwing `"TypeError: Function constructor not supported"`. Now `js-function-ctor` joins the param strings with commas, wraps the body in `(function(){})`, and runs it through `js-eval`. Side helpers (`js-fn-args-to-strs`, `js-fn-take-init`, `js-fn-take-last`, `js-fn-join-commas`) keep the implementation self-contained and use existing primitives. Now `Function('a', 'b', 'return a + b')(3,4) === 7`. built-ins/Function: 0/14 → 4/14. conformance.sh: 148/148. + - 2026-05-08 — **`arguments` object inside JS functions; `Array.from` calls mapFn correctly.** Three related fixes: (1) Every JS function body now binds `arguments` to `(cons p1 (cons p2 ... __extra_args__))` — a list of all received args, declared and rest. (2) `Array.from(iter, mapFn)` now invokes mapFn through `js-call-with-this` with the index as second arg (was `(map-fn x)` direct, missing index and inheriting outer `this`). (3) Defaults the `thisArg` to `js-global-this` when caller didn't pass one (per non-strict ES). Now `function f() { return arguments[1]; } f(1, 2)` returns 2; `Array.from([1,2,3], (v, i) => v + i*100)` returns `[1, 102, 203]`. conformance.sh: 148/148. - 2026-05-08 — **`String(arr)` consults `Array.prototype.toString` (not the hardcoded join).** Was always emitting the comma-joined elements via `js-list-join`, so user-visible mutations of `Array.prototype.toString` had no effect on `String(arr)` / `"" + arr`. Now look up the override via `js-dict-get-walk` and call it on the list as `this`; fall back to `(js-list-join v ",")` when the override doesn't return a string. Default behaviour preserved (Array.prototype.toString already calls `js-list-join`). built-ins/String fail count: 11 → 9. conformance.sh: 148/148.