blob: de893c14a8bbece6dc79f8ec9327eb3d1f9b81b6 [file] [log] [blame]
kocienda66a6d362001-08-24 14:24:45 +00001/*
weinig@apple.com2947a912008-07-07 02:49:29 +00002 * Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
weinig@apple.com50800142008-07-05 05:44:38 +00003 * Copyright (C) 2008 Apple Inc. All Rights Reserved.
kocienda66a6d362001-08-24 14:24:45 +00004 *
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
mjscdff33b2006-01-23 21:41:36 +000017 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
ggaren07d4ce62005-07-14 18:27:04 +000018 * Boston, MA 02110-1301, USA.
mjs6f821c82002-03-22 00:31:57 +000019 *
kocienda66a6d362001-08-24 14:24:45 +000020 */
21
mjsb64c50a2005-10-03 21:13:12 +000022#include "config.h"
cwzwarich@webkit.org0b51a732008-11-05 23:21:32 +000023#include "Operations.h"
darineba979c2005-09-04 01:18:13 +000024
weinig@apple.com50800142008-07-05 05:44:38 +000025#include "Error.h"
fpizlo@apple.comfb7eff22014-02-11 01:45:50 +000026#include "JSCInlines.h"
darin@apple.com3dcb6362008-06-16 04:00:19 +000027#include "JSObject.h"
weinig@apple.com50800142008-07-05 05:44:38 +000028#include "JSString.h"
darin6c5f29d2006-07-05 04:13:32 +000029#include <wtf/MathExtras.h>
kocienda66a6d362001-08-24 14:24:45 +000030
cwzwarich@webkit.org3f782f62008-09-08 01:28:33 +000031namespace JSC {
darin140be372005-04-20 10:14:35 +000032
ggaren@apple.comdc067b62009-05-01 22:43:39 +000033bool JSValue::equalSlowCase(ExecState* exec, JSValue v1, JSValue v2)
mjs@apple.com8ac54692008-09-25 00:26:38 +000034{
35 return equalSlowCaseInline(exec, v1, v2);
kocienda66a6d362001-08-24 14:24:45 +000036}
37
barraclough@apple.comb749f0b2009-12-07 23:14:04 +000038bool JSValue::strictEqualSlowCase(ExecState* exec, JSValue v1, JSValue v2)
mjs@apple.com1011aaa2008-09-16 06:08:51 +000039{
barraclough@apple.comb749f0b2009-12-07 23:14:04 +000040 return strictEqualSlowCaseInline(exec, v1, v2);
mrowe@apple.com2f6dfdf2008-05-22 01:20:45 +000041}
42
ggaren@apple.comdc067b62009-05-01 22:43:39 +000043NEVER_INLINE JSValue jsAddSlowCase(CallFrame* callFrame, JSValue v1, JSValue v2)
ggaren@apple.combb639262009-02-20 06:04:21 +000044{
45 // exception for the Date exception in defaultValue()
ggaren@apple.comdc067b62009-05-01 22:43:39 +000046 JSValue p1 = v1.toPrimitive(callFrame);
47 JSValue p2 = v2.toPrimitive(callFrame);
ggaren@apple.combb639262009-02-20 06:04:21 +000048
ggaren@apple.com64be5e92012-01-24 07:34:10 +000049 if (p1.isString())
50 return jsString(callFrame, asString(p1), p2.toString(callFrame));
51
barraclough@apple.comb61916c2009-12-11 22:57:39 +000052 if (p2.isString())
ggaren@apple.com64be5e92012-01-24 07:34:10 +000053 return jsString(callFrame, p1.toString(callFrame), asString(p2));
ggaren@apple.combb639262009-02-20 06:04:21 +000054
oliver@apple.com5b67d9e2010-10-25 22:40:53 +000055 return jsNumber(p1.toNumber(callFrame) + p2.toNumber(callFrame));
ggaren@apple.combb639262009-02-20 06:04:21 +000056}
57
ggaren@apple.com9a9a4b52013-04-18 19:32:17 +000058JSValue jsTypeStringForValue(VM& vm, JSGlobalObject* globalObject, JSValue v)
ggaren@apple.combb639262009-02-20 06:04:21 +000059{
60 if (v.isUndefined())
ggaren@apple.com9a9a4b52013-04-18 19:32:17 +000061 return vm.smallStrings.undefinedString();
ggaren@apple.combb639262009-02-20 06:04:21 +000062 if (v.isBoolean())
ggaren@apple.com9a9a4b52013-04-18 19:32:17 +000063 return vm.smallStrings.booleanString();
ggaren@apple.combb639262009-02-20 06:04:21 +000064 if (v.isNumber())
ggaren@apple.com9a9a4b52013-04-18 19:32:17 +000065 return vm.smallStrings.numberString();
ggaren@apple.combb639262009-02-20 06:04:21 +000066 if (v.isString())
ggaren@apple.com9a9a4b52013-04-18 19:32:17 +000067 return vm.smallStrings.stringString();
utatane.tea@gmail.com947fa4e2015-01-31 01:23:56 +000068 if (v.isSymbol())
69 return vm.smallStrings.symbolString();
ggaren@apple.combb639262009-02-20 06:04:21 +000070 if (v.isObject()) {
fpizlo@apple.com19f3e012015-04-29 21:27:48 +000071 JSObject* object = asObject(v);
ggaren@apple.combb639262009-02-20 06:04:21 +000072 // Return "undefined" for objects that should be treated
73 // as null when doing comparisons.
fpizlo@apple.com19f3e012015-04-29 21:27:48 +000074 if (object->structure(vm)->masqueradesAsUndefined(globalObject))
ggaren@apple.com9a9a4b52013-04-18 19:32:17 +000075 return vm.smallStrings.undefinedString();
fpizlo@apple.com19f3e012015-04-29 21:27:48 +000076 if (object->type() == JSFunctionType)
ggaren@apple.com9a9a4b52013-04-18 19:32:17 +000077 return vm.smallStrings.functionString();
fpizlo@apple.com19f3e012015-04-29 21:27:48 +000078 if (object->inlineTypeFlags() & TypeOfShouldCallGetCallData) {
79 CallData callData;
80 JSObject* object = asObject(v);
utatane.tea@gmail.comf76f1b42016-03-05 17:01:04 +000081 if (object->methodTable(vm)->getCallData(object, callData) != CallType::None)
fpizlo@apple.com19f3e012015-04-29 21:27:48 +000082 return vm.smallStrings.functionString();
83 }
ggaren@apple.combb639262009-02-20 06:04:21 +000084 }
ggaren@apple.com9a9a4b52013-04-18 19:32:17 +000085 return vm.smallStrings.objectString();
oliver@apple.come722ad02013-01-09 02:37:29 +000086}
87
88JSValue jsTypeStringForValue(CallFrame* callFrame, JSValue v)
89{
ggaren@apple.com9a9a4b52013-04-18 19:32:17 +000090 return jsTypeStringForValue(callFrame->vm(), callFrame->lexicalGlobalObject(), v);
ggaren@apple.combb639262009-02-20 06:04:21 +000091}
92
utatane.tea@gmail.com0bfb74c2015-02-24 23:01:58 +000093bool jsIsObjectTypeOrNull(CallFrame* callFrame, JSValue v)
ggaren@apple.combb639262009-02-20 06:04:21 +000094{
95 if (!v.isCell())
96 return v.isNull();
97
mhahnenberg@apple.comb6f85192014-02-27 01:27:18 +000098 JSType type = v.asCell()->type();
utatane.tea@gmail.com947fa4e2015-01-31 01:23:56 +000099 if (type == StringType || type == SymbolType)
ggaren@apple.combb639262009-02-20 06:04:21 +0000100 return false;
weinig@apple.com58576b22011-09-16 21:34:20 +0000101 if (type >= ObjectType) {
mhahnenberg@apple.comb6f85192014-02-27 01:27:18 +0000102 if (asObject(v)->structure(callFrame->vm())->masqueradesAsUndefined(callFrame->lexicalGlobalObject()))
ggaren@apple.combb639262009-02-20 06:04:21 +0000103 return false;
104 CallData callData;
mhahnenberg@apple.com6fb47cf2011-10-10 22:32:00 +0000105 JSObject* object = asObject(v);
utatane.tea@gmail.comf76f1b42016-03-05 17:01:04 +0000106 if (object->methodTable(callFrame->vm())->getCallData(object, callData) != CallType::None)
ggaren@apple.combb639262009-02-20 06:04:21 +0000107 return false;
108 }
109 return true;
110}
111
ggaren@apple.comdc067b62009-05-01 22:43:39 +0000112bool jsIsFunctionType(JSValue v)
ggaren@apple.combb639262009-02-20 06:04:21 +0000113{
114 if (v.isObject()) {
115 CallData callData;
mhahnenberg@apple.com6fb47cf2011-10-10 22:32:00 +0000116 JSObject* object = asObject(v);
utatane.tea@gmail.comf76f1b42016-03-05 17:01:04 +0000117 if (object->methodTable()->getCallData(object, callData) != CallType::None)
ggaren@apple.combb639262009-02-20 06:04:21 +0000118 return true;
119 }
120 return false;
121}
122
cwzwarich@webkit.org3f782f62008-09-08 01:28:33 +0000123} // namespace JSC