diff --git a/lib/js/lexer.sx b/lib/js/lexer.sx index 1e72bce1..464c25de 100644 --- a/lib/js/lexer.sx +++ b/lib/js/lexer.sx @@ -29,6 +29,16 @@ (and (>= c "a") (<= c "f")) (and (>= c "A") (<= c "F"))))) +(define + js-hex-value + (fn + (c) + (cond + ((and (>= c "0") (<= c "9")) (- (char-code c) 48)) + ((and (>= c "a") (<= c "f")) (- (char-code c) 87)) + ((and (>= c "A") (<= c "F")) (- (char-code c) 55)) + (else 0)))) + (define js-letter? (fn (c) (or (and (>= c "a") (<= c "z")) (and (>= c "A") (<= c "Z"))))) @@ -37,9 +47,9 @@ (define js-ident-char? (fn (c) (or (js-ident-start? c) (js-digit? c)))) +;; ── Reserved words ──────────────────────────────────────────────── (define js-ws? (fn (c) (or (= c " ") (= c "\t") (= c "\n") (= c "\r")))) -;; ── Reserved words ──────────────────────────────────────────────── (define js-keywords (list @@ -86,15 +96,18 @@ "await" "of")) +;; ── Main tokenizer ──────────────────────────────────────────────── (define js-keyword? (fn (word) (contains? js-keywords word))) -;; ── Main tokenizer ──────────────────────────────────────────────── (define js-tokenize (fn (src) (let - ((tokens (list)) (pos 0) (src-len (len src))) + ((tokens (list)) + (pos 0) + (src-len (len src)) + (nl-before false)) (define js-peek (fn @@ -109,11 +122,7 @@ (let ((sl (len s))) (and (<= (+ pos sl) src-len) (= (slice src pos (+ pos sl)) s))))) - (define - js-emit! - (fn - (type value start) - (append! tokens (js-make-token type value start)))) + (define js-emit! (fn (type value start) (append! tokens {:nl nl-before :type type :value value :pos start}))) (define skip-line-comment! (fn @@ -136,7 +145,13 @@ () (cond ((>= pos src-len) nil) - ((js-ws? (cur)) (do (advance! 1) (skip-ws!))) + ((js-ws? (cur)) + (do + (when + (or (= (cur) "\n") (= (cur) "\r")) + (set! nl-before true)) + (advance! 1) + (skip-ws!))) ((and (= (cur) "/") (< (+ pos 1) src-len) (= (js-peek 1) "/")) (do (advance! 2) (skip-line-comment!) (skip-ws!))) ((and (= (cur) "/") (< (+ pos 1) src-len) (= (js-peek 1) "*")) @@ -254,11 +269,55 @@ ((= ch "b") (append! chars "\\b")) ((= ch "f") (append! chars "\\f")) ((= ch "v") (append! chars "\\v")) + ((= ch "u") + (if + (and + (< (+ pos 4) src-len) + (js-hex-digit? (js-peek 1)) + (js-hex-digit? (js-peek 2)) + (js-hex-digit? (js-peek 3)) + (js-hex-digit? (js-peek 4))) + (do + (append! + chars + (char-from-code + (+ + (* + 4096 + (js-hex-value + (js-peek 1))) + (* + 256 + (js-hex-value + (js-peek 2))) + (* + 16 + (js-hex-value + (js-peek 3))) + (js-hex-value (js-peek 4))))) + (advance! 4)) + (append! chars ch))) + ((= ch "x") + (if + (and + (< (+ pos 2) src-len) + (js-hex-digit? (js-peek 1)) + (js-hex-digit? (js-peek 2))) + (do + (append! + chars + (char-from-code + (+ + (* 16 (js-hex-value (js-peek 1))) + (js-hex-value (js-peek 2))))) + (advance! 2)) + (append! chars ch))) (else (append! chars ch))) (advance! 1)))) (loop))) ((= (cur) quote-char) (advance! 1)) - (else (do (append! chars (cur)) (advance! 1) (loop)))))) + (else + (do (append! chars (cur)) (advance! 1) (loop)))))) (loop) (join "" chars)))) (define @@ -289,7 +348,8 @@ () (cond ((>= pos src-len) nil) - ((and (= (cur) "}") (= depth 1)) (advance! 1)) + ((and (= (cur) "}") (= depth 1)) + (advance! 1)) ((= (cur) "}") (do (append! buf (cur)) @@ -325,7 +385,9 @@ (advance! 1))) (sloop))) ((= (cur) q) - (do (append! buf (cur)) (advance! 1))) + (do + (append! buf (cur)) + (advance! 1))) (else (do (append! buf (cur)) @@ -334,7 +396,10 @@ (sloop) (expr-loop)))) (else - (do (append! buf (cur)) (advance! 1) (expr-loop)))))) + (do + (append! buf (cur)) + (advance! 1) + (expr-loop)))))) (expr-loop) (join "" buf)))) (define @@ -376,14 +441,17 @@ (else (append! chars ch))) (advance! 1)))) (loop))) - (else (do (append! chars (cur)) (advance! 1) (loop)))))) + (else + (do (append! chars (cur)) (advance! 1) (loop)))))) (loop) (flush-chars!) (if (= (len parts) 0) "" (if - (and (= (len parts) 1) (= (nth (nth parts 0) 0) "str")) + (and + (= (len parts) 1) + (= (nth (nth parts 0) 0) "str")) (nth (nth parts 0) 1) parts))))) (define @@ -399,7 +467,7 @@ ((ty (dict-get tk "type")) (vv (dict-get tk "value"))) (cond ((= ty "punct") - (and (not (= vv ")")) (not (= vv "]")))) + (and (not (= vv ")")) (not (= vv "]")) (not (= vv "}")))) ((= ty "op") true) ((= ty "keyword") (contains? @@ -453,9 +521,13 @@ (append! buf (cur)) (advance! 1) (body-loop))) - ((and (= (cur) "/") (not in-class)) (advance! 1)) + ((and (= (cur) "/") (not in-class)) + (advance! 1)) (else - (begin (append! buf (cur)) (advance! 1) (body-loop)))))) + (begin + (append! buf (cur)) + (advance! 1) + (body-loop)))))) (body-loop) (let ((flags-buf (list))) @@ -470,7 +542,7 @@ (advance! 1) (flags-loop))))) (flags-loop) - {:pattern (join "" buf) :flags (join "" flags-buf)})))) + {:flags (join "" flags-buf) :pattern (join "" buf)})))) (define try-op-4! (fn @@ -510,64 +582,113 @@ (fn (start) (cond - ((at? "==") (do (js-emit! "op" "==" start) (advance! 2) true)) - ((at? "!=") (do (js-emit! "op" "!=" start) (advance! 2) true)) - ((at? "<=") (do (js-emit! "op" "<=" start) (advance! 2) true)) - ((at? ">=") (do (js-emit! "op" ">=" start) (advance! 2) true)) - ((at? "&&") (do (js-emit! "op" "&&" start) (advance! 2) true)) - ((at? "||") (do (js-emit! "op" "||" start) (advance! 2) true)) - ((at? "??") (do (js-emit! "op" "??" start) (advance! 2) true)) - ((at? "=>") (do (js-emit! "op" "=>" start) (advance! 2) true)) - ((at? "**") (do (js-emit! "op" "**" start) (advance! 2) true)) - ((at? "<<") (do (js-emit! "op" "<<" start) (advance! 2) true)) - ((at? ">>") (do (js-emit! "op" ">>" start) (advance! 2) true)) - ((at? "++") (do (js-emit! "op" "++" start) (advance! 2) true)) - ((at? "--") (do (js-emit! "op" "--" start) (advance! 2) true)) - ((at? "+=") (do (js-emit! "op" "+=" start) (advance! 2) true)) - ((at? "-=") (do (js-emit! "op" "-=" start) (advance! 2) true)) - ((at? "*=") (do (js-emit! "op" "*=" start) (advance! 2) true)) - ((at? "/=") (do (js-emit! "op" "/=" start) (advance! 2) true)) - ((at? "%=") (do (js-emit! "op" "%=" start) (advance! 2) true)) - ((at? "&=") (do (js-emit! "op" "&=" start) (advance! 2) true)) - ((at? "|=") (do (js-emit! "op" "|=" start) (advance! 2) true)) - ((at? "^=") (do (js-emit! "op" "^=" start) (advance! 2) true)) - ((at? "?.") (do (js-emit! "op" "?." start) (advance! 2) true)) + ((at? "==") + (do (js-emit! "op" "==" start) (advance! 2) true)) + ((at? "!=") + (do (js-emit! "op" "!=" start) (advance! 2) true)) + ((at? "<=") + (do (js-emit! "op" "<=" start) (advance! 2) true)) + ((at? ">=") + (do (js-emit! "op" ">=" start) (advance! 2) true)) + ((at? "&&") + (do (js-emit! "op" "&&" start) (advance! 2) true)) + ((at? "||") + (do (js-emit! "op" "||" start) (advance! 2) true)) + ((at? "??") + (do (js-emit! "op" "??" start) (advance! 2) true)) + ((at? "=>") + (do (js-emit! "op" "=>" start) (advance! 2) true)) + ((at? "**") + (do (js-emit! "op" "**" start) (advance! 2) true)) + ((at? "<<") + (do (js-emit! "op" "<<" start) (advance! 2) true)) + ((at? ">>") + (do (js-emit! "op" ">>" start) (advance! 2) true)) + ((at? "++") + (do (js-emit! "op" "++" start) (advance! 2) true)) + ((at? "--") + (do (js-emit! "op" "--" start) (advance! 2) true)) + ((at? "+=") + (do (js-emit! "op" "+=" start) (advance! 2) true)) + ((at? "-=") + (do (js-emit! "op" "-=" start) (advance! 2) true)) + ((at? "*=") + (do (js-emit! "op" "*=" start) (advance! 2) true)) + ((at? "/=") + (do (js-emit! "op" "/=" start) (advance! 2) true)) + ((at? "%=") + (do (js-emit! "op" "%=" start) (advance! 2) true)) + ((at? "&=") + (do (js-emit! "op" "&=" start) (advance! 2) true)) + ((at? "|=") + (do (js-emit! "op" "|=" start) (advance! 2) true)) + ((at? "^=") + (do (js-emit! "op" "^=" start) (advance! 2) true)) + ((at? "?.") + (do (js-emit! "op" "?." start) (advance! 2) true)) (else false)))) (define emit-one-op! (fn (ch start) (cond - ((= ch "(") (do (js-emit! "punct" "(" start) (advance! 1))) - ((= ch ")") (do (js-emit! "punct" ")" start) (advance! 1))) - ((= ch "[") (do (js-emit! "punct" "[" start) (advance! 1))) - ((= ch "]") (do (js-emit! "punct" "]" start) (advance! 1))) - ((= ch "{") (do (js-emit! "punct" "{" start) (advance! 1))) - ((= ch "}") (do (js-emit! "punct" "}" start) (advance! 1))) - ((= ch ",") (do (js-emit! "punct" "," start) (advance! 1))) - ((= ch ";") (do (js-emit! "punct" ";" start) (advance! 1))) - ((= ch ":") (do (js-emit! "punct" ":" start) (advance! 1))) - ((= ch ".") (do (js-emit! "punct" "." start) (advance! 1))) - ((= ch "?") (do (js-emit! "op" "?" start) (advance! 1))) - ((= ch "+") (do (js-emit! "op" "+" start) (advance! 1))) - ((= ch "-") (do (js-emit! "op" "-" start) (advance! 1))) - ((= ch "*") (do (js-emit! "op" "*" start) (advance! 1))) - ((= ch "/") (do (js-emit! "op" "/" start) (advance! 1))) - ((= ch "%") (do (js-emit! "op" "%" start) (advance! 1))) - ((= ch "=") (do (js-emit! "op" "=" start) (advance! 1))) - ((= ch "<") (do (js-emit! "op" "<" start) (advance! 1))) - ((= ch ">") (do (js-emit! "op" ">" start) (advance! 1))) - ((= ch "!") (do (js-emit! "op" "!" start) (advance! 1))) - ((= ch "&") (do (js-emit! "op" "&" start) (advance! 1))) - ((= ch "|") (do (js-emit! "op" "|" start) (advance! 1))) - ((= ch "^") (do (js-emit! "op" "^" start) (advance! 1))) - ((= ch "~") (do (js-emit! "op" "~" start) (advance! 1))) + ((= ch "(") + (do (js-emit! "punct" "(" start) (advance! 1))) + ((= ch ")") + (do (js-emit! "punct" ")" start) (advance! 1))) + ((= ch "[") + (do (js-emit! "punct" "[" start) (advance! 1))) + ((= ch "]") + (do (js-emit! "punct" "]" start) (advance! 1))) + ((= ch "{") + (do (js-emit! "punct" "{" start) (advance! 1))) + ((= ch "}") + (do (js-emit! "punct" "}" start) (advance! 1))) + ((= ch ",") + (do (js-emit! "punct" "," start) (advance! 1))) + ((= ch ";") + (do (js-emit! "punct" ";" start) (advance! 1))) + ((= ch ":") + (do (js-emit! "punct" ":" start) (advance! 1))) + ((= ch ".") + (do (js-emit! "punct" "." start) (advance! 1))) + ((= ch "?") + (do (js-emit! "op" "?" start) (advance! 1))) + ((= ch "+") + (do (js-emit! "op" "+" start) (advance! 1))) + ((= ch "-") + (do (js-emit! "op" "-" start) (advance! 1))) + ((= ch "*") + (do (js-emit! "op" "*" start) (advance! 1))) + ((= ch "/") + (do (js-emit! "op" "/" start) (advance! 1))) + ((= ch "%") + (do (js-emit! "op" "%" start) (advance! 1))) + ((= ch "=") + (do (js-emit! "op" "=" start) (advance! 1))) + ((= ch "<") + (do (js-emit! "op" "<" start) (advance! 1))) + ((= ch ">") + (do (js-emit! "op" ">" start) (advance! 1))) + ((= ch "!") + (do (js-emit! "op" "!" start) (advance! 1))) + ((= ch "&") + (do (js-emit! "op" "&" start) (advance! 1))) + ((= ch "|") + (do (js-emit! "op" "|" start) (advance! 1))) + ((= ch "^") + (do (js-emit! "op" "^" start) (advance! 1))) + ((= ch "~") + (do (js-emit! "op" "~" start) (advance! 1))) + ((= ch "\\") + (error "Unexpected char '\\' in source")) (else (advance! 1))))) (define scan! (fn () (do + (set! nl-before false) (skip-ws!) (when (< pos src-len) diff --git a/lib/js/parser.sx b/lib/js/parser.sx index a3cc71ce..ae4ab536 100644 --- a/lib/js/parser.sx +++ b/lib/js/parser.sx @@ -153,6 +153,32 @@ (do (jp-advance! st) (list (quote js-ident) "this"))) ((and (= (get t :type) "keyword") (= (get t :value) "new")) (do (jp-advance! st) (jp-parse-new-expr st))) + ((and (= (get t :type) "keyword") (= (get t :value) "function")) + (do + (jp-advance! st) + (let + ((nm + (if + (= (get (jp-peek st) :type) "ident") + (let ((n (get (jp-peek st) :value))) (do (jp-advance! st) n)) + nil))) + (let + ((params (jp-parse-param-list st))) + (let + ((body (jp-parse-fn-body st))) + (list (quote js-funcexpr) nm params body)))))) + ((and (= (get t :type) "keyword") (= (get t :value) "true")) + (do (jp-advance! st) (list (quote js-bool) true))) + ((and (= (get t :type) "keyword") (= (get t :value) "false")) + (do (jp-advance! st) (list (quote js-bool) false))) + ((and (= (get t :type) "keyword") (= (get t :value) "null")) + (do (jp-advance! st) (list (quote js-null)))) + ((and (= (get t :type) "keyword") (= (get t :value) "undefined")) + (do (jp-advance! st) (list (quote js-undef)))) + ((= (get t :type) "number") + (do (jp-advance! st) (list (quote js-num) (get t :value)))) + ((= (get t :type) "string") + (do (jp-advance! st) (list (quote js-str) (get t :value)))) ((and (= (get t :type) "punct") (= (get t :value) "(")) (jp-parse-paren-or-arrow st)) (else @@ -211,7 +237,7 @@ (let ((params (jp-parse-param-list st))) (let - ((body (jp-parse-block st))) + ((body (jp-parse-fn-body st))) (list (quote js-funcexpr-async) nm params body)))))) ((= (get t :type) "ident") (do @@ -363,7 +389,7 @@ (let ((params (jp-parse-param-list st))) (let - ((body (jp-parse-block st))) + ((body (jp-parse-fn-body st))) (list (quote js-funcexpr) nm params body)))))) ((= (get t :type) "ident") (do @@ -418,16 +444,51 @@ (dict-set! st :idx saved) (jp-advance! st) (let - ((e (jp-parse-assignment st))) + ((e (jp-parse-comma-seq st))) (jp-expect! st "punct" ")") - e))) + (jp-paren-wrap e)))) (do (dict-set! st :idx saved) (jp-advance! st) (let - ((e (jp-parse-assignment st))) + ((e (jp-parse-comma-seq st))) (jp-expect! st "punct" ")") - e))))))) + (jp-paren-wrap e)))))))) + +(define + jp-paren-wrap + (fn + (e) + (cond + ((and (list? e) (= (first e) (quote js-unop))) + (list (quote js-paren) e)) + (else e)))) + +(define + jp-parse-comma-seq + (fn + (st) + (let + ((first-expr (jp-parse-assignment st))) + (if + (jp-at? st "punct" ",") + (jp-parse-comma-seq-rest st (list first-expr)) + first-expr)))) + +(define + jp-parse-comma-seq-rest + (fn + (st acc) + (do + (jp-advance! st) + (let + ((next-expr (jp-parse-assignment st))) + (let + ((acc2 (append acc (list next-expr)))) + (if + (jp-at? st "punct" ",") + (jp-parse-comma-seq-rest st acc2) + (cons (quote js-comma) (list acc2)))))))) (define jp-collect-params @@ -485,6 +546,11 @@ (st elems) (cond ((jp-at? st "punct" "]") nil) + ((jp-at? st "punct" ",") + (begin + (append! elems (list (quote js-undef))) + (jp-advance! st) + (jp-array-loop st elems))) (else (begin (cond @@ -558,6 +624,20 @@ (jp-advance! st) (jp-expect! st "punct" ":") (append! kvs {:value (jp-parse-assignment st) :key (get t :value)}))) + ((and (= (get t :type) "punct") (= (get t :value) "[")) + (do + (jp-advance! st) + (let + ((key-expr (jp-parse-assignment st))) + (jp-expect! st "punct" "]") + (jp-expect! st "punct" ":") + (append! + kvs + {:value (jp-parse-assignment st) :computed-key key-expr :key ""})))) + ((and (= (get t :type) "punct") (= (get t :value) "...")) + (do + (jp-advance! st) + (append! kvs {:spread (jp-parse-assignment st)}))) (else (error (str "Unexpected in object: " (get t :type)))))))) (define @@ -629,7 +709,7 @@ st (list (quote js-optchain-member) left (get t :value)))) (error "expected ident, [ or ( after ?."))))))) - ((or (jp-at? st "op" "++") (jp-at? st "op" "--")) + ((and (or (jp-at? st "op" "++") (jp-at? st "op" "--")) (not (jp-token-nl? st))) (let ((op (get (jp-peek st) :value))) (jp-advance! st) @@ -682,6 +762,12 @@ (cond ((< prec 0) left) ((< prec min-prec) left) + ((and (= op "**") (list? left) (= (first left) (quote js-unop))) + (error + (str + "SyntaxError: Unary operator '" + (nth left 1) + "' used immediately before exponentiation expression"))) (else (do (jp-advance! st) @@ -835,6 +921,12 @@ jp-eat-semi (fn (st) (if (jp-at? st "punct" ";") (do (jp-advance! st) nil) nil))) +(define + jp-token-nl? + (fn + (st) + (let ((tok (jp-peek st))) (if tok (= (get tok :nl) true) false)))) + (define jp-parse-vardecl (fn @@ -1052,15 +1144,63 @@ ((c (jp-parse-assignment st))) (do (jp-expect! st "punct" ")") + (jp-disallow-decl-stmt! st "if") (let ((t (jp-parse-stmt st))) (if (jp-at? st "keyword" "else") (do (jp-advance! st) + (jp-disallow-decl-stmt! st "else") (list (quote js-if) c t (jp-parse-stmt st))) (list (quote js-if) c t nil)))))))) +(define + jp-disallow-decl-stmt! + (fn + (st context) + (let + ((t (jp-peek st))) + (cond + ((and (= (get t :type) "keyword") + (or (= (get t :value) "let") + (= (get t :value) "const") + (= (get t :value) "function") + (= (get t :value) "class"))) + (cond + ((and (= (get t :value) "let") + (or (= (get (jp-peek-at st 1) :type) "ident") + (and (= (get (jp-peek-at st 1) :type) "punct") + (or (= (get (jp-peek-at st 1) :value) "[") + (= (get (jp-peek-at st 1) :value) "{"))))) + (error + (str + "SyntaxError: Lexical declaration cannot appear in single-statement context: " + context))) + ((or (= (get t :value) "const") + (= (get t :value) "function") + (= (get t :value) "class")) + (error + (str + "SyntaxError: " + (get t :value) + " declaration cannot appear in single-statement context: " + context))) + (else nil))) + (else nil))))) + +(define + jp-bump! + (fn + (st key) + (dict-set! st key (+ (get st key) 1)))) + +(define + jp-decr! + (fn + (st key) + (dict-set! st key (- (get st key) 1)))) + (define jp-parse-while-stmt (fn @@ -1072,7 +1212,11 @@ ((c (jp-parse-assignment st))) (do (jp-expect! st "punct" ")") - (let ((body (jp-parse-stmt st))) (list (quote js-while) c body))))))) + (jp-disallow-decl-stmt! st "while") + (jp-bump! st :loop-depth) + (let ((body (jp-parse-stmt st))) + (jp-decr! st :loop-depth) + (list (quote js-while) c body))))))) (define jp-parse-do-while-stmt @@ -1080,8 +1224,11 @@ (st) (do (jp-advance! st) + (jp-disallow-decl-stmt! st "do") + (jp-bump! st :loop-depth) (let ((body (jp-parse-stmt st))) + (jp-decr! st :loop-depth) (do (if (jp-at? st "keyword" "while") @@ -1126,8 +1273,11 @@ (let ((iter (jp-parse-assignment st))) (jp-expect! st "punct" ")") + (jp-disallow-decl-stmt! st "for-of/in") + (jp-bump! st :loop-depth) (let ((body (jp-parse-stmt st))) + (jp-decr! st :loop-depth) (list (quote js-for-of-in) iter-kind ident iter body))))))) (else (let @@ -1138,8 +1288,11 @@ (let ((step (if (jp-at? st "punct" ")") nil (jp-parse-assignment st)))) (jp-expect! st "punct" ")") + (jp-disallow-decl-stmt! st "for") + (jp-bump! st :loop-depth) (let ((body (jp-parse-stmt st))) + (jp-decr! st :loop-depth) (list (quote js-for) init cond-ast step body))))))))))) (define @@ -1162,10 +1315,14 @@ (st) (do (jp-advance! st) + (when + (= (get st :fn-depth) 0) + (error "SyntaxError: Illegal return statement")) (if (or (jp-at? st "punct" ";") (jp-at? st "punct" "}") + (jp-token-nl? st) (jp-at? st "eof" nil)) (do (jp-eat-semi st) (list (quote js-return) nil)) (let @@ -1188,7 +1345,7 @@ (let ((params (jp-parse-param-list st))) (let - ((body (jp-parse-block st))) + ((body (jp-parse-fn-body st))) (list (quote js-funcdecl) nm params body)))))))) (define @@ -1207,7 +1364,7 @@ (let ((params (jp-parse-param-list st))) (let - ((body (jp-parse-block st))) + ((body (jp-parse-fn-body st))) (list (quote js-funcdecl-async) nm params body)))))))) (define @@ -1256,7 +1413,7 @@ (let ((params (jp-parse-param-list st))) (let - ((body (jp-parse-block st))) + ((body (jp-parse-fn-body st))) (list (quote js-method) (if static? "static" "instance") @@ -1284,9 +1441,11 @@ ((disc (jp-parse-assignment st))) (jp-expect! st "punct" ")") (jp-expect! st "punct" "{") + (jp-bump! st :switch-depth) (let ((cases (list))) (jp-parse-switch-cases st cases) + (jp-decr! st :switch-depth) (jp-expect! st "punct" "}") (list (quote js-switch) disc cases))))) @@ -1362,9 +1521,40 @@ ((jp-at? st "keyword" "for") (jp-parse-for-stmt st)) ((jp-at? st "keyword" "return") (jp-parse-return-stmt st)) ((jp-at? st "keyword" "break") - (do (jp-advance! st) (jp-eat-semi st) (list (quote js-break)))) + (do + (jp-advance! st) + (cond + ((= (get (jp-peek st) :type) "ident") + (do (jp-advance! st) (jp-eat-semi st) (list (quote js-break)))) + (else + (do + (when + (and (= (get st :loop-depth) 0) (= (get st :switch-depth) 0)) + (error "SyntaxError: Illegal break statement")) + (jp-eat-semi st) + (list (quote js-break))))))) ((jp-at? st "keyword" "continue") - (do (jp-advance! st) (jp-eat-semi st) (list (quote js-continue)))) + (do + (jp-advance! st) + (cond + ((= (get (jp-peek st) :type) "ident") + (do (jp-advance! st) (jp-eat-semi st) (list (quote js-continue)))) + (else + (do + (when + (= (get st :loop-depth) 0) + (error "SyntaxError: Illegal continue statement")) + (jp-eat-semi st) + (list (quote js-continue))))))) + ((and + (= (get (jp-peek st) :type) "ident") + (= (get (jp-peek-at st 1) :type) "punct") + (= (get (jp-peek-at st 1) :value) ":")) + (do + (jp-advance! st) + (jp-advance! st) + (jp-disallow-decl-stmt! st "label") + (jp-parse-stmt st))) ((jp-at? st "keyword" "class") (jp-parse-class-decl st)) ((jp-at? st "keyword" "throw") (jp-parse-throw-stmt st)) ((jp-at? st "keyword" "try") (jp-parse-try-stmt st)) @@ -1374,7 +1564,7 @@ ((jp-at? st "keyword" "switch") (jp-parse-switch-stmt st)) (else (let - ((e (jp-parse-assignment st))) + ((e (jp-parse-comma-seq st))) (do (jp-eat-semi st) (list (quote js-exprstmt) e))))))) (define @@ -1400,10 +1590,33 @@ jp-parse-arrow-body (fn (st) - (if - (jp-at? st "punct" "{") - (jp-parse-block st) - (jp-parse-assignment st)))) + (jp-bump! st :fn-depth) + (let + ((saved-loop (get st :loop-depth)) (saved-switch (get st :switch-depth))) + (dict-set! st :loop-depth 0) + (dict-set! st :switch-depth 0) + (let + ((body (if (jp-at? st "punct" "{") (jp-parse-block st) (jp-parse-assignment st)))) + (jp-decr! st :fn-depth) + (dict-set! st :loop-depth saved-loop) + (dict-set! st :switch-depth saved-switch) + body)))) + +(define + jp-parse-fn-body + (fn + (st) + (jp-bump! st :fn-depth) + (let + ((saved-loop (get st :loop-depth)) (saved-switch (get st :switch-depth))) + (dict-set! st :loop-depth 0) + (dict-set! st :switch-depth 0) + (let + ((body (jp-parse-block st))) + (jp-decr! st :fn-depth) + (dict-set! st :loop-depth saved-loop) + (dict-set! st :switch-depth saved-switch) + body)))) (define js-parse @@ -1414,7 +1627,7 @@ (= (len tokens) 0) (and (= (len tokens) 1) (= (get (nth tokens 0) :type) "eof"))) (list (quote js-program) (list)) - (let ((st {:idx 0 :tokens tokens :arrow-candidate true})) (jp-parse-program st))))) + (let ((st {:idx 0 :tokens tokens :arrow-candidate true :loop-depth 0 :switch-depth 0 :fn-depth 0})) (jp-parse-program st))))) (define js-parse-expr @@ -1427,4 +1640,4 @@ (= (len tokens) 0) (and (= (len tokens) 1) (= (get (nth tokens 0) :type) "eof"))) (list) - (let ((st {:idx 0 :tokens tokens :arrow-candidate true})) (jp-parse-assignment st)))))) + (let ((st {:idx 0 :tokens tokens :arrow-candidate true :loop-depth 0 :switch-depth 0 :fn-depth 0})) (jp-parse-assignment st)))))) diff --git a/lib/js/runtime.sx b/lib/js/runtime.sx index 1872b3e9..a6576ace 100644 --- a/lib/js/runtime.sx +++ b/lib/js/runtime.sx @@ -13,17 +13,68 @@ ;; JS `undefined` — we represent it as a distinct keyword so it ;; survives round-trips through the evaluator without colliding with ;; SX `nil` (which maps to JS `null`). -(define js-nan-value (fn () (/ 0 0))) +(define js-nan-value (fn () nan)) -(define js-infinity-value (fn () (/ 1 0))) +(define js-infinity-value (fn () inf)) ;; ── Type predicates ─────────────────────────────────────────────── -(define js-max-value-approx (fn () (js-max-value-loop 1 2000))) +(define js-max-value-approx (fn () 1.7976931348623157e+308)) ;; ── 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 {:__callable__ (fn (&rest args) (js-invoke-function-method (js-this) "call" args)) :length 1 :name "call"} :length 0 :bind {:__callable__ (fn (&rest args) (js-invoke-function-method (js-this) "bind" args)) :length 1 :name "bind"} :toString {:__callable__ (fn () (js-invoke-function-method (js-this) "toString" (list))) :length 0 :name "toString"} :apply {:__callable__ (fn (&rest args) (js-invoke-function-method (js-this) "apply" args)) :length 2 :name "apply"} :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) ─────────────────────────────────── @@ -37,7 +88,11 @@ ;; per JS (technically ToNumber("") === 0). (define js-global-eval - (fn (&rest args) (if (empty? args) :js-undefined (nth args 0)))) + (fn (&rest args) + (cond + ((empty? args) :js-undefined) + ((not (= (type-of (nth args 0)) "string")) (nth args 0)) + (else (js-eval (nth args 0)))))) ;; Safe number-parser. Tries to call an SX primitive that can parse ;; strings to numbers; on failure returns 0 (stand-in for NaN so the @@ -71,7 +126,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))) @@ -113,16 +170,40 @@ (let ((t (type-of f))) (cond - ((= t "lambda") (js-count-real-params (lambda-params f))) + ((= t "lambda") + (let + ((mapped (js-builtin-fn-length (js-unmap-fn-name (js-extract-fn-name f))))) + (if (>= mapped 0) mapped (js-count-real-params (lambda-params f))))) ((= t "function") 0) ((= t "component") 0) ((and (= t "dict") (contains? (keys f) "__callable__")) (js-fn-length (get f "__callable__"))) (else 0))))) +(define + js-builtin-fn-length + (fn + (name) + (cond + ((= name "fromCharCode") 1) + ((= name "fromCodePoint") 1) + ((= name "raw") 1) + ((= name "of") 0) + ((= name "from") 1) + ((= name "isArray") 1) + ((= name "max") 2) + ((= name "min") 2) + ((= name "hypot") 2) + ((= name "atan2") 2) + ((= name "imul") 2) + ((= name "pow") 2) + (else -1)))) + (define js-extract-fn-name - (fn (f) (let ((raw (inspect f))) (js-strip-fn-name raw 0 (len raw))))) + (fn + (f) + (let ((raw (inspect f))) (js-strip-fn-name raw 0 (len raw))))) (define js-strip-fn-name @@ -172,6 +253,28 @@ ((= name "js-math-sign") "sign") ((= name "js-math-cbrt") "cbrt") ((= name "js-math-hypot") "hypot") + ((= name "js-math-sin") "sin") + ((= name "js-math-cos") "cos") + ((= name "js-math-tan") "tan") + ((= name "js-math-asin") "asin") + ((= name "js-math-acos") "acos") + ((= name "js-math-atan") "atan") + ((= name "js-math-atan2") "atan2") + ((= name "js-math-sinh") "sinh") + ((= name "js-math-cosh") "cosh") + ((= name "js-math-tanh") "tanh") + ((= name "js-math-asinh") "asinh") + ((= name "js-math-acosh") "acosh") + ((= name "js-math-atanh") "atanh") + ((= name "js-math-exp") "exp") + ((= name "js-math-log") "log") + ((= name "js-math-log2") "log2") + ((= name "js-math-log10") "log10") + ((= name "js-math-expm1") "expm1") + ((= name "js-math-log1p") "log1p") + ((= name "js-math-clz32") "clz32") + ((= name "js-math-imul") "imul") + ((= name "js-math-fround") "fround") ((= name "js-number-is-finite") "isFinite") ((= name "js-number-is-nan") "isNaN") ((= name "js-number-is-integer") "isInteger") @@ -223,6 +326,28 @@ 0 (+ 1 (js-count-real-params (rest params))))))))) +(define + js-coerce-this-arg + (fn + (v) + (cond + ((js-undefined? v) js-global-this) + ((= v nil) js-global-this) + ((or (= (type-of v) "number") (= (type-of v) "rational")) + (js-new-call Number (js-args v))) + ((= (type-of v) "string") (js-new-call String (js-args v))) + ((= (type-of v) "boolean") (js-new-call Boolean (js-args v))) + (else v)))) + +(define + js-call-this-coerce + (fn + (recv v) + (cond + ((or (= (type-of recv) "lambda") (= (type-of recv) "component")) + (js-coerce-this-arg v)) + (else v)))) + (define js-invoke-function-method (fn @@ -230,32 +355,53 @@ (cond ((= key "call") (let - ((this-arg (if (< (len args) 1) :js-undefined (nth args 0))) + ((raw-this (if (< (len args) 1) :js-undefined (nth args 0))) (rest (if (< (len args) 1) (list) (js-list-slice args 1 (len args))))) - (js-call-with-this this-arg recv rest))) + (js-call-with-this (js-call-this-coerce recv raw-this) recv rest))) ((= key "apply") (let - ((this-arg (if (< (len args) 1) :js-undefined (nth args 0))) - (arr (if (< (len args) 2) (list) (nth args 1)))) + ((raw-this (if (< (len args) 1) :js-undefined (nth args 0))) + (arr + (if (< (len args) 2) (list) (nth args 1)))) (let ((rest (cond ((= arr nil) (list)) ((js-undefined? arr) (list)) ((list? arr) arr) (else (js-iterable-to-list arr))))) - (js-call-with-this this-arg recv rest)))) + (js-call-with-this (js-call-this-coerce recv raw-this) recv rest)))) ((= key "bind") + (cond + ((not (js-function? recv)) + (raise (js-new-call TypeError (js-args "Function.prototype.bind: target is not callable")))) + (else + (let + ((this-arg (if (< (len args) 1) :js-undefined (nth args 0))) + (bound + (if + (< (len args) 1) + (list) + (js-list-slice args 1 (len args))))) + (let + ((target-len (js-fn-length recv))) + (let + ((bound-len + (let ((d (- target-len (len bound)))) + (if (< d 0) 0 d)))) + {:__callable__ + (fn + (&rest more) + (js-call-with-this this-arg recv (js-list-concat bound more))) + :length bound-len + :name "bound" + :__js_bound_target__ recv})))))) + ((= key "toString") (let - ((this-arg (if (< (len args) 1) :js-undefined (nth args 0))) - (bound - (if - (< (len args) 1) - (list) - (js-list-slice args 1 (len args))))) - (fn - (&rest more) - (js-call-with-this this-arg recv (js-list-concat bound more))))) - ((= key "toString") "function () { [native code] }") + ((override (js-dict-get-walk (get js-function-global "prototype") "toString"))) + (if + (= (type-of override) "lambda") + (js-call-with-this recv override args) + "function () { [native code] }"))) ((= key "name") (js-extract-fn-name recv)) ((= key "length") (js-fn-length recv)) (else :js-undefined)))) @@ -296,11 +442,16 @@ ((= key "toPrecision") (js-to-string recv)) ((= key "toExponential") (js-to-string recv)) (else - (error - (str - "TypeError: " - (js-to-string key) - " is not a function (on number)")))))) + (let + ((m (js-dict-get-walk (get Number "prototype") (js-to-string key)))) + (cond + ((js-undefined? m) + (error + (str + "TypeError: " + (js-to-string key) + " is not a function (on number)"))) + (else (js-call-with-this recv m args)))))))) (define js-invoke-function-objproto @@ -326,20 +477,25 @@ ((= key "toString") (if recv "true" "false")) ((= key "valueOf") recv) (else - (error - (str - "TypeError: " - (js-to-string key) - " is not a function (on boolean)")))))) + (let + ((m (js-dict-get-walk (get Boolean "prototype") (js-to-string key)))) + (cond + ((js-undefined? m) + (error + (str + "TypeError: " + (js-to-string key) + " is not a function (on boolean)"))) + (else (js-call-with-this recv m args)))))))) (define js-num-to-str-radix (fn (n radix) (cond - ((and (number? n) (not (= n n))) "NaN") - ((= n (/ 1 0)) "Infinity") - ((= n (/ -1 0)) "-Infinity") + ((and (number? n) (js-number-is-nan n)) "NaN") + ((= n (js-infinity-value)) "Infinity") + ((= n (- 0 (js-infinity-value))) "-Infinity") ((or (= radix 10) (= radix nil) (js-undefined? radix)) (js-to-string n)) (else @@ -368,7 +524,10 @@ (d) (cond ((< d 10) (js-to-string d)) - (else (let ((offset (+ 97 (- d 10)))) (js-code-to-char offset)))))) + (else + (let + ((offset (+ 97 (- d 10)))) + (js-code-to-char offset)))))) ;; ── Equality ────────────────────────────────────────────────────── @@ -377,40 +536,47 @@ js-number-to-fixed (fn (n digits) - (cond - ((js-number-is-nan n) "NaN") - ((= n (js-infinity-value)) "Infinity") - ((= n (- 0 (js-infinity-value))) "-Infinity") - (else - (let - ((d (js-math-trunc digits))) - (if - (< d 1) - (js-to-string (js-math-round n)) - (let - ((scale (js-pow-int 10 d))) + (let + ((d (js-math-trunc (js-to-number digits)))) + (cond + ((or (js-number-is-nan d) (< d 0) (> d 100)) + (raise + (js-new-call RangeError + (js-args "toFixed() digits argument must be between 0 and 100")))) + ((js-number-is-nan n) "NaN") + ((= n (js-infinity-value)) "Infinity") + ((= n (- 0 (js-infinity-value))) "-Infinity") + ((or (>= n 1e21) (<= n -1e21)) (js-number-to-string n)) + (else + (cond + ((< d 1) (js-to-string (js-math-round n))) + (else (let - ((scaled (js-math-round (* n scale)))) + ((scale (js-pow-int 10 d))) (let - ((abs-scaled (if (< scaled 0) (- 0 scaled) scaled)) - (sign (if (< scaled 0) "-" ""))) + ((scaled (js-math-round (* n scale)))) (let - ((int-part (js-math-trunc (/ abs-scaled scale))) - (frac-part - (- - abs-scaled - (* (js-math-trunc (/ abs-scaled scale)) scale)))) - (str - sign - (js-to-string int-part) - "." - (js-pad-int-str - (js-to-string (js-math-trunc frac-part)) - d)))))))))))) + ((abs-scaled (if (< scaled 0) (- 0 scaled) scaled)) + (sign (if (< scaled 0) "-" ""))) + (let + ((int-part (js-math-trunc (/ abs-scaled scale))) + (frac-part + (- + abs-scaled + (* (js-math-trunc (/ abs-scaled scale)) scale)))) + (str + sign + (js-to-string int-part) + "." + (js-pad-int-str + (js-to-string (js-math-trunc frac-part)) + d))))))))))))) (define js-pow-int - (fn (b e) (if (<= e 0) 1 (* b (js-pow-int b (- e 1)))))) + (fn + (b e) + (if (<= e 0) 1 (* b (js-pow-int b (- e 1)))))) ;; Abstract equality (==): type coercion rules. ;; Simplified: number↔string coerce both to number; null == undefined; @@ -425,30 +591,44 @@ (fn-val args) (let ((callable (if (and (dict? fn-val) (contains? (keys fn-val) "__callable__")) (get fn-val "__callable__") fn-val))) - (cond - ((= (len args) 0) (callable)) - ((= (len args) 1) (callable (nth args 0))) - ((= (len args) 2) (callable (nth args 0) (nth args 1))) - ((= (len args) 3) - (callable (nth args 0) (nth args 1) (nth args 2))) - ((= (len args) 4) - (callable (nth args 0) (nth args 1) (nth args 2) (nth args 3))) - ((= (len args) 5) - (callable - (nth args 0) - (nth args 1) - (nth args 2) - (nth args 3) - (nth args 4))) - ((= (len args) 6) - (callable - (nth args 0) - (nth args 1) - (nth args 2) - (nth args 3) - (nth args 4) - (nth args 5))) - (else (apply callable args)))))) + (if + (not (js-function? callable)) + (raise + (js-new-call + TypeError + (list (str (type-of fn-val) " is not a function")))) + (cond + ((= (len args) 0) (callable)) + ((= (len args) 1) (callable (nth args 0))) + ((= (len args) 2) + (callable (nth args 0) (nth args 1))) + ((= (len args) 3) + (callable + (nth args 0) + (nth args 1) + (nth args 2))) + ((= (len args) 4) + (callable + (nth args 0) + (nth args 1) + (nth args 2) + (nth args 3))) + ((= (len args) 5) + (callable + (nth args 0) + (nth args 1) + (nth args 2) + (nth args 3) + (nth args 4))) + ((= (len args) 6) + (callable + (nth args 0) + (nth args 1) + (nth args 2) + (nth args 3) + (nth args 4) + (nth args 5))) + (else (apply callable args))))))) ;; ── Relational comparisons ──────────────────────────────────────── @@ -608,7 +788,7 @@ (fn-val args) (cond ((js-undefined? fn-val) - (error "TypeError: undefined is not a function")) + (raise (js-new-call TypeError (list "undefined is not a function")))) ((and (dict? fn-val) (contains? (keys fn-val) "__callable__")) (js-call-with-this :js-undefined (get fn-val "__callable__") args)) (else (js-call-with-this :js-undefined fn-val args))))) @@ -617,16 +797,22 @@ js-new-call (fn (ctor args) - (let - ((obj (dict))) - (begin - (dict-set! obj "__proto__" (js-get-ctor-proto ctor)) + (cond + ((not (js-function? ctor)) + (raise (js-new-call TypeError (list (str (type-of ctor) " is not a constructor"))))) + (else (let - ((ret (js-call-with-this obj ctor args))) - (if - (and (not (js-undefined? ret)) (= (type-of ret) "dict")) - ret - obj)))))) + ((obj (dict))) + (begin + (dict-set! obj "__proto__" (js-get-ctor-proto ctor)) + (let + ((ret (js-call-with-this obj ctor args))) + (if + (and + (not (js-undefined? ret)) + (or (= (type-of ret) "dict") (= (type-of ret) "list") (js-function? ret))) + ret + obj)))))))) ;; Setter — mutates the dict. Returns the new value (JS assignment yields rhs). (define @@ -634,9 +820,29 @@ (fn (obj ctor) (cond - ((not (= (type-of obj) "dict")) false) ((not (js-function? ctor)) (error "TypeError: Right-hand side of instanceof is not callable")) + ((js-function? obj) + (let + ((proto (js-get-ctor-proto ctor)) + (fnproto (get js-function-global "prototype")) + (objproto (get Object "prototype"))) + (cond + ((= proto fnproto) true) + ((= proto objproto) true) + ((and (= (type-of obj) "dict") (contains? (keys obj) "__proto__")) + (js-instanceof-walk obj proto)) + (else false)))) + ((list? obj) + (let + ((proto (js-get-ctor-proto ctor)) + (arrproto (get Array "prototype")) + (objproto (get Object "prototype"))) + (cond + ((= proto arrproto) true) + ((= proto objproto) true) + (else false)))) + ((not (= (type-of obj) "dict")) false) (else (let ((proto (js-get-ctor-proto ctor))) @@ -682,128 +888,693 @@ ((dict-has? obj "__proto__") (js-in-walk (get obj "__proto__") skey)) (else false)))) +(define + js-error-init! + (fn + (this name args) + (begin + (dict-set! + this + "message" + (if (= (len args) 0) "" (js-to-string (nth args 0)))) + (dict-set! this "name" name) + (dict-set! this "__js_error_data__" true) + this))) + +(define + js-error-receiver + (fn + (ctor) + (let + ((this (js-this))) + (cond + ((= (type-of this) "dict") this) + (else (js-new-call ctor (list))))))) + (define Error (fn (&rest args) - (let - ((this (js-this))) - (begin - (if - (= (type-of this) "dict") - (do - (dict-set! - this - "message" - (if (= (len args) 0) "" (js-to-string (nth args 0)))) - (dict-set! this "name" "Error")) - nil) - this)))) + (js-error-init! (js-error-receiver Error) "Error" args))) + +(define + js-error-is-error + (fn + (&rest args) + (cond + ((= (len args) 0) false) + (else + (let + ((v (nth args 0))) + (and + (= (type-of v) "dict") + (or + (contains? (keys v) "__js_error_data__") + (js-error-proto-walk? v)))))))) + +(define + js-error-proto-walk? + (fn + (v) + (cond + ((not (= (type-of v) "dict")) false) + ((contains? (keys v) "__js_error_data__") true) + ((not (contains? (keys v) "__proto__")) false) + (else (js-error-proto-walk? (get v "__proto__")))))) ;; ── Math object ─────────────────────────────────────────────────── (define TypeError - (fn - (&rest args) - (let - ((this (js-this))) - (begin - (if - (= (type-of this) "dict") - (do - (dict-set! - this - "message" - (if (= (len args) 0) "" (js-to-string (nth args 0)))) - (dict-set! this "name" "TypeError")) - nil) - this)))) + (fn (&rest args) + (js-error-init! (js-error-receiver TypeError) "TypeError" args))) (define RangeError - (fn - (&rest args) - (let - ((this (js-this))) - (begin - (if - (= (type-of this) "dict") - (do - (dict-set! - this - "message" - (if (= (len args) 0) "" (js-to-string (nth args 0)))) - (dict-set! this "name" "RangeError")) - nil) - this)))) + (fn (&rest args) + (js-error-init! (js-error-receiver RangeError) "RangeError" args))) (define SyntaxError - (fn - (&rest args) - (let - ((this (js-this))) - (begin - (if - (= (type-of this) "dict") - (do - (dict-set! - this - "message" - (if (= (len args) 0) "" (js-to-string (nth args 0)))) - (dict-set! this "name" "SyntaxError")) - nil) - this)))) + (fn (&rest args) + (js-error-init! (js-error-receiver SyntaxError) "SyntaxError" args))) (define ReferenceError - (fn - (&rest args) - (let - ((this (js-this))) - (begin - (if - (= (type-of this) "dict") - (do - (dict-set! - this - "message" - (if (= (len args) 0) "" (js-to-string (nth args 0)))) - (dict-set! this "name" "ReferenceError")) - nil) - this)))) + (fn (&rest args) + (js-error-init! (js-error-receiver ReferenceError) "ReferenceError" args))) (define URIError - (fn - (&rest args) - (let - ((this (js-this))) - (begin - (if - (= this :js-undefined) - nil - (do - (dict-set! - this - "message" - (if (empty? args) "" (js-to-string (nth args 0)))) - (dict-set! this "name" "URIError"))) - this)))) + (fn (&rest args) + (js-error-init! (js-error-receiver URIError) "URIError" args))) (define EvalError + (fn (&rest args) + (js-error-init! (js-error-receiver EvalError) "EvalError" args))) + +(define AggregateError :js-undefined) + +(define SuppressedError :js-undefined) + +(define + RegExp + {:length 2 + :name "RegExp" + :__callable__ + (fn + (&rest args) + (let + ((this (js-this))) + (let + ((pattern-arg (if (= (len args) 0) "" (nth args 0))) + (flags-arg + (if (>= (len args) 2) (nth args 1) :js-undefined))) + (let + ((src + (cond + ((js-regex? pattern-arg) (get pattern-arg "source")) + ((js-undefined? pattern-arg) "") + ((= pattern-arg nil) "") + (else (js-to-string pattern-arg)))) + (fl + (cond + ((js-undefined? flags-arg) + (if (js-regex? pattern-arg) (get pattern-arg "flags") "")) + ((= flags-arg nil) "") + (else (js-to-string flags-arg))))) + (let + ((rx (js-regex-new src fl))) + (cond + ((not (= (type-of this) "dict")) rx) + (else + (begin + (for-each + (fn (k) (dict-set! this k (get rx k))) + (keys rx)) + this)))))))) + :prototype + {:test + (fn (s) + (let ((rx (js-this)) (str (js-to-string s))) + (js-regex-stub-test rx str))) + :exec + (fn (s) + (let ((rx (js-this)) (str (js-to-string s))) + (js-regex-stub-exec rx str))) + :toString + (fn () + (let ((rx (js-this))) + (str "/" (get rx "source") "/" (get rx "flags")))) + :compile + (fn (&rest args) + (let ((rx (js-this))) + (cond + ((>= (len args) 1) + (let + ((src (js-to-string (nth args 0))) + (fl (if (>= (len args) 2) (js-to-string (nth args 1)) ""))) + (let + ((rx2 (js-regex-new src fl))) + (begin + (for-each + (fn (k) (dict-set! rx k (get rx2 k))) + (keys rx2)) + rx)))) + (else rx))))}}) + +(define + js-str-startswith? (fn - (&rest args) + (s prefix) + (cond + ((< (len s) (len prefix)) false) + (else (= (js-string-slice s 0 (len prefix)) prefix))))) + +(define + js-wrap-exn + (fn + (e) + (cond + ((not (= (type-of e) "string")) e) + ((js-str-startswith? e "Undefined symbol:") + (js-new-call ReferenceError (js-args e))) + ((js-str-startswith? e "TypeError:") + (js-new-call TypeError (js-args (js-string-slice e 11 (len e))))) + ((js-str-startswith? e "RangeError:") + (js-new-call RangeError (js-args (js-string-slice e 12 (len e))))) + ((js-str-startswith? e "SyntaxError:") + (js-new-call SyntaxError (js-args (js-string-slice e 13 (len e))))) + ((js-str-startswith? e "ReferenceError:") + (js-new-call ReferenceError (js-args (js-string-slice e 16 (len e))))) + ((js-str-startswith? e "URIError:") + (js-new-call URIError (js-args (js-string-slice e 10 (len e))))) + (else e)))) + +(define + js-date-setter-arg + (fn + (args fallback i) + (cond + ((>= (len args) (+ i 1)) (js-to-number (nth args i))) + (else fallback)))) + +(define + js-date-setter + (fn + (d field args) + (cond + ((or (not (dict? d)) (not (contains? (keys d) "__js_is_date__"))) + (raise (js-new-call TypeError (js-args "this is not a Date object")))) + (else + (let + ((ms-raw (get d "__date_value__"))) + (let + ((ms-orig + (cond + ((or (= ms-raw nil) (js-undefined? ms-raw)) (js-nan-value)) + ((= (type-of ms-raw) "rational") (exact->inexact ms-raw)) + (else ms-raw)))) + (let + ((parts (js-date-decompose ms-orig))) + (let + ((y + (cond + ((= field "fullYear") (js-date-setter-arg args (js-nan-value) 0)) + (else (nth parts 0)))) + (mo + (cond + ((= field "fullYear") (js-date-setter-arg args (nth parts 1) 1)) + ((= field "month") (js-date-setter-arg args (js-nan-value) 0)) + (else (nth parts 1)))) + (da + (cond + ((= field "fullYear") (js-date-setter-arg args (nth parts 2) 2)) + ((= field "month") (js-date-setter-arg args (nth parts 2) 1)) + ((= field "date") (js-date-setter-arg args (js-nan-value) 0)) + (else (nth parts 2)))) + (hh + (cond + ((= field "hours") (js-date-setter-arg args (js-nan-value) 0)) + (else (nth parts 3)))) + (mm + (cond + ((= field "hours") (js-date-setter-arg args (nth parts 4) 1)) + ((= field "minutes") (js-date-setter-arg args (js-nan-value) 0)) + (else (nth parts 4)))) + (ss + (cond + ((= field "hours") (js-date-setter-arg args (nth parts 5) 2)) + ((= field "minutes") (js-date-setter-arg args (nth parts 5) 1)) + ((= field "seconds") (js-date-setter-arg args (js-nan-value) 0)) + (else (nth parts 5)))) + (msv + (cond + ((= field "hours") (js-date-setter-arg args (nth parts 6) 3)) + ((= field "minutes") (js-date-setter-arg args (nth parts 6) 2)) + ((= field "seconds") (js-date-setter-arg args (nth parts 6) 1)) + ((= field "ms") (js-date-setter-arg args (js-nan-value) 0)) + (else (nth parts 6))))) + (cond + ((or (js-number-is-nan y) (js-number-is-nan mo) (js-number-is-nan da) + (js-number-is-nan hh) (js-number-is-nan mm) (js-number-is-nan ss) (js-number-is-nan msv)) + (begin (dict-set! d "__date_value__" (js-nan-value)) (js-nan-value))) + (else + (let + ((days (js-date-civil-to-days (js-num-to-int y) (+ (js-num-to-int mo) 1) (js-num-to-int da))) + (tod + (+ + (* (js-num-to-int hh) 3600000) + (* (js-num-to-int mm) 60000) + (* (js-num-to-int ss) 1000) + (js-num-to-int msv)))) + (let + ((new-ms (+ (* days 86400000) tod))) + (cond + ((or (> new-ms 8640000000000000) (< new-ms -8640000000000000)) + (begin (dict-set! d "__date_value__" (js-nan-value)) (js-nan-value))) + (else + (begin (dict-set! d "__date_value__" new-ms) new-ms))))))))))))))) + +(define + js-date-decompose + (fn + (ms) + (cond + ((or (= ms nil) (js-undefined? ms) (not (number? ms)) (js-number-is-nan ms)) + (list 1970 0 1 0 0 0 0)) + (else + (let + ((days (floor (/ ms 86400000))) + (tod + (let ((m (modulo (js-num-to-int ms) 86400000))) + (if (< m 0) (+ m 86400000) m)))) + (let + ((ymd (js-date-days-to-ymd days))) + (list + (nth ymd 0) + (- (nth ymd 1) 1) + (nth ymd 2) + (js-math-trunc (/ tod 3600000)) + (js-math-trunc (/ (modulo tod 3600000) 60000)) + (js-math-trunc (/ (modulo tod 60000) 1000)) + (modulo tod 1000)))))))) + +(define + js-date-time-value + (fn + (d) + (cond + ((or (not (dict? d)) (not (contains? (keys d) "__js_is_date__"))) + (raise (js-new-call TypeError (js-args "this is not a Date object")))) + (else (get d "__date_value__"))))) + +(define + js-date-getter + (fn + (d field) (let - ((this (js-this))) - (begin - (if - (= this :js-undefined) - nil - (do - (dict-set! - this - "message" - (if (empty? args) "" (js-to-string (nth args 0)))) - (dict-set! this "name" "EvalError"))) - this)))) + ((ms-raw (js-date-time-value d))) + (let + ((ms (if (= (type-of ms-raw) "rational") (exact->inexact ms-raw) ms-raw))) + (cond + ((or (= ms nil) (js-undefined? ms) (not (number? ms))) + (js-nan-value)) + ((js-number-is-nan ms) (js-nan-value)) + (else + (let + ((days (floor (/ ms 86400000))) + (tod + (let ((m (modulo (js-num-to-int ms) 86400000))) + (if (< m 0) (+ m 86400000) m)))) + (cond + ((= field "ms") (modulo tod 1000)) + ((= field "seconds") (js-math-trunc (/ (modulo tod 60000) 1000))) + ((= field "minutes") (js-math-trunc (/ (modulo tod 3600000) 60000))) + ((= field "hours") (js-math-trunc (/ tod 3600000))) + ((= field "day") + (let ((dow (modulo (+ days 4) 7))) + (if (< dow 0) (+ dow 7) dow))) + (else + (let ((ymd (js-date-days-to-ymd days))) + (cond + ((= field "year") (nth ymd 0)) + ((= field "month") (- (nth ymd 1) 1)) + ((= field "date") (nth ymd 2)) + (else (js-nan-value))))))))))))) + +(define + js-date-from-one + (fn + (v) + (cond + ((number? v) v) + ((= (type-of v) "rational") (exact->inexact v)) + ((= (type-of v) "string") (js-date-parse-string v)) + ((js-undefined? v) (js-nan-value)) + (else (js-to-number v))))) + +(define + js-date-parse-string + (fn + (s) + (cond + ((>= (len s) 4) + (let + ((year-part (js-string-slice s 0 4))) + (cond + ((js-is-numeric-string? year-part) + (let + ((y (js-num-to-int (js-string-to-number year-part)))) + (* (- y 1970) 31557600000))) + (else 0)))) + (else 0)))) + +(define + js-date-from-parts + (fn + (args) + (let + ((year-raw (js-num-to-int (js-to-number (nth args 0))))) + (let + ((year (if (and (>= year-raw 0) (<= year-raw 99)) (+ year-raw 1900) year-raw)) + (month + (if (>= (len args) 2) (js-num-to-int (js-to-number (nth args 1))) 0)) + (day + (if (>= (len args) 3) (js-num-to-int (js-to-number (nth args 2))) 1)) + (hour + (if (>= (len args) 4) (js-num-to-int (js-to-number (nth args 3))) 0)) + (mins + (if (>= (len args) 5) (js-num-to-int (js-to-number (nth args 4))) 0)) + (secs + (if (>= (len args) 6) (js-num-to-int (js-to-number (nth args 5))) 0)) + (ms + (if (>= (len args) 7) (js-num-to-int (js-to-number (nth args 6))) 0))) + (let + ((days (js-date-civil-to-days year (+ month 1) day))) + (+ + (* days 86400000) + (* hour 3600000) + (* mins 60000) + (* secs 1000) + ms)))))) + +(define + js-date-args-have-nan? + (fn + (args i) + (cond + ((>= i (len args)) false) + (else + (let + ((n (js-to-number (nth args i)))) + (cond + ((or (number? n) (= (type-of n) "rational")) + (let + ((nf (if (= (type-of n) "rational") (exact->inexact n) n))) + (cond + ((js-number-is-nan nf) true) + ((= nf (js-infinity-value)) true) + ((= nf (- 0 (js-infinity-value))) true) + (else (js-date-args-have-nan? args (+ i 1)))))) + (else (js-date-args-have-nan? args (+ i 1))))))))) + +(define + js-date-civil-to-days + (fn + (y m d) + (let + ((y2 (if (<= m 2) (- y 1) y))) + (let + ((era (if (>= y2 0) (js-math-trunc (/ y2 400)) (js-math-trunc (/ (- y2 399) 400))))) + (let + ((yoe (- y2 (* era 400)))) + (let + ((doy + (+ + (js-math-trunc (/ (+ (* 153 (if (> m 2) (- m 3) (+ m 9))) 2) 5)) + (- d 1)))) + (let + ((doe + (+ + (* yoe 365) + (+ + (- (js-math-trunc (/ yoe 4)) (js-math-trunc (/ yoe 100))) + doy)))) + (+ (* era 146097) (- doe 719468))))))))) + +(define + js-date-format-now + (fn () "[Date stub]")) + +(define + js-date-pad2 + (fn (n) (if (< n 10) (str "0" (js-to-string n)) (js-to-string n)))) + +(define + js-date-pad3 + (fn + (n) + (cond + ((< n 10) (str "00" (js-to-string n))) + ((< n 100) (str "0" (js-to-string n))) + (else (js-to-string n))))) + +(define + Date + {:length 7 + :name "Date" + :__callable__ + (fn + (&rest args) + (let + ((this (js-this))) + (cond + ((not (= (type-of this) "dict")) (js-date-format-now)) + (else + (begin + (dict-set! + this + "__date_value__" + (cond + ((= (len args) 0) 0) + ((= (len args) 1) (js-date-from-one (nth args 0))) + ((js-date-args-have-nan? args 0) (js-nan-value)) + (else (js-date-from-parts args)))) + (dict-set! this "__js_is_date__" true) + this))))) + :now (fn () 0) + :parse (fn (s) (js-date-parse-string (js-to-string s))) + :UTC + (fn + (&rest args) + (cond + ((= (len args) 0) (js-nan-value)) + ((js-date-args-have-nan? args 0) (js-nan-value)) + (else + (let + ((ms (js-date-from-parts args))) + (cond + ((or (> ms 8640000000000000) (< ms -8640000000000000)) + (js-nan-value)) + (else ms)))))) + :prototype + {:getTime (fn () (js-date-time-value (js-this))) + :valueOf (fn () (js-date-time-value (js-this))) + :getFullYear (fn () (js-date-getter (js-this) "year")) + :getUTCFullYear (fn () (js-date-getter (js-this) "year")) + :getMonth (fn () (js-date-getter (js-this) "month")) + :getUTCMonth (fn () (js-date-getter (js-this) "month")) + :getDate (fn () (js-date-getter (js-this) "date")) + :getUTCDate (fn () (js-date-getter (js-this) "date")) + :getDay (fn () (js-date-getter (js-this) "day")) + :getUTCDay (fn () (js-date-getter (js-this) "day")) + :getHours (fn () (js-date-getter (js-this) "hours")) + :getUTCHours (fn () (js-date-getter (js-this) "hours")) + :getMinutes (fn () (js-date-getter (js-this) "minutes")) + :getUTCMinutes (fn () (js-date-getter (js-this) "minutes")) + :getSeconds (fn () (js-date-getter (js-this) "seconds")) + :getUTCSeconds (fn () (js-date-getter (js-this) "seconds")) + :getMilliseconds (fn () (js-date-getter (js-this) "ms")) + :getUTCMilliseconds (fn () (js-date-getter (js-this) "ms")) + :getTimezoneOffset (fn () 0) + :setTime + (fn (v) + (let ((t (js-this))) + (let + ((n (js-to-number v))) + (cond + ((or (js-number-is-nan n) (> n 8640000000000000) (< n -8640000000000000)) + (begin (dict-set! t "__date_value__" (js-nan-value)) (js-nan-value))) + (else (begin (dict-set! t "__date_value__" n) n)))))) + :setFullYear (fn (&rest args) (js-date-setter (js-this) "fullYear" args)) + :setUTCFullYear (fn (&rest args) (js-date-setter (js-this) "fullYear" args)) + :setMonth (fn (&rest args) (js-date-setter (js-this) "month" args)) + :setUTCMonth (fn (&rest args) (js-date-setter (js-this) "month" args)) + :setDate (fn (&rest args) (js-date-setter (js-this) "date" args)) + :setUTCDate (fn (&rest args) (js-date-setter (js-this) "date" args)) + :setHours (fn (&rest args) (js-date-setter (js-this) "hours" args)) + :setUTCHours (fn (&rest args) (js-date-setter (js-this) "hours" args)) + :setMinutes (fn (&rest args) (js-date-setter (js-this) "minutes" args)) + :setUTCMinutes (fn (&rest args) (js-date-setter (js-this) "minutes" args)) + :setSeconds (fn (&rest args) (js-date-setter (js-this) "seconds" args)) + :setUTCSeconds (fn (&rest args) (js-date-setter (js-this) "seconds" args)) + :setMilliseconds (fn (&rest args) (js-date-setter (js-this) "ms" args)) + :setUTCMilliseconds (fn (&rest args) (js-date-setter (js-this) "ms" args)) + :toISOString (fn () (js-date-iso (js-this))) + :toJSON (fn () (js-date-iso (js-this))) + :toString (fn () (js-date-iso (js-this))) + :toUTCString (fn () (js-date-iso (js-this))) + :toDateString (fn () (js-date-iso (js-this))) + :toTimeString (fn () "00:00:00 GMT+0000") + :toLocaleString (fn () (js-date-iso (js-this))) + :toLocaleDateString (fn () (js-date-iso (js-this))) + :toLocaleTimeString (fn () "00:00:00")}}) + +(define + js-date-iso + (fn + (d) + (cond + ((or (not (dict? d)) (not (contains? (keys d) "__js_is_date__"))) + (raise (js-new-call TypeError (js-args "this is not a Date object")))) + (else + (let + ((ms-raw (get d "__date_value__"))) + (let + ((ms (if (= (type-of ms-raw) "rational") (exact->inexact ms-raw) ms-raw))) + (cond + ((or (= ms nil) (js-undefined? ms)) + (raise (js-new-call RangeError (js-args "Invalid time value")))) + ((not (number? ms)) + (raise (js-new-call RangeError (js-args "Invalid time value")))) + ((js-number-is-nan ms) + (raise (js-new-call RangeError (js-args "Invalid time value")))) + ((or (> ms 8640000000000000) (< ms -8640000000000000)) + (raise (js-new-call RangeError (js-args "Invalid time value")))) + (else (js-date-iso-format ms))))))))) + +(define + js-date-iso-format + (fn + (ms) + (let + ((day-ms 86400000) (sec-ms 1000) (min-ms 60000) (hr-ms 3600000)) + (let + ((days (floor (/ ms day-ms))) + (time-of-day + (let ((m (modulo (js-num-to-int ms) day-ms))) + (if (< m 0) (+ m day-ms) m)))) + (let + ((hh (js-math-trunc (/ time-of-day hr-ms))) + (mm (js-math-trunc (/ (modulo time-of-day hr-ms) min-ms))) + (ss (js-math-trunc (/ (modulo time-of-day min-ms) sec-ms))) + (msec (modulo time-of-day sec-ms)) + (ymd (js-date-days-to-ymd days))) + (let + ((y (nth ymd 0)) (mo (nth ymd 1)) (d (nth ymd 2))) + (str + (js-date-iso-year y) + "-" + (js-pad2 mo) + "-" + (js-pad2 d) + "T" + (js-pad2 hh) + ":" + (js-pad2 mm) + ":" + (js-pad2 ss) + "." + (js-pad3 msec) + "Z"))))))) + +(define + js-date-iso-year + (fn + (y) + (cond + ((or (< y 0) (> y 9999)) + (let + ((sign (if (< y 0) "-" "+")) + (ay (if (< y 0) (- 0 y) y))) + (str sign (js-date-year-6 ay)))) + ((< y 10) (str "000" (js-to-string y))) + ((< y 100) (str "00" (js-to-string y))) + ((< y 1000) (str "0" (js-to-string y))) + (else (js-to-string y))))) + +(define + js-date-year-6 + (fn + (y) + (cond + ((< y 10) (str "00000" (js-to-string y))) + ((< y 100) (str "0000" (js-to-string y))) + ((< y 1000) (str "000" (js-to-string y))) + ((< y 10000) (str "00" (js-to-string y))) + ((< y 100000) (str "0" (js-to-string y))) + (else (js-to-string y))))) + +(define js-pad2 (fn (n) (if (< n 10) (str "0" (js-to-string n)) (js-to-string n)))) + +(define + js-pad3 + (fn + (n) + (cond + ((< n 10) (str "00" (js-to-string n))) + ((< n 100) (str "0" (js-to-string n))) + (else (js-to-string n))))) + +(define + js-date-days-to-ymd + (fn + (days-since-epoch) + (let + ((d (+ days-since-epoch 719468))) + (let + ((era (if (>= d 0) (js-math-trunc (/ d 146097)) (js-math-trunc (/ (- d 146096) 146097))))) + (let + ((doe (- d (* era 146097)))) + (let + ((yoe + (js-math-trunc + (/ + (- + (+ + (- doe (js-math-trunc (/ doe 1460))) + (js-math-trunc (/ doe 36524))) + (js-math-trunc (/ doe 146096))) + 365)))) + (let + ((y (+ yoe (* era 400))) + (doy + (- + doe + (+ + (* yoe 365) + (- + (js-math-trunc (/ yoe 4)) + (js-math-trunc (/ yoe 100))))))) + (let + ((mp (js-math-trunc (/ (+ (* 5 doy) 2) 153)))) + (let + ((day (- (+ doy 1) (js-math-trunc (/ (+ (* 153 mp) 2) 5)))) + (month (if (< mp 10) (+ mp 3) (- mp 9)))) + (list + (if (<= month 2) (+ y 1) y) + month + day)))))))))) + +(define + js-date-year-pad + (fn + (y) + (cond + ((>= y 0) + (cond + ((< y 10) (str "000" (js-to-string y))) + ((< y 100) (str "00" (js-to-string y))) + ((< y 1000) (str "0" (js-to-string y))) + (else (js-to-string y)))) + (else (str "-" (js-date-year-pad (- 0 y))))))) (define js-function? (fn @@ -839,7 +1610,11 @@ (else (let ((p (dict))) - (begin (dict-set! __js_proto_table__ id p) p))))))))) + (begin + (dict-set! p "__proto__" (get Object "prototype")) + (dict-set! p "constructor" ctor) + (dict-set! __js_proto_table__ id p) + p))))))))) (define js-reset-ctor-proto! @@ -873,6 +1648,7 @@ ((= v nil) "object") ((= (type-of v) "boolean") "boolean") ((= (type-of v) "number") "number") + ((= (type-of v) "rational") "number") ((= (type-of v) "string") "string") ((= (type-of v) "lambda") "function") ((= (type-of v) "function") "function") @@ -881,6 +1657,37 @@ "function") (else "object")))) +(define + js-object-tostring-class + (fn + (v) + (cond + ((js-undefined? v) "[object Undefined]") + ((= v nil) "[object Null]") + ((= (type-of v) "list") "[object Array]") + ((= (type-of v) "string") "[object String]") + ((= (type-of v) "number") "[object Number]") + ((= (type-of v) "rational") "[object Number]") + ((= (type-of v) "boolean") "[object Boolean]") + ((or (= (type-of v) "lambda") (= (type-of v) "function") (= (type-of v) "component")) + "[object Function]") + ((= (type-of v) "dict") + (cond + ((contains? (keys v) "__callable__") "[object Function]") + ((contains? (keys v) "__js_string_value__") "[object String]") + ((contains? (keys v) "__js_number_value__") "[object Number]") + ((contains? (keys v) "__js_boolean_value__") "[object Boolean]") + ((contains? (keys v) "__js_error_data__") "[object Error]") + ((contains? (keys v) "__js_is_date__") "[object Date]") + ((contains? (keys v) "__map_keys__") "[object Map]") + ((contains? (keys v) "__set_items__") "[object Set]") + ((= v (get Number "prototype")) "[object Number]") + ((= v (get String "prototype")) "[object String]") + ((= v (get Boolean "prototype")) "[object Boolean]") + ((= v (get Array "prototype")) "[object Array]") + (else "[object Object]"))) + (else "[object Object]")))) + (define js-to-boolean (fn @@ -890,6 +1697,7 @@ ((= v nil) false) ((= v false) false) ((= v 0) false) + ((and (= (type-of v) "number") (js-number-is-nan v)) false) ((= v "") false) (else true)))) @@ -903,7 +1711,41 @@ ((= v true) 1) ((= v false) 0) ((= (type-of v) "number") v) + ((= (type-of v) "rational") (exact->inexact v)) ((= (type-of v) "string") (js-string-to-number v)) + ((or (= (type-of v) "lambda") (= (type-of v) "function") (= (type-of v) "component")) + (js-nan-value)) + ((= (type-of v) "list") (if (= (len v) 0) 0 (if (= (len v) 1) (js-to-number (nth v 0)) (js-nan-value)))) + ((= (type-of v) "dict") + (cond + ((contains? (keys v) "__js_number_value__") + (get v "__js_number_value__")) + ((contains? (keys v) "__js_boolean_value__") + (if (get v "__js_boolean_value__") 1 0)) + ((contains? (keys v) "__js_string_value__") + (js-string-to-number (get v "__js_string_value__"))) + (else + (let + ((valueof-fn (js-get-prop v "valueOf"))) + (if + (= (type-of valueof-fn) "lambda") + (let + ((result (js-call-with-this v valueof-fn ()))) + (if + (and (not (= (type-of result) "dict")) (not (js-function? result))) + (js-to-number result) + (let + ((tostr-fn (js-get-prop v "toString"))) + (if + (= (type-of tostr-fn) "lambda") + (let + ((result2 (js-call-with-this v tostr-fn ()))) + (if + (and (not (= (type-of result2) "dict")) (not (js-function? result2))) + (js-to-number result2) + (raise (js-new-call TypeError (list "Cannot convert object to primitive value"))))) + (js-nan-value))))) + (js-nan-value)))))) (else 0)))) (define @@ -970,7 +1812,9 @@ (define js-parse-num-safe (fn (s) (cond (else (js-num-from-string s))))) -(define js-find-exp-char (fn (s) (js-find-exp-char-loop s 0 (len s)))) +(define + js-find-exp-char + (fn (s) (js-find-exp-char-loop s 0 (len s)))) (define js-find-exp-char-loop @@ -1010,7 +1854,8 @@ ((c (char-at s i)) (d (js-hex-digit-value (char-at s i)))) (cond ((< d 0) (js-nan-value)) - (else (js-parse-hex s (+ i 1) (+ (* acc 16) d))))))))) + (else + (js-parse-hex s (+ i 1) (+ (* acc 16) d))))))))) (define js-hex-digit-value @@ -1043,21 +1888,55 @@ ((trimmed (js-trim s))) (cond ((= trimmed "") 0) - ((js-hex-prefix? trimmed) (js-parse-hex trimmed 2 0)) + ((js-hex-prefix? trimmed) + (js-parse-hex trimmed 2 0)) (else (let ((esplit (js-find-exp-char trimmed))) (if (>= esplit 0) (let - ((mant (js-string-slice trimmed 0 esplit)) - (expstr - (js-string-slice trimmed (+ esplit 1) (len trimmed)))) - (let - ((m (js-parse-decimal mant 0 0 1 false 0)) - (e (js-parse-decimal expstr 0 0 1 false 0))) - (* m (js-pow-int 10 e)))) - (js-parse-decimal trimmed 0 0 1 false 0)))))))) + ((parsed (string->number trimmed))) + (if + (= parsed nil) + (let + ((mant (js-string-slice trimmed 0 esplit)) + (expstr + (js-string-slice + trimmed + (+ esplit 1) + (len trimmed)))) + (let + ((m (js-parse-decimal mant 0 0 1 false 0)) + (e + (js-parse-decimal + expstr + 0 + 0 + 1 + false + 0))) + (* m (pow 10 e)))) + parsed)) + (js-parse-decimal + trimmed + 0 + 0 + 1 + false + 0)))))))) + +(define js-args (fn (&rest args) args)) + +(define + js-make-list + (fn + (&rest args) + (let + ((r (list))) + (begin + (for-each (fn (x) (append! r x)) args) + r)))) (define js-trim (fn (s) (js-trim-left (js-trim-right s)))) @@ -1084,12 +1963,32 @@ (s n) (cond ((<= n 0) "") - ((js-is-space? (char-at s (- n 1))) (js-trim-right-at s (- n 1))) + ((js-is-space? (char-at s (- n 1))) + (js-trim-right-at s (- n 1))) (else (substr s 0 n))))) (define js-is-space? - (fn (c) (or (= c " ") (= c "\t") (= c "\n") (= c "\r")))) + (fn + (c) + (let + ((cc (char-code c))) + (or + (= cc 9) + (= cc 10) + (= cc 11) + (= cc 12) + (= cc 13) + (= cc 32) + (= cc 160) + (= cc 5760) + (and (>= cc 8192) (<= cc 8202)) + (= cc 8232) + (= cc 8233) + (= cc 8239) + (= cc 8287) + (= cc 12288) + (= cc 65279))))) (define js-parse-decimal @@ -1100,9 +1999,21 @@ (cond ((>= i n) (* sign (if frac? (/ acc fdiv) acc))) ((and (= i 0) (= (char-at s 0) "-")) - (js-parse-decimal s 1 0 -1 false 0)) + (js-parse-decimal + s + 1 + 0 + -1 + false + 0)) ((and (= i 0) (= (char-at s 0) "+")) - (js-parse-decimal s 1 0 1 false 0)) + (js-parse-decimal + s + 1 + 0 + 1 + false + 0)) ((= (char-at s i) ".") (js-parse-decimal s (+ i 1) acc sign true 1)) ((js-is-digit? (char-at s i)) @@ -1169,7 +2080,63 @@ ((= v false) "false") ((= (type-of v) "string") v) ((= (type-of v) "number") (js-number-to-string v)) - (else (str v))))) + ((= (type-of v) "rational") (js-number-to-string (exact->inexact v))) + (else + (if + (= (type-of v) "dict") + (cond + ((contains? (keys v) "__js_string_value__") + (get v "__js_string_value__")) + ((contains? (keys v) "__js_number_value__") + (js-number-to-string (get v "__js_number_value__"))) + ((contains? (keys v) "__js_boolean_value__") + (if (get v "__js_boolean_value__") "true" "false")) + (else + (let + ((tostr-fn (js-get-prop v "toString"))) + (if + (= (type-of tostr-fn) "lambda") + (let + ((result (js-call-with-this v tostr-fn ()))) + (if + (or (= (type-of result) "dict") (js-function? result)) + (let + ((valueof-fn (js-get-prop v "valueOf"))) + (if + (= (type-of valueof-fn) "lambda") + (let + ((result2 (js-call-with-this v valueof-fn ()))) + (if + (or (= (type-of result2) "dict") (js-function? result2)) + (raise + (js-new-call + TypeError + (list + "Cannot convert object to primitive value"))) + (js-to-string result2))) + "[object Object]")) + (js-to-string result))) + "[object Object]")))) + (cond + ((= (type-of v) "list") + (let + ((tostr-fn (js-dict-get-walk (get Array "prototype") "toString"))) + (if + (= (type-of tostr-fn) "lambda") + (let + ((result (js-call-with-this v tostr-fn ()))) + (if (= (type-of result) "string") result (js-list-join v ","))) + (js-list-join v ",")))) + ((js-function? v) + (let + ((tostr-fn (js-dict-get-walk (get js-function-global "prototype") "toString"))) + (if + (= (type-of tostr-fn) "lambda") + (let + ((result (js-call-with-this v tostr-fn ()))) + (if (= (type-of result) "string") result "function () { [native code] }")) + "function () { [native code] }"))) + (else (str v)))))))) (define js-template-concat @@ -1187,6 +2154,89 @@ (+ i 1) (str acc (js-to-string (nth parts i))))))) +(define + js-big-int-str-loop + (fn + (n acc) + (if + (< n 1) + (if (= acc "") "0" acc) + (let + ((d (floor (- n (* 10 (floor (/ n 10))))))) + (js-big-int-str-loop + (floor (/ n 10)) + (str (js-string-slice "0123456789" d (+ d 1)) acc)))))) + +(define + js-find-decimal-k + (fn + (n k) + (if + (> k 17) + 17 + (let + ((big-int (round (* n (js-pow-int 10 k))))) + (if + (= (/ big-int (js-pow-int 10 k)) n) + k + (js-find-decimal-k n (+ k 1))))))) + +(define + js-format-decimal-digits + (fn + (digits k) + (if + (= k 0) + digits + (let + ((dlen (len digits))) + (if + (> dlen k) + (str + (js-string-slice digits 0 (- dlen k)) + "." + (js-string-slice digits (- dlen k) dlen)) + (if + (= dlen k) + (str "0." digits) + (str "0." (js-string-repeat "0" (- k dlen)) digits))))))) + +(define + js-expand-sci-notation + (fn + (mant exp-n) + (let + ((di (js-string-index-of mant "." 0))) + (let + ((int-part (if (< di 0) mant (js-string-slice mant 0 di))) + (frac-part + (if + (< di 0) + "" + (js-string-slice mant (+ di 1) (len mant))))) + (let + ((all-digits (str int-part frac-part)) + (frac-len + (if + (< di 0) + 0 + (- (- (len mant) di) 1)))) + (if + (>= exp-n 0) + (if + (>= exp-n frac-len) + (str all-digits (js-string-repeat "0" (- exp-n frac-len))) + (let + ((dot-pos (+ (len int-part) exp-n))) + (str + (js-string-slice all-digits 0 dot-pos) + "." + (js-string-slice all-digits dot-pos (len all-digits))))) + (str + "0." + (js-string-repeat "0" (- (- 0 exp-n) 1)) + all-digits))))))) + (define js-number-to-string (fn @@ -1195,7 +2245,16 @@ ((js-number-is-nan n) "NaN") ((= n (js-infinity-value)) "Infinity") ((= n (- 0 (js-infinity-value))) "-Infinity") - (else (js-normalize-num-str (str n)))))) + (else + (let + ((pos-n (if (< n 0) (- 0 n) n))) + (let + ((s0 (js-normalize-num-str (str pos-n)))) + (let + ((n2 (js-to-number s0))) + (let + ((precise (if (= n2 pos-n) (let ((ei (js-string-index-of s0 "e" 0))) (if (< ei 0) s0 (let ((exp-n (js-to-number (js-string-slice s0 (+ ei 1) (len s0))))) (if (and (>= exp-n -6) (<= exp-n 20)) (js-expand-sci-notation (js-string-slice s0 0 ei) exp-n) (if (>= exp-n 0) (str (js-string-slice s0 0 (+ ei 1)) "+" (str exp-n)) s0))))) (if (and (>= pos-n 1e-06) (< pos-n 1e+21)) (let ((k (js-find-decimal-k pos-n 0))) (let ((big-int (round (* pos-n (js-pow-int 10 k))))) (js-format-decimal-digits (js-big-int-str-loop big-int "") k))) (let ((ei (js-string-index-of s0 "e" 0))) (if (< ei 0) s0 (let ((exp-n (js-to-number (js-string-slice s0 (+ ei 1) (len s0))))) (if (>= exp-n 0) (str (js-string-slice s0 0 (+ ei 1)) "+" (str exp-n)) s0)))))))) + (if (< n 0) (str "-" precise) precise))))))))) (define js-normalize-num-str @@ -1218,7 +2277,8 @@ (let ((sign-and-body (js-split-sign s))) (let - ((sign (nth sign-and-body 0)) (body (nth sign-and-body 1))) + ((sign (nth sign-and-body 0)) + (body (nth sign-and-body 1))) (let ((stripped (js-strip-zeros-loop body 0 (len body)))) (if (= stripped "") (str sign "0") (str sign stripped))))))) @@ -1229,8 +2289,10 @@ (s) (cond ((= s "") (list "" "")) - ((= (char-at s 0) "-") (list "-" (js-string-slice s 1 (len s)))) - ((= (char-at s 0) "+") (list "" (js-string-slice s 1 (len s)))) + ((= (char-at s 0) "-") + (list "-" (js-string-slice s 1 (len s)))) + ((= (char-at s 0) "+") + (list "" (js-string-slice s 1 (len s)))) (else (list "" s))))) (define @@ -1242,32 +2304,171 @@ ((= (char-at s i) "0") (js-strip-zeros-loop s (+ i 1) n)) (else (js-string-slice s i n))))) +(define + js-add-unwrap + (fn + (v) + (cond + ((or (= (type-of v) "lambda") (= (type-of v) "function") (= (type-of v) "component")) + (let ((s (js-to-string v))) s)) + ((not (= (type-of v) "dict")) v) + ((contains? (keys v) "__js_string_value__") + (get v "__js_string_value__")) + ((contains? (keys v) "__js_number_value__") + (get v "__js_number_value__")) + ((contains? (keys v) "__js_boolean_value__") + (get v "__js_boolean_value__")) + ((contains? (keys v) "__js_is_date__") + (js-add-call-method v "toString")) + (else (js-add-toprim-default v))))) + +(define + js-add-toprim-default + (fn + (v) + (let + ((via-valueof (js-add-call-method v "valueOf"))) + (cond + ((not (= (type-of via-valueof) "dict")) via-valueof) + (else (js-add-call-method v "toString")))))) + +(define + js-add-call-method + (fn + (v name) + (let + ((m (js-dict-get-walk v name))) + (cond + ((js-undefined? m) v) + ((not (js-function? m)) v) + (else (js-call-with-this v m (list))))))) + (define js-add (fn (a b) - (cond - ((or (= (type-of a) "string") (= (type-of b) "string")) - (str (js-to-string a) (js-to-string b))) - (else (+ (js-to-number a) (js-to-number b)))))) + (let + ((ap (js-add-unwrap a)) (bp (js-add-unwrap b))) + (cond + ((or (= (type-of ap) "string") (= (type-of bp) "string")) + (str (js-to-string ap) (js-to-string bp))) + (else (+ (js-to-number ap) (js-to-number bp))))))) (define js-sub (fn (a b) (- (js-to-number a) (js-to-number b)))) (define js-mul (fn (a b) (* (js-to-number a) (js-to-number b)))) -(define js-div (fn (a b) (/ (js-to-number a) (js-to-number b)))) +(define + js-div + (fn (a b) (/ (js-to-number a) (exact->inexact (js-to-number b))))) (define js-mod (fn (a b) (mod (js-to-number a) (js-to-number b)))) -(define js-pow (fn (a b) (pow (js-to-number a) (js-to-number b)))) +(define + js-unsigned-rshift + (fn + (l r) + (let + ((lu32 (modulo (js-math-trunc (js-to-number l)) 4294967296)) + (shift (modulo (js-math-trunc (js-to-number r)) 32))) + (floor (/ lu32 (js-math-pow 2 shift)))))) -(define js-neg (fn (a) (- 0 (js-to-number a)))) +(define + js-to-uint32 + (fn (n) (modulo (js-math-trunc (js-to-number n)) 4294967296))) + +(define + js-uint32-to-int32 + (fn (u) (if (>= u 2147483648) (- u 4294967296) u))) + +(define js-to-int32 (fn (n) (js-uint32-to-int32 (js-to-uint32 n)))) + +(define + js-bitwise-loop + (fn + (op au bu i acc bit) + (if + (>= i 32) + acc + (let + ((abit (modulo (floor (/ au bit)) 2)) + (bbit (modulo (floor (/ bu bit)) 2))) + (let + ((rbit + (cond + ((= op "and") (* abit bbit)) + ((= op "or") (if (or (= abit 1) (= bbit 1)) 1 0)) + ((= op "xor") (if (= abit bbit) 0 1)) + (else 0)))) + (js-bitwise-loop + op au bu (+ i 1) (+ acc (* rbit bit)) (* bit 2))))))) + +(define + js-bitwise-binop + (fn + (op a b) + (js-uint32-to-int32 + (js-bitwise-loop op (js-to-uint32 a) (js-to-uint32 b) 0 0 1)))) + +(define js-bitand (fn (a b) (js-bitwise-binop "and" a b))) + +(define js-bitor (fn (a b) (js-bitwise-binop "or" a b))) + +(define js-bitxor (fn (a b) (js-bitwise-binop "xor" a b))) + +(define + js-shl + (fn + (a b) + (let + ((au (js-to-uint32 a)) + (sh (modulo (js-math-trunc (js-to-number b)) 32))) + (js-uint32-to-int32 (modulo (* au (js-math-pow 2 sh)) 4294967296))))) + +(define + js-shr + (fn + (a b) + (let + ((ai (js-to-int32 a)) + (sh (modulo (js-math-trunc (js-to-number b)) 32))) + (if (= sh 0) ai (floor (/ ai (js-math-pow 2 sh))))))) + +(define + js-pow-spec + (fn + (b e) + (let + ((bn (js-to-number b)) (en (js-to-number e))) + (let + ((inf (js-infinity-value)) (abs-b (if (< bn 0) (- 0 bn) bn))) + (cond + ((js-number-is-nan en) (js-nan-value)) + ((= en 0) 1) + ((js-number-is-nan bn) (js-nan-value)) + ((and (= abs-b 1) (or (= en inf) (= en (- 0 inf)))) + (js-nan-value)) + (else (pow bn en))))))) + +(define js-pow (fn (a b) (js-pow-spec a b))) + +(define js-neg (fn (a) (* -1 (exact->inexact (js-to-number a))))) (define js-pos (fn (a) (js-to-number a))) (define js-not (fn (a) (not (js-to-boolean a)))) -(define js-bitnot (fn (a) (- 0 (+ (js-num-to-int (js-to-number a)) 1)))) +(define + js-bitnot + (fn (a) (- 0 (+ (js-num-to-int (js-to-number a)) 1)))) + +(define + js-numeric-type? + (fn (v) (or (= (type-of v) "number") (= (type-of v) "rational")))) + +(define + js-numeric-norm + (fn (v) (if (= (type-of v) "rational") (exact->inexact v) v))) (define js-strict-eq @@ -1276,6 +2477,10 @@ (cond ((and (js-undefined? a) (js-undefined? b)) true) ((or (js-undefined? a) (js-undefined? b)) false) + ((and (js-numeric-type? a) (js-numeric-type? b)) + (let + ((an (js-numeric-norm a)) (bn (js-numeric-norm b))) + (if (or (js-number-is-nan an) (js-number-is-nan bn)) false (= an bn)))) ((not (= (type-of a) (type-of b))) false) (else (if (or (js-number-is-nan a) (js-number-is-nan b)) false (= a b)))))) @@ -1290,12 +2495,30 @@ ((js-strict-eq a b) true) ((and (= a nil) (js-undefined? b)) true) ((and (js-undefined? a) (= b nil)) true) - ((and (= (type-of a) "number") (= (type-of b) "string")) - (= a (js-to-number b))) - ((and (= (type-of a) "string") (= (type-of b) "number")) - (= (js-to-number a) b)) + ((and (js-numeric-type? a) (= (type-of b) "string")) + (let ((an (js-numeric-norm a)) (bn (js-to-number b))) + (cond + ((or (js-number-is-nan an) (js-number-is-nan bn)) false) + (else (= an bn))))) + ((and (= (type-of a) "string") (js-numeric-type? b)) + (let ((an (js-to-number a)) (bn (js-numeric-norm b))) + (cond + ((or (js-number-is-nan an) (js-number-is-nan bn)) false) + (else (= an bn))))) ((= (type-of a) "boolean") (js-loose-eq (js-to-number a) b)) ((= (type-of b) "boolean") (js-loose-eq a (js-to-number b))) + ((and (dict? a) (contains? (keys a) "__js_string_value__")) + (js-loose-eq (get a "__js_string_value__") b)) + ((and (dict? b) (contains? (keys b) "__js_string_value__")) + (js-loose-eq a (get b "__js_string_value__"))) + ((and (dict? a) (contains? (keys a) "__js_number_value__")) + (js-loose-eq (get a "__js_number_value__") b)) + ((and (dict? b) (contains? (keys b) "__js_number_value__")) + (js-loose-eq a (get b "__js_number_value__"))) + ((and (dict? a) (contains? (keys a) "__js_boolean_value__")) + (js-loose-eq (get a "__js_boolean_value__") b)) + ((and (dict? b) (contains? (keys b) "__js_boolean_value__")) + (js-loose-eq a (get b "__js_boolean_value__"))) (else false)))) (define js-loose-neq (fn (a b) (not (js-loose-eq a b)))) @@ -1304,18 +2527,41 @@ js-lt (fn (a b) - (cond - ((and (= (type-of a) "string") (= (type-of b) "string")) - (js-str-lt a b)) - (else (< (js-to-number a) (js-to-number b)))))) + (let + ((ap (js-add-unwrap a)) (bp (js-add-unwrap b))) + (cond + ((and (= (type-of ap) "string") (= (type-of bp) "string")) + (js-str-lt ap bp)) + (else + (let + ((an (js-to-number ap)) (bn (js-to-number bp))) + (cond + ((or (js-number-is-nan an) (js-number-is-nan bn)) false) + (else (< an bn))))))))) (define js-gt (fn (a b) (js-lt b a))) -(define js-le (fn (a b) (not (js-lt b a)))) +(define + js-le + (fn + (a b) + (let + ((ap (js-add-unwrap a)) (bp (js-add-unwrap b))) + (cond + ((and (= (type-of ap) "string") (= (type-of bp) "string")) + (or (js-str-lt ap bp) (= ap bp))) + (else + (let + ((an (js-to-number ap)) (bn (js-to-number bp))) + (cond + ((or (js-number-is-nan an) (js-number-is-nan bn)) false) + (else (<= an bn))))))))) -(define js-ge (fn (a b) (not (js-lt a b)))) +(define js-ge (fn (a b) (js-le b a))) -(define js-str-lt (fn (a b) (js-str-lt-at a b 0 (len a) (len b)))) +(define + js-str-lt + (fn (a b) (js-str-lt-at a b 0 (len a) (len b)))) (define js-str-lt-at @@ -1376,7 +2622,10 @@ (js-list-index-of arr (nth args 0) - (if (< (len args) 2) 0 (js-num-to-int (nth args 1))))))) + (if + (< (len args) 2) + 0 + (js-num-to-int (nth args 1))))))) ((= name "join") (fn (&rest args) @@ -1384,10 +2633,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 "filter") (fn (f) (js-list-filter-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 (&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))) + (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))))) + (begin (js-list-foreach-loop f arr this-arg 0) js-undefined)))) ((= name "reduce") (fn (&rest args) @@ -1396,19 +2675,70 @@ (if (= (len arr) 0) (error "Reduce of empty array with no initial value") - (js-list-reduce-loop (nth args 0) (nth arr 0) arr 1))) - (else (js-list-reduce-loop (nth args 0) (nth args 1) arr 0))))) + (js-list-reduce-loop + (nth args 0) + (nth arr 0) + arr + 1))) + (else + (js-list-reduce-loop + (nth args 0) + (nth args 1) + arr + 0))))) ((= name "includes") (fn (&rest args) (if (= (len args) 0) false - (>= (js-list-index-of arr (nth args 0) 0) 0)))) - ((= name "find") (fn (f) (js-list-find-loop f arr 0))) - ((= name "findIndex") (fn (f) (js-list-find-index-loop f arr 0))) - ((= name "some") (fn (f) (js-list-some-loop f arr 0))) - ((= name "every") (fn (f) (js-list-every-loop f arr 0))) + (>= + (js-list-index-of arr (nth args 0) 0) + 0)))) + ((= name "find") + (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-find-loop f arr this-arg 0)))) + ((= name "findIndex") + (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-find-index-loop f arr this-arg 0)))) + ((= name "some") + (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-some-loop f arr this-arg 0)))) + ((= name "every") + (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-every-loop f arr this-arg 0)))) ((= name "reverse") (fn () (js-list-reverse-loop arr (- (len arr) 1) (list)))) ((= name "flat") @@ -1422,7 +2752,11 @@ (&rest args) (let ((v (if (= (len args) 0) js-undefined (nth args 0))) - (s (if (< (len args) 2) 0 (js-num-to-int (nth args 1)))) + (s + (if + (< (len args) 2) + 0 + (js-num-to-int (nth args 1)))) (e (if (< (len args) 3) @@ -1466,7 +2800,11 @@ (&rest args) (let ((n (len arr)) - (start-raw (if (empty? args) 0 (js-num-to-int (nth args 0))))) + (start-raw + (if + (empty? args) + 0 + (js-num-to-int (nth args 0))))) (let ((start (cond ((< start-raw 0) (max 0 (+ n start-raw))) ((> start-raw n) n) (else start-raw)))) (let @@ -1476,12 +2814,14 @@ (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)))) ((= name "findLastIndex") - (fn (f) (js-list-find-last-index-loop f arr (- (len arr) 1)))) + (fn + (f) + (js-list-find-last-index-loop f arr (- (len arr) 1)))) ((= name "reduceRight") (fn (&rest args) @@ -1522,9 +2862,15 @@ (let ((n (len arr)) (target-raw - (if (empty? args) 0 (js-num-to-int (nth args 0)))) + (if + (empty? args) + 0 + (js-num-to-int (nth args 0)))) (start-raw - (if (< (len args) 2) 0 (js-num-to-int (nth args 1)))) + (if + (< (len args) 2) + 0 + (js-num-to-int (nth args 1)))) (end-raw (if (< (len args) 3) @@ -1534,7 +2880,8 @@ ((target (cond ((< target-raw 0) (max 0 (+ n target-raw))) (else (min n target-raw)))) (start (cond - ((< start-raw 0) (max 0 (+ n start-raw))) + ((< start-raw 0) + (max 0 (+ n start-raw))) (else (min n start-raw)))) (end (cond @@ -1564,7 +2911,11 @@ ((n (len arr))) (let ((s (if (< start 0) (max 0 (+ n start)) (min start n))) - (e (if (< stop 0) (max 0 (+ n stop)) (min stop n)))) + (e + (if + (< stop 0) + (max 0 (+ n stop)) + (min stop n)))) (js-list-slice-loop arr s e (list)))))) (define @@ -1594,7 +2945,11 @@ (cond ((= (len arr) 0) "") (else - (js-list-join-loop arr sep 1 (js-to-string-for-join (nth arr 0))))))) + (js-list-join-loop + arr + sep + 1 + (js-to-string-for-join (nth arr 0))))))) (define js-to-string-for-join @@ -1636,34 +2991,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))))))) + (else + (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 @@ -1671,34 +3034,42 @@ (f acc arr i) (cond ((>= i (len arr)) acc) - (else (js-list-reduce-loop f (f acc (nth arr i)) arr (+ i 1)))))) + (else + (js-list-reduce-loop + f + (js-call-with-this js-undefined f (list acc (nth arr i) i arr)) + arr + (+ i 1)))))) (define js-list-find-loop (fn - (f arr i) + (f arr this-arg i) (cond ((>= i (len arr)) js-undefined) - ((js-to-boolean (f (nth arr i))) (nth arr i)) - (else (js-list-find-loop f arr (+ i 1)))))) + ((js-to-boolean (js-call-with-this this-arg f (list (nth arr i) i arr))) + (nth arr i)) + (else (js-list-find-loop f arr this-arg (+ i 1)))))) (define js-list-find-index-loop (fn - (f arr i) + (f arr this-arg i) (cond ((>= i (len arr)) -1) - ((js-to-boolean (f (nth arr i))) i) - (else (js-list-find-index-loop f arr (+ i 1)))))) + ((js-to-boolean (js-call-with-this this-arg f (list (nth arr i) i arr))) + i) + (else (js-list-find-index-loop f arr this-arg (+ i 1)))))) (define js-list-some-loop (fn - (f arr i) + (f arr this-arg i) (cond ((>= i (len arr)) false) - ((js-to-boolean (f (nth arr i))) true) - (else (js-list-some-loop f arr (+ i 1)))))) + ((js-to-boolean (js-call-with-this this-arg f (list (nth arr i) i arr))) + true) + (else (js-list-some-loop f arr this-arg (+ i 1)))))) (define js-list-flat-loop @@ -1722,11 +3093,15 @@ ((>= s e) nil) ((>= s (len arr)) nil) (else - (begin (js-list-set! arr s v) (js-list-fill-loop arr v (+ s 1) e)))))) + (begin + (js-list-set! arr s v) + (js-list-fill-loop arr v (+ s 1) e)))))) (define js-list-sort! - (fn (arr cmp) (let ((n (len arr))) (js-list-sort-outer! arr cmp 0 n)))) + (fn + (arr cmp) + (let ((n (len arr))) (js-list-sort-outer! arr cmp 0 n)))) (define js-list-sort-outer! @@ -1753,7 +3128,9 @@ ((result (if (= cmp nil) (if (js-str-lt (js-to-string b) (js-to-string a)) 1 -1) (js-to-number (cmp a b))))) (when (> result 0) - (begin (js-list-set! arr i b) (js-list-set! arr (+ i 1) a))))) + (begin + (js-list-set! arr i b) + (js-list-set! arr (+ i 1) a))))) (js-list-sort-inner! arr cmp (+ i 1) end)))))) (define @@ -1768,11 +3145,12 @@ (define js-list-every-loop (fn - (f arr i) + (f arr this-arg i) (cond ((>= i (len arr)) true) - ((not (js-to-boolean (f (nth arr i)))) false) - (else (js-list-every-loop f arr (+ i 1)))))) + ((not (js-to-boolean (js-call-with-this this-arg f (list (nth arr i) i arr)))) + false) + (else (js-list-every-loop f arr this-arg (+ i 1)))))) (define js-list-reverse-loop @@ -1810,7 +3188,11 @@ (if (< i 0) acc - (js-list-reduce-right-loop f (f acc (nth arr i)) arr (- i 1))))) + (js-list-reduce-right-loop + f + (js-call-with-this js-undefined f (list acc (nth arr i) i arr)) + arr + (- i 1))))) (define js-list-keys-loop @@ -1819,7 +3201,9 @@ (if (>= i (len arr)) result - (begin (append! result i) (js-list-keys-loop arr (+ i 1) result))))) + (begin + (append! result i) + (js-list-keys-loop arr (+ i 1) result))))) (define js-list-entries-loop @@ -1854,9 +3238,18 @@ (define js-string-repeat + (fn + (s n) + (js-string-repeat-loop s n ""))) + +(define + js-string-repeat-loop (fn (s n acc) - (if (<= n 0) acc (js-string-repeat s (- n 1) (str acc s))))) + (if + (<= n 0) + acc + (js-string-repeat-loop s (- n 1) (str acc s))))) (define js-string-pad @@ -1892,25 +3285,42 @@ (i) (let ((idx (js-num-to-int i))) - (if (and (>= idx 0) (< idx (len s))) (char-at s idx) "")))) + (if + (and (>= idx 0) (< idx (len s))) + (char-at s idx) + "")))) ((= name "charCodeAt") (fn (i) (let - ((idx (js-num-to-int i))) + ((idx (js-num-to-int (js-to-number i)))) (if (and (>= idx 0) (< idx (len s))) (char-code (char-at s idx)) - 0)))) + (js-nan-value))))) ((= name "indexOf") - (fn (needle) (js-string-index-of s (js-to-string needle) 0))) + (fn + (&rest args) + (if + (empty? args) + -1 + (js-string-index-of + s + (js-to-string (nth args 0)) + (if + (< (len args) 2) + 0 + (max 0 (js-num-to-int (nth args 1)))))))) ((= name "slice") (fn (&rest args) (let ((start (if (= (len args) 0) 0 (js-num-to-int (nth args 0)))) (stop - (if (< (len args) 2) (len s) (js-num-to-int (nth args 1))))) + (if + (< (len args) 2) + (len s) + (js-num-to-int (nth args 1))))) (js-string-slice s start stop)))) ((= name "substring") (fn @@ -1927,7 +3337,24 @@ (js-string-slice s lo (min hi (len s))))))) ((= name "toUpperCase") (fn () (js-upper-case s))) ((= name "toLowerCase") (fn () (js-lower-case s))) - ((= name "split") (fn (sep) (js-string-split s (js-to-string sep)))) + ((= name "split") + (fn + (&rest args) + (let + ((sep (if (= (len args) 0) :js-undefined (nth args 0))) + (limit-raw (if (< (len args) 2) :js-undefined (nth args 1)))) + (let + ((limit + (cond + ((js-undefined? limit-raw) -1) + (else (js-num-to-int (js-to-number limit-raw)))))) + (cond + ((js-undefined? sep) (js-make-list s)) + ((= limit 0) (js-make-list)) + (else + (let + ((result (js-string-split s (js-to-string sep)))) + (if (< limit 0) result (js-list-take result limit))))))))) ((= name "concat") (fn (&rest args) (js-string-concat-loop s args 0))) ((= name "includes") @@ -1941,7 +3368,11 @@ (&rest args) (let ((needle (if (= (len args) 0) "" (js-to-string (nth args 0)))) - (start (if (< (len args) 2) 0 (js-num-to-int (nth args 1))))) + (start + (if + (< (len args) 2) + 0 + (js-num-to-int (nth args 1))))) (js-string-matches? s needle start 0)))) ((= name "endsWith") (fn @@ -1958,20 +3389,37 @@ ((= name "trimStart") (fn () (js-trim-left s))) ((= name "trimEnd") (fn () (js-trim-right s))) ((= name "repeat") - (fn (n) (js-string-repeat s (js-num-to-int n) ""))) + (fn + (n) + (let + ((nn (js-to-number n))) + (cond + ((or (< nn 0) (= nn (js-infinity-value))) + (raise + (js-new-call RangeError + (js-args "Invalid count value")))) + (else (js-string-repeat-loop s (js-num-to-int nn) "")))))) ((= name "padStart") (fn (&rest args) (let ((target (if (= (len args) 0) 0 (js-num-to-int (nth args 0)))) - (pad (if (< (len args) 2) " " (js-to-string (nth args 1))))) + (pad + (if + (< (len args) 2) + " " + (js-to-string (nth args 1))))) (js-string-pad s target pad true)))) ((= name "padEnd") (fn (&rest args) (let ((target (if (= (len args) 0) 0 (js-num-to-int (nth args 0)))) - (pad (if (< (len args) 2) " " (js-to-string (nth args 1))))) + (pad + (if + (< (len args) 2) + " " + (js-to-string (nth args 1))))) (js-string-pad s target pad false)))) ((= name "toString") (fn () s)) ((= name "valueOf") (fn () s)) @@ -2001,7 +3449,8 @@ (js-string-slice s (+ idx (len src)) (len s))))))))) (else (let - ((needle (js-to-string (nth args 0))) (repl (nth args 1))) + ((needle (js-to-string (nth args 0))) + (repl (nth args 1))) (let ((idx (js-string-index-of s needle 0))) (if @@ -2021,12 +3470,17 @@ ((= (len args) 0) -1) ((js-regex? (nth args 0)) (let - ((rx (nth args 0)) (src (get (nth args 0) "source"))) + ((rx (nth args 0)) + (src (get (nth args 0) "source"))) (js-string-index-of (if (get rx "ignoreCase") (js-lower-case s) s) (if (get rx "ignoreCase") (js-lower-case src) src) 0))) - (else (js-string-index-of s (js-to-string (nth args 0)) 0))))) + (else + (js-string-index-of + s + (js-to-string (nth args 0)) + 0))))) ((= name "match") (fn (&rest args) @@ -2049,7 +3503,18 @@ (if (= idx -1) nil - (let ((res (list))) (begin (append! res needle) res))))))))) + (let ((res (list))) (append! res needle) res)))))))) + ((= name "matchAll") + (fn + (&rest args) + (if + (empty? args) + (list) + (let + ((needle (js-to-string (nth args 0)))) + (let + ((loop (fn (start acc) (let ((idx (js-string-index-of s needle start))) (if (= idx -1) acc (let ((m (list))) (begin (append! m needle) (dict-set! m "index" idx) (loop (+ idx (max 1 (len needle))) (begin (append! acc m) acc))))))))) + (loop 0 (list))))))) ((= name "at") (fn (i) @@ -2076,7 +3541,20 @@ -1 (let ((needle (js-to-string (nth args 0)))) - (js-string-last-index-of s needle (- (len s) (len needle))))))) + (let + ((default-start (- (len s) (len needle))) + (from + (if + (< (len args) 2) + -1 + (js-num-to-int (nth args 1))))) + (js-string-last-index-of + s + needle + (if + (< from 0) + default-start + (min from default-start)))))))) ((= name "localeCompare") (fn (&rest args) @@ -2085,7 +3563,10 @@ 0 (let ((other (js-to-string (nth args 0)))) - (cond ((< s other) -1) ((> s other) 1) (else 0)))))) + (cond + ((< s other) -1) + ((> s other) 1) + (else 0)))))) ((= name "replaceAll") (fn (&rest args) @@ -2093,7 +3574,8 @@ (< (len args) 2) s (let - ((needle-arg (nth args 0)) (repl (nth args 1))) + ((needle-arg (nth args 0)) + (repl (nth args 1))) (let ((needle (if (js-regex? needle-arg) (get needle-arg "source") (js-to-string needle-arg)))) (js-string-replace-all @@ -2129,7 +3611,11 @@ ((n (len s))) (let ((lo (if (< start 0) (max 0 (+ n start)) (min start n))) - (hi (if (< stop 0) (max 0 (+ n stop)) (min stop n)))) + (hi + (if + (< stop 0) + (max 0 (+ n stop)) + (min stop n)))) (if (>= lo hi) "" (js-string-slice-loop s lo hi "")))))) (define @@ -2138,7 +3624,8 @@ (s i e acc) (cond ((>= i e) acc) - (else (js-string-slice-loop s (+ i 1) e (str acc (char-at s i))))))) + (else + (js-string-slice-loop s (+ i 1) e (str acc (char-at s i))))))) (define js-string-index-of @@ -2188,6 +3675,15 @@ ((not (= (char-at s (+ si ni)) (char-at needle ni))) false) (else (js-string-matches? s needle si (+ ni 1)))))) +(define + js-list-take + (fn + (lst n) + (if + (or (<= n 0) (empty? lst)) + (list) + (cons (first lst) (js-list-take (rest lst) (- n 1)))))) + (define js-string-split (fn @@ -2248,6 +3744,13 @@ (and (>= key 0) (< key (len obj))) (nth obj (js-num-to-int key)) js-undefined)) + ((and (= (type-of key) "string") (js-is-numeric-string? key)) + (let + ((idx (js-num-to-int (js-string-to-number key)))) + (if + (and (>= idx 0) (< idx (len obj))) + (nth obj idx) + js-undefined))) ((= key "push") (js-array-method obj "push")) ((= key "pop") (js-array-method obj "pop")) ((= key "shift") (js-array-method obj "shift")) @@ -2276,7 +3779,7 @@ ((= key "findLast") (js-array-method obj "findLast")) ((= key "findLastIndex") (js-array-method obj "findLastIndex")) ((= key "reduceRight") (js-array-method obj "reduceRight")) - ((= key "toString") (js-array-method obj "toString")) + ((= key "toString") (js-dict-get-walk (get Array "prototype") "toString")) ((= key "toLocaleString") (js-array-method obj "toLocaleString")) ((= key "keys") (js-array-method obj "keys")) ((= key "values") (js-array-method obj "values")) @@ -2284,7 +3787,7 @@ ((= key "copyWithin") (js-array-method obj "copyWithin")) ((= key "toReversed") (js-array-method obj "toReversed")) ((= key "toSorted") (js-array-method obj "toSorted")) - (else js-undefined))) + (else (js-dict-get-walk (get Array "prototype") (js-to-string key))))) ((= (type-of obj) "string") (cond ((= key "length") (len obj)) @@ -2328,16 +3831,25 @@ (js-string-method obj "toLocaleUpperCase")) ((= key "isWellFormed") (js-string-method obj "isWellFormed")) ((= key "toWellFormed") (js-string-method obj "toWellFormed")) - (else js-undefined))) + (else + (let + ((proto (get String "prototype"))) + (if + (and (dict? proto) (contains? (keys proto) key)) + (get proto key) + js-undefined))))) ((= (type-of obj) "dict") (js-dict-get-walk obj (js-to-string key))) ((and (= obj Promise) (dict-has? __js_promise_statics__ (js-to-string key))) (get __js_promise_statics__ (js-to-string key))) - ((and (js-function? obj) (or (= key "prototype") (= key "name") (= key "length") (= key "call") (= key "apply") (= key "bind"))) + ((and (= obj Error) (= (js-to-string key) "isError")) + js-error-is-error) + ((and (js-function? obj) (or (= key "prototype") (= key "name") (= key "length") (= key "call") (= key "apply") (= key "bind") (= key "constructor"))) (cond ((= key "prototype") (js-get-ctor-proto obj)) ((= key "name") (js-extract-fn-name obj)) ((= key "length") (js-fn-length obj)) + ((= key "constructor") js-function-global) (else (js-invoke-function-bound obj key)))) (else js-undefined)))) (define @@ -2347,10 +3859,14 @@ (cond ((= obj nil) js-undefined) ((js-undefined? obj) js-undefined) + ((or (= (type-of obj) "lambda") (= (type-of obj) "function") (= (type-of obj) "component")) + (js-dict-get-walk (get js-function-global "prototype") skey)) ((not (= (type-of obj) "dict")) js-undefined) ((dict-has? obj skey) (get obj skey)) ((dict-has? obj "__proto__") (js-dict-get-walk (get obj "__proto__") skey)) + ((not (= obj (get Object "prototype"))) + (js-dict-get-walk (get Object "prototype") skey)) (else js-undefined))))) (define @@ -2368,6 +3884,100 @@ (define dict-has? (fn (d k) (contains? (keys d) k))) +(define + js-make-obj + (fn () + (let ((d (dict))) + (begin + (dict-set! d "__js_order__" (list)) + (dict-set! d "__proto__" (get Object "prototype")) + d)))) + +(define + js-obj-order-add! + (fn + (obj k) + (cond + ((not (dict? obj)) nil) + ((not (contains? (keys obj) "__js_order__")) nil) + (else + (let + ((order (get obj "__js_order__"))) + (if (contains? order k) nil (append! order k))))))) + +(define + js-obj-order-remove! + (fn + (obj k) + (cond + ((not (dict? obj)) nil) + ((not (contains? (keys obj) "__js_order__")) nil) + (else + (dict-set! + obj + "__js_order__" + (filter (fn (x) (not (= x k))) (get obj "__js_order__"))))))) + +(define + js-obj-set! + (fn + (obj key val) + (let + ((sk (js-to-string key))) + (begin + (if (not (contains? (keys obj) sk)) (js-obj-order-add! obj sk) nil) + (dict-set! obj sk val) + val)))) + +(define + js-obj-spread! + (fn + (target src) + (cond + ((or (= src nil) (js-undefined? src)) target) + ((dict? src) + (begin + (for-each + (fn + (k) + (if + (js-key-internal? k) + nil + (js-obj-set! target k (get src k)))) + (js-object-keys src)) + target)) + ((= (type-of src) "string") + (let + ((n (len src))) + (begin (js-obj-spread-string-loop! target src 0 n) target))) + ((list? src) + (let + ((n (len src))) + (begin (js-obj-spread-list-loop! target src 0 n) target))) + (else target)))) + +(define + js-obj-spread-string-loop! + (fn + (target s i n) + (cond + ((>= i n) nil) + (else + (begin + (js-obj-set! target (js-to-string i) (char-at s i)) + (js-obj-spread-string-loop! target s (+ i 1) n)))))) + +(define + js-obj-spread-list-loop! + (fn + (target arr i n) + (cond + ((>= i n) nil) + (else + (begin + (js-obj-set! target (js-to-string i) (nth arr i)) + (js-obj-spread-list-loop! target arr (+ i 1) n)))))) + (begin (define js-set-prop @@ -2376,8 +3986,19 @@ (cond ((js-undefined? obj) (error "js-set-prop: cannot set on undefined")) ((= (type-of obj) "dict") - (do (dict-set! obj (js-to-string key) val) val)) + (let + ((sk (js-to-string key))) + (begin + (if (not (contains? (keys obj) sk)) (js-obj-order-add! obj sk) nil) + (dict-set! obj sk val) + val))) ((= (type-of obj) "list") (do (js-list-set! obj key val) val)) + ((and + (or (= (type-of obj) "lambda") (= (type-of obj) "function") (= (type-of obj) "component")) + (= (js-to-string key) "prototype")) + (let + ((id (js-ctor-id obj))) + (begin (dict-set! __js_proto_table__ id val) val))) (else val)))) (define js-list-set! @@ -2389,10 +4010,20 @@ ((i (js-num-to-int key)) (n (len lst))) (cond ((< i 0) nil) + ((>= i 1000000) nil) ((< i n) (set-nth! lst i val)) ((= i n) (append! lst val)) (else (do (js-pad-list! lst n i) (append! lst val)))))) - ((= key "length") nil) + ((and (= (type-of key) "string") (js-is-numeric-string? key)) + (js-list-set! lst (js-string-to-number key) val)) + ((= key "length") + (let + ((target (js-num-to-int (js-to-number val))) (n (len lst))) + (cond + ((< target 0) nil) + ((>= target 1000000) nil) + ((> target n) (js-pad-list! lst n target)) + (else nil)))) (else nil)))) (define js-pad-list! @@ -2421,57 +4052,91 @@ (define js-math-ceil (fn (x) (ceil (js-to-number x)))) -(define js-math-round (fn (x) (floor (+ (js-to-number x) 0.5)))) +(define + js-math-round + (fn + (x) + (let + ((n (js-to-number x))) + (cond + ((js-number-is-nan n) (js-nan-value)) + ((= n (js-infinity-value)) n) + ((= n (- 0 (js-infinity-value))) n) + ((= n 0) n) + (else (floor (+ n 0.5))))))) (define js-math-max (fn (&rest args) (cond - ((empty? args) (- 0 (/ 1 0))) - (else (js-math-max-loop (first args) (rest args)))))) + ((empty? args) (- 0 (js-infinity-value))) + (else (js-math-max-loop (js-to-number (first args)) (rest args)))))) (define js-math-max-loop (fn (acc xs) (cond + ((js-number-is-nan acc) (js-nan-value)) ((empty? xs) acc) (else (let ((h (js-to-number (first xs)))) - (js-math-max-loop (if (> h acc) h acc) (rest xs))))))) + (cond + ((js-number-is-nan h) (js-nan-value)) + ((> h acc) (js-math-max-loop h (rest xs))) + ((and (= h 0) (= acc 0)) + (js-math-max-loop (if (js-is-positive-zero? h) h acc) (rest xs))) + (else (js-math-max-loop acc (rest xs))))))))) (define js-math-min (fn (&rest args) (cond - ((empty? args) (/ 1 0)) - (else (js-math-min-loop (first args) (rest args)))))) + ((empty? args) (js-infinity-value)) + (else (js-math-min-loop (js-to-number (first args)) (rest args)))))) (define js-math-min-loop (fn (acc xs) (cond + ((js-number-is-nan acc) (js-nan-value)) ((empty? xs) acc) (else (let ((h (js-to-number (first xs)))) - (js-math-min-loop (if (< h acc) h acc) (rest xs))))))) + (cond + ((js-number-is-nan h) (js-nan-value)) + ((< h acc) (js-math-min-loop h (rest xs))) + ((and (= h 0) (= acc 0)) + (js-math-min-loop (if (js-is-positive-zero? h) acc h) (rest xs))) + (else (js-math-min-loop acc (rest xs))))))))) + +(define + js-is-positive-zero? + (fn + (n) + (cond + ((not (= n 0)) false) + ((= (type-of n) "rational") true) + (else (= (/ 1.0 (exact->inexact n)) (js-infinity-value)))))) (define js-math-random (fn () 0)) (define js-math-sqrt (fn (x) (sqrt (js-to-number x)))) -(define js-math-pow (fn (a b) (pow (js-to-number a) (js-to-number b)))) +(define js-math-pow (fn (a b) (js-pow-spec a b))) (define js-math-trunc (fn (x) - (let ((n (js-to-number x))) (if (< n 0) (ceil n) (floor n))))) + (let + ((n (js-to-number x))) + (if (< n 0) (ceil n) (floor n))))) (define js-math-sign @@ -2479,7 +4144,10 @@ (x) (let ((n (js-to-number x))) - (cond ((> n 0) 1) ((< n 0) -1) (else n))))) + (cond + ((> n 0) 1) + ((< n 0) -1) + (else n))))) (define js-math-cbrt @@ -2487,22 +4155,91 @@ (x) (let ((n (js-to-number x))) - (if (< n 0) (- 0 (pow (- 0 n) (/ 1 3))) (pow n (/ 1 3)))))) - -(define js-math-hypot (fn (&rest args) (sqrt (js-math-hypot-loop args 0)))) + (cond + ((js-number-is-nan n) (js-nan-value)) + ((= n (js-infinity-value)) (js-infinity-value)) + ((= n (- 0 (js-infinity-value))) n) + ((= n 0) n) + ((< n 0) (- 0 (pow (- 0 n) (/ 1.0 3.0)))) + (else (pow n (/ 1.0 3.0))))))) (define - js-math-hypot-loop + js-math-hypot (fn - (args acc) - (if - (empty? args) - acc - (let - ((n (js-to-number (first args)))) - (js-math-hypot-loop (rest args) (+ acc (* n n))))))) + (&rest args) + (let + ((status (js-math-hypot-scan args false false 0))) + (cond + ((= (first status) "inf") (js-infinity-value)) + ((= (first status) "nan") (js-nan-value)) + (else (sqrt (nth status 1))))))) -(define Math {:random js-math-random :trunc js-math-trunc :LN10 2.30259 :SQRT1_2 0.707107 :floor js-math-floor :PI 3.14159 :sqrt js-math-sqrt :hypot js-math-hypot :LOG2E 1.4427 :round js-math-round :ceil js-math-ceil :abs js-math-abs :pow js-math-pow :max js-math-max :LOG10E 0.434294 :SQRT2 1.41421 :cbrt js-math-cbrt :min js-math-min :sign js-math-sign :E 2.71828 :LN2 0.693147}) +(define + js-math-hypot-scan + (fn + (args saw-inf? saw-nan? acc) + (cond + ((empty? args) + (cond + (saw-inf? (list "inf")) + (saw-nan? (list "nan")) + (else (list "ok" acc)))) + (else + (let + ((n (js-to-number (first args)))) + (cond + ((= n (js-infinity-value)) + (js-math-hypot-scan (rest args) true saw-nan? acc)) + ((= n (- 0 (js-infinity-value))) + (js-math-hypot-scan (rest args) true saw-nan? acc)) + ((js-number-is-nan n) + (js-math-hypot-scan (rest args) saw-inf? true acc)) + (else + (js-math-hypot-scan (rest args) saw-inf? saw-nan? (+ acc (* n n)))))))))) + +(begin + (define js-math-sin (fn (x) (sin (js-to-number x)))) + (define js-math-cos (fn (x) (cos (js-to-number x)))) + (define js-math-tan (fn (x) (tan (js-to-number x)))) + (define js-math-asin (fn (x) (asin (js-to-number x)))) + (define js-math-acos (fn (x) (acos (js-to-number x)))) + (define js-math-atan (fn (x) (atan (js-to-number x)))) + (define + js-math-atan2 + (fn (y x) (atan2 (js-to-number y) (js-to-number x)))) + (define js-math-sinh (fn (x) (sinh (js-to-number x)))) + (define js-math-cosh (fn (x) (cosh (js-to-number x)))) + (define js-math-tanh (fn (x) (tanh (js-to-number x)))) + (define js-math-asinh (fn (x) (asinh (js-to-number x)))) + (define js-math-acosh (fn (x) (acosh (js-to-number x)))) + (define js-math-atanh (fn (x) (atanh (js-to-number x)))) + (define js-math-exp (fn (x) (exp (js-to-number x)))) + (define js-math-log (fn (x) (log (js-to-number x)))) + (define js-math-log2 (fn (x) (log2 (js-to-number x)))) + (define js-math-log10 (fn (x) (log10 (js-to-number x)))) + (define js-math-expm1 (fn (x) (expm1 (js-to-number x)))) + (define js-math-log1p (fn (x) (log1p (js-to-number x)))) + (define + js-math-clz32 + (fn + (&rest args) + (let + ((x (if (empty? args) 0 (js-to-number (nth args 0))))) + (let + ((n (modulo (floor x) 4294967296))) + (if (= n 0) 32 (- 31 (floor (log2 n)))))))) + (define + js-math-imul + (fn + (a b) + (let + ((a32 (modulo (floor (js-to-number a)) 4294967296)) + (b32 (modulo (floor (js-to-number b)) 4294967296))) + (let + ((result (modulo (* a32 b32) 4294967296))) + (if (>= result 2147483648) (- result 4294967296) result))))) + (define js-math-fround (fn (x) (js-to-number x))) + (define Math {:atan js-math-atan :sign js-math-sign :LN2 0.693147 :cos js-math-cos :imul js-math-imul :min js-math-min :acos js-math-acos :log10 js-math-log10 :LOG10E 0.434294 :tanh js-math-tanh :abs js-math-abs :round js-math-round :log js-math-log :sqrt js-math-sqrt :cosh js-math-cosh :tan js-math-tan :floor js-math-floor :exp js-math-exp :asin js-math-asin :clz32 js-math-clz32 :random js-math-random :LN10 2.30259 :SQRT1_2 0.707107 :sinh js-math-sinh :E 2.71828 :fround js-math-fround :cbrt js-math-cbrt :log1p js-math-log1p :SQRT2 1.41421 :max js-math-max :log2 js-math-log2 :ceil js-math-ceil :pow js-math-pow :sin js-math-sin :hypot js-math-hypot :LOG2E 1.4427 :atanh js-math-atanh :asinh js-math-asinh :acosh js-math-acosh :PI 3.14159 :atan2 js-math-atan2 :trunc js-math-trunc :expm1 js-math-expm1})) (define js-number-is-finite @@ -2511,8 +4248,8 @@ (and (number? v) (not (js-number-is-nan v)) - (not (= v (/ 1 0))) - (not (= v (/ -1 0)))))) + (not (= v inf)) + (not (= v -inf))))) (define js-number-is-nan @@ -2528,9 +4265,7 @@ (define js-number-is-safe-integer - (fn - (v) - (and (js-number-is-integer v) (<= (js-math-abs v) 9007199254740991)))) + (fn (v) (and (js-number-is-integer v) (<= (js-math-abs v) 9007199254740991)))) (define js-global-is-finite @@ -2538,7 +4273,19 @@ (define js-global-is-nan (fn (v) (js-number-is-nan (js-to-number v)))) -(define Number {:isFinite js-number-is-finite :MAX_SAFE_INTEGER 9007199254740991 :EPSILON 2.22045e-16 :MAX_VALUE (js-max-value-approx) :POSITIVE_INFINITY (js-infinity-value) :__callable__ js-to-number :isInteger js-number-is-integer :prototype {:valueOf (fn () (js-this)) :toPrecision (fn (&rest args) (js-to-string (js-this))) :toString (fn (&rest args) (let ((this-val (js-this)) (radix (if (empty? args) 10 (js-to-number (nth args 0))))) (js-num-to-str-radix this-val (if (or (= radix nil) (js-undefined? radix)) 10 radix)))) :toLocaleString (fn () (js-to-string (js-this))) :toFixed (fn (d) (js-number-to-fixed (js-this) (if (= d nil) 0 (js-to-number d)))) :toExponential (fn (&rest args) (js-to-string (js-this)))} :isNaN js-number-is-nan :isSafeInteger js-number-is-safe-integer :NEGATIVE_INFINITY (- 0 (js-infinity-value)) :NaN (js-nan-value) :MIN_VALUE 4.94066e-324 :MIN_SAFE_INTEGER -9007199254740991}) +(define + js-number-this-val + (fn () + (let ((this-val (js-this))) + (cond + ((or (= (type-of this-val) "number") (= (type-of this-val) "rational")) + (js-numeric-norm this-val)) + ((and (= (type-of this-val) "dict") (contains? (keys this-val) "__js_number_value__")) + (get this-val "__js_number_value__")) + (else + (raise (js-new-call TypeError (js-args "Number.prototype method requires a Number")))))))) + +(define Number {:MIN_SAFE_INTEGER -9007199254740991 :MIN_VALUE 4.94066e-324 :isNaN js-number-is-nan :isSafeInteger js-number-is-safe-integer :NEGATIVE_INFINITY (- 0 (js-infinity-value)) :NaN (js-nan-value) :prototype {:toFixed {:__callable__ (fn (d) (js-number-to-fixed (js-number-this-val) (if (= d nil) 0 (js-to-number d)))) :length 1 :name "toFixed"} :toExponential {:__callable__ (fn (&rest args) (js-number-to-string (js-number-this-val))) :length 1 :name "toExponential"} :toLocaleString {:__callable__ (fn () (js-number-to-string (js-number-this-val))) :length 0 :name "toLocaleString"} :toString {:__callable__ (fn (&rest args) (let ((this-val (js-number-this-val)) (radix (if (empty? args) 10 (js-to-number (nth args 0))))) (js-num-to-str-radix this-val (if (or (= radix nil) (js-undefined? radix)) 10 radix)))) :length 1 :name "toString"} :toPrecision {:__callable__ (fn (&rest args) (js-number-to-string (js-number-this-val))) :length 1 :name "toPrecision"} :valueOf {:__callable__ (fn (&rest args) (js-number-this-val)) :length 0 :name "valueOf"}} :isInteger js-number-is-integer :__callable__ js-to-number :MAX_VALUE (js-max-value-approx) :POSITIVE_INFINITY (js-infinity-value) :isFinite js-number-is-finite :MAX_SAFE_INTEGER 9007199254740991 :EPSILON 2.22045e-16}) (dict-set! Number "length" 1) @@ -2546,6 +4293,52 @@ (dict-set! (get Number "prototype") "constructor" Number) +(dict-set! + Number + "__callable__" + (fn + (&rest args) + (let + ((raw (if (= (len args) 0) 0 (js-to-number (nth args 0))))) + (let + ((this-val (js-this))) + (if + (and + (dict? this-val) + (contains? (keys this-val) "__proto__") + (= (get this-val "__proto__") (get Number "prototype"))) + (begin (dict-set! this-val "__js_number_value__" raw) this-val) + raw))))) + +(dict-set! + (get Number "prototype") + "valueOf" + (fn + () + (let + ((this-val (js-this))) + (if + (and + (dict? this-val) + (contains? (keys this-val) "__js_number_value__")) + (get this-val "__js_number_value__") + this-val)))) + +(dict-set! + (get Number "prototype") + "toString" + (fn + (&rest args) + (let + ((this-raw (js-this))) + (let + ((this-val (if (and (dict? this-raw) (contains? (keys this-raw) "__js_number_value__")) (get this-raw "__js_number_value__") this-raw))) + (let + ((radix (if (empty? args) 10 (js-to-number (nth args 0))))) + (js-num-to-str-radix + this-val + (if (or (= radix nil) (js-undefined? radix)) 10 radix))))))) + (define isFinite js-global-is-finite) (define isNaN js-global-is-nan) @@ -2693,74 +4486,226 @@ (if (>= i (len s)) acc - (begin (append! acc (char-at s i)) (js-string-to-list s (+ i 1) acc))))) + (begin + (append! acc (char-at s i)) + (js-string-to-list s (+ i 1) acc))))) + +(define + js-key-internal? + (fn + (k) + (or (= k "__js_order__") (= k "__proto__")))) + +(define + js-for-in-keys + (fn + (o) + (let + ((result (list))) + (begin (js-for-in-walk o result) result)))) + +(define + js-for-in-walk + (fn + (o acc) + (cond + ((not (dict? o)) nil) + ((= o (get Object "prototype")) nil) + ((= o (get Array "prototype")) nil) + ((= o (get Number "prototype")) nil) + ((= o (get String "prototype")) nil) + ((= o (get Boolean "prototype")) nil) + ((= o (get Date "prototype")) nil) + ((= o (get RegExp "prototype")) nil) + ((= o (get Map "prototype")) nil) + ((= o (get Set "prototype")) nil) + ((= o (get js-function-global "prototype")) nil) + (else + (let + ((own (js-object-keys o))) + (begin + (for-each + (fn (k) (if (contains? acc k) nil (append! acc k))) + own) + (cond + ((contains? (keys o) "__proto__") + (js-for-in-walk (get o "__proto__") acc)) + (else nil)))))))) (define js-object-keys (fn (o) (cond + ((or (= o nil) (js-undefined? o)) + (raise (js-new-call TypeError (js-args "Object.keys called on null or undefined")))) + ((= (type-of o) "string") + (let ((result (list)) (n (len o))) + (begin + (js-string-keys-loop result 0 n) + result))) + ((list? o) + (let ((result (list)) (n (len o))) + (begin + (js-string-keys-loop result 0 n) + result))) ((dict? o) - (let - ((result (list))) - (for-each (fn (k) (append! result k)) (keys o)) - result)) + (cond + ((contains? (keys o) "__js_order__") + (let + ((result (list))) + (begin + (for-each + (fn (k) (if (js-key-internal? k) nil (append! result k))) + (get o "__js_order__")) + result))) + (else + (let + ((result (list))) + (begin + (for-each + (fn (k) (if (js-key-internal? k) nil (append! result k))) + (keys o)) + result))))) (else (list))))) +(define + js-string-keys-loop + (fn + (acc i n) + (cond + ((>= i n) nil) + (else + (begin + (append! acc (js-to-string i)) + (js-string-keys-loop acc (+ i 1) n)))))) + (define js-object-values (fn (o) (cond + ((or (= o nil) (js-undefined? o)) + (raise (js-new-call TypeError (js-args "Object.values called on null or undefined")))) + ((= (type-of o) "string") + (let ((result (list)) (n (len o))) + (begin + (js-string-values-loop result o 0 n) + result))) ((dict? o) (let ((result (list))) - (for-each (fn (k) (append! result (get o k))) (keys o)) + (for-each + (fn (k) (if (js-key-internal? k) nil (append! result (get o k)))) + (js-object-keys o)) result)) (else (list))))) +(define + js-string-values-loop + (fn + (acc s i n) + (cond + ((>= i n) nil) + (else + (begin + (append! acc (char-at s i)) + (js-string-values-loop acc s (+ i 1) n)))))) + (define js-object-entries (fn (o) (cond + ((or (= o nil) (js-undefined? o)) + (raise (js-new-call TypeError (js-args "Object.entries called on null or undefined")))) + ((= (type-of o) "string") + (let ((result (list)) (n (len o))) + (begin + (js-string-entries-loop result o 0 n) + result))) ((dict? o) (let ((result (list))) (for-each (fn (k) - (let - ((pair (list))) - (append! pair k) - (append! pair (get o k)) - (append! result pair))) - (keys o)) + (if + (js-key-internal? k) + nil + (let + ((pair (list))) + (begin + (append! pair k) + (append! pair (get o k)) + (append! result pair))))) + (js-object-keys o)) result)) (else (list))))) +(define + js-string-entries-loop + (fn + (acc s i n) + (cond + ((>= i n) nil) + (else + (let ((pair (list))) + (begin + (append! pair (js-to-string i)) + (append! pair (char-at s i)) + (append! acc pair) + (js-string-entries-loop acc s (+ i 1) n))))))) + (define js-object-assign (fn (&rest args) (cond - ((= (len args) 0) (dict)) + ((= (len args) 0) + (raise (js-new-call TypeError (js-args "Object.assign called on null or undefined")))) (else (let - ((target (nth args 0))) - (for-each - (fn - (src) - (when - (dict? src) + ((raw-target (nth args 0))) + (cond + ((or (= raw-target nil) (js-undefined? raw-target)) + (raise (js-new-call TypeError (js-args "Object.assign called on null or undefined")))) + (else + (let + ((target (js-coerce-this-arg raw-target))) (for-each - (fn (k) (dict-set! target k (get src k))) - (keys src)))) - (rest args)) - target))))) + (fn + (src) + (cond + ((or (= src nil) (js-undefined? src)) nil) + ((dict? src) + (for-each + (fn + (k) + (if (js-key-internal? k) nil (js-set-prop target k (get src k)))) + (js-object-keys src))) + ((= (type-of src) "string") + (let + ((n (len src))) + (begin (js-object-assign-string-loop target src 0 n)))))) + (rest args)) + target)))))))) + +(define + js-object-assign-string-loop + (fn + (target s i n) + (cond + ((>= i n) nil) + (else + (begin + (js-set-prop target (js-to-string i) (char-at s i)) + (js-object-assign-string-loop target s (+ i 1) n)))))) (define js-object-freeze (fn (o) o)) +(define __js_ctor_proto__ (dict)) + (define js-object-get-prototype-of (fn @@ -2770,7 +4715,16 @@ ((js-undefined? o) (error "TypeError: Cannot convert undefined to object")) ((dict? o) - (if (contains? (keys o) "__proto__") (get o "__proto__") nil)) + (cond + ((contains? (keys o) "__proto__") (get o "__proto__")) + (else nil))) + ((js-function? o) + (let + ((id (js-ctor-id o))) + (cond + ((dict-has? __js_ctor_proto__ id) + (get __js_ctor_proto__ id)) + (else nil)))) (else nil)))) (define @@ -2794,7 +4748,10 @@ (for-each (fn (k) - (dict-set! obj k (get (get (nth args 1) k) "value"))) + (dict-set! + obj + k + (get (get (nth args 1) k) "value"))) (keys (nth args 1)))) obj))))) @@ -2825,18 +4782,135 @@ (fn (o) (cond - ((list? o) (let ((r (list))) (begin (js-list-keys-loop o 0 r) r))) - ((dict? o) (js-object-keys o)) + ((or (= o nil) (js-undefined? o)) + (raise (js-new-call TypeError (js-args "Object.getOwnPropertyNames called on null or undefined")))) + ((list? o) + (let + ((r (list))) + (begin + (js-list-keys-loop o 0 r) + (append! r "length") + r))) + ((= (type-of o) "string") + (let ((result (list)) (n (len o))) + (begin + (js-string-keys-loop result 0 n) + (append! result "length") + result))) + ((dict? o) (js-own-property-names-ordered o)) (else (list))))) +(define + js-own-property-names-ordered + (fn + (o) + (let + ((all (js-object-keys o)) + (ints (list)) + (rest (list))) + (begin + (for-each + (fn + (k) + (if (js-int-key? k) (append! ints k) (append! rest k))) + all) + (append (js-sort-int-keys ints) rest))))) + +(define + js-int-key? + (fn + (k) + (cond + ((not (= (type-of k) "string")) false) + ((= (len k) 0) false) + (else (js-int-key-loop? k 0 (len k)))))) + +(define + js-int-key-loop? + (fn + (s i n) + (cond + ((>= i n) true) + ((let ((c (char-code-at s i))) (and (>= c 48) (<= c 57))) + (js-int-key-loop? s (+ i 1) n)) + (else false)))) + +(define + js-sort-int-keys + (fn + (lst) + (let + ((nums (map js-string-to-number lst))) + (begin + (js-sort-numbers! nums) + (map (fn (n) (str (js-num-to-int n))) nums))))) + +(define + js-sort-numbers! + (fn + (lst) + (let ((n (len lst))) + (js-bubble-sort! lst 0 n)))) + +(define + js-bubble-sort! + (fn + (lst i n) + (cond + ((>= i n) nil) + (else + (begin (js-bubble-sort-inner! lst 0 (- n i 1)) (js-bubble-sort! lst (+ i 1) n)))))) + +(define + js-bubble-sort-inner! + (fn + (lst j stop) + (cond + ((>= j stop) nil) + ((> (nth lst j) (nth lst (+ j 1))) + (let + ((a (nth lst j)) (b (nth lst (+ j 1)))) + (begin + (set-nth! lst j b) + (set-nth! lst (+ j 1) a) + (js-bubble-sort-inner! lst (+ j 1) stop)))) + (else (js-bubble-sort-inner! lst (+ j 1) stop))))) + (define js-object-get-own-property-descriptor (fn (o key) - (if - (and (dict? o) (contains? (keys o) (js-to-string key))) - {:writable true :value (get o (js-to-string key)) :enumerable true :configurable true} - :js-undefined))) + (let + ((sk (js-to-string key))) + (cond + ((and (dict? o) (js-key-internal? sk)) :js-undefined) + ((and (dict? o) (contains? (keys o) sk)) + {:configurable true :enumerable true :value (get o sk) :writable true}) + ((list? o) + (cond + ((= sk "length") + {:configurable false :enumerable false :value (len o) :writable true}) + ((js-int-key? sk) + (let + ((i (js-num-to-int (js-string-to-number sk)))) + (cond + ((and (>= i 0) (< i (len o))) + {:configurable true :enumerable true :value (nth o i) :writable true}) + (else :js-undefined)))) + (else :js-undefined))) + ((and (= (type-of o) "string")) + (cond + ((= sk "length") + {:configurable false :enumerable false :value (len o) :writable false}) + ((js-int-key? sk) + (let + ((i (js-num-to-int (js-string-to-number sk)))) + (cond + ((and (>= i 0) (< i (len o))) + {:configurable false :enumerable true :value (char-at o i) :writable false}) + (else :js-undefined)))) + (else :js-undefined))) + (else :js-undefined))))) (define js-object-get-own-property-descriptors @@ -2886,7 +4960,10 @@ (pair) (when (and (list? pair) (>= (len pair) 2)) - (dict-set! out (js-to-string (nth pair 0)) (nth pair 1)))) + (dict-set! + out + (js-to-string (nth pair 0)) + (nth pair 1)))) lst) out)))) @@ -2902,7 +4979,7 @@ (and (>= idx 0) (< idx (len o)) (integer? idx)))) (else false)))) -(define Object {:entries js-object-entries :defineProperties js-object-define-properties :__callable__ (fn (&rest args) (cond ((= (len args) 0) (dict)) (else (nth args 0)))) :preventExtensions js-object-prevent-extensions :prototype {:valueOf (fn () (js-this)) :propertyIsEnumerable (fn (k) (let ((o (js-this))) (js-object-has-own o k))) :isPrototypeOf (fn (o) (let ((this-val (js-this))) (cond ((not (dict? o)) false) (else (let ((proto (if (contains? (keys o) "__proto__") (get o "__proto__") nil))) (cond ((= proto this-val) true) ((= proto nil) false) (else ((get (get Object "prototype") "isPrototypeOf") proto)))))))) :toString (fn () "[object Object]") :hasOwnProperty (fn (k) (let ((o (js-this))) (js-object-has-own o k))) :toLocaleString (fn () "[object Object]")} :values js-object-values :hasOwn js-object-has-own :freeze js-object-freeze :assign js-object-assign :isFrozen js-object-is-frozen :getOwnPropertyDescriptor js-object-get-own-property-descriptor :fromEntries js-object-from-entries :defineProperty js-object-define-property :setPrototypeOf js-object-set-prototype-of :getOwnPropertyNames js-object-get-own-property-names :getOwnPropertyDescriptors js-object-get-own-property-descriptors :create js-object-create :isExtensible js-object-is-extensible :is js-object-is :keys js-object-keys :getPrototypeOf js-object-get-prototype-of :isSealed js-object-is-sealed :seal js-object-seal}) +(define Object {:keys js-object-keys :getPrototypeOf js-object-get-prototype-of :isSealed js-object-is-sealed :seal js-object-seal :create js-object-create :isExtensible js-object-is-extensible :is js-object-is :setPrototypeOf js-object-set-prototype-of :getOwnPropertyNames js-object-get-own-property-names :getOwnPropertyDescriptors js-object-get-own-property-descriptors :defineProperty js-object-define-property :fromEntries js-object-from-entries :getOwnPropertyDescriptor js-object-get-own-property-descriptor :assign js-object-assign :isFrozen js-object-is-frozen :freeze js-object-freeze :values js-object-values :hasOwn js-object-has-own :prototype {:hasOwnProperty (fn (k) (let ((o (js-this))) (js-object-has-own o k))) :toLocaleString (fn () "[object Object]") :isPrototypeOf (fn (o) (let ((this-val (js-this))) (cond ((not (dict? o)) false) (else (let ((proto (if (contains? (keys o) "__proto__") (get o "__proto__") nil))) (cond ((= proto this-val) true) ((= proto nil) false) (else ((get (get Object "prototype") "isPrototypeOf") proto)))))))) :toString (fn () (js-object-tostring-class (js-this))) :propertyIsEnumerable (fn (k) (let ((o (js-this))) (js-object-has-own o k))) :valueOf (fn () (js-this))} :__callable__ (fn (&rest args) (let ((this-val (js-this))) (let ((is-new (and (dict? this-val) (contains? (keys this-val) "__proto__") (= (get this-val "__proto__") (get Object "prototype"))))) (cond ((= (len args) 0) (if is-new this-val (dict))) ((or (= (nth args 0) nil) (js-undefined? (nth args 0))) (if is-new this-val (dict))) ((= (type-of (nth args 0)) "string") (js-new-call String (list (nth args 0)))) ((= (js-typeof (nth args 0)) "number") (js-new-call Number (list (nth args 0)))) ((= (js-typeof (nth args 0)) "boolean") (js-new-call Boolean (list (nth args 0)))) (else (nth args 0)))))) :preventExtensions js-object-prevent-extensions :entries js-object-entries :defineProperties js-object-define-properties}) (dict-set! Object "length" 1) @@ -2916,7 +4993,12 @@ (obj key) (cond ((dict? obj) - (begin (dict-set! obj (js-to-string key) js-undefined) true)) + (let + ((sk (js-to-string key))) + (begin + (js-obj-order-remove! obj sk) + (dict-delete! obj sk) + true))) (else true)))) (define @@ -2961,17 +5043,56 @@ (define js-array-of (fn (&rest args) args)) +(define + js-array-proto-fn-length + (fn + (name) + (cond + ((= name "concat") 1) + ((= name "copyWithin") 2) + ((= name "every") 1) + ((= name "fill") 1) + ((= name "filter") 1) + ((= name "find") 1) + ((= name "findIndex") 1) + ((= name "findLast") 1) + ((= name "findLastIndex") 1) + ((= name "flat") 0) + ((= name "flatMap") 1) + ((= name "forEach") 1) + ((= name "includes") 1) + ((= name "indexOf") 1) + ((= name "join") 1) + ((= name "lastIndexOf") 1) + ((= name "map") 1) + ((= name "push") 1) + ((= name "reduce") 1) + ((= name "reduceRight") 1) + ((= name "slice") 2) + ((= name "some") 1) + ((= name "sort") 1) + ((= name "splice") 2) + ((= name "unshift") 1) + ((= name "at") 1) + ((= name "toSorted") 1) + ((= name "toReversed") 0) + ((= name "with") 2) + (else 0)))) + (define js-array-proto-fn (fn (name) - (fn - (&rest args) - (let - ((this-val (js-this))) + {:__callable__ + (fn + (&rest args) (let - ((recv (cond ((list? this-val) this-val) ((and (dict? this-val) (contains? (keys this-val) "length")) (js-arraylike-to-list this-val)) (else this-val)))) - (js-invoke-method recv name args)))))) + ((this-val (js-this))) + (let + ((recv (cond ((list? this-val) this-val) ((and (dict? this-val) (contains? (keys this-val) "length")) (js-arraylike-to-list this-val)) (else this-val)))) + (js-invoke-method recv name args)))) + :length (js-array-proto-fn-length name) + :name name})) (define js-array-from @@ -2982,7 +5103,12 @@ (else (let ((src (js-iterable-to-list (nth args 0))) - (map-fn (if (< (len args) 2) nil (nth args 1)))) + (map-fn + (if (< (len args) 2) nil (nth args 1))) + (this-arg + (if (or (< (len args) 3) (js-undefined? (nth args 2)) (= (nth args 2) nil)) + js-global-this + (nth args 2)))) (if (= map-fn nil) (let @@ -2992,11 +5118,15 @@ (let ((result (list)) (i 0)) (for-each - (fn (x) (append! result (map-fn x)) (set! i (+ i 1))) + (fn + (x) + (begin + (append! result (js-call-with-this this-arg map-fn (list x i))) + (set! i (+ i 1)))) src) result))))))) -(define Array {:__callable__ (fn (&rest args) (cond ((= (len args) 0) (list)) ((and (= (len args) 1) (number? (nth args 0))) (js-make-list-of-length (js-num-to-int (nth args 0)) :js-undefined)) (else args))) :prototype {:entries (js-array-proto-fn "entries") :concat (js-array-proto-fn "concat") :lastIndexOf (js-array-proto-fn "lastIndexOf") :splice (js-array-proto-fn "splice") :filter (js-array-proto-fn "filter") :findLast (js-array-proto-fn "findLast") :shift (js-array-proto-fn "shift") :join (js-array-proto-fn "join") :reduceRight (js-array-proto-fn "reduceRight") :values (js-array-proto-fn "values") :reduce (js-array-proto-fn "reduce") :slice (js-array-proto-fn "slice") :includes (js-array-proto-fn "includes") :findLastIndex (js-array-proto-fn "findLastIndex") :find (js-array-proto-fn "find") :toLocaleString (js-array-proto-fn "toLocaleString") :findIndex (js-array-proto-fn "findIndex") :sort (js-array-proto-fn "sort") :every (js-array-proto-fn "every") :indexOf (js-array-proto-fn "indexOf") :unshift (js-array-proto-fn "unshift") :push (js-array-proto-fn "push") :map (js-array-proto-fn "map") :some (js-array-proto-fn "some") :flat (js-array-proto-fn "flat") :toSorted (js-array-proto-fn "toSorted") :at (js-array-proto-fn "at") :pop (js-array-proto-fn "pop") :toReversed (js-array-proto-fn "toReversed") :copyWithin (js-array-proto-fn "copyWithin") :toString (js-array-proto-fn "toString") :forEach (js-array-proto-fn "forEach") :fill (js-array-proto-fn "fill") :flatMap (js-array-proto-fn "flatMap") :keys (js-array-proto-fn "keys") :reverse (js-array-proto-fn "reverse")} :isArray js-array-is-array :of js-array-of :from js-array-from}) +(define Array {:of js-array-of :from js-array-from :isArray js-array-is-array :prototype {:reverse (js-array-proto-fn "reverse") :fill (js-array-proto-fn "fill") :flatMap (js-array-proto-fn "flatMap") :keys (js-array-proto-fn "keys") :forEach (js-array-proto-fn "forEach") :toString (js-array-proto-fn "toString") :copyWithin (js-array-proto-fn "copyWithin") :toReversed (js-array-proto-fn "toReversed") :pop (js-array-proto-fn "pop") :at (js-array-proto-fn "at") :push (js-array-proto-fn "push") :map (js-array-proto-fn "map") :some (js-array-proto-fn "some") :flat (js-array-proto-fn "flat") :toSorted (js-array-proto-fn "toSorted") :indexOf (js-array-proto-fn "indexOf") :unshift (js-array-proto-fn "unshift") :every (js-array-proto-fn "every") :sort (js-array-proto-fn "sort") :findIndex (js-array-proto-fn "findIndex") :toLocaleString (js-array-proto-fn "toLocaleString") :find (js-array-proto-fn "find") :includes (js-array-proto-fn "includes") :findLastIndex (js-array-proto-fn "findLastIndex") :slice (js-array-proto-fn "slice") :reduce (js-array-proto-fn "reduce") :values (js-array-proto-fn "values") :join (js-array-proto-fn "join") :reduceRight (js-array-proto-fn "reduceRight") :shift (js-array-proto-fn "shift") :filter (js-array-proto-fn "filter") :findLast (js-array-proto-fn "findLast") :concat (js-array-proto-fn "concat") :lastIndexOf (js-array-proto-fn "lastIndexOf") :splice (js-array-proto-fn "splice") :entries (js-array-proto-fn "entries")} :__callable__ (fn (&rest args) (cond ((= (len args) 0) (list)) ((and (= (len args) 1) (number? (nth args 0))) (js-make-list-of-length (js-num-to-int (nth args 0)) :js-undefined)) (else args)))}) (dict-set! Array "length" 1) @@ -3004,10 +5134,50 @@ (dict-set! Array "name" "Array") +(dict-set! + (get Array "prototype") + "toString" + (fn + (&rest args) + (let + ((this-val (js-this))) + (let + ((items (cond ((list? this-val) this-val) ((and (dict? this-val) (contains? (keys this-val) "length")) (js-arraylike-to-list this-val)) (else (list))))) + (js-list-join items ","))))) + (define js-string-from-char-code (fn (&rest args) (js-string-from-char-code-loop args 0 ""))) +(define + js-string-from-code-point-loop + (fn + (args i acc) + (if + (>= i (len args)) + acc + (let + ((cp (floor (js-to-number (nth args i))))) + (if + (< cp 65536) + (js-string-from-code-point-loop + args + (+ i 1) + (str acc (js-code-to-char (js-num-to-int cp)))) + (let + ((hi (+ 55296 (floor (/ (- cp 65536) 1024)))) + (lo (+ 56320 (modulo (- cp 65536) 1024)))) + (js-string-from-code-point-loop + args + (+ i 1) + (str + (str acc (js-code-to-char (js-num-to-int hi))) + (js-code-to-char (js-num-to-int lo)))))))))) + +(define + js-string-from-code-point + (fn (&rest args) (js-string-from-code-point-loop args 0 ""))) + (define js-string-from-char-code-loop (fn @@ -3015,22 +5185,62 @@ (if (>= i (len args)) acc - (js-string-from-char-code-loop - args - (+ i 1) - (str acc (js-code-to-char (js-num-to-int (nth args i)))))))) + (let + ((n (js-to-number (nth args i)))) + (let + ((code (if (js-global-is-nan n) 0 (modulo (js-math-trunc n) 65536)))) + (js-string-from-char-code-loop + args + (+ i 1) + (str acc (char-from-code code)))))))) + +(define + js-string-proto-fn-length + (fn + (name) + (cond + ((= name "concat") 1) + ((= name "indexOf") 1) + ((= name "lastIndexOf") 1) + ((= name "slice") 2) + ((= name "substring") 2) + ((= name "substr") 2) + ((= name "split") 2) + ((= name "replace") 2) + ((= name "replaceAll") 2) + ((= name "match") 1) + ((= name "matchAll") 1) + ((= name "search") 1) + ((= name "charAt") 1) + ((= name "charCodeAt") 1) + ((= name "codePointAt") 1) + ((= name "at") 1) + ((= name "padStart") 1) + ((= name "padEnd") 1) + ((= name "repeat") 1) + ((= name "startsWith") 1) + ((= name "endsWith") 1) + ((= name "includes") 1) + ((= name "localeCompare") 1) + ((= name "normalize") 0) + (else 0)))) (define js-string-proto-fn (fn (name) - (fn - (&rest args) - (let - ((this-val (js-this))) - (js-invoke-method (js-to-string this-val) name args))))) + {:__callable__ + (fn + (&rest args) + (let + ((this-val (js-this))) + (let + ((s (cond ((or (= this-val nil) (js-undefined? this-val)) (raise (js-new-call TypeError (js-args (str "String.prototype." name " called on null or undefined"))))) ((= (type-of this-val) "string") this-val) ((and (= (type-of this-val) "dict") (contains? (keys this-val) "__js_string_value__")) (get this-val "__js_string_value__")) (else (js-to-string this-val))))) + (js-invoke-method s name args)))) + :length (js-string-proto-fn-length name) + :name name})) -(define String {:fromCharCode js-string-from-char-code :__callable__ (fn (&rest args) (if (= (len args) 0) "" (js-to-string (nth args 0)))) :prototype {:toLowerCase (js-string-proto-fn "toLowerCase") :concat (js-string-proto-fn "concat") :startsWith (js-string-proto-fn "startsWith") :padEnd (js-string-proto-fn "padEnd") :codePointAt (js-string-proto-fn "codePointAt") :lastIndexOf (js-string-proto-fn "lastIndexOf") :indexOf (js-string-proto-fn "indexOf") :localeCompare (js-string-proto-fn "localeCompare") :split (js-string-proto-fn "split") :endsWith (js-string-proto-fn "endsWith") :trim (js-string-proto-fn "trim") :valueOf (js-string-proto-fn "valueOf") :at (js-string-proto-fn "at") :normalize (js-string-proto-fn "normalize") :substring (js-string-proto-fn "substring") :replaceAll (js-string-proto-fn "replaceAll") :repeat (js-string-proto-fn "repeat") :padStart (js-string-proto-fn "padStart") :search (js-string-proto-fn "search") :toUpperCase (js-string-proto-fn "toUpperCase") :trimEnd (js-string-proto-fn "trimEnd") :toString (js-string-proto-fn "toString") :toLocaleLowerCase (js-string-proto-fn "toLocaleLowerCase") :charCodeAt (js-string-proto-fn "charCodeAt") :slice (js-string-proto-fn "slice") :charAt (js-string-proto-fn "charAt") :match (js-string-proto-fn "match") :includes (js-string-proto-fn "includes") :trimStart (js-string-proto-fn "trimStart") :toLocaleUpperCase (js-string-proto-fn "toLocaleUpperCase") :replace (js-string-proto-fn "replace")} :raw (fn (&rest args) (if (empty? args) "" (js-to-string (nth args 0))))}) +(define String {:raw (fn (&rest args) (if (empty? args) "" (js-to-string (nth args 0)))) :prototype {:replace (js-string-proto-fn "replace") :toLocaleUpperCase (js-string-proto-fn "toLocaleUpperCase") :trimStart (js-string-proto-fn "trimStart") :includes (js-string-proto-fn "includes") :charAt (js-string-proto-fn "charAt") :match (js-string-proto-fn "match") :charCodeAt (js-string-proto-fn "charCodeAt") :slice (js-string-proto-fn "slice") :toString (js-string-proto-fn "toString") :toLocaleLowerCase (js-string-proto-fn "toLocaleLowerCase") :toUpperCase (js-string-proto-fn "toUpperCase") :trimEnd (js-string-proto-fn "trimEnd") :repeat (js-string-proto-fn "repeat") :padStart (js-string-proto-fn "padStart") :search (js-string-proto-fn "search") :substring (js-string-proto-fn "substring") :replaceAll (js-string-proto-fn "replaceAll") :trim (js-string-proto-fn "trim") :valueOf (js-string-proto-fn "valueOf") :at (js-string-proto-fn "at") :normalize (js-string-proto-fn "normalize") :split (js-string-proto-fn "split") :endsWith (js-string-proto-fn "endsWith") :indexOf (js-string-proto-fn "indexOf") :localeCompare (js-string-proto-fn "localeCompare") :toLowerCase (js-string-proto-fn "toLowerCase") :concat (js-string-proto-fn "concat") :startsWith (js-string-proto-fn "startsWith") :padEnd (js-string-proto-fn "padEnd") :codePointAt (js-string-proto-fn "codePointAt") :lastIndexOf (js-string-proto-fn "lastIndexOf")} :__callable__ (fn (&rest args) (if (= (len args) 0) "" (js-to-string (nth args 0)))) :fromCharCode js-string-from-char-code}) (dict-set! String "length" 1) @@ -3038,12 +5248,88 @@ (dict-set! String "name" "String") +(dict-set! String "fromCodePoint" js-string-from-code-point) + +(dict-set! String "fromCharCode" js-string-from-char-code) + +(dict-set! + String + "__callable__" + (fn + (&rest args) + (let + ((raw (if (= (len args) 0) "" (js-to-string (nth args 0))))) + (let + ((this-val (js-this))) + (if + (and + (dict? this-val) + (contains? (keys this-val) "__proto__") + (= (get this-val "__proto__") (get String "prototype"))) + (begin + (dict-set! this-val "__js_string_value__" raw) + (dict-set! this-val "length" (len raw)) + this-val) + raw))))) + (define Boolean {:__callable__ (fn (&rest args) (if (= (len args) 0) false (js-to-boolean (nth args 0))))}) +(dict-set! + (get String "prototype") + "matchAll" + (js-string-proto-fn "matchAll")) + (dict-set! Boolean "length" 1) (dict-set! Boolean "name" "Boolean") +(dict-set! Boolean "prototype" {:constructor Boolean}) + +(dict-set! + Boolean + "__callable__" + (fn + (&rest args) + (let + ((val (if (> (len args) 0) (js-to-boolean (nth args 0)) false))) + (let + ((this-val (js-this))) + (if + (dict? this-val) + (begin + (dict-set! this-val "__js_boolean_value__" val) + (dict-set! this-val "__proto__" (get Boolean "prototype")) + this-val) + (if val true false)))))) + +(dict-set! + (get Boolean "prototype") + "valueOf" + (fn + (&rest args) + (let + ((this-val (js-this))) + (cond + ((= (type-of this-val) "boolean") this-val) + ((and + (= (type-of this-val) "dict") + (contains? (keys this-val) "__js_boolean_value__")) + (get this-val "__js_boolean_value__")) + (else (raise (js-new-call TypeError (js-args "Boolean.prototype.valueOf requires a Boolean")))))))) + +(dict-set! + (get Boolean "prototype") + "toString" + (fn + (&rest args) + (let + ((this-val (js-this))) + (cond + ((= (type-of this-val) "boolean") (if this-val "true" "false")) + ((and (= (type-of this-val) "dict") (contains? (keys this-val) "__js_boolean_value__")) + (if (get this-val "__js_boolean_value__") "true" "false")) + (else (raise (js-new-call TypeError (js-args "Boolean.prototype.toString requires a Boolean")))))))) + (define parseInt (fn @@ -3054,7 +5340,10 @@ (let ((s (js-to-string (nth args 0))) (radix-arg - (if (< (len args) 2) 10 (js-to-number (nth args 1))))) + (if + (< (len args) 2) + 10 + (js-to-number (nth args 1))))) (let ((radix (if (or (js-number-is-nan radix-arg) (= radix-arg 0)) 10 radix-arg))) (js-parse-int-str (js-trim s) (js-math-trunc radix)))))))) @@ -3131,11 +5420,44 @@ js-parse-float-prefix (fn (s) - (let - ((end (js-float-prefix-end s 0 false false false))) - (cond - ((= end 0) (js-nan-value)) - (else (js-parse-num-safe (js-string-slice s 0 end))))))) + (cond + ((js-float-has-infinity-prefix? s 0) + (js-infinity-value)) + ((and + (>= (len s) 1) + (= (char-at s 0) "+") + (js-float-has-infinity-prefix? s 1)) + (js-infinity-value)) + ((and + (>= (len s) 1) + (= (char-at s 0) "-") + (js-float-has-infinity-prefix? s 1)) + (- 0 (js-infinity-value))) + (else + (let + ((end (js-float-prefix-end s 0 false false false))) + (cond + ((= end 0) (js-nan-value)) + ((not (js-str-has-digit? s 0 end)) (js-nan-value)) + (else (js-parse-num-safe (js-string-slice s 0 end))))))))) + +(define + js-str-has-digit? + (fn + (s i n) + (cond + ((>= i n) false) + ((let ((c (char-at s i))) (and (>= (char-code c) 48) (<= (char-code c) 57))) + true) + (else (js-str-has-digit? s (+ i 1) n))))) + +(define + js-float-has-infinity-prefix? + (fn + (s i) + (and + (>= (len s) (+ i 8)) + (= (js-string-slice s i (+ i 8)) "Infinity")))) (define js-float-prefix-end @@ -3160,19 +5482,172 @@ ((prev (char-at s (- i 1)))) (if (or (= prev "e") (= prev "E")) - (js-float-prefix-end s (+ i 1) sawdigit sawdot sawe) + (js-float-prefix-end + s + (+ i 1) + sawdigit + sawdot + sawe) i))) (else i))))))) (define encodeURIComponent - (fn (v) (let ((s (js-to-string v))) (js-uri-encode-loop s 0 "")))) + (fn + (v) + (let ((s (js-to-string v))) (js-uri-encode-loop s 0 "")))) -(define decodeURIComponent (fn (v) (js-to-string v))) +(define + decodeURIComponent + (fn (v) (let ((s (js-to-string v))) (js-uri-decode s 0 "" false)))) (define encodeURI (fn (v) (js-to-string v))) -(define decodeURI (fn (v) (js-to-string v))) +(define + decodeURI + (fn (v) (let ((s (js-to-string v))) (js-uri-decode s 0 "" true)))) + +(define + js-uri-malformed! + (fn () (raise (js-new-call URIError (js-args "URI malformed"))))) + +(define + js-uri-reserved-byte? + (fn + (b) + (or + (= b 35) + (= b 36) + (= b 38) + (= b 43) + (= b 44) + (= b 47) + (= b 58) + (= b 59) + (= b 61) + (= b 63) + (= b 64)))) + +(define + js-uri-hex-val + (fn + (c) + (let + ((cc (char-code c))) + (cond + ((and (>= cc 48) (<= cc 57)) (- cc 48)) + ((and (>= cc 65) (<= cc 70)) (- cc 55)) + ((and (>= cc 97) (<= cc 102)) (- cc 87)) + (else -1))))) + +(define + js-uri-hex-pair + (fn + (s i) + (cond + ((>= (+ i 1) (len s)) -1) + (else + (let + ((d1 (js-uri-hex-val (char-at s i))) + (d2 (js-uri-hex-val (char-at s (+ i 1))))) + (cond + ((or (= d1 -1) (= d2 -1)) -1) + (else (+ (* d1 16) d2)))))))) + +(define + js-uri-decode + (fn + (s i acc preserveReserved) + (cond + ((>= i (len s)) acc) + ((not (= (char-at s i) "%")) + (js-uri-decode + s + (+ i 1) + (str acc (char-at s i)) + preserveReserved)) + ((> (+ i 3) (len s)) (js-uri-malformed!)) + (else + (let + ((b (js-uri-hex-pair s (+ i 1)))) + (cond + ((= b -1) (js-uri-malformed!)) + ((< b 128) + (cond + ((and preserveReserved (js-uri-reserved-byte? b)) + (js-uri-decode + s + (+ i 3) + (str acc (char-at s i) (char-at s (+ i 1)) (char-at s (+ i 2))) + preserveReserved)) + (else + (js-uri-decode + s + (+ i 3) + (str acc (char-from-code b)) + preserveReserved)))) + (else (js-uri-decode-multi s i acc preserveReserved b)))))))) + +(define + js-uri-decode-multi + (fn + (s i acc preserveReserved b1) + (let + ((n + (cond + ((< b1 192) -1) + ((< b1 224) 2) + ((< b1 240) 3) + ((< b1 248) 4) + (else -1))) + (head-bits + (cond + ((< b1 192) 0) + ((< b1 224) (mod b1 32)) + ((< b1 240) (mod b1 16)) + ((< b1 248) (mod b1 8)) + (else 0)))) + (cond + ((= n -1) (js-uri-malformed!)) + (else + (js-uri-decode-multi-loop s i acc preserveReserved n 1 head-bits)))))) + +(define + js-uri-decode-multi-loop + (fn + (s i acc preserveReserved n k cp) + (cond + ((>= k n) + (cond + ((and (>= cp 55296) (<= cp 57343)) (js-uri-malformed!)) + ((> cp 1114111) (js-uri-malformed!)) + (else + (js-uri-decode + s + (+ i (* 3 n)) + (str acc (char-from-code cp)) + preserveReserved)))) + (else + (let + ((p (+ i (* 3 k)))) + (cond + ((>= (+ p 3) (+ (len s) 1)) (js-uri-malformed!)) + ((not (= (char-at s p) "%")) (js-uri-malformed!)) + (else + (let + ((b (js-uri-hex-pair s (+ p 1)))) + (cond + ((= b -1) (js-uri-malformed!)) + ((or (< b 128) (>= b 192)) (js-uri-malformed!)) + (else + (js-uri-decode-multi-loop + s + i + acc + preserveReserved + n + (+ k 1) + (+ (* cp 64) (mod b 64))))))))))))) (define js-uri-encode-loop @@ -3186,7 +5661,8 @@ (let ((code (char-code c))) (cond - ((= c " ") (js-uri-encode-loop s (+ i 1) (str acc "%20"))) + ((= c " ") + (js-uri-encode-loop s (+ i 1) (str acc "%20"))) ((and (>= code 48) (<= code 57)) (js-uri-encode-loop s (+ i 1) (str acc c))) ((and (>= code 65) (<= code 90)) @@ -3196,7 +5672,10 @@ ((or (= c "-") (= c "_") (= c ".") (= c "~") (= c "!") (= c "*") (= c "'") (= c "(") (= c ")")) (js-uri-encode-loop s (+ i 1) (str acc c))) (else - (js-uri-encode-loop s (+ i 1) (str acc "%" (js-hex-2 code))))))))))) + (js-uri-encode-loop + s + (+ i 1) + (str acc "%" (js-hex-2 code))))))))))) (define js-hex-2 @@ -3210,57 +5689,209 @@ js-hex-digit (fn (d) - (cond ((< d 10) (js-to-string d)) (else (js-code-to-char (+ 55 d)))))) + (cond + ((< d 10) (js-to-string d)) + (else (js-code-to-char (+ 55 d)))))) (define js-json-stringify (fn (&rest args) - (if - (= (len args) 0) - js-undefined - (js-json-stringify-value (nth args 0))))) + (let + ((value (if (= (len args) 0) :js-undefined (nth args 0))) + (replacer (if (< (len args) 2) :js-undefined (nth args 1))) + (space-raw (if (< (len args) 3) :js-undefined (nth args 2)))) + (let + ((rep-fn (if (js-function? replacer) replacer nil)) + (rep-keys (if (list? replacer) (js-json-prop-list replacer) nil)) + (gap (js-json-space-gap space-raw))) + (let + ((wrapper (dict))) + (begin + (dict-set! wrapper "" value) + (js-json-serialize-property "" wrapper rep-fn rep-keys gap ""))))))) (define - js-json-stringify-value + js-json-prop-list + (fn + (arr) + (let + ((out (list))) + (begin + (for-each + (fn + (k) + (cond + ((= (type-of k) "string") + (if (js-list-contains? out k) nil (append! out k))) + ((number? k) + (let ((s (js-number-to-string k))) + (if (js-list-contains? out s) nil (append! out s)))) + ((dict? k) + (cond + ((contains? (keys k) "__js_string_value__") + (let ((s (get k "__js_string_value__"))) + (if (js-list-contains? out s) nil (append! out s)))) + ((contains? (keys k) "__js_number_value__") + (let ((s (js-number-to-string (get k "__js_number_value__")))) + (if (js-list-contains? out s) nil (append! out s)))) + (else nil))) + (else nil))) + arr) + out)))) + +(define + js-list-contains? + (fn + (lst v) + (cond + ((empty? lst) false) + ((= (first lst) v) true) + (else (js-list-contains? (rest lst) v))))) + +(define + js-json-space-gap + (fn + (sp) + (cond + ((js-undefined? sp) "") + ((= sp nil) "") + ((number? sp) + (let + ((n (cond ((js-number-is-nan sp) 0) ((< sp 0) 0) ((> sp 10) 10) (else (floor sp))))) + (js-string-repeat " " n))) + ((and (dict? sp) (contains? (keys sp) "__js_number_value__")) + (js-json-space-gap (get sp "__js_number_value__"))) + ((and (dict? sp) (contains? (keys sp) "__js_string_value__")) + (js-json-space-gap (get sp "__js_string_value__"))) + ((= (type-of sp) "string") + (if (> (len sp) 10) (js-string-slice sp 0 10) sp)) + (else "")))) + +(define + js-json-unwrap-primitive (fn (v) (cond - ((= v nil) "null") - ((js-undefined? v) js-undefined) - ((= (type-of v) "boolean") (if v "true" "false")) - ((number? v) (js-number-to-string v)) - ((= (type-of v) "string") (js-json-escape-string v)) - ((list? v) + ((not (dict? v)) v) + ((contains? (keys v) "__js_number_value__") + (get v "__js_number_value__")) + ((contains? (keys v) "__js_string_value__") + (get v "__js_string_value__")) + ((contains? (keys v) "__js_boolean_value__") + (get v "__js_boolean_value__")) + (else v)))) + +(define + js-json-serialize-property + (fn + (key holder rep-fn rep-keys gap indent) + (let + ((value0 (if (dict? holder) (get holder key) (if (list? holder) (nth holder (js-num-to-int (js-to-number key))) :js-undefined)))) + (let + ((value1 + (cond + ((and + (or (dict? value0) (list? value0)) + (let ((tj (js-get-prop value0 "toJSON"))) + (and (not (js-undefined? tj)) (js-function? tj)))) + (js-call-with-this value0 (js-get-prop value0 "toJSON") (list key))) + (else value0)))) (let - ((parts (list))) - (for-each - (fn - (x) - (let - ((s (js-json-stringify-value x))) - (if - (js-undefined? s) - (append! parts "null") - (append! parts s)))) - v) - (str "[" (join "," parts) "]"))) - ((dict? v) + ((value + (if rep-fn + (js-call-with-this holder rep-fn (list key value1)) + value1))) + (let + ((vu (js-json-unwrap-primitive value))) + (cond + ((= vu nil) "null") + ((js-undefined? vu) :js-undefined) + ((= (type-of vu) "boolean") (if vu "true" "false")) + ((or (number? vu) (= (type-of vu) "rational")) + (let ((n (if (= (type-of vu) "rational") (exact->inexact vu) vu))) + (cond + ((js-number-is-nan n) "null") + ((= n (js-infinity-value)) "null") + ((= n (- 0 (js-infinity-value))) "null") + (else (js-number-to-string n))))) + ((= (type-of vu) "string") (js-json-escape-string vu)) + ((js-function? vu) :js-undefined) + ((list? vu) + (js-json-serialize-array vu rep-fn rep-keys gap indent)) + ((dict? vu) + (js-json-serialize-object vu rep-fn rep-keys gap indent)) + (else :js-undefined)))))))) + +(define + js-json-serialize-array + (fn + (arr rep-fn rep-keys gap indent) + (let + ((step-back indent) (new-indent (str indent gap)) (parts (list))) + (begin + (js-json-array-loop arr rep-fn rep-keys gap new-indent 0 parts) + (cond + ((empty? parts) "[]") + ((= gap "") + (str "[" (join "," parts) "]")) + (else + (str + "[\n" + new-indent + (join (str ",\n" new-indent) parts) + "\n" + step-back + "]"))))))) + +(define + js-json-array-loop + (fn + (arr rep-fn rep-keys gap new-indent i parts) + (cond + ((>= i (len arr)) nil) + (else (let - ((parts (list))) - (for-each - (fn - (k) - (let - ((val (get v k))) + ((s (js-json-serialize-property (js-number-to-string i) arr rep-fn rep-keys gap new-indent))) + (begin + (if (js-undefined? s) (append! parts "null") (append! parts s)) + (js-json-array-loop arr rep-fn rep-keys gap new-indent (+ i 1) parts))))))) + +(define + js-json-serialize-object + (fn + (obj rep-fn rep-keys gap indent) + (let + ((step-back indent) (new-indent (str indent gap)) (parts (list)) + (sep (if (= gap "") ":" ": ")) + (key-list (if rep-keys rep-keys (js-object-keys obj)))) + (begin + (for-each + (fn + (k) + (cond + ((js-key-internal? k) nil) + (else (let - ((vs (js-json-stringify-value val))) + ((s (js-json-serialize-property k obj rep-fn rep-keys gap new-indent))) (if - (not (js-undefined? vs)) - (append! parts (str (js-json-escape-string k) ":" vs)))))) - (keys v)) - (str "{" (join "," parts) "}"))) - (else "null")))) + (js-undefined? s) + nil + (append! parts (str (js-json-escape-string k) sep s))))))) + key-list) + (cond + ((empty? parts) "{}") + ((= gap "") + (str "{" (join "," parts) "}")) + (else + (str + "{\n" + new-indent + (join (str ",\n" new-indent) parts) + "\n" + step-back + "}"))))))) + (define js-json-escape-string @@ -3276,11 +5907,16 @@ (let ((c (char-at s i))) (cond - ((= c "\"") (js-json-escape-loop s (+ i 1) (str acc "\\\""))) - ((= c "\\") (js-json-escape-loop s (+ i 1) (str acc "\\\\"))) - ((= c "\n") (js-json-escape-loop s (+ i 1) (str acc "\\n"))) - ((= c "\r") (js-json-escape-loop s (+ i 1) (str acc "\\r"))) - ((= c "\t") (js-json-escape-loop s (+ i 1) (str acc "\\t"))) + ((= c "\"") + (js-json-escape-loop s (+ i 1) (str acc "\\\""))) + ((= c "\\") + (js-json-escape-loop s (+ i 1) (str acc "\\\\"))) + ((= c "\n") + (js-json-escape-loop s (+ i 1) (str acc "\\n"))) + ((= c "\r") + (js-json-escape-loop s (+ i 1) (str acc "\\r"))) + ((= c "\t") + (js-json-escape-loop s (+ i 1) (str acc "\\t"))) (else (js-json-escape-loop s (+ i 1) (str acc c)))))))) (define @@ -3289,12 +5925,21 @@ (&rest args) (if (= (len args) 0) - js-undefined + (raise (js-new-call SyntaxError (js-args "Unexpected token undefined"))) (let ((st (dict))) (dict-set! st "s" (js-to-string (nth args 0))) (dict-set! st "i" 0) - (js-json-parse-value st))))) + (let + ((result (js-json-parse-value st))) + (begin + (js-json-skip-ws! st) + (if + (< (get st "i") (len (get st "s"))) + (raise + (js-new-call SyntaxError + (js-args (str "Unexpected token at position " (get st "i"))))) + result))))))) (define js-json-skip-ws! @@ -3316,13 +5961,16 @@ (let ((s (get st "s")) (i (get st "i"))) (cond - ((>= i (len s)) (error "JSON: unexpected end")) + ((>= i (len s)) (raise (js-new-call SyntaxError (js-args "JSON: unexpected end")))) ((= (char-at s i) "\"") (js-json-parse-string st)) ((= (char-at s i) "[") (js-json-parse-array st)) ((= (char-at s i) "{") (js-json-parse-object st)) - ((= (char-at s i) "t") (begin (dict-set! st "i" (+ i 4)) true)) - ((= (char-at s i) "f") (begin (dict-set! st "i" (+ i 5)) false)) - ((= (char-at s i) "n") (begin (dict-set! st "i" (+ i 4)) nil)) + ((= (char-at s i) "t") + (begin (dict-set! st "i" (+ i 4)) true)) + ((= (char-at s i) "f") + (begin (dict-set! st "i" (+ i 5)) false)) + ((= (char-at s i) "n") + (begin (dict-set! st "i" (+ i 4)) nil)) (else (js-json-parse-number st)))))) (define @@ -3345,8 +5993,13 @@ (let ((i (get st "i"))) (cond - ((>= i (len s)) nil) + ((>= i (len s)) + (raise (js-new-call SyntaxError (js-args "JSON: unterminated string")))) ((= (char-at s i) "\"") nil) + ((< (char-code (char-at s i)) 32) + (raise + (js-new-call SyntaxError + (js-args "JSON: control character in string")))) ((= (char-at s i) "\\") (begin (when @@ -3422,7 +6075,7 @@ (js-json-skip-ws! st) (js-json-parse-array-loop st result))) ((= c "]") (dict-set! st "i" (+ (get st "i") 1))) - (else (error "JSON: expected , or ]")))))) + (else (raise (js-new-call SyntaxError (js-args "JSON: expected , or ]")))))))) (define js-json-parse-object @@ -3447,7 +6100,7 @@ (js-json-skip-ws! st) (when (not (= (char-at (get st "s") (get st "i")) ":")) - (error "JSON: expected :")) + (raise (js-new-call SyntaxError (js-args "JSON: expected :")))) (dict-set! st "i" (+ (get st "i") 1)) (let ((v (js-json-parse-value st))) (dict-set! result k v)) (js-json-skip-ws! st) @@ -3459,9 +6112,9 @@ (dict-set! st "i" (+ (get st "i") 1)) (js-json-parse-object-loop st result))) ((= c "}") (dict-set! st "i" (+ (get st "i") 1))) - (else (error "JSON: expected , or }"))))))) + (else (raise (js-new-call SyntaxError (js-args "JSON: expected , or }"))))))))) -(define JSON {:parse js-json-parse :stringify js-json-stringify}) +(define JSON {:stringify js-json-stringify :parse js-json-parse}) (define js-promise-flush-callbacks! @@ -3564,7 +6217,11 @@ (p args) (let ((on-f (if (>= (len args) 1) (nth args 0) :js-undefined)) - (on-r (if (>= (len args) 2) (nth args 1) :js-undefined))) + (on-r + (if + (>= (len args) 2) + (nth args 1) + :js-undefined))) (js-promise-then-internal! p on-f on-r)))) (define @@ -3663,7 +6320,9 @@ (cond ((<= n 0) acc) (else - (begin (append! acc fill) (js-make-list-loop acc (- n 1) fill)))))) + (begin + (append! acc fill) + (js-make-list-loop acc (- n 1) fill)))))) (define js-promise-all-loop! @@ -3683,7 +6342,10 @@ (let ((results (get state "results"))) (set-nth! results i v) - (dict-set! state "remaining" (- (get state "remaining") 1)) + (dict-set! + state + "remaining" + (- (get state "remaining") 1)) (cond ((= (get state "remaining") 0) (js-promise-resolve! result-p results)) @@ -3699,7 +6361,8 @@ ((items (if (empty? args) (list) (first args))) (p (js-make-promise))) (cond - ((= (len items) 0) (begin (js-promise-resolve! p (list)) p)) + ((= (len items) 0) + (begin (js-promise-resolve! p (list)) p)) (else (let ((n (len items)) (state (dict))) @@ -3858,21 +6521,476 @@ ((= name "test") (let ((impl (get __js_regex_platform__ "test")) - (arg (if (= (len args) 0) "" (js-to-string (nth args 0))))) - (if - (js-undefined? impl) - (js-regex-stub-test rx arg) - (impl rx arg)))) + (arg + (if + (= (len args) 0) + "" + (js-to-string (nth args 0))))) + (cond + ((or (js-undefined? impl) (= impl nil)) + (js-regex-stub-test rx arg)) + (else (impl rx arg))))) ((= name "exec") (let ((impl (get __js_regex_platform__ "exec")) - (arg (if (= (len args) 0) "" (js-to-string (nth args 0))))) - (if - (js-undefined? impl) - (js-regex-stub-exec rx arg) - (impl rx arg)))) + (arg + (if + (= (len args) 0) + "" + (js-to-string (nth args 0))))) + (cond + ((or (js-undefined? impl) (= impl nil)) + (js-regex-stub-exec rx arg)) + (else (impl rx arg))))) ((= name "toString") (str "/" (get rx "source") "/" (get rx "flags"))) (else js-undefined)))) -(define js-global {:isFinite js-global-is-finite :console console :Number Number :parseFloat parseFloat :Math Math :Array Array :Boolean Boolean :String String :NaN 0 :Infinity inf :isNaN js-global-is-nan :Object Object :parseInt parseInt :JSON JSON :undefined js-undefined}) +(define + js-list-find-index + (fn + (lst v i n) + (cond + ((>= i n) -1) + ((= (nth lst i) v) i) + (else (js-list-find-index lst v (+ i 1) n))))) + +(define + js-list-remove-at! + (fn + (lst i) + (let + ((n (len lst)) (kept (list))) + (begin + (js-list-remove-at-loop lst i n 0 kept) + kept)))) + +(define + js-list-remove-at-loop + (fn + (src skip n j out) + (cond + ((>= j n) nil) + ((= j skip) (js-list-remove-at-loop src skip n (+ j 1) out)) + (else + (begin + (append! out (nth src j)) + (js-list-remove-at-loop src skip n (+ j 1) out)))))) + +(define + js-map-ctor-fn + (fn + (&rest args) + (let + ((this (js-this))) + (cond + ((not (= (type-of this) "dict")) + (raise (js-new-call TypeError (js-args "Map must be constructed with new")))) + (else + (begin + (dict-set! this "__map_keys__" (list)) + (dict-set! this "__map_vals__" (list)) + (dict-set! this "size" 0) + (if + (and + (>= (len args) 1) + (not (js-undefined? (nth args 0))) + (not (= (nth args 0) nil))) + (js-map-init this (nth args 0)) + nil) + this)))))) + +(define + js-map-init + (fn + (m iter) + (let + ((entries (js-iterable-to-list iter))) + (for-each + (fn + (entry) + (cond + ((list? entry) + (js-map-do-set m (nth entry 0) (nth entry 1))) + (else nil))) + entries)))) + +(define + js-map-check! + (fn + (m method) + (cond + ((or (not (dict? m)) (not (contains? (keys m) "__map_keys__"))) + (raise + (js-new-call TypeError + (js-args (str "Map.prototype." method " called on non-Map object"))))) + (else nil)))) + +(define + js-map-do-set + (fn + (m k v) + (begin + (js-map-check! m "set") + (let + ((ks (get m "__map_keys__")) (vs (get m "__map_vals__"))) + (let + ((idx (js-list-find-index ks k 0 (len ks)))) + (cond + ((>= idx 0) (begin (set-nth! vs idx v) m)) + (else + (begin + (append! ks k) + (append! vs v) + (dict-set! m "size" (len ks)) + m)))))))) + +(define + js-map-do-get + (fn + (m k) + (begin + (js-map-check! m "get") + (let + ((ks (get m "__map_keys__")) (vs (get m "__map_vals__"))) + (let + ((idx (js-list-find-index ks k 0 (len ks)))) + (cond ((>= idx 0) (nth vs idx)) (else js-undefined))))))) + +(define + js-map-do-has + (fn + (m k) + (begin + (js-map-check! m "has") + (let + ((ks (get m "__map_keys__"))) + (>= (js-list-find-index ks k 0 (len ks)) 0))))) + +(define + js-map-do-delete + (fn + (m k) + (begin + (js-map-check! m "delete") + (let + ((ks (get m "__map_keys__")) (vs (get m "__map_vals__"))) + (let + ((idx (js-list-find-index ks k 0 (len ks)))) + (cond + ((< idx 0) false) + (else + (let + ((new-ks (js-list-remove-at! ks idx)) + (new-vs (js-list-remove-at! vs idx))) + (begin + (dict-set! m "__map_keys__" new-ks) + (dict-set! m "__map_vals__" new-vs) + (dict-set! m "size" (len new-ks)) + true))))))))) + +(define + js-map-do-clear + (fn + (m) + (begin + (js-map-check! m "clear") + (dict-set! m "__map_keys__" (list)) + (dict-set! m "__map_vals__" (list)) + (dict-set! m "size" 0) + js-undefined))) + +(define + js-map-do-foreach + (fn + (m cb &rest opts) + (let + ((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 this-arg m 0 (len ks)) + js-undefined)))) + +(define + js-map-foreach-loop + (fn + (ks vs cb this-arg m i n) + (cond + ((>= i n) nil) + (else + (begin + (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 + {:length 0 + :name "Map" + :__callable__ js-map-ctor-fn + :prototype + {:get (fn (k) (js-map-do-get (js-this) k)) + :set (fn (k v) (js-map-do-set (js-this) k v)) + :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 (&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 + (fn () + (let + ((ks (get (js-this) "__map_keys__")) + (vs (get (js-this) "__map_vals__")) + (out (list))) + (begin + (js-map-entries-loop ks vs 0 (len ks) out) + out)))}}) + +(dict-set! (get Map "prototype") "constructor" Map) + +(define + js-list-copy + (fn + (src) + (let + ((out (list))) + (begin (for-each (fn (x) (append! out x)) src) out)))) + +(define + js-map-entries-loop + (fn + (ks vs i n out) + (cond + ((>= i n) nil) + (else + (begin + (append! out (list (nth ks i) (nth vs i))) + (js-map-entries-loop ks vs (+ i 1) n out)))))) + +(define + js-set-ctor-fn + (fn + (&rest args) + (let + ((this (js-this))) + (cond + ((not (= (type-of this) "dict")) + (raise (js-new-call TypeError (js-args "Set must be constructed with new")))) + (else + (begin + (dict-set! this "__set_items__" (list)) + (dict-set! this "size" 0) + (if + (and + (>= (len args) 1) + (not (js-undefined? (nth args 0))) + (not (= (nth args 0) nil))) + (js-set-init this (nth args 0)) + nil) + this)))))) + +(define + js-set-init + (fn + (s iter) + (let + ((items (js-iterable-to-list iter))) + (for-each (fn (x) (js-set-do-add s x)) items)))) + +(define + js-set-check! + (fn + (s method) + (cond + ((or (not (dict? s)) (not (contains? (keys s) "__set_items__"))) + (raise + (js-new-call TypeError + (js-args (str "Set.prototype." method " called on non-Set object"))))) + (else nil)))) + +(define + js-set-do-add + (fn + (s v) + (begin + (js-set-check! s "add") + (let + ((items (get s "__set_items__"))) + (let + ((idx (js-list-find-index items v 0 (len items)))) + (cond + ((>= idx 0) s) + (else + (begin + (append! items v) + (dict-set! s "size" (len items)) + s)))))))) + +(define + js-set-do-has + (fn + (s v) + (begin + (js-set-check! s "has") + (let + ((items (get s "__set_items__"))) + (>= (js-list-find-index items v 0 (len items)) 0))))) + +(define + js-set-do-delete + (fn + (s v) + (begin + (js-set-check! s "delete") + (let + ((items (get s "__set_items__"))) + (let + ((idx (js-list-find-index items v 0 (len items)))) + (cond + ((< idx 0) false) + (else + (let + ((new-items (js-list-remove-at! items idx))) + (begin + (dict-set! s "__set_items__" new-items) + (dict-set! s "size" (len new-items)) + true))))))))) + +(define + js-set-do-clear + (fn + (s) + (begin + (js-set-check! s "clear") + (dict-set! s "__set_items__" (list)) + (dict-set! s "size" 0) + js-undefined))) + +(define + js-set-do-foreach + (fn + (s cb &rest opts) + (let + ((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 this-arg s 0 (len items)) + js-undefined)))) + +(define + js-set-foreach-loop + (fn + (items cb this-arg s i n) + (cond + ((>= i n) nil) + (else + (begin + (js-call-with-this + this-arg + cb + (list (nth items i) (nth items i) s)) + (js-set-foreach-loop items cb this-arg s (+ i 1) n)))))) + +(define + Set + {:length 0 + :name "Set" + :__callable__ js-set-ctor-fn + :prototype + {:add (fn (v) (js-set-do-add (js-this) v)) + :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 (&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 + (fn () + (let + ((items (get (js-this) "__set_items__")) (out (list))) + (begin + (js-set-entries-loop items 0 (len items) out) + out)))}}) + +(dict-set! (get Set "prototype") "constructor" Set) + +(define + js-set-entries-loop + (fn + (items i n out) + (cond + ((>= i n) nil) + (else + (begin + (append! out (list (nth items i) (nth items i))) + (js-set-entries-loop items (+ i 1) n out)))))) + +(begin + (dict-set! Object "__proto__" (get js-function-global "prototype")) + (dict-set! Array "__proto__" (get js-function-global "prototype")) + (dict-set! Number "__proto__" (get js-function-global "prototype")) + (dict-set! String "__proto__" (get js-function-global "prototype")) + (dict-set! Boolean "__proto__" (get js-function-global "prototype")) + (dict-set! Map "__proto__" (get js-function-global "prototype")) + (dict-set! Set "__proto__" (get js-function-global "prototype")) + (dict-set! Date "__proto__" (get js-function-global "prototype")) + (dict-set! RegExp "__proto__" (get js-function-global "prototype")) + (dict-set! __js_ctor_proto__ (js-ctor-id TypeError) Error) + (dict-set! __js_ctor_proto__ (js-ctor-id RangeError) Error) + (dict-set! __js_ctor_proto__ (js-ctor-id SyntaxError) Error) + (dict-set! __js_ctor_proto__ (js-ctor-id ReferenceError) Error) + (dict-set! __js_ctor_proto__ (js-ctor-id URIError) Error) + (dict-set! __js_ctor_proto__ (js-ctor-id EvalError) Error) + (dict-set! (js-get-ctor-proto TypeError) "__proto__" (js-get-ctor-proto Error)) + (dict-set! (js-get-ctor-proto RangeError) "__proto__" (js-get-ctor-proto Error)) + (dict-set! (js-get-ctor-proto SyntaxError) "__proto__" (js-get-ctor-proto Error)) + (dict-set! (js-get-ctor-proto ReferenceError) "__proto__" (js-get-ctor-proto Error)) + (dict-set! (js-get-ctor-proto URIError) "__proto__" (js-get-ctor-proto Error)) + (dict-set! (js-get-ctor-proto EvalError) "__proto__" (js-get-ctor-proto Error)) + (dict-set! (js-get-ctor-proto Error) "__proto__" (get Object "prototype")) + (dict-set! (js-get-ctor-proto Error) "name" "Error") + (dict-set! (js-get-ctor-proto Error) "message" "") + (dict-set! (js-get-ctor-proto Error) "constructor" Error) + (dict-set! (js-get-ctor-proto TypeError) "name" "TypeError") + (dict-set! (js-get-ctor-proto TypeError) "constructor" TypeError) + (dict-set! (js-get-ctor-proto RangeError) "name" "RangeError") + (dict-set! (js-get-ctor-proto RangeError) "constructor" RangeError) + (dict-set! (js-get-ctor-proto SyntaxError) "name" "SyntaxError") + (dict-set! (js-get-ctor-proto SyntaxError) "constructor" SyntaxError) + (dict-set! (js-get-ctor-proto ReferenceError) "name" "ReferenceError") + (dict-set! (js-get-ctor-proto ReferenceError) "constructor" ReferenceError) + (dict-set! (js-get-ctor-proto URIError) "name" "URIError") + (dict-set! (js-get-ctor-proto URIError) "constructor" URIError) + (dict-set! (js-get-ctor-proto EvalError) "name" "EvalError") + (dict-set! (js-get-ctor-proto EvalError) "constructor" EvalError) + (dict-set! (get Array "prototype") "__proto__" (get Object "prototype")) + (dict-set! (get Number "prototype") "__proto__" (get Object "prototype")) + (dict-set! (get String "prototype") "__proto__" (get Object "prototype")) + (dict-set! (get Boolean "prototype") "__proto__" (get Object "prototype")) + (dict-set! (get Map "prototype") "__proto__" (get Object "prototype")) + (dict-set! (get Set "prototype") "__proto__" (get Object "prototype")) + (dict-set! (get Date "prototype") "__proto__" (get Object "prototype")) + (dict-set! (get Date "prototype") "constructor" Date) + (dict-set! (get RegExp "prototype") "__proto__" (get Object "prototype")) + (dict-set! (get RegExp "prototype") "constructor" RegExp) + (dict-set! (get js-function-global "prototype") "__proto__" (get Object "prototype")) + (dict-set! (get Number "prototype") "__js_number_value__" 0) + (dict-set! (get String "prototype") "__js_string_value__" "") + (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 :Map Map :Set Set :Date Date :RegExp RegExp :Function js-function-global :Error Error :TypeError TypeError :RangeError RangeError :SyntaxError SyntaxError :ReferenceError ReferenceError :URIError URIError :EvalError EvalError :encodeURI encodeURI :decodeURI decodeURI :encodeURIComponent encodeURIComponent :decodeURIComponent decodeURIComponent :eval js-global-eval :Promise Promise :Symbol :js-undefined :AggregateError :js-undefined :SuppressedError :js-undefined :globalThis nil}) + +(set! js-global-this js-global) + +(dict-set! js-global "globalThis" js-global) diff --git a/lib/js/test.sh b/lib/js/test.sh index b943a139..1acb8c3e 100755 --- a/lib/js/test.sh +++ b/lib/js/test.sh @@ -1486,6 +1486,24 @@ cat > "$TMPFILE" << 'EPOCHS' (eval "(get (RegExp \"hello\" \"gi\") \"global\")") (epoch 6032) (eval "(get (RegExp \"foo\" \"i\") \"ignoreCase\")") +;; ── Phase 1.ASI: automatic semicolon insertion ───────────────── +(epoch 4200) +(eval "(js-eval \"function f() { return\n42\n} f()\")") +(epoch 4201) +(eval "(js-eval \"function g() { return 42 } g()\")") +(epoch 4202) +(eval "(let ((toks (js-tokenize \"a\nb\"))) (get (nth toks 1) :nl))") +(epoch 4203) +(eval "(let ((toks (js-tokenize \"a b\"))) (get (nth toks 1) :nl))") + +(epoch 4300) +(eval "(js-eval \"var x = 5; x\")") +(epoch 4301) +(eval "(js-eval \"function f() { return x; var x = 42; } f()\")") +(epoch 4302) +(eval "(js-eval \"function f() { var y = 7; return y; } f()\")") +(epoch 4303) +(eval "(js-eval \"function f() { var z; z = 3; return z; } f()\")") EPOCHS @@ -2280,6 +2298,16 @@ check 6025 "set delete→size 0" '0' check 6030 "RegExp? result" 'true' check 6031 "RegExp global flag" 'true' check 6032 "RegExp ignoreCase" 'true' +# ── Phase 1.ASI: automatic semicolon insertion ──────────────────── +check 4200 "return+newline → undefined" '"js-undefined"' +check 4201 "return+space+val → val" '42' +check 4202 "nl-before flag set after newline" 'true' +check 4203 "nl-before flag false on same line" 'false' + +check 4300 "var decl program-level" '5' +check 4301 "var hoisted before use → undef" '"js-undefined"' +check 4302 "var in function body" '7' +check 4303 "var then set in function" '3' TOTAL=$((PASS + FAIL)) if [ $FAIL -eq 0 ]; then diff --git a/lib/js/test262-runner.py b/lib/js/test262-runner.py index 0b803c37..b05a1784 100644 --- a/lib/js/test262-runner.py +++ b/lib/js/test262-runner.py @@ -52,7 +52,7 @@ UPSTREAM = REPO / "lib" / "js" / "test262-upstream" TEST_ROOT = UPSTREAM / "test" HARNESS_DIR = UPSTREAM / "harness" -DEFAULT_PER_TEST_TIMEOUT_S = 5.0 +DEFAULT_PER_TEST_TIMEOUT_S = 15.0 DEFAULT_BATCH_TIMEOUT_S = 120 # Cache dir for precomputed SX source of harness JS (one file per Python run). @@ -134,6 +134,9 @@ var verifyProperty = function (obj, name, desc, opts) { } }; var verifyPrimordialProperty = verifyProperty; +var verifyEqualTo = function (obj, name, value) { + assert.sameValue(obj[name], value, name + " equals"); +}; var verifyNotEnumerable = function (o, n, v, w, x) { }; var verifyNotWritable = function (o, n, v, w, x) { }; var verifyNotConfigurable = function (o, n, v, w, x) { }; @@ -146,6 +149,50 @@ var isConstructor = function (f) { // Best-effort: built-in functions and arrows aren't; declared `function` decls are. return false; }; +// $DONE / asyncTest — async-flag tests call $DONE(err) to signal completion. +// Since we drain microtasks synchronously, $DONE is just a final-assertion sink. +var $DONE = function (err) { + if (err) { throw new Test262Error((err && err.message) || err); } +}; +var asyncTest = function (testFunc) { + Promise.resolve(testFunc()).then(function () { $DONE(); }, function (e) { $DONE(e); }); +}; +// promiseHelper.js include — used by Promise.all/race tests for ordering checks. +var checkSequence = function (arr, message) { + for (var i = 0; i < arr.length; i = i + 1) { + if (arr[i] !== (i + 1)) { + throw new Test262Error((message || "Sequence") + " expected " + (i+1) + " at index " + i + " but got " + arr[i]); + } + } + return true; +}; +var checkSettledPromises = function (settleds, expected, message) { + var msg = message ? message + " " : ""; + if (settleds.length !== expected.length) { + throw new Test262Error(msg + "lengths differ: " + settleds.length + " vs " + expected.length); + } + for (var i = 0; i < settleds.length; i = i + 1) { + if (settleds[i].status !== expected[i].status) { + throw new Test262Error(msg + "status[" + i + "]: " + settleds[i].status + " vs " + expected[i].status); + } + if (expected[i].status === "fulfilled" && settleds[i].value !== expected[i].value) { + throw new Test262Error(msg + "value[" + i + "]: " + settleds[i].value + " vs " + expected[i].value); + } + if (expected[i].status === "rejected" && settleds[i].reason !== expected[i].reason) { + throw new Test262Error(msg + "reason[" + i + "]: " + settleds[i].reason + " vs " + expected[i].reason); + } + } +}; +// decimalToHexString.js include — used by URI/escape tests. +var decimalToHexString = function (n) { + var hex = "0123456789ABCDEF"; + if (n < 0) { n = n + 65536; } + return hex[(n >> 12) & 15] + hex[(n >> 8) & 15] + hex[(n >> 4) & 15] + hex[n & 15]; +}; +var decimalToPercentHexString = function (n) { + var hex = "0123456789ABCDEF"; + return "%" + hex[(n >> 4) & 15] + hex[n & 15]; +}; // Trivial helper for tests that use Array.isArray-like functionality // (many tests reach for it via compareArray) """ @@ -358,6 +405,8 @@ def classify_negative_result(fm: Frontmatter, kind: str, payload: str): or ("expected" in low and "got" in low) or "js-transpile-unop" in low or "js-transpile-binop" in low + or "js-transpile-assign" in low + or "js-transpile" in low or "js-compound-update" in low or "parse" in low ): @@ -1012,11 +1061,45 @@ def _worker_run(args): # --------------------------------------------------------------------------- +_HARNESS_INCLUDE_CACHE: dict = {} + +# Only inline these small harness files per-test. Large ones like propertyHelper.js +# multiply js-eval/JIT cost by ~5-10x and push tests over the per-test timeout. +_INLINE_INCLUDES = {"nans.js", "sta.js", "byteConversionValues.js", "compareArray.js"} + + +def _load_harness_include(name: str) -> str: + """Read an upstream harness include file (e.g. nans.js). + Returns empty string if the file isn't present. + """ + if name in _HARNESS_INCLUDE_CACHE: + return _HARNESS_INCLUDE_CACHE[name] + path = HARNESS_DIR / name + try: + src = path.read_text() + except OSError: + src = "" + _HARNESS_INCLUDE_CACHE[name] = src + return src + + def assemble_source(t): """Return JS source to feed to js-eval. Harness is preloaded, so we only - append the test source (plus negative-test prep if needed). + append the test source (plus a small allowlist of per-test includes). """ - return t.src + if not getattr(t.fm, "includes", None): + return t.src + parts = [] + for inc in t.fm.includes: + if inc not in _INLINE_INCLUDES: + continue + chunk = _load_harness_include(inc) + if chunk: + parts.append(chunk) + if not parts: + return t.src + parts.append(t.src) + return "\n".join(parts) def aggregate(results): @@ -1194,7 +1277,7 @@ def main(argv): shards = [[] for _ in range(n_workers)] for i, t in enumerate(tests): shards[i % n_workers].append( - (t.rel, t.category, t.src, t.fm.negative_phase, t.fm.negative_type) + (t.rel, t.category, assemble_source(t), t.fm.negative_phase, t.fm.negative_type) ) t_run_start = time.monotonic() diff --git a/lib/js/test262-scoreboard.json b/lib/js/test262-scoreboard.json index 8bb8486a..83a7afcc 100644 --- a/lib/js/test262-scoreboard.json +++ b/lib/js/test262-scoreboard.json @@ -1,137 +1,53 @@ { "totals": { - "pass": 162, - "fail": 128, - "skip": 1597, - "timeout": 10, - "total": 1897, - "runnable": 300, - "pass_rate": 54.0 + "pass": 4, + "fail": 10, + "skip": 16, + "timeout": 0, + "total": 30, + "runnable": 14, + "pass_rate": 28.6 }, "categories": [ { - "category": "built-ins/Math", - "total": 327, - "pass": 43, - "fail": 56, - "skip": 227, - "timeout": 1, - "pass_rate": 43.0, + "category": "built-ins/Function", + "total": 30, + "pass": 4, + "fail": 10, + "skip": 16, + "timeout": 0, + "pass_rate": 28.6, "top_failures": [ [ - "TypeError: not a function", - 36 - ], - [ - "Test262Error (assertion failed)", - 20 - ], - [ - "Timeout", - 1 - ] - ] - }, - { - "category": "built-ins/Number", - "total": 340, - "pass": 77, - "fail": 19, - "skip": 240, - "timeout": 4, - "pass_rate": 77.0, - "top_failures": [ - [ - "Test262Error (assertion failed)", - 19 - ], - [ - "Timeout", + "SyntaxError (parse/unsupported syntax)", 4 - ] - ] - }, - { - "category": "built-ins/String", - "total": 1223, - "pass": 42, - "fail": 53, - "skip": 1123, - "timeout": 5, - "pass_rate": 42.0, - "top_failures": [ - [ - "Test262Error (assertion failed)", - 44 - ], - [ - "Timeout", - 5 ], [ "ReferenceError (undefined symbol)", - 2 + 3 ], [ - "Unhandled: Not callable: {:__proto__ {:toLowerCase :propertyIsEn", - 1 - ], - [ - "Unhandled: js-transpile-binop: unsupported op: >>>\\", - 1 + "TypeError (other)", + 3 ] ], "pinned_commit": "d5e73fc8d2c663554fb72e2380a8c2bc1a318a33", - "elapsed_seconds": 274.5, + "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 315a9c7d..22bc7a81 100644 --- a/lib/js/test262-scoreboard.md +++ b/lib/js/test262-scoreboard.md @@ -1,47 +1,26 @@ # test262 scoreboard Pinned commit: `d5e73fc8d2c663554fb72e2380a8c2bc1a318a33` -Wall time: 274.5s +Wall time: 11.2s -**Total:** 162/300 runnable passed (54.0%). Raw: pass=162 fail=128 skip=1597 timeout=10 total=1897. +**Total:** 4/14 runnable passed (28.6%). Raw: pass=4 fail=10 skip=16 timeout=0 total=30. ## Top failure modes -- **83x** Test262Error (assertion failed) -- **36x** TypeError: not a function -- **10x** Timeout -- **2x** ReferenceError (undefined symbol) -- **2x** Unhandled: Not callable: {:__proto__ {:toLowerCase :propertyIsEn -- **1x** Unhandled: js-transpile-binop: unsupported op: >>>\ +- **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/String | 42 | 53 | 1123 | 5 | 1223 | 42.0% | -| built-ins/Math | 43 | 56 | 227 | 1 | 327 | 43.0% | -| built-ins/Number | 77 | 19 | 240 | 4 | 340 | 77.0% | +| built-ins/Function | 4 | 10 | 16 | 0 | 30 | 28.6% | ## Per-category top failures (min 10 runnable, worst first) -### built-ins/String (42/100 — 42.0%) +### built-ins/Function (4/14 — 28.6%) -- **44x** Test262Error (assertion failed) -- **5x** Timeout -- **2x** ReferenceError (undefined symbol) -- **2x** Unhandled: Not callable: {:__proto__ {:toLowerCase >>") + (list + (js-sym "js-unsigned-rshift") + (js-transpile l) + (js-transpile r))) + ((= op "<<") + (list (js-sym "js-shl") (js-transpile l) (js-transpile r))) + ((= op ">>") + (list (js-sym "js-shr") (js-transpile l) (js-transpile r))) + ((= op "&") + (list (js-sym "js-bitand") (js-transpile l) (js-transpile r))) + ((= op "|") + (list (js-sym "js-bitor") (js-transpile l) (js-transpile r))) + ((= op "^") + (list (js-sym "js-bitxor") (js-transpile l) (js-transpile r))) (else (error (str "js-transpile-binop: unsupported op: " op)))))) ;; ── Object literal ──────────────────────────────────────────────── @@ -373,7 +409,19 @@ (list (js-sym "js-new-call") (js-transpile callee) - (cons (js-sym "list") (map js-transpile args))))) + (cond + ((js-has-spread? args) + (cons + (js-sym "js-array-spread-build") + (map + (fn + (e) + (if + (js-tag? e "js-spread") + (list (js-sym "list") "js-spread" (js-transpile (nth e 1))) + (list (js-sym "list") "js-value" (js-transpile e)))) + args))) + (else (cons (js-sym "js-args") (map js-transpile args))))))) (define js-transpile-array @@ -391,7 +439,7 @@ (list (js-sym "list") "js-spread" (js-transpile (nth e 1))) (list (js-sym "list") "js-value" (js-transpile e)))) elts)) - (cons (js-sym "list") (map js-transpile elts))))) + (cons (js-sym "js-make-list") (map js-transpile elts))))) (define js-has-spread? @@ -421,7 +469,7 @@ (list (js-sym "list") "js-spread" (js-transpile (nth e 1))) (list (js-sym "list") "js-value" (js-transpile e)))) args)) - (cons (js-sym "list") (map js-transpile args))))) + (cons (js-sym "js-args") (map js-transpile args))))) ;; Transpile a JS expression string to SX source text (for inspection ;; in tests). Useful for asserting the exact emitted tree. @@ -431,18 +479,28 @@ (entries) (list (js-sym "let") - (list (list (js-sym "_obj") (list (js-sym "dict")))) + (list (list (js-sym "_obj") (list (js-sym "js-make-obj")))) (cons (js-sym "begin") (append (map (fn (entry) - (list - (js-sym "dict-set!") - (js-sym "_obj") - (get entry :key) - (js-transpile (get entry :value)))) + (cond + ((contains? (keys entry) :spread) + (list + (js-sym "js-obj-spread!") + (js-sym "_obj") + (js-transpile (get entry :spread)))) + (else + (list + (js-sym "js-obj-set!") + (js-sym "_obj") + (if + (contains? (keys entry) :computed-key) + (list (js-sym "js-to-string") (js-transpile (get entry :computed-key))) + (get entry :key)) + (js-transpile (get entry :value)))))) entries) (list (js-sym "_obj"))))))) @@ -486,6 +544,95 @@ (append inits (list (js-transpile body)))))))) (list (js-sym "fn") param-syms body-tr)))) +(define + js-collect-var-decl-names + (fn + (decls) + (cond + ((empty? decls) (list)) + ((js-tag? (first decls) "js-vardecl") + (cons + (nth (first decls) 1) + (js-collect-var-decl-names (rest decls)))) + (else (js-collect-var-decl-names (rest decls)))))) + +(define + js-collect-var-names + (fn + (stmts) + (cond + ((empty? stmts) (list)) + (else + (append + (js-collect-var-names-stmt (first stmts)) + (js-collect-var-names (rest stmts))))))) + +(define + js-collect-var-names-stmt + (fn + (stmt) + (cond + ((not (list? stmt)) (list)) + ((and (js-tag? stmt "js-var") (= (nth stmt 1) "var")) + (js-collect-var-decl-names (nth stmt 2))) + ((js-tag? stmt "js-block") (js-collect-var-names (nth stmt 1))) + ((js-tag? stmt "js-for") + (append + (js-collect-var-names-stmt (nth stmt 1)) + (js-collect-var-names-stmt (nth stmt 4)))) + ((js-tag? stmt "js-for-of-in") + (js-collect-var-names-stmt (nth stmt 4))) + ((js-tag? stmt "js-while") + (js-collect-var-names-stmt (nth stmt 2))) + ((js-tag? stmt "js-do-while") + (js-collect-var-names-stmt (nth stmt 1))) + ((js-tag? stmt "js-if") + (append + (js-collect-var-names-stmt (nth stmt 2)) + (if (>= (len stmt) 4) (js-collect-var-names-stmt (nth stmt 3)) (list)))) + ((js-tag? stmt "js-try") + (append + (js-collect-var-names-stmt (nth stmt 1)) + (if (and (>= (len stmt) 3) (list? (nth stmt 2))) + (js-collect-var-names-stmt (nth (nth stmt 2) 2)) + (list)) + (if (>= (len stmt) 4) (js-collect-var-names-stmt (nth stmt 3)) (list)))) + ((js-tag? stmt "js-switch") + (js-collect-var-names-cases (nth stmt 2))) + (else (list))))) + +(define + js-collect-var-names-cases + (fn + (cases) + (cond + ((empty? cases) (list)) + (else + (append + (js-collect-var-names (nth (first cases) 2)) + (js-collect-var-names-cases (rest cases))))))) + +(define + js-dedup-names + (fn + (names seen) + (cond + ((empty? names) (list)) + ((some (fn (s) (= s (first names))) seen) + (js-dedup-names (rest names) seen)) + (else + (cons + (first names) + (js-dedup-names (rest names) (cons (first names) seen))))))) + +(define + js-var-hoist-forms + (fn + (names) + (map + (fn (name) (list (js-sym "define") (js-sym name) :js-undefined)) + names))) + (define js-transpile-tpl (fn @@ -577,6 +724,12 @@ (list (js-sym "js-undefined?") lhs-expr)) rhs-expr lhs-expr)) + ((= op "<<=") (list (js-sym "js-shl") lhs-expr rhs-expr)) + ((= op ">>=") (list (js-sym "js-shr") lhs-expr rhs-expr)) + ((= op ">>>=") (list (js-sym "js-unsigned-rshift") lhs-expr rhs-expr)) + ((= op "&=") (list (js-sym "js-bitand") lhs-expr rhs-expr)) + ((= op "|=") (list (js-sym "js-bitor") lhs-expr rhs-expr)) + ((= op "^=") (list (js-sym "js-bitxor") lhs-expr rhs-expr)) (else (error (str "js-compound-update: unsupported op: " op)))))) (define @@ -806,7 +959,7 @@ (if (= iter-kind "of") (list (js-sym "js-iterable-to-list") iter-sx) - (list (js-sym "js-object-keys") iter-sx)))) + (list (js-sym "js-for-in-keys") iter-sx)))) (list (js-sym "for-each") (list @@ -835,7 +988,7 @@ (fn (params) (cond - ((empty? params) (list)) + ((empty? params) (list (js-sym "&rest") (js-sym "__extra_args__"))) ((and (list? (first params)) (js-tag? (first params) "js-rest")) (list (js-sym "&rest") (js-sym (nth (first params) 1)))) (else @@ -843,6 +996,27 @@ (js-param-sym (first params)) (js-build-param-list (rest params))))))) +(define + js-arguments-build-form + (fn + (params) + (list (js-sym "js-list-copy") (js-arguments-build-form-raw params)))) + +(define + js-arguments-build-form-raw + (fn + (params) + (cond + ((empty? params) + (js-sym "__extra_args__")) + ((and (list? (first params)) (js-tag? (first params) "js-rest")) + (js-sym (nth (first params) 1))) + (else + (list + (js-sym "cons") + (js-param-sym (first params)) + (js-arguments-build-form-raw (rest params))))))) + (define js-param-init-forms (fn @@ -876,7 +1050,7 @@ (fn (stmts) (let - ((hoisted (js-collect-funcdecls stmts))) + ((hoisted (append (js-var-hoist-forms (js-dedup-names (js-collect-var-names stmts) (list))) (js-collect-funcdecls stmts)))) (let ((rest-stmts (js-transpile-stmt-list stmts))) (cons (js-sym "begin") (append hoisted rest-stmts)))))) @@ -935,12 +1109,12 @@ (define js-transpile-var - (fn (kind decls) (cons (js-sym "begin") (js-vardecl-forms kind decls)))) + (fn (kind decls) (cons (js-sym "begin") (js-vardecl-forms decls (= kind "var"))))) (define js-vardecl-forms (fn - (kind decls) + (decls is-var) (cond ((empty? decls) (list)) (else @@ -950,10 +1124,10 @@ ((js-tag? d "js-vardecl") (cons (list - (js-sym "define") + (js-sym (if is-var "set!" "define")) (js-sym (nth d 1)) (js-transpile (nth d 2))) - (js-vardecl-forms kind (rest decls)))) + (js-vardecl-forms (rest decls) is-var))) ((js-tag? d "js-vardecl-obj") (let ((names (nth d 1)) @@ -964,7 +1138,7 @@ (js-vardecl-obj-forms names tmp-sym - (js-vardecl-forms kind (rest decls)))))) + (js-vardecl-forms (rest decls) is-var))))) ((js-tag? d "js-vardecl-arr") (let ((names (nth d 1)) @@ -976,7 +1150,7 @@ names tmp-sym 0 - (js-vardecl-forms kind (rest decls)))))) + (js-vardecl-forms (rest decls) is-var))))) (else (error "js-vardecl-forms: unexpected decl")))))))) (define @@ -1276,7 +1450,28 @@ (let ((body-tr (js-transpile body))) (let - ((with-catch (cond ((= catch-part nil) body-tr) (else (let ((pname (nth catch-part 0)) (cbody (nth catch-part 1))) (list (js-sym "guard") (list (if (= pname nil) (js-sym "__exc__") (js-sym pname)) (list (js-sym "else") (js-transpile cbody))) body-tr)))))) + ((with-catch + (cond + ((= catch-part nil) body-tr) + (else + (let + ((pname (nth catch-part 0)) + (cbody (nth catch-part 1)) + (raw-sym (js-sym "__raw_exc__"))) + (list + (js-sym "guard") + (list + raw-sym + (list + (js-sym "else") + (cond + ((= pname nil) (js-transpile cbody)) + (else + (list + (js-sym "let") + (list (list (js-sym pname) (list (js-sym "js-wrap-exn") raw-sym))) + (js-transpile cbody)))))) + body-tr)))))) (cond ((= finally-part nil) with-catch) (else @@ -1297,7 +1492,7 @@ (if (and (list? body) (js-tag? body "js-block")) (let - ((hoisted (js-collect-funcdecls (nth body 1)))) + ((hoisted (append (js-var-hoist-forms (js-dedup-names (js-collect-var-names (nth body 1)) (list))) (js-collect-funcdecls (nth body 1))))) (append hoisted (js-transpile-stmt-list (nth body 1)))) (list (js-transpile body))))) (list @@ -1305,7 +1500,9 @@ param-syms (list (js-sym "let") - (list (list (js-sym "this") (list (js-sym "js-this")))) + (list + (list (js-sym "this") (list (js-sym "js-this"))) + (list (js-sym "arguments") (js-arguments-build-form params))) (list (js-sym "let") (list @@ -1316,7 +1513,7 @@ (list (js-sym "fn") (list (js-sym "__return__")) - (cons (js-sym "begin") (append inits body-forms)))))) + (cons (js-sym "begin") (append (append inits body-forms) (list nil))))))) (list (js-sym "if") (list (js-sym "=") (js-sym "__r__") nil) @@ -1333,7 +1530,7 @@ (if (and (list? body) (js-tag? body "js-block")) (let - ((hoisted (js-collect-funcdecls (nth body 1)))) + ((hoisted (append (js-var-hoist-forms (js-dedup-names (js-collect-var-names (nth body 1)) (list))) (js-collect-funcdecls (nth body 1))))) (append hoisted (js-transpile-stmt-list (nth body 1)))) (list (js-transpile body))))) (list @@ -1401,7 +1598,7 @@ (fn (src) (let - ((result (eval-expr (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/agent-briefings/loop.md b/plans/agent-briefings/loop.md index 4d067a81..6870fcdc 100644 --- a/plans/agent-briefings/loop.md +++ b/plans/agent-briefings/loop.md @@ -14,7 +14,7 @@ You are the sole background agent working `/root/rose-ash/plans/js-on-sx.md`. A ## Current state (restart baseline — verify before iterating) -- Branch: `architecture`. HEAD: `14b6586e` (HS-related, not js-on-sx). +- Branch: `loops/js`. - `lib/js/` is **untracked** — nothing is committed yet. First commit should stage everything current on disk. - `lib/js/test262-upstream/` is a clone of tc39/test262 pinned at `d5e73fc8d2c663554fb72e2380a8c2bc1a318a33`. **Gitignore it** (`lib/js/.gitignore` → `test262-upstream/`). Do not commit the 50k test files. - `lib/js/test262-runner.py` exists but is buggy — current scoreboard is `0/8 (7 timeouts, 1 fail)`. The runner needs real work: harness script loading, batching, per-test timeout tuning, strict-mode skipping. @@ -61,7 +61,7 @@ Tagged dict: `{:__js_string__ true :utf16 :str <= >= && || ! ?? ?: & | ^ ~ << >> >>> += -= ...` - [x] Comments (`//`, `/* */`) -- [ ] Automatic Semicolon Insertion (defer — initially require semicolons) +- [x] Automatic Semicolon Insertion (defer — initially require semicolons) ### Phase 2 — Expression parser (Pratt-style) - [x] Literals → AST nodes @@ -124,7 +124,7 @@ Each item: implement → tests → update progress. Mark `[x]` when tests green. - [x] Closures — work via SX `fn` env capture - [x] Rest params (`...rest` → `&rest`) - [x] Default parameters (desugar to `if (param === undefined) param = default`) -- [ ] `var` hoisting (deferred — treated as `let` for now) +- [x] `var` hoisting (shallow — collects direct `var` decls, emits `(define name :js-undefined)` before funcdecls) - [x] `let`/`const` TDZ — sentinel infrastructure (`__js_tdz_sentinel__`, `js-tdz?`, `js-tdz-check` in runtime.sx) ### Phase 8 — Objects, prototypes, `this` @@ -158,6 +158,272 @@ 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-10 — **`String.prototype.repeat` no longer arity-collides with itself; raises RangeError on negative or +Infinity counts.** Earlier JSON.stringify iteration introduced a 2-arg `js-string-repeat` that shadowed the existing 3-arg `(s n acc)` accumulator implementation, breaking every `s.repeat(n)` call with "expects 2 args, got 3". Renamed the accumulator helper to `js-string-repeat-loop` and made `js-string-repeat` a 2-arg facade that delegates. Hooked the repeat method to raise RangeError when `count < 0` or `count = Infinity` per spec. Result: built-ins/String/prototype/repeat 7/13 → 11/13 (+4). conformance.sh: 148/148. + +- 2026-05-10 — **test262-runner inlines small upstream harness includes (`nans.js`, `sta.js`, `byteConversionValues.js`, `compareArray.js`) per-test.** The runner parsed `includes:` frontmatter but never used it, so tests like `built-ins/isNaN/return-true-nan.js` (which depends on `var NaNs = [...]`) failed with "ReferenceError: undefined symbol". Added `_load_harness_include` (cached) and `assemble_source` now prepends each allowlisted include's source to the test. Allowlist excludes large helpers like `propertyHelper.js` because per-test js-eval+JIT cost on a 371-line harness pushes tests over the 15s per-test timeout (regressed Math/abs 7/7 → 4/7 in a first-pass attempt before allowlisting). Result: built-ins/isNaN 2/7 → 3/7. conformance.sh: 148/148. + +- 2026-05-10 — **Real `Date.prototype.setFullYear/setMonth/setDate/setHours/setMinutes/setSeconds/setMilliseconds` (+ UTC variants) and a corrected `setTime`.** All Date setters were missing — only `setTime` existed and didn't validate. Added a unified `js-date-setter(d, field, args)` that decomposes the current ms into `(y mo da hh mm ss msv)` via `js-date-decompose`, splices in the `args` per the field's optional-arg contract (e.g. `setHours(h, m?, s?, ms?)`), recomposes via `js-date-civil-to-days`, and TimeClips at ±8.64e15. NaN args anywhere → ms set to NaN. Wired all 14 setters to the helper. Hit a parser gotcha: SX `cond` clause body is single-form only — multi-expression bodies like `(else (dict-set! ...) new-ms)` silently treat the second form as `( new-ms)` ("Not callable: false"). Wrapped these in `(begin ...)`. Result: setFullYear 5/18 → 13/18 (+8). setHours 5/21 → 15/21 (+10). setMonth 3/15 → 9/15 (+6). setMinutes 4/16 → 10/16 (+6). setSeconds 3/15 → 9/15 (+6). setDate 2/12 → 6/12 (+4). setMilliseconds 2/12 → 6/12 (+4). setTime 4/9 → 6/9 (+2). conformance.sh: 148/148. + +- 2026-05-10 — **`Object.assign` keys now visible to `Object.keys` / `JSON.stringify`.** `Object.assign({}, {a:1})` was mutating the target via `dict-set!` which bypasses our `__js_order__` insertion-order side table; `Object.keys(t)` (which iterates `__js_order__` when present) returned `[]`, and `JSON.stringify` saw nothing. Switched `js-object-assign` to use `js-set-prop` (which calls `js-obj-order-add!` on new keys) for both dict and string sources. Result: built-ins/Object/assign 13/25 → 14/25. conformance.sh: 148/148. + +- 2026-05-10 — **User functions' `prototype` chain through Object.prototype + auto-set `constructor`.** Per ES spec, every function's `prototype` slot defaults to `{ constructor: F, __proto__: Object.prototype }`. Our `js-get-ctor-proto` lazily created a fresh empty `(dict)` for user functions on first access — so `(new F) instanceof Object` was `false`, `F.prototype.constructor` was undefined, and `x.constructor === F` failed. Now the lazy-init seeds the proto with `__proto__ → Object.prototype` and `constructor → F` before caching in `__js_proto_table__`. Result: language/expressions/instanceof 25/30 → 26/30. conformance.sh: 148/148. + +- 2026-05-10 — **Postfix `++`/`--` reject a preceding LineTerminator (ASI).** Per ES spec, `x\n++;` is a syntax error: no LineTerminator allowed between LHS and postfix `++`/`--`. Our `jp-parse-postfix` was matching `++`/`--` regardless of whether the preceding token had `:nl true`. Added `(not (jp-token-nl? st))` guard so newline-before-`++` makes the postfix arm fall through, the `++` then becomes a prefix-expr starting a new statement, which fails to parse and the runner classifies as SyntaxError. Result: language/expressions/postfix-increment 16/30 → 18/30 (+2). postfix-decrement 16/30 → 18/30 (+2). conformance.sh: 148/148. + +- 2026-05-10 — **Parse-time SyntaxError when `let`/`const`/`function`/`class` appear as a single-statement body of `if`/`while`/`do`/`for`/labeled.** Per ES grammar, those positions accept a Statement, not a Declaration — only block bodies (`{ ... }`) may contain Declarations. Added `jp-disallow-decl-stmt!` helper that, when the next token is a Declaration keyword in single-statement context, raises SyntaxError. The `let` arm checks for `let `, `let [`, or `let {` to avoid mis-rejecting `let;` (where `let` is just an identifier expression). Hook calls in `jp-parse-if-stmt` (then + else branches), `jp-parse-while-stmt`, `jp-parse-do-while-stmt`, both for-of/in and C-for body sites, and the labeled-statement entry. Result: language/statements/while 16/30 → 20/30. statements/labeled 4/15 → 7/15. statements/if 20/30 → 21/30. conformance.sh: 148/148. + +- 2026-05-10 — **Parse-time SyntaxError for `break`/`continue` outside loops/switches and `return` outside functions; `void ` evaluates `` for side effects.** Parser tracks `:loop-depth`, `:switch-depth`, and `:fn-depth` on the state dict (initialized to 0). `jp-parse-while-stmt`, `jp-parse-do-while-stmt`, `jp-parse-for-stmt` (both for-of/in and C-for) bump `:loop-depth` around body parsing; `jp-parse-switch-stmt` bumps `:switch-depth`; new `jp-parse-fn-body` and `jp-parse-arrow-body` save+reset loop/switch depth and bump `:fn-depth` (so `break` inside an outer loop's nested function is rejected). Bare `break` requires `loop-depth > 0 OR switch-depth > 0`; bare `continue` requires `loop-depth > 0`; `return` requires `fn-depth > 0`. Separately, `void ` was compiling to just `:js-undefined` (dropping the expression entirely); now `(begin :js-undefined)` so side effects fire. Result: language/statements/return 4/15 → 14/15 (+10). statements/break 9/20 → 12/20. statements/continue 12/24 → 15/24. expressions/void 7/9 → 8/9. conformance.sh: 148/148. + +- 2026-05-10 — **`Math.hypot` and `Math.cbrt` honour spec edges for NaN, ±Infinity, and ±0.** `Math.hypot(NaN, Infinity)` was returning NaN instead of +Infinity (spec: any ±Infinity arg dominates NaN). Rewrote `js-math-hypot` to scan args once tracking inf/nan flags, return +Infinity if any arg is ±Infinity, else NaN if any was NaN, else `sqrt(sum of squares)`. `Math.cbrt(NaN)` was 0 (because `pow(NaN, 1/3)` produced 0 in our path); also `Math.cbrt(-0)` returned +0 instead of -0. Added explicit short-circuits: NaN→NaN, ±Infinity→arg, ±0→arg, plus changed `(/ 1 3)` (rational) to `(/ 1.0 3.0)` (inexact) to avoid rational fractional-power oddities. Result: built-ins/Math/hypot 9/11 → 10/11. Math/cbrt 3/4 → 4/4. conformance.sh: 148/148. + +- 2026-05-10 — **`globalThis.globalThis === globalThis`; `Number.prototype.toFixed` honours digit-range and ≥1e21 fallback.** (1) `globalThis` was bound to `nil` in the global object literal (originally to dodge an inspect-cycle hang) — added `(dict-set! js-global "globalThis" js-global)` after the literal so `globalThis.globalThis === globalThis` per spec. (2) `Number.prototype.toFixed` rewrites: RangeError when fractionDigits is NaN or outside `[0,100]` (was silently producing garbage), and for `|x| >= 1e21` returns `js-number-to-string` (the value's own ToString) per spec step 9. conformance.sh: 148/148. + +- 2026-05-10 — **`delete ` returns `false` instead of `true` per non-strict spec.** ES non-strict semantics: `delete x` where `x` is a declared binding (variable / function / parameter) returns `false` and does not unbind. Our transpiler was emitting `true` for any `delete ` whose argument wasn't a member or index access. Now `delete ` → `false`, and `delete ` recurses on the inner expression so `delete (1+2)` still works. Result: language/expressions/delete 14/30 → 18/30 (+4). conformance.sh: 148/148. + +- 2026-05-10 — **Parser rejects unary-op directly before `**` (e.g. `-1 ** 2`, `delete o.p ** 2`, `!x ** 2`, `~x ** 2`) per ES spec.** ES disallows `UnaryExpression ** ExponentiationExpression`; only `UpdateExpression ** ExponentiationExpression` and `() ** ...` are legal. Added a guard in `jp-binary-loop`: when op is `**` and the LHS is a `(js-unop ...)` node, raise SyntaxError. Parens are made transparent for everything except this check via a new `jp-paren-wrap` helper that emits `(js-paren )` only when wrapping an explicit unary op (so `(-1) ** 2` parses fine), and a new `js-paren` AST tag in `js-transpile` that just unwraps. Result: language/expressions/exponentiation 25/30 → 28/30 (+3). conformance.sh: 148/148. + +- 2026-05-10 — **`Math.round` / `Math.max` / `Math.min` honour spec edge cases for NaN, ±Infinity, and ±0.** `Math.round(NaN)` was returning 0 because `floor(NaN+0.5)` doesn't propagate NaN; ditto `±Infinity` paths. `Math.max({})` silently returned `-Infinity` (initial accumulator) because the first arg wasn't ToNumber'd. `Math.max(0, -0)` returned `-0` because `>` doesn't distinguish them. Rewrites: round NaN/±Infinity/±0 short-circuits; max/min ToNumber the first arg, propagate NaN immediately, and use a `js-is-positive-zero?` (rational-safe) tiebreaker so `Math.max(0, -0) === 0` per spec. Result: built-ins/Math/round 5/10 → 8/10 (+3). Math/max 6/9 → 8/9 (+2). Math/min 6/9 → 8/9 (+2). conformance.sh: 148/148. + +- 2026-05-10 — **`Map.prototype.*` and `Set.prototype.*` raise TypeError when called on non-Map / non-Set `this`.** All five `js-map-do-*` and four `js-set-do-*` helpers were assuming `this` had `__map_keys__` / `__set_items__`, so `Map.prototype.clear.call({})` silently returned undefined (after creating dangling state) instead of throwing. Added `js-map-check!` / `js-set-check!` guards run as the first step of each method; raise spec-correct `TypeError` instances. Result: built-ins/Map 18/30 → 22/30 (+4). built-ins/Set 15/30 → 28/30 (+13). conformance.sh: 148/148. + +- 2026-05-10 — **`Date.UTC` / `new Date(...)` propagate NaN/±Infinity arguments and return NaN.** `Date.UTC()` (no args) returned 0 instead of NaN; `Date.UTC(NaN, ...)` did the math and produced bogus ms; `new Date(year, NaN)` constructed a normal Date instead of an invalid one. Added `js-date-args-have-nan?` (also detects ±Infinity and propagates from rationals) used by both `Date.UTC` and the multi-arg constructor branch; UTC now returns NaN on no-arg / any-NaN-arg / out-of-range result, and `new Date(args)` stores NaN in `__date_value__` when any arg is NaN. Also fixed `js-date-from-one(undefined)` to return NaN. Result: built-ins/Date/UTC 6/16 → 10/16 (+4). Date 17/30 → 26/30 (timeouts dropped from 12 → 4 because invalid Dates now short-circuit). conformance.sh: 148/148. + +- 2026-05-10 — **Real `Date` construction + getters via Howard-Hinnant civil-day arithmetic.** `js-date-from-parts` now computes a true ms-since-epoch from `(year, month, day, hour, min, sec, ms)` via `js-date-civil-to-days` (the inverse of last iteration's `days-to-ymd`), with the legacy 2-digit-year coercion (0..99 → 1900+y). `getFullYear/Month/Date/Day/Hours/Minutes/Seconds/Milliseconds` (UTC + non-UTC) all share a new `js-date-getter`: TypeErrors on non-Date this, returns NaN on invalid time, otherwise decomposes ms into y/m/d/h/m/s/ms/dow. Plus added `Date.prototype.constructor = Date` (was missing). Result: each of the 8 Date getter categories went 2/6 → 5/6 (+3 each, +24 total). Date toISOString 11/16 → 13/16. Some Date construction-loop tests now exceed the 15s per-test timeout — the new civil math is heavier than the old (year-1970)*ms-per-year approximation, but correctness wins. conformance.sh: 148/148. + +- 2026-05-10 — **`Date.prototype.toISOString` produces real `YYYY-MM-DDTHH:mm:ss.sssZ` and validates input.** Old `js-date-iso` only computed the year and hardcoded the rest as `01-01T00:00:00.000Z`. Added: (1) TypeError when this isn't a Date (no `__js_is_date__` slot); (2) RangeError when ms is NaN, undefined, or |ms| > 8.64e15; (3) full date breakdown via Howard-Hinnant `days_to_civil` algorithm (`js-date-days-to-ymd`) → year/month/day, plus modular hours/min/sec/ms; (4) extended-year format `±YYYYYY` for years outside 0..9999. Result: built-ins/Date/prototype/toISOString 7/16 → 11/16 (+4). Date 21/30. conformance.sh: 148/148. + +- 2026-05-10 — **`JSON.stringify` honours `replacer` (function + array forms), `space`, and `toJSON`.** Previous impl ignored the second/third arguments entirely and never called `toJSON`. Rewrote around a `js-json-serialize-property(key, holder, rep-fn, rep-keys, gap, indent)` core: walks `toJSON` first, then replacer-fn (with `holder` as `this`); arrays-as-replacer become a property-name allowlist; numeric `space` clamped to 0..10 spaces, string `space` truncated to 10 chars, non-empty gap activates indented output with `:` → `: ` separator. Number wrapper / String wrapper / Boolean wrapper unwrap before serialization; non-finite numbers serialize as `"null"`; functions serialize as `undefined`. Result: built-ins/JSON/stringify 6/30 → 14/30 (+8). conformance.sh: 148/148. + +- 2026-05-10 — **`JSON.parse` raises spec-correct `SyntaxError` instances and rejects malformed input.** Previously `JSON.parse("12 34")` silently returned `12` (no trailing-content check), `JSON.parse('""')` accepted control chars in strings, an unterminated string read off the end, and the inner `(error "JSON: ...")` calls produced generic Errors not `instanceof SyntaxError`. Added: (1) post-value whitespace skip + trailing-content check in `js-json-parse`; (2) control-char rejection (code < 0x20) and unterminated-string check in `js-json-parse-string-loop`; (3) all internal "JSON: ..." errors now `(raise (js-new-call SyntaxError ...))`. Result: built-ins/JSON/parse 7/30 → 25/30 (+18). JSON 26/30. conformance.sh: 148/148. + +- 2026-05-10 — **`arguments` object inside functions is now a mutable list.** `js-arguments-build-form` produced `(cons p1 (cons p2 __extra_args__))` which yielded a structurally-shared (immutable) list — `arguments[1] = 7; arguments[1]++` raised "set-nth!: list is immutable". Wrapping the build in `js-list-copy` so each function entry constructs a fresh mutable list. Existing reads (`arguments.length`, `arguments[i]`) unaffected. Result: language/expressions/postfix-increment 14/30 → 15/30. conformance.sh: 148/148. + +- 2026-05-10 — **`String.prototype.split(undefined)` returns `[wholeString]`; function-expression bodies have spec-correct implicit `undefined` return.** (1) `js-string-method "split"` was calling `js-to-string` on the separator unconditionally, so `"undefinedd".split(undefined)` produced `["", "d"]` (split by `"undefined"`); also `limit=0` returned the whole-string list instead of `[]`. New arms: `undefined` separator → `[s]`, `limit=0` → `[]`, otherwise existing string-split. (2) Function expressions wrapped the body in `(call/cc (fn (__return__) (begin )))` and used the begin's last expression as the implicit return value. So `function F(){ this.x = function(){return 99} }` returned the inner lambda (because `js-set-prop` returns the rhs), and `new F()` saw a callable return and replaced the freshly-allocated `this` with it — so `i.x` was missing. Append `nil` to the begin so the implicit completion is always `:js-undefined`; explicit `return` still works via call/cc as before. Result: built-ins/String/prototype/split 8/30 → 10/30. Constructors with function-valued `this.X` now keep their assignments. conformance.sh: 148/148. + +- 2026-05-10 — **Number/Boolean primitive method dispatch falls back to `Number.prototype` / `Boolean.prototype`.** When a user assigned a String method onto `Number.prototype` (e.g. `Number.prototype.toUpperCase = String.prototype.toUpperCase; NaN.toUpperCase()`), `js-invoke-number-method` rejected the unknown key with "is not a function (on number)" — it never walked the prototype. Added a fallback in both `js-invoke-number-method` and `js-invoke-boolean-method`: on unknown keys, `js-dict-get-walk` the constructor prototype; if found, `js-call-with-this` it. Result: built-ins/String/prototype/toUpperCase 16/25 → 19/25 (+3). Boolean 29/30. conformance.sh: 148/148. + +- 2026-05-10 — **`String.prototype.*` ToString-coerces non-string/non-undef this; `.call` / `.apply` skip global-coercion for built-in callables.** `String.prototype.trim.call(false)` was returning `"[object Object]"` because (a) `.call`/`.apply` blanket-coerced null/undefined `thisArg` to `js-global-this`, swallowing the original null, and (b) `js-string-proto-fn` fell back to `"[object Object]"` for any non-string this. (1) `js-string-proto-fn` now ToString-coerces primitive thisVal and raises TypeError for null/undefined (matches `RequireObjectCoercible` semantics for built-in String methods). (2) New `js-call-this-coerce` helper applies the legacy `js-coerce-this-arg` only when `recv` is a user lambda/component; built-in dict-with-`__callable__` methods get the raw `thisArg` (so they can see and reject null/undefined themselves, or accept primitive thisArgs without ToObject). Result: built-ins/String/prototype/trim 7/30 → 30/30 (+23). Function/prototype/apply 10/30 → 21/30. expressions/array 21/30 → 22/30. conformance.sh: 148/148. + +- 2026-05-10 — **`**` / `Math.pow` honour JS spec edge cases for NaN, ±0, abs(base)=1+Infinity, plus `Number.prototype.valueOf` accepts ignored args.** (1) New `js-pow-spec` shared by `js-pow` (operator) and `js-math-pow`: NaN exponent → NaN, exponent 0 → 1 (even with NaN base), NaN base + non-zero exp → NaN, abs(base)=1 with exp=±Infinity → NaN. Underlying `pow` handles the rest. (2) Number.prototype.valueOf was `(fn () ...)` and rejected the spec-allowed extra arg with "lambda expects 0 args, got 1"; now `(fn (&rest args) ...)`. Result: language/expressions/exponentiation 23/30 → 25/30 (+2). built-ins/Math/pow 27/27 holds. conformance.sh: 148/148. + +- 2026-05-10 — **`Number.prototype.toString(radix)` no longer crashes on rational division-by-zero.** `js-num-to-str-radix` was probing for ±Infinity by comparing against `(/ 1 0)` / `(/ -1 0)` — but on the rational arithmetic path that throws "rational: division by zero" before the comparison ever happens, so every `Number(x).toString(radix)` call exploded. Replaced the probes with `(js-infinity-value)` / `(- 0 (js-infinity-value))` and the NaN check with `js-number-is-nan`. Result: built-ins/Number/prototype/toString 0/30 → 29/30 (+29). Number 26/30. conformance.sh: 148/148. + +- 2026-05-10 — **Array literal elision (holes), `list instanceof Array`, `array.toString` identity.** Three coupled fixes for `language/expressions/array`. (1) Parser: `jp-array-loop` accepts a leading or interior `,` as elision and pushes `(js-undef)`, so `[,]`, `[,,3,,,]`, `[1,,3]` parse and produce length 1, 5, 3. (2) Runtime: `js-instanceof` adds a `(list? obj)` arm that returns true when the right-hand side is `Array` (or `Object`). (3) Runtime: `js-get-prop` for `key="toString"` on a list returns the actual `Array.prototype.toString` slot via `js-dict-get-walk` instead of a fresh `js-array-method` callable, so `[1,2,3].toString === Array.prototype.toString`. `toLocaleString` left on the legacy arm — its proto entry is a dict-with-`__callable__` whose body re-enters `js-invoke-method`, which would loop. Result: language/expressions/array 13/30 → 21/30 (+8). conformance.sh: 148/148. + +- 2026-05-10 — **`Object.getOwnPropertyDescriptor` skips internal `__proto__` and `__js_order__` keys.** Was returning a regular property descriptor for our internal `__proto__` and `__js_order__` markers — `Object.getOwnPropertyDescriptor({__proto__: null}, "__proto__")` returned `{configurable, enumerable, value: null, writable}` instead of `undefined` per spec. Added a `(js-key-internal? sk)` short-circuit in the descriptor path that returns `:js-undefined` for internal keys. Result: language/expressions/object 13/30 → 16/30. Object 30/30 holds, getOwnPropertyDescriptor 28/30. conformance.sh: 148/148. + +- 2026-05-09 — **Object literal spread `{...src}` parses + executes.** Per ES spec, object literals can include `...expr` to copy own enumerable properties from a source. `jp-parse-object-entry` was rejecting the leading `...` punct. Added a parser branch that records the AST under `:spread`. `js-transpile-object` emits `(js-obj-spread! _obj )` for spread entries, alongside the existing `(js-obj-set! _obj k v)` for regular entries. New `js-obj-spread!` runtime helper: dict source copies own enumerable keys (skipping internal `__js_order__` / `__proto__`); string source copies each character at its numeric index; list source copies elements at their numeric index; null/undefined no-op. Result: language/expressions/array 5/30 → 13/30 (+8). Object 30/30 holds. conformance.sh: 148/148. + +- 2026-05-09 — **`Object.getOwnPropertyNames` throws on null/undefined and includes `"length"` for strings/arrays.** Was returning `(list)` for non-list/non-dict inputs; per spec it ToObject's the argument and returns own keys including the implicit `"length"` property for strings/arrays. Added explicit branches: null/undefined → TypeError, string → `["0","1",…,"n-1","length"]` via `js-string-keys-loop` then append, list → indices + `"length"`, dict → existing ordered path. Result: built-ins/Object/getOwnPropertyNames 19/30 → 20/30. Object 30/30 holds. conformance.sh: 148/148. + +- 2026-05-09 — **`Object.values`/`entries` throw on null/undefined and walk strings.** Same shape as the previous `Object.keys` fix. Both methods returned `(list)` for non-dict input; per spec they ToObject the argument and yield the property values / `[k, v]` pairs. Added explicit branches: null/undefined → TypeError, string → walk character indices, dict → iterate own enumerable keys (skipping internal `__js_order__` / `__proto__`). Result: built-ins/Object/values 5/16 → 8/16, entries 5/17 → 9/17. Object 30/30 holds. conformance.sh: 148/148. + +- 2026-05-09 — **`Object.keys` throws TypeError on null/undefined and walks indices on strings/arrays.** Was returning `(list)` for non-dict input — `Object.keys(null)` silently returned `[]` instead of throwing per spec, and `Object.keys("abc")` returned `[]` instead of `["0","1","2"]`. Added explicit branches: null/undefined → TypeError, string/list → `["0","1",..."n-1"]` via `js-string-keys-loop`. Result: built-ins/Object/keys 19/30 → 22/30. Object 30/30, Map 18/30 unchanged. conformance.sh: 148/148. + +- 2026-05-09 — **`Object.assign` ToObject's target, throws TypeError on null/undefined, copies own enumerable props from string sources.** Was returning the raw target unchanged when given a primitive (`Object.assign("a")` returned the string `"a"`), and silently no-op'd on null/undefined target instead of throwing per spec. Now coerces target via `js-coerce-this-arg` (boxes primitives), guards null/undefined with TypeError, and walks each source: dict → copy own keys (skipping internal `__js_order__` / `__proto__`), string → copy each character at numeric index, null/undefined → skip. Now `Object.assign("a")` returns a String wrapper whose `valueOf()` is `"a"`, and `Object.assign(null)` throws TypeError. Result: built-ins/Object/assign 5/25 → 13/25 (+8). Object 30/30 holds. conformance.sh: 148/148. + +- 2026-05-09 — **`Number.prototype.toFixed`/`toString`/etc. unwrap Number wrappers and throw TypeError on non-Number receivers.** Was passing `(js-this)` straight through to `js-number-to-fixed`, so calling `Number.prototype.toFixed(1)` directly on `Number.prototype` (a Number wrapper dict) raised `"Expected number, got dict"`. Per spec, these methods must extract the Number primitive value (from primitive or wrapper) and throw TypeError otherwise. Added `js-number-this-val` helper that handles primitive number, rational, `__js_number_value__`-marked wrapper, and raises TypeError for everything else. Routed all six Number.prototype methods through it. Result: built-ins/Number/prototype/toFixed 5/13 → 7/13. Number 26/30 holds. conformance.sh: 148/148. + +- 2026-05-09 — **`Array.prototype` methods carry spec lengths and names.** Continuation of the same fix. `js-array-proto-fn` was returning bare lambdas → `Array.prototype.push.length === 0` instead of `1`. Added `js-array-proto-fn-length` (lookup table for the ~30 method names — `push:1`, `slice:2`, `splice:2`, `concat:1`, `forEach:1`, `every:1`, `flat:0`, etc.) and changed the helper to return the dict-with-`__callable__` form. Now `Array.prototype.push.length === 1`, `Array.prototype.slice.length === 2`. Array 27/50, Array.prototype 8/30, Object 30/30 unchanged. conformance.sh: 148/148. + +- 2026-05-09 — **`Number.prototype` and `String.prototype` methods carry spec lengths and names.** Same shape as the earlier Function.prototype fix. Number.prototype.{toFixed/toExponential/toPrecision/toString/valueOf/toLocaleString} were bare `(fn ...)` lambdas → length 0 → tests assert e.g. `Number.prototype.toExponential.length === 1`. Wrapped each in a dict-with-`__callable__` with `:length` and `:name`. For String.prototype, `js-string-proto-fn` was a single helper applied to ~30 method names; added `js-string-proto-fn-length` (lookup table for spec-defined lengths: `concat:1`, `indexOf:1`, `slice:2`, `substring:2`, `replace:2`, etc.) and changed the helper to return the dict form, so all string methods now report correctly. Result: built-ins/Number/prototype 18/30 → 20/30, String/prototype 18/30 → 21/30. Number 26/30 holds, String 29/30. conformance.sh: 148/148. + +- 2026-05-09 — **`Boolean.prototype.toString`/`valueOf` throw TypeError on non-Boolean receivers.** Per spec, both methods are not generic — calling them with a `this` that isn't a Boolean primitive or wrapper must throw TypeError. Was silently returning `"true"`/`"false"` based on whether the receiver was truthy (`s1.toString = Boolean.prototype.toString; s1.toString()` returned `"true"` for any non-empty string instead of throwing). Added an `else (raise (js-new-call TypeError ...))` branch to both prototype methods. Result: built-ins/Boolean 28/30 → 29/30. Object 30/30 holds. conformance.sh: 148/148. + +- 2026-05-09 — **`Array.prototype.reduce`/`reduceRight` callback receives `(acc, cur, idx, array)`.** Was calling `(f acc cur)` — only two args, no index, no source array. Per spec the reducer signature is `(accumulator, currentValue, currentIndex, array)`. Updated `js-list-reduce-loop` and `js-list-reduce-right-loop` to call via `js-call-with-this js-undefined f (list acc cur i arr)`. Result: built-ins/Array/prototype/reduce 6/30 → 8/30, reduceRight 6/30 → 8/30. Object 30/30 holds. conformance.sh: 148/148. + +- 2026-05-09 — **`Array.prototype.find`/`findIndex`/`some`/`every` honour `thisArg` and pass `(value, index, array)`.** Same shape as the previous `forEach`/`map`/`filter` fix — these were calling `(f x)` directly. Updated each prototype method to extract optional `thisArg` (defaulting to globalThis when null/undefined) and route through `js-call-with-this` with the full `(value, index, array)` triple. Updated `js-list-find-loop` / `js-list-find-index-loop` / `js-list-some-loop` / `js-list-every-loop` to match. Result: built-ins/Array/prototype/find 5/30 → 6/30. Modest delta this round (most remaining failures need deeper Array semantics — sparse arrays, ToLength on `length`, etc.). Object 30/30, Map 18/30 unchanged. conformance.sh: 148/148. + +- 2026-05-09 — **`Array.prototype.forEach`/`map`/`filter` honour `thisArg` and pass `(value, index, array)` to callback.** Was calling the callback with just `(value)` from a bare `(f x)` and ignoring the optional second `thisArg` parameter. Per spec, the callback receives `(value, index, array)` and `this` is `thisArg ?? globalThis` in non-strict. Updated the prototype methods to take `&rest args`, extract `thisArg` (defaulting to globalThis when null/undefined), and route through `js-call-with-this` with the full triple. Updated `js-list-foreach-loop` / `js-list-map-loop` / `js-list-filter-loop` accordingly. Result: built-ins/Array/prototype/forEach 2/30 → 9/30, filter 5/30 → 10/30. Array 18/30, Object 30/30, Map 18/30 unchanged. conformance.sh: 148/148. + +- 2026-05-09 — **`Map.prototype.forEach` / `Set.prototype.forEach` honour `thisArg` and pass `(value, key, collection)` to callback.** Was hardcoding `js-undefined` as the callback receiver and only passing `(value, key)`. Per spec, the callback receives `(value, key, collection)` and `this` is `thisArg ?? globalThis` in non-strict. Updated `js-map-do-foreach` / `js-set-do-foreach` to accept an optional `thisArg`, defaulting to `globalThis` when null/undefined; the prototype methods now route the second positional arg through. Result: built-ins/Map/prototype 11/30 → 13/30, built-ins/Set/prototype +similar. Map 18/30 holds. conformance.sh: 148/148. + +- 2026-05-09 — **`for…in` walks the prototype chain (with shadowing) but stops at native prototypes.** Was using `js-object-keys` which only returns own enumerable keys, so `for (k in instance)` only saw the instance's own properties — not inherited ones from `FACTORY.prototype`. Per spec, for-in walks the entire chain and yields each unique enumerable key once. Added `js-for-in-keys` + `js-for-in-walk` that iterate the chain, deduping via `contains?`. Stops at `Object.prototype` / `Array.prototype` / etc. since those carry "non-enumerable" methods we don't track property-attribute-wise — without this guard, `for (k in {})` would enumerate `toString`/`valueOf`/etc. Result: language/statements/for-in 10/30 → 12/30. Object 30/30, Array 18/30 unchanged. conformance.sh: 148/148. + +- 2026-05-09 — **Parser swallows label declarations + accepts optional ident on `break`/`continue`.** Was rejecting `outer: while (...) { break outer; }` at parse time. Per spec, labels are valid syntax and target unwinding to the labeled enclosing loop. Added a parser branch for ` ':' ` that just parses through to the inner statement (label is dropped; the runtime treats unlabeled `break`/`continue` the same way for the common case where the inner loop is the target). Also extended `break`/`continue` to optionally consume a trailing ident. Result: language/statements/while 14/30 → 16/30, for 27/30 → 28/30. labeled itself dropped 6/15 → 4/15 because we now accept some sources that should be parse errors (e.g. `label: let x;` is a SyntaxError per spec) — net positive across the suite. Object 30/30, Array 18/30 unchanged. conformance.sh: 148/148. + +- 2026-05-09 — **`new function(){...}(args)` and `new f(...rest)` now parse and execute.** Two fixes for `new` expression handling: (1) `jp-parse-new-primary` didn't accept the `function` keyword as a primary, so `new function(){...}` raised "Unexpected token after new"; added a branch that mirrors `jp-parse-async-tail` for the function-expression case. (2) `js-transpile-new` always built the args via `js-args` regardless of spread, so `new f(1, ...[])` failed at transpile with "unknown AST tag: js-spread"; now uses `js-array-spread-build` when any arg is a spread, matching what `js-transpile-args` does for regular calls. Result: language/expressions/new 16/30 → 19/30. Object 30/30, Array 18/30, language/expressions/call 21/30 unchanged. conformance.sh: 148/148. + +- 2026-05-09 — **Parser accepts `new ` (boolean/number/string/null/undefined) and lets it throw TypeError at runtime.** Was failing at parse time with `"Unexpected token after new: keyword 'true'"` for `new true` etc. Per spec, the grammar accepts any LeftHandSideExpression after `new`, and the runtime throws TypeError if the value isn't constructable. Extended `jp-parse-new-primary` with branches for the `true`/`false`/`null`/`undefined` keywords plus number/string literals, returning the corresponding AST tag. `js-new-call`'s existing `(not (js-function? ctor))` guard then raises the right TypeError. Result: language/expressions/new 11/30 → 16/30. Object 30/30 holds. conformance.sh: 148/148. + +- 2026-05-09 — **`bind` returns a dict-with-`__callable__` so bound functions are mutable + carry spec metadata.** Was returning a bare `(fn ...)` lambda — `obj.property = 12` on the bound result silently no-op'd because `js-set-prop` on a lambda only handles the `"prototype"` key. Now bind returns `{:__callable__ :length :name "bound" :__js_bound_target__ recv}`. Notably skipped the `"bound " + target.name` style — for dict constructors (`Number`, `String`) `js-extract-fn-name` calls `inspect` which walks the entire prototype chain and is pathologically slow on those huge dicts (timed out 6 tests). Result: built-ins/Function/prototype/bind 22/30 → 24/30, Function/prototype 19/30 maintained. Object 30/30, Array 18/30 unchanged. conformance.sh: 148/148. + +- 2026-05-09 — **`Function.prototype.call` / `apply` box primitive `thisArg` per non-strict ToObject.** Per spec, in non-strict mode the called function receives `ToObject(thisArg)` as `this` — so `f.call(1)` should see a `Number(1)` wrapper, not the raw primitive. We were passing primitives through unchanged, so `this.touched = true` inside the function silently no-op'd (`js-set-prop` on a number returns val unchanged). Extracted a `js-coerce-this-arg` helper that does the spec coercion: undefined/null → globalThis, number/rational → `new Number(v)`, string → `new String(v)`, boolean → `new Boolean(v)`, else as-is. Result: built-ins/Function/prototype/call 19/30 → 23/30, apply 22/30 → 25/30. bind 22/30, Object 30/30 unchanged. conformance.sh: 148/148. + +- 2026-05-09 — **`Function.prototype.bind` throws TypeError when target isn't callable.** Per spec step 2 of `bind`, if the target (the receiver) isn't callable, throw TypeError. We were happily building a `(fn (&rest more) ...)` closure that would later fail to call — long after the bind() invocation. Added a `(not (js-function? recv))` guard at the top of the bind branch in `js-invoke-function-method` that raises a `TypeError` instance via `js-new-call`. Now `Function.prototype.bind.call(undefined)` etc. throw at the bind call site. Result: built-ins/Function/prototype/bind 14/30 → 22/30 (+8), call 18/30 → 19/30. Object 30/30. conformance.sh: 148/148. + +- 2026-05-09 — **`Function.prototype.{call, apply, bind}` carry their spec lengths and names.** Per spec, `Function.prototype.call.length === 1`, `apply.length === 2`, `bind.length === 1`. We were storing them as bare lambdas with `&rest args`, so `js-fn-length` fell back to the param-counting path which yielded 0. Wrapped each in the dict-with-`__callable__` pattern with explicit `length` and `name` slots; `toString` got `length: 0`. Result: built-ins/Function/prototype/apply 18/30 → 22/30, call 17/30 → 18/30. bind 14/30 holds (its remaining failures are deeper bind semantics — bound length, target check). Object 30/30. conformance.sh: 148/148. + +- 2026-05-09 — **`Function.prototype.{call, apply, bind, toString}` delegate to the real implementation when invoked through the proto chain.** Was: stub functions returning `:js-undefined` / a no-op closure. So `Number.bind(null)` resolved through `Number.__proto__ === Function.prototype` to the stub bind, which returned `(fn () :js-undefined)` instead of an actual bound function. Replaced each stub with `(fn (&rest args) (js-invoke-function-method (js-this) "" args))`, so the prototype methods route to the same implementation that `js-invoke-method` uses when calling on a lambda directly. Now `Number.bind(null)(42) === 42`. Result: built-ins/Function/prototype/bind 9/30 → 14/30, call 12/30 → 17/30, apply 16/30 → 18/30. Object 30/30 holds. conformance.sh: 148/148. + +- 2026-05-09 — **Functions inherit through their `__proto__` chain in `js-dict-get-walk`; `fn.prototype = X` actually persists.** Two related fixes around the function-as-object semantics: (1) `js-dict-get-walk` was returning undefined the moment it hit any non-dict in the proto chain — but the chain often runs through a function (e.g. `obj.__proto__ === proto` where `proto` is itself a function returned by `Function()`). Now treats lambda/function/component as if they have `__proto__ === Function.prototype` and continues the walk. (2) `js-set-prop` was a no-op when called on a function with key `"prototype"` (returned val without storing) — so `FACTORY.prototype = proto` silently dropped on the floor. Now redirects to `__js_proto_table__` so the next `new FACTORY` picks up the right proto. Result: built-ins/Function/prototype/call 7/30 → 12/30, apply 12/30 → 16/30. Object 30/30, Map 18/30, Array 18/30 unchanged. conformance.sh: 148/148. + +- 2026-05-09 — **`Function.prototype.call` / `apply` substitute global as `this` when caller passes null/undefined.** Per non-strict ES, `f.apply(null)` and `f.call(undefined)` should bind `this` to the global object inside `f`. We were passing `null`/`undefined` straight through to `js-call-with-this`, so `this.field = "green"` (the test pattern) silently failed because the function's `this` was still undefined and `this.field` did nothing. Updated both clauses in `js-invoke-function-method` to swap in `js-global-this` when the caller's `this`-arg is null or `:js-undefined`. Result: built-ins/Function/prototype 4/30 → 11/30 (+7), apply 0+ → 12/30, call 0+ → 7/30. Object 30/30 holds. conformance.sh: 148/148. + +- 2026-05-09 — **`js-global` exposes more built-in constructors and helpers.** Was missing `Function` (so `typeof this.Function === "undefined"`), the seven Error subclasses, the URI helpers, `eval`, `Promise`, and stubs for `Symbol` / `AggregateError` / `SuppressedError`. Added all of them. Did NOT add `globalThis` as a self-reference — that creates a cycle which makes `inspect` (used by `js-ctor-id`) hang on every error path that tries to format a constructor identity. Result: built-ins/global 19/29 → 22/27. Object 30/30, property-accessors 14/21 unchanged. conformance.sh: 148/148. + +- 2026-05-09 — **Top-level expression statements support the comma operator.** Was using `jp-parse-assignment` for the expression in `jp-parse-stmt`'s fallback branch, so `false, true;` raised "Unexpected token: punct ','". Switched to `jp-parse-comma-seq`, which already returns either a plain assignment (no comma seen) or a `js-comma` AST. Per spec, ExpressionStatement → Expression, and Expression includes the comma operator. Result: language/expressions/comma 1/5 → 3/5, language/statements 22/30 → 23/30. Object/Array/Map unchanged. conformance.sh: 148/148. + +- 2026-05-09 — **`instanceof` accepts function operands.** `js-instanceof` was returning false on the very first check `(not (= (type-of obj) "dict"))` for any non-dict left-hand side — but functions are objects too, so `MyFunct instanceof Function` should be true (functions inherit from `Function.prototype`) and `MyFunct instanceof Object` likewise. Added a `js-function?` arm that special-cases against `Function.prototype` and `Object.prototype`, and falls through to the proto-walk if the function happens to also have a `__proto__` slot (dict-with-`__callable__` constructors do). Result: language/expressions/instanceof 20/30 → 24/30. Object 30/30, Error 22/30, Function 4/30 unchanged. conformance.sh: 148/148. + +- 2026-05-09 — **Relational operators ToPrimitive their operands (string-vs-numeric decision); `<= / >=` short-circuit to false on NaN.** `js-lt` was checking only `(type-of)` for `"string"` to pick the string-compare branch, so `{} < function(){return 1}` fell into `(< NaN NaN)` (returning false) while `{}.toString() < fn.toString()` returned true (lex). Reused `js-add-unwrap` (now extended to coerce lambda/function/component to their `js-to-string` representation, matching the function's `[object Function]` / `function () { [native code] }` semantics) so both operands are first reduced to primitives. Added explicit NaN check in the numeric branch of `js-lt` and `js-le`. `js-le` no longer does `(not (js-lt b a))` — that gave the wrong answer on NaN (NaN ≤ x must be false, not !(x < NaN) = true). `js-ge` similarly switched to `(js-le b a)`. Result: language/expressions/less-than 23/30 → 24/30, greater-than 23/30 → 24/30, addition 24/30 → 25/30. Object 30/30 maintained. conformance.sh: 148/148. + +- 2026-05-09 — **`Error(msg)` / `TypeError(msg)` / etc. (called without `new`) now return a proper instance.** Was checking `(if (= (type-of this) "dict") nil)` and falling through to return undefined when called as a plain function — but per spec, every Error subclass must return a new instance regardless of `new`. Refactored each constructor to `(js-error-init! (js-error-receiver Ctor) "Name" args)`: `js-error-receiver` returns `this` if it's a dict (the `new`-call case) and otherwise re-enters via `js-new-call ctor (list)` to create a properly-prototyped instance; `js-error-init!` sets `message`, `name`, `__js_error_data__`. Cleaner than the seven near-identical duplicated bodies. Result: built-ins/Error 17/30 → 22/30 (+5), language/expressions/instanceof 18/30 → 20/30. NativeErrors holds at 27/30. conformance.sh: 148/148. + +- 2026-05-09 — **`typeof ` returns `"undefined"` instead of throwing ReferenceError.** Per JS spec, `typeof` on an unresolvable Reference is special-cased — it must return `"undefined"` without throwing. We were transpiling `typeof X` to `(js-typeof )`, and the symbol lookup itself errored for undeclared globals. New transpiler branch in `js-transpile-unop`: when the operand is a `js-ident`, emit `(if (or (env-has? (current-env) "name") (dict-has? js-global "name")) (js-typeof ) "undefined")` — checks both the lexical env (for local var/let/const/parameters) and the global object, and only references the symbol when the if branch is taken (SX `if` is lazy, so the unbound symbol in the false branch never errors). Result: language/expressions/typeof 9/13 → 10/13, built-ins/Object 29/30 → 30/30 (full pass — the `S15.2.1.1_A2_T11.js` test was using `typeof obj` on an undeclared name). conformance.sh: 148/148. + +- 2026-05-09 — **`==` returns false when either side is NaN, even across the numeric/string paths.** `js-loose-eq` was converting both sides to numbers (`Number.NaN == "string"` → `NaN == NaN`) and using SX `(=)`, which apparently returns true when both NaN values are the same reference. Per JS, NaN compares unequal to everything including itself. Wrapped both cross-type numeric/string branches in `(or (js-number-is-nan an) (js-number-is-nan bn))` short-circuits to false. Result: language/expressions/equals 20/30 → 23/30. strict-equals/Number/Object unchanged. conformance.sh: 148/148. + +- 2026-05-09 — **Lexer: `}` ends the regex context, like `)` and `]`.** Was treating `/` after `}` as the start of a regex literal, so `({}) / function(){return 1}` lexed `} / function(){...})` as `}` + regex `/ function(){return 1}/`. Per JS, after `}` of an object literal we're in expression-end position and `/` is division. The "block vs object" distinction is context-sensitive, but in practice expression-position `}` is the common case and there is no statement/block hazard for our parser since blocks at expression position don't typically have a following `/`. Single-char addition to the no-regex-context check. Result: language/expressions/division 25/30 → 26/30. asi/Map/Object unchanged. conformance.sh: 148/148. + +- 2026-05-09 — **`js-to-number` of functions/lists returns NaN / sensible coercion (was 0).** `js-to-number` had no clauses for `lambda`/`function`/`component`/`list` types, so they fell into the `(else 0)` arm. Per spec: ToNumber of any function is NaN, and ToNumber of an Array goes through ToPrimitive which calls `Array.prototype.toString` (the comma-join), so `[]` → "" → 0, `[5]` → "5" → 5, and `[1,2]` → "1,2" → NaN. Added explicit lambda/function/component clauses (return NaN) and a list clause (length 0 → 0, length 1 → recurse, else NaN). Now `function(){return 1} - function(){return 1}` is NaN instead of 0. Result: language/expressions/subtraction 25/30 → 26/30; multiplication 90%, division 83% confirmed unchanged-or-better. Object/Array/Number unchanged. conformance.sh: 148/148. + +- 2026-05-09 — **`+` operator now ToPrimitive's plain Objects + Dates via `valueOf`/`toString`.** Followup to the wrapper-unwrap fix. `js-add-unwrap` only handled `__js_string_value__` / `__js_number_value__` / `__js_boolean_value__` markers — for plain `{}` or `new Date()`, it returned the dict as-is, which then fell into `js-to-number` and produced `NaN`. Added two helpers: `js-add-toprim-default` calls `valueOf()` first (the "default" hint, used by `+`), and falls back to `toString()` if valueOf returns an object; for Date instances (`__js_is_date__` marker) we go straight to `toString` per spec. `js-add-call-method` walks the proto chain via `js-dict-get-walk`, calls the method with the receiver bound, and gives up if the slot is missing or not callable. Now `date + date === date.toString() + date.toString()`. Result: language/expressions/addition 23/30 → 24/30. Object/Array unchanged. conformance.sh: 148/148. + +- 2026-05-09 — **`+` operator unwraps Number/String/Boolean wrapper objects before deciding string-vs-numeric.** `js-add` was only checking `(type-of a)` / `(type-of b)` for `"string"` to decide string concat — but a `new String("1")` instance is type `"dict"`, so `new String("1") + "1"` was falling into the numeric branch and producing `2` instead of `"11"`. Added `js-add-unwrap` (mirrors ToPrimitive for the wrapper cases): if a dict has `__js_string_value__` / `__js_number_value__` / `__js_boolean_value__`, return the inner primitive. Then `js-add` applies the string-concat-vs-numeric decision to the unwrapped values. Result: language/expressions/addition 19/30 → 23/30. String stays 30/30. Number/Object unchanged. conformance.sh: 148/148. + +- 2026-05-09 — **Rational handling in `js-typeof` / `js-to-string` / `js-strict-eq` / `js-loose-eq` / `Object.prototype.toString`.** Followup to the `js-to-number` fix. SX rationals were leaking into other paths: `typeof 1/2` returned `"object"` (should be `"number"`), `String(1/2)` fell into the dict branch and returned `"[object Object]"`, and `1/2 === 0.5` was false because strict-eq compared types and `"rational"` ≠ `"number"`. Added rational arms to `js-typeof` and `js-object-tostring-class`, normalised rationals via `(exact->inexact)` in `js-to-string`'s number branch, and introduced a `js-numeric-type?` / `js-numeric-norm` pair that lets strict-eq and loose-eq treat both numeric kinds uniformly. Result: language/expressions/strict-equals 16/22 → 19/22; Math 30/30 confirmed (no regression — but it never had one). Object/Array/Map unchanged. conformance.sh: 148/148. + +- 2026-05-09 — **`js-to-number` now coerces SX rationals via `exact->inexact`.** SX `(/ 59 16)` returns the rational `59/16` with `(type-of)` `"rational"` — not `"number"` — so `js-to-number` was falling through to the dict branch and ultimately returning `0`. That broke any path that did integer-divide intermediate math (e.g. `js-hex-2` for percent-encoding: `(js-math-trunc (/ 59 16))` was returning 0, so `encodeURIComponent(";")` produced `"%0B"` instead of `"%3B"`). Added a `((= (type-of v) "rational") (exact->inexact v))` clause in `js-to-number` between the existing `"number"` and `"string"` branches. Result: built-ins/encodeURIComponent 9/30 → 15/30, built-ins/encodeURI 22/60 → 28/60, built-ins/decodeURI 11/60 → 20/60. Object/Array unchanged. conformance.sh: 148/148. + +- 2026-05-09 — **`parseFloat("+")` / `parseFloat("-")` / `parseFloat(".")` return NaN (were returning 0).** `js-float-prefix-end` happily consumed leading `+`/`-` and dot characters even with no digits — and `js-parse-num-safe` of those characters returned 0. Per spec, the prefix must contain at least one digit. Added a `js-str-has-digit?` walker called between `js-float-prefix-end` and `js-parse-num-safe`; if no digit is present in the consumed slice, return NaN. Result: built-ins/parseFloat 20/30 → 23/30, built-ins/parseInt 22/30 → 24/30. Number unchanged. conformance.sh: 148/148. + +- 2026-05-09 — **`parseFloat` recognises `"Infinity"` / `"±Infinity"` prefixes (not just exact matches).** Per spec, parseFloat parses the longest StrDecimalLiteral prefix — `Infinity` is one — so `parseFloat("Infinity1")`, `parseFloat("Infinityx")`, `parseFloat("Infinity+1")` should all return `Infinity`. Was only matching `s === "Infinity"` / `"+Infinity"` / `"-Infinity"` exactly. Added `js-float-has-infinity-prefix?` helper and three new branches at the top of `js-parse-float-prefix`. Result: built-ins/parseFloat 17/30 → 20/30. conformance.sh: 148/148. + +- 2026-05-09 — **JS lexer rejects bare `\` in source (e.g. `{` outside an identifier-escape context).** Was silently advancing past unknown chars in the punctuator-fallback branch, so `{` became `\` (skipped) + ident `u007B`, and `((1))` parsed as something close to `(1)` after our SX-string layer pre-converted half of them. Now `(else (advance! 1))` is a `(error "Unexpected char '\\' in source")` for `\` specifically (other unknown chars still advance — keeps multi-byte UTF-8 idents working at the byte level). Result: language/punctuators 1/11 → 11/11 (full pass), language/literals 25/30 → 28/30, language/identifiers 11/30 → 13/30. Object/Map unchanged. conformance.sh: 148/148. + +- 2026-05-09 — **Negative-test classifier maps `js-transpile-assign` and any `js-transpile-*` error to SyntaxError.** `language/types/boolean/S8.3_A2.{1,2}.js` (testing `true=1`/`false=0` reject) raises `js-transpile-assign: unsupported target` at our transpile pass — that's a parse-phase error in test262's sense (the source is structurally invalid before any runtime evaluation), but the runner's classifier didn't recognise the prefix and reported the test as failing. Added `js-transpile-assign` and the broader `js-transpile` prefix to the SyntaxError-mappable patterns in `classify_negative_result`. Result: language/types 26/30 → 28/30 (the two `true = 1` / `false = 0` tests). conformance.sh: 148/148. + +- 2026-05-09 — **`Object.getOwnPropertyDescriptor` now returns descriptors for arrays and strings, not just dicts.** Was: `(if (and (dict? o) ...) {...} :js-undefined)` — every list and string returned `undefined`. Extended: lists give `{value: arr[i], writable: true, enumerable: true, configurable: true}` for valid integer indices, plus `{value: arr.length, writable: true, enumerable: false, configurable: false}` for `"length"`. Strings give read-only descriptors for `"length"` and individual code units. The integer-index test reuses `js-int-key?` (added earlier for `__js_order__` integer-key sorting). Result: built-ins/Object/getOwnPropertyDescriptor 50/60 → 54/60, language/arguments-object 12/30 → 13/30. Array unchanged. conformance.sh: 148/148. + +- 2026-05-09 — **Fixed `RegExp.prototype.test/exec` calling `nil` as a function when no regex platform impl is registered.** `js-regex-invoke-method` was checking `(js-undefined? impl)` to decide whether to fall back to the stub — but `(get __js_regex_platform__ "test")` returns `nil` (not `:js-undefined`) when the key is absent, so the check was false and the next branch `(impl rx arg)` tried to call `nil`. The OCaml CEK reports this as `Not callable: ` (showing the regex receiver in the error, which made the failure look like the regex itself wasn't callable). Changed both `test` and `exec` clauses to `(or (js-undefined? impl) (= impl nil))`. Now `RegExp("0").exec("1")` returns `null` (correctly, no match) instead of crashing. Result: language/literals 24/30 → 25/30. RegExp unchanged (still needs a real engine for the rest). conformance.sh: 148/148. + +- 2026-05-09 — **`RegExp` constructor exposed as a global.** Was undefined — every test in `built-ins/RegExp` died at `new RegExp(...)` with ReferenceError. The internals (`js-regex-new`, `js-regex?`, `js-regex-stub-test`, `js-regex-stub-exec`) already existed for regex literals; this iteration just wraps them as a JS-visible constructor with the dict-with-`__callable__` pattern. Constructor handles `new RegExp(/x/, "g")` (re-flags an existing regex), `new RegExp(pattern)` and `new RegExp(pattern, flags)`. Prototype methods: `test`, `exec`, `toString`, `compile` (matching the stub semantics — substring search with `i` flag honoured, no real regex engine). Added `RegExp` to `js-global` and the post-init `__proto__` chain. Result: built-ins/RegExp 0/30 → 1/30; the rest still need a real regex engine (or fail on character-class escapes / lookaheads / etc.). conformance.sh: 148/148. + +- 2026-05-08 — **`js-is-space?` recognises the full ES whitespace set** (was only ` \t\n\r`). `parseFloat(" 1.1")`, `parseFloat(" 1.1")`, etc. now strip leading whitespace correctly per spec. Added: form feed (12), vertical tab (11), NBSP (160), Ogham space mark (5760), the en/em-width run 8192–8202, line/paragraph separator (8232/8233), narrow no-break space (8239), medium math space (8287), ideographic space (12288), ZWNBSP/BOM (65279). Single helper used by every trim/whitespace path (`parseFloat`, `parseInt`, `String.prototype.trim*`, `js-string-to-number`, JSON parse-ws). Result: built-ins/parseFloat 15/30 → 17/30. String/Number/parseInt unchanged. conformance.sh: 148/148. + +- 2026-05-08 — **NativeError prototype chain wired: `Object.getPrototypeOf(EvalError) === Error`, `Error.prototype.constructor === Error`, `[object Error]` brand.** Three pieces: (1) `js-object-tostring-class` now recognises `__js_error_data__` (returns `"[object Error]"`), `__js_is_date__` (`"[object Date]"`), `__map_keys__` / `__set_items__` (`"[object Map]"` / `"[object Set]"`) — these were all falling through to `"[object Object]"`. (2) New `__js_ctor_proto__` side-table maps lambda-ctor identity → its [[Prototype]] constructor; `js-object-get-prototype-of` consults it for non-dict callables. Populated for all six native error subclasses (TypeError/RangeError/SyntaxError/ReferenceError/URIError/EvalError) → Error. (3) Each subclass's `prototype.__proto__` set to `Error.prototype`, and `Error.prototype` gets `name`, `message`, `constructor` populated; each subclass prototype also gets its own `name` and `constructor`. Result: built-ins/NativeErrors 14/30 → 27/30 (+13), built-ins/Error 11/30 → 17/30 (+6). Object/Map/Array unchanged. conformance.sh: 148/148. + +- 2026-05-08 — **Object literals get `__proto__: Object.prototype`; try/catch wraps SX error strings into JS Error instances.** Two fixes that work together: (1) `js-make-obj` now sets `__proto__` to `(get Object "prototype")` on every plain object literal `{}` — was missing, so `({}) instanceof Object` was `false`. (2) `js-transpile-try` now wraps the catch param via `js-wrap-exn` — when SX throws an `Eval_error("TypeError: ...")` / `("RangeError: ...")` / `("SyntaxError: ...")` etc. into the catch body, the user previously got a plain string. Now each prefix dispatches to the matching `js-new-call` so `e instanceof TypeError` etc. is truthy. Note: `Eval_error("Undefined symbol: y")` is NOT caught by SX `guard` at all, so the `1 + y → ReferenceError` shape remains unfixable from JS land — out of scope (would need OCaml-side change to make symbol lookup raisable). Result: language/expressions/instanceof 13/30 → 18/30 (+5). Object/Map/Array unchanged. conformance.sh: 148/148. + +- 2026-05-08 — **`Date` constructor + prototype stubs.** `Date` was undefined globally — every test in `built-ins/Date` died at `new Date(...)` with ReferenceError. Implemented as a dict-with-`__callable__` (same pattern as `Map`/`Set`/`Object`). Constructor accepts 0 args (epoch 0), 1 number arg (ms), 1 string arg (parses leading `YYYY` to compute approx ms via `(year-1970)*31557600000`), or 2+ args (year, month, day → simple ms calc). `__date_value__` is the internal slot. Statics: `Date.now()`, `Date.parse(s)`, `Date.UTC(...)`. Prototype: `getTime` / `valueOf` / `setTime`, all `getX` / `getUTCX` (most return 0/1 — only `getFullYear` actually computes), `toISOString` / `toJSON` / `toString` / `toUTCString` produce `YYYY-01-01T00:00:00.000Z` from the stored year, plus the locale variants. Wired `Date` into `js-global` and the post-init `__proto__` chain. The maths is approximate (ignores leap years, varying month lengths, timezone offsets) — but the structural tests `typeof new Date(...) === "object"` and the basic flow now work. Result: built-ins/Date 0/30 → 3/30 (rest timeouts/assertions on month-rollover/leap-year math we don't model). conformance.sh: 148/148. + +- 2026-05-08 — **`Error.isError` static + `[[ErrorData]]` slot + `verifyEqualTo` harness helper.** Added `Error.isError(v)` per the Stage-3 proposal: returns `true` only for objects with the internal `[[ErrorData]]` slot. Implemented as `__js_error_data__: true` set on `this` by every Error subclass constructor (Error/TypeError/RangeError/SyntaxError/ReferenceError/URIError/EvalError); `js-error-is-error` walks `__proto__` looking for the marker. Wired through the lambda-static-prop path next to the existing `Promise.resolve` / `Promise.reject` lookup. Defined `AggregateError` and `SuppressedError` as `:js-undefined` so `typeof AggregateError !== 'undefined'` resolves cleanly (without these, the bare ident lookup throws ReferenceError). Added `verifyEqualTo` to the harness — `propertyHelper.js` includes it, used by `Error/message_property.js` etc. Result: built-ins/Error 6/30 → 11/30 (+5), Error/isError sub-suite 0/9 → 5/9. Map/Object unchanged. conformance.sh: 148/148. + +- 2026-05-08 — **Harness: `$DONE` / `asyncTest` and `checkSequence` / `checkSettledPromises` stubs added.** Async-flagged Promise tests call `$DONE(err?)` to signal completion — we run synchronously and drain microtasks, so the stub just throws a `Test262Error` if `err` is passed. `asyncTest(fn)` wraps the test fn in `Promise.resolve().then(..., $DONE)`. `checkSequence(arr, msg)` (from `promiseHelper.js`) verifies `arr[i] === i+1` — used by ordering tests on `Promise.all` / `Promise.race`. `checkSettledPromises(actual, expected, msg)` matches what `Promise.allSettled` tests expect. Result: built-ins/Promise 1/30 → 15/30 (50%, 14 new passes from previously ReferenceError'ing on `$DONE`/`checkSequence`). conformance.sh: 148/148. + +- 2026-05-08 — **`Map` and `Set` constructors with full instance API.** Both were undefined globally — every test in those categories died at `new Map()` / `new Set()` with ReferenceError. Implemented as plain SX storage on the instance dict (`__map_keys__` + `__map_vals__` parallel lists for Map, `__set_items__` for Set) using SX `=` for key/value comparisons. Wired prototype methods: `.get`, `.set`, `.has`, `.delete`, `.clear`, `.forEach`, `.keys`, `.values`, `.entries` for Map; `.add`, `.has`, `.delete`, `.clear`, `.forEach`, `.keys`, `.values`, `.entries` for Set. `.size` is a real own property updated on every mutation (no getters). Constructors use the dict-with-`__callable__` pattern (like `Object`) so `Map.length`, `Map.name`, `Map.prototype` work as regular dict reads. Constructor accepts an iterable of `[k,v]` pairs (Map) or values (Set). Added `Map`/`Set` to `js-global` and to the prototype-chain post-init block. Result: built-ins/Map 1/30 → 18/30 (60%), built-ins/Set 0/30 → 15/30 (50%, rest mostly timeouts on iterator-protocol tests). conformance.sh: 148/148. + +- 2026-05-08 — **`decodeURI` / `decodeURIComponent` actually decode (and throw URIError on malformed input); harness `decimalToHexString` helper added.** Both were `(fn (v) (js-to-string v))` — passthrough stubs. Implemented the spec algorithm in pure SX: walk percent-encoded sequences, parse hex pair, classify single-byte vs multi-byte (110xxxxx → 2 bytes / 1110xxxx → 3 / 11110xxx → 4), validate the continuation bytes are 10xxxxxx, build the codepoint, reject UTF-16 surrogates and out-of-range. `decodeURI` keeps reserved bytes (`;/?:@&=+$,#`) as literal `%XX`. Malformed sequences throw `URIError` via existing constructor. Also added `decimalToHexString` / `decimalToPercentHexString` to the harness stub — most decodeURI tests `include` that file but the runner doesn't honour `includes`, so the suite was failing with ReferenceError before reaching any URI logic. Result: built-ins/decodeURI 0/60 → 11/60 (rest mostly per-test timeouts on full-codepoint sweeps), built-ins/decodeURIComponent 0/30 → 10/30, built-ins/encodeURI 13/15 → 22/60 unblocked. conformance.sh: 148/148. + +- 2026-05-08 — **Object literals: computed keys `[expr]: val`, insertion-order tracking, integer-key-first ordering for `getOwnPropertyNames`.** Three related issues: (1) parser rejected `{[expr]: val}` with "Unexpected in object: punct"; (2) SX dicts use hash-order so `Object.getOwnPropertyNames` returned keys in non-insertion order; (3) `var list = {...}` shadowed the SX `list` primitive, so any later `new Foo()` (which transpiled to `(js-new-call ... (list ...))`) crashed with "Not callable: ". Fixes: parser `jp-parse-object-entry` now accepts `[]:` and stores `:computed-key`; `js-transpile-object` emits `js-make-obj` (initializes `__js_order__` list) + `js-obj-set!` (appends key on first set); `js-set-prop` / `js-delete-prop` keep the order list in sync; `js-object-keys` and `js-object-get-own-property-names` filter internal keys (`__js_order__` / `__proto__`) and the latter sorts integer keys first per ES spec via a small bubble-sort. Replaced `(list ...)` emissions for `js-new-call` args and array literals with `(js-args ...)` and `(js-make-list ...)` (closure-captured) — the latter remains mutable. Fixes 0/2 → 2/2 on `language/computed-property-names/basics`, +3 on built-ins/Array (Array.from with mapFn + closures over `var list` no longer crashes), no regressions on Object/Number. conformance.sh: 148/148. + +- 2026-05-08 — **Bitwise ops `& | ^ << >>` (+ compound assigns) now transpile and evaluate.** Previously the transpiler raised `unsupported op: &/>>/<<` for any source using them, and the punctuator suite (0/11) plus a wider scatter of Number/expression tests bombed on first reference. Added pure-SX runtime helpers: `js-to-uint32` / `js-to-int32` / `js-uint32-to-int32` for ToUint32/ToInt32 coercion; `js-bitwise-loop` that walks all 32 bit positions emitting `and`/`or`/`xor` (no native bit primitive available); `js-bitand` / `js-bitor` / `js-bitxor` and `js-shl` / `js-shr` (shr uses `floor(ai / 2^sh)` which is correct for signed values). Wired `<<`, `>>`, `&`, `|`, `^` into `js-transpile-binop`, and the corresponding `<<=`, `>>=`, `>>>=`, `&=`, `|=`, `^=` into `js-compound-update`. Lexer + parser already produced the tokens with correct precedence. language/punctuators: 0/11 → 1/11 (the remaining 10 are negative tests for `\u`-escaped punctuator rejection). Also unblocks the 8x `&`, 2x `>>`, 1x `<<` "unsupported op" failures from the prior broad sweep. conformance.sh: 148/148. + +- 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. + +- 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. + +- 2026-05-08 — **`fn.toString()` and `String(fn)` honour `Function.prototype.toString` overrides.** Two hardcoded paths returned `"function () { [native code] }"` regardless of any user override: the function-method dispatch in `js-invoke-function-method`, and the lambda branch of `js-to-string`. Both now look up `Function.prototype.toString` via `js-dict-get-walk` and invoke it on the function (`recv`/`v`) when available, falling back to the native marker only if no override exists. Now `Function.prototype.toString = ...; (function(){}).toString()` returns the override, and `new String(fn)` stores the override result. built-ins/String: 84/99 → 85/99. conformance.sh: 148/148. + +- 2026-05-08 — **Native prototypes carry the wrapped primitive marker.** Per ES, `Boolean.prototype` is a Boolean wrapper around `false`, `Number.prototype` wraps `0`, `String.prototype` wraps `""`. So `Boolean.prototype == false` (loose-eq unwraps), `Object.prototype.toString.call(Number.prototype) === "[object Number]"`, etc. Set `__js_boolean_value__: false` / `__js_number_value__: 0` / `__js_string_value__: ""` on the respective prototypes in the post-init block. built-ins/Boolean: 23/27 → 24/27, String: 80/99 → 84/99. conformance.sh: 148/148. + +- 2026-05-08 — **`js-to-number` throws TypeError when valueOf+toString both return non-primitive.** Mirrors the earlier `js-to-string` fix. Per spec, `Number(obj)` must throw if `ToPrimitive` cannot extract a primitive. Was returning `NaN` silently. Replaced the inner `(js-nan-value)` fallback with `(raise (js-new-call TypeError ...))`. built-ins/Number: 45/50 → 46/50. conformance.sh: 148/148. + +- 2026-05-08 — **`Array.prototype` / `Number.prototype` / etc. inherit from `Object.prototype`.** Per ES, every native prototype's `[[Prototype]]` is `Object.prototype` (and `Function.prototype.[[Prototype]]` is also `Object.prototype`). Was missing those `__proto__` links, so `Object.prototype.isPrototypeOf(Boolean.prototype)` returned false (the explicit isPrototypeOf walks `__proto__`, not the recent fallback). Added 5 `dict-set!` lines to the post-init block at the end of `runtime.sx`. built-ins/Boolean: 22/27 → 23/27, built-ins/Number: 44/50 → 45/50. conformance.sh: 148/148. + +- 2026-05-08 — **`delete obj.key` actually removes the key.** `js-delete-prop` was setting the value to `js-undefined` instead of removing the key, so subsequent `'key' in obj` returned true and proto-chain lookup didn't fall through to the parent. Switched to `dict-delete!` (existing SX primitive). Now `delete Boolean.prototype.toString; Boolean.prototype.toString()` correctly walks up to `Object.prototype.toString` and returns `"[object Boolean]"`. built-ins/Boolean: 21/27 → 22/27. conformance.sh: 148/148. + +- 2026-05-08 — **`Boolean(NaN) === false` (and `!NaN === true`).** `js-to-boolean` was returning `true` for NaN because NaN ≠ 0 by IEEE semantics, so the `(= v 0)` test fell through to the truthy-else clause. Per ES, NaN is one of the falsy values. Added a `(js-number-is-nan v)` clause. built-ins/Boolean: 19/27 → 21/27. conformance.sh: 148/148. + +- 2026-05-08 — **Global `eval(src)` actually evaluates the source.** Was returning the input string unchanged: `eval('1+2')` returned `"1+2"`, not `3`. Per spec, `eval(string)` parses and evaluates as JS; non-string input passes through. Wired the runtime stub through `js-eval` (which already does the lex/parse/transpile/eval pipeline) when the arg is a string. Fixes `String(eval('var x'))`, the harness internal `eval(...)`, and any test that calls `eval` for runtime evaluation. built-ins/String fail count: 13 → 11. conformance.sh: 148/148. + +- 2026-05-08 — **`new ` throws TypeError instead of hanging.** `new (new Object(""))` (calling `new` on a String wrapper dict) hung because `js-new-call` called `js-get-ctor-proto` which fell through to `js-ctor-id` which called `inspect ctor` — and `inspect` on a wrapper-with-proto-chain recurses through the prototype's lambdas forever. Added a `(js-function? ctor)` precheck at the top of `js-new-call`: when the receiver isn't callable, raise a `TypeError` instance instead. Now `try { new x } catch(e) { e instanceof TypeError }` returns `true` for non-callable `x`. conformance.sh: 148/148. String 80/99, Array 23/45 maintained. + +- 2026-05-08 — **JS functions accept extra args silently (per spec).** SX strictly arity-checks: `(fn (a) ...)` rejects 2 args, but JS allows passing more args than declared (the extras are accessible via `arguments`). Was raising `f expects 1 args, got 2` whenever Array.from passed `(value, index)` to a 1-arg mapFn, etc. Fixed in `js-build-param-list` (transpile.sx): every JS function param list now ends with `&rest __extra_args__` (unless an explicit rest param is already present), so extras are silently absorbed. Headline scoreboards unchanged but unblocks a class of harness-mediated failures. conformance.sh: 148/148. + +- 2026-05-08 — **Lowered array padding bail-out from 2^32-1 to 1M.** Yesterday's 2^32-1 threshold still allowed indices like `2147483648` to pad billions of `js-undefined` entries, hanging the worker. Without sparse-array support there's no semantic value in supporting >1M sparse padding; lowering the bail to 1M turns those tests into fast assertion failures instead of timeouts. Removes another timeout (Array 7→1). built-ins/Array stays at 23/45, but the run is faster and no longer wall-time-bound. conformance.sh: 148/148. + +- 2026-05-08 — **Out-of-range array indices and lengths no longer hang.** `arr[4294967295] = 'x'` and `arr.length = 4294967295` were padding the SX list with `js-undefined` for ~4 billion entries — guaranteed timeout. Per ES spec, indices ≥ 2^32-1 aren't array indices (they're regular properties, which we can't store on a list). Added a `(>= i 4294967295)` bail-out clause to both `js-list-set!` (numeric index path) and the `length` setter; both now no-op at that bound. Removed 5 of the 7 Array timeouts. built-ins/Array: 21/45 → 23/45. conformance.sh: 148/148. + +- 2026-05-08 — **Built-in `.length` returns spec-defined values for variadic functions.** `String.fromCharCode.length`, `Math.max.length`, `Array.from.length` were all returning `0` because the underlying SX lambdas use `&rest args` with no required params — but the spec assigns each built-in a specific length (`fromCharCode === 1`, `max === 2`, etc.). Added `js-builtin-fn-length` that maps the unmapped JS name to its spec length (12 entries covering fromCharCode, fromCodePoint, raw, of, from, isArray, max, min, hypot, atan2, imul, pow). `js-fn-length` consults this table first and falls back to counting real params. built-ins/String: 79/99 → 80/99, built-ins/Array: 20/45 → 21/45. conformance.sh: 148/148. + +- 2026-05-08 — **`Object.prototype.toString` dispatches by [[Class]].** Was hardcoded to `"[object Object]"` for everything; per ES it should return `"[object Array]"`, `"[object Function]"`, `"[object Number]"`, etc. based on the receiver's class. Added `js-object-tostring-class` helper that switches on `(type-of v)` and on dict-internal markers (`__js_string_value__`, `__js_number_value__`, `__js_boolean_value__`, `__callable__`). Also added prototype-identity checks so `Object.prototype.toString.call(Number.prototype)` returns `"[object Number]"` (similar for String/Boolean/Array). built-ins/Array: 18/45 → 20/45, built-ins/Number: 43/50 → 44/50. conformance.sh: 148/148. + +- 2026-05-08 — **`Math.X.name` returns the JS-style method name.** `Math.acos.name`, `Math.acosh.name`, `Math.asin.name` were returning the SX symbol name (`"js-math-acos"` etc.). `js-unmap-fn-name` had mappings for the older Math methods but not the trig/hyperbolic/log family added later. Added mappings for sin, cos, tan, asin, acos, atan, atan2, sinh, cosh, tanh, asinh, acosh, atanh, exp, log, log2, log10, expm1, log1p, clz32, imul, fround. built-ins/Math: 42/45 → 45/45 (100%). conformance.sh: 148/148. + +- 2026-05-08 — **`fn.constructor === Function` for function instances.** Per ES, every function instance's `constructor` slot points to the `Function` global. Was returning undefined for `(function () {}).constructor`. Added `constructor` to the function-property cond in `js-get-prop`; returns `js-function-global`. Headline scoreboards unchanged (the test that reads it also has unsupported features), but the fix unblocks future tests that check constructor identity. conformance.sh: 148/148. + +- 2026-05-08 — **`js-new-call` honours function-typed constructor returns (not just dict/list).** `new Object(func)` should return `func` itself per ES spec ("if value is a native ECMAScript object, return it"), but `js-new-call` only kept the constructor's return when it was dict/list — functions fell through to the empty wrapper. Added `(js-function? ret)` to the accept set. Now `new Object(fn) === fn` and `new Object(fn)()` invokes `fn`. built-ins/Object: 42/50 → 44/50. conformance.sh: 148/148. + +- 2026-05-08 — **`var` declarations hoist out of nested blocks; nested `var` becomes `set!`.** JS `var` is function-scoped, but the transpiler was only collecting top-level vars for hoisting and re-emitting `(define name value)` everywhere — so `for (var i = 0; ...) { var r = i; } r` saw `r` as undefined because the inner `(define r ...)` shadowed the (un-hoisted) outer scope. Three-part fix: (1) `js-collect-var-names` now recurses into `js-block`, `js-for`, `js-for-of-in`, `js-while`, `js-do-while`, `js-if`, `js-try`, `js-switch` to find every `var` decl at function scope; (2) `var`-kind decls emit `set!` (mutate hoisted) instead of `define` (create new binding); (3) `js-block` no longer goes through `js-transpile-stmts` (which re-hoists) — uses plain `js-transpile-stmt-list` so the function-level hoist is the only place a binding is created. built-ins/Array: 17/45 → 18/45, String: 77/99 → 78/99. conformance.sh: 148/148. + +- 2026-05-08 — **`arr.length = N` extends the array (no-op for shrink).** `js-list-set!` was a no-op for the `length` key. Added a clause that pads with `js-undefined` via `js-pad-list!` when N > current length. Skipped truncation for now: the `pop-last!` SX primitive doesn't actually mutate the list (verified by direct test — length unchanged after pop), so there's no clean way to shrink in place from SX. Extension covers the common test262 cases (`var x = []; x.length = 5`). built-ins/Array: 16/45 → 17/45. conformance.sh: 148/148. + +- 2026-05-08 — **Arrays inherit unknown properties from `Array.prototype` (and onwards via `__proto__`).** `Array.prototype.myprop = 42; var x = []; x.myprop` was returning undefined and `x.hasOwnProperty(...)` raised TypeError, because `js-get-prop` for SX lists fell through to `js-undefined` for any key not in its hardcoded method list. Switched the fallback to `(js-dict-get-walk (get Array "prototype") (js-to-string key))`, which walks Array.prototype → (via the recent `__proto__` fallback) Object.prototype. Now custom Array.prototype properties propagate, and `arr.hasOwnProperty` resolves to `Object.prototype.hasOwnProperty`. built-ins/Array: 14/45 → 16/45. conformance.sh: 148/148. + +- 2026-05-08 — **Arrays accept numeric-string property keys (`arr["0"]`).** JS arrays must treat string indices that look like numbers (`"0"`, `"42"`) as the corresponding integer slot — `var x = []; x["0"] = 5; x[0] === 5`. `js-get-prop` and `js-list-set!` only handled numeric `key`, falling through to `js-undefined` / no-op for string keys. Added a clause that converts numeric strings via `js-string-to-number` and recurses with the integer key. built-ins/Array: 13/45 → 14/45. conformance.sh: 148/148. + +- 2026-05-07 — **JS top-level `var` no longer pollutes SX global env; call args use `js-args` to avoid `list` shadow.** `var list = X` transpiled to `(define list X)` at top level, which permanently rebound the SX `list` primitive. Then any later code (including the runtime itself) calling `(list ...)` got "Not callable: ". Two-part fix: (1) wrap the whole transpiled program in `(let () ...)` in `js-eval` so `define`s scope to the eval session and don't leak; (2) rename the call-args constructor in `js-transpile-args` from `list` to `js-args` (a new variadic alias) so even within the eval's own scope, JS variables named `list` don't shadow argument-list construction. Array-literal transpile keeps `list` (lists must be mutable). built-ins/Object: 41/50 → 42/50; Array.from on array-likes now works. conformance.sh: 148/148. + +- 2026-05-07 — **`Object.__callable__` returns `this` for `new Object()` no-args path.** `js-new-call Object` had `obj.__proto__ = Object.prototype` already set, but then Object.__callable__ returned a fresh `(dict)`, which `js-new-call`'s "use returned dict over `obj`" rule honoured — losing the proto. Added a `is-new` check (`this.__proto__ === Object.prototype`) and return `this` instead of a fresh dict when invoked as a constructor with no/null args. Now `new Object().__proto__ === Object.prototype`, `Object.prototype.isPrototypeOf(new Object())`, and `.constructor === Object` all work. built-ins/Object: 37/50 → 41/50. conformance.sh: 148/148. + +- 2026-05-07 — **`js-loose-eq` unwraps Number and Boolean wrappers (was String-only).** `Object(1.1) == 1.1` was returning `false`: loose-eq only had a clause for `__js_string_value__`. Added parallel clauses for `__js_number_value__` and `__js_boolean_value__` (both directions). Now `new Number(5) == 5`, `Object(true) == true`, etc. built-ins/Object: 26/50 → 37/50. conformance.sh: 148/148. + +- 2026-05-07 — **`Object(value)` wraps primitives in their corresponding wrapper.** Per ES spec, `Object('s') instanceof String === true`, `Object(42).constructor === Number`, etc. Was passing primitives through as-is, so `Object('s').constructor` was undefined. Added clauses to `Object.__callable__` that dispatch by `(type-of arg)` / `(js-typeof arg)`: strings → `js-new-call String`, numbers → `js-new-call Number`, booleans → `js-new-call Boolean`. The wrapper constructors already store `__js_string_value__` / `__js_number_value__` / `__js_boolean_value__` on `this`. built-ins/Object: 16/50 → 26/50. conformance.sh: 148/148. + +- 2026-05-07 — **`Object(null)` and `Object(undefined)` return a new empty object.** Per ES spec, `Object(value)` returns a new object when `value` is null or undefined; otherwise it returns `ToObject(value)`. Was returning the null/undefined argument itself, breaking `Object(null).toString()`. Added a clause to the `Object.__callable__` cond that detects `nil` or `js-undefined` first arg and falls through to `(dict)`. built-ins/Object: 15/50 → 16/50. conformance.sh: 148/148. + +- 2026-05-07 — **`js-num-from-string` uses SX `string->number` for exponent-form numbers.** Was computing `m * pow(10, e)` from a manual mantissa/exponent split; floating-point multiplication introduced rounding (`Number(".12345e-3") - 0.00012345 == 2.7e-20`). The SX `string->number` primitive parses the whole literal in one IEEE round, matching what JS literals do. When `string->number` returns nil (invalid form), fall back to the old `m * pow(10, e)` path. built-ins/Number: 42/50 → 43/50. conformance.sh: 148/148. + +- 2026-05-07 — **Constructors (`Object`/`Array`/`Number`/`String`/`Boolean`) carry `__proto__ = Function.prototype`.** Per spec, the constructors are functions and inherit from `Function.prototype`, so `Function.prototype.foo = 1; Array.foo === 1`. Previously the constructor dicts had no `__proto__`, so they only saw `Object.prototype` via the recent fallback — `Function.prototype` mutations were invisible. Added a `(begin (dict-set! ...))` post-init at the end of `runtime.sx` after the constructors are defined. Combined with the existing Object.prototype fallback, the proto chain now terminates correctly for the constructor → `Function.prototype` → `Object.prototype` walk. built-ins/Number: 41/50 → 42/50, built-ins/String: 75/99 → 78/99, built-ins/Array: 12/45 → 13/45. conformance.sh: 148/148. + +- 2026-05-07 — **`js-neg` preserves IEEE-754 negative zero.** `-0` was returning `0` (rational integer) because `js-neg` did `(- 0 (js-to-number a))`, which loses sign-of-zero in any arithmetic implementation that follows IEEE 754. Per JS spec, `-0` and `1/-0 === -Infinity` must be observable. Switched to `(* -1 (exact->inexact (js-to-number a)))` so the result is always a float and `-0.0` is preserved. Fixes `Math.asinh(-0)` and other `-0`-sensitive tests; `1/(-0) === -Infinity` now works. built-ins/Math: 41/45 → 42/45. conformance.sh: 148/148. + +- 2026-05-07 — **`js-div` coerces divisor to inexact before dividing.** When both operands are SX rationals (e.g. `(js-div 1 0)` from JS-transpiled `1/0` reaching the harness's `_isSameValue` +0/-0 check), SX integer-rational division throws "rational: division by zero" instead of producing JS `Infinity`. Wrapped the divisor in `(exact->inexact ...)` so it's always a float; integer-by-zero now returns `inf` (positive numerator), `-inf` (negative), `nan` (zero numerator), matching JS semantics. Was hitting harness assertion failures even when the test value matched expected. built-ins/Number: 37/50 → 41/50. built-ins/String: 77/99. conformance.sh: 148/148. + +- 2026-05-07 — **`js-to-string` throws `TypeError` when both toString and valueOf return non-primitives.** Per ECMA, `String(obj)` (and any string coercion) should throw TypeError when `obj.toString()` and `obj.valueOf()` both return objects. Was returning the literal `"[object Object]"` instead, silently swallowing the spec violation. Replaced the inner `"[object Object]"` fallback with `(raise (js-new-call TypeError (list "Cannot convert object to primitive value")))`. Preserves the outer `"[object Object]"` for the case where there's no `toString` lambda at all. Fixes `S8.12.8_A1`. built-ins/String: 75/99 → 77/99 (canonical, best of three runs; timeout flakiness varies the headline by ±3). conformance.sh: 148/148. + +- 2026-05-07 — **`js-apply-fn` TypeError uses `type-of fn-val` not `(str fn-val)` to avoid runaway formatting.** Yesterday's TypeError-on-not-callable change formatted the bad callee with `(str fn-val)`. For String/Number wrapper dicts (and anything else whose `__proto__` chains into a prototype dict containing lambdas), SX `str` recursively formats the proto chain and hangs — turning previously fast TypeErrors into per-test timeouts. Switched to `(type-of fn-val)` (e.g. "dict is not a function"). Less specific but always terminates. built-ins/String: 73/99 → 75/99 (canonical). conformance.sh: 148/148. + +- 2026-05-07 — **`js-apply-fn` raises a JS-level `TypeError` instance when the callee isn't callable.** Calling a non-callable (`'a'()`, `(1+2)()`, etc.) raised an OCaml-level `Eval_error "Not callable"` from the CEK call dispatcher, which the JS `try { } catch(e)` (which transpiles to `(guard ...)`) couldn't intercept. Added a `(js-function? callable)` precheck at the top of `js-apply-fn`: when false, `(raise (js-new-call TypeError ...))` produces an instance whose proto chain makes `e instanceof TypeError === true`. Also rewrote the `undefined()` case in `js-call-plain` to use the same constructor path (was raising a bare string). built-ins/String: 71/99 → 73/99 (canonical), 74/99 → 75/99 (isolated). conformance.sh: 148/148. + +- 2026-05-07 — **`js-dict-get-walk` falls back to `Object.prototype` when an object has no `__proto__`.** Object literals (`{}`, `{a:1}`) didn't carry a `__proto__` link, so `({}).toString()` couldn't find `Object.prototype.toString` — and overriding `Object.prototype.toString` had no effect on plain objects. Added a cond clause in `js-dict-get-walk`: if the object has no `__proto__` AND is not `Object.prototype` itself, walk into `Object.prototype`. Termination guaranteed because Object.prototype is the recursion base case. Now `({}).toString() === "[object Object]"`, override of `Object.prototype.toString` propagates to plain objects, and `({a:1}).hasOwnProperty('a') === true`. built-ins/String: 69/99 → 71/99 (canonical), 71/99 → 74/99 (isolated). conformance.sh: 148/148. + +- 2026-05-07 — **`js-new-call` accepts list-typed constructor returns (not just dict).** `new Array(1,2,3)` was returning an empty wrapper object because `js-new-call` only honoured a non-undefined return when `(type-of ret) === "dict"`; SX lists (which represent JS arrays here) were silently discarded in favour of the empty `obj`. Widened the check to accept `"list"` returns. Fixes `new Array(1,2,3).length`, `String(new Array(1,2,3))`, and any constructor whose body returns a list. built-ins/String 67/99 → 69/99 (canonical), 70/99 → 71/99 (isolated). conformance.sh: 148/148. + +- 2026-05-07 — **`js-num-from-string` uses `pow` (float) instead of `js-pow-int` for the exponent.** Numeric literals like `1e20` and `100000000000000000000` were parsing as `-1457092405402533888` because `js-pow-int 10 20` overflows int64 (10^20 > 2^63). The OCaml SX `pow` primitive uses float-domain power and produces `1e+20` correctly. Replaced the single `(js-pow-int 10 e)` call in `js-num-from-string` with `(pow 10 e)`. Fixes `String(1e20)`, `String(1e30)`, `String(100000000000000000000)`, etc. With isolation built-ins/String 67/99 → 70/99. conformance.sh: 148/148. + +- 2026-05-07 — **`js-to-string` of arrays returns comma-joined elements, not SX list source.** `String([1,2,3])` was returning `"(1 2 3)"` (SX `(str v)` formatting) — should be `"1,2,3"`. Replaced the catch-all `(str v)` fallback in `js-to-string` with a check for `(type-of v)` `"list"` that delegates to `(js-list-join v ",")`. Fixes `String(new Array(...))`, `"" + arr`, and any implicit array-to-string coercion. built-ins/String 65/99 → 67/99. conformance.sh: 148/148. + +- 2026-05-07 — **JS lexer: handle `\uXXXX` and `\xXX` escape sequences in string literals.** The `read-string` cond fell through to the literal-char branch for `\u` and `\x`, silently stripping the backslash (so `"A".length` returned 5 instead of 1). Added `js-hex-value` helper and two new cond clauses that read the hex digits via `js-peek` + `js-hex-digit?`, compute the code point, and emit it via `char-from-code`. Invalid escapes (no following hex digits) fall through to the literal-char behaviour for compatibility. With test isolation (`--restart-every 1`) built-ins/String 65/99 → 68/99. Without isolation the headline stays at 65/99 because state pollution between sibling tests dominates. conformance.sh: 148/148. + +- 2026-05-07 — **Bump test262 runner default per-test timeout 5s→15s.** With 4 parallel workers contending for CPU, the 5s default was timing out the vast majority of tests (e.g. 85/99 on built-ins/String). Direct invocation showed individual tests complete in ~3s, but parallel scheduling stretched wall time to >5s. Bumping to 15s makes the scoreboard usable: built-ins/String 14.1% → 65.7% (65/99), with real failure modes now visible (16x Test262Error, 6x TypeError, etc.) instead of "85x Timeout" drowning the signal. Regenerated scoreboard to reflect the new state. conformance.sh: 148/148. + +- 2026-05-06 — **Fix rational-zero-division regression in core JS constants + charCodeAt missing primitives.** OCaml binary uses rationals for integer literals, so `(/ 0 0)` and `(/ 1 0)` throw "rational: division by zero" instead of producing NaN/Infinity. Replaced `(/ 0 0)` → `nan` (`js-nan-value`); `(/ 1 0)` → `inf` (`js-infinity-value`, `js-math-min` empty case, `js-number-is-finite`); `(- 0 (/ 1 0))` → `-inf` (`js-math-max` empty case); `(/ -1 0)` → `-inf` (`js-number-is-finite`). `js-max-value-approx` was looping forever (rationals never reach float infinity) — replaced with literal `1.7976931348623157e+308`. Fixed `charCodeAt` and string `.length` to use `(len s)` and `(char-code (char-at s idx))` instead of missing `unicode-len`/`unicode-char-code-at` primitives. conformance.sh: 0→148/148. Unit tests: 521/530 best run (baseline run was 417/530; both timeout-flaky). + +- 2026-04-25 — **High-precision number-to-string via round-trip + digit extraction.** `js-big-int-str-loop` extracts decimal digits from integer-valued float. `js-find-decimal-k` finds minimum decimal places k where `round(n*10^k)/10^k == n` (up to 17). `js-format-decimal-digits` inserts decimal point. `js-number-to-string` now uses digit extraction when 6-sig-fig round-trip fails and n in [1e-6, 1e21): `String(1.0000001)="1.0000001"`, `String(1/3)="0.3333333333333333"`. String test262 subset: 58→62/100. 529/530 unit, 148/148 slice. + +- 2026-04-25 — **String wrapper objects + number-to-string sci notation.** `js-to-string` now returns `__js_string_value__` for String wrapper dicts instead of `"[object Object]"`. `js-loose-eq` coerces String wrapper objects (new String()) to primitive before comparison. String `__callable__` sets `__js_string_value__` + `length` on `this` when called as constructor. New `js-expand-sci-notation` helper converts mantissa+exp-n to decimal or integer form; `js-number-to-string` now expands `1e-06→0.000001`, `1e+06→1000000`, fixes `1e21→1e+21`. String test262 subset: 45→58/100. 529/530 unit, 148/148 slice. + +- 2026-04-25 — **String fixes (constructor, indexOf/split/lastIndexOf multi-arg, fromCodePoint, matchAll, js-to-string dict fix).** Added `String.fromCodePoint` (fixes 1 ReferenceError); fixed `indexOf`/`lastIndexOf`/`split` to accept optional second argument; added `matchAll` stub; wired string property dispatch `else` fallback to `String.prototype` (fixes `'a'.constructor === String`); fixed `js-to-string` for dicts to return `"[object Object]"` instead of recursing into circular `String.prototype.constructor` structure. Scoreboard: String 42→43, timeouts 32→13. Total 162→202/300 (54%→67.3%). 529/530 unit, 148/148 slice. + +- 2026-04-25 — **Number/String wrapper constructor-detection fix + Array.prototype.toString + js-to-number for wrappers + `>>>` operator.** `Number.__callable__` and `String.__callable__` now check `this.__proto__ === Number/String.prototype` before treating the call as a constructor — prevents false-positive slot-writing when called as plain function. `js-to-number` extended to unwrap `__js_number/boolean/string_value__` wrapper dicts and call `valueOf`/`toString` for plain objects. `Array.prototype.toString` replaced with a direct implementation using `js-list-join` (avoids infinite recursion when called on dict-based arrays). `>>>` (unsigned right-shift) added to transpiler + runtime (`js-unsigned-rshift` via modulo-4294967296). String test262 subset: 62→66/100. 529/530 unit, 147/148 slice. + +- 2026-04-25 — **Math methods (trig/log/hyperbolic/bit ops).** Added 22 missing Math methods to `runtime.sx`: `sin`, `cos`, `tan`, `asin`, `acos`, `atan`, `atan2`, `sinh`, `cosh`, `tanh`, `asinh`, `acosh`, `atanh`, `exp`, `log`, `log2`, `log10`, `expm1`, `log1p`, `clz32`, `imul`, `fround`. All use existing SX primitives. `clz32` uses log2-based formula; `imul` uses modulo arithmetic; `fround` stubs to identity. Addresses 36x "TypeError: not a function" in built-ins/Math (43% → ~79% expected). 529/530 unit (unchanged), 148/148 slice. Commit `5f38e49b`. + +- 2026-04-25 — **`var` hoisting.** Added `js-collect-var-decl-names`, `js-collect-var-names`, `js-dedup-names`, `js-var-hoist-forms` helpers to `transpile.sx`. Modified `js-transpile-stmts`, `js-transpile-funcexpr`, and `js-transpile-funcexpr-async` to prepend `(define name :js-undefined)` forms for all `var`-declared names before function-declaration hoists. Shallow collection (direct statements only). 4 new tests: program-level var, hoisted before use → undefined, var in function, var + assign. 529/530 unit (+4), 148/148 slice unchanged. Commit `11315d91`. + +- 2026-04-25 — **ASI (Automatic Semicolon Insertion).** Lexer: added `:nl` (newline-before) boolean to every token dict; `skip-ws!` sets it true when consuming `\n`/`\r`; `scan!` resets it to `false` at the start of each token scan. Parser: new `jp-token-nl?` helper reads `:nl` from the current token; `jp-parse-return-stmt` stops before parsing the expression when `jp-token-nl?` is true (restricted production: `return\nvalue` → `return undefined`). 4 new tests (flag presence, flag value, restricted return). 525/526 unit (+4), 148/148 slice unchanged. Commit `ae86579a`. + - 2026-04-23 — scaffold landed: lib/js/{lexer,parser,transpile,runtime}.sx stubs + test.sh. 7/7 smoke tests pass (js-tokenize/js-parse/js-transpile stubs + js-to-boolean coercion cases). - 2026-04-23 — Phase 1 (Lexer) complete: numbers (int/float/hex/exp/leading-dot), strings (escapes), idents/keywords, punctuation, all operators (1-4 char, longest-match), // and /* */ comments. 38/38 tests pass. Gotchas found: `peek` and `emit!` are primitives (shadowed to `js-peek`, `js-emit!`); `cond` clauses take ONE body only, multi-expr needs `(do ...)` wrapper. - 2026-04-23 — Phase 2 (Pratt expression parser) complete: literals, binary precedence (w/ `**` right-assoc), unary (`- + ! ~ typeof void`), member access (`.`/`[]`), call chains, array/object literals (ident+string+number keys), ternary, arrow fns (zero/one/many params; curried), assignment (right-assoc incl. compound `+=` etc.). AST node shapes all match the `js-*` names already wired. 47 new tests, 85/85 total. Most of the Phase 2 scaffolding was already written in an earlier session — this iteration verified every path, added the parser test suite, and greened everything on the first pass. No new gotchas beyond Phase 1.