docs
Loading...
Searching...
No Matches
JSValueRef.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2006-2019 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#ifndef JSValueRef_h
27#define JSValueRef_h
28
31
32#ifndef __cplusplus
33#include <stdbool.h>
34#endif
35
36/*!
37@enum JSType
38@abstract A constant identifying the type of a JSValue.
39@constant kJSTypeUndefined The unique undefined value.
40@constant kJSTypeNull The unique null value.
41@constant kJSTypeBoolean A primitive boolean value, one of true or false.
42@constant kJSTypeNumber A primitive number value.
43@constant kJSTypeString A primitive string value.
44@constant kJSTypeObject An object value (meaning that this JSValueRef is a JSObjectRef).
45@constant kJSTypeSymbol A primitive symbol value.
46@constant kJSTypeBigInt A primitive BigInt value.
47*/
48typedef enum {
55 kJSTypeSymbol JSC_API_AVAILABLE(macos(10.15), ios(13.0)),
56 kJSTypeBigInt JSC_API_AVAILABLE(macos(JSC_MAC_TBA), ios(JSC_IOS_TBA))
57} JSType;
58
59/*!
60 @enum JSTypedArrayType
61 @abstract A constant identifying the Typed Array type of a JSObjectRef.
62 @constant kJSTypedArrayTypeInt8Array Int8Array
63 @constant kJSTypedArrayTypeInt16Array Int16Array
64 @constant kJSTypedArrayTypeInt32Array Int32Array
65 @constant kJSTypedArrayTypeUint8Array Uint8Array
66 @constant kJSTypedArrayTypeUint8ClampedArray Uint8ClampedArray
67 @constant kJSTypedArrayTypeUint16Array Uint16Array
68 @constant kJSTypedArrayTypeUint32Array Uint32Array
69 @constant kJSTypedArrayTypeFloat32Array Float32Array
70 @constant kJSTypedArrayTypeFloat64Array Float64Array
71 @constant kJSTypedArrayTypeBigInt64Array BigInt64Array
72 @constant kJSTypedArrayTypeBigUint64Array BigUint64Array
73 @constant kJSTypedArrayTypeArrayBuffer ArrayBuffer
74 @constant kJSTypedArrayTypeNone Not a Typed Array
75
76 */
92
93#ifdef __cplusplus
94extern "C" {
95#endif
96
97/*!
98@function
99@abstract Returns a JavaScript value's type.
100@param ctx The execution context to use.
101@param value The JSValue whose type you want to obtain.
102@result A value of type JSType that identifies value's type.
103*/
105
106/*!
107@function
108@abstract Tests whether a JavaScript value's type is the undefined type.
109@param ctx The execution context to use.
110@param value The JSValue to test.
111@result true if value's type is the undefined type, otherwise false.
112*/
114
115/*!
116@function
117@abstract Tests whether a JavaScript value's type is the null type.
118@param ctx The execution context to use.
119@param value The JSValue to test.
120@result true if value's type is the null type, otherwise false.
121*/
123
124/*!
125@function
126@abstract Tests whether a JavaScript value's type is the boolean type.
127@param ctx The execution context to use.
128@param value The JSValue to test.
129@result true if value's type is the boolean type, otherwise false.
130*/
132
133/*!
134@function
135@abstract Tests whether a JavaScript value's type is the number type.
136@param ctx The execution context to use.
137@param value The JSValue to test.
138@result true if value's type is the number type, otherwise false.
139*/
141
142/*!
143@function
144@abstract Tests whether a JavaScript value's type is the string type.
145@param ctx The execution context to use.
146@param value The JSValue to test.
147@result true if value's type is the string type, otherwise false.
148*/
150
151/*!
152@function
153@abstract Tests whether a JavaScript value's type is the symbol type.
154@param ctx The execution context to use.
155@param value The JSValue to test.
156@result true if value's type is the symbol type, otherwise false.
157*/
159
160/*!
161@function
162@abstract Tests whether a JavaScript value's type is the object type.
163@param ctx The execution context to use.
164@param value The JSValue to test.
165@result true if value's type is the object type, otherwise false.
166*/
168
169
170/*!
171@function
172@abstract Tests whether a JavaScript value is an object with a given class in its class chain.
173@param ctx The execution context to use.
174@param value The JSValue to test.
175@param jsClass The JSClass to test against.
176@result true if value is an object and has jsClass in its class chain, otherwise false.
177*/
179
180/*!
181@function
182@abstract Tests whether a JavaScript value is an array.
183@param ctx The execution context to use.
184@param value The JSValue to test.
185@result true if value is an array, otherwise false.
186*/
188
189/*!
190@function
191@abstract Tests whether a JavaScript value is a date.
192@param ctx The execution context to use.
193@param value The JSValue to test.
194@result true if value is a date, otherwise false.
195*/
197
198/*!
199@function
200@abstract Returns a JavaScript value's Typed Array type.
201@param ctx The execution context to use.
202@param value The JSValue whose Typed Array type to return.
203@param exception A pointer to a JSValueRef in which to store an exception, if any. Pass NULL if you do not care to store an exception.
204@result A value of type JSTypedArrayType that identifies value's Typed Array type, or kJSTypedArrayTypeNone if the value is not a Typed Array object.
205 */
207
208/* Comparing values */
209
210/*!
211@function
212@abstract Tests whether two JavaScript values are equal, as compared by the JS == operator.
213@param ctx The execution context to use.
214@param a The first value to test.
215@param b The second value to test.
216@param exception A pointer to a JSValueRef in which to store an exception, if any. Pass NULL if you do not care to store an exception.
217@result true if the two values are equal, false if they are not equal or an exception is thrown.
218*/
220
221/*!
222@function
223@abstract Tests whether two JavaScript values are strict equal, as compared by the JS === operator.
224@param ctx The execution context to use.
225@param a The first value to test.
226@param b The second value to test.
227@result true if the two values are strict equal, otherwise false.
228*/
230
231/*!
232@function
233@abstract Tests whether a JavaScript value is an object constructed by a given constructor, as compared by the JS instanceof operator.
234@param ctx The execution context to use.
235@param value The JSValue to test.
236@param constructor The constructor to test against.
237@param exception A pointer to a JSValueRef in which to store an exception, if any. Pass NULL if you do not care to store an exception.
238@result true if value is an object constructed by constructor, as compared by the JS instanceof operator, otherwise false.
239*/
241
242/* Creating values */
243
244/*!
245@function
246@abstract Creates a JavaScript value of the undefined type.
247@param ctx The execution context to use.
248@result The unique undefined value.
249*/
251
252/*!
253@function
254@abstract Creates a JavaScript value of the null type.
255@param ctx The execution context to use.
256@result The unique null value.
257*/
259
260/*!
261@function
262@abstract Creates a JavaScript value of the boolean type.
263@param ctx The execution context to use.
264@param boolean The bool to assign to the newly created JSValue.
265@result A JSValue of the boolean type, representing the value of boolean.
266*/
268
269/*!
270@function
271@abstract Creates a JavaScript value of the number type.
272@param ctx The execution context to use.
273@param number The double to assign to the newly created JSValue.
274@result A JSValue of the number type, representing the value of number.
275*/
277
278/*!
279@function
280@abstract Creates a JavaScript value of the string type.
281@param ctx The execution context to use.
282@param string The JSString to assign to the newly created JSValue. The
283 newly created JSValue retains string, and releases it upon garbage collection.
284@result A JSValue of the string type, representing the value of string.
285*/
287
288/*!
289 @function
290 @abstract Creates a JavaScript value of the symbol type.
291 @param ctx The execution context to use.
292 @param description A description of the newly created symbol value.
293 @result A unique JSValue of the symbol type, whose description matches the one provided.
294 */
296
297/* Converting to and from JSON formatted strings */
298
299/*!
300 @function
301 @abstract Creates a JavaScript value from a JSON formatted string.
302 @param ctx The execution context to use.
303 @param string The JSString containing the JSON string to be parsed.
304 @result A JSValue containing the parsed value, or NULL if the input is invalid.
305 */
307
308/*!
309 @function
310 @abstract Creates a JavaScript string containing the JSON serialized representation of a JS value.
311 @param ctx The execution context to use.
312 @param value The value to serialize.
313 @param indent The number of spaces to indent when nesting. If 0, the resulting JSON will not contains newlines. The size of the indent is clamped to 10 spaces.
314 @param exception A pointer to a JSValueRef in which to store an exception, if any. Pass NULL if you do not care to store an exception.
315 @result A JSString with the result of serialization, or NULL if an exception is thrown.
316 */
317JS_EXPORT JSStringRef JSValueCreateJSONString(JSContextRef ctx, JSValueRef value, unsigned indent, JSValueRef* exception) JSC_API_AVAILABLE(macos(10.7), ios(7.0));
318
319/* Converting to primitive values */
320
321/*!
322@function
323@abstract Converts a JavaScript value to boolean and returns the resulting boolean.
324@param ctx The execution context to use.
325@param value The JSValue to convert.
326@result The boolean result of conversion.
327*/
329
330/*!
331@function
332@abstract Converts a JavaScript value to number and returns the resulting number.
333@param ctx The execution context to use.
334@param value The JSValue to convert.
335@param exception A pointer to a JSValueRef in which to store an exception, if any. Pass NULL if you do not care to store an exception.
336@result The numeric result of conversion, or NaN if an exception is thrown.
337*/
339
340/*!
341@function
342@abstract Converts a JavaScript value to string and copies the result into a JavaScript string.
343@param ctx The execution context to use.
344@param value The JSValue to convert.
345@param exception A pointer to a JSValueRef in which to store an exception, if any. Pass NULL if you do not care to store an exception.
346@result A JSString with the result of conversion, or NULL if an exception is thrown. Ownership follows the Create Rule.
347*/
349
350/*!
351@function
352@abstract Converts a JavaScript value to object and returns the resulting object.
353@param ctx The execution context to use.
354@param value The JSValue to convert.
355@param exception A pointer to a JSValueRef in which to store an exception, if any. Pass NULL if you do not care to store an exception.
356@result The JSObject result of conversion, or NULL if an exception is thrown.
357*/
359
360/* Garbage collection */
361/*!
362@function
363@abstract Protects a JavaScript value from garbage collection.
364@param ctx The execution context to use.
365@param value The JSValue to protect.
366@discussion Use this method when you want to store a JSValue in a global or on the heap, where the garbage collector will not be able to discover your reference to it.
367
368A value may be protected multiple times and must be unprotected an equal number of times before becoming eligible for garbage collection.
369*/
371
372/*!
373@function
374@abstract Unprotects a JavaScript value from garbage collection.
375@param ctx The execution context to use.
376@param value The JSValue to unprotect.
377@discussion A value may be protected multiple times and must be unprotected an
378 equal number of times before becoming eligible for garbage collection.
379*/
381
382/*!
383@enum JSTypedErrorType
384@abstract A constant identifying the type of a JavaScript error object.
385@constant kJSTypedError_Error A plain Error, or an error type with no dedicated constant.
386@constant kJSTypedError_TypeError A TypeError.
387@constant kJSTypedError_RangeError A RangeError.
388@constant kJSTypedError_SyntaxError A SyntaxError.
389@constant kJSTypedError_ReferenceError A ReferenceError.
390@constant kJSTypedError_EvalError An EvalError.
391*/
400
401/*!
402@function
403@abstract Extracts the type, message, source location, and stack trace of an error value in one call.
404@param ctx The execution context to use.
405@param value The JSValue to inspect, typically an exception returned by another function.
406@param outType Receives the error's JSTypedErrorType. Error types with no dedicated constant report kJSTypedError_Error. May be NULL.
407@param outMessage Receives the error's message as a JSString, or NULL if it has none. May be NULL.
408@param outSourceURL Receives the URL of the script that created the error as a JSString, or NULL if unknown. May be NULL.
409@param outLine Receives the 1-based line number the error originated at, or 0 if unknown. May be NULL.
410@param outColumn Receives the 1-based column number the error originated at, or 0 if unknown. May be NULL.
411@param outStack Receives the error's stack trace as a JSString, or NULL if it has none. May be NULL.
412@result true if details were extracted; false if ctx or value is NULL.
413@discussion Values that are not error objects (for example, a thrown string) report
414 kJSTypedError_Error with the value's string representation as the message and no source location.
415 Reading the details may materialize the error's lazily computed own properties, the same way a
416 script reading its stack property would. Each returned JSString is owned by the caller, who is
417 responsible for releasing it with JSStringRelease.
418*/
419JS_EXPORT bool JSValueGetErrorDetails(JSContextRef ctx, JSValueRef value, JSTypedErrorType* outType, JSStringRef* outMessage, JSStringRef* outSourceURL, unsigned* outLine, unsigned* outColumn, JSStringRef* outStack);
420
421/*!
422@function
423@abstract Tests whether a JavaScript value is an actual function object.
424@param ctx The execution context to use.
425@param value The JSValue to test.
426@result true if the value is a function object: an ordinary JavaScript function (including
427 arrow, async, generator, and bound functions and class constructors), a built-in function,
428 or a function created through the C API. false for values that are merely callable without
429 being functions, such as a Proxy whose target is callable; use JSObjectIsFunction to test
430 callability.
431*/
433
434#ifdef __cplusplus
435}
436#endif
437
438#endif /* JSValueRef_h */
struct OpaqueJSClass * JSClassRef
Definition JSBase.h:52
struct OpaqueJSString * JSStringRef
Definition JSBase.h:49
struct OpaqueJSValue * JSObjectRef
Definition JSBase.h:69
const struct OpaqueJSValue * JSValueRef
Definition JSBase.h:66
#define JS_EXPORT
Definition JSBase.h:93
const struct OpaqueJSContext * JSContextRef
Definition JSBase.h:43
JSValueRef JSValueMakeFromJSONString(JSContextRef ctx, JSStringRef string) JSC_API_AVAILABLE(macos(10.7)
Creates a JavaScript value from a JSON formatted string.
enum JSTypedArrayType ios
JSTypedArrayType JSValueGetTypedArrayType(JSContextRef ctx, JSValueRef value, JSValueRef *exception) JSC_API_AVAILABLE(macos(10.12)
Returns a JavaScript value's Typed Array type.
bool JSValueIsNumber(JSContextRef ctx, JSValueRef value)
Tests whether a JavaScript value's type is the number type.
bool JSValueIsFunctionObject(JSContextRef ctx, JSValueRef value)
Tests whether a JavaScript value is an actual function object.
JSValueRef JSValueMakeNull(JSContextRef ctx)
Creates a JavaScript value of the null type.
JSValueRef JSValueMakeSymbol(JSContextRef ctx, JSStringRef description) JSC_API_AVAILABLE(macos(10.15)
Creates a JavaScript value of the symbol type.
bool JSValueGetErrorDetails(JSContextRef ctx, JSValueRef value, JSTypedErrorType *outType, JSStringRef *outMessage, JSStringRef *outSourceURL, unsigned *outLine, unsigned *outColumn, JSStringRef *outStack)
Extracts the type, message, source location, and stack trace of an error value in one call.
double JSValueToNumber(JSContextRef ctx, JSValueRef value, JSValueRef *exception)
Converts a JavaScript value to number and returns the resulting number.
bool JSValueIsSymbol(JSContextRef ctx, JSValueRef value) JSC_API_AVAILABLE(macos(10.15)
Tests whether a JavaScript value's type is the symbol type.
bool JSValueIsNull(JSContextRef ctx, JSValueRef value)
Tests whether a JavaScript value's type is the null type.
JSStringRef JSValueCreateJSONString(JSContextRef ctx, JSValueRef value, unsigned indent, JSValueRef *exception) JSC_API_AVAILABLE(macos(10.7)
Creates a JavaScript string containing the JSON serialized representation of a JS value.
JSValueRef JSValueMakeUndefined(JSContextRef ctx)
Creates a JavaScript value of the undefined type.
void JSValueUnprotect(JSContextRef ctx, JSValueRef value)
Unprotects a JavaScript value from garbage collection.
bool JSValueIsObject(JSContextRef ctx, JSValueRef value)
Tests whether a JavaScript value's type is the object type.
bool JSValueIsInstanceOfConstructor(JSContextRef ctx, JSValueRef value, JSObjectRef constructor, JSValueRef *exception)
Tests whether a JavaScript value is an object constructed by a given constructor, as compared by the ...
bool JSValueIsArray(JSContextRef ctx, JSValueRef value) JSC_API_AVAILABLE(macos(10.11)
Tests whether a JavaScript value is an array.
bool JSValueIsUndefined(JSContextRef ctx, JSValueRef value)
Tests whether a JavaScript value's type is the undefined type.
JSValueRef JSValueMakeNumber(JSContextRef ctx, double number)
Creates a JavaScript value of the number type.
bool JSValueIsDate(JSContextRef ctx, JSValueRef value) JSC_API_AVAILABLE(macos(10.11)
Tests whether a JavaScript value is a date.
bool JSValueIsString(JSContextRef ctx, JSValueRef value)
Tests whether a JavaScript value's type is the string type.
bool JSValueIsObjectOfClass(JSContextRef ctx, JSValueRef value, JSClassRef jsClass)
Tests whether a JavaScript value is an object with a given class in its class chain.
bool JSValueIsStrictEqual(JSContextRef ctx, JSValueRef a, JSValueRef b)
Tests whether two JavaScript values are strict equal, as compared by the JS === operator.
JSObjectRef JSValueToObject(JSContextRef ctx, JSValueRef value, JSValueRef *exception)
Converts a JavaScript value to object and returns the resulting object.
JSType
A constant identifying the type of a JSValue.
Definition JSValueRef.h:48
@ kJSTypeNull
Definition JSValueRef.h:50
@ kJSTypeString
Definition JSValueRef.h:53
@ kJSTypeBoolean
Definition JSValueRef.h:51
@ kJSTypeObject
Definition JSValueRef.h:54
@ kJSTypeNumber
Definition JSValueRef.h:52
@ kJSTypeUndefined
Definition JSValueRef.h:49
JSTypedErrorType
A constant identifying the type of a JavaScript error object.
Definition JSValueRef.h:392
@ kJSTypedError_ReferenceError
Definition JSValueRef.h:397
@ kJSTypedError_TypeError
Definition JSValueRef.h:394
@ kJSTypedError_Error
Definition JSValueRef.h:393
@ kJSTypedError_EvalError
Definition JSValueRef.h:398
@ kJSTypedError_SyntaxError
Definition JSValueRef.h:396
@ kJSTypedError_RangeError
Definition JSValueRef.h:395
JSStringRef JSValueToStringCopy(JSContextRef ctx, JSValueRef value, JSValueRef *exception)
Converts a JavaScript value to string and copies the result into a JavaScript string.
bool JSValueIsEqual(JSContextRef ctx, JSValueRef a, JSValueRef b, JSValueRef *exception)
Tests whether two JavaScript values are equal, as compared by the JS == operator.
JSValueRef JSValueMakeString(JSContextRef ctx, JSStringRef string)
Creates a JavaScript value of the string type.
bool JSValueToBoolean(JSContextRef ctx, JSValueRef value)
Converts a JavaScript value to boolean and returns the resulting boolean.
bool JSValueIsBoolean(JSContextRef ctx, JSValueRef value)
Tests whether a JavaScript value's type is the boolean type.
JSTypedArrayType
A constant identifying the Typed Array type of a JSObjectRef.
Definition JSValueRef.h:77
@ kJSTypedArrayTypeUint16Array
Definition JSValueRef.h:83
@ kJSTypedArrayTypeInt32Array
Definition JSValueRef.h:80
@ kJSTypedArrayTypeInt16Array
Definition JSValueRef.h:79
@ kJSTypedArrayTypeNone
Definition JSValueRef.h:88
@ kJSTypedArrayTypeInt8Array
Definition JSValueRef.h:78
@ kJSTypedArrayTypeArrayBuffer
Definition JSValueRef.h:87
@ kJSTypedArrayTypeUint32Array
Definition JSValueRef.h:84
@ kJSTypedArrayTypeBigUint64Array
Definition JSValueRef.h:90
@ kJSTypedArrayTypeUint8ClampedArray
Definition JSValueRef.h:82
@ kJSTypedArrayTypeBigInt64Array
Definition JSValueRef.h:89
@ kJSTypedArrayTypeFloat32Array
Definition JSValueRef.h:85
@ kJSTypedArrayTypeUint8Array
Definition JSValueRef.h:81
@ kJSTypedArrayTypeFloat64Array
Definition JSValueRef.h:86
void JSValueProtect(JSContextRef ctx, JSValueRef value)
Protects a JavaScript value from garbage collection.
JSType JSValueGetType(JSContextRef ctx, JSValueRef value)
Returns a JavaScript value's type.
JSValueRef JSValueMakeBoolean(JSContextRef ctx, bool boolean)
Creates a JavaScript value of the boolean type.
#define JSC_API_AVAILABLE(...)
Definition WebKitAvailability.h:46