-
Notifications
You must be signed in to change notification settings - Fork 162
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
5 changed files
with
101 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters