aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/control/security/capability.lux
blob: 45b2bd0835ea77805e1e5af7d61bc6ab9595fcef (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
(.module:
  [library
   [lux #*
    [abstract
     [monad (#+ do)]]
    [control
     ["." io (#+ IO)]
     ["<>" parser
      ["<c>" code]]
     [concurrency
      ["." async (#+ Async)]]]
    [data
     [text
      ["%" format (#+ format)]]
     [collection
      ["." list ("#\." functor)]]]
    [type
     abstract]
    ["." meta]
    ["." macro
     ["." code]
     [syntax (#+ syntax:)
      ["|.|" export]
      ["|.|" declaration]
      ["|.|" annotations]]]]])

(abstract: .public (Capability brand input output)
  {}

  (-> input output)

  (def: capability
    (All [brand input output]
      (-> (-> input output)
          (Capability brand input output)))
    (|>> :abstraction))

  (def: .public (use capability input)
    (All [brand input output]
      (-> (Capability brand input output)
          input
          output))
    ((:representation capability) input))

  (syntax: .public (capability: [[export_policy declaration annotations [forger input output]]
                                 (|export|.parser
                                  ($_ <>.and
                                      |declaration|.parser
                                      (<>.maybe |annotations|.parser)
                                      (<c>.form ($_ <>.and <c>.local_identifier <c>.any <c>.any))))])
    (do {! meta.monad}
      [this_module meta.current_module_name
       .let [[name vars] declaration]
       g!brand (\ ! map (|>> %.code code.text)
                  (macro.identifier (format (%.name [this_module name]))))
       .let [capability (` (..Capability (.primitive (~ g!brand)) (~ input) (~ output)))]]
      (in (list (` (type: (~ export_policy)
                     (~ (|declaration|.format declaration))
                     (~ capability)))
                (` (def: (~ (code.local_identifier forger))
                     (All [(~+ (list\map code.local_identifier vars))]
                       (-> (-> (~ input) (~ output))
                           (~ capability)))
                     (~! ..capability)))
                ))))

  (def: .public (async capability)
    (All [brand input output]
      (-> (Capability brand input (IO output))
          (Capability brand input (Async output))))
    (..capability (|>> ((:representation capability)) async.future)))
  )