Skip to content

Commit 4fef861

Browse files
committed
support arbitrary emit values, not just seqs
1 parent 3c28620 commit 4fef861

File tree

5 files changed

+18
-17
lines changed

5 files changed

+18
-17
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* Add support for custom index objects via the `LocalIndex` protocol
77
* Generate default constructor for :abstract classes too if one not provided
88
* Support passing parameter and return types separately when defining methods
9-
* Experimental new map compilation facilitated by `:emit-seq-fn`.
9+
* Experimental new map-style ops facilitated by `:emit-fn`.
1010

1111
## 0.5.4 (2022-04-14)
1212

src/insn/core.clj

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,12 @@
6969
7070
:debug string of arbitrary debug information - optional.
7171
72-
:emit-seq-fn fn to call with each bytecode seq. This is an advanced
73-
option and, if present, will be passed both the ASM
74-
MethodVisitor and the raw (possibly nested) :emit op
75-
seq for each method. Can be used to support new op
76-
data types. For example, the `insn.op-map/emit-seq` fn
77-
can be used to enable map-style op sequences.
72+
:emit-fn fn to be called to emit method bytecode. This is an
73+
advanced option and, if present, will be passed both
74+
the ASM MethodVisitor and the raw :emit value for each
75+
method. Can be used to support new op data types. For
76+
example, the `insn.op-map/emit-seq` fn can be used to
77+
enable map-style op sequences.
7878
7979
Each field and method can also be given :annotations and a :signature
8080
as per above.
@@ -200,13 +200,13 @@
200200
:emit [[:aload 0]
201201
[:invokespecial :super :init [:void]]
202202
[:return]]}))
203-
emit-seq (:emit-seq-fn t op/emit-seq)]
203+
emit-fn (:emit-fn t op/emit-seq)]
204204
(binding [util/*this* this
205205
util/*super* super]
206206
(doseq [f (:fields t)]
207207
(visit-field cv f))
208208
(doseq [m (:methods t)]
209-
(visit-method cv m emit-seq))
209+
(visit-method cv m emit-fn))
210210
(doto cv
211211
(ann/visit (:annotations t))
212212
.visitEnd))
@@ -225,7 +225,7 @@
225225
(ann/visit fv (:annotations f))
226226
(.visitEnd fv)))
227227

228-
(defn- visit-method [^ClassVisitor cv m emit-seq]
228+
(defn- visit-method [^ClassVisitor cv m emit-fn]
229229
(let [mname (util/method-name (:name m))
230230
clinit? (= mname "<clinit>")
231231
init? (= mname "<init>")
@@ -255,7 +255,7 @@
255255
(binding [util/*labels* (atom {})]
256256
(if (fn? emit)
257257
(emit mv)
258-
(emit-seq mv emit)))
258+
(emit-fn mv emit)))
259259
(ann/visit mv (:annotations m))
260260
(doseq [[i anns] (or (:parameter-annotations m) (:param-annotations m))]
261261
(ann/visit mv i anns))

src/insn/op.clj

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@
347347
(.visitLookupSwitchInsn v (util/label-from default) (int-array tkeys)
348348
(util/label-array tlabels)))
349349

350-
(defn lookupswitch
350+
(defn ^::op lookupswitch
351351
"Like `lookupswitch*` with the keys and labels given as a map."
352352
[v default m]
353353
(let [[tkeys tlabels] ((juxt identity (partial map m))
@@ -416,7 +416,8 @@
416416
(doseq [op ops]
417417
(apply (::fn op) v (::args op))))))
418418

419-
(def ^:private keyword-opcode*
419+
(def keyword-opcodes
420+
"Map of op keyword to ASM Opcode number."
420421
(into {} (for [var (vals (ns-publics *ns*))
421422
:let [m (meta var)]
422423
:when (::op m)]
@@ -429,7 +430,7 @@
429430
"Return the ASM opcode number as a long for the given op keyword. If
430431
the keyword is invalid an error is raised."
431432
^long [k]
432-
(let [v (util/check-valid "op keyword" keyword-opcode* k)]
433+
(let [v (util/check-valid "op keyword" keyword-opcodes k)]
433434
(.longValue ^Integer v)))
434435

435436
(defn emit-seq

src/insn/op_map.clj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@
105105
{:op :areturn}
106106
..."
107107
(fn [v op] (:op op))
108-
:default ::default)
108+
:default ::invalid)
109109

110110
(defmacro ^:private defops
111111
([ops mkeys]

test/insn/core_test.clj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -407,8 +407,8 @@
407407
:type (.getReturnType m)
408408
:anns (map #(.annotationType %) (aget (.getParameterAnnotations m) 0))})))))
409409

410-
(deftest test-emit-seq-fn
411-
(let [t {:emit-seq-fn op-map/emit-seq
410+
(deftest test-emit-fn
411+
(let [t {:emit-fn op-map/emit-seq
412412
:methods [{:name :init :params []
413413
:emit [{:op :aload :index 0}
414414
{:op :invokespecial :class :super :name :init}

0 commit comments

Comments
 (0)