This repository has been archived by the owner on Oct 26, 2023. It is now read-only.
forked from kingland/go-v8
-
Notifications
You must be signed in to change notification settings - Fork 3
/
v8_script.go
94 lines (81 loc) · 1.94 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
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
}
// 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) *Script {
codePtr := unsafe.Pointer((*reflect.StringHeader)(unsafe.Pointer(&code)).Data)
self := C.V8_Compile(e.self, (*C.char)(codePtr), C.int(len(code)), unsafe.Pointer(origin))
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 (cs ContextScope) Run(s *Script) *Value {
return newValue(cs.GetEngine(), C.V8_Script_Run(s.self))
}
func (e *Engine) Run(s *Script) *Value{
return newValue(e, C.V8_Script_Run(s.self))
}
// The origin, within a file, of a script.
//
type ScriptOrigin struct {
Name string
LineOffset int
ColumnOffset int
}
func (e *Engine) NewScriptOrigin(name string, lineOffset, columnOffset int) *ScriptOrigin {
return &ScriptOrigin{
Name: name,
LineOffset: lineOffset,
ColumnOffset: columnOffset,
}
}
//export go_script_origin_get_name
func go_script_origin_get_name(p unsafe.Pointer) *C.char {
if p == nil {
return C.CString("")
}
o := (*ScriptOrigin)(p)
return C.CString(o.Name)
}
//export go_script_origin_get_line
func go_script_origin_get_line(p unsafe.Pointer) int {
if p == nil {
return 0
}
o := (*ScriptOrigin)(p)
return o.LineOffset
}
//export go_script_origin_get_column
func go_script_origin_get_column(p unsafe.Pointer) int {
if p == nil {
return 0
}
o := (*ScriptOrigin)(p)
return o.ColumnOffset
}