aboutsummaryrefslogtreecommitdiff
path: root/new-luxc/source/luxc/lang/translation/jvm.lux
blob: 88e607217f39c80eda91dfaff7d6a237d5c6db10 (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
(.module:
  [lux (#- Definition)
   [control
    pipe
    [monad (#+ do)]
    ["ex" exception (#+ exception:)]
    [concurrency
     ["." atom (#+ Atom atom)]]]
   [data
    ["." error (#+ Error)]
    ["." text ("#/." hash)
     format]
    [collection
     ["." array]
     [list ("#/." functor)]
     ["." dictionary (#+ Dictionary)]]]
   ["." host (#+ import: do-to object)
    [jvm
     ["." loader (#+ Library)]]]
   ["." io (#+ IO io)]
   [world
    [binary (#+ Binary)]]
   [tool
    [compiler
     ["." name]
     [phase
      ["." translation]]]]]
  [///
   [host
    ["." jvm (#+ Inst Definition Host State)
     ["." type]
     ["." def]
     ["." inst]]]]
  )

(import: org/objectweb/asm/Label)

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

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

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

(import: java/lang/ClassLoader)

(type: #export ByteCode Binary)

(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.Failure error)
      (ex.throw cannot-load [class-name error]))
    
    (#error.Failure error)
    (ex.throw invalid-field [class-name ..value-field error])))

(def: class-path-separator ".")

(def: (evaluate! library loader eval-class valueI)
  (-> Library ClassLoader Text Inst (Error Any))
  (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))))]
    (io.run (do (error.with-error io.monad)
              [_ (loader.store eval-class bytecode library)
               class (loader.load eval-class loader)]
              (:: io.monad wrap (class-value eval-class class))))))

(def: (execute! library loader temp-label [class-name class-bytecode])
  (-> Library ClassLoader Text Definition (Error Any))
  (io.run (do (error.with-error io.monad)
            [_ (loader.store class-name class-bytecode library)]
            (loader.load class-name loader))))

(def: (define! library loader [module name] valueI)
  (-> Library 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
      [value (evaluate! library loader class-name valueI)]
      (wrap [class-name value]))))

(def: #export init
  (IO Host)
  (io (let [library (loader.new-library [])
            loader (loader.memory library)]
        (: Host
           (structure
            (def: (evaluate! temp-label valueI)
              (let [eval-class (|> temp-label name.normalize (text.replace-all " " "$"))]
                (..evaluate! library loader eval-class valueI)))
            (def: execute! (..execute! library loader))
            (def: define! (..define! library 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)))