-
Notifications
You must be signed in to change notification settings - Fork 87
/
v8_script.go
161 lines (134 loc) · 3.59 KB
/
v8_script.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
package v8
/*
#include "v8_wrap.h"
#include <stdlib.h>
*/
import "C"
import "unsafe"
import "reflect"
import "runtime"
// A compiled JavaScript script.
//
type Script struct {
self unsafe.Pointer
}
// Pre-compiles the specified script (context-independent).
//
func (e *Engine) PreCompile(code []byte) *ScriptData {
codePtr := unsafe.Pointer((*reflect.StringHeader)(unsafe.Pointer(&code)).Data)
return newScriptData(C.V8_PreCompile(
e.self, (*C.char)(codePtr), C.int(len(code)),
))
}
// Compiles the specified script (context-independent).
// 'data' is the Pre-parsing data, as obtained by PreCompile()
// using pre_data speeds compilation if it's done multiple times.
//
func (e *Engine) Compile(code []byte, origin *ScriptOrigin, data *ScriptData) *Script {
var originPtr unsafe.Pointer
var dataPtr unsafe.Pointer
if origin != nil {
originPtr = origin.self
}
if data != nil {
dataPtr = data.self
}
codePtr := unsafe.Pointer((*reflect.StringHeader)(unsafe.Pointer(&code)).Data)
self := C.V8_Compile(e.self, (*C.char)(codePtr), C.int(len(code)), originPtr, dataPtr)
if self == nil {
return nil
}
result := &Script{
self: self,
}
runtime.SetFinalizer(result, func(s *Script) {
if traceDispose {
println("v8.Script.Dispose()", s.self)
}
C.V8_DisposeScript(s.self)
})
return result
}
// Runs the script returning the resulting value.
//
func (s *Script) Run() *Value {
return newValue(C.V8_Script_Run(s.self))
}
// Pre-compilation data that can be associated with a script. This
// data can be calculated for a script in advance of actually
// compiling it, and can be stored between compilations. When script
// data is given to the compile method compilation will be faster.
//
type ScriptData struct {
self unsafe.Pointer
}
func newScriptData(self unsafe.Pointer) *ScriptData {
if self == nil {
return nil
}
result := &ScriptData{
self: self,
}
runtime.SetFinalizer(result, func(s *ScriptData) {
if traceDispose {
println("v8.ScriptData.Dispose()")
}
C.V8_DisposeScriptData(s.self)
})
return result
}
// Load previous pre-compilation data.
//
func NewScriptData(data []byte) *ScriptData {
return newScriptData(C.V8_NewScriptData(
(*C.char)((unsafe.Pointer)(((*reflect.SliceHeader)(unsafe.Pointer(&data))).Data)),
C.int(len(data)),
))
}
// Returns the length of Data().
//
func (sd *ScriptData) Length() int {
return int(C.V8_ScriptData_Length(sd.self))
}
// Returns a serialized representation of this ScriptData that can later be
// passed to New(). NOTE: Serialized data is platform-dependent.
//
func (sd *ScriptData) Data() []byte {
return C.GoBytes(
unsafe.Pointer(C.V8_ScriptData_Data(sd.self)),
C.V8_ScriptData_Length(sd.self),
)
}
// Returns true if the source code could not be parsed.
//
func (sd *ScriptData) HasError() bool {
return C.V8_ScriptData_HasError(sd.self) == 1
}
// The origin, within a file, of a script.
//
type ScriptOrigin struct {
self unsafe.Pointer
Name string
LineOffset int
ColumnOffset int
}
func (e *Engine) NewScriptOrigin(name string, lineOffset, columnOffset int) *ScriptOrigin {
namePtr := unsafe.Pointer((*reflect.StringHeader)(unsafe.Pointer(&name)).Data)
self := C.V8_NewScriptOrigin(e.self, (*C.char)(namePtr), C.int(len(name)), C.int(lineOffset), C.int(columnOffset))
if self == nil {
return nil
}
result := &ScriptOrigin{
self: self,
Name: name,
LineOffset: lineOffset,
ColumnOffset: columnOffset,
}
runtime.SetFinalizer(result, func(so *ScriptOrigin) {
if traceDispose {
println("v8.ScriptOrigin.Dispose()")
}
C.V8_DisposeScriptOrigin(so.self)
})
return result
}