2 * Copyright (C) 2006 Apple Computer, Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, 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 COMPUTER, 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.
29 #include <JavaScriptCore/JSBase.h>
30 #include <JavaScriptCore/WebKitAvailability.h>
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).
61 @abstract Returns a JavaScript value's type.
62 @param ctx The execution context to use.
63 @param value The JSValue whose type you want to obtain.
64 @result A value of type JSType that identifies value's type.
66 JS_EXPORT JSType
JSValueGetType(JSContextRef ctx
, JSValueRef value
);
70 @abstract Tests whether a JavaScript value's type is the undefined type.
71 @param ctx The execution context to use.
72 @param value The JSValue to test.
73 @result true if value's type is the undefined type, otherwise false.
75 JS_EXPORT
bool JSValueIsUndefined(JSContextRef ctx
, JSValueRef value
);
79 @abstract Tests whether a JavaScript value's type is the null type.
80 @param ctx The execution context to use.
81 @param value The JSValue to test.
82 @result true if value's type is the null type, otherwise false.
84 JS_EXPORT
bool JSValueIsNull(JSContextRef ctx
, JSValueRef value
);
88 @abstract Tests whether a JavaScript value's type is the boolean type.
89 @param ctx The execution context to use.
90 @param value The JSValue to test.
91 @result true if value's type is the boolean type, otherwise false.
93 JS_EXPORT
bool JSValueIsBoolean(JSContextRef ctx
, JSValueRef value
);
97 @abstract Tests whether a JavaScript value's type is the number type.
98 @param ctx The execution context to use.
99 @param value The JSValue to test.
100 @result true if value's type is the number type, otherwise false.
102 JS_EXPORT
bool JSValueIsNumber(JSContextRef ctx
, JSValueRef value
);
106 @abstract Tests whether a JavaScript value's type is the string type.
107 @param ctx The execution context to use.
108 @param value The JSValue to test.
109 @result true if value's type is the string type, otherwise false.
111 JS_EXPORT
bool JSValueIsString(JSContextRef ctx
, JSValueRef value
);
115 @abstract Tests whether a JavaScript value's type is the object type.
116 @param ctx The execution context to use.
117 @param value The JSValue to test.
118 @result true if value's type is the object type, otherwise false.
120 JS_EXPORT
bool JSValueIsObject(JSContextRef ctx
, JSValueRef value
);
124 @abstract Tests whether a JavaScript value is an object with a given class in its class chain.
125 @param ctx The execution context to use.
126 @param value The JSValue to test.
127 @param jsClass The JSClass to test against.
128 @result true if value is an object and has jsClass in its class chain, otherwise false.
130 JS_EXPORT
bool JSValueIsObjectOfClass(JSContextRef ctx
, JSValueRef value
, JSClassRef jsClass
);
132 /* Comparing values */
136 @abstract Tests whether two JavaScript values are equal, as compared by the JS == operator.
137 @param ctx The execution context to use.
138 @param a The first value to test.
139 @param b The second value to test.
140 @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.
141 @result true if the two values are equal, false if they are not equal or an exception is thrown.
143 JS_EXPORT
bool JSValueIsEqual(JSContextRef ctx
, JSValueRef a
, JSValueRef b
, JSValueRef
* exception
);
147 @abstract Tests whether two JavaScript values are strict equal, as compared by the JS === operator.
148 @param ctx The execution context to use.
149 @param a The first value to test.
150 @param b The second value to test.
151 @result true if the two values are strict equal, otherwise false.
153 JS_EXPORT
bool JSValueIsStrictEqual(JSContextRef ctx
, JSValueRef a
, JSValueRef b
);
157 @abstract Tests whether a JavaScript value is an object constructed by a given constructor, as compared by the JS instanceof operator.
158 @param ctx The execution context to use.
159 @param value The JSValue to test.
160 @param constructor The constructor to test against.
161 @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.
162 @result true if value is an object constructed by constructor, as compared by the JS instanceof operator, otherwise false.
164 JS_EXPORT
bool JSValueIsInstanceOfConstructor(JSContextRef ctx
, JSValueRef value
, JSObjectRef constructor
, JSValueRef
* exception
);
166 /* Creating values */
170 @abstract Creates a JavaScript value of the undefined type.
171 @param ctx The execution context to use.
172 @result The unique undefined value.
174 JS_EXPORT JSValueRef
JSValueMakeUndefined(JSContextRef ctx
);
178 @abstract Creates a JavaScript value of the null type.
179 @param ctx The execution context to use.
180 @result The unique null value.
182 JS_EXPORT JSValueRef
JSValueMakeNull(JSContextRef ctx
);
186 @abstract Creates a JavaScript value of the boolean type.
187 @param ctx The execution context to use.
188 @param boolean The bool to assign to the newly created JSValue.
189 @result A JSValue of the boolean type, representing the value of boolean.
191 JS_EXPORT JSValueRef
JSValueMakeBoolean(JSContextRef ctx
, bool boolean
);
195 @abstract Creates a JavaScript value of the number type.
196 @param ctx The execution context to use.
197 @param number The double to assign to the newly created JSValue.
198 @result A JSValue of the number type, representing the value of number.
200 JS_EXPORT JSValueRef
JSValueMakeNumber(JSContextRef ctx
, double number
);
204 @abstract Creates a JavaScript value of the string type.
205 @param ctx The execution context to use.
206 @param string The JSString to assign to the newly created JSValue. The
207 newly created JSValue retains string, and releases it upon garbage collection.
208 @result A JSValue of the string type, representing the value of string.
210 JS_EXPORT JSValueRef
JSValueMakeString(JSContextRef ctx
, JSStringRef string
);
212 /* Converting to and from JSON formatted strings */
216 @abstract Creates a JavaScript value from a JSON formatted string.
217 @param ctx The execution context to use.
218 @param string The JSString containing the JSON string to be parsed.
219 @result A JSValue containing the parsed value, or NULL if the input is invalid.
221 JS_EXPORT JSValueRef
JSValueMakeFromJSONString(JSContextRef ctx
, JSStringRef string
) AVAILABLE_AFTER_WEBKIT_VERSION_4_0
;
225 @abstract Creates a JavaScript string containing the JSON serialized representation of a JS value.
226 @param ctx The execution context to use.
227 @param value The value to serialize.
228 @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.
229 @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.
230 @result A JSString with the result of serialization, or NULL if an exception is thrown.
232 JS_EXPORT JSStringRef
JSValueCreateJSONString(JSContextRef ctx
, JSValueRef value
, unsigned indent
, JSValueRef
* exception
) AVAILABLE_AFTER_WEBKIT_VERSION_4_0
;
234 /* Converting to primitive values */
238 @abstract Converts a JavaScript value to boolean and returns the resulting boolean.
239 @param ctx The execution context to use.
240 @param value The JSValue to convert.
241 @result The boolean result of conversion.
243 JS_EXPORT
bool JSValueToBoolean(JSContextRef ctx
, JSValueRef value
);
247 @abstract Converts a JavaScript value to number and returns the resulting number.
248 @param ctx The execution context to use.
249 @param value The JSValue to convert.
250 @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.
251 @result The numeric result of conversion, or NaN if an exception is thrown.
253 JS_EXPORT
double JSValueToNumber(JSContextRef ctx
, JSValueRef value
, JSValueRef
* exception
);
257 @abstract Converts a JavaScript value to string and copies the result into a JavaScript string.
258 @param ctx The execution context to use.
259 @param value The JSValue to convert.
260 @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.
261 @result A JSString with the result of conversion, or NULL if an exception is thrown. Ownership follows the Create Rule.
263 JS_EXPORT JSStringRef
JSValueToStringCopy(JSContextRef ctx
, JSValueRef value
, JSValueRef
* exception
);
267 @abstract Converts a JavaScript value to object and returns the resulting object.
268 @param ctx The execution context to use.
269 @param value The JSValue to convert.
270 @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.
271 @result The JSObject result of conversion, or NULL if an exception is thrown.
273 JS_EXPORT JSObjectRef
JSValueToObject(JSContextRef ctx
, JSValueRef value
, JSValueRef
* exception
);
275 /* Garbage collection */
278 @abstract Protects a JavaScript value from garbage collection.
279 @param ctx The execution context to use.
280 @param value The JSValue to protect.
281 @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.
283 A value may be protected multiple times and must be unprotected an equal number of times before becoming eligible for garbage collection.
285 JS_EXPORT
void JSValueProtect(JSContextRef ctx
, JSValueRef value
);
289 @abstract Unprotects a JavaScript value from garbage collection.
290 @param ctx The execution context to use.
291 @param value The JSValue to unprotect.
292 @discussion A value may be protected multiple times and must be unprotected an
293 equal number of times before becoming eligible for garbage collection.
295 JS_EXPORT
void JSValueUnprotect(JSContextRef ctx
, JSValueRef value
);
301 #endif /* JSValueRef_h */