Skip to content

Commit

Permalink
Merge #24
Browse files Browse the repository at this point in the history
24: feat(go-wasmer) New package to get CLI support r=Hywan a=Hywan

Partially address #17.

1. `go install github.com/wasmerio/go-ext-wasm/go-wasmer`
2. `go-wasmer call nbody.wasm main 10`

Co-authored-by: Ivan Enderlin <[email protected]>
  • Loading branch information
bors[bot] and Hywan committed Jun 3, 2019
2 parents b3baaf4 + f7bbf67 commit 1b8bfc0
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 6 deletions.
13 changes: 9 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,17 @@ Wasmer is a Go library for executing WebAssembly binaries.
To install the library, follow the classical:

```sh
$ CGO_ENABLED=1 go install github.com/wasmerio/go-ext-wasm/wasmer
$ go get github.com/wasmerio/go-ext-wasm/wasmer
```

The `CGO_ENABLED` part is very likely to be optional. `go install`
will work on many macOS and Linux distributions. It will not work on
Windows yet, we are working on it.
To install the `go-wasmer` CLI, follow the classical:

```sh
$ go install github.com/wasmerio/go-ext-wasm/go-wasmer
```

`go install` will work on many macOS and Linux distributions. It will
not work on Windows yet, we are working on it.

# Documentation

Expand Down
81 changes: 81 additions & 0 deletions go-wasmer/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package main

import (
"fmt"
"github.com/urfave/cli"
wasm "github.com/wasmerio/go-ext-wasm/wasmer"
"log"
"os"
"strconv"
)

func main() {
app := cli.NewApp()
app.Name = "go-wasmer"
app.Usage = "Run WebAssembly binaries."
app.Version = "0.1.0"

app.Commands = []cli.Command{
{
Name: "call",
Aliases: []string{},
Usage: "Call a WebAssembly exported function with arguments: <wasm-file> <function-name> <arguments...>",
Action: func(c *cli.Context) error {
if c.NArg() < 2 {
return cli.NewExitError(fmt.Sprintf("`call` expects at least 2 arguments: <wasm-file> and <function-name>; given %d.", c.NArg()), 1)
}

arguments := c.Args()
wasmFile := arguments[0]
functionName := arguments[1]
functionArguments := arguments[2:]

bytes, err := wasm.ReadBytes(wasmFile)

if err != nil {
return cli.NewExitError(fmt.Sprintf("`call` — Failed to read bytes from `%s`: %s.", wasmFile, err), 2)
}

instance, err := wasm.NewInstance(bytes)

if err != nil {
return cli.NewExitError(fmt.Sprintf("`call` — Failed to instantiate `%s`: %s.", wasmFile, err), 3)
}

exportedFunction, exists := instance.Exports[functionName]

if !exists {
return cli.NewExitError(fmt.Sprintf("`call` — Exported function `%s` does not exist on the WebAssembly module `%s`.", functionName, wasmFile), 4)
}

exportedFunctionArguments := make([]interface{}, len(functionArguments))

for nth, functionArgument := range functionArguments {
parsed, err := strconv.ParseInt(functionArgument, 10, 32)

if err != nil {
return cli.NewExitError(fmt.Sprintf("`call` — Failed to parse the #%d argument `%s` of the exported function `%s` of the WebAssembly module `%s`: %s.", nth+1, functionArgument, functionName, wasmFile, err), 5)
}

exportedFunctionArguments[nth] = (int32)(parsed)
}

result, err := exportedFunction(exportedFunctionArguments...)

if err != nil {
return cli.NewExitError(fmt.Sprintf("`call` — Exported function `%s` of the WebAssembly module `%s` failed: %s.", functionName, wasmFile, err), 6)
}

fmt.Println(result)

return nil
},
},
}

err := app.Run(os.Args)

if err != nil {
log.Fatal(err)
}
}
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ module github.com/wasmerio/go-ext-wasm

go 1.12

require github.com/stretchr/testify v1.3.0
require (
github.com/stretchr/testify v1.3.0
github.com/urfave/cli v1.20.0
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/urfave/cli v1.20.0 h1:fDqGv3UG/4jbVl/QkFwEdddtEDjh/5Ov6X+0B/3bPaw=
github.com/urfave/cli v1.20.0/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA=
6 changes: 5 additions & 1 deletion justfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Compile the library.
# Build the `wasmer` library.
build go-build-args='-v':
#!/usr/bin/env bash
set -euo pipefail
Expand All @@ -19,6 +19,10 @@ build go-build-args='-v':
fi
go build {{go-build-args}} .
# Build the `go-wasmer` bin.
build-bin go-build-args='-v':
cd go-wasmer && go build {{go-build-args}} -o ../target/go/go-wasmer .
# Compile the Rust part for this specific system.
rust:
cargo build --release
Expand Down

0 comments on commit 1b8bfc0

Please sign in to comment.