| 1 | /* |
| 2 | * Copyright (C) 2006 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 | |
| 29 | #include <JavaScriptCore/JSBase.h> |
| 30 | #include <JavaScriptCore/WebKitAvailability.h> |
| 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 | */ |
| 47 | typedef enum { |
| 48 | kJSTypeUndefined, |
| 49 | kJSTypeNull, |
| 50 | kJSTypeBoolean, |
| 51 | kJSTypeNumber, |
| 52 | kJSTypeString, |
| 53 | kJSTypeObject, |
| 54 | kJSTypeSymbol JSC_API_AVAILABLE(macos(JSC_MAC_TBA), ios(JSC_IOS_TBA)) |
| 55 | } JSType; |
| 56 | |
| 57 | /*! |
| 58 | @enum JSTypedArrayType |
| 59 | @abstract A constant identifying the Typed Array type of a JSObjectRef. |
| 60 | @constant kJSTypedArrayTypeInt8Array Int8Array |
| 61 | @constant kJSTypedArrayTypeInt16Array Int16Array |
| 62 | @constant kJSTypedArrayTypeInt32Array Int32Array |
| 63 | @constant kJSTypedArrayTypeUint8Array Uint8Array |
| 64 | @constant kJSTypedArrayTypeUint8ClampedArray Uint8ClampedArray |
| 65 | @constant kJSTypedArrayTypeUint16Array Uint16Array |
| 66 | @constant kJSTypedArrayTypeUint32Array Uint32Array |
| 67 | @constant kJSTypedArrayTypeFloat32Array Float32Array |
| 68 | @constant kJSTypedArrayTypeFloat64Array Float64Array |
| 69 | @constant kJSTypedArrayTypeArrayBuffer ArrayBuffer |
| 70 | @constant kJSTypedArrayTypeNone Not a Typed Array |
| 71 | |
| 72 | */ |
| 73 | typedef enum { |
| 74 | kJSTypedArrayTypeInt8Array, |
| 75 | kJSTypedArrayTypeInt16Array, |
| 76 | kJSTypedArrayTypeInt32Array, |
| 77 | kJSTypedArrayTypeUint8Array, |
| 78 | kJSTypedArrayTypeUint8ClampedArray, |
| 79 | kJSTypedArrayTypeUint16Array, |
| 80 | kJSTypedArrayTypeUint32Array, |
| 81 | kJSTypedArrayTypeFloat32Array, |
| 82 | kJSTypedArrayTypeFloat64Array, |
| 83 | kJSTypedArrayTypeArrayBuffer, |
| 84 | kJSTypedArrayTypeNone, |
| 85 | } JSTypedArrayType JSC_API_AVAILABLE(macos(10.12), ios(10.0)); |
| 86 | |
| 87 | #ifdef __cplusplus |
| 88 | extern "C" { |
| 89 | #endif |
| 90 | |
| 91 | /*! |
| 92 | @function |
| 93 | @abstract Returns a JavaScript value's type. |
| 94 | @param ctx The execution context to use. |
| 95 | @param value The JSValue whose type you want to obtain. |
| 96 | @result A value of type JSType that identifies value's type. |
| 97 | */ |
| 98 | JS_EXPORT JSType JSValueGetType(JSContextRef ctx, JSValueRef value); |
| 99 | |
| 100 | /*! |
| 101 | @function |
| 102 | @abstract Tests whether a JavaScript value's type is the undefined type. |
| 103 | @param ctx The execution context to use. |
| 104 | @param value The JSValue to test. |
| 105 | @result true if value's type is the undefined type, otherwise false. |
| 106 | */ |
| 107 | JS_EXPORT bool JSValueIsUndefined(JSContextRef ctx, JSValueRef value); |
| 108 | |
| 109 | /*! |
| 110 | @function |
| 111 | @abstract Tests whether a JavaScript value's type is the null type. |
| 112 | @param ctx The execution context to use. |
| 113 | @param value The JSValue to test. |
| 114 | @result true if value's type is the null type, otherwise false. |
| 115 | */ |
| 116 | JS_EXPORT bool JSValueIsNull(JSContextRef ctx, JSValueRef value); |
| 117 | |
| 118 | /*! |
| 119 | @function |
| 120 | @abstract Tests whether a JavaScript value's type is the boolean type. |
| 121 | @param ctx The execution context to use. |
| 122 | @param value The JSValue to test. |
| 123 | @result true if value's type is the boolean type, otherwise false. |
| 124 | */ |
| 125 | JS_EXPORT bool JSValueIsBoolean(JSContextRef ctx, JSValueRef value); |
| 126 | |
| 127 | /*! |
| 128 | @function |
| 129 | @abstract Tests whether a JavaScript value's type is the number type. |
| 130 | @param ctx The execution context to use. |
| 131 | @param value The JSValue to test. |
| 132 | @result true if value's type is the number type, otherwise false. |
| 133 | */ |
| 134 | JS_EXPORT bool JSValueIsNumber(JSContextRef ctx, JSValueRef value); |
| 135 | |
| 136 | /*! |
| 137 | @function |
| 138 | @abstract Tests whether a JavaScript value's type is the string type. |
| 139 | @param ctx The execution context to use. |
| 140 | @param value The JSValue to test. |
| 141 | @result true if value's type is the string type, otherwise false. |
| 142 | */ |
| 143 | JS_EXPORT bool JSValueIsString(JSContextRef ctx, JSValueRef value); |
| 144 | |
| 145 | /*! |
| 146 | @function |
| 147 | @abstract Tests whether a JavaScript value's type is the object type. |
| 148 | @param ctx The execution context to use. |
| 149 | @param value The JSValue to test. |
| 150 | @result true if value's type is the object type, otherwise false. |
| 151 | */ |
| 152 | JS_EXPORT bool JSValueIsObject(JSContextRef ctx, JSValueRef value); |
| 153 | |
| 154 | |
| 155 | /*! |
| 156 | @function |
| 157 | @abstract Tests whether a JavaScript value is an object with a given class in its class chain. |
| 158 | @param ctx The execution context to use. |
| 159 | @param value The JSValue to test. |
| 160 | @param jsClass The JSClass to test against. |
| 161 | @result true if value is an object and has jsClass in its class chain, otherwise false. |
| 162 | */ |
| 163 | JS_EXPORT bool JSValueIsObjectOfClass(JSContextRef ctx, JSValueRef value, JSClassRef jsClass); |
| 164 | |
| 165 | /*! |
| 166 | @function |
| 167 | @abstract Tests whether a JavaScript value is an array. |
| 168 | @param ctx The execution context to use. |
| 169 | @param value The JSValue to test. |
| 170 | @result true if value is an array, otherwise false. |
| 171 | */ |
| 172 | JS_EXPORT bool JSValueIsArray(JSContextRef ctx, JSValueRef value) JSC_API_AVAILABLE(macos(10.11), ios(9.0)); |
| 173 | |
| 174 | /*! |
| 175 | @function |
| 176 | @abstract Tests whether a JavaScript value is a date. |
| 177 | @param ctx The execution context to use. |
| 178 | @param value The JSValue to test. |
| 179 | @result true if value is a date, otherwise false. |
| 180 | */ |
| 181 | JS_EXPORT bool JSValueIsDate(JSContextRef ctx, JSValueRef value) JSC_API_AVAILABLE(macos(10.11), ios(9.0)); |
| 182 | |
| 183 | /*! |
| 184 | @function |
| 185 | @abstract Returns a JavaScript value's Typed Array type. |
| 186 | @param ctx The execution context to use. |
| 187 | @param value The JSValue whose Typed Array type to return. |
| 188 | @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. |
| 189 | @result A value of type JSTypedArrayType that identifies value's Typed Array type, or kJSTypedArrayTypeNone if the value is not a Typed Array object. |
| 190 | */ |
| 191 | JS_EXPORT JSTypedArrayType JSValueGetTypedArrayType(JSContextRef ctx, JSValueRef value, JSValueRef* exception) JSC_API_AVAILABLE(macos(10.12), ios(10.0)); |
| 192 | |
| 193 | /* Comparing values */ |
| 194 | |
| 195 | /*! |
| 196 | @function |
| 197 | @abstract Tests whether two JavaScript values are equal, as compared by the JS == operator. |
| 198 | @param ctx The execution context to use. |
| 199 | @param a The first value to test. |
| 200 | @param b The second value to test. |
| 201 | @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. |
| 202 | @result true if the two values are equal, false if they are not equal or an exception is thrown. |
| 203 | */ |
| 204 | JS_EXPORT bool JSValueIsEqual(JSContextRef ctx, JSValueRef a, JSValueRef b, JSValueRef* exception); |
| 205 | |
| 206 | /*! |
| 207 | @function |
| 208 | @abstract Tests whether two JavaScript values are strict equal, as compared by the JS === operator. |
| 209 | @param ctx The execution context to use. |
| 210 | @param a The first value to test. |
| 211 | @param b The second value to test. |
| 212 | @result true if the two values are strict equal, otherwise false. |
| 213 | */ |
| 214 | JS_EXPORT bool JSValueIsStrictEqual(JSContextRef ctx, JSValueRef a, JSValueRef b); |
| 215 | |
| 216 | /*! |
| 217 | @function |
| 218 | @abstract Tests whether a JavaScript value is an object constructed by a given constructor, as compared by the JS instanceof operator. |
| 219 | @param ctx The execution context to use. |
| 220 | @param value The JSValue to test. |
| 221 | @param constructor The constructor to test against. |
| 222 | @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. |
| 223 | @result true if value is an object constructed by constructor, as compared by the JS instanceof operator, otherwise false. |
| 224 | */ |
| 225 | JS_EXPORT bool JSValueIsInstanceOfConstructor(JSContextRef ctx, JSValueRef value, JSObjectRef constructor, JSValueRef* exception); |
| 226 | |
| 227 | /* Creating values */ |
| 228 | |
| 229 | /*! |
| 230 | @function |
| 231 | @abstract Creates a JavaScript value of the undefined type. |
| 232 | @param ctx The execution context to use. |
| 233 | @result The unique undefined value. |
| 234 | */ |
| 235 | JS_EXPORT JSValueRef JSValueMakeUndefined(JSContextRef ctx); |
| 236 | |
| 237 | /*! |
| 238 | @function |
| 239 | @abstract Creates a JavaScript value of the null type. |
| 240 | @param ctx The execution context to use. |
| 241 | @result The unique null value. |
| 242 | */ |
| 243 | JS_EXPORT JSValueRef JSValueMakeNull(JSContextRef ctx); |
| 244 | |
| 245 | /*! |
| 246 | @function |
| 247 | @abstract Creates a JavaScript value of the boolean type. |
| 248 | @param ctx The execution context to use. |
| 249 | @param boolean The bool to assign to the newly created JSValue. |
| 250 | @result A JSValue of the boolean type, representing the value of boolean. |
| 251 | */ |
| 252 | JS_EXPORT JSValueRef JSValueMakeBoolean(JSContextRef ctx, bool boolean); |
| 253 | |
| 254 | /*! |
| 255 | @function |
| 256 | @abstract Creates a JavaScript value of the number type. |
| 257 | @param ctx The execution context to use. |
| 258 | @param number The double to assign to the newly created JSValue. |
| 259 | @result A JSValue of the number type, representing the value of number. |
| 260 | */ |
| 261 | JS_EXPORT JSValueRef JSValueMakeNumber(JSContextRef ctx, double number); |
| 262 | |
| 263 | /*! |
| 264 | @function |
| 265 | @abstract Creates a JavaScript value of the string type. |
| 266 | @param ctx The execution context to use. |
| 267 | @param string The JSString to assign to the newly created JSValue. The |
| 268 | newly created JSValue retains string, and releases it upon garbage collection. |
| 269 | @result A JSValue of the string type, representing the value of string. |
| 270 | */ |
| 271 | JS_EXPORT JSValueRef JSValueMakeString(JSContextRef ctx, JSStringRef string); |
| 272 | |
| 273 | /* Converting to and from JSON formatted strings */ |
| 274 | |
| 275 | /*! |
| 276 | @function |
| 277 | @abstract Creates a JavaScript value from a JSON formatted string. |
| 278 | @param ctx The execution context to use. |
| 279 | @param string The JSString containing the JSON string to be parsed. |
| 280 | @result A JSValue containing the parsed value, or NULL if the input is invalid. |
| 281 | */ |
| 282 | JS_EXPORT JSValueRef JSValueMakeFromJSONString(JSContextRef ctx, JSStringRef string) JSC_API_AVAILABLE(macos(10.7), ios(7.0)); |
| 283 | |
| 284 | /*! |
| 285 | @function |
| 286 | @abstract Creates a JavaScript string containing the JSON serialized representation of a JS value. |
| 287 | @param ctx The execution context to use. |
| 288 | @param value The value to serialize. |
| 289 | @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. |
| 290 | @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. |
| 291 | @result A JSString with the result of serialization, or NULL if an exception is thrown. |
| 292 | */ |
| 293 | JS_EXPORT JSStringRef JSValueCreateJSONString(JSContextRef ctx, JSValueRef value, unsigned indent, JSValueRef* exception) JSC_API_AVAILABLE(macos(10.7), ios(7.0)); |
| 294 | |
| 295 | /* Converting to primitive values */ |
| 296 | |
| 297 | /*! |
| 298 | @function |
| 299 | @abstract Converts a JavaScript value to boolean and returns the resulting boolean. |
| 300 | @param ctx The execution context to use. |
| 301 | @param value The JSValue to convert. |
| 302 | @result The boolean result of conversion. |
| 303 | */ |
| 304 | JS_EXPORT bool JSValueToBoolean(JSContextRef ctx, JSValueRef value); |
| 305 | |
| 306 | /*! |
| 307 | @function |
| 308 | @abstract Converts a JavaScript value to number and returns the resulting number. |
| 309 | @param ctx The execution context to use. |
| 310 | @param value The JSValue to convert. |
| 311 | @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. |
| 312 | @result The numeric result of conversion, or NaN if an exception is thrown. |
| 313 | */ |
| 314 | JS_EXPORT double JSValueToNumber(JSContextRef ctx, JSValueRef value, JSValueRef* exception); |
| 315 | |
| 316 | /*! |
| 317 | @function |
| 318 | @abstract Converts a JavaScript value to string and copies the result into a JavaScript string. |
| 319 | @param ctx The execution context to use. |
| 320 | @param value The JSValue to convert. |
| 321 | @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. |
| 322 | @result A JSString with the result of conversion, or NULL if an exception is thrown. Ownership follows the Create Rule. |
| 323 | */ |
| 324 | JS_EXPORT JSStringRef JSValueToStringCopy(JSContextRef ctx, JSValueRef value, JSValueRef* exception); |
| 325 | |
| 326 | /*! |
| 327 | @function |
| 328 | @abstract Converts a JavaScript value to object and returns the resulting object. |
| 329 | @param ctx The execution context to use. |
| 330 | @param value The JSValue to convert. |
| 331 | @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. |
| 332 | @result The JSObject result of conversion, or NULL if an exception is thrown. |
| 333 | */ |
| 334 | JS_EXPORT JSObjectRef JSValueToObject(JSContextRef ctx, JSValueRef value, JSValueRef* exception); |
| 335 | |
| 336 | /* Garbage collection */ |
| 337 | /*! |
| 338 | @function |
| 339 | @abstract Protects a JavaScript value from garbage collection. |
| 340 | @param ctx The execution context to use. |
| 341 | @param value The JSValue to protect. |
| 342 | @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. |
| 343 | |
| 344 | A value may be protected multiple times and must be unprotected an equal number of times before becoming eligible for garbage collection. |
| 345 | */ |
| 346 | JS_EXPORT void JSValueProtect(JSContextRef ctx, JSValueRef value); |
| 347 | |
| 348 | /*! |
| 349 | @function |
| 350 | @abstract Unprotects a JavaScript value from garbage collection. |
| 351 | @param ctx The execution context to use. |
| 352 | @param value The JSValue to unprotect. |
| 353 | @discussion A value may be protected multiple times and must be unprotected an |
| 354 | equal number of times before becoming eligible for garbage collection. |
| 355 | */ |
| 356 | JS_EXPORT void JSValueUnprotect(JSContextRef ctx, JSValueRef value); |
| 357 | |
| 358 | #ifdef __cplusplus |
| 359 | } |
| 360 | #endif |
| 361 | |
| 362 | #endif /* JSValueRef_h */ |
| 363 | |