Value.cpp 42 KB

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