]> git.saurik.com Git - apple/javascriptcore.git/blame - API/JSValueRef.cpp
JavaScriptCore-1097.3.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))
6fe7ccc8
A
133 return jsCast<JSCallbackObject<JSGlobalObject>*>(o)->inherits(jsClass);
134 if (o->inherits(&JSCallbackObject<JSNonFinalObject>::s_info))
135 return jsCast<JSCallbackObject<JSNonFinalObject>*>(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;
6fe7ccc8 178 bool result = jsConstructor->methodTable()->hasInstance(jsConstructor, 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))
6fe7ccc8 220 value = std::numeric_limits<double>::quiet_NaN();
4e4e5a6f 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 237 UString str = string->ustring();
6fe7ccc8
A
238 if (str.is8Bit()) {
239 LiteralParser<LChar> parser(exec, str.characters8(), str.length(), StrictJSON);
240 return toRef(exec, parser.tryLiteralParse());
241 }
242 LiteralParser<UChar> parser(exec, str.characters16(), str.length(), StrictJSON);
4e4e5a6f
A
243 return toRef(exec, parser.tryLiteralParse());
244}
245
246JSStringRef JSValueCreateJSONString(JSContextRef ctx, JSValueRef apiValue, unsigned indent, JSValueRef* exception)
247{
248 ExecState* exec = toJS(ctx);
249 APIEntryShim entryShim(exec);
250 JSValue value = toJS(exec, apiValue);
251 UString result = JSONStringify(exec, value, indent);
252 if (exception)
253 *exception = 0;
254 if (exec->hadException()) {
255 if (exception)
256 *exception = toRef(exec, exec->exception());
257 exec->clearException();
258 return 0;
259 }
14957cd0 260 return OpaqueJSString::create(result).leakRef();
4e4e5a6f
A
261}
262
b37bf2e1
A
263bool JSValueToBoolean(JSContextRef ctx, JSValueRef value)
264{
265 ExecState* exec = toJS(ctx);
f9bf01c6 266 APIEntryShim entryShim(exec);
ba379fdc
A
267
268 JSValue jsValue = toJS(exec, value);
9dae56ea 269 return jsValue.toBoolean(exec);
b37bf2e1
A
270}
271
272double JSValueToNumber(JSContextRef ctx, JSValueRef value, JSValueRef* exception)
273{
b37bf2e1 274 ExecState* exec = toJS(ctx);
f9bf01c6 275 APIEntryShim entryShim(exec);
b37bf2e1 276
ba379fdc 277 JSValue jsValue = toJS(exec, value);
9dae56ea
A
278
279 double number = jsValue.toNumber(exec);
b37bf2e1
A
280 if (exec->hadException()) {
281 if (exception)
ba379fdc 282 *exception = toRef(exec, exec->exception());
b37bf2e1 283 exec->clearException();
6fe7ccc8 284 number = std::numeric_limits<double>::quiet_NaN();
b37bf2e1
A
285 }
286 return number;
287}
288
289JSStringRef JSValueToStringCopy(JSContextRef ctx, JSValueRef value, JSValueRef* exception)
290{
b37bf2e1 291 ExecState* exec = toJS(ctx);
f9bf01c6 292 APIEntryShim entryShim(exec);
9dae56ea 293
ba379fdc 294 JSValue jsValue = toJS(exec, value);
b37bf2e1 295
6fe7ccc8 296 RefPtr<OpaqueJSString> stringRef(OpaqueJSString::create(jsValue.toString(exec)->value(exec)));
b37bf2e1
A
297 if (exec->hadException()) {
298 if (exception)
ba379fdc 299 *exception = toRef(exec, exec->exception());
b37bf2e1 300 exec->clearException();
9dae56ea 301 stringRef.clear();
b37bf2e1 302 }
14957cd0 303 return stringRef.release().leakRef();
b37bf2e1
A
304}
305
306JSObjectRef JSValueToObject(JSContextRef ctx, JSValueRef value, JSValueRef* exception)
307{
b37bf2e1 308 ExecState* exec = toJS(ctx);
f9bf01c6 309 APIEntryShim entryShim(exec);
9dae56ea 310
ba379fdc 311 JSValue jsValue = toJS(exec, value);
b37bf2e1 312
9dae56ea 313 JSObjectRef objectRef = toRef(jsValue.toObject(exec));
b37bf2e1
A
314 if (exec->hadException()) {
315 if (exception)
ba379fdc 316 *exception = toRef(exec, exec->exception());
b37bf2e1
A
317 exec->clearException();
318 objectRef = 0;
319 }
320 return objectRef;
321}
322
9dae56ea 323void JSValueProtect(JSContextRef ctx, JSValueRef value)
b37bf2e1 324{
9dae56ea 325 ExecState* exec = toJS(ctx);
f9bf01c6 326 APIEntryShim entryShim(exec);
9dae56ea 327
f9bf01c6 328 JSValue jsValue = toJSForGC(exec, value);
b37bf2e1
A
329 gcProtect(jsValue);
330}
331
9dae56ea 332void JSValueUnprotect(JSContextRef ctx, JSValueRef value)
b37bf2e1 333{
9dae56ea 334 ExecState* exec = toJS(ctx);
f9bf01c6 335 APIEntryShim entryShim(exec);
9dae56ea 336
f9bf01c6 337 JSValue jsValue = toJSForGC(exec, value);
b37bf2e1
A
338 gcUnprotect(jsValue);
339}