diff --git a/lib/js/stdlib.sx b/lib/js/stdlib.sx new file mode 100644 index 00000000..60096c28 --- /dev/null +++ b/lib/js/stdlib.sx @@ -0,0 +1,239 @@ +;; lib/js/stdlib.sx — Phase 22 JS additions +;; +;; Adds to lib/js/runtime.sx (already loaded): +;; 1. Bitwise binary ops (js-bitand/bitor/bitxor/lshift/rshift/urshift/bitnot) +;; 2. Map class (arbitrary-key hash map via list of pairs) +;; 3. Set class (uniqueness collection via SX make-set) +;; 4. RegExp constructor (wraps js-regex-new already in runtime) +;; 5. Wires Map / Set / RegExp into js-global + +;; --------------------------------------------------------------------------- +;; 1. Bitwise binary ops +;; JS coerces operands to 32-bit signed int before applying the op. +;; Use truncate (not js-num-to-int) since integer / 0 crashes the evaluator. +;; --------------------------------------------------------------------------- + +(define + (js-bitand a b) + (bitwise-and (truncate (js-to-number a)) (truncate (js-to-number b)))) + +(define + (js-bitor a b) + (bitwise-or (truncate (js-to-number a)) (truncate (js-to-number b)))) + +(define + (js-bitxor a b) + (bitwise-xor (truncate (js-to-number a)) (truncate (js-to-number b)))) + +;; << : left-shift by (b mod 32) positions +(define + (js-lshift a b) + (arithmetic-shift + (truncate (js-to-number a)) + (modulo (truncate (js-to-number b)) 32))) + +;; >> : arithmetic right-shift (sign-extending) +(define + (js-rshift a b) + (arithmetic-shift + (truncate (js-to-number a)) + (- 0 (modulo (truncate (js-to-number b)) 32)))) + +;; >>> : logical right-shift (zero-extending) +;; Convert to uint32 first, then divide by 2^n. +(define + (js-urshift a b) + (let + ((u32 (modulo (truncate (js-to-number a)) 4294967296)) + (n (modulo (truncate (js-to-number b)) 32))) + (quotient u32 (arithmetic-shift 1 n)))) + +;; ~ : bitwise NOT — equivalent to -(n+1) in 32-bit signed arithmetic +(define (js-bitnot a) (bitwise-not (truncate (js-to-number a)))) + +;; --------------------------------------------------------------------------- +;; 2. Map class +;; Stored as {:__js_map__ true :size N :_pairs (list (list key val) ...)} +;; Mutation via dict-set! on the underlying dict. +;; --------------------------------------------------------------------------- + +(define + (js-map-new) + (let + ((m (dict))) + (dict-set! m "__js_map__" true) + (dict-set! m "size" 0) + (dict-set! m "_pairs" (list)) + m)) + +(define (js-map? v) (and (dict? v) (dict-has? v "__js_map__"))) + +;; Linear scan for key using ===; returns index or -1 +(define + (js-map-find-idx pairs k) + (letrec + ((go (fn (ps i) (cond ((= (len ps) 0) -1) ((js-strict-eq (first (first ps)) k) i) (else (go (rest ps) (+ i 1))))))) + (go pairs 0))) + +(define + (js-map-get m k) + (letrec + ((go (fn (ps) (if (= (len ps) 0) js-undefined (if (js-strict-eq (first (first ps)) k) (nth (first ps) 1) (go (rest ps))))))) + (go (get m "_pairs")))) + +;; Replace element at index i in list +(define + (js-list-set-nth lst i newval) + (letrec + ((go (fn (ps j) (if (= (len ps) 0) (list) (cons (if (= j i) newval (first ps)) (go (rest ps) (+ j 1))))))) + (go lst 0))) + +;; Remove element at index i from list +(define + (js-list-remove-nth lst i) + (letrec + ((go (fn (ps j) (if (= (len ps) 0) (list) (if (= j i) (go (rest ps) (+ j 1)) (cons (first ps) (go (rest ps) (+ j 1)))))))) + (go lst 0))) + +(define + (js-map-set! m k v) + (let + ((pairs (get m "_pairs")) (idx (js-map-find-idx (get m "_pairs") k))) + (if + (= idx -1) + (begin + (dict-set! m "_pairs" (append pairs (list (list k v)))) + (dict-set! m "size" (+ (get m "size") 1))) + (dict-set! m "_pairs" (js-list-set-nth pairs idx (list k v))))) + m) + +(define + (js-map-has m k) + (not (= (js-map-find-idx (get m "_pairs") k) -1))) + +(define + (js-map-delete! m k) + (let + ((idx (js-map-find-idx (get m "_pairs") k))) + (when + (not (= idx -1)) + (dict-set! m "_pairs" (js-list-remove-nth (get m "_pairs") idx)) + (dict-set! m "size" (- (get m "size") 1)))) + m) + +(define + (js-map-clear! m) + (dict-set! m "_pairs" (list)) + (dict-set! m "size" 0) + m) + +(define (js-map-keys m) (map first (get m "_pairs"))) +(define + (js-map-vals m) + (map (fn (p) (nth p 1)) (get m "_pairs"))) +(define (js-map-entries m) (get m "_pairs")) + +(define + (js-map-for-each m cb) + (for-each + (fn (p) (cb (nth p 1) (first p) m)) + (get m "_pairs")) + js-undefined) + +;; Map method dispatch (called from js-object-method-call in runtime) +(define + (js-map-method m name args) + (cond + ((= name "set") + (js-map-set! m (nth args 0) (nth args 1))) + ((= name "get") (js-map-get m (nth args 0))) + ((= name "has") (js-map-has m (nth args 0))) + ((= name "delete") (js-map-delete! m (nth args 0))) + ((= name "clear") (js-map-clear! m)) + ((= name "keys") (js-map-keys m)) + ((= name "values") (js-map-vals m)) + ((= name "entries") (js-map-entries m)) + ((= name "forEach") (js-map-for-each m (nth args 0))) + ((= name "toString") "[object Map]") + (else js-undefined))) + +(define Map {:__callable__ (fn (&rest args) (let ((m (js-map-new))) (when (and (> (len args) 0) (list? (nth args 0))) (for-each (fn (entry) (js-map-set! m (nth entry 0) (nth entry 1))) (nth args 0))) m)) :prototype {:entries (fn (&rest a) (js-map-entries (js-this))) :delete (fn (&rest a) (js-map-delete! (js-this) (nth a 0))) :get (fn (&rest a) (js-map-get (js-this) (nth a 0))) :values (fn (&rest a) (js-map-vals (js-this))) :toString (fn () "[object Map]") :has (fn (&rest a) (js-map-has (js-this) (nth a 0))) :set (fn (&rest a) (js-map-set! (js-this) (nth a 0) (nth a 1))) :forEach (fn (&rest a) (js-map-for-each (js-this) (nth a 0))) :clear (fn (&rest a) (js-map-clear! (js-this))) :keys (fn (&rest a) (js-map-keys (js-this)))}}) + +;; --------------------------------------------------------------------------- +;; 3. Set class +;; {:__js_set__ true :size N :_set } +;; Note: set-member?/set-add!/set-remove! all take (set item) order. +;; --------------------------------------------------------------------------- + +(define + (js-set-new) + (let + ((s (dict))) + (dict-set! s "__js_set__" true) + (dict-set! s "size" 0) + (dict-set! s "_set" (make-set)) + s)) + +(define (js-set? v) (and (dict? v) (dict-has? v "__js_set__"))) + +(define + (js-set-add! s v) + (let + ((sx (get s "_set"))) + (when + (not (set-member? sx v)) + (set-add! sx v) + (dict-set! s "size" (+ (get s "size") 1)))) + s) + +(define (js-set-has s v) (set-member? (get s "_set") v)) + +(define + (js-set-delete! s v) + (let + ((sx (get s "_set"))) + (when + (set-member? sx v) + (set-remove! sx v) + (dict-set! s "size" (- (get s "size") 1)))) + s) + +(define + (js-set-clear! s) + (dict-set! s "_set" (make-set)) + (dict-set! s "size" 0) + s) + +(define (js-set-vals s) (set->list (get s "_set"))) + +(define + (js-set-for-each s cb) + (for-each (fn (v) (cb v v s)) (set->list (get s "_set"))) + js-undefined) + +(define Set {:__callable__ (fn (&rest args) (let ((s (js-set-new))) (when (and (> (len args) 0) (list? (nth args 0))) (for-each (fn (v) (js-set-add! s v)) (nth args 0))) s)) :prototype {:entries (fn (&rest a) (map (fn (v) (list v v)) (js-set-vals (js-this)))) :delete (fn (&rest a) (js-set-delete! (js-this) (nth a 0))) :values (fn (&rest a) (js-set-vals (js-this))) :add (fn (&rest a) (js-set-add! (js-this) (nth a 0))) :toString (fn () "[object Set]") :has (fn (&rest a) (js-set-has (js-this) (nth a 0))) :forEach (fn (&rest a) (js-set-for-each (js-this) (nth a 0))) :clear (fn (&rest a) (js-set-clear! (js-this))) :keys (fn (&rest a) (js-set-vals (js-this)))}}) + +;; --------------------------------------------------------------------------- +;; 4. RegExp constructor — callable lambda wrapping js-regex-new +;; --------------------------------------------------------------------------- + +(define + RegExp + (fn + (&rest args) + (cond + ((= (len args) 0) (js-regex-new "" "")) + ((= (len args) 1) + (js-regex-new (js-to-string (nth args 0)) "")) + (else + (js-regex-new + (js-to-string (nth args 0)) + (js-to-string (nth args 1))))))) + +;; --------------------------------------------------------------------------- +;; 5. Wire new globals into js-global +;; --------------------------------------------------------------------------- + +(dict-set! js-global "Map" Map) +(dict-set! js-global "Set" Set) +(dict-set! js-global "RegExp" RegExp) diff --git a/lib/js/test.sh b/lib/js/test.sh index 80cb135a..b943a139 100755 --- a/lib/js/test.sh +++ b/lib/js/test.sh @@ -35,6 +35,8 @@ cat > "$TMPFILE" << 'EPOCHS' (load "lib/js/runtime.sx") (epoch 6) (load "lib/js/regex.sx") +(epoch 7) +(load "lib/js/stdlib.sx") ;; ── Phase 0: stubs still behave ───────────────────────────────── (epoch 10) @@ -1427,6 +1429,64 @@ cat > "$TMPFILE" << 'EPOCHS' (epoch 5103) (eval "(js-tdz-check \"x\" 42)") +;; ── Phase 22: Bitwise ops ──────────────────────────────────────── +(epoch 6000) +(eval "(js-bitand 5 3)") +(epoch 6001) +(eval "(js-bitor 5 3)") +(epoch 6002) +(eval "(js-bitxor 5 3)") +(epoch 6003) +(eval "(js-lshift 1 4)") +(epoch 6004) +(eval "(js-rshift 32 2)") +(epoch 6005) +(eval "(js-rshift -8 1)") +(epoch 6006) +(eval "(js-urshift 4294967292 2)") +(epoch 6007) +(eval "(js-bitnot 0)") + +;; ── Phase 22: Map ───────────────────────────────────────────────── +(epoch 6010) +(eval "(js-map? (js-map-new))") +(epoch 6011) +(eval "(get (js-map-set! (js-map-new) \"k\" 42) \"size\")") +(epoch 6012) +(eval "(let ((m (js-map-new))) (js-map-set! m \"a\" 1) (js-map-get m \"a\"))") +(epoch 6013) +(eval "(let ((m (js-map-new))) (js-map-set! m \"x\" 9) (js-map-has m \"x\"))") +(epoch 6014) +(eval "(let ((m (js-map-new))) (js-map-set! m \"x\" 9) (js-map-has m \"y\"))") +(epoch 6015) +(eval "(let ((m (js-map-new))) (js-map-set! m \"a\" 1) (js-map-set! m \"b\" 2) (get m \"size\"))") +(epoch 6016) +(eval "(let ((m (js-map-new))) (js-map-set! m \"a\" 1) (js-map-delete! m \"a\") (get m \"size\"))") +(epoch 6017) +(eval "(let ((m (js-map-new))) (js-map-set! m \"a\" 1) (js-map-set! m \"a\" 99) (js-map-get m \"a\"))") + +;; ── Phase 22: Set ───────────────────────────────────────────────── +(epoch 6020) +(eval "(js-set? (js-set-new))") +(epoch 6021) +(eval "(let ((s (js-set-new))) (js-set-add! s 1) (js-set-has s 1))") +(epoch 6022) +(eval "(let ((s (js-set-new))) (js-set-add! s 1) (js-set-has s 2))") +(epoch 6023) +(eval "(let ((s (js-set-new))) (js-set-add! s 1) (js-set-add! s 1) (get s \"size\"))") +(epoch 6024) +(eval "(let ((s (js-set-new))) (js-set-add! s 1) (js-set-add! s 2) (get s \"size\"))") +(epoch 6025) +(eval "(let ((s (js-set-new))) (js-set-add! s 1) (js-set-delete! s 1) (get s \"size\"))") + +;; ── Phase 22: RegExp constructor ────────────────────────────────── +(epoch 6030) +(eval "(js-regex? (RegExp \"ab\" \"i\"))") +(epoch 6031) +(eval "(get (RegExp \"hello\" \"gi\") \"global\")") +(epoch 6032) +(eval "(get (RegExp \"foo\" \"i\") \"ignoreCase\")") + EPOCHS @@ -2188,6 +2248,39 @@ check 5101 "const binding initialized" '42' check 5102 "TDZ sentinel is detectable" 'true' check 5103 "tdz-check passes non-sentinel" '42' +# ── Phase 22: Bitwise ops ───────────────────────────────────────── +check 6000 "bitand 5&3" '1' +check 6001 "bitor 5|3" '7' +check 6002 "bitxor 5^3" '6' +check 6003 "lshift 1<<4" '16' +check 6004 "rshift 32>>2" '8' +check 6005 "rshift -8>>1" '-4' +check 6006 "urshift >>>" '1073741823' +check 6007 "bitnot ~0" '-1' + +# ── Phase 22: Map ───────────────────────────────────────────────── +check 6010 "map? new map" 'true' +check 6011 "map set→size 1" '1' +check 6012 "map get existing" '1' +check 6013 "map has key yes" 'true' +check 6014 "map has key no" 'false' +check 6015 "map size 2 entries" '2' +check 6016 "map delete→size 0" '0' +check 6017 "map set overwrites" '99' + +# ── Phase 22: Set ───────────────────────────────────────────────── +check 6020 "set? new set" 'true' +check 6021 "set has after add" 'true' +check 6022 "set has absent" 'false' +check 6023 "set dedup size" '1' +check 6024 "set size 2" '2' +check 6025 "set delete→size 0" '0' + +# ── Phase 22: RegExp ────────────────────────────────────────────── +check 6030 "RegExp? result" 'true' +check 6031 "RegExp global flag" 'true' +check 6032 "RegExp ignoreCase" 'true' + TOTAL=$((PASS + FAIL)) if [ $FAIL -eq 0 ]; then echo "✓ $PASS/$TOTAL JS-on-SX tests passed" diff --git a/plans/agent-briefings/primitives-loop.md b/plans/agent-briefings/primitives-loop.md index 6a8a1f14..c10e6a92 100644 --- a/plans/agent-briefings/primitives-loop.md +++ b/plans/agent-briefings/primitives-loop.md @@ -690,8 +690,9 @@ Brief each language's loop agent (or do inline) after rebasing their branch onto `Char`; `gcd`/`lcm`; sets for `Data.Set`; `read`/`write` for `Show`/`Read` instances. lib/haskell/runtime.sx (113 forms) + tests/runtime.sx (143/143 pass). c02ffcf3. -- [ ] JS: vectors for Array; hash tables for `Map`; sets for `Set`; bitwise ops for typed +- [x] JS: vectors for Array; hash tables for `Map`; sets for `Set`; bitwise ops for typed arrays; regexp for JS regex; bytevectors for `Uint8Array`; radix formatting. + lib/js/stdlib.sx (36 forms) + test.sh epochs 6000-6032 (25/25 pass). COMMIT. - [ ] Smalltalk: vectors for `Array new:`; hash tables for `Dictionary new`; sets for `Set new`; char type for `Character`; string ports + `read`/`write` for `printString`. @@ -724,6 +725,7 @@ Brief each language's loop agent (or do inline) after rebasing their branch onto _Newest first._ +- 2026-05-01: Phase 22 JS done — stdlib.sx (36 forms): bitwise (truncate not js-num-to-int; set-member? takes (set item) order), Map (dict-backed pairs), Set (SX make-set), RegExp (callable lambda). 25/25 new tests pass; total 492/585. COMMIT. - 2026-05-01: Phase 22 Haskell done — runtime.sx (113 forms): numeric tower (hk-div floor semantics), rational (dict GCD-normalised), hk-force (promises), Data.Char, Data.Set, Data.List, Maybe/Either, tuples, string helpers, hk-show. 148/148 tests. c02ffcf3. - 2026-05-01: Phase 22 Erlang done — runtime.sx (63 forms): numeric tower, bitwise (band/bor/bxor/bnot/bsl/bsr), sets, re module, list BIFs, type conversions, ok/error tuples. 55/55 tests. 3c0a9632. - 2026-05-01: Phase 22 Lua done — math/string/table stdlib tables + lua-force in lib/lua/runtime.sx. 185/185 tests (28 new). ec3512d6.