jsc_value.hpp
6.9 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
////////////////////////////////////////////////////////////////////////////
//
// Copyright 2016 Realm Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
////////////////////////////////////////////////////////////////////////////
#pragma once
#include "jsc_protected.hpp"
#include "jsc_types.hpp"
#include "jsc_string.hpp"
namespace realm {
namespace js {
static inline bool is_object_of_type(JSContextRef ctx, JSValueRef value, jsc::String type) {
JSObjectRef global_object = JSContextGetGlobalObject(ctx);
JSValueRef exception = nullptr;
JSValueRef constructor = JSObjectGetProperty(ctx, global_object, type, &exception);
if (exception) {
throw jsc::Exception(ctx, exception);
}
bool result = JSValueIsInstanceOfConstructor(ctx, value, jsc::Value::validated_to_constructor(ctx, constructor), &exception);
if (exception) {
throw jsc::Exception(ctx, exception);
}
return result;
}
template<>
inline const char *jsc::Value::typeof(JSContextRef ctx, const JSValueRef &value) {
switch (JSValueGetType(ctx, value)) {
case kJSTypeNull: return "null";
case kJSTypeNumber: return "number";
case kJSTypeObject: return "object";
case kJSTypeString: return "string";
case kJSTypeBoolean: return "boolean";
case kJSTypeUndefined: return "undefined";
#if defined __IPHONE_12_2 || defined __MAC_10_14_4
case kJSTypeSymbol: return "symbol";
#endif
}
}
template<>
inline bool jsc::Value::is_array(JSContextRef ctx, const JSValueRef &value) {
// JSValueIsArray() is not available until iOS 9.
static const jsc::String type = "Array";
return is_object_of_type(ctx, value, type);
}
template<>
inline bool jsc::Value::is_array_buffer(JSContextRef ctx, const JSValueRef &value) {
static const jsc::String type = "ArrayBuffer";
return is_object_of_type(ctx, value, type);
}
template<>
inline bool jsc::Value::is_date(JSContextRef ctx, const JSValueRef &value) {
static const jsc::String type = "Date";
return is_object_of_type(ctx, value, type);
}
template<>
inline bool jsc::Value::is_boolean(JSContextRef ctx, const JSValueRef &value) {
return JSValueIsBoolean(ctx, value);
}
template<>
inline bool jsc::Value::is_constructor(JSContextRef ctx, const JSValueRef &value) {
return JSValueIsObject(ctx, value) && JSObjectIsConstructor(ctx, (JSObjectRef)value);
}
template<>
inline bool jsc::Value::is_function(JSContextRef ctx, const JSValueRef &value) {
return JSValueIsObject(ctx, value) && JSObjectIsFunction(ctx, (JSObjectRef)value);
}
template<>
inline bool jsc::Value::is_null(JSContextRef ctx, const JSValueRef &value) {
return JSValueIsNull(ctx, value);
}
template<>
inline bool jsc::Value::is_number(JSContextRef ctx, const JSValueRef &value) {
return JSValueIsNumber(ctx, value);
}
template<>
inline bool jsc::Value::is_object(JSContextRef ctx, const JSValueRef &value) {
return JSValueIsObject(ctx, value);
}
template<>
inline bool jsc::Value::is_string(JSContextRef ctx, const JSValueRef &value) {
return JSValueIsString(ctx, value);
}
template<>
bool jsc::Value::is_binary(JSContextRef ctx, const JSValueRef &value);
template<>
inline bool jsc::Value::is_undefined(JSContextRef ctx, const JSValueRef &value) {
return JSValueIsUndefined(ctx, value);
}
template<>
inline bool jsc::Value::is_valid(const JSValueRef &value) {
return value != nullptr;
}
template<>
inline JSValueRef jsc::Value::from_boolean(JSContextRef ctx, bool boolean) {
return JSValueMakeBoolean(ctx, boolean);
}
template<>
inline JSValueRef jsc::Value::from_null(JSContextRef ctx) {
return JSValueMakeNull(ctx);
}
template<>
inline JSValueRef jsc::Value::from_number(JSContextRef ctx, double number) {
return JSValueMakeNumber(ctx, number);
}
template<>
inline JSValueRef jsc::Value::from_nonnull_string(JSContextRef ctx, const jsc::String &string) {
return JSValueMakeString(ctx, string);
}
template<>
inline JSValueRef jsc::Value::from_undefined(JSContextRef ctx) {
return JSValueMakeUndefined(ctx);
}
template<>
JSValueRef jsc::Value::from_nonnull_binary(JSContextRef ctx, BinaryData data);
template<>
inline bool jsc::Value::to_boolean(JSContextRef ctx, const JSValueRef &value) {
return JSValueToBoolean(ctx, value);
}
template<>
inline jsc::String jsc::Value::to_string(JSContextRef ctx, const JSValueRef &value) {
JSValueRef exception = nullptr;
jsc::String string = JSValueToStringCopy(ctx, value, &exception);
// Since the string's retain value is +2 here, we need to manually release it before returning.
JSStringRelease(string);
if (exception) {
throw jsc::Exception(ctx, exception);
}
return string;
}
template<>
inline double jsc::Value::to_number(JSContextRef ctx, const JSValueRef &value) {
JSValueRef exception = nullptr;
double number = JSValueToNumber(ctx, value, &exception);
if (exception) {
throw jsc::Exception(ctx, exception);
}
if (isnan(number)) {
throw std::invalid_argument(util::format("Value '%1' not convertible to a number.",
(std::string)to_string(ctx, value)));
}
return number;
}
template<>
inline JSObjectRef jsc::Value::to_object(JSContextRef ctx, const JSValueRef &value) {
JSValueRef exception = nullptr;
JSObjectRef object = JSValueToObject(ctx, value, &exception);
if (exception) {
throw jsc::Exception(ctx, exception);
}
return object;
}
template<>
inline JSObjectRef jsc::Value::to_array(JSContextRef ctx, const JSValueRef &value) {
return to_object(ctx, value);
}
template<>
inline JSObjectRef jsc::Value::to_constructor(JSContextRef ctx, const JSValueRef &value) {
return to_object(ctx, value);
}
template<>
inline JSObjectRef jsc::Value::to_date(JSContextRef ctx, const JSValueRef &value) {
if (JSValueIsString(ctx, value)) {
JSValueRef error;
std::array<JSValueRef, 1> args { value };
if (JSObjectRef result = JSObjectMakeDate(ctx, args.size(), args.data(), &error)) {
return result;
} else {
throw jsc::Exception(ctx, error);
}
}
return to_object(ctx, value);
}
template<>
inline JSObjectRef jsc::Value::to_function(JSContextRef ctx, const JSValueRef &value) {
return to_object(ctx, value);
}
template<>
OwnedBinaryData jsc::Value::to_binary(JSContextRef ctx, JSValueRef value);
} // js
} // realm