land/cmd/land/testdata/fileops.wast

84 lines
2.0 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
(call $open
(i32.const 200)
(i32.const 42)))
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
(call $write
(get_local $fd)
(i32.const 230)
(i32.const 14))
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
(call $read
(get_local $fd)
(i32.const 255)
(i32.const 14))
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 $removeFile
(call $unlink
(i32.const 200))
2018-06-18 02:35:10 +00:00
(drop))
(func $main (result i32)
(local $fd i32)
;; set $fd to the file descriptor
(set_local $fd
(call $openFile))
2018-06-18 02:35:10 +00:00
(call $writeHelloWorld
(get_local $fd))
2018-06-18 02:35:10 +00:00
;; close file
(call $close
(get_local $fd))
2018-06-18 01:48:10 +00:00
;; set $fd to the new file descriptor
(set_local $fd
(call $openFile))
2018-06-18 01:48:10 +00:00
2018-06-18 02:35:10 +00:00
;; read Hello, World
(call $readHello
(get_local $fd))
2018-06-18 02:35:10 +00:00
;; istty
(call $istty
(get_local $fd))
2018-06-18 01:48:10 +00:00
(drop)
2018-06-18 02:35:10 +00:00
;; close file again
(call $close
(get_local $fd))
2018-06-18 02:35:10 +00:00
;; unlink filename
(call $removeFile)
2018-06-18 01:48:10 +00:00
;; return 0
(set_local $fd
(i32.const 0))
2018-06-18 02:35:10 +00:00
(drop))
2018-06-18 01:48:10 +00:00
(export "main" (func $main)))