Skip to content

Commit 58eaf58

Browse files
committed
feat: add repl demo
Signed-off-by: Christian Stewart <christian@aperture.us>
1 parent 828038c commit 58eaf58

5 files changed

Lines changed: 66 additions & 0 deletions

File tree

.github/renovate.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@
1212
"matchManagers": ["gomod"],
1313
"matchDepTypes": ["replace"],
1414
"enabled": false
15+
},
16+
{
17+
"matchManagers": ["gomod"],
18+
"matchPackageNames": [
19+
"github.com/paralin/go-quickjs-wasi",
20+
],
21+
"enabled": false
1522
}
1623
]
1724
}

repl/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
vendor
2+
repl

repl/go.mod

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module github.com/paralin/go-quickjs-wasi/repl
2+
3+
go 1.24.4
4+
5+
require (
6+
github.com/paralin/go-quickjs-wasi v0.10.2-0.20250704023701-828038c31a0f
7+
github.com/tetratelabs/wazero v1.9.0
8+
)

repl/go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
github.com/paralin/go-quickjs-wasi v0.10.2-0.20250704023701-828038c31a0f h1:aKm7CtT5xA1lbVZ0QiSVZcg8igR8LoUw8BMiwjHrggU=
2+
github.com/paralin/go-quickjs-wasi v0.10.2-0.20250704023701-828038c31a0f/go.mod h1:xCQtRFXtSfOH63fUqZNtIZRBsYFcpo09xb1vLBD9VgU=
3+
github.com/tetratelabs/wazero v1.9.0 h1:IcZ56OuxrtaEz8UYNRHBrUa9bYeX9oVY93KspZZBf/I=
4+
github.com/tetratelabs/wazero v1.9.0/go.mod h1:TSbcXCfFP0L2FGkRPxHphadXPjo1T6W+CseNNY7EkjM=

repl/repl.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"log"
6+
"os"
7+
8+
quickjswasi "github.com/paralin/go-quickjs-wasi"
9+
"github.com/tetratelabs/wazero"
10+
"github.com/tetratelabs/wazero/imports/wasi_snapshot_preview1"
11+
"github.com/tetratelabs/wazero/sys"
12+
)
13+
14+
func main() {
15+
// Choose the context to use for function calls.
16+
ctx := context.Background()
17+
18+
// Create a new WebAssembly Runtime.
19+
r := wazero.NewRuntime(ctx)
20+
defer r.Close(ctx) // This closes everything this Runtime created.
21+
22+
// Configure the module with stdin, stdout, and stderr.
23+
config := wazero.NewModuleConfig().
24+
WithStdin(os.Stdin).
25+
WithStdout(os.Stdout).
26+
WithStderr(os.Stderr)
27+
28+
// Instantiate WASI, which implements system call APIs.
29+
// This is required for the Wasm module to print to the console.
30+
wasi_snapshot_preview1.MustInstantiate(ctx, r)
31+
32+
// Instantiate the Wasm module with just the wasm filename as argument.
33+
// This will automatically run the "_start" function of the module.
34+
mod, err := r.InstantiateWithConfig(ctx, quickjswasi.QuickJSWASM, config.WithArgs(quickjswasi.QuickJSWASMFilename))
35+
if err != nil {
36+
// Note: Most compilers do not exit the module after running "_start",
37+
// unless there was an error. This allows you to call exported functions.
38+
if exitErr, ok := err.(*sys.ExitError); ok && exitErr.ExitCode() != 0 {
39+
log.Panicf("exit_code: %d\n", exitErr.ExitCode())
40+
} else if !ok {
41+
log.Panicln(err)
42+
}
43+
}
44+
_ = mod
45+
}

0 commit comments

Comments
 (0)