vm

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Jun 18, 2025 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddPending

func AddPending(rt *sobek.Runtime)

AddPending adds a pending operation for the given runtime

func Cleanup

func Cleanup(rt *sobek.Runtime, job ...func())

Cleanup adds cleanup functions for the given runtime

func RemovePending

func RemovePending(rt *sobek.Runtime)

RemovePending removes a pending operation for the given runtime

Types

type Enqueue

type Enqueue func(func() error)

Enqueue add a job to the job queue.

func EnqueueJob

func EnqueueJob(rt *sobek.Runtime) Enqueue

EnqueueJob returns a function to enqueue jobs for the given runtime

type EventLoop

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

EventLoop implements an event loop for asynchronous JavaScript operations

func NewEventLoop

func NewEventLoop() *EventLoop

NewEventLoop creates a new EventLoop instance

func (*EventLoop) AddPending

func (e *EventLoop) AddPending()

AddPending increments the pending operation counter

func (*EventLoop) Cleanup

func (e *EventLoop) Cleanup(job ...func())

Cleanup add a function to execute when run finish.

func (*EventLoop) EnqueueJob

func (e *EventLoop) EnqueueJob() Enqueue

EnqueueJob return a function Enqueue to add a job to the job queue.

func (*EventLoop) RemovePending

func (e *EventLoop) RemovePending()

RemovePending decrements the pending operation counter

func (*EventLoop) Start

func (e *EventLoop) Start(task func() error) (err error)

Start the event loop and execute the provided function

func (*EventLoop) Stop

func (e *EventLoop) Stop(err error)

Stop the eventloop with the provided error

type GlobalModule

type GlobalModule interface {
	GetGlobalName() string
	CreateGlobalObject(runtime *sobek.Runtime) sobek.Value
}

GlobalModule interface for modules that provide global objects These modules will be automatically available as globals (like fetch, console)

type Module

type Module interface {
	Name() string
	Setup(runtime *sobek.Runtime, manager *VMManager) error
	Cleanup() error
	IsEnabled(enabledModules map[string]bool) bool
}

Module interface defines how modules integrate with the VM

type ModuleCreator

type ModuleCreator interface {
	CreateModuleObject(runtime *sobek.Runtime) sobek.Value
}

ModuleCreator interface for modules that can create their own objects This replaces the old require override pattern

type ModuleLoader

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

ModuleLoader provides a global require system for modules Based on a standard loader pattern but simplified for our use case

func NewModuleLoader

func NewModuleLoader() *ModuleLoader

NewModuleLoader creates a new module loader

func (*ModuleLoader) EnableRequire

func (l *ModuleLoader) EnableRequire(rt *sobek.Runtime, enabledModules map[string]bool)

EnableRequire sets up the global require function in the runtime

func (*ModuleLoader) RegisterModule

func (l *ModuleLoader) RegisterModule(module Module)

RegisterModule registers a module with the loader

func (*ModuleLoader) SetupGlobals

func (l *ModuleLoader) SetupGlobals(rt *sobek.Runtime, enabledModules map[string]bool)

SetupGlobals sets up global objects for modules that implement GlobalModule

type ModuleRegistry

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

ModuleRegistry manages available modules

func NewModuleRegistry

func NewModuleRegistry() *ModuleRegistry

NewModuleRegistry creates a new module registry

func (*ModuleRegistry) Get

func (r *ModuleRegistry) Get(name string) (Module, bool)

Get retrieves a module by name

func (*ModuleRegistry) GetEnabled

func (r *ModuleRegistry) GetEnabled(enabledModules map[string]bool) []Module

GetEnabled returns all enabled modules based on configuration

func (*ModuleRegistry) List

func (r *ModuleRegistry) List() []string

List returns all registered module names

func (*ModuleRegistry) Register

func (r *ModuleRegistry) Register(module Module)

Register adds a module to the registry

type VM

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

VM wraps a Sobek runtime with event loop support

func (*VM) Close

func (vm *VM) Close() error

Close cleans up the VM and its modules

func (*VM) RunString

func (vm *VM) RunString(code string) (ret sobek.Value, err error)

RunString executes JavaScript code in the VM with event loop support This matches the standard pattern where RunString always uses the event loop

func (*VM) Runtime

func (vm *VM) Runtime() *sobek.Runtime

Runtime returns the underlying Sobek runtime

func (*VM) SetGlobal

func (vm *VM) SetGlobal(name string, value interface{})

SetGlobal sets a global variable in the VM

type VMManager

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

VMManager manages Sobek VM instances

func NewVMManager

func NewVMManager(enabledModules []string) *VMManager

NewVMManager creates a new VM manager with specified enabled modules

func (*VMManager) CreateVM

func (m *VMManager) CreateVM(ctx context.Context) (*VM, error)

CreateVM creates a new VM instance with all enabled modules Each VM is completely isolated

func (*VMManager) GetEnabledModules

func (m *VMManager) GetEnabledModules() []string

GetEnabledModules returns the list of enabled module names

func (*VMManager) RegisterModule

func (m *VMManager) RegisterModule(module Module) error

RegisterModule adds a module to the manager

Jump to

Keyboard shortcuts

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