JSValueRef.cpp 7.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"

29
#include <wtf/Platform.h>
ggaren's avatar
ggaren committed
30
#include "APICast.h"
ggaren's avatar
ggaren committed
31 32
#include "JSCallbackObject.h"

33
#include <kjs/JSGlobalObject.h>
darin@apple.com's avatar
darin@apple.com committed
34
#include <kjs/JSString.h>
ggaren's avatar
ggaren committed
35 36 37
#include <kjs/operations.h>
#include <kjs/protect.h>
#include <kjs/ustring.h>
darin@apple.com's avatar
darin@apple.com committed
38
#include <kjs/JSValue.h>
ggaren's avatar
ggaren committed
39 40 41 42 43

#include <wtf/Assertions.h>

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

mjs's avatar
mjs committed
44
JSType JSValueGetType(JSContextRef, JSValueRef value)
ggaren's avatar
ggaren committed
45
{
46
    JSC::JSValue* jsValue = toJS(value);
darin@apple.com's avatar
darin@apple.com committed
47 48 49 50 51 52 53 54 55 56 57 58
    if (jsValue->isUndefined())
        return kJSTypeUndefined;
    if (jsValue->isNull())
        return kJSTypeNull;
    if (jsValue->isBoolean())
        return kJSTypeBoolean;
    if (jsValue->isNumber())
        return kJSTypeNumber;
    if (jsValue->isString())
        return kJSTypeString;
    ASSERT(jsValue->isObject());
    return kJSTypeObject;
ggaren's avatar
ggaren committed
59 60
}

61
using namespace JSC; // placed here to avoid conflict between JSC::JSType and JSType, above.
ggaren's avatar
ggaren committed
62

mjs's avatar
mjs committed
63
bool JSValueIsUndefined(JSContextRef, JSValueRef value)
ggaren's avatar
ggaren committed
64 65 66 67 68
{
    JSValue* jsValue = toJS(value);
    return jsValue->isUndefined();
}

mjs's avatar
mjs committed
69
bool JSValueIsNull(JSContextRef, JSValueRef value)
ggaren's avatar
ggaren committed
70 71 72 73 74
{
    JSValue* jsValue = toJS(value);
    return jsValue->isNull();
}

mjs's avatar
mjs committed
75
bool JSValueIsBoolean(JSContextRef, JSValueRef value)
ggaren's avatar
ggaren committed
76 77 78 79 80
{
    JSValue* jsValue = toJS(value);
    return jsValue->isBoolean();
}

mjs's avatar
mjs committed
81
bool JSValueIsNumber(JSContextRef, JSValueRef value)
ggaren's avatar
ggaren committed
82 83 84 85 86
{
    JSValue* jsValue = toJS(value);
    return jsValue->isNumber();
}

mjs's avatar
mjs committed
87
bool JSValueIsString(JSContextRef, JSValueRef value)
ggaren's avatar
ggaren committed
88 89 90 91 92
{
    JSValue* jsValue = toJS(value);
    return jsValue->isString();
}

mjs's avatar
mjs committed
93
bool JSValueIsObject(JSContextRef, JSValueRef value)
ggaren's avatar
ggaren committed
94 95 96 97 98
{
    JSValue* jsValue = toJS(value);
    return jsValue->isObject();
}

mjs's avatar
mjs committed
99
bool JSValueIsObjectOfClass(JSContextRef, JSValueRef value, JSClassRef jsClass)
ggaren's avatar
ggaren committed
100 101 102
{
    JSValue* jsValue = toJS(value);
    
103 104 105 106 107 108
    if (JSObject* o = jsValue->getObject()) {
        if (o->inherits(&JSCallbackObject<JSGlobalObject>::info))
            return static_cast<JSCallbackObject<JSGlobalObject>*>(o)->inherits(jsClass);
        else if (o->inherits(&JSCallbackObject<JSObject>::info))
            return static_cast<JSCallbackObject<JSObject>*>(o)->inherits(jsClass);
    }
ggaren's avatar
ggaren committed
109 110 111
    return false;
}

mjs's avatar
mjs committed
112
bool JSValueIsEqual(JSContextRef ctx, JSValueRef a, JSValueRef b, JSValueRef* exception)
ggaren's avatar
ggaren committed
113
{
mjs's avatar
mjs committed
114
    ExecState* exec = toJS(ctx);
darin@apple.com's avatar
darin@apple.com committed
115
    exec->globalData().heap.registerThread();
ap@webkit.org's avatar
ap@webkit.org committed
116
    JSLock lock(exec);
ap@webkit.org's avatar
ap@webkit.org committed
117

ggaren's avatar
ggaren committed
118 119 120
    JSValue* jsA = toJS(a);
    JSValue* jsB = toJS(b);

ggaren's avatar
ggaren committed
121 122 123 124
    bool result = equal(exec, jsA, jsB); // false if an exception is thrown
    if (exec->hadException()) {
        if (exception)
            *exception = toRef(exec->exception());
ggaren's avatar
ggaren committed
125
        exec->clearException();
ggaren's avatar
ggaren committed
126
    }
ggaren's avatar
ggaren committed
127 128 129
    return result;
}

ap@webkit.org's avatar
ap@webkit.org committed
130
bool JSValueIsStrictEqual(JSContextRef, JSValueRef a, JSValueRef b)
ggaren's avatar
ggaren committed
131 132 133 134
{
    JSValue* jsA = toJS(a);
    JSValue* jsB = toJS(b);
    
135
    bool result = strictEqual(jsA, jsB);
ggaren's avatar
ggaren committed
136 137 138
    return result;
}

mjs's avatar
mjs committed
139
bool JSValueIsInstanceOfConstructor(JSContextRef ctx, JSValueRef value, JSObjectRef constructor, JSValueRef* exception)
ggaren's avatar
ggaren committed
140
{
mjs's avatar
mjs committed
141
    ExecState* exec = toJS(ctx);
darin@apple.com's avatar
darin@apple.com committed
142
    exec->globalData().heap.registerThread();
ap@webkit.org's avatar
ap@webkit.org committed
143
    JSLock lock(exec);
ap@webkit.org's avatar
ap@webkit.org committed
144

ggaren's avatar
ggaren committed
145
    JSValue* jsValue = toJS(value);
ggaren's avatar
ggaren committed
146
    JSObject* jsConstructor = toJS(constructor);
mjs@apple.com's avatar
mjs@apple.com committed
147
    if (!jsConstructor->structureID()->typeInfo().implementsHasInstance())
ggaren's avatar
ggaren committed
148
        return false;
149
    bool result = jsConstructor->hasInstance(exec, jsValue, jsConstructor->get(exec, exec->propertyNames().prototype)); // false if an exception is thrown
ggaren's avatar
ggaren committed
150 151 152
    if (exec->hadException()) {
        if (exception)
            *exception = toRef(exec->exception());
ggaren's avatar
ggaren committed
153
        exec->clearException();
ggaren's avatar
ggaren committed
154
    }
ggaren's avatar
ggaren committed
155 156 157
    return result;
}

mjs's avatar
mjs committed
158
JSValueRef JSValueMakeUndefined(JSContextRef)
ggaren's avatar
ggaren committed
159 160 161 162
{
    return toRef(jsUndefined());
}

mjs's avatar
mjs committed
163
JSValueRef JSValueMakeNull(JSContextRef)
ggaren's avatar
ggaren committed
164 165 166 167
{
    return toRef(jsNull());
}

mjs's avatar
mjs committed
168
JSValueRef JSValueMakeBoolean(JSContextRef, bool value)
ggaren's avatar
ggaren committed
169 170 171 172
{
    return toRef(jsBoolean(value));
}

ap@webkit.org's avatar
ap@webkit.org committed
173
JSValueRef JSValueMakeNumber(JSContextRef ctx, double value)
ggaren's avatar
ggaren committed
174
{
ap@webkit.org's avatar
ap@webkit.org committed
175
    ExecState* exec = toJS(ctx);
darin@apple.com's avatar
darin@apple.com committed
176
    exec->globalData().heap.registerThread();
ap@webkit.org's avatar
ap@webkit.org committed
177
    JSLock lock(exec);
ap@webkit.org's avatar
ap@webkit.org committed
178 179

    return toRef(jsNumber(exec, value));
ggaren's avatar
ggaren committed
180 181
}

ap@webkit.org's avatar
ap@webkit.org committed
182
JSValueRef JSValueMakeString(JSContextRef ctx, JSStringRef string)
ggaren's avatar
ggaren committed
183
{
ap@webkit.org's avatar
ap@webkit.org committed
184
    ExecState* exec = toJS(ctx);
darin@apple.com's avatar
darin@apple.com committed
185
    exec->globalData().heap.registerThread();
ap@webkit.org's avatar
ap@webkit.org committed
186
    JSLock lock(exec);
ap@webkit.org's avatar
ap@webkit.org committed
187

ap@webkit.org's avatar
ap@webkit.org committed
188
    return toRef(jsString(exec, string->ustring()));
mjs's avatar
mjs committed
189 190
}

mjs@apple.com's avatar
mjs@apple.com committed
191
bool JSValueToBoolean(JSContextRef ctx, JSValueRef value)
mjs's avatar
mjs committed
192
{
mjs@apple.com's avatar
mjs@apple.com committed
193 194 195
    ExecState* exec = toJS(ctx);
    JSValue* jsValue = toJS(value);
    return jsValue->toBoolean(exec);
ggaren's avatar
ggaren committed
196 197
}

mjs's avatar
mjs committed
198
double JSValueToNumber(JSContextRef ctx, JSValueRef value, JSValueRef* exception)
ggaren's avatar
ggaren committed
199
{
mjs's avatar
mjs committed
200
    ExecState* exec = toJS(ctx);
darin@apple.com's avatar
darin@apple.com committed
201
    exec->globalData().heap.registerThread();
ap@webkit.org's avatar
ap@webkit.org committed
202
    JSLock lock(exec);
ap@webkit.org's avatar
ap@webkit.org committed
203 204

    JSValue* jsValue = toJS(value);
ggaren's avatar
ggaren committed
205 206

    double number = jsValue->toNumber(exec);
ggaren's avatar
ggaren committed
207
    if (exec->hadException()) {
ggaren's avatar
ggaren committed
208 209
        if (exception)
            *exception = toRef(exec->exception());
ggaren's avatar
ggaren committed
210
        exec->clearException();
ggaren's avatar
ggaren committed
211 212
        number = NaN;
    }
ggaren's avatar
ggaren committed
213 214 215
    return number;
}

mjs's avatar
mjs committed
216
JSStringRef JSValueToStringCopy(JSContextRef ctx, JSValueRef value, JSValueRef* exception)
ggaren's avatar
ggaren committed
217
{
mjs's avatar
mjs committed
218
    ExecState* exec = toJS(ctx);
darin@apple.com's avatar
darin@apple.com committed
219
    exec->globalData().heap.registerThread();
ap@webkit.org's avatar
ap@webkit.org committed
220
    JSLock lock(exec);
ap@webkit.org's avatar
ap@webkit.org committed
221 222

    JSValue* jsValue = toJS(value);
ggaren's avatar
ggaren committed
223
    
ap@webkit.org's avatar
ap@webkit.org committed
224
    RefPtr<OpaqueJSString> stringRef(OpaqueJSString::create(jsValue->toString(exec)));
ggaren's avatar
ggaren committed
225 226 227 228
    if (exec->hadException()) {
        if (exception)
            *exception = toRef(exec->exception());
        exec->clearException();
ap@webkit.org's avatar
ap@webkit.org committed
229
        stringRef.clear();
ggaren's avatar
ggaren committed
230
    }
ap@webkit.org's avatar
ap@webkit.org committed
231
    return stringRef.release().releaseRef();
ggaren's avatar
ggaren committed
232 233
}

mjs's avatar
mjs committed
234
JSObjectRef JSValueToObject(JSContextRef ctx, JSValueRef value, JSValueRef* exception)
ggaren's avatar
ggaren committed
235
{
mjs's avatar
mjs committed
236
    ExecState* exec = toJS(ctx);
darin@apple.com's avatar
darin@apple.com committed
237
    exec->globalData().heap.registerThread();
ap@webkit.org's avatar
ap@webkit.org committed
238
    JSLock lock(exec);
ap@webkit.org's avatar
ap@webkit.org committed
239

ggaren's avatar
ggaren committed
240 241 242 243
    JSValue* jsValue = toJS(value);
    
    JSObjectRef objectRef = toRef(jsValue->toObject(exec));
    if (exec->hadException()) {
ggaren's avatar
ggaren committed
244 245
        if (exception)
            *exception = toRef(exec->exception());
ggaren's avatar
ggaren committed
246
        exec->clearException();
247
        objectRef = 0;
ggaren's avatar
ggaren committed
248 249 250 251
    }
    return objectRef;
}    

ap@webkit.org's avatar
ap@webkit.org committed
252
void JSValueProtect(JSContextRef ctx, JSValueRef value)
ggaren's avatar
ggaren committed
253
{
ap@webkit.org's avatar
ap@webkit.org committed
254
    ExecState* exec = toJS(ctx);
darin@apple.com's avatar
darin@apple.com committed
255
    exec->globalData().heap.registerThread();
ap@webkit.org's avatar
ap@webkit.org committed
256
    JSLock lock(exec);
ap@webkit.org's avatar
ap@webkit.org committed
257

ggaren's avatar
ggaren committed
258 259 260 261
    JSValue* jsValue = toJS(value);
    gcProtect(jsValue);
}

ap@webkit.org's avatar
ap@webkit.org committed
262
void JSValueUnprotect(JSContextRef ctx, JSValueRef value)
ggaren's avatar
ggaren committed
263
{
ap@webkit.org's avatar
ap@webkit.org committed
264
    ExecState* exec = toJS(ctx);
darin@apple.com's avatar
darin@apple.com committed
265
    exec->globalData().heap.registerThread();
ap@webkit.org's avatar
ap@webkit.org committed
266
    JSLock lock(exec);
ap@webkit.org's avatar
ap@webkit.org committed
267

ggaren's avatar
ggaren committed
268 269 270
    JSValue* jsValue = toJS(value);
    gcUnprotect(jsValue);
}