Skip to content

Commit

Permalink
Remove wat2wasm
Browse files Browse the repository at this point in the history
  • Loading branch information
sorawit committed Jun 21, 2020
1 parent 65caeda commit 62c6ca4
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 135 deletions.
2 changes: 0 additions & 2 deletions go-owasm/api/bindings.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,5 +87,3 @@ typedef struct Env {
Error do_compile(Span input, Span *output);

Error do_run(Span code, uint32_t gas_limit, bool is_prepare, Env env);

Error do_wat2wasm(Span input, Span *output);
31 changes: 31 additions & 0 deletions go-owasm/api/common_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,36 @@
package api

import (
"io/ioutil"
"os"
"os/exec"
)

// wat2wasm compiles the given Wat content to Wasm, relying on the host's wat2wasm program.
func wat2wasm(wat []byte) []byte {
inputFile, err := ioutil.TempFile("", "input")
if err != nil {
panic(err)
}
defer os.Remove(inputFile.Name())
outputFile, err := ioutil.TempFile("", "output")
if err != nil {
panic(err)
}
defer os.Remove(outputFile.Name())
if _, err := inputFile.Write(wat); err != nil {
panic(err)
}
if err := exec.Command("wat2wasm", inputFile.Name(), "-o", outputFile.Name()).Run(); err != nil {
panic(err)
}
output, err := ioutil.ReadFile(outputFile.Name())
if err != nil {
panic(err)
}
return output
}

type RawRequest struct {
ExternalID int64
DataSourceID int64
Expand Down
9 changes: 0 additions & 9 deletions go-owasm/api/lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,3 @@ func run(code []byte, gasLimit uint32, isPrepare bool, env EnvInterface) error {
},
}))
}

func Wat2Wasm(code []byte, spanSize int) ([]byte, error) {
inputSpan := copySpan(code)
defer freeSpan(inputSpan)
outputSpan := newSpan(spanSize)
defer freeSpan(outputSpan)
err := parseErrorFromC(C.do_wat2wasm(inputSpan, &outputSpan))
return readSpan(outputSpan), err
}
68 changes: 19 additions & 49 deletions go-owasm/api/lib_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,35 +26,11 @@ func readWasmFile(fileName string) []byte {

func TestSuccessWatToOwasm(t *testing.T) {
code := readWatFile("test")
spanSize := 1 * 1024 * 1024
wasm, err := Wat2Wasm(code, spanSize)
require.NoError(t, err)

wasm := wat2wasm(code)
expectedWasm := readWasmFile("test")
require.Equal(t, expectedWasm, wasm)
}

func TestFailEmptyWatContent(t *testing.T) {
code := []byte("")
spanSize := 1 * 1024 * 1024
_, err := Wat2Wasm(code, spanSize)
require.Equal(t, ErrParseFail, err)
}

func TestFailInvalidWatContent(t *testing.T) {
code := []byte("invalid wat content")
spanSize := 1 * 1024 * 1024
_, err := Wat2Wasm(code, spanSize)
require.Equal(t, ErrParseFail, err)
}

func TestFailSpanExceededCapacity(t *testing.T) {
code := readWatFile("test")
smallSpanSize := 10
_, err := Wat2Wasm(code, smallSpanSize)
require.EqualError(t, err, "span exceeded capacity")
}

func TestFailCompileInvalidContent(t *testing.T) {
code := []byte("invalid content")
spanSize := 1 * 1024 * 1024
Expand All @@ -63,7 +39,7 @@ func TestFailCompileInvalidContent(t *testing.T) {
}
func TestRunError(t *testing.T) {
spanSize := 1 * 1024 * 1024
wasm, _ := Wat2Wasm([]byte(`(module
wasm := wat2wasm([]byte(`(module
(type (func (param i64 i64 i32 i64) (result i64)))
(func
i32.const 0
Expand All @@ -76,7 +52,7 @@ func TestRunError(t *testing.T) {
(export "prepare" (func 0))
(export "execute" (func 1)))
`), spanSize)
`))
code, _ := Compile(wasm, spanSize)

err := Prepare(code, 100000, NewMockEnv([]byte("")))
Expand All @@ -85,7 +61,7 @@ func TestRunError(t *testing.T) {

func TestInvaildSignature(t *testing.T) {
spanSize := 1 * 1024 * 1024
wasm, _ := Wat2Wasm([]byte(`(module
wasm := wat2wasm([]byte(`(module
(func (param i64 i64 i32 i64)
(local $idx i32)
(set_local $idx (i32.const 0))
Expand All @@ -99,7 +75,7 @@ func TestInvaildSignature(t *testing.T) {
(memory 17)
(export "prepare" (func 0))
(export "execute" (func 1)))
`), spanSize)
`))
code, _ := Compile(wasm, spanSize)

err := Prepare(code, 100000, NewMockEnv([]byte("")))
Expand All @@ -109,7 +85,7 @@ func TestInvaildSignature(t *testing.T) {

func TestGasLimit(t *testing.T) {
spanSize := 1 * 1024 * 1024
wasm, _ := Wat2Wasm([]byte(`(module
wasm := wat2wasm([]byte(`(module
(type (func (param i64 i64 i32 i64) (result i64)))
(func
(local $idx i32)
Expand All @@ -124,7 +100,7 @@ func TestGasLimit(t *testing.T) {
(memory 17)
(export "prepare" (func 0))
(export "execute" (func 1)))
`), spanSize)
`))
code, err := Compile(wasm, spanSize)
err = Prepare(code, 100000, NewMockEnv([]byte("")))
require.NoError(t, err)
Expand All @@ -135,7 +111,7 @@ func TestGasLimit(t *testing.T) {

func TestCompileErrorNoMemory(t *testing.T) {
spanSize := 1 * 1024 * 1024
wasm, _ := Wat2Wasm([]byte(`(module
wasm := wat2wasm([]byte(`(module
(type (func (param i64 i64 i32 i64) (result i64)))
(func
(local $idx i32)
Expand All @@ -150,16 +126,15 @@ func TestCompileErrorNoMemory(t *testing.T) {
(export "prepare" (func 0))
(export "execute" (func 1)))
`), spanSize)
`))
code, err := Compile(wasm, spanSize)

require.Equal(t, ErrNoMemoryWasm, err)
require.Equal(t, []uint8([]byte{}), code)
}

func TestCompileErrorMinimumMemoryExceed(t *testing.T) {
spanSize := 1 * 1024 * 1024
wasm, _ := Wat2Wasm([]byte(`(module
wasm := wat2wasm([]byte(`(module
(type (func (param i64 i64 i32 i64) (result i64)))
(func
(local $idx i32)
Expand All @@ -175,11 +150,11 @@ func TestCompileErrorMinimumMemoryExceed(t *testing.T) {
(export "prepare" (func 0))
(export "execute" (func 1)))
`), spanSize)
`))
_, err := Compile(wasm, spanSize)
require.NoError(t, err)

wasm, _ = Wat2Wasm([]byte(`(module
wasm = wat2wasm([]byte(`(module
(type (func (param i64 i64 i32 i64) (result i64)))
(func
(local $idx i32)
Expand All @@ -195,14 +170,14 @@ func TestCompileErrorMinimumMemoryExceed(t *testing.T) {
(export "prepare" (func 0))
(export "execute" (func 1)))
`), spanSize)
`))
_, err = Compile(wasm, spanSize)
require.Equal(t, ErrMinimumMemoryExceed, err)
}

func TestCompileErrorSetMaximumMemory(t *testing.T) {
spanSize := 1 * 1024 * 1024
wasm, _ := Wat2Wasm([]byte(`(module
wasm := wat2wasm([]byte(`(module
(type (func (param i64 i64 i32 i64) (result i64)))
(func
(local $idx i32)
Expand All @@ -218,17 +193,15 @@ func TestCompileErrorSetMaximumMemory(t *testing.T) {
(export "prepare" (func 0))
(export "execute" (func 1)))
`), spanSize)
`))
code, err := Compile(wasm, spanSize)

require.Equal(t, ErrSetMaximumMemory, err)
require.Equal(t, []uint8([]byte{}), code)
}

func TestCompileErrorCheckWasmImports(t *testing.T) {
spanSize := 1 * 1024 * 1024

wasm, _ := Wat2Wasm([]byte(`(module
wasm := wat2wasm([]byte(`(module
(type (func (param i64 i64 i32 i64) (result i64)))
(import "env" "beeb" (func (type 0)))
(func
Expand All @@ -246,17 +219,15 @@ func TestCompileErrorCheckWasmImports(t *testing.T) {
(data (i32.const 1048576) "beeb")
(export "prepare" (func 0))
(export "execute" (func 1)))
`), spanSize)
`))
code, err := Compile(wasm, spanSize)

require.Equal(t, ErrCheckWasmImports, err)
require.Equal(t, []uint8([]byte{}), code)
}

func TestCompileErrorCheckWasmExports(t *testing.T) {
spanSize := 1 * 1024 * 1024

wasm, _ := Wat2Wasm([]byte(`(module
wasm := wat2wasm([]byte(`(module
(type (func (param i64 i64 i32 i64) (result i64)))
(import "env" "ask_external_data" (func (type 0)))
(func
Expand All @@ -272,9 +243,8 @@ func TestCompileErrorCheckWasmExports(t *testing.T) {
(memory 17)
(data (i32.const 1048576) "beeb")
(export "prepare" (func 0)))
`), spanSize)
`))
code, err := Compile(wasm, spanSize)

require.Equal(t, ErrCheckWasmExports, err)
require.Equal(t, []uint8([]byte{}), code)
}
43 changes: 17 additions & 26 deletions go-owasm/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@ package main

import (
"fmt"
"io/ioutil"
"os"

"github.com/bandprotocol/bandchain/go-owasm/api"
)

type Env struct{}
Expand Down Expand Up @@ -42,31 +38,26 @@ func (e *Env) GetExternalData(eid int64, vid int64) []byte {
return []byte("switez")
}

func Wat2Wasm(fileName string, spanSize int) error {
code, _ := ioutil.ReadFile(fmt.Sprintf("./wasm/%s.wat", fileName))
wasm, err := api.Wat2Wasm(code, spanSize)
if err != nil {
panic(err)
}
// func Wat2Wasm(fileName string, spanSize int) error {
// code, _ := ioutil.ReadFile(fmt.Sprintf("./wasm/%s.wat", fileName))
// wasm, err := api.Wat2Wasm(code, spanSize)
// if err != nil {
// panic(err)
// }

fmt.Println("wasm:", wasm)
f, err := os.Create(fmt.Sprintf("./wasm/%s.wasm", fileName))
defer f.Close()
n, err := f.Write(wasm)
fmt.Printf("wrote %d bytes\n", n)
if err != nil {
return err
}
f.Sync()
// fmt.Println("wasm:", wasm)
// f, err := os.Create(fmt.Sprintf("./wasm/%s.wasm", fileName))
// defer f.Close()
// n, err := f.Write(wasm)
// fmt.Printf("wrote %d bytes\n", n)
// if err != nil {
// return err
// }
// f.Sync()

return nil
}
// return nil
// }

func main() {
fmt.Println("Hello, World!")
code, _ := ioutil.ReadFile("./wasm/fun3.wat")
spanSize := 1 * 1024 * 1024
wasm, e := api.Wat2Wasm(code, spanSize)
fmt.Println("wasm", wasm)
fmt.Println(e)
}
14 changes: 0 additions & 14 deletions go-owasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,20 +54,6 @@ pub extern "C" fn do_run(code: Span, gas_limit: u32, is_prepare: bool, env: Env)
}
}

#[no_mangle]
pub extern "C" fn do_wat2wasm(input: Span, output: &mut Span) -> Error {
match wat2wasm(input.read()) {
Ok(_wasm) => output.write(&_wasm),
Err(e) => match e.kind() {
wabt::ErrorKind::Parse(_) => Error::ParseError,
wabt::ErrorKind::WriteBinary => Error::WriteBinaryError,
wabt::ErrorKind::ResolveNames(_) => Error::ResolveNamesError,
wabt::ErrorKind::Validate(_) => Error::ValidateError,
_ => Error::UnknownError,
},
}
}

fn inject_memory(module: Module) -> Result<Module, Error> {
let mut m = module;
let section = match m.memory_section() {
Expand Down
9 changes: 0 additions & 9 deletions go-owasm/wasm/fun.wat

This file was deleted.

26 changes: 0 additions & 26 deletions go-owasm/wasm/fun3.wat

This file was deleted.

0 comments on commit 62c6ca4

Please sign in to comment.