package wasm
import (
"github.com/tetratelabs/wazero/api"
"github.com/tetratelabs/wazero/internal/internalapi"
"github.com/tetratelabs/wazero/internal/wasmdebug"
)
func (m *Module ) ImportedFunctions () (ret []api .FunctionDefinition ) {
for i := uint32 (0 ); i < m .ImportFunctionCount ; i ++ {
ret = append (ret , m .FunctionDefinition (i ))
}
return
}
func (m *Module ) ExportedFunctions () map [string ]api .FunctionDefinition {
ret := map [string ]api .FunctionDefinition {}
for i := range m .ExportSection {
exp := &m .ExportSection [i ]
if exp .Type == ExternTypeFunc {
d := m .FunctionDefinition (exp .Index )
ret [exp .Name ] = d
}
}
return ret
}
func (m *Module ) FunctionDefinition (index Index ) *FunctionDefinition {
m .buildFunctionDefinitions ()
return &m .FunctionDefinitionSection [index ]
}
func (m *Module ) buildFunctionDefinitions () {
m .functionDefinitionSectionInitOnce .Do (m .buildFunctionDefinitionsOnce )
}
func (m *Module ) buildFunctionDefinitionsOnce () {
var moduleName string
var functionNames NameMap
var localNames , resultNames IndirectNameMap
if m .NameSection != nil {
moduleName = m .NameSection .ModuleName
functionNames = m .NameSection .FunctionNames
localNames = m .NameSection .LocalNames
resultNames = m .NameSection .ResultNames
}
importCount := m .ImportFunctionCount
m .FunctionDefinitionSection = make ([]FunctionDefinition , importCount +uint32 (len (m .FunctionSection )))
importFuncIdx := Index (0 )
for i := range m .ImportSection {
imp := &m .ImportSection [i ]
if imp .Type != ExternTypeFunc {
continue
}
def := &m .FunctionDefinitionSection [importFuncIdx ]
def .importDesc = imp
def .index = importFuncIdx
def .Functype = &m .TypeSection [imp .DescFunc ]
importFuncIdx ++
}
for codeIndex , typeIndex := range m .FunctionSection {
code := &m .CodeSection [codeIndex ]
idx := importFuncIdx + Index (codeIndex )
def := &m .FunctionDefinitionSection [idx ]
def .index = idx
def .Functype = &m .TypeSection [typeIndex ]
def .goFunc = code .GoFunc
}
n , nLen := 0 , len (functionNames )
for i := range m .FunctionDefinitionSection {
d := &m .FunctionDefinitionSection [i ]
funcIdx := d .index
var funcName string
for ; n < nLen ; n ++ {
next := &functionNames [n ]
if next .Index > funcIdx {
break
} else if next .Index == funcIdx {
funcName = next .Name
break
}
}
d .moduleName = moduleName
d .name = funcName
d .Debugname = wasmdebug .FuncName (moduleName , funcName , funcIdx )
d .paramNames = paramNames (localNames , funcIdx , len (d .Functype .Params ))
d .resultNames = paramNames (resultNames , funcIdx , len (d .Functype .Results ))
for i := range m .ExportSection {
e := &m .ExportSection [i ]
if e .Type == ExternTypeFunc && e .Index == funcIdx {
d .exportNames = append (d .exportNames , e .Name )
}
}
}
}
type FunctionDefinition struct {
internalapi .WazeroOnlyType
moduleName string
index Index
name string
Debugname string
goFunc interface {}
Functype *FunctionType
importDesc *Import
exportNames []string
paramNames []string
resultNames []string
}
func (f *FunctionDefinition ) ModuleName () string {
return f .moduleName
}
func (f *FunctionDefinition ) Index () uint32 {
return f .index
}
func (f *FunctionDefinition ) Name () string {
return f .name
}
func (f *FunctionDefinition ) DebugName () string {
return f .Debugname
}
func (f *FunctionDefinition ) Import () (moduleName , name string , isImport bool ) {
if f .importDesc != nil {
importDesc := f .importDesc
moduleName , name , isImport = importDesc .Module , importDesc .Name , true
}
return
}
func (f *FunctionDefinition ) ExportNames () []string {
return f .exportNames
}
func (f *FunctionDefinition ) GoFunction () interface {} {
return f .goFunc
}
func (f *FunctionDefinition ) ParamTypes () []ValueType {
return f .Functype .Params
}
func (f *FunctionDefinition ) ParamNames () []string {
return f .paramNames
}
func (f *FunctionDefinition ) ResultTypes () []ValueType {
return f .Functype .Results
}
func (f *FunctionDefinition ) ResultNames () []string {
return f .resultNames
}
The pages are generated with Golds v0.8.2 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds .