From 1e71fcd3777b154c901701743ae3930e472e86fc Mon Sep 17 00:00:00 2001 From: techforindustry Date: Thu, 12 Sep 2019 17:03:08 -0600 Subject: [PATCH] Initial puts commit --- cmd/wasm-run/main.go | 5 ++ internal/emlibc/resolver.go | 114 +++++++++++++++++++++++++++++ internal/emlibc/test/puts.go | 3 + internal/emlibc/test/puts.wasm | Bin 0 -> 160 bytes internal/emlibc/test/puts.wasm.map | 1 + internal/emlibc/test/puts.wast | 35 +++++++++ internal/emlibc/test/src/puts.c | 6 ++ wasm/init_expr.go | 4 +- 8 files changed, 166 insertions(+), 2 deletions(-) create mode 100644 internal/emlibc/resolver.go create mode 100644 internal/emlibc/test/puts.go create mode 100644 internal/emlibc/test/puts.wasm create mode 100644 internal/emlibc/test/puts.wasm.map create mode 100644 internal/emlibc/test/puts.wast create mode 100644 internal/emlibc/test/src/puts.c diff --git a/cmd/wasm-run/main.go b/cmd/wasm-run/main.go index 578113c4..607d1f97 100644 --- a/cmd/wasm-run/main.go +++ b/cmd/wasm-run/main.go @@ -11,6 +11,8 @@ import ( "log" "os" + "github.com/go-interpreter/wagon/internal/emlibc" + "github.com/go-interpreter/wagon/exec" "github.com/go-interpreter/wagon/validate" "github.com/go-interpreter/wagon/wasm" @@ -95,6 +97,9 @@ func run(w io.Writer, fname string, verify bool) { } func importer(name string) (*wasm.Module, error) { + if name == "env" { + return emlibc.ResolveEnv(name) + } f, err := os.Open(name + ".wasm") if err != nil { return nil, err diff --git a/internal/emlibc/resolver.go b/internal/emlibc/resolver.go new file mode 100644 index 00000000..00ed719d --- /dev/null +++ b/internal/emlibc/resolver.go @@ -0,0 +1,114 @@ +package emlibc + +import ( + "errors" + "fmt" + "reflect" + + "github.com/go-interpreter/wagon/exec" + + "github.com/go-interpreter/wagon/wasm" +) + +func ResolveEnv(name string) (*wasm.Module, error) { + if name == "env" { + return GetEnv(), nil + } + fmt.Println("tried resolve", name) + return nil, errors.New("Not Found") +} +func clen(n []byte) int { + for i := 0; i < len(n); i++ { + if n[i] == 0 { + return i + } + } + return len(n) +} +func GetEnv() *wasm.Module { + + m := wasm.NewModule() + print := func(proc *exec.Process, v int32) int32 { + fmt.Printf("result = %v\n", v) + return 0 + } + puts := func(proc *exec.Process, v int32) int32 { + + buf := []byte{} + temp := make([]byte, 1) + for i := int(v); i < proc.MemSize(); i++ { + _, err := proc.ReadAt(temp, int64(i)) + if err != nil { + fmt.Println(err) + } + if temp[0] == 0 { + break + } + buf = append(buf, temp[0]) + } + fmt.Println(string(buf)) + return 0 + } + m.Types = &wasm.SectionTypes{ + Entries: []wasm.FunctionSig{ + { + Form: 0, // value for the 'func' type constructor + ParamTypes: []wasm.ValueType{wasm.ValueTypeI32}, + ReturnTypes: []wasm.ValueType{wasm.ValueTypeI32}, + }, + }, + } + m.GlobalIndexSpace = []wasm.GlobalEntry{ + { + Type: wasm.GlobalVar{ + Type: wasm.ValueTypeI32, + }, + Init: []byte{65, 0, 11}, + }, + } + // m.LinearMemoryIndexSpace = [][]byte{make([]byte, 256)} + m.Memory = &wasm.SectionMemories{ + Entries: []wasm.Memory{ + { + Limits: wasm.ResizableLimits{Initial: 1}, + }, + }, + } + m.FunctionIndexSpace = []wasm.Function{ + { + Sig: &m.Types.Entries[0], + Host: reflect.ValueOf(print), + Body: &wasm.FunctionBody{}, // create a dummy wasm body (the actual value will be taken from Host.) + }, + { + Sig: &m.Types.Entries[0], + Host: reflect.ValueOf(puts), + Body: &wasm.FunctionBody{}, // create a dummy wasm body (the actual value will be taken from Host.) + }, + } + m.Export = &wasm.SectionExports{ + Entries: map[string]wasm.ExportEntry{ + "print": { + FieldStr: "print", + Kind: wasm.ExternalFunction, + Index: 0, + }, + "_puts": { + FieldStr: "_puts", + Kind: wasm.ExternalFunction, + Index: 1, + }, + "__memory_base": { + FieldStr: "__memory_base", + Kind: wasm.ExternalGlobal, + Index: 0, + }, + "memory": { + FieldStr: "memory", + Kind: wasm.ExternalMemory, + Index: 0, + }, + }, + } + return m +} diff --git a/internal/emlibc/test/puts.go b/internal/emlibc/test/puts.go new file mode 100644 index 00000000..6dc8acf1 --- /dev/null +++ b/internal/emlibc/test/puts.go @@ -0,0 +1,3 @@ +//go:generate emcc -Os src/puts.c -s SIDE_MODULE=1 -o puts.wasm -s TOTAL_MEMORY=65536 -s TOTAL_STACK=4096 + +package test diff --git a/internal/emlibc/test/puts.wasm b/internal/emlibc/test/puts.wasm new file mode 100644 index 0000000000000000000000000000000000000000..915f303bba83e6ad2dc588a82df9eb399766ec84 GIT binary patch literal 160 zcmW-YK?=e!5Jmq?h@zoTbmyXi?M1rt7Sjk>v@~fkQE-(W&7;}6_>aef4;wuJph9(& zDR + + +int main(){ + puts("Hello"); +} \ No newline at end of file diff --git a/wasm/init_expr.go b/wasm/init_expr.go index 3a59fb3a..e05b37d0 100644 --- a/wasm/init_expr.go +++ b/wasm/init_expr.go @@ -144,7 +144,8 @@ func (m *Module) ExecInitExpr(expr []byte) (interface{}, error) { if globalVar == nil { return nil, InvalidGlobalIndexError(index) } - lastVal = globalVar.Type.Type + return m.ExecInitExpr(globalVar.Init) + // lastVal = globalVar.Type.Type case end: break default: @@ -155,7 +156,6 @@ func (m *Module) ExecInitExpr(expr []byte) (interface{}, error) { if len(stack) == 0 { return nil, nil } - v := stack[len(stack)-1] switch lastVal { case ValueTypeI32: