1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
* vim: set ts=8 sts=2 et sw=2 tw=80:
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "vm/EqualityOperations.h" // js::LooselyEqual, js::StrictlyEqual, js::SameValue
#include "mozilla/Assertions.h" // MOZ_ASSERT, MOZ_ASSERT_IF
#include "jsnum.h" // js::StringToNumber
#include "jstypes.h" // JS_PUBLIC_API
#include "js/Context.h" // js::AssertHeapIsIdle
#include "js/Equality.h" // JS::LooselyEqual, JS::StrictlyEqual, JS::SameValue
#include "js/Result.h" // JS_TRY_VAR_OR_RETURN_FALSE
#include "js/RootingAPI.h" // JS::Rooted
#include "js/Value.h" // JS::Int32Value, JS::SameType, JS::Value
#include "vm/BigIntType.h" // JS::BigInt
#include "vm/JSContext.h" // CHECK_THREAD
#include "vm/JSObject.h" // js::ToPrimitive
#include "vm/StringType.h" // js::EqualStrings
#ifdef ENABLE_RECORD_TUPLE
# include "vm/RecordType.h"
# include "vm/TupleType.h"
#endif
#include "builtin/Boolean-inl.h" // js::EmulatesUndefined
#include "vm/JSContext-inl.h" // JSContext::check
static bool EqualGivenSameType(JSContext* cx, JS::Handle<JS::Value> lval,
JS::Handle<JS::Value> rval, bool* equal) {
MOZ_ASSERT(JS::SameType(lval, rval));
if (lval.isString()) {
return js::EqualStrings(cx, lval.toString(), rval.toString(), equal);
}
if (lval.isDouble()) {
*equal = (lval.toDouble() == rval.toDouble());
return true;
}
if (lval.isBigInt()) {
*equal = JS::BigInt::equal(lval.toBigInt(), rval.toBigInt());
return true;
}
#ifdef ENABLE_RECORD_TUPLE
// Record & Tuple proposal, section 3.2.6 (Strict Equality Comparison), step 3
// - https://tc39.es/proposal-record-tuple/#sec-strict-equality-comparison
//
// When computing equality, records and tuples are compared using the
// SameValueZero algorithm.
//
// NOTE: Since Records and Tuples are impemented as ExtendedPrimitives,
// "SameType" refers to the fact that both lval and rval are
// ExtendedPrimitives. They can still be different types (for example, a
// Record and a Tuple).
if (lval.isExtendedPrimitive()) {
JSObject* lobj = &lval.toExtendedPrimitive();
JSObject* robj = &rval.toExtendedPrimitive();
if (lobj->getClass() != robj->getClass()) {
*equal = false;
return true;
}
if (lobj->is<js::RecordType>()) {
return js::RecordType::sameValueZero(cx, &lobj->as<js::RecordType>(),
&robj->as<js::RecordType>(), equal);
}
if (lobj->is<js::TupleType>()) {
return js::TupleType::sameValueZero(cx, &lobj->as<js::TupleType>(),
&robj->as<js::TupleType>(), equal);
}
MOZ_CRASH("Unknown ExtendedPrimitive type");
}
#endif
// Note: we can do a bitwise comparison even for Int32Value because both
// Values have the same type.
MOZ_ASSERT(CanUseBitwiseCompareForStrictlyEqual(lval) || lval.isInt32());
*equal = (lval.asRawBits() == rval.asRawBits());
MOZ_ASSERT_IF(lval.isUndefined() || lval.isNull(), *equal);
return true;
}
static bool LooselyEqualBooleanAndOther(JSContext* cx,
JS::Handle<JS::Value> lval,
JS::Handle<JS::Value> rval,
bool* result) {
MOZ_ASSERT(!rval.isBoolean());
JS::Rooted<JS::Value> lvalue(cx, JS::Int32Value(lval.toBoolean() ? 1 : 0));
// The tail-call would end up in Step 3.
if (rval.isNumber()) {
*result = (lvalue.toNumber() == rval.toNumber());
return true;
}
// The tail-call would end up in Step 6.
if (rval.isString()) {
double num;
if (!StringToNumber(cx, rval.toString(), &num)) {
return false;
}
*result = (lvalue.toNumber() == num);
return true;
}
return js::LooselyEqual(cx, lvalue, rval, result);
}
// ES6 draft rev32 7.2.12 Abstract Equality Comparison
bool js::LooselyEqual(JSContext* cx, JS::Handle<JS::Value> lval,
JS::Handle<JS::Value> rval, bool* result) {
// Step 3.
if (JS::SameType(lval, rval)) {
return EqualGivenSameType(cx, lval, rval, result);
}
// Handle int32 x double.
if (lval.isNumber() && rval.isNumber()) {
*result = (lval.toNumber() == rval.toNumber());
return true;
}
// Step 4. This a bit more complex, because of the undefined emulating object.
if (lval.isNullOrUndefined()) {
// We can return early here, because null | undefined is only equal to the
// same set.
*result = rval.isNullOrUndefined() ||
(rval.isObject() && EmulatesUndefined(&rval.toObject()));
return true;
}
// Step 5.
if (rval.isNullOrUndefined()) {
MOZ_ASSERT(!lval.isNullOrUndefined());
*result = lval.isObject() && EmulatesUndefined(&lval.toObject());
return true;
}
// Step 6.
if (lval.isNumber() && rval.isString()) {
double num;
if (!StringToNumber(cx, rval.toString(), &num)) {
return false;
}
*result = (lval.toNumber() == num);
return true;
}
// Step 7.
if (lval.isString() && rval.isNumber()) {
double num;
if (!StringToNumber(cx, lval.toString(), &num)) {
return false;
}
*result = (num == rval.toNumber());
return true;
}
// Step 8.
if (lval.isBoolean()) {
return LooselyEqualBooleanAndOther(cx, lval, rval, result);
}
// Step 9.
if (rval.isBoolean()) {
return LooselyEqualBooleanAndOther(cx, rval, lval, result);
}
// Step 10.
if ((lval.isString() || lval.isNumber() || lval.isSymbol()) &&
rval.isObject()) {
JS::Rooted<JS::Value> rvalue(cx, rval);
if (!ToPrimitive(cx, &rvalue)) {
return false;
}
return js::LooselyEqual(cx, lval, rvalue, result);
}
// Step 11.
if (lval.isObject() &&
(rval.isString() || rval.isNumber() || rval.isSymbol())) {
JS::Rooted<JS::Value> lvalue(cx, lval);
if (!ToPrimitive(cx, &lvalue)) {
return false;
}
return js::LooselyEqual(cx, lvalue, rval, result);
}
if (lval.isBigInt()) {
JS::Rooted<JS::BigInt*> lbi(cx, lval.toBigInt());
bool tmpResult;
JS_TRY_VAR_OR_RETURN_FALSE(cx, tmpResult,
JS::BigInt::looselyEqual(cx, lbi, rval));
*result = tmpResult;
return true;
}
if (rval.isBigInt()) {
JS::Rooted<JS::BigInt*> rbi(cx, rval.toBigInt());
bool tmpResult;
JS_TRY_VAR_OR_RETURN_FALSE(cx, tmpResult,
JS::BigInt::looselyEqual(cx, rbi, lval));
*result = tmpResult;
return true;
}
// Step 12.
*result = false;
return true;
}
JS_PUBLIC_API bool JS::LooselyEqual(JSContext* cx, Handle<Value> value1,
Handle<Value> value2, bool* equal) {
js::AssertHeapIsIdle();
CHECK_THREAD(cx);
cx->check(value1, value2);
MOZ_ASSERT(equal);
return js::LooselyEqual(cx, value1, value2, equal);
}
bool js::StrictlyEqual(JSContext* cx, JS::Handle<JS::Value> lval,
JS::Handle<JS::Value> rval, bool* equal) {
if (SameType(lval, rval)) {
return EqualGivenSameType(cx, lval, rval, equal);
}
if (lval.isNumber() && rval.isNumber()) {
*equal = (lval.toNumber() == rval.toNumber());
return true;
}
*equal = false;
return true;
}
JS_PUBLIC_API bool JS::StrictlyEqual(JSContext* cx, Handle<Value> value1,
Handle<Value> value2, bool* equal) {
js::AssertHeapIsIdle();
CHECK_THREAD(cx);
cx->check(value1, value2);
MOZ_ASSERT(equal);
return js::StrictlyEqual(cx, value1, value2, equal);
}
static inline bool IsNegativeZero(const JS::Value& v) {
return v.isDouble() && mozilla::IsNegativeZero(v.toDouble());
}
static inline bool IsNaN(const JS::Value& v) {
return v.isDouble() && std::isnan(v.toDouble());
}
bool js::SameValue(JSContext* cx, JS::Handle<JS::Value> v1,
JS::Handle<JS::Value> v2, bool* same) {
if (IsNegativeZero(v1)) {
*same = IsNegativeZero(v2);
return true;
}
if (IsNegativeZero(v2)) {
*same = false;
return true;
}
#ifdef ENABLE_RECORD_TUPLE
if (v1.isExtendedPrimitive()) {
JSObject* lobj = &v1.toExtendedPrimitive();
JSObject* robj = &v2.toExtendedPrimitive();
if (lobj->getClass() != robj->getClass()) {
*same = false;
return true;
}
if (lobj->is<js::RecordType>()) {
return js::RecordType::sameValue(cx, &lobj->as<js::RecordType>(),
&robj->as<js::RecordType>(), same);
}
if (lobj->is<js::TupleType>()) {
return js::TupleType::sameValue(cx, &lobj->as<js::TupleType>(),
&robj->as<js::TupleType>(), same);
}
MOZ_CRASH("Unknown ExtendedPrimitive type");
}
#endif
return js::SameValueZero(cx, v1, v2, same);
}
#ifdef ENABLE_RECORD_TUPLE
bool js::SameValueZeroLinear(const JS::Value& lval, const JS::Value& rval) {
if (lval.isNumber() && rval.isNumber()) {
return IsNaN(lval) ? IsNaN(rval) : lval.toNumber() == rval.toNumber();
}
if (lval.type() != rval.type()) {
return false;
}
switch (lval.type()) {
case ValueType::Double:
return IsNaN(lval) ? IsNaN(rval) : lval.toDouble() == rval.toDouble();
case ValueType::BigInt:
// BigInt values are considered equal if they represent the same
// mathematical value.
return BigInt::equal(lval.toBigInt(), rval.toBigInt());
case ValueType::String:
MOZ_ASSERT(lval.toString()->isLinear() && rval.toString()->isLinear());
return EqualStrings(&lval.toString()->asLinear(),
&rval.toString()->asLinear());
case ValueType::ExtendedPrimitive: {
JSObject& lobj = lval.toExtendedPrimitive();
JSObject& robj = rval.toExtendedPrimitive();
if (lobj.getClass() != robj.getClass()) {
return false;
}
if (lobj.is<RecordType>()) {
return RecordType::sameValueZero(&lobj.as<RecordType>(),
&robj.as<RecordType>());
}
MOZ_ASSERT(lobj.is<TupleType>());
return TupleType::sameValueZero(&lobj.as<TupleType>(),
&robj.as<TupleType>());
}
default:
MOZ_ASSERT(CanUseBitwiseCompareForStrictlyEqual(lval));
return lval.asRawBits() == rval.asRawBits();
}
}
#endif
JS_PUBLIC_API bool JS::SameValue(JSContext* cx, Handle<Value> value1,
Handle<Value> value2, bool* same) {
js::AssertHeapIsIdle();
CHECK_THREAD(cx);
cx->check(value1, value2);
MOZ_ASSERT(same);
return js::SameValue(cx, value1, value2, same);
}
bool js::SameValueZero(JSContext* cx, Handle<Value> v1, Handle<Value> v2,
bool* same) {
if (IsNaN(v1) && IsNaN(v2)) {
*same = true;
return true;
}
return js::StrictlyEqual(cx, v1, v2, same);
}
|