Skip to content

make Executable and NewExecutable private #16

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 1 commit into from
Apr 8, 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
2 changes: 1 addition & 1 deletion machines/extism/bytecodeEvaluator.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ func (be *BytecodeEvaluator) Eval(ctx context.Context) (engine.EvaluatorResponse
logger = logger.With("exeID", exeID)

// 1. Type assert to WASM module, and get the compiled plugin object
wasmExe, ok := be.execUnit.GetContent().(*Executable)
wasmExe, ok := be.execUnit.GetContent().(*executable)
if !ok {
return nil, fmt.Errorf(
"invalid executable type: expected *Executable, got %T",
Expand Down
4 changes: 2 additions & 2 deletions machines/extism/bytecodeEvaluator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,7 @@ func TestEvalWithCancelledContext(t *testing.T) {
require.NoError(t, err, "Failed to compile plugin")

// Create our executable
exec := NewExecutable(wasmContent, compiledPlugin, "greet")
exec := newExecutable(wasmContent, compiledPlugin, "greet")

// Create a context provider
ctxProvider := data.NewContextProvider(constants.EvalData)
Expand Down Expand Up @@ -608,7 +608,7 @@ func TestStaticAndDynamicDataCombination(t *testing.T) {
require.NoError(t, err, "Failed to compile plugin")

// Create our executable
exec := NewExecutable(wasmContent, compiledPlugin, "greet")
exec := newExecutable(wasmContent, compiledPlugin, "greet")

// Create a context provider for runtime data
ctxProvider := data.NewContextProvider(constants.EvalData)
Expand Down
2 changes: 1 addition & 1 deletion machines/extism/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func (c *Compiler) Compile(scriptReader io.ReadCloser) (script.ExecutableContent
}

// Create executable with the compiled plugin
executable := NewExecutable(scriptBytes, plugin, funcName)
executable := newExecutable(scriptBytes, plugin, funcName)
if executable == nil {
logger.Warn("Failed to create Executable from WASM plugin")
return nil, ErrExecCreationFailed
Expand Down
6 changes: 3 additions & 3 deletions machines/extism/compiler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func TestCompiler(t *testing.T) {
require.NotNil(t, execContent)

// Type assertion
executable, ok := execContent.(*Executable)
executable, ok := execContent.(*executable)
require.True(t, ok, "Expected *Executable type")

// Validate source matches
Expand Down Expand Up @@ -138,7 +138,7 @@ func TestCompiler(t *testing.T) {
require.NotNil(t, execContent)

// Type assertion
executable, ok := execContent.(*Executable)
executable, ok := execContent.(*executable)
require.True(t, ok, "Expected *Executable type")

// Validate source matches
Expand Down Expand Up @@ -189,7 +189,7 @@ func TestCompiler(t *testing.T) {
require.NotNil(t, execContent)

// Type assertion
executable, ok := execContent.(*Executable)
executable, ok := execContent.(*executable)
require.True(t, ok, "Expected *Executable type")

// Validate source matches
Expand Down
22 changes: 11 additions & 11 deletions machines/extism/executable.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,58 +11,58 @@ import (

var ErrExecutableClosed = errors.New("executable is closed")

// Executable implements script.ExecutableContent for Extism WASM modules
type Executable struct {
// executable implements script.ExecutableContent for Extism WASM modules
type executable struct {
scriptBytes []byte
ByteCode compiledPlugin
entryPoint string
closed atomic.Bool
rwMutex sync.RWMutex
}

// NewExecutable creates a new Executable instance
func NewExecutable(scriptBytes []byte, byteCode compiledPlugin, entryPoint string) *Executable {
// newExecutable creates a new Executable instance
func newExecutable(scriptBytes []byte, byteCode compiledPlugin, entryPoint string) *executable {
if len(scriptBytes) == 0 || byteCode == nil || entryPoint == "" {
return nil
}
return &Executable{
return &executable{
scriptBytes: scriptBytes,
ByteCode: byteCode,
entryPoint: entryPoint,
}
}

// GetSource returns the original script content
func (e *Executable) GetSource() string {
func (e *executable) GetSource() string {
return string(e.scriptBytes)
}

// GetByteCode returns the compiled plugin as a generic interface
func (e *Executable) GetByteCode() any {
func (e *executable) GetByteCode() any {
e.rwMutex.RLock()
defer e.rwMutex.RUnlock()
return e.ByteCode
}

// GetExtismByteCode returns the compiled plugin with its proper type
func (e *Executable) GetExtismByteCode() compiledPlugin {
func (e *executable) GetExtismByteCode() compiledPlugin {
e.rwMutex.RLock()
defer e.rwMutex.RUnlock()
return e.ByteCode
}

// GetMachineType returns the Extism machine type
func (e *Executable) GetMachineType() machineTypes.Type {
func (e *executable) GetMachineType() machineTypes.Type {
return machineTypes.Extism
}

// GetEntryPoint returns the name of the entry point function
func (e *Executable) GetEntryPoint() string {
func (e *executable) GetEntryPoint() string {
return e.entryPoint
}

// Close implements io.Closer
func (e *Executable) Close(ctx context.Context) error {
func (e *executable) Close(ctx context.Context) error {
e.rwMutex.Lock()
defer e.rwMutex.Unlock()

Expand Down
10 changes: 5 additions & 5 deletions machines/extism/executable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,25 +73,25 @@ func TestNewExecutable(t *testing.T) {

// Empty entry point test
t.Run("empty entry point", func(t *testing.T) {
exe := NewExecutable(wasmBytes, mockPlugin, "")
exe := newExecutable(wasmBytes, mockPlugin, "")
assert.Nil(t, exe)
})

// Empty script bytes test
t.Run("empty script bytes", func(t *testing.T) {
exe := NewExecutable(nil, mockPlugin, entryPoint)
exe := newExecutable(nil, mockPlugin, entryPoint)
assert.Nil(t, exe)
})

// Nil plugin test
t.Run("nil plugin", func(t *testing.T) {
exe := NewExecutable(wasmBytes, nil, entryPoint)
exe := newExecutable(wasmBytes, nil, entryPoint)
assert.Nil(t, exe)
})

// Valid creation test
t.Run("valid creation", func(t *testing.T) {
exe := NewExecutable(wasmBytes, mockPlugin, entryPoint)
exe := newExecutable(wasmBytes, mockPlugin, entryPoint)
require.NotNil(t, exe)

// Verify properties
Expand All @@ -117,7 +117,7 @@ func TestExecutable_Close(t *testing.T) {
mockPlugin.On("Close", ctx).Return(nil)

// Create executable
exe := NewExecutable(wasmBytes, mockPlugin, entryPoint)
exe := newExecutable(wasmBytes, mockPlugin, entryPoint)
require.NotNil(t, exe)

// Verify initial state
Expand Down
2 changes: 1 addition & 1 deletion machines/risor/bytecodeEvaluator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ func TestLoadInputData(t *testing.T) {
// TestGetMachineType tests the GetMachineType method
func TestGetMachineType(t *testing.T) {
t.Parallel()
exe := &Executable{}
exe := &executable{}
require.Equal(t, types.Risor, exe.GetMachineType())
}

Expand Down
4 changes: 2 additions & 2 deletions machines/risor/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func (c *Compiler) Compile(scriptLoader io.ReadCloser) (script.ExecutableContent
return c.compile(scriptBodyBytes)
}

func (c *Compiler) compile(scriptBodyBytes []byte) (*Executable, error) {
func (c *Compiler) compile(scriptBodyBytes []byte) (*executable, error) {
logger := c.logger.WithGroup("compile")
if len(scriptBodyBytes) == 0 {
return nil, ErrContentNil
Expand Down Expand Up @@ -104,7 +104,7 @@ func (c *Compiler) compile(scriptBodyBytes []byte) (*Executable, error) {
return nil, ErrNoInstructions
}

risorExec := NewExecutable(scriptBodyBytes, bc)
risorExec := newExecutable(scriptBodyBytes, bc)
if risorExec == nil {
logger.Warn("Failed to create Executable from bytecode")
return nil, ErrExecCreationFailed
Expand Down
2 changes: 1 addition & 1 deletion machines/risor/compiler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func runTestCase(t *testing.T, tt testCase) {
require.Equal(t, tt.script, execContent.GetSource(), "Script content does not match")

// Check that the bytecode is correct
risorExec, ok := execContent.(*Executable)
risorExec, ok := execContent.(*executable)
require.True(t, ok, "Expected execContent to be a *Executable")
require.NotNil(t, risorExec.GetRisorByteCode(), "Expected bytecode to be non-nil")

Expand Down
14 changes: 7 additions & 7 deletions machines/risor/executable.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,34 @@ import (
machineTypes "github.com/robbyt/go-polyscript/machines/types"
)

type Executable struct {
type executable struct {
scriptBodyBytes []byte
ByteCode *risorCompiler.Code
}

func NewExecutable(scriptBodyBytes []byte, byteCode *risorCompiler.Code) *Executable {
func newExecutable(scriptBodyBytes []byte, byteCode *risorCompiler.Code) *executable {
if len(scriptBodyBytes) == 0 || byteCode == nil {
return nil
}

return &Executable{
return &executable{
scriptBodyBytes: scriptBodyBytes,
ByteCode: byteCode,
}
}

func (e *Executable) GetSource() string {
func (e *executable) GetSource() string {
return string(e.scriptBodyBytes)
}

func (e *Executable) GetByteCode() any {
func (e *executable) GetByteCode() any {
return e.ByteCode
}

func (e *Executable) GetRisorByteCode() *risorCompiler.Code {
func (e *executable) GetRisorByteCode() *risorCompiler.Code {
return e.ByteCode
}

func (e *Executable) GetMachineType() machineTypes.Type {
func (e *executable) GetMachineType() machineTypes.Type {
return machineTypes.Risor
}
22 changes: 11 additions & 11 deletions machines/risor/executable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func TestNewExecutableValid(t *testing.T) {
content := "print('Hello, World!')"
bytecode := &risorCompiler.Code{}

executable := NewExecutable([]byte(content), bytecode)
executable := newExecutable([]byte(content), bytecode)
require.NotNil(t, executable)
assert.Equal(t, content, executable.GetSource())
assert.Equal(t, bytecode, executable.GetByteCode())
Expand All @@ -24,29 +24,29 @@ func TestNewExecutableValid(t *testing.T) {
func TestNewExecutableNilContent(t *testing.T) {
bytecode := &risorCompiler.Code{}

executable := NewExecutable(nil, bytecode)
executable := newExecutable(nil, bytecode)
require.Nil(t, executable)
}

// TestNewExecutableNilByteCode tests creating an Executable with nil bytecode
func TestNewExecutableNilByteCode(t *testing.T) {
content := "print('Hello, World!')"

executable := NewExecutable([]byte(content), nil)
executable := newExecutable([]byte(content), nil)
require.Nil(t, executable)
}

// TestNewExecutableNilContentAndByteCode tests creating an Executable with nil content and bytecode
func TestNewExecutableNilContentAndByteCode(t *testing.T) {
executable := NewExecutable(nil, nil)
executable := newExecutable(nil, nil)
require.Nil(t, executable)
}

// TestExecutable_GetBody tests the GetBody method of Executable
func TestExecutable_GetBody(t *testing.T) {
content := "print('Hello, World!')"
bytecode := &risorCompiler.Code{}
executable := NewExecutable([]byte(content), bytecode)
executable := newExecutable([]byte(content), bytecode)
require.NotNil(t, executable)

body := executable.GetSource()
Expand All @@ -57,7 +57,7 @@ func TestExecutable_GetBody(t *testing.T) {
func TestExecutable_GetByteCode(t *testing.T) {
content := "print('Hello, World!')"
bytecode := &risorCompiler.Code{}
executable := NewExecutable([]byte(content), bytecode)
executable := newExecutable([]byte(content), bytecode)
require.NotNil(t, executable)

code := executable.GetByteCode()
Expand All @@ -72,7 +72,7 @@ func TestExecutable_GetByteCode(t *testing.T) {
func TestExecutable_GetRisorByteCode(t *testing.T) {
content := "print('Hello, World!')"
bytecode := &risorCompiler.Code{}
executable := NewExecutable([]byte(content), bytecode)
executable := newExecutable([]byte(content), bytecode)
require.NotNil(t, executable)

code := executable.GetRisorByteCode()
Expand All @@ -84,26 +84,26 @@ func TestNewExecutable(t *testing.T) {
content := "print('test')"
bytecode := &risorCompiler.Code{}

exe := NewExecutable([]byte(content), bytecode)
exe := newExecutable([]byte(content), bytecode)
require.NotNil(t, exe)
assert.Equal(t, content, exe.GetSource())
assert.Equal(t, bytecode, exe.ByteCode)
})

t.Run("nil content", func(t *testing.T) {
bytecode := &risorCompiler.Code{}
exe := NewExecutable(nil, bytecode)
exe := newExecutable(nil, bytecode)
assert.Nil(t, exe)
})

t.Run("nil bytecode", func(t *testing.T) {
content := "print('test')"
exe := NewExecutable([]byte(content), nil)
exe := newExecutable([]byte(content), nil)
assert.Nil(t, exe)
})

t.Run("both nil", func(t *testing.T) {
exe := NewExecutable(nil, nil)
exe := newExecutable(nil, nil)
assert.Nil(t, exe)
})
}
4 changes: 2 additions & 2 deletions machines/starlark/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (c *Compiler) Compile(scriptReader io.ReadCloser) (script.ExecutableContent
return c.compile(scriptBodyBytes)
}

func (c *Compiler) compile(scriptBodyBytes []byte) (*Executable, error) {
func (c *Compiler) compile(scriptBodyBytes []byte) (*executable, error) {
logger := c.logger.WithGroup("compile")
if len(scriptBodyBytes) == 0 {
logger.Error("Compile called with nil script")
Expand All @@ -76,7 +76,7 @@ func (c *Compiler) compile(scriptBodyBytes []byte) (*Executable, error) {
}

// Create executable with the compiled program
starlarkExec := NewExecutable(scriptBodyBytes, program)
starlarkExec := newExecutable(scriptBodyBytes, program)
if starlarkExec == nil {
logger.Warn("Failed to create Executable from program")
return nil, ErrExecCreationFailed
Expand Down
Loading