Documentation
¶
Overview ¶
Package eventloop owns a QuickJS runtime, context, and scheduler on one OS thread.
Index ¶
- Variables
- func RegisterContextResource(ctx *quickjs.Context, resource Resource) bool
- type Context
- func (c *Context) Bind(name string, target any) error
- func (c *Context) CheckGeneration(generation uint64) error
- func (c *Context) Eval(code string, opts ...quickjs.EvalOption) *quickjs.Value
- func (c *Context) Exception() error
- func (c *Context) Generation() uint64
- func (c *Context) Globals() *quickjs.Value
- func (c *Context) LoadModule(code, moduleName string, opts ...quickjs.EvalOption) *quickjs.Value
- func (c *Context) Raw() *quickjs.Context
- func (c *Context) Throw(value *quickjs.Value) *quickjs.Value
- func (c *Context) ThrowError(err error) *quickjs.Value
- func (c *Context) ThrowRangeError(format string, args ...any) *quickjs.Value
- func (c *Context) ThrowTypeError(format string, args ...any) *quickjs.Value
- type EventLoop
- func (l *EventLoop) CheckGeneration(generation uint64) error
- func (l *EventLoop) ClearImmediate(immediate *Immediate)
- func (l *EventLoop) ClearInterval(interval *Interval)
- func (l *EventLoop) ClearTimeout(timer *Timer)
- func (l *EventLoop) Close() error
- func (l *EventLoop) Context() *Context
- func (l *EventLoop) ContextTask(task func(*Context) error) error
- func (l *EventLoop) Do(task Task) error
- func (l *EventLoop) DoContext(task func(*Context) error) error
- func (l *EventLoop) Generation() uint64
- func (l *EventLoop) RegisterResource(resource Resource) bool
- func (l *EventLoop) Reload() error
- func (l *EventLoop) Run(task Task) error
- func (l *EventLoop) RunContext(task func(*Context) error) error
- func (l *EventLoop) Schedule(task Task) bool
- func (l *EventLoop) SetImmediate(task Task) *Immediate
- func (l *EventLoop) SetInterval(task Task, interval time.Duration) *Interval
- func (l *EventLoop) SetTimeout(task Task, delay time.Duration) *Timer
- func (l *EventLoop) Start() error
- func (l *EventLoop) Stop() error
- type GlobalInstaller
- type Immediate
- type Interval
- type Logger
- type Option
- type Resource
- type Task
- type Timer
Constants ¶
This section is empty.
Variables ¶
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 ¶
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 ¶
Bind publishes a reflection-backed host object at globalThis[name]. Exported struct fields become accessors and exported methods become JS functions.
func (*Context) CheckGeneration ¶
CheckGeneration verifies that generation still refers to the active context.
func (*Context) Eval ¶
Eval evaluates JavaScript on the owner goroutine. Call it from a ContextTask, DoContext, or RunContext callback.
func (*Context) Generation ¶
Generation returns the generation that owns the current raw context.
func (*Context) LoadModule ¶
LoadModule evaluates an ESM source with the supplied module name.
func (*Context) ThrowError ¶
ThrowError creates and throws a generic error in this context.
func (*Context) ThrowRangeError ¶
ThrowRangeError creates and throws a RangeError in this context.
type EventLoop ¶
type EventLoop struct {
// contains filtered or unexported fields
}
EventLoop owns all QuickJS operations and scheduler state.
func (*EventLoop) CheckGeneration ¶
CheckGeneration verifies that generation still refers to the active context.
func (*EventLoop) ClearImmediate ¶
ClearImmediate cancels a next-turn callback.
func (*EventLoop) ClearInterval ¶
ClearInterval cancels a repeating timer.
func (*EventLoop) ClearTimeout ¶
ClearTimeout cancels a one-shot timer.
func (*EventLoop) ContextTask ¶
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) Generation ¶
Generation returns the current monotonically increasing context generation.
func (*EventLoop) RegisterResource ¶
RegisterResource arranges for a resource to close before the runtime closes.
func (*EventLoop) Reload ¶
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) RunContext ¶
RunContext starts a new or stopped loop, executes the task, and drains work until the loop becomes idle.
func (*EventLoop) Schedule ¶
Schedule queues task without touching QuickJS. It is safe from any goroutine.
func (*EventLoop) SetImmediate ¶
SetImmediate queues a callback for the next scheduler turn.
func (*EventLoop) SetInterval ¶
SetInterval queues a repeating callback.
func (*EventLoop) SetTimeout ¶
SetTimeout queues a one-shot callback.
type GlobalInstaller ¶
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.
type Interval ¶
type Interval struct {
// contains filtered or unexported fields
}
Interval is a cancellable repeating timer handle.
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 ¶
WithLogger sets the logger used for background failures.
func WithModuleImport ¶
WithModuleImport controls QuickJS's default file-backed ESM loader. Native modules registered with WithRegistry remain available when it is disabled.
func WithRegistry ¶
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.