Skip to content

rename NewCompiler and NewBytecodeEvaluator to just New #24

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .github/workflows/go-coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,5 @@ jobs:
-Dsonar.tests=.
-Dsonar.test.inclusions=**/*_test.go
-Dsonar.language=go
-Dsonar.sourceEncoding=UTF-8
-Dsonar.sourceEncoding=UTF-8
-Dsonar.cpd.exclusions=**/*
2 changes: 1 addition & 1 deletion machines/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ This package contains virtual machine implementations for executing scripts in v

4. **Data Preparation Stage**
- This phase is optional, and must happen prior to evaluation when runtime input data is used
- The `BytecodeEvaluator` implements the `engine.EvaluatorWithPrep` interface, which has a `PrepareContext` method
- The `Evaluator` implements the `engine.EvaluatorWithPrep` interface, which has a `PrepareContext` method
- The `PrepareContext` method takes a `context.Context` and a variadic list of `any`
- `PrepareContext` calls the `data.Provider` to convert and store the data, somewhere accessible to the Evaluator
- The conversion is fairly opinionated, and handled by the `data.Provider`
Expand Down
4 changes: 2 additions & 2 deletions machines/TESTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ Use these consistent naming patterns:
| Compilation | `TestCompiler_Compile` |
| Options | `TestCompilerOptions` or `TestCompilerOptionsDetailed` |
| Executables | `TestExecutable` |
| Evaluator execution | `TestBytecodeEvaluator_Evaluate` |
| Context preparation | `TestBytecodeEvaluator_PrepareContext` |
| Evaluator execution | `TestEvaluator_Evaluate` |
| Context preparation | `TestEvaluator_PrepareContext` |
| Response handling | `TestResponseMethods` |
| Type conversion | `TestToGoType`/`TestToMachineType` |

Expand Down
4 changes: 2 additions & 2 deletions machines/extism/compiler/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ type Compiler struct {
logger *slog.Logger
}

// NewCompiler creates a new Extism WASM Compiler instance with the provided options.
func NewCompiler(opts ...FunctionalOption) (*Compiler, error) {
// New creates a new Extism WASM Compiler instance with the provided options.
func New(opts ...FunctionalOption) (*Compiler, error) {
// Initialize the compiler with an empty struct
c := &Compiler{}

Expand Down
22 changes: 11 additions & 11 deletions machines/extism/compiler/compiler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func readTestWasm(t *testing.T) []byte {
func createTestCompiler(t *testing.T, entryPoint string) *Compiler {
t.Helper()

comp, err := NewCompiler(
comp, err := New(
WithEntryPoint(entryPoint),
WithLogHandler(slog.NewTextHandler(io.Discard, nil)),
)
Expand Down Expand Up @@ -64,11 +64,11 @@ func (m *mockScriptReaderCloser) Close() error {
return args.Error(0)
}

func TestNewCompiler(t *testing.T) {
func TestNew(t *testing.T) {
t.Parallel()

t.Run("basic creation", func(t *testing.T) {
comp, err := NewCompiler(
comp, err := New(
WithEntryPoint("main"),
WithLogHandler(slog.NewTextHandler(io.Discard, nil)),
)
Expand All @@ -82,7 +82,7 @@ func TestNewCompiler(t *testing.T) {
})

t.Run("with entry point", func(t *testing.T) {
comp, err := NewCompiler(
comp, err := New(
WithEntryPoint("custom_function"),
WithLogHandler(slog.NewTextHandler(io.Discard, nil)),
)
Expand All @@ -91,7 +91,7 @@ func TestNewCompiler(t *testing.T) {
})

t.Run("with custom runtime config", func(t *testing.T) {
comp, err := NewCompiler(
comp, err := New(
WithEntryPoint("main"),
WithLogHandler(slog.NewTextHandler(io.Discard, nil)),
WithRuntimeConfig(wazero.NewRuntimeConfig()),
Expand All @@ -103,7 +103,7 @@ func TestNewCompiler(t *testing.T) {
t.Run("with custom logger", func(t *testing.T) {
handler := slog.NewTextHandler(io.Discard, nil)
logger := slog.New(handler)
comp, err := NewCompiler(
comp, err := New(
WithEntryPoint("main"),
WithLogger(logger),
)
Expand Down Expand Up @@ -200,7 +200,7 @@ func TestCompiler_Compile(t *testing.T) {
wasmBytes := readTestWasm(t)
entryPoint := "greet"

comp, err := NewCompiler(
comp, err := New(
WithEntryPoint(entryPoint),
WithLogHandler(slog.NewTextHandler(io.Discard, nil)),
WithRuntimeConfig(wazero.NewRuntimeConfig()),
Expand All @@ -227,7 +227,7 @@ func TestCompiler_Compile(t *testing.T) {

t.Run("error cases", func(t *testing.T) {
t.Run("nil content", func(t *testing.T) {
comp, err := NewCompiler(
comp, err := New(
WithEntryPoint("main"),
WithLogHandler(slog.NewTextHandler(io.Discard, nil)),
)
Expand All @@ -241,7 +241,7 @@ func TestCompiler_Compile(t *testing.T) {
})

t.Run("empty content", func(t *testing.T) {
comp, err := NewCompiler(
comp, err := New(
WithEntryPoint("main"),
WithLogHandler(slog.NewTextHandler(io.Discard, nil)),
)
Expand All @@ -260,7 +260,7 @@ func TestCompiler_Compile(t *testing.T) {
})

t.Run("invalid wasm binary", func(t *testing.T) {
comp, err := NewCompiler(
comp, err := New(
WithEntryPoint("main"),
WithLogHandler(slog.NewTextHandler(io.Discard, nil)),
)
Expand All @@ -280,7 +280,7 @@ func TestCompiler_Compile(t *testing.T) {

t.Run("missing function", func(t *testing.T) {
wasmBytes := readTestWasm(t)
comp, err := NewCompiler(
comp, err := New(
WithEntryPoint("nonexistent_function"),
WithLogHandler(slog.NewTextHandler(io.Discard, nil)),
)
Expand Down
10 changes: 5 additions & 5 deletions machines/extism/compiler/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -623,7 +623,7 @@ func TestCompilerOptions(t *testing.T) {

t.Run("Logger", func(t *testing.T) {
t.Run("default initialization", func(t *testing.T) {
c, err := NewCompiler()
c, err := New()
require.NoError(t, err)
require.NotNil(t, c.logHandler, "logHandler should be initialized")
require.NotNil(t, c.logger, "logger should be initialized")
Expand All @@ -633,7 +633,7 @@ func TestCompilerOptions(t *testing.T) {
var buf bytes.Buffer
customHandler := slog.NewTextHandler(&buf, nil)

c, err := NewCompiler(WithLogHandler(customHandler))
c, err := New(WithLogHandler(customHandler))
require.NoError(t, err)

require.Equal(t, customHandler, c.logHandler, "custom handler should be set")
Expand All @@ -648,7 +648,7 @@ func TestCompilerOptions(t *testing.T) {
customHandler := slog.NewTextHandler(&buf, nil)
customLogger := slog.New(customHandler)

c, err := NewCompiler(WithLogger(customLogger))
c, err := New(WithLogger(customLogger))
require.NoError(t, err)

require.Equal(t, customLogger, c.logger, "custom logger should be set")
Expand All @@ -664,7 +664,7 @@ func TestCompilerOptions(t *testing.T) {
customLogger := slog.New(slog.NewTextHandler(&loggerBuf, nil))

t.Run("handler then logger", func(t *testing.T) {
c1, err := NewCompiler(
c1, err := New(
WithLogHandler(customHandler),
WithLogger(customLogger),
)
Expand All @@ -685,7 +685,7 @@ func TestCompilerOptions(t *testing.T) {
loggerBuf.Reset()

t.Run("logger then handler", func(t *testing.T) {
c2, err := NewCompiler(
c2, err := New(
WithLogger(customLogger),
WithLogHandler(customHandler),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,33 +18,34 @@ import (
"github.com/robbyt/go-polyscript/machines/extism/internal"
)

// BytecodeEvaluator executes compiled WASM modules with provided runtime data
type BytecodeEvaluator struct {
// Evaluator executes compiled WASM modules with provided runtime data
type Evaluator struct {
execUnit *script.ExecutableUnit
logHandler slog.Handler
logger *slog.Logger
}

func NewBytecodeEvaluator(
// New creates a new Evaluator object
func New(
handler slog.Handler,
execUnit *script.ExecutableUnit,
) *BytecodeEvaluator {
handler, logger := helpers.SetupLogger(handler, "extism", "BytecodeEvaluator")
) *Evaluator {
handler, logger := helpers.SetupLogger(handler, "extism", "Evaluator")

return &BytecodeEvaluator{
return &Evaluator{
execUnit: execUnit,
logHandler: handler,
logger: logger,
}
}

func (be *BytecodeEvaluator) String() string {
return "extism.BytecodeEvaluator"
func (be *Evaluator) String() string {
return "extism.Evaluator"
}

// loadInputData retrieves input data using the data provider in the executable unit.
// Returns a map that will be used as input for the WASM module.
func (be *BytecodeEvaluator) loadInputData(ctx context.Context) (map[string]any, error) {
func (be *Evaluator) loadInputData(ctx context.Context) (map[string]any, error) {
logger := be.logger.WithGroup("loadInputData")

// If no executable unit or data provider, return empty map
Expand Down Expand Up @@ -112,7 +113,7 @@ func execHelper(

// exec handles WASM-specific execution details
// Using the interface and helper function to improve testability
func (be *BytecodeEvaluator) exec(
func (be *Evaluator) exec(
ctx context.Context,
plugin adapters.CompiledPlugin,
entryPoint string,
Expand Down Expand Up @@ -144,7 +145,7 @@ func (be *BytecodeEvaluator) exec(
// Eval implements engine.Evaluator
// TODO: Some error paths in this method are hard to test with the current design
// Consider adding more integration tests to cover these paths.
func (be *BytecodeEvaluator) Eval(ctx context.Context) (engine.EvaluatorResponse, error) {
func (be *Evaluator) Eval(ctx context.Context) (engine.EvaluatorResponse, error) {
logger := be.logger.WithGroup("Eval")
if be.execUnit == nil {
return nil, fmt.Errorf("executable unit is nil")
Expand Down Expand Up @@ -212,7 +213,7 @@ func (be *BytecodeEvaluator) Eval(ctx context.Context) (engine.EvaluatorResponse
// PrepareContext implements the EvalDataPreparer interface for Extism WebAssembly modules.
// It enriches the provided context with data for script evaluation, using the
// ExecutableUnit's DataProvider to store the data.
func (be *BytecodeEvaluator) PrepareContext(
func (be *Evaluator) PrepareContext(
ctx context.Context,
d ...any,
) (context.Context, error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,8 @@ func (m *mockExecutableContent) GetByteCode() any {
return m.bytecode
}

// TestBytecodeEvaluator_Evaluate tests evaluating WASM scripts with Extism
func TestBytecodeEvaluator_Evaluate(t *testing.T) {
// TestEvaluator_Evaluate tests evaluating WASM scripts with Extism
func TestEvaluator_Evaluate(t *testing.T) {
t.Parallel()

t.Run("success cases", func(t *testing.T) {
Expand Down Expand Up @@ -162,7 +162,7 @@ func TestBytecodeEvaluator_Evaluate(t *testing.T) {
Content: content,
}

evaluator := NewBytecodeEvaluator(handler, exe)
evaluator := New(handler, exe)

ctx := context.Background()
evalData := map[string]any{"test": "data"}
Expand Down Expand Up @@ -201,7 +201,7 @@ func TestBytecodeEvaluator_Evaluate(t *testing.T) {
Content: content,
}

evaluator := NewBytecodeEvaluator(handler, exe)
evaluator := New(handler, exe)
ctx := context.Background()
evalData := map[string]any{"test": "data"}
ctx = context.WithValue(ctx, constants.EvalData, evalData)
Expand Down Expand Up @@ -252,7 +252,7 @@ func TestBytecodeEvaluator_Evaluate(t *testing.T) {
DataProvider: ctxProvider,
}

evaluator := NewBytecodeEvaluator(handler, dummyExe)
evaluator := New(handler, dummyExe)
ctx := context.Background()

if tt.ctxData != nil {
Expand Down Expand Up @@ -301,7 +301,7 @@ func TestBytecodeEvaluator_Evaluate(t *testing.T) {
// Test nil executable unit
t.Run("nil executable unit", func(t *testing.T) {
handler := slog.NewTextHandler(os.Stdout, nil)
evaluator := NewBytecodeEvaluator(handler, nil)
evaluator := New(handler, nil)

ctx := context.Background()
_, err := evaluator.Eval(ctx)
Expand All @@ -327,7 +327,7 @@ func TestBytecodeEvaluator_Evaluate(t *testing.T) {
DataProvider: ctxProvider,
}

evaluator := NewBytecodeEvaluator(handler, exe)
evaluator := New(handler, exe)

ctx := context.Background()
_, err := evaluator.Eval(ctx)
Expand All @@ -353,7 +353,7 @@ func TestBytecodeEvaluator_Evaluate(t *testing.T) {
DataProvider: ctxProvider,
}

evaluator := NewBytecodeEvaluator(handler, exe)
evaluator := New(handler, exe)

ctx := context.Background()
_, err := evaluator.Eval(ctx)
Expand Down Expand Up @@ -390,7 +390,7 @@ func TestBytecodeEvaluator_Evaluate(t *testing.T) {
DataProvider: data.NewContextProvider(constants.EvalData),
}

evaluator := NewBytecodeEvaluator(handler, execUnit)
evaluator := New(handler, execUnit)

// Add test data to context
ctx = context.WithValue(ctx, constants.EvalData, map[string]any{"test": "data"})
Expand Down Expand Up @@ -430,7 +430,7 @@ func TestBytecodeEvaluator_Evaluate(t *testing.T) {
Content: content,
}

evaluator := NewBytecodeEvaluator(handler, exe)
evaluator := New(handler, exe)
ctx := context.Background()
evalData := map[string]any{"test": "data"}
ctx = context.WithValue(ctx, constants.EvalData, evalData)
Expand All @@ -456,7 +456,7 @@ func TestBytecodeEvaluator_Evaluate(t *testing.T) {
Content: content,
}

evaluator := NewBytecodeEvaluator(handler, exe)
evaluator := New(handler, exe)
ctx := context.Background()

_, err := evaluator.Eval(ctx)
Expand All @@ -482,7 +482,7 @@ func TestBytecodeEvaluator_Evaluate(t *testing.T) {
}

// Create with nil handler
evaluator := NewBytecodeEvaluator(nil, exe)
evaluator := New(nil, exe)

// Shouldn't panic
require.NotNil(t, evaluator)
Expand All @@ -493,11 +493,11 @@ func TestBytecodeEvaluator_Evaluate(t *testing.T) {
// Test String method
t.Run("String method", func(t *testing.T) {
handler := slog.NewTextHandler(os.Stdout, nil)
evaluator := NewBytecodeEvaluator(handler, nil)
evaluator := New(handler, nil)

// Test the string representation
strRep := evaluator.String()
require.Equal(t, "extism.BytecodeEvaluator", strRep)
require.Equal(t, "extism.Evaluator", strRep)
})

// Test the exec helper function
Expand Down Expand Up @@ -607,8 +607,8 @@ func TestBytecodeEvaluator_Evaluate(t *testing.T) {
})
}

// TestBytecodeEvaluator_PrepareContext tests the PrepareContext method with various scenarios
func TestBytecodeEvaluator_PrepareContext(t *testing.T) {
// TestEvaluator_PrepareContext tests the PrepareContext method with various scenarios
func TestEvaluator_PrepareContext(t *testing.T) {
t.Parallel()

tests := []struct {
Expand Down Expand Up @@ -711,7 +711,7 @@ func TestBytecodeEvaluator_PrepareContext(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
handler := slog.NewTextHandler(os.Stdout, nil)
exe := tt.setupExe(t)
evaluator := NewBytecodeEvaluator(handler, exe)
evaluator := New(handler, exe)

ctx := context.Background()
enrichedCtx, err := evaluator.PrepareContext(ctx, tt.inputs...)
Expand Down
Loading