*in* is a Reader, not a map

(map? *in*) was true because *in* was a plain map of read-line-fn/read-fn
closures; the JVM *in* is a java.io.Reader so map? is false. A defrecord
doesn't help (records are maps). Make the reader a reify over a new IReader
protocol — a non-map value — and route read/read-line/read+string/line-seq
through its -read-line/-read-form/-read+string methods instead of keyword
access. with-in-str's __string-reader and the stdin *in* both reify it.
Closes *in*-bound + *in*-is-bound.
This commit is contained in:
Yogthos 2026-06-21 23:45:24 -04:00
parent 8ce00d29fd
commit d1c2811d13
4 changed files with 551 additions and 543 deletions

View file

@ -11,7 +11,7 @@
;; reset between cases so there is no leakage — same isolation a fresh process gives.
;;
;; chez --script host/chez/run-corpus.ss
;; JOLT_CHEZ_ZJ_FLOOR=N override the regression floor (default 2723)
;; JOLT_CHEZ_ZJ_FLOOR=N override the regression floor (default 2725)
;; JOLT_CORPUS_LIMIT=N every-Nth stride (fast iteration; floor drops to 0)
;; JOLT_DUMP_CRASH_LABELS=1 list crash + allowlisted labels
(import (chezscheme))
@ -91,7 +91,6 @@
"^Type tag on var"
"close on throw" "macroexpand-1"
"bean is the map" "proxy resolves nil"
"*in* is bound" "*in* bound"
"bigdec" "bigdec int M" "bigdec suffix M"
"transient vector" "transient map"
"atom override fires nested"
@ -192,7 +191,7 @@
;; Regression floor: fail on any NEW divergence or if pass drops below the floor.
(define base-floor (let ((s (getenv "JOLT_CHEZ_ZJ_FLOOR")))
(if s (string->number s) 2723)))
(if s (string->number s) 2725)))
(define floor (if limit 0 base-floor))
(when (or (> (length diverged) 0) (< pass floor))
(printf "REGRESSION: pass ~a < floor ~a or ~a new divergence(s)\n"

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View file

@ -15,41 +15,51 @@
(def ^:private reader-eof :jolt/reader-eof)
;; *in* is a Reader, not a map — so (map? *in*) is false, matching the JVM
;; (java.io.Reader). The reader is a reify over IReader (a non-map value, unlike
;; a defrecord which IS a map); read/read-line/read+string dispatch through its
;; methods. Each implementation closes over its own buffer atom: read may pull a
;; whole line to parse a form and hands the remainder to the next read/read-line.
(defprotocol IReader
(-read-line [rdr] "Next line, newline stripped; nil at EOF.")
(-read-form [rdr] "Next form; the reader-eof sentinel at end of input.")
(-read+string [rdr eof-error? eof-value]
"Next form plus the exact text consumed (leading whitespace included), as
[form text]. On EOF: throws, or returns [eof-value \"\"] when eof-error? is false."))
(defn __string-reader
"A reader over string s (the with-in-str expansion calls this)."
[s]
(let [buf (atom s)]
{:buf buf
:fill-fn nil
:read-line-fn
(fn []
(reify IReader
(-read-line [_]
(let [cur @buf]
(when (pos? (count cur))
(let [i (str-find "\n" cur)]
(if (nil? i)
(do (reset! buf "") cur)
(do (reset! buf (subs cur (inc i))) (subs cur 0 i)))))))
:read-fn
(fn []
(-read-form [_]
(let [r (__parse-next @buf)]
(if (nil? r)
reader-eof
(do (reset! buf (nth r 1)) (nth r 0)))))}))
(do (reset! buf (nth r 1)) (nth r 0)))))
(-read+string [_ eof-error? eof-value]
(let [s @buf
r (__parse-next s)]
(if (nil? r)
(if eof-error?
(throw (ex-info "EOF while reading" {}))
[eof-value ""])
(do (reset! buf (nth r 1))
[(nth r 0) (subs s 0 (- (count s) (count (nth r 1))))])))))))
;; Real stdin, with a leftover buffer shared by read and read-line: read may
;; pull a whole line to parse a form and must hand the remainder to the next
;; read/read-line.
;; Real stdin, with a leftover buffer shared by read and read-line.
(def ^:private stdin-buf (atom ""))
(def ^:dynamic *in*
{:buf stdin-buf
:fill-fn (fn []
(let [line (__stdin-read-line)]
(if (nil? line)
false
(do (swap! stdin-buf (fn [b] (str b line "\n"))) true))))
:read-line-fn
(fn []
(reify IReader
(-read-line [_]
(let [cur @stdin-buf]
(if (pos? (count cur))
(let [i (str-find "\n" cur)]
@ -57,8 +67,7 @@
(do (reset! stdin-buf "") cur)
(do (reset! stdin-buf (subs cur (inc i))) (subs cur 0 i))))
(__stdin-read-line))))
:read-fn
(fn []
(-read-form [_]
(loop []
(let [r (__parse-next @stdin-buf)]
(if (nil? r)
@ -66,25 +75,38 @@
(if (nil? line)
reader-eof
(do (swap! stdin-buf (fn [b] (str b line "\n"))) (recur))))
(do (reset! stdin-buf (nth r 1)) (nth r 0))))))})
(do (reset! stdin-buf (nth r 1)) (nth r 0))))))
(-read+string [_ eof-error? eof-value]
(loop []
(let [s @stdin-buf
r (__parse-next s)]
(if (nil? r)
(let [line (__stdin-read-line)]
(if (nil? line)
(if eof-error?
(throw (ex-info "EOF while reading" {}))
[eof-value ""])
(do (swap! stdin-buf (fn [b] (str b line "\n"))) (recur))))
(do (reset! stdin-buf (nth r 1))
[(nth r 0) (subs s 0 (- (count s) (count (nth r 1))))])))))))
(defn read-line
"Reads the next line from the stream that is the current value of *in*.
Returns nil at EOF."
[]
((:read-line-fn *in*)))
(-read-line *in*))
(defn read
"Reads the next object from stream (defaults to *in*). At EOF, throws
or returns eof-value when eof-error? is false."
([] (read *in*))
([stream]
(let [v ((:read-fn stream))]
(let [v (-read-form stream)]
(if (= v reader-eof)
(throw (ex-info "EOF while reading" {}))
v)))
([stream eof-error? eof-value]
(let [v ((:read-fn stream))]
(let [v (-read-form stream)]
(if (= v reader-eof)
(if eof-error? (throw (ex-info "EOF while reading" {})) eof-value)
v))))
@ -95,26 +117,11 @@
`(binding [*in* (__string-reader ~s)]
~@body))
;; Like read, and also returns the exact text consumed for the form (leading
;; whitespace included). On EOF: throws, or returns [eof-value ""] when
;; eof-error? is false.
(defn read+string
([] (read+string *in*))
([stream] (read+string stream true nil))
([stream eof-error? eof-value]
(let [buf (get stream :buf)
fill (get stream :fill-fn)]
(loop []
(let [s (deref buf)
r (__parse-next s)]
(if (nil? r)
(if (and fill (fill))
(recur)
(if eof-error?
(throw (ex-info "EOF while reading" {}))
[eof-value ""]))
(do (reset! buf (nth r 1))
[(nth r 0) (subs s 0 (- (count s) (count (nth r 1))))])))))))
(-read+string stream eof-error? eof-value)))
(defn line-seq
"Returns the lines of text from rdr as a lazy sequence of strings, as by
@ -124,7 +131,7 @@
(if (string? rdr)
(seq (str-split "\n" rdr))
(lazy-seq
(let [line ((:read-line-fn rdr))]
(let [line (-read-line rdr)]
(when line
(cons line (line-seq rdr)))))))