Documentation
¶
Overview ¶
Package v8go provides an API to execute JavaScript.
Index ¶
- Constants
- Variables
- func BuildCallCount() uint64
- func JSONStringify(ctx *Context, val Valuer) (string, error)
- func ResetBuildCallCount()
- func SetFlags(flags ...string)
- func Version() string
- type BatchScope
- func (s *BatchScope) Array(elems []LocalRef) LocalRef
- func (s *BatchScope) Bool(v bool) LocalRef
- func (s *BatchScope) Bytes(v []byte) LocalRef
- func (s *BatchScope) Close()
- func (s *BatchScope) Float64(v float64) LocalRef
- func (s *BatchScope) Int32(v int32) LocalRef
- func (s *BatchScope) Null() LocalRef
- func (s *BatchScope) Object(shape ShapeRef, vals []LocalRef) LocalRef
- func (s *BatchScope) Result(root LocalRef) (*Value, error)
- func (s *BatchScope) Shape(keys []string) ShapeRef
- func (s *BatchScope) Size() uint32
- func (s *BatchScope) String(v string) LocalRef
- func (s *BatchScope) Undefined() LocalRef
- type CPUProfile
- type CPUProfileNode
- func (c *CPUProfileNode) GetBailoutReason() string
- func (c *CPUProfileNode) GetChild(index int) *CPUProfileNode
- func (c *CPUProfileNode) GetChildrenCount() int
- func (c *CPUProfileNode) GetColumnNumber() int
- func (c *CPUProfileNode) GetFunctionName() string
- func (c *CPUProfileNode) GetHitCount() int
- func (c *CPUProfileNode) GetLineNumber() int
- func (c *CPUProfileNode) GetNodeId() int
- func (c *CPUProfileNode) GetParent() *CPUProfileNode
- func (c *CPUProfileNode) GetScriptId() int
- func (c *CPUProfileNode) GetScriptResourceName() string
- type CPUProfiler
- type CompileMode
- type CompileOptions
- type CompilerCachedData
- type ConsoleAPIMessage
- type ConsoleAPIMessageHandler
- type Context
- type ContextOption
- type Exception
- func NewError(iso *Isolate, msg string) *Exception
- func NewRangeError(iso *Isolate, msg string) *Exception
- func NewReferenceError(iso *Isolate, msg string) *Exception
- func NewSyntaxError(iso *Isolate, msg string) *Exception
- func NewTypeError(iso *Isolate, msg string) *Exception
- func NewWasmCompileError(iso *Isolate, msg string) *Exception
- func NewWasmLinkError(iso *Isolate, msg string) *Exception
- func NewWasmRuntimeError(iso *Isolate, msg string) *Exception
- type Function
- type FunctionCallback
- type FunctionCallbackInfo
- type FunctionCallbackWithError
- type FunctionTemplate
- func (tmpl *FunctionTemplate) GetFunction(ctx *Context) *Function
- func (tmpl *FunctionTemplate) Inherit(base *FunctionTemplate)
- func (tmpl *FunctionTemplate) InstanceTemplate() *ObjectTemplate
- func (tmpl *FunctionTemplate) PrototypeTemplate() *ObjectTemplate
- func (t FunctionTemplate) Set(name string, val interface{}, attributes ...PropertyAttribute) error
- func (t FunctionTemplate) SetSymbol(key *Symbol, val interface{}, attributes ...PropertyAttribute) error
- type HeapStatistics
- type Injector
- type Inspector
- type InspectorClient
- type Isolate
- func (i *Isolate) Close()deprecated
- func (i *Isolate) CompileUnboundScript(source, origin string, opts CompileOptions) (*UnboundScript, error)
- func (i *Isolate) Dispose()
- func (i *Isolate) GetHeapStatistics() HeapStatistics
- func (i *Isolate) IsExecutionTerminating() bool
- func (i *Isolate) TerminateExecution()
- func (i *Isolate) ThrowException(value *Value) *Value
- type IsolateOption
- type JSError
- type LocalRef
- type MessageErrorLevel
- type Object
- func (o *Object) Delete(key string) bool
- func (o *Object) DeleteIdx(idx uint32) bool
- func (o *Object) DeleteSymbol(key *Symbol) bool
- func (o *Object) Get(key string) (*Value, error)
- func (o *Object) GetIdx(idx uint32) (*Value, error)
- func (o *Object) GetInternalField(idx uint32) *Value
- func (o *Object) GetSymbol(key *Symbol) (*Value, error)
- func (o *Object) Has(key string) bool
- func (o *Object) HasIdx(idx uint32) bool
- func (o *Object) HasSymbol(key *Symbol) bool
- func (o *Object) InternalFieldCount() uint32
- func (o *Object) MethodCall(methodName string, args ...Valuer) (*Value, error)
- func (o *Object) Set(key string, val interface{}) error
- func (o *Object) SetIdx(idx uint32, val interface{}) error
- func (o *Object) SetInternalField(idx uint32, val interface{}) error
- func (o *Object) SetSymbol(key *Symbol, val interface{}) error
- type ObjectTemplate
- func (o *ObjectTemplate) InternalFieldCount() uint32
- func (o *ObjectTemplate) MarkAsUndetectable()
- func (o *ObjectTemplate) NewInstance(ctx *Context) (*Object, error)
- func (t ObjectTemplate) Set(name string, val interface{}, attributes ...PropertyAttribute) error
- func (o *ObjectTemplate) SetAccessorProperty(key string, get *FunctionTemplate, set *FunctionTemplate, ...)
- func (o *ObjectTemplate) SetCallAsFunctionHandler(callback FunctionCallbackWithError)
- func (o *ObjectTemplate) SetInternalFieldCount(fieldCount uint32)
- func (t ObjectTemplate) SetSymbol(key *Symbol, val interface{}, attributes ...PropertyAttribute) error
- type Payload
- type Promise
- func (p *Promise) Catch(cb FunctionCallback) *Promise
- func (p *Promise) CatchWithError(cb FunctionCallbackWithError) *Promise
- func (p *Promise) Result() *Value
- func (p *Promise) State() PromiseState
- func (p *Promise) Then(cbs ...FunctionCallback) *Promise
- func (p *Promise) ThenWithError(cbs ...FunctionCallbackWithError) *Promise
- type PromiseResolver
- type PromiseState
- type PropertyAttribute
- type ShapeDef
- type ShapeRef
- type Span
- type Symbol
- func SymbolAsyncIterator(iso *Isolate) *Symbol
- func SymbolHasInstance(iso *Isolate) *Symbol
- func SymbolIsConcatSpreadable(iso *Isolate) *Symbol
- func SymbolIterator(iso *Isolate) *Symbol
- func SymbolMatch(iso *Isolate) *Symbol
- func SymbolReplace(iso *Isolate) *Symbol
- func SymbolSearch(iso *Isolate) *Symbol
- func SymbolSplit(iso *Isolate) *Symbol
- func SymbolToPrimitive(iso *Isolate) *Symbol
- func SymbolToStringTag(iso *Isolate) *Symbol
- func SymbolUnscopables(iso *Isolate) *Symbol
- type UnboundScript
- type Value
- func (v *Value) ArrayBufferViewBytes() []byte
- func (v *Value) ArrayIndex() (idx uint32, ok bool)
- func (v *Value) AsException() (*Exception, error)
- func (v *Value) AsFunction() (*Function, error)
- func (v *Value) AsObject() (*Object, error)
- func (v *Value) AsPromise() (*Promise, error)
- func (v *Value) AsSymbol() (*Symbol, error)
- func (v *Value) BigInt() *big.Int
- func (v *Value) Boolean() bool
- func (v *Value) DetailString() string
- func (v *Value) Format(s fmt.State, verb rune)
- func (v *Value) Int32() int32
- func (v *Value) Integer() int64
- func (v *Value) IsArgumentsObject() bool
- func (v *Value) IsArray() bool
- func (v *Value) IsArrayBuffer() bool
- func (v *Value) IsArrayBufferView() bool
- func (v *Value) IsAsyncFunction() bool
- func (v *Value) IsBigInt() bool
- func (v *Value) IsBigInt64Array() bool
- func (v *Value) IsBigIntObject() bool
- func (v *Value) IsBigUint64Array() bool
- func (v *Value) IsBoolean() bool
- func (v *Value) IsDataView() bool
- func (v *Value) IsDate() bool
- func (v *Value) IsExternal() bool
- func (v *Value) IsFalse() bool
- func (v *Value) IsFloat32Array() bool
- func (v *Value) IsFloat64Array() bool
- func (v *Value) IsFunction() bool
- func (v *Value) IsGeneratorFunction() bool
- func (v *Value) IsGeneratorObject() bool
- func (v *Value) IsInt8Array() bool
- func (v *Value) IsInt16Array() bool
- func (v *Value) IsInt32() bool
- func (v *Value) IsInt32Array() bool
- func (v *Value) IsMap() bool
- func (v *Value) IsMapIterator() bool
- func (v *Value) IsModuleNamespaceObject() bool
- func (v *Value) IsName() bool
- func (v *Value) IsNativeError() bool
- func (v *Value) IsNull() bool
- func (v *Value) IsNullOrUndefined() bool
- func (v *Value) IsNumber() bool
- func (v *Value) IsNumberObject() bool
- func (v *Value) IsObject() bool
- func (v *Value) IsPromise() bool
- func (v *Value) IsProxy() bool
- func (v *Value) IsRegExp() bool
- func (v *Value) IsSet() bool
- func (v *Value) IsSetIterator() bool
- func (v *Value) IsSharedArrayBuffer() bool
- func (v *Value) IsString() bool
- func (v *Value) IsStringObject() bool
- func (v *Value) IsSymbol() bool
- func (v *Value) IsSymbolObject() bool
- func (v *Value) IsTrue() bool
- func (v *Value) IsTypedArray() bool
- func (v *Value) IsUint8Array() bool
- func (v *Value) IsUint8ClampedArray() bool
- func (v *Value) IsUint16Array() bool
- func (v *Value) IsUint32() bool
- func (v *Value) IsUint32Array() bool
- func (v *Value) IsUndefined() bool
- func (v *Value) IsWasmModuleObject() bool
- func (v *Value) IsWeakMap() bool
- func (v *Value) IsWeakSet() bool
- func (v *Value) MarshalJSON() ([]byte, error)
- func (v *Value) Number() float64
- func (v *Value) Object() *Object
- func (v *Value) Release()
- func (v *Value) SameValue(other *Value) bool
- func (v *Value) SharedArrayBufferGetContents() ([]byte, func(), error)
- func (v *Value) StrictEquals(other *Value) bool
- func (v *Value) String() string
- func (v *Value) TypeOf() string
- func (v *Value) Uint32() uint32
- type ValueError
- type Valuer
Examples ¶
Constants ¶
const ( SpanStaged uint32 = C.GAV8_SPAN_STAGED SpanPinned uint32 = C.GAV8_SPAN_PINNED )
Span kinds. A leaf's bytes are either staged into Payload.Buf — Off is a byte offset into it — or left where they are and pinned, in which case Off indexes Payload.Ptrs. A producer picks per value against a size threshold, so a single payload normally carries both.
const ( // OpEnd terminates the program. Exactly one value must remain on the // stack, and it is the root. OpEnd uint32 = C.GAV8_OP_END // OpNull pushes null. OpNull uint32 = C.GAV8_OP_NULL // OpUndef pushes undefined. OpUndef uint32 = C.GAV8_OP_UNDEF // OpTrue pushes true. OpTrue uint32 = C.GAV8_OP_TRUE // OpFalse pushes false. OpFalse uint32 = C.GAV8_OP_FALSE // OpBool pushes the next Nums entry as a boolean (0 is false). OpBool uint32 = C.GAV8_OP_BOOL // OpInt pushes the next Nums entry as a number. OpInt uint32 = C.GAV8_OP_INT // OpF64 pushes the next Floats entry. OpF64 uint32 = C.GAV8_OP_F64 // OpStr pushes a string from the next value Span. OpStr uint32 = C.GAV8_OP_STR // OpBytes pushes a Uint8Array from the next value Span. OpBytes uint32 = C.GAV8_OP_BYTES // OpObj is followed by a shape id. It pops that shape's key count off the // stack, in shape order, and pushes the object. Every key in the shape // gets a property, whatever its value; only [OpObjOmit] reads anything // into an undefined. OpObj uint32 = C.GAV8_OP_OBJ // OpMark remembers the current stack depth. OpMark uint32 = C.GAV8_OP_MARK // OpArrFromMark pops everything pushed since the matching OpMark into an // array. OpArrFromMark uint32 = C.GAV8_OP_ARR_FROM_MARK // OpRepeat is followed by a body length in words. It takes n from the next // Counts entry and runs that many words n times before continuing after // them. Bodies may nest. n == 0 skips the body and leaves every other // cursor untouched, so an empty slice costs nothing. The body may not be // empty. OpRepeat uint32 = C.GAV8_OP_REPEAT // OpNullable is followed by a body length in words. It takes a flag from // the next Counts entry — only zero is null, any other value is present — // and either pushes null and skips the body, or runs the body, which must // push exactly one value. // // This is how a *T is encoded, and nothing else expresses it: OpRepeat with // a count of 0 or 1 gives "the value or nothing", but OpObj pops a fixed // arity from its shape, so a skipped push does not yield null — it takes // the previous field's value and shifts every field after it. // // On the null path only the flag is consumed. Nums, Floats, Spans and the // rest of Counts stay where they were, because a producer stages no payload // for a value it is not sending; consuming one would desynchronise every // leaf that follows. The body must also be self-contained: it may nest // anything, including OpRepeat, OpObj and further OpNullable, but it may // not pop values pushed before it or close an OpMark from outside it. Both // are errors, since either would make the tree depend on the flag in a way // the generator did not write. OpNullable uint32 = C.GAV8_OP_NULLABLE // OpOptional is [OpNullable] with a different absent value: the flag comes // from the next Counts entry, zero pushes undefined instead of null, and // everything else — the skipped body, the untouched cursors, the // exactly-one-value contract, the self-containment rules — is identical. // // It is how an ABSENT object key is encoded, and null cannot stand in for // it: {"note":null} and {} are different values, and a producer whose // field is conditionally emitted is asking for exactly that distinction. // Pair it with [OpObjOmit], which reads the sentinel. OpOptional uint32 = C.GAV8_OP_OPTIONAL // OpObjOmit is followed by a shape id. It pops the same fixed arity as // [OpObj] and builds an object from only the pairs whose value is not // undefined, in shape order; all of them undefined yields {}. // // Undefined is the omit sentinel because it cannot occur as a legitimate // value: null, booleans, numbers, strings, Uint8Array, arrays and objects // are the whole of what a leaf can be, so nothing but [OpUndef] and an // absent [OpOptional] produces one. A presence bitmask would instead be a // second source of truth, and a producer whose bit and whose staged value // disagreed would build a valid object with its fields shifted. OpObjOmit uint32 = C.GAV8_OP_OBJ_OMIT )
Opcodes for a build program. See BuildValue for what a program is and why it is shaped this way.
These values are the ABI: a producer in another package (or another language) emits them as plain uint32 words, so they may be added to but never renumbered.
const ( // InvalidLocalRef is returned by every LocalRef builder that fails. InvalidLocalRef LocalRef = C.GAV8_INVALID // InvalidShapeRef is returned by [BatchScope.Shape] when it fails. InvalidShapeRef ShapeRef = C.GAV8_INVALID )
Variables ¶
var ( CompileModeDefault = CompileMode(C.ScriptCompilerNoCompileOptions) CompileModeEager = CompileMode(C.ScriptCompilerEagerCompile) )
Functions ¶
func BuildCallCount ¶
func BuildCallCount() uint64
BuildCallCount reports how many times the C entry point behind BuildValue has been entered, successfully or not, since the process started or since ResetBuildCallCount.
It is a diagnostic, and the one that matters: building a tree in a single crossing is the whole reason this API exists, so a test can measure it rather than assume it. The counter is process-wide, so a test that asserts an exact value must not run in parallel with another that builds.
func JSONStringify ¶
JSONStringify tries to stringify the JSON-serializable object value and returns it as string.
Example ¶
package main
import (
"fmt"
v8 "github.com/hlvs-apps/v8go"
)
func main() {
ctx := v8.NewContext()
defer ctx.Isolate().Dispose()
defer ctx.Close()
val, _ := v8.JSONParse(ctx, `{
"a": 1,
"b": "foo"
}`)
jsonStr, _ := v8.JSONStringify(ctx, val)
fmt.Println(jsonStr)
}
Output: {"a":1,"b":"foo"}
func ResetBuildCallCount ¶
func ResetBuildCallCount()
ResetBuildCallCount zeroes the counter BuildCallCount reports.
func SetFlags ¶
func SetFlags(flags ...string)
SetFlags sets flags for V8. For possible flags: https://github.com/v8/v8/blob/master/src/flags/flag-definitions.h Flags are expected to be prefixed with `--`, for example: `--harmony`. Flags can be reverted using the `--no` prefix equivalent, for example: `--use_strict` vs `--nouse_strict`. Flags will affect all Isolates created, even after creation.
Types ¶
type BatchScope ¶
type BatchScope struct {
// contains filtered or unexported fields
}
BatchScope builds a whole JavaScript value tree in a single cgo crossing per node, without a tracked value per node.
The Object/Value API creates a v8::Global for every value it makes, and registers it with the Context so it can be released later: a malloc, a GC-root registration and a hash-map insert, before any V8 work happens. That is affordable for a handful of values and roughly an order of magnitude slower than JSON.parse for a tree of a few thousand. A BatchScope instead holds one v8::HandleScope open for its lifetime and hands back uint32 indices into a handle table, so a node costs a v8::Local and nothing else. Exactly one tracked value is ever created: the root that BatchScope.Result returns.
A scope holds the isolate's lock the whole time it is open, so nothing else can enter that isolate meanwhile — build and close promptly. It also pins its goroutine to the OS thread it started on, because the lock belongs to the thread that took it; BatchScope.Close unpins.
A BatchScope is not safe for concurrent use, and must be closed on the goroutine that opened it.
func NewBatchScope ¶
func NewBatchScope(ctx *Context) *BatchScope
NewBatchScope opens a value-building scope on ctx. The caller must Close it, normally with defer:
s := v8.NewBatchScope(ctx) defer s.Close()
It panics if ctx is nil or already closed.
func (*BatchScope) Array ¶
func (s *BatchScope) Array(elems []LocalRef) LocalRef
Array builds a JS array from elems.
func (*BatchScope) Bool ¶
func (s *BatchScope) Bool(v bool) LocalRef
Bool adds a JS boolean to the scope.
func (*BatchScope) Bytes ¶
func (s *BatchScope) Bytes(v []byte) LocalRef
Bytes adds a JS Uint8Array holding a copy of v to the scope. This is what lets binary payloads — crypto output, compressed blocks, file and response bodies, SQL blob cells — reach JS without a base64 round trip.
The slice is copied into V8 during the call; V8 never retains a pointer into Go memory.
func (*BatchScope) Close ¶
func (s *BatchScope) Close()
Close destroys the scope's HandleScope, which frees every value it built at once, and unpins the goroutine. All LocalRefs and ShapeRefs from this scope become invalid; values already handed out by Result are unaffected.
Close is idempotent and must run on the goroutine that opened the scope.
func (*BatchScope) Float64 ¶
func (s *BatchScope) Float64(v float64) LocalRef
Float64 adds a JS number to the scope.
func (*BatchScope) Int32 ¶
func (s *BatchScope) Int32(v int32) LocalRef
Int32 adds a JS number to the scope, as a V8 integer.
func (*BatchScope) Null ¶
func (s *BatchScope) Null() LocalRef
Null adds the JS null value to the scope.
func (*BatchScope) Object ¶
func (s *BatchScope) Object(shape ShapeRef, vals []LocalRef) LocalRef
Object builds a JS object with the shape's keys and the given values, in shape order. len(vals) must equal the number of keys in the shape; Object returns InvalidLocalRef otherwise.
The object gets the context's Object.prototype, so it is structurally indistinguishable from the same data parsed out of JSON.
It is not, however, indistinguishable in performance. V8's bulk object constructor produces dictionary-mode ("slow properties") objects, where JSON.parse produces hidden-class ones, and JS reads from these roughly 11x slower — a difference that only starts to matter after about 70 full passes over the data, which is why it is still the right trade for a result set rendered once. See the comment on gav8_obj in gav8_values.cc for the measurement and for the alternative.
func (*BatchScope) Result ¶
func (s *BatchScope) Result(root LocalRef) (*Value, error)
Result wraps root as a *Value owned by the scope's Context, the way any other v8go value is owned, and returns it. The value outlives the scope; every other node in the tree is freed by Close.
Result does not close the scope. It returns an error if root is invalid, or if any builder call on this scope failed — a tree with a hole in it never reaches the caller as a value.
func (*BatchScope) Shape ¶
func (s *BatchScope) Shape(keys []string) ShapeRef
Shape interns keys once and returns a handle to be reused for every object that has those keys in that order. Interning once per shape instead of once per object is what lets V8 build the hidden class once and share it across them, which is the whole reason this is faster than setting properties one by one.
Keys must be unique; Shape returns InvalidShapeRef if they are not.
func (*BatchScope) Size ¶
func (s *BatchScope) Size() uint32
Size reports how many values the scope has created. A test that knows how many nodes its tree has can assert on this: a number larger than the node count means something is allocating per node behind the caller's back.
func (*BatchScope) String ¶
func (s *BatchScope) String(v string) LocalRef
String adds a JS string to the scope. The bytes are UTF-8 and length-delimited, so an interior NUL is a character like any other rather than a terminator.
The string's bytes are read, and copied into V8, during the call; V8 never retains a pointer into Go memory.
func (*BatchScope) Undefined ¶
func (s *BatchScope) Undefined() LocalRef
Undefined adds the JS undefined value to the scope.
type CPUProfile ¶
type CPUProfile struct {
// contains filtered or unexported fields
}
func (*CPUProfile) Delete ¶
func (c *CPUProfile) Delete()
Deletes the profile and removes it from CpuProfiler's list. All pointers to nodes previously returned become invalid.
func (*CPUProfile) GetDuration ¶
func (c *CPUProfile) GetDuration() time.Duration
Returns the duration of the profile.
func (*CPUProfile) GetTopDownRoot ¶
func (c *CPUProfile) GetTopDownRoot() *CPUProfileNode
Returns the root node of the top down call tree.
type CPUProfileNode ¶
type CPUProfileNode struct {
// contains filtered or unexported fields
}
func (*CPUProfileNode) GetBailoutReason ¶
func (c *CPUProfileNode) GetBailoutReason() string
Returns the bailout reason for the function if the optimization was disabled for it.
func (*CPUProfileNode) GetChild ¶
func (c *CPUProfileNode) GetChild(index int) *CPUProfileNode
Retrieves a child node by index.
func (*CPUProfileNode) GetChildrenCount ¶
func (c *CPUProfileNode) GetChildrenCount() int
func (*CPUProfileNode) GetColumnNumber ¶
func (c *CPUProfileNode) GetColumnNumber() int
Returns number of the column where the function originates.
func (*CPUProfileNode) GetFunctionName ¶
func (c *CPUProfileNode) GetFunctionName() string
Returns function name (empty string for anonymous functions.)
func (*CPUProfileNode) GetHitCount ¶
func (c *CPUProfileNode) GetHitCount() int
Returns count of samples where the function was currently executing.
func (*CPUProfileNode) GetLineNumber ¶
func (c *CPUProfileNode) GetLineNumber() int
Returns number of the line where the function originates.
func (*CPUProfileNode) GetParent ¶
func (c *CPUProfileNode) GetParent() *CPUProfileNode
Retrieves the ancestor node, or nil if the root.
func (*CPUProfileNode) GetScriptId ¶
func (c *CPUProfileNode) GetScriptId() int
Returns id for script from where the function originates.
func (*CPUProfileNode) GetScriptResourceName ¶
func (c *CPUProfileNode) GetScriptResourceName() string
Returns resource name for script from where the function originates.
type CPUProfiler ¶
type CPUProfiler struct {
// contains filtered or unexported fields
}
func NewCPUProfiler ¶
func NewCPUProfiler(iso *Isolate) *CPUProfiler
CPUProfiler is used to control CPU profiling.
func (*CPUProfiler) StartProfiling ¶
func (c *CPUProfiler) StartProfiling(title string)
StartProfiling starts collecting a CPU profile. Title may be an empty string. Several profiles may be collected at once. Attempts to start collecting several profiles with the same title are silently ignored.
func (*CPUProfiler) StopProfiling ¶
func (c *CPUProfiler) StopProfiling(title string) *CPUProfile
Stops collecting CPU profile with a given title and returns it. If the title given is empty, finishes the last profile started.
type CompileMode ¶
type CompileOptions ¶
type CompileOptions struct {
CachedData *CompilerCachedData
Mode CompileMode
}
type CompilerCachedData ¶
type ConsoleAPIMessage ¶
type ConsoleAPIMessage struct {
ErrorLevel MessageErrorLevel
Message string
Url string
LineNumber uint
ColumnNumber uint
// contains filtered or unexported fields
}
ConsoleAPIMessage contains the information from v8 from console function calls.
The fields correspond to the arguments for the C++ function v8_inspector::InspectorClient::consoleAPIMessage
Note: Stack traces are not supported.
See also: https://v8.github.io/api/head/classv8__inspector_1_1V8InspectorClient.html
type ConsoleAPIMessageHandler ¶
type ConsoleAPIMessageHandler interface {
ConsoleAPIMessage(message ConsoleAPIMessage)
}
A ConsoleAPIMessageHandler will receive JavaScript `console` API calls.
type Context ¶
type Context struct {
// contains filtered or unexported fields
}
Context is a global root execution environment that allows separate, unrelated, JavaScript applications to run in a single instance of V8.
Example ¶
package main
import (
"fmt"
v8 "github.com/hlvs-apps/v8go"
)
func main() {
ctx := v8.NewContext()
defer ctx.Isolate().Dispose()
defer ctx.Close()
ctx.RunScript("const add = (a, b) => a + b", "math.js")
ctx.RunScript("const result = add(3, 4)", "main.js")
val, _ := ctx.RunScript("result", "value.js")
fmt.Println(val)
}
Output: 7
Example (GlobalTemplate) ¶
package main
import (
"fmt"
v8 "github.com/hlvs-apps/v8go"
)
func main() {
iso := v8.NewIsolate()
defer iso.Dispose()
obj := v8.NewObjectTemplate(iso)
obj.Set("version", "v1.0.0")
ctx := v8.NewContext(iso, obj)
defer ctx.Close()
val, _ := ctx.RunScript("version", "main.js")
fmt.Println(val)
}
Output: v1.0.0
Example (Isolate) ¶
package main
import (
"fmt"
v8 "github.com/hlvs-apps/v8go"
)
func main() {
iso := v8.NewIsolate()
defer iso.Dispose()
ctx1 := v8.NewContext(iso)
defer ctx1.Close()
ctx1.RunScript("const foo = 'bar'", "context_one.js")
val, _ := ctx1.RunScript("foo", "foo.js")
fmt.Println(val)
ctx2 := v8.NewContext(iso)
defer ctx2.Close()
_, err := ctx2.RunScript("foo", "context_two.js")
fmt.Println(err)
}
Output: bar ReferenceError: foo is not defined
func NewContext ¶
func NewContext(opt ...ContextOption) *Context
NewContext creates a new JavaScript context; if no Isolate is passed as a ContextOption than a new Isolate will be created.
func (*Context) Close ¶
func (c *Context) Close()
Close will dispose the context and free the memory. Access to any values associated with the context after calling Close may panic.
func (*Context) Global ¶
Global returns the global proxy object. Global proxy object is a thin wrapper whose prototype points to actual context's global object with the properties like Object, etc. This is done that way for security reasons. Please note that changes to global proxy object prototype most probably would break the VM — V8 expects only global object as a prototype of global proxy object.
func (*Context) PerformMicrotaskCheckpoint ¶
func (c *Context) PerformMicrotaskCheckpoint()
PerformMicrotaskCheckpoint runs the default MicrotaskQueue until empty. This is used to make progress on Promises.
func (*Context) RetainedValueCount ¶
type ContextOption ¶
type ContextOption interface {
// contains filtered or unexported methods
}
ContextOption sets options such as Isolate and Global Template to the NewContext.
type Exception ¶
type Exception struct {
*Value
}
An Exception is a JavaScript exception.
func NewRangeError ¶
NewRangeError creates a RangeError.
func NewReferenceError ¶
NewReferenceError creates a ReferenceError.
func NewSyntaxError ¶
NewSyntaxError creates a SyntaxError.
func NewTypeError ¶
NewTypeError creates a TypeError.
func NewWasmCompileError ¶
NewWasmCompileError creates a WasmCompileError.
func NewWasmLinkError ¶
NewWasmLinkError creates a WasmLinkError.
func NewWasmRuntimeError ¶
NewWasmRuntimeError creates a WasmRuntimeError.
type Function ¶
type Function struct {
*Value
}
Function is a JavaScript function.
func (*Function) NewInstance ¶
Invoke a constructor function to create an object instance.
func (*Function) SourceMapUrl ¶
Return the source map url for a function.
type FunctionCallback ¶
type FunctionCallback func(info *FunctionCallbackInfo) *Value
FunctionCallback is a callback that is executed in Go when a function is executed in JS.
type FunctionCallbackInfo ¶
type FunctionCallbackInfo struct {
// contains filtered or unexported fields
}
FunctionCallbackInfo is the argument that is passed to a FunctionCallback.
func (*FunctionCallbackInfo) Args ¶
func (i *FunctionCallbackInfo) Args() []*Value
Args returns a slice of the value arguments that are passed to the JS function.
func (*FunctionCallbackInfo) Context ¶
func (i *FunctionCallbackInfo) Context() *Context
Context is the current context that the callback is being executed in.
func (*FunctionCallbackInfo) Release ¶
func (i *FunctionCallbackInfo) Release()
func (*FunctionCallbackInfo) This ¶
func (i *FunctionCallbackInfo) This() *Object
This returns the receiver object "this".
type FunctionCallbackWithError ¶
type FunctionCallbackWithError func(info *FunctionCallbackInfo) (*Value, error)
FunctionCallbackWithError is a callback that is executed in Go when a function is executed in JS. If a ValueError is returned, its value will be thrown as an exception in V8, otherwise Error() is invoked, and the string is thrown.
type FunctionTemplate ¶
type FunctionTemplate struct {
// contains filtered or unexported fields
}
FunctionTemplate is used to create functions at runtime. There can only be one function created from a FunctionTemplate in a context. The lifetime of the created function is equal to the lifetime of the context.
A FunctionTemplate can be used to create "constructors", and add methods to the "class". FunctionTemplate.PrototypeTemplate can be used to add normal methods on the class, and FunctionTemplate.InstanceTemplate can be used to add fields automatically to new instances of a class.
V8 API Docs: https://v8.github.io/api/head/classv8_1_1FunctionTemplate.html
Example ¶
package main
import (
"fmt"
v8 "github.com/hlvs-apps/v8go"
)
func main() {
iso := v8.NewIsolate()
defer iso.Dispose()
global := v8.NewObjectTemplate(iso)
printfn := v8.NewFunctionTemplate(iso, func(info *v8.FunctionCallbackInfo) *v8.Value {
fmt.Printf("%+v\n", info.Args())
return nil
})
global.Set("print", printfn, v8.ReadOnly)
ctx := v8.NewContext(iso, global)
defer ctx.Close()
ctx.RunScript("print('foo', 'bar', 0, 1)", "")
}
Output: [foo bar 0 1]
Example (Fetch) ¶
package main
import (
"fmt"
"io"
"net/http"
"strings"
v8 "github.com/hlvs-apps/v8go"
)
func main() {
iso := v8.NewIsolate()
defer iso.Dispose()
global := v8.NewObjectTemplate(iso)
fetchfn := v8.NewFunctionTemplate(iso, func(info *v8.FunctionCallbackInfo) *v8.Value {
args := info.Args()
url := args[0].String()
resolver, _ := v8.NewPromiseResolver(info.Context())
go func() {
res, _ := http.Get(url) //nolint:gosec,noctx,bodyclose
body, _ := io.ReadAll(res.Body)
val, _ := v8.NewValue(iso, string(body))
resolver.Resolve(val)
}()
return resolver.GetPromise().Value
})
global.Set("fetch", fetchfn, v8.ReadOnly)
ctx := v8.NewContext(iso, global)
defer ctx.Close()
val, _ := ctx.RunScript("fetch('https://rogchap.com/v8go')", "")
prom, _ := val.AsPromise()
// wait for the promise to resolve
for prom.State() == v8.Pending {
continue
}
fmt.Printf("%s\n", strings.Split(prom.Result().String(), "\n")[0])
}
Output: <!DOCTYPE html>
func NewFunctionTemplate ¶
func NewFunctionTemplate(iso *Isolate, callback FunctionCallback) *FunctionTemplate
NewFunctionTemplate creates a FunctionTemplate for a given callback. Prefer using NewFunctionTemplateWithError.
func NewFunctionTemplateWithError ¶
func NewFunctionTemplateWithError( iso *Isolate, callback FunctionCallbackWithError, ) *FunctionTemplate
NewFunctionTemplateWithError creates a FunctionTemplate for a given callback. If the callback returns an error, it will be thrown as a JS error.
func (*FunctionTemplate) GetFunction ¶
func (tmpl *FunctionTemplate) GetFunction(ctx *Context) *Function
GetFunction returns an instance of this function template bound to the given context.
func (*FunctionTemplate) Inherit ¶
func (tmpl *FunctionTemplate) Inherit(base *FunctionTemplate)
func (*FunctionTemplate) InstanceTemplate ¶
func (tmpl *FunctionTemplate) InstanceTemplate() *ObjectTemplate
InstanceTemplate gets the ObjectTemplate that is used for new object instances created when this function is used as a constructor.
You can add functions and values to new instance using ObjectTemplate.Set and ObjectTemplate.SetSymbol. Those values will become own properties on the instance, not the prototype.
Adding a function to an instance template corresponds to the following JavaScript:
class Example() {
constructor() {
this.foo = function() { /* creates a function on the instance */ }
}
}
func (*FunctionTemplate) PrototypeTemplate ¶
func (tmpl *FunctionTemplate) PrototypeTemplate() *ObjectTemplate
PrototypeTemplate gets the ObjectTemplate that is used to create the prototype object associated with the function.
You can call ObjectTemplate.Set or ObjectTemplate.SetSymbol, passing a FunctionTemplate to add a "method" to the class.
Adding a function to a prototype template corresponds normal method on a JavaScript "class":
class Example {
foo() { /* this is a method on the prototype */ }
}
Or the old-school way
function Example() {}
Example.prototype.foo = function() { }
The function becomes an own property on the prototype, not the instance.
func (FunctionTemplate) Set ¶
func (t FunctionTemplate) Set(name string, val interface{}, attributes ...PropertyAttribute) error
Set adds a property to each instance created by this template. The property must be defined either as a primitive value, or a template. If the value passed is a Go supported primitive (string, int32, uint32, int64, uint64, float64, big.Int) then a value will be created and set as the value property.
func (FunctionTemplate) SetSymbol ¶
func (t FunctionTemplate) SetSymbol(key *Symbol, val interface{}, attributes ...PropertyAttribute) error
SetSymbol adds a property to each instance created by this template. The property must be defined either as a primitive value, or a template. If the value passed is a Go supported primitive (string, int32, uint32, int64, uint64, float64, big.Int) then a value will be created and set as the value property.
type HeapStatistics ¶
type HeapStatistics struct {
TotalHeapSize uint64
TotalHeapSizeExecutable uint64
TotalPhysicalSize uint64
TotalAvailableSize uint64
UsedHeapSize uint64
HeapSizeLimit uint64
MallocedMemory uint64
ExternalMemory uint64
PeakMallocedMemory uint64
NumberOfNativeContexts uint64
NumberOfDetachedContexts uint64
}
HeapStatistics represents V8 isolate heap statistics.
type Injector ¶
type Injector interface {
// Inject is called when the isolate is created and allows
// for injecting a custom implementation to the isolate.
Inject(*Isolate, *ObjectTemplate) error
}
Injector is an interface that allows for injecting a custom implementation to the v8go isolate.
type Inspector ¶
type Inspector struct {
// contains filtered or unexported fields
}
An Inspector in v8 provides access to internals of the engine, such as console output
To receive console output, you need to first create an InspectorClient which will handle the interaction for a specific Context.
After a Context is created, you need to register it with the Inspector using Inspector.ContextCreated, and cleanup using Inspector.ContextDestroyed.
See also: https://v8.github.io/api/head/classv8__inspector_1_1V8Inspector.html
func NewInspector ¶
func NewInspector(iso *Isolate, client *InspectorClient) *Inspector
NewInspector creates an Inspector for a specific Isolate iso communicating with the InspectorClient client.
Before disposing the iso, be sure to dispose the inspector using Inspector.Dispose.
func (*Inspector) ContextCreated ¶
ContextCreated tells the inspector that a new Context has been created. This must be called before the InspectorClient can be used.
func (*Inspector) ContextDestroyed ¶
ContextDestroyed must be called before a Context is closed.
type InspectorClient ¶
type InspectorClient struct {
// contains filtered or unexported fields
}
An InspectorClient is the bridge from the Inspector to your code.
func NewInspectorClient ¶
func NewInspectorClient(handler ConsoleAPIMessageHandler) *InspectorClient
Create a new InspectorClient passing a handler that will receive the callbacks from v8.
func (*InspectorClient) Dispose ¶
func (c *InspectorClient) Dispose()
Dispose frees up resources taken up by the InspectorClient. Be sure to call this after calling Inspector.Dispose.
type Isolate ¶
type Isolate struct {
// contains filtered or unexported fields
}
Isolate is a JavaScript VM instance with its own heap and garbage collector. Most applications will create one isolate with many V8 contexts for execution.
func NewIsolate ¶
func NewIsolate(opts ...IsolateOption) *Isolate
NewIsolate creates a new V8 isolate with the provided options. Only one thread may access a given isolate at a time, but different threads may access different isolates simultaneously. When an isolate is no longer used its resources should be freed by calling iso.Dispose(). An *Isolate can be used as a v8go.ContextOption to create a new Context, rather than creating a new default Isolate.
func (*Isolate) CompileUnboundScript ¶
func (i *Isolate) CompileUnboundScript( source, origin string, opts CompileOptions, ) (*UnboundScript, error)
CompileUnboundScript will create an UnboundScript (i.e. context-indepdent) using the provided source JavaScript, origin (a.k.a. filename), and options. If options contain a non-null CachedData, compilation of the script will use that code cache. error will be of type `JSError` if not nil.
func (*Isolate) Dispose ¶
func (i *Isolate) Dispose()
Dispose will dispose the Isolate VM; subsequent calls will panic.
func (*Isolate) GetHeapStatistics ¶
func (i *Isolate) GetHeapStatistics() HeapStatistics
GetHeapStatistics returns heap statistics for an isolate.
func (*Isolate) IsExecutionTerminating ¶
IsExecutionTerminating returns whether V8 is currently terminating Javascript execution. If true, there are still JavaScript frames on the stack and the termination exception is still active.
func (*Isolate) TerminateExecution ¶
func (i *Isolate) TerminateExecution()
TerminateExecution terminates forcefully the current thread of JavaScript execution in the given isolate.
func (*Isolate) ThrowException ¶
ThrowException schedules an exception to be thrown when returning to JavaScript. When an exception has been scheduled it is illegal to invoke any JavaScript operation; the caller must return immediately and only after the exception has been handled does it become legal to invoke JavaScript operations.
type IsolateOption ¶
type IsolateOption func(*isolateConfig)
IsolateOption configures an Isolate on creation.
func WithResourceConstraints ¶
func WithResourceConstraints(initialHeapSizeInBytes, maxHeapSizeInBytes uint64) IsolateOption
WithResourceConstraints sets memory constraints for the isolate. If constraints are set, v8go will try to call `TerminateExecution` when the hard limit is hit.
type JSError ¶
JSError is an error that is returned if there is are any JavaScript exceptions handled in the context. When used with the fmt verb `%+v`, will output the JavaScript stack trace, if available.
type LocalRef ¶
type LocalRef uint32
LocalRef identifies one value inside a BatchScope. It is an index into the scope's handle table, not a pointer, so it cannot dangle: closing the scope invalidates every ref at once. Refs from one scope are meaningless in another.
A builder that fails returns InvalidLocalRef instead of an error, and every method that consumes a ref propagates an invalid one rather than panicking. That lets a caller build an entire tree unchecked and find out at BatchScope.Result, which reports what actually went wrong.
type MessageErrorLevel ¶
type MessageErrorLevel uint8
Represents the level of console output from JavaScript. E.g., `console.log`, `console.error`, etc.
The values reflect the values of v8::Isolate::MessageErrorLevel
See also: https://v8.github.io/api/head/classv8_1_1Isolate.html
const ( ErrorLevelLog MessageErrorLevel = 1 << iota ErrorLevelDebug ErrorLevelInfo ErrorLevelError ErrorLevelWarning ErrorLevelAll = ErrorLevelLog | ErrorLevelDebug | ErrorLevelInfo | ErrorLevelError | ErrorLevelWarning )
func (MessageErrorLevel) String ¶
func (lvl MessageErrorLevel) String() string
type Object ¶
type Object struct {
*Value
}
Object is a JavaScript object (ECMA-262, 4.3.3).
Example (Global) ¶
package main
import (
"fmt"
v8 "github.com/hlvs-apps/v8go"
)
func main() {
iso := v8.NewIsolate()
defer iso.Dispose()
ctx := v8.NewContext(iso)
defer ctx.Close()
global := ctx.Global()
console := v8.NewObjectTemplate(iso)
logfn := v8.NewFunctionTemplate(iso, func(info *v8.FunctionCallbackInfo) *v8.Value {
fmt.Println(info.Args()[0])
return nil
})
console.Set("log", logfn)
consoleObj, _ := console.NewInstance(ctx)
global.Set("console", consoleObj)
ctx.RunScript("console.log('foo')", "")
}
Output: foo
func (*Object) Delete ¶
Delete returns true if successful in deleting a named property on the object.
func (*Object) DeleteIdx ¶
DeleteIdx returns true if successful in deleting a value at a given index of the object.
func (*Object) DeleteSymbol ¶
DeleteSymbol returns true if successful in deleting a named property on the object.
func (*Object) GetInternalField ¶
GetInternalField gets the Value set by SetInternalField for the given index or the JS undefined value if the index hadn't been set. Panics if given an out of range index, or the field contains a Data other than a Value.
func (*Object) Has ¶
Has calls the abstract operation HasProperty(O, P) described in ECMA-262, 7.3.10. Returns true, if the object has the property, either own or on the prototype chain.
func (*Object) HasSymbol ¶
HasSymbol calls the abstract operation HasProperty(O, P) described in ECMA-262, 7.3.10. Returns true, if the object has the property, either own or on the prototype chain.
func (*Object) InternalFieldCount ¶
InternalFieldCount returns the number of internal fields this Object has.
func (*Object) MethodCall ¶
func (*Object) Set ¶
Set will set a property on the Object to a given value. Supports all value types, eg: Object, Array, Date, Set, Map etc If the value passed is a Go supported primitive (string, int32, uint32, int64, uint64, float64, big.Int) then a *Value will be created and set as the value property.
func (*Object) SetIdx ¶
Set will set a given index on the Object to a given value. Supports all value types, eg: Object, Array, Date, Set, Map etc If the value passed is a Go supported primitive (string, int32, uint32, int64, uint64, float64, big.Int) then a *Value will be created and set as the value property.
func (*Object) SetInternalField ¶
SetInternalField sets the value of an internal field for an ObjectTemplate instance. Panics if the index isn't in the range set by (*ObjectTemplate).SetInternalFieldCount.
func (*Object) SetSymbol ¶
SetSymbol will set a property on the Object to a given value. Supports all value types, eg: Object, Array, Date, Set, Map etc If the value passed is a Go supported primitive (string, int32, uint32, int64, uint64, float64, big.Int) then a *Value will be created and set as the value property.
type ObjectTemplate ¶
type ObjectTemplate struct {
// contains filtered or unexported fields
}
ObjectTemplate is used to create objects at runtime. Properties added to an ObjectTemplate are added to each object created from the ObjectTemplate.
func NewObjectTemplate ¶
func NewObjectTemplate(iso *Isolate) *ObjectTemplate
NewObjectTemplate creates a new ObjectTemplate. The *ObjectTemplate can be used as a v8go.ContextOption to create a global object in a Context.
func (*ObjectTemplate) InternalFieldCount ¶
func (o *ObjectTemplate) InternalFieldCount() uint32
InternalFieldCount returns the number of internal fields that instances of this template will have.
func (*ObjectTemplate) MarkAsUndetectable ¶
func (o *ObjectTemplate) MarkAsUndetectable()
MarkAsUndetectable marks object instances of the template as undetectable. Undetectable objects behave like undefined, but you can access properties defined on undetectable objects.
Note: Undetectable objects MUST have a CallAsFunctionHandler, see ObjectTemplate.SetCallAsFunctionHandler.
func (*ObjectTemplate) NewInstance ¶
func (o *ObjectTemplate) NewInstance(ctx *Context) (*Object, error)
NewInstance creates a new Object based on the template.
func (ObjectTemplate) Set ¶
func (t ObjectTemplate) Set(name string, val interface{}, attributes ...PropertyAttribute) error
Set adds a property to each instance created by this template. The property must be defined either as a primitive value, or a template. If the value passed is a Go supported primitive (string, int32, uint32, int64, uint64, float64, big.Int) then a value will be created and set as the value property.
func (*ObjectTemplate) SetAccessorProperty ¶
func (o *ObjectTemplate) SetAccessorProperty( key string, get *FunctionTemplate, set *FunctionTemplate, attributes PropertyAttribute, )
SetAccessorProperty creates a named accessor property, i.e., a property that is implemented as a function call. Arguments get and set represents the getter and setter, and can both be nil.
Note: The ReadOnly should not be used with a readonly property. If set is nil, the property will be readonly, and passing None is a sensible default.
This corresponds to ObjectTemplate::SetAccessorProperty in the C++ API.
Example ¶
package main
import (
"fmt"
v8 "github.com/hlvs-apps/v8go"
)
func main() {
iso := v8.NewIsolate()
defer iso.Dispose()
tmpl := v8.NewObjectTemplate(iso)
tmpl.SetAccessorProperty(
"prop",
// Getter
v8.NewFunctionTemplateWithError(
iso,
func(*v8.FunctionCallbackInfo) (*v8.Value, error) {
return v8.NewValue(iso, "Value")
},
),
nil, // Setter
v8.None,
)
global := v8.NewObjectTemplate(iso)
global.Set("obj", tmpl)
ctx := v8.NewContext(iso, global)
defer ctx.Close()
value, _ := ctx.RunScript("obj.prop", "")
fmt.Printf("Property value: %s\n", value.String())
}
Output: Property value: Value
Example (Helpers) ¶
package main
import (
"fmt"
v8 "github.com/hlvs-apps/v8go"
)
// SetObjectTemplateAccessorProperty shows an example of a helper that client
// code could optionally introduce.
//
// ObjectTemplate.SetAccessorProperty requires FunctionTemplate instances as
// arguments, but you rarely need the actual function template outside the
// scope of setting an accessor property.
//
// If many accessor properties must be created, this example could reduce
// repetitive trivial code.
func SetObjectTemplateAccessorProperty(
iso *v8.Isolate,
templ *v8.ObjectTemplate,
key string,
get v8.FunctionCallbackWithError,
set v8.FunctionCallbackWithError,
attributes v8.PropertyAttribute,
) {
var (
v8get *v8.FunctionTemplate
v8set *v8.FunctionTemplate
)
if get != nil {
v8get = v8.NewFunctionTemplateWithError(iso, get)
}
if set != nil {
v8set = v8.NewFunctionTemplateWithError(iso, set)
}
templ.SetAccessorProperty(key, v8get, v8set, attributes)
}
func main() {
iso := v8.NewIsolate()
defer iso.Dispose()
tmpl := v8.NewObjectTemplate(iso)
current, _ := v8.NewValue(iso, "current")
SetObjectTemplateAccessorProperty(iso, tmpl,
"prop",
// Getter
func(*v8.FunctionCallbackInfo) (*v8.Value, error) {
return current, nil
},
// Setter
func(info *v8.FunctionCallbackInfo) (*v8.Value, error) {
current = info.Args()[0]
return nil, nil
},
v8.None,
)
global := v8.NewObjectTemplate(iso)
global.Set("obj", tmpl)
ctx := v8.NewContext(iso, global)
defer ctx.Close()
value, _ := ctx.RunScript("obj.prop", "")
fmt.Printf("Property value before set: %s\n", value.String())
value, _ = ctx.RunScript("obj.prop = 'new value'; obj.prop", "")
fmt.Printf("Property value after set: %s\n", value.String())
}
Output: Property value before set: current Property value after set: new value
func (*ObjectTemplate) SetCallAsFunctionHandler ¶
func (o *ObjectTemplate) SetCallAsFunctionHandler(callback FunctionCallbackWithError)
SetCallAsFunctionHandler sets the callback to be used when calling instances created from this template. If no callback is set, instances behave like normal JavaScript objects that cannot be called as a function.
func (*ObjectTemplate) SetInternalFieldCount ¶
func (o *ObjectTemplate) SetInternalFieldCount(fieldCount uint32)
SetInternalFieldCount sets the number of internal fields that instances of this template will have.
func (ObjectTemplate) SetSymbol ¶
func (t ObjectTemplate) SetSymbol(key *Symbol, val interface{}, attributes ...PropertyAttribute) error
SetSymbol adds a property to each instance created by this template. The property must be defined either as a primitive value, or a template. If the value passed is a Go supported primitive (string, int32, uint32, int64, uint64, float64, big.Int) then a value will be created and set as the value property.
type Payload ¶
type Payload struct {
// Ops is the program. See [BuildValue].
Ops []uint32
// Shapes are the object shapes the program's OpObj operands index.
Shapes []ShapeDef
// Buf holds the bytes of every staged string and byte slice, concatenated.
Buf []byte
// Spans locates each string and byte-slice leaf.
Spans []Span
// KeySpans is how many leading entries of Spans are shape keys.
KeySpans int
// Ptrs holds the backing pointers of leaves that were pinned rather than
// staged, indexed by a pinned Span's Off. Only the first len(Ptrs) entries
// are read; a pooled backing array may hold anything past that. See
// [BuildValue] for the pinning rule.
Ptrs []unsafe.Pointer
// Nums holds the int64 scalars, positionally. Booleans ride here as 0/1.
Nums []int64
// Floats holds the float64 scalars, positionally.
Floats []float64
// Counts holds the program's control-flow values in execution order: one
// entry per OpRepeat executed, holding the length of that region, and one
// per OpNullable or OpOptional executed, holding its present/absent flag.
Counts []int32
}
Payload is the data half of a build: flat arrays the program indexes with implicitly advancing cursors, in the order a producer filled them.
Spans is one array with two regions. Spans[:KeySpans] are the shape keys and Spans[KeySpans:] are the values, so the value cursor starts at KeySpans; keys live in the same array to avoid a second buffer.
Nothing here is retained past the call: every leaf is copied into V8 while the call runs. See BuildValue for the pinning rule that applies to Ptrs.
type Promise ¶
type Promise struct {
*Object
}
Promise is the JavaScript promise object defined in ES6.
func (*Promise) Catch ¶
func (p *Promise) Catch(cb FunctionCallback) *Promise
Catch invokes the given function if the promise is rejected. See Then for other details.
func (*Promise) CatchWithError ¶
func (p *Promise) CatchWithError(cb FunctionCallbackWithError) *Promise
func (*Promise) Result ¶
Result is the value result of the Promise. The Promise must NOT be in a Pending state, otherwise may panic. Call promise.State() to validate state before calling for the result.
func (*Promise) State ¶
func (p *Promise) State() PromiseState
State returns the current state of the Promise.
func (*Promise) Then ¶
func (p *Promise) Then(cbs ...FunctionCallback) *Promise
Then accepts 1 or 2 callbacks. The first is invoked when the promise has been fulfilled. The second is invoked when the promise has been rejected. The returned Promise resolves after the callback finishes execution.
V8 only invokes the callback when processing "microtasks". The default MicrotaskPolicy processes them when the call depth decreases to 0. Call (*Context).PerformMicrotaskCheckpoint to trigger it manually.
func (*Promise) ThenWithError ¶
func (p *Promise) ThenWithError(cbs ...FunctionCallbackWithError) *Promise
type PromiseResolver ¶
type PromiseResolver struct {
*Object
// contains filtered or unexported fields
}
PromiseResolver is the resolver object for the promise. Most cases will create a new PromiseResolver and return the associated Promise from the resolver.
func NewPromiseResolver ¶
func NewPromiseResolver(ctx *Context) (*PromiseResolver, error)
NewPromiseResolver creates a new Promise resolver for the given context. The associated Promise will be in a Pending state.
func (*PromiseResolver) GetPromise ¶
func (r *PromiseResolver) GetPromise() *Promise
GetPromise returns the associated Promise object for this resolver. The Promise object is unique to the resolver and returns the same object on multiple calls.
func (*PromiseResolver) Reject ¶
func (r *PromiseResolver) Reject(err *Value) bool
Reject invokes the Promise reject state with the given value. The Promise state will transition from Pending to Rejected.
func (*PromiseResolver) Resolve ¶
func (r *PromiseResolver) Resolve(val Valuer) bool
Resolve invokes the Promise resolve state with the given value. The Promise state will transition from Pending to Fulfilled.
type PromiseState ¶
type PromiseState int
PromiseState is the state of the Promise.
const ( Pending PromiseState = iota Fulfilled Rejected )
type PropertyAttribute ¶
type PropertyAttribute uint8
PropertyAttribute are the attribute flags for a property on an Object. Typical usage when setting an Object or TemplateObject property, and can also be validated when accessing a property.
const ( // None. None PropertyAttribute = 0 // ReadOnly, ie. not writable. ReadOnly PropertyAttribute = 1 << iota // DontEnum, ie. not enumerable. DontEnum // DontDelete, ie. not configurable. DontDelete )
type ShapeDef ¶
ShapeDef names one object shape as a run of key spans: its keys are Payload.Spans[First : First+N], which must lie inside the key region.
type ShapeRef ¶
type ShapeRef uint32
ShapeRef identifies one interned key set inside a BatchScope. See BatchScope.Shape.
type Span ¶
Span locates one string or byte-slice leaf, either in Payload.Buf or through Payload.Ptrs. See SpanStaged and SpanPinned.
The layout is part of the ABI — 16 bytes, fields at offsets 0, 4 and 8 — because a producer may write these as a flat array without going through this type.
type Symbol ¶
type Symbol struct {
*Value
}
A Symbol represents a JavaScript symbol (ECMA-262 edition 6).
func SymbolAsyncIterator ¶
func SymbolHasInstance ¶
func SymbolIterator ¶
func SymbolMatch ¶
func SymbolReplace ¶
func SymbolSearch ¶
func SymbolSplit ¶
func SymbolToPrimitive ¶
func SymbolToStringTag ¶
func SymbolUnscopables ¶
func (*Symbol) Description ¶
Description returns the string representation of the symbol, e.g. "Symbol.asyncIterator".
type UnboundScript ¶
type UnboundScript struct {
// contains filtered or unexported fields
}
func (*UnboundScript) CreateCodeCache ¶
func (u *UnboundScript) CreateCodeCache() *CompilerCachedData
Create a code cache from the unbound script.
func (*UnboundScript) Run ¶
func (u *UnboundScript) Run(ctx *Context) (*Value, error)
Run will bind the unbound script to the provided context and run it. If the context provided does not belong to the same isolate that the script was compiled in, Run will panic. If an error occurs, it will be of type `JSError`.
type Value ¶
type Value struct {
// contains filtered or unexported fields
}
Value represents all Javascript values and objects.
func BuildValue ¶
BuildValue constructs a whole JavaScript value tree in a single cgo crossing and returns its root, owned by ctx the way any other v8go value is.
A program, not a serialization. p.Ops is a stack machine's instruction stream, and it encodes the shape of the data rather than the data itself: OpRepeat runs a body n times, so a marshaller that knows the Go type emits the loop as a loop. A []struct{ID int64; Name, Email string} is seven ops whether it holds one row or a million:
OpMark OpRepeat, 4 // n comes from Counts OpInt // from Nums OpStr // from Spans OpStr OpObj, shape // pops 3, pushes the object OpArrFromMark OpEnd
That is what makes this cheap. The alternative — a cgo call per node — costs ~64-84ns a time, so a 20-column, 1000-row result set spends over a millisecond on boundary crossings alone, more than JSON.parse needs to build the same tree from scratch. Here the per-node cost is a C switch dispatch, about 2ns.
Exactly one tracked value is created, for the root; every interior node lives and dies as a v8::Local inside the call.
It is safe to call from inside a FunctionTemplate callback, which is the shape the builder exists for: the host native receives a call from JS and answers it with a value rather than with JSON. V8 is already entered there — locked, in a HandleScope, in a Context::Scope, with a call in flight — and nesting another set of those is fine. What is NOT fine there is panicking: a Go panic unwinds out through V8's C++ frames without running their destructors, so the isolate is left entered and the process aborts at the next Isolate::Dispose, reported as a teardown fault far from its cause. BuildValue therefore reports everything, including a recovered panic, as an error.
Pinning. Everything in p is read, and copied into V8, during the call, and none of it is retained. The one caller obligation is p.Ptrs: each entry that the spans actually reference must point either to non-Go memory or to a Go object the caller has pinned with a runtime.Pinner that outlives the call. Only the first len(p.Ptrs) entries are read, and the addresses cross as addresses — a pooled backing array may hold whatever it likes in the slots past len.
A malformed program is an error, never a crash: every index is checked against its array before it is used, and a failure builds nothing.
func JSONParse ¶
JSONParse tries to parse the string and returns it as *Value if successful. Any JS errors will be returned as `JSError`.
Example ¶
package main
import (
"fmt"
v8 "github.com/hlvs-apps/v8go"
)
func main() {
ctx := v8.NewContext()
defer ctx.Isolate().Dispose()
defer ctx.Close()
val, _ := v8.JSONParse(ctx, `{"foo": "bar"}`)
fmt.Println(val)
}
Output: [object Object]
func NewValue ¶
NewValue will create a primitive value. Supported values types to create are:
string -> V8::String int32 -> V8::Integer uint32 -> V8::Integer int64 -> V8::BigInt uint64 -> V8::BigInt bool -> V8::Boolean *big.Int -> V8::BigInt
func (*Value) ArrayBufferViewBytes ¶
ArrayBufferViewBytes copies the bytes viewed by this value into a new, Go-owned byte slice and returns it. The value must be an ArrayBufferView — for example a Uint8Array, any other typed array, or a DataView. If it is not, ArrayBufferViewBytes returns nil.
Unlike SharedArrayBufferGetContents, the returned slice is an independent copy that does not alias V8-managed memory: it stays valid after the value (or its context) is released and needs no cleanup. It performs a single memcpy out of V8 and is binary-safe — every byte value round-trips exactly.
func (*Value) ArrayIndex ¶
ArrayIndex attempts to converts a string to an array index. Returns ok false if conversion fails.
func (*Value) AsException ¶
func (*Value) AsFunction ¶
func (*Value) AsObject ¶
AsObject will cast the value to the Object type. If the value is not an Object then an error is returned. Use `value.Object()` to do the JS equivalent of `Object(value)`.
func (*Value) AsSymbol ¶
AsSymbol will cast the value to the Symbol type. If the value is not a Symbol then an error is returned.
func (*Value) Boolean ¶
Boolean perform the equivalent of `Boolean(value)` in JS. This can never fail.
func (*Value) DetailString ¶
DetailString provide a string representation of this value usable for debugging.
func (*Value) Format ¶
Format implements the fmt.Formatter interface to provide a custom formatter primarily to output the detail string (for debugging) with `%+v` verb.
func (*Value) Int32 ¶
Int32 perform the equivalent of `Number(value)` in JS and convert the result to a signed 32-bit integer by performing the steps in https://tc39.es/ecma262/#sec-toint32.
func (*Value) Integer ¶
Integer perform the equivalent of `Number(value)` in JS and convert the result to an integer. Negative values are rounded up, positive values are rounded down. NaN is converted to 0. Infinite values yield undefined results.
func (*Value) IsArgumentsObject ¶
IsArgumentsObject returns true if this value is an Arguments object.
func (*Value) IsArray ¶
IsArray returns true if this value is an array. Note that it will return false for a `Proxy` of an array.
func (*Value) IsArrayBuffer ¶
IsArrayBuffer returns true if this value is an `ArrayBuffer`.
func (*Value) IsArrayBufferView ¶
IsArrayBufferView returns true if this value is an `ArrayBufferView`.
func (*Value) IsAsyncFunction ¶
IsAsyncFunc returns true if this value is an async function.
func (*Value) IsBigInt ¶
IsBigInt returns true if this value is a bigint. This is equivalent to `typeof value === 'bigint'` in JS.
func (*Value) IsBigInt64Array ¶
IsBigInt64Array returns true if this value is a `BigInt64Array`.
func (*Value) IsBigIntObject ¶
IsBigIntObject returns true if this value is a BigInt object.
func (*Value) IsBigUint64Array ¶
IsBigUint64Array returns true if this value is a BigUint64Array`.
func (*Value) IsBoolean ¶
IsBoolean returns true if this value is boolean. This is equivalent to `typeof value === 'boolean'` in JS.
func (*Value) IsDataView ¶
IsDataView returns true if this value is a `DataView`.
func (*Value) IsExternal ¶
IsExternal returns true if this value is an `External` object.
func (*Value) IsFalse ¶
IsFalse returns true if this value is false. This is not the same as `!BooleanValue()`. The latter performs a conversion to boolean, i.e. the result of `!Boolean(value)` in JS, whereas this checks `value === false`.
func (*Value) IsFloat32Array ¶
IsFloat32Array returns true if this value is a `Float32Array`.
func (*Value) IsFloat64Array ¶
IsFloat64Array returns true if this value is a `Float64Array`.
func (*Value) IsFunction ¶
IsFunction returns true if this value is a function. This is equivalent to `typeof value === 'function'` in JS.
func (*Value) IsGeneratorFunction ¶
Is IsGeneratorFunc returns true if this value is a Generator function.
func (*Value) IsGeneratorObject ¶
IsGeneratorObject returns true if this value is a Generator object (iterator).
func (*Value) IsInt8Array ¶
IsInt8Array returns true if this value is an `Int8Array`.
func (*Value) IsInt16Array ¶
IsInt16Array returns true if this value is an `Int16Array`.
func (*Value) IsInt32Array ¶
IsInt32Array returns true if this value is an `Int32Array`.
func (*Value) IsMapIterator ¶
IsMapIterator returns true if this value is a `Map` Iterator.
func (*Value) IsModuleNamespaceObject ¶
IsModuleNamespaceObject returns true if the value is a `Module` Namespace `Object`.
func (*Value) IsName ¶
IsName returns true if this value is a symbol or a string. This is equivalent to `typeof value === 'string' || typeof value === 'symbol'` in JS.
func (*Value) IsNativeError ¶
IsNativeError returns true if this value is a NativeError.
func (*Value) IsNullOrUndefined ¶
IsNullOrUndefined returns true if this value is either the null or the undefined value. See ECMA-262 4.3.11. and 4.3.12 This is equivalent to `value == null` in JS.
func (*Value) IsNumber ¶
IsNumber returns true if this value is a number. This is equivalent to `typeof value === 'number'` in JS.
func (*Value) IsNumberObject ¶
IsNumberObject returns true if this value is a `Number` object.
func (*Value) IsSetIterator ¶
IsSetIterator returns true if this value is a `Set` Iterator.
func (*Value) IsSharedArrayBuffer ¶
IsSharedArrayBuffer returns true if this value is a `SharedArrayBuffer`.
func (*Value) IsString ¶
IsString returns true if this value is an instance of the String type. See ECMA-262 8.4. This is equivalent to `typeof value === 'string'` in JS.
func (*Value) IsStringObject ¶
IsStringObject returns true if this value is a `String` object.
func (*Value) IsSymbol ¶
IsSymbol returns true if this value is a symbol. This is equivalent to `typeof value === 'symbol'` in JS.
func (*Value) IsSymbolObject ¶
IsSymbolObject returns true if this value is a `Symbol` object.
func (*Value) IsTrue ¶
IsTrue returns true if this value is true. This is not the same as `BooleanValue()`. The latter performs a conversion to boolean, i.e. the result of `Boolean(value)` in JS, whereas this checks `value === true`.
func (*Value) IsTypedArray ¶
IsTypedArray returns true if this value is one of TypedArrays.
func (*Value) IsUint8Array ¶
IsUint8Array returns true if this value is an `Uint8Array`.
func (*Value) IsUint8ClampedArray ¶
IsUint8ClampedArray returns true if this value is an `Uint8ClampedArray`.
func (*Value) IsUint16Array ¶
IsUint16Array returns true if this value is an `Uint16Array`.
func (*Value) IsUint32Array ¶
IsUint32Array returns true if this value is an `Uint32Array`.
func (*Value) IsUndefined ¶
IsUndefined returns true if this value is the undefined value. See ECMA-262 4.3.10.
func (*Value) IsWasmModuleObject ¶
IsWasmModuleObject returns true if this value is a `WasmModuleObject`.
func (*Value) MarshalJSON ¶
MarshalJSON implements the json.Marshaler interface.
func (*Value) Object ¶
Object perform the equivalent of Object(value) in JS. To just cast this value as an Object use AsObject() instead.
func (*Value) Release ¶
func (v *Value) Release()
Release this value. Using the value after calling this function will result in undefined behavior.
func (*Value) SameValue ¶
SameValue returns true if the other value is the same value. This is equivalent to `Object.is(v, other)` in JS.
func (*Value) SharedArrayBufferGetContents ¶
func (*Value) StrictEquals ¶
func (*Value) String ¶
String perform the equivalent of `String(value)` in JS. Primitive values are returned as-is, objects will return `[object Object]` and functions will print their definition.
func (*Value) Uint32 ¶
Uint32 perform the equivalent of `Number(value)` in JS and convert the result to an unsigned 32-bit integer by performing the steps in https://tc39.es/ecma262/#sec-touint32.
type ValueError ¶
A ValueError can be returned from a FunctionCallbackWithError, and its value will be thrown as an exception in V8.
Source Files
¶
- cgo.go
- cgo_linux_amd64.go
- context.go
- cpuprofile.go
- cpuprofilenode.go
- cpuprofiler.go
- errors.go
- exception.go
- function.go
- function_template.go
- gav8_build.go
- gav8_values.go
- injector.go
- inspector.go
- isolate.go
- json.go
- object.go
- object_template.go
- promise.go
- script_compiler.go
- symbol.go
- template.go
- unbound_script.go
- v8go.go
- value.go
Directories
¶
| Path | Synopsis |
|---|---|
|
deps
|
|
|
darwin_amd64
module
|
|
|
darwin_arm64
module
|
|
|
linux_amd64
module
|
|
|
linux_arm64
module
|
|
|
windows_amd64
module
|
|
|
windows_arm64
module
|