Value.cpp 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2020-2021, Linus Groh <linusg@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/AllOf.h>
  8. #include <AK/FlyString.h>
  9. #include <AK/String.h>
  10. #include <AK/StringBuilder.h>
  11. #include <AK/Utf8View.h>
  12. #include <LibCrypto/BigInt/SignedBigInteger.h>
  13. #include <LibCrypto/NumberTheory/ModularFunctions.h>
  14. #include <LibJS/Heap/Heap.h>
  15. #include <LibJS/Runtime/Accessor.h>
  16. #include <LibJS/Runtime/Array.h>
  17. #include <LibJS/Runtime/BigInt.h>
  18. #include <LibJS/Runtime/BigIntObject.h>
  19. #include <LibJS/Runtime/BooleanObject.h>
  20. #include <LibJS/Runtime/BoundFunction.h>
  21. #include <LibJS/Runtime/Error.h>
  22. #include <LibJS/Runtime/Function.h>
  23. #include <LibJS/Runtime/GlobalObject.h>
  24. #include <LibJS/Runtime/NativeFunction.h>
  25. #include <LibJS/Runtime/NumberObject.h>
  26. #include <LibJS/Runtime/Object.h>
  27. #include <LibJS/Runtime/PrimitiveString.h>
  28. #include <LibJS/Runtime/RegExpObject.h>
  29. #include <LibJS/Runtime/StringObject.h>
  30. #include <LibJS/Runtime/Symbol.h>
  31. #include <LibJS/Runtime/SymbolObject.h>
  32. #include <LibJS/Runtime/Value.h>
  33. #include <ctype.h>
  34. #include <math.h>
  35. namespace JS {
  36. // Used in various abstract operations to make it obvious when a non-optional return value must be discarded.
  37. static const double INVALID { 0 };
  38. static inline bool same_type_for_equality(const Value& lhs, const Value& rhs)
  39. {
  40. if (lhs.type() == rhs.type())
  41. return true;
  42. if (lhs.is_number() && rhs.is_number())
  43. return true;
  44. return false;
  45. }
  46. static const Crypto::SignedBigInteger BIGINT_ZERO { 0 };
  47. static bool is_valid_bigint_value(StringView string)
  48. {
  49. string = string.trim_whitespace();
  50. if (string.length() > 1 && (string[0] == '-' || string[0] == '+'))
  51. string = string.substring_view(1, string.length() - 1);
  52. return all_of(string.begin(), string.end(), [](auto ch) { return isdigit(ch); });
  53. }
  54. ALWAYS_INLINE bool both_number(const Value& lhs, const Value& rhs)
  55. {
  56. return lhs.is_number() && rhs.is_number();
  57. }
  58. ALWAYS_INLINE bool both_bigint(const Value& lhs, const Value& rhs)
  59. {
  60. return lhs.is_bigint() && rhs.is_bigint();
  61. }
  62. static String double_to_string(double d)
  63. {
  64. // https://tc39.es/ecma262/#sec-numeric-types-number-tostring
  65. if (isnan(d))
  66. return "NaN";
  67. if (d == +0.0 || d == -0.0)
  68. return "0";
  69. if (d < +0.0) {
  70. StringBuilder builder;
  71. builder.append('-');
  72. builder.append(double_to_string(-d));
  73. return builder.to_string();
  74. }
  75. if (d == static_cast<double>(INFINITY))
  76. return "Infinity";
  77. StringBuilder number_string_builder;
  78. size_t start_index = 0;
  79. size_t end_index = 0;
  80. size_t int_part_end = 0;
  81. // generate integer part (reversed)
  82. double int_part;
  83. double frac_part;
  84. frac_part = modf(d, &int_part);
  85. while (int_part > 0) {
  86. number_string_builder.append('0' + (int)fmod(int_part, 10));
  87. end_index++;
  88. int_part = floor(int_part / 10);
  89. }
  90. auto reversed_integer_part = number_string_builder.to_string().reverse();
  91. number_string_builder.clear();
  92. number_string_builder.append(reversed_integer_part);
  93. int_part_end = end_index;
  94. int exponent = 0;
  95. // generate fractional part
  96. while (frac_part > 0) {
  97. double old_frac_part = frac_part;
  98. frac_part *= 10;
  99. frac_part = modf(frac_part, &int_part);
  100. if (old_frac_part == frac_part)
  101. break;
  102. number_string_builder.append('0' + (int)int_part);
  103. end_index++;
  104. exponent--;
  105. }
  106. auto number_string = number_string_builder.to_string();
  107. // FIXME: Remove this hack.
  108. // FIXME: Instead find the shortest round-trippable representation.
  109. // Remove decimals after the 15th position
  110. if (end_index > int_part_end + 15) {
  111. exponent += end_index - int_part_end - 15;
  112. end_index = int_part_end + 15;
  113. }
  114. // remove leading zeroes
  115. while (start_index < end_index && number_string[start_index] == '0') {
  116. start_index++;
  117. }
  118. // remove trailing zeroes
  119. while (end_index > 0 && number_string[end_index - 1] == '0') {
  120. end_index--;
  121. exponent++;
  122. }
  123. if (end_index <= start_index)
  124. return "0";
  125. auto digits = number_string.substring_view(start_index, end_index - start_index);
  126. int number_of_digits = end_index - start_index;
  127. exponent += number_of_digits;
  128. StringBuilder builder;
  129. if (number_of_digits <= exponent && exponent <= 21) {
  130. builder.append(digits);
  131. builder.append(String::repeated('0', exponent - number_of_digits));
  132. return builder.to_string();
  133. }
  134. if (0 < exponent && exponent <= 21) {
  135. builder.append(digits.substring_view(0, exponent));
  136. builder.append('.');
  137. builder.append(digits.substring_view(exponent));
  138. return builder.to_string();
  139. }
  140. if (-6 < exponent && exponent <= 0) {
  141. builder.append("0.");
  142. builder.append(String::repeated('0', -exponent));
  143. builder.append(digits);
  144. return builder.to_string();
  145. }
  146. if (number_of_digits == 1) {
  147. builder.append(digits);
  148. builder.append('e');
  149. if (exponent - 1 > 0)
  150. builder.append('+');
  151. else
  152. builder.append('-');
  153. builder.append(String::number(fabs(exponent - 1)));
  154. return builder.to_string();
  155. }
  156. builder.append(digits[0]);
  157. builder.append('.');
  158. builder.append(digits.substring_view(1));
  159. builder.append('e');
  160. if (exponent - 1 > 0)
  161. builder.append('+');
  162. else
  163. builder.append('-');
  164. builder.append(String::number(fabs(exponent - 1)));
  165. return builder.to_string();
  166. }
  167. bool Value::is_array() const
  168. {
  169. return is_object() && as_object().is_array();
  170. }
  171. Array& Value::as_array()
  172. {
  173. VERIFY(is_array());
  174. return static_cast<Array&>(*m_value.as_object);
  175. }
  176. bool Value::is_function() const
  177. {
  178. return is_object() && as_object().is_function();
  179. }
  180. Function& Value::as_function()
  181. {
  182. VERIFY(is_function());
  183. return static_cast<Function&>(as_object());
  184. }
  185. // 7.2.4 IsConstructor, https://tc39.es/ecma262/#sec-isconstructor
  186. bool Value::is_constructor() const
  187. {
  188. if (!is_function())
  189. return false;
  190. if (is<NativeFunction>(as_object()))
  191. return static_cast<const NativeFunction&>(as_object()).has_constructor();
  192. // ScriptFunction or BoundFunction
  193. return true;
  194. }
  195. // 7.2.8 IsRegExp, https://tc39.es/ecma262/#sec-isregexp
  196. bool Value::is_regexp(GlobalObject& global_object) const
  197. {
  198. if (!is_object())
  199. return false;
  200. auto matcher = as_object().get(global_object.vm().well_known_symbol_match());
  201. if (global_object.vm().exception())
  202. return false;
  203. if (!matcher.is_empty() && !matcher.is_undefined())
  204. return matcher.to_boolean();
  205. return is<RegExpObject>(as_object());
  206. }
  207. String Value::typeof() const
  208. {
  209. switch (m_type) {
  210. case Value::Type::Undefined:
  211. return "undefined";
  212. case Value::Type::Null:
  213. return "object";
  214. case Value::Type::Int32:
  215. case Value::Type::Double:
  216. return "number";
  217. case Value::Type::String:
  218. return "string";
  219. case Value::Type::Object:
  220. if (is_function())
  221. return "function";
  222. return "object";
  223. case Value::Type::Boolean:
  224. return "boolean";
  225. case Value::Type::Symbol:
  226. return "symbol";
  227. case Value::Type::BigInt:
  228. return "bigint";
  229. default:
  230. VERIFY_NOT_REACHED();
  231. }
  232. }
  233. String Value::to_string_without_side_effects() const
  234. {
  235. switch (m_type) {
  236. case Type::Undefined:
  237. return "undefined";
  238. case Type::Null:
  239. return "null";
  240. case Type::Boolean:
  241. return m_value.as_bool ? "true" : "false";
  242. case Type::Int32:
  243. return String::number(m_value.as_i32);
  244. case Type::Double:
  245. return double_to_string(m_value.as_double);
  246. case Type::String:
  247. return m_value.as_string->string();
  248. case Type::Symbol:
  249. return m_value.as_symbol->to_string();
  250. case Type::BigInt:
  251. return m_value.as_bigint->to_string();
  252. case Type::Object:
  253. return String::formatted("[object {}]", as_object().class_name());
  254. case Type::Accessor:
  255. return "<accessor>";
  256. case Type::NativeProperty:
  257. return "<native-property>";
  258. default:
  259. VERIFY_NOT_REACHED();
  260. }
  261. }
  262. PrimitiveString* Value::to_primitive_string(GlobalObject& global_object)
  263. {
  264. if (is_string())
  265. return &as_string();
  266. auto string = to_string(global_object);
  267. if (global_object.vm().exception())
  268. return nullptr;
  269. return js_string(global_object.heap(), string);
  270. }
  271. String Value::to_string(GlobalObject& global_object, bool legacy_null_to_empty_string) const
  272. {
  273. switch (m_type) {
  274. case Type::Undefined:
  275. return "undefined";
  276. case Type::Null:
  277. return !legacy_null_to_empty_string ? "null" : String::empty();
  278. case Type::Boolean:
  279. return m_value.as_bool ? "true" : "false";
  280. case Type::Int32:
  281. return String::number(m_value.as_i32);
  282. case Type::Double:
  283. return double_to_string(m_value.as_double);
  284. case Type::String:
  285. return m_value.as_string->string();
  286. case Type::Symbol:
  287. global_object.vm().throw_exception<TypeError>(global_object, ErrorType::Convert, "symbol", "string");
  288. return {};
  289. case Type::BigInt:
  290. return m_value.as_bigint->big_integer().to_base10();
  291. case Type::Object: {
  292. auto primitive_value = to_primitive(global_object, PreferredType::String);
  293. if (global_object.vm().exception())
  294. return {};
  295. return primitive_value.to_string(global_object);
  296. }
  297. default:
  298. VERIFY_NOT_REACHED();
  299. }
  300. }
  301. bool Value::to_boolean() const
  302. {
  303. switch (m_type) {
  304. case Type::Undefined:
  305. case Type::Null:
  306. return false;
  307. case Type::Boolean:
  308. return m_value.as_bool;
  309. case Type::Int32:
  310. return m_value.as_i32 != 0;
  311. case Type::Double:
  312. if (is_nan())
  313. return false;
  314. return m_value.as_double != 0;
  315. case Type::String:
  316. return !m_value.as_string->string().is_empty();
  317. case Type::Symbol:
  318. return true;
  319. case Type::BigInt:
  320. return m_value.as_bigint->big_integer() != BIGINT_ZERO;
  321. case Type::Object:
  322. return true;
  323. default:
  324. VERIFY_NOT_REACHED();
  325. }
  326. }
  327. Value Value::to_primitive(GlobalObject& global_object, PreferredType preferred_type) const
  328. {
  329. auto get_hint_for_preferred_type = [&]() -> String {
  330. switch (preferred_type) {
  331. case PreferredType::Default:
  332. return "default";
  333. case PreferredType::String:
  334. return "string";
  335. case PreferredType::Number:
  336. return "number";
  337. default:
  338. VERIFY_NOT_REACHED();
  339. }
  340. };
  341. if (is_object()) {
  342. auto& vm = global_object.vm();
  343. auto to_primitive_method = get_method(global_object, *this, vm.well_known_symbol_to_primitive());
  344. if (vm.exception())
  345. return {};
  346. if (to_primitive_method) {
  347. auto hint = get_hint_for_preferred_type();
  348. auto result = vm.call(*to_primitive_method, *this, js_string(vm, hint));
  349. if (vm.exception())
  350. return {};
  351. if (!result.is_object())
  352. return result;
  353. vm.throw_exception<TypeError>(global_object, ErrorType::ToPrimitiveReturnedObject, to_string_without_side_effects(), hint);
  354. return {};
  355. }
  356. if (preferred_type == PreferredType::Default)
  357. preferred_type = PreferredType::Number;
  358. return as_object().ordinary_to_primitive(preferred_type);
  359. }
  360. return *this;
  361. }
  362. Object* Value::to_object(GlobalObject& global_object) const
  363. {
  364. switch (m_type) {
  365. case Type::Undefined:
  366. case Type::Null:
  367. global_object.vm().throw_exception<TypeError>(global_object, ErrorType::ToObjectNullOrUndefined);
  368. return nullptr;
  369. case Type::Boolean:
  370. return BooleanObject::create(global_object, m_value.as_bool);
  371. case Type::Int32:
  372. case Type::Double:
  373. return NumberObject::create(global_object, as_double());
  374. case Type::String:
  375. return StringObject::create(global_object, *m_value.as_string);
  376. case Type::Symbol:
  377. return SymbolObject::create(global_object, *m_value.as_symbol);
  378. case Type::BigInt:
  379. return BigIntObject::create(global_object, *m_value.as_bigint);
  380. case Type::Object:
  381. return &const_cast<Object&>(as_object());
  382. default:
  383. dbgln("Dying because I can't to_object() on {}", *this);
  384. VERIFY_NOT_REACHED();
  385. }
  386. }
  387. FLATTEN Value Value::to_numeric(GlobalObject& global_object) const
  388. {
  389. auto primitive = to_primitive(global_object, Value::PreferredType::Number);
  390. if (global_object.vm().exception())
  391. return {};
  392. if (primitive.is_bigint())
  393. return primitive;
  394. return primitive.to_number(global_object);
  395. }
  396. Value Value::to_number(GlobalObject& global_object) const
  397. {
  398. switch (m_type) {
  399. case Type::Undefined:
  400. return js_nan();
  401. case Type::Null:
  402. return Value(0);
  403. case Type::Boolean:
  404. return Value(m_value.as_bool ? 1 : 0);
  405. case Type::Int32:
  406. case Type::Double:
  407. return *this;
  408. case Type::String: {
  409. auto string = as_string().string().trim_whitespace();
  410. if (string.is_empty())
  411. return Value(0);
  412. if (string == "Infinity" || string == "+Infinity")
  413. return js_infinity();
  414. if (string == "-Infinity")
  415. return js_negative_infinity();
  416. char* endptr;
  417. auto parsed_double = strtod(string.characters(), &endptr);
  418. if (*endptr)
  419. return js_nan();
  420. return Value(parsed_double);
  421. }
  422. case Type::Symbol:
  423. global_object.vm().throw_exception<TypeError>(global_object, ErrorType::Convert, "symbol", "number");
  424. return {};
  425. case Type::BigInt:
  426. global_object.vm().throw_exception<TypeError>(global_object, ErrorType::Convert, "BigInt", "number");
  427. return {};
  428. case Type::Object: {
  429. auto primitive = to_primitive(global_object, PreferredType::Number);
  430. if (global_object.vm().exception())
  431. return {};
  432. return primitive.to_number(global_object);
  433. }
  434. default:
  435. VERIFY_NOT_REACHED();
  436. }
  437. }
  438. BigInt* Value::to_bigint(GlobalObject& global_object) const
  439. {
  440. auto& vm = global_object.vm();
  441. auto primitive = to_primitive(global_object, PreferredType::Number);
  442. if (vm.exception())
  443. return nullptr;
  444. switch (primitive.type()) {
  445. case Type::Undefined:
  446. vm.throw_exception<TypeError>(global_object, ErrorType::Convert, "undefined", "BigInt");
  447. return nullptr;
  448. case Type::Null:
  449. vm.throw_exception<TypeError>(global_object, ErrorType::Convert, "null", "BigInt");
  450. return nullptr;
  451. case Type::Boolean: {
  452. auto value = primitive.as_bool() ? 1 : 0;
  453. return js_bigint(vm.heap(), Crypto::SignedBigInteger { value });
  454. }
  455. case Type::BigInt:
  456. return &primitive.as_bigint();
  457. case Type::Int32:
  458. case Type::Double:
  459. vm.throw_exception<TypeError>(global_object, ErrorType::Convert, "number", "BigInt");
  460. return {};
  461. case Type::String: {
  462. auto& string = primitive.as_string().string();
  463. if (!is_valid_bigint_value(string)) {
  464. vm.throw_exception<SyntaxError>(global_object, ErrorType::BigIntInvalidValue, string);
  465. return {};
  466. }
  467. return js_bigint(vm.heap(), Crypto::SignedBigInteger::from_base10(string.trim_whitespace()));
  468. }
  469. case Type::Symbol:
  470. vm.throw_exception<TypeError>(global_object, ErrorType::Convert, "symbol", "BigInt");
  471. return {};
  472. default:
  473. VERIFY_NOT_REACHED();
  474. }
  475. }
  476. // FIXME: These two conversions are wrong for JS, and seem likely to be footguns
  477. i32 Value::as_i32() const
  478. {
  479. return static_cast<i32>(as_double());
  480. }
  481. u32 Value::as_u32() const
  482. {
  483. VERIFY(as_double() >= 0);
  484. return min((u32)as_i32(), NumericLimits<u32>::max());
  485. }
  486. double Value::to_double(GlobalObject& global_object) const
  487. {
  488. auto number = to_number(global_object);
  489. if (global_object.vm().exception())
  490. return INVALID;
  491. return number.as_double();
  492. }
  493. StringOrSymbol Value::to_property_key(GlobalObject& global_object) const
  494. {
  495. auto key = to_primitive(global_object, PreferredType::String);
  496. if (global_object.vm().exception())
  497. return {};
  498. if (key.is_symbol())
  499. return &key.as_symbol();
  500. return to_string(global_object);
  501. }
  502. i32 Value::to_i32_slow_case(GlobalObject& global_object) const
  503. {
  504. VERIFY(type() != Type::Int32);
  505. auto number = to_number(global_object);
  506. if (global_object.vm().exception())
  507. return INVALID;
  508. double value = number.as_double();
  509. if (!isfinite(value) || value == 0)
  510. return 0;
  511. auto abs = fabs(value);
  512. auto int_val = floor(abs);
  513. if (signbit(value))
  514. int_val = -int_val;
  515. auto int32bit = fmod(int_val, 4294967296.0);
  516. if (int32bit >= 2147483648.0)
  517. int32bit -= 4294967296.0;
  518. return static_cast<i32>(int32bit);
  519. }
  520. u32 Value::to_u32(GlobalObject& global_object) const
  521. {
  522. // 7.1.7 ToUint32, https://tc39.es/ecma262/#sec-touint32
  523. auto number = to_number(global_object);
  524. if (global_object.vm().exception())
  525. return INVALID;
  526. double value = number.as_double();
  527. if (!isfinite(value) || value == 0)
  528. return 0;
  529. auto int_val = floor(fabs(value));
  530. if (signbit(value))
  531. int_val = -int_val;
  532. auto int32bit = fmod(int_val, NumericLimits<u32>::max() + 1.0);
  533. return static_cast<u32>(int32bit);
  534. }
  535. size_t Value::to_length(GlobalObject& global_object) const
  536. {
  537. // 7.1.20 ToLength, https://tc39.es/ecma262/#sec-tolength
  538. auto& vm = global_object.vm();
  539. auto len = to_integer_or_infinity(global_object);
  540. if (vm.exception())
  541. return INVALID;
  542. if (len <= 0)
  543. return 0;
  544. return min(len, MAX_ARRAY_LIKE_INDEX);
  545. }
  546. size_t Value::to_index(GlobalObject& global_object) const
  547. {
  548. // 7.1.22 ToIndex, https://tc39.es/ecma262/#sec-toindex
  549. auto& vm = global_object.vm();
  550. if (is_undefined())
  551. return 0;
  552. auto integer_index = to_integer_or_infinity(global_object);
  553. if (vm.exception())
  554. return INVALID;
  555. if (integer_index < 0) {
  556. vm.throw_exception<RangeError>(global_object, ErrorType::InvalidIndex);
  557. return INVALID;
  558. }
  559. auto index = Value(integer_index).to_length(global_object);
  560. VERIFY(!vm.exception());
  561. if (integer_index != index) {
  562. vm.throw_exception<RangeError>(global_object, ErrorType::InvalidIndex);
  563. return INVALID;
  564. }
  565. return index;
  566. }
  567. double Value::to_integer_or_infinity(GlobalObject& global_object) const
  568. {
  569. // 7.1.5 ToIntegerOrInfinity, https://tc39.es/ecma262/#sec-tointegerorinfinity
  570. auto& vm = global_object.vm();
  571. auto number = to_number(global_object);
  572. if (vm.exception())
  573. return INVALID;
  574. if (number.is_nan() || number.as_double() == 0)
  575. return 0;
  576. if (number.is_infinity())
  577. return number.as_double();
  578. auto integer = floor(fabs(number.as_double()));
  579. if (number.as_double() < 0)
  580. integer = -integer;
  581. return integer;
  582. }
  583. Value greater_than(GlobalObject& global_object, Value lhs, Value rhs)
  584. {
  585. TriState relation = abstract_relation(global_object, false, lhs, rhs);
  586. if (relation == TriState::Unknown)
  587. return Value(false);
  588. return Value(relation == TriState::True);
  589. }
  590. Value greater_than_equals(GlobalObject& global_object, Value lhs, Value rhs)
  591. {
  592. TriState relation = abstract_relation(global_object, true, lhs, rhs);
  593. if (relation == TriState::Unknown || relation == TriState::True)
  594. return Value(false);
  595. return Value(true);
  596. }
  597. Value less_than(GlobalObject& global_object, Value lhs, Value rhs)
  598. {
  599. TriState relation = abstract_relation(global_object, true, lhs, rhs);
  600. if (relation == TriState::Unknown)
  601. return Value(false);
  602. return Value(relation == TriState::True);
  603. }
  604. Value less_than_equals(GlobalObject& global_object, Value lhs, Value rhs)
  605. {
  606. TriState relation = abstract_relation(global_object, false, lhs, rhs);
  607. if (relation == TriState::Unknown || relation == TriState::True)
  608. return Value(false);
  609. return Value(true);
  610. }
  611. Value bitwise_and(GlobalObject& global_object, Value lhs, Value rhs)
  612. {
  613. auto lhs_numeric = lhs.to_numeric(global_object);
  614. if (global_object.vm().exception())
  615. return {};
  616. auto rhs_numeric = rhs.to_numeric(global_object);
  617. if (global_object.vm().exception())
  618. return {};
  619. if (both_number(lhs_numeric, rhs_numeric)) {
  620. if (!lhs_numeric.is_finite_number() || !rhs_numeric.is_finite_number())
  621. return Value(0);
  622. return Value(lhs_numeric.to_i32(global_object) & rhs_numeric.to_i32(global_object));
  623. }
  624. if (both_bigint(lhs_numeric, rhs_numeric))
  625. return js_bigint(global_object.heap(), lhs_numeric.as_bigint().big_integer().bitwise_and(rhs_numeric.as_bigint().big_integer()));
  626. global_object.vm().throw_exception<TypeError>(global_object, ErrorType::BigIntBadOperatorOtherType, "bitwise AND");
  627. return {};
  628. }
  629. Value bitwise_or(GlobalObject& global_object, Value lhs, Value rhs)
  630. {
  631. auto lhs_numeric = lhs.to_numeric(global_object);
  632. if (global_object.vm().exception())
  633. return {};
  634. auto rhs_numeric = rhs.to_numeric(global_object);
  635. if (global_object.vm().exception())
  636. return {};
  637. if (both_number(lhs_numeric, rhs_numeric)) {
  638. if (!lhs_numeric.is_finite_number() && !rhs_numeric.is_finite_number())
  639. return Value(0);
  640. if (!lhs_numeric.is_finite_number())
  641. return rhs_numeric;
  642. if (!rhs_numeric.is_finite_number())
  643. return lhs_numeric;
  644. return Value(lhs_numeric.to_i32(global_object) | rhs_numeric.to_i32(global_object));
  645. }
  646. if (both_bigint(lhs_numeric, rhs_numeric))
  647. return js_bigint(global_object.heap(), lhs_numeric.as_bigint().big_integer().bitwise_or(rhs_numeric.as_bigint().big_integer()));
  648. global_object.vm().throw_exception<TypeError>(global_object, ErrorType::BigIntBadOperatorOtherType, "bitwise OR");
  649. return {};
  650. }
  651. Value bitwise_xor(GlobalObject& global_object, Value lhs, Value rhs)
  652. {
  653. auto lhs_numeric = lhs.to_numeric(global_object);
  654. if (global_object.vm().exception())
  655. return {};
  656. auto rhs_numeric = rhs.to_numeric(global_object);
  657. if (global_object.vm().exception())
  658. return {};
  659. if (both_number(lhs_numeric, rhs_numeric)) {
  660. if (!lhs_numeric.is_finite_number() && !rhs_numeric.is_finite_number())
  661. return Value(0);
  662. if (!lhs_numeric.is_finite_number())
  663. return rhs_numeric;
  664. if (!rhs_numeric.is_finite_number())
  665. return lhs_numeric;
  666. return Value(lhs_numeric.to_i32(global_object) ^ rhs_numeric.to_i32(global_object));
  667. }
  668. if (both_bigint(lhs_numeric, rhs_numeric))
  669. return js_bigint(global_object.heap(), lhs_numeric.as_bigint().big_integer().bitwise_xor(rhs_numeric.as_bigint().big_integer()));
  670. global_object.vm().throw_exception<TypeError>(global_object, ErrorType::BigIntBadOperatorOtherType, "bitwise XOR");
  671. return {};
  672. }
  673. Value bitwise_not(GlobalObject& global_object, Value lhs)
  674. {
  675. auto lhs_numeric = lhs.to_numeric(global_object);
  676. if (global_object.vm().exception())
  677. return {};
  678. if (lhs_numeric.is_number())
  679. return Value(~lhs_numeric.to_i32(global_object));
  680. auto big_integer_bitwise_not = lhs_numeric.as_bigint().big_integer();
  681. big_integer_bitwise_not = big_integer_bitwise_not.plus(Crypto::SignedBigInteger { 1 });
  682. big_integer_bitwise_not.negate();
  683. return js_bigint(global_object.heap(), big_integer_bitwise_not);
  684. }
  685. Value unary_plus(GlobalObject& global_object, Value lhs)
  686. {
  687. return lhs.to_number(global_object);
  688. }
  689. Value unary_minus(GlobalObject& global_object, Value lhs)
  690. {
  691. auto lhs_numeric = lhs.to_numeric(global_object);
  692. if (global_object.vm().exception())
  693. return {};
  694. if (lhs_numeric.is_number()) {
  695. if (lhs_numeric.is_nan())
  696. return js_nan();
  697. return Value(-lhs_numeric.as_double());
  698. }
  699. if (lhs_numeric.as_bigint().big_integer() == BIGINT_ZERO)
  700. return js_bigint(global_object.heap(), BIGINT_ZERO);
  701. auto big_integer_negated = lhs_numeric.as_bigint().big_integer();
  702. big_integer_negated.negate();
  703. return js_bigint(global_object.heap(), big_integer_negated);
  704. }
  705. Value left_shift(GlobalObject& global_object, Value lhs, Value rhs)
  706. {
  707. // 6.1.6.1.9 Number::leftShift
  708. // https://tc39.es/ecma262/#sec-numeric-types-number-leftShift
  709. auto lhs_numeric = lhs.to_numeric(global_object);
  710. if (global_object.vm().exception())
  711. return {};
  712. auto rhs_numeric = rhs.to_numeric(global_object);
  713. if (global_object.vm().exception())
  714. return {};
  715. if (both_number(lhs_numeric, rhs_numeric)) {
  716. if (!lhs_numeric.is_finite_number())
  717. return Value(0);
  718. if (!rhs_numeric.is_finite_number())
  719. return lhs_numeric;
  720. // Ok, so this performs toNumber() again but that "can't" throw
  721. auto lhs_i32 = lhs_numeric.to_i32(global_object);
  722. auto rhs_u32 = rhs_numeric.to_u32(global_object);
  723. return Value(lhs_i32 << rhs_u32);
  724. }
  725. if (both_bigint(lhs_numeric, rhs_numeric)) {
  726. auto multiplier_divisor = Crypto::SignedBigInteger { Crypto::NumberTheory::Power(Crypto::UnsignedBigInteger(2), rhs_numeric.as_bigint().big_integer().unsigned_value()) };
  727. if (rhs_numeric.as_bigint().big_integer().is_negative())
  728. return js_bigint(global_object.heap(), lhs_numeric.as_bigint().big_integer().divided_by(multiplier_divisor).quotient);
  729. else
  730. return js_bigint(global_object.heap(), lhs_numeric.as_bigint().big_integer().multiplied_by(multiplier_divisor));
  731. }
  732. global_object.vm().throw_exception<TypeError>(global_object, ErrorType::BigIntBadOperatorOtherType, "left-shift");
  733. return {};
  734. }
  735. Value right_shift(GlobalObject& global_object, Value lhs, Value rhs)
  736. {
  737. // 6.1.6.1.11 Number::signedRightShift
  738. // https://tc39.es/ecma262/#sec-numeric-types-number-signedRightShift
  739. auto lhs_numeric = lhs.to_numeric(global_object);
  740. if (global_object.vm().exception())
  741. return {};
  742. auto rhs_numeric = rhs.to_numeric(global_object);
  743. if (global_object.vm().exception())
  744. return {};
  745. if (both_number(lhs_numeric, rhs_numeric)) {
  746. if (!lhs_numeric.is_finite_number())
  747. return Value(0);
  748. if (!rhs_numeric.is_finite_number())
  749. return lhs_numeric;
  750. // Ok, so this performs toNumber() again but that "can't" throw
  751. auto lhs_i32 = lhs_numeric.to_i32(global_object);
  752. auto rhs_u32 = rhs_numeric.to_u32(global_object);
  753. return Value(lhs_i32 >> rhs_u32);
  754. }
  755. if (both_bigint(lhs_numeric, rhs_numeric)) {
  756. auto rhs_negated = rhs_numeric.as_bigint().big_integer();
  757. rhs_negated.negate();
  758. return left_shift(global_object, lhs, js_bigint(global_object.heap(), rhs_negated));
  759. }
  760. global_object.vm().throw_exception<TypeError>(global_object, ErrorType::BigIntBadOperatorOtherType, "right-shift");
  761. return {};
  762. }
  763. Value unsigned_right_shift(GlobalObject& global_object, Value lhs, Value rhs)
  764. {
  765. // 6.1.6.1.11 Number::unsignedRightShift
  766. // https://tc39.es/ecma262/#sec-numeric-types-number-unsignedRightShift
  767. auto lhs_numeric = lhs.to_numeric(global_object);
  768. if (global_object.vm().exception())
  769. return {};
  770. auto rhs_numeric = rhs.to_numeric(global_object);
  771. if (global_object.vm().exception())
  772. return {};
  773. if (both_number(lhs_numeric, rhs_numeric)) {
  774. if (!lhs_numeric.is_finite_number())
  775. return Value(0);
  776. if (!rhs_numeric.is_finite_number())
  777. return lhs_numeric;
  778. // Ok, so this performs toNumber() again but that "can't" throw
  779. auto lhs_u32 = lhs_numeric.to_u32(global_object);
  780. auto rhs_u32 = rhs_numeric.to_u32(global_object) % 32;
  781. return Value(lhs_u32 >> rhs_u32);
  782. }
  783. global_object.vm().throw_exception<TypeError>(global_object, ErrorType::BigIntBadOperator, "unsigned right-shift");
  784. return {};
  785. }
  786. Value add(GlobalObject& global_object, Value lhs, Value rhs)
  787. {
  788. if (both_number(lhs, rhs)) {
  789. if (lhs.type() == Value::Type::Int32 && rhs.type() == Value::Type::Int32) {
  790. Checked<i32> result;
  791. result = lhs.to_i32(global_object);
  792. result += rhs.to_i32(global_object);
  793. if (!result.has_overflow())
  794. return Value(result.value());
  795. }
  796. return Value(lhs.as_double() + rhs.as_double());
  797. }
  798. auto& vm = global_object.vm();
  799. auto lhs_primitive = lhs.to_primitive(global_object);
  800. if (vm.exception())
  801. return {};
  802. auto rhs_primitive = rhs.to_primitive(global_object);
  803. if (vm.exception())
  804. return {};
  805. if (lhs_primitive.is_string() || rhs_primitive.is_string()) {
  806. auto lhs_string = lhs_primitive.to_string(global_object);
  807. if (vm.exception())
  808. return {};
  809. auto rhs_string = rhs_primitive.to_string(global_object);
  810. if (vm.exception())
  811. return {};
  812. StringBuilder builder(lhs_string.length() + rhs_string.length());
  813. builder.append(lhs_string);
  814. builder.append(rhs_string);
  815. return js_string(vm.heap(), builder.to_string());
  816. }
  817. auto lhs_numeric = lhs_primitive.to_numeric(global_object);
  818. if (vm.exception())
  819. return {};
  820. auto rhs_numeric = rhs_primitive.to_numeric(global_object);
  821. if (vm.exception())
  822. return {};
  823. if (both_number(lhs_numeric, rhs_numeric))
  824. return Value(lhs_numeric.as_double() + rhs_numeric.as_double());
  825. if (both_bigint(lhs_numeric, rhs_numeric))
  826. return js_bigint(vm.heap(), lhs_numeric.as_bigint().big_integer().plus(rhs_numeric.as_bigint().big_integer()));
  827. vm.throw_exception<TypeError>(global_object, ErrorType::BigIntBadOperatorOtherType, "addition");
  828. return {};
  829. }
  830. Value sub(GlobalObject& global_object, Value lhs, Value rhs)
  831. {
  832. auto lhs_numeric = lhs.to_numeric(global_object);
  833. if (global_object.vm().exception())
  834. return {};
  835. auto rhs_numeric = rhs.to_numeric(global_object);
  836. if (global_object.vm().exception())
  837. return {};
  838. if (both_number(lhs_numeric, rhs_numeric))
  839. return Value(lhs_numeric.as_double() - rhs_numeric.as_double());
  840. if (both_bigint(lhs_numeric, rhs_numeric))
  841. return js_bigint(global_object.heap(), lhs_numeric.as_bigint().big_integer().minus(rhs_numeric.as_bigint().big_integer()));
  842. global_object.vm().throw_exception<TypeError>(global_object, ErrorType::BigIntBadOperatorOtherType, "subtraction");
  843. return {};
  844. }
  845. Value mul(GlobalObject& global_object, Value lhs, Value rhs)
  846. {
  847. auto lhs_numeric = lhs.to_numeric(global_object);
  848. if (global_object.vm().exception())
  849. return {};
  850. auto rhs_numeric = rhs.to_numeric(global_object);
  851. if (global_object.vm().exception())
  852. return {};
  853. if (both_number(lhs_numeric, rhs_numeric))
  854. return Value(lhs_numeric.as_double() * rhs_numeric.as_double());
  855. if (both_bigint(lhs_numeric, rhs_numeric))
  856. return js_bigint(global_object.heap(), lhs_numeric.as_bigint().big_integer().multiplied_by(rhs_numeric.as_bigint().big_integer()));
  857. global_object.vm().throw_exception<TypeError>(global_object, ErrorType::BigIntBadOperatorOtherType, "multiplication");
  858. return {};
  859. }
  860. Value div(GlobalObject& global_object, Value lhs, Value rhs)
  861. {
  862. auto& vm = global_object.vm();
  863. auto lhs_numeric = lhs.to_numeric(global_object);
  864. if (vm.exception())
  865. return {};
  866. auto rhs_numeric = rhs.to_numeric(global_object);
  867. if (vm.exception())
  868. return {};
  869. if (both_number(lhs_numeric, rhs_numeric))
  870. return Value(lhs_numeric.as_double() / rhs_numeric.as_double());
  871. if (both_bigint(lhs_numeric, rhs_numeric)) {
  872. if (rhs_numeric.as_bigint().big_integer() == BIGINT_ZERO) {
  873. vm.throw_exception<RangeError>(global_object, ErrorType::DivisionByZero);
  874. return {};
  875. }
  876. return js_bigint(global_object.heap(), lhs_numeric.as_bigint().big_integer().divided_by(rhs_numeric.as_bigint().big_integer()).quotient);
  877. }
  878. vm.throw_exception<TypeError>(global_object, ErrorType::BigIntBadOperatorOtherType, "division");
  879. return {};
  880. }
  881. Value mod(GlobalObject& global_object, Value lhs, Value rhs)
  882. {
  883. auto& vm = global_object.vm();
  884. auto lhs_numeric = lhs.to_numeric(global_object);
  885. if (vm.exception())
  886. return {};
  887. auto rhs_numeric = rhs.to_numeric(global_object);
  888. if (vm.exception())
  889. return {};
  890. if (both_number(lhs_numeric, rhs_numeric)) {
  891. if (lhs_numeric.is_nan() || rhs_numeric.is_nan())
  892. return js_nan();
  893. auto index = lhs_numeric.as_double();
  894. auto period = rhs_numeric.as_double();
  895. auto trunc = (double)(i32)(index / period);
  896. return Value(index - trunc * period);
  897. }
  898. if (both_bigint(lhs_numeric, rhs_numeric)) {
  899. if (rhs_numeric.as_bigint().big_integer() == BIGINT_ZERO) {
  900. vm.throw_exception<RangeError>(global_object, ErrorType::DivisionByZero);
  901. return {};
  902. }
  903. return js_bigint(global_object.heap(), lhs_numeric.as_bigint().big_integer().divided_by(rhs_numeric.as_bigint().big_integer()).remainder);
  904. }
  905. vm.throw_exception<TypeError>(global_object, ErrorType::BigIntBadOperatorOtherType, "modulo");
  906. return {};
  907. }
  908. Value exp(GlobalObject& global_object, Value lhs, Value rhs)
  909. {
  910. auto& vm = global_object.vm();
  911. auto lhs_numeric = lhs.to_numeric(global_object);
  912. if (vm.exception())
  913. return {};
  914. auto rhs_numeric = rhs.to_numeric(global_object);
  915. if (vm.exception())
  916. return {};
  917. if (both_number(lhs_numeric, rhs_numeric))
  918. return Value(pow(lhs_numeric.as_double(), rhs_numeric.as_double()));
  919. if (both_bigint(lhs_numeric, rhs_numeric)) {
  920. if (rhs_numeric.as_bigint().big_integer().is_negative()) {
  921. vm.throw_exception<RangeError>(global_object, ErrorType::NegativeExponent);
  922. return {};
  923. }
  924. return js_bigint(vm.heap(), Crypto::NumberTheory::Power(lhs_numeric.as_bigint().big_integer(), rhs_numeric.as_bigint().big_integer()));
  925. }
  926. vm.throw_exception<TypeError>(global_object, ErrorType::BigIntBadOperatorOtherType, "exponentiation");
  927. return {};
  928. }
  929. Value in(GlobalObject& global_object, Value lhs, Value rhs)
  930. {
  931. if (!rhs.is_object()) {
  932. global_object.vm().throw_exception<TypeError>(global_object, ErrorType::InOperatorWithObject);
  933. return {};
  934. }
  935. auto lhs_property_key = lhs.to_property_key(global_object);
  936. if (global_object.vm().exception())
  937. return {};
  938. return Value(rhs.as_object().has_property(lhs_property_key));
  939. }
  940. Value instance_of(GlobalObject& global_object, Value lhs, Value rhs)
  941. {
  942. auto& vm = global_object.vm();
  943. if (!rhs.is_object()) {
  944. vm.throw_exception<TypeError>(global_object, ErrorType::NotAnObject, rhs.to_string_without_side_effects());
  945. return {};
  946. }
  947. auto has_instance_method = get_method(global_object, Value(&rhs.as_object()), vm.well_known_symbol_has_instance());
  948. if (vm.exception())
  949. return {};
  950. if (has_instance_method) {
  951. auto has_instance_result = vm.call(*has_instance_method, rhs, lhs);
  952. if (vm.exception())
  953. return {};
  954. return Value(has_instance_result.to_boolean());
  955. }
  956. if (!rhs.is_function()) {
  957. vm.throw_exception<TypeError>(global_object, ErrorType::NotAFunction, rhs.to_string_without_side_effects());
  958. return {};
  959. }
  960. return ordinary_has_instance(global_object, lhs, rhs);
  961. }
  962. Value ordinary_has_instance(GlobalObject& global_object, Value lhs, Value rhs)
  963. {
  964. auto& vm = global_object.vm();
  965. if (!rhs.is_function())
  966. return Value(false);
  967. auto& rhs_function = rhs.as_function();
  968. if (is<BoundFunction>(rhs_function)) {
  969. auto& bound_target = static_cast<const BoundFunction&>(rhs_function);
  970. return instance_of(global_object, lhs, Value(&bound_target.target_function()));
  971. }
  972. if (!lhs.is_object())
  973. return Value(false);
  974. Object* lhs_object = &lhs.as_object();
  975. auto rhs_prototype = rhs_function.get(vm.names.prototype);
  976. if (vm.exception())
  977. return {};
  978. if (!rhs_prototype.is_object()) {
  979. vm.throw_exception<TypeError>(global_object, ErrorType::InstanceOfOperatorBadPrototype, rhs.to_string_without_side_effects());
  980. return {};
  981. }
  982. while (true) {
  983. lhs_object = lhs_object->prototype();
  984. if (vm.exception())
  985. return {};
  986. if (!lhs_object)
  987. return Value(false);
  988. if (same_value(rhs_prototype, lhs_object))
  989. return Value(true);
  990. }
  991. }
  992. bool same_value(Value lhs, Value rhs)
  993. {
  994. if (!same_type_for_equality(lhs, rhs))
  995. return false;
  996. if (lhs.is_number()) {
  997. if (lhs.is_nan() && rhs.is_nan())
  998. return true;
  999. if (lhs.is_positive_zero() && rhs.is_negative_zero())
  1000. return false;
  1001. if (lhs.is_negative_zero() && rhs.is_positive_zero())
  1002. return false;
  1003. return lhs.as_double() == rhs.as_double();
  1004. }
  1005. if (lhs.is_bigint()) {
  1006. auto lhs_big_integer = lhs.as_bigint().big_integer();
  1007. auto rhs_big_integer = rhs.as_bigint().big_integer();
  1008. if (lhs_big_integer == BIGINT_ZERO && rhs_big_integer == BIGINT_ZERO && lhs_big_integer.is_negative() != rhs_big_integer.is_negative())
  1009. return false;
  1010. return lhs_big_integer == rhs_big_integer;
  1011. }
  1012. return same_value_non_numeric(lhs, rhs);
  1013. }
  1014. bool same_value_zero(Value lhs, Value rhs)
  1015. {
  1016. if (!same_type_for_equality(lhs, rhs))
  1017. return false;
  1018. if (lhs.is_number()) {
  1019. if (lhs.is_nan() && rhs.is_nan())
  1020. return true;
  1021. return lhs.as_double() == rhs.as_double();
  1022. }
  1023. if (lhs.is_bigint())
  1024. return lhs.as_bigint().big_integer() == rhs.as_bigint().big_integer();
  1025. return same_value_non_numeric(lhs, rhs);
  1026. }
  1027. bool same_value_non_numeric(Value lhs, Value rhs)
  1028. {
  1029. VERIFY(!lhs.is_number() && !lhs.is_bigint());
  1030. VERIFY(same_type_for_equality(lhs, rhs));
  1031. switch (lhs.type()) {
  1032. case Value::Type::Undefined:
  1033. case Value::Type::Null:
  1034. return true;
  1035. case Value::Type::String:
  1036. return lhs.as_string().string() == rhs.as_string().string();
  1037. case Value::Type::Symbol:
  1038. return &lhs.as_symbol() == &rhs.as_symbol();
  1039. case Value::Type::Boolean:
  1040. return lhs.as_bool() == rhs.as_bool();
  1041. case Value::Type::Object:
  1042. return &lhs.as_object() == &rhs.as_object();
  1043. default:
  1044. VERIFY_NOT_REACHED();
  1045. }
  1046. }
  1047. bool strict_eq(Value lhs, Value rhs)
  1048. {
  1049. if (!same_type_for_equality(lhs, rhs))
  1050. return false;
  1051. if (lhs.is_number()) {
  1052. if (lhs.is_nan() || rhs.is_nan())
  1053. return false;
  1054. if (lhs.as_double() == rhs.as_double())
  1055. return true;
  1056. return false;
  1057. }
  1058. if (lhs.is_bigint())
  1059. return lhs.as_bigint().big_integer() == rhs.as_bigint().big_integer();
  1060. return same_value_non_numeric(lhs, rhs);
  1061. }
  1062. bool abstract_eq(GlobalObject& global_object, Value lhs, Value rhs)
  1063. {
  1064. if (same_type_for_equality(lhs, rhs))
  1065. return strict_eq(lhs, rhs);
  1066. if (lhs.is_nullish() && rhs.is_nullish())
  1067. return true;
  1068. if (lhs.is_number() && rhs.is_string())
  1069. return abstract_eq(global_object, lhs, rhs.to_number(global_object));
  1070. if (lhs.is_string() && rhs.is_number())
  1071. return abstract_eq(global_object, lhs.to_number(global_object), rhs);
  1072. if (lhs.is_bigint() && rhs.is_string()) {
  1073. auto& rhs_string = rhs.as_string().string();
  1074. if (!is_valid_bigint_value(rhs_string))
  1075. return false;
  1076. return abstract_eq(global_object, lhs, js_bigint(global_object.heap(), Crypto::SignedBigInteger::from_base10(rhs_string)));
  1077. }
  1078. if (lhs.is_string() && rhs.is_bigint())
  1079. return abstract_eq(global_object, rhs, lhs);
  1080. if (lhs.is_boolean())
  1081. return abstract_eq(global_object, lhs.to_number(global_object), rhs);
  1082. if (rhs.is_boolean())
  1083. return abstract_eq(global_object, lhs, rhs.to_number(global_object));
  1084. if ((lhs.is_string() || lhs.is_number() || lhs.is_bigint() || lhs.is_symbol()) && rhs.is_object()) {
  1085. auto rhs_primitive = rhs.to_primitive(global_object);
  1086. if (global_object.vm().exception())
  1087. return false;
  1088. return abstract_eq(global_object, lhs, rhs_primitive);
  1089. }
  1090. if (lhs.is_object() && (rhs.is_string() || rhs.is_number() || lhs.is_bigint() || rhs.is_symbol())) {
  1091. auto lhs_primitive = lhs.to_primitive(global_object);
  1092. if (global_object.vm().exception())
  1093. return false;
  1094. return abstract_eq(global_object, lhs_primitive, rhs);
  1095. }
  1096. if ((lhs.is_bigint() && rhs.is_number()) || (lhs.is_number() && rhs.is_bigint())) {
  1097. if (lhs.is_nan() || lhs.is_infinity() || rhs.is_nan() || rhs.is_infinity())
  1098. return false;
  1099. if ((lhs.is_number() && !lhs.is_integer()) || (rhs.is_number() && !rhs.is_integer()))
  1100. return false;
  1101. if (lhs.is_number())
  1102. return Crypto::SignedBigInteger { lhs.to_i32(global_object) } == rhs.as_bigint().big_integer();
  1103. else
  1104. return Crypto::SignedBigInteger { rhs.to_i32(global_object) } == lhs.as_bigint().big_integer();
  1105. }
  1106. return false;
  1107. }
  1108. TriState abstract_relation(GlobalObject& global_object, bool left_first, Value lhs, Value rhs)
  1109. {
  1110. Value x_primitive;
  1111. Value y_primitive;
  1112. if (left_first) {
  1113. x_primitive = lhs.to_primitive(global_object, Value::PreferredType::Number);
  1114. if (global_object.vm().exception())
  1115. return {};
  1116. y_primitive = rhs.to_primitive(global_object, Value::PreferredType::Number);
  1117. if (global_object.vm().exception())
  1118. return {};
  1119. } else {
  1120. y_primitive = lhs.to_primitive(global_object, Value::PreferredType::Number);
  1121. if (global_object.vm().exception())
  1122. return {};
  1123. x_primitive = rhs.to_primitive(global_object, Value::PreferredType::Number);
  1124. if (global_object.vm().exception())
  1125. return {};
  1126. }
  1127. if (x_primitive.is_string() && y_primitive.is_string()) {
  1128. auto x_string = x_primitive.as_string().string();
  1129. auto y_string = y_primitive.as_string().string();
  1130. Utf8View x_code_points { x_string };
  1131. Utf8View y_code_points { y_string };
  1132. if (x_code_points.starts_with(y_code_points))
  1133. return TriState::False;
  1134. if (y_code_points.starts_with(x_code_points))
  1135. return TriState::True;
  1136. for (auto k = x_code_points.begin(), l = y_code_points.begin();
  1137. k != x_code_points.end() && l != y_code_points.end();
  1138. ++k, ++l) {
  1139. if (*k != *l) {
  1140. if (*k < *l) {
  1141. return TriState::True;
  1142. } else {
  1143. return TriState::False;
  1144. }
  1145. }
  1146. }
  1147. VERIFY_NOT_REACHED();
  1148. }
  1149. if (x_primitive.is_bigint() && y_primitive.is_string()) {
  1150. auto& y_string = y_primitive.as_string().string();
  1151. if (!is_valid_bigint_value(y_string))
  1152. return TriState::Unknown;
  1153. if (x_primitive.as_bigint().big_integer() < Crypto::SignedBigInteger::from_base10(y_string))
  1154. return TriState::True;
  1155. else
  1156. return TriState::False;
  1157. }
  1158. if (x_primitive.is_string() && y_primitive.is_bigint()) {
  1159. auto& x_string = x_primitive.as_string().string();
  1160. if (!is_valid_bigint_value(x_string))
  1161. return TriState::Unknown;
  1162. if (Crypto::SignedBigInteger::from_base10(x_string) < y_primitive.as_bigint().big_integer())
  1163. return TriState::True;
  1164. else
  1165. return TriState::False;
  1166. }
  1167. auto x_numeric = x_primitive.to_numeric(global_object);
  1168. if (global_object.vm().exception())
  1169. return {};
  1170. auto y_numeric = y_primitive.to_numeric(global_object);
  1171. if (global_object.vm().exception())
  1172. return {};
  1173. if (x_numeric.is_nan() || y_numeric.is_nan())
  1174. return TriState::Unknown;
  1175. if (x_numeric.is_positive_infinity() || y_numeric.is_negative_infinity())
  1176. return TriState::False;
  1177. if (x_numeric.is_negative_infinity() || y_numeric.is_positive_infinity())
  1178. return TriState::True;
  1179. if (x_numeric.is_number() && y_numeric.is_number()) {
  1180. if (x_numeric.as_double() < y_numeric.as_double())
  1181. return TriState::True;
  1182. else
  1183. return TriState::False;
  1184. }
  1185. if (x_numeric.is_bigint() && y_numeric.is_bigint()) {
  1186. if (x_numeric.as_bigint().big_integer() < y_numeric.as_bigint().big_integer())
  1187. return TriState::True;
  1188. else
  1189. return TriState::False;
  1190. }
  1191. VERIFY((x_numeric.is_number() && y_numeric.is_bigint()) || (x_numeric.is_bigint() && y_numeric.is_number()));
  1192. bool x_lower_than_y;
  1193. if (x_numeric.is_number()) {
  1194. x_lower_than_y = x_numeric.is_integer()
  1195. ? Crypto::SignedBigInteger { x_numeric.to_i32(global_object) } < y_numeric.as_bigint().big_integer()
  1196. : (Crypto::SignedBigInteger { x_numeric.to_i32(global_object) } < y_numeric.as_bigint().big_integer() || Crypto::SignedBigInteger { x_numeric.to_i32(global_object) + 1 } < y_numeric.as_bigint().big_integer());
  1197. } else {
  1198. x_lower_than_y = y_numeric.is_integer()
  1199. ? x_numeric.as_bigint().big_integer() < Crypto::SignedBigInteger { y_numeric.to_i32(global_object) }
  1200. : (x_numeric.as_bigint().big_integer() < Crypto::SignedBigInteger { y_numeric.to_i32(global_object) } || x_numeric.as_bigint().big_integer() < Crypto::SignedBigInteger { y_numeric.to_i32(global_object) + 1 });
  1201. }
  1202. if (x_lower_than_y)
  1203. return TriState::True;
  1204. else
  1205. return TriState::False;
  1206. }
  1207. // 7.3.10 GetMethod, https://tc39.es/ecma262/#sec-getmethod
  1208. Function* get_method(GlobalObject& global_object, Value value, const PropertyName& property_name)
  1209. {
  1210. auto& vm = global_object.vm();
  1211. auto* object = value.to_object(global_object);
  1212. if (vm.exception())
  1213. return nullptr;
  1214. auto property_value = object->get(property_name);
  1215. if (vm.exception())
  1216. return nullptr;
  1217. if (property_value.is_empty() || property_value.is_nullish())
  1218. return nullptr;
  1219. if (!property_value.is_function()) {
  1220. vm.throw_exception<TypeError>(global_object, ErrorType::NotAFunction, property_value.to_string_without_side_effects());
  1221. return nullptr;
  1222. }
  1223. return &property_value.as_function();
  1224. }
  1225. // 7.3.18 LengthOfArrayLike, https://tc39.es/ecma262/#sec-lengthofarraylike
  1226. size_t length_of_array_like(GlobalObject& global_object, const Object& object)
  1227. {
  1228. auto& vm = global_object.vm();
  1229. auto result = object.get(vm.names.length).value_or(js_undefined());
  1230. if (vm.exception())
  1231. return INVALID;
  1232. return result.to_length(global_object);
  1233. }
  1234. // 7.3.22 SpeciesConstructor, https://tc39.es/ecma262/#sec-speciesconstructor
  1235. Object* species_constructor(GlobalObject& global_object, const Object& object, Object& default_constructor)
  1236. {
  1237. auto& vm = global_object.vm();
  1238. auto constructor = object.get(vm.names.constructor).value_or(js_undefined());
  1239. if (vm.exception())
  1240. return nullptr;
  1241. if (constructor.is_undefined())
  1242. return &default_constructor;
  1243. if (!constructor.is_object()) {
  1244. vm.throw_exception<TypeError>(global_object, ErrorType::NotAConstructor, constructor.to_string_without_side_effects());
  1245. return nullptr;
  1246. }
  1247. auto species = constructor.as_object().get(vm.well_known_symbol_species()).value_or(js_undefined());
  1248. if (species.is_nullish())
  1249. return &default_constructor;
  1250. if (species.is_constructor())
  1251. return &species.as_object();
  1252. vm.throw_exception<TypeError>(global_object, ErrorType::NotAConstructor, species.to_string_without_side_effects());
  1253. return nullptr;
  1254. }
  1255. }