aboutsummaryrefslogtreecommitdiff
path: root/cmd
diff options
context:
space:
mode:
authorXe <me@christine.website>2023-01-01 23:04:07 -0500
committerXe <me@christine.website>2023-01-01 23:04:07 -0500
commite763c835a35d0a52acb9d3fde6e37a4b260b2100 (patch)
treef40de600ebdcf2d2e8852d0ea262cbc963735f54 /cmd
parentf58bd3056a1bb00801aedb6ebe6916978c043ee2 (diff)
downloadx-e763c835a35d0a52acb9d3fde6e37a4b260b2100.tar.xz
x-e763c835a35d0a52acb9d3fde6e37a4b260b2100.zip
cmd/hlang: move run to its own package
Signed-off-by: Xe <me@christine.website>
Diffstat (limited to 'cmd')
-rw-r--r--cmd/hlang/http.go5
-rw-r--r--cmd/hlang/main.go3
-rw-r--r--cmd/hlang/run/run.go (renamed from cmd/hlang/run.go)18
-rw-r--r--cmd/hlang/run/run_test.go (renamed from cmd/hlang/run_test.go)4
-rw-r--r--cmd/hlang/run/testdata/h.wasm (renamed from cmd/hlang/testdata/h.wasm)bin69 -> 69 bytes
5 files changed, 21 insertions, 9 deletions
diff --git a/cmd/hlang/http.go b/cmd/hlang/http.go
index 2dc61b6..ef496a3 100644
--- a/cmd/hlang/http.go
+++ b/cmd/hlang/http.go
@@ -11,6 +11,7 @@ import (
"github.com/rs/cors"
"within.website/ln/ex"
+ "within.website/x/cmd/hlang/run"
)
var (
@@ -75,7 +76,7 @@ func runPlayground(w http.ResponseWriter, r *http.Request) {
return
}
- er, err := run(comp.Binary)
+ er, err := run.Run(comp.Binary)
if err != nil {
httpError(w, fmt.Errorf("runtime error: %v", err), http.StatusInternalServerError)
return
@@ -84,7 +85,7 @@ func runPlayground(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(struct {
Program *CompiledProgram `json:"prog"`
- Results *ExecResult `json:"res"`
+ Results *run.ExecResult `json:"res"`
}{
Program: comp,
Results: er,
diff --git a/cmd/hlang/main.go b/cmd/hlang/main.go
index 69f8f82..538e0a9 100644
--- a/cmd/hlang/main.go
+++ b/cmd/hlang/main.go
@@ -7,6 +7,7 @@ import (
"log"
"os"
+ "within.website/x/cmd/hlang/run"
"within.website/x/internal"
)
@@ -46,7 +47,7 @@ func oneOff() error {
}
log.Println("running...")
- er, err := run(comp.Binary)
+ er, err := run.Run(comp.Binary)
if err != nil {
return err
}
diff --git a/cmd/hlang/run.go b/cmd/hlang/run/run.go
index baa6756..5526df4 100644
--- a/cmd/hlang/run.go
+++ b/cmd/hlang/run/run.go
@@ -1,28 +1,38 @@
-package main
+package run
import (
"context"
"time"
"github.com/tetratelabs/wazero"
+ "github.com/tetratelabs/wazero/api"
)
type Process struct {
Output []byte
}
-func (p *Process) Putchar(char int32) {
+func (p *Process) Putchar(ctx context.Context, stack []uint64) {
+ x := api.DecodeI32(stack[0])
+ p.putchar(x)
+}
+
+func (p *Process) putchar(char int32) {
p.Output = append(p.Output, byte(char))
}
-func run(bin []byte) (*ExecResult, error) {
+func Run(bin []byte) (*ExecResult, error) {
ctx := context.Background()
r := wazero.NewRuntime(ctx)
defer r.Close(ctx)
p := &Process{}
- env, err := r.NewHostModuleBuilder("h").NewFunctionBuilder().WithFunc(func(char int32) { p.Putchar(char) }).Export("h").Instantiate(ctx, r)
+ env, err := r.NewHostModuleBuilder("h").
+ NewFunctionBuilder().
+ WithGoFunction(api.GoFunc(p.Putchar), []api.ValueType{api.ValueTypeI32}, nil).
+ Export("h").
+ Instantiate(ctx, r)
if err != nil {
return nil, err
}
diff --git a/cmd/hlang/run_test.go b/cmd/hlang/run/run_test.go
index 9fe38cb..5e2fdae 100644
--- a/cmd/hlang/run_test.go
+++ b/cmd/hlang/run/run_test.go
@@ -1,4 +1,4 @@
-package main
+package run
import (
_ "embed"
@@ -10,7 +10,7 @@ var bin []byte
func BenchmarkRun(b *testing.B) {
for i := 0; i < b.N; i++ {
- if _, err := run(bin); err != nil {
+ if _, err := Run(bin); err != nil {
b.Fatal(err)
}
}
diff --git a/cmd/hlang/testdata/h.wasm b/cmd/hlang/run/testdata/h.wasm
index 6a0d90d..6a0d90d 100644
--- a/cmd/hlang/testdata/h.wasm
+++ b/cmd/hlang/run/testdata/h.wasm
Binary files differ