1 | // -*- mode: c++; c-basic-offset: 4 -*-
|
---|
2 | /*
|
---|
3 | * Copyright (C) 2006 Apple Computer, Inc. All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | * 1. Redistributions of source code must retain the above copyright
|
---|
9 | * notice, this list of conditions and the following disclaimer.
|
---|
10 | * 2. Redistributions in binary form must reproduce the above copyright
|
---|
11 | * notice, this list of conditions and the following disclaimer in the
|
---|
12 | * documentation and/or other materials provided with the distribution.
|
---|
13 | *
|
---|
14 | * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
|
---|
15 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
---|
16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
---|
17 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
|
---|
18 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
---|
19 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
---|
20 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
---|
21 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
|
---|
22 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
---|
24 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
25 | */
|
---|
26 |
|
---|
27 | #ifndef JSValueRef_h
|
---|
28 | #define JSValueRef_h
|
---|
29 |
|
---|
30 | #include <JavaScriptCore/JSBase.h>
|
---|
31 |
|
---|
32 | #include <stdbool.h>
|
---|
33 |
|
---|
34 | /*!
|
---|
35 | @enum JSType
|
---|
36 | @abstract A constant identifying the type of a JSValue.
|
---|
37 | @constant kJSTypeUndefined The unique undefined value.
|
---|
38 | @constant kJSTypeNull The unique null value.
|
---|
39 | @constant kJSTypeBoolean A primitive boolean value, one of true or false.
|
---|
40 | @constant kJSTypeNumber A primitive number value.
|
---|
41 | @constant kJSTypeString A primitive string value.
|
---|
42 | @constant kJSTypeObject An object value (meaning that this JSValueRef is a JSObjectRef).
|
---|
43 | */
|
---|
44 | typedef enum {
|
---|
45 | kJSTypeUndefined,
|
---|
46 | kJSTypeNull,
|
---|
47 | kJSTypeBoolean,
|
---|
48 | kJSTypeNumber,
|
---|
49 | kJSTypeString,
|
---|
50 | kJSTypeObject
|
---|
51 | } JSType;
|
---|
52 |
|
---|
53 | #ifdef __cplusplus
|
---|
54 | extern "C" {
|
---|
55 | #endif
|
---|
56 |
|
---|
57 | /*!
|
---|
58 | @function
|
---|
59 | @abstract Returns a JavaScript value's type.
|
---|
60 | @param ctx The execution context to use.
|
---|
61 | @param value The JSValue whose type you want to obtain.
|
---|
62 | @result A value of type JSType that identifies value's type.
|
---|
63 | */
|
---|
64 | JSType JSValueGetType(JSContextRef ctx, JSValueRef value);
|
---|
65 |
|
---|
66 | /*!
|
---|
67 | @function
|
---|
68 | @abstract Tests whether a JavaScript value's type is the undefined type.
|
---|
69 | @param ctx The execution context to use.
|
---|
70 | @param value The JSValue to test.
|
---|
71 | @result true if value's type is the undefined type, otherwise false.
|
---|
72 | */
|
---|
73 | bool JSValueIsUndefined(JSContextRef ctx, JSValueRef value);
|
---|
74 |
|
---|
75 | /*!
|
---|
76 | @function
|
---|
77 | @abstract Tests whether a JavaScript value's type is the null type.
|
---|
78 | @param ctx The execution context to use.
|
---|
79 | @param value The JSValue to test.
|
---|
80 | @result true if value's type is the null type, otherwise false.
|
---|
81 | */
|
---|
82 | bool JSValueIsNull(JSContextRef ctx, JSValueRef value);
|
---|
83 |
|
---|
84 | /*!
|
---|
85 | @function
|
---|
86 | @abstract Tests whether a JavaScript value's type is the boolean type.
|
---|
87 | @param ctx The execution context to use.
|
---|
88 | @param value The JSValue to test.
|
---|
89 | @result true if value's type is the boolean type, otherwise false.
|
---|
90 | */
|
---|
91 | bool JSValueIsBoolean(JSContextRef ctx, JSValueRef value);
|
---|
92 |
|
---|
93 | /*!
|
---|
94 | @function
|
---|
95 | @abstract Tests whether a JavaScript value's type is the number type.
|
---|
96 | @param ctx The execution context to use.
|
---|
97 | @param value The JSValue to test.
|
---|
98 | @result true if value's type is the number type, otherwise false.
|
---|
99 | */
|
---|
100 | bool JSValueIsNumber(JSContextRef ctx, JSValueRef value);
|
---|
101 |
|
---|
102 | /*!
|
---|
103 | @function
|
---|
104 | @abstract Tests whether a JavaScript value's type is the string type.
|
---|
105 | @param ctx The execution context to use.
|
---|
106 | @param value The JSValue to test.
|
---|
107 | @result true if value's type is the string type, otherwise false.
|
---|
108 | */
|
---|
109 | bool JSValueIsString(JSContextRef ctx, JSValueRef value);
|
---|
110 |
|
---|
111 | /*!
|
---|
112 | @function
|
---|
113 | @abstract Tests whether a JavaScript value's type is the object type.
|
---|
114 | @param ctx The execution context to use.
|
---|
115 | @param value The JSValue to test.
|
---|
116 | @result true if value's type is the object type, otherwise false.
|
---|
117 | */
|
---|
118 | bool JSValueIsObject(JSContextRef ctx, JSValueRef value);
|
---|
119 |
|
---|
120 | /*!
|
---|
121 | @function
|
---|
122 | @abstract Tests whether a JavaScript value is an object with a given
|
---|
123 | @param ctx The execution context to use.
|
---|
124 | class in its class chain.
|
---|
125 | @param value The JSValue to test.
|
---|
126 | @result true if value is an object and has jsClass in its class chain,
|
---|
127 | otherwise false.
|
---|
128 | */
|
---|
129 | bool JSValueIsObjectOfClass(JSContextRef ctx, JSValueRef value, JSClassRef jsClass);
|
---|
130 |
|
---|
131 | // Comparing values
|
---|
132 |
|
---|
133 | /*!
|
---|
134 | @function
|
---|
135 | @abstract Tests whether two JavaScript values are equal, as compared by the JS == operator.
|
---|
136 | @param ctx The execution context to use.
|
---|
137 | @param a The first value to test.
|
---|
138 | @param b The second value to test.
|
---|
139 | @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.
|
---|
140 | @result true if the two values are equal, false if they are not equal or an exception is thrown.
|
---|
141 | */
|
---|
142 | bool JSValueIsEqual(JSContextRef ctx, JSValueRef a, JSValueRef b, JSValueRef* exception);
|
---|
143 |
|
---|
144 | /*!
|
---|
145 | @function
|
---|
146 | @abstract Tests whether two JavaScript values are strict equal, as compared by the JS === operator.
|
---|
147 | @param ctx The execution context to use.
|
---|
148 | @param a The first value to test.
|
---|
149 | @param b The second value to test.
|
---|
150 | @result true if the two values are strict equal, otherwise false.
|
---|
151 | */
|
---|
152 | bool JSValueIsStrictEqual(JSContextRef ctx, JSValueRef a, JSValueRef b);
|
---|
153 |
|
---|
154 | /*!
|
---|
155 | @function
|
---|
156 | @abstract Tests whether a JavaScript value is an object constructed by a given constructor, as compared by the JS instanceof operator.
|
---|
157 | @param ctx The execution context to use.
|
---|
158 | @param value The JSValue to test.
|
---|
159 | @param object The constructor to test against.
|
---|
160 | @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.
|
---|
161 | @result true if value is an object constructed by constructor, as compared by the JS instanceof operator, otherwise false.
|
---|
162 | */
|
---|
163 | bool JSValueIsInstanceOfConstructor(JSContextRef ctx, JSValueRef value, JSObjectRef constructor, JSValueRef* exception);
|
---|
164 |
|
---|
165 | // Creating values
|
---|
166 |
|
---|
167 | /*!
|
---|
168 | @function
|
---|
169 | @abstract Creates a JavaScript value of the undefined type.
|
---|
170 | @param ctx The execution context to use.
|
---|
171 | @result The unique undefined value.
|
---|
172 | */
|
---|
173 | JSValueRef JSValueMakeUndefined(JSContextRef ctx);
|
---|
174 |
|
---|
175 | /*!
|
---|
176 | @function
|
---|
177 | @abstract Creates a JavaScript value of the null type.
|
---|
178 | @param ctx The execution context to use.
|
---|
179 | @result The unique null value.
|
---|
180 | */
|
---|
181 | JSValueRef JSValueMakeNull(JSContextRef ctx);
|
---|
182 |
|
---|
183 | /*!
|
---|
184 | @function
|
---|
185 | @abstract Creates a JavaScript value of the boolean type.
|
---|
186 | @param ctx The execution context to use.
|
---|
187 | @param boolean The bool to assign to the newly created JSValue.
|
---|
188 | @result A JSValue of the boolean type, representing the value of boolean.
|
---|
189 | */
|
---|
190 | JSValueRef JSValueMakeBoolean(JSContextRef ctx, bool boolean);
|
---|
191 |
|
---|
192 | /*!
|
---|
193 | @function
|
---|
194 | @abstract Creates a JavaScript value of the number type.
|
---|
195 | @param ctx The execution context to use.
|
---|
196 | @param number The double to assign to the newly created JSValue.
|
---|
197 | @result A JSValue of the number type, representing the value of number.
|
---|
198 | */
|
---|
199 | JSValueRef JSValueMakeNumber(JSContextRef ctx, double number);
|
---|
200 |
|
---|
201 | /*!
|
---|
202 | @function
|
---|
203 | @abstract Creates a JavaScript value of the string type.
|
---|
204 | @param ctx The execution context to use.
|
---|
205 | @param string The JSString to assign to the newly created JSValue. The
|
---|
206 | newly created JSValue retains string, and releases it upon garbage collection.
|
---|
207 | @result A JSValue of the string type, representing the value of string.
|
---|
208 | */
|
---|
209 | JSValueRef JSValueMakeString(JSContextRef ctx, JSStringRef string);
|
---|
210 |
|
---|
211 | // Converting to primitive values
|
---|
212 |
|
---|
213 | /*!
|
---|
214 | @function
|
---|
215 | @abstract Converts a JavaScript value to boolean and returns the resulting boolean.
|
---|
216 | @param ctx The execution context to use.
|
---|
217 | @param value The JSValue to convert.
|
---|
218 | @result The boolean result of conversion.
|
---|
219 | */
|
---|
220 | bool JSValueToBoolean(JSContextRef ctx, JSValueRef value);
|
---|
221 |
|
---|
222 | /*!
|
---|
223 | @function
|
---|
224 | @abstract Converts a JavaScript value to number and returns the resulting number.
|
---|
225 | @param ctx The execution context to use.
|
---|
226 | @param value The JSValue to convert.
|
---|
227 | @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.
|
---|
228 | @result The numeric result of conversion, or NaN if an exception is thrown.
|
---|
229 | */
|
---|
230 | double JSValueToNumber(JSContextRef ctx, JSValueRef value, JSValueRef* exception);
|
---|
231 |
|
---|
232 | /*!
|
---|
233 | @function
|
---|
234 | @abstract Converts a JavaScript value to string and copies the result into a JavaScript string.
|
---|
235 | @param ctx The execution context to use.
|
---|
236 | @param value The JSValue to convert.
|
---|
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 A JSString with the result of conversion, or NULL if an exception is thrown. Ownership follows the Create Rule.
|
---|
239 | */
|
---|
240 | JSStringRef JSValueToStringCopy(JSContextRef ctx, JSValueRef value, JSValueRef* exception);
|
---|
241 |
|
---|
242 | /*!
|
---|
243 | @function
|
---|
244 | @abstract Converts a JavaScript value to object and returns the resulting object.
|
---|
245 | @param ctx The execution context to use.
|
---|
246 | @param value The JSValue to convert.
|
---|
247 | @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.
|
---|
248 | @result The JSObject result of conversion, or NULL if an exception is thrown.
|
---|
249 | */
|
---|
250 | JSObjectRef JSValueToObject(JSContextRef ctx, JSValueRef value, JSValueRef* exception);
|
---|
251 |
|
---|
252 | // Garbage collection
|
---|
253 | /*!
|
---|
254 | @function
|
---|
255 | @abstract Protects a JavaScript value from garbage collection.
|
---|
256 | @param ctx The execution context to use.
|
---|
257 | @param value The JSValue to protect.
|
---|
258 | @discussion A value may be protected multiple times and must be unprotected an
|
---|
259 | equal number of times before becoming eligible for garbage collection.
|
---|
260 | */
|
---|
261 | void JSValueProtect(JSContextRef ctx, JSValueRef value);
|
---|
262 |
|
---|
263 | /*!
|
---|
264 | @function
|
---|
265 | @abstract Unprotects a JavaScript value from garbage collection.
|
---|
266 | @param ctx The execution context to use.
|
---|
267 | @param value The JSValue to unprotect.
|
---|
268 | @discussion A value may be protected multiple times and must be unprotected an
|
---|
269 | equal number of times before becoming eligible for garbage collection.
|
---|
270 | */
|
---|
271 | void JSValueUnprotect(JSContextRef ctx, JSValueRef value);
|
---|
272 |
|
---|
273 | #ifdef __cplusplus
|
---|
274 | }
|
---|
275 | #endif
|
---|
276 |
|
---|
277 | #endif // JSValueRef_h
|
---|