blob: 3f0181c994d1fbdc105aaf4b7425126b782dd1cd (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
|
(ns lux.host
(:require (clojure [string :as string]
[template :refer [do-template]])
clojure.core.match
clojure.core.match.array
(lux [base :as & :refer [|do return* return |let |case]]
[type :as &type])
[lux.type.host :as &host-type]
[lux.host.generics :as &host-generics])
(:import (java.lang.reflect Field Method Constructor Modifier Type
GenericArrayType ParameterizedType TypeVariable)
(org.objectweb.asm Opcodes
Label
ClassWriter
MethodVisitor)))
;; [Constants]
(def function-class "lux.Function")
(def module-separator "/")
(def class-name-separator ".")
(def class-separator "/")
(def bytecode-version Opcodes/V1_6)
;; [Resources]
(defn ^String ->module-class [old]
old)
(def ->package ->module-class)
(defn unfold-array [type]
"(-> Type (, Int Type))"
(|case type
(&/$Primitive "#Array" (&/$Cons param (&/$Nil)))
(|let [[count inner] (unfold-array param)]
(&/T [(inc count) inner]))
_
(&/T [0 type])))
(let [ex-type-class (str "L" (&host-generics/->bytecode-class-name "java.lang.Object") ";")
object-array (str "[" "L" (&host-generics/->bytecode-class-name "java.lang.Object") ";")]
(defn ->java-sig [^objects type]
"(-> Type (Lux Text))"
(|case type
(&/$Primitive ?name params)
(cond (= &host-type/array-data-tag ?name) (|do [:let [[level base] (unfold-array type)]
base-sig (|case base
(&/$Primitive base-class _)
(return (&host-generics/->type-signature base-class))
_
(->java-sig base))]
(return (str (->> (&/|repeat level "[") (&/fold str ""))
base-sig)))
(= &host-type/null-data-tag ?name) (return (&host-generics/->type-signature "java.lang.Object"))
:else (return (&host-generics/->type-signature ?name)))
(&/$Function _ _)
(return (&host-generics/->type-signature function-class))
(&/$Sum _)
(return object-array)
(&/$Product _)
(return object-array)
(&/$Named ?name ?type)
(->java-sig ?type)
(&/$Apply ?A ?F)
(|do [type* (&type/apply-type ?F ?A)]
(->java-sig type*))
(&/$Ex _)
(return ex-type-class)
_
(if (&type/type= &type/Top type)
(return "V")
(assert false (str '->java-sig " " (&type/show-type type))))
)))
(do-template [<name> <static?>]
(defn <name> [class-loader target field]
(|let [target-class (Class/forName target true class-loader)]
(if-let [^Type gtype (first (for [^Field =field (seq (.getDeclaredFields target-class))
:when (and (.equals ^Object field (.getName =field))
(.equals ^Object <static?> (Modifier/isStatic (.getModifiers =field))))]
(.getGenericType =field)))]
(|let [gvars (->> target-class .getTypeParameters seq &/->list)]
(return (&/T [gvars gtype])))
(&/fail-with-loc (str "[Host Error] Field does not exist: " target "." field)))))
lookup-static-field true
lookup-field false
)
(do-template [<name> <static?> <method-type>]
(defn <name> [class-loader target method-name args]
(|let [target-class (Class/forName target true class-loader)]
(if-let [[^Method method ^Class declarer] (first (for [^Method =method (.getDeclaredMethods target-class)
:when (and (.equals ^Object method-name (.getName =method))
(.equals ^Object <static?> (Modifier/isStatic (.getModifiers =method)))
(let [param-types (&/->list (seq (.getParameterTypes =method)))]
(and (= (&/|length args) (&/|length param-types))
(&/fold2 #(and %1 (.equals ^Object %2 %3))
true
args
(&/|map #(.getName ^Class %) param-types)))))]
[=method
(.getDeclaringClass =method)]))]
(if (= target-class declarer)
(|let [parent-gvars (->> target-class .getTypeParameters seq &/->list)
gvars (->> method .getTypeParameters seq &/->list)
gargs (->> method .getGenericParameterTypes seq &/->list)
_ (when (.getAnnotation method java.lang.Deprecated)
(println (str "[Host Warning] Deprecated method: " target "." method-name " " (->> args &/->seq print-str))))]
(return (&/T [(.getGenericReturnType method)
(->> method .getExceptionTypes &/->list (&/|map #(.getName ^Class %)))
parent-gvars
gvars
gargs])))
(&/fail-with-loc (str "[Host Error] " <method-type> " method " (pr-str method-name) " for " "(" (->> args (&/|interpose ", ") (&/fold str "")) ")" " belongs to parent " (.getName declarer) " instead of " target)))
(&/fail-with-loc (str "[Host Error] " <method-type> " method does not exist: " target "." method-name " " "(" (->> args (&/|interpose ", ") (&/fold str "")) ")")))))
lookup-static-method true "Static"
lookup-virtual-method false "Virtual"
)
(defn lookup-constructor [class-loader target args]
(let [target-class (Class/forName target true class-loader)]
(if-let [^Constructor ctor (first (for [^Constructor =method (.getDeclaredConstructors target-class)
:when (let [param-types (&/->list (seq (.getParameterTypes =method)))]
(and (= (&/|length args) (&/|length param-types))
(&/fold2 #(and %1 (.equals ^Object %2 %3))
true
args
(&/|map #(.getName ^Class %) param-types))))]
=method))]
(|let [gvars (->> target-class .getTypeParameters seq &/->list)
gargs (->> ctor .getGenericParameterTypes seq &/->list)
exs (->> ctor .getExceptionTypes &/->list (&/|map #(.getName ^Class %)))
_ (when (.getAnnotation ctor java.lang.Deprecated)
(println (str "[Host Warning] Deprecated constructor: " target " " (->> args &/->seq print-str))))]
(return (&/T [exs gvars gargs])))
(&/fail-with-loc (str "[Host Error] Constructor does not exist: " target " " (->> args &/->seq print-str))))))
(defn abstract-methods [class-loader super-class]
"(-> ClassLoader SuperClassDecl (Lux (List (, Text (List Text)))))"
(|let [[super-name super-params] super-class]
(return (&/->list (for [^Method =method (.getDeclaredMethods (Class/forName super-name true class-loader))
:when (Modifier/isAbstract (.getModifiers =method))]
(&/T [(.getName =method) (&/|map #(.getName ^Class %) (&/->list (seq (.getParameterTypes =method))))]))))))
(defn def-name [name]
(str (&/normalize-name name) "_" (Long/toUnsignedString (hash name))))
(defn location [scope]
(let [scope (&/$Cons (def-name (&/|head scope))
(&/|map &/normalize-name (&/|tail scope)))]
(->> scope
(&/|interpose "$")
(&/fold str ""))))
(defn primitive-jvm-type? [type]
(case type
("boolean" "byte" "short" "int" "long" "float" "double" "char")
true
;; else
false))
(defn dummy-value [^MethodVisitor writer class]
(|case class
(&/$GenericClass "boolean" (&/$Nil))
(doto writer
(.visitLdcInsn false))
(&/$GenericClass "byte" (&/$Nil))
(doto writer
(.visitLdcInsn (byte 0)))
(&/$GenericClass "short" (&/$Nil))
(doto writer
(.visitLdcInsn (short 0)))
(&/$GenericClass "int" (&/$Nil))
(doto writer
(.visitLdcInsn (int 0)))
(&/$GenericClass "long" (&/$Nil))
(doto writer
(.visitLdcInsn (long 0)))
(&/$GenericClass "float" (&/$Nil))
(doto writer
(.visitLdcInsn (float 0.0)))
(&/$GenericClass "double" (&/$Nil))
(doto writer
(.visitLdcInsn (double 0.0)))
(&/$GenericClass "char" (&/$Nil))
(doto writer
(.visitLdcInsn (char 0)))
_
(doto writer
(.visitInsn Opcodes/ACONST_NULL))))
(defn ^:private dummy-return [^MethodVisitor writer output]
(|case output
(&/$GenericClass "void" (&/$Nil))
(.visitInsn writer Opcodes/RETURN)
(&/$GenericClass "boolean" (&/$Nil))
(doto writer
(dummy-value output)
(.visitInsn Opcodes/IRETURN))
(&/$GenericClass "byte" (&/$Nil))
(doto writer
(dummy-value output)
(.visitInsn Opcodes/IRETURN))
(&/$GenericClass "short" (&/$Nil))
(doto writer
(dummy-value output)
(.visitInsn Opcodes/IRETURN))
(&/$GenericClass "int" (&/$Nil))
(doto writer
(dummy-value output)
(.visitInsn Opcodes/IRETURN))
(&/$GenericClass "long" (&/$Nil))
(doto writer
(dummy-value output)
(.visitInsn Opcodes/LRETURN))
(&/$GenericClass "float" (&/$Nil))
(doto writer
(dummy-value output)
(.visitInsn Opcodes/FRETURN))
(&/$GenericClass "double" (&/$Nil))
(doto writer
(dummy-value output)
(.visitInsn Opcodes/DRETURN))
(&/$GenericClass "char" (&/$Nil))
(doto writer
(dummy-value output)
(.visitInsn Opcodes/IRETURN))
_
(doto writer
(dummy-value output)
(.visitInsn Opcodes/ARETURN))))
(defn ^:private ->dummy-type [real-name store-name gclass]
(|case gclass
(&/$GenericClass _name _params)
(if (= real-name _name)
(&/$GenericClass store-name (&/|map (partial ->dummy-type real-name store-name) _params))
gclass)
_
gclass))
(def init-method-name "<init>")
(defn ^:private dummy-ctor [^MethodVisitor writer real-name store-name super-class ctor-args]
(|let [ctor-arg-types (->> ctor-args (&/|map (comp &host-generics/->type-signature (comp (partial ->dummy-type real-name store-name) &/|first))) (&/fold str ""))]
(doto writer
(.visitVarInsn Opcodes/ALOAD 0)
(-> (doto (dummy-value arg-type)
(-> (.visitTypeInsn Opcodes/CHECKCAST (&host-generics/->bytecode-class-name arg-type))
(->> (when (not (primitive-jvm-type? arg-type))))))
(->> (doseq [ctor-arg (&/->seq ctor-args)
:let [;; arg-term (&/|first ctor-arg)
arg-type (->dummy-type real-name store-name (&/|first ctor-arg))]])))
(.visitMethodInsn Opcodes/INVOKESPECIAL (&host-generics/->bytecode-class-name (&host-generics/super-class-name super-class)) init-method-name (str "(" ctor-arg-types ")V"))
(.visitInsn Opcodes/RETURN))))
(defn ^:private compile-dummy-method [^ClassWriter =class real-name store-name super-class method-def]
(|case method-def
(&/$ConstructorMethodSyntax =privacy-modifier ?strict =anns =gvars =exceptions =inputs =ctor-args body)
(|let [=output (&/$GenericClass "void" (&/|list))
method-decl [init-method-name =anns =gvars (&/|map (partial ->dummy-type real-name store-name) =exceptions) (&/|map (comp (partial ->dummy-type real-name store-name) &/|second) =inputs) (->dummy-type real-name store-name =output)]
[simple-signature generic-signature] (&host-generics/method-signatures method-decl)]
(doto (.visitMethod =class Opcodes/ACC_PUBLIC
init-method-name
simple-signature
generic-signature
(->> =exceptions (&/|map &host-generics/gclass->bytecode-class-name) &/->seq (into-array java.lang.String)))
.visitCode
(dummy-ctor real-name store-name super-class =ctor-args)
(.visitMaxs 0 0)
(.visitEnd)))
(&/$VirtualMethodSyntax =name =privacy-modifier =final? ?strict =anns =gvars =exceptions =inputs =output body)
(|let [method-decl [=name =anns =gvars (&/|map (partial ->dummy-type real-name store-name) =exceptions) (&/|map (comp (partial ->dummy-type real-name store-name) &/|second) =inputs) (->dummy-type real-name store-name =output)]
[simple-signature generic-signature] (&host-generics/method-signatures method-decl)]
(doto (.visitMethod =class (+ Opcodes/ACC_PUBLIC
(if =final? Opcodes/ACC_FINAL 0))
=name
simple-signature
generic-signature
(->> =exceptions (&/|map &host-generics/gclass->bytecode-class-name) &/->seq (into-array java.lang.String)))
.visitCode
(dummy-return =output)
(.visitMaxs 0 0)
(.visitEnd)))
(&/$OverridenMethodSyntax =class-decl =name ?strict =anns =gvars =exceptions =inputs =output body)
(|let [method-decl [=name =anns =gvars (&/|map (partial ->dummy-type real-name store-name) =exceptions) (&/|map (comp (partial ->dummy-type real-name store-name) &/|second) =inputs) (->dummy-type real-name store-name =output)]
[simple-signature generic-signature] (&host-generics/method-signatures method-decl)]
(doto (.visitMethod =class Opcodes/ACC_PUBLIC
=name
simple-signature
generic-signature
(->> =exceptions (&/|map &host-generics/gclass->bytecode-class-name) &/->seq (into-array java.lang.String)))
.visitCode
(dummy-return =output)
(.visitMaxs 0 0)
(.visitEnd)))
(&/$StaticMethodSyntax =name =privacy-modifier ?strict =anns =gvars =exceptions =inputs =output body)
(|let [method-decl [=name =anns =gvars (&/|map (partial ->dummy-type real-name store-name) =exceptions) (&/|map (comp (partial ->dummy-type real-name store-name) &/|second) =inputs) (->dummy-type real-name store-name =output)]
[simple-signature generic-signature] (&host-generics/method-signatures method-decl)]
(doto (.visitMethod =class (+ Opcodes/ACC_PUBLIC Opcodes/ACC_STATIC)
=name
simple-signature
generic-signature
(->> =exceptions (&/|map &host-generics/gclass->bytecode-class-name) &/->seq (into-array java.lang.String)))
.visitCode
(dummy-return =output)
(.visitMaxs 0 0)
(.visitEnd)))
(&/$AbstractMethodSyntax =name =privacy-modifier =anns =gvars =exceptions =inputs =output)
(|let [method-decl [=name =anns =gvars (&/|map (partial ->dummy-type real-name store-name) =exceptions) (&/|map (comp (partial ->dummy-type real-name store-name) &/|second) =inputs) (->dummy-type real-name store-name =output)]
[simple-signature generic-signature] (&host-generics/method-signatures method-decl)]
(doto (.visitMethod =class (+ Opcodes/ACC_PUBLIC Opcodes/ACC_ABSTRACT)
=name
simple-signature
generic-signature
(->> =exceptions (&/|map &host-generics/gclass->bytecode-class-name) &/->seq (into-array java.lang.String)))
(.visitEnd)))
(&/$NativeMethodSyntax =name =privacy-modifier =anns =gvars =exceptions =inputs =output)
(|let [method-decl [=name =anns =gvars (&/|map (partial ->dummy-type real-name store-name) =exceptions) (&/|map (comp (partial ->dummy-type real-name store-name) &/|second) =inputs) (->dummy-type real-name store-name =output)]
[simple-signature generic-signature] (&host-generics/method-signatures method-decl)]
(doto (.visitMethod =class (+ Opcodes/ACC_PUBLIC Opcodes/ACC_NATIVE)
=name
simple-signature
generic-signature
(->> =exceptions (&/|map &host-generics/gclass->bytecode-class-name) &/->seq (into-array java.lang.String)))
(.visitEnd)))
_
(assert false (println-str 'compile-dummy-method (&/adt->text method-def)))
))
(defn privacy-modifier->flag [privacy-modifier]
"(-> PrivacyModifier Int)"
(|case privacy-modifier
(&/$PublicPM) Opcodes/ACC_PUBLIC
(&/$PrivatePM) Opcodes/ACC_PRIVATE
(&/$ProtectedPM) Opcodes/ACC_PROTECTED
(&/$DefaultPM) 0
))
(defn state-modifier->flag [state-modifier]
"(-> StateModifier Int)"
(|case state-modifier
(&/$DefaultSM) 0
(&/$VolatileSM) Opcodes/ACC_VOLATILE
(&/$FinalSM) Opcodes/ACC_FINAL))
(defn inheritance-modifier->flag [inheritance-modifier]
"(-> InheritanceModifier Int)"
(|case inheritance-modifier
(&/$DefaultIM) 0
(&/$AbstractIM) Opcodes/ACC_ABSTRACT
(&/$FinalIM) Opcodes/ACC_FINAL))
(defn use-dummy-class [class-decl super-class interfaces ctor-args fields methods]
(|do [module &/get-module-name
:let [[?name ?params] class-decl
dummy-name ?name;; (str ?name "__DUMMY__")
dummy-full-name (str module "/" dummy-name)
real-name (str (&host-generics/->class-name module) "." ?name)
store-name (str (&host-generics/->class-name module) "." dummy-name)
class-signature (&host-generics/gclass-decl->signature class-decl (&/$Cons super-class interfaces))
=class (doto (new ClassWriter ClassWriter/COMPUTE_MAXS)
(.visit bytecode-version (+ Opcodes/ACC_PUBLIC Opcodes/ACC_SUPER)
dummy-full-name
(if (= "" class-signature) nil class-signature)
(&host-generics/->bytecode-class-name (&host-generics/super-class-name super-class))
(->> interfaces (&/|map (comp &host-generics/->bytecode-class-name &host-generics/super-class-name)) &/->seq (into-array String))))
_ (&/|map (fn [field]
(|case field
(&/$ConstantFieldAnalysis =name =anns =type ?value)
(doto (.visitField =class (+ Opcodes/ACC_PUBLIC Opcodes/ACC_STATIC Opcodes/ACC_FINAL) =name
(&host-generics/gclass->simple-signature =type)
(&host-generics/gclass->signature =type)
nil)
(.visitEnd))
(&/$VariableFieldAnalysis =name =privacy-modifier =state-modifier =anns =type)
(doto (.visitField =class (+ Opcodes/ACC_PUBLIC (state-modifier->flag =state-modifier)) =name
(&host-generics/gclass->simple-signature =type)
(&host-generics/gclass->signature =type)
nil)
(.visitEnd))
))
fields)
_ (&/|map (partial compile-dummy-method =class real-name store-name super-class) methods)
bytecode (.toByteArray (doto =class .visitEnd))]
^ClassLoader loader &/loader
!classes &/classes
:let [_ (swap! !classes assoc store-name bytecode)
_ (.loadClass loader store-name)]
_ (&/push-dummy-name real-name store-name)]
(return nil)))
|