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),
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{
Entries: map[string]wasm.ExportEntry{
@ -129,6 +139,16 @@ func (p *Process) importer(name string) (*wasm.Module, error) {
Kind: wasm.ExternalFunction,
Index: 4,
},
"isatty": {
FieldStr: "isatty",
Kind: wasm.ExternalFunction,
Index: 5,
},
"unlink": {
FieldStr: "unlink",
Kind: wasm.ExternalFunction,
Index: 6,
},
},
}
return m, nil
@ -277,6 +297,20 @@ func (p *Process) close(fd int32) int32 {
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) {
foundMain := false
mainID := uint32(0)

Binary file not shown.

View File

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