diff --git a/Tupfile.rules b/Tupfile.rules index 6c7b69a..271061b 100644 --- a/Tupfile.rules +++ b/Tupfile.rules @@ -1 +1,3 @@ .gitignore + +: *_test.go |> go test -v -cover -race -o %o |> ./bin/%d.test diff --git a/cmd/land/Tupfile b/cmd/land/Tupfile index 5e7a943..625d9cd 100644 --- a/cmd/land/Tupfile +++ b/cmd/land/Tupfile @@ -1 +1,3 @@ +include_rules + : *.go |> go build -o %o |> ../../bin/land diff --git a/cmd/land/main.go b/cmd/land/main.go index 5f0c46c..1aa046b 100644 --- a/cmd/land/main.go +++ b/cmd/land/main.go @@ -3,6 +3,7 @@ package main import ( "context" "flag" + "fmt" "os" "github.com/Xe/ln" @@ -29,24 +30,7 @@ func main() { ln.FatalErr(ctx, err, ln.F{"msg": "can't create process", "fname": fname}) } - foundMain := false - mainID := uint32(0) - for name, entry := range p.mod.Export.Entries { - if name == "main" && entry.FieldStr == "main" { - mainID = entry.Index - foundMain = true - break - } - } + returnCode, err := p.Main() - if !foundMain { - ln.Fatal(ctx, ln.F{"msg": "no main function exported"}) - } - - returnCode, err := p.vm.ExecCode(int64(mainID)) - if err != nil { - ln.FatalErr(ctx, err) - } - - ln.Log(ctx, ln.Info("process returned code"), ln.F{"code": returnCode}) + ln.Log(ctx, ln.Info("process returned code"), ln.F{"code": returnCode, "type": fmt.Sprintf("%T", returnCode)}) } diff --git a/cmd/land/process.go b/cmd/land/process.go index e27781d..69f6575 100644 --- a/cmd/land/process.go +++ b/cmd/land/process.go @@ -114,3 +114,26 @@ func (p *Process) log(ptr int32, len int32) int32 { return 0 } + +func (p *Process) Main() (uint32, error) { + foundMain := false + mainID := uint32(0) + for name, entry := range p.mod.Export.Entries { + if name == "main" && entry.FieldStr == "main" { + mainID = entry.Index + foundMain = true + break + } + } + + if !foundMain { + return 1, errors.New("no main function found") + } + + returnCode, err := p.vm.ExecCode(int64(mainID)) + if err != nil { + return 1, err + } + + return returnCode.(uint32), nil +} diff --git a/cmd/land/process_test.go b/cmd/land/process_test.go new file mode 100644 index 0000000..98a3d0b --- /dev/null +++ b/cmd/land/process_test.go @@ -0,0 +1,28 @@ +package main + +import ( + "os" + "testing" +) + +func TestHelloWorld(t *testing.T) { + fin, err := os.Open("./testdata/hello.wasm") + if err != nil { + t.Fatal(err) + } + defer fin.Close() + + p, err := NewProcess(fin) + if err != nil { + t.Fatal(err) + } + + ret, err := p.Main() + if err != nil { + t.Fatal(err) + } + + if ret != 0 { + t.Fatalf("expected return code to be 0, got: %d", ret) + } +} diff --git a/cmd/land/testdata/.gitignore b/cmd/land/testdata/.gitignore new file mode 100644 index 0000000..45d4a47 --- /dev/null +++ b/cmd/land/testdata/.gitignore @@ -0,0 +1 @@ +!*.wasm diff --git a/cmd/land/testdata/hello.wasm b/cmd/land/testdata/hello.wasm new file mode 100644 index 0000000..1828cc6 Binary files /dev/null and b/cmd/land/testdata/hello.wasm differ