A virtual jailed shell environment for Go apps backed by an io/fs#FS.
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

feat(command): add python3 backed by wasi

Signed-off-by: Xe Iaso <me@xeiaso.net>

Xe Iaso 90803a1a 4399ab67

+62
command/internal/python3/python.wasm

This is a binary file and will not be displayed.

+62
command/internal/python3/python3.go
··· 1 + package python3 2 + 3 + import ( 4 + "context" 5 + _ "embed" 6 + 7 + "github.com/tetratelabs/wazero" 8 + "github.com/tetratelabs/wazero/imports/wasi_snapshot_preview1" 9 + "tangled.org/xeiaso.net/kefka/command" 10 + ) 11 + 12 + var ( 13 + //go:embed python.wasm 14 + pyWASM []byte 15 + 16 + r wazero.Runtime 17 + code wazero.CompiledModule 18 + ) 19 + 20 + func init() { 21 + ctx := context.Background() 22 + r = wazero.NewRuntime(ctx) 23 + 24 + wasi_snapshot_preview1.MustInstantiate(ctx, r) 25 + 26 + var err error 27 + code, err = r.CompileModule(ctx, pyWASM) 28 + if err != nil { 29 + panic(err) 30 + } 31 + } 32 + 33 + type Impl struct{} 34 + 35 + func (Impl) Exec(ctx context.Context, ec *command.ExecContext, args []string) error { 36 + fsConfig := wazero.NewFSConfig(). 37 + WithFSMount(ec.FS, "/") 38 + 39 + config := wazero.NewModuleConfig(). 40 + // stdio 41 + WithStdin(ec.Stdin). 42 + WithStdout(ec.Stdout). 43 + WithStderr(ec.Stderr). 44 + // argv 45 + WithArgs(append([]string{"python3"}, args...)...). 46 + WithName("python3"). 47 + // filesystem 48 + WithFSConfig(fsConfig). 49 + // time 50 + WithSysNanosleep(). 51 + WithSysNanotime(). 52 + WithSysWalltime() 53 + 54 + mod, err := r.InstantiateModule(ctx, code, config) 55 + if err != nil { 56 + return err 57 + } 58 + 59 + defer mod.Close(ctx) 60 + 61 + return nil 62 + }