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.
Этот коммит содержится в:
родитель
1dbfc976e8
коммит
55144ad608
3 изменённых файлов: 20 добавлений и 1 удалений
|
@ -821,6 +821,11 @@ func (c *Compiler) parseFuncDecl(f *ir.Function) *Frame {
|
||||||
// External/exported functions may not retain pointer values.
|
// External/exported functions may not retain pointer values.
|
||||||
// https://golang.org/cmd/cgo/#hdr-Passing_pointers
|
// https://golang.org/cmd/cgo/#hdr-Passing_pointers
|
||||||
if f.IsExported() {
|
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")
|
nocaptureKind := llvm.AttributeKindID("nocapture")
|
||||||
nocapture := c.ctx.CreateEnumAttribute(nocaptureKind, 0)
|
nocapture := c.ctx.CreateEnumAttribute(nocaptureKind, 0)
|
||||||
for i, typ := range paramTypes {
|
for i, typ := range paramTypes {
|
||||||
|
|
12
ir/ir.go
12
ir/ir.go
|
@ -28,6 +28,7 @@ type Program struct {
|
||||||
type Function struct {
|
type Function struct {
|
||||||
*ssa.Function
|
*ssa.Function
|
||||||
LLVMFn llvm.Value
|
LLVMFn llvm.Value
|
||||||
|
module string // go:wasm-module
|
||||||
linkName string // go:linkname, go:export, go:interrupt
|
linkName string // go:linkname, go:export, go:interrupt
|
||||||
exported bool // go:export
|
exported bool // go:export
|
||||||
nobounds bool // go:nobounds
|
nobounds bool // go:nobounds
|
||||||
|
@ -229,6 +230,12 @@ func (f *Function) parsePragmas() {
|
||||||
}
|
}
|
||||||
f.linkName = parts[1]
|
f.linkName = parts[1]
|
||||||
f.exported = true
|
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":
|
case "//go:inline":
|
||||||
f.inline = InlineHint
|
f.inline = InlineHint
|
||||||
case "//go:noinline":
|
case "//go:noinline":
|
||||||
|
@ -291,6 +298,11 @@ func (f *Function) Inline() InlineType {
|
||||||
return f.inline
|
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.
|
// Return the link name for this function.
|
||||||
func (f *Function) LinkName() string {
|
func (f *Function) LinkName() string {
|
||||||
if f.linkName != "" {
|
if f.linkName != "" {
|
||||||
|
|
|
@ -3,7 +3,9 @@
|
||||||
The examples here show two different ways of using WebAssembly with TinyGo:
|
The examples here show two different ways of using WebAssembly with TinyGo:
|
||||||
|
|
||||||
1. Defining and exporting functions via the `//go:export <name>` directive. See
|
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
|
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
|
standard library implementation works. See [the main folder](./main) for an
|
||||||
example of this.
|
example of this.
|
||||||
|
|
Загрузка…
Создание таблицы
Сослаться в новой задаче