diff --git a/core.go b/core.go index 9ce104e..2987b27 100644 --- a/core.go +++ b/core.go @@ -13,6 +13,11 @@ import ( "github.com/tetratelabs/wazero/imports/wasi_snapshot_preview1" ) +var ( + ErrModuleNotImported = fmt.Errorf("water: importing a module not imported by the WebAssembly module") + ErrFuncNotImported = fmt.Errorf("water: importing a function not imported by the WebAssembly module") +) + // Core provides the low-level access to the WebAssembly runtime // environment. type Core interface { @@ -246,17 +251,11 @@ func (c *core) ImportFunction(module, name string, f any) error { // Unsafe: check if the WebAssembly module really imports this function under // the given module and name. If not, we warn and skip the import. if mod, ok := c.ImportedFunctions()[module]; !ok { - log.LWarnf(c.config.Logger(), "water: module %s is not imported, skipping...", module) - - // list all the imported modules - for mod := range c.ImportedFunctions() { - log.LWarnf(c.config.Logger(), "water: module %s is imported", mod) - } - - return nil + log.LDebugf(c.config.Logger(), "water: module %s is not imported.", module) + return ErrModuleNotImported } else if _, ok := mod[name]; !ok { - log.LWarnf(c.config.Logger(), "water: function %s.%s is not imported, skipping...", module, name) - return nil + log.LWarnf(c.config.Logger(), "water: function %s.%s is not imported.", module, name) + return ErrFuncNotImported } if _, ok := c.importModules[module]; !ok { diff --git a/transport/v0/transport_module.go b/transport/v0/transport_module.go index 57e0227..fd3b069 100644 --- a/transport/v0/transport_module.go +++ b/transport/v0/transport_module.go @@ -307,7 +307,9 @@ func (tm *TransportModule) LinkNetworkInterface(dialer *ManagedDialer, listener } if err := tm.core.ImportFunction("env", "host_dial", dialerFunc); err != nil { - return fmt.Errorf("water: linking dialer function, (*water.Core).ImportFunction: %w", err) + if dialer != nil || err != water.ErrFuncNotImported { + return fmt.Errorf("water: linking dialer function, (*water.Core).ImportFunction: %w", err) + } } var acceptFunc func() (fd int32) @@ -327,7 +329,9 @@ func (tm *TransportModule) LinkNetworkInterface(dialer *ManagedDialer, listener } if err := tm.core.ImportFunction("env", "host_accept", acceptFunc); err != nil { - return fmt.Errorf("water: linking listener function, (*water.Core).ImportFunction: %w", err) + if listener != nil || err != water.ErrFuncNotImported { + return fmt.Errorf("water: linking listener function, (*water.Core).ImportFunction: %w", err) + } } return nil @@ -347,17 +351,33 @@ func (tm *TransportModule) Initialize() error { if err = tm.core.ImportFunction("env", "host_defer", func() { tm.DeferAll() }); err != nil { - return fmt.Errorf("water: (*water.Core).ImportFunction returned error "+ - "when importing host_defer function: %w", err) + if err == water.ErrFuncNotImported { + log.LWarnf(tm.core.Logger(), "water: host_defer function is not imported by WATM, "+ + "deferred functions will not be executed when WATM exits") + } else { + return fmt.Errorf("water: (*water.Core).ImportFunction returned error "+ + "when importing host_defer function: %w", err) + } } // import pull_config function (it is called pushConfig here in the host) - // if tm.core.ImportedFunctions()["env"]["pull_config"] != nil { if err = tm.core.ImportFunction("env", "pull_config", tm.pushConfig); err != nil { - return fmt.Errorf("water: (*water.Core).ImportFunction returned error "+ - "when importing pull_config function: %w", err) + if err == water.ErrFuncNotImported { + // If a config is provided, we will warn the user that the config WILL NOT be + // pushed to the WASM module. + if tm.core.Config().TransportModuleConfig != nil { + f, err := tm.core.Config().TransportModuleConfig.AsFile() + if f != nil && err == nil { + // there is a config file provided, must warn + log.LWarnf(tm.core.Logger(), "water: pull_config function is not imported by WATM, "+ + "config file will not be pushed to the WASM module") + } + } + } else { + return fmt.Errorf("water: (*water.Core).ImportFunction returned error "+ + "when importing pull_config function: %w", err) + } } - // } // instantiate the WASM module if err = tm.core.Instantiate(); err != nil {