File tree Expand file tree Collapse file tree 3 files changed +20
-1
lines changed Expand file tree Collapse file tree 3 files changed +20
-1
lines changed Original file line number Diff line number Diff 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 {
Original file line number Diff line number Diff line change @@ -28,6 +28,7 @@ type Program struct {
2828type 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.
295307func (f * Function ) LinkName () string {
296308 if f .linkName != "" {
Original file line number Diff line number Diff line change 33The examples here show two different ways of using WebAssembly with TinyGo:
44
551 . 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> ` .
791 . Defining and executing a ` func main() ` . This is similar to how the Go
810standard library implementation works. See [ the main folder] ( ./main ) for an
911example of this.
You can’t perform that action at this time.
0 commit comments