*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. ;; reset between cases so there is no leakage — same isolation a fresh process gives.
;; ;;
;; chez --script host/chez/run-corpus.ss ;; 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_CORPUS_LIMIT=N every-Nth stride (fast iteration; floor drops to 0)
;; JOLT_DUMP_CRASH_LABELS=1 list crash + allowlisted labels ;; JOLT_DUMP_CRASH_LABELS=1 list crash + allowlisted labels
(import (chezscheme)) (import (chezscheme))
@ -91,7 +91,6 @@
"^Type tag on var" "^Type tag on var"
"close on throw" "macroexpand-1" "close on throw" "macroexpand-1"
"bean is the map" "proxy resolves nil" "bean is the map" "proxy resolves nil"
"*in* is bound" "*in* bound"
"bigdec" "bigdec int M" "bigdec suffix M" "bigdec" "bigdec int M" "bigdec suffix M"
"transient vector" "transient map" "transient vector" "transient map"
"atom override fires nested" "atom override fires nested"
@ -192,7 +191,7 @@
;; Regression floor: fail on any NEW divergence or if pass drops below the floor. ;; Regression floor: fail on any NEW divergence or if pass drops below the floor.
(define base-floor (let ((s (getenv "JOLT_CHEZ_ZJ_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)) (define floor (if limit 0 base-floor))
(when (or (> (length diverged) 0) (< pass floor)) (when (or (> (length diverged) 0) (< pass floor))
(printf "REGRESSION: pass ~a < floor ~a or ~a new divergence(s)\n" (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,76 +15,98 @@
(def ^:private reader-eof :jolt/reader-eof) (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 (defn __string-reader
"A reader over string s (the with-in-str expansion calls this)." "A reader over string s (the with-in-str expansion calls this)."
[s] [s]
(let [buf (atom s)] (let [buf (atom s)]
{:buf buf (reify IReader
:fill-fn nil (-read-line [_]
:read-line-fn (let [cur @buf]
(fn [] (when (pos? (count cur))
(let [cur @buf] (let [i (str-find "\n" cur)]
(when (pos? (count cur)) (if (nil? i)
(let [i (str-find "\n" cur)] (do (reset! buf "") cur)
(if (nil? i) (do (reset! buf (subs cur (inc i))) (subs cur 0 i)))))))
(do (reset! buf "") cur) (-read-form [_]
(do (reset! buf (subs cur (inc i))) (subs cur 0 i))))))) (let [r (__parse-next @buf)]
:read-fn (if (nil? r)
(fn [] reader-eof
(let [r (__parse-next @buf)] (do (reset! buf (nth r 1)) (nth r 0)))))
(if (nil? r) (-read+string [_ eof-error? eof-value]
reader-eof (let [s @buf
(do (reset! buf (nth r 1)) (nth r 0)))))})) 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 ;; Real stdin, with a leftover buffer shared by read and read-line.
;; pull a whole line to parse a form and must hand the remainder to the next
;; read/read-line.
(def ^:private stdin-buf (atom "")) (def ^:private stdin-buf (atom ""))
(def ^:dynamic *in* (def ^:dynamic *in*
{:buf stdin-buf (reify IReader
:fill-fn (fn [] (-read-line [_]
(let [line (__stdin-read-line)] (let [cur @stdin-buf]
(if (nil? line) (if (pos? (count cur))
false (let [i (str-find "\n" cur)]
(do (swap! stdin-buf (fn [b] (str b line "\n"))) true)))) (if (nil? i)
:read-line-fn (do (reset! stdin-buf "") cur)
(fn [] (do (reset! stdin-buf (subs cur (inc i))) (subs cur 0 i))))
(let [cur @stdin-buf] (__stdin-read-line))))
(if (pos? (count cur)) (-read-form [_]
(let [i (str-find "\n" cur)] (loop []
(if (nil? i) (let [r (__parse-next @stdin-buf)]
(do (reset! stdin-buf "") cur) (if (nil? r)
(do (reset! stdin-buf (subs cur (inc i))) (subs cur 0 i)))) (let [line (__stdin-read-line)]
(__stdin-read-line)))) (if (nil? line)
:read-fn reader-eof
(fn [] (do (swap! stdin-buf (fn [b] (str b line "\n"))) (recur))))
(loop [] (do (reset! stdin-buf (nth r 1)) (nth r 0))))))
(let [r (__parse-next @stdin-buf)] (-read+string [_ eof-error? eof-value]
(if (nil? r) (loop []
(let [line (__stdin-read-line)] (let [s @stdin-buf
(if (nil? line) r (__parse-next s)]
reader-eof (if (nil? r)
(do (swap! stdin-buf (fn [b] (str b line "\n"))) (recur)))) (let [line (__stdin-read-line)]
(do (reset! stdin-buf (nth r 1)) (nth r 0))))))}) (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 (defn read-line
"Reads the next line from the stream that is the current value of *in*. "Reads the next line from the stream that is the current value of *in*.
Returns nil at EOF." Returns nil at EOF."
[] []
((:read-line-fn *in*))) (-read-line *in*))
(defn read (defn read
"Reads the next object from stream (defaults to *in*). At EOF, throws "Reads the next object from stream (defaults to *in*). At EOF, throws
or returns eof-value when eof-error? is false." or returns eof-value when eof-error? is false."
([] (read *in*)) ([] (read *in*))
([stream] ([stream]
(let [v ((:read-fn stream))] (let [v (-read-form stream)]
(if (= v reader-eof) (if (= v reader-eof)
(throw (ex-info "EOF while reading" {})) (throw (ex-info "EOF while reading" {}))
v))) v)))
([stream eof-error? eof-value] ([stream eof-error? eof-value]
(let [v ((:read-fn stream))] (let [v (-read-form stream)]
(if (= v reader-eof) (if (= v reader-eof)
(if eof-error? (throw (ex-info "EOF while reading" {})) eof-value) (if eof-error? (throw (ex-info "EOF while reading" {})) eof-value)
v)))) v))))
@ -95,26 +117,11 @@
`(binding [*in* (__string-reader ~s)] `(binding [*in* (__string-reader ~s)]
~@body)) ~@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 (defn read+string
([] (read+string *in*)) ([] (read+string *in*))
([stream] (read+string stream true nil)) ([stream] (read+string stream true nil))
([stream eof-error? eof-value] ([stream eof-error? eof-value]
(let [buf (get stream :buf) (-read+string stream eof-error? eof-value)))
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))))])))))))
(defn line-seq (defn line-seq
"Returns the lines of text from rdr as a lazy sequence of strings, as by "Returns the lines of text from rdr as a lazy sequence of strings, as by
@ -124,7 +131,7 @@
(if (string? rdr) (if (string? rdr)
(seq (str-split "\n" rdr)) (seq (str-split "\n" rdr))
(lazy-seq (lazy-seq
(let [line ((:read-line-fn rdr))] (let [line (-read-line rdr)]
(when line (when line
(cons line (line-seq rdr))))))) (cons line (line-seq rdr)))))))