aboutsummaryrefslogtreecommitdiff
path: root/lux-jvm/source/program.lux
blob: 60fb953aa91fffec0ac5b7784c7250f7e94164dd (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
(.module:
  [library
   [lux (#- Definition)
    [program (#+ program:)]
    ["@" target]
    ["." ffi (#+ import:)]
    [abstract
     [monad (#+ do)]]
    [control
     ["." io (#+ IO)]
     ["." try (#+ Try)]
     [concurrency
      ["." async (#+ Async)]]]
    [data
     ["." product]
     [text
      ["%" format (#+ format)]]
     [collection
      [array (#+ Array)]
      ["." dictionary]]]
    ["." world #_
     ["." file]
     ["#/." program]]
    [target
     ["." jvm #_
      [bytecode (#+ Bytecode)]
      ["#/." type
       ["#/." box]]]]
    [tool
     [compiler
      [reference (#+)]
      ["." phase]
      [default
       ["." platform (#+ Platform)]]
      [meta
       [archive (#+ Archive)]
       ["." packager #_
        ["#" jvm]]]
      [language
       [lux
        ["$" synthesis (#+ Synthesis)]
        ["." generation]
        [analysis
         [macro (#+ Expander)]]
        [phase
         [extension (#+ Phase Bundle Operation Handler Extender)
          ["." analysis #_
           ["#" jvm]]
          ## ["." generation #_
          ##  ["#" jvm]]
          ## ["." directive #_
          ##  ["#" jvm]]
          ]
         [generation
          ["." jvm #_
           ["#/." runtime]
           ## ["#/." host]
           ]]]]]]]]]
  [program
   ["/" compositor
    ["/." cli]
    ["/." static]]]
  [luxc
   [lang
    [host
     ["_" jvm]]
    ["." directive #_
     ["#" jvm]]
    [translation
     ["." jvm
      ["." runtime]
      ["." expression]
      ["." function]
      ["#/." program]
      ["translation" extension]]]]])

(import: java/lang/reflect/Method
  ["#::."
   (invoke [java/lang/Object [java/lang/Object]] #try java/lang/Object)])

(import: java/lang/ClassLoader)

(import: (java/lang/Class c)
  ["#::."
   (getMethod [java/lang/String [(java/lang/Class java/lang/Object)]] #try java/lang/reflect/Method)])

(import: java/lang/Object
  ["#::."
   (getClass [] (java/lang/Class java/lang/Object))])

(def: _object_class
  (java/lang/Class java/lang/Object)
  (ffi.class_for java/lang/Object))

(def: _apply1_args
  (Array (java/lang/Class java/lang/Object))
  (|> (ffi.array (java/lang/Class java/lang/Object) 1)
      (ffi.array_write 0 _object_class)))

(def: _apply2_args
  (Array (java/lang/Class java/lang/Object))
  (|> (ffi.array (java/lang/Class java/lang/Object) 2)
      (ffi.array_write 0 _object_class)
      (ffi.array_write 1 _object_class)))

(def: _apply5_args
  (Array (java/lang/Class java/lang/Object))
  (|> (ffi.array (java/lang/Class java/lang/Object) 5)
      (ffi.array_write 0 _object_class)
      (ffi.array_write 1 _object_class)
      (ffi.array_write 2 _object_class)
      (ffi.array_write 3 _object_class)
      (ffi.array_write 4 _object_class)))

(def: #export (expander macro inputs lux)
  Expander
  (do try.monad
    [apply_method (|> macro
                      (:as java/lang/Object)
                      (java/lang/Object::getClass)
                      (java/lang/Class::getMethod "apply" _apply2_args))]
    (:as (Try (Try [Lux (List Code)]))
         (java/lang/reflect/Method::invoke
          (:as java/lang/Object macro)
          (|> (ffi.array java/lang/Object 2)
              (ffi.array_write 0 (:as java/lang/Object inputs))
              (ffi.array_write 1 (:as java/lang/Object lux)))
          apply_method))))

(def: how_to_wrap_a_phase
  Synthesis
  (let [java/lang/String (jvm/type.class "java.lang.String" (list))
        <java/lang/Class> (jvm/type.array (jvm/type.class "java.lang.Class" (list)))
        java/lang/Object (jvm/type.class "java.lang.Object" (list))
        <java/lang/Object> (jvm/type.array java/lang/Object)

        jvm_type (: (All [c] (-> (jvm/type.Type c) Synthesis))
                    (|>> jvm/type.format
                         $.text))
        class_type (: (-> Text Synthesis)
                      (function (_ name)
                        (|> (jvm/type.class name (list))
                            jvm_type)))
        unwrap_long (: (-> Synthesis Synthesis)
                       (|>> (list ($.text jvm/type/box.long)
                                  ($.text "long"))
                            (#$.Extension "jvm object cast")))
        long_to_int (: (-> Synthesis Synthesis)
                       (|>> (list)
                            (#$.Extension "jvm conversion long-to-int")))
        literal_nat (: (-> Nat Synthesis)
                       (|>> .i64 $.i64 unwrap_long long_to_int))
        array_write (: (-> Text Nat Synthesis Synthesis Synthesis)
                       (function (_ element_class index value array)
                         ((#$.Extension "jvm array write object"
                                        (list (jvm_type (jvm/type.array (jvm/type.class element_class (list))))
                                              (literal_nat index)
                                              value
                                              array)))))
        object_array (: (-> Text Nat Synthesis)
                        (function (_ class_name size)
                          (#$.Extension "jvm array new object"
                                        (list (class_type class_name)
                                              (literal_nat size)))))
        class_of (: (-> Synthesis Synthesis)
                    (function (_ object)
                      (#$.Extension "jvm member invoke virtual"
                                    (list& (class_type "java.lang.Object")
                                           ($.text "getClass")
                                           (class_type "java.lang.Class")
                                           object
                                           (list)))))
        input (: (All [c] (-> (jvm/type.Type c) Synthesis Synthesis))
                 (function (_ value_type value)
                   ($.tuple (list (jvm_type value_type) value))))
        
        example_object (#$.Extension "jvm member invoke constructor"
                                     (list& (class_type "java.lang.Object")
                                            (list)))
        phase_arity 3
        $phase ($.variable/local 1)
        $archive ($.variable/local 2)
        $input ($.variable/local 3)
        $state ($.variable/local 4)
        apply_method (#$.Extension "jvm member invoke virtual"
                                   (list& (class_type "java.lang.Class")
                                          ($.text "getMethod")
                                          (class_type "java.lang.reflect.Method")
                                          (class_of $phase)
                                          (list (input java/lang/String
                                                       ($.text runtime.apply_method))
                                                (input <java/lang/Class>
                                                       (|> (object_array "java.lang.Class" phase_arity)
                                                           (array_write "java.lang.Class" 0 (class_of example_object))
                                                           (array_write "java.lang.Class" 1 (class_of example_object))
                                                           (array_write "java.lang.Class" 2 (class_of example_object)))))))]
    (#$.Extension "jvm member invoke virtual"
                  (list& (class_type "java.lang.reflect.Method")
                         ($.text "invoke")
                         (class_type "java.lang.Object")
                         apply_method
                         (list (input java/lang/Object
                                      $phase)
                               (input <java/lang/Object>
                                      (|> (object_array "java.lang.Object" phase_arity)
                                          (array_write "java.lang.Object" 0 $archive)
                                          (array_write "java.lang.Object" 1 $input)
                                          (array_write "java.lang.Object" 2 $state))))))))

(def: (phase_wrapper archive)
  (-> Archive (generation.Operation _.Anchor _.Inst _.Definition platform.Phase_Wrapper))
  (do phase.monad
    [instanceG (function.function' (#.Some [0 (.nat -1)]) expression.translate archive [(list) 4 ..how_to_wrap_a_phase])
     phase_wrapper (generation.evaluate! [0 (.nat -2)] instanceG)]
    (wrap (function (_ phase)
            (<| try.assumed
                (: (Try java/lang/Object))
                (do try.monad
                  [apply_method (|> phase_wrapper
                                    (:as java/lang/Object)
                                    (java/lang/Object::getClass)
                                    (java/lang/Class::getMethod runtime.apply_method _apply1_args))]
                  (java/lang/reflect/Method::invoke
                   (:as java/lang/Object phase_wrapper)
                   (|> (ffi.array java/lang/Object 1)
                       (ffi.array_write 0 (:as java/lang/Object phase)))
                   apply_method)))))))

(def: #export platform
  ## (IO (Platform Anchor (Bytecode Any) Definition))
  (IO [java/lang/ClassLoader
       (Platform _.Anchor _.Inst _.Definition)])
  (do io.monad
    [## host jvm/host.host
     [loader host] jvm.host]
    (wrap [loader
           {#platform.&file_system (file.async file.default)
            #platform.host host
            ## #platform.phase jvm.generate
            #platform.phase expression.translate
            ## #platform.runtime runtime.generate
            #platform.runtime runtime.translate
            #platform.phase_wrapper ..phase_wrapper
            #platform.write product.right}])))

(def: (extender phase_wrapper)
  (-> platform.Phase_Wrapper Extender)
  ## TODO: Stop relying on coercions ASAP.
  (<| (:as Extender)
      (function (@self handler))
      (:as Handler)
      (function (@self name phase))
      (:as Phase)
      (function (@self archive parameters))
      (:as Operation)
      (function (@self state))
      (:as Try)
      try.assumed
      (:as Try)
      (do try.monad
        [method (|> handler
                    (:as java/lang/Object)
                    (java/lang/Object::getClass)
                    (java/lang/Class::getMethod "apply" _apply5_args))]
        (java/lang/reflect/Method::invoke
         (:as java/lang/Object handler)
         (|> (ffi.array java/lang/Object 5)
             (ffi.array_write 0 (:as java/lang/Object name))
             (ffi.array_write 1 (:as java/lang/Object (phase_wrapper phase)))
             (ffi.array_write 2 (:as java/lang/Object archive))
             (ffi.array_write 3 (:as java/lang/Object parameters))
             (ffi.array_write 4 (:as java/lang/Object state)))
         method))))

(def: (declare_success! _)
  (-> Any (Async Any))
  (async.future (\ world/program.default exit +0)))

(program: [{service /cli.service}]
  (let [static {#/static.host @.jvm
                #/static.host_module_extension ".jvm"
                #/static.target (/cli.target service)
                #/static.artifact_extension ".class"}]
    (exec (do async.monad
            [[loader platform] (async.future ..platform)
             _ (/.compiler {#/static.host @.jvm
                            #/static.host_module_extension ".jvm"
                            #/static.target (/cli.target service)
                            #/static.artifact_extension ".class"}
                           ..expander
                           (analysis.bundle loader)
                           (io.io platform)
                           ## generation.bundle
                           translation.bundle
                           (|>> ..extender (directive.bundle loader))
                           (jvm/program.program jvm/runtime.class_name)
                           [_.Anchor _.Inst _.Definition]
                           ..extender
                           service
                           [(packager.package static)
                            (format (/cli.target service) (\ file.default separator) "program.jar")])]
            (..declare_success! []))
      (io.io []))))