1/*
2 * Copyright (C) 1999-2000 Harri Porten ([email protected])
3 * Copyright (C) 2008, 2016 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 "JSBigInt.h"
27#include "JSCInlines.h"
28#include "JSObject.h"
29#include "JSString.h"
30#include <wtf/MathExtras.h>
31
32namespace JSC {
33
34bool JSValue::equalSlowCase(ExecState* exec, JSValue v1, JSValue v2)
35{
36 return equalSlowCaseInline(exec, v1, v2);
37}
38
39bool JSValue::strictEqualSlowCase(ExecState* exec, JSValue v1, JSValue v2)
40{
41 return strictEqualSlowCaseInline(exec, v1, v2);
42}
43
44NEVER_INLINE JSValue jsAddSlowCase(CallFrame* callFrame, JSValue v1, JSValue v2)
45{
46 // exception for the Date exception in defaultValue()
47 VM& vm = callFrame->vm();
48 auto scope = DECLARE_THROW_SCOPE(vm);
49 JSValue p1 = v1.toPrimitive(callFrame);
50 RETURN_IF_EXCEPTION(scope, { });
51 JSValue p2 = v2.toPrimitive(callFrame);
52 RETURN_IF_EXCEPTION(scope, { });
53
54 if (p1.isString()) {
55 if (p2.isCell()) {
56 JSString* p2String = p2.toString(callFrame);
57 RETURN_IF_EXCEPTION(scope, { });
58 RELEASE_AND_RETURN(scope, jsString(callFrame, asString(p1), p2String));
59 }
60 String p2String = p2.toWTFString(callFrame);
61 RETURN_IF_EXCEPTION(scope, { });
62 RELEASE_AND_RETURN(scope, jsString(callFrame, asString(p1), p2String));
63 }
64
65 if (p2.isString()) {
66 if (p1.isCell()) {
67 JSString* p1String = p1.toString(callFrame);
68 RETURN_IF_EXCEPTION(scope, { });
69 RELEASE_AND_RETURN(scope, jsString(callFrame, p1String, asString(p2)));
70 }
71 String p1String = p1.toWTFString(callFrame);
72 RETURN_IF_EXCEPTION(scope, { });
73 RELEASE_AND_RETURN(scope, jsString(callFrame, p1String, asString(p2)));
74 }
75
76 auto leftNumeric = p1.toNumeric(callFrame);
77 RETURN_IF_EXCEPTION(scope, { });
78 auto rightNumeric = p2.toNumeric(callFrame);
79 RETURN_IF_EXCEPTION(scope, { });
80
81 if (WTF::holds_alternative<JSBigInt*>(leftNumeric) || WTF::holds_alternative<JSBigInt*>(rightNumeric)) {
82 if (WTF::holds_alternative<JSBigInt*>(leftNumeric) && WTF::holds_alternative<JSBigInt*>(rightNumeric)) {
83 scope.release();
84 return JSBigInt::add(callFrame, WTF::get<JSBigInt*>(leftNumeric), WTF::get<JSBigInt*>(rightNumeric));
85 }
86
87 return throwTypeError(callFrame, scope, "Invalid mix of BigInt and other type in addition."_s);
88 }
89
90 return jsNumber(WTF::get<double>(leftNumeric) + WTF::get<double>(rightNumeric));
91}
92
93JSValue jsTypeStringForValue(VM& vm, JSGlobalObject* globalObject, JSValue v)
94{
95 if (v.isUndefined())
96 return vm.smallStrings.undefinedString();
97 if (v.isBoolean())
98 return vm.smallStrings.booleanString();
99 if (v.isNumber())
100 return vm.smallStrings.numberString();
101 if (v.isString())
102 return vm.smallStrings.stringString();
103 if (v.isSymbol())
104 return vm.smallStrings.symbolString();
105 if (v.isBigInt())
106 return vm.smallStrings.bigintString();
107 if (v.isObject()) {
108 JSObject* object = asObject(v);
109 // Return "undefined" for objects that should be treated
110 // as null when doing comparisons.
111 if (object->structure(vm)->masqueradesAsUndefined(globalObject))
112 return vm.smallStrings.undefinedString();
113 if (object->isFunction(vm))
114 return vm.smallStrings.functionString();
115 }
116 return vm.smallStrings.objectString();
117}
118
119JSValue jsTypeStringForValue(CallFrame* callFrame, JSValue v)
120{
121 return jsTypeStringForValue(callFrame->vm(), callFrame->lexicalGlobalObject(), v);
122}
123
124bool jsIsObjectTypeOrNull(CallFrame* callFrame, JSValue v)
125{
126 VM& vm = callFrame->vm();
127 if (!v.isCell())
128 return v.isNull();
129
130 JSType type = v.asCell()->type();
131 if (type == StringType || type == SymbolType || type == BigIntType)
132 return false;
133 if (type >= ObjectType) {
134 if (asObject(v)->structure(vm)->masqueradesAsUndefined(callFrame->lexicalGlobalObject()))
135 return false;
136 JSObject* object = asObject(v);
137 if (object->isFunction(vm))
138 return false;
139 }
140 return true;
141}
142
143size_t normalizePrototypeChain(CallFrame* callFrame, JSCell* base, bool& sawPolyProto)
144{
145 VM& vm = callFrame->vm();
146 size_t count = 0;
147 sawPolyProto = false;
148 JSCell* current = base;
149 JSGlobalObject* globalObject = callFrame->lexicalGlobalObject();
150 while (1) {
151 Structure* structure = current->structure(vm);
152 if (structure->isProxy())
153 return InvalidPrototypeChain;
154
155 sawPolyProto |= structure->hasPolyProto();
156
157 JSValue prototype = structure->prototypeForLookup(globalObject, current);
158 if (prototype.isNull())
159 return count;
160
161 current = prototype.asCell();
162 structure = current->structure(vm);
163 if (structure->isDictionary()) {
164 if (structure->hasBeenFlattenedBefore())
165 return InvalidPrototypeChain;
166 structure->flattenDictionaryStructure(vm, asObject(current));
167 }
168
169 ++count;
170 }
171}
172
173} // namespace JSC
174