compiler/tests/fixtures/runtime/hosted-system-alone/main.ldn
skip to the library navigation
1-- D476: a program that never waits on anything uses hosted/system alone.
2-- It reads no stream and writes none, since the system's handle is no
3-- world, so it answers with its exit status: zero when its arguments,
4-- its environment, a file opened, examined and closed, a directory read,
5-- random bytes and the wall clock all came from the one handle, and the
6-- number of the first that did not otherwise.
7import core/cmp
8import core/io
9import core/text
10import core/time
11import hosted/system
12
13-- The arguments are `one` and the path of an eleven-byte input file.
14checked: (host: system.host) -> (failed: i32) ! ... =
15 failed = 2
16 return when io.argument_count(host) <> 2
17 first: []u8 = text.host_bytes(try io.argument_at(host, 0))
18 return when cmp.compare(first, "one") <> cmp.equal
19 failed = 3
20 _ = io.variable_count(host)
21 _ = io.variable_named(host, text.host_over("LANDIN_NO_SUCH_VARIABLE"))
22 else (problem)
23 failed = if problem == io.no_variable then 4 else 3 end if
24 end
25 return when failed <> 4
26 path: text.host_string = try io.argument_at(host, 1)
27 opened: io.file = try io.open_read(host, path)
28 found: io.status = try io.status_of_file(host, opened)
29 try io.close(host, opened)
30 return when found.size <> 11
31 failed = 5
32 mut buffer: [io.directory_buffer]u8 = zeroed
33 mut listing := try io.open_directory(host, text.host_over("."), buffer)
34 item: io.entry = try io.read_entry(host, listing)
35 named: bool = lenof text.host_bytes(item.name) > 0
36 try io.close_directory(host, listing)
37 return when not named
38 failed = 6
39 mut bytes: [16]u8 = zeroed
40 try io.fill_random(host, bytes[0..<16])
41 failed = 7
42 now: time.wall_time = time.wall_now(host)
43 return when now.seconds < 1600000000
44 failed = 0
45end checked
46
47public main: () -> (code: i32) =
48 code = checked(system.new()) else (problem)
49 _ = problem
50 code = 1
51 return
52 end
53end main