| 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 "JSObject.h" |
| 27 | #include "JSString.h" |
| 28 | #include <math.h> |
| 29 | #include <stdio.h> |
| 30 | #include <wtf/MathExtras.h> |
| 31 | |
| 32 | namespace JSC { |
| 33 | |
| 34 | bool JSValue::equalSlowCase(ExecState* exec, JSValue v1, JSValue v2) |
| 35 | { |
| 36 | return equalSlowCaseInline(exec, v1, v2); |
| 37 | } |
| 38 | |
| 39 | bool JSValue::strictEqualSlowCase(ExecState* exec, JSValue v1, JSValue v2) |
| 40 | { |
| 41 | return strictEqualSlowCaseInline(exec, v1, v2); |
| 42 | } |
| 43 | |
| 44 | NEVER_INLINE JSValue throwOutOfMemoryError(ExecState* exec) |
| 45 | { |
| 46 | JSObject* error = Error::create(exec, GeneralError, message: "Out of memory" ); |
| 47 | exec->setException(error); |
| 48 | return error; |
| 49 | } |
| 50 | |
| 51 | NEVER_INLINE JSValue jsAddSlowCase(CallFrame* callFrame, JSValue v1, JSValue v2) |
| 52 | { |
| 53 | // exception for the Date exception in defaultValue() |
| 54 | JSValue p1 = v1.toPrimitive(exec: callFrame); |
| 55 | JSValue p2 = v2.toPrimitive(exec: callFrame); |
| 56 | |
| 57 | if (p1.isString()) { |
| 58 | return p2.isString() |
| 59 | ? jsString(exec: callFrame, s1: asString(value: p1), s2: asString(value: p2)) |
| 60 | : jsString(exec: callFrame, s1: asString(value: p1), u2: p2.toString(exec: callFrame)); |
| 61 | } |
| 62 | if (p2.isString()) |
| 63 | return jsString(exec: callFrame, u1: p1.toString(exec: callFrame), s2: asString(value: p2)); |
| 64 | |
| 65 | return jsNumber(exec: callFrame, d: p1.toNumber(exec: callFrame) + p2.toNumber(exec: callFrame)); |
| 66 | } |
| 67 | |
| 68 | JSValue jsTypeStringForValue(CallFrame* callFrame, JSValue v) |
| 69 | { |
| 70 | if (v.isUndefined()) |
| 71 | return jsNontrivialString(exec: callFrame, s: "undefined" ); |
| 72 | if (v.isBoolean()) |
| 73 | return jsNontrivialString(exec: callFrame, s: "boolean" ); |
| 74 | if (v.isNumber()) |
| 75 | return jsNontrivialString(exec: callFrame, s: "number" ); |
| 76 | if (v.isString()) |
| 77 | return jsNontrivialString(exec: callFrame, s: "string" ); |
| 78 | if (v.isObject()) { |
| 79 | // Return "undefined" for objects that should be treated |
| 80 | // as null when doing comparisons. |
| 81 | if (asObject(value: v)->structure()->typeInfo().masqueradesAsUndefined()) |
| 82 | return jsNontrivialString(exec: callFrame, s: "undefined" ); |
| 83 | CallData callData; |
| 84 | if (asObject(value: v)->getCallData(callData) != CallTypeNone) |
| 85 | return jsNontrivialString(exec: callFrame, s: "function" ); |
| 86 | } |
| 87 | return jsNontrivialString(exec: callFrame, s: "object" ); |
| 88 | } |
| 89 | |
| 90 | bool jsIsObjectType(JSValue v) |
| 91 | { |
| 92 | if (!v.isCell()) |
| 93 | return v.isNull(); |
| 94 | |
| 95 | JSType type = asCell(value: v)->structure()->typeInfo().type(); |
| 96 | if (type == NumberType || type == StringType) |
| 97 | return false; |
| 98 | if (type == ObjectType) { |
| 99 | if (asObject(value: v)->structure()->typeInfo().masqueradesAsUndefined()) |
| 100 | return false; |
| 101 | CallData callData; |
| 102 | if (asObject(value: v)->getCallData(callData) != CallTypeNone) |
| 103 | return false; |
| 104 | } |
| 105 | return true; |
| 106 | } |
| 107 | |
| 108 | bool jsIsFunctionType(JSValue v) |
| 109 | { |
| 110 | if (v.isObject()) { |
| 111 | CallData callData; |
| 112 | if (asObject(value: v)->getCallData(callData) != CallTypeNone) |
| 113 | return true; |
| 114 | } |
| 115 | return false; |
| 116 | } |
| 117 | |
| 118 | } // namespace JSC |
| 119 | |