Value.cpp 45 KB

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