Skip to content

Commit 9654c5e

Browse files
committed
cmd/compile: allow wasmexport used without exportname
1 parent 42f9ee9 commit 9654c5e

File tree

4 files changed

+23
-10
lines changed

4 files changed

+23
-10
lines changed

src/cmd/compile/doc.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -320,17 +320,19 @@ by ``importmodule'' and ``importname''. For example,
320320
321321
causes g to refer to the WebAssembly function f from module a_module.
322322
323-
//go:wasmexport exportname
323+
//go:wasmexport [exportname]
324324
325325
The //go:wasmexport directive is wasm-only and must be followed by a
326326
function definition.
327327
It specifies that the function is exported to the wasm host as ``exportname''.
328+
If exportname is omitted, the function's own name will be used as the export name.
328329
For example,
329-
330330
//go:wasmexport h
331331
func hWasm() { ... }
332-
333-
make Go function hWasm available outside this WebAssembly module as h.
332+
makes Go function hWasm available outside this WebAssembly module as h.
333+
//go:wasmexport
334+
func hWasm() { ... }
335+
makes Go function hWasm available outside this WebAssembly module as hWasm.
334336
335337
For both go:wasmimport and go:wasmexport,
336338
the types of parameters and return values to the Go function are translated to

src/cmd/compile/internal/noder/noder.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -258,19 +258,23 @@ func (p *noder) pragma(pos syntax.Pos, blankLine bool, text string, old syntax.P
258258
}
259259
}
260260

261-
case strings.HasPrefix(text, "go:wasmexport "):
261+
case strings.HasPrefix(text, "go:wasmexport"):
262262
f := strings.Fields(text)
263-
if len(f) != 2 {
264-
// TODO: maybe make the name optional? It was once mentioned on proposal 65199.
265-
p.error(syntax.Error{Pos: pos, Msg: "usage: //go:wasmexport exportname"})
263+
if len(f) > 2 {
264+
p.error(syntax.Error{Pos: pos, Msg: "usage: //go:wasmexport [exportname]"})
266265
break
267266
}
268267

268+
var exportName string
269+
if len(f) == 2 {
270+
exportName = f[1]
271+
}
272+
269273
if buildcfg.GOARCH == "wasm" {
270274
// Only actually use them if we're compiling to WASM though.
271275
pragma.WasmExport = &WasmExport{
272276
Pos: pos,
273-
Name: f[1],
277+
Name: exportName,
274278
}
275279
}
276280

src/cmd/compile/internal/noder/writer.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1125,7 +1125,11 @@ func (w *writer) funcExt(obj *types2.Func) {
11251125
w.String("")
11261126
}
11271127
if we != nil {
1128-
w.String(we.Name)
1128+
if we.Name != "" {
1129+
w.String(we.Name)
1130+
} else {
1131+
w.String(decl.Name.Value)
1132+
}
11291133
} else {
11301134
w.String("")
11311135
}

test/wasmexport.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ package p
1313
//go:wasmexport F
1414
func F() {} // OK
1515

16+
//go:wasmexport
17+
func G() {} // OK
18+
1619
type S int32
1720

1821
//go:wasmexport M

0 commit comments

Comments
 (0)