From eedf0b024db71ddc4977094e495977fd90c33dc4 Mon Sep 17 00:00:00 2001 From: Christine Dodrill Date: Mon, 18 Jun 2018 02:35:10 +0000 Subject: [PATCH] cmd/land: add other file operations --- cmd/land/process.go | 34 ++++++++++++++ cmd/land/testdata/fileops.wasm | Bin 184 -> 260 bytes userland/src/lib/fileops.wast | 82 +++++++++++++++++++++++---------- 3 files changed, 91 insertions(+), 25 deletions(-) diff --git a/cmd/land/process.go b/cmd/land/process.go index 1d5e9fd..c4e1662 100644 --- a/cmd/land/process.go +++ b/cmd/land/process.go @@ -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) diff --git a/cmd/land/testdata/fileops.wasm b/cmd/land/testdata/fileops.wasm index 71d662eccff013d944cdf2176311c10e5980321f..c0eb0a47812ecee0ae444f77b103b95c13777438 100644 GIT binary patch literal 260 zcmXYpJx;_h5QX3PZ?iF4xpo1Y4k0e#4o*NDi@1pJIw%ROsC+$6!ZpB7C}!q+Pwx%< zav=b8t9r_mdzS0wc{?ab89l(B6%XlCzQ$<@KxHw#hXmT}>pZRjY_sR!haJ_p_;vl- zDF})R`Bo{(*$q965NL>7w(6%q~2=`6y!H~Ye8SaPN$uPT%-zvD(P0bi-^*vEG qsw+WrD1(1)3Jbd#M~4np>pNCES`2>m^{YNTg&3#D^L3iz(6xV5Su(Kz delta 114 zcmZo++QF#8kXW3{$iTqBD9)0=SkG9Wz*G++nd`w617kgtKFdUHeJ3VHW>#iK21a%c zMwZ;f%sd7bE(1nAM#g%_6O4{p0*s0b3Ji|V7#;Zpn4}aK1Q?Ko|AT~?Cq7gL01k8& A^Z)<= diff --git a/userland/src/lib/fileops.wast b/userland/src/lib/fileops.wast index 2086629..97559bd 100644 --- a/userland/src/lib/fileops.wast +++ b/userland/src/lib/fileops.wast @@ -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)))