@@ -21,12 +21,13 @@ import (
21
21
// The linkName value contains a valid link name, even if //go:linkname is not
22
22
// present.
23
23
type functionInfo struct {
24
- module string // go:wasm-module
25
- linkName string // go:linkname, go:export
26
- exported bool // go:export, CGo
27
- nobounds bool // go:nobounds
28
- variadic bool // go:variadic (CGo only)
29
- inline inlineType // go:inline
24
+ module string // go:wasm-module
25
+ importName string // go:linkname, go:export - The name the developer assigns
26
+ linkName string // go:linkname, go:export - The name that we map for the particular module -> importName
27
+ exported bool // go:export, CGo
28
+ nobounds bool // go:nobounds
29
+ variadic bool // go:variadic (CGo only)
30
+ inline inlineType // go:inline
30
31
}
31
32
32
33
type inlineType int
@@ -151,8 +152,16 @@ func (c *compilerContext) getFunction(fn *ssa.Function) llvm.Value {
151
152
if info .exported {
152
153
// Set the wasm-import-module attribute if the function's module is set.
153
154
if info .module != "" {
155
+
156
+ // We need to add the wasm-import-module and the wasm-import-name
154
157
wasmImportModuleAttr := c .ctx .CreateStringAttribute ("wasm-import-module" , info .module )
155
158
llvmFn .AddFunctionAttr (wasmImportModuleAttr )
159
+
160
+ // Add the Wasm Import Name, if we are a named wasm import
161
+ if info .importName != "" {
162
+ wasmImportNameAttr := c .ctx .CreateStringAttribute ("wasm-import-name" , info .importName )
163
+ llvmFn .AddFunctionAttr (wasmImportNameAttr )
164
+ }
156
165
}
157
166
nocaptureKind := llvm .AttributeKindID ("nocapture" )
158
167
nocapture := c .ctx .CreateEnumAttribute (nocaptureKind , 0 )
@@ -191,6 +200,10 @@ func (info *functionInfo) parsePragmas(f *ssa.Function) {
191
200
return
192
201
}
193
202
if decl , ok := f .Syntax ().(* ast.FuncDecl ); ok && decl .Doc != nil {
203
+
204
+ // Our importName for a wasm module (if we are compiling to wasm), or llvm link name
205
+ var importName string
206
+
194
207
for _ , comment := range decl .Doc .List {
195
208
text := comment .Text
196
209
if strings .HasPrefix (text , "//export " ) {
@@ -207,7 +220,8 @@ func (info *functionInfo) parsePragmas(f *ssa.Function) {
207
220
if len (parts ) != 2 {
208
221
continue
209
222
}
210
- info .linkName = parts [1 ]
223
+
224
+ importName = parts [1 ]
211
225
info .exported = true
212
226
case "//go:wasm-module" :
213
227
// Alternative comment for setting the import module.
@@ -250,6 +264,17 @@ func (info *functionInfo) parsePragmas(f *ssa.Function) {
250
264
}
251
265
}
252
266
}
267
+
268
+ // Set the importName for our exported function if we have one
269
+ if importName != "" {
270
+ if info .module == "" {
271
+ info .linkName = importName
272
+ } else {
273
+ // WebAssembly import
274
+ info .importName = importName
275
+ }
276
+ }
277
+
253
278
}
254
279
}
255
280
0 commit comments