1/*
2 * Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
3 * Copyright (C) 2008 Apple Inc. All Rights Reserved.
4 *
5 *  This library is free software; you can redistribute it and/or
6 *  modify it under the terms of the GNU Library General Public
7 *  License as published by the Free Software Foundation; either
8 *  version 2 of the License, or (at your option) any later version.
9 *
10 *  This library is distributed in the hope that it will be useful,
11 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 *  Library General Public License for more details.
14 *
15 *  You should have received a copy of the GNU Library General Public License
16 *  along with this library; see the file COPYING.LIB.  If not, write to
17 *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 *  Boston, MA 02110-1301, USA.
19 *
20 */
21
22#include "config.h"
23#include "Operations.h"
24
25#include "Error.h"
26#include "JSCInlines.h"
27#include "JSObject.h"
28#include "JSString.h"
29#include <wtf/MathExtras.h>
30
31namespace JSC {
32
33bool JSValue::equalSlowCase(ExecState* exec, JSValue v1, JSValue v2)
34{
35    return equalSlowCaseInline(exec, v1, v2);
36}
37
38bool JSValue::strictEqualSlowCase(ExecState* exec, JSValue v1, JSValue v2)
39{
40    return strictEqualSlowCaseInline(exec, v1, v2);
41}
42
43NEVER_INLINE JSValue jsAddSlowCase(CallFrame* callFrame, JSValue v1, JSValue v2)
44{
45    // exception for the Date exception in defaultValue()
46    JSValue p1 = v1.toPrimitive(callFrame);
47    JSValue p2 = v2.toPrimitive(callFrame);
48
49    if (p1.isString())
50        return jsString(callFrame, asString(p1), p2.toString(callFrame));
51
52    if (p2.isString())
53        return jsString(callFrame, p1.toString(callFrame), asString(p2));
54
55    return jsNumber(p1.toNumber(callFrame) + p2.toNumber(callFrame));
56}
57
58JSValue jsTypeStringForValue(VM& vm, JSGlobalObject* globalObject, JSValue v)
59{
60    if (v.isUndefined())
61        return vm.smallStrings.undefinedString();
62    if (v.isBoolean())
63        return vm.smallStrings.booleanString();
64    if (v.isNumber())
65        return vm.smallStrings.numberString();
66    if (v.isString())
67        return vm.smallStrings.stringString();
68    if (v.isObject()) {
69        // Return "undefined" for objects that should be treated
70        // as null when doing comparisons.
71        if (asObject(v)->structure(vm)->masqueradesAsUndefined(globalObject))
72            return vm.smallStrings.undefinedString();
73        CallData callData;
74        JSObject* object = asObject(v);
75        if (object->methodTable(vm)->getCallData(object, callData) != CallTypeNone)
76            return vm.smallStrings.functionString();
77    }
78    return vm.smallStrings.objectString();
79}
80
81JSValue jsTypeStringForValue(CallFrame* callFrame, JSValue v)
82{
83    return jsTypeStringForValue(callFrame->vm(), callFrame->lexicalGlobalObject(), v);
84}
85
86bool jsIsObjectType(CallFrame* callFrame, JSValue v)
87{
88    if (!v.isCell())
89        return v.isNull();
90
91    JSType type = v.asCell()->type();
92    if (type == StringType)
93        return false;
94    if (type >= ObjectType) {
95        if (asObject(v)->structure(callFrame->vm())->masqueradesAsUndefined(callFrame->lexicalGlobalObject()))
96            return false;
97        CallData callData;
98        JSObject* object = asObject(v);
99        if (object->methodTable(callFrame->vm())->getCallData(object, callData) != CallTypeNone)
100            return false;
101    }
102    return true;
103}
104
105bool jsIsFunctionType(JSValue v)
106{
107    if (v.isObject()) {
108        CallData callData;
109        JSObject* object = asObject(v);
110        if (object->methodTable()->getCallData(object, callData) != CallTypeNone)
111            return true;
112    }
113    return false;
114}
115
116} // namespace JSC
117