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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
|
/*
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <AK/FlyString.h>
#include <AK/String.h>
#include <AK/StringBuilder.h>
#include <LibJS/Heap/Heap.h>
#include <LibJS/Interpreter.h>
#include <LibJS/Runtime/Array.h>
#include <LibJS/Runtime/BooleanObject.h>
#include <LibJS/Runtime/Error.h>
#include <LibJS/Runtime/Function.h>
#include <LibJS/Runtime/NumberObject.h>
#include <LibJS/Runtime/Object.h>
#include <LibJS/Runtime/PrimitiveString.h>
#include <LibJS/Runtime/StringObject.h>
#include <LibJS/Runtime/Symbol.h>
#include <LibJS/Runtime/SymbolObject.h>
#include <LibJS/Runtime/Value.h>
#include <math.h>
// 2 ** 53 - 1
#define MAX_ARRAY_LIKE_INDEX 9007199254740991.0
namespace JS {
bool Value::is_array() const
{
return is_object() && as_object().is_array();
}
bool Value::is_function() const
{
return is_object() && as_object().is_function();
}
Function& Value::as_function()
{
ASSERT(is_function());
return static_cast<Function&>(as_object());
}
String Value::to_string_without_side_effects() const
{
if (is_boolean())
return as_bool() ? "true" : "false";
if (is_null())
return "null";
if (is_undefined())
return "undefined";
if (is_number()) {
if (is_nan())
return "NaN";
if (is_infinity())
return as_double() < 0 ? "-Infinity" : "Infinity";
if (is_integer())
return String::number(to_i32());
return String::format("%.4f", as_double());
}
if (is_string())
return m_value.as_string->string();
if (is_symbol())
return as_symbol().to_string();
ASSERT(is_object());
return String::format("[object %s]", as_object().class_name());
}
PrimitiveString* Value::to_primitive_string(Interpreter& interpreter)
{
if (is_string())
return &as_string();
auto string = to_string(interpreter);
if (interpreter.exception())
return nullptr;
return js_string(interpreter, string);
}
String Value::to_string(Interpreter& interpreter) const
{
if (is_boolean())
return as_bool() ? "true" : "false";
if (is_null())
return "null";
if (is_undefined())
return "undefined";
if (is_number()) {
if (is_nan())
return "NaN";
if (is_infinity())
return as_double() < 0 ? "-Infinity" : "Infinity";
if (is_integer())
return String::number(to_i32());
return String::format("%.4f", as_double());
}
if (is_symbol()) {
interpreter.throw_exception<TypeError>("Can't convert symbol to string");
return {};
}
if (is_object()) {
auto primitive_value = as_object().to_primitive(Object::PreferredType::String);
// FIXME: Maybe we should pass in the Interpreter& and call interpreter.exception() instead?
if (primitive_value.is_empty())
return {};
return primitive_value.to_string(interpreter);
}
ASSERT(is_string());
return m_value.as_string->string();
}
bool Value::to_boolean() const
{
switch (m_type) {
case Type::Boolean:
return m_value.as_bool;
case Type::Number:
if (is_nan()) {
return false;
}
return !(m_value.as_double == 0 || m_value.as_double == -0);
case Type::Null:
case Type::Undefined:
return false;
case Type::String:
return !as_string().string().is_empty();
case Type::Object:
case Type::Symbol:
return true;
default:
ASSERT_NOT_REACHED();
}
}
Value Value::to_primitive(Interpreter&) const
{
if (is_object())
return as_object().to_primitive();
return *this;
}
Object* Value::to_object(Interpreter& interpreter) const
{
if (is_object())
return &const_cast<Object&>(as_object());
if (is_string())
return StringObject::create(interpreter.global_object(), *m_value.as_string);
if (is_symbol())
return SymbolObject::create(interpreter.global_object(), *m_value.as_symbol);
if (is_number())
return NumberObject::create(interpreter.global_object(), m_value.as_double);
if (is_boolean())
return BooleanObject::create(interpreter.global_object(), m_value.as_bool);
if (is_null() || is_undefined()) {
interpreter.throw_exception<TypeError>("ToObject on null or undefined.");
return nullptr;
}
dbg() << "Dying because I can't to_object() on " << *this;
ASSERT_NOT_REACHED();
}
Value Value::to_number(Interpreter& interpreter) const
{
switch (m_type) {
case Type::Empty:
ASSERT_NOT_REACHED();
return {};
case Type::Undefined:
return js_nan();
case Type::Null:
return Value(0);
case Type::Boolean:
return Value(m_value.as_bool ? 1 : 0);
case Type::Number:
return Value(m_value.as_double);
case Type::String: {
auto string = as_string().string().trim_whitespace();
if (string.is_empty())
return Value(0);
if (string == "Infinity" || string == "+Infinity")
return js_infinity();
if (string == "-Infinity")
return js_negative_infinity();
char* endptr;
auto parsed_double = strtod(string.characters(), &endptr);
if (*endptr)
return js_nan();
return Value(parsed_double);
}
case Type::Symbol:
// FIXME: Get access to the interpreter and throw a TypeError
ASSERT_NOT_REACHED();
case Type::Object:
auto primitive = m_value.as_object->to_primitive(Object::PreferredType::Number);
if (interpreter.exception())
return {};
return primitive.to_number(interpreter);
}
ASSERT_NOT_REACHED();
}
double Value::to_double(Interpreter& interpreter) const
{
auto number = to_number(interpreter);
if (interpreter.exception())
return 0;
return number.as_double();
}
i32 Value::to_i32() const
{
return static_cast<i32>(as_double());
}
i32 Value::to_i32(Interpreter& interpreter) const
{
auto number = to_number(interpreter);
if (interpreter.exception())
return 0;
return number.to_i32();
}
size_t Value::to_size_t() const
{
ASSERT(type() == Type::Number);
if (is_nan() || as_double() <= 0)
return 0;
return min((double)(i32)as_double(), MAX_ARRAY_LIKE_INDEX);
}
size_t Value::to_size_t(Interpreter& interpreter) const
{
if (is_empty())
return 0;
auto number = to_number(interpreter);
if (interpreter.exception())
return 0;
return number.to_size_t();
}
Value greater_than(Interpreter& interpreter, Value lhs, Value rhs)
{
auto lhs_number = lhs.to_number(interpreter);
if (interpreter.exception())
return {};
auto rhs_number = rhs.to_number(interpreter);
if (interpreter.exception())
return {};
return Value(lhs_number.as_double() > rhs_number.as_double());
}
Value greater_than_equals(Interpreter& interpreter, Value lhs, Value rhs)
{
auto lhs_number = lhs.to_number(interpreter);
if (interpreter.exception())
return {};
auto rhs_number = rhs.to_number(interpreter);
if (interpreter.exception())
return {};
return Value(lhs_number.as_double() >= rhs_number.as_double());
}
Value less_than(Interpreter& interpreter, Value lhs, Value rhs)
{
auto lhs_number = lhs.to_number(interpreter);
if (interpreter.exception())
return {};
auto rhs_number = rhs.to_number(interpreter);
if (interpreter.exception())
return {};
return Value(lhs_number.as_double() < rhs_number.as_double());
}
Value less_than_equals(Interpreter& interpreter, Value lhs, Value rhs)
{
auto lhs_number = lhs.to_number(interpreter);
if (interpreter.exception())
return {};
auto rhs_number = rhs.to_number(interpreter);
if (interpreter.exception())
return {};
return Value(lhs_number.as_double() <= rhs_number.as_double());
}
Value bitwise_and(Interpreter& interpreter, Value lhs, Value rhs)
{
auto lhs_number = lhs.to_number(interpreter);
if (interpreter.exception())
return {};
auto rhs_number = rhs.to_number(interpreter);
if (interpreter.exception())
return {};
return Value((i32)lhs_number.as_double() & (i32)rhs_number.as_double());
}
Value bitwise_or(Interpreter& interpreter, Value lhs, Value rhs)
{
bool lhs_invalid = lhs.is_undefined() || lhs.is_null() || lhs.is_nan() || lhs.is_infinity();
bool rhs_invalid = rhs.is_undefined() || rhs.is_null() || rhs.is_nan() || rhs.is_infinity();
if (lhs_invalid && rhs_invalid)
return Value(0);
if (lhs_invalid || rhs_invalid)
return lhs_invalid ? rhs.to_number(interpreter) : lhs.to_number(interpreter);
if (!rhs.is_number() && !lhs.is_number())
return Value(0);
auto lhs_number = lhs.to_number(interpreter);
if (interpreter.exception())
return {};
auto rhs_number = rhs.to_number(interpreter);
if (interpreter.exception())
return {};
return Value((i32)lhs_number.as_double() | (i32)rhs_number.as_double());
}
Value bitwise_xor(Interpreter& interpreter, Value lhs, Value rhs)
{
auto lhs_number = lhs.to_number(interpreter);
if (interpreter.exception())
return {};
auto rhs_number = rhs.to_number(interpreter);
if (interpreter.exception())
return {};
return Value((i32)lhs_number.as_double() ^ (i32)rhs_number.as_double());
}
Value bitwise_not(Interpreter& interpreter, Value lhs)
{
auto lhs_number = lhs.to_number(interpreter);
if (interpreter.exception())
return {};
return Value(~(i32)lhs_number.as_double());
}
Value unary_plus(Interpreter& interpreter, Value lhs)
{
return lhs.to_number(interpreter);
}
Value unary_minus(Interpreter& interpreter, Value lhs)
{
auto lhs_number = lhs.to_number(interpreter);
if (interpreter.exception())
return {};
if (lhs_number.is_nan())
return js_nan();
return Value(-lhs_number.as_double());
}
Value left_shift(Interpreter& interpreter, Value lhs, Value rhs)
{
auto lhs_number = lhs.to_number(interpreter);
if (interpreter.exception())
return {};
if (!lhs_number.is_finite_number())
return Value(0);
auto rhs_number = rhs.to_number(interpreter);
if (interpreter.exception())
return {};
if (!rhs_number.is_finite_number())
return lhs_number;
return Value((i32)lhs_number.as_double() << (i32)rhs_number.as_double());
}
Value right_shift(Interpreter& interpreter, Value lhs, Value rhs)
{
auto lhs_number = lhs.to_number(interpreter);
if (interpreter.exception())
return {};
if (!lhs_number.is_finite_number())
return Value(0);
auto rhs_number = rhs.to_number(interpreter);
if (interpreter.exception())
return {};
if (!rhs_number.is_finite_number())
return lhs_number;
return Value((i32)lhs_number.as_double() >> (i32)rhs_number.as_double());
}
Value unsigned_right_shift(Interpreter& interpreter, Value lhs, Value rhs)
{
auto lhs_number = lhs.to_number(interpreter);
if (interpreter.exception())
return {};
if (!lhs_number.is_finite_number())
return Value(0);
auto rhs_number = rhs.to_number(interpreter);
if (interpreter.exception())
return {};
if (!rhs_number.is_finite_number())
return lhs_number;
return Value((unsigned)lhs_number.as_double() >> (i32)rhs_number.as_double());
}
Value add(Interpreter& interpreter, Value lhs, Value rhs)
{
auto lhs_primitive = lhs.to_primitive(interpreter);
if (interpreter.exception())
return {};
auto rhs_primitive = rhs.to_primitive(interpreter);
if (interpreter.exception())
return {};
if (lhs_primitive.is_string() || rhs_primitive.is_string()) {
auto lhs_string = lhs_primitive.to_string(interpreter);
if (interpreter.exception())
return {};
auto rhs_string = rhs_primitive.to_string(interpreter);
if (interpreter.exception())
return {};
StringBuilder builder(lhs_string.length() + rhs_string.length());
builder.append(lhs_string);
builder.append(rhs_string);
return js_string(interpreter, builder.to_string());
}
auto lhs_number = lhs_primitive.to_number(interpreter);
if (interpreter.exception())
return {};
auto rhs_number = rhs_primitive.to_number(interpreter);
if (interpreter.exception())
return {};
return Value(lhs_number.as_double() + rhs_number.as_double());
}
Value sub(Interpreter& interpreter, Value lhs, Value rhs)
{
auto lhs_number = lhs.to_number(interpreter);
if (interpreter.exception())
return {};
auto rhs_number = rhs.to_number(interpreter);
if (interpreter.exception())
return {};
return Value(lhs_number.as_double() - rhs_number.as_double());
}
Value mul(Interpreter& interpreter, Value lhs, Value rhs)
{
auto lhs_number = lhs.to_number(interpreter);
if (interpreter.exception())
return {};
auto rhs_number = rhs.to_number(interpreter);
if (interpreter.exception())
return {};
return Value(lhs_number.as_double() * rhs_number.as_double());
}
Value div(Interpreter& interpreter, Value lhs, Value rhs)
{
auto lhs_number = lhs.to_number(interpreter);
if (interpreter.exception())
return {};
auto rhs_number = rhs.to_number(interpreter);
if (interpreter.exception())
return {};
return Value(lhs_number.as_double() / rhs_number.as_double());
}
Value mod(Interpreter& interpreter, Value lhs, Value rhs)
{
auto lhs_number = lhs.to_number(interpreter);
if (interpreter.exception())
return {};
auto rhs_number = rhs.to_number(interpreter);
if (interpreter.exception())
return {};
if (lhs_number.is_nan() || rhs_number.is_nan())
return js_nan();
auto index = lhs_number.as_double();
auto period = rhs_number.as_double();
auto trunc = (double)(i32)(index / period);
return Value(index - trunc * period);
}
Value exp(Interpreter& interpreter, Value lhs, Value rhs)
{
auto lhs_number = lhs.to_number(interpreter);
if (interpreter.exception())
return {};
auto rhs_number = rhs.to_number(interpreter);
if (interpreter.exception())
return {};
return Value(pow(lhs_number.as_double(), rhs_number.as_double()));
}
Value in(Interpreter& interpreter, Value lhs, Value rhs)
{
if (!rhs.is_object())
return interpreter.throw_exception<TypeError>("'in' operator must be used on object");
auto lhs_string = lhs.to_string(interpreter);
if (interpreter.exception())
return {};
return Value(!rhs.as_object().get(lhs_string).is_empty());
}
Value instance_of(Interpreter&, Value lhs, Value rhs)
{
if (!lhs.is_object() || !rhs.is_object())
return Value(false);
auto constructor_prototype_property = rhs.as_object().get("prototype");
if (!constructor_prototype_property.is_object())
return Value(false);
return Value(lhs.as_object().has_prototype(&constructor_prototype_property.as_object()));
}
const LogStream& operator<<(const LogStream& stream, const Value& value)
{
return stream << (value.is_empty() ? "<empty>" : value.to_string_without_side_effects());
}
bool same_value(Interpreter& interpreter, Value lhs, Value rhs)
{
if (lhs.type() != rhs.type())
return false;
if (lhs.is_number()) {
if (lhs.is_nan() && rhs.is_nan())
return true;
if (lhs.is_positive_zero() && rhs.is_negative_zero())
return false;
if (lhs.is_negative_zero() && rhs.is_positive_zero())
return false;
return lhs.as_double() == rhs.as_double();
}
return same_value_non_numeric(interpreter, lhs, rhs);
}
bool same_value_zero(Interpreter& interpreter, Value lhs, Value rhs)
{
if (lhs.type() != rhs.type())
return false;
if (lhs.is_number()) {
if (lhs.is_nan() && rhs.is_nan())
return true;
if ((lhs.is_positive_zero() || lhs.is_negative_zero()) && (rhs.is_positive_zero() || rhs.is_negative_zero()))
return true;
return lhs.as_double() == rhs.as_double();
}
return same_value_non_numeric(interpreter, lhs, rhs);
}
bool same_value_non_numeric(Interpreter&, Value lhs, Value rhs)
{
ASSERT(!lhs.is_number());
ASSERT(lhs.type() == rhs.type());
switch (lhs.type()) {
case Value::Type::Empty:
ASSERT_NOT_REACHED();
case Value::Type::Undefined:
case Value::Type::Null:
return true;
case Value::Type::String:
return lhs.as_string().string() == rhs.as_string().string();
case Value::Type::Symbol:
return &lhs.as_symbol() == &rhs.as_symbol();
case Value::Type::Boolean:
return lhs.as_bool() == rhs.as_bool();
case Value::Type::Object:
return &lhs.as_object() == &rhs.as_object();
default:
ASSERT_NOT_REACHED();
}
}
bool strict_eq(Interpreter& interpreter, Value lhs, Value rhs)
{
if (lhs.type() != rhs.type())
return false;
if (lhs.is_number()) {
if (lhs.is_nan() || rhs.is_nan())
return false;
if (lhs.as_double() == rhs.as_double())
return true;
if ((lhs.is_positive_zero() || lhs.is_negative_zero()) && (rhs.is_positive_zero() || rhs.is_negative_zero()))
return true;
return false;
}
return same_value_non_numeric(interpreter, lhs, rhs);
}
bool abstract_eq(Interpreter& interpreter, Value lhs, Value rhs)
{
if (lhs.type() == rhs.type())
return strict_eq(interpreter, lhs, rhs);
if ((lhs.is_undefined() || lhs.is_null()) && (rhs.is_undefined() || rhs.is_null()))
return true;
if (lhs.is_number() && rhs.is_string())
return abstract_eq(interpreter, lhs, rhs.to_number(interpreter));
if (lhs.is_string() && rhs.is_number())
return abstract_eq(interpreter, lhs.to_number(interpreter), rhs);
if (lhs.is_boolean())
return abstract_eq(interpreter, lhs.to_number(interpreter), rhs);
if (rhs.is_boolean())
return abstract_eq(interpreter, lhs, rhs.to_number(interpreter));
if ((lhs.is_string() || lhs.is_number() || lhs.is_symbol()) && rhs.is_object())
return abstract_eq(interpreter, lhs, rhs.to_primitive(interpreter));
if (lhs.is_object() && (rhs.is_string() || rhs.is_number() || rhs.is_symbol()))
return abstract_eq(interpreter, lhs.to_primitive(interpreter), rhs);
return false;
}
}
|