aboutsummaryrefslogtreecommitdiff
path: root/lux-jvm/source/luxc/lang/host/jvm/def.lux
blob: fd79d21195a58f1b29a69441295a49ff30754e5c (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
(.using
 [library
  [lux {"-" Type}
   ["[0]" ffi {"+" import: do_to}]
   [control
    ["[0]" function]]
   [data
    ["[0]" product]
    ["[0]" text
     ["%" format {"+" format}]]
    [collection
     ["[0]" array {"+" Array}]
     ["[0]" list ("[1]@[0]" functor)]]]
   [math
    [number
     ["i" int]]]
   [target
    [jvm
     [encoding
      ["[0]" name]]
     ["[0]" type {"+" Type Constraint}
      [category {"+" Class Value Method}]
      ["[0]" signature]
      ["[0]" descriptor]]]]]]
 ["[0]" //])

(def: signature (|>> type.signature signature.signature))
(def: descriptor (|>> type.descriptor descriptor.descriptor))
(def: class_name (|>> type.descriptor descriptor.class_name name.read))

(import: java/lang/Object
  "[1]::[0]")

(import: java/lang/String
  "[1]::[0]")

(import: org/objectweb/asm/Opcodes
  "[1]::[0]"
  ("static" ACC_PUBLIC int)
  ("static" ACC_PROTECTED int)
  ("static" ACC_PRIVATE int)

  ("static" ACC_TRANSIENT int)
  ("static" ACC_VOLATILE int)

  ("static" ACC_ABSTRACT int)
  ("static" ACC_FINAL int)
  ("static" ACC_STATIC int)
  ("static" ACC_SYNCHRONIZED int)
  ("static" ACC_STRICT int)

  ("static" ACC_SUPER int)
  ("static" ACC_INTERFACE int)

  ("static" V1_1 int)
  ("static" V1_2 int)
  ("static" V1_3 int)
  ("static" V1_4 int)
  ("static" V1_5 int)
  ("static" V1_6 int)
  ("static" V1_7 int)
  ("static" V1_8 int))

(import: org/objectweb/asm/FieldVisitor
  "[1]::[0]"
  (visitEnd [] void))

(import: org/objectweb/asm/MethodVisitor
  "[1]::[0]"
  (visitCode [] void)
  (visitMaxs [int int] void)
  (visitEnd [] void))

(import: org/objectweb/asm/ClassWriter
  "[1]::[0]"
  ("static" COMPUTE_MAXS int)
  ("static" COMPUTE_FRAMES int)
  (new [int])
  (visit [int int java/lang/String java/lang/String java/lang/String [java/lang/String]] void)
  (visitEnd [] void)
  (visitField [int java/lang/String java/lang/String java/lang/String java/lang/Object] org/objectweb/asm/FieldVisitor)
  (visitMethod [int java/lang/String java/lang/String java/lang/String [java/lang/String]] org/objectweb/asm/MethodVisitor)
  (toByteArray [] [byte]))

(def: (string_array values)
  (-> (List Text) (Array Text))
  (let [output (ffi.array java/lang/String (list.size values))]
    (exec (list@each (function (_ [idx value])
                       (ffi.write! idx value output))
                     (list.enumeration values))
      output)))

(def: (version_flag version)
  (-> //.Version Int)
  (case version
    {//.#V1_1} (org/objectweb/asm/Opcodes::V1_1)
    {//.#V1_2} (org/objectweb/asm/Opcodes::V1_2)
    {//.#V1_3} (org/objectweb/asm/Opcodes::V1_3)
    {//.#V1_4} (org/objectweb/asm/Opcodes::V1_4)
    {//.#V1_5} (org/objectweb/asm/Opcodes::V1_5)
    {//.#V1_6} (org/objectweb/asm/Opcodes::V1_6)
    {//.#V1_7} (org/objectweb/asm/Opcodes::V1_7)
    {//.#V1_8} (org/objectweb/asm/Opcodes::V1_8)))

(def: (visibility_flag visibility)
  (-> //.Visibility Int)
  (case visibility
    {//.#Public}    (org/objectweb/asm/Opcodes::ACC_PUBLIC)
    {//.#Protected} (org/objectweb/asm/Opcodes::ACC_PROTECTED)
    {//.#Private}   (org/objectweb/asm/Opcodes::ACC_PRIVATE)
    {//.#Default}   +0))

(def: (class_flags config)
  (-> //.Class_Config Int)
  ($_ i.+
      (if (the //.#finalC config) (org/objectweb/asm/Opcodes::ACC_FINAL) +0)))

(def: (method_flags config)
  (-> //.Method_Config Int)
  ($_ i.+
      (if (the //.#staticM config) (org/objectweb/asm/Opcodes::ACC_STATIC) +0)
      (if (the //.#finalM config) (org/objectweb/asm/Opcodes::ACC_FINAL) +0)
      (if (the //.#synchronizedM config) (org/objectweb/asm/Opcodes::ACC_SYNCHRONIZED) +0)
      (if (the //.#strictM config) (org/objectweb/asm/Opcodes::ACC_STRICT) +0)))

(def: (field_flags config)
  (-> //.Field_Config Int)
  ($_ i.+
      (if (the //.#staticF config) (org/objectweb/asm/Opcodes::ACC_STATIC) +0)
      (if (the //.#finalF config) (org/objectweb/asm/Opcodes::ACC_FINAL) +0)
      (if (the //.#transientF config) (org/objectweb/asm/Opcodes::ACC_TRANSIENT) +0)
      (if (the //.#volatileF config) (org/objectweb/asm/Opcodes::ACC_VOLATILE) +0)))

(def: param_signature
  (-> (Type Class) Text)
  (|>> ..signature (format ":")))

(def: (formal_param [name super interfaces])
  (-> Constraint Text)
  (format name
          (param_signature super)
          (|> interfaces
              (list@each param_signature)
              (text.interposed ""))))

(def: (constraints_signature constraints super interfaces)
  (-> (List Constraint) (Type Class) (List (Type Class))
      Text)
  (let [formal_params (if (list.empty? constraints)
                        ""
                        (format "<"
                                (|> constraints
                                    (list@each formal_param)
                                    (text.interposed ""))
                                ">"))]
    (format formal_params
            (..signature super)
            (|> interfaces
                (list@each ..signature)
                (text.interposed "")))))

(def: class_computes
  Int
  ($_ i.+
      (org/objectweb/asm/ClassWriter::COMPUTE_MAXS)
      ... (org/objectweb/asm/ClassWriter::COMPUTE_FRAMES)
      ))

(def: binary_name (|>> name.internal name.read))

(template [<name> <flag>]
  [(def: .public (<name> version visibility config name constraints super interfaces
                         definitions)
     (-> //.Version //.Visibility //.Class_Config Text (List Constraint) (Type Class) (List (Type Class)) //.Def
         (ffi.type [byte]))
     (let [writer (|> (do_to (org/objectweb/asm/ClassWriter::new class_computes)
                        (org/objectweb/asm/ClassWriter::visit (version_flag version)
                                                              ($_ i.+
                                                                  (org/objectweb/asm/Opcodes::ACC_SUPER)
                                                                  <flag>
                                                                  (visibility_flag visibility)
                                                                  (class_flags config))
                                                              (..binary_name name)
                                                              (constraints_signature constraints super interfaces)
                                                              (..class_name super)
                                                              (|> interfaces
                                                                  (list@each ..class_name)
                                                                  string_array)))
                      definitions)
           _ (org/objectweb/asm/ClassWriter::visitEnd writer)]
       (org/objectweb/asm/ClassWriter::toByteArray writer)))]

  [class    +0]
  [abstract (org/objectweb/asm/Opcodes::ACC_ABSTRACT)]
  )

(def: $Object
  (Type Class)
  (type.class "java.lang.Object" (list)))

(def: .public (interface version visibility config name constraints interfaces
                         definitions)
  (-> //.Version //.Visibility //.Class_Config Text (List Constraint) (List (Type Class)) //.Def
      (ffi.type [byte]))
  (let [writer (|> (do_to (org/objectweb/asm/ClassWriter::new class_computes)
                     (org/objectweb/asm/ClassWriter::visit (version_flag version)
                                                           ($_ i.+
                                                               (org/objectweb/asm/Opcodes::ACC_ABSTRACT)
                                                               (org/objectweb/asm/Opcodes::ACC_INTERFACE)
                                                               (visibility_flag visibility)
                                                               (class_flags config))
                                                           (..binary_name name)
                                                           (constraints_signature constraints $Object interfaces)
                                                           (..class_name $Object)
                                                           (|> interfaces
                                                               (list@each ..class_name)
                                                               string_array)))
                   definitions)
        _ (org/objectweb/asm/ClassWriter::visitEnd writer)]
    (org/objectweb/asm/ClassWriter::toByteArray writer)))

(def: .public (method visibility config name type then)
  (-> //.Visibility //.Method_Config Text (Type Method) //.Inst
      //.Def)
  (function (_ writer)
    (let [=method (org/objectweb/asm/ClassWriter::visitMethod ($_ i.+
                                                                  (visibility_flag visibility)
                                                                  (method_flags config))
                                                              (..binary_name name)
                                                              (..descriptor type)
                                                              (..signature type)
                                                              (string_array (list))
                                                              writer)
          _ (org/objectweb/asm/MethodVisitor::visitCode =method)
          _ (then =method)
          _ (org/objectweb/asm/MethodVisitor::visitMaxs +0 +0 =method)
          _ (org/objectweb/asm/MethodVisitor::visitEnd =method)]
      writer)))

(def: .public (abstract_method visibility config name type)
  (-> //.Visibility //.Method_Config Text (Type Method)
      //.Def)
  (function (_ writer)
    (let [=method (org/objectweb/asm/ClassWriter::visitMethod ($_ i.+
                                                                  (visibility_flag visibility)
                                                                  (method_flags config)
                                                                  (org/objectweb/asm/Opcodes::ACC_ABSTRACT))
                                                              (..binary_name name)
                                                              (..descriptor type)
                                                              (..signature type)
                                                              (string_array (list))
                                                              writer)
          _ (org/objectweb/asm/MethodVisitor::visitEnd =method)]
      writer)))

(def: .public (field visibility config name type)
  (-> //.Visibility //.Field_Config Text (Type Value) //.Def)
  (function (_ writer)
    (let [=field (do_to (org/objectweb/asm/ClassWriter::visitField ($_ i.+
                                                                       (visibility_flag visibility)
                                                                       (field_flags config))
                                                                   (..binary_name name)
                                                                   (..descriptor type)
                                                                   (..signature type)
                                                                   (ffi.null)
                                                                   writer)
                   (org/objectweb/asm/FieldVisitor::visitEnd))]
      writer)))

(template [<name> <lux_type> <jvm_type> <prepare>]
  [(def: .public (<name> visibility config name value)
     (-> //.Visibility //.Field_Config Text <lux_type> //.Def)
     (function (_ writer)
       (let [=field (do_to (org/objectweb/asm/ClassWriter::visitField ($_ i.+
                                                                          (visibility_flag visibility)
                                                                          (field_flags config))
                                                                      (..binary_name name)
                                                                      (..descriptor <jvm_type>)
                                                                      (..signature <jvm_type>)
                                                                      (<prepare> value)
                                                                      writer)
                      (org/objectweb/asm/FieldVisitor::visitEnd))]
         writer)))]

  [boolean_field Bit  type.boolean                           function.identity]
  [byte_field    Int  type.byte                              ffi.long_to_byte]
  [short_field   Int  type.short                             ffi.long_to_short]
  [int_field     Int  type.int                               ffi.long_to_int]
  [long_field    Int  type.long                              function.identity]
  [float_field   Frac type.float                             ffi.double_to_float]
  [double_field  Frac type.double                            function.identity]
  [char_field    Nat  type.char                              (|>> .int ffi.long_to_int ffi.int_to_char)]
  [string_field  Text (type.class "java.lang.String" (list)) function.identity]
  )

(def: .public (fuse defs)
  (-> (List //.Def) //.Def)
  (case defs
    {.#End}
    function.identity

    {.#Item singleton {.#End}}
    singleton

    {.#Item head tail}
    (function.composite (fuse tail) head)))