OSDN Git Service

Merge commit 'c0957e2d' into manualmerge
[android-x86/external-webkit.git] / JavaScriptCore / API / JSValueRef.cpp
1 /*
2  * Copyright (C) 2006, 2007 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 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. 
24  */
25
26 #include "config.h"
27 #include "JSValueRef.h"
28
29 #include <wtf/Platform.h>
30 #include "APICast.h"
31 #include "JSCallbackObject.h"
32
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>
39
40 #include <wtf/Assertions.h>
41
42 #include <algorithm> // for std::min
43
44 JSType JSValueGetType(JSContextRef ctx, JSValueRef value)
45 {
46     JSC::ExecState* exec = toJS(ctx);
47     exec->globalData().heap.registerThread();
48     JSC::JSLock lock(exec);
49
50     JSC::JSValue jsValue = toJS(exec, value);
51
52     if (jsValue.isUndefined())
53         return kJSTypeUndefined;
54     if (jsValue.isNull())
55         return kJSTypeNull;
56     if (jsValue.isBoolean())
57         return kJSTypeBoolean;
58     if (jsValue.isNumber())
59         return kJSTypeNumber;
60     if (jsValue.isString())
61         return kJSTypeString;
62     ASSERT(jsValue.isObject());
63     return kJSTypeObject;
64 }
65
66 using namespace JSC; // placed here to avoid conflict between JSC::JSType and JSType, above.
67
68 bool JSValueIsUndefined(JSContextRef ctx, JSValueRef value)
69 {
70     ExecState* exec = toJS(ctx);
71     exec->globalData().heap.registerThread();
72     JSLock lock(exec);
73
74     JSValue jsValue = toJS(exec, value);
75     return jsValue.isUndefined();
76 }
77
78 bool JSValueIsNull(JSContextRef ctx, JSValueRef value)
79 {
80     ExecState* exec = toJS(ctx);
81     exec->globalData().heap.registerThread();
82     JSLock lock(exec);
83
84     JSValue jsValue = toJS(exec, value);
85     return jsValue.isNull();
86 }
87
88 bool JSValueIsBoolean(JSContextRef ctx, JSValueRef value)
89 {
90     ExecState* exec = toJS(ctx);
91     exec->globalData().heap.registerThread();
92     JSLock lock(exec);
93
94     JSValue jsValue = toJS(exec, value);
95     return jsValue.isBoolean();
96 }
97
98 bool JSValueIsNumber(JSContextRef ctx, JSValueRef value)
99 {
100     ExecState* exec = toJS(ctx);
101     exec->globalData().heap.registerThread();
102     JSLock lock(exec);
103
104     JSValue jsValue = toJS(exec, value);
105     return jsValue.isNumber();
106 }
107
108 bool JSValueIsString(JSContextRef ctx, JSValueRef value)
109 {
110     ExecState* exec = toJS(ctx);
111     exec->globalData().heap.registerThread();
112     JSLock lock(exec);
113
114     JSValue jsValue = toJS(exec, value);
115     return jsValue.isString();
116 }
117
118 bool JSValueIsObject(JSContextRef ctx, JSValueRef value)
119 {
120     ExecState* exec = toJS(ctx);
121     exec->globalData().heap.registerThread();
122     JSLock lock(exec);
123
124     JSValue jsValue = toJS(exec, value);
125     return jsValue.isObject();
126 }
127
128 bool JSValueIsObjectOfClass(JSContextRef ctx, JSValueRef value, JSClassRef jsClass)
129 {
130     ExecState* exec = toJS(ctx);
131     exec->globalData().heap.registerThread();
132     JSLock lock(exec);
133
134     JSValue jsValue = toJS(exec, value);
135     
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);
141     }
142     return false;
143 }
144
145 bool JSValueIsEqual(JSContextRef ctx, JSValueRef a, JSValueRef b, JSValueRef* exception)
146 {
147     ExecState* exec = toJS(ctx);
148     exec->globalData().heap.registerThread();
149     JSLock lock(exec);
150
151     JSValue jsA = toJS(exec, a);
152     JSValue jsB = toJS(exec, b);
153
154     bool result = JSValue::equal(exec, jsA, jsB); // false if an exception is thrown
155     if (exec->hadException()) {
156         if (exception)
157             *exception = toRef(exec, exec->exception());
158         exec->clearException();
159     }
160     return result;
161 }
162
163 bool JSValueIsStrictEqual(JSContextRef ctx, JSValueRef a, JSValueRef b)
164 {
165     ExecState* exec = toJS(ctx);
166     exec->globalData().heap.registerThread();
167     JSLock lock(exec);
168
169     JSValue jsA = toJS(exec, a);
170     JSValue jsB = toJS(exec, b);
171
172     return JSValue::strictEqual(jsA, jsB);
173 }
174
175 bool JSValueIsInstanceOfConstructor(JSContextRef ctx, JSValueRef value, JSObjectRef constructor, JSValueRef* exception)
176 {
177     ExecState* exec = toJS(ctx);
178     exec->globalData().heap.registerThread();
179     JSLock lock(exec);
180
181     JSValue jsValue = toJS(exec, value);
182
183     JSObject* jsConstructor = toJS(constructor);
184     if (!jsConstructor->structure()->typeInfo().implementsHasInstance())
185         return false;
186     bool result = jsConstructor->hasInstance(exec, jsValue, jsConstructor->get(exec, exec->propertyNames().prototype)); // false if an exception is thrown
187     if (exec->hadException()) {
188         if (exception)
189             *exception = toRef(exec, exec->exception());
190         exec->clearException();
191     }
192     return result;
193 }
194
195 JSValueRef JSValueMakeUndefined(JSContextRef ctx)
196 {
197     ExecState* exec = toJS(ctx);
198     exec->globalData().heap.registerThread();
199     JSLock lock(exec);
200
201     return toRef(exec, jsUndefined());
202 }
203
204 JSValueRef JSValueMakeNull(JSContextRef ctx)
205 {
206     ExecState* exec = toJS(ctx);
207     exec->globalData().heap.registerThread();
208     JSLock lock(exec);
209
210     return toRef(exec, jsNull());
211 }
212
213 JSValueRef JSValueMakeBoolean(JSContextRef ctx, bool value)
214 {
215     ExecState* exec = toJS(ctx);
216     exec->globalData().heap.registerThread();
217     JSLock lock(exec);
218
219     return toRef(exec, jsBoolean(value));
220 }
221
222 JSValueRef JSValueMakeNumber(JSContextRef ctx, double value)
223 {
224     ExecState* exec = toJS(ctx);
225     exec->globalData().heap.registerThread();
226     JSLock lock(exec);
227
228     return toRef(exec, jsNumber(exec, value));
229 }
230
231 JSValueRef JSValueMakeString(JSContextRef ctx, JSStringRef string)
232 {
233     ExecState* exec = toJS(ctx);
234     exec->globalData().heap.registerThread();
235     JSLock lock(exec);
236
237     return toRef(exec, jsString(exec, string->ustring()));
238 }
239
240 bool JSValueToBoolean(JSContextRef ctx, JSValueRef value)
241 {
242     ExecState* exec = toJS(ctx);
243     exec->globalData().heap.registerThread();
244     JSLock lock(exec);
245
246     JSValue jsValue = toJS(exec, value);
247     return jsValue.toBoolean(exec);
248 }
249
250 double JSValueToNumber(JSContextRef ctx, JSValueRef value, JSValueRef* exception)
251 {
252     ExecState* exec = toJS(ctx);
253     exec->globalData().heap.registerThread();
254     JSLock lock(exec);
255
256     JSValue jsValue = toJS(exec, value);
257
258     double number = jsValue.toNumber(exec);
259     if (exec->hadException()) {
260         if (exception)
261             *exception = toRef(exec, exec->exception());
262         exec->clearException();
263         number = NaN;
264     }
265     return number;
266 }
267
268 JSStringRef JSValueToStringCopy(JSContextRef ctx, JSValueRef value, JSValueRef* exception)
269 {
270     ExecState* exec = toJS(ctx);
271     exec->globalData().heap.registerThread();
272     JSLock lock(exec);
273
274     JSValue jsValue = toJS(exec, value);
275     
276     RefPtr<OpaqueJSString> stringRef(OpaqueJSString::create(jsValue.toString(exec)));
277     if (exec->hadException()) {
278         if (exception)
279             *exception = toRef(exec, exec->exception());
280         exec->clearException();
281         stringRef.clear();
282     }
283     return stringRef.release().releaseRef();
284 }
285
286 JSObjectRef JSValueToObject(JSContextRef ctx, JSValueRef value, JSValueRef* exception)
287 {
288     ExecState* exec = toJS(ctx);
289     exec->globalData().heap.registerThread();
290     JSLock lock(exec);
291
292     JSValue jsValue = toJS(exec, value);
293     
294     JSObjectRef objectRef = toRef(jsValue.toObject(exec));
295     if (exec->hadException()) {
296         if (exception)
297             *exception = toRef(exec, exec->exception());
298         exec->clearException();
299         objectRef = 0;
300     }
301     return objectRef;
302 }    
303
304 void JSValueProtect(JSContextRef ctx, JSValueRef value)
305 {
306     ExecState* exec = toJS(ctx);
307     exec->globalData().heap.registerThread();
308     JSLock lock(exec);
309
310     JSValue jsValue = toJS(exec, value);
311     gcProtect(jsValue);
312 }
313
314 void JSValueUnprotect(JSContextRef ctx, JSValueRef value)
315 {
316     ExecState* exec = toJS(ctx);
317     exec->globalData().heap.registerThread();
318     JSLock lock(exec);
319
320     JSValue jsValue = toJS(exec, value);
321     gcUnprotect(jsValue);
322 }