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

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

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

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

(import: java/lang/ClassLoader)

(type: .public ByteCode Binary)

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

(exception: .public (cannot_load [class Text
                                  error Text])
  (exception.report
   ["Class" class]
   ["Error" error]))

(exception: .public (invalid_field [class Text
                                    field Text
                                    error Text])
  (exception.report
   ["Class" class]
   ["Field" field]
   ["Error" error]))

(exception: .public (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.except ..invalid_value class_name))
      
      {try.#Failure error}
      (exception.except ..cannot_load [class_name error]))
    
    {try.#Failure error}
    (exception.except ..invalid_field [class_name ..value_field error])))

(def: class_path_separator ".")

(def: .public bytecode_name
  (-> Text Text)
  (text.replaced ..class_path_separator .module_separator))

(def: .public (class_name [module_id artifact_id])
  (-> unit.ID 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 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) (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 in (..class_value eval_class class))]
               (in [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 each (function (_ library)
                                                     (dictionary.key? library class_name)))
                                  (try.lifted io.monad)
                                  (: (IO (Try Bit))))
              _ (if existing_class?
                  (in [])
                  (loader.store class_name class_bytecode library))]
             (loader.load class_name loader))))

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

(def: .public 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 each product.left
                  (..evaluate! library loader (format "E" (..class_name 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.else (..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.else (..class_name context)
                                                custom)]
                   _ (loader.store class_name bytecode library)
                   class (loader.load class_name loader)]
                  (# io.monad in (..class_value class_name class)))))))])))

(def: .public $Variant
  (type.array ..$Value))

(def: .public $Tuple
  (type.array ..$Value))

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

(def: .public $Function
  (type.class "library.lux.Function" ... (..class_name [0 1])
              (list)))