-
-
Notifications
You must be signed in to change notification settings - Fork 218
/
isolate.go
185 lines (161 loc) · 5.37 KB
/
isolate.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// Copyright 2019 Roger Chapman and the v8go contributors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package v8go
// #include <stdlib.h>
// #include "v8go.h"
import "C"
import (
"sync"
"unsafe"
)
var v8once sync.Once
// 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.
type Isolate struct {
ptr C.IsolatePtr
cbMutex sync.RWMutex
cbSeq int
cbs map[int]FunctionCallback
null *Value
undefined *Value
}
// HeapStatistics represents V8 isolate heap statistics
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
}
// NewIsolate creates a new V8 isolate. 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 NewIsolate() *Isolate {
initializeIfNecessary()
iso := &Isolate{
ptr: C.NewIsolate(),
cbs: make(map[int]FunctionCallback),
}
iso.null = newValueNull(iso)
iso.undefined = newValueUndefined(iso)
return iso
}
// TerminateExecution terminates forcefully the current thread
// of JavaScript execution in the given isolate.
func (i *Isolate) TerminateExecution() {
C.IsolateTerminateExecution(i.ptr)
}
// 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 (i *Isolate) IsExecutionTerminating() bool {
return C.IsolateIsExecutionTerminating(i.ptr) == 1
}
type CompileOptions struct {
CachedData *CompilerCachedData
Mode CompileMode
}
// 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 (i *Isolate) CompileUnboundScript(source, origin string, opts CompileOptions) (*UnboundScript, error) {
cSource := C.CString(source)
cOrigin := C.CString(origin)
defer C.free(unsafe.Pointer(cSource))
defer C.free(unsafe.Pointer(cOrigin))
var cOptions C.CompileOptions
if opts.CachedData != nil {
if opts.Mode != 0 {
panic("On CompileOptions, Mode and CachedData can't both be set")
}
cOptions.compileOption = C.ScriptCompilerConsumeCodeCache
cOptions.cachedData = C.ScriptCompilerCachedData{
data: (*C.uchar)(unsafe.Pointer(&opts.CachedData.Bytes[0])),
length: C.int(len(opts.CachedData.Bytes)),
}
} else {
cOptions.compileOption = C.int(opts.Mode)
}
rtn := C.IsolateCompileUnboundScript(i.ptr, cSource, cOrigin, cOptions)
if rtn.ptr == nil {
return nil, newJSError(rtn.error)
}
if opts.CachedData != nil {
opts.CachedData.Rejected = int(rtn.cachedDataRejected) == 1
}
return &UnboundScript{
ptr: rtn.ptr,
iso: i,
}, nil
}
// GetHeapStatistics returns heap statistics for an isolate.
func (i *Isolate) GetHeapStatistics() HeapStatistics {
hs := C.IsolationGetHeapStatistics(i.ptr)
return HeapStatistics{
TotalHeapSize: uint64(hs.total_heap_size),
TotalHeapSizeExecutable: uint64(hs.total_heap_size_executable),
TotalPhysicalSize: uint64(hs.total_physical_size),
TotalAvailableSize: uint64(hs.total_available_size),
UsedHeapSize: uint64(hs.used_heap_size),
HeapSizeLimit: uint64(hs.heap_size_limit),
MallocedMemory: uint64(hs.malloced_memory),
ExternalMemory: uint64(hs.external_memory),
PeakMallocedMemory: uint64(hs.peak_malloced_memory),
NumberOfNativeContexts: uint64(hs.number_of_native_contexts),
NumberOfDetachedContexts: uint64(hs.number_of_detached_contexts),
}
}
// Dispose will dispose the Isolate VM; subsequent calls will panic.
func (i *Isolate) Dispose() {
if i.ptr == nil {
return
}
C.IsolateDispose(i.ptr)
i.ptr = nil
}
// 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.
func (i *Isolate) ThrowException(value *Value) *Value {
if i.ptr == nil {
panic("Isolate has been disposed")
}
return &Value{
ptr: C.IsolateThrowException(i.ptr, value.ptr),
}
}
// Deprecated: use `iso.Dispose()`.
func (i *Isolate) Close() {
i.Dispose()
}
func (i *Isolate) apply(opts *contextOptions) {
opts.iso = i
}
func (i *Isolate) registerCallback(cb FunctionCallback) int {
i.cbMutex.Lock()
i.cbSeq++
ref := i.cbSeq
i.cbs[ref] = cb
i.cbMutex.Unlock()
return ref
}
func (i *Isolate) getCallback(ref int) FunctionCallback {
i.cbMutex.RLock()
defer i.cbMutex.RUnlock()
return i.cbs[ref]
}