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(JSGlobalObject* globalObject, JSValue v1, JSValue v2)
35{
36 return equalSlowCaseInline(globalObject, v1, v2);
37}
38
39bool JSValue::strictEqualSlowCase(JSGlobalObject* globalObject, JSValue v1, JSValue v2)
40{
41 return strictEqualSlowCaseInline(globalObject, v1, v2);
42}
43
44NEVER_INLINE JSValue jsAddSlowCase(JSGlobalObject* globalObject, JSValue v1, JSValue v2)
45{
46 // exception for the Date exception in defaultValue()
47 VM& vm = globalObject->vm();
48 auto scope = DECLARE_THROW_SCOPE(vm);
49 JSValue p1 = v1.toPrimitive(globalObject);
50 RETURN_IF_EXCEPTION(scope, { });
51 JSValue p2 = v2.toPrimitive(globalObject);
52 RETURN_IF_EXCEPTION(scope, { });
53
54 if (p1.isString()) {
55 if (p2.isCell()) {
56 JSString* p2String = p2.toString(globalObject);
57 RETURN_IF_EXCEPTION(scope, { });
58 RELEASE_AND_RETURN(scope, jsString(globalObject, asString(p1), p2String));
59 }
60 String p2String = p2.toWTFString(globalObject);
61 RETURN_IF_EXCEPTION(scope, { });
62 RELEASE_AND_RETURN(scope, jsString(globalObject, asString(p1), p2String));
63 }
64
65 if (p2.isString()) {
66 if (p1.isCell()) {
67 JSString* p1String = p1.toString(globalObject);
68 RETURN_IF_EXCEPTION(scope, { });
69 RELEASE_AND_RETURN(scope, jsString(globalObject, p1String, asString(p2)));
70 }
71 String p1String = p1.toWTFString(globalObject);
72 RETURN_IF_EXCEPTION(scope, { });
73 RELEASE_AND_RETURN(scope, jsString(globalObject, p1String, asString(p2)));
74 }
75
76 auto leftNumeric = p1.toNumeric(globalObject);
77 RETURN_IF_EXCEPTION(scope, { });
78 auto rightNumeric = p2.toNumeric(globalObject);
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(globalObject, WTF::get<JSBigInt*>(leftNumeric), WTF::get<JSBigInt*>(rightNumeric));
85 }
86
87 return throwTypeError(globalObject, 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(JSGlobalObject* globalObject, JSValue v)
120{
121 return jsTypeStringForValue(globalObject->vm(), globalObject, v);
122}
123
124bool jsIsObjectTypeOrNull(JSGlobalObject* globalObject, JSValue v)
125{
126 VM& vm = globalObject->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(globalObject))
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(JSGlobalObject* globalObject, JSCell* base, bool& sawPolyProto)
144{
145 VM& vm = globalObject->vm();
146 size_t count = 0;
147 sawPolyProto = false;
148 JSCell* current = base;
149 while (1) {
150 Structure* structure = current->structure(vm);
151 if (structure->isProxy())
152 return InvalidPrototypeChain;
153
154 sawPolyProto |= structure->hasPolyProto();
155
156 JSValue prototype = structure->prototypeForLookup(globalObject, current);
157 if (prototype.isNull())
158 return count;
159
160 current = prototype.asCell();
161 structure = current->structure(vm);
162 if (structure->isDictionary()) {
163 if (structure->hasBeenFlattenedBefore())
164 return InvalidPrototypeChain;
165 structure->flattenDictionaryStructure(vm, asObject(current));
166 }
167
168 ++count;
169 }
170}
171
172} // namespace JSC
173