cmd/land: add other file operations

This commit is contained in:
Cadey Ratio 2018-06-18 02:35:10 +00:00
parent 7ec598dc43
commit eedf0b024d
3 changed files with 91 additions and 25 deletions

View File

@ -101,6 +101,16 @@ func (p *Process) importer(name string) (*wasm.Module, error) {
Host: reflect.ValueOf(p.close), Host: reflect.ValueOf(p.close),
Body: &wasm.FunctionBody{}, Body: &wasm.FunctionBody{},
}, },
{
Sig: &m.Types.Entries[2],
Host: reflect.ValueOf(isatty),
Body: &wasm.FunctionBody{},
},
{
Sig: &m.Types.Entries[2],
Host: reflect.ValueOf(p.unlink),
Body: &wasm.FunctionBody{},
},
} }
m.Export = &wasm.SectionExports{ m.Export = &wasm.SectionExports{
Entries: map[string]wasm.ExportEntry{ Entries: map[string]wasm.ExportEntry{
@ -129,6 +139,16 @@ func (p *Process) importer(name string) (*wasm.Module, error) {
Kind: wasm.ExternalFunction, Kind: wasm.ExternalFunction,
Index: 4, Index: 4,
}, },
"isatty": {
FieldStr: "isatty",
Kind: wasm.ExternalFunction,
Index: 5,
},
"unlink": {
FieldStr: "unlink",
Kind: wasm.ExternalFunction,
Index: 6,
},
}, },
} }
return m, nil return m, nil
@ -277,6 +297,20 @@ func (p *Process) close(fd int32) int32 {
return 0 return 0
} }
func (p *Process) unlink(fnameP int32) int32 {
fname := string(p.readMem(fnameP))
err := p.fs.RemoveAll(fname)
if err != nil {
panic(err)
}
return 0
}
func isatty(fd int32) int32 {
return 0
}
func (p *Process) Main() (uint32, error) { func (p *Process) Main() (uint32, error) {
foundMain := false foundMain := false
mainID := uint32(0) mainID := uint32(0)

Binary file not shown.

View File

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