aboutsummaryrefslogtreecommitdiff
path: root/src/lux/host.clj
blob: b05c30ad3ab2df158da861d66f6f996630575fce (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
;;  Copyright (c) Eduardo Julian. All rights reserved.
;;  This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
;;  If a copy of the MPL was not distributed with this file,
;;  You can obtain one at http://mozilla.org/MPL/2.0/.

(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 fail fail* |let |case]]
                 [type :as &type]))
  (:import (java.lang.reflect Field Method Constructor Modifier)
           java.util.regex.Pattern
           (org.objectweb.asm Opcodes
                              Label
                              ClassWriter
                              MethodVisitor)))

;; [Constants]
(def prefix "lux.")
(def function-class (str prefix "Function"))
(def module-separator "/")
(def class-name-separator ".")
(def class-separator "/")
(def array-data-tag "#Array")
(def null-data-tag "#Null")

;; [Utils]
(def class-name-re #"((\[+)L([\.a-zA-Z0-9]+);|([\.a-zA-Z0-9]+))")

(comment
  (let [class (class (to-array []))]
    (str (if-let [pkg (.getPackage class)]
           (str (.getName pkg) ".")
           "")
         (.getSimpleName class)))

  (.getName String) "java.lang.String"

  (.getName (class (to-array []))) "[Ljava.lang.Object;"

  (re-find class-name-re "java.lang.String")
  ["java.lang.String" "java.lang.String" nil nil "java.lang.String"]

  (re-find class-name-re "[Ljava.lang.Object;")
  ["[Ljava.lang.Object;" "[Ljava.lang.Object;" "[" "java.lang.Object" nil]
  )

(defn ^:private class->type [^Class class]
  "(-> Class Type)"
  (do ;; (prn 'class->type/_0 class (.getSimpleName class) (.getName class))
      (if-let [[_ _ arr-brackets arr-base simple-base] (re-find class-name-re (.getName class))]
        (let [base (or arr-base simple-base)]
          ;; (prn 'class->type/_1 class base arr-brackets)
          (let [output-type (if (.equals "void" base)
                              &type/Unit
                              (reduce (fn [inner _] (&type/Data$ array-data-tag (&/|list inner)))
                                      (&type/Data$ base &/Nil$)
                                      (range (count (or arr-brackets ""))))
                              )]
            ;; (prn 'class->type/_2 class (&type/show-type output-type))
            output-type)
          ))))

(defn ^:private method->type [^Method method]
  "(-> Method Type)"
  (class->type (.getReturnType method)))

;; [Resources]
(do-template [<name> <old-sep> <new-sep>]
  (let [regex (-> <old-sep> Pattern/quote re-pattern)]
    (defn <name> [old]
      (string/replace old regex <new-sep>)))

  ^String ->class        class-name-separator class-separator
  ^String ->class-name   module-separator     class-name-separator
  ^String ->module-class module-separator     class-separator
  )

(def ->package ->module-class)

(defn ->type-signature [class]
  ;; (assert (string? class))
  (case class
    "void"    "V"
    "boolean" "Z"
    "byte"    "B"
    "short"   "S"
    "int"     "I"
    "long"    "J"
    "float"   "F"
    "double"  "D"
    "char"    "C"
    ;; else
    (let [class* (->class class)]
      (if (.startsWith class* "[")
        class*
        (str "L" class* ";")))
    ))

(defn unfold-array [type]
  "(-> Type (, Int Type))"
  (|case type
    (&/$DataT "#Array" (&/$Cons param (&/$Nil)))
    (|let [[count inner] (unfold-array param)]
      (&/T (inc count) inner))

    _
    (&/T 0 type)))

(defn ->java-sig [^objects type]
  "(-> Type Text)"
  (|case type
    (&/$DataT ?name params)
    (cond (= array-data-tag ?name) (|let [[level base] (unfold-array type)
                                          base-sig (|case base
                                                     (&/$DataT base-class _)
                                                     (->class base-class)

                                                     _
                                                     (->java-sig base))]
                                     (str (->> (&/|repeat level "[") (&/fold str ""))
                                          "L" base-sig ";"))
          (= null-data-tag ?name)  (->type-signature "java.lang.Object")
          :else                    (->type-signature ?name))

    (&/$LambdaT _ _)
    (->type-signature function-class)

    (&/$TupleT (&/$Nil))
    "V"

    (&/$NamedT ?name ?type)
    (->java-sig ?type)

    _
    (assert false (str '->java-sig " " (&type/show-type type)))
    ))

(do-template [<name> <static?>]
  (defn <name> [class-loader target field]
    (if-let [type* (first (for [^Field =field (.getDeclaredFields (Class/forName (&type/as-obj target) true class-loader))
                                :when (and (.equals ^Object field (.getName =field))
                                           (.equals ^Object <static?> (Modifier/isStatic (.getModifiers =field))))]
                            (.getType =field)))]
      (return (class->type type*))
      (fail (str "[Host Error] Field does not exist: " target "." field))))

  lookup-static-field true
  lookup-field        false
  )

(do-template [<name> <static?>]
  (defn <name> [class-loader target method-name args]
    ;; (prn '<name> target method-name)
    (if-let [method (first (for [^Method =method (.getDeclaredMethods (Class/forName (&type/as-obj target) true class-loader))
                                 :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))]
      (return (&/T (method->type method) (->> method .getExceptionTypes &/->list (&/|map #(.getName %)))))
      (fail (str "[Host Error] Method does not exist: " target "." method-name))))

  lookup-static-method  true
  lookup-virtual-method false
  )

(defn lookup-constructor [class-loader target args]
  ;; (prn 'lookup-constructor class-loader target (&type/as-obj target))
  (if-let [ctor (first (for [^Constructor =method (.getDeclaredConstructors (Class/forName (&type/as-obj target) true class-loader))
                             :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))]
    (return (&/T &type/Unit (->> ctor .getExceptionTypes &/->list (&/|map #(.getName %)))))
    (fail (str "[Host Error] Constructor does not exist: " target))))

(defn abstract-methods [class-loader class]
  (return (&/->list (for [^Method =method (.getDeclaredMethods (Class/forName (&type/as-obj class) true class-loader))
                          :when (.equals true (Modifier/isAbstract (.getModifiers =method)))]
                      (&/T (.getName =method) (&/|map #(.getName ^Class %) (&/->list (seq (.getParameterTypes =method)))))))))

(defn location [scope]
  (->> scope (&/|map &/normalize-name) (&/|interpose "$") (&/fold str "")))

(defn modifiers->int [mods]
  (+ (case (:visibility mods)
       "default" 0
       "public" Opcodes/ACC_PUBLIC
       "private" Opcodes/ACC_PRIVATE
       "protected" Opcodes/ACC_PROTECTED)
     (if (:static? mods) Opcodes/ACC_STATIC 0)
     (if (:final? mods) Opcodes/ACC_FINAL 0)
     (if (:abstract? mods) Opcodes/ACC_ABSTRACT 0)
     (case (:concurrency mods)
       "synchronized" Opcodes/ACC_SYNCHRONIZED
       "volatile" Opcodes/ACC_VOLATILE
       ;; else
       0)))

(let [object-real-class (->class "java.lang.Object")]
  (defn ^:private dummy-return [writer name output]
    (case output
      "void" (if (= "<init>" name)
               (doto writer
                 (.visitVarInsn Opcodes/ALOAD 0)
                 (.visitMethodInsn Opcodes/INVOKESPECIAL object-real-class "<init>" "()V")
                 (.visitInsn Opcodes/RETURN))
               (.visitInsn writer Opcodes/RETURN))
      "boolean" (doto writer
                  (.visitLdcInsn false)
                  (.visitInsn Opcodes/IRETURN))
      "byte" (doto writer
               (.visitLdcInsn (byte 0))
               (.visitInsn Opcodes/IRETURN))
      "short" (doto writer
                (.visitLdcInsn (short 0))
                (.visitInsn Opcodes/IRETURN))
      "int" (doto writer
              (.visitLdcInsn (int 0))
              (.visitInsn Opcodes/IRETURN))
      "long" (doto writer
               (.visitLdcInsn (long 0))
               (.visitInsn Opcodes/LRETURN))
      "float" (doto writer
                (.visitLdcInsn (float 0.0))
                (.visitInsn Opcodes/FRETURN))
      "double" (doto writer
                 (.visitLdcInsn (double 0.0))
                 (.visitInsn Opcodes/DRETURN))
      "char" (doto writer
               (.visitLdcInsn (char 0))
               (.visitInsn Opcodes/IRETURN))
      ;; else
      (doto writer
        (.visitInsn Opcodes/ACONST_NULL)
        (.visitInsn Opcodes/ARETURN)))))

(defn use-dummy-class [name super-class interfaces fields methods]
  (|do [module &/get-module-name
        :let [full-name (str module "/" name)
              =class (doto (new ClassWriter ClassWriter/COMPUTE_MAXS)
                       (.visit Opcodes/V1_5 (+ Opcodes/ACC_PUBLIC Opcodes/ACC_SUPER)
                               full-name nil (->class super-class) (->> interfaces (&/|map ->class) &/->seq (into-array String))))
              _ (&/|map (fn [field]
                          (doto (.visitField =class (modifiers->int (:modifiers field)) (:name field)
                                             (->type-signature (:type field)) nil nil)
                            (.visitEnd)))
                        fields)
              _ (&/|map (fn [method]
                          (|let [signature (str "(" (&/fold str "" (&/|map ->type-signature (:inputs method))) ")"
                                                (->type-signature (:output method)))]
                            (doto (.visitMethod =class (modifiers->int (:modifiers method))
                                                (:name method)
                                                signature
                                                nil
                                                (->> (:exceptions method) (&/|map ->class) &/->seq (into-array java.lang.String)))
                              .visitCode
                              (dummy-return (:name method) (:output method))
                              (.visitMaxs 0 0)
                              (.visitEnd))))
                        methods)
              bytecode (.toByteArray (doto =class .visitEnd))]
        loader &/loader
        !classes &/classes
        :let [real-name (str (->class-name module) "." name)
              _ (swap! !classes assoc real-name bytecode)
              _ (.loadClass loader real-name)]]
    (return nil)))