land/userland/src/lib/fileops.wast

84 lines
1.8 KiB
Plaintext
Raw Normal View History

2018-06-18 01:48:10 +00:00
(module
;; import functions from env
2018-06-18 02:35:10 +00:00
(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))
(func $istty (import "env" "isatty") (param i32) (result i32))
(func $unlink (import "env" "unlink") (param i32) (result i32))
2018-06-18 01:48:10 +00:00
2018-06-18 02:35:10 +00:00
;; memory
2018-06-18 01:48:10 +00:00
(memory $mem 1)
2018-06-18 02:35:10 +00:00
;; constants
2018-06-18 01:48:10 +00:00
(data (i32.const 200) "data")
(data (i32.const 230) "Hello, world!\n")
2018-06-18 02:35:10 +00:00
;; functions
(func $openFile (result i32)
2018-06-18 01:48:10 +00:00
;; open stdout
(i32.const 200)
(i32.const 42)
2018-06-18 02:35:10 +00:00
(call $open))
2018-06-18 01:48:10 +00:00
2018-06-18 02:35:10 +00:00
(func $writeHelloWorld (param $fd i32)
2018-06-18 01:48:10 +00:00
;; write hello world
(get_local $fd)
(i32.const 230)
(i32.const 14)
(call $write)
2018-06-18 02:35:10 +00:00
(drop))
2018-06-18 01:48:10 +00:00
2018-06-18 02:35:10 +00:00
(func $readHello (param $fd i32)
;; read hello world
2018-06-18 01:48:10 +00:00
(get_local $fd)
2018-06-18 02:35:10 +00:00
(i32.const 255)
(i32.const 14)
(call $read)
(drop))
2018-06-18 01:48:10 +00:00
2018-06-18 02:35:10 +00:00
(func $removeFile
2018-06-18 01:48:10 +00:00
(i32.const 200)
2018-06-18 02:35:10 +00:00
(call $unlink)
(drop))
(func $main (result i32)
(local $fd i32)
;; set $fd to the file descriptor
(call $openFile)
(set_local $fd)
(get_local $fd)
(call $writeHelloWorld)
;; close file
(get_local $fd)
(call $close)
2018-06-18 01:48:10 +00:00
;; set $fd to the new file descriptor
2018-06-18 02:35:10 +00:00
(call $openFile)
2018-06-18 01:48:10 +00:00
(set_local $fd)
2018-06-18 02:35:10 +00:00
;; read Hello, World
2018-06-18 01:48:10 +00:00
(get_local $fd)
2018-06-18 02:35:10 +00:00
(call $readHello)
;; istty
(get_local $fd)
(call $istty)
2018-06-18 01:48:10 +00:00
(drop)
2018-06-18 02:35:10 +00:00
;; close file again
(get_local $fd)
(call $close)
;; unlink filename
(call $removeFile)
2018-06-18 01:48:10 +00:00
;; return 0
2018-06-18 02:35:10 +00:00
(i32.const 0)
(set_local $fd)
(drop))
2018-06-18 01:48:10 +00:00
(export "main" (func $main)))