package wasm
import (
"context"
"fmt"
"github.com/tetratelabs/wazero/api"
"github.com/tetratelabs/wazero/internal/internalapi"
)
func (m *ModuleInstance ) LookupFunction (t *TableInstance , typeId FunctionTypeID , tableOffset Index ) api .Function {
fm , index := m .Engine .LookupFunction (t , typeId , tableOffset )
if source := fm .Source ; source .IsHostModule {
def := &source .FunctionDefinitionSection [index ]
goF := source .CodeSection [index ].GoFunc
switch typed := goF .(type ) {
case api .GoFunction :
return &lookedUpGoFunction {def : def , g : goFunctionAsGoModuleFunction (typed )}
case api .GoModuleFunction :
return &lookedUpGoFunction {def : def , lookedUpModule : m , g : typed }
default :
panic (fmt .Sprintf ("unexpected GoFunc type: %T" , goF ))
}
} else {
return fm .Engine .NewFunction (index )
}
}
type lookedUpGoFunction struct {
internalapi .WazeroOnly
def *FunctionDefinition
lookedUpModule *ModuleInstance
g api .GoModuleFunction
}
func goFunctionAsGoModuleFunction(g api .GoFunction ) api .GoModuleFunction {
return api .GoModuleFunc (func (ctx context .Context , _ api .Module , stack []uint64 ) {
g .Call (ctx , stack )
})
}
func (l *lookedUpGoFunction ) Definition () api .FunctionDefinition { return l .def }
func (l *lookedUpGoFunction ) Call (ctx context .Context , params ...uint64 ) ([]uint64 , error ) {
typ := l .def .Functype
stackSize := typ .ParamNumInUint64
rn := typ .ResultNumInUint64
if rn > stackSize {
stackSize = rn
}
stack := make ([]uint64 , stackSize )
copy (stack , params )
return stack [:rn ], l .CallWithStack (ctx , stack )
}
func (l *lookedUpGoFunction ) CallWithStack (ctx context .Context , stack []uint64 ) error {
l .g .Call (ctx , l .lookedUpModule , stack )
return nil
}
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 .