Value.cpp 41 KB

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