Skip to content

cmd/compile: allow wasmexport used without exportname #73774

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions src/cmd/compile/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,17 +320,19 @@ by ``importmodule'' and ``importname''. For example,

causes g to refer to the WebAssembly function f from module a_module.

//go:wasmexport exportname
//go:wasmexport [exportname]

The //go:wasmexport directive is wasm-only and must be followed by a
function definition.
It specifies that the function is exported to the wasm host as ``exportname''.
If exportname is omitted, the function's own name will be used as the export name.
For example,

//go:wasmexport h
func hWasm() { ... }

make Go function hWasm available outside this WebAssembly module as h.
makes Go function hWasm available outside this WebAssembly module as h.
//go:wasmexport
func hWasm() { ... }
makes Go function hWasm available outside this WebAssembly module as hWasm.

For both go:wasmimport and go:wasmexport,
the types of parameters and return values to the Go function are translated to
Expand Down
14 changes: 9 additions & 5 deletions src/cmd/compile/internal/noder/noder.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,19 +258,23 @@ func (p *noder) pragma(pos syntax.Pos, blankLine bool, text string, old syntax.P
}
}

case strings.HasPrefix(text, "go:wasmexport "):
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"})
if len(f) > 2 {
p.error(syntax.Error{Pos: pos, Msg: "usage: //go:wasmexport [exportname]"})
break
}

var exportName string
if len(f) == 2 {
exportName = f[1]
}

if buildcfg.GOARCH == "wasm" {
// Only actually use them if we're compiling to WASM though.
pragma.WasmExport = &WasmExport{
Pos: pos,
Name: f[1],
Name: exportName,
}
}

Expand Down
6 changes: 5 additions & 1 deletion src/cmd/compile/internal/noder/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -1125,7 +1125,11 @@ func (w *writer) funcExt(obj *types2.Func) {
w.String("")
}
if we != nil {
w.String(we.Name)
if we.Name != "" {
w.String(we.Name)
} else {
w.String(decl.Name.Value)
}
} else {
w.String("")
}
Expand Down
3 changes: 3 additions & 0 deletions test/wasmexport.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ package p
//go:wasmexport F
func F() {} // OK

//go:wasmexport
func G() {} // OK

type S int32

//go:wasmexport M
Expand Down