quickjsengine

package
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Jul 10, 2025 License: Apache-2.0 Imports: 4 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewFactory

func NewFactory(options ...Option) jsexecutor.JsEngineFactory

NewFactory returns a JsEngineFactory that creates QuickJS engines with the given options.

Types

type Engine

type Engine struct {
	Runtime   *quickjs.Runtime // QuickJS runtime instance
	Ctx       *quickjs.Context // QuickJS context instance
	Option    *EngineOption    // Engine configuration options
	RpcScript string           // Embedded RPC script for executing requests
}

Engine represents a QuickJS engine instance with its runtime, context, and options.

func (*Engine) Close

func (e *Engine) Close() error

Close releases all resources associated with the engine, including context and runtime.

func (*Engine) Execute

func (e *Engine) Execute(req *jsexecutor.JsRequest) (*jsexecutor.JsResponse, error)

Execute runs a JavaScript request using the embedded RPC script as the entry point. The request is marshaled to JS, passed to the RPC function, and the response is unmarshaled back to Go.

func (*Engine) Init

func (e *Engine) Init(scripts []*jsexecutor.InitScript) error

Init executes the provided initialization scripts in the engine context. Each script is evaluated in order. If any script fails, an error is returned.

func (*Engine) Reload

func (e *Engine) Reload(scripts []*jsexecutor.InitScript) error

Reload resets the engine context and re-initializes it with the provided scripts. The old context is closed and a new one is created.

type EngineOption

type EngineOption struct {
	Timeout            uint64 `json:"timeout"`            // Script execution timeout in seconds (0 = no timeout)
	MemoryLimit        uint64 `json:"memoryLimit"`        // Memory limit in bytes (0 = no limit)
	GCThreshold        int64  `json:"gcThreshold"`        // GC threshold in bytes (-1 = disable, 0 = default)
	MaxStackSize       uint64 `json:"maxStackSize"`       // Stack size in bytes (0 = default)
	CanBlock           bool   `json:"canBlock"`           // Whether the runtime can block (for async operations)
	EnableModuleImport bool   `json:"enableModuleImport"` // Enable ES6 module import support
	Strip              int    `json:"strip"`              // Strip level for bytecode compilation
}

EngineOption holds configuration options for a QuickJS engine instance.

type Option

type Option func(*Engine) error

Option is a function that applies a configuration to an Engine instance.

func WithCanBlock

func WithCanBlock(canBlock bool) Option

WithCanBlock enables or disables blocking operations in the runtime. Enabling this may affect performance and security.

func WithEnableModuleImport

func WithEnableModuleImport(enable bool) Option

WithEnableModuleImport enables or disables ES6 module import support. Enabling this may have security implications.

func WithGCThreshold

func WithGCThreshold(threshold int64) Option

WithGCThreshold sets the garbage collection threshold for the engine. Use -1 to disable automatic GC, 0 for default, or a positive value for a custom threshold.

func WithMaxStackSize

func WithMaxStackSize(size uint64) Option

WithMaxStackSize sets the stack size for the JavaScript runtime in bytes. If size is 0, the default stack size is used.

func WithMemoryLimit

func WithMemoryLimit(limit uint64) Option

WithMemoryLimit sets the memory limit for the JavaScript runtime in bytes. If limit is 0, there is no memory limit.

func WithRpcScript

func WithRpcScript(script string) Option

WithRpcScript sets the RPC script for the engine. The script must not be empty.

func WithStrip

func WithStrip(strip int) Option

WithStrip sets the strip level for bytecode compilation. 0 = no stripping, higher values strip more debug information.

func WithTimeout

func WithTimeout(timeout uint64) Option

WithTimeout sets the script execution timeout in seconds. If timeout is 0, there is no timeout.

Jump to

Keyboard shortcuts

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