]> git.saurik.com Git - apple/javascriptcore.git/blob - runtime/Operations.cpp
JavaScriptCore-7601.1.46.3.tar.gz
[apple/javascriptcore.git] / runtime / Operations.cpp
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
31 namespace JSC {
32
33 bool JSValue::equalSlowCase(ExecState* exec, JSValue v1, JSValue v2)
34 {
35 return equalSlowCaseInline(exec, v1, v2);
36 }
37
38 bool JSValue::strictEqualSlowCase(ExecState* exec, JSValue v1, JSValue v2)
39 {
40 return strictEqualSlowCaseInline(exec, v1, v2);
41 }
42
43 NEVER_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
58 JSValue 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.isSymbol())
69 return vm.smallStrings.symbolString();
70 if (v.isObject()) {
71 JSObject* object = asObject(v);
72 // Return "undefined" for objects that should be treated
73 // as null when doing comparisons.
74 if (object->structure(vm)->masqueradesAsUndefined(globalObject))
75 return vm.smallStrings.undefinedString();
76 if (object->type() == JSFunctionType)
77 return vm.smallStrings.functionString();
78 if (object->inlineTypeFlags() & TypeOfShouldCallGetCallData) {
79 CallData callData;
80 JSObject* object = asObject(v);
81 if (object->methodTable(vm)->getCallData(object, callData) != CallTypeNone)
82 return vm.smallStrings.functionString();
83 }
84 }
85 return vm.smallStrings.objectString();
86 }
87
88 JSValue jsTypeStringForValue(CallFrame* callFrame, JSValue v)
89 {
90 return jsTypeStringForValue(callFrame->vm(), callFrame->lexicalGlobalObject(), v);
91 }
92
93 bool jsIsObjectTypeOrNull(CallFrame* callFrame, JSValue v)
94 {
95 if (!v.isCell())
96 return v.isNull();
97
98 JSType type = v.asCell()->type();
99 if (type == StringType || type == SymbolType)
100 return false;
101 if (type >= ObjectType) {
102 if (asObject(v)->structure(callFrame->vm())->masqueradesAsUndefined(callFrame->lexicalGlobalObject()))
103 return false;
104 CallData callData;
105 JSObject* object = asObject(v);
106 if (object->methodTable(callFrame->vm())->getCallData(object, callData) != CallTypeNone)
107 return false;
108 }
109 return true;
110 }
111
112 bool jsIsFunctionType(JSValue v)
113 {
114 if (v.isObject()) {
115 CallData callData;
116 JSObject* object = asObject(v);
117 if (object->methodTable()->getCallData(object, callData) != CallTypeNone)
118 return true;
119 }
120 return false;
121 }
122
123 } // namespace JSC