land/userland/src/lib/fileops.wast

52 lines
1.1 KiB
Plaintext

(module
;; import functions from env
(func $close (import "env" "close") (param i32) (result i32))
(func $open (import "env" "open") (param i32 i32) (result i32))
(func $write (import "env" "write") (param i32 i32 i32) (result i32))
(func $read (import "env" "read") (param i32 i32 i32) (result i32))
(memory $mem 1)
(data (i32.const 200) "data")
(data (i32.const 230) "Hello, world!\n")
(func $main (result i32)
(local $fd i32)
;; open stdout
(i32.const 200)
(i32.const 42)
(call $open)
;; set $fd to the file descriptor
(set_local $fd)
;; write hello world
(get_local $fd)
(i32.const 230)
(i32.const 14)
(call $write)
(drop)
;; close file
(get_local $fd)
(call $close)
;; open new file
(i32.const 200)
(i32.const 42)
(call $open)
;; set $fd to the new file descriptor
(set_local $fd)
;; read hello world
(get_local $fd)
(i32.const 255)
(i32.const 14)
(call $read)
(drop)
;; return 0
)
(export "main" (func $main)))