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_util.go
113 lines (106 loc) · 2.79 KB
/
v8_util.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
package v8
/*
#include "v8_wrap.h"
*/
import "C"
import "unsafe"
import "reflect"
var (
jsonObjectBegin = []byte("{")
jsonObjectEnd = []byte("}")
jsonColon = []byte(":")
jsonComma = []byte(",")
jsonQuote = []byte("\"")
jsonArrayBegin = []byte("[")
jsonArrayEnd = []byte("]")
jsonTrue = []byte("true")
jsonFalse = []byte("false")
jsonNull = []byte("null")
jsonDateFormat = "2006-01-02T15:04:05Z0700"
)
func (cs ContextScope) Eval(code string) *Value {
if script := cs.context.engine.Compile([]byte(code), nil); script != nil {
return cs.Run(script)
}
return nil
}
func (cs ContextScope) ParseJSON(json string) *Value {
jsonPtr := unsafe.Pointer((*reflect.StringHeader)(unsafe.Pointer(&json)).Data)
return newValue(cs.GetEngine(), C.V8_ParseJSON(cs.context.self, (*C.char)(jsonPtr), C.int(len(json))))
}
func ToJSON(value *Value) []byte {
return AppendJSON(make([]byte, 0, 1024), value)
}
func AppendJSON(dst []byte, value *Value) []byte {
switch {
case value.IsArray():
dst = append(dst, jsonArrayBegin...)
array := value.ToArray()
length := array.Length()
for i := 0; i < length; i++ {
dst = AppendJSON(dst, array.GetElement(i))
if i < length-1 {
dst = append(dst, jsonComma...)
}
}
dst = append(dst, jsonArrayEnd...)
case value.IsDate():
dst = append(dst, jsonQuote...)
date := value.ToTime()
dst = append(dst, date.Format(jsonDateFormat)...)
dst = append(dst, jsonQuote...)
case value.IsObject():
dst = append(dst, jsonObjectBegin...)
object := value.ToObject()
names := object.GetOwnPropertyNames()
length := names.Length()
for i := 0; i < length; i++ {
name := names.GetElement(i).ToString()
dst = append(dst, jsonQuote...)
dst = append(dst, name...)
dst = append(dst, jsonQuote...)
dst = append(dst, jsonColon...)
dst = AppendJSON(dst, object.GetProperty(name))
if i < length-1 {
dst = append(dst, jsonComma...)
}
}
dst = append(dst, jsonObjectEnd...)
case value.IsString():
dst = append(dst, jsonQuote...)
str := value.ToString()
for i := 0; i < len(str); i++ {
c := str[i]
switch c {
case '"':
dst = append(dst, '\\', '"')
case '\\':
dst = append(dst, '\\', '\\')
case '/':
dst = append(dst, '\\', '/')
case '\n':
dst = append(dst, '\\', 'n')
case '\r':
dst = append(dst, '\\', 'r')
case '\t':
dst = append(dst, '\\', 't')
case '\b':
dst = append(dst, '\\', 'b')
case '\f':
dst = append(dst, '\\', 'f')
default:
dst = append(dst, c)
}
}
dst = append(dst, jsonQuote...)
case value.IsNumber():
dst = append(dst, value.ToString()...)
case value.IsTrue():
dst = append(dst, jsonTrue...)
case value.IsFalse():
dst = append(dst, jsonFalse...)
case value.IsNull():
dst = append(dst, jsonNull...)
}
return dst
}