Skip to content

Commit 55144ad

Browse files
pkedydeadprogram
authored andcommitted
WASM: Support for setting an imported function's module name (#455)
* wasm: add support for setting a function's Wasm import module name by using the //go:wasm-module comment.
1 parent 1dbfc97 commit 55144ad

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

compiler/compiler.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -821,6 +821,11 @@ func (c *Compiler) parseFuncDecl(f *ir.Function) *Frame {
821821
// External/exported functions may not retain pointer values.
822822
// https://golang.org/cmd/cgo/#hdr-Passing_pointers
823823
if f.IsExported() {
824+
// Set the wasm-import-module attribute if the function's module is set.
825+
if f.Module() != "" {
826+
wasmImportModuleAttr := c.ctx.CreateStringAttribute("wasm-import-module", f.Module())
827+
frame.fn.LLVMFn.AddFunctionAttr(wasmImportModuleAttr)
828+
}
824829
nocaptureKind := llvm.AttributeKindID("nocapture")
825830
nocapture := c.ctx.CreateEnumAttribute(nocaptureKind, 0)
826831
for i, typ := range paramTypes {

ir/ir.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ type Program struct {
2828
type Function struct {
2929
*ssa.Function
3030
LLVMFn llvm.Value
31+
module string // go:wasm-module
3132
linkName string // go:linkname, go:export, go:interrupt
3233
exported bool // go:export
3334
nobounds bool // go:nobounds
@@ -229,6 +230,12 @@ func (f *Function) parsePragmas() {
229230
}
230231
f.linkName = parts[1]
231232
f.exported = true
233+
case "//go:wasm-module":
234+
// Alternative comment for setting the import module.
235+
if len(parts) != 2 {
236+
continue
237+
}
238+
f.module = parts[1]
232239
case "//go:inline":
233240
f.inline = InlineHint
234241
case "//go:noinline":
@@ -291,6 +298,11 @@ func (f *Function) Inline() InlineType {
291298
return f.inline
292299
}
293300

301+
// Return the module name if not the default.
302+
func (f *Function) Module() string {
303+
return f.module
304+
}
305+
294306
// Return the link name for this function.
295307
func (f *Function) LinkName() string {
296308
if f.linkName != "" {

src/examples/wasm/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
The examples here show two different ways of using WebAssembly with TinyGo:
44

55
1. Defining and exporting functions via the `//go:export <name>` directive. See
6-
[the export folder](./export) for an example of this.
6+
[the export folder](./export) for an example of this. Additionally, the Wasm
7+
module (which has a default value of `env`) can be specified using
8+
`//go:wasm-module <module>`.
79
1. Defining and executing a `func main()`. This is similar to how the Go
810
standard library implementation works. See [the main folder](./main) for an
911
example of this.

0 commit comments

Comments
 (0)