From bfac1fc444ee62c7988fe241a87268e799aa54e1 Mon Sep 17 00:00:00 2001 From: Yogthos Date: Sun, 7 Jun 2026 21:52:13 -0400 Subject: [PATCH] =?UTF-8?q?core:=20Phase=204=20=E2=80=94=20move=20ns-name?= =?UTF-8?q?=20and=20the=20array=20reads/aset=20to=20the=20overlay?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ns-name is pure over get + symbol. aget/alength read jolt's mutable backing arrays (nth/count), and aset writes a slot through jolt.host/ref-put!; both handle the multi-dimensional form by walking. The array constructors (object-array/make-array/to-array/...) stay native — they build the mutable backing. Added array spec cases; ns-name already covered. --- jolt-core/clojure/core/20-coll.clj | 19 +++++++++++++++++++ src/jolt/core.janet | 24 ++++-------------------- test/spec/host-interop-spec.janet | 7 +++++++ 3 files changed, 30 insertions(+), 20 deletions(-) diff --git a/jolt-core/clojure/core/20-coll.clj b/jolt-core/clojure/core/20-coll.clj index efd9fdb..3194fc6 100644 --- a/jolt-core/clojure/core/20-coll.clj +++ b/jolt-core/clojure/core/20-coll.clj @@ -301,3 +301,22 @@ (if (future? x) (boolean (get x :cached)) (throw "future-done? requires a future"))) (defn future-cancelled? [x] (and (future? x) (boolean (get x :cancelled)))) + +;; ns-name: a namespace object's :name as a symbol. Pure over get + symbol. +(defn ns-name [ns] + (let [nm (get ns :name)] (if nm (symbol (str nm)) nil))) + +;; Java-array element access. Jolt arrays are mutable backing arrays; aget/alength +;; read them (nth/count) and aset writes a slot through ref-put!. Both handle the +;; multi-dimensional form (aget a i j ... / aset a i j ... v) by walking. The array +;; constructors (object-array/make-array/to-array/...) stay native — they build the +;; mutable backing. +(defn aget [arr & idxs] + (reduce (fn [v i] (nth v i)) arr idxs)) +(defn alength [arr] (count arr)) +(defn aset [arr & idxs+val] + (let [n (count idxs+val) + val (nth idxs+val (dec n)) + target (reduce (fn [t k] (nth t k)) arr (take (- n 2) idxs+val))] + (jolt.host/ref-put! target (nth idxs+val (- n 2)) val) + val)) diff --git a/src/jolt/core.janet b/src/jolt/core.janet index 676f458..fcd7ce4 100644 --- a/src/jolt/core.janet +++ b/src/jolt/core.janet @@ -1675,18 +1675,9 @@ # numeric arrays use Janet arrays. aget/aset/alength/aclone work over both. # ============================================================ -(defn core-alength [arr] (length arr)) - -(defn core-aget [arr & idxs] - # multi-dim: aget arr i j ... walks nested arrays - (var v arr) (each i idxs (set v (in v i))) v) - -(defn core-aset [arr & more] - # (aset arr i v) or (aset arr i j ... v): last arg is the value - (let [n (length more) val (in more (- n 1))] - (var target arr) (var k 0) - (while (< k (- n 2)) (set target (in target (in more k))) (++ k)) - (put target (in more (- n 2)) val) val)) +# alength / aget / aset now live in the Clojure collection tier — count/nth reads +# and an aset write through jolt.host/ref-put!. The typed/object array constructors +# below stay native (they build the mutable backing). (defn core-aclone [arr] (if (buffer? arr) (buffer/slice arr) (array/slice arr))) @@ -2124,10 +2115,7 @@ # resolve stub — returns nil (symbols not found in Jolt's clojure.core) (defn core-resolve [sym] nil) # shadowed by the resolve special form (needs ctx) -(defn core-ns-name [ns] - # ns object -> its name as a symbol (works whether ns is a table/struct/phm) - (let [nm (core-get ns :name)] - (if nm {:jolt/type :symbol :ns nil :name (string nm)} nil))) +# ns-name now lives in the Clojure collection tier (pure over get + symbol). # update lives in the Clojure kernel tier — core/00-kernel.clj. update-in stays # (it's recursive and has internal callers). @@ -2875,9 +2863,6 @@ "prn" core-prn "pr-str" core-pr-str # Java-style arrays (buffers for bytes, arrays otherwise) - "alength" core-alength - "aget" core-aget - "aset" core-aset "aclone" core-aclone "object-array" core-object-array "int-array" core-int-array @@ -3018,7 +3003,6 @@ "ThreadLocal" core-ThreadLocal "IllegalStateException" core-IllegalStateException "resolve" core-resolve - "ns-name" core-ns-name "update-in" core-update-in "assoc-in" core-assoc-in "fnil" core-fnil diff --git a/test/spec/host-interop-spec.janet b/test/spec/host-interop-spec.janet index 7de9922..b9a297d 100644 --- a/test/spec/host-interop-spec.janet +++ b/test/spec/host-interop-spec.janet @@ -31,3 +31,10 @@ ["janet-type string" ":string" "(do (require (quote [jolt.interop :as j])) (j/janet-type \"x\"))"] ["janet-type number" ":number" "(do (require (quote [jolt.interop :as j])) (j/janet-type 1))"] ["janet-type keyword" ":keyword" "(do (require (quote [jolt.interop :as j])) (j/janet-type :a))"]) + +(defspec "interop / arrays (aget/aset/alength)" + ["alength" "3" "(alength (object-array [1 2 3]))"] + ["aget" "20" "(aget (object-array [10 20 30]) 1)"] + ["aset returns val" "9" "(aset (object-array [1 2 3]) 1 9)"] + ["aset mutates" "[7 2 3]" "(let [a (object-array [1 2 3])] (aset a 0 7) (vec a))"] + ["aget 2d" "4" "(aget (to-array-2d [[1 2] [3 4]]) 1 1)"])