Skip to content

WASM: Support for setting an imported function's module name #455

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

Merged
merged 2 commits into from
Sep 12, 2019
Merged
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
5 changes: 5 additions & 0 deletions compiler/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -821,6 +821,11 @@ func (c *Compiler) parseFuncDecl(f *ir.Function) *Frame {
// External/exported functions may not retain pointer values.
// https://golang.org/cmd/cgo/#hdr-Passing_pointers
if f.IsExported() {
// Set the wasm-import-module attribute if the function's module is set.
if f.Module() != "" {
wasmImportModuleAttr := c.ctx.CreateStringAttribute("wasm-import-module", f.Module())
frame.fn.LLVMFn.AddFunctionAttr(wasmImportModuleAttr)
}
nocaptureKind := llvm.AttributeKindID("nocapture")
nocapture := c.ctx.CreateEnumAttribute(nocaptureKind, 0)
for i, typ := range paramTypes {
Expand Down
12 changes: 12 additions & 0 deletions ir/ir.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type Program struct {
type Function struct {
*ssa.Function
LLVMFn llvm.Value
module string // go:wasm-module
linkName string // go:linkname, go:export, go:interrupt
exported bool // go:export
nobounds bool // go:nobounds
Expand Down Expand Up @@ -229,6 +230,12 @@ func (f *Function) parsePragmas() {
}
f.linkName = parts[1]
f.exported = true
case "//go:wasm-module":
// Alternative comment for setting the import module.
if len(parts) != 2 {
continue
}
f.module = parts[1]
case "//go:inline":
f.inline = InlineHint
case "//go:noinline":
Expand Down Expand Up @@ -291,6 +298,11 @@ func (f *Function) Inline() InlineType {
return f.inline
}

// Return the module name if not the default.
func (f *Function) Module() string {
return f.module
}

// Return the link name for this function.
func (f *Function) LinkName() string {
if f.linkName != "" {
Expand Down
4 changes: 3 additions & 1 deletion src/examples/wasm/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
The examples here show two different ways of using WebAssembly with TinyGo:

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