core: Phase 4 — move ns-name and the array reads/aset to the overlay
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.
This commit is contained in:
parent
790f54d2b3
commit
bfac1fc444
3 changed files with 30 additions and 20 deletions
|
|
@ -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))
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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)"])
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue