@effekt-lang/effekt 0.83.0 → 0.85.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/effekt +0 -0
- package/libraries/chez/common/effekt_primitives.ss +9 -0
- package/libraries/common/array.effekt +3 -1
- package/libraries/common/bench.effekt +0 -5
- package/libraries/common/bytearray.effekt +27 -10
- package/libraries/common/dictionary.effekt +2 -2
- package/libraries/common/effekt.effekt +49 -7
- package/libraries/common/exception.effekt +3 -3
- package/libraries/common/heap.effekt +100 -90
- package/libraries/common/io/console.effekt +3 -3
- package/libraries/common/io/error.effekt +9 -4
- package/libraries/common/io/filesystem.effekt +73 -44
- package/libraries/common/io/network.effekt +277 -96
- package/libraries/common/io/signal.effekt +5 -4
- package/libraries/common/io.effekt +44 -0
- package/libraries/common/process.effekt +118 -83
- package/libraries/common/ref.effekt +8 -5
- package/libraries/common/result.effekt +17 -1
- package/libraries/common/stream.effekt +2 -2
- package/libraries/common/string.effekt +10 -2
- package/libraries/common/stringbuffer.effekt +1 -1
- package/libraries/js/effekt_builtins.js +9 -0
- package/libraries/js/effekt_runtime.js +3 -0
- package/libraries/llvm/bytearray.c +51 -3
- package/libraries/llvm/forward-declare-c.ll +2 -0
- package/libraries/llvm/io.c +495 -182
- package/libraries/llvm/rts.ll +251 -79
- package/libraries/llvm/types.c +3 -2
- package/licenses/eclipse public license, version 2.0 - epl-2.0.html +5 -5
- package/licenses/mit license - mit-license.html +87 -49
- package/licenses/the bsd license - bsd-license.html +85 -47
- package/licenses/the mit license - mit.html +88 -50
- package/package.json +1 -1
|
@@ -3,37 +3,103 @@ module io/network
|
|
|
3
3
|
import stream
|
|
4
4
|
import bytearray
|
|
5
5
|
|
|
6
|
+
import io
|
|
6
7
|
import io/error
|
|
7
8
|
|
|
8
9
|
|
|
9
|
-
/// A TCP
|
|
10
|
-
|
|
10
|
+
/// A TCP connection, either open or closed.
|
|
11
|
+
/// Reading from or writing to a closed connection raises `EBADF`, but closing it again does nothing.
|
|
12
|
+
/// Closing is explicit.
|
|
13
|
+
extern type Connection =
|
|
14
|
+
jsNode "number"
|
|
15
|
+
llvm "i64"
|
|
16
|
+
|
|
17
|
+
/// Reads up to `size` bytes from a TCP connection into `buffer`, starting at `offset`.
|
|
18
|
+
///
|
|
19
|
+
/// Returns as soon as at least one byte is available, so the result may be smaller than `size`.
|
|
20
|
+
/// Returns 0 once the peer has closed the connection (end of file), and every subsequent call returns 0 too.
|
|
21
|
+
/// At most one read can be pending on a connection: another one raises `EALREADY`.
|
|
22
|
+
/// Raises `EBADF` if the connection is closed.
|
|
23
|
+
/// Panics unless:
|
|
24
|
+
/// - `size` is positive (a read of nothing would look like end of file), and
|
|
25
|
+
/// - `[offset, offset + size)` is a valid range inside `buffer` (like `bytearray::copy`).
|
|
26
|
+
def read(handle: Connection, buffer: ByteArray, offset: Int, size: Int): Int / Exception[IOError] = {
|
|
27
|
+
if (size <= 0) { panic("io/network::read: size must be positive") }
|
|
28
|
+
if (buffer.inBounds(offset, size)) {
|
|
29
|
+
internal::checkResult(internal::read(handle, buffer, offset, size))
|
|
30
|
+
} else {
|
|
31
|
+
panic("io/network::read: window out of bounds")
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/// Writes `size` bytes from `buffer`, starting at `offset`, to a TCP connection.
|
|
36
|
+
///
|
|
37
|
+
/// Returns once all bytes are written, and returns `size`.
|
|
38
|
+
/// Raises `EBADF` if the connection is closed.
|
|
39
|
+
/// Panics unless `[offset, offset + size)` is a valid range inside `buffer` (like `bytearray::copy`).
|
|
40
|
+
def write(handle: Connection, buffer: ByteArray, offset: Int, size: Int): Int / Exception[IOError] = {
|
|
41
|
+
if (buffer.inBounds(offset, size)) {
|
|
42
|
+
internal::checkResult(internal::write(handle, buffer, offset, size))
|
|
43
|
+
} else {
|
|
44
|
+
panic("io/network::write: window out of bounds")
|
|
45
|
+
}
|
|
46
|
+
}
|
|
11
47
|
|
|
12
|
-
///
|
|
13
|
-
|
|
14
|
-
|
|
48
|
+
/// An IP address (v4 or v6) resolved by the system from a previously given host.
|
|
49
|
+
extern type Address =
|
|
50
|
+
jsNode "string"
|
|
51
|
+
llvm "%Pos"
|
|
52
|
+
|
|
53
|
+
/// The text of the given `address`, such as `127.0.0.1` or `::1`.
|
|
54
|
+
extern def toString(address: Address) at {}: String =
|
|
55
|
+
jsNode "${address}"
|
|
56
|
+
llvm "ret %Pos ${address}"
|
|
57
|
+
|
|
58
|
+
/// Resolves the given `host`, a name or an IPv4 or IPv6 address, to its addresses, in the order which the system prefers.
|
|
59
|
+
/// Raises the error of the resolver, such as `EAI_NONAME`, if it has none.
|
|
60
|
+
def resolve(host: String): List[Address] / Exception[IOError] = {
|
|
61
|
+
val resolution = internal::checkResult(internal::resolve(host))
|
|
62
|
+
val addresses = list::collect[Address] { internal::addresses(resolution) }
|
|
63
|
+
internal::release(resolution)
|
|
64
|
+
addresses
|
|
65
|
+
}
|
|
15
66
|
|
|
16
|
-
///
|
|
17
|
-
def
|
|
18
|
-
internal::checkResult(internal::
|
|
67
|
+
/// Establishes a TCP connection to the given `address` and port.
|
|
68
|
+
def connect(address: Address, port: Int): Connection / Exception[IOError] =
|
|
69
|
+
internal::connection(internal::checkResult(internal::connect(address, port)))
|
|
19
70
|
|
|
20
|
-
/// Establishes a TCP connection to the
|
|
71
|
+
/// Establishes a TCP connection to the given `host` and port,
|
|
72
|
+
/// trying the addresses of the host in turn until one accepts (o/w raising the error of the last one).
|
|
21
73
|
def connect(host: String, port: Int): Connection / Exception[IOError] =
|
|
22
|
-
internal::
|
|
74
|
+
internal::eachAddress(resolve(host)) { address => connect(address, port) }
|
|
23
75
|
|
|
24
76
|
/// Closes a TCP connection and releases associated resources.
|
|
25
77
|
def close(handle: Connection): Unit =
|
|
26
78
|
internal::close(handle)
|
|
27
79
|
|
|
28
|
-
/// A TCP listener
|
|
29
|
-
|
|
80
|
+
/// A TCP listener, either listening or shut down.
|
|
81
|
+
/// Listening on a listener that was shut down raises `EBADF`, but shutting it down again does nothing.
|
|
82
|
+
extern type Listener =
|
|
83
|
+
jsNode "number"
|
|
84
|
+
llvm "i64"
|
|
30
85
|
|
|
31
|
-
/// Creates a TCP listener bound to the
|
|
86
|
+
/// Creates a TCP listener bound to the given `address` and port, reserving it (o/w raising `EADDRINUSE`).
|
|
87
|
+
/// Port 0 asks the system for a free port, which `port` then returns.
|
|
88
|
+
def bind(address: Address, port: Int): Listener / Exception[IOError] =
|
|
89
|
+
internal::listener(internal::checkResult(internal::bind(address, port)))
|
|
90
|
+
|
|
91
|
+
/// Creates a TCP listener bound to the given `host` and port,
|
|
92
|
+
/// trying the addresses of the host in turn until one can be bound (o/w raising the error of the last one).
|
|
32
93
|
def bind(host: String, port: Int): Listener / Exception[IOError] =
|
|
33
|
-
internal::
|
|
94
|
+
internal::eachAddress(resolve(host)) { address => bind(address, port) }
|
|
95
|
+
|
|
96
|
+
/// The port which the given `listener` is bound to.
|
|
97
|
+
/// Raises `EBADF` if the listener was shut down.
|
|
98
|
+
def port(listener: Listener): Int / Exception[IOError] =
|
|
99
|
+
internal::checkResult(internal::port(listener))
|
|
34
100
|
|
|
35
|
-
/// Starts listening for incoming connections and handles
|
|
36
|
-
///
|
|
101
|
+
/// Starts listening for incoming connections and handles each with the given `handler`, including those which arrived since `bind`.
|
|
102
|
+
/// Returns only once `shutdown` is called on this `Listener`, so it is usually run with `spawn`.
|
|
37
103
|
def listen(listener: Listener, handler: Connection => Unit at {io, async, global}): Unit / Exception[IOError] = {
|
|
38
104
|
internal::checkResult(internal::listen(listener, handler)); ()
|
|
39
105
|
}
|
|
@@ -42,6 +108,9 @@ def listen(listener: Listener, handler: Connection => Unit at {io, async, global
|
|
|
42
108
|
def shutdown(listener: Listener): Unit =
|
|
43
109
|
internal::shutdown(listener)
|
|
44
110
|
|
|
111
|
+
/// Runs `program` with `emit` writing to the connection and `next` reading from it.
|
|
112
|
+
/// `next` returns the bytes read so far (at most 4096) and performs `stop` at end of file,
|
|
113
|
+
/// so a loop over `next` ends when the peer closes the connection.
|
|
45
114
|
def streaming[R](connection: Connection) { program: () => R / {next[ByteArray], emit[ByteArray]} }: R / Exception[IOError] =
|
|
46
115
|
try {
|
|
47
116
|
program()
|
|
@@ -51,6 +120,7 @@ def streaming[R](connection: Connection) { program: () => R / {next[ByteArray],
|
|
|
51
120
|
} with next[ByteArray] { () =>
|
|
52
121
|
val bytes = bytearray::allocate(4096)
|
|
53
122
|
read(connection, bytes, 0, bytes.size) match {
|
|
123
|
+
case 0 => resume { do stop() }
|
|
54
124
|
case r and r == bytes.size => resume { return bytes }
|
|
55
125
|
case r => resume { bytes.resize(r) }
|
|
56
126
|
}
|
|
@@ -58,148 +128,259 @@ def streaming[R](connection: Connection) { program: () => R / {next[ByteArray],
|
|
|
58
128
|
|
|
59
129
|
namespace internal {
|
|
60
130
|
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
131
|
+
/// The addresses of a `resolution`, in order.
|
|
132
|
+
def addresses(resolution: Int): Unit / emit[Address] = {
|
|
133
|
+
with val i = for[Int] { range(0, addressCount(resolution)) }
|
|
134
|
+
do emit(addressAt(resolution, i))
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/// Runs `attempt` on each of the `addresses` of a host in turn until one succeeds,
|
|
138
|
+
/// and otherwise raises the error of the last attempt.
|
|
139
|
+
def eachAddress[R](addresses: List[Address]) { attempt: Address => R / Exception[IOError] }: R / Exception[IOError] = {
|
|
140
|
+
val Cons(first, rest) = addresses else panic("io/network: `resolve` returned no addresses")
|
|
141
|
+
firstSuccess(first, rest) {attempt}
|
|
142
|
+
}
|
|
67
143
|
|
|
68
144
|
extern jsNode """
|
|
69
|
-
const net = require('net');
|
|
145
|
+
const network$net = require('net');
|
|
146
|
+
const network$dns = require('node:dns');
|
|
147
|
+
|
|
148
|
+
// Resumes with a handle to the addresses of `host` (see `io`), in the order of the system, or with the (negative) error.
|
|
149
|
+
function network$resolve(host, callback) {
|
|
150
|
+
network$dns.lookup(host, { all: true, verbatim: true }, (err, addresses) => {
|
|
151
|
+
if (err) { callback(network$errno(err)); } else { callback(io$open(addresses.map(entry => entry.address))); }
|
|
152
|
+
});
|
|
153
|
+
}
|
|
70
154
|
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
155
|
+
// Error results are negative libuv codes, as Node reports them in `err.errno` (see `io/error`).
|
|
156
|
+
function network$errno(err) { return (typeof err.errno === 'number' && err.errno < 0) ? err.errno : error$code.UNKNOWN; }
|
|
157
|
+
|
|
158
|
+
// A connection records errors and end of file as they happen, and wakes the one which is pending a read.
|
|
159
|
+
// Its listeners stay attached for its whole life, so an error with no read pending is kept
|
|
160
|
+
// for the next operation instead of crashing the process.
|
|
161
|
+
function network$connection(socket) {
|
|
162
|
+
const conn = { socket, error: null, ended: false, waiter: null };
|
|
163
|
+
const wake = () => { const w = conn.waiter; if (w) { conn.waiter = null; w(); } };
|
|
164
|
+
socket.on('readable', wake);
|
|
165
|
+
socket.on('end', () => { conn.ended = true; wake(); });
|
|
166
|
+
socket.on('error', err => { if (!conn.error) conn.error = err; wake(); });
|
|
167
|
+
socket.on('close', () => { conn.ended = true; wake(); });
|
|
168
|
+
return conn;
|
|
169
|
+
}
|
|
75
170
|
|
|
76
|
-
function
|
|
77
|
-
const socket = net.createConnection({ host, port });
|
|
78
|
-
socket.
|
|
79
|
-
|
|
171
|
+
function network$connect(host, port, callback) {
|
|
172
|
+
const socket = network$net.createConnection({ host, port });
|
|
173
|
+
socket.pause();
|
|
174
|
+
const onConnect = () => {
|
|
175
|
+
socket.removeListener('error', onError);
|
|
176
|
+
callback(io$open(network$connection(socket)));
|
|
177
|
+
};
|
|
178
|
+
const onError = err => {
|
|
179
|
+
socket.removeListener('connect', onConnect);
|
|
180
|
+
socket.destroy();
|
|
181
|
+
callback(network$errno(err));
|
|
182
|
+
};
|
|
183
|
+
socket.once('connect', onConnect);
|
|
184
|
+
socket.once('error', onError);
|
|
80
185
|
}
|
|
81
186
|
|
|
82
|
-
function
|
|
83
|
-
const
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
socket.
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
187
|
+
function network$read(handle, buffer, offset, size, callback) {
|
|
188
|
+
const conn = io$get(handle);
|
|
189
|
+
if (conn === undefined) { callback(error$code.EBADF); return; }
|
|
190
|
+
if (conn.waiter) { callback(error$code.EALREADY); return; }
|
|
191
|
+
|
|
192
|
+
const attempt = () => {
|
|
193
|
+
const chunk = conn.socket.read();
|
|
194
|
+
if (chunk !== null) {
|
|
195
|
+
const n = Math.min(size, chunk.length);
|
|
196
|
+
chunk.copy(io$toBuffer(buffer), offset, 0, n);
|
|
197
|
+
if (n < chunk.length) conn.socket.unshift(chunk.subarray(n));
|
|
198
|
+
callback(n);
|
|
199
|
+
} else if (conn.error) {
|
|
200
|
+
callback(network$errno(conn.error));
|
|
201
|
+
} else if (conn.ended || conn.socket.readableEnded) {
|
|
202
|
+
callback(0);
|
|
203
|
+
} else {
|
|
204
|
+
conn.waiter = attempt;
|
|
205
|
+
}
|
|
206
|
+
};
|
|
207
|
+
|
|
208
|
+
attempt();
|
|
101
209
|
}
|
|
102
210
|
|
|
103
|
-
function
|
|
104
|
-
const
|
|
105
|
-
|
|
106
|
-
|
|
211
|
+
function network$write(handle, buffer, offset, size, callback) {
|
|
212
|
+
const conn = io$get(handle);
|
|
213
|
+
if (conn === undefined) { callback(error$code.EBADF); return; }
|
|
214
|
+
if (conn.error) { callback(network$errno(conn.error)); return; }
|
|
215
|
+
|
|
216
|
+
conn.socket.write(io$toBuffer(buffer).subarray(offset, offset + size), err => {
|
|
217
|
+
if (err) callback(network$errno(err)); else callback(size);
|
|
107
218
|
});
|
|
108
219
|
}
|
|
109
220
|
|
|
110
|
-
function
|
|
111
|
-
const
|
|
112
|
-
if (
|
|
113
|
-
|
|
114
|
-
const { socket } = entry;
|
|
221
|
+
function network$close(handle, callback) {
|
|
222
|
+
const conn = io$close(handle);
|
|
223
|
+
if (conn === undefined) { callback($effekt.unit); return; }
|
|
224
|
+
const { socket } = conn;
|
|
115
225
|
if (socket.destroyed) { callback($effekt.unit); return; }
|
|
116
|
-
socket.once('error', _err => {});
|
|
117
226
|
socket.end(() => { socket.destroy(); callback($effekt.unit); });
|
|
118
227
|
}
|
|
119
228
|
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
229
|
+
// Node binds only when listening, so `bind` listens already, and keeps connections alive until `listen` takes them.
|
|
230
|
+
function network$bind(host, port, callback) {
|
|
231
|
+
const server = network$net.createServer();
|
|
232
|
+
const entry = { server, accept: null, pending: [], finished: null };
|
|
233
|
+
server.on('connection', socket => {
|
|
234
|
+
socket.pause();
|
|
235
|
+
if (entry.accept) entry.accept(socket); else entry.pending.push(socket);
|
|
236
|
+
});
|
|
123
237
|
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
server.once('error', err => callback(-(err.errno || 1)));
|
|
238
|
+
const onError = err => callback(network$errno(err));
|
|
239
|
+
server.once('error', onError);
|
|
127
240
|
server.listen(port, host, () => {
|
|
128
|
-
server.
|
|
241
|
+
server.removeListener('error', onError);
|
|
129
242
|
server.on('error', _err => {});
|
|
130
|
-
|
|
131
|
-
socket.pause();
|
|
132
|
-
const connHandle = tcp$alloc({ socket });
|
|
133
|
-
$effekt.runToplevel((ks, k) => handler(connHandle, ks, k));
|
|
134
|
-
});
|
|
135
|
-
callback(0);
|
|
243
|
+
callback(io$open(entry));
|
|
136
244
|
});
|
|
137
245
|
}
|
|
138
246
|
|
|
139
|
-
function
|
|
140
|
-
const entry =
|
|
141
|
-
if (
|
|
142
|
-
|
|
143
|
-
|
|
247
|
+
function network$port(listenerHandle) {
|
|
248
|
+
const entry = io$get(listenerHandle);
|
|
249
|
+
if (entry === undefined) return error$code.EBADF;
|
|
250
|
+
return entry.server.address().port;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
// Settles with `EBADF` if the listener was shut down, and with 0 once `shutdown` is called.
|
|
254
|
+
// On the other hand, errors of listening, such as `EADDRINUSE`, are raised by `bind`.
|
|
255
|
+
function network$listen(listenerHandle, handler, callback) {
|
|
256
|
+
const entry = io$get(listenerHandle);
|
|
257
|
+
if (entry === undefined) { callback(error$code.EBADF); return; }
|
|
258
|
+
|
|
259
|
+
entry.finished = () => callback(0);
|
|
260
|
+
entry.accept = socket => {
|
|
261
|
+
const connHandle = io$open(network$connection(socket));
|
|
262
|
+
$effekt.runToplevel((ks, k) => handler(connHandle, ks, k));
|
|
263
|
+
};
|
|
264
|
+
entry.pending.splice(0).forEach(entry.accept);
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
function network$shutdown(listenerHandle, callback) {
|
|
268
|
+
const entry = io$close(listenerHandle);
|
|
269
|
+
if (entry === undefined) { callback($effekt.unit); return; }
|
|
270
|
+
// Stop accepting without waiting for open connections to end (to avoid a deadlock on Node)
|
|
271
|
+
entry.server.close();
|
|
272
|
+
if (entry.finished) entry.finished();
|
|
273
|
+
callback($effekt.unit);
|
|
144
274
|
}
|
|
145
275
|
"""
|
|
146
276
|
|
|
147
|
-
extern def
|
|
148
|
-
jsNode "$effekt.capture(k =>
|
|
277
|
+
extern def resolve(host: String) at async: Int =
|
|
278
|
+
jsNode "$effekt.capture(k => network$resolve(${host}, k))"
|
|
149
279
|
llvm """
|
|
150
|
-
notail call void @
|
|
280
|
+
notail call void @c_tcp_resolve(%Pos ${host}, %Stack %stack)
|
|
151
281
|
ret void
|
|
152
282
|
"""
|
|
153
283
|
|
|
154
|
-
|
|
155
|
-
|
|
284
|
+
/// The number of addresses in a `resolution`, and each of them by its index, as for `args`.
|
|
285
|
+
extern def addressCount(resolution: Int) at io: Int =
|
|
286
|
+
jsNode "io$get(${resolution}).length"
|
|
287
|
+
llvm """
|
|
288
|
+
%count = call %Int @c_tcp_address_count(%Int ${resolution})
|
|
289
|
+
ret %Int %count
|
|
290
|
+
"""
|
|
291
|
+
|
|
292
|
+
extern def addressAt(resolution: Int, index: Int) at io: Address =
|
|
293
|
+
jsNode "io$get(${resolution})[${index}]"
|
|
294
|
+
llvm """
|
|
295
|
+
%address = call %Pos @c_tcp_address(%Int ${resolution}, %Int ${index})
|
|
296
|
+
ret %Pos %address
|
|
297
|
+
"""
|
|
298
|
+
|
|
299
|
+
extern def release(resolution: Int) at io: Unit =
|
|
300
|
+
jsNode "(io$close(${resolution}), $effekt.unit)"
|
|
301
|
+
llvm """
|
|
302
|
+
call void @c_tcp_resolution_release(%Int ${resolution})
|
|
303
|
+
ret %Pos zeroinitializer
|
|
304
|
+
"""
|
|
305
|
+
|
|
306
|
+
extern def connect(address: Address, port: Int) at async: Int =
|
|
307
|
+
jsNode "$effekt.capture(k => network$connect(${address}, ${port}, k))"
|
|
308
|
+
llvm """
|
|
309
|
+
notail call void @c_tcp_connect(%Pos ${address}, %Int ${port}, %Stack %stack)
|
|
310
|
+
ret void
|
|
311
|
+
"""
|
|
312
|
+
|
|
313
|
+
extern def read(handle: Connection, buffer: ByteArray, offset: Int, size: Int) at async: Int =
|
|
314
|
+
jsNode "$effekt.capture(k => network$read(${handle}, ${buffer}, ${offset}, ${size}, k))"
|
|
156
315
|
llvm """
|
|
157
316
|
notail call void @c_tcp_read(%Int ${handle}, %Pos ${buffer}, %Int ${offset}, %Int ${size}, %Stack %stack)
|
|
158
317
|
ret void
|
|
159
318
|
"""
|
|
160
319
|
|
|
161
|
-
extern def write(handle:
|
|
162
|
-
jsNode "$effekt.capture(k =>
|
|
320
|
+
extern def write(handle: Connection, buffer: ByteArray, offset: Int, size: Int) at async: Int =
|
|
321
|
+
jsNode "$effekt.capture(k => network$write(${handle}, ${buffer}, ${offset}, ${size}, k))"
|
|
163
322
|
llvm """
|
|
164
323
|
notail call void @c_tcp_write(%Int ${handle}, %Pos ${buffer}, %Int ${offset}, %Int ${size}, %Stack %stack)
|
|
165
324
|
ret void
|
|
166
325
|
"""
|
|
167
326
|
|
|
168
|
-
extern def close(handle:
|
|
169
|
-
jsNode "$effekt.capture(k =>
|
|
327
|
+
extern def close(handle: Connection) at async: Unit =
|
|
328
|
+
jsNode "$effekt.capture(k => network$close(${handle}, k))"
|
|
170
329
|
llvm """
|
|
171
330
|
notail call void @c_tcp_close(%Int ${handle}, %Stack %stack)
|
|
172
331
|
ret void
|
|
173
332
|
"""
|
|
174
333
|
|
|
175
|
-
extern def bind(
|
|
176
|
-
jsNode "
|
|
334
|
+
extern def bind(address: Address, port: Int) at async: Int =
|
|
335
|
+
jsNode "$effekt.capture(k => network$bind(${address}, ${port}, k))"
|
|
336
|
+
llvm """
|
|
337
|
+
notail call void @c_tcp_bind(%Pos ${address}, %Int ${port}, %Stack %stack)
|
|
338
|
+
ret void
|
|
339
|
+
"""
|
|
340
|
+
|
|
341
|
+
extern def port(listener: Listener) at io: Int =
|
|
342
|
+
jsNode "network$port(${listener})"
|
|
177
343
|
llvm """
|
|
178
|
-
%result = call %Int @
|
|
344
|
+
%result = call %Int @c_tcp_port(%Int ${listener})
|
|
179
345
|
ret %Int %result
|
|
180
346
|
"""
|
|
181
347
|
|
|
182
|
-
extern def listen(listener:
|
|
183
|
-
jsNode "$effekt.capture(k =>
|
|
348
|
+
extern def listen(listener: Listener, handler: Connection => Unit at {io, async, global}) at async: Int =
|
|
349
|
+
jsNode "$effekt.capture(k => network$listen(${listener}, ${handler}, k))"
|
|
184
350
|
llvm """
|
|
185
351
|
notail call void @c_tcp_listen(%Int ${listener}, %Neg ${handler}, %Stack %stack)
|
|
186
352
|
ret void
|
|
187
353
|
"""
|
|
188
354
|
|
|
189
|
-
extern def shutdown(handle:
|
|
190
|
-
jsNode "$effekt.capture(k =>
|
|
355
|
+
extern def shutdown(handle: Listener) at async: Unit =
|
|
356
|
+
jsNode "$effekt.capture(k => network$shutdown(${handle}, k))"
|
|
191
357
|
llvm """
|
|
192
358
|
notail call void @c_tcp_shutdown(%Int ${handle}, %Stack %stack)
|
|
193
359
|
ret void
|
|
194
360
|
"""
|
|
195
361
|
|
|
362
|
+
/// The connection whose handle `connect` returned, or which `listen` passed to its handler.
|
|
363
|
+
extern def connection(handle: Int) at {}: Connection =
|
|
364
|
+
jsNode "${handle}"
|
|
365
|
+
llvm "ret %Int ${handle}"
|
|
366
|
+
|
|
367
|
+
/// The listener whose handle `bind` returned.
|
|
368
|
+
extern def listener(handle: Int) at {}: Listener =
|
|
369
|
+
jsNode "${handle}"
|
|
370
|
+
llvm "ret %Int ${handle}"
|
|
371
|
+
|
|
196
372
|
extern llvm """
|
|
373
|
+
declare void @c_tcp_resolve(%Pos, %Stack)
|
|
374
|
+
declare %Int @c_tcp_address_count(%Int)
|
|
375
|
+
declare %Pos @c_tcp_address(%Int, %Int)
|
|
376
|
+
declare void @c_tcp_resolution_release(%Int)
|
|
197
377
|
declare void @c_tcp_connect(%Pos, %Int, %Stack)
|
|
198
378
|
declare void @c_tcp_read(%Int, %Pos, %Int, %Int, %Stack)
|
|
199
379
|
declare void @c_tcp_write(%Int, %Pos, %Int, %Int, %Stack)
|
|
200
380
|
declare void @c_tcp_close(%Int, %Stack)
|
|
201
|
-
declare
|
|
202
|
-
declare
|
|
381
|
+
declare void @c_tcp_bind(%Pos, %Int, %Stack)
|
|
382
|
+
declare %Int @c_tcp_port(%Int)
|
|
383
|
+
declare void @c_tcp_listen(%Int, %Neg, %Stack)
|
|
203
384
|
declare void @c_tcp_shutdown(%Int, %Stack)
|
|
204
385
|
"""
|
|
205
386
|
}
|
|
@@ -12,8 +12,7 @@ extern type Signal[A, B] =
|
|
|
12
12
|
extern llvm """
|
|
13
13
|
%Signal = type { %Header, i64, ptr, %Pos }
|
|
14
14
|
|
|
15
|
-
define void @signal_erase(ptr %
|
|
16
|
-
%base = getelementptr %Header, ptr %envPtr, i64 -1
|
|
15
|
+
define void @signal_erase(ptr %base) {
|
|
17
16
|
%statePtr = getelementptr %Signal, ptr %base, i64 0, i32 1
|
|
18
17
|
%stackPtr = getelementptr %Signal, ptr %base, i64 0, i32 2
|
|
19
18
|
%valuePtr = getelementptr %Signal, ptr %base, i64 0, i32 3
|
|
@@ -24,8 +23,10 @@ define void @signal_erase(ptr %envPtr) {
|
|
|
24
23
|
%value = load %Pos, ptr %valuePtr
|
|
25
24
|
call void @erasePositive(%Pos %value)
|
|
26
25
|
call void @eraseStack(%Stack %stack)
|
|
26
|
+
call void @free(ptr %base)
|
|
27
27
|
ret void
|
|
28
28
|
none:
|
|
29
|
+
call void @free(ptr %base)
|
|
29
30
|
ret void
|
|
30
31
|
}
|
|
31
32
|
"""
|
|
@@ -82,7 +83,7 @@ extern def fire[A, B](signal: Signal[A, B], value: A) at async: B =
|
|
|
82
83
|
store i64 3, ptr %statePtr
|
|
83
84
|
notail call void @erasePositive(%Pos ${signal})
|
|
84
85
|
notail call void @resume_Pos(%Stack %otherStack, %Pos ${value})
|
|
85
|
-
|
|
86
|
+
musttail call tailcc void @return_Pos(%Stack %stack, %Pos %otherValue) "no_callee_saved_registers"
|
|
86
87
|
ret void
|
|
87
88
|
panic:
|
|
88
89
|
notail call void @exit(i32 1)
|
|
@@ -110,7 +111,7 @@ extern def wait[A, B](signal: Signal[A, B], value: B) at async: A =
|
|
|
110
111
|
store i64 3, ptr %statePtr
|
|
111
112
|
notail call void @erasePositive(%Pos ${signal})
|
|
112
113
|
notail call void @resume_Pos(%Stack %otherStack, %Pos ${value})
|
|
113
|
-
|
|
114
|
+
musttail call tailcc void @return_Pos(%Stack %stack, %Pos %otherValue) "no_callee_saved_registers"
|
|
114
115
|
ret void
|
|
115
116
|
panic:
|
|
116
117
|
notail call void @exit(i32 1)
|
|
@@ -4,6 +4,50 @@ module io
|
|
|
4
4
|
// Event Loop
|
|
5
5
|
// ----------
|
|
6
6
|
|
|
7
|
+
extern jsNode """
|
|
8
|
+
function io$toBuffer(bytes) {
|
|
9
|
+
return Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
10
|
+
}
|
|
11
|
+
"""
|
|
12
|
+
|
|
13
|
+
// Files, connections and listeners are generational handles into a single table of open I/O resources,
|
|
14
|
+
// as on the LLVM backend (see `io.c`): a slot of the table, and the generation which the slot had when it was opened.
|
|
15
|
+
// Closing a resource starts the next generation of its slot, so its handles no longer match.
|
|
16
|
+
// This is a sort-of cheap way to recover at least some safety :)
|
|
17
|
+
extern jsNode """
|
|
18
|
+
const io$slots = [];
|
|
19
|
+
const io$free = [];
|
|
20
|
+
|
|
21
|
+
function io$open(value) {
|
|
22
|
+
const slot = io$free.length > 0 ? io$free.pop() : io$slots.push({ value: null, generation: 0, open: false }) - 1;
|
|
23
|
+
const entry = io$slots[slot];
|
|
24
|
+
entry.value = value;
|
|
25
|
+
entry.open = true;
|
|
26
|
+
return entry.generation * 0x100000000 + slot;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// The resource of a handle which is still open, or `undefined`.
|
|
30
|
+
function io$get(handle) {
|
|
31
|
+
if (handle < 0) return undefined;
|
|
32
|
+
const entry = io$slots[handle % 0x100000000];
|
|
33
|
+
return (entry && entry.open && entry.generation === Math.floor(handle / 0x100000000)) ? entry.value : undefined;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// Closes a handle which is still open and returns its resource, or returns `undefined`.
|
|
37
|
+
function io$close(handle) {
|
|
38
|
+
const value = io$get(handle);
|
|
39
|
+
if (value === undefined) return undefined;
|
|
40
|
+
|
|
41
|
+
const slot = handle % 0x100000000;
|
|
42
|
+
const entry = io$slots[slot];
|
|
43
|
+
entry.open = false;
|
|
44
|
+
entry.value = null;
|
|
45
|
+
entry.generation = (entry.generation + 1) % 0x200000; // 21 bits, so that a handle stays an exact integer on JS
|
|
46
|
+
io$free.push(slot);
|
|
47
|
+
return value;
|
|
48
|
+
}
|
|
49
|
+
"""
|
|
50
|
+
|
|
7
51
|
extern llvm """
|
|
8
52
|
declare void @c_yield(%Stack)
|
|
9
53
|
"""
|