Skip to content

Commit

Permalink
简化 run-wasm 命令
Browse files Browse the repository at this point in the history
  • Loading branch information
chai2010 committed Jun 20, 2024
1 parent 225f8e2 commit 1494ca2
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 27 deletions.
2 changes: 1 addition & 1 deletion api/api_nowasm.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func RunCode(cfg *config.Config, filename, code string, args ...string) (stdoutS
}

// main 执行
stdout, stderr, err := wazero.RunWasm(cfg, filename, wasmBytes, mainFunc, args...)
stdout, stderr, err := wazero.RunWasm(filename, wasmBytes, mainFunc, args...)
stdoutStderr = append(stdout, stderr...)
return
}
16 changes: 9 additions & 7 deletions internal/app/apprun-wasm/apprun.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,14 @@ import (
)

var CmdRunWasm = &cli.Command{
Name: "run-wasm",
Usage: "run wasm program",
Name: "run-wasm",
Usage: "run wasm program",
ArgsUsage: "file.wasm",
Flags: []cli.Flag{
appbase.MakeFlag_target(),
appbase.MakeFlag_tags(),
&cli.StringFlag{
Name: "main-func",
Usage: "set main func",
Value: "__main__.main",
Value: "_main",
},
},
Action: CmdRunAction,
Expand All @@ -35,16 +34,18 @@ func CmdRunAction(c *cli.Context) error {
}

wasmBytes, err := os.ReadFile(input)
if err != nil {
return err
}

var appArgs []string
if c.NArg() > 2 {
appArgs = c.Args().Slice()[2:]
}

var opt = appbase.BuildOptions(c)
var mainFunc = c.String("main-func")

stdout, stderr, err := wazero.RunWasm(opt.Config(), input, wasmBytes, mainFunc, appArgs...)
stdout, stderr, err := wazero.RunWasm(input, wasmBytes, mainFunc, appArgs...)
if err != nil {
if len(stdout) > 0 {
fmt.Fprint(os.Stdout, string(stdout))
Expand All @@ -56,6 +57,7 @@ func CmdRunAction(c *cli.Context) error {
os.Exit(exitCode)
}
fmt.Println(err)
return nil
}
if len(stdout) > 0 {
fmt.Fprint(os.Stdout, string(stdout))
Expand Down
2 changes: 1 addition & 1 deletion internal/app/apprun/apprun.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func CmdRunAction(c *cli.Context) error {
appArgs = c.Args().Slice()[1:]
}

stdout, stderr, err := wazero.RunWasm(opt.Config(), input, wasmBytes, mainFunc, appArgs...)
stdout, stderr, err := wazero.RunWasm(input, wasmBytes, mainFunc, appArgs...)
if err != nil {
if len(stdout) > 0 {
fmt.Fprint(os.Stdout, string(stdout))
Expand Down
6 changes: 3 additions & 3 deletions internal/app/apptest/apptest.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func runTest(cfg *config.Config, pkgpath, runPattern string, appArgs ...string)
os.Exit(1)
}

m, err := wazero.BuildModule(cfg, wasmName, wasmBytes, wasmArgs...)
m, err := wazero.BuildModule(wasmName, wasmBytes, wasmArgs...)
if err != nil {
fmt.Println(err)
os.Exit(1)
Expand Down Expand Up @@ -147,7 +147,7 @@ func runTest(cfg *config.Config, pkgpath, runPattern string, appArgs ...string)

// 重新加载
{
m, err = wazero.BuildModule(cfg, wasmName, wasmBytes, wasmArgs...)
m, err = wazero.BuildModule(wasmName, wasmBytes, wasmArgs...)
if err != nil {
fmt.Println(err)
os.Exit(1)
Expand Down Expand Up @@ -253,7 +253,7 @@ func runTest(cfg *config.Config, pkgpath, runPattern string, appArgs ...string)

// 重新加载
{
m, err = wazero.BuildModule(cfg, wasmName, wasmBytes, wasmArgs...)
m, err = wazero.BuildModule(wasmName, wasmBytes, wasmArgs...)
if err != nil {
fmt.Println(err)
os.Exit(1)
Expand Down
2 changes: 2 additions & 0 deletions internal/app/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"wa-lang.org/wa/internal/app/applsp"
"wa-lang.org/wa/internal/app/appplay"
"wa-lang.org/wa/internal/app/apprun"
apprun_wasm "wa-lang.org/wa/internal/app/apprun-wasm"
"wa-lang.org/wa/internal/app/appssa"
"wa-lang.org/wa/internal/app/apptest"
"wa-lang.org/wa/internal/app/appwat2wasm"
Expand Down Expand Up @@ -88,6 +89,7 @@ func Main() {
appinit.CmdInit,
appbuild.CmdBuild,
apprun.CmdRun,
apprun_wasm.CmdRunWasm,
appfmt.CmdFmt,
apptest.CmdTest,
appyacc.CmdYacc,
Expand Down
32 changes: 23 additions & 9 deletions internal/wazero/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ import (

// wasm 模块, 可多次执行
type Module struct {
cfg *config.Config

wasmName string
wasmBytes []byte
wasmArgs []string
Expand All @@ -38,11 +36,8 @@ type Module struct {
}

// 构建模块(会执行编译)
func BuildModule(
cfg *config.Config, wasmName string, wasmBytes []byte, wasmArgs ...string,
) (*Module, error) {
func BuildModule(wasmName string, wasmBytes []byte, wasmArgs ...string) (*Module, error) {
m := &Module{
cfg: cfg,
wasmName: wasmName,
wasmBytes: wasmBytes,
wasmArgs: wasmArgs,
Expand All @@ -61,7 +56,7 @@ func (p *Module) RunMain(mainFunc string) (stdout, stderr []byte, err error) {

if mainFunc != "" {
fn := p.wazeroModule.ExportedFunction(mainFunc)
if fn == nil {
if fn == nil && mainFunc != "_main" {
err = fmt.Errorf("wazero: func %q not found", mainFunc)
return
}
Expand Down Expand Up @@ -155,7 +150,26 @@ func (p *Module) buildModule() error {
return err
}

switch p.cfg.WaOS {
// 根据导入的函数识别宿主类型
var waOS = config.WaOS_unknown
for _, importedFunc := range p.wazeroCompileModule.ImportedFunctions() {
moduleName, funcName, isImport := importedFunc.Import()
if !isImport {
continue
}

if moduleName == "syscall_js" && funcName == "print_str" {
waOS = config.WaOS_js
break
}

if moduleName == "wasi_snapshot_preview1" {
waOS = config.WaOS_wasi
break
}
}

switch waOS {
case config.WaOS_unknown:
if _, err = UnknownInstantiate(p.wazeroCtx, p.wazeroRuntime); err != nil {
p.wazeroInitErr = err
Expand All @@ -173,7 +187,7 @@ func (p *Module) buildModule() error {
}

default:
return fmt.Errorf("unknown waos: %q", p.cfg.WaOS)
return fmt.Errorf("unknown waos: %q", waOS)
}

return nil
Expand Down
4 changes: 1 addition & 3 deletions internal/wazero/module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ func Add(a:i32, b:i32) => i32 {
`
wasmBytes := tBuildWasm(t, waCode)

m, err := wazero.BuildModule(
api.DefaultConfig(), wasmName, wasmBytes,
)
m, err := wazero.BuildModule(wasmName, wasmBytes)
if err != nil {
t.Fatal(err)
}
Expand Down
5 changes: 2 additions & 3 deletions internal/wazero/util.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package wazero

import (
"wa-lang.org/wa/internal/config"
"wa-lang.org/wazero"
"wa-lang.org/wazero/sys"
)
Expand All @@ -11,8 +10,8 @@ func Wat2Wasm(source []byte) ([]byte, error) {
}

// 单次执行 wasm
func RunWasm(cfg *config.Config, wasmName string, wasmBytes []byte, mainFunc string, wasmArgs ...string) (stdout, stderr []byte, err error) {
m, err := BuildModule(cfg, wasmName, wasmBytes, wasmArgs...)
func RunWasm(wasmName string, wasmBytes []byte, mainFunc string, wasmArgs ...string) (stdout, stderr []byte, err error) {
m, err := BuildModule(wasmName, wasmBytes, wasmArgs...)
if err != nil {
return
}
Expand Down

0 comments on commit 1494ca2

Please sign in to comment.