aboutsummaryrefslogtreecommitdiff
path: root/tests/programs
diff options
context:
space:
mode:
authorGravatar zinc <zinc@downsta.ir> 2026-10-15 02:46:57 +0330
committerGravatar zinc <zinc@downsta.ir> 2026-10-15 02:46:57 +0330
commit54d316a531ddb8e293711761e4f29e93d282599c (patch)
treee062d2ef306dd9c9e28b293804d48b72789e0174 /tests/programs
parentrt: gc_drops finalizes a droppable value that reaches itself (a cycle, or a d... (diff)
std: File, the first user of drops: std.Host.open(path, mode) returns an owned std.File (or null) with write and read, and its drop closes the descriptor; Host.writefile is built on it; file_drop.zc, learn and readme
Diffstat (limited to 'tests/programs')
-rw-r--r--tests/programs/file_drop.zc19
1 files changed, 19 insertions, 0 deletions
diff --git a/tests/programs/file_drop.zc b/tests/programs/file_drop.zc
new file mode 100644
index 0000000..0c0738a
--- /dev/null
+++ b/tests/programs/file_drop.zc
@@ -0,0 +1,19 @@
+namespace;
+// std.File: Host.open gives an owned file whose drop closes the descriptor at its block end (a write to it then writes nothing)
+include rt, std;
+fn main() {
+ int fd = 0 - 1;
+ {
+ std.File* f = std.Host.open("/tmp/zc-file-drop.txt", 1);
+ fd = f.fd;
+ rt.put_n(f.write("hello")); rt.put_c(' ');
+ rt.put_n(rt.syscall(rt.SYS_WRITE, fd, "!", 1)); rt.put_c(' ');
+ }
+ rt.put_n(rt.syscall(rt.SYS_WRITE, fd, "?", 1) <= 0); rt.put_c(' ');
+ char* s = rt.readfile("/tmp/zc-file-drop.txt");
+ rt.put_s(s); rt.put_c(' ');
+ rt.free(s);
+ std.File* g = std.Host.open("/tmp/zc-no-such-dir/x", 1);
+ rt.put_n(g == 0); rt.put_c(10);
+ return 0;
+}