aboutsummaryrefslogtreecommitdiff
path: root/new-luxc/source/luxc/generator/common.jvm.lux
blob: 0dd19d032407b82b98fa64574ce22a7b1bcb9bf0 (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
(;module:
  lux
  (lux [io]
       (concurrency ["A" atom])
       (data ["R" result]
             (coll ["d" dict])
             text/format)
       [host #+ jvm-import]))

## [Host]
(jvm-import org.objectweb.asm.Opcodes
  (#static V1_6 int))

(jvm-import java.lang.Object)

(jvm-import (java.lang.Class a))

(jvm-import java.lang.ClassLoader
  (loadClass [String] (Class Object)))

## [Types]
(type: #export Bytecode host;Byte-Array)

(type: #export Class-Store (A;Atom (d;Dict Text Bytecode)))

(type: #export Host
  {#loader ClassLoader
   #store Class-Store})

(def: #export (store-class name byte-code)
  (-> Text Bytecode (Lux Unit))
  (function [compiler]
    (let [store (|> (get@ #;host compiler)
                    (:! Host)
                    (get@ #store))]
      (if (d;contains? name (|> store A;get io;run))
        (#R;Error (format "Cannot store class that already exists: " name))
        (#R;Success [compiler (io;run (A;update (d;put name byte-code) store))])
        ))))

(def: #export (load-class name)
  (-> Text (Lux (Class Object)))
  (function [compiler]
    (let [host (:! Host (get@ #;host compiler))
          store (|> host (get@ #store) A;get io;run)]
      (if (d;contains? name store)
        (#R;Success [compiler (ClassLoader.loadClass [name] (get@ #loader host))])
        (#R;Error (format "Unknown class: " name))))))

(def: #export bytecode-version Int Opcodes.V1_6)