isolates

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 3, 2023 License: MIT Imports: 18 Imported by: 26

README

V8 Bindings for Go

Usage

To use import v8:

import (
  v8 "github.com/grexie/go-v8"
)

See the v8_isolate_test.go file for a basic example of how to setup a v8.Isolate, v8.Context, create values and run JavaScript code. For more detailed API information see GoDoc.

Substantially based on the great work by Augusto Roman (@augustoroman):

github.com/augustoroman/v8

I've added native promises, JSON stringify / parse, value caching, weak callbacks, function templates and constructors, v8 Inspector and a terminal-based allocation tracer api for debugging.

Thanks be to God for the help He has given me in writing this.

Bugs

Please open an issue to report a bug. Before opening a new issue please see if there are already issues matching your case.

Installation

For now, please follow his instructions for installation of the v8 libraries and headers:

https://github.com/augustoroman/v8

There's a script included install-v8.sh that can be used to install the version of libraries this library is developed against for both ARMv6 and AMD64 (linux and macOS):

./path/to/behrsin/go-v8/install-v8.sh

This will download the binaries and install them into the go-v8 folder under libv8/ and include/.

Debug

There is a heap tracing tool which monitors active V8 objects referenced by Go. To use it look at StartTracer and EnableAllocationStackTraces. You can mount TracerHandler() using a net/http.(*ServeMux) to enable the stack traces over HTTP or you can use v8.DumpTracer to writer the trace output to a log file or os.Stdout periodically.

Documentation

Index

Constants

This section is empty.

Variables

View Source
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),
}

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.).

Functions

func DisableAllocationStackTraces

func DisableAllocationStackTraces()

func DumpTracer

func DumpTracer(ctx context.Context, w io.Writer, allocations bool)

func EnableAllocationStackTraces

func EnableAllocationStackTraces()

func Initialize

func Initialize()

func StartTracer

func StartTracer(t TracerType)

func StopTracer

func StopTracer(t TracerType)

func TracerHandler

func TracerHandler() http.Handler

func WithContext

func WithContext(ctx context.Context) context.Context

Types

type CallerInfo

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

type Context

type Context struct {
	refutils.RefHolder
	// contains filtered or unexported fields
}

func (*Context) AddMicrotask

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

func (*Context) Create

func (c *Context) Create(ctx context.Context, v interface{}) (*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) NewFunctionTemplate

func (c *Context) NewFunctionTemplate(ctx context.Context, cb Function) (*FunctionTemplate, 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) ParseJSON

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

func (*Context) Run

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

func (*Context) True

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

func (*Context) Undefined

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

type ExecutionContext

type ExecutionContext struct {
	refutils.RefHolder
	// contains filtered or unexported fields
}

func FromContext

func FromContext(ctx context.Context) *ExecutionContext

func (*ExecutionContext) AddExecutionEnterCallback

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

func (*ExecutionContext) AddExecutionExitCallback

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

func (*ExecutionContext) GetIsolate

func (ec *ExecutionContext) GetIsolate() *Isolate

type Function

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

type FunctionArgs

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

func (*FunctionArgs) Arg

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

type FunctionTemplate

type FunctionTemplate struct {
	refutils.RefHolder
	// 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) Release

func (i *Inspector) Release()

func (*Inspector) RemoveContext

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

type InspectorCallbacks

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

type Isolate

type Isolate struct {
	refutils.RefHolder
	// contains filtered or unexported fields
}

func NewIsolate

func NewIsolate() *Isolate

func NewIsolateWithSnapshot

func NewIsolateWithSnapshot(snapshot *Snapshot) *Isolate

func (*Isolate) AddShutdownHook

func (i *Isolate) AddShutdownHook(shutdownHook interface{})

func (*Isolate) EnqueueMicrotaskWithValue

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

func (*Isolate) Enter

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

func (*Isolate) Exit

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

func (*Isolate) GetData

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

func (*Isolate) GetExecutionContext

func (i *Isolate) GetExecutionContext() *ExecutionContext

func (*Isolate) GetHeapStatistics

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

func (*Isolate) IsActive

func (i *Isolate) IsActive() bool

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) RequestGarbageCollectionForTesting

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

func (*Isolate) RunMicrotasksInBackground

func (i *Isolate) RunMicrotasksInBackground()

func (*Isolate) RunMicrotasksSync

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

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) (*Value, error)) (*Value, error)

func (*Isolate) Terminate

func (i *Isolate) Terminate()

type Kind

type Kind uint8

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.

const (
	KindUndefined Kind = iota
	KindNull
	KindName
	KindString
	KindSymbol
	KindFunction
	KindArray
	KindObject
	KindBoolean
	KindNumber
	KindExternal
	KindInt32
	KindUint32
	KindDate
	KindArgumentsObject
	KindBooleanObject
	KindNumberObject
	KindStringObject
	KindSymbolObject
	KindNativeError
	KindRegExp
	KindAsyncFunction
	KindGeneratorFunction
	KindGeneratorObject
	KindPromise
	KindMap
	KindSet
	KindMapIterator
	KindSetIterator
	KindWeakMap
	KindWeakSet
	KindArrayBuffer
	KindArrayBufferView
	KindTypedArray
	KindUint8Array
	KindUint8ClampedArray
	KindInt8Array
	KindUint16Array
	KindInt16Array
	KindUint32Array
	KindInt32Array
	KindFloat32Array
	KindFloat64Array
	KindDataView
	KindSharedArrayBuffer
	KindProxy
	KindWebAssemblyCompiledModule
)

func (Kind) String

func (k Kind) String() string

type Marshaler

type Marshaler interface {
	MarshalV8() interface{}
}

type ObjectTemplate

type ObjectTemplate struct {
	refutils.RefHolder
	// contains filtered or unexported fields
}

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

type PromiseState uint8

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.

const (
	PromiseStatePending PromiseState = iota
	PromiseStateResolved
	PromiseStateRejected
)

func (PromiseState) String

func (s PromiseState) String() string

type PropertyDescriptor

type PropertyDescriptor struct {
	Get          *Value
	Set          *Value
	Enumerable   bool
	Configurable bool
}

type Resolver

type Resolver struct {
	refutils.RefHolder
	// 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

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 CreateSnapshot

func CreateSnapshot(code string) *Snapshot

func ImportSnapshot

func ImportSnapshot(data []byte) *Snapshot

func (*Snapshot) Export

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

type TracerType

type TracerType uint8
const (
	SimpleTracer TracerType = iota
)

type Value

type Value struct {
	refutils.RefHolder
	// contains filtered or unexported fields
}

func (*Value) Bind

func (v *Value) Bind(ctx context.Context, argv ...*Value) (*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 *Value, argv ...*Value) (*Value, error)

func (*Value) CallMethod

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

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) 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) Int64

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

func (*Value) IsKind

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

func (*Value) MarshalJSON

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

func (*Value) New

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

func (*Value) PromiseInfo

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

func (*Value) Receiver

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

func (*Value) Ref

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

func (*Value) Set

func (v *Value) Set(ctx context.Context, key string, value *Value) 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) error

func (*Value) String

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

func (*Value) Unmarshal

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

func (*Value) Unref

func (v *Value) Unref()

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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