]> git.saurik.com Git - apple/javascriptcore.git/blame - API/JSValueRef.cpp
JavaScriptCore-584.tar.gz
[apple/javascriptcore.git] / API / JSValueRef.cpp
CommitLineData
b37bf2e1
A
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"
f9bf01c6 31#include "APIShims.h"
b37bf2e1
A
32#include "JSCallbackObject.h"
33
9dae56ea
A
34#include <runtime/JSGlobalObject.h>
35#include <runtime/JSString.h>
36#include <runtime/Operations.h>
37#include <runtime/Protect.h>
38#include <runtime/UString.h>
39#include <runtime/JSValue.h>
b37bf2e1
A
40
41#include <wtf/Assertions.h>
42
43#include <algorithm> // for std::min
44
f9bf01c6
A
45using namespace JSC;
46
47::JSType JSValueGetType(JSContextRef ctx, JSValueRef value)
b37bf2e1 48{
f9bf01c6
A
49 ExecState* exec = toJS(ctx);
50 APIEntryShim entryShim(exec);
ba379fdc 51
f9bf01c6 52 JSValue jsValue = toJS(exec, value);
ba379fdc 53
9dae56ea
A
54 if (jsValue.isUndefined())
55 return kJSTypeUndefined;
56 if (jsValue.isNull())
57 return kJSTypeNull;
58 if (jsValue.isBoolean())
59 return kJSTypeBoolean;
60 if (jsValue.isNumber())
61 return kJSTypeNumber;
62 if (jsValue.isString())
63 return kJSTypeString;
64 ASSERT(jsValue.isObject());
65 return kJSTypeObject;
b37bf2e1
A
66}
67
ba379fdc 68bool JSValueIsUndefined(JSContextRef ctx, JSValueRef value)
b37bf2e1 69{
ba379fdc 70 ExecState* exec = toJS(ctx);
f9bf01c6 71 APIEntryShim entryShim(exec);
ba379fdc
A
72
73 JSValue jsValue = toJS(exec, value);
9dae56ea 74 return jsValue.isUndefined();
b37bf2e1
A
75}
76
ba379fdc 77bool JSValueIsNull(JSContextRef ctx, JSValueRef value)
b37bf2e1 78{
ba379fdc 79 ExecState* exec = toJS(ctx);
f9bf01c6 80 APIEntryShim entryShim(exec);
ba379fdc
A
81
82 JSValue jsValue = toJS(exec, value);
9dae56ea 83 return jsValue.isNull();
b37bf2e1
A
84}
85
ba379fdc 86bool JSValueIsBoolean(JSContextRef ctx, JSValueRef value)
b37bf2e1 87{
ba379fdc 88 ExecState* exec = toJS(ctx);
f9bf01c6 89 APIEntryShim entryShim(exec);
ba379fdc
A
90
91 JSValue jsValue = toJS(exec, value);
9dae56ea 92 return jsValue.isBoolean();
b37bf2e1
A
93}
94
ba379fdc 95bool JSValueIsNumber(JSContextRef ctx, JSValueRef value)
b37bf2e1 96{
ba379fdc 97 ExecState* exec = toJS(ctx);
f9bf01c6 98 APIEntryShim entryShim(exec);
ba379fdc
A
99
100 JSValue jsValue = toJS(exec, value);
9dae56ea 101 return jsValue.isNumber();
b37bf2e1
A
102}
103
ba379fdc 104bool JSValueIsString(JSContextRef ctx, JSValueRef value)
b37bf2e1 105{
ba379fdc 106 ExecState* exec = toJS(ctx);
f9bf01c6 107 APIEntryShim entryShim(exec);
ba379fdc
A
108
109 JSValue jsValue = toJS(exec, value);
9dae56ea 110 return jsValue.isString();
b37bf2e1
A
111}
112
ba379fdc 113bool JSValueIsObject(JSContextRef ctx, JSValueRef value)
b37bf2e1 114{
ba379fdc 115 ExecState* exec = toJS(ctx);
f9bf01c6 116 APIEntryShim entryShim(exec);
ba379fdc
A
117
118 JSValue jsValue = toJS(exec, value);
9dae56ea 119 return jsValue.isObject();
b37bf2e1
A
120}
121
ba379fdc 122bool JSValueIsObjectOfClass(JSContextRef ctx, JSValueRef value, JSClassRef jsClass)
b37bf2e1 123{
ba379fdc 124 ExecState* exec = toJS(ctx);
f9bf01c6 125 APIEntryShim entryShim(exec);
ba379fdc
A
126
127 JSValue jsValue = toJS(exec, value);
b37bf2e1 128
9dae56ea 129 if (JSObject* o = jsValue.getObject()) {
b37bf2e1
A
130 if (o->inherits(&JSCallbackObject<JSGlobalObject>::info))
131 return static_cast<JSCallbackObject<JSGlobalObject>*>(o)->inherits(jsClass);
132 else if (o->inherits(&JSCallbackObject<JSObject>::info))
133 return static_cast<JSCallbackObject<JSObject>*>(o)->inherits(jsClass);
134 }
135 return false;
136}
137
138bool JSValueIsEqual(JSContextRef ctx, JSValueRef a, JSValueRef b, JSValueRef* exception)
139{
b37bf2e1 140 ExecState* exec = toJS(ctx);
f9bf01c6 141 APIEntryShim entryShim(exec);
9dae56ea 142
ba379fdc
A
143 JSValue jsA = toJS(exec, a);
144 JSValue jsB = toJS(exec, b);
b37bf2e1 145
ba379fdc 146 bool result = JSValue::equal(exec, jsA, jsB); // false if an exception is thrown
b37bf2e1
A
147 if (exec->hadException()) {
148 if (exception)
ba379fdc 149 *exception = toRef(exec, exec->exception());
b37bf2e1
A
150 exec->clearException();
151 }
152 return result;
153}
154
ba379fdc 155bool JSValueIsStrictEqual(JSContextRef ctx, JSValueRef a, JSValueRef b)
b37bf2e1 156{
ba379fdc 157 ExecState* exec = toJS(ctx);
f9bf01c6 158 APIEntryShim entryShim(exec);
ba379fdc
A
159
160 JSValue jsA = toJS(exec, a);
161 JSValue jsB = toJS(exec, b);
162
f9bf01c6 163 return JSValue::strictEqual(exec, jsA, jsB);
b37bf2e1
A
164}
165
166bool JSValueIsInstanceOfConstructor(JSContextRef ctx, JSValueRef value, JSObjectRef constructor, JSValueRef* exception)
167{
b37bf2e1 168 ExecState* exec = toJS(ctx);
f9bf01c6 169 APIEntryShim entryShim(exec);
9dae56ea 170
ba379fdc
A
171 JSValue jsValue = toJS(exec, value);
172
b37bf2e1 173 JSObject* jsConstructor = toJS(constructor);
9dae56ea 174 if (!jsConstructor->structure()->typeInfo().implementsHasInstance())
b37bf2e1 175 return false;
9dae56ea 176 bool result = jsConstructor->hasInstance(exec, jsValue, jsConstructor->get(exec, exec->propertyNames().prototype)); // false if an exception is thrown
b37bf2e1
A
177 if (exec->hadException()) {
178 if (exception)
ba379fdc 179 *exception = toRef(exec, exec->exception());
b37bf2e1
A
180 exec->clearException();
181 }
182 return result;
183}
184
ba379fdc 185JSValueRef JSValueMakeUndefined(JSContextRef ctx)
b37bf2e1 186{
ba379fdc 187 ExecState* exec = toJS(ctx);
f9bf01c6 188 APIEntryShim entryShim(exec);
ba379fdc
A
189
190 return toRef(exec, jsUndefined());
b37bf2e1
A
191}
192
ba379fdc 193JSValueRef JSValueMakeNull(JSContextRef ctx)
b37bf2e1 194{
ba379fdc 195 ExecState* exec = toJS(ctx);
f9bf01c6 196 APIEntryShim entryShim(exec);
ba379fdc
A
197
198 return toRef(exec, jsNull());
b37bf2e1
A
199}
200
ba379fdc 201JSValueRef JSValueMakeBoolean(JSContextRef ctx, bool value)
b37bf2e1 202{
ba379fdc 203 ExecState* exec = toJS(ctx);
f9bf01c6 204 APIEntryShim entryShim(exec);
ba379fdc
A
205
206 return toRef(exec, jsBoolean(value));
b37bf2e1
A
207}
208
9dae56ea 209JSValueRef JSValueMakeNumber(JSContextRef ctx, double value)
b37bf2e1 210{
9dae56ea 211 ExecState* exec = toJS(ctx);
f9bf01c6 212 APIEntryShim entryShim(exec);
9dae56ea 213
ba379fdc 214 return toRef(exec, jsNumber(exec, value));
b37bf2e1
A
215}
216
9dae56ea 217JSValueRef JSValueMakeString(JSContextRef ctx, JSStringRef string)
b37bf2e1 218{
9dae56ea 219 ExecState* exec = toJS(ctx);
f9bf01c6 220 APIEntryShim entryShim(exec);
9dae56ea 221
ba379fdc 222 return toRef(exec, jsString(exec, string->ustring()));
b37bf2e1
A
223}
224
225bool JSValueToBoolean(JSContextRef ctx, JSValueRef value)
226{
227 ExecState* exec = toJS(ctx);
f9bf01c6 228 APIEntryShim entryShim(exec);
ba379fdc
A
229
230 JSValue jsValue = toJS(exec, value);
9dae56ea 231 return jsValue.toBoolean(exec);
b37bf2e1
A
232}
233
234double JSValueToNumber(JSContextRef ctx, JSValueRef value, JSValueRef* exception)
235{
b37bf2e1 236 ExecState* exec = toJS(ctx);
f9bf01c6 237 APIEntryShim entryShim(exec);
b37bf2e1 238
ba379fdc 239 JSValue jsValue = toJS(exec, value);
9dae56ea
A
240
241 double number = jsValue.toNumber(exec);
b37bf2e1
A
242 if (exec->hadException()) {
243 if (exception)
ba379fdc 244 *exception = toRef(exec, exec->exception());
b37bf2e1
A
245 exec->clearException();
246 number = NaN;
247 }
248 return number;
249}
250
251JSStringRef JSValueToStringCopy(JSContextRef ctx, JSValueRef value, JSValueRef* exception)
252{
b37bf2e1 253 ExecState* exec = toJS(ctx);
f9bf01c6 254 APIEntryShim entryShim(exec);
9dae56ea 255
ba379fdc 256 JSValue jsValue = toJS(exec, value);
b37bf2e1 257
9dae56ea 258 RefPtr<OpaqueJSString> stringRef(OpaqueJSString::create(jsValue.toString(exec)));
b37bf2e1
A
259 if (exec->hadException()) {
260 if (exception)
ba379fdc 261 *exception = toRef(exec, exec->exception());
b37bf2e1 262 exec->clearException();
9dae56ea 263 stringRef.clear();
b37bf2e1 264 }
9dae56ea 265 return stringRef.release().releaseRef();
b37bf2e1
A
266}
267
268JSObjectRef JSValueToObject(JSContextRef ctx, JSValueRef value, JSValueRef* exception)
269{
b37bf2e1 270 ExecState* exec = toJS(ctx);
f9bf01c6 271 APIEntryShim entryShim(exec);
9dae56ea 272
ba379fdc 273 JSValue jsValue = toJS(exec, value);
b37bf2e1 274
9dae56ea 275 JSObjectRef objectRef = toRef(jsValue.toObject(exec));
b37bf2e1
A
276 if (exec->hadException()) {
277 if (exception)
ba379fdc 278 *exception = toRef(exec, exec->exception());
b37bf2e1
A
279 exec->clearException();
280 objectRef = 0;
281 }
282 return objectRef;
283}
284
9dae56ea 285void JSValueProtect(JSContextRef ctx, JSValueRef value)
b37bf2e1 286{
9dae56ea 287 ExecState* exec = toJS(ctx);
f9bf01c6 288 APIEntryShim entryShim(exec);
9dae56ea 289
f9bf01c6 290 JSValue jsValue = toJSForGC(exec, value);
b37bf2e1
A
291 gcProtect(jsValue);
292}
293
9dae56ea 294void JSValueUnprotect(JSContextRef ctx, JSValueRef value)
b37bf2e1 295{
9dae56ea 296 ExecState* exec = toJS(ctx);
f9bf01c6 297 APIEntryShim entryShim(exec);
9dae56ea 298
f9bf01c6 299 JSValue jsValue = toJSForGC(exec, value);
b37bf2e1
A
300 gcUnprotect(jsValue);
301}