blob: 47fbbefcf254f7b3f1e4519959f7a390c3b0ff96 (
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
|
(.module:
[lux (#- Definition)
[control
[monad (#+ do)]
["ex" exception (#+ exception:)]
pipe]
[concurrency
["." atom (#+ Atom atom)]]
[data
["." error (#+ Error)]
["." text ("text/." Hash<Text>)
format]
[collection
["." array]
[list ("list/." Functor<List>)]
["." dictionary (#+ Dictionary)]]]
["." host (#+ import: do-to object)]
["." io (#+ IO io)]
[world
[binary (#+ Binary)]]
[platform
[compiler
["." name]
[phase
["." translation]]]]]
[///
[host
["." jvm (#+ Inst Definition Host State)
["." type]
["." def]
["." inst]]]]
)
(import: org/objectweb/asm/Label)
(import: java/lang/reflect/AccessibleObject
(setAccessible [boolean] void))
(import: java/lang/reflect/Field
(get [#? Object] #try #? Object))
(import: java/lang/reflect/Method
(invoke [Object (Array Object)] #try Object))
(import: (java/lang/Class a)
(getField [String] #try Field)
(getDeclaredMethod [String (Array (Class Object))] #try Method))
(import: java/lang/Object
(getClass [] (Class Object)))
(import: java/lang/Integer
(#static TYPE (Class Integer)))
(import: java/lang/ClassLoader
(loadClass [String] #try (Class Object)))
(def: ClassLoader::defineClass
Method
(case (Class::getDeclaredMethod "defineClass"
(|> (host.array (Class Object) 4)
(host.array-write 0 (:coerce (Class Object) (host.class-for String)))
(host.array-write 1 (Object::getClass (host.array byte 0)))
(host.array-write 2 (:coerce (Class Object) (Integer::TYPE)))
(host.array-write 3 (:coerce (Class Object) (Integer::TYPE))))
(host.class-for java/lang/ClassLoader))
(#error.Success method)
(do-to method
(AccessibleObject::setAccessible #1))
(#error.Error error)
(error! error)))
(type: #export ByteCode Binary)
(def: (define-class class-name bytecode loader)
(-> Text ByteCode ClassLoader (Error Object))
(Method::invoke loader
(array.from-list (list (:coerce Object class-name)
(:coerce Object bytecode)
(:coerce Object (host.long-to-int +0))
(:coerce Object (host.long-to-int (.int (host.array-length bytecode))))))
ClassLoader::defineClass))
(type: Store (Atom (Dictionary Text ByteCode)))
(exception: #export (class-already-stored {class Text})
(ex.report ["Class" class]))
(exception: #export (unknown-class {class Text} {known-classes (List Text)})
(ex.report ["Class" class]
["Known Classes" (|> known-classes
(list/map (|>> (format "\n\t")))
(text.join-with ""))]))
(exception: #export (cannot-define-class {class Text} {error Text})
(ex.report ["Class" class]
["Error" error]))
(def: (memory-class-loader store)
(-> Store ClassLoader)
(object [] ClassLoader []
[]
(ClassLoader (findClass {class-name String}) Class
(let [classes (|> store atom.read io.run)]
(case (dictionary.get class-name classes)
(#.Some bytecode)
(case (define-class class-name bytecode (:coerce ClassLoader _jvm_this))
(#error.Success class)
(:assume class)
(#error.Error error)
(error! (ex.construct cannot-define-class [class-name error])))
#.None
(error! (ex.construct unknown-class [class-name (dictionary.keys classes)])))))))
(def: (store! name bytecode store)
(-> Text ByteCode Store (Error Any))
(if (|> store atom.read io.run (dictionary.contains? name))
(ex.throw class-already-stored name)
(exec (io.run (atom.update (dictionary.put name bytecode) store))
(#error.Success []))))
(def: (load! name loader)
(-> Text ClassLoader (Error (Class Object)))
(ClassLoader::loadClass name loader))
(def: #export value-field Text "_value")
(def: #export $Object jvm.Type (type.class "java.lang.Object" (list)))
(exception: #export (cannot-load {class Text} {error Text})
(ex.report ["Class" class]
["Error" error]))
(exception: #export (invalid-field {class Text} {field Text} {error Text})
(ex.report ["Class" class]
["Field" field]
["Error" error]))
(exception: #export (invalid-value {class Text})
(ex.report ["Class" class]))
(def: (class-value class-name class)
(-> Text (Class Object) (Error Any))
(case (Class::getField ..value-field class)
(#error.Success field)
(case (Field::get #.None field)
(#error.Success ?value)
(case ?value
(#.Some value)
(#error.Success value)
#.None
(ex.throw invalid-value class-name))
(#error.Error error)
(ex.throw cannot-load [class-name error]))
(#error.Error error)
(ex.throw invalid-field [class-name ..value-field error])))
(def: module-separator "/")
(def: class-path-separator ".")
(def: (evaluate! store loader eval-class valueI)
(-> Store ClassLoader Text Inst (Error Any))
(do error.Monad<Error>
[#let [bytecode-name (text.replace-all class-path-separator module-separator eval-class)
bytecode (def.class #jvm.V1_6
#jvm.Public jvm.noneC
bytecode-name
(list) ["java.lang.Object" (list)]
(list)
(|>> (def.field #jvm.Public ($_ jvm.++F jvm.finalF jvm.staticF)
..value-field ..$Object)
(def.method #jvm.Public ($_ jvm.++M jvm.staticM jvm.strictM)
"<clinit>"
(type.method (list) #.None (list))
(|>> valueI
(inst.PUTSTATIC bytecode-name ..value-field ..$Object)
inst.RETURN))))]
_ (..store! eval-class bytecode store)
class (..load! eval-class loader)]
(class-value eval-class class)))
(def: (execute! store loader temp-label [class-name class-bytecode])
(-> Store ClassLoader Text Definition (Error Any))
(do error.Monad<Error>
[_ (..store! class-name class-bytecode store)]
(..load! class-name loader)))
(def: (define! store loader [module name] valueI)
(-> Store ClassLoader Name Inst (Error [Text Any]))
(let [class-name (format (text.replace-all module-separator class-path-separator module)
class-path-separator (name.normalize name)
"___" (%n (text/hash name)))]
(do error.Monad<Error>
[value (evaluate! store loader class-name valueI)]
(wrap [class-name value]))))
(def: #export init
(IO Host)
(io (let [store (: Store (atom (dictionary.new text.Hash<Text>)))
loader (memory-class-loader store)]
(: Host
(structure
(def: (evaluate! temp-label valueI)
(let [eval-class (|> temp-label name.normalize (text.replace-all " " "$"))]
(..evaluate! store loader eval-class valueI)))
(def: execute! (..execute! store loader))
(def: define! (..define! store loader)))))))
(def: #export runtime-class "LuxRuntime")
(def: #export function-class "LuxFunction")
(def: #export runnable-class "LuxRunnable")
(def: #export unit "")
(def: #export $Variant jvm.Type (type.array 1 ..$Object))
(def: #export $Tuple jvm.Type (type.array 1 ..$Object))
(def: #export $Function jvm.Type (type.class ..function-class (list)))
|