简化 init 工程模板和 run 命令实现

This commit is contained in:
chai2010
2023-11-22 22:21:27 +08:00
parent f9ae1f5784
commit 4d7cc1a8e2
19 changed files with 58 additions and 180 deletions

View File

@@ -12,7 +12,7 @@ import (
)
// 执行凹代码
func RunCode(cfg *config.Config, filename, code string, args ...string) (stdoutStderr []byte, err error) {
func RunCode(cfg *config.Config, filename, code string, mainFunc string, args ...string) (stdoutStderr []byte, err error) {
// 编译为 wat 格式
watBytes, err := BuildFile(cfg, filename, code)
if err != nil {
@@ -26,7 +26,7 @@ func RunCode(cfg *config.Config, filename, code string, args ...string) (stdoutS
}
// main 执行
stdout, stderr, err := wazero.RunWasm(cfg, filename, wasmBytes, args...)
stdout, stderr, err := wazero.RunWasm(cfg, filename, wasmBytes, mainFunc, args...)
stdoutStderr = append(stdout, stderr...)
return
}

View File

@@ -22,7 +22,7 @@ func ExampleRunCode() {
}
`
output, err := api.RunCode(api.DefaultConfig(), "hello.wa", code)
output, err := api.RunCode(api.DefaultConfig(), "hello.wa", code, "__main__.main")
if err != nil {
log.Fatal(err)
}
@@ -48,7 +48,7 @@ func ExampleRunCode_args() {
cfg.WaOS = api.WaOS_wasi
args := []string{"aa", "bb"}
output, err := api.RunCode(cfg, "hello.wa", code, args...)
output, err := api.RunCode(cfg, "hello.wa", code, "__main__.main", args...)
if err != nil {
if len(output) != 0 {
log.Println(string(output))
@@ -74,7 +74,7 @@ func ExampleRunCode_wz() {
`
output, err := api.RunCode(api.DefaultConfig(), "hello.wa", code)
output, err := api.RunCode(api.DefaultConfig(), "hello.wa", code, "__main__.main")
if err != nil {
if len(output) != 0 {
log.Println(string(output))