@shd101wyy/yo 0.1.29 → 0.1.30
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 +3 -3
- package/.github/skills/yo-async-effects/async-effects-recipes.md +19 -11
- package/.github/skills/yo-core-patterns/core-patterns-cheatsheet.md +33 -13
- package/.github/skills/yo-project-workflow/workflow-cheatsheet.md +1 -1
- package/.github/skills/yo-syntax/syntax-cheatsheet.md +59 -21
- package/README.md +4 -3
- package/out/cjs/index.cjs +771 -676
- package/out/cjs/yo-cli.cjs +1003 -898
- package/out/cjs/yo-lsp.cjs +834 -739
- package/out/esm/index.mjs +716 -621
- package/out/types/src/codegen/exprs/async.d.ts +2 -0
- package/out/types/src/codegen/exprs/await.d.ts +1 -0
- package/out/types/src/codegen/exprs/closures.d.ts +4 -0
- package/out/types/src/codegen/functions/context.d.ts +6 -0
- package/out/types/src/env.d.ts +2 -0
- package/out/types/src/evaluator/builtins/pragma.d.ts +9 -0
- package/out/types/src/evaluator/builtins/unsafe.d.ts +8 -0
- package/out/types/src/evaluator/context.d.ts +2 -0
- package/out/types/src/evaluator/index.d.ts +1 -1
- package/out/types/src/evaluator/memory-safety.d.ts +14 -0
- package/out/types/src/evaluator/types/flowability.d.ts +6 -0
- package/out/types/src/expr-traversal.d.ts +1 -0
- package/out/types/src/expr.d.ts +4 -1
- package/out/types/src/public-safe-report.d.ts +19 -0
- package/out/types/src/tests/comptime-ref-gate.test.d.ts +1 -0
- package/out/types/src/tests/pragma-validation.test.d.ts +1 -0
- package/out/types/src/tests/public-safe-report.test.d.ts +1 -0
- package/out/types/src/tests/type-representation-pointer.test.d.ts +1 -0
- package/out/types/src/tests/unsafe-gate.test.d.ts +1 -0
- package/out/types/src/tests/unsafe-report-classify.test.d.ts +1 -0
- package/out/types/src/types/definitions.d.ts +2 -0
- package/out/types/src/types/utils.d.ts +4 -0
- package/out/types/src/unsafe-report.d.ts +29 -0
- package/out/types/src/value.d.ts +1 -0
- package/out/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +1 -1
- package/scripts/add-pragma-for-pointer-decls.ts +134 -0
- package/scripts/add-pragma.ts +58 -0
- package/scripts/migrate-amp-method-calls.ts +186 -0
- package/scripts/migrate-clone-calls.ts +93 -0
- package/scripts/migrate-get-unwrap.ts +166 -0
- package/scripts/migrate-index-patterns.ts +210 -0
- package/scripts/migrate-index-trait.ts +142 -0
- package/scripts/migrate-iterator.ts +150 -0
- package/scripts/migrate-self-ptr.ts +220 -0
- package/scripts/migrate-skip-pragmas.ts +109 -0
- package/scripts/migrate-tostring.ts +134 -0
- package/scripts/trim-pragma.ts +130 -0
- package/scripts/wrap-extern-calls.ts +161 -0
- package/std/alg/hash.yo +3 -2
- package/std/allocator.yo +6 -5
- package/std/async.yo +2 -2
- package/std/collections/array_list.yo +59 -40
- package/std/collections/btree_map.yo +19 -18
- package/std/collections/deque.yo +9 -8
- package/std/collections/hash_map.yo +101 -13
- package/std/collections/hash_set.yo +5 -4
- package/std/collections/linked_list.yo +39 -4
- package/std/collections/ordered_map.yo +3 -3
- package/std/collections/priority_queue.yo +14 -13
- package/std/crypto/md5.yo +2 -1
- package/std/crypto/random.yo +16 -15
- package/std/crypto/sha256.yo +2 -1
- package/std/encoding/base64.yo +14 -14
- package/std/encoding/hex.yo +3 -3
- package/std/encoding/json.yo +59 -10
- package/std/encoding/punycode.yo +24 -23
- package/std/encoding/toml.yo +4 -3
- package/std/encoding/utf16.yo +2 -2
- package/std/env.yo +43 -28
- package/std/error.yo +6 -6
- package/std/fmt/display.yo +2 -2
- package/std/fmt/index.yo +6 -5
- package/std/fmt/to_string.yo +39 -38
- package/std/fmt/writer.yo +9 -8
- package/std/fs/dir.yo +34 -33
- package/std/fs/file.yo +52 -51
- package/std/fs/metadata.yo +10 -9
- package/std/fs/temp.yo +24 -13
- package/std/fs/walker.yo +10 -9
- package/std/gc.yo +1 -0
- package/std/glob.yo +7 -7
- package/std/http/client.yo +15 -14
- package/std/http/http.yo +6 -6
- package/std/http/index.yo +1 -1
- package/std/imm/list.yo +33 -0
- package/std/imm/map.yo +2 -1
- package/std/imm/set.yo +1 -0
- package/std/imm/sorted_map.yo +1 -0
- package/std/imm/sorted_set.yo +1 -0
- package/std/imm/string.yo +27 -23
- package/std/imm/vec.yo +18 -2
- package/std/io/reader.yo +2 -1
- package/std/io/writer.yo +3 -2
- package/std/libc/assert.yo +1 -0
- package/std/libc/ctype.yo +1 -0
- package/std/libc/dirent.yo +1 -0
- package/std/libc/errno.yo +1 -0
- package/std/libc/fcntl.yo +1 -0
- package/std/libc/float.yo +1 -0
- package/std/libc/limits.yo +1 -0
- package/std/libc/math.yo +1 -0
- package/std/libc/signal.yo +1 -0
- package/std/libc/stdatomic.yo +1 -0
- package/std/libc/stdint.yo +1 -0
- package/std/libc/stdio.yo +1 -0
- package/std/libc/stdlib.yo +1 -0
- package/std/libc/string.yo +1 -0
- package/std/libc/sys/stat.yo +1 -0
- package/std/libc/time.yo +1 -0
- package/std/libc/unistd.yo +1 -0
- package/std/libc/wctype.yo +1 -0
- package/std/libc/windows.yo +2 -0
- package/std/log.yo +7 -6
- package/std/net/addr.yo +5 -4
- package/std/net/dns.yo +7 -6
- package/std/net/errors.yo +8 -8
- package/std/net/tcp.yo +19 -18
- package/std/net/udp.yo +13 -12
- package/std/os/signal.yo +3 -3
- package/std/path.yo +1 -0
- package/std/prelude.yo +353 -182
- package/std/process/command.yo +40 -23
- package/std/process/index.yo +2 -1
- package/std/regex/compiler.yo +10 -9
- package/std/regex/index.yo +41 -41
- package/std/regex/match.yo +2 -2
- package/std/regex/parser.yo +21 -21
- package/std/regex/vm.yo +42 -41
- package/std/string/string.yo +95 -40
- package/std/string/string_builder.yo +9 -9
- package/std/string/unicode.yo +50 -49
- package/std/sync/channel.yo +2 -1
- package/std/sync/cond.yo +5 -4
- package/std/sync/mutex.yo +4 -3
- package/std/sys/advise.yo +1 -0
- package/std/sys/bufio/buf_reader.yo +17 -16
- package/std/sys/bufio/buf_writer.yo +10 -9
- package/std/sys/clock.yo +1 -0
- package/std/sys/copy.yo +1 -0
- package/std/sys/dir.yo +10 -9
- package/std/sys/dns.yo +6 -5
- package/std/sys/errors.yo +11 -11
- package/std/sys/events.yo +1 -0
- package/std/sys/externs.yo +38 -37
- package/std/sys/file.yo +17 -16
- package/std/sys/future.yo +4 -3
- package/std/sys/iov.yo +1 -0
- package/std/sys/mmap.yo +1 -0
- package/std/sys/path.yo +1 -0
- package/std/sys/perm.yo +2 -1
- package/std/sys/pipe.yo +1 -0
- package/std/sys/process.yo +5 -4
- package/std/sys/signal.yo +1 -0
- package/std/sys/socketpair.yo +1 -0
- package/std/sys/sockinfo.yo +1 -0
- package/std/sys/statfs.yo +2 -1
- package/std/sys/statx.yo +1 -0
- package/std/sys/sysinfo.yo +1 -0
- package/std/sys/tcp.yo +15 -14
- package/std/sys/temp.yo +1 -0
- package/std/sys/time.yo +2 -1
- package/std/sys/timer.yo +6 -6
- package/std/sys/tty.yo +2 -1
- package/std/sys/udp.yo +13 -12
- package/std/sys/unix.yo +12 -11
- package/std/testing/bench.yo +4 -3
- package/std/thread.yo +7 -6
- package/std/time/datetime.yo +18 -15
- package/std/time/duration.yo +11 -10
- package/std/time/instant.yo +4 -4
- package/std/time/sleep.yo +1 -0
- package/std/url/index.yo +3 -3
- package/std/worker.yo +4 -3
package/std/libc/time.yo
CHANGED
package/std/libc/unistd.yo
CHANGED
package/std/libc/wctype.yo
CHANGED
package/std/libc/windows.yo
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
//! Windows-specific C API bindings.
|
|
2
2
|
//! Only available when compiling for Windows.
|
|
3
3
|
// Windows type aliases (using Yo types that match the C types)
|
|
4
|
+
pragma(Pragma.AllowUnsafe);
|
|
5
|
+
pragma(Pragma.AllowUnsafe);
|
|
4
6
|
DWORD :: ulong; // unsigned long on Windows
|
|
5
7
|
WCHAR :: ushort; // unsigned short (UTF-16 code unit)
|
|
6
8
|
BOOL :: int; // int
|
package/std/log.yo
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
//! Structured logging with configurable level and output destination.
|
|
2
|
+
pragma(Pragma.AllowUnsafe);
|
|
2
3
|
open(import("./string"));
|
|
3
4
|
{ ToString } :: import("./fmt");
|
|
4
5
|
{ fwrite, stdout, stderr, FILE } :: import("./libc/stdio");
|
|
@@ -7,9 +8,9 @@ Level :: enum(Trace, Debug, Info, Warn, Error);
|
|
|
7
8
|
impl(
|
|
8
9
|
Level,
|
|
9
10
|
ToString(
|
|
10
|
-
to_string : (fn(self :
|
|
11
|
+
to_string : (fn(ref(self) : Self) -> String)(
|
|
11
12
|
match(
|
|
12
|
-
self
|
|
13
|
+
self,
|
|
13
14
|
.Trace => String.from("TRACE"),
|
|
14
15
|
.Debug => String.from("DEBUG"),
|
|
15
16
|
.Info => String.from("INFO "),
|
|
@@ -60,7 +61,7 @@ _write_string :: (fn(s : String, dest : *(FILE)) -> unit)({
|
|
|
60
61
|
cond(
|
|
61
62
|
(bytes.len() > usize(0)) => {
|
|
62
63
|
ptr := bytes.ptr().unwrap();
|
|
63
|
-
fwrite(*(void)(ptr), usize(1), bytes.len(), dest);
|
|
64
|
+
unsafe(fwrite(*(void)(ptr), usize(1), bytes.len(), dest));
|
|
64
65
|
},
|
|
65
66
|
true => ()
|
|
66
67
|
);
|
|
@@ -79,11 +80,11 @@ log :: (fn(level : Level, msg : String) -> unit)({
|
|
|
79
80
|
.Stderr => stderr
|
|
80
81
|
);
|
|
81
82
|
// Write "[LEVEL] msg\n"
|
|
82
|
-
fwrite(*(void)(*(u8)("[")), usize(1), usize(1), dest);
|
|
83
|
+
unsafe(fwrite(*(void)(*(u8)("[")), usize(1), usize(1), dest));
|
|
83
84
|
_write_string(level_str, dest);
|
|
84
|
-
fwrite(*(void)(*(u8)("] ")), usize(1), usize(2), dest);
|
|
85
|
+
unsafe(fwrite(*(void)(*(u8)("] ")), usize(1), usize(2), dest));
|
|
85
86
|
_write_string(msg, dest);
|
|
86
|
-
fwrite(*(void)(*(u8)("\n")), usize(1), usize(1), dest);
|
|
87
|
+
unsafe(fwrite(*(void)(*(u8)("\n")), usize(1), usize(1), dest));
|
|
87
88
|
});
|
|
88
89
|
export(log);
|
|
89
90
|
// ============================================================================
|
package/std/net/addr.yo
CHANGED
|
@@ -7,6 +7,7 @@
|
|
|
7
7
|
// { IpAddr, SocketAddr } :: import "std/net/addr";
|
|
8
8
|
//
|
|
9
9
|
// addr := SocketAddr.new(IpAddr.loopback_v4(), u16(8080));
|
|
10
|
+
pragma(Pragma.AllowUnsafe);
|
|
10
11
|
open(import("../string"));
|
|
11
12
|
{ ToString } :: import("../fmt");
|
|
12
13
|
{ Writer } :: import("../fmt/writer");
|
|
@@ -40,7 +41,7 @@ impl(
|
|
|
40
41
|
val := u32(0);
|
|
41
42
|
i := usize(0);
|
|
42
43
|
while(runtime(i < bytes.len()), {
|
|
43
|
-
ch := bytes
|
|
44
|
+
ch := bytes(i);
|
|
44
45
|
cond(
|
|
45
46
|
(ch == _ASCII_DOT) => {
|
|
46
47
|
// '.' separator
|
|
@@ -145,10 +146,10 @@ impl(
|
|
|
145
146
|
self -> {
|
|
146
147
|
return(
|
|
147
148
|
match(
|
|
148
|
-
self
|
|
149
|
+
self,
|
|
149
150
|
.V4(a, b, c, d) => {
|
|
150
151
|
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
|
+
unsafe(snprintf(*(char)(&(buf(usize(0)))), usize(16), "%d.%d.%d.%d", i32(a), i32(b), i32(c), i32(d)));
|
|
152
153
|
String.from_cstr(&(buf(usize(0)))).unwrap()
|
|
153
154
|
},
|
|
154
155
|
.V6(segs) => {
|
|
@@ -205,7 +206,7 @@ impl(
|
|
|
205
206
|
self -> {
|
|
206
207
|
ip_str := self.ip.to_string();
|
|
207
208
|
buf := Array(u8, usize(8)).fill(u8(0));
|
|
208
|
-
snprintf(*(char)(&(buf(usize(0)))), usize(8), "%d", i32(self.port));
|
|
209
|
+
unsafe(snprintf(*(char)(&(buf(usize(0)))), usize(8), "%d", i32(self.port)));
|
|
209
210
|
port_str := String.from_cstr(&(buf(usize(0)))).unwrap();
|
|
210
211
|
return(
|
|
211
212
|
match(
|
package/std/net/dns.yo
CHANGED
|
@@ -9,14 +9,15 @@
|
|
|
9
9
|
// { IpAddr } :: import "std/net/addr";
|
|
10
10
|
// { Exception } :: import "std/error";
|
|
11
11
|
//
|
|
12
|
-
// main :: (fn(io :
|
|
12
|
+
// main :: (fn(io : Io, exn : Exception) -> unit)({
|
|
13
13
|
// addrs := io.await(lookup_host(`localhost`, io), { io, exn });
|
|
14
14
|
// i := usize(0);
|
|
15
15
|
// while runtime((i < addrs.len())), {
|
|
16
|
-
// println(addrs
|
|
16
|
+
// println(addrs(i).to_string());
|
|
17
17
|
// i = (i + usize(1));
|
|
18
18
|
// };
|
|
19
19
|
// });
|
|
20
|
+
pragma(Pragma.AllowUnsafe);
|
|
20
21
|
{ GlobalAllocator } :: import("../allocator");
|
|
21
22
|
{ malloc, free } :: GlobalAllocator;
|
|
22
23
|
{ ArrayList } :: import("../collections/array_list");
|
|
@@ -24,14 +25,14 @@ open(import("../string"));
|
|
|
24
25
|
open(import("../fmt"));
|
|
25
26
|
{ NetError } :: import("./errors");
|
|
26
27
|
{ IpAddr, SocketAddr } :: import("./addr");
|
|
27
|
-
{ Error, AnyError, Exception,
|
|
28
|
+
{ Error, AnyError, Exception, IoExn } :: import("../error");
|
|
28
29
|
IO_dns :: import("../sys/dns");
|
|
29
30
|
{ AF_INET, AF_INET6 } :: import("../sys/socket");
|
|
30
31
|
// ============================================================================
|
|
31
32
|
// DNS lookup
|
|
32
33
|
// ============================================================================
|
|
33
34
|
/// Resolve a hostname to a list of IP addresses.
|
|
34
|
-
lookup_host :: (fn(host : String, io :
|
|
35
|
+
lookup_host :: (fn(host : String, io : Io) -> Impl(Future(ArrayList(IpAddr), IoExn)))(
|
|
35
36
|
io.async((e) => {
|
|
36
37
|
host_cstr_bytes := host.to_cstr();
|
|
37
38
|
host_cstr := host_cstr_bytes.ptr().unwrap();
|
|
@@ -82,13 +83,13 @@ lookup_host :: (fn(host : String, io : IO) -> Impl(Future(ArrayList(IpAddr), IOE
|
|
|
82
83
|
})
|
|
83
84
|
);
|
|
84
85
|
/// Resolve a hostname and port to a list of socket addresses.
|
|
85
|
-
resolve :: (fn(host : String, port : u16, io :
|
|
86
|
+
resolve :: (fn(host : String, port : u16, io : Io) -> Impl(Future(ArrayList(SocketAddr), IoExn)))(
|
|
86
87
|
io.async((e) => {
|
|
87
88
|
addrs := e.io.await(lookup_host(host, io), e);
|
|
88
89
|
result := ArrayList(SocketAddr).new();
|
|
89
90
|
i := usize(0);
|
|
90
91
|
while(runtime(i < addrs.len()), {
|
|
91
|
-
ip := addrs
|
|
92
|
+
ip := addrs(i);
|
|
92
93
|
result.push(SocketAddr.new(ip, port));
|
|
93
94
|
i = (i + usize(1));
|
|
94
95
|
});
|
package/std/net/errors.yo
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
//! Network error types.
|
|
2
2
|
// std/net/errors.yo - Network error type
|
|
3
3
|
//
|
|
4
|
-
// Higher-level network error that wraps
|
|
4
|
+
// Higher-level network error that wraps IoError with network-specific variants.
|
|
5
5
|
//
|
|
6
6
|
// Example:
|
|
7
7
|
// { NetError } :: import "std/net/errors";
|
|
8
8
|
open(import("../string"));
|
|
9
|
-
{
|
|
9
|
+
{ IoError } :: import("../sys/errors");
|
|
10
10
|
{ ToString } :: import("../fmt");
|
|
11
11
|
{ Error, AnyError, Exception } :: import("../error");
|
|
12
12
|
// ============================================================================
|
|
@@ -33,14 +33,14 @@ NetError :: enum(
|
|
|
33
33
|
/// DNS resolution failed.
|
|
34
34
|
DNSFailed(msg : String),
|
|
35
35
|
/// A wrapped lower-level I/O error.
|
|
36
|
-
|
|
36
|
+
Io(err : IoError),
|
|
37
37
|
/// An unclassified network error with a message.
|
|
38
38
|
Other(msg : String)
|
|
39
39
|
);
|
|
40
40
|
impl(
|
|
41
41
|
NetError,
|
|
42
|
-
/// Convert an `
|
|
43
|
-
from_io : (fn(err :
|
|
42
|
+
/// Convert an `IoError` into the corresponding `NetError` variant.
|
|
43
|
+
from_io : (fn(err : IoError) -> Self)(
|
|
44
44
|
match(
|
|
45
45
|
err,
|
|
46
46
|
.ConnectionRefused =>.ConnectionRefused,
|
|
@@ -51,7 +51,7 @@ impl(
|
|
|
51
51
|
.TimedOut =>.TimedOut,
|
|
52
52
|
.HostUnreachable =>.HostUnreachable,
|
|
53
53
|
.NetworkUnreachable =>.NetworkUnreachable,
|
|
54
|
-
_ =>.
|
|
54
|
+
_ =>.Io(err)
|
|
55
55
|
)
|
|
56
56
|
)
|
|
57
57
|
);
|
|
@@ -71,7 +71,7 @@ impl(
|
|
|
71
71
|
.HostUnreachable => String.from("host unreachable"),
|
|
72
72
|
.NetworkUnreachable => String.from("network unreachable"),
|
|
73
73
|
.DNSFailed(msg) => `DNS lookup failed: ${msg}`,
|
|
74
|
-
.
|
|
74
|
+
.Io(err) => err.to_string(),
|
|
75
75
|
.Other(msg) => msg
|
|
76
76
|
);
|
|
77
77
|
return(s);
|
|
@@ -87,7 +87,7 @@ impl(
|
|
|
87
87
|
check : (fn(result : i32, exn : Exception) -> i32)(
|
|
88
88
|
cond(
|
|
89
89
|
(result >= i32(0)) => result,
|
|
90
|
-
true => exn.throw(dyn(Self.from_io(
|
|
90
|
+
true => exn.throw(dyn(Self.from_io(IoError.from_errno(i32(0) - result))))
|
|
91
91
|
)
|
|
92
92
|
)
|
|
93
93
|
);
|
package/std/net/tcp.yo
CHANGED
|
@@ -9,21 +9,22 @@
|
|
|
9
9
|
// { SocketAddr, IpAddr } :: import "std/net/addr";
|
|
10
10
|
// { Exception } :: import "std/error";
|
|
11
11
|
//
|
|
12
|
-
// main :: (fn(io :
|
|
12
|
+
// main :: (fn(io : Io, exn : Exception) -> unit)({
|
|
13
13
|
// addr := SocketAddr.new(IpAddr.any_v4(), u16(8080));
|
|
14
14
|
// listener := io.await(TcpListener.bind(addr, io), { io, exn });
|
|
15
15
|
// stream := io.await(listener.accept(io), { io, exn });
|
|
16
16
|
// // ...
|
|
17
17
|
// });
|
|
18
|
+
pragma(Pragma.AllowUnsafe);
|
|
18
19
|
{ GlobalAllocator } :: import("../allocator");
|
|
19
20
|
{ malloc, free } :: GlobalAllocator;
|
|
20
21
|
{ ArrayList } :: import("../collections/array_list");
|
|
21
22
|
open(import("../string"));
|
|
22
23
|
open(import("../fmt"));
|
|
23
24
|
{ NetError } :: import("./errors");
|
|
24
|
-
{
|
|
25
|
+
{ IoError } :: import("../sys/errors");
|
|
25
26
|
{ IpAddr, SocketAddr } :: import("./addr");
|
|
26
|
-
{ Error, AnyError, Exception,
|
|
27
|
+
{ Error, AnyError, Exception, IoExn } :: import("../error");
|
|
27
28
|
IO_tcp :: import("../sys/tcp");
|
|
28
29
|
{ AF_INET, AF_INET6, SOCK_STREAM, SOL_SOCKET, SO_REUSEADDR, IPPROTO_TCP, TCP_NODELAY, SO_KEEPALIVE } :: import("../sys/socket");
|
|
29
30
|
// ============================================================================
|
|
@@ -37,7 +38,7 @@ _make_sockaddr :: (fn(addr : SocketAddr) -> IO_tcp.SockAddr)(
|
|
|
37
38
|
// Build the IP string "a.b.c.d"
|
|
38
39
|
{ snprintf } :: import("../libc/stdio");
|
|
39
40
|
buf := Array(u8, usize(16)).fill(u8(0));
|
|
40
|
-
snprintf(*(char)(&(buf(usize(0)))), usize(16), "%d.%d.%d.%d", i32(a), i32(b), i32(c), i32(d));
|
|
41
|
+
unsafe(snprintf(*(char)(&(buf(usize(0)))), usize(16), "%d.%d.%d.%d", i32(a), i32(b), i32(c), i32(d)));
|
|
41
42
|
IO_tcp.make_sockaddr_in(&(buf(usize(0))), addr.port)
|
|
42
43
|
},
|
|
43
44
|
.V6(_) =>
|
|
@@ -46,7 +47,7 @@ _make_sockaddr :: (fn(addr : SocketAddr) -> IO_tcp.SockAddr)(
|
|
|
46
47
|
)
|
|
47
48
|
);
|
|
48
49
|
_throw_io :: (fn(errno_val : i32, exn : Exception) -> unit)(
|
|
49
|
-
exn.throw(dyn(NetError.from_io(
|
|
50
|
+
exn.throw(dyn(NetError.from_io(IoError.from_errno(errno_val))))
|
|
50
51
|
);
|
|
51
52
|
// ============================================================================
|
|
52
53
|
// TcpStream (declared before TcpListener so accept can reference it)
|
|
@@ -69,7 +70,7 @@ TcpListener :: object(
|
|
|
69
70
|
impl(
|
|
70
71
|
TcpListener,
|
|
71
72
|
/// Bind to a socket address and start listening.
|
|
72
|
-
bind : (fn(addr : SocketAddr, io :
|
|
73
|
+
bind : (fn(addr : SocketAddr, io : Io) -> Impl(Future(TcpListener, IoExn)))(
|
|
73
74
|
io.async((e) => {
|
|
74
75
|
domain := cond(
|
|
75
76
|
addr.ip.is_v4() => AF_INET,
|
|
@@ -106,7 +107,7 @@ impl(
|
|
|
106
107
|
})
|
|
107
108
|
),
|
|
108
109
|
/// Accept an incoming connection, returning a new `TcpStream`.
|
|
109
|
-
accept : (fn(self : Self, io :
|
|
110
|
+
accept : (fn(self : Self, io : Io) -> Impl(Future(TcpStream, IoExn)))({
|
|
110
111
|
fd := self._fd;
|
|
111
112
|
io.async((e) => {
|
|
112
113
|
// Allocate space for the peer address
|
|
@@ -141,7 +142,7 @@ impl(
|
|
|
141
142
|
self._local_addr
|
|
142
143
|
),
|
|
143
144
|
/// Close the listener socket.
|
|
144
|
-
close : (fn(self : Self, io :
|
|
145
|
+
close : (fn(self : Self, io : Io) -> Impl(Future(unit, IoExn)))({
|
|
145
146
|
fd := self._fd;
|
|
146
147
|
io.async((e) => {
|
|
147
148
|
cond(
|
|
@@ -186,7 +187,7 @@ export(TcpListener);
|
|
|
186
187
|
impl(
|
|
187
188
|
TcpStream,
|
|
188
189
|
/// Connect to a remote address, returning a new `TcpStream`.
|
|
189
|
-
connect : (fn(addr : SocketAddr, io :
|
|
190
|
+
connect : (fn(addr : SocketAddr, io : Io) -> Impl(Future(TcpStream, IoExn)))(
|
|
190
191
|
io.async((e) => {
|
|
191
192
|
domain := cond(
|
|
192
193
|
addr.ip.is_v4() => AF_INET,
|
|
@@ -215,7 +216,7 @@ impl(
|
|
|
215
216
|
),
|
|
216
217
|
/// Read bytes from the stream into `buf`.
|
|
217
218
|
/// Returns the number of bytes read, or 0 if the peer closed the connection.
|
|
218
|
-
read : (fn(self : Self, buf : *(u8), size : usize, io :
|
|
219
|
+
read : (fn(self : Self, buf : *(u8), size : usize, io : Io) -> Impl(Future(i32, IoExn)))({
|
|
219
220
|
fd := self._fd;
|
|
220
221
|
io.async((e) => {
|
|
221
222
|
r := e.io.await(IO_tcp.recv(fd, buf, size, i32(0)), e.io);
|
|
@@ -224,7 +225,7 @@ impl(
|
|
|
224
225
|
}),
|
|
225
226
|
/// Write a `str` to the stream.
|
|
226
227
|
/// Returns the number of bytes written.
|
|
227
|
-
write_str : (fn(self : Self, data : str, io :
|
|
228
|
+
write_str : (fn(self : Self, data : str, io : Io) -> Impl(Future(i32, IoExn)))({
|
|
228
229
|
fd := self._fd;
|
|
229
230
|
s := String.from(data);
|
|
230
231
|
s_bytes := s.as_bytes();
|
|
@@ -235,7 +236,7 @@ impl(
|
|
|
235
236
|
}),
|
|
236
237
|
/// Write a `String` to the stream.
|
|
237
238
|
/// Returns the number of bytes written.
|
|
238
|
-
write_string : (fn(self : Self, data : String, io :
|
|
239
|
+
write_string : (fn(self : Self, data : String, io : Io) -> Impl(Future(i32, IoExn)))({
|
|
239
240
|
fd := self._fd;
|
|
240
241
|
data_bytes := data.as_bytes();
|
|
241
242
|
io.async((e) => {
|
|
@@ -245,7 +246,7 @@ impl(
|
|
|
245
246
|
}),
|
|
246
247
|
/// Write raw bytes from an `ArrayList(u8)` to the stream.
|
|
247
248
|
/// Returns the number of bytes written.
|
|
248
|
-
write_bytes : (fn(self : Self, data : ArrayList(u8), io :
|
|
249
|
+
write_bytes : (fn(self : Self, data : ArrayList(u8), io : Io) -> Impl(Future(i32, IoExn)))({
|
|
249
250
|
fd := self._fd;
|
|
250
251
|
io.async((e) => {
|
|
251
252
|
r := e.io.await(IO_tcp.send(fd, data.ptr().unwrap(), data.len(), i32(0)), e.io);
|
|
@@ -253,7 +254,7 @@ impl(
|
|
|
253
254
|
})
|
|
254
255
|
}),
|
|
255
256
|
/// Read all available data from the stream into a byte list.
|
|
256
|
-
read_bytes : (fn(self : Self, io :
|
|
257
|
+
read_bytes : (fn(self : Self, io : Io) -> Impl(Future(ArrayList(u8), IoExn)))({
|
|
257
258
|
fd := self._fd;
|
|
258
259
|
io.async((e) => {
|
|
259
260
|
buf_size := usize(4096);
|
|
@@ -286,7 +287,7 @@ impl(
|
|
|
286
287
|
}),
|
|
287
288
|
/// Shut down part or all of the connection.
|
|
288
289
|
/// `how`: 0 = read, 1 = write, 2 = both.
|
|
289
|
-
shutdown : (fn(self : Self, how : i32, io :
|
|
290
|
+
shutdown : (fn(self : Self, how : i32, io : Io) -> Impl(Future(unit, IoExn)))({
|
|
290
291
|
fd := self._fd;
|
|
291
292
|
io.async((e) => {
|
|
292
293
|
r := e.io.await(IO_tcp.shutdown(fd, how), e.io);
|
|
@@ -299,7 +300,7 @@ impl(
|
|
|
299
300
|
})
|
|
300
301
|
}),
|
|
301
302
|
/// Close the stream.
|
|
302
|
-
close : (fn(self : Self, io :
|
|
303
|
+
close : (fn(self : Self, io : Io) -> Impl(Future(unit, IoExn)))({
|
|
303
304
|
fd := self._fd;
|
|
304
305
|
io.async((e) => {
|
|
305
306
|
cond(
|
|
@@ -326,7 +327,7 @@ impl(
|
|
|
326
327
|
self._fd
|
|
327
328
|
),
|
|
328
329
|
/// Enable or disable TCP_NODELAY (disables Nagle's algorithm).
|
|
329
|
-
set_nodelay : (fn(self : Self, nodelay : bool, io :
|
|
330
|
+
set_nodelay : (fn(self : Self, nodelay : bool, io : Io) -> Impl(Future(unit, IoExn)))({
|
|
330
331
|
sock_fd := self._fd;
|
|
331
332
|
io.async((e) => {
|
|
332
333
|
val := cond(nodelay => i32(1), true => i32(0));
|
|
@@ -341,7 +342,7 @@ impl(
|
|
|
341
342
|
})
|
|
342
343
|
}),
|
|
343
344
|
/// Enable or disable SO_KEEPALIVE on the socket.
|
|
344
|
-
set_keepalive : (fn(self : Self, enabled : bool, io :
|
|
345
|
+
set_keepalive : (fn(self : Self, enabled : bool, io : Io) -> Impl(Future(unit, IoExn)))({
|
|
345
346
|
sock_fd := self._fd;
|
|
346
347
|
io.async((e) => {
|
|
347
348
|
val := cond(enabled => i32(1), true => i32(0));
|
package/std/net/udp.yo
CHANGED
|
@@ -9,20 +9,21 @@
|
|
|
9
9
|
// { SocketAddr, IpAddr } :: import "std/net/addr";
|
|
10
10
|
// { Exception } :: import "std/error";
|
|
11
11
|
//
|
|
12
|
-
// main :: (fn(io :
|
|
12
|
+
// main :: (fn(io : Io, exn : Exception) -> unit)({
|
|
13
13
|
// addr := SocketAddr.new(IpAddr.loopback_v4(), u16(9999));
|
|
14
14
|
// sock := io.await(UdpSocket.bind(addr, io), { io, exn });
|
|
15
15
|
// io.await(sock.close(io), { io, exn });
|
|
16
16
|
// });
|
|
17
|
+
pragma(Pragma.AllowUnsafe);
|
|
17
18
|
{ GlobalAllocator } :: import("../allocator");
|
|
18
19
|
{ malloc, free } :: GlobalAllocator;
|
|
19
20
|
{ ArrayList } :: import("../collections/array_list");
|
|
20
21
|
open(import("../string"));
|
|
21
22
|
open(import("../fmt"));
|
|
22
23
|
{ NetError } :: import("./errors");
|
|
23
|
-
{
|
|
24
|
+
{ IoError } :: import("../sys/errors");
|
|
24
25
|
{ IpAddr, SocketAddr } :: import("./addr");
|
|
25
|
-
{ Error, AnyError, Exception,
|
|
26
|
+
{ Error, AnyError, Exception, IoExn } :: import("../error");
|
|
26
27
|
IO_udp :: import("../sys/udp");
|
|
27
28
|
IO_tcp :: import("../sys/tcp");
|
|
28
29
|
{ AF_INET, AF_INET6, SOCK_DGRAM, SOL_SOCKET, SO_BROADCAST } :: import("../sys/socket");
|
|
@@ -35,7 +36,7 @@ _make_sockaddr :: (fn(addr : SocketAddr) -> IO_tcp.SockAddr)(
|
|
|
35
36
|
addr.ip,
|
|
36
37
|
.V4(a, b, c, d) => {
|
|
37
38
|
buf := Array(u8, usize(16)).fill(u8(0));
|
|
38
|
-
snprintf(*(char)(&(buf(usize(0)))), usize(16), "%d.%d.%d.%d", i32(a), i32(b), i32(c), i32(d));
|
|
39
|
+
unsafe(snprintf(*(char)(&(buf(usize(0)))), usize(16), "%d.%d.%d.%d", i32(a), i32(b), i32(c), i32(d)));
|
|
39
40
|
IO_tcp.make_sockaddr_in(&(buf(usize(0))), addr.port)
|
|
40
41
|
},
|
|
41
42
|
.V6(_) =>
|
|
@@ -43,7 +44,7 @@ _make_sockaddr :: (fn(addr : SocketAddr) -> IO_tcp.SockAddr)(
|
|
|
43
44
|
)
|
|
44
45
|
);
|
|
45
46
|
_throw_net_io :: (fn(errno_val : i32, exn : Exception) -> unit)(
|
|
46
|
-
exn.throw(dyn(NetError.from_io(
|
|
47
|
+
exn.throw(dyn(NetError.from_io(IoError.from_errno(errno_val))))
|
|
47
48
|
);
|
|
48
49
|
// ============================================================================
|
|
49
50
|
// UdpSocket
|
|
@@ -57,7 +58,7 @@ UdpSocket :: object(
|
|
|
57
58
|
impl(
|
|
58
59
|
UdpSocket,
|
|
59
60
|
/// Create and bind a UDP socket to the given address.
|
|
60
|
-
bind : (fn(addr : SocketAddr, io :
|
|
61
|
+
bind : (fn(addr : SocketAddr, io : Io) -> Impl(Future(UdpSocket, IoExn)))(
|
|
61
62
|
io.async((e) => {
|
|
62
63
|
domain := cond(
|
|
63
64
|
addr.ip.is_v4() => AF_INET,
|
|
@@ -80,7 +81,7 @@ impl(
|
|
|
80
81
|
),
|
|
81
82
|
/// Send a datagram to a specific address.
|
|
82
83
|
/// Returns the number of bytes sent.
|
|
83
|
-
send_to : (fn(self : Self, data : ArrayList(u8), addr : SocketAddr, io :
|
|
84
|
+
send_to : (fn(self : Self, data : ArrayList(u8), addr : SocketAddr, io : Io) -> Impl(Future(i32, IoExn)))({
|
|
84
85
|
fd := self._fd;
|
|
85
86
|
io.async((e) => {
|
|
86
87
|
saddr := _make_sockaddr(addr);
|
|
@@ -91,7 +92,7 @@ impl(
|
|
|
91
92
|
}),
|
|
92
93
|
/// Receive a datagram into the provided buffer.
|
|
93
94
|
/// Returns the number of bytes received.
|
|
94
|
-
recv : (fn(self : Self, buf : *(u8), size : usize, io :
|
|
95
|
+
recv : (fn(self : Self, buf : *(u8), size : usize, io : Io) -> Impl(Future(i32, IoExn)))({
|
|
95
96
|
fd := self._fd;
|
|
96
97
|
io.async((e) => {
|
|
97
98
|
r := e.io.await(IO_udp.recv(fd, buf, size, i32(0)), e.io);
|
|
@@ -100,7 +101,7 @@ impl(
|
|
|
100
101
|
}),
|
|
101
102
|
/// Receive a datagram and retrieve the sender's address.
|
|
102
103
|
/// Returns the number of bytes received.
|
|
103
|
-
recv_from : (fn(self : Self, buf : *(u8), size : usize, src_addr : *(u8), src_addr_len : *(u32), io :
|
|
104
|
+
recv_from : (fn(self : Self, buf : *(u8), size : usize, src_addr : *(u8), src_addr_len : *(u32), io : Io) -> Impl(Future(i32, IoExn)))({
|
|
104
105
|
fd := self._fd;
|
|
105
106
|
io.async((e) => {
|
|
106
107
|
r := e.io.await(IO_udp.recvfrom(fd, buf, size, i32(0), src_addr, src_addr_len), e.io);
|
|
@@ -108,7 +109,7 @@ impl(
|
|
|
108
109
|
})
|
|
109
110
|
}),
|
|
110
111
|
/// Send data on a connected socket (requires prior connect).
|
|
111
|
-
send : (fn(self : Self, data : ArrayList(u8), io :
|
|
112
|
+
send : (fn(self : Self, data : ArrayList(u8), io : Io) -> Impl(Future(i32, IoExn)))({
|
|
112
113
|
fd := self._fd;
|
|
113
114
|
io.async((e) => {
|
|
114
115
|
r := e.io.await(IO_udp.send(fd, data.ptr().unwrap(), data.len(), i32(0)), e.io);
|
|
@@ -116,7 +117,7 @@ impl(
|
|
|
116
117
|
})
|
|
117
118
|
}),
|
|
118
119
|
/// Close the socket.
|
|
119
|
-
close : (fn(self : Self, io :
|
|
120
|
+
close : (fn(self : Self, io : Io) -> Impl(Future(unit, IoExn)))({
|
|
120
121
|
fd := self._fd;
|
|
121
122
|
io.async((e) => {
|
|
122
123
|
cond(
|
|
@@ -135,7 +136,7 @@ impl(
|
|
|
135
136
|
})
|
|
136
137
|
}),
|
|
137
138
|
/// Enable or disable SO_BROADCAST on the socket.
|
|
138
|
-
set_broadcast : (fn(self : Self, enabled : bool, io :
|
|
139
|
+
set_broadcast : (fn(self : Self, enabled : bool, io : Io) -> Impl(Future(unit, IoExn)))({
|
|
139
140
|
fd := self._fd;
|
|
140
141
|
io.async((e) => {
|
|
141
142
|
val := cond(enabled => i32(1), true => i32(0));
|
package/std/os/signal.yo
CHANGED
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
//! handler :: (fn(data: *(u8)) -> unit)({ println("Caught SIGINT"); });
|
|
11
11
|
//! on_signal(.Interrupt, handler);
|
|
12
12
|
//! ```
|
|
13
|
-
{
|
|
13
|
+
{ IoError } :: import("../sys/errors");
|
|
14
14
|
{ on_signal : _on_signal, off_signal : _off_signal, SignalHandler } :: import("../sys/signal");
|
|
15
15
|
{ platform, Platform } :: import("../process");
|
|
16
16
|
{ Error, AnyError, Exception } :: import("../error");
|
|
@@ -83,7 +83,7 @@ on_signal :: (fn(sig : Signal, handler : SignalHandler, exn : Exception) -> unit
|
|
|
83
83
|
r := _on_signal(num, handler);
|
|
84
84
|
cond(
|
|
85
85
|
(r < i32(0)) => {
|
|
86
|
-
exn.throw(dyn(
|
|
86
|
+
exn.throw(dyn(IoError.from_errno(i32(0) - r)));
|
|
87
87
|
},
|
|
88
88
|
true => ()
|
|
89
89
|
);
|
|
@@ -94,7 +94,7 @@ off_signal :: (fn(sig : Signal, exn : Exception) -> unit)({
|
|
|
94
94
|
r := _off_signal(num);
|
|
95
95
|
cond(
|
|
96
96
|
(r < i32(0)) => {
|
|
97
|
-
exn.throw(dyn(
|
|
97
|
+
exn.throw(dyn(IoError.from_errno(i32(0) - r)));
|
|
98
98
|
},
|
|
99
99
|
true => ()
|
|
100
100
|
);
|