eventloop

package
v0.0.0-...-015fb79 Latest Latest
Warning

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

Go to latest
Published: Aug 30, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

Package eventloop owns a QuickJS runtime, context, and scheduler on one OS thread.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrClosed reports that the event loop has been closed permanently.
	ErrClosed = errors.New("event loop is closed")
	// ErrStopped reports that a synchronous task requires a running loop.
	ErrStopped = errors.New("event loop is stopped")
	// ErrAlreadyRunning reports an attempt to run a blocking task on a running loop.
	ErrAlreadyRunning = errors.New("event loop is already running")
	// ErrNilTask reports a nil task callback.
	ErrNilTask = errors.New("event loop task is nil")
	// ErrStaleGeneration reports a callback targeting an older reloaded context.
	ErrStaleGeneration = errors.New("event loop context generation is stale")
)

Functions

func RegisterContextResource

func RegisterContextResource(ctx *quickjs.Context, resource Resource) bool

RegisterContextResource associates a host resource with the EventLoop that owns ctx. It is useful to module installers that only receive *quickjs.Context.

Types

type Context

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

Context is the canonical owner-bound adapter for one EventLoop generation. Raw returns a QuickJS context that must only be used on the loop owner.

func (*Context) Bind

func (c *Context) Bind(name string, target any) error

Bind publishes a reflection-backed host object at globalThis[name]. Exported struct fields become accessors and exported methods become JS functions.

func (*Context) CheckGeneration

func (c *Context) CheckGeneration(generation uint64) error

CheckGeneration verifies that generation still refers to the active context.

func (*Context) Eval

func (c *Context) Eval(code string, opts ...quickjs.EvalOption) *quickjs.Value

Eval evaluates JavaScript on the owner goroutine. Call it from a ContextTask, DoContext, or RunContext callback.

func (*Context) Exception

func (c *Context) Exception() error

Exception returns the most recent QuickJS exception.

func (*Context) Generation

func (c *Context) Generation() uint64

Generation returns the generation that owns the current raw context.

func (*Context) Globals

func (c *Context) Globals() *quickjs.Value

Globals returns the global object for this context.

func (*Context) LoadModule

func (c *Context) LoadModule(code, moduleName string, opts ...quickjs.EvalOption) *quickjs.Value

LoadModule evaluates an ESM source with the supplied module name.

func (*Context) Raw

func (c *Context) Raw() *quickjs.Context

Raw returns the underlying owner-bound QuickJS context.

func (*Context) Throw

func (c *Context) Throw(value *quickjs.Value) *quickjs.Value

Throw throws an existing exception value in this context.

func (*Context) ThrowError

func (c *Context) ThrowError(err error) *quickjs.Value

ThrowError creates and throws a generic error in this context.

func (*Context) ThrowRangeError

func (c *Context) ThrowRangeError(format string, args ...any) *quickjs.Value

ThrowRangeError creates and throws a RangeError in this context.

func (*Context) ThrowTypeError

func (c *Context) ThrowTypeError(format string, args ...any) *quickjs.Value

ThrowTypeError creates and throws a TypeError in this context.

type EventLoop

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

EventLoop owns all QuickJS operations and scheduler state.

func New

func New(opts ...Option) (*EventLoop, error)

func (*EventLoop) CheckGeneration

func (l *EventLoop) CheckGeneration(generation uint64) error

CheckGeneration verifies that generation still refers to the active context.

func (*EventLoop) ClearImmediate

func (l *EventLoop) ClearImmediate(immediate *Immediate)

ClearImmediate cancels a next-turn callback.

func (*EventLoop) ClearInterval

func (l *EventLoop) ClearInterval(interval *Interval)

ClearInterval cancels a repeating timer.

func (*EventLoop) ClearTimeout

func (l *EventLoop) ClearTimeout(timer *Timer)

ClearTimeout cancels a one-shot timer.

func (*EventLoop) Close

func (l *EventLoop) Close() error

Close permanently stops the owner and releases QuickJS resources.

func (*EventLoop) Context

func (l *EventLoop) Context() *Context

EventLoop returns the adapter for the current context generation.

func (*EventLoop) ContextTask

func (l *EventLoop) ContextTask(task func(*Context) error) error

ContextTask executes a task with the canonical adapter. It is synchronous and may be called before Start; all QuickJS work still runs on the owner.

func (*EventLoop) Do

func (l *EventLoop) Do(task Task) error

Do executes task synchronously while the loop is running.

func (*EventLoop) DoContext

func (l *EventLoop) DoContext(task func(*Context) error) error

DoContext executes a task synchronously while the loop is running.

func (*EventLoop) Generation

func (l *EventLoop) Generation() uint64

Generation returns the current monotonically increasing context generation.

func (*EventLoop) RegisterResource

func (l *EventLoop) RegisterResource(resource Resource) bool

RegisterResource arranges for a resource to close before the runtime closes.

func (*EventLoop) Reload

func (l *EventLoop) Reload() error

Reload replaces the QuickJS runtime and context on the same owner goroutine. Queued tasks, timers, module values, and registered host resources do not cross the generation boundary.

func (*EventLoop) Run

func (l *EventLoop) Run(task Task) error

Run starts or resumes the loop, executes task, and drains until quiescent.

func (*EventLoop) RunContext

func (l *EventLoop) RunContext(task func(*Context) error) error

RunContext starts a new or stopped loop, executes the task, and drains work until the loop becomes idle.

func (*EventLoop) Schedule

func (l *EventLoop) Schedule(task Task) bool

Schedule queues task without touching QuickJS. It is safe from any goroutine.

func (*EventLoop) SetImmediate

func (l *EventLoop) SetImmediate(task Task) *Immediate

SetImmediate queues a callback for the next scheduler turn.

func (*EventLoop) SetInterval

func (l *EventLoop) SetInterval(task Task, interval time.Duration) *Interval

SetInterval queues a repeating callback.

func (*EventLoop) SetTimeout

func (l *EventLoop) SetTimeout(task Task, delay time.Duration) *Timer

SetTimeout queues a one-shot callback.

func (*EventLoop) Start

func (l *EventLoop) Start() error

Start starts or resumes continuous pumping.

func (*EventLoop) Stop

func (l *EventLoop) Stop() error

Stop pauses pumping while retaining queued tasks and timers.

type GlobalInstaller

type GlobalInstaller func(ctx *quickjs.Context) error

GlobalInstaller installs explicit globals in one QuickJS context.

type Immediate

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

Immediate is a cancellable next-turn callback handle.

func (*Immediate) Cancel

func (i *Immediate) Cancel()

Cancel prevents an immediate callback from running.

func (*Immediate) Canceled

func (i *Immediate) Canceled() bool

Canceled reports whether the immediate callback has been cancelled.

type Interval

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

Interval is a cancellable repeating timer handle.

func (*Interval) Cancel

func (i *Interval) Cancel()

Cancel prevents future interval callbacks from running.

func (*Interval) Canceled

func (i *Interval) Canceled() bool

Canceled reports whether the interval has been cancelled.

type Logger

type Logger interface {
	Debug(message string)
	Debugf(format string, args ...any)
	Info(message string)
	Infof(format string, args ...any)
	Warn(message string)
	Warnf(format string, args ...any)
	Error(message string)
	Errorf(format string, args ...any)
}

Logger receives errors from background tasks and QuickJS pump failures.

type Option

type Option func(*config)

Option configures a new EventLoop.

func WithGlobals

func WithGlobals(installers ...GlobalInstaller) Option

WithGlobals installs the supplied globals after module registration.

func WithLogger

func WithLogger(logger Logger) Option

WithLogger sets the logger used for background failures.

func WithModuleImport

func WithModuleImport(enabled bool) Option

WithModuleImport controls QuickJS's default file-backed ESM loader. Native modules registered with WithRegistry remain available when it is disabled.

func WithRegistry

func WithRegistry(registry *module.Registry) Option

WithRegistry registers the supplied in-memory ESM modules in the new context.

type Resource

type Resource interface {
	Close() error
}

Resource is closed on the EventLoop owner before its QuickJS context closes.

type Task

type Task func(ctx *quickjs.Context) error

Task is executed on the event loop owner goroutine.

type Timer

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

Timer is a cancellable one-shot timer handle.

func (*Timer) Cancel

func (t *Timer) Cancel()

Cancel prevents a timer callback from running.

func (*Timer) Canceled

func (t *Timer) Canceled() bool

Canceled reports whether the timer has been cancelled.

Jump to

Keyboard shortcuts

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