aboutsummaryrefslogtreecommitdiff
path: root/lux-jvm/source/luxc/lang/translation/jvm.lux
blob: 1b916d925f6841882c58d349d6dd868952cb5d8f (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
(.module:
  [library
   [lux (#- Module Definition)
    ["." ffi (#+ import: do_to object)]
    [abstract
     [monad (#+ do)]]
    [control
     pipe
     ["." try (#+ Try)]
     ["." exception (#+ exception:)]
     ["." io (#+ IO io)]
     [concurrency
      ["." atom (#+ Atom atom)]]]
    [data
     [binary (#+ Binary)]
     ["." product]
     ["." maybe]
     ["." text ("#@." hash)
      ["%" format (#+ format)]]
     [collection
      ["." array]
      ["." dictionary (#+ Dictionary)]]]
    [target
     [jvm
      ["." loader (#+ Library)]
      ["." type
       ["." descriptor]]]]
    [tool
     [compiler
      [language
       [lux
        ["." version]
        ["." generation]]]
      [meta
       [io (#+ lux_context)]
       [archive
        [descriptor (#+ Module)]
        ["." artifact]]]]]]]
  [///
   [host
    ["." jvm (#+ Inst Definition Host State)
     ["." def]
     ["." inst]]]]
  )

(import: java/lang/reflect/Field
  ["#::."
   (get [#? java/lang/Object] #try #? java/lang/Object)])

(import: (java/lang/Class a)
  ["#::."
   (getField [java/lang/String] #try java/lang/reflect/Field)])

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

(import: java/lang/ClassLoader)

(type: #export ByteCode Binary)

(def: #export value_field Text "_value")
(def: #export $Value (type.class "java.lang.Object" (list)))

(exception: #export (cannot_load {class Text} {error Text})
  (exception.report
   ["Class" class]
   ["Error" error]))

(exception: #export (invalid_field {class Text} {field Text} {error Text})
  (exception.report
   ["Class" class]
   ["Field" field]
   ["Error" error]))

(exception: #export (invalid_value {class Text})
  (exception.report
   ["Class" class]))

(def: (class_value class_name class)
  (-> Text (java/lang/Class java/lang/Object) (Try Any))
  (case (java/lang/Class::getField ..value_field class)
    (#try.Success field)
    (case (java/lang/reflect/Field::get #.None field)
      (#try.Success ?value)
      (case ?value
        (#.Some value)
        (#try.Success value)
        
        #.None
        (exception.throw ..invalid_value class_name))
      
      (#try.Failure error)
      (exception.throw ..cannot_load [class_name error]))
    
    (#try.Failure error)
    (exception.throw ..invalid_field [class_name ..value_field error])))

(def: class_path_separator ".")

(def: #export bytecode_name
  (-> Text Text)
  (text.replace_all ..class_path_separator .module_separator))

(def: #export (class_name [module_id artifact_id])
  (-> generation.Context Text)
  (format lux_context
          ..class_path_separator (%.nat version.version)
          ..class_path_separator (%.nat module_id)
          ..class_path_separator (%.nat artifact_id)))

(def: (evaluate! library loader context valueI)
  (-> Library java/lang/ClassLoader generation.Context Inst (Try [Any Definition]))
  (let [eval_class (..class_name context)
        bytecode_name (..bytecode_name eval_class)
        bytecode (def.class #jvm.V1_6
                            #jvm.Public jvm.noneC
                            bytecode_name
                            (list) $Value
                            (list)
                            (|>> (def.field #jvm.Public ($_ jvm.++F jvm.finalF jvm.staticF)
                                            ..value_field ..$Value)
                                 (def.method #jvm.Public ($_ jvm.++M jvm.staticM jvm.strictM)
                                             "<clinit>"
                                             (type.method [(list) (list) type.void (list)])
                                             (|>> valueI
                                                  (inst.PUTSTATIC (type.class bytecode_name (list)) ..value_field ..$Value)
                                                  inst.RETURN))))]
    (io.run (do (try.with io.monad)
              [_ (loader.store eval_class bytecode library)
               class (loader.load eval_class loader)
               value (\ io.monad wrap (..class_value eval_class class))]
              (wrap [value
                     [eval_class bytecode]])))))

(def: (execute! library loader [class_name class_bytecode])
  (-> Library java/lang/ClassLoader Definition (Try Any))
  (io.run (do (try.with io.monad)
            [existing_class? (|> (atom.read library)
                                 (\ io.monad map (function (_ library)
                                                   (dictionary.key? library class_name)))
                                 (try.lifted io.monad)
                                 (: (IO (Try Bit))))
             _ (if existing_class?
                 (wrap [])
                 (loader.store class_name class_bytecode library))]
            (loader.load class_name loader))))

(def: (define! library loader context custom valueI)
  (-> Library java/lang/ClassLoader generation.Context (Maybe Text) Inst (Try [Text Any Definition]))
  (do try.monad
    [[value definition] (evaluate! library loader context valueI)]
    (wrap [(maybe.default (..class_name context)
                          custom)
           value definition])))

(def: #export host
  (IO [java/lang/ClassLoader Host])
  (io (let [library (loader.new_library [])
            loader (loader.memory library)]
        [loader
         (: Host
            (implementation
             (def: (evaluate! context valueI)
               (\ try.monad map product.left
                  (..evaluate! library loader context valueI)))
             
             (def: execute!
               (..execute! library loader))
             
             (def: define!
               (..define! library loader))

             (def: (ingest context bytecode)
               [(..class_name context) bytecode])

             (def: (re_learn context custom [_ bytecode])
               (io.run
                (loader.store (maybe.default (..class_name context) custom) bytecode library)))
             
             (def: (re_load context custom [directive_name bytecode])
               (io.run
                (do (try.with io.monad)
                  [#let [class_name (maybe.default (..class_name context)
                                                   custom)]
                   _ (loader.store class_name bytecode library)
                   class (loader.load class_name loader)]
                  (\ io.monad wrap (..class_value class_name class)))))))])))

(def: #export $Variant
  (type.array ..$Value))

(def: #export $Tuple
  (type.array ..$Value))

(def: #export $Runtime
  (type.class (..class_name [0 0]) (list)))

(def: #export $Function
  (type.class (..class_name [0 1]) (list)))