v8

package module
v0.0.0-...-e7084fc Latest Latest
Warning

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

Go to latest
Published: Jan 14, 2019 License: MIT Imports: 17 Imported by: 0

README

V8 Bindings for Go

Usage

To use import v8:

import (
  v8 "github.com/behrsin/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(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

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

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

func (*Context) False

func (c *Context) False() (*Value, error)

func (*Context) GetIsolate

func (c *Context) GetIsolate() *Isolate

func (*Context) Global

func (c *Context) Global() (*Value, error)

func (*Context) NewFunctionTemplate

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

func (*Context) NewResolver

func (c *Context) NewResolver() (*Resolver, error)

func (*Context) Null

func (c *Context) Null() (*Value, error)

func (*Context) ParseJSON

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

func (*Context) Run

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

func (*Context) True

func (c *Context) True() (*Value, error)

func (*Context) Undefined

func (c *Context) Undefined() (*Value, error)

type Function

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

type FunctionArgs

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

func (*FunctionArgs) Arg

func (c *FunctionArgs) Arg(n int) *Value

type FunctionTemplate

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

func (*FunctionTemplate) GetFunction

func (f *FunctionTemplate) GetFunction() (*Value, error)

func (*FunctionTemplate) GetInstanceTemplate

func (f *FunctionTemplate) GetInstanceTemplate() (*ObjectTemplate, error)

func (*FunctionTemplate) GetPrototypeTemplate

func (f *FunctionTemplate) GetPrototypeTemplate() (*ObjectTemplate, error)

func (*FunctionTemplate) Inherit

func (f *FunctionTemplate) Inherit(parent *FunctionTemplate) error

func (*FunctionTemplate) SetHiddenPrototype

func (f *FunctionTemplate) SetHiddenPrototype(value bool) error

func (*FunctionTemplate) SetName

func (f *FunctionTemplate) SetName(name string) error

type Getter

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

type GetterArgs

type GetterArgs struct {
	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) GetData

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

func (*Isolate) GetHeapStatistics

func (i *Isolate) GetHeapStatistics() (HeapStatistics, error)

func (*Isolate) IsRunning

func (i *Isolate) IsRunning() bool

func (*Isolate) NewContext

func (i *Isolate) NewContext() (*Context, error)

func (*Isolate) NewInspector

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

func (*Isolate) RequestGarbageCollectionForTesting

func (i *Isolate) RequestGarbageCollectionForTesting()

func (*Isolate) SendLowMemoryNotification

func (i *Isolate) SendLowMemoryNotification()

func (*Isolate) SetData

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

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(name string, getter Getter, setter Setter) error

func (*ObjectTemplate) SetInternalFieldCount

func (o *ObjectTemplate) SetInternalFieldCount(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() (*Value, error)

func (*Resolver) Reject

func (r *Resolver) Reject(value interface{}) error

func (*Resolver) RejectWithValue

func (r *Resolver) RejectWithValue(v *Value) error

func (*Resolver) Resolve

func (r *Resolver) Resolve(value interface{}) error

func (*Resolver) ResolveWithValue

func (r *Resolver) ResolveWithValue(v *Value) error

type Setter

type Setter func(SetterArgs) error

type SetterArgs

type SetterArgs struct {
	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(argv ...*Value) (*Value, error)

func (*Value) Bool

func (v *Value) Bool() (bool, error)

func (*Value) Bytes

func (v *Value) Bytes() ([]byte, error)

func (*Value) Call

func (v *Value) Call(self *Value, argv ...*Value) (*Value, error)

func (*Value) CallMethod

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

func (*Value) Date

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

func (*Value) DefineProperty

func (v *Value) DefineProperty(key string, descriptor *PropertyDescriptor) error

func (*Value) Float64

func (v *Value) Float64() (float64, error)

func (*Value) Get

func (v *Value) Get(key string) (*Value, error)

func (*Value) GetContext

func (v *Value) GetContext() *Context

func (*Value) GetIndex

func (v *Value) GetIndex(i int) (*Value, error)

func (*Value) GetInternalField

func (v *Value) GetInternalField(i int) (int64, error)

func (*Value) GetInternalFieldCount

func (v *Value) GetInternalFieldCount() (int, error)

func (*Value) Int64

func (v *Value) Int64() (int64, error)

func (*Value) IsKind

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

func (*Value) MarshalJSON

func (v *Value) MarshalJSON() ([]byte, error)

func (*Value) New

func (v *Value) New(argv ...*Value) (*Value, error)

func (*Value) PromiseInfo

func (v *Value) PromiseInfo() (PromiseState, *Value, error)

func (*Value) Receiver

func (v *Value) Receiver(t reflect.Type) *reflect.Value

func (*Value) Set

func (v *Value) Set(key string, value *Value) error

func (*Value) SetIndex

func (v *Value) SetIndex(i int, value *Value) error

func (*Value) SetInternalField

func (v *Value) SetInternalField(i int, value uint32) error

func (*Value) SetReceiver

func (v *Value) SetReceiver(value *reflect.Value)

func (*Value) String

func (v *Value) String() string

func (*Value) Unmarshal

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

Jump to

Keyboard shortcuts

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