aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/lux/tool/compiler/phase/generation/jvm/function/method/new.lux
blob: c0bf6e44bac7dcee97114304036373b265d73386 (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
(.module:
  [lux (#- type)
   [abstract
    ["." monad (#+ do)]]
   [control
    [state (#+ State)]]
   [data
    [number
     ["n" nat]]
    [collection
     ["." list ("#@." monoid)]]]
   [target
    [jvm
     ["." descriptor (#+ Descriptor Value Return)]
     ["." modifier (#+ Modifier) ("#@." monoid)]
     ["." field (#+ Field)]
     ["." method (#+ Method)]
     ["_" instruction (#+ Instruction)]
     ["." constant
      [pool (#+ Pool)]]
     [encoding
      [name (#+ External)]
      ["." unsigned]]]]]
  ["." //
   ["#." init]
   ["/#" // #_
    ["#." arity]
    ["#." field
     ["#/." foreign]
     ["#/." partial]]
    ["/#" // #_
     [runtime (#+ Operation)]
     ["#." value]
     ["#." reference]
     [////
      [analysis (#+ Environment)]
      ["." arity (#+ Arity)]
      ["." phase]]]]])

(def: (arguments arity)
  (-> Arity (List (Descriptor (Value Any))))
  (list.repeat (dec arity) ////value.type))

(def: #export (type environment arity)
  (-> Environment Arity [(List (Descriptor (Value Any)))
                         (Descriptor (Return Any))])
  [(list@compose (///field/foreign.closure environment)
                 (if (arity.multiary? arity)
                   (list& ///arity.type (arguments arity))
                   (list)))
   descriptor.void])

(def: #export (instance class environment arity)
  (-> External Environment Arity (Operation (Instruction Any)))
  (do phase.monad
    [foreign* (monad.map @ ////reference.variable environment)]
    (wrap ($_ _.compose
              (_.new class)
              _.dup
              (monad.seq _.monad foreign*)
              (///field/partial.new arity)
              (_.invokespecial class "<init>" (..type environment arity))))))

(def: #export (method class environment arity)
  (-> External Environment Arity (State Pool Method))
  (let [after-this (: (-> Nat Nat)
                      (n.+ 1))
        environment-size (list.size environment)
        after-environment (: (-> Nat Nat)
                             (|>> after-this (n.+ environment-size)))
        after-arity (: (-> Nat Nat)
                       (|>> after-environment (n.+ 1)))]
    (method.method //.modifier "<init>"
                   (descriptor.method (..type environment arity))
                   (list)
                   ($_ _.compose
                       ////reference.this
                       (//init.instruction environment-size arity)
                       (monad.map _.monad (function (_ register)
                                            ($_ _.compose
                                                ////reference.this
                                                (_.aload (unsigned.u1 (after-this register)))
                                                (_.putfield class (////reference.foreign-name register) ////value.type)))
                                  (list.indices environment-size))
                       (monad.map _.monad (function (_ register)
                                            ($_ _.compose
                                                ////reference.this
                                                (_.aload (unsigned.u1 (after-arity register)))
                                                (_.putfield class (////reference.partial-name register) ////value.type)))
                                  (list.indices (n.- ///arity.minimum arity)))
                       _.areturn))))