]> git.saurik.com Git - apple/javascriptcore.git/blame - runtime/Operations.cpp
JavaScriptCore-554.1.tar.gz
[apple/javascriptcore.git] / runtime / Operations.cpp
CommitLineData
b37bf2e1 1/*
9dae56ea
A
2 * Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
3 * Copyright (C) 2008 Apple Inc. All Rights Reserved.
b37bf2e1
A
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
b37bf2e1 22#include "config.h"
9dae56ea
A
23#include "Operations.h"
24
25#include "Error.h"
26#include "JSObject.h"
27#include "JSString.h"
28#include <math.h>
29#include <stdio.h>
30#include <wtf/MathExtras.h>
b37bf2e1 31
9dae56ea
A
32#if HAVE(FLOAT_H)
33#include <float.h>
34#endif
b37bf2e1 35
9dae56ea
A
36namespace JSC {
37
ba379fdc 38bool JSValue::equalSlowCase(ExecState* exec, JSValue v1, JSValue v2)
b37bf2e1 39{
9dae56ea 40 return equalSlowCaseInline(exec, v1, v2);
b37bf2e1
A
41}
42
ba379fdc 43bool JSValue::strictEqualSlowCase(JSValue v1, JSValue v2)
b37bf2e1 44{
9dae56ea 45 return strictEqualSlowCaseInline(v1, v2);
b37bf2e1
A
46}
47
ba379fdc 48NEVER_INLINE JSValue throwOutOfMemoryError(ExecState* exec)
b37bf2e1 49{
9dae56ea
A
50 JSObject* error = Error::create(exec, GeneralError, "Out of memory");
51 exec->setException(error);
52 return error;
b37bf2e1
A
53}
54
ba379fdc
A
55NEVER_INLINE JSValue jsAddSlowCase(CallFrame* callFrame, JSValue v1, JSValue v2)
56{
57 // exception for the Date exception in defaultValue()
58 JSValue p1 = v1.toPrimitive(callFrame);
59 JSValue p2 = v2.toPrimitive(callFrame);
60
61 if (p1.isString() || p2.isString()) {
62 RefPtr<UString::Rep> value = concatenate(p1.toString(callFrame).rep(), p2.toString(callFrame).rep());
63 if (!value)
64 return throwOutOfMemoryError(callFrame);
65 return jsString(callFrame, value.release());
66 }
67
68 return jsNumber(callFrame, p1.toNumber(callFrame) + p2.toNumber(callFrame));
69}
70
71JSValue jsTypeStringForValue(CallFrame* callFrame, JSValue v)
72{
73 if (v.isUndefined())
74 return jsNontrivialString(callFrame, "undefined");
75 if (v.isBoolean())
76 return jsNontrivialString(callFrame, "boolean");
77 if (v.isNumber())
78 return jsNontrivialString(callFrame, "number");
79 if (v.isString())
80 return jsNontrivialString(callFrame, "string");
81 if (v.isObject()) {
82 // Return "undefined" for objects that should be treated
83 // as null when doing comparisons.
84 if (asObject(v)->structure()->typeInfo().masqueradesAsUndefined())
85 return jsNontrivialString(callFrame, "undefined");
86 CallData callData;
87 if (asObject(v)->getCallData(callData) != CallTypeNone)
88 return jsNontrivialString(callFrame, "function");
89 }
90 return jsNontrivialString(callFrame, "object");
91}
92
93bool jsIsObjectType(JSValue v)
94{
95 if (!v.isCell())
96 return v.isNull();
97
98 JSType type = asCell(v)->structure()->typeInfo().type();
99 if (type == NumberType || type == StringType)
100 return false;
101 if (type == ObjectType) {
102 if (asObject(v)->structure()->typeInfo().masqueradesAsUndefined())
103 return false;
104 CallData callData;
105 if (asObject(v)->getCallData(callData) != CallTypeNone)
106 return false;
107 }
108 return true;
109}
110
111bool jsIsFunctionType(JSValue v)
112{
113 if (v.isObject()) {
114 CallData callData;
115 if (asObject(v)->getCallData(callData) != CallTypeNone)
116 return true;
117 }
118 return false;
119}
120
9dae56ea 121} // namespace JSC