@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.
Files changed (174) hide show
  1. package/.github/skills/yo-async-effects/SKILL.md +3 -3
  2. package/.github/skills/yo-async-effects/async-effects-recipes.md +19 -11
  3. package/.github/skills/yo-core-patterns/core-patterns-cheatsheet.md +33 -13
  4. package/.github/skills/yo-project-workflow/workflow-cheatsheet.md +1 -1
  5. package/.github/skills/yo-syntax/syntax-cheatsheet.md +59 -21
  6. package/README.md +4 -3
  7. package/out/cjs/index.cjs +771 -676
  8. package/out/cjs/yo-cli.cjs +1003 -898
  9. package/out/cjs/yo-lsp.cjs +834 -739
  10. package/out/esm/index.mjs +716 -621
  11. package/out/types/src/codegen/exprs/async.d.ts +2 -0
  12. package/out/types/src/codegen/exprs/await.d.ts +1 -0
  13. package/out/types/src/codegen/exprs/closures.d.ts +4 -0
  14. package/out/types/src/codegen/functions/context.d.ts +6 -0
  15. package/out/types/src/env.d.ts +2 -0
  16. package/out/types/src/evaluator/builtins/pragma.d.ts +9 -0
  17. package/out/types/src/evaluator/builtins/unsafe.d.ts +8 -0
  18. package/out/types/src/evaluator/context.d.ts +2 -0
  19. package/out/types/src/evaluator/index.d.ts +1 -1
  20. package/out/types/src/evaluator/memory-safety.d.ts +14 -0
  21. package/out/types/src/evaluator/types/flowability.d.ts +6 -0
  22. package/out/types/src/expr-traversal.d.ts +1 -0
  23. package/out/types/src/expr.d.ts +4 -1
  24. package/out/types/src/public-safe-report.d.ts +19 -0
  25. package/out/types/src/tests/comptime-ref-gate.test.d.ts +1 -0
  26. package/out/types/src/tests/pragma-validation.test.d.ts +1 -0
  27. package/out/types/src/tests/public-safe-report.test.d.ts +1 -0
  28. package/out/types/src/tests/type-representation-pointer.test.d.ts +1 -0
  29. package/out/types/src/tests/unsafe-gate.test.d.ts +1 -0
  30. package/out/types/src/tests/unsafe-report-classify.test.d.ts +1 -0
  31. package/out/types/src/types/definitions.d.ts +2 -0
  32. package/out/types/src/types/utils.d.ts +4 -0
  33. package/out/types/src/unsafe-report.d.ts +29 -0
  34. package/out/types/src/value.d.ts +1 -0
  35. package/out/types/tsconfig.tsbuildinfo +1 -1
  36. package/package.json +1 -1
  37. package/scripts/add-pragma-for-pointer-decls.ts +134 -0
  38. package/scripts/add-pragma.ts +58 -0
  39. package/scripts/migrate-amp-method-calls.ts +186 -0
  40. package/scripts/migrate-clone-calls.ts +93 -0
  41. package/scripts/migrate-get-unwrap.ts +166 -0
  42. package/scripts/migrate-index-patterns.ts +210 -0
  43. package/scripts/migrate-index-trait.ts +142 -0
  44. package/scripts/migrate-iterator.ts +150 -0
  45. package/scripts/migrate-self-ptr.ts +220 -0
  46. package/scripts/migrate-skip-pragmas.ts +109 -0
  47. package/scripts/migrate-tostring.ts +134 -0
  48. package/scripts/trim-pragma.ts +130 -0
  49. package/scripts/wrap-extern-calls.ts +161 -0
  50. package/std/alg/hash.yo +3 -2
  51. package/std/allocator.yo +6 -5
  52. package/std/async.yo +2 -2
  53. package/std/collections/array_list.yo +59 -40
  54. package/std/collections/btree_map.yo +19 -18
  55. package/std/collections/deque.yo +9 -8
  56. package/std/collections/hash_map.yo +101 -13
  57. package/std/collections/hash_set.yo +5 -4
  58. package/std/collections/linked_list.yo +39 -4
  59. package/std/collections/ordered_map.yo +3 -3
  60. package/std/collections/priority_queue.yo +14 -13
  61. package/std/crypto/md5.yo +2 -1
  62. package/std/crypto/random.yo +16 -15
  63. package/std/crypto/sha256.yo +2 -1
  64. package/std/encoding/base64.yo +14 -14
  65. package/std/encoding/hex.yo +3 -3
  66. package/std/encoding/json.yo +59 -10
  67. package/std/encoding/punycode.yo +24 -23
  68. package/std/encoding/toml.yo +4 -3
  69. package/std/encoding/utf16.yo +2 -2
  70. package/std/env.yo +43 -28
  71. package/std/error.yo +6 -6
  72. package/std/fmt/display.yo +2 -2
  73. package/std/fmt/index.yo +6 -5
  74. package/std/fmt/to_string.yo +39 -38
  75. package/std/fmt/writer.yo +9 -8
  76. package/std/fs/dir.yo +34 -33
  77. package/std/fs/file.yo +52 -51
  78. package/std/fs/metadata.yo +10 -9
  79. package/std/fs/temp.yo +24 -13
  80. package/std/fs/walker.yo +10 -9
  81. package/std/gc.yo +1 -0
  82. package/std/glob.yo +7 -7
  83. package/std/http/client.yo +15 -14
  84. package/std/http/http.yo +6 -6
  85. package/std/http/index.yo +1 -1
  86. package/std/imm/list.yo +33 -0
  87. package/std/imm/map.yo +2 -1
  88. package/std/imm/set.yo +1 -0
  89. package/std/imm/sorted_map.yo +1 -0
  90. package/std/imm/sorted_set.yo +1 -0
  91. package/std/imm/string.yo +27 -23
  92. package/std/imm/vec.yo +18 -2
  93. package/std/io/reader.yo +2 -1
  94. package/std/io/writer.yo +3 -2
  95. package/std/libc/assert.yo +1 -0
  96. package/std/libc/ctype.yo +1 -0
  97. package/std/libc/dirent.yo +1 -0
  98. package/std/libc/errno.yo +1 -0
  99. package/std/libc/fcntl.yo +1 -0
  100. package/std/libc/float.yo +1 -0
  101. package/std/libc/limits.yo +1 -0
  102. package/std/libc/math.yo +1 -0
  103. package/std/libc/signal.yo +1 -0
  104. package/std/libc/stdatomic.yo +1 -0
  105. package/std/libc/stdint.yo +1 -0
  106. package/std/libc/stdio.yo +1 -0
  107. package/std/libc/stdlib.yo +1 -0
  108. package/std/libc/string.yo +1 -0
  109. package/std/libc/sys/stat.yo +1 -0
  110. package/std/libc/time.yo +1 -0
  111. package/std/libc/unistd.yo +1 -0
  112. package/std/libc/wctype.yo +1 -0
  113. package/std/libc/windows.yo +2 -0
  114. package/std/log.yo +7 -6
  115. package/std/net/addr.yo +5 -4
  116. package/std/net/dns.yo +7 -6
  117. package/std/net/errors.yo +8 -8
  118. package/std/net/tcp.yo +19 -18
  119. package/std/net/udp.yo +13 -12
  120. package/std/os/signal.yo +3 -3
  121. package/std/path.yo +1 -0
  122. package/std/prelude.yo +353 -182
  123. package/std/process/command.yo +40 -23
  124. package/std/process/index.yo +2 -1
  125. package/std/regex/compiler.yo +10 -9
  126. package/std/regex/index.yo +41 -41
  127. package/std/regex/match.yo +2 -2
  128. package/std/regex/parser.yo +21 -21
  129. package/std/regex/vm.yo +42 -41
  130. package/std/string/string.yo +95 -40
  131. package/std/string/string_builder.yo +9 -9
  132. package/std/string/unicode.yo +50 -49
  133. package/std/sync/channel.yo +2 -1
  134. package/std/sync/cond.yo +5 -4
  135. package/std/sync/mutex.yo +4 -3
  136. package/std/sys/advise.yo +1 -0
  137. package/std/sys/bufio/buf_reader.yo +17 -16
  138. package/std/sys/bufio/buf_writer.yo +10 -9
  139. package/std/sys/clock.yo +1 -0
  140. package/std/sys/copy.yo +1 -0
  141. package/std/sys/dir.yo +10 -9
  142. package/std/sys/dns.yo +6 -5
  143. package/std/sys/errors.yo +11 -11
  144. package/std/sys/events.yo +1 -0
  145. package/std/sys/externs.yo +38 -37
  146. package/std/sys/file.yo +17 -16
  147. package/std/sys/future.yo +4 -3
  148. package/std/sys/iov.yo +1 -0
  149. package/std/sys/mmap.yo +1 -0
  150. package/std/sys/path.yo +1 -0
  151. package/std/sys/perm.yo +2 -1
  152. package/std/sys/pipe.yo +1 -0
  153. package/std/sys/process.yo +5 -4
  154. package/std/sys/signal.yo +1 -0
  155. package/std/sys/socketpair.yo +1 -0
  156. package/std/sys/sockinfo.yo +1 -0
  157. package/std/sys/statfs.yo +2 -1
  158. package/std/sys/statx.yo +1 -0
  159. package/std/sys/sysinfo.yo +1 -0
  160. package/std/sys/tcp.yo +15 -14
  161. package/std/sys/temp.yo +1 -0
  162. package/std/sys/time.yo +2 -1
  163. package/std/sys/timer.yo +6 -6
  164. package/std/sys/tty.yo +2 -1
  165. package/std/sys/udp.yo +13 -12
  166. package/std/sys/unix.yo +12 -11
  167. package/std/testing/bench.yo +4 -3
  168. package/std/thread.yo +7 -6
  169. package/std/time/datetime.yo +18 -15
  170. package/std/time/duration.yo +11 -10
  171. package/std/time/instant.yo +4 -4
  172. package/std/time/sleep.yo +1 -0
  173. package/std/url/index.yo +3 -3
  174. package/std/worker.yo +4 -3
@@ -1,10 +1,11 @@
1
1
  //! ToString trait and implementations for all primitive types.
2
+ pragma(Pragma.AllowUnsafe);
2
3
  { String, rune } :: import("../string");
3
4
  { snprintf } :: import("../libc/stdio");
4
5
  /// Trait for converting values to their `String` representation.
5
6
  /// Types implementing this trait can be used with `println`, `print`, and template strings.
6
7
  ToString :: trait(
7
- to_string : (fn(self : *(Self)) -> String)
8
+ to_string : (fn(ref(self) : Self) -> String)
8
9
  );
9
10
  export(ToString);
10
11
  // === Helper function for integer conversion ===
@@ -16,10 +17,10 @@ _INT_BUFFER_SIZE :: usize(24);
16
17
  impl(
17
18
  i8,
18
19
  ToString(
19
- to_string : (fn(self : *(Self)) -> String)({
20
+ to_string : (fn(ref(self) : Self) -> String)({
20
21
  buffer := Array(u8, _INT_BUFFER_SIZE).fill(0);
21
22
  buffer_ptr := (&(buffer(0)));
22
- snprintf(*(char)(buffer_ptr), _INT_BUFFER_SIZE, "%hhd", self.*);
23
+ unsafe(snprintf(*(char)(buffer_ptr), _INT_BUFFER_SIZE, "%hhd", self));
23
24
  String.from_cstr(buffer_ptr).unwrap()
24
25
  })
25
26
  )
@@ -27,10 +28,10 @@ impl(
27
28
  impl(
28
29
  i16,
29
30
  ToString(
30
- to_string : (fn(self : *(Self)) -> String)({
31
+ to_string : (fn(ref(self) : Self) -> String)({
31
32
  buffer := Array(u8, _INT_BUFFER_SIZE).fill(0);
32
33
  buffer_ptr := (&(buffer(0)));
33
- snprintf(*(char)(buffer_ptr), _INT_BUFFER_SIZE, "%hd", self.*);
34
+ unsafe(snprintf(*(char)(buffer_ptr), _INT_BUFFER_SIZE, "%hd", self));
34
35
  String.from_cstr(buffer_ptr).unwrap()
35
36
  })
36
37
  )
@@ -38,10 +39,10 @@ impl(
38
39
  impl(
39
40
  i32,
40
41
  ToString(
41
- to_string : (fn(self : *(Self)) -> String)({
42
+ to_string : (fn(ref(self) : Self) -> String)({
42
43
  buffer := Array(u8, _INT_BUFFER_SIZE).fill(0);
43
44
  buffer_ptr := (&(buffer(0)));
44
- snprintf(*(char)(buffer_ptr), _INT_BUFFER_SIZE, "%d", self.*);
45
+ unsafe(snprintf(*(char)(buffer_ptr), _INT_BUFFER_SIZE, "%d", self));
45
46
  String.from_cstr(buffer_ptr).unwrap()
46
47
  })
47
48
  )
@@ -49,10 +50,10 @@ impl(
49
50
  impl(
50
51
  i64,
51
52
  ToString(
52
- to_string : (fn(self : *(Self)) -> String)({
53
+ to_string : (fn(ref(self) : Self) -> String)({
53
54
  buffer := Array(u8, _INT_BUFFER_SIZE).fill(0);
54
55
  buffer_ptr := (&(buffer(0)));
55
- snprintf(*(char)(buffer_ptr), _INT_BUFFER_SIZE, "%lld", self.*);
56
+ unsafe(snprintf(*(char)(buffer_ptr), _INT_BUFFER_SIZE, "%lld", self));
56
57
  String.from_cstr(buffer_ptr).unwrap()
57
58
  })
58
59
  )
@@ -60,10 +61,10 @@ impl(
60
61
  impl(
61
62
  isize,
62
63
  ToString(
63
- to_string : (fn(self : *(Self)) -> String)({
64
+ to_string : (fn(ref(self) : Self) -> String)({
64
65
  buffer := Array(u8, _INT_BUFFER_SIZE).fill(0);
65
66
  buffer_ptr := (&(buffer(0)));
66
- snprintf(*(char)(buffer_ptr), _INT_BUFFER_SIZE, "%zd", self.*);
67
+ unsafe(snprintf(*(char)(buffer_ptr), _INT_BUFFER_SIZE, "%zd", self));
67
68
  String.from_cstr(buffer_ptr).unwrap()
68
69
  })
69
70
  )
@@ -72,10 +73,10 @@ impl(
72
73
  impl(
73
74
  u8,
74
75
  ToString(
75
- to_string : (fn(self : *(Self)) -> String)({
76
+ to_string : (fn(ref(self) : Self) -> String)({
76
77
  buffer := Array(u8, _INT_BUFFER_SIZE).fill(0);
77
78
  buffer_ptr := (&(buffer(0)));
78
- snprintf(*(char)(buffer_ptr), _INT_BUFFER_SIZE, "%hhu", self.*);
79
+ unsafe(snprintf(*(char)(buffer_ptr), _INT_BUFFER_SIZE, "%hhu", self));
79
80
  String.from_cstr(buffer_ptr).unwrap()
80
81
  })
81
82
  )
@@ -83,10 +84,10 @@ impl(
83
84
  impl(
84
85
  u16,
85
86
  ToString(
86
- to_string : (fn(self : *(Self)) -> String)({
87
+ to_string : (fn(ref(self) : Self) -> String)({
87
88
  buffer := Array(u8, _INT_BUFFER_SIZE).fill(0);
88
89
  buffer_ptr := (&(buffer(0)));
89
- snprintf(*(char)(buffer_ptr), _INT_BUFFER_SIZE, "%hu", self.*);
90
+ unsafe(snprintf(*(char)(buffer_ptr), _INT_BUFFER_SIZE, "%hu", self));
90
91
  String.from_cstr(buffer_ptr).unwrap()
91
92
  })
92
93
  )
@@ -94,10 +95,10 @@ impl(
94
95
  impl(
95
96
  u32,
96
97
  ToString(
97
- to_string : (fn(self : *(Self)) -> String)({
98
+ to_string : (fn(ref(self) : Self) -> String)({
98
99
  buffer := Array(u8, _INT_BUFFER_SIZE).fill(0);
99
100
  buffer_ptr := (&(buffer(0)));
100
- snprintf(*(char)(buffer_ptr), _INT_BUFFER_SIZE, "%u", self.*);
101
+ unsafe(snprintf(*(char)(buffer_ptr), _INT_BUFFER_SIZE, "%u", self));
101
102
  String.from_cstr(buffer_ptr).unwrap()
102
103
  })
103
104
  )
@@ -105,10 +106,10 @@ impl(
105
106
  impl(
106
107
  u64,
107
108
  ToString(
108
- to_string : (fn(self : *(Self)) -> String)({
109
+ to_string : (fn(ref(self) : Self) -> String)({
109
110
  buffer := Array(u8, _INT_BUFFER_SIZE).fill(0);
110
111
  buffer_ptr := (&(buffer(0)));
111
- snprintf(*(char)(buffer_ptr), _INT_BUFFER_SIZE, "%llu", self.*);
112
+ unsafe(snprintf(*(char)(buffer_ptr), _INT_BUFFER_SIZE, "%llu", self));
112
113
  String.from_cstr(buffer_ptr).unwrap()
113
114
  })
114
115
  )
@@ -116,10 +117,10 @@ impl(
116
117
  impl(
117
118
  usize,
118
119
  ToString(
119
- to_string : (fn(self : *(Self)) -> String)({
120
+ to_string : (fn(ref(self) : Self) -> String)({
120
121
  buffer := Array(u8, _INT_BUFFER_SIZE).fill(0);
121
122
  buffer_ptr := (&(buffer(0)));
122
- snprintf(*(char)(buffer_ptr), _INT_BUFFER_SIZE, "%zu", self.*);
123
+ unsafe(snprintf(*(char)(buffer_ptr), _INT_BUFFER_SIZE, "%zu", self));
123
124
  String.from_cstr(buffer_ptr).unwrap()
124
125
  })
125
126
  )
@@ -131,11 +132,11 @@ _FLOAT_BUFFER_SIZE :: usize(64);
131
132
  impl(
132
133
  f32,
133
134
  ToString(
134
- to_string : (fn(self : *(Self)) -> String)({
135
+ to_string : (fn(ref(self) : Self) -> String)({
135
136
  buffer := Array(u8, _FLOAT_BUFFER_SIZE).fill(0);
136
137
  buffer_ptr := (&(buffer(0)));
137
138
  // Use %g for general format (removes trailing zeros)
138
- snprintf(*(char)(buffer_ptr), _FLOAT_BUFFER_SIZE, "%g", f64(self.*));
139
+ unsafe(snprintf(*(char)(buffer_ptr), _FLOAT_BUFFER_SIZE, "%g", f64(self)));
139
140
  String.from_cstr(buffer_ptr).unwrap()
140
141
  })
141
142
  )
@@ -143,11 +144,11 @@ impl(
143
144
  impl(
144
145
  f64,
145
146
  ToString(
146
- to_string : (fn(self : *(Self)) -> String)({
147
+ to_string : (fn(ref(self) : Self) -> String)({
147
148
  buffer := Array(u8, _FLOAT_BUFFER_SIZE).fill(0);
148
149
  buffer_ptr := (&(buffer(0)));
149
150
  // Use %g for general format (removes trailing zeros)
150
- snprintf(*(char)(buffer_ptr), _FLOAT_BUFFER_SIZE, "%g", self.*);
151
+ unsafe(snprintf(*(char)(buffer_ptr), _FLOAT_BUFFER_SIZE, "%g", self));
151
152
  String.from_cstr(buffer_ptr).unwrap()
152
153
  })
153
154
  )
@@ -156,9 +157,9 @@ impl(
156
157
  impl(
157
158
  bool,
158
159
  ToString(
159
- to_string : (fn(self : *(Self)) -> String)(
160
+ to_string : (fn(ref(self) : Self) -> String)(
160
161
  cond(
161
- self.* => String.from("true"),
162
+ self => String.from("true"),
162
163
  true => String.from("false")
163
164
  )
164
165
  )
@@ -168,10 +169,10 @@ impl(
168
169
  impl(
169
170
  char,
170
171
  ToString(
171
- to_string : (fn(self : *(Self)) -> String)({
172
+ to_string : (fn(ref(self) : Self) -> String)({
172
173
  // char is a single byte, create a 2-byte buffer (char + null)
173
174
  buffer := Array(u8, usize(2)).fill(0);
174
- buffer(0) = u8(self.*);
175
+ buffer(0) = u8(self);
175
176
  buffer_ptr := (&(buffer(0)));
176
177
  String.from_cstr(buffer_ptr).unwrap()
177
178
  })
@@ -181,9 +182,9 @@ impl(
181
182
  impl(
182
183
  String,
183
184
  ToString(
184
- to_string : (fn(self : *(Self)) -> String)(
185
+ to_string : (fn(ref(self) : Self) -> String)(
185
186
  // String already is a string, just return a clone
186
- self.*
187
+ self
187
188
  )
188
189
  )
189
190
  );
@@ -191,9 +192,9 @@ impl(
191
192
  impl(
192
193
  str,
193
194
  ToString(
194
- to_string : (fn(self : *(Self)) -> String)({
195
+ to_string : (fn(ref(self) : Self) -> String)({
195
196
  // Convert byte array to String assuming UTF-8
196
- return(String.from(self.*));
197
+ return(String.from(self));
197
198
  })
198
199
  )
199
200
  );
@@ -202,9 +203,9 @@ impl(
202
203
  impl(
203
204
  rune,
204
205
  ToString(
205
- to_string : (fn(self : *(Self)) -> String)({
206
+ to_string : (fn(ref(self) : Self) -> String)({
206
207
  // Get the code point value
207
- code := self.*.char;
208
+ code := self.char;
208
209
  // UTF-8 encoding:
209
210
  // 0x00-0x7F: 1 byte
210
211
  // 0x80-0x7FF: 2 bytes
@@ -309,7 +310,7 @@ __derive_tostring :: (fn(comptime(T) : Type, comptime(ctx) : DeriveContext, comp
309
310
  (fi == 0) => a,
310
311
  true => __derive_join_plus(a, "String.from(\", \")")
311
312
  );
312
- __derive_join_plus(with_sep, __ts_s3("(&(self.*.", fname, ")).to_string()"))
313
+ __derive_join_plus(with_sep, __ts_s3("self.", fname, ".to_string()"))
313
314
  })
314
315
  );
315
316
  full_body :: __derive_join_plus(body, "String.from(\")\")");
@@ -355,7 +356,7 @@ __derive_tostring :: (fn(comptime(T) : Type, comptime(ctx) : DeriveContext, comp
355
356
  (fi == 0) => a,
356
357
  true => __derive_join_plus(a, "String.from(\", \")")
357
358
  );
358
- __derive_join_plus(with_sep, __ts_s3("(&(__v_", fname, ")).to_string()"))
359
+ __derive_join_plus(with_sep, __ts_s3("__v_", fname, ".to_string()"))
359
360
  })
360
361
  );
361
362
  full_body :: __derive_join_plus(body, "String.from(\")\")");
@@ -368,7 +369,7 @@ __derive_tostring :: (fn(comptime(T) : Type, comptime(ctx) : DeriveContext, comp
368
369
  )
369
370
  })
370
371
  );
371
- match_body :: __ts_s3("match(self.*,\n ", ts_branches, "\n )");
372
+ match_body :: __ts_s3("match(self,\n ", ts_branches, "\n )");
372
373
  ctx.make_impl(
373
374
  quote(
374
375
  ToString(
package/std/fmt/writer.yo CHANGED
@@ -11,6 +11,7 @@
11
11
  //! .write_byte(u8(33));
12
12
  //! s := w.to_string(); // "hello world!"
13
13
  //! ```
14
+ pragma(Pragma.AllowUnsafe);
14
15
  { String } :: import("../string");
15
16
  { ArrayList } :: import("../collections/array_list");
16
17
  { rune } :: import("../string/rune");
@@ -46,7 +47,7 @@ impl(
46
47
  bytes := s.as_bytes();
47
48
  i := usize(0);
48
49
  while(i < bytes.len(), i = (i + usize(1)), {
49
- self.buf.push(bytes.get(i).unwrap());
50
+ self.buf.push(bytes(i));
50
51
  });
51
52
  self
52
53
  }),
@@ -59,7 +60,7 @@ impl(
59
60
  write_bytes : (fn(self : Self, data : ArrayList(u8)) -> Self)({
60
61
  i := usize(0);
61
62
  while(i < data.len(), i = (i + usize(1)), {
62
- self.buf.push(data.get(i).unwrap());
63
+ self.buf.push(data(i));
63
64
  });
64
65
  self
65
66
  }),
@@ -92,7 +93,7 @@ impl(
92
93
  write_i64 : (fn(self : Self, n : i64) -> Self)({
93
94
  buf := Array(u8, usize(24)).fill(u8(0));
94
95
  buf_ptr := &(buf(usize(0)));
95
- snprintf(*(char)(buf_ptr), usize(24), "%lld", n);
96
+ unsafe(snprintf(*(char)(buf_ptr), usize(24), "%lld", n));
96
97
  s := String.from_cstr(buf_ptr).unwrap();
97
98
  self.write_string(s)
98
99
  }),
@@ -100,7 +101,7 @@ impl(
100
101
  write_u64 : (fn(self : Self, n : u64) -> Self)({
101
102
  buf := Array(u8, usize(24)).fill(u8(0));
102
103
  buf_ptr := &(buf(usize(0)));
103
- snprintf(*(char)(buf_ptr), usize(24), "%llu", n);
104
+ unsafe(snprintf(*(char)(buf_ptr), usize(24), "%llu", n));
104
105
  s := String.from_cstr(buf_ptr).unwrap();
105
106
  self.write_string(s)
106
107
  }),
@@ -108,7 +109,7 @@ impl(
108
109
  write_f64 : (fn(self : Self, n : f64, precision : i32) -> Self)({
109
110
  buf := Array(u8, usize(64)).fill(u8(0));
110
111
  buf_ptr := &(buf(usize(0)));
111
- snprintf(*(char)(buf_ptr), usize(64), "%.*f", precision, n);
112
+ unsafe(snprintf(*(char)(buf_ptr), usize(64), "%.*f", precision, n));
112
113
  s := String.from_cstr(buf_ptr).unwrap();
113
114
  self.write_string(s)
114
115
  }),
@@ -123,7 +124,7 @@ impl(
123
124
  write_hex : (fn(self : Self, n : u64) -> Self)({
124
125
  buf := Array(u8, usize(20)).fill(u8(0));
125
126
  buf_ptr := &(buf(usize(0)));
126
- snprintf(*(char)(buf_ptr), usize(20), "%llx", n);
127
+ unsafe(snprintf(*(char)(buf_ptr), usize(20), "%llx", n));
127
128
  s := String.from_cstr(buf_ptr).unwrap();
128
129
  self.write_string(s)
129
130
  }),
@@ -131,7 +132,7 @@ impl(
131
132
  write_octal : (fn(self : Self, n : u64) -> Self)({
132
133
  buf := Array(u8, usize(24)).fill(u8(0));
133
134
  buf_ptr := &(buf(usize(0)));
134
- snprintf(*(char)(buf_ptr), usize(24), "%llo", n);
135
+ unsafe(snprintf(*(char)(buf_ptr), usize(24), "%llo", n));
135
136
  s := String.from_cstr(buf_ptr).unwrap();
136
137
  self.write_string(s)
137
138
  }),
@@ -152,7 +153,7 @@ impl(
152
153
  // Push in reverse order (most significant first)
153
154
  i := bits.len();
154
155
  while(i > usize(0), i = (i - usize(1)), {
155
- self.buf.push(bits.get(i - usize(1)).unwrap());
156
+ self.buf.push(bits(i - usize(1)));
156
157
  });
157
158
  }
158
159
  );
package/std/fs/dir.yo CHANGED
@@ -8,19 +8,20 @@
8
8
  //! { create_dir, remove_dir, read_dir } :: import "std/fs/dir";
9
9
  //! { Path } :: import "std/path";
10
10
  //!
11
- //! main :: (fn(io : IO, exn : Exception) -> unit)({
11
+ //! main :: (fn(io : Io, exn : Exception) -> unit)({
12
12
  //! io.await(create_dir(Path.new(`/tmp/yo_test`), io), { io, exn });
13
13
  //! entries := io.await(read_dir(Path.new(`/tmp/yo_test`), io), { io, exn });
14
14
  //! io.await(remove_dir(Path.new(`/tmp/yo_test`), io), { io, exn });
15
15
  //! });
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
  { Path } :: import("../path");
22
- { IOError } :: import("../sys/errors");
23
- { Error, AnyError, Exception, IOErr } :: import("../error");
23
+ { IoError } :: import("../sys/errors");
24
+ { Error, AnyError, Exception, IoExn } :: import("../error");
24
25
  { EEXIST, ENOENT } :: import("../libc/errno");
25
26
  IO_dir :: import("../sys/dir");
26
27
  IO_file :: import("../sys/file");
@@ -55,24 +56,24 @@ export(DirEntry);
55
56
  // Directory operations
56
57
  // ============================================================================
57
58
  /// Create a directory at the given path.
58
- create_dir :: (fn(path : Path, io : IO) -> Impl(Future(unit, IOErr)))(
59
+ create_dir :: (fn(path : Path, io : Io) -> Impl(Future(unit, IoExn)))(
59
60
  io.async((e) => {
60
61
  cstr_bytes := path.to_string().to_cstr();
61
62
  cstr := cstr_bytes.ptr().unwrap();
62
63
  result := e.io.await(IO_dir.mkdir(AT_FDCWD, cstr, i32(DEFAULT_DIR_MODE)), e.io);
63
64
  cond(
64
- (result < i32(0)) => e.exn.throw(dyn(IOError.from_errno(i32(0) - result))),
65
+ (result < i32(0)) => e.exn.throw(dyn(IoError.from_errno(i32(0) - result))),
65
66
  true => ()
66
67
  )
67
68
  })
68
69
  );
69
70
  /// Create a directory (`str` path variant).
70
- create_dir_str :: (fn(path : str, io : IO) -> Impl(Future(unit, IOErr)))(
71
+ create_dir_str :: (fn(path : str, io : Io) -> Impl(Future(unit, IoExn)))(
71
72
  create_dir(Path.new(String.from(path)), io)
72
73
  );
73
74
  /// Create a directory and all missing parent directories.
74
75
  /// Does not error if the directory already exists.
75
- create_dir_all :: (fn(path : Path, io : IO) -> Impl(Future(unit, IOErr)))(
76
+ create_dir_all :: (fn(path : Path, io : Io) -> Impl(Future(unit, IoExn)))(
76
77
  io.async((e) => {
77
78
  path_s := path.to_string();
78
79
  cstr_bytes := path_s.to_cstr();
@@ -97,8 +98,8 @@ create_dir_all :: (fn(path : Path, io : IO) -> Impl(Future(unit, IOErr)))(
97
98
  (platform == Platform.Windows) => {
98
99
  cond(
99
100
  (bytes.len() >= usize(3)) => {
100
- first := bytes.get(usize(0)).unwrap();
101
- second := bytes.get(usize(1)).unwrap();
101
+ first := bytes(usize(0));
102
+ second := bytes(usize(1));
102
103
  is_drive_root := ((((first >= u8(65)) && (first <= u8(90))) || ((first >= u8(97)) && (first <= u8(122)))) && (second == u8(58)));
103
104
  cond(
104
105
  is_drive_root => {
@@ -113,7 +114,7 @@ create_dir_all :: (fn(path : Path, io : IO) -> Impl(Future(unit, IOErr)))(
113
114
  true => ()
114
115
  );
115
116
  while(runtime(i < bytes.len()), {
116
- b := bytes.get(i).unwrap();
117
+ b := bytes(i);
117
118
  cond(
118
119
  ((b == u8(47)) || (b == u8(92))) => {
119
120
  // '/' found - try to create this prefix
@@ -124,7 +125,7 @@ create_dir_all :: (fn(path : Path, io : IO) -> Impl(Future(unit, IOErr)))(
124
125
  // Ignore EEXIST errors
125
126
  cond(
126
127
  ((r < i32(0)) && ((i32(0) - r) != i32(EEXIST))) => {
127
- e.exn.throw(dyn(IOError.from_errno(i32(0) - r)));
128
+ e.exn.throw(dyn(IoError.from_errno(i32(0) - r)));
128
129
  },
129
130
  true => ()
130
131
  );
@@ -141,51 +142,51 @@ create_dir_all :: (fn(path : Path, io : IO) -> Impl(Future(unit, IOErr)))(
141
142
  final_errno := (i32(0) - final_result);
142
143
  cond(
143
144
  (final_errno == i32(EEXIST)) => (),
144
- true => e.exn.throw(dyn(IOError.from_errno(final_errno)))
145
+ true => e.exn.throw(dyn(IoError.from_errno(final_errno)))
145
146
  )
146
147
  }
147
148
  )
148
149
  },
149
- true => e.exn.throw(dyn(IOError.from_errno(errno)))
150
+ true => e.exn.throw(dyn(IoError.from_errno(errno)))
150
151
  )
151
152
  }
152
153
  )
153
154
  })
154
155
  );
155
156
  /// Create a directory and all missing parents (`str` path variant).
156
- create_dir_all_str :: (fn(path : str, io : IO) -> Impl(Future(unit, IOErr)))(
157
+ create_dir_all_str :: (fn(path : str, io : Io) -> Impl(Future(unit, IoExn)))(
157
158
  create_dir_all(Path.new(String.from(path)), io)
158
159
  );
159
160
  /// Remove an empty directory. Throws if the directory is not empty.
160
- remove_dir :: (fn(path : Path, io : IO) -> Impl(Future(unit, IOErr)))(
161
+ remove_dir :: (fn(path : Path, io : Io) -> Impl(Future(unit, IoExn)))(
161
162
  io.async((e) => {
162
163
  cstr_bytes := path.to_string().to_cstr();
163
164
  cstr := cstr_bytes.ptr().unwrap();
164
165
  result := e.io.await(IO_dir.unlink(AT_FDCWD, cstr, AT_REMOVEDIR), e.io);
165
166
  cond(
166
- (result < i32(0)) => e.exn.throw(dyn(IOError.from_errno(i32(0) - result))),
167
+ (result < i32(0)) => e.exn.throw(dyn(IoError.from_errno(i32(0) - result))),
167
168
  true => ()
168
169
  )
169
170
  })
170
171
  );
171
- remove_dir_str :: (fn(path : str, io : IO) -> Impl(Future(unit, IOErr)))(
172
+ remove_dir_str :: (fn(path : str, io : Io) -> Impl(Future(unit, IoExn)))(
172
173
  remove_dir(Path.new(String.from(path)), io)
173
174
  );
174
175
  /// Remove a file at the given path.
175
- remove_file :: (fn(path : Path, io : IO) -> Impl(Future(unit, IOErr)))(
176
+ remove_file :: (fn(path : Path, io : Io) -> Impl(Future(unit, IoExn)))(
176
177
  io.async((e) => {
177
178
  cstr_bytes := path.to_string().to_cstr();
178
179
  cstr := cstr_bytes.ptr().unwrap();
179
180
  result := e.io.await(IO_dir.unlink(AT_FDCWD, cstr, i32(0)), e.io);
180
181
  cond(
181
- (result < i32(0)) => e.exn.throw(dyn(IOError.from_errno(i32(0) - result))),
182
+ (result < i32(0)) => e.exn.throw(dyn(IoError.from_errno(i32(0) - result))),
182
183
  true => ()
183
184
  )
184
185
  })
185
186
  );
186
- remove_file_str :: (fn(path : str, io : IO) -> Impl(Future(unit, IOErr)))(remove_file(Path.new(String.from(path)), io));
187
+ remove_file_str :: (fn(path : str, io : Io) -> Impl(Future(unit, IoExn)))(remove_file(Path.new(String.from(path)), io));
187
188
  /// Rename or move a file or directory from `from` to `to`.
188
- rename :: (fn(from : Path, to : Path, io : IO) -> Impl(Future(unit, IOErr)))(
189
+ rename :: (fn(from : Path, to : Path, io : Io) -> Impl(Future(unit, IoExn)))(
189
190
  io.async((e) => {
190
191
  from_cstr_bytes := from.to_string().to_cstr();
191
192
  from_cstr := from_cstr_bytes.ptr().unwrap();
@@ -193,14 +194,14 @@ rename :: (fn(from : Path, to : Path, io : IO) -> Impl(Future(unit, IOErr)))(
193
194
  to_cstr := to_cstr_bytes.ptr().unwrap();
194
195
  result := e.io.await(IO_dir.rename(AT_FDCWD, from_cstr, AT_FDCWD, to_cstr), e.io);
195
196
  cond(
196
- (result < i32(0)) => e.exn.throw(dyn(IOError.from_errno(i32(0) - result))),
197
+ (result < i32(0)) => e.exn.throw(dyn(IoError.from_errno(i32(0) - result))),
197
198
  true => ()
198
199
  )
199
200
  })
200
201
  );
201
- rename_str :: (fn(from : str, to : str, io : IO) -> Impl(Future(unit, IOErr)))(rename(Path.new(String.from(from)), Path.new(String.from(to)), io));
202
+ rename_str :: (fn(from : str, to : str, io : Io) -> Impl(Future(unit, IoExn)))(rename(Path.new(String.from(from)), Path.new(String.from(to)), io));
202
203
  /// Create a hard link from `src` to `dst`.
203
- hard_link :: (fn(src : Path, dst : Path, io : IO) -> Impl(Future(unit, IOErr)))(
204
+ hard_link :: (fn(src : Path, dst : Path, io : Io) -> Impl(Future(unit, IoExn)))(
204
205
  io.async((e) => {
205
206
  src_cstr_bytes := src.to_string().to_cstr();
206
207
  src_cstr := src_cstr_bytes.ptr().unwrap();
@@ -208,14 +209,14 @@ hard_link :: (fn(src : Path, dst : Path, io : IO) -> Impl(Future(unit, IOErr)))(
208
209
  dst_cstr := dst_cstr_bytes.ptr().unwrap();
209
210
  result := e.io.await(IO_dir.link(AT_FDCWD, src_cstr, AT_FDCWD, dst_cstr, i32(0)), e.io);
210
211
  cond(
211
- (result < i32(0)) => e.exn.throw(dyn(IOError.from_errno(i32(0) - result))),
212
+ (result < i32(0)) => e.exn.throw(dyn(IoError.from_errno(i32(0) - result))),
212
213
  true => ()
213
214
  )
214
215
  })
215
216
  );
216
- hard_link_str :: (fn(src : str, dst : str, io : IO) -> Impl(Future(unit, IOErr)))(hard_link(Path.new(String.from(src)), Path.new(String.from(dst)), io));
217
+ hard_link_str :: (fn(src : str, dst : str, io : Io) -> Impl(Future(unit, IoExn)))(hard_link(Path.new(String.from(src)), Path.new(String.from(dst)), io));
217
218
  /// Create a symbolic link at `dst` pointing to `src`.
218
- symlink :: (fn(src : Path, dst : Path, io : IO) -> Impl(Future(unit, IOErr)))(
219
+ symlink :: (fn(src : Path, dst : Path, io : Io) -> Impl(Future(unit, IoExn)))(
219
220
  io.async((e) => {
220
221
  src_cstr_bytes := src.to_string().to_cstr();
221
222
  src_cstr := src_cstr_bytes.ptr().unwrap();
@@ -223,25 +224,25 @@ symlink :: (fn(src : Path, dst : Path, io : IO) -> Impl(Future(unit, IOErr)))(
223
224
  dst_cstr := dst_cstr_bytes.ptr().unwrap();
224
225
  result := e.io.await(IO_dir.symlink(src_cstr, AT_FDCWD, dst_cstr), e.io);
225
226
  cond(
226
- (result < i32(0)) => e.exn.throw(dyn(IOError.from_errno(i32(0) - result))),
227
+ (result < i32(0)) => e.exn.throw(dyn(IoError.from_errno(i32(0) - result))),
227
228
  true => ()
228
229
  )
229
230
  })
230
231
  );
231
- symlink_str :: (fn(src : str, dst : str, io : IO) -> Impl(Future(unit, IOErr)))(symlink(Path.new(String.from(src)), Path.new(String.from(dst)), io));
232
+ symlink_str :: (fn(src : str, dst : str, io : Io) -> Impl(Future(unit, IoExn)))(symlink(Path.new(String.from(src)), Path.new(String.from(dst)), io));
232
233
  // ============================================================================
233
234
  // Directory listing
234
235
  // ============================================================================
235
236
  /// Read all entries from a directory, returning an `ArrayList(DirEntry)`.
236
237
  /// Skips the `.` and `..` entries.
237
- read_dir :: (fn(path : Path, io : IO) -> Impl(Future(ArrayList(DirEntry), IOErr)))(
238
+ read_dir :: (fn(path : Path, io : Io) -> Impl(Future(ArrayList(DirEntry), IoExn)))(
238
239
  io.async((e) => {
239
240
  cstr_bytes := path.to_string().to_cstr();
240
241
  cstr := cstr_bytes.ptr().unwrap();
241
242
  // Open the directory
242
243
  fd_result := e.io.await(IO_file.openat(AT_FDCWD, cstr, O_RDONLY | O_DIRECTORY, i32(0)), e.io);
243
244
  cond(
244
- (fd_result < i32(0)) => e.exn.throw(dyn(IOError.from_errno(i32(0) - fd_result))),
245
+ (fd_result < i32(0)) => e.exn.throw(dyn(IoError.from_errno(i32(0) - fd_result))),
245
246
  true => ()
246
247
  );
247
248
  fd := fd_result;
@@ -254,7 +255,7 @@ read_dir :: (fn(path : Path, io : IO) -> Impl(Future(ArrayList(DirEntry), IOErr)
254
255
  (n < i32(0)) => {
255
256
  free(.Some(*(void)(buf)));
256
257
  e.io.await(IO_file.close(fd), e.io);
257
- e.exn.throw(dyn(IOError.from_errno(i32(0) - n)));
258
+ e.exn.throw(dyn(IoError.from_errno(i32(0) - n)));
258
259
  },
259
260
  (n == i32(0)) => {
260
261
  break;
@@ -295,7 +296,7 @@ read_dir :: (fn(path : Path, io : IO) -> Impl(Future(ArrayList(DirEntry), IOErr)
295
296
  entries
296
297
  })
297
298
  );
298
- read_dir_str :: (fn(path : str, io : IO) -> Impl(Future(ArrayList(DirEntry), IOErr)))(read_dir(Path.new(String.from(path)), io));
299
+ read_dir_str :: (fn(path : str, io : Io) -> Impl(Future(ArrayList(DirEntry), IoExn)))(read_dir(Path.new(String.from(path)), io));
299
300
  export(
300
301
  create_dir,
301
302
  create_dir_str,