-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathJSValue.hpp
453 lines (336 loc) · 12.4 KB
/
JSValue.hpp
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
/**
* HAL
*
* Copyright (c) 2014 by Appcelerator, Inc. All Rights Reserved.
* Licensed under the terms of the Apache Public License.
* Please see the LICENSE included with this distribution for details.
*/
#ifndef _HAL_JSVALUE_HPP_
#define _HAL_JSVALUE_HPP_
#include "HAL/detail/JSBase.hpp"
#include "HAL/JSContext.hpp"
#include <vector>
#include <ostream>
namespace HAL {
class JSString;
class JSValue;
class JSBoolean;
class JSNumber;
class JSObject;
class JSFunction;
class JSArray;
class JSDate;
class JSError;
class JSRegExp;
namespace detail {
template<typename T>
class JSExportClass;
HAL_EXPORT std::vector<JSValue> to_vector(const JSContext&, size_t, const JSValueRef[]);
HAL_EXPORT std::vector<JSValueRef> to_vector(const std::vector<JSValue>&);
}}
namespace HAL {
/*!
@class
@discussion A JSValue is an RAII wrapper around a JSValueRef, the
JavaScriptCore C API representation of a JavaScript value. This is
the base class for all JavaScript values. These are the direct
descendants of JSValue:
JSUndefined
JSNull
JSBoolean
JSNumber
The only way to create a JSValue is by using the
JSContext::CreateXXX member functions.
*/
class HAL_EXPORT JSValue HAL_PERFORMANCE_COUNTER1(JSValue) {
public:
/*!
@enum Type
@abstract An enum identifying the type of a JSValue.
@constant Undefined The unique undefined value.
@constant Null The unique null value.
@constant Boolean A primitive boolean value, one of true or false.
@constant Number A primitive number value.
@constant String A primitive string value.
@constant Object An object value (meaning that this JSValue is a JSObject).
*/
enum class Type {
Undefined,
Null,
Boolean,
Number,
String,
Object
};
/*!
@method
@abstract Return a JSString containing the JSON serialized
representation of this JavaScript value.
@param indent The number of spaces to indent when nesting. If 0
(the default), the resulting JSON will not contain newlines. The
size of the indent is clamped to 10 spaces.
@result A JSString containing the JSON serialized representation
of this JavaScript value.
*/
virtual JSString ToJSONString(unsigned indent = 0) final;
/*!
@method
@abstract Convert this JSValue to a JSString.
@result A JSString with the result of conversion.
*/
explicit operator JSString() const;
/*!
@method
@abstract Convert this JSValue to a std::string.
@result A std::string with the result of conversion.
*/
explicit operator std::string() const;
/*!
@method
@abstract Convert a JSValue to a boolean.
@result The boolean result of conversion.
*/
explicit operator bool() const HAL_NOEXCEPT;
/*!
@method
@abstract Convert a JSValue to a double.
@result The double result of conversion.
*/
explicit operator double() const;
/*!
@method
@abstract Convert a JSValue to an int32_t according to the rules
specified by the JavaScript language.
@result The int32_t result of conversion.
*/
explicit operator int32_t() const;
/*!
@method
@abstract Convert a JSValue to an uint32_t according to the rules
specified by the JavaScript language.
@discussion The JSValue is converted to an uint32_t according to
the rules specified by the JavaScript language (implements
ToUInt32, defined in ECMA-262 9.6).
@result The uint32_t result of the conversion.
*/
explicit operator uint32_t() const{
// As commented in the spec, the operation of ToInt32 and
// ToUint32 only differ in how the result is interpreted; see
// NOTEs in sections 9.5 and 9.6.
return operator int32_t();
}
/*!
@method
@abstract Convert this JSValue to a JSObject.
@result A JSObject with the result of conversion.
@throws std::runtime_error if this JSValue could not be converted
to a JSObject.
*/
explicit operator JSObject() const;
/*!
@method
@abstract Return this JavaScript value's type.
@result A value of type JSValue::Type that identifies this
JavaScript value's type.
*/
virtual Type GetType() const HAL_NOEXCEPT final;
/*!
@method
@abstract Determine whether this JavaScript value's type is the
undefined type.
@result true if this JavaScript value's type is the undefined
type.
*/
virtual bool IsUndefined() const HAL_NOEXCEPT final;
/*!
@method
@abstract Determine whether this JavaScript value's type is the
null type.
@result true if this JavaScript value's type is the null type.
*/
virtual bool IsNull() const HAL_NOEXCEPT final;
/*!
@method
@abstract Determine whether this JavaScript value's type is the
null type.
@result true if this JavaScript value's type is the null type.
*/
virtual bool IsNativeNull() const HAL_NOEXCEPT final;
/*!
@method
@abstract Determine whether this JavaScript value's type is the
boolean type.
@result true if this JavaScript value's type is the boolean type.
*/
virtual bool IsBoolean() const HAL_NOEXCEPT final;
/*!
@method
@abstract Determine whether this JavaScript value's type is the
number type.
@result true if this JavaScript value's type is the number type.
*/
virtual bool IsNumber() const HAL_NOEXCEPT final;
/*!
@method
@abstract Determine whether this JavaScript value's type is the
string type.
@result true if this JavaScript value's type is the string type.
*/
virtual bool IsString() const HAL_NOEXCEPT final;
/*!
@method
@abstract Determine whether this JavaScript value's type is the
object type.
@result true if this JavaScript value's type is the object type.
*/
virtual bool IsObject() const HAL_NOEXCEPT final;
/*!
@method
@abstract Determine whether this JavaScript's value is an object
with a given class in its class chain.
@param jsClass The JSClass to test against.
@result true if this JavaScript value is an object with a given
class in its class chain.
*/
virtual bool IsObjectOfClass(const JSClass& js_class) const HAL_NOEXCEPT final;
/*!
@method
@abstract Determine whether this JavaScript value was constructed
by the given constructor, as compared by the JavaScript
'instanceof' operator.
@param constructor The constructor to test against.
@result true if this JavaScript value was constructed by the
given constructor as compared by the JavaScript 'instanceof'
operator.
*/
virtual bool IsInstanceOfConstructor(const JSObject& constructor) const final;
/*!
@method
@abstract Determine whether this JavaScript value is equal to
another JavaScript by usong the JavaScript == operator.
@param js_value The JavaScript value to test.
@result true this JavaScript value is equal to another JavaScript
by usong the JavaScript == operator.
*/
virtual bool IsEqualWithTypeCoercion(const JSValue& js_value) const final;
/*!
@method
@abstract Return the execution context of this JavaScript value.
@result The the execution context of this JavaScript value.
*/
virtual JSContext get_context() const HAL_NOEXCEPT final {
return js_context__;
}
/*!
@method
@abstract Mark this value as native nullptr.
For interoperability with the JavaScriptCore C API.
*/
virtual void MarkAsNativeNull() HAL_NOEXCEPT final {
is_native_nullptr__ = true;
}
virtual ~JSValue() HAL_NOEXCEPT;
JSValue(const JSValue&) HAL_NOEXCEPT;
JSValue(JSValue&&) HAL_NOEXCEPT;
JSValue& operator=(JSValue);
void swap(JSValue&) HAL_NOEXCEPT;
protected:
// A JSContext can create a JSValue.
friend class JSContext;
JSValue(const JSContext& js_context, const JSString& js_string, bool parse_as_json = false);
// These classes create a JSValue using the following constructor.
// friend class JSUndefined;
// friend class JSNull;
// friend class JSBoolean;
// friend class JSNumber;
friend class JSArray; // for generating error messages
friend class JSDate; // for generating error messages
friend class JSFunction; // for generating error messages
friend class JSRegExp; // for generating error messages
friend class JSError; // for generating error messages
template<typename T>
friend class detail::JSExportClass;
// JSObject needs access to the JSValue constructor for
// GetPrototype() and for generating error messages, as well as
// operator JSValueRef() for SetPrototype().
friend class JSObject;
// For interoperability with the JavaScriptCore C API.
JSValue(const JSContext& js_context, JSValueRef js_value_ref) HAL_NOEXCEPT;
// For interoperability with the JavaScriptCore C API.
explicit operator JSValueRef() const HAL_NOEXCEPT {
if (is_native_nullptr__) {
return nullptr;
}
return js_value_ref__;
}
// These classes and functions need access to operator
// JSValueRef().
HAL_EXPORT friend bool operator==(const JSValue& lhs, const JSValue& rhs) HAL_NOEXCEPT;
HAL_EXPORT friend std::vector<JSValue> detail::to_vector(const JSContext&, size_t, const JSValueRef[]);
HAL_EXPORT friend std::vector<JSValueRef> detail::to_vector(const std::vector<JSValue>&);
private:
// Prevent heap based objects.
static void * operator new(std::size_t); // #1: To prevent allocation of scalar objects
static void * operator new [] (std::size_t); // #2: To prevent allocation of array of objects
JSContext js_context__;
bool is_native_nullptr__{false};
// Silence 4251 on Windows since private member variables do not
// need to be exported from a DLL.
#pragma warning(push)
#pragma warning(disable: 4251)
JSValueRef js_value_ref__ { nullptr };
#pragma warning(pop)
#undef HAL_JSVALUE_LOCK_GUARD
#ifdef HAL_THREAD_SAFE
std::recursive_mutex mutex__;
#define HAL_JSVALUE_LOCK_GUARD std::lock_guard<std::recursive_mutex> lock(mutex__)
#else
#define HAL_JSVALUE_LOCK_GUARD
#endif // HAL_THREAD_SAFE
};
inline
void swap(JSValue& first, JSValue& second) HAL_NOEXCEPT {
first.swap(second);
}
inline
std::string to_string(const JSValue& js_value) {
return static_cast<std::string>(js_value);
}
HAL_EXPORT std::string to_string(const JSValue::Type& js_value_type) HAL_NOEXCEPT;
/*!
@function
@abstract Determine whether two JavaScript values are strict equal,
as compared by the JS === operator.
@param lhs The first value to test.
@param rhs The second value to test.
@result true if the two values are equal, false if they are not
equal.
*/
HAL_EXPORT bool operator==(const JSValue& lhs, const JSValue& rhs) HAL_NOEXCEPT;
// Return true if the two JSValues are not strict equal, as compared by the JS === operator.
inline
bool operator!=(const JSValue& lhs, const JSValue& rhs) HAL_NOEXCEPT {
return ! (lhs == rhs);
}
/*!
@function
@abstract Determine whether two JavaScript values are equal, as
compared by the JS == operator.
@param lhs The first value to test.
@param rhs The second value to test.
@result true if the two values are equal, false if they are not
equal.
*/
HAL_EXPORT bool IsEqualWithTypeCoercion(const JSValue& lhs, const JSValue& rhs);
inline
std::ostream& operator << (std::ostream& ostream, const JSValue& js_value) {
ostream << to_string(js_value);
return ostream;
}
inline
std::ostream& operator << (std::ostream& ostream, const JSValue::Type& js_value_type) {
ostream << to_string(js_value_type);
return ostream;
}
} // namespace HAL {
#endif // _HAL_JSVALUE_HPP_