@shd101wyy/yo 0.1.25 → 0.1.27
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/.github/skills/yo-async-effects/SKILL.md +4 -4
- package/.github/skills/yo-async-effects/async-effects-recipes.md +40 -40
- package/.github/skills/yo-core-patterns/SKILL.md +1 -1
- package/.github/skills/yo-core-patterns/core-patterns-cheatsheet.md +30 -26
- package/.github/skills/yo-project-workflow/SKILL.md +6 -3
- package/.github/skills/yo-project-workflow/workflow-cheatsheet.md +34 -11
- package/.github/skills/yo-syntax/SKILL.md +7 -6
- package/.github/skills/yo-syntax/syntax-cheatsheet.md +78 -60
- package/.github/skills/yo-wasm-integration/wasm-integration-cheatsheet.md +3 -3
- package/README.md +10 -8
- package/out/cjs/index.cjs +583 -567
- package/out/cjs/yo-cli.cjs +664 -632
- package/out/cjs/yo-lsp.cjs +510 -485
- package/out/esm/index.mjs +538 -522
- package/out/types/src/codegen/codegen-c.d.ts +2 -2
- package/out/types/src/codegen/functions/collection.d.ts +2 -2
- package/out/types/src/codegen/functions/context.d.ts +3 -2
- package/out/types/src/codegen/types/collection.d.ts +2 -2
- package/out/types/src/codegen/utils/index.d.ts +3 -1
- package/out/types/src/doc/builder.d.ts +2 -2
- package/out/types/src/evaluator/calls/closure-type.d.ts +2 -2
- package/out/types/src/evaluator/calls/record-type.d.ts +11 -0
- package/out/types/src/evaluator/context.d.ts +8 -9
- package/out/types/src/evaluator/index.d.ts +3 -3
- package/out/types/src/evaluator/types/record.d.ts +14 -0
- package/out/types/src/evaluator/types/validation.d.ts +2 -2
- package/out/types/src/evaluator/values/anonymous-module.d.ts +5 -5
- package/out/types/src/evaluator/values/impl.d.ts +1 -1
- package/out/types/src/expr.d.ts +1 -4
- package/out/types/src/formatter.d.ts +11 -0
- package/out/types/src/function-value.d.ts +1 -1
- package/out/types/src/lsp/document-manager.d.ts +1 -1
- package/out/types/src/lsp/formatting.d.ts +2 -0
- package/out/types/src/module-manager.d.ts +3 -3
- package/out/types/src/tests/formatter.test.d.ts +1 -0
- package/out/types/src/types/creators.d.ts +3 -4
- package/out/types/src/types/definitions.d.ts +8 -19
- package/out/types/src/types/guards.d.ts +3 -3
- package/out/types/src/types/tags.d.ts +0 -1
- package/out/types/src/types/utils.d.ts +1 -1
- package/out/types/src/value-tag.d.ts +0 -1
- package/out/types/src/value.d.ts +6 -13
- package/out/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +1 -1
- package/std/alg/hash.yo +13 -21
- package/std/allocator.yo +25 -40
- package/std/async.yo +3 -7
- package/std/build.yo +105 -151
- package/std/cli/arg_parser.yo +184 -169
- package/std/collections/array_list.yo +350 -314
- package/std/collections/btree_map.yo +142 -131
- package/std/collections/deque.yo +132 -128
- package/std/collections/hash_map.yo +542 -566
- package/std/collections/hash_set.yo +623 -687
- package/std/collections/linked_list.yo +275 -293
- package/std/collections/ordered_map.yo +113 -85
- package/std/collections/priority_queue.yo +73 -73
- package/std/crypto/md5.yo +191 -95
- package/std/crypto/random.yo +56 -64
- package/std/crypto/sha256.yo +151 -107
- package/std/encoding/base64.yo +87 -81
- package/std/encoding/hex.yo +43 -50
- package/std/encoding/html.yo +56 -81
- package/std/encoding/html_char_utils.yo +7 -13
- package/std/encoding/html_entities.yo +2248 -2253
- package/std/encoding/json.yo +316 -224
- package/std/encoding/punycode.yo +86 -116
- package/std/encoding/toml.yo +67 -66
- package/std/encoding/utf16.yo +37 -44
- package/std/env.yo +62 -91
- package/std/error.yo +12 -20
- package/std/fmt/display.yo +5 -9
- package/std/fmt/index.yo +8 -14
- package/std/fmt/to_string.yo +330 -315
- package/std/fmt/writer.yo +58 -87
- package/std/fs/dir.yo +83 -102
- package/std/fs/file.yo +147 -180
- package/std/fs/metadata.yo +45 -78
- package/std/fs/temp.yo +55 -65
- package/std/fs/types.yo +27 -40
- package/std/fs/walker.yo +53 -68
- package/std/gc.yo +5 -8
- package/std/glob.yo +30 -43
- package/std/http/client.yo +107 -120
- package/std/http/http.yo +106 -96
- package/std/http/index.yo +4 -6
- package/std/imm/list.yo +88 -93
- package/std/imm/map.yo +528 -464
- package/std/imm/set.yo +52 -57
- package/std/imm/sorted_map.yo +340 -286
- package/std/imm/sorted_set.yo +57 -63
- package/std/imm/string.yo +404 -345
- package/std/imm/vec.yo +173 -181
- package/std/io/reader.yo +3 -6
- package/std/io/writer.yo +4 -8
- package/std/libc/assert.yo +5 -9
- package/std/libc/ctype.yo +32 -22
- package/std/libc/dirent.yo +26 -25
- package/std/libc/errno.yo +164 -90
- package/std/libc/fcntl.yo +52 -45
- package/std/libc/float.yo +66 -44
- package/std/libc/limits.yo +42 -33
- package/std/libc/math.yo +53 -82
- package/std/libc/signal.yo +72 -47
- package/std/libc/stdatomic.yo +217 -188
- package/std/libc/stdint.yo +5 -29
- package/std/libc/stdio.yo +5 -29
- package/std/libc/stdlib.yo +32 -39
- package/std/libc/string.yo +5 -23
- package/std/libc/sys/stat.yo +58 -56
- package/std/libc/time.yo +5 -19
- package/std/libc/unistd.yo +5 -20
- package/std/libc/wctype.yo +6 -9
- package/std/libc/windows.yo +26 -30
- package/std/log.yo +41 -55
- package/std/net/addr.yo +102 -97
- package/std/net/dns.yo +27 -28
- package/std/net/errors.yo +50 -49
- package/std/net/tcp.yo +113 -124
- package/std/net/udp.yo +55 -66
- package/std/os/env.yo +35 -33
- package/std/os/signal.yo +15 -25
- package/std/path.yo +276 -311
- package/std/prelude.yo +6316 -4333
- package/std/process/command.yo +87 -103
- package/std/process/index.yo +12 -31
- package/std/regex/compiler.yo +196 -95
- package/std/regex/flags.yo +58 -39
- package/std/regex/index.yo +157 -173
- package/std/regex/match.yo +20 -31
- package/std/regex/node.yo +134 -152
- package/std/regex/parser.yo +283 -259
- package/std/regex/unicode.yo +172 -202
- package/std/regex/vm.yo +155 -171
- package/std/string/index.yo +5 -7
- package/std/string/rune.yo +45 -55
- package/std/string/string.yo +937 -964
- package/std/string/string_builder.yo +94 -104
- package/std/string/unicode.yo +46 -64
- package/std/sync/channel.yo +72 -73
- package/std/sync/cond.yo +31 -36
- package/std/sync/mutex.yo +30 -32
- package/std/sync/once.yo +13 -16
- package/std/sync/rwlock.yo +26 -31
- package/std/sync/waitgroup.yo +20 -25
- package/std/sys/advise.yo +16 -24
- package/std/sys/bufio/buf_reader.yo +77 -93
- package/std/sys/bufio/buf_writer.yo +52 -65
- package/std/sys/clock.yo +4 -9
- package/std/sys/constants.yo +77 -61
- package/std/sys/copy.yo +4 -10
- package/std/sys/dir.yo +26 -43
- package/std/sys/dns.yo +41 -61
- package/std/sys/errors.yo +95 -103
- package/std/sys/events.yo +45 -57
- package/std/sys/externs.yo +319 -267
- package/std/sys/fallocate.yo +7 -11
- package/std/sys/fcntl.yo +14 -22
- package/std/sys/file.yo +26 -40
- package/std/sys/future.yo +5 -8
- package/std/sys/iov.yo +12 -25
- package/std/sys/lock.yo +12 -13
- package/std/sys/mmap.yo +38 -43
- package/std/sys/path.yo +3 -8
- package/std/sys/perm.yo +7 -21
- package/std/sys/pipe.yo +5 -12
- package/std/sys/process.yo +23 -29
- package/std/sys/seek.yo +10 -12
- package/std/sys/signal.yo +7 -13
- package/std/sys/signals.yo +52 -35
- package/std/sys/socket.yo +63 -58
- package/std/sys/socketpair.yo +3 -6
- package/std/sys/sockinfo.yo +11 -20
- package/std/sys/statfs.yo +11 -34
- package/std/sys/statx.yo +25 -52
- package/std/sys/sysinfo.yo +15 -20
- package/std/sys/tcp.yo +62 -92
- package/std/sys/temp.yo +5 -9
- package/std/sys/time.yo +5 -15
- package/std/sys/timer.yo +6 -11
- package/std/sys/tty.yo +10 -18
- package/std/sys/udp.yo +22 -39
- package/std/sys/umask.yo +3 -6
- package/std/sys/unix.yo +33 -52
- package/std/testing/bench.yo +49 -52
- package/std/thread.yo +10 -15
- package/std/time/datetime.yo +105 -89
- package/std/time/duration.yo +43 -56
- package/std/time/instant.yo +13 -18
- package/std/time/sleep.yo +5 -9
- package/std/url/index.yo +184 -209
- package/std/worker.yo +6 -10
- package/out/types/src/evaluator/calls/module-type.d.ts +0 -11
- package/out/types/src/evaluator/types/module.d.ts +0 -19
package/std/net/addr.yo
CHANGED
|
@@ -7,56 +7,52 @@
|
|
|
7
7
|
// { IpAddr, SocketAddr } :: import "std/net/addr";
|
|
8
8
|
//
|
|
9
9
|
// addr := SocketAddr.new(IpAddr.loopback_v4(), u16(8080));
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
{
|
|
13
|
-
{
|
|
14
|
-
{
|
|
15
|
-
{
|
|
16
|
-
{ GlobalAllocator } :: import "../allocator";
|
|
10
|
+
open(import("../string"));
|
|
11
|
+
{ ToString } :: import("../fmt");
|
|
12
|
+
{ Writer } :: import("../fmt/writer");
|
|
13
|
+
{ NetError } :: import("./errors");
|
|
14
|
+
{ snprintf } :: import("../libc/stdio");
|
|
15
|
+
{ GlobalAllocator } :: import("../allocator");
|
|
17
16
|
{ malloc, free } :: GlobalAllocator;
|
|
18
|
-
{ Error, AnyError, Exception } :: import
|
|
19
|
-
|
|
17
|
+
{ Error, AnyError, Exception } :: import("../error");
|
|
20
18
|
// ============================================================================
|
|
21
19
|
// IpAddr
|
|
22
20
|
// ============================================================================
|
|
23
|
-
|
|
24
21
|
// ASCII constants for parsing
|
|
25
|
-
_ASCII_DOT
|
|
26
|
-
_ASCII_ZERO :: u8(48);
|
|
27
|
-
_ASCII_NINE :: u8(57);
|
|
28
|
-
|
|
22
|
+
_ASCII_DOT :: u8(46); // '.'
|
|
23
|
+
_ASCII_ZERO :: u8(48); // '0'
|
|
24
|
+
_ASCII_NINE :: u8(57); // '9'
|
|
29
25
|
/// An IP address, either IPv4 or IPv6.
|
|
30
26
|
IpAddr :: enum(
|
|
31
27
|
/// An IPv4 address represented as four octets.
|
|
32
|
-
V4(a: u8, b: u8, c: u8, d: u8),
|
|
28
|
+
V4(a : u8, b : u8, c : u8, d : u8),
|
|
33
29
|
/// An IPv6 address represented as eight 16-bit segments.
|
|
34
|
-
V6(segments: Array(u16, usize(8)))
|
|
30
|
+
V6(segments : Array(u16, usize(8)))
|
|
35
31
|
);
|
|
36
|
-
|
|
37
|
-
|
|
32
|
+
impl(
|
|
33
|
+
IpAddr,
|
|
38
34
|
/// Parse an IPv4 address from a dotted-decimal `String` (e.g. "127.0.0.1").
|
|
39
35
|
/// Throws on invalid input.
|
|
40
|
-
parse_v4 : (fn(s: String, using(exn : Exception)) -> IpAddr)({
|
|
36
|
+
parse_v4 : (fn(s : String, using(exn : Exception)) -> IpAddr)({
|
|
41
37
|
bytes := s.as_bytes();
|
|
42
38
|
parts := Array(u8, usize(4)).fill(u8(0));
|
|
43
39
|
part_idx := usize(0);
|
|
44
40
|
val := u32(0);
|
|
45
41
|
i := usize(0);
|
|
46
|
-
while
|
|
42
|
+
while(runtime(i < bytes.len()), {
|
|
47
43
|
ch := bytes.get(i).unwrap();
|
|
48
44
|
cond(
|
|
49
45
|
(ch == _ASCII_DOT) => {
|
|
50
46
|
// '.' separator
|
|
51
47
|
cond(
|
|
52
48
|
(part_idx >= usize(3)) => {
|
|
53
|
-
exn.throw(dyn
|
|
49
|
+
exn.throw(dyn(NetError.Other(String.from("invalid IPv4 address"))));
|
|
54
50
|
},
|
|
55
51
|
true => ()
|
|
56
52
|
);
|
|
57
53
|
cond(
|
|
58
54
|
(val > u32(255)) => {
|
|
59
|
-
exn.throw(dyn
|
|
55
|
+
exn.throw(dyn(NetError.Other(String.from("IPv4 octet out of range"))));
|
|
60
56
|
},
|
|
61
57
|
true => ()
|
|
62
58
|
);
|
|
@@ -66,151 +62,160 @@ impl(IpAddr,
|
|
|
66
62
|
},
|
|
67
63
|
((ch >= _ASCII_ZERO) && (ch <= _ASCII_NINE)) => {
|
|
68
64
|
// digit
|
|
69
|
-
val = ((val * u32(10)) + u32(
|
|
65
|
+
val = ((val * u32(10)) + u32(ch - _ASCII_ZERO));
|
|
70
66
|
},
|
|
71
67
|
true => {
|
|
72
|
-
exn.throw(dyn
|
|
68
|
+
exn.throw(dyn(NetError.Other(String.from("invalid character in IPv4 address"))));
|
|
73
69
|
}
|
|
74
70
|
);
|
|
75
71
|
i = (i + usize(1));
|
|
76
|
-
};
|
|
72
|
+
});
|
|
77
73
|
cond(
|
|
78
74
|
(part_idx != usize(3)) => {
|
|
79
|
-
exn.throw(dyn
|
|
75
|
+
exn.throw(dyn(NetError.Other(String.from("invalid IPv4 address"))));
|
|
80
76
|
},
|
|
81
77
|
true => ()
|
|
82
78
|
);
|
|
83
79
|
cond(
|
|
84
80
|
(val > u32(255)) => {
|
|
85
|
-
exn.throw(dyn
|
|
81
|
+
exn.throw(dyn(NetError.Other(String.from("IPv4 octet out of range"))));
|
|
86
82
|
},
|
|
87
83
|
true => ()
|
|
88
84
|
);
|
|
89
85
|
parts(usize(3)) = u8(val);
|
|
90
86
|
.V4(parts(usize(0)), parts(usize(1)), parts(usize(2)), parts(usize(3)))
|
|
91
87
|
}),
|
|
92
|
-
|
|
93
88
|
/// Return the IPv4 loopback address (127.0.0.1).
|
|
94
89
|
loopback_v4 : (fn() -> IpAddr)(
|
|
95
90
|
.V4(u8(127), u8(0), u8(0), u8(1))
|
|
96
91
|
),
|
|
97
|
-
|
|
98
92
|
/// Return the IPv6 loopback address (::1).
|
|
99
93
|
loopback_v6 : (fn() -> IpAddr)({
|
|
100
94
|
segs := Array(u16, usize(8)).fill(u16(0));
|
|
101
95
|
segs(usize(7)) = u16(1);
|
|
102
96
|
.V6(segs)
|
|
103
97
|
}),
|
|
104
|
-
|
|
105
98
|
/// Return the IPv4 unspecified address (0.0.0.0).
|
|
106
99
|
any_v4 : (fn() -> IpAddr)(
|
|
107
100
|
.V4(u8(0), u8(0), u8(0), u8(0))
|
|
108
101
|
),
|
|
109
|
-
|
|
110
102
|
/// Return `true` if this is a loopback address.
|
|
111
|
-
is_loopback : (fn(self: Self) -> bool)(
|
|
112
|
-
match(
|
|
103
|
+
is_loopback : (fn(self : Self) -> bool)(
|
|
104
|
+
match(
|
|
105
|
+
self,
|
|
113
106
|
.V4(a, _, _, _) => (a == u8(127)),
|
|
114
107
|
.V6(segs) => {
|
|
115
108
|
// ::1
|
|
116
109
|
all_zero := true;
|
|
117
110
|
i := usize(0);
|
|
118
|
-
while
|
|
111
|
+
while(runtime(i < usize(7)), {
|
|
119
112
|
cond(
|
|
120
|
-
(segs(i) != u16(0)) => {
|
|
113
|
+
(segs(i) != u16(0)) => {
|
|
114
|
+
all_zero = false;
|
|
115
|
+
},
|
|
121
116
|
true => ()
|
|
122
117
|
);
|
|
123
118
|
i = (i + usize(1));
|
|
124
|
-
};
|
|
125
|
-
|
|
119
|
+
});
|
|
120
|
+
all_zero && (segs(usize(7)) == u16(1))
|
|
126
121
|
}
|
|
127
122
|
)
|
|
128
123
|
),
|
|
129
|
-
|
|
130
124
|
/// Return `true` if this is an IPv4 address.
|
|
131
|
-
is_v4 : (fn(self: Self) -> bool)(
|
|
132
|
-
match(
|
|
125
|
+
is_v4 : (fn(self : Self) -> bool)(
|
|
126
|
+
match(
|
|
127
|
+
self,
|
|
133
128
|
.V4(_, _, _, _) => true,
|
|
134
129
|
.V6(_) => false
|
|
135
130
|
)
|
|
136
131
|
),
|
|
137
|
-
|
|
138
132
|
/// Return `true` if this is an IPv6 address.
|
|
139
|
-
is_v6 : (fn(self: Self) -> bool)(
|
|
140
|
-
match(
|
|
133
|
+
is_v6 : (fn(self : Self) -> bool)(
|
|
134
|
+
match(
|
|
135
|
+
self,
|
|
141
136
|
.V4(_, _, _, _) => false,
|
|
142
137
|
.V6(_) => true
|
|
143
138
|
)
|
|
144
139
|
)
|
|
145
140
|
);
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
141
|
+
impl(
|
|
142
|
+
IpAddr,
|
|
143
|
+
ToString(
|
|
144
|
+
to_string : (
|
|
145
|
+
self -> {
|
|
146
|
+
return(
|
|
147
|
+
match(
|
|
148
|
+
self.*,
|
|
149
|
+
.V4(a, b, c, d) => {
|
|
150
|
+
buf := Array(u8, usize(16)).fill(u8(0));
|
|
151
|
+
snprintf(*(char)(&(buf(usize(0)))), usize(16), "%d.%d.%d.%d", i32(a), i32(b), i32(c), i32(d));
|
|
152
|
+
String.from_cstr(&(buf(usize(0)))).unwrap()
|
|
153
|
+
},
|
|
154
|
+
.V6(segs) => {
|
|
155
|
+
w := Writer.new();
|
|
156
|
+
i := usize(0);
|
|
157
|
+
while(runtime(i < usize(8)), {
|
|
158
|
+
cond(
|
|
159
|
+
(i > usize(0)) => {
|
|
160
|
+
w.write_str(":");
|
|
161
|
+
},
|
|
162
|
+
true => ()
|
|
163
|
+
);
|
|
164
|
+
w.write_hex(u64(segs(i)));
|
|
165
|
+
i = (i + usize(1));
|
|
166
|
+
});
|
|
167
|
+
w.to_string()
|
|
168
|
+
}
|
|
169
|
+
)
|
|
170
|
+
);
|
|
167
171
|
}
|
|
168
|
-
)
|
|
169
|
-
|
|
170
|
-
)
|
|
171
|
-
|
|
172
|
-
export IpAddr;
|
|
173
|
-
|
|
172
|
+
)
|
|
173
|
+
)
|
|
174
|
+
);
|
|
175
|
+
export(IpAddr);
|
|
174
176
|
// ============================================================================
|
|
175
177
|
// SocketAddr
|
|
176
178
|
// ============================================================================
|
|
177
|
-
|
|
178
179
|
/// An IP address paired with a port number.
|
|
179
180
|
SocketAddr :: struct(
|
|
180
181
|
/// The IP address.
|
|
181
|
-
ip
|
|
182
|
+
ip : IpAddr,
|
|
182
183
|
/// The port number.
|
|
183
184
|
port : u16
|
|
184
185
|
);
|
|
185
|
-
|
|
186
|
-
|
|
186
|
+
impl(
|
|
187
|
+
SocketAddr,
|
|
187
188
|
/// Create a new socket address from an IP and port.
|
|
188
|
-
new : (fn(ip: IpAddr, port: u16) -> Self)(
|
|
189
|
-
Self(ip: ip, port: port)
|
|
189
|
+
new : (fn(ip : IpAddr, port : u16) -> Self)(
|
|
190
|
+
Self(ip : ip, port : port)
|
|
190
191
|
),
|
|
191
|
-
|
|
192
192
|
/// Create an IPv4 loopback socket address (127.0.0.1) with the given port.
|
|
193
|
-
loopback : (fn(port: u16) -> Self)(
|
|
194
|
-
Self(ip: IpAddr.loopback_v4(), port: port)
|
|
193
|
+
loopback : (fn(port : u16) -> Self)(
|
|
194
|
+
Self(ip : IpAddr.loopback_v4(), port : port)
|
|
195
195
|
),
|
|
196
|
-
|
|
197
196
|
/// Create an IPv4 unspecified socket address (0.0.0.0) with the given port.
|
|
198
|
-
any : (fn(port: u16) -> Self)(
|
|
199
|
-
Self(ip: IpAddr.any_v4(), port: port)
|
|
197
|
+
any : (fn(port : u16) -> Self)(
|
|
198
|
+
Self(ip : IpAddr.any_v4(), port : port)
|
|
200
199
|
)
|
|
201
200
|
);
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
)
|
|
215
|
-
|
|
216
|
-
|
|
201
|
+
impl(
|
|
202
|
+
SocketAddr,
|
|
203
|
+
ToString(
|
|
204
|
+
to_string : (
|
|
205
|
+
self -> {
|
|
206
|
+
ip_str := self.ip.to_string();
|
|
207
|
+
buf := Array(u8, usize(8)).fill(u8(0));
|
|
208
|
+
snprintf(*(char)(&(buf(usize(0)))), usize(8), "%d", i32(self.port));
|
|
209
|
+
port_str := String.from_cstr(&(buf(usize(0)))).unwrap();
|
|
210
|
+
return(
|
|
211
|
+
match(
|
|
212
|
+
self.ip,
|
|
213
|
+
.V4(_, _, _, _) => `${ip_str}:${port_str}`,
|
|
214
|
+
.V6(_) => `[${ip_str}]:${port_str}`
|
|
215
|
+
)
|
|
216
|
+
);
|
|
217
|
+
}
|
|
218
|
+
)
|
|
219
|
+
)
|
|
220
|
+
);
|
|
221
|
+
export(SocketAddr);
|
package/std/net/dns.yo
CHANGED
|
@@ -21,40 +21,38 @@
|
|
|
21
21
|
// i = (i + usize(1));
|
|
22
22
|
// };
|
|
23
23
|
// });
|
|
24
|
-
|
|
25
|
-
{ GlobalAllocator } :: import "../allocator";
|
|
24
|
+
{ GlobalAllocator } :: import("../allocator");
|
|
26
25
|
{ malloc, free } :: GlobalAllocator;
|
|
27
|
-
{ ArrayList } :: import
|
|
28
|
-
open
|
|
29
|
-
open
|
|
30
|
-
{ NetError } :: import
|
|
31
|
-
{ IpAddr, SocketAddr } :: import
|
|
32
|
-
{ Error, AnyError, Exception } :: import
|
|
33
|
-
IO_dns :: import
|
|
34
|
-
{ AF_INET, AF_INET6 } :: import
|
|
35
|
-
|
|
26
|
+
{ ArrayList } :: import("../collections/array_list");
|
|
27
|
+
open(import("../string"));
|
|
28
|
+
open(import("../fmt"));
|
|
29
|
+
{ NetError } :: import("./errors");
|
|
30
|
+
{ IpAddr, SocketAddr } :: import("./addr");
|
|
31
|
+
{ Error, AnyError, Exception } :: import("../error");
|
|
32
|
+
IO_dns :: import("../sys/dns");
|
|
33
|
+
{ AF_INET, AF_INET6 } :: import("../sys/socket");
|
|
36
34
|
// ============================================================================
|
|
37
35
|
// DNS lookup
|
|
38
36
|
// ============================================================================
|
|
39
|
-
|
|
40
37
|
/// Resolve a hostname to a list of IP addresses.
|
|
41
|
-
lookup_host :: (fn(host: String, using(io : IO)) -> Impl(Future(ArrayList(IpAddr), IO, Exception)))(
|
|
38
|
+
lookup_host :: (fn(host : String, using(io : IO)) -> Impl(Future(ArrayList(IpAddr), IO, Exception)))(
|
|
42
39
|
io.async((using(io : IO, exn : Exception)) => {
|
|
43
40
|
host_cstr_bytes := host.to_cstr();
|
|
44
41
|
host_cstr := host_cstr_bytes.ptr().unwrap();
|
|
45
42
|
result_ptr := IO_dns.alloc_result();
|
|
46
|
-
ret := io.await(IO_dns.getaddrinfo(host_cstr
|
|
43
|
+
ret := io.await(IO_dns.getaddrinfo(host_cstr,.None,.None, result_ptr));
|
|
47
44
|
cond(
|
|
48
45
|
(ret != i32(0)) => {
|
|
49
46
|
IO_dns.free_result(result_ptr);
|
|
50
|
-
exn.throw(dyn
|
|
47
|
+
exn.throw(dyn(NetError.DNSFailed(host)));
|
|
51
48
|
},
|
|
52
49
|
true => ()
|
|
53
50
|
);
|
|
54
51
|
addrs := ArrayList(IpAddr).new();
|
|
55
52
|
cur := IO_dns.get_result(result_ptr);
|
|
56
|
-
while
|
|
57
|
-
match(
|
|
53
|
+
while(runtime(true), {
|
|
54
|
+
match(
|
|
55
|
+
cur,
|
|
58
56
|
.Some(ai) => {
|
|
59
57
|
family := IO_dns.addrinfo_family(ai);
|
|
60
58
|
addr_ptr := IO_dns.addrinfo_addr(ai);
|
|
@@ -72,32 +70,33 @@ lookup_host :: (fn(host: String, using(io : IO)) -> Impl(Future(ArrayList(IpAddr
|
|
|
72
70
|
);
|
|
73
71
|
cur = IO_dns.addrinfo_next(ai);
|
|
74
72
|
},
|
|
75
|
-
.None => {
|
|
73
|
+
.None => {
|
|
74
|
+
break;
|
|
75
|
+
}
|
|
76
76
|
);
|
|
77
|
-
};
|
|
77
|
+
});
|
|
78
78
|
// Free the result
|
|
79
|
-
match(
|
|
79
|
+
match(
|
|
80
|
+
IO_dns.get_result(result_ptr),
|
|
80
81
|
.Some(ai) => IO_dns.freeaddrinfo(ai),
|
|
81
82
|
.None => ()
|
|
82
83
|
);
|
|
83
84
|
IO_dns.free_result(result_ptr);
|
|
84
|
-
return
|
|
85
|
+
return(addrs);
|
|
85
86
|
})
|
|
86
87
|
);
|
|
87
|
-
|
|
88
88
|
/// Resolve a hostname and port to a list of socket addresses.
|
|
89
|
-
resolve :: (fn(host: String, port: u16, using(io : IO)) -> Impl(Future(ArrayList(SocketAddr), IO, Exception)))(
|
|
89
|
+
resolve :: (fn(host : String, port : u16, using(io : IO)) -> Impl(Future(ArrayList(SocketAddr), IO, Exception)))(
|
|
90
90
|
io.async((using(io : IO, exn : Exception)) => {
|
|
91
91
|
addrs := io.await(lookup_host(host, using(io)));
|
|
92
92
|
result := ArrayList(SocketAddr).new();
|
|
93
93
|
i := usize(0);
|
|
94
|
-
while
|
|
94
|
+
while(runtime(i < addrs.len()), {
|
|
95
95
|
ip := addrs.get(i).unwrap();
|
|
96
96
|
result.push(SocketAddr.new(ip, port));
|
|
97
97
|
i = (i + usize(1));
|
|
98
|
-
};
|
|
99
|
-
return
|
|
98
|
+
});
|
|
99
|
+
return(result);
|
|
100
100
|
})
|
|
101
101
|
);
|
|
102
|
-
|
|
103
|
-
export lookup_host, resolve;
|
|
102
|
+
export(lookup_host, resolve);
|
package/std/net/errors.yo
CHANGED
|
@@ -5,16 +5,13 @@
|
|
|
5
5
|
//
|
|
6
6
|
// Example:
|
|
7
7
|
// { NetError } :: import "std/net/errors";
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
{
|
|
11
|
-
{
|
|
12
|
-
{ Error, AnyError, Exception } :: import "../error";
|
|
13
|
-
|
|
8
|
+
open(import("../string"));
|
|
9
|
+
{ IOError } :: import("../sys/errors");
|
|
10
|
+
{ ToString } :: import("../fmt");
|
|
11
|
+
{ Error, AnyError, Exception } :: import("../error");
|
|
14
12
|
// ============================================================================
|
|
15
13
|
// NetError
|
|
16
14
|
// ============================================================================
|
|
17
|
-
|
|
18
15
|
/// Network-specific error type wrapping lower-level I/O errors.
|
|
19
16
|
NetError :: enum(
|
|
20
17
|
/// The remote host refused the connection.
|
|
@@ -34,60 +31,64 @@ NetError :: enum(
|
|
|
34
31
|
/// The network is unreachable.
|
|
35
32
|
NetworkUnreachable,
|
|
36
33
|
/// DNS resolution failed.
|
|
37
|
-
DNSFailed(msg: String),
|
|
34
|
+
DNSFailed(msg : String),
|
|
38
35
|
/// A wrapped lower-level I/O error.
|
|
39
|
-
IO(err: IOError),
|
|
36
|
+
IO(err : IOError),
|
|
40
37
|
/// An unclassified network error with a message.
|
|
41
|
-
Other(msg: String)
|
|
38
|
+
Other(msg : String)
|
|
42
39
|
);
|
|
43
|
-
|
|
44
|
-
|
|
40
|
+
impl(
|
|
41
|
+
NetError,
|
|
45
42
|
/// Convert an `IOError` into the corresponding `NetError` variant.
|
|
46
|
-
from_io : (fn(err: IOError) -> Self)(
|
|
47
|
-
match(
|
|
48
|
-
|
|
49
|
-
.
|
|
50
|
-
.
|
|
51
|
-
.
|
|
52
|
-
.
|
|
53
|
-
.
|
|
54
|
-
.
|
|
55
|
-
.
|
|
56
|
-
|
|
43
|
+
from_io : (fn(err : IOError) -> Self)(
|
|
44
|
+
match(
|
|
45
|
+
err,
|
|
46
|
+
.ConnectionRefused =>.ConnectionRefused,
|
|
47
|
+
.ConnectionReset =>.ConnectionReset,
|
|
48
|
+
.ConnectionAborted =>.ConnectionAborted,
|
|
49
|
+
.AddressInUse =>.AddrInUse,
|
|
50
|
+
.AddressNotAvailable =>.AddrNotAvailable,
|
|
51
|
+
.TimedOut =>.TimedOut,
|
|
52
|
+
.HostUnreachable =>.HostUnreachable,
|
|
53
|
+
.NetworkUnreachable =>.NetworkUnreachable,
|
|
54
|
+
_ =>.IO(err)
|
|
55
|
+
)
|
|
56
|
+
)
|
|
57
|
+
);
|
|
58
|
+
impl(
|
|
59
|
+
NetError,
|
|
60
|
+
ToString(
|
|
61
|
+
to_string : (
|
|
62
|
+
self -> {
|
|
63
|
+
s := match(
|
|
64
|
+
self,
|
|
65
|
+
.ConnectionRefused => String.from("connection refused"),
|
|
66
|
+
.ConnectionReset => String.from("connection reset by peer"),
|
|
67
|
+
.ConnectionAborted => String.from("connection aborted"),
|
|
68
|
+
.AddrInUse => String.from("address already in use"),
|
|
69
|
+
.AddrNotAvailable => String.from("address not available"),
|
|
70
|
+
.TimedOut => String.from("operation timed out"),
|
|
71
|
+
.HostUnreachable => String.from("host unreachable"),
|
|
72
|
+
.NetworkUnreachable => String.from("network unreachable"),
|
|
73
|
+
.DNSFailed(msg) => `DNS lookup failed: ${msg}`,
|
|
74
|
+
.IO(err) => err.to_string(),
|
|
75
|
+
.Other(msg) => msg
|
|
76
|
+
);
|
|
77
|
+
return(s);
|
|
78
|
+
}
|
|
57
79
|
)
|
|
58
80
|
)
|
|
59
81
|
);
|
|
60
|
-
|
|
61
|
-
impl(NetError, ToString(
|
|
62
|
-
to_string : (self -> {
|
|
63
|
-
s := match(self,
|
|
64
|
-
.ConnectionRefused => String.from("connection refused"),
|
|
65
|
-
.ConnectionReset => String.from("connection reset by peer"),
|
|
66
|
-
.ConnectionAborted => String.from("connection aborted"),
|
|
67
|
-
.AddrInUse => String.from("address already in use"),
|
|
68
|
-
.AddrNotAvailable => String.from("address not available"),
|
|
69
|
-
.TimedOut => String.from("operation timed out"),
|
|
70
|
-
.HostUnreachable => String.from("host unreachable"),
|
|
71
|
-
.NetworkUnreachable => String.from("network unreachable"),
|
|
72
|
-
.DNSFailed(msg) => `DNS lookup failed: ${msg}`,
|
|
73
|
-
.IO(err) => err.to_string(),
|
|
74
|
-
.Other(msg) => msg
|
|
75
|
-
);
|
|
76
|
-
return s;
|
|
77
|
-
})
|
|
78
|
-
));
|
|
79
|
-
|
|
80
82
|
impl(NetError, Error());
|
|
81
|
-
|
|
82
|
-
|
|
83
|
+
impl(
|
|
84
|
+
NetError,
|
|
83
85
|
/// Check a raw syscall result and throw on error.
|
|
84
86
|
/// Positive values are returned as-is; negative values are converted to a `NetError` and thrown.
|
|
85
|
-
check : (fn(result: i32, using(exn : Exception)) -> i32)(
|
|
87
|
+
check : (fn(result : i32, using(exn : Exception)) -> i32)(
|
|
86
88
|
cond(
|
|
87
89
|
(result >= i32(0)) => result,
|
|
88
|
-
true => exn.throw(dyn
|
|
90
|
+
true => exn.throw(dyn(Self.from_io(IOError.from_errno(i32(0) - result))))
|
|
89
91
|
)
|
|
90
92
|
)
|
|
91
93
|
);
|
|
92
|
-
|
|
93
|
-
export NetError;
|
|
94
|
+
export(NetError);
|