Skip to content

Commit

Permalink
调整生成导入代码位置
Browse files Browse the repository at this point in the history
  • Loading branch information
chai2010 committed Nov 20, 2023
1 parent 6ecc8a2 commit bb9fd9c
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 34 deletions.
31 changes: 16 additions & 15 deletions internal/backends/compiler_wat/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,10 @@ func (p *Compiler) CompileWsFiles(prog *loader.Program) {
p.module.BaseWat = sb.String()
}

func (p *Compiler) CompileWhostFiles(prog *loader.Program) string {
func (p *Compiler) CompileWImportFiles(prog *loader.Program) string {
var sb strings.Builder

sb.WriteString(waroot.GetBaseWhostCode(p.prog.Cfg.WaOS))
sb.WriteString(waroot.GetBaseImportCode(p.prog.Cfg.WaOS))
sb.WriteString("\n")

var pkgpathList = make([]string, 0, len(prog.Pkgs))
Expand All @@ -142,7 +142,7 @@ func (p *Compiler) CompileWhostFiles(prog *loader.Program) string {

for _, pkgpath := range pkgpathList {
pkg := prog.Pkgs[pkgpath]
if len(pkg.WhostFiles) == 0 {
if len(pkg.WImportFiles) == 0 {
continue
}

Expand All @@ -152,7 +152,7 @@ func (p *Compiler) CompileWhostFiles(prog *loader.Program) string {
sb.WriteString(lineCommentSep)
sb.WriteString("\n")

for _, sf := range pkg.WhostFiles {
for _, sf := range pkg.WImportFiles {
sb.WriteString("// file: " + sf.Name + "\n")
sb.WriteString("\n")

Expand Down Expand Up @@ -374,10 +374,11 @@ type JSFunc struct {
}

type JSModule struct {
Filename string
Pkg string
Globals []JSGlobal
Funcs []JSFunc
Filename string
Pkg string
Globals []JSGlobal
Funcs []JSFunc
ImportCode string
}

func stripNamePrefix(name string) string {
Expand Down Expand Up @@ -481,20 +482,20 @@ func (p *Compiler) funcsForJSBinding() []JSFunc {
}

func (p *Compiler) GenJSBinding(wasmFilename string) string {
var bf bytes.Buffer
bf.WriteString(p.CompileWhostFiles(p.prog))

// 模板
t, err := template.New("js").Parse(js_binding_tmpl)
if err != nil {
logger.Fatal(err)
}
data := JSModule{
Filename: wasmFilename,
Pkg: p.prog.Manifest.MainPkg,
Globals: p.globalsForJsBinding(),
Funcs: p.funcsForJSBinding(),
Filename: wasmFilename,
Pkg: p.prog.Manifest.MainPkg,
Globals: p.globalsForJsBinding(),
Funcs: p.funcsForJSBinding(),
ImportCode: p.CompileWImportFiles(p.prog),
}

var bf bytes.Buffer
err = t.Execute(&bf, data)
if err != nil {
logger.Fatal(err)
Expand Down
3 changes: 2 additions & 1 deletion internal/backends/compiler_wat/js_binding_tmpl.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,8 @@ class WaApp {
}

let imports = {
syscall_js: syscall
syscall_js: syscall,
{{$.ImportCode}}
}

WebAssembly.instantiateStreaming(fetch(url), imports).then(res => {
Expand Down
10 changes: 5 additions & 5 deletions internal/loader/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ type Program struct {

// 单个包对象
type Package struct {
Pkg *types.Package // 类型检查后的包
Info *types.Info // 包的类型检查信息
Files []*ast.File // AST语法树
WsFiles []*WsFile // 汇编代码
WhostFiles []*WhostFile // 宿主代码文件
Pkg *types.Package // 类型检查后的包
Info *types.Info // 包的类型检查信息
Files []*ast.File // AST语法树
WsFiles []*WsFile // 汇编代码
WImportFiles []*WhostFile // 宿主代码文件

SSAPkg *ssa.Package
TestInfo *TestInfo
Expand Down
6 changes: 3 additions & 3 deletions internal/loader/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ func (p *_Loader) Import(pkgpath string) (*types.Package, error) {
}

// 解析当前包的宿主代码
pkg.WhostFiles, err = p.ParseDir_wahostFiles(pkgpath)
pkg.WImportFiles, err = p.ParseDir_hostImpoertFiles(pkgpath)
if err != nil {
logger.Tracef(&config.EnableTrace_loader, "err: %v", err)
return nil, err
Expand Down Expand Up @@ -433,15 +433,15 @@ func (p *_Loader) ParseDir_wsFiles(pkgpath string) (files []*WsFile, err error)
return
}

func (p *_Loader) ParseDir_wahostFiles(pkgpath string) (files []*WhostFile, err error) {
func (p *_Loader) ParseDir_hostImpoertFiles(pkgpath string) (files []*WhostFile, err error) {
logger.Tracef(&config.EnableTrace_loader, "pkgpath: %v", pkgpath)

if p.cfg.WaOS == "" {
panic("unreachable")
}

var (
extNames = []string{fmt.Sprintf(".wa-host.%s", p.cfg.WaOS)}
extNames = []string{fmt.Sprintf(".import.%s", p.cfg.WaOS)}
unitTestMode bool = false

filenames []string
Expand Down
File renamed without changes.
Empty file removed waroot/src/base.wa-host.wasi
Empty file.
17 changes: 7 additions & 10 deletions waroot/waroot.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,8 @@ var baseWsFile_clang string
//go:embed src/base.wat.ws
var baseWsFile_wat string

//go:embed src/base.wa-host.js
var baseWhostFile_js string

//go:embed src/base.wa-host.wasi
var baseWhostFile_wasi string
//go:embed src/base.import.js
var baseImportFile_js string

// 获取汇编基础代码
func GetBaseWsCode(backend string) string {
Expand All @@ -70,14 +67,14 @@ func GetBaseWsCode(backend string) string {
}

// 获取宿主基础代码
func GetBaseWhostCode(waos string) string {
func GetBaseImportCode(waos string) string {
switch waos {
case config.WaOS_unknown:
return baseWhostFile_js
case config.WaOS_js:
return baseWhostFile_js
return baseImportFile_js
case config.WaOS_unknown:
return ""
case config.WaOS_wasi:
return baseWhostFile_wasi
return ""
}
for _, s := range config.WaBackend_List {
if s == waos {
Expand Down

0 comments on commit bb9fd9c

Please sign in to comment.