diff options
Diffstat (limited to 'src/cmd/compile/internal/noder')
| -rw-r--r-- | src/cmd/compile/internal/noder/linker.go | 5 | ||||
| -rw-r--r-- | src/cmd/compile/internal/noder/noder.go | 27 | ||||
| -rw-r--r-- | src/cmd/compile/internal/noder/reader.go | 17 | ||||
| -rw-r--r-- | src/cmd/compile/internal/noder/writer.go | 13 |
4 files changed, 57 insertions, 5 deletions
diff --git a/src/cmd/compile/internal/noder/linker.go b/src/cmd/compile/internal/noder/linker.go index f5667f57ab..486013c7df 100644 --- a/src/cmd/compile/internal/noder/linker.go +++ b/src/cmd/compile/internal/noder/linker.go @@ -278,6 +278,11 @@ func (l *linker) relocFuncExt(w *pkgbits.Encoder, name *ir.Name) { w.String("") w.String("") } + if name.Func.WasmExport != nil { + w.String(name.Func.WasmExport.Name) + } else { + w.String("") + } } // Relocated extension data. diff --git a/src/cmd/compile/internal/noder/noder.go b/src/cmd/compile/internal/noder/noder.go index 1652dc6618..7905c374c5 100644 --- a/src/cmd/compile/internal/noder/noder.go +++ b/src/cmd/compile/internal/noder/noder.go @@ -171,6 +171,7 @@ type pragmas struct { Pos []pragmaPos // position of each individual flag Embeds []pragmaEmbed WasmImport *WasmImport + WasmExport *WasmExport } // WasmImport stores metadata associated with the //go:wasmimport pragma @@ -180,6 +181,12 @@ type WasmImport struct { Name string } +// WasmExport stores metadata associated with the //go:wasmexport pragma +type WasmExport struct { + Pos syntax.Pos + Name string +} + type pragmaPos struct { Flag ir.PragmaFlag Pos syntax.Pos @@ -204,6 +211,9 @@ func (p *noder) checkUnusedDuringParse(pragma *pragmas) { if pragma.WasmImport != nil { p.error(syntax.Error{Pos: pragma.WasmImport.Pos, Msg: "misplaced go:wasmimport directive"}) } + if pragma.WasmExport != nil { + p.error(syntax.Error{Pos: pragma.WasmExport.Pos, Msg: "misplaced go:wasmexport directive"}) + } } // pragma is called concurrently if files are parsed concurrently. @@ -246,6 +256,23 @@ func (p *noder) pragma(pos syntax.Pos, blankLine bool, text string, old syntax.P Name: f[2], } } + + case strings.HasPrefix(text, "go:wasmexport "): + f := strings.Fields(text) + if len(f) != 2 { + // TODO: maybe make the name optional? It was once mentioned on proposal 65199. + p.error(syntax.Error{Pos: pos, Msg: "usage: //go:wasmexport exportname"}) + break + } + + if buildcfg.GOARCH == "wasm" { + // Only actually use them if we're compiling to WASM though. + pragma.WasmExport = &WasmExport{ + Pos: pos, + Name: f[1], + } + } + case strings.HasPrefix(text, "go:linkname "): f := strings.Fields(text) if !(2 <= len(f) && len(f) <= 3) { diff --git a/src/cmd/compile/internal/noder/reader.go b/src/cmd/compile/internal/noder/reader.go index ff44adedb4..1dd2e09b0d 100644 --- a/src/cmd/compile/internal/noder/reader.go +++ b/src/cmd/compile/internal/noder/reader.go @@ -1132,15 +1132,22 @@ func (r *reader) funcExt(name *ir.Name, method *types.Sym) { r.linkname(name) if buildcfg.GOARCH == "wasm" { - xmod := r.String() - xname := r.String() + importmod := r.String() + importname := r.String() + exportname := r.String() - if xmod != "" && xname != "" { + if importmod != "" && importname != "" { fn.WasmImport = &ir.WasmImport{ - Module: xmod, - Name: xname, + Module: importmod, + Name: importname, } } + if exportname != "" { + if method != nil { + base.ErrorfAt(fn.Pos(), 0, "cannot use //go:wasmexport on a method") + } + fn.WasmExport = &ir.WasmExport{Name: exportname} + } } if r.Bool() { diff --git a/src/cmd/compile/internal/noder/writer.go b/src/cmd/compile/internal/noder/writer.go index c1560941b8..9f862f9a4c 100644 --- a/src/cmd/compile/internal/noder/writer.go +++ b/src/cmd/compile/internal/noder/writer.go @@ -1050,6 +1050,7 @@ func (w *writer) funcExt(obj *types2.Func) { w.p.errorf(decl, "go:nosplit and go:systemstack cannot be combined") } wi := asWasmImport(decl.Pragma) + we := asWasmExport(decl.Pragma) if decl.Body != nil { if pragma&ir.Noescape != 0 { @@ -1104,6 +1105,11 @@ func (w *writer) funcExt(obj *types2.Func) { w.String("") w.String("") } + if we != nil { + w.String(we.Name) + } else { + w.String("") + } } w.Bool(false) // stub extension @@ -3011,6 +3017,13 @@ func asWasmImport(p syntax.Pragma) *WasmImport { return p.(*pragmas).WasmImport } +func asWasmExport(p syntax.Pragma) *WasmExport { + if p == nil { + return nil + } + return p.(*pragmas).WasmExport +} + // isPtrTo reports whether from is the type *to. func isPtrTo(from, to types2.Type) bool { ptr, ok := types2.Unalias(from).(*types2.Pointer) |
