Truly ignore //export when //go:wasmexport is used (#4500)

This commit is contained in:
Ayke
2024-10-05 18:53:38 +02:00
committed by GitHub
parent 453a1d35c3
commit d1b7238a36
+20 -17
View File
@@ -273,21 +273,32 @@ func (c *compilerContext) parsePragmas(info *functionInfo, f *ssa.Function) {
if syntax == nil { if syntax == nil {
return return
} }
// Read all pragmas of this function.
var pragmas []*ast.Comment
hasWasmExport := false
if decl, ok := syntax.(*ast.FuncDecl); ok && decl.Doc != nil { if decl, ok := syntax.(*ast.FuncDecl); ok && decl.Doc != nil {
for _, comment := range decl.Doc.List { for _, comment := range decl.Doc.List {
text := comment.Text text := comment.Text
if strings.HasPrefix(text, "//export ") { if strings.HasPrefix(text, "//go:") || strings.HasPrefix(text, "//export ") {
// Rewrite '//export' to '//go:export' for compatibility with pragmas = append(pragmas, comment)
// gc. if strings.HasPrefix(comment.Text, "//go:wasmexport ") {
text = "//go:" + text[2:] hasWasmExport = true
} }
if !strings.HasPrefix(text, "//go:") { }
}
}
// Parse each pragma.
for _, comment := range pragmas {
parts := strings.Fields(comment.Text)
switch parts[0] {
case "//export", "//go:export":
if len(parts) != 2 {
continue continue
} }
parts := strings.Fields(text) if hasWasmExport {
switch parts[0] { // //go:wasmexport overrides //export.
case "//go:export":
if len(parts) != 2 {
continue continue
} }
@@ -388,14 +399,6 @@ func (c *compilerContext) parsePragmas(info *functionInfo, f *ssa.Function) {
} }
} }
} }
}
// If both //go:wasmexport and //go:export or //export are declared,
// only honor go:wasmexport.
if info.wasmExport != "" {
// TODO: log warning?
info.exported = false
}
} }
// Check whether this function can be used in //go:wasmimport or // Check whether this function can be used in //go:wasmimport or