jsruntime

package
v1.0.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 1, 2025 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type JSRuntime

type JSRuntime interface {
	// Execute runs JavaScript code and returns the result as a string
	Execute(code string) (string, error)
	// ExecuteWithProps runs a cached bundle with props injected
	// The bundle is compiled once and cached; only props change per request
	ExecuteWithProps(bundle, propsJSON string) (string, error)
	// Close releases resources (called when returning to pool)
	Reset()
	// Destroy permanently destroys the runtime
	Destroy()
}

JSRuntime is the interface for JavaScript execution

type Pool

type Pool struct {
	// contains filtered or unexported fields
}

Pool manages a pool of JS runtimes for reuse

func NewPool

func NewPool(config PoolConfig) *Pool

NewPool creates a new runtime pool

func (*Pool) Close

func (p *Pool) Close()

Close marks the pool as closed and destroys all runtimes

func (*Pool) Execute

func (p *Pool) Execute(code string) (string, error)

Execute is a convenience method that gets a runtime, executes code, and returns it

func (*Pool) ExecuteWithProps

func (p *Pool) ExecuteWithProps(bundle, propsJSON string) (string, error)

ExecuteWithProps executes a cached bundle with props

func (*Pool) Get

func (p *Pool) Get() JSRuntime

Get retrieves a runtime from the pool Blocks if pool is empty - never creates additional runtimes beyond pool size This is critical for v8go safety: concurrent Isolate creation causes crashes

func (*Pool) Put

func (p *Pool) Put(rt JSRuntime)

Put returns a runtime to the pool

func (*Pool) Stats

func (p *Pool) Stats() map[string]interface{}

Stats returns pool statistics

type PoolConfig

type PoolConfig struct {
	RuntimeType RuntimeType
	PoolSize    int // Maximum number of runtimes to keep in pool
}

PoolConfig configures the runtime pool

type RuntimeType

type RuntimeType string

RuntimeType represents the type of JavaScript runtime

const (
	RuntimeQuickJS   RuntimeType = "quickjs"
	RuntimeV8        RuntimeType = "v8"
	RuntimeModerncJS RuntimeType = "moderncjs"
)

func DefaultRuntimeType

func DefaultRuntimeType() RuntimeType

DefaultRuntimeType returns the runtime type for this build

type V8Runtime

type V8Runtime struct {
	// contains filtered or unexported fields
}

V8Runtime wraps V8 for pooled usage

func NewV8Runtime

func NewV8Runtime() *V8Runtime

NewV8Runtime creates a new V8 runtime

func (*V8Runtime) Destroy

func (v *V8Runtime) Destroy()

Destroy permanently destroys the runtime

func (*V8Runtime) Execute

func (v *V8Runtime) Execute(code string) (string, error)

Execute runs JavaScript code and returns the result

func (*V8Runtime) ExecuteWithProps

func (v *V8Runtime) ExecuteWithProps(bundle, propsJSON string) (string, error)

ExecuteWithProps runs a cached bundle with props injected The bundle is compiled once as UnboundScript and cached Only the small props script is compiled each request

func (*V8Runtime) Reset

func (v *V8Runtime) Reset()

Reset prepares the runtime for reuse Context is reused to avoid expensive context creation/destruction Context is only recreated periodically to prevent memory buildup.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL