# WASI 0.1, the first stage: arguments, environment, clocks, random bytes, the standard streams, the descriptors of preopens, the end of the program.
# One command in a line: `arg`/`env`/`preopen`/`hooks` before `start`; then `mem` and `call`; `signal on|off` (after `start`) sets or takes away the hook `signal` of the guest. A `call` prints `name rc`, or `name trapped` when the program ended.
arg "prog"
arg "a b"
arg "c"
env "K=v"
env "X=1"
preopen "$TMP" "/data" rw
hooks on
start 8192

stdin "hello stdin data"

# arguments and environment
call args_sizes_get 0 4
mem dump 0 8 sizes
call args_get 16 64
mem dump 16 12 argv
mem dump 64 12 argbuf
call environ_sizes_get 100 104
mem dump 100 8 envsizes
call environ_get 120 160
mem dump 120 8 environ
mem dump 160 8 envbuf
call args_sizes_get 8190 4
call args_get 16 8190
call args_get 8190 64

# clocks (through the hooks: 1000 + 7 * id) and random bytes
call clock_time_get 0 1 200
mem dump 200 8 realtime
call clock_time_get 1 1 208
mem dump 208 8 monotonic
call clock_time_get 3 1 216
mem dump 216 8 thread
call clock_time_get 4 1 224
call clock_time_get 0 1 8190
clock_status 52
call clock_time_get 0 1 232
clock_status 0
call clock_res_get 4 240
call clock_res_get 9 240
call random_get 300 16
mem dump 300 16 random
call random_get 8190 16
call random_get 8190 0
random_status 29
call random_get 320 4
random_status 0
call sched_yield

# the standard streams
mem put 400 "hello "
mem put 410 "world\n"
mem u32 500 400
mem u32 504 6
mem u32 508 410
mem u32 512 6
call fd_write 1 500 2 520
mem dump 520 4 nwritten
call fd_write 2 500 2 520
call fd_write 0 500 2 520
call fd_write 9 500 2 520
call fd_write 3 500 2 520
call fd_write 1 500 0 520
mem dump 520 4 nwritten0
mem u32 540 8190
mem u32 544 100
call fd_write 1 500 1 520
call fd_write 1 540 1 520
mem u32 548 0
mem u32 552 0
call fd_write 1 548 1 520
call fd_write 1 500 2 8190
stdout_status 28
call fd_write 1 500 2 520
stdout_status 0

mem u32 600 700
mem u32 604 5
mem u32 608 710
mem u32 612 5
call fd_read 0 600 2 620
mem dump 620 4 nread
mem dump 700 10 read1
call fd_read 0 600 2 620
mem dump 700 10 read2
mem u32 616 0
call fd_read 1 600 2 620
call fd_read 3 600 1 620
call fd_read 0 600 2 8190
mem u32 624 8190
mem u32 628 4
call fd_read 0 624 1 620
stdin "x"
mem u32 632 704
mem u32 636 0
mem u32 640 700
mem u32 644 0
call fd_read 0 632 2 620
mem dump 620 4 nread_empty_first

# descriptors
call fd_fdstat_get 0 800
mem dump 800 24 fdstat0
call fd_fdstat_get 1 800
mem dump 800 24 fdstat1
call fd_fdstat_get 3 800
mem dump 800 24 fdstat3
call fd_fdstat_get 7 800
call fd_fdstat_get 3 8180
call fd_prestat_get 3 840
mem dump 840 8 prestat
call fd_prestat_get 4 840
call fd_prestat_get 1 840
call fd_prestat_get 3 8190
call fd_prestat_dir_name 3 860 5
mem dump 860 5 dirname
call fd_prestat_dir_name 3 870 4
call fd_prestat_dir_name 3 8190 5
call fd_prestat_dir_name 5 870 5
call fd_seek 1 0 0 900
call fd_seek 0 0 3 900
call fd_seek 3 0 0 900
call fd_seek 9 0 0 900
call fd_tell 0 900
call fd_tell 3 900
call fd_advise 3 0 0 0
call fd_advise 1 0 0 0
call fd_advise 9 0 0 9
call fd_renumber 3 3
call fd_renumber 3 9
call fd_renumber 9 3
call fd_renumber 2 3
call fd_write 3 500 2 520
call fd_write 2 500 2 520
call fd_close 3
call fd_close 3
call fd_close 0
call fd_read 0 600 2 620
call fd_close 9

# proc_raise asks the hook `signal` of the guest: 31 is INVAL and 0 is success whatever the hook; with no hook the answer is NOTSUP and the program goes on
call proc_raise 31
call proc_raise 0
call proc_raise 20
call proc_raise 27
call proc_raise 9
# with the hook (it tells the number; 20 is accepted, 9 ends the program by a trap, any other is refused with PERM)
signal on
call proc_raise 31
call proc_raise 0
call proc_raise 20
call proc_raise 27
call proc_raise 9
signal off
call proc_raise 9

# the end of the program
call fd_write 1 500 2 520
call fd_fdstat_get 1 800
call proc_exit 7
