]>
git.saurik.com Git - apple/javascriptcore.git/blob - API/JSValueRef.cpp
2207181bd6223e833533c1a2f2b4c4ac792c834f
2 * Copyright (C) 2006, 2007 Apple 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.
27 #include "JSValueRef.h"
29 #include <wtf/Platform.h>
31 #include "JSCallbackObject.h"
33 #include <runtime/JSGlobalObject.h>
34 #include <runtime/JSString.h>
35 #include <runtime/Operations.h>
36 #include <runtime/Protect.h>
37 #include <runtime/UString.h>
38 #include <runtime/JSValue.h>
40 #include <wtf/Assertions.h>
42 #include <algorithm> // for std::min
44 JSType
JSValueGetType ( JSContextRef ctx
, JSValueRef value
)
46 JSC :: ExecState
* exec
= toJS ( ctx
);
47 exec
-> globalData (). heap
. registerThread ();
48 JSC :: JSLock
lock ( exec
);
50 JSC :: JSValue jsValue
= toJS ( exec
, value
);
52 if ( jsValue
. isUndefined ())
53 return kJSTypeUndefined
;
56 if ( jsValue
. isBoolean ())
57 return kJSTypeBoolean
;
58 if ( jsValue
. isNumber ())
60 if ( jsValue
. isString ())
62 ASSERT ( jsValue
. isObject ());
66 using namespace JSC
; // placed here to avoid conflict between JSC::JSType and JSType, above.
68 bool JSValueIsUndefined ( JSContextRef ctx
, JSValueRef value
)
70 ExecState
* exec
= toJS ( ctx
);
71 exec
-> globalData (). heap
. registerThread ();
74 JSValue jsValue
= toJS ( exec
, value
);
75 return jsValue
. isUndefined ();
78 bool JSValueIsNull ( JSContextRef ctx
, JSValueRef value
)
80 ExecState
* exec
= toJS ( ctx
);
81 exec
-> globalData (). heap
. registerThread ();
84 JSValue jsValue
= toJS ( exec
, value
);
85 return jsValue
. isNull ();
88 bool JSValueIsBoolean ( JSContextRef ctx
, JSValueRef value
)
90 ExecState
* exec
= toJS ( ctx
);
91 exec
-> globalData (). heap
. registerThread ();
94 JSValue jsValue
= toJS ( exec
, value
);
95 return jsValue
. isBoolean ();
98 bool JSValueIsNumber ( JSContextRef ctx
, JSValueRef value
)
100 ExecState
* exec
= toJS ( ctx
);
101 exec
-> globalData (). heap
. registerThread ();
104 JSValue jsValue
= toJS ( exec
, value
);
105 return jsValue
. isNumber ();
108 bool JSValueIsString ( JSContextRef ctx
, JSValueRef value
)
110 ExecState
* exec
= toJS ( ctx
);
111 exec
-> globalData (). heap
. registerThread ();
114 JSValue jsValue
= toJS ( exec
, value
);
115 return jsValue
. isString ();
118 bool JSValueIsObject ( JSContextRef ctx
, JSValueRef value
)
120 ExecState
* exec
= toJS ( ctx
);
121 exec
-> globalData (). heap
. registerThread ();
124 JSValue jsValue
= toJS ( exec
, value
);
125 return jsValue
. isObject ();
128 bool JSValueIsObjectOfClass ( JSContextRef ctx
, JSValueRef value
, JSClassRef jsClass
)
130 ExecState
* exec
= toJS ( ctx
);
131 exec
-> globalData (). heap
. registerThread ();
134 JSValue jsValue
= toJS ( exec
, value
);
136 if ( JSObject
* o
= jsValue
. getObject ()) {
137 if ( o
-> inherits (& JSCallbackObject
< JSGlobalObject
>:: info
))
138 return static_cast < JSCallbackObject
< JSGlobalObject
>*>( o
)-> inherits ( jsClass
);
139 else if ( o
-> inherits (& JSCallbackObject
< JSObject
>:: info
))
140 return static_cast < JSCallbackObject
< JSObject
>*>( o
)-> inherits ( jsClass
);
145 bool JSValueIsEqual ( JSContextRef ctx
, JSValueRef a
, JSValueRef b
, JSValueRef
* exception
)
147 ExecState
* exec
= toJS ( ctx
);
148 exec
-> globalData (). heap
. registerThread ();
151 JSValue jsA
= toJS ( exec
, a
);
152 JSValue jsB
= toJS ( exec
, b
);
154 bool result
= JSValue :: equal ( exec
, jsA
, jsB
); // false if an exception is thrown
155 if ( exec
-> hadException ()) {
157 * exception
= toRef ( exec
, exec
-> exception ());
158 exec
-> clearException ();
163 bool JSValueIsStrictEqual ( JSContextRef ctx
, JSValueRef a
, JSValueRef b
)
165 ExecState
* exec
= toJS ( ctx
);
166 exec
-> globalData (). heap
. registerThread ();
169 JSValue jsA
= toJS ( exec
, a
);
170 JSValue jsB
= toJS ( exec
, b
);
172 return JSValue :: strictEqual ( jsA
, jsB
);
175 bool JSValueIsInstanceOfConstructor ( JSContextRef ctx
, JSValueRef value
, JSObjectRef constructor
, JSValueRef
* exception
)
177 ExecState
* exec
= toJS ( ctx
);
178 exec
-> globalData (). heap
. registerThread ();
181 JSValue jsValue
= toJS ( exec
, value
);
183 JSObject
* jsConstructor
= toJS ( constructor
);
184 if (! jsConstructor
-> structure ()-> typeInfo (). implementsHasInstance ())
186 bool result
= jsConstructor
-> hasInstance ( exec
, jsValue
, jsConstructor
-> get ( exec
, exec
-> propertyNames (). prototype
)); // false if an exception is thrown
187 if ( exec
-> hadException ()) {
189 * exception
= toRef ( exec
, exec
-> exception ());
190 exec
-> clearException ();
195 JSValueRef
JSValueMakeUndefined ( JSContextRef ctx
)
197 ExecState
* exec
= toJS ( ctx
);
198 exec
-> globalData (). heap
. registerThread ();
201 return toRef ( exec
, jsUndefined ());
204 JSValueRef
JSValueMakeNull ( JSContextRef ctx
)
206 ExecState
* exec
= toJS ( ctx
);
207 exec
-> globalData (). heap
. registerThread ();
210 return toRef ( exec
, jsNull ());
213 JSValueRef
JSValueMakeBoolean ( JSContextRef ctx
, bool value
)
215 ExecState
* exec
= toJS ( ctx
);
216 exec
-> globalData (). heap
. registerThread ();
219 return toRef ( exec
, jsBoolean ( value
));
222 JSValueRef
JSValueMakeNumber ( JSContextRef ctx
, double value
)
224 ExecState
* exec
= toJS ( ctx
);
225 exec
-> globalData (). heap
. registerThread ();
228 return toRef ( exec
, jsNumber ( exec
, value
));
231 JSValueRef
JSValueMakeString ( JSContextRef ctx
, JSStringRef string
)
233 ExecState
* exec
= toJS ( ctx
);
234 exec
-> globalData (). heap
. registerThread ();
237 return toRef ( exec
, jsString ( exec
, string
-> ustring ()));
240 bool JSValueToBoolean ( JSContextRef ctx
, JSValueRef value
)
242 ExecState
* exec
= toJS ( ctx
);
243 exec
-> globalData (). heap
. registerThread ();
246 JSValue jsValue
= toJS ( exec
, value
);
247 return jsValue
. toBoolean ( exec
);
250 double JSValueToNumber ( JSContextRef ctx
, JSValueRef value
, JSValueRef
* exception
)
252 ExecState
* exec
= toJS ( ctx
);
253 exec
-> globalData (). heap
. registerThread ();
256 JSValue jsValue
= toJS ( exec
, value
);
258 double number
= jsValue
. toNumber ( exec
);
259 if ( exec
-> hadException ()) {
261 * exception
= toRef ( exec
, exec
-> exception ());
262 exec
-> clearException ();
268 JSStringRef
JSValueToStringCopy ( JSContextRef ctx
, JSValueRef value
, JSValueRef
* exception
)
270 ExecState
* exec
= toJS ( ctx
);
271 exec
-> globalData (). heap
. registerThread ();
274 JSValue jsValue
= toJS ( exec
, value
);
276 RefPtr
< OpaqueJSString
> stringRef ( OpaqueJSString :: create ( jsValue
. toString ( exec
)));
277 if ( exec
-> hadException ()) {
279 * exception
= toRef ( exec
, exec
-> exception ());
280 exec
-> clearException ();
283 return stringRef
. release (). releaseRef ();
286 JSObjectRef
JSValueToObject ( JSContextRef ctx
, JSValueRef value
, JSValueRef
* exception
)
288 ExecState
* exec
= toJS ( ctx
);
289 exec
-> globalData (). heap
. registerThread ();
292 JSValue jsValue
= toJS ( exec
, value
);
294 JSObjectRef objectRef
= toRef ( jsValue
. toObject ( exec
));
295 if ( exec
-> hadException ()) {
297 * exception
= toRef ( exec
, exec
-> exception ());
298 exec
-> clearException ();
304 void JSValueProtect ( JSContextRef ctx
, JSValueRef value
)
306 ExecState
* exec
= toJS ( ctx
);
307 exec
-> globalData (). heap
. registerThread ();
310 JSValue jsValue
= toJS ( exec
, value
);
314 void JSValueUnprotect ( JSContextRef ctx
, JSValueRef value
)
316 ExecState
* exec
= toJS ( ctx
);
317 exec
-> globalData (). heap
. registerThread ();
320 JSValue jsValue
= toJS ( exec
, value
);
321 gcUnprotect ( jsValue
);