wasi-module-sdk-go/exec/lookup.go

44 lines
906 B
Go
Raw Normal View History

package exec
import (
"errors"
"fmt"
2023-08-15 20:10:30 +00:00
2023-09-05 20:06:42 +00:00
wasiv1 "code.icb4dc0.de/buildr/api/generated/wasi/v1"
2023-08-15 20:10:30 +00:00
"code.icb4dc0.de/buildr/wasi-module-sdk-go/mem"
)
func LookPath(file string) (string, error) {
2023-09-05 20:06:42 +00:00
lookupPathReq := &wasiv1.LookupPathRequest{
Command: file,
}
data, err := lookupPathReq.MarshalVT()
if err != nil {
return "", err
}
result := _lookPath(mem.DataToManagedPtr(data))
if result == 0 {
return "", fmt.Errorf("failed to lookup in path: %s", file)
}
resultPtr := uint32(result >> 32)
resultSize := uint32(result)
if resultSize == 0 {
return "", fmt.Errorf("failed to lookup in path: %s", file)
}
2023-09-05 20:06:42 +00:00
lookupPathResp := new(wasiv1.LookupPathResponse)
if err := lookupPathResp.UnmarshalVT(mem.PtrToData(resultPtr, resultSize)); err != nil {
return "", err
}
if len(lookupPathResp.Error) > 0 {
return "", errors.New(lookupPathResp.Error)
}
return lookupPathResp.Path, nil
}