aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/meta/compiler/language/lux/phase/translation/jvm/host.lux
blob: 7c0882b27fc85ca233bc06a0ee16193c153927fd (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
... This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
... If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/.

(.require
 [library
  [lux (.except Definition)
   ["[0]" ffi (.only import object)]
   [abstract
    [monad (.only do)]]
   [control
    ["[0]" maybe]
    ["[0]" try (.only Try)]
    ["[0]" exception (.only Exception)]
    ["[0]" io (.only IO io)]
    [concurrency
     ["[0]" atom (.only Atom atom)]]]
   [data
    ["[0]" product]
    [binary (.only Binary)
     ["[0]" \\format]]
    ["[0]" text (.use "[1]#[0]" hash)
     ["%" \\format]]
    [collection
     ["[0]" array]
     ["[0]" dictionary (.only Dictionary)]
     ["[0]" sequence]]]
   [meta
    [compiler
     [target
      [jvm
       ["_" bytecode (.only Bytecode)]
       ["[0]" loader (.only Library)]
       ["[0]" modifier (.only Modifier) (.use "[1]#[0]" monoid)]
       ["[0]" field (.only Field)]
       ["[0]" method (.only Method)]
       ["[0]" version]
       ["[0]" class (.only Class)]
       ["[0]" encoding
        ["[1]/[0]" name]]
       ["[0]" type (.only)
        ["[0]" descriptor]]]]]
    [compiler
     [meta
      [archive
       ["[0]" unit]]]]]]]
 ["[0]" //
  ["[1][0]" runtime (.only Definition)]
  ["[1][0]" type]
  ["[1][0]" value]])

(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
  "[1]::[0]")

(def value::modifier
  (all modifier#composite
       field.public
       field.final
       field.static
       ))

(def init::type
  (type.method [(list) (list) type.void (list)]))

(def init::modifier
  (all modifier#composite
       method.public
       method.static
       method.strict
       ))

(exception.def .public (cannot_load [class error])
  (Exception [Text Text])
  (exception.report
   (list ["Class" class]
         ["Error" error])))

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

(exception.def .public (invalid_value class)
  (Exception Text)
  (exception.report
   (list ["Class" class])))

(def (class_value class_name class)
  (-> Text (java/lang/Class java/lang/Object)
      (Try Any))
  (when (java/lang/Class::getField (ffi.as_string //value.field) class)
    {try.#Success field}
    (when (java/lang/reflect/Field::get {.#None} field)
      {try.#Success ?value}
      (when ?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 (evaluate! library loader eval_class [@it valueG])
  (-> Library java/lang/ClassLoader Text [(Maybe unit.ID) (Bytecode Any)]
      (Try [Any Definition]))
  (let [bytecode_name (text.replaced class_path_separator .module_separator eval_class)
        :value: (when @it
                  {.#Some @it}
                  (type.class (//runtime.class_name @it) (list))

                  {.#None}
                  //type.value)
        bytecode (class.class version.v6_0
                   class.public
                   (encoding/name.internal bytecode_name)
                   {.#None}
                   (encoding/name.internal "java.lang.Object") (list)
                   (list (field.field ..value::modifier //value.field false :value: (sequence.sequence)))
                   (list (method.method ..init::modifier "<clinit>"
                           false ..init::type
                           (list)
                           {.#Some
                            (all _.composite
                                 valueG
                                 (_.putstatic (type.class bytecode_name (list)) //value.field :value:)
                                 _.return)}))
                   (list))]
    (io.run! (do [! (try.with io.monad)]
               [bytecode (of ! each (\\format.result class.format)
                             (io.io bytecode))
                _ (loader.store eval_class bytecode library)
                class (loader.load eval_class loader)
                value (of 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)
                                  (of io.monad each (function (_ library)
                                                      (dictionary.key? library class_name)))
                                  (try.lifted io.monad)
                                  (is (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 @it,valueG)
  (-> Library java/lang/ClassLoader unit.ID (Maybe Text) [(Maybe unit.ID) (Bytecode Any)]
      (Try [Text Any Definition]))
  (let [class_name (maybe.else (//runtime.class_name context)
                               custom)]
    (do try.monad
      [[value definition] (evaluate! library loader class_name @it,valueG)]
      (in [class_name value definition]))))

(def .public host
  (IO [java/lang/ClassLoader //runtime.Host])
  (io (let [library (loader.new_library [])
            loader (loader.memory library)
            id (atom.atom 0)]
        [loader
         (is //runtime.Host
             (implementation
              (def (evaluate @it,valueG)
                (let [[id _] (io.run! (atom.update! ++ id))]
                  (of try.monad each product.left
                      (..evaluate! library loader (%.format "E" (%.nat id)) @it,valueG))))
              
              (def execute
                (..execute! library loader))
              
              (def define
                (..define! library loader))

              (def (ingest context bytecode)
                [(//runtime.class_name context) bytecode])

              (def (re_learn context custom [_ bytecode])
                (io.run! (loader.store (maybe.else (//runtime.class_name context) custom) bytecode library)))
              
              (def (re_load context custom [declaration_name bytecode])
                (io.run!
                 (do (try.with io.monad)
                   [.let [class_name (maybe.else (//runtime.class_name context)
                                                 custom)]
                    _ (loader.store class_name bytecode library)
                    class (loader.load class_name loader)]
                   (of io.monad in (..class_value class_name class)))))
              ))])))