From 0b4f5e1df9a61fbbf70507efbff090b0ff931854 Mon Sep 17 00:00:00 2001 From: giles Date: Fri, 8 May 2026 13:55:12 +0000 Subject: [PATCH] js-on-sx: top-level this resolves to the global object MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Per ES non-strict script semantics, top-level this is the global object (window/global/globalThis). Was throwing "Undefined symbol: this". Two-part fix: 1. js-global-this runtime variable set to js-global after globals are defined; js-this falls back to it when no this is active. 2. js-eval wraps transpiled body in (let ((this (js-this))) ...) so JS this resolves to bound this, or top-level to global. Fixes String(this), this.Object === Object, etc. built-ins/Object: 46/50 → 47/50. conformance.sh: 148/148. --- lib/js/runtime.sx | 6 +++- lib/js/test262-scoreboard.json | 54 ++++++++++++++++++++++------------ lib/js/test262-scoreboard.md | 16 ++++++---- lib/js/transpile.sx | 2 +- plans/js-on-sx.md | 2 ++ 5 files changed, 53 insertions(+), 27 deletions(-) diff --git a/lib/js/runtime.sx b/lib/js/runtime.sx index cca6a397..b2cb2f20 100644 --- a/lib/js/runtime.sx +++ b/lib/js/runtime.sx @@ -75,7 +75,9 @@ (if (dict-has? __js_this_cell__ "this") (get __js_this_cell__ "this") - :js-undefined))) + js-global-this))) + +(define js-global-this :js-undefined) (define js-this-set! (fn (v) (dict-set! __js_this_cell__ "this" v))) @@ -4674,3 +4676,5 @@ (dict-set! (get Boolean "prototype") "__js_boolean_value__" false)) (define js-global {:undefined js-undefined :JSON JSON :parseInt parseInt :Object Object :isNaN js-global-is-nan :Infinity inf :NaN 0 :String String :Boolean Boolean :Array Array :Math Math :parseFloat parseFloat :Number Number :console console :isFinite js-global-is-finite}) + +(set! js-global-this js-global) diff --git a/lib/js/test262-scoreboard.json b/lib/js/test262-scoreboard.json index 546be845..e2663cd9 100644 --- a/lib/js/test262-scoreboard.json +++ b/lib/js/test262-scoreboard.json @@ -1,37 +1,53 @@ { "totals": { - "pass": 46, - "fail": 4, - "skip": 0, - "timeout": 0, - "total": 50, - "runnable": 50, - "pass_rate": 92.0 + "pass": 86, + "fail": 11, + "skip": 1, + "timeout": 2, + "total": 100, + "runnable": 99, + "pass_rate": 86.9 }, "categories": [ { - "category": "built-ins/Object", - "total": 50, - "pass": 46, - "fail": 4, - "skip": 0, - "timeout": 0, - "pass_rate": 92.0, + "category": "built-ins/String", + "total": 100, + "pass": 86, + "fail": 11, + "skip": 1, + "timeout": 2, + "pass_rate": 86.9, "top_failures": [ [ - "ReferenceError (undefined symbol)", - 4 + "Test262Error (assertion failed)", + 10 + ], + [ + "Timeout", + 2 + ], + [ + "SyntaxError (parse/unsupported syntax)", + 1 ] ] } ], "top_failure_modes": [ [ - "ReferenceError (undefined symbol)", - 4 + "Test262Error (assertion failed)", + 10 + ], + [ + "Timeout", + 2 + ], + [ + "SyntaxError (parse/unsupported syntax)", + 1 ] ], "pinned_commit": "d5e73fc8d2c663554fb72e2380a8c2bc1a318a33", - "elapsed_seconds": 64.1, + "elapsed_seconds": 179.5, "workers": 1 } \ No newline at end of file diff --git a/lib/js/test262-scoreboard.md b/lib/js/test262-scoreboard.md index a14c2efd..8e7f7df3 100644 --- a/lib/js/test262-scoreboard.md +++ b/lib/js/test262-scoreboard.md @@ -1,22 +1,26 @@ # test262 scoreboard Pinned commit: `d5e73fc8d2c663554fb72e2380a8c2bc1a318a33` -Wall time: 64.1s +Wall time: 179.5s -**Total:** 46/50 runnable passed (92.0%). Raw: pass=46 fail=4 skip=0 timeout=0 total=50. +**Total:** 86/99 runnable passed (86.9%). Raw: pass=86 fail=11 skip=1 timeout=2 total=100. ## Top failure modes -- **4x** ReferenceError (undefined symbol) +- **10x** Test262Error (assertion failed) +- **2x** Timeout +- **1x** SyntaxError (parse/unsupported syntax) ## Categories (worst pass-rate first, min 10 runnable) | Category | Pass | Fail | Skip | Timeout | Total | Pass % | |---|---:|---:|---:|---:|---:|---:| -| built-ins/Object | 46 | 4 | 0 | 0 | 50 | 92.0% | +| built-ins/String | 86 | 11 | 1 | 2 | 100 | 86.9% | ## Per-category top failures (min 10 runnable, worst first) -### built-ins/Object (46/50 — 92.0%) +### built-ins/String (86/99 — 86.9%) -- **4x** ReferenceError (undefined symbol) +- **10x** Test262Error (assertion failed) +- **2x** Timeout +- **1x** SyntaxError (parse/unsupported syntax) diff --git a/lib/js/transpile.sx b/lib/js/transpile.sx index f717625a..d5013408 100644 --- a/lib/js/transpile.sx +++ b/lib/js/transpile.sx @@ -1498,7 +1498,7 @@ (fn (src) (let - ((result (eval-expr (list (quote let) (list) (js-transpile (js-parse (js-tokenize src))))))) + ((result (eval-expr (list (quote let) (list (list (js-sym "this") (list (js-sym "js-this")))) (js-transpile (js-parse (js-tokenize src))))))) (js-drain-microtasks!) result))) diff --git a/plans/js-on-sx.md b/plans/js-on-sx.md index b98c9a51..dfafe0cd 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 — **Top-level `this` resolves to the global object.** Per non-strict ES script semantics, `this` at the top level is the global object (window/global/globalThis). Was throwing "Undefined symbol: this" because the SX let-wrap added by `js-eval` didn't bind `this`. Two-part fix: (1) added `js-global-this` runtime variable, set to `js-global` after globals are defined, with `js-this` falling back to it when no `this` is currently active; (2) `js-eval` wraps the transpiled body in `(let ((this (js-this))) ...)` so the JS-source `this` resolves to the function's bound `this` or, at top level, to the global. Fixes `String(this)`, `this.Object === Object`, etc. built-ins/Object: 46/50 → 47/50. conformance.sh: 148/148. + - 2026-05-08 — **Comma operator `(a, b, c)` parses and evaluates left-to-right, returning last.** Was failing with `Expected punct ')' got punct ','` because `jp-try-arrow-or-paren` only consumed a single assignment expression. Added `jp-parse-comma-seq` / `jp-parse-comma-seq-rest` helpers that build a `js-comma` AST node with the list of expressions; the transpiler emits `(begin ...)` which evaluates each in order and returns the last. Fixes `Object((null,2,3),1,2)`-style tests. built-ins/Object: 44/50 → 46/50. conformance.sh: 148/148. - 2026-05-08 — **ToPrimitive treats functions as non-primitive in `js-to-string` / `js-to-number`.** Per ES, ToPrimitive only accepts strings/numbers/booleans/null/undefined as primitives — objects AND functions must trigger the next conversion step. Was treating function returns from toString/valueOf as primitives (recursing to extract a string), so a `toString` returning a function wouldn't fall through to `valueOf`. Widened the dict-only check to `(or (= type "dict") (js-function? result))` in both ToPrimitive paths. Now `var o = {toString: () => function(){}, valueOf: () => { throw 'x' }}; new String(o)` propagates `'x'` from valueOf. built-ins/String: 85/99 → 86/99. conformance.sh: 148/148.