tobogganSe connecter

louise

Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.

201 lignes · 4135 octetsBrut · Historique
1// `@remote` functions (ADR 0072): entry points another node spawns, by
2// key, with one argument that crosses hosts. The name used as a value is
3// a `Remote<A>`; called, it is a function.
4
5import std/dist/remote.{type Remote}
6import std/ffi.{type Resource}
7import std/file.{type fs}
8import std/io
9import std/list
10import std/net.{type net}
11import std/process.{type Dynamic, type Pid, type Subject, type proc}
12import std/time.{type clock}
13import std/wire.{type Arg}
14
15pub type Job { Job(id: Int, words: List<String>, reply: Subject<String>) }
16
17@thread_safe
18type Handle { Handle }
19
20// Accepted: a record, a list, a subject, nothing.
21@remote
22fn worker :: job: Job -> () / <proc>
23{
24 process.send job.reply (show job.id)
25}
26
27@remote
28fn sum :: numbers: List<Int> -> () / <io>
29{
30 io.println (show numbers)
31}
32
33@remote
34fn notify :: to: Subject<Pid> -> () / <proc, clock>
35{
36 process.send to (process.self ())
37 let _ = time.now ()
38}
39
40@remote
41fn ping :: -> ()
42{
43 ()
44}
45
46// A reference is a `Remote<A>`, whatever reads it.
47fn reference :: -> Remote<Job>
48{
49 worker
50}
51//~ TYPE reference: fn() -> Remote<Job>
52
53fn inferred ::
54{
55 (sum, notify, ping)
56}
57//~ TYPE inferred: fn() -> (Remote<List<Int>>, Remote<Subject<Pid>>, Remote<()>)
58
59// Called, it is a function like any other.
60fn called :: reply: Subject<String> -> () / <proc>
61{
62 worker Job(1, [], reply)
63}
64
65// A reference is not a closure: wrap it in a lambda to pass one.
66fn as_closure :: jobs: List<Job> -> () / <proc>
67{
68 list.each jobs worker
69 //~^ ERROR E0202 `worker` is `@remote`: its name, not called, is a reference
70 list.each jobs fn job { worker job }
71}
72
73// The dispatcher's argument pairs a value with its own type's
74// descriptor: a program cannot pair it with another.
75fn forged :: d: Dynamic -> Arg
76{
77 wire.Arg("u", d)
78 //~^ ERROR E0117 `Arg` is opaque outside `std/wire`
79}
80
81fn not_wire :: f: fn() -> () -> Arg
82{
83 wire.arg f
84 //~^ ERROR E0706 cannot cross hosts
85}
86
87// Refused: generic, ...
88@remote
89fn generic :: x: a -> ()
90//~^ ERROR E0707 `@remote` function `generic` cannot be generic
91{
92 ()
93}
94
95@remote
96fn polymorphic :: x: Int -> () / <e>
97//~^ ERROR E0707 cannot be generic
98{
99 ()
100}
101
102// ... not one argument, a result, ...
103@remote
104fn two :: a: Int b: Int -> ()
105//~^ ERROR E0707 `@remote` function `two` takes 2 arguments
106{
107 ()
108}
109
110@remote
111fn answers :: x: Int -> Int
112//~^ ERROR E0707 returns `Int`; it returns `()`
113{
114 x
115}
116
117// ... an effect nothing handles around an actor, ...
118effect ask
119{
120 fn ask :: -> Int
121}
122
123@remote
124fn asks :: x: Int -> () / <ask>
125//~^ ERROR E0707 performs `ask`, which nothing handles
126{
127 let _ = ask ()
128 ()
129}
130
131// Local actors receive the fs root; a remotely invocable host entry does
132// not (ADR 0084).
133@remote
134fn reads_the_host :: path: String -> () / <fs>
135//~^ ERROR E0707 performs `fs`, which is denied to `@remote` functions
136{
137 let _ = file.read path
138 ()
139}
140
141// Network opening is independently named and denied by the same policy; raw
142// handle operations do not secretly make the opening root remotely available.
143@remote
144fn opens_the_host :: host: String -> () / <net>
145//~^ ERROR E0707 performs `net`, which is denied to `@remote` functions
146{
147 let _ = net.connect host 443
148 ()
149}
150
151// ... an argument that cannot cross hosts.
152@remote
153fn callback :: f: fn() -> () -> ()
154//~^ ERROR E0706 a function's code
155{
156 ()
157}
158
159@remote
160fn resource :: r: Resource<Handle> -> ()
161//~^ ERROR E0706 a `Resource` is a handle to foreign state
162{
163 ()
164}
165
166@remote
167fn dynamic :: d: Dynamic -> ()
168//~^ ERROR E0706 a `Dynamic` value's type is known only
169{
170 ()
171}
172
173// A signature is required, and a body.
174@remote
175fn unannotated :: x
176//~^ ERROR E0107 `@remote` function `unannotated` needs a type
177//~^^^ ERROR E0107 `@remote` function `unannotated` needs a return type
178{
179 ()
180}
181
182@remote
183@extern c "foreign"
184fn foreign :: x: Int -> ()
185//~^ ERROR E0114 `@remote` function `foreign` needs a body
186
187// Only on top-level functions.
188// The attribute's error is on its line.
189@remote
190//~^ ERROR E0114 `@remote` does not apply to a type
191type Message
192{ Message }
193
194trait Run<a>
195{
196 @remote
197 //~^ ERROR E0114 `@remote` does not apply to a method
198 fn run ::
199 x: a
200 -> ()
201}