Skip to content

Commit

Permalink
Check validity of stdout/stderr and replace them with io.Discard if t…
Browse files Browse the repository at this point in the history
…hey are invalid (#145)
  • Loading branch information
blotus authored Sep 13, 2024
1 parent 6767c34 commit 160a907
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion internal/re2_wazero.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
_ "embed"
"encoding/binary"
"errors"
"io"
"os"
"runtime"
"strings"
Expand Down Expand Up @@ -182,8 +183,20 @@ func init() {
}
wasmCompiled = code

// In some situations (eg, running as a service on windows)
// Stdout and Stderr may not be available.
// In this case, use io.Discard to avoid InstantiateModule returning an error.
var stdout, stderr io.Writer = os.Stdout, os.Stderr

if _, err := os.Stdout.Stat(); err != nil {
stdout = io.Discard
}
if _, err := os.Stderr.Stat(); err != nil {
stderr = io.Discard
}

wasmRT = rt
root, err := wasmRT.InstantiateModule(ctx, wasmCompiled, wazero.NewModuleConfig().WithSysWalltime().WithSysNanotime().WithSysNanosleep().WithStdout(os.Stdout).WithStderr(os.Stderr).WithStartFunctions("_initialize").WithName(""))
root, err := wasmRT.InstantiateModule(ctx, wasmCompiled, wazero.NewModuleConfig().WithSysWalltime().WithSysNanotime().WithSysNanosleep().WithStdout(stdout).WithStderr(stderr).WithStartFunctions("_initialize").WithName(""))
if err != nil {
panic(err)
}
Expand Down

0 comments on commit 160a907

Please sign in to comment.