Skip to content

Commit 9444a84

Browse files
authored
major reorg of machines to move files into sub-packages (#18)
* major reorg of machines to move files into sub-packages * clean up old calls to CompilerOptions * export ConvertToStarlarkValue * more cleanup, and harmonization
1 parent 3c97f94 commit 9444a84

File tree

83 files changed

+632
-532
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

83 files changed

+632
-532
lines changed

engine/benchmark_test.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ import (
3434
"github.com/robbyt/go-polyscript/engine/options"
3535
"github.com/robbyt/go-polyscript/execution/constants"
3636
"github.com/robbyt/go-polyscript/execution/data"
37-
"github.com/robbyt/go-polyscript/machines/risor"
38-
"github.com/robbyt/go-polyscript/machines/starlark"
37+
risorCompiler "github.com/robbyt/go-polyscript/machines/risor/compiler"
38+
starlarkCompiler "github.com/robbyt/go-polyscript/machines/starlark/compiler"
3939
)
4040

4141
// quietHandler is a slog.Handler that discards all logs
@@ -71,7 +71,7 @@ func BenchmarkEvaluationPatterns(b *testing.B) {
7171
options.WithDefaults(),
7272
options.WithDataProvider(dataProvider),
7373
options.WithLogHandler(quietHandler),
74-
risor.WithGlobals([]string{constants.Ctx}),
74+
risorCompiler.WithGlobals([]string{constants.Ctx}),
7575
)
7676
if err != nil {
7777
b.Fatalf("Failed to create evaluator: %v", err)
@@ -95,7 +95,7 @@ func BenchmarkEvaluationPatterns(b *testing.B) {
9595
options.WithDefaults(),
9696
options.WithDataProvider(dataProvider),
9797
options.WithLogHandler(quietHandler),
98-
risor.WithGlobals([]string{constants.Ctx}),
98+
risorCompiler.WithGlobals([]string{constants.Ctx}),
9999
)
100100
if err != nil {
101101
b.Fatalf("Failed to create evaluator: %v", err)
@@ -139,7 +139,7 @@ func BenchmarkDataProviders(b *testing.B) {
139139
options.WithDefaults(),
140140
options.WithDataProvider(dataProvider),
141141
options.WithLogHandler(quietHandler),
142-
risor.WithGlobals([]string{constants.Ctx}),
142+
risorCompiler.WithGlobals([]string{constants.Ctx}),
143143
)
144144
if err != nil {
145145
b.Fatalf("Failed to create evaluator: %v", err)
@@ -161,7 +161,7 @@ func BenchmarkDataProviders(b *testing.B) {
161161
options.WithDefaults(),
162162
options.WithDataProvider(dataProvider),
163163
options.WithLogHandler(quietHandler),
164-
risor.WithGlobals([]string{constants.Ctx}),
164+
risorCompiler.WithGlobals([]string{constants.Ctx}),
165165
)
166166
if err != nil {
167167
b.Fatalf("Failed to create evaluator: %v", err)
@@ -188,7 +188,7 @@ func BenchmarkDataProviders(b *testing.B) {
188188
options.WithDefaults(),
189189
options.WithDataProvider(compositeProvider),
190190
options.WithLogHandler(quietHandler),
191-
risor.WithGlobals([]string{constants.Ctx}),
191+
risorCompiler.WithGlobals([]string{constants.Ctx}),
192192
)
193193
if err != nil {
194194
b.Fatalf("Failed to create evaluator: %v", err)
@@ -245,7 +245,7 @@ message = "Hello, " + name + "!"
245245
options.WithDefaults(),
246246
options.WithDataProvider(staticProvider),
247247
options.WithLogHandler(quietHandler),
248-
risor.WithGlobals([]string{constants.Ctx}),
248+
risorCompiler.WithGlobals([]string{constants.Ctx}),
249249
)
250250
if err != nil {
251251
b.Fatalf("Failed to create Risor evaluator: %v", err)
@@ -266,7 +266,7 @@ message = "Hello, " + name + "!"
266266
options.WithDefaults(),
267267
options.WithDataProvider(staticProvider),
268268
options.WithLogHandler(quietHandler),
269-
starlark.WithGlobals([]string{constants.Ctx}),
269+
starlarkCompiler.WithGlobals([]string{constants.Ctx}),
270270
)
271271
if err != nil {
272272
b.Fatalf("Failed to create Starlark evaluator: %v", err)

engine/evaluator_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
"github.com/robbyt/go-polyscript/engine/options"
1313
"github.com/robbyt/go-polyscript/execution/constants"
1414
"github.com/robbyt/go-polyscript/execution/data"
15-
"github.com/robbyt/go-polyscript/machines/risor"
15+
risorCompiler "github.com/robbyt/go-polyscript/machines/risor/compiler"
1616
"github.com/stretchr/testify/assert"
1717
"github.com/stretchr/testify/require"
1818
)
@@ -32,7 +32,7 @@ method + " " + greeting
3232
`,
3333
options.WithLogHandler(handler),
3434
options.WithDataProvider(provider),
35-
risor.WithGlobals([]string{constants.Ctx}),
35+
risorCompiler.WithGlobals([]string{constants.Ctx}),
3636
)
3737
require.NoError(t, err)
3838
require.NotNil(t, evaluator)
@@ -86,7 +86,7 @@ func TestEvaluatorWithPrepErrors(t *testing.T) {
8686
evaluator, err := polyscript.FromRisorString(`ctx["static"]`,
8787
options.WithLogHandler(handler),
8888
options.WithDataProvider(staticProvider),
89-
risor.WithGlobals([]string{constants.Ctx}),
89+
risorCompiler.WithGlobals([]string{constants.Ctx}),
9090
)
9191
require.NoError(t, err)
9292

@@ -108,7 +108,7 @@ func TestEvaluatorWithPrepErrors(t *testing.T) {
108108
evaluator, err = polyscript.FromRisorString(`ctx["request"]["ID"] || "no id"`,
109109
options.WithLogHandler(handler),
110110
options.WithDataProvider(contextProvider),
111-
risor.WithGlobals([]string{constants.Ctx}),
111+
risorCompiler.WithGlobals([]string{constants.Ctx}),
112112
)
113113
require.NoError(t, err)
114114

examples/data-prep/extism/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
"github.com/robbyt/go-polyscript/engine/options"
1515
"github.com/robbyt/go-polyscript/execution/constants"
1616
"github.com/robbyt/go-polyscript/execution/data"
17-
"github.com/robbyt/go-polyscript/machines/extism"
17+
"github.com/robbyt/go-polyscript/machines/extism/compiler"
1818
)
1919

2020
// ExtismEvaluator is a type alias to make testing cleaner
@@ -41,7 +41,7 @@ func createExtismEvaluator(
4141
wasmFilePath,
4242
options.WithLogHandler(logger.Handler()),
4343
options.WithDataProvider(compositeProvider),
44-
extism.WithEntryPoint("greet"),
44+
compiler.WithEntryPoint("greet"),
4545
)
4646
}
4747

examples/data-prep/risor/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
"github.com/robbyt/go-polyscript/engine/options"
1414
"github.com/robbyt/go-polyscript/execution/constants"
1515
"github.com/robbyt/go-polyscript/execution/data"
16-
"github.com/robbyt/go-polyscript/machines/risor"
16+
"github.com/robbyt/go-polyscript/machines/risor/compiler"
1717
)
1818

1919
// RisorEvaluator is a type alias to make testing cleaner
@@ -47,7 +47,7 @@ func createRisorEvaluator(
4747
options.WithDefaults(),
4848
options.WithLogHandler(logger.Handler()),
4949
options.WithDataProvider(compositeProvider),
50-
risor.WithGlobals(globals),
50+
compiler.WithGlobals(globals),
5151
)
5252
}
5353

examples/data-prep/starlark/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515
"github.com/robbyt/go-polyscript/engine/options"
1616
"github.com/robbyt/go-polyscript/execution/constants"
1717
"github.com/robbyt/go-polyscript/execution/data"
18-
"github.com/robbyt/go-polyscript/machines/starlark"
18+
"github.com/robbyt/go-polyscript/machines/starlark/compiler"
1919
)
2020

2121
// StarlarkEvaluator is a type alias to make testing cleaner
@@ -49,7 +49,7 @@ func createStarlarkEvaluator(
4949
options.WithDefaults(),
5050
options.WithLogHandler(logger.Handler()),
5151
options.WithDataProvider(compositeProvider),
52-
starlark.WithGlobals(globals),
52+
compiler.WithGlobals(globals),
5353
)
5454
}
5555

examples/multiple-instantiation/extism/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
"github.com/robbyt/go-polyscript/engine/options"
1414
"github.com/robbyt/go-polyscript/execution/constants"
1515
"github.com/robbyt/go-polyscript/execution/data"
16-
"github.com/robbyt/go-polyscript/machines/extism"
16+
"github.com/robbyt/go-polyscript/machines/extism/compiler"
1717
)
1818

1919
// ExtismEvaluator is a type alias to make testing cleaner
@@ -67,7 +67,7 @@ func createEvaluator(handler slog.Handler) (ExtismEvaluator, error) {
6767
wasmFilePath,
6868
options.WithLogHandler(handler),
6969
options.WithDataProvider(dataProvider),
70-
extism.WithEntryPoint("greet"),
70+
compiler.WithEntryPoint("greet"),
7171
)
7272
if err != nil {
7373
logger.Error("Failed to create evaluator", "error", err)

examples/multiple-instantiation/risor/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
"github.com/robbyt/go-polyscript/engine/options"
1313
"github.com/robbyt/go-polyscript/execution/constants"
1414
"github.com/robbyt/go-polyscript/execution/data"
15-
"github.com/robbyt/go-polyscript/machines/risor"
15+
"github.com/robbyt/go-polyscript/machines/risor/compiler"
1616
)
1717

1818
// RisorEvaluator is a type alias to make testing cleaner
@@ -40,7 +40,7 @@ func createEvaluator(handler slog.Handler) (RisorEvaluator, error) {
4040
options.WithDefaults(),
4141
options.WithLogHandler(handler),
4242
options.WithDataProvider(ctxProvider),
43-
risor.WithGlobals(globals),
43+
compiler.WithGlobals(globals),
4444
)
4545
if err != nil {
4646
logger.Error("Failed to create evaluator", "error", err)

examples/multiple-instantiation/starlark/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
"github.com/robbyt/go-polyscript/engine/options"
1313
"github.com/robbyt/go-polyscript/execution/constants"
1414
"github.com/robbyt/go-polyscript/execution/data"
15-
"github.com/robbyt/go-polyscript/machines/starlark"
15+
"github.com/robbyt/go-polyscript/machines/starlark/compiler"
1616
)
1717

1818
// StarlarkEvaluator is a type alias to make testing cleaner
@@ -40,7 +40,7 @@ func createEvaluator(handler slog.Handler) (StarlarkEvaluator, error) {
4040
options.WithDefaults(),
4141
options.WithLogHandler(handler),
4242
options.WithDataProvider(ctxProvider),
43-
starlark.WithGlobals(globals),
43+
compiler.WithGlobals(globals),
4444
)
4545
if err != nil {
4646
logger.Error("Failed to create evaluator", "error", err)

examples/simple/extism/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
"github.com/robbyt/go-polyscript"
1212
"github.com/robbyt/go-polyscript/engine/options"
1313
"github.com/robbyt/go-polyscript/execution/data"
14-
"github.com/robbyt/go-polyscript/machines/extism"
14+
"github.com/robbyt/go-polyscript/machines/extism/compiler"
1515
)
1616

1717
// findWasmFile searches for the Extism WASM file in various likely locations
@@ -66,7 +66,7 @@ func runExtismExample(handler slog.Handler) (map[string]any, error) {
6666
options.WithDefaults(),
6767
options.WithLogHandler(handler),
6868
options.WithDataProvider(dataProvider),
69-
extism.WithEntryPoint("greet"),
69+
compiler.WithEntryPoint("greet"),
7070
)
7171
if err != nil {
7272
logger.Error("Failed to create evaluator", "error", err)

examples/simple/risor/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
"github.com/robbyt/go-polyscript/engine/options"
1212
"github.com/robbyt/go-polyscript/execution/constants"
1313
"github.com/robbyt/go-polyscript/execution/data"
14-
"github.com/robbyt/go-polyscript/machines/risor"
14+
"github.com/robbyt/go-polyscript/machines/risor/compiler"
1515
)
1616

1717
//go:embed testdata/script.risor
@@ -39,7 +39,7 @@ func runRisorExample(handler slog.Handler) (map[string]any, error) {
3939
options.WithDefaults(), // Add defaults option to ensure all required fields are set
4040
options.WithLogHandler(handler),
4141
options.WithDataProvider(dataProvider),
42-
risor.WithGlobals(globals),
42+
compiler.WithGlobals(globals),
4343
)
4444
if err != nil {
4545
logger.Error("Failed to create evaluator", "error", err)

examples/simple/starlark/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
"github.com/robbyt/go-polyscript/engine/options"
1212
"github.com/robbyt/go-polyscript/execution/constants"
1313
"github.com/robbyt/go-polyscript/execution/data"
14-
"github.com/robbyt/go-polyscript/machines/starlark"
14+
"github.com/robbyt/go-polyscript/machines/starlark/compiler"
1515
)
1616

1717
//go:embed testdata/script.star
@@ -39,7 +39,7 @@ func runStarlarkExample(handler slog.Handler) (map[string]any, error) {
3939
options.WithDefaults(),
4040
options.WithLogHandler(handler),
4141
options.WithDataProvider(dataProvider),
42-
starlark.WithGlobals(globals),
42+
compiler.WithGlobals(globals),
4343
)
4444
if err != nil {
4545
logger.Error("Failed to create evaluator", "error", err)

execution/script/compilerOptions.go

Lines changed: 0 additions & 22 deletions
This file was deleted.

execution/script/errors.go

Lines changed: 0 additions & 8 deletions
This file was deleted.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package script
2+
3+
import (
4+
"testing"
5+
6+
machineTypes "github.com/robbyt/go-polyscript/machines/types"
7+
"github.com/stretchr/testify/require"
8+
)
9+
10+
func TestExecutableContent(t *testing.T) {
11+
t.Parallel()
12+
13+
t.Run("GetSource", func(t *testing.T) {
14+
mockContent := new(MockExecutableContent)
15+
expectedSource := "print('Hello, World!')"
16+
mockContent.On("GetSource").Return(expectedSource)
17+
18+
source := mockContent.GetSource()
19+
require.Equal(t, expectedSource, source, "Expected source to match")
20+
mockContent.AssertExpectations(t)
21+
})
22+
23+
t.Run("GetByteCode", func(t *testing.T) {
24+
mockContent := new(MockExecutableContent)
25+
expectedByteCode := []byte{0x01, 0x02, 0x03}
26+
mockContent.On("GetByteCode").Return(expectedByteCode)
27+
28+
byteCode := mockContent.GetByteCode()
29+
require.Equal(t, expectedByteCode, byteCode, "Expected bytecode to match")
30+
mockContent.AssertExpectations(t)
31+
})
32+
33+
t.Run("GetMachineType", func(t *testing.T) {
34+
mockContent := new(MockExecutableContent)
35+
expectedType := machineTypes.Risor
36+
mockContent.On("GetMachineType").Return(expectedType)
37+
38+
machineType := mockContent.GetMachineType()
39+
require.Equal(t, expectedType, machineType, "Expected machine type to match")
40+
mockContent.AssertExpectations(t)
41+
})
42+
}

execution/script/executableUnit_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
var emptyScriptData = make(map[string]any)
2222

2323
// Mock implementations
24-
2524
type mockLoader struct {
2625
mock.Mock
2726
}

execution/script/mocks.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func (m *MockCompiler) Compile(scriptReader io.ReadCloser) (ExecutableContent, e
2222
return execContent, args.Error(1)
2323
}
2424

25-
// MockExecutableContent is a mock implementation of the ExecutableContent interface.
25+
// MockExecutableContent is a mock implementation of the ExecutableContent interface for testing.
2626
type MockExecutableContent struct {
2727
mock.Mock
2828
}
@@ -41,8 +41,3 @@ func (m *MockExecutableContent) GetMachineType() machineTypes.Type {
4141
args := m.Called()
4242
return args.Get(0).(machineTypes.Type)
4343
}
44-
45-
func (m *MockExecutableContent) GetBodyChecksum() string {
46-
args := m.Called()
47-
return args.String(0)
48-
}

machines/extism/interfaces.go renamed to machines/extism/adapters/interfaces.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package extism
1+
package adapters
22

33
import (
44
"context"
@@ -10,14 +10,14 @@ type ExtismExecutable struct {
1010
GetExtismExecutable func() *extismSDK.CompiledPlugin
1111
}
1212

13-
// compiledPlugin is an interface for abstracting the extismSDK.CompiledPlugin
14-
type compiledPlugin interface {
15-
Instance(ctx context.Context, config extismSDK.PluginInstanceConfig) (pluginInstance, error)
13+
// CompiledPlugin is an interface for abstracting the extismSDK.CompiledPlugin
14+
type CompiledPlugin interface {
15+
Instance(ctx context.Context, config extismSDK.PluginInstanceConfig) (PluginInstance, error)
1616
Close(ctx context.Context) error
1717
}
1818

19-
// pluginInstance is an interface for abstracting the extismSDK.Plugin
20-
type pluginInstance interface {
19+
// PluginInstance is an interface for abstracting the extismSDK.Plugin
20+
type PluginInstance interface {
2121
Call(name string, data []byte) (uint32, []byte, error)
2222
CallWithContext(ctx context.Context, name string, data []byte) (uint32, []byte, error)
2323
FunctionExists(name string) bool

0 commit comments

Comments
 (0)