|
| 1 | +package extism |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "log/slog" |
| 6 | + "os" |
| 7 | + |
| 8 | + extismSDK "github.com/extism/go-sdk" |
| 9 | + "github.com/tetratelabs/wazero" |
| 10 | +) |
| 11 | + |
| 12 | +// compilerOptions holds the configuration for the Extism compiler |
| 13 | +type compilerOptions struct { |
| 14 | + EntryPoint string |
| 15 | + LogHandler slog.Handler |
| 16 | + Logger *slog.Logger |
| 17 | + EnableWASI bool |
| 18 | + RuntimeConfig wazero.RuntimeConfig |
| 19 | + HostFunctions []extismSDK.HostFunction |
| 20 | +} |
| 21 | + |
| 22 | +// CompilerOption is a function that configures a compilerOptions instance |
| 23 | +type CompilerOption func(*compilerOptions) error |
| 24 | + |
| 25 | +// WithEntryPoint creates an option to set the entry point for Extism WASM modules |
| 26 | +func WithEntryPoint(entryPoint string) CompilerOption { |
| 27 | + return func(cfg *compilerOptions) error { |
| 28 | + if entryPoint == "" { |
| 29 | + return fmt.Errorf("entry point cannot be empty") |
| 30 | + } |
| 31 | + cfg.EntryPoint = entryPoint |
| 32 | + return nil |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +// WithLogHandler creates an option to set the log handler for Extism compiler. |
| 37 | +// This is the preferred option for logging configuration as it provides |
| 38 | +// more flexibility through the slog.Handler interface. |
| 39 | +func WithLogHandler(handler slog.Handler) CompilerOption { |
| 40 | + return func(cfg *compilerOptions) error { |
| 41 | + if handler == nil { |
| 42 | + return fmt.Errorf("log handler cannot be nil") |
| 43 | + } |
| 44 | + cfg.LogHandler = handler |
| 45 | + // Clear logger if handler is explicitly set |
| 46 | + cfg.Logger = nil |
| 47 | + return nil |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +// WithLogger creates an option to set a specific logger for Extism compiler. |
| 52 | +// This is less flexible than WithLogHandler but allows users to customize |
| 53 | +// their logging group configuration. |
| 54 | +func WithLogger(logger *slog.Logger) CompilerOption { |
| 55 | + return func(cfg *compilerOptions) error { |
| 56 | + if logger == nil { |
| 57 | + return fmt.Errorf("logger cannot be nil") |
| 58 | + } |
| 59 | + cfg.Logger = logger |
| 60 | + // Clear handler if logger is explicitly set |
| 61 | + cfg.LogHandler = nil |
| 62 | + return nil |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +// WithWASIEnabled creates an option to enable or disable WASI support |
| 67 | +func WithWASIEnabled(enabled bool) CompilerOption { |
| 68 | + return func(cfg *compilerOptions) error { |
| 69 | + cfg.EnableWASI = enabled |
| 70 | + return nil |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +// WithRuntimeConfig creates an option to set a custom wazero runtime configuration |
| 75 | +func WithRuntimeConfig(config wazero.RuntimeConfig) CompilerOption { |
| 76 | + return func(cfg *compilerOptions) error { |
| 77 | + if config == nil { |
| 78 | + return fmt.Errorf("runtime config cannot be nil") |
| 79 | + } |
| 80 | + cfg.RuntimeConfig = config |
| 81 | + return nil |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +// WithHostFunctions creates an option to set additional host functions |
| 86 | +func WithHostFunctions(funcs []extismSDK.HostFunction) CompilerOption { |
| 87 | + return func(cfg *compilerOptions) error { |
| 88 | + cfg.HostFunctions = funcs |
| 89 | + return nil |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +// applyDefaults sets the default values for a compilerConfig |
| 94 | +func applyDefaults(cfg *compilerOptions) { |
| 95 | + // Default to stderr for logging if neither handler nor logger specified |
| 96 | + if cfg.LogHandler == nil && cfg.Logger == nil { |
| 97 | + cfg.LogHandler = slog.NewTextHandler(os.Stderr, nil) |
| 98 | + } |
| 99 | + |
| 100 | + // Default entry point |
| 101 | + if cfg.EntryPoint == "" { |
| 102 | + cfg.EntryPoint = defaultEntryPoint |
| 103 | + } |
| 104 | + |
| 105 | + // Default WASI setting |
| 106 | + cfg.EnableWASI = true |
| 107 | + |
| 108 | + // Default runtime config |
| 109 | + if cfg.RuntimeConfig == nil { |
| 110 | + cfg.RuntimeConfig = wazero.NewRuntimeConfig() |
| 111 | + } |
| 112 | + |
| 113 | + // Default to empty host functions |
| 114 | + if cfg.HostFunctions == nil { |
| 115 | + cfg.HostFunctions = []extismSDK.HostFunction{} |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +// validate checks if the configuration is valid |
| 120 | +func validate(cfg *compilerOptions) error { |
| 121 | + // Ensure we have either a logger or a handler |
| 122 | + if cfg.LogHandler == nil && cfg.Logger == nil { |
| 123 | + return fmt.Errorf("either log handler or logger must be specified") |
| 124 | + } |
| 125 | + |
| 126 | + // Entry point must be non-empty |
| 127 | + if cfg.EntryPoint == "" { |
| 128 | + return fmt.Errorf("entry point must be specified") |
| 129 | + } |
| 130 | + |
| 131 | + // Runtime config cannot be nil |
| 132 | + if cfg.RuntimeConfig == nil { |
| 133 | + return fmt.Errorf("runtime config cannot be nil") |
| 134 | + } |
| 135 | + |
| 136 | + return nil |
| 137 | +} |
0 commit comments