-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathJSValue.swift
More file actions
163 lines (136 loc) · 4.09 KB
/
Copy pathJSValue.swift
File metadata and controls
163 lines (136 loc) · 4.09 KB
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
#if os(Linux)
import CJavaScriptCore
#else
import JavaScriptCore
#endif
protocol JSValueInitializable {
init(from jsValue: JSValue) throws
}
extension String: JSValueInitializable {
init(from jsValue: JSValue) throws {
self = try jsValue.toString()
}
}
public class JSValue {
let context: JSContextRef
let pointer: JSValueRef
init(context: JSContextRef, pointer: JSValueRef) {
self.context = context
self.pointer = pointer
}
init(undefinedIn context: JSContextRef) {
self.context = context
self.pointer = JSValueMakeUndefined(context)
}
init(bool: Bool, in context: JSContextRef) {
self.context = context
self.pointer = JSValueMakeBoolean(context, bool)
}
init(number: Double, in context: JSContextRef) {
self.context = context
self.pointer = JSValueMakeNumber(context, number)
}
init(string: String, in context: JSContextRef) {
self.context = context
let bytes = [UInt16](string.utf16)
let stringRef = JSStringCreateWithCharacters(bytes, bytes.count)
defer { JSStringRelease(stringRef) }
self.pointer = JSValueMakeString(context, stringRef)
}
}
extension JSValue {
convenience
public init(undefinedIn context: JSContext) {
self.init(undefinedIn: context.context)
}
convenience
public init(bool: Bool, in context: JSContext) {
self.init(bool: bool, in: context.context)
}
convenience
public init(number: Double, in context: JSContext) {
self.init(number: number, in: context.context)
}
convenience
public init(string: String, in context: JSContext) {
self.init(string: string, in: context.context)
}
}
extension JSValue {
subscript(_ property: String) -> JSValue? {
guard isObject else {
return nil
}
let bytes = [UInt16](property.utf16)
let property = JSStringCreateWithCharacters(bytes, bytes.count)
defer { JSStringRelease(property) }
var exception: JSValueRef?
let result = JSObjectGetProperty(context, pointer, property, &exception)
if exception != nil {
return nil
}
return JSValue(context: context, pointer: result!)
}
}
extension JSValue {
public var isNull: Bool {
return JSValueIsNull(context, pointer)
}
public var isUndefined: Bool {
return JSValueIsUndefined(context, pointer)
}
public var isBool: Bool {
return JSValueIsBoolean(context, pointer)
}
public var isNumber: Bool {
return JSValueIsNumber(context, pointer)
}
public var isString: Bool {
return JSValueIsString(context, pointer)
}
public var isObject: Bool {
return JSValueIsObject(context, pointer)
}
}
extension JSValue {
public func toBool() -> Bool {
return JSValueToBoolean(context, pointer)
}
public func toDouble() throws -> Double {
return try JSValueToNumber(context, pointer)
}
public func toInt() throws -> Int {
return Int(try JSValueToNumber(context, pointer))
}
public func toString() throws -> String {
let stringRef = try JSValueToStringCopy(context, pointer)
defer { JSStringRelease(stringRef) }
let len = JSStringGetLength(stringRef)
let characters = JSStringGetCharactersPtr(stringRef)
let buffer = UnsafeBufferPointer<UInt16>(start: characters!, count: len)
return String(decoding: buffer, as: UTF16.self)
}
}
extension Array where Element == JSValue {
init(
start: UnsafePointer<JSValueRef?>?,
count: Int,
in context: JSObjectRef
) {
var arguments = [JSValue]()
for i in 0..<count {
let valueRef = start!.advanced(by: i).pointee!
arguments.append(JSValue(context: context, pointer: valueRef))
}
self = arguments
}
}
extension JSValue: CustomStringConvertible {
public var description: String {
do {
return try toString()
} catch {
return "unknown"
}
}
}