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

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

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

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

(import: #long 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 meta.version)
          ..class-path-separator (%.nat module-id)
          ..class-path-separator (%.nat artifact-id)))

(def: (evaluate! library loader eval-class valueI)
  (-> Library java/lang/ClassLoader Text Inst (Try [Any Definition]))
  (let [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) 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 temp-label [class-name class-bytecode])
  (-> Library java/lang/ClassLoader Text Definition (Try Any))
  (io.run (do (try.with io.monad)
            [existing-class? (|> (atom.read library)
                                 (:: io.monad map (dictionary.contains? class-name))
                                 (try.lift 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 valueI)
  (-> Library java/lang/ClassLoader generation.Context Inst (Try [Text Any Definition]))
  (let [class-name (..class-name context)]
    (do try.monad
      [[value definition] (evaluate! library loader class-name valueI)]
      (wrap [class-name value definition]))))

(def: #export host
  (IO Host)
  (io (let [library (loader.new-library [])
            loader (loader.memory library)]
        (: Host
           (structure
            (def: (evaluate! temp-label valueI)
              (:: try.monad map product.left
                  (..evaluate! library loader (text.replace-all " " "$" temp-label) valueI)))
            
            (def: execute!
              (..execute! library loader))
            
            (def: define!
              (..define! library loader))

            (def: (ingest context bytecode)
              [(..class-name context) bytecode])

            (def: (re-learn context [_ bytecode])
              (io.run
               (loader.store (..class-name context) bytecode library)))
            
            (def: (re-load context [_ bytecode])
              (io.run
               (do (try.with io.monad)
                 [#let [class-name (..class-name context)]
                  _ (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)))