Moonshark/runner/context.go
2025-06-06 18:57:47 -05:00

59 lines
1.2 KiB
Go

package runner
import (
"sync"
)
// Generic interface to support different types of execution contexts for the runner
type ExecutionContext interface {
Get(key string) any
Set(key string, value any)
ToMap() map[string]any
Release()
}
// This is a generic context that satisfies the runner's ExecutionContext interface
type Context struct {
Values map[string]any // Any data we want to pass to the state's global ctx table.
}
// Context pool to reduce allocations
var contextPool = sync.Pool{
New: func() any {
return &Context{
Values: make(map[string]any, 32),
}
},
}
// Gets a new context from the pool
func NewContext() *Context {
ctx := contextPool.Get().(*Context)
return ctx
}
// Release returns the context to the pool after clearing its values
func (c *Context) Release() {
// Clear all values to prevent data leakage
for k := range c.Values {
delete(c.Values, k)
}
contextPool.Put(c)
}
// Set adds a value to the context
func (c *Context) Set(key string, value any) {
c.Values[key] = value
}
// Get retrieves a value from the context
func (c *Context) Get(key string) any {
return c.Values[key]
}
// We can just return the Values map as it's already g2g for Lua
func (c *Context) ToMap() map[string]any {
return c.Values
}