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 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919
|
/*
* Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
* Copyright (C) 2002-2020 Apple Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*
*/
#pragma once
#include "CallFrame.h"
#include "ExceptionHelpers.h"
#include "JSBigInt.h"
#include "JSGlobalObject.h"
#include <wtf/text/MakeString.h>
WTF_ALLOW_UNSAFE_BUFFER_USAGE_BEGIN
namespace JSC {
#define InvalidPrototypeChain (std::numeric_limits<size_t>::max())
NEVER_INLINE JSValue jsAddSlowCase(JSGlobalObject*, JSValue, JSValue);
JSString* jsTypeStringForValueWithConcurrency(VM&, JSGlobalObject*, JSValue, Concurrency);
size_t normalizePrototypeChain(JSGlobalObject*, JSCell*, bool& sawPolyProto);
template<Concurrency concurrency>
ALWAYS_INLINE TriState jsTypeofIsObjectWithConcurrency(JSGlobalObject* globalObject, JSValue value)
{
if (!value.isObject())
return triState(value.isNull());
JSObject* object = asObject(value);
if (object->structure()->masqueradesAsUndefined(globalObject))
return TriState::False;
return invert(object->isCallableWithConcurrency<concurrency>());
}
template<Concurrency concurrency>
ALWAYS_INLINE TriState jsTypeofIsFunctionWithConcurrency(JSGlobalObject* globalObject, JSValue value)
{
if (!value.isObject())
return TriState::False;
JSObject* object = asObject(value);
if (object->structure()->masqueradesAsUndefined(globalObject))
return TriState::False;
return object->isCallableWithConcurrency<concurrency>();
}
inline JSString* jsTypeStringForValue(JSGlobalObject* globalObject, JSValue value)
{
return jsTypeStringForValueWithConcurrency(getVM(globalObject), globalObject, value, Concurrency::MainThread);
}
ALWAYS_INLINE bool jsTypeofIsObject(JSGlobalObject* globalObject, JSValue value)
{
auto result = jsTypeofIsObjectWithConcurrency<Concurrency::MainThread>(globalObject, value);
ASSERT(result != TriState::Indeterminate);
return result == TriState::True;
}
ALWAYS_INLINE bool jsTypeofIsFunction(JSGlobalObject* globalObject, JSValue value)
{
auto result = jsTypeofIsFunctionWithConcurrency<Concurrency::MainThread>(globalObject, value);
ASSERT(result != TriState::Indeterminate);
return result == TriState::True;
}
ALWAYS_INLINE JSString* jsString(JSGlobalObject* globalObject, const String& u1, JSString* s2)
{
VM& vm = getVM(globalObject);
auto scope = DECLARE_THROW_SCOPE(vm);
unsigned length1 = u1.length();
if (!length1)
return s2;
unsigned length2 = s2->length();
if (!length2)
return jsString(vm, u1);
static_assert(JSString::MaxLength == std::numeric_limits<int32_t>::max());
if (sumOverflows<int32_t>(length1, length2)) {
throwOutOfMemoryError(globalObject, scope);
return nullptr;
}
// (1) Cost of making JSString : sizeof(JSString) (for new string) + sizeof(StringImpl header) + length1 + length2
// (2) Cost of making JSRopeString: sizeof(JSString) (for u1) + sizeof(JSRopeString)
// We do not account u1 cost in (2) since u1 may be shared StringImpl, and it may not introduce additional cost.
// We conservatively consider the cost of u1. Currently, we are not considering about is8Bit() case because 16-bit
// strings are relatively rare. But we can do that if we need to consider it.
if (s2->isRope() || (StringImpl::headerSize<LChar>() + length1 + length2) >= sizeof(JSRopeString))
return JSRopeString::create(vm, jsString(vm, u1), s2);
ASSERT(!s2->isRope());
auto u2 = s2->value(globalObject);
scope.assertNoException();
String newString = tryMakeString(u1, u2.data);
if (!newString) {
throwOutOfMemoryError(globalObject, scope);
return nullptr;
}
return JSString::create(vm, newString.releaseImpl().releaseNonNull());
}
ALWAYS_INLINE JSString* jsString(JSGlobalObject* globalObject, JSString* s1, const String& u2)
{
VM& vm = getVM(globalObject);
auto scope = DECLARE_THROW_SCOPE(vm);
unsigned length1 = s1->length();
if (!length1)
return jsString(vm, u2);
unsigned length2 = u2.length();
if (!length2)
return s1;
static_assert(JSString::MaxLength == std::numeric_limits<int32_t>::max());
if (sumOverflows<int32_t>(length1, length2)) {
throwOutOfMemoryError(globalObject, scope);
return nullptr;
}
// (1) Cost of making JSString : sizeof(JSString) (for new string) + sizeof(StringImpl header) + length1 + length2
// (2) Cost of making JSRopeString: sizeof(JSString) (for u2) + sizeof(JSRopeString)
if (s1->isRope() || (StringImpl::headerSize<LChar>() + length1 + length2) >= sizeof(JSRopeString))
return JSRopeString::create(vm, s1, jsString(vm, u2));
ASSERT(!s1->isRope());
auto u1 = s1->value(globalObject);
scope.assertNoException();
String newString = tryMakeString(u1.data, u2);
if (!newString) {
throwOutOfMemoryError(globalObject, scope);
return nullptr;
}
return JSString::create(vm, newString.releaseImpl().releaseNonNull());
}
ALWAYS_INLINE JSString* jsString(JSGlobalObject* globalObject, JSString* s1, JSString* s2)
{
VM& vm = getVM(globalObject);
auto scope = DECLARE_THROW_SCOPE(vm);
unsigned length1 = s1 ? s1->length() : 0;
if (!length1)
return s2;
unsigned length2 = s2 ? s2->length() : 0;
if (!length2)
return s1;
static_assert(JSString::MaxLength == std::numeric_limits<int32_t>::max());
if (sumOverflows<int32_t>(length1, length2)) {
throwOutOfMemoryError(globalObject, scope);
return nullptr;
}
return JSRopeString::create(vm, s1, s2);
}
ALWAYS_INLINE JSString* jsString(JSGlobalObject* globalObject, JSString* s1, JSString* s2, JSString* s3)
{
VM& vm = getVM(globalObject);
auto scope = DECLARE_THROW_SCOPE(vm);
unsigned length1 = s1 ? s1->length() : 0;
if (!length1)
RELEASE_AND_RETURN(scope, jsString(globalObject, s2, s3));
unsigned length2 = s2 ? s2->length() : 0;
if (!length2)
RELEASE_AND_RETURN(scope, jsString(globalObject, s1, s3));
unsigned length3 = s3 ? s3->length() : 0;
if (!length3)
RELEASE_AND_RETURN(scope, jsString(globalObject, s1, s2));
static_assert(JSString::MaxLength == std::numeric_limits<int32_t>::max());
if (sumOverflows<int32_t>(length1, length2, length3)) {
throwOutOfMemoryError(globalObject, scope);
return nullptr;
}
return JSRopeString::create(vm, s1, s2, s3);
}
ALWAYS_INLINE JSString* jsString(JSGlobalObject* globalObject, const String& u1, const String& u2)
{
VM& vm = getVM(globalObject);
auto scope = DECLARE_THROW_SCOPE(vm);
unsigned length1 = u1.length();
if (!length1)
return jsString(vm, u2);
unsigned length2 = u2.length();
if (!length2)
return jsString(vm, u1);
static_assert(JSString::MaxLength == std::numeric_limits<int32_t>::max());
if (sumOverflows<int32_t>(length1, length2)) {
throwOutOfMemoryError(globalObject, scope);
return nullptr;
}
// (1) Cost of making JSString : sizeof(JSString) (for new string) + sizeof(StringImpl header) + length1 + length2
// (2) Cost of making JSRopeString: sizeof(JSString) (for u1) + sizeof(JSString) (for u2) + sizeof(JSRopeString)
if ((StringImpl::headerSize<LChar>() + length1 + length2) >= (sizeof(JSRopeString) + sizeof(JSString)))
return JSRopeString::create(vm, jsString(vm, u1), jsString(vm, u2));
String newString = tryMakeString(u1, u2);
if (!newString) {
throwOutOfMemoryError(globalObject, scope);
return nullptr;
}
return JSString::create(vm, newString.releaseImpl().releaseNonNull());
}
ALWAYS_INLINE JSString* jsString(JSGlobalObject* globalObject, const String& u1, const String& u2, const String& u3)
{
VM& vm = getVM(globalObject);
auto scope = DECLARE_THROW_SCOPE(vm);
unsigned length1 = u1.length();
unsigned length2 = u2.length();
unsigned length3 = u3.length();
ASSERT(length1 <= JSString::MaxLength);
ASSERT(length2 <= JSString::MaxLength);
ASSERT(length3 <= JSString::MaxLength);
if (!length1)
RELEASE_AND_RETURN(scope, jsString(globalObject, u2, u3));
if (!length2)
RELEASE_AND_RETURN(scope, jsString(globalObject, u1, u3));
if (!length3)
RELEASE_AND_RETURN(scope, jsString(globalObject, u1, u2));
static_assert(JSString::MaxLength == std::numeric_limits<int32_t>::max());
if (sumOverflows<int32_t>(length1, length2, length3)) {
throwOutOfMemoryError(globalObject, scope);
return nullptr;
}
// (1) Cost of making JSString : sizeof(JSString) (for new string) + sizeof(StringImpl header) + length1 + length2 + length3
// (2) Cost of making JSRopeString: sizeof(JSString) (for u1) + sizeof(JSString) (for u2) + sizeof(JSString) (for u3) + sizeof(JSRopeString)
if ((StringImpl::headerSize<LChar>() + length1 + length2 + length3) >= (sizeof(JSRopeString) + sizeof(JSString) * 2))
return JSRopeString::create(vm, jsString(vm, u1), jsString(vm, u2), jsString(vm, u3));
String newString = tryMakeString(u1, u2, u3);
if (!newString) {
throwOutOfMemoryError(globalObject, scope);
return nullptr;
}
return JSString::create(vm, newString.releaseImpl().releaseNonNull());
}
ALWAYS_INLINE JSValue jsStringFromRegisterArray(JSGlobalObject* globalObject, Register* strings, unsigned count)
{
VM& vm = getVM(globalObject);
auto scope = DECLARE_THROW_SCOPE(vm);
JSRopeString::RopeBuilder<RecordOverflow> ropeBuilder(vm);
for (unsigned i = 0; i < count; ++i) {
JSValue v = strings[-static_cast<int>(i)].jsValue();
JSString* string = v.toString(globalObject);
RETURN_IF_EXCEPTION(scope, { });
if (!ropeBuilder.append(string))
return throwOutOfMemoryError(globalObject, scope);
}
return ropeBuilder.release();
}
ALWAYS_INLINE JSBigInt::ComparisonResult compareBigInt(JSValue left, JSValue right)
{
ASSERT(left.isBigInt());
ASSERT(right.isBigInt());
#if USE(BIGINT32)
if (left.isBigInt32()) {
if (right.isBigInt32())
return JSBigInt::compare(left.bigInt32AsInt32(), right.bigInt32AsInt32());
ASSERT(right.isHeapBigInt());
return JSBigInt::compare(left.bigInt32AsInt32(), right.asHeapBigInt());
}
if (right.isBigInt32()) {
ASSERT(left.isHeapBigInt());
return JSBigInt::compare(left.asHeapBigInt(), right.bigInt32AsInt32());
}
#endif
return JSBigInt::compare(left.asHeapBigInt(), right.asHeapBigInt());
}
ALWAYS_INLINE JSBigInt::ComparisonResult compareBigIntToOtherPrimitive(JSGlobalObject* globalObject, JSBigInt* v1, JSValue primValue)
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
ASSERT(primValue.isPrimitive());
ASSERT(!primValue.isBigInt());
if (primValue.isString()) {
auto string = asString(primValue)->value(globalObject);
RETURN_IF_EXCEPTION(scope, JSBigInt::ComparisonResult::Undefined);
JSValue bigIntValue = JSBigInt::stringToBigInt(globalObject, WTFMove(string));
RETURN_IF_EXCEPTION(scope, JSBigInt::ComparisonResult::Undefined);
if (!bigIntValue)
return JSBigInt::ComparisonResult::Undefined;
if (bigIntValue.isHeapBigInt())
return JSBigInt::compare(v1, bigIntValue.asHeapBigInt());
ASSERT(bigIntValue.isBigInt32());
#if USE(BIGINT32)
// FIXME: use something less hacky, e.g. some kind of JSBigInt::compareToInt32
return JSBigInt::compareToDouble(v1, static_cast<double>(bigIntValue.bigInt32AsInt32()));
#endif
}
double numberValue = primValue.toNumber(globalObject);
RETURN_IF_EXCEPTION(scope, JSBigInt::ComparisonResult::Undefined);
return JSBigInt::compareToDouble(v1, numberValue);
}
ALWAYS_INLINE JSBigInt::ComparisonResult compareBigInt32ToOtherPrimitive(JSGlobalObject* globalObject, int32_t v1, JSValue primValue)
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
ASSERT(primValue.isPrimitive());
ASSERT(!primValue.isBigInt());
auto compare = [&] (auto v1, auto v2) {
static_assert(std::is_same_v<decltype(v1), decltype(v2)>);
if (v1 == v2)
return JSBigInt::ComparisonResult::Equal;
if (v1 < v2)
return JSBigInt::ComparisonResult::LessThan;
return JSBigInt::ComparisonResult::GreaterThan;
};
if (primValue.isString()) {
auto string = asString(primValue)->value(globalObject);
RETURN_IF_EXCEPTION(scope, JSBigInt::ComparisonResult::Undefined);
JSValue bigIntValue = JSBigInt::stringToBigInt(globalObject, WTFMove(string));
RETURN_IF_EXCEPTION(scope, JSBigInt::ComparisonResult::Undefined);
if (!bigIntValue)
return JSBigInt::ComparisonResult::Undefined;
if (bigIntValue.isHeapBigInt())
return JSBigInt::compare(v1, bigIntValue.asHeapBigInt());
ASSERT(bigIntValue.isBigInt32());
#if USE(BIGINT32)
return compare(v1, bigIntValue.bigInt32AsInt32());
#endif
}
// Note that 0n <=> -0.0 is handling -0.0 as +0.
double numberValue = primValue.toNumber(globalObject);
RETURN_IF_EXCEPTION(scope, JSBigInt::ComparisonResult::Undefined);
if (std::isnan(numberValue))
return JSBigInt::ComparisonResult::Undefined;
return compare(static_cast<double>(v1), numberValue);
}
ALWAYS_INLINE bool bigIntCompareResult(JSBigInt::ComparisonResult comparisonResult, JSBigInt::ComparisonMode comparisonMode)
{
if (comparisonMode == JSBigInt::ComparisonMode::LessThan)
return comparisonResult == JSBigInt::ComparisonResult::LessThan;
ASSERT(comparisonMode == JSBigInt::ComparisonMode::LessThanOrEqual);
return comparisonResult == JSBigInt::ComparisonResult::LessThan || comparisonResult == JSBigInt::ComparisonResult::Equal;
}
ALWAYS_INLINE bool bigIntCompare(JSGlobalObject* globalObject, JSValue v1, JSValue v2, JSBigInt::ComparisonMode comparisonMode)
{
ASSERT(v1.isBigInt() || v2.isBigInt());
ASSERT(v1.isPrimitive() && v2.isPrimitive());
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
if (v1.isBigInt() && v2.isBigInt())
return bigIntCompareResult(compareBigInt(v1, v2), comparisonMode);
#if USE(BIGINT32)
if (v1.isBigInt32()) {
ASSERT(!v2.isBigInt());
auto comparisonResult = compareBigInt32ToOtherPrimitive(globalObject, v1.bigInt32AsInt32(), v2);
RETURN_IF_EXCEPTION(scope, false);
return bigIntCompareResult(comparisonResult, comparisonMode);
}
if (v2.isBigInt32()) {
ASSERT(!v1.isBigInt());
auto comparisonResult = compareBigInt32ToOtherPrimitive(globalObject, v2.bigInt32AsInt32(), v1);
RETURN_IF_EXCEPTION(scope, false);
return bigIntCompareResult(invertBigIntCompareResult(comparisonResult), comparisonMode);
}
ASSERT(!v1.isBigInt32() && !v2.isBigInt32());
#endif
if (v1.isHeapBigInt()) {
ASSERT(!v2.isBigInt());
auto comparisonResult = compareBigIntToOtherPrimitive(globalObject, v1.asHeapBigInt(), v2);
RETURN_IF_EXCEPTION(scope, false);
return bigIntCompareResult(comparisonResult, comparisonMode);
}
// Here we check inverted because BigInt is the v2
ASSERT(!v1.isBigInt());
ASSERT(v2.isHeapBigInt());
auto comparisonResult = compareBigIntToOtherPrimitive(globalObject, v2.asHeapBigInt(), v1);
RETURN_IF_EXCEPTION(scope, false);
return bigIntCompareResult(invertBigIntCompareResult(comparisonResult), comparisonMode);
}
ALWAYS_INLINE bool toPrimitiveNumeric(JSGlobalObject* globalObject, JSValue v, JSValue& p, double& n)
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
p = v.toPrimitive(globalObject, PreferNumber);
RETURN_IF_EXCEPTION(scope, false);
if (p.isBigInt())
return true;
n = p.toNumber(globalObject);
RETURN_IF_EXCEPTION(scope, false);
return !p.isString();
}
// See ES5 11.8.1/11.8.2/11.8.5 for definition of leftFirst, this value ensures correct
// evaluation ordering for argument conversions for '<' and '>'. For '<' pass the value
// true, for leftFirst, for '>' pass the value false (and reverse operand order).
template<bool leftFirst>
ALWAYS_INLINE bool jsLess(JSGlobalObject* globalObject, JSValue v1, JSValue v2)
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
if (v1.isInt32() && v2.isInt32())
return v1.asInt32() < v2.asInt32();
if (v1.isNumber() && v2.isNumber())
return v1.asNumber() < v2.asNumber();
if (isJSString(v1) && isJSString(v2)) {
auto s1 = asString(v1)->value(globalObject);
RETURN_IF_EXCEPTION(scope, false);
auto s2 = asString(v2)->value(globalObject);
RETURN_IF_EXCEPTION(scope, false);
return codePointCompareLessThan(s1, s2);
}
double n1;
double n2;
JSValue p1;
JSValue p2;
bool wasNotString1;
bool wasNotString2;
if (leftFirst) {
wasNotString1 = toPrimitiveNumeric(globalObject, v1, p1, n1);
RETURN_IF_EXCEPTION(scope, false);
wasNotString2 = toPrimitiveNumeric(globalObject, v2, p2, n2);
} else {
wasNotString2 = toPrimitiveNumeric(globalObject, v2, p2, n2);
RETURN_IF_EXCEPTION(scope, false);
wasNotString1 = toPrimitiveNumeric(globalObject, v1, p1, n1);
}
RETURN_IF_EXCEPTION(scope, false);
if (wasNotString1 || wasNotString2) {
if (p1.isBigInt() || p2.isBigInt())
RELEASE_AND_RETURN(scope, bigIntCompare(globalObject, p1, p2, JSBigInt::ComparisonMode::LessThan));
return n1 < n2;
}
return codePointCompareLessThan(asString(p1)->value(globalObject), asString(p2)->value(globalObject));
}
// See ES5 11.8.3/11.8.4/11.8.5 for definition of leftFirst, this value ensures correct
// evaluation ordering for argument conversions for '<=' and '=>'. For '<=' pass the
// value true, for leftFirst, for '=>' pass the value false (and reverse operand order).
template<bool leftFirst>
ALWAYS_INLINE bool jsLessEq(JSGlobalObject* globalObject, JSValue v1, JSValue v2)
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
if (v1.isInt32() && v2.isInt32())
return v1.asInt32() <= v2.asInt32();
if (v1.isNumber() && v2.isNumber())
return v1.asNumber() <= v2.asNumber();
if (isJSString(v1) && isJSString(v2)) {
auto s1 = asString(v1)->value(globalObject);
RETURN_IF_EXCEPTION(scope, false);
auto s2 = asString(v2)->value(globalObject);
RETURN_IF_EXCEPTION(scope, false);
return !codePointCompareLessThan(s2, s1);
}
double n1;
double n2;
JSValue p1;
JSValue p2;
bool wasNotString1;
bool wasNotString2;
if (leftFirst) {
wasNotString1 = toPrimitiveNumeric(globalObject, v1, p1, n1);
RETURN_IF_EXCEPTION(scope, false);
wasNotString2 = toPrimitiveNumeric(globalObject, v2, p2, n2);
} else {
wasNotString2 = toPrimitiveNumeric(globalObject, v2, p2, n2);
RETURN_IF_EXCEPTION(scope, false);
wasNotString1 = toPrimitiveNumeric(globalObject, v1, p1, n1);
}
RETURN_IF_EXCEPTION(scope, false);
if (wasNotString1 || wasNotString2) {
if (p1.isBigInt() || p2.isBigInt())
RELEASE_AND_RETURN(scope, bigIntCompare(globalObject, p1, p2, JSBigInt::ComparisonMode::LessThanOrEqual));
return n1 <= n2;
}
return !codePointCompareLessThan(asString(p2)->value(globalObject), asString(p1)->value(globalObject));
}
// Fast-path choices here are based on frequency data from SunSpider:
// <times> Add case: <t1> <t2>
// ---------------------------
// 5626160 Add case: 3 3 (of these, 3637690 are for immediate values)
// 247412 Add case: 5 5
// 20900 Add case: 5 6
// 13962 Add case: 5 3
// 4000 Add case: 3 5
ALWAYS_INLINE JSValue jsAddNonNumber(JSGlobalObject* globalObject, JSValue v1, JSValue v2)
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
ASSERT(!v1.isNumber() || !v2.isNumber());
if (LIKELY(v1.isString() && !v2.isObject())) {
if (v2.isString())
RELEASE_AND_RETURN(scope, jsString(globalObject, asString(v1), asString(v2)));
String s2 = v2.toWTFString(globalObject);
RETURN_IF_EXCEPTION(scope, { });
RELEASE_AND_RETURN(scope, jsString(globalObject, asString(v1), s2));
}
// All other cases are pretty uncommon
RELEASE_AND_RETURN(scope, jsAddSlowCase(globalObject, v1, v2));
}
ALWAYS_INLINE JSValue jsAdd(JSGlobalObject* globalObject, JSValue v1, JSValue v2)
{
if (v1.isNumber() && v2.isNumber())
return jsNumber(v1.asNumber() + v2.asNumber());
return jsAddNonNumber(globalObject, v1, v2);
}
template<typename DoubleOperation, typename BigIntOp>
ALWAYS_INLINE JSValue arithmeticBinaryOp(JSGlobalObject* globalObject, JSValue v1, JSValue v2, DoubleOperation&& doubleOp, BigIntOp&& bigIntOp, ASCIILiteral errorMessage)
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
JSValue leftNumeric = v1.toNumeric(globalObject);
RETURN_IF_EXCEPTION(scope, { });
JSValue rightNumeric = v2.toNumeric(globalObject);
RETURN_IF_EXCEPTION(scope, { });
if (leftNumeric.isNumber() && rightNumeric.isNumber())
return jsNumber(doubleOp(leftNumeric.asNumber(), rightNumeric.asNumber()));
#if USE(BIGINT32)
if (leftNumeric.isBigInt32()) {
if (rightNumeric.isBigInt32())
RELEASE_AND_RETURN(scope, bigIntOp(globalObject, leftNumeric.bigInt32AsInt32(), rightNumeric.bigInt32AsInt32()));
if (rightNumeric.isHeapBigInt())
RELEASE_AND_RETURN(scope, bigIntOp(globalObject, leftNumeric.bigInt32AsInt32(), rightNumeric.asHeapBigInt()));
} else if (leftNumeric.isHeapBigInt()) {
if (rightNumeric.isBigInt32())
RELEASE_AND_RETURN(scope, bigIntOp(globalObject, leftNumeric.asHeapBigInt(), rightNumeric.bigInt32AsInt32()));
else if (rightNumeric.isHeapBigInt())
RELEASE_AND_RETURN(scope, bigIntOp(globalObject, leftNumeric.asHeapBigInt(), rightNumeric.asHeapBigInt()));
}
#else
if (leftNumeric.isHeapBigInt() && rightNumeric.isHeapBigInt())
RELEASE_AND_RETURN(scope, bigIntOp(globalObject, leftNumeric.asHeapBigInt(), rightNumeric.asHeapBigInt()));
#endif
return throwTypeError(globalObject, scope, errorMessage);
}
ALWAYS_INLINE JSValue jsSub(JSGlobalObject* globalObject, JSValue v1, JSValue v2)
{
auto doubleOp = [] (double left, double right) -> double {
return left - right;
};
auto bigIntOp = [] (JSGlobalObject* globalObject, auto left, auto right) {
return JSBigInt::sub(globalObject, left, right);
};
return arithmeticBinaryOp(globalObject, v1, v2, doubleOp, bigIntOp, "Invalid mix of BigInt and other type in subtraction."_s);
}
ALWAYS_INLINE JSValue jsMul(JSGlobalObject* globalObject, JSValue v1, JSValue v2)
{
auto doubleOp = [] (double left, double right) -> double {
return left * right;
};
auto bigIntOp = [] (JSGlobalObject* globalObject, auto left, auto right) {
return JSBigInt::multiply(globalObject, left, right);
};
return arithmeticBinaryOp(globalObject, v1, v2, doubleOp, bigIntOp, "Invalid mix of BigInt and other type in multiplication."_s);
}
ALWAYS_INLINE JSValue jsDiv(JSGlobalObject* globalObject, JSValue v1, JSValue v2)
{
auto doubleOp = [] (double left, double right) -> double {
return left / right;
};
auto bigIntOp = [] (JSGlobalObject* globalObject, auto left, auto right) {
return JSBigInt::divide(globalObject, left, right);
};
return arithmeticBinaryOp(globalObject, v1, v2, doubleOp, bigIntOp, "Invalid mix of BigInt and other type in division."_s);
}
ALWAYS_INLINE JSValue jsRemainder(JSGlobalObject* globalObject, JSValue v1, JSValue v2)
{
auto doubleOp = [] (double left, double right) -> double {
return Math::fmodDouble(left, right);
};
auto bigIntOp = [] (JSGlobalObject* globalObject, auto left, auto right) {
return JSBigInt::remainder(globalObject, left, right);
};
return arithmeticBinaryOp(globalObject, v1, v2, doubleOp, bigIntOp, "Invalid mix of BigInt and other type in remainder."_s);
}
ALWAYS_INLINE JSValue jsPow(JSGlobalObject* globalObject, JSValue v1, JSValue v2)
{
auto doubleOp = [] (double left, double right) -> double {
return operationMathPow(left, right);
};
auto bigIntOp = [] (JSGlobalObject* globalObject, auto left, auto right) {
return JSBigInt::exponentiate(globalObject, left, right);
};
return arithmeticBinaryOp(globalObject, v1, v2, doubleOp, bigIntOp, "Invalid mix of BigInt and other type in exponentiation."_s);
}
ALWAYS_INLINE JSValue jsInc(JSGlobalObject* globalObject, JSValue v)
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
auto operandNumeric = v.toNumeric(globalObject);
RETURN_IF_EXCEPTION(scope, { });
if (operandNumeric.isNumber())
return jsNumber(operandNumeric.asNumber() + 1);
#if USE(BIGINT32)
if (operandNumeric.isBigInt32())
RELEASE_AND_RETURN(scope, JSBigInt::inc(globalObject, operandNumeric.bigInt32AsInt32()));
#endif
ASSERT(operandNumeric.isHeapBigInt());
RELEASE_AND_RETURN(scope, JSBigInt::inc(globalObject, operandNumeric.asHeapBigInt()));
}
ALWAYS_INLINE JSValue jsDec(JSGlobalObject* globalObject, JSValue v)
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
auto operandNumeric = v.toNumeric(globalObject);
RETURN_IF_EXCEPTION(scope, { });
if (operandNumeric.isNumber())
return jsNumber(operandNumeric.asNumber() - 1);
#if USE(BIGINT32)
if (operandNumeric.isBigInt32())
RELEASE_AND_RETURN(scope, JSBigInt::dec(globalObject, operandNumeric.bigInt32AsInt32()));
#endif
ASSERT(operandNumeric.isHeapBigInt());
RELEASE_AND_RETURN(scope, JSBigInt::dec(globalObject, operandNumeric.asHeapBigInt()));
}
ALWAYS_INLINE JSValue jsBitwiseNot(JSGlobalObject* globalObject, JSValue v)
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
auto operandNumeric = v.toBigIntOrInt32(globalObject);
RETURN_IF_EXCEPTION(scope, { });
if (operandNumeric.isInt32())
return jsNumber(~operandNumeric.asInt32());
#if USE(BIGINT32)
if (operandNumeric.isBigInt32())
RELEASE_AND_RETURN(scope, JSBigInt::bitwiseNot(globalObject, operandNumeric.bigInt32AsInt32()));
#endif
ASSERT(operandNumeric.isHeapBigInt());
RELEASE_AND_RETURN(scope, JSBigInt::bitwiseNot(globalObject, operandNumeric.asHeapBigInt()));
}
template <bool isLeft>
ALWAYS_INLINE JSValue shift(JSGlobalObject* globalObject, JSValue v1, JSValue v2)
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
auto leftNumeric = v1.toBigIntOrInt32(globalObject);
RETURN_IF_EXCEPTION(scope, { });
auto rightNumeric = v2.toBigIntOrInt32(globalObject);
RETURN_IF_EXCEPTION(scope, { });
if (leftNumeric.isInt32() && rightNumeric.isInt32()) {
int32_t leftInt32 = leftNumeric.asInt32();
int32_t rightInt32 = rightNumeric.asInt32() & 31;
int32_t result = isLeft ? (leftInt32 << rightInt32) : (leftInt32 >> rightInt32);
return jsNumber(result);
}
#if USE(BIGINT32)
if (leftNumeric.isBigInt32()) {
if (rightNumeric.isBigInt32()) {
if (isLeft)
RELEASE_AND_RETURN(scope, JSBigInt::leftShift(globalObject, leftNumeric.bigInt32AsInt32(), rightNumeric.bigInt32AsInt32()));
RELEASE_AND_RETURN(scope, JSBigInt::signedRightShift(globalObject, leftNumeric.bigInt32AsInt32(), rightNumeric.bigInt32AsInt32()));
}
if (rightNumeric.isHeapBigInt()) {
if (isLeft)
RELEASE_AND_RETURN(scope, JSBigInt::leftShift(globalObject, leftNumeric.bigInt32AsInt32(), rightNumeric.asHeapBigInt()));
RELEASE_AND_RETURN(scope, JSBigInt::signedRightShift(globalObject, leftNumeric.bigInt32AsInt32(), rightNumeric.asHeapBigInt()));
}
} else if (leftNumeric.isHeapBigInt()) {
if (rightNumeric.isBigInt32()) {
if (isLeft)
RELEASE_AND_RETURN(scope, JSBigInt::leftShift(globalObject, leftNumeric.asHeapBigInt(), rightNumeric.bigInt32AsInt32()));
RELEASE_AND_RETURN(scope, JSBigInt::signedRightShift(globalObject, leftNumeric.asHeapBigInt(), rightNumeric.bigInt32AsInt32()));
} else if (rightNumeric.isHeapBigInt()) {
if (isLeft)
RELEASE_AND_RETURN(scope, JSBigInt::leftShift(globalObject, leftNumeric.asHeapBigInt(), rightNumeric.asHeapBigInt()));
RELEASE_AND_RETURN(scope, JSBigInt::signedRightShift(globalObject, leftNumeric.asHeapBigInt(), rightNumeric.asHeapBigInt()));
}
}
#else
if (leftNumeric.isHeapBigInt() && rightNumeric.isHeapBigInt()) {
if (isLeft)
RELEASE_AND_RETURN(scope, JSBigInt::leftShift(globalObject, leftNumeric.asHeapBigInt(), rightNumeric.asHeapBigInt()));
RELEASE_AND_RETURN(scope, JSBigInt::signedRightShift(globalObject, leftNumeric.asHeapBigInt(), rightNumeric.asHeapBigInt()));
}
#endif
auto errorMessage = isLeft ? "Invalid mix of BigInt and other type in left shift operation."_s : "Invalid mix of BigInt and other type in signed right shift operation."_s;
return throwTypeError(globalObject, scope, errorMessage);
}
ALWAYS_INLINE JSValue jsLShift(JSGlobalObject* globalObject, JSValue v1, JSValue v2)
{
constexpr bool isLeft = true;
return shift<isLeft>(globalObject, v1, v2);
}
ALWAYS_INLINE JSValue jsRShift(JSGlobalObject* globalObject, JSValue v1, JSValue v2)
{
constexpr bool isLeft = false;
return shift<isLeft>(globalObject, v1, v2);
}
ALWAYS_INLINE JSValue jsURShift(JSGlobalObject* globalObject, JSValue left, JSValue right)
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
std::optional<uint32_t> leftUint32 = left.toUInt32AfterToNumeric(globalObject);
RETURN_IF_EXCEPTION(scope, { });
std::optional<uint32_t> rightUint32 = right.toUInt32AfterToNumeric(globalObject);
RETURN_IF_EXCEPTION(scope, { });
if (UNLIKELY(!leftUint32 || !rightUint32)) {
throwTypeError(globalObject, scope, "BigInt does not support >>> operator"_s);
return { };
}
return jsNumber(static_cast<int32_t>(leftUint32.value() >> (rightUint32.value() & 31)));
}
template<typename Int32Operation, typename BigIntOp>
ALWAYS_INLINE JSValue bitwiseBinaryOp(JSGlobalObject* globalObject, JSValue v1, JSValue v2, Int32Operation&& int32Op, BigIntOp&& bigIntOp, ASCIILiteral errorMessage)
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
auto leftNumeric = v1.toBigIntOrInt32(globalObject);
RETURN_IF_EXCEPTION(scope, { });
auto rightNumeric = v2.toBigIntOrInt32(globalObject);
RETURN_IF_EXCEPTION(scope, { });
if (leftNumeric.isInt32() && rightNumeric.isInt32())
return jsNumber(int32Op(leftNumeric.asInt32(), rightNumeric.asInt32()));
#if USE(BIGINT32)
if (leftNumeric.isBigInt32()) {
if (rightNumeric.isBigInt32())
RELEASE_AND_RETURN(scope, bigIntOp(globalObject, leftNumeric.bigInt32AsInt32(), rightNumeric.bigInt32AsInt32()));
if (rightNumeric.isHeapBigInt())
RELEASE_AND_RETURN(scope, bigIntOp(globalObject, leftNumeric.bigInt32AsInt32(), rightNumeric.asHeapBigInt()));
} else if (leftNumeric.isHeapBigInt()) {
if (rightNumeric.isBigInt32())
RELEASE_AND_RETURN(scope, bigIntOp(globalObject, leftNumeric.asHeapBigInt(), rightNumeric.bigInt32AsInt32()));
else if (rightNumeric.isHeapBigInt())
RELEASE_AND_RETURN(scope, bigIntOp(globalObject, leftNumeric.asHeapBigInt(), rightNumeric.asHeapBigInt()));
}
#else
if (leftNumeric.isHeapBigInt() && rightNumeric.isHeapBigInt())
RELEASE_AND_RETURN(scope, bigIntOp(globalObject, leftNumeric.asHeapBigInt(), rightNumeric.asHeapBigInt()));
#endif
return throwTypeError(globalObject, scope, errorMessage);
}
ALWAYS_INLINE JSValue jsBitwiseAnd(JSGlobalObject* globalObject, JSValue v1, JSValue v2)
{
auto int32Op = [] (int32_t left, int32_t right) -> int32_t {
return left & right;
};
auto bigIntOp = [] (JSGlobalObject* globalObject, auto left, auto right) {
return JSBigInt::bitwiseAnd(globalObject, left, right);
};
// FIXME: currently, for pairs of BigInt32, we unbox them, do the "and" and re-box them.
// We could do it directly on the JSValue.
return bitwiseBinaryOp(globalObject, v1, v2, int32Op, bigIntOp, "Invalid mix of BigInt and other type in bitwise 'and' operation."_s);
}
ALWAYS_INLINE JSValue jsBitwiseOr(JSGlobalObject* globalObject, JSValue v1, JSValue v2)
{
auto int32Op = [] (int32_t left, int32_t right) -> int32_t {
return left | right;
};
// FIXME: currently, for pairs of BigInt32, we unbox them, do the "or" and re-box them.
// We could do it directly on the JSValue.
auto bigIntOp = [] (JSGlobalObject* globalObject, auto left, auto right) {
return JSBigInt::bitwiseOr(globalObject, left, right);
};
return bitwiseBinaryOp(globalObject, v1, v2, int32Op, bigIntOp, "Invalid mix of BigInt and other type in bitwise 'or' operation."_s);
}
ALWAYS_INLINE JSValue jsBitwiseXor(JSGlobalObject* globalObject, JSValue v1, JSValue v2)
{
auto int32Op = [] (int32_t left, int32_t right) -> int32_t {
return left ^ right;
};
// FIXME: currently, for pairs of BigInt32, we unbox them, do the "xor" and re-box them.
// We could do it directly on the JSValue, and just remember to do an or with 0x12 at the end to restore the tag
auto bigIntOp = [] (JSGlobalObject* globalObject, auto left, auto right) {
return JSBigInt::bitwiseXor(globalObject, left, right);
};
return bitwiseBinaryOp(globalObject, v1, v2, int32Op, bigIntOp, "Invalid mix of BigInt and other type in bitwise 'xor' operation."_s);
}
ALWAYS_INLINE EncodedJSValue getByValWithIndexAndThis(JSGlobalObject* globalObject, JSCell* base, uint32_t index, JSValue thisValue)
{
if (base->isObject()) {
if (JSValue result = asObject(base)->tryGetIndexQuickly(index))
return JSValue::encode(result);
} if (isJSString(base)) {
if (asString(base)->canGetIndex(index))
return JSValue::encode(asString(base)->getIndex(globalObject, index));
}
PropertySlot slot(thisValue, PropertySlot::PropertySlot::InternalMethodType::Get);
return JSValue::encode(JSValue(base).get(globalObject, index, slot));
}
ALWAYS_INLINE EncodedJSValue getByValWithIndex(JSGlobalObject* globalObject, JSCell* base, uint32_t index)
{
return getByValWithIndexAndThis(globalObject, base, index, base);
}
} // namespace JSC
WTF_ALLOW_UNSAFE_BUFFER_USAGE_END
|