aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/target/jvm/method.lux
blob: be590f84925bb670b86bf7ff695a27251f1236c5 (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
(.using
 [library
  [lux {"-" Type static public private}
   [abstract
    [equivalence {"+" Equivalence}]
    ["[0]" monad {"+" do}]]
   [control
    ["[0]" try]]
   [data
    ["[0]" product]
    ["[0]" format "_"
     ["[1]" binary {"+" Writer} ("[1]#[0]" monoid)]]
    [collection
     ["[0]" sequence {"+" Sequence}]]]]]
 ["[0]" // "_"
  ["[1][0]" modifier {"+" Modifier modifiers:}]
  ["[1][0]" index {"+" Index}]
  ["[1][0]" attribute {"+" Attribute}
   ["[2][0]" code]]
  ["[1][0]" constant {"+" UTF8}
   ["[2][0]" pool {"+" Pool Resource}]]
  ["[1][0]" bytecode {"+" Bytecode}
   ["[2][0]" environment {"+" Environment}]
   ["[2][0]" instruction]]
  ["[1][0]" type {"+" Type}
   [descriptor {"+" Descriptor}]
   ["[2][0]" category]
   ["[2][0]" signature {"+" Signature}]]])

(type: .public Method
  (Rec Method
    (Record
     [#modifier (Modifier Method)
      #name (Index UTF8)
      #descriptor (Index (Descriptor //category.Method))
      #attributes (Sequence Attribute)])))

(modifiers: Method
  ["0001" public]
  ["0002" private]
  ["0004" protected]
  ["0008" static]
  ["0010" final]
  ["0020" synchronized]
  ["0040" bridge]
  ["0080" var_args]
  ["0100" native]
  ["0400" abstract]
  ["0800" strict]
  ["1000" synthetic]
  )

(def: .public (method modifier name with_signature? type attributes code)
  (-> (Modifier Method) UTF8 Bit (Type //category.Method) (List (Resource Attribute)) (Maybe (Bytecode Any))
      (Resource Method))
  (do [! //pool.monad]
    [@name (//pool.utf8 name)
     @descriptor (//pool.descriptor (//type.descriptor type))
     attributes (|> (if with_signature?
                      (list& (//attribute.signature (//type.signature type)) attributes)
                      attributes)
                    (monad.all !)
                    (# ! each sequence.of_list))
     attributes (case code
                  {.#Some code}
                  (do !
                    [environment (case (if (//modifier.has? static modifier)
                                         (//environment.static type)
                                         (//environment.virtual type))
                                   {try.#Success environment}
                                   (in environment)
                                   
                                   {try.#Failure error}
                                   (function (_ _) {try.#Failure error}))
                     [environment exceptions instruction output] (//bytecode.resolve environment code)
                     .let [bytecode (|> instruction //instruction.result format.instance)]
                     @code (//attribute.code [//code.#limit (the //environment.#limit environment)
                                              //code.#code bytecode
                                              //code.#exception_table exceptions
                                              //code.#attributes (sequence.sequence)])]
                    (in (sequence.suffix @code attributes)))
                  
                  {.#None}
                  (in attributes))]
    (in [#modifier modifier
         #name @name
         #descriptor @descriptor
         #attributes attributes])))

(def: .public equivalence
  (Equivalence Method)
  ($_ product.equivalence
      //modifier.equivalence
      //index.equivalence
      //index.equivalence
      (sequence.equivalence //attribute.equivalence)
      ))

(def: .public (writer field)
  (Writer Method)
  (`` ($_ format#composite
          (~~ (template [<writer> <slot>]
                [(<writer> (the <slot> field))]

                [//modifier.writer #modifier]
                [//index.writer #name]
                [//index.writer #descriptor]
                [(format.sequence_16 //attribute.writer) #attributes]))
          )))