Get Started



import "github.com/grexie/isolates"



var ErrNoContext = errors.New("isolates.ExecutionContext: no context available")
var ErrNotResolved = errors.New("unable to resolve")

Version exposes the compiled-in version of the linked V8 library. This can be used to test for specific javascript functionality support (e.g. ES6 destructuring isn't supported before major version 5.).

var Version = struct{ Major, Minor, Build, Patch int }{
    Major: int(C.version.major),
    Minor: int(C.version.minor),
    Build: int(C.version.build),
    Patch: int(C.version.patch),

func AddMarshaller

func AddMarshaller(t reflect.Type, marshaller any) error

func AddUnmarshaller

func AddUnmarshaller(t reflect.Type, unmarshaller any) error

func Initialize

func Initialize()

func WithContext

func WithContext(ctx context.Context) context.Context

type CallerInfo

type CallerInfo struct {
    Name     string
    Filename string
    Line     int
    Column   int

type Console

type Console interface {
    Assert(args ...any)
    Count(args ...any)
    CountReset(args ...any)
    Debug(args ...any)
    Dir(args ...any)
    DirXML(args ...any)
    Error(args ...any)
    Group(args ...any)
    GroupCollapsed(args ...any)
    GroupEnd(args ...any)
    Info(args ...any)
    Log(args ...any)
    Table(args ...any)
    Time(args ...any)
    TimeEnd(args ...any)
    TimeLog(args ...any)
    Trace(args ...any)
    Warn(args ...any)
    Profile(args ...any)
    ProfileEnd(args ...any)
    TimeStamp(args ...any)

type Context

type Context struct {
    // contains filtered or unexported fields

func (*Context) AddMicrotask

func (c *Context) AddMicrotask(ctx context.Context, fn func(in FunctionArgs) error) error

func (*Context) Assign

func (c *Context) Assign(ctx context.Context, objects ...any) (*Value, error)

func (*Context) AssignAll

func (c *Context) AssignAll(ctx context.Context, objects ...any) (*Value, error)

func (*Context) Create

func (c *Context) Create(ctx context.Context, v interface{}) (*Value, error)

func (*Context) CreateAll

func (c *Context) CreateAll(ctx context.Context, objects ...any) ([]*Value, error)

func (*Context) CreateConstructor

func (c *Context) CreateConstructor(ctx context.Context, cons interface{}) (*Value, error)

func (*Context) CreateConstructorWithName

func (c *Context) CreateConstructorWithName(ctx context.Context, name string, cons interface{}) (*Value, error)

func (*Context) CreateFunction

func (c *Context) CreateFunction(ctx context.Context, name *string, function Function) (*Value, error)

func (*Context) CreateRequire

func (c *Context) CreateRequire(ctx context.Context, fs any, path string, extensions *Value, modules map[string]*Module, runtimes map[string]bool) (*Value, error)

func (*Context) CreateWithName

func (c *Context) CreateWithName(ctx context.Context, name string, v interface{}) (*Value, error)

func (*Context) CreateWithoutMarshallers

func (c *Context) CreateWithoutMarshallers(ctx context.Context, v interface{}) (*Value, error)

func (*Context) Data

func (c *Context) Data(key any) (any, bool)

func (*Context) ErrorConstructor

func (c *Context) ErrorConstructor(ctx context.Context) (*Value, error)

func (*Context) False

func (c *Context) False(ctx context.Context) (*Value, error)

func (*Context) GetIsolate

func (c *Context) GetIsolate() *Isolate

func (*Context) Global

func (c *Context) Global(ctx context.Context) (*Value, error)

func (*Context) New

func (c *Context) New(ctx context.Context, cons any, args ...any) (any, error)

func (*Context) NewFunctionTemplate

func (c *Context) NewFunctionTemplate(ctx context.Context, cb Function) (*FunctionTemplate, error)

func (*Context) NewObject

func (c *Context) NewObject(ctx context.Context) (*Value, error)

func (*Context) NewResolver

func (c *Context) NewResolver(ctx context.Context) (*Resolver, error)

func (*Context) Null

func (c *Context) Null(ctx context.Context) (*Value, error)

func (*Context) ObjectCreate

func (c *Context) ObjectCreate(ctx context.Context, args ...any) (*Value, error)

func (*Context) ParseJSON

func (c *Context) ParseJSON(ctx context.Context, json string) (*Value, error)

func (*Context) Receiver

func (c *Context) Receiver(ctx context.Context, self *Value, t reflect.Type) (reflect.Value, error)

func (*Context) Receivers

func (c *Context) Receivers() int

func (*Context) Run

func (c *Context) Run(ctx context.Context, code string, filename string, module *Module) (*Value, error)

func (*Context) RunWithRuntime

func (c *Context) RunWithRuntime(ctx context.Context, fs any, path string, env func(RuntimeFunctionArgs) error, security map[string]bool) (*Value, error)

func (*Context) SetData

func (c *Context) SetData(key any, value any)

func (*Context) True

func (c *Context) True(ctx context.Context) (*Value, error)

func (*Context) Undefined

func (c *Context) Undefined(ctx context.Context) (*Value, error)

func (*Context) Values

func (c *Context) Values() int

type Error

type Error interface {
    Message() string
    // contains filtered or unexported methods

type ExecutionContext

type ExecutionContext struct {
    // contains filtered or unexported fields

func For

func For(ctx context.Context) *ExecutionContext

func (*ExecutionContext) AddExecutionEnterCallback

func (ec *ExecutionContext) AddExecutionEnterCallback(callback func())

func (*ExecutionContext) AddExecutionExitCallback

func (ec *ExecutionContext) AddExecutionExitCallback(callback func())

func (*ExecutionContext) AddReleaser

func (ec *ExecutionContext) AddReleaser(releaser ...Releaser)

func (*ExecutionContext) Assert

func (c *ExecutionContext) Assert(args ...any)

Assert implements Console.

func (*ExecutionContext) Background

func (c *ExecutionContext) Background(callback func(context.Context))

func (*ExecutionContext) Clear

func (c *ExecutionContext) Clear()

Clear implements Console.

func (*ExecutionContext) Context

func (ec *ExecutionContext) Context() *Context

func (*ExecutionContext) Count

func (c *ExecutionContext) Count(args ...any)

Count implements Console.

func (*ExecutionContext) CountReset

func (c *ExecutionContext) CountReset(args ...any)

CountReset implements Console.

func (*ExecutionContext) Data

func (c *ExecutionContext) Data(key any) (any, bool)

func (*ExecutionContext) Debug

func (c *ExecutionContext) Debug(args ...any)

Debug implements Console.

func (*ExecutionContext) Dir

func (c *ExecutionContext) Dir(args ...any)

Dir implements Console.

func (*ExecutionContext) DirXML

func (c *ExecutionContext) DirXML(args ...any)

DirXML implements Console.

func (*ExecutionContext) Error

func (c *ExecutionContext) Error(args ...any)

Error implements Console.

func (*ExecutionContext) Group

func (c *ExecutionContext) Group(args ...any)

Group implements Console.

func (*ExecutionContext) GroupCollapsed

func (c *ExecutionContext) GroupCollapsed(args ...any)

GroupCollapsed implements Console.

func (*ExecutionContext) GroupEnd

func (c *ExecutionContext) GroupEnd(args ...any)

GroupEnd implements Console.

func (*ExecutionContext) Info

func (c *ExecutionContext) Info(args ...any)

Info implements Console.

func (*ExecutionContext) Isolate

func (ec *ExecutionContext) Isolate() *Isolate

func (*ExecutionContext) Log

func (c *ExecutionContext) Log(args ...any)

Log implements Console.

func (*ExecutionContext) New

func (c *ExecutionContext) New(constructor any, args ...any) (any, error)

func (*ExecutionContext) Profile

func (c *ExecutionContext) Profile(args ...any)

Profile implements Console.

func (*ExecutionContext) ProfileEnd

func (c *ExecutionContext) ProfileEnd(args ...any)

ProfileEnd implements Console.

func (*ExecutionContext) SetContext

func (ec *ExecutionContext) SetContext(c *Context)

func (*ExecutionContext) SetData

func (c *ExecutionContext) SetData(key any, value any)

func (*ExecutionContext) Sync

func (c *ExecutionContext) Sync(callback func(context.Context) (any, error)) (any, error)

func (*ExecutionContext) Table

func (c *ExecutionContext) Table(args ...any)

Table implements Console.

func (*ExecutionContext) Time

func (c *ExecutionContext) Time(args ...any)

Time implements Console.

func (*ExecutionContext) TimeEnd

func (c *ExecutionContext) TimeEnd(args ...any)

TimeEnd implements Console.

func (*ExecutionContext) TimeLog

func (c *ExecutionContext) TimeLog(args ...any)

TimeLog implements Console.

func (*ExecutionContext) TimeStamp

func (c *ExecutionContext) TimeStamp(args ...any)

TimeStamp implements Console.

func (*ExecutionContext) Trace

func (c *ExecutionContext) Trace(args ...any)

Trace implements Console.

func (*ExecutionContext) Warn

func (c *ExecutionContext) Warn(args ...any)

Warn implements Console.

type Function

type Function func(FunctionArgs) (*Value, error)

type FunctionArgs

type FunctionArgs struct {
    ExecutionContext context.Context
    Context          *Context
    This             *Value
    IsConstructCall  bool
    Args             []*Value
    Caller           CallerInfo
    Holder           *Value

func (*FunctionArgs) Arg

func (c *FunctionArgs) Arg(ctx context.Context, n int) *Value

func (*FunctionArgs) Background

func (c *FunctionArgs) Background(callback func(in FunctionArgs))

func (*FunctionArgs) Sync

func (c *FunctionArgs) Sync(callback func(in FunctionArgs) (any, error)) (any, error)

func (*FunctionArgs) WithArgs

func (c *FunctionArgs) WithArgs(args ...any) (FunctionArgs, error)

type FunctionTemplate

type FunctionTemplate struct {
    // contains filtered or unexported fields

func (*FunctionTemplate) GetFunction

func (f *FunctionTemplate) GetFunction(ctx context.Context) (*Value, error)

func (*FunctionTemplate) GetInstanceTemplate

func (f *FunctionTemplate) GetInstanceTemplate(ctx context.Context) (*ObjectTemplate, error)

func (*FunctionTemplate) GetPrototypeTemplate

func (f *FunctionTemplate) GetPrototypeTemplate(ctx context.Context) (*ObjectTemplate, error)

func (*FunctionTemplate) Inherit

func (f *FunctionTemplate) Inherit(ctx context.Context, parent *FunctionTemplate) error

func (*FunctionTemplate) SetName

func (f *FunctionTemplate) SetName(ctx context.Context, name string) error

type Getter

type Getter func(GetterArgs) (*Value, error)

type GetterArgs

type GetterArgs struct {
    ExecutionContext context.Context
    Context          *Context
    Caller           CallerInfo
    This             *Value
    Holder           *Value
    Key              string

type HeapStatistics

type HeapStatistics struct {
    TotalHeapSize           uint64
    TotalHeapSizeExecutable uint64
    TotalPhysicalSize       uint64
    TotalAvailableSize      uint64
    UsedHeapSize            uint64
    HeapSizeLimit           uint64
    MallocedMemory          uint64
    PeakMallocedMemory      uint64
    DoesZapGarbage          bool

type Inspector

type Inspector struct {
    // contains filtered or unexported fields

func (*Inspector) AddContext

func (i *Inspector) AddContext(context *Context, name string)

func (*Inspector) DispatchMessage

func (i *Inspector) DispatchMessage(message string)

func (*Inspector) RemoveContext

func (i *Inspector) RemoveContext(context *Context)

type InspectorCallbacks

type InspectorCallbacks interface {
    V8InspectorSendResponse(callId int, message string)
    V8InspectorSendNotification(message string)

type Isolate

type Isolate struct {
    // contains filtered or unexported fields

func NewIsolate

func NewIsolate() *Isolate

func NewIsolateWithSnapshot

func NewIsolateWithSnapshot(snapshot *Snapshot) *Isolate

func (*Isolate) AddExecutionEnterCallback

func (i *Isolate) AddExecutionEnterCallback(callback func())

func (*Isolate) AddExecutionExitCallback

func (i *Isolate) AddExecutionExitCallback(callback func())

func (*Isolate) AddExecutionExitCallbackOnce

func (i *Isolate) AddExecutionExitCallbackOnce(callback func())

func (*Isolate) AddShutdownHook

func (i *Isolate) AddShutdownHook(ctx context.Context, shutdownHook interface{})

func (*Isolate) Background

func (i *Isolate) Background(ctx context.Context, callback func(ctx context.Context))

func (*Isolate) Contexts

func (i *Isolate) Contexts(ctx context.Context) ([]*Context, error)

func (*Isolate) EnqueueMicrotaskWithValue

func (i *Isolate) EnqueueMicrotaskWithValue(ctx context.Context, fn *Value) error

func (*Isolate) GetData

func (i *Isolate) GetData(key string) interface{}

func (*Isolate) GetExecutionContext

func (i *Isolate) GetExecutionContext() context.Context

func (*Isolate) GetHeapStatistics

func (i *Isolate) GetHeapStatistics(ctx context.Context) (HeapStatistics, error)

func (*Isolate) IsRunning

func (i *Isolate) IsRunning(ctx context.Context) (bool, error)

func (*Isolate) NewContext

func (i *Isolate) NewContext(ctx context.Context) (*Context, error)

func (*Isolate) NewInspector

func (i *Isolate) NewInspector(callbacks InspectorCallbacks) *Inspector

func (*Isolate) PerformMicrotaskCheckpointInBackground

func (i *Isolate) PerformMicrotaskCheckpointInBackground(ctx context.Context)

func (*Isolate) PerformMicrotaskCheckpointSync

func (i *Isolate) PerformMicrotaskCheckpointSync(ctx context.Context) error

func (*Isolate) RequestGarbageCollectionForTesting

func (i *Isolate) RequestGarbageCollectionForTesting(ctx context.Context)

func (*Isolate) SendLowMemoryNotification

func (i *Isolate) SendLowMemoryNotification(ctx context.Context)

func (*Isolate) SetData

func (i *Isolate) SetData(key string, value interface{})

func (*Isolate) Sync

func (i *Isolate) Sync(ctx context.Context, fn func(context.Context) (interface{}, error)) (result interface{}, err error)

func (*Isolate) Terminate

func (i *Isolate) Terminate()

func (*Isolate) Wait

func (i *Isolate) Wait(ctx context.Context) error

type Kind

Kind is an underlying V8 representation of a *Value. Javascript values may have multiple underyling kinds. For example, a function will be both KindObject and KindFunction.

type Kind uint8
const (
    KindUndefined Kind = iota

func (Kind) String

func (k Kind) String() string

type Marshaler

type Marshaler interface {
    MarshalV8(ctx context.Context) any

type Module

type Module struct {

    Context  *Context
    ID       string
    FS       any
    Filename string
    Dirname  string
    Main     bool

    Require *Value
    Error   error

func (*Module) ImportModuleDynamically

func (m *Module) ImportModuleDynamically(ctx context.Context, specifier string, resourceName string, importAssertions []*Value) (*Value, error)

func (*Module) V8Func\_compile

func (m *Module) V8Func_compile(in FunctionArgs) (*Value, error)

func (*Module) V8GetDirname

func (m *Module) V8GetDirname(in GetterArgs) (*Value, error)

func (*Module) V8GetFilename

func (m *Module) V8GetFilename(in GetterArgs) (*Value, error)

func (*Module) V8GetFs

func (m *Module) V8GetFs(in GetterArgs) (*Value, error)

func (*Module) V8GetId

func (m *Module) V8GetId(in GetterArgs) (*Value, error)

func (*Module) V8GetRequire

func (m *Module) V8GetRequire(in GetterArgs) (*Value, error)

type ObjectTemplate

type ObjectTemplate struct {
    // contains filtered or unexported fields

func (*ObjectTemplate) Copy

func (o *ObjectTemplate) Copy(ctx context.Context, other *ObjectTemplate) error

func (*ObjectTemplate) SetAccessor

func (o *ObjectTemplate) SetAccessor(ctx context.Context, name string, getter Getter, setter Setter) error

func (*ObjectTemplate) SetInternalFieldCount

func (o *ObjectTemplate) SetInternalFieldCount(ctx context.Context, count int) error

type PromiseState

PromiseState defines the state of a promise: either pending, resolved, or rejected. Promises that are pending have no result value yet. A promise that is resolved has a result value, and a promise that is rejected has a result value that is usually the error.

type PromiseState uint8
const (
    PromiseStatePending PromiseState = iota

func (PromiseState) String

func (s PromiseState) String() string

type PropertyDescriptor

type PropertyDescriptor struct {
    Get          *Value `v8:"get"`
    Set          *Value `v8:"set"`
    Value        *Value `v8:"value"`
    Enumerable   bool   `v8:"enumerable"`
    Configurable bool   `v8:"configurable"`
    Writable     bool   `v8:"writable"`

func (*PropertyDescriptor) MarshalV8

func (d *PropertyDescriptor) MarshalV8(ctx context.Context) any

func (*PropertyDescriptor) V8Construct

func (d *PropertyDescriptor) V8Construct(in FunctionArgs) error

type Releaser

type Releaser interface {

type ResolveResult

type ResolveResult struct {
    // contains filtered or unexported fields

func RegisterRuntime

func RegisterRuntime(name string, path string, factory func(in FunctionArgs) (*Value, error)) *ResolveResult

func RegisterRuntimeLibrary

func RegisterRuntimeLibrary(name string, fs any, path string) *ResolveResult

type Resolver

type Resolver struct {
    // contains filtered or unexported fields

func (*Resolver) Promise

func (r *Resolver) Promise(ctx context.Context) (*Value, error)

func (*Resolver) Reject

func (r *Resolver) Reject(ctx context.Context, value interface{}) error

func (*Resolver) RejectWithValue

func (r *Resolver) RejectWithValue(ctx context.Context, v *Value) error

func (*Resolver) Resolve

func (r *Resolver) Resolve(ctx context.Context, value interface{}) error

func (*Resolver) ResolveWithValue

func (r *Resolver) ResolveWithValue(ctx context.Context, v *Value) error

func (*Resolver) ToCallback

func (r *Resolver) ToCallback(ctx context.Context, callback *Value) error

type RuntimeFunctionArgs

type RuntimeFunctionArgs struct {
    Module  *Module
    Require *Value
    Exports *Value

type Setter

type Setter func(SetterArgs) error

type SetterArgs

type SetterArgs struct {
    ExecutionContext context.Context
    Context          *Context
    Caller           CallerInfo
    This             *Value
    Holder           *Value
    Key              string
    Value            *Value

type Snapshot

type Snapshot struct {
    // contains filtered or unexported fields

func (*Snapshot) Export

func (s *Snapshot) Export() []byte

type Value

type Value struct {
    // contains filtered or unexported fields

func (*Value) Await

func (v *Value) Await(ctx context.Context) (*Value, error)

func (*Value) Bind

func (v *Value) Bind(ctx context.Context, argv ...any) (*Value, error)

func (*Value) BindMethod

func (v *Value) BindMethod(ctx context.Context, method string, argv ...any) (*Value, error)

func (*Value) Bool

func (v *Value) Bool(ctx context.Context) (bool, error)

func (*Value) Bytes

func (v *Value) Bytes(ctx context.Context) ([]byte, error)

func (*Value) Call

func (v *Value) Call(ctx context.Context, self any, args ...any) (*Value, error)

func (*Value) CallMethod

func (v *Value) CallMethod(ctx context.Context, name string, argv ...any) (*Value, error)

func (*Value) CallValue

func (v *Value) CallValue(ctx context.Context, self *Value, argv ...*Value) (*Value, error)

func (*Value) Constructor

func (v *Value) Constructor(t reflect.Type) *FunctionTemplate

func (*Value) Date

func (v *Value) Date(ctx context.Context) (time.Time, error)

func (*Value) DefineProperty

func (v *Value) DefineProperty(ctx context.Context, key string, descriptor *PropertyDescriptor) error

func (*Value) Equals

func (v *Value) Equals(ctx context.Context, other *Value) (bool, error)

func (*Value) Error

func (v *Value) Error() string

func (*Value) Float64

func (v *Value) Float64(ctx context.Context) (float64, error)

func (*Value) Get

func (v *Value) Get(ctx context.Context, key string) (*Value, error)

func (*Value) GetByteLength

func (v *Value) GetByteLength(ctx context.Context) (int, error)

func (*Value) GetContext

func (v *Value) GetContext() *Context

func (*Value) GetIndex

func (v *Value) GetIndex(ctx context.Context, i int) (*Value, error)

func (*Value) GetInternalField

func (v *Value) GetInternalField(ctx context.Context, i int) (int64, error)

func (*Value) GetInternalFieldCount

func (v *Value) GetInternalFieldCount(ctx context.Context) (int, error)

func (*Value) GetLength

func (v *Value) GetLength(ctx context.Context) (int64, error)

func (*Value) GetOwnPropertyDescriptors

func (v *Value) GetOwnPropertyDescriptors(ctx context.Context) (map[string]PropertyDescriptor, error)

func (*Value) GetPrototype

func (v *Value) GetPrototype(ctx context.Context) (*Value, error)

func (*Value) InstanceOf

func (v *Value) InstanceOf(ctx context.Context, object *Value) bool

func (*Value) Int64

func (v *Value) Int64(ctx context.Context) (int64, error)

func (*Value) IsKind

func (v *Value) IsKind(k Kind) bool

func (*Value) IsNil

func (v *Value) IsNil() bool

func (*Value) Keys

func (v *Value) Keys(ctx context.Context) ([]string, error)

func (*Value) MarshalJSON

func (v *Value) MarshalJSON(ctx context.Context) ([]byte, error)

func (*Value) New

func (v *Value) New(ctx context.Context, args ...any) (*Value, error)

func (*Value) NewValue

func (v *Value) NewValue(ctx context.Context, argv ...*Value) (*Value, error)

func (*Value) PromiseInfo

func (v *Value) PromiseInfo(ctx context.Context) (PromiseState, *Value, error)

func (*Value) RebindAll

func (v *Value) RebindAll(ctx context.Context) error

func (*Value) RebindMethod

func (v *Value) RebindMethod(ctx context.Context, method string, args ...any) error

func (*Value) Receiver

func (v *Value) Receiver(ctx context.Context) reflect.Value

func (*Value) Ref

func (v *Value) Ref() refutils.ID

func (*Value) Set

func (v *Value) Set(ctx context.Context, key string, value any) error

func (*Value) SetBytes

func (v *Value) SetBytes(ctx context.Context, bytes []byte) error

func (*Value) SetIndex

func (v *Value) SetIndex(ctx context.Context, i int, value *Value) error

func (*Value) SetInternalField

func (v *Value) SetInternalField(ctx context.Context, i int, value uint32) error

func (*Value) SetReceiver

func (v *Value) SetReceiver(ctx context.Context, value reflect.Value)

func (*Value) SetValue

func (v *Value) SetValue(ctx context.Context, key string, value *Value) error

func (*Value) StrictEquals

func (v *Value) StrictEquals(ctx context.Context, other *Value) (bool, error)

func (*Value) String

func (v *Value) String() string

func (*Value) StringValue

func (v *Value) StringValue(ctx context.Context) (string, error)

func (*Value) ToError

func (v *Value) ToError(ctx context.Context) error

func (*Value) Unmarshal

func (v *Value) Unmarshal(ctx context.Context, t reflect.Type) (*reflect.Value, error)

func (*Value) Unref

func (v *Value) Unref()

Up next