aboutsummaryrefslogtreecommitdiff
path: root/cmd
diff options
context:
space:
mode:
authorXe <me@christine.website>2023-01-01 13:11:43 -0500
committerXe <me@christine.website>2023-01-01 13:11:43 -0500
commit88ab7e70b442a45a67e4a9f4f1ee79b0aa622273 (patch)
tree0d821d3dcce14d37ed78679301ff735b35665714 /cmd
parent2ab0ff014798d89b1bb6e6e26adc664443fa495c (diff)
downloadx-88ab7e70b442a45a67e4a9f4f1ee79b0aa622273.tar.xz
x-88ab7e70b442a45a67e4a9f4f1ee79b0aa622273.zip
switch to wazero
Signed-off-by: Xe <me@christine.website>
Diffstat (limited to 'cmd')
-rw-r--r--cmd/hlang/h/parser_test.go11
-rw-r--r--cmd/hlang/http.go2
-rw-r--r--cmd/hlang/main.go1
-rw-r--r--cmd/hlang/nguh/compile_test.go19
-rw-r--r--cmd/hlang/run.go70
-rw-r--r--cmd/hlang/run_test.go17
-rw-r--r--cmd/hlang/testdata/h.wasmbin0 -> 69 bytes
7 files changed, 70 insertions, 50 deletions
diff --git a/cmd/hlang/h/parser_test.go b/cmd/hlang/h/parser_test.go
new file mode 100644
index 0000000..c55cd7d
--- /dev/null
+++ b/cmd/hlang/h/parser_test.go
@@ -0,0 +1,11 @@
+package h
+
+import "testing"
+
+func BenchmarkParse(b *testing.B) {
+ for i := 0; i < b.N; i++ {
+ if _, err := Parse("h h h"); err != nil {
+ b.Fatal(err)
+ }
+ }
+}
diff --git a/cmd/hlang/http.go b/cmd/hlang/http.go
index 4a7f9df..2dc61b6 100644
--- a/cmd/hlang/http.go
+++ b/cmd/hlang/http.go
@@ -384,7 +384,6 @@ const playgroundTemplate = `<html>
const programData = document.getElementById("program").value;
const output = document.getElementById("output");
const astBox = document.getElementById("ast_box");
- const gasUsed = document.getElementById("gas_used");
const execTime = document.getElementById("exec_time");
const status = document.getElementById("status");
@@ -400,7 +399,6 @@ const playgroundTemplate = `<html>
status.innerHTML = "success";
astBox.innerHTML = data.prog.ast;
output.innerHTML = data.res.out;
- gasUsed.innerHTML = data.res.gas;
execTime.innerHTML = data.res.exec_duration;
})
.catch(function(error) {
diff --git a/cmd/hlang/main.go b/cmd/hlang/main.go
index c17b5e7..69f8f82 100644
--- a/cmd/hlang/main.go
+++ b/cmd/hlang/main.go
@@ -53,7 +53,6 @@ func oneOff() error {
log.Println("success!")
- log.Printf("gas used:\t%d", er.GasUsed)
log.Printf("exec time:\t%s", er.ExecTime)
log.Println("output:")
fmt.Print(er.Output)
diff --git a/cmd/hlang/nguh/compile_test.go b/cmd/hlang/nguh/compile_test.go
index 2b130f9..698fb7b 100644
--- a/cmd/hlang/nguh/compile_test.go
+++ b/cmd/hlang/nguh/compile_test.go
@@ -1,8 +1,6 @@
package nguh
import (
- "encoding/json"
- "os"
"testing"
"github.com/eaburns/peggy/peg"
@@ -11,10 +9,23 @@ import (
func TestCompile(t *testing.T) {
inp := &peg.Node{Text: "h"}
- result, err := Compile(inp)
+ _, err := Compile(inp)
if err != nil {
t.Fatal(err)
}
+}
+
+func BenchmarkCompile(b *testing.B) {
+ inp := &peg.Node{Kids: []*peg.Node{
+ {Text: "h"},
+ {Text: "h"},
+ {Text: "h"},
+ },
+ }
- json.NewEncoder(os.Stdout).Encode(result)
+ for i := 0; i < b.N; i++ {
+ if _, err := Compile(inp); err != nil {
+ b.Fatal(err)
+ }
+ }
}
diff --git a/cmd/hlang/run.go b/cmd/hlang/run.go
index fb55bb7..baa6756 100644
--- a/cmd/hlang/run.go
+++ b/cmd/hlang/run.go
@@ -1,73 +1,57 @@
package main
import (
- "errors"
+ "context"
"time"
- "github.com/perlin-network/life/compiler"
- "github.com/perlin-network/life/exec"
+ "github.com/tetratelabs/wazero"
)
type Process struct {
Output []byte
}
-// ResolveGlobal does nothing, currently.
-func (p *Process) ResolveGlobal(module, field string) int64 { return 0 }
-
-// ResolveFunc resolves h's ABI and importable function.
-func (p *Process) ResolveFunc(module, field string) exec.FunctionImport {
- switch module {
- case "h":
- switch field {
- case "h":
- return func(vm *exec.VirtualMachine) int64 {
- frame := vm.GetCurrentFrame()
- data := frame.Locals[0]
- p.Output = append(p.Output, byte(data))
-
- return 0
- }
-
- default:
- panic("impossible state")
- }
-
- default:
- panic("impossible state")
- }
-}
-
-type ExecResult struct {
- Output string `json:"out"`
- GasUsed uint64 `json:"gas"`
- ExecTime time.Duration `json:"exec_duration"`
+func (p *Process) Putchar(char int32) {
+ p.Output = append(p.Output, byte(char))
}
func run(bin []byte) (*ExecResult, error) {
+ ctx := context.Background()
+ r := wazero.NewRuntime(ctx)
+ defer r.Close(ctx)
+
p := &Process{}
- var cfg exec.VMConfig
- gp := &compiler.SimpleGasPolicy{GasPerInstruction: 1}
- vm, err := exec.NewVirtualMachine(bin, cfg, p, gp)
+ env, err := r.NewHostModuleBuilder("h").NewFunctionBuilder().WithFunc(func(char int32) { p.Putchar(char) }).Export("h").Instantiate(ctx, r)
if err != nil {
return nil, err
}
+ defer env.Close(ctx)
- mainFunc, ok := vm.GetFunctionExport("h")
- if !ok {
- return nil, errors.New("impossible state: no h function exposed")
+ code, err := r.CompileModule(ctx, bin)
+ if err != nil {
+ return nil, err
}
- t0 := time.Now()
- _, err = vm.Run(mainFunc)
+ mod, err := r.InstantiateModule(ctx, code, wazero.NewModuleConfig())
if err != nil {
return nil, err
}
+ defer mod.Close(ctx)
+
+ t0 := time.Now()
+ if _, err = mod.ExportedFunction("h").Call(ctx); err != nil {
+ return nil, err
+ }
+ runTime := time.Since(t0)
return &ExecResult{
Output: string(p.Output),
- GasUsed: vm.Gas,
- ExecTime: time.Since(t0),
+ ExecTime: runTime,
}, nil
}
+
+type ExecResult struct {
+ Output string `json:"out"`
+ ExecTime time.Duration `json:"exec_duration"`
+}
diff --git a/cmd/hlang/run_test.go b/cmd/hlang/run_test.go
new file mode 100644
index 0000000..9fe38cb
--- /dev/null
+++ b/cmd/hlang/run_test.go
@@ -0,0 +1,17 @@
+package main
+
+import (
+ _ "embed"
+ "testing"
+)
+
+//go:embed testdata/h.wasm
+var bin []byte
+
+func BenchmarkRun(b *testing.B) {
+ for i := 0; i < b.N; i++ {
+ if _, err := run(bin); err != nil {
+ b.Fatal(err)
+ }
+ }
+}
diff --git a/cmd/hlang/testdata/h.wasm b/cmd/hlang/testdata/h.wasm
new file mode 100644
index 0000000..6a0d90d
--- /dev/null
+++ b/cmd/hlang/testdata/h.wasm
Binary files differ