Files
monkey/options.go
Chuck Smith 07fd82b261
Some checks failed
Build / build (push) Successful in 10m29s
Publish Image / publish (push) Failing after 31s
Test / build (push) Failing after 6m34s
optimizations
2024-04-02 14:08:08 -04:00

50 lines
1.1 KiB
Go

package monkey
import (
"io"
"monkey/internal/context"
"monkey/internal/vm"
"os"
)
// Options represents the options for running a script with Monkey.
type Options struct {
Debug bool // Whether to enable debug mode or not
Trace bool // Whether to enable trace mode or not
Args []string // The arguments passed to the script
Stdin io.Reader // The input reader for the script
Stdout io.Writer // The output writer for the script
Stderr io.Writer // The error writer for the script
Exit func(int) // A function to call when the script exits
}
func (opts Options) ToVMOptions() []vm.Option {
if opts.Stdin == nil {
opts.Stdin = os.Stdin
}
if opts.Stdout == nil {
opts.Stdout = os.Stdout
}
if opts.Stderr == nil {
opts.Stderr = os.Stderr
}
if opts.Exit == nil {
opts.Exit = os.Exit
}
ctx := context.New(
context.WithArgs(opts.Args),
context.WithStdin(opts.Stdin),
context.WithStdout(opts.Stdout),
context.WithStderr(opts.Stderr),
context.WithExit(opts.Exit),
)
return []vm.Option{
vm.WithDebug(opts.Debug),
vm.WithTrace(opts.Trace),
vm.WithContext(ctx),
}
}