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