JSValueRef.cpp 9.56 KB
Newer Older
ggaren's avatar
ggaren committed
1
/*
darin@apple.com's avatar
darin@apple.com committed
2
 * Copyright (C) 2006, 2007 Apple Inc. All rights reserved.
ggaren's avatar
ggaren committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
 */

darin@apple.com's avatar
darin@apple.com committed
26 27 28
#include "config.h"
#include "JSValueRef.h"

ggaren's avatar
ggaren committed
29
#include "APICast.h"
30
#include "APIShims.h"
ggaren's avatar
ggaren committed
31 32
#include "JSCallbackObject.h"

33
#include <runtime/JSGlobalObject.h>
34
#include <runtime/JSONObject.h>
35
#include <runtime/JSString.h>
36
#include <runtime/LiteralParser.h>
37 38
#include <runtime/Operations.h>
#include <runtime/Protect.h>
39
#include <runtime/JSValue.h>
ggaren's avatar
ggaren committed
40 41

#include <wtf/Assertions.h>
barraclough@apple.com's avatar
barraclough@apple.com committed
42
#include <wtf/text/StringHash.h>
43
#include <wtf/text/WTFString.h>
ggaren's avatar
ggaren committed
44 45 46

#include <algorithm> // for std::min

47 48 49
using namespace JSC;

::JSType JSValueGetType(JSContextRef ctx, JSValueRef value)
ggaren's avatar
ggaren committed
50
{
51 52
    ExecState* exec = toJS(ctx);
    APIEntryShim entryShim(exec);
weinig@apple.com's avatar
weinig@apple.com committed
53

54
    JSValue jsValue = toJS(exec, value);
weinig@apple.com's avatar
weinig@apple.com committed
55

weinig@apple.com's avatar
weinig@apple.com committed
56
    if (jsValue.isUndefined())
darin@apple.com's avatar
darin@apple.com committed
57
        return kJSTypeUndefined;
weinig@apple.com's avatar
weinig@apple.com committed
58
    if (jsValue.isNull())
darin@apple.com's avatar
darin@apple.com committed
59
        return kJSTypeNull;
weinig@apple.com's avatar
weinig@apple.com committed
60
    if (jsValue.isBoolean())
darin@apple.com's avatar
darin@apple.com committed
61
        return kJSTypeBoolean;
weinig@apple.com's avatar
weinig@apple.com committed
62
    if (jsValue.isNumber())
darin@apple.com's avatar
darin@apple.com committed
63
        return kJSTypeNumber;
weinig@apple.com's avatar
weinig@apple.com committed
64
    if (jsValue.isString())
darin@apple.com's avatar
darin@apple.com committed
65
        return kJSTypeString;
weinig@apple.com's avatar
weinig@apple.com committed
66
    ASSERT(jsValue.isObject());
darin@apple.com's avatar
darin@apple.com committed
67
    return kJSTypeObject;
ggaren's avatar
ggaren committed
68 69
}

weinig@apple.com's avatar
weinig@apple.com committed
70
bool JSValueIsUndefined(JSContextRef ctx, JSValueRef value)
ggaren's avatar
ggaren committed
71
{
weinig@apple.com's avatar
weinig@apple.com committed
72
    ExecState* exec = toJS(ctx);
73
    APIEntryShim entryShim(exec);
weinig@apple.com's avatar
weinig@apple.com committed
74 75

    JSValue jsValue = toJS(exec, value);
weinig@apple.com's avatar
weinig@apple.com committed
76
    return jsValue.isUndefined();
ggaren's avatar
ggaren committed
77 78
}

weinig@apple.com's avatar
weinig@apple.com committed
79
bool JSValueIsNull(JSContextRef ctx, JSValueRef value)
ggaren's avatar
ggaren committed
80
{
weinig@apple.com's avatar
weinig@apple.com committed
81
    ExecState* exec = toJS(ctx);
82
    APIEntryShim entryShim(exec);
weinig@apple.com's avatar
weinig@apple.com committed
83 84

    JSValue jsValue = toJS(exec, value);
weinig@apple.com's avatar
weinig@apple.com committed
85
    return jsValue.isNull();
ggaren's avatar
ggaren committed
86 87
}

weinig@apple.com's avatar
weinig@apple.com committed
88
bool JSValueIsBoolean(JSContextRef ctx, JSValueRef value)
ggaren's avatar
ggaren committed
89
{
weinig@apple.com's avatar
weinig@apple.com committed
90
    ExecState* exec = toJS(ctx);
91
    APIEntryShim entryShim(exec);
weinig@apple.com's avatar
weinig@apple.com committed
92 93

    JSValue jsValue = toJS(exec, value);
weinig@apple.com's avatar
weinig@apple.com committed
94
    return jsValue.isBoolean();
ggaren's avatar
ggaren committed
95 96
}

weinig@apple.com's avatar
weinig@apple.com committed
97
bool JSValueIsNumber(JSContextRef ctx, JSValueRef value)
ggaren's avatar
ggaren committed
98
{
weinig@apple.com's avatar
weinig@apple.com committed
99
    ExecState* exec = toJS(ctx);
100
    APIEntryShim entryShim(exec);
weinig@apple.com's avatar
weinig@apple.com committed
101 102

    JSValue jsValue = toJS(exec, value);
weinig@apple.com's avatar
weinig@apple.com committed
103
    return jsValue.isNumber();
ggaren's avatar
ggaren committed
104 105
}

weinig@apple.com's avatar
weinig@apple.com committed
106
bool JSValueIsString(JSContextRef ctx, JSValueRef value)
ggaren's avatar
ggaren committed
107
{
weinig@apple.com's avatar
weinig@apple.com committed
108
    ExecState* exec = toJS(ctx);
109
    APIEntryShim entryShim(exec);
weinig@apple.com's avatar
weinig@apple.com committed
110 111

    JSValue jsValue = toJS(exec, value);
weinig@apple.com's avatar
weinig@apple.com committed
112
    return jsValue.isString();
ggaren's avatar
ggaren committed
113 114
}

weinig@apple.com's avatar
weinig@apple.com committed
115
bool JSValueIsObject(JSContextRef ctx, JSValueRef value)
ggaren's avatar
ggaren committed
116
{
weinig@apple.com's avatar
weinig@apple.com committed
117
    ExecState* exec = toJS(ctx);
118
    APIEntryShim entryShim(exec);
weinig@apple.com's avatar
weinig@apple.com committed
119 120

    JSValue jsValue = toJS(exec, value);
weinig@apple.com's avatar
weinig@apple.com committed
121
    return jsValue.isObject();
ggaren's avatar
ggaren committed
122 123
}

weinig@apple.com's avatar
weinig@apple.com committed
124
bool JSValueIsObjectOfClass(JSContextRef ctx, JSValueRef value, JSClassRef jsClass)
ggaren's avatar
ggaren committed
125
{
weinig@apple.com's avatar
weinig@apple.com committed
126
    ExecState* exec = toJS(ctx);
127
    APIEntryShim entryShim(exec);
weinig@apple.com's avatar
weinig@apple.com committed
128 129

    JSValue jsValue = toJS(exec, value);
ggaren's avatar
ggaren committed
130
    
weinig@apple.com's avatar
weinig@apple.com committed
131
    if (JSObject* o = jsValue.getObject()) {
132
        if (o->inherits(&JSCallbackObject<JSGlobalObject>::s_info))
133
            return jsCast<JSCallbackObject<JSGlobalObject>*>(o)->inherits(jsClass);
134 135
        if (o->inherits(&JSCallbackObject<JSDestructibleObject>::s_info))
            return jsCast<JSCallbackObject<JSDestructibleObject>*>(o)->inherits(jsClass);
136
    }
ggaren's avatar
ggaren committed
137 138 139
    return false;
}

mjs's avatar
mjs committed
140
bool JSValueIsEqual(JSContextRef ctx, JSValueRef a, JSValueRef b, JSValueRef* exception)
ggaren's avatar
ggaren committed
141
{
mjs's avatar
mjs committed
142
    ExecState* exec = toJS(ctx);
143
    APIEntryShim entryShim(exec);
ap@webkit.org's avatar
ap@webkit.org committed
144

weinig@apple.com's avatar
weinig@apple.com committed
145 146
    JSValue jsA = toJS(exec, a);
    JSValue jsB = toJS(exec, b);
ggaren's avatar
ggaren committed
147

ggaren@apple.com's avatar
ggaren@apple.com committed
148
    bool result = JSValue::equal(exec, jsA, jsB); // false if an exception is thrown
ggaren's avatar
ggaren committed
149 150
    if (exec->hadException()) {
        if (exception)
weinig@apple.com's avatar
weinig@apple.com committed
151
            *exception = toRef(exec, exec->exception());
ggaren's avatar
ggaren committed
152
        exec->clearException();
ggaren's avatar
ggaren committed
153
    }
ggaren's avatar
ggaren committed
154 155 156
    return result;
}

weinig@apple.com's avatar
weinig@apple.com committed
157
bool JSValueIsStrictEqual(JSContextRef ctx, JSValueRef a, JSValueRef b)
ggaren's avatar
ggaren committed
158
{
weinig@apple.com's avatar
weinig@apple.com committed
159
    ExecState* exec = toJS(ctx);
160
    APIEntryShim entryShim(exec);
weinig@apple.com's avatar
weinig@apple.com committed
161 162 163 164

    JSValue jsA = toJS(exec, a);
    JSValue jsB = toJS(exec, b);

165
    return JSValue::strictEqual(exec, jsA, jsB);
ggaren's avatar
ggaren committed
166 167
}

mjs's avatar
mjs committed
168
bool JSValueIsInstanceOfConstructor(JSContextRef ctx, JSValueRef value, JSObjectRef constructor, JSValueRef* exception)
ggaren's avatar
ggaren committed
169
{
mjs's avatar
mjs committed
170
    ExecState* exec = toJS(ctx);
171
    APIEntryShim entryShim(exec);
ap@webkit.org's avatar
ap@webkit.org committed
172

weinig@apple.com's avatar
weinig@apple.com committed
173 174
    JSValue jsValue = toJS(exec, value);

ggaren's avatar
ggaren committed
175
    JSObject* jsConstructor = toJS(constructor);
darin@apple.com's avatar
darin@apple.com committed
176
    if (!jsConstructor->structure()->typeInfo().implementsHasInstance())
ggaren's avatar
ggaren committed
177
        return false;
178
    bool result = jsConstructor->hasInstance(exec, jsValue); // false if an exception is thrown
ggaren's avatar
ggaren committed
179 180
    if (exec->hadException()) {
        if (exception)
weinig@apple.com's avatar
weinig@apple.com committed
181
            *exception = toRef(exec, exec->exception());
ggaren's avatar
ggaren committed
182
        exec->clearException();
ggaren's avatar
ggaren committed
183
    }
ggaren's avatar
ggaren committed
184 185 186
    return result;
}

weinig@apple.com's avatar
weinig@apple.com committed
187
JSValueRef JSValueMakeUndefined(JSContextRef ctx)
ggaren's avatar
ggaren committed
188
{
weinig@apple.com's avatar
weinig@apple.com committed
189
    ExecState* exec = toJS(ctx);
190
    APIEntryShim entryShim(exec);
weinig@apple.com's avatar
weinig@apple.com committed
191 192

    return toRef(exec, jsUndefined());
ggaren's avatar
ggaren committed
193 194
}

weinig@apple.com's avatar
weinig@apple.com committed
195
JSValueRef JSValueMakeNull(JSContextRef ctx)
ggaren's avatar
ggaren committed
196
{
weinig@apple.com's avatar
weinig@apple.com committed
197
    ExecState* exec = toJS(ctx);
198
    APIEntryShim entryShim(exec);
weinig@apple.com's avatar
weinig@apple.com committed
199 200

    return toRef(exec, jsNull());
ggaren's avatar
ggaren committed
201 202
}

weinig@apple.com's avatar
weinig@apple.com committed
203
JSValueRef JSValueMakeBoolean(JSContextRef ctx, bool value)
ggaren's avatar
ggaren committed
204
{
weinig@apple.com's avatar
weinig@apple.com committed
205
    ExecState* exec = toJS(ctx);
206
    APIEntryShim entryShim(exec);
weinig@apple.com's avatar
weinig@apple.com committed
207 208

    return toRef(exec, jsBoolean(value));
ggaren's avatar
ggaren committed
209 210
}

ap@webkit.org's avatar
ap@webkit.org committed
211
JSValueRef JSValueMakeNumber(JSContextRef ctx, double value)
ggaren's avatar
ggaren committed
212
{
ap@webkit.org's avatar
ap@webkit.org committed
213
    ExecState* exec = toJS(ctx);
214
    APIEntryShim entryShim(exec);
ap@webkit.org's avatar
ap@webkit.org committed
215

216 217 218 219
    // Our JSValue representation relies on a standard bit pattern for NaN. NaNs
    // generated internally to JavaScriptCore naturally have that representation,
    // but an external NaN might not.
    if (isnan(value))
220
        value = QNaN;
221

222
    return toRef(exec, jsNumber(value));
ggaren's avatar
ggaren committed
223 224
}

ap@webkit.org's avatar
ap@webkit.org committed
225
JSValueRef JSValueMakeString(JSContextRef ctx, JSStringRef string)
ggaren's avatar
ggaren committed
226
{
ap@webkit.org's avatar
ap@webkit.org committed
227
    ExecState* exec = toJS(ctx);
228
    APIEntryShim entryShim(exec);
ap@webkit.org's avatar
ap@webkit.org committed
229

230
    return toRef(exec, jsString(exec, string->string()));
mjs's avatar
mjs committed
231 232
}

233 234 235 236
JSValueRef JSValueMakeFromJSONString(JSContextRef ctx, JSStringRef string)
{
    ExecState* exec = toJS(ctx);
    APIEntryShim entryShim(exec);
237
    String str = string->string();
238 239 240
    unsigned length = str.length();
    if (length && str.is8Bit()) {
        LiteralParser<LChar> parser(exec, str.characters8(), length, StrictJSON);
241 242
        return toRef(exec, parser.tryLiteralParse());
    }
243
    LiteralParser<UChar> parser(exec, str.characters(), length, StrictJSON);
244 245 246 247 248 249 250 251
    return toRef(exec, parser.tryLiteralParse());
}

JSStringRef JSValueCreateJSONString(JSContextRef ctx, JSValueRef apiValue, unsigned indent, JSValueRef* exception)
{
    ExecState* exec = toJS(ctx);
    APIEntryShim entryShim(exec);
    JSValue value = toJS(exec, apiValue);
252
    String result = JSONStringify(exec, value, indent);
253 254 255 256 257 258 259 260
    if (exception)
        *exception = 0;
    if (exec->hadException()) {
        if (exception)
            *exception = toRef(exec, exec->exception());
        exec->clearException();
        return 0;
    }
261
    return OpaqueJSString::create(result).leakRef();
262 263
}

mjs@apple.com's avatar
mjs@apple.com committed
264
bool JSValueToBoolean(JSContextRef ctx, JSValueRef value)
mjs's avatar
mjs committed
265
{
mjs@apple.com's avatar
mjs@apple.com committed
266
    ExecState* exec = toJS(ctx);
267
    APIEntryShim entryShim(exec);
weinig@apple.com's avatar
weinig@apple.com committed
268 269

    JSValue jsValue = toJS(exec, value);
270
    return jsValue.toBoolean(exec);
ggaren's avatar
ggaren committed
271 272
}

mjs's avatar
mjs committed
273
double JSValueToNumber(JSContextRef ctx, JSValueRef value, JSValueRef* exception)
ggaren's avatar
ggaren committed
274
{
mjs's avatar
mjs committed
275
    ExecState* exec = toJS(ctx);
276
    APIEntryShim entryShim(exec);
ap@webkit.org's avatar
ap@webkit.org committed
277

weinig@apple.com's avatar
weinig@apple.com committed
278
    JSValue jsValue = toJS(exec, value);
ggaren's avatar
ggaren committed
279

weinig@apple.com's avatar
weinig@apple.com committed
280
    double number = jsValue.toNumber(exec);
ggaren's avatar
ggaren committed
281
    if (exec->hadException()) {
ggaren's avatar
ggaren committed
282
        if (exception)
weinig@apple.com's avatar
weinig@apple.com committed
283
            *exception = toRef(exec, exec->exception());
ggaren's avatar
ggaren committed
284
        exec->clearException();
285
        number = QNaN;
ggaren's avatar
ggaren committed
286
    }
ggaren's avatar
ggaren committed
287 288 289
    return number;
}

mjs's avatar
mjs committed
290
JSStringRef JSValueToStringCopy(JSContextRef ctx, JSValueRef value, JSValueRef* exception)
ggaren's avatar
ggaren committed
291
{
mjs's avatar
mjs committed
292
    ExecState* exec = toJS(ctx);
293
    APIEntryShim entryShim(exec);
ap@webkit.org's avatar
ap@webkit.org committed
294

weinig@apple.com's avatar
weinig@apple.com committed
295
    JSValue jsValue = toJS(exec, value);
ggaren's avatar
ggaren committed
296
    
297
    RefPtr<OpaqueJSString> stringRef(OpaqueJSString::create(jsValue.toString(exec)->value(exec)));
ggaren's avatar
ggaren committed
298 299
    if (exec->hadException()) {
        if (exception)
weinig@apple.com's avatar
weinig@apple.com committed
300
            *exception = toRef(exec, exec->exception());
ggaren's avatar
ggaren committed
301
        exec->clearException();
ap@webkit.org's avatar
ap@webkit.org committed
302
        stringRef.clear();
ggaren's avatar
ggaren committed
303
    }
304
    return stringRef.release().leakRef();
ggaren's avatar
ggaren committed
305 306
}

mjs's avatar
mjs committed
307
JSObjectRef JSValueToObject(JSContextRef ctx, JSValueRef value, JSValueRef* exception)
ggaren's avatar
ggaren committed
308
{
mjs's avatar
mjs committed
309
    ExecState* exec = toJS(ctx);
310
    APIEntryShim entryShim(exec);
ap@webkit.org's avatar
ap@webkit.org committed
311

weinig@apple.com's avatar
weinig@apple.com committed
312
    JSValue jsValue = toJS(exec, value);
ggaren's avatar
ggaren committed
313
    
weinig@apple.com's avatar
weinig@apple.com committed
314
    JSObjectRef objectRef = toRef(jsValue.toObject(exec));
ggaren's avatar
ggaren committed
315
    if (exec->hadException()) {
ggaren's avatar
ggaren committed
316
        if (exception)
weinig@apple.com's avatar
weinig@apple.com committed
317
            *exception = toRef(exec, exec->exception());
ggaren's avatar
ggaren committed
318
        exec->clearException();
319
        objectRef = 0;
ggaren's avatar
ggaren committed
320 321 322 323
    }
    return objectRef;
}    

ap@webkit.org's avatar
ap@webkit.org committed
324
void JSValueProtect(JSContextRef ctx, JSValueRef value)
ggaren's avatar
ggaren committed
325
{
ap@webkit.org's avatar
ap@webkit.org committed
326
    ExecState* exec = toJS(ctx);
327
    APIEntryShim entryShim(exec);
ap@webkit.org's avatar
ap@webkit.org committed
328

329
    JSValue jsValue = toJSForGC(exec, value);
ggaren's avatar
ggaren committed
330 331 332
    gcProtect(jsValue);
}

ap@webkit.org's avatar
ap@webkit.org committed
333
void JSValueUnprotect(JSContextRef ctx, JSValueRef value)
ggaren's avatar
ggaren committed
334
{
ap@webkit.org's avatar
ap@webkit.org committed
335
    ExecState* exec = toJS(ctx);
336
    APIEntryShim entryShim(exec);
ap@webkit.org's avatar
ap@webkit.org committed
337

338
    JSValue jsValue = toJSForGC(exec, value);
ggaren's avatar
ggaren committed
339 340
    gcUnprotect(jsValue);
}