mirror of
https://gitee.com/wa-lang/wa.git
synced 2025-12-06 17:19:15 +08:00
修复 wa play 命令
This commit is contained in:
@@ -72,15 +72,16 @@ func LoadProgramVFS(vfs *config.PkgVFS, cfg *config.Config, pkgPath string) (*Pr
|
||||
}
|
||||
|
||||
// 构建 wat 目标
|
||||
func BuildFile(cfg *config.Config, filename string, src interface{}) (wat []byte, err error) {
|
||||
func BuildFile(cfg *config.Config, filename string, src interface{}) (mainFunc string, wat []byte, err error) {
|
||||
prog, err := LoadProgramFile(cfg, filename, src)
|
||||
if err != nil || prog == nil {
|
||||
logger.Tracef(&config.EnableTrace_api, "LoadProgramVFS failed, err = %v", err)
|
||||
return nil, err
|
||||
return "", nil, err
|
||||
}
|
||||
|
||||
mainFunc = prog.Manifest.MainPkg + ".main"
|
||||
watOut, err := compiler_wat.New().Compile(prog)
|
||||
return []byte(watOut), err
|
||||
return mainFunc, []byte(watOut), err
|
||||
}
|
||||
|
||||
// 构建 wat 目标
|
||||
|
||||
@@ -12,9 +12,9 @@ import (
|
||||
)
|
||||
|
||||
// 执行凹代码
|
||||
func RunCode(cfg *config.Config, filename, code string, mainFunc string, args ...string) (stdoutStderr []byte, err error) {
|
||||
func RunCode(cfg *config.Config, filename, code string, args ...string) (stdoutStderr []byte, err error) {
|
||||
// 编译为 wat 格式
|
||||
watBytes, err := BuildFile(cfg, filename, code)
|
||||
mainFunc, watBytes, err := BuildFile(cfg, filename, code)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
@@ -48,7 +48,7 @@ func ExampleRunCode_args() {
|
||||
cfg.WaOS = api.WaOS_wasi
|
||||
|
||||
args := []string{"aa", "bb"}
|
||||
output, err := api.RunCode(cfg, "hello.wa", code, "__main__.main", args...)
|
||||
output, err := api.RunCode(cfg, "hello.wa", code, args...)
|
||||
if err != nil {
|
||||
if len(output) != 0 {
|
||||
log.Println(string(output))
|
||||
|
||||
@@ -20,10 +20,10 @@ var CmdDev = &cli.Command{
|
||||
Usage: "only for dev/debug",
|
||||
Flags: []cli.Flag{
|
||||
&cli.BoolFlag{
|
||||
Name: "hello",
|
||||
Name: "hello",
|
||||
},
|
||||
&cli.BoolFlag{
|
||||
Name: "count-code-lines",
|
||||
Name: "count-code-lines",
|
||||
},
|
||||
},
|
||||
Action: func(c *cli.Context) error {
|
||||
@@ -33,7 +33,7 @@ var CmdDev = &cli.Command{
|
||||
}
|
||||
|
||||
if c.Bool("hello") {
|
||||
wat, err := api.BuildFile(
|
||||
_, wat, err := api.BuildFile(
|
||||
config.DefaultConfig(),
|
||||
"hello.wa", "func main() { println(123) }",
|
||||
)
|
||||
|
||||
@@ -47,7 +47,7 @@ func (p *WebServer) compileAndRun(req *Request) (*Response, error) {
|
||||
filename = "prog.wz"
|
||||
}
|
||||
|
||||
result, err := api.RunCode(api.DefaultConfig(), filename, "__main__.main", req.Body)
|
||||
result, err := api.RunCode(api.DefaultConfig(), filename, req.Body, "")
|
||||
if err != nil {
|
||||
resp := &Response{Errors: err.Error()}
|
||||
return resp, nil
|
||||
|
||||
@@ -37,7 +37,7 @@ func tMatchErrMsg(err error, errMsg string) bool {
|
||||
}
|
||||
|
||||
func tBuildWat(waCode string) string {
|
||||
watBytes, err := api.BuildFile(api.DefaultConfig(), "main.wa", waCode)
|
||||
_, watBytes, err := api.BuildFile(api.DefaultConfig(), "main.wa", waCode)
|
||||
if err != nil {
|
||||
return err.Error()
|
||||
}
|
||||
|
||||
@@ -52,7 +52,7 @@ func Add(a:i32, b:i32) => i32 {
|
||||
}
|
||||
|
||||
func tBuildWasm(t *testing.T, waCode string) []byte {
|
||||
watBytes, err := api.BuildFile(api.DefaultConfig(), "main.wa", waCode)
|
||||
_, watBytes, err := api.BuildFile(api.DefaultConfig(), "main.wa", waCode)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user