Value.cpp 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235
  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. u32 Value::as_u32() const
  425. {
  426. ASSERT(as_double() >= 0);
  427. return min((double)as_i32(), MAX_U32);
  428. }
  429. size_t Value::as_size_t() const
  430. {
  431. ASSERT(as_double() >= 0);
  432. return min((double)as_i32(), MAX_ARRAY_LIKE_INDEX);
  433. }
  434. double Value::to_double(GlobalObject& global_object) const
  435. {
  436. auto number = to_number(global_object);
  437. if (global_object.vm().exception())
  438. return INVALID;
  439. return number.as_double();
  440. }
  441. i32 Value::to_i32(GlobalObject& global_object) const
  442. {
  443. auto number = to_number(global_object);
  444. if (global_object.vm().exception())
  445. return INVALID;
  446. if (number.is_nan() || number.is_infinity())
  447. return 0;
  448. return number.as_i32();
  449. }
  450. u32 Value::to_u32(GlobalObject& global_object) const
  451. {
  452. // 7.1.7 ToUint32, https://tc39.es/ecma262/#sec-touint32
  453. auto number = to_number(global_object);
  454. if (global_object.vm().exception())
  455. return INVALID;
  456. if (number.is_nan() || number.is_infinity())
  457. return 0;
  458. if (number.as_double() <= 0)
  459. return 0;
  460. return number.as_u32();
  461. }
  462. size_t Value::to_size_t(GlobalObject& global_object) const
  463. {
  464. // FIXME: Replace uses of this function with to_length/to_index for correct behaviour and remove this eventually.
  465. if (is_empty())
  466. return 0;
  467. auto number = to_number(global_object);
  468. if (global_object.vm().exception())
  469. return INVALID;
  470. if (number.is_nan())
  471. return 0;
  472. if (number.as_double() <= 0)
  473. return 0;
  474. return number.as_size_t();
  475. }
  476. size_t Value::to_length(GlobalObject& global_object) const
  477. {
  478. // 7.1.20 ToLength, https://tc39.es/ecma262/#sec-tolength
  479. auto& vm = global_object.vm();
  480. auto len = to_integer_or_infinity(global_object);
  481. if (vm.exception())
  482. return INVALID;
  483. if (len <= 0)
  484. return 0;
  485. return min(len, MAX_ARRAY_LIKE_INDEX);
  486. }
  487. size_t Value::to_index(GlobalObject& global_object) const
  488. {
  489. // 7.1.22 ToIndex, https://tc39.es/ecma262/#sec-toindex
  490. auto& vm = global_object.vm();
  491. if (is_undefined())
  492. return 0;
  493. auto integer_index = to_integer_or_infinity(global_object);
  494. if (vm.exception())
  495. return INVALID;
  496. if (integer_index < 0) {
  497. vm.throw_exception<RangeError>(global_object, ErrorType::InvalidIndex);
  498. return INVALID;
  499. }
  500. auto index = Value(integer_index).to_length(global_object);
  501. ASSERT(!vm.exception());
  502. if (integer_index != index) {
  503. vm.throw_exception<RangeError>(global_object, ErrorType::InvalidIndex);
  504. return INVALID;
  505. }
  506. return index;
  507. }
  508. double Value::to_integer_or_infinity(GlobalObject& global_object) const
  509. {
  510. // 7.1.5 ToIntegerOrInfinity, https://tc39.es/ecma262/#sec-tointegerorinfinity
  511. auto& vm = global_object.vm();
  512. auto number = to_number(global_object);
  513. if (vm.exception())
  514. return INVALID;
  515. if (number.is_nan() || number.as_double() == 0)
  516. return 0;
  517. if (number.is_infinity())
  518. return number.as_double();
  519. auto integer = floor(abs(number.as_double()));
  520. if (number.as_double() < 0)
  521. integer = -integer;
  522. return integer;
  523. }
  524. Value greater_than(GlobalObject& global_object, Value lhs, Value rhs)
  525. {
  526. TriState relation = abstract_relation(global_object, false, lhs, rhs);
  527. if (relation == TriState::Unknown)
  528. return Value(false);
  529. return Value(relation == TriState::True);
  530. }
  531. Value greater_than_equals(GlobalObject& global_object, Value lhs, Value rhs)
  532. {
  533. TriState relation = abstract_relation(global_object, true, lhs, rhs);
  534. if (relation == TriState::Unknown || relation == TriState::True)
  535. return Value(false);
  536. return Value(true);
  537. }
  538. Value less_than(GlobalObject& global_object, Value lhs, Value rhs)
  539. {
  540. TriState relation = abstract_relation(global_object, true, lhs, rhs);
  541. if (relation == TriState::Unknown)
  542. return Value(false);
  543. return Value(relation == TriState::True);
  544. }
  545. Value less_than_equals(GlobalObject& global_object, Value lhs, Value rhs)
  546. {
  547. TriState relation = abstract_relation(global_object, false, lhs, rhs);
  548. if (relation == TriState::Unknown || relation == TriState::True)
  549. return Value(false);
  550. return Value(true);
  551. }
  552. Value bitwise_and(GlobalObject& global_object, Value lhs, Value rhs)
  553. {
  554. auto lhs_numeric = lhs.to_numeric(global_object.global_object());
  555. if (global_object.vm().exception())
  556. return {};
  557. auto rhs_numeric = rhs.to_numeric(global_object.global_object());
  558. if (global_object.vm().exception())
  559. return {};
  560. if (both_number(lhs_numeric, rhs_numeric)) {
  561. if (!lhs_numeric.is_finite_number() || !rhs_numeric.is_finite_number())
  562. return Value(0);
  563. return Value((i32)lhs_numeric.as_double() & (i32)rhs_numeric.as_double());
  564. }
  565. if (both_bigint(lhs_numeric, rhs_numeric))
  566. return js_bigint(global_object.heap(), lhs_numeric.as_bigint().big_integer().bitwise_and(rhs_numeric.as_bigint().big_integer()));
  567. global_object.vm().throw_exception<TypeError>(global_object.global_object(), ErrorType::BigIntBadOperatorOtherType, "bitwise AND");
  568. return {};
  569. }
  570. Value bitwise_or(GlobalObject& global_object, Value lhs, Value rhs)
  571. {
  572. auto lhs_numeric = lhs.to_numeric(global_object.global_object());
  573. if (global_object.vm().exception())
  574. return {};
  575. auto rhs_numeric = rhs.to_numeric(global_object.global_object());
  576. if (global_object.vm().exception())
  577. return {};
  578. if (both_number(lhs_numeric, rhs_numeric)) {
  579. if (!lhs_numeric.is_finite_number() && !rhs_numeric.is_finite_number())
  580. return Value(0);
  581. if (!lhs_numeric.is_finite_number())
  582. return rhs_numeric;
  583. if (!rhs_numeric.is_finite_number())
  584. return lhs_numeric;
  585. return Value((i32)lhs_numeric.as_double() | (i32)rhs_numeric.as_double());
  586. }
  587. if (both_bigint(lhs_numeric, rhs_numeric))
  588. return js_bigint(global_object.heap(), lhs_numeric.as_bigint().big_integer().bitwise_or(rhs_numeric.as_bigint().big_integer()));
  589. global_object.vm().throw_exception<TypeError>(global_object.global_object(), ErrorType::BigIntBadOperatorOtherType, "bitwise OR");
  590. return {};
  591. }
  592. Value bitwise_xor(GlobalObject& global_object, Value lhs, Value rhs)
  593. {
  594. auto lhs_numeric = lhs.to_numeric(global_object.global_object());
  595. if (global_object.vm().exception())
  596. return {};
  597. auto rhs_numeric = rhs.to_numeric(global_object.global_object());
  598. if (global_object.vm().exception())
  599. return {};
  600. if (both_number(lhs_numeric, rhs_numeric)) {
  601. if (!lhs_numeric.is_finite_number() && !rhs_numeric.is_finite_number())
  602. return Value(0);
  603. if (!lhs_numeric.is_finite_number())
  604. return rhs_numeric;
  605. if (!rhs_numeric.is_finite_number())
  606. return lhs_numeric;
  607. return Value((i32)lhs_numeric.as_double() ^ (i32)rhs_numeric.as_double());
  608. }
  609. if (both_bigint(lhs_numeric, rhs_numeric))
  610. return js_bigint(global_object.heap(), lhs_numeric.as_bigint().big_integer().bitwise_xor(rhs_numeric.as_bigint().big_integer()));
  611. global_object.vm().throw_exception<TypeError>(global_object.global_object(), ErrorType::BigIntBadOperatorOtherType, "bitwise XOR");
  612. return {};
  613. }
  614. Value bitwise_not(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. return Value(~lhs_numeric.to_i32(global_object));
  621. auto big_integer_bitwise_not = lhs_numeric.as_bigint().big_integer();
  622. big_integer_bitwise_not = big_integer_bitwise_not.plus(Crypto::SignedBigInteger { 1 });
  623. big_integer_bitwise_not.negate();
  624. return js_bigint(global_object.heap(), big_integer_bitwise_not);
  625. }
  626. Value unary_plus(GlobalObject& global_object, Value lhs)
  627. {
  628. return lhs.to_number(global_object.global_object());
  629. }
  630. Value unary_minus(GlobalObject& global_object, Value lhs)
  631. {
  632. auto lhs_numeric = lhs.to_numeric(global_object.global_object());
  633. if (global_object.vm().exception())
  634. return {};
  635. if (lhs_numeric.is_number()) {
  636. if (lhs_numeric.is_nan())
  637. return js_nan();
  638. return Value(-lhs_numeric.as_double());
  639. }
  640. if (lhs_numeric.as_bigint().big_integer() == BIGINT_ZERO)
  641. return js_bigint(global_object.heap(), BIGINT_ZERO);
  642. auto big_integer_negated = lhs_numeric.as_bigint().big_integer();
  643. big_integer_negated.negate();
  644. return js_bigint(global_object.heap(), big_integer_negated);
  645. }
  646. Value left_shift(GlobalObject& global_object, Value lhs, Value rhs)
  647. {
  648. auto lhs_numeric = lhs.to_numeric(global_object.global_object());
  649. if (global_object.vm().exception())
  650. return {};
  651. auto rhs_numeric = rhs.to_numeric(global_object.global_object());
  652. if (global_object.vm().exception())
  653. return {};
  654. if (both_number(lhs_numeric, rhs_numeric)) {
  655. if (!lhs_numeric.is_finite_number())
  656. return Value(0);
  657. if (!rhs_numeric.is_finite_number())
  658. return lhs_numeric;
  659. return Value((i32)lhs_numeric.as_double() << (i32)rhs_numeric.as_double());
  660. }
  661. if (both_bigint(lhs_numeric, rhs_numeric))
  662. TODO();
  663. global_object.vm().throw_exception<TypeError>(global_object.global_object(), ErrorType::BigIntBadOperatorOtherType, "left-shift");
  664. return {};
  665. }
  666. Value right_shift(GlobalObject& global_object, Value lhs, Value rhs)
  667. {
  668. auto lhs_numeric = lhs.to_numeric(global_object.global_object());
  669. if (global_object.vm().exception())
  670. return {};
  671. auto rhs_numeric = rhs.to_numeric(global_object.global_object());
  672. if (global_object.vm().exception())
  673. return {};
  674. if (both_number(lhs_numeric, rhs_numeric)) {
  675. if (!lhs_numeric.is_finite_number())
  676. return Value(0);
  677. if (!rhs_numeric.is_finite_number())
  678. return lhs_numeric;
  679. return Value((i32)lhs_numeric.as_double() >> (i32)rhs_numeric.as_double());
  680. }
  681. if (both_bigint(lhs_numeric, rhs_numeric))
  682. TODO();
  683. global_object.vm().throw_exception<TypeError>(global_object.global_object(), ErrorType::BigIntBadOperatorOtherType, "right-shift");
  684. return {};
  685. }
  686. Value unsigned_right_shift(GlobalObject& global_object, Value lhs, Value rhs)
  687. {
  688. auto lhs_numeric = lhs.to_numeric(global_object.global_object());
  689. if (global_object.vm().exception())
  690. return {};
  691. auto rhs_numeric = rhs.to_numeric(global_object.global_object());
  692. if (global_object.vm().exception())
  693. return {};
  694. if (both_number(lhs_numeric, rhs_numeric)) {
  695. if (!lhs_numeric.is_finite_number())
  696. return Value(0);
  697. if (!rhs_numeric.is_finite_number())
  698. return lhs_numeric;
  699. return Value((unsigned)lhs_numeric.as_double() >> (i32)rhs_numeric.as_double());
  700. }
  701. global_object.vm().throw_exception<TypeError>(global_object.global_object(), ErrorType::BigIntBadOperator, "unsigned right-shift");
  702. return {};
  703. }
  704. Value add(GlobalObject& global_object, Value lhs, Value rhs)
  705. {
  706. auto lhs_primitive = lhs.to_primitive();
  707. if (global_object.vm().exception())
  708. return {};
  709. auto rhs_primitive = rhs.to_primitive();
  710. if (global_object.vm().exception())
  711. return {};
  712. if (lhs_primitive.is_string() || rhs_primitive.is_string()) {
  713. auto lhs_string = lhs_primitive.to_string(global_object.global_object());
  714. if (global_object.vm().exception())
  715. return {};
  716. auto rhs_string = rhs_primitive.to_string(global_object.global_object());
  717. if (global_object.vm().exception())
  718. return {};
  719. StringBuilder builder(lhs_string.length() + rhs_string.length());
  720. builder.append(lhs_string);
  721. builder.append(rhs_string);
  722. return js_string(global_object.heap(), builder.to_string());
  723. }
  724. auto lhs_numeric = lhs_primitive.to_numeric(global_object.global_object());
  725. if (global_object.vm().exception())
  726. return {};
  727. auto rhs_numeric = rhs_primitive.to_numeric(global_object.global_object());
  728. if (global_object.vm().exception())
  729. return {};
  730. if (both_number(lhs_numeric, rhs_numeric))
  731. return Value(lhs_numeric.as_double() + rhs_numeric.as_double());
  732. if (both_bigint(lhs_numeric, rhs_numeric))
  733. return js_bigint(global_object.heap(), lhs_numeric.as_bigint().big_integer().plus(rhs_numeric.as_bigint().big_integer()));
  734. global_object.vm().throw_exception<TypeError>(global_object.global_object(), ErrorType::BigIntBadOperatorOtherType, "addition");
  735. return {};
  736. }
  737. Value sub(GlobalObject& global_object, Value lhs, Value rhs)
  738. {
  739. auto lhs_numeric = lhs.to_numeric(global_object.global_object());
  740. if (global_object.vm().exception())
  741. return {};
  742. auto rhs_numeric = rhs.to_numeric(global_object.global_object());
  743. if (global_object.vm().exception())
  744. return {};
  745. if (both_number(lhs_numeric, rhs_numeric))
  746. return Value(lhs_numeric.as_double() - rhs_numeric.as_double());
  747. if (both_bigint(lhs_numeric, rhs_numeric))
  748. return js_bigint(global_object.heap(), lhs_numeric.as_bigint().big_integer().minus(rhs_numeric.as_bigint().big_integer()));
  749. global_object.vm().throw_exception<TypeError>(global_object.global_object(), ErrorType::BigIntBadOperatorOtherType, "subtraction");
  750. return {};
  751. }
  752. Value mul(GlobalObject& global_object, Value lhs, Value rhs)
  753. {
  754. auto lhs_numeric = lhs.to_numeric(global_object.global_object());
  755. if (global_object.vm().exception())
  756. return {};
  757. auto rhs_numeric = rhs.to_numeric(global_object.global_object());
  758. if (global_object.vm().exception())
  759. return {};
  760. if (both_number(lhs_numeric, rhs_numeric))
  761. return Value(lhs_numeric.as_double() * rhs_numeric.as_double());
  762. if (both_bigint(lhs_numeric, rhs_numeric))
  763. return js_bigint(global_object.heap(), lhs_numeric.as_bigint().big_integer().multiplied_by(rhs_numeric.as_bigint().big_integer()));
  764. global_object.vm().throw_exception<TypeError>(global_object.global_object(), ErrorType::BigIntBadOperatorOtherType, "multiplication");
  765. return {};
  766. }
  767. Value div(GlobalObject& global_object, Value lhs, Value rhs)
  768. {
  769. auto lhs_numeric = lhs.to_numeric(global_object.global_object());
  770. if (global_object.vm().exception())
  771. return {};
  772. auto rhs_numeric = rhs.to_numeric(global_object.global_object());
  773. if (global_object.vm().exception())
  774. return {};
  775. if (both_number(lhs_numeric, rhs_numeric))
  776. return Value(lhs_numeric.as_double() / rhs_numeric.as_double());
  777. if (both_bigint(lhs_numeric, rhs_numeric))
  778. return js_bigint(global_object.heap(), lhs_numeric.as_bigint().big_integer().divided_by(rhs_numeric.as_bigint().big_integer()).quotient);
  779. global_object.vm().throw_exception<TypeError>(global_object.global_object(), ErrorType::BigIntBadOperatorOtherType, "division");
  780. return {};
  781. }
  782. Value mod(GlobalObject& global_object, Value lhs, Value rhs)
  783. {
  784. auto lhs_numeric = lhs.to_numeric(global_object.global_object());
  785. if (global_object.vm().exception())
  786. return {};
  787. auto rhs_numeric = rhs.to_numeric(global_object.global_object());
  788. if (global_object.vm().exception())
  789. return {};
  790. if (both_number(lhs_numeric, rhs_numeric)) {
  791. if (lhs_numeric.is_nan() || rhs_numeric.is_nan())
  792. return js_nan();
  793. auto index = lhs_numeric.as_double();
  794. auto period = rhs_numeric.as_double();
  795. auto trunc = (double)(i32)(index / period);
  796. return Value(index - trunc * period);
  797. }
  798. if (both_bigint(lhs_numeric, rhs_numeric))
  799. return js_bigint(global_object.heap(), lhs_numeric.as_bigint().big_integer().divided_by(rhs_numeric.as_bigint().big_integer()).remainder);
  800. global_object.vm().throw_exception<TypeError>(global_object.global_object(), ErrorType::BigIntBadOperatorOtherType, "modulo");
  801. return {};
  802. }
  803. Value exp(GlobalObject& global_object, Value lhs, Value rhs)
  804. {
  805. auto& vm = global_object.vm();
  806. auto lhs_numeric = lhs.to_numeric(global_object);
  807. if (vm.exception())
  808. return {};
  809. auto rhs_numeric = rhs.to_numeric(global_object);
  810. if (vm.exception())
  811. return {};
  812. if (both_number(lhs_numeric, rhs_numeric))
  813. return Value(pow(lhs_numeric.as_double(), rhs_numeric.as_double()));
  814. if (both_bigint(lhs_numeric, rhs_numeric))
  815. return js_bigint(vm.heap(), Crypto::NumberTheory::Power(lhs_numeric.as_bigint().big_integer(), rhs_numeric.as_bigint().big_integer()));
  816. vm.throw_exception<TypeError>(global_object, ErrorType::BigIntBadOperatorOtherType, "exponentiation");
  817. return {};
  818. }
  819. Value in(GlobalObject& global_object, Value lhs, Value rhs)
  820. {
  821. if (!rhs.is_object()) {
  822. global_object.vm().throw_exception<TypeError>(global_object.global_object(), ErrorType::InOperatorWithObject);
  823. return {};
  824. }
  825. auto lhs_string = lhs.to_string(global_object.global_object());
  826. if (global_object.vm().exception())
  827. return {};
  828. return Value(rhs.as_object().has_property(lhs_string));
  829. }
  830. Value instance_of(GlobalObject& global_object, Value lhs, Value rhs)
  831. {
  832. auto& vm = global_object.vm();
  833. if (!rhs.is_object()) {
  834. vm.throw_exception<TypeError>(global_object, ErrorType::NotAnObject, rhs.to_string_without_side_effects());
  835. return {};
  836. }
  837. auto has_instance_method = rhs.as_object().get(vm.well_known_symbol_has_instance());
  838. if (!has_instance_method.is_empty()) {
  839. if (!has_instance_method.is_function()) {
  840. vm.throw_exception<TypeError>(global_object, ErrorType::NotAFunction, has_instance_method.to_string_without_side_effects());
  841. return {};
  842. }
  843. auto has_instance_result = vm.call(has_instance_method.as_function(), rhs, lhs);
  844. if (vm.exception())
  845. return {};
  846. return Value(has_instance_result.to_boolean());
  847. }
  848. if (!rhs.is_function()) {
  849. vm.throw_exception<TypeError>(global_object, ErrorType::NotAFunction, rhs.to_string_without_side_effects());
  850. return {};
  851. }
  852. return ordinary_has_instance(global_object, lhs, rhs);
  853. }
  854. Value ordinary_has_instance(GlobalObject& global_object, Value lhs, Value rhs)
  855. {
  856. auto& vm = global_object.vm();
  857. if (!rhs.is_function())
  858. return Value(false);
  859. auto& rhs_function = rhs.as_function();
  860. if (is<BoundFunction>(rhs_function)) {
  861. auto& bound_target = static_cast<const BoundFunction&>(rhs_function);
  862. return instance_of(global_object, lhs, Value(&bound_target.target_function()));
  863. }
  864. if (!lhs.is_object())
  865. return Value(false);
  866. Object* lhs_object = &lhs.as_object();
  867. auto rhs_prototype = rhs_function.get(vm.names.prototype);
  868. if (vm.exception())
  869. return {};
  870. if (!rhs_prototype.is_object()) {
  871. vm.throw_exception<TypeError>(global_object, ErrorType::InstanceOfOperatorBadPrototype, rhs.to_string_without_side_effects());
  872. return {};
  873. }
  874. while (true) {
  875. lhs_object = lhs_object->prototype();
  876. if (vm.exception())
  877. return {};
  878. if (!lhs_object)
  879. return Value(false);
  880. if (same_value(rhs_prototype, lhs_object))
  881. return Value(true);
  882. }
  883. }
  884. bool same_value(Value lhs, Value rhs)
  885. {
  886. if (lhs.type() != rhs.type())
  887. return false;
  888. if (lhs.is_number()) {
  889. if (lhs.is_nan() && rhs.is_nan())
  890. return true;
  891. if (lhs.is_positive_zero() && rhs.is_negative_zero())
  892. return false;
  893. if (lhs.is_negative_zero() && rhs.is_positive_zero())
  894. return false;
  895. return lhs.as_double() == rhs.as_double();
  896. }
  897. if (lhs.is_bigint()) {
  898. auto lhs_big_integer = lhs.as_bigint().big_integer();
  899. auto rhs_big_integer = rhs.as_bigint().big_integer();
  900. if (lhs_big_integer == BIGINT_ZERO && rhs_big_integer == BIGINT_ZERO && lhs_big_integer.is_negative() != rhs_big_integer.is_negative())
  901. return false;
  902. return lhs_big_integer == rhs_big_integer;
  903. }
  904. return same_value_non_numeric(lhs, rhs);
  905. }
  906. bool same_value_zero(Value lhs, Value rhs)
  907. {
  908. if (lhs.type() != rhs.type())
  909. return false;
  910. if (lhs.is_number()) {
  911. if (lhs.is_nan() && rhs.is_nan())
  912. return true;
  913. return lhs.as_double() == rhs.as_double();
  914. }
  915. if (lhs.is_bigint())
  916. return lhs.as_bigint().big_integer() == rhs.as_bigint().big_integer();
  917. return same_value_non_numeric(lhs, rhs);
  918. }
  919. bool same_value_non_numeric(Value lhs, Value rhs)
  920. {
  921. ASSERT(!lhs.is_number() && !lhs.is_bigint());
  922. ASSERT(lhs.type() == rhs.type());
  923. switch (lhs.type()) {
  924. case Value::Type::Undefined:
  925. case Value::Type::Null:
  926. return true;
  927. case Value::Type::String:
  928. return lhs.as_string().string() == rhs.as_string().string();
  929. case Value::Type::Symbol:
  930. return &lhs.as_symbol() == &rhs.as_symbol();
  931. case Value::Type::Boolean:
  932. return lhs.as_bool() == rhs.as_bool();
  933. case Value::Type::Object:
  934. return &lhs.as_object() == &rhs.as_object();
  935. default:
  936. ASSERT_NOT_REACHED();
  937. }
  938. }
  939. bool strict_eq(Value lhs, Value rhs)
  940. {
  941. if (lhs.type() != rhs.type())
  942. return false;
  943. if (lhs.is_number()) {
  944. if (lhs.is_nan() || rhs.is_nan())
  945. return false;
  946. if (lhs.as_double() == rhs.as_double())
  947. return true;
  948. return false;
  949. }
  950. if (lhs.is_bigint())
  951. return lhs.as_bigint().big_integer() == rhs.as_bigint().big_integer();
  952. return same_value_non_numeric(lhs, rhs);
  953. }
  954. bool abstract_eq(GlobalObject& global_object, Value lhs, Value rhs)
  955. {
  956. if (lhs.type() == rhs.type())
  957. return strict_eq(lhs, rhs);
  958. if (lhs.is_nullish() && rhs.is_nullish())
  959. return true;
  960. if (lhs.is_number() && rhs.is_string())
  961. return abstract_eq(global_object, lhs, rhs.to_number(global_object.global_object()));
  962. if (lhs.is_string() && rhs.is_number())
  963. return abstract_eq(global_object, lhs.to_number(global_object.global_object()), rhs);
  964. if (lhs.is_bigint() && rhs.is_string()) {
  965. auto& rhs_string = rhs.as_string().string();
  966. if (!is_valid_bigint_value(rhs_string))
  967. return false;
  968. return abstract_eq(global_object, lhs, js_bigint(global_object.heap(), Crypto::SignedBigInteger::from_base10(rhs_string)));
  969. }
  970. if (lhs.is_string() && rhs.is_bigint())
  971. return abstract_eq(global_object, rhs, lhs);
  972. if (lhs.is_boolean())
  973. return abstract_eq(global_object, lhs.to_number(global_object.global_object()), rhs);
  974. if (rhs.is_boolean())
  975. return abstract_eq(global_object, lhs, rhs.to_number(global_object.global_object()));
  976. if ((lhs.is_string() || lhs.is_number() || lhs.is_bigint() || lhs.is_symbol()) && rhs.is_object())
  977. return abstract_eq(global_object, lhs, rhs.to_primitive());
  978. if (lhs.is_object() && (rhs.is_string() || rhs.is_number() || lhs.is_bigint() || rhs.is_symbol()))
  979. return abstract_eq(global_object, lhs.to_primitive(), rhs);
  980. if ((lhs.is_bigint() && rhs.is_number()) || (lhs.is_number() && rhs.is_bigint())) {
  981. if (lhs.is_nan() || lhs.is_infinity() || rhs.is_nan() || rhs.is_infinity())
  982. return false;
  983. if ((lhs.is_number() && !lhs.is_integer()) || (rhs.is_number() && !rhs.is_integer()))
  984. return false;
  985. if (lhs.is_number())
  986. return Crypto::SignedBigInteger { lhs.as_i32() } == rhs.as_bigint().big_integer();
  987. else
  988. return Crypto::SignedBigInteger { rhs.as_i32() } == lhs.as_bigint().big_integer();
  989. }
  990. return false;
  991. }
  992. TriState abstract_relation(GlobalObject& global_object, bool left_first, Value lhs, Value rhs)
  993. {
  994. Value x_primitive;
  995. Value y_primitive;
  996. if (left_first) {
  997. x_primitive = lhs.to_primitive(Value::PreferredType::Number);
  998. if (global_object.vm().exception())
  999. return {};
  1000. y_primitive = rhs.to_primitive(Value::PreferredType::Number);
  1001. if (global_object.vm().exception())
  1002. return {};
  1003. } else {
  1004. y_primitive = lhs.to_primitive(Value::PreferredType::Number);
  1005. if (global_object.vm().exception())
  1006. return {};
  1007. x_primitive = rhs.to_primitive(Value::PreferredType::Number);
  1008. if (global_object.vm().exception())
  1009. return {};
  1010. }
  1011. if (x_primitive.is_string() && y_primitive.is_string()) {
  1012. auto x_string = x_primitive.as_string().string();
  1013. auto y_string = y_primitive.as_string().string();
  1014. if (x_string.starts_with(y_string))
  1015. return TriState::False;
  1016. if (y_string.starts_with(x_string))
  1017. return TriState::True;
  1018. Utf8View x_code_points { x_string };
  1019. Utf8View y_code_points { y_string };
  1020. for (auto k = x_code_points.begin(), l = y_code_points.begin();
  1021. k != x_code_points.end() && l != y_code_points.end();
  1022. ++k, ++l) {
  1023. if (*k != *l) {
  1024. if (*k < *l) {
  1025. return TriState::True;
  1026. } else {
  1027. return TriState::False;
  1028. }
  1029. }
  1030. }
  1031. ASSERT_NOT_REACHED();
  1032. }
  1033. if (x_primitive.is_bigint() && y_primitive.is_string()) {
  1034. auto& y_string = y_primitive.as_string().string();
  1035. if (!is_valid_bigint_value(y_string))
  1036. return TriState::Unknown;
  1037. if (x_primitive.as_bigint().big_integer() < Crypto::SignedBigInteger::from_base10(y_string))
  1038. return TriState::True;
  1039. else
  1040. return TriState::False;
  1041. }
  1042. if (x_primitive.is_string() && y_primitive.is_bigint()) {
  1043. auto& x_string = x_primitive.as_string().string();
  1044. if (!is_valid_bigint_value(x_string))
  1045. return TriState::Unknown;
  1046. if (Crypto::SignedBigInteger::from_base10(x_string) < y_primitive.as_bigint().big_integer())
  1047. return TriState::True;
  1048. else
  1049. return TriState::False;
  1050. }
  1051. auto x_numeric = x_primitive.to_numeric(global_object.global_object());
  1052. if (global_object.vm().exception())
  1053. return {};
  1054. auto y_numeric = y_primitive.to_numeric(global_object.global_object());
  1055. if (global_object.vm().exception())
  1056. return {};
  1057. if (x_numeric.is_nan() || y_numeric.is_nan())
  1058. return TriState::Unknown;
  1059. if (x_numeric.is_positive_infinity() || y_numeric.is_negative_infinity())
  1060. return TriState::False;
  1061. if (x_numeric.is_negative_infinity() || y_numeric.is_positive_infinity())
  1062. return TriState::True;
  1063. if (x_numeric.is_number() && y_numeric.is_number()) {
  1064. if (x_numeric.as_double() < y_numeric.as_double())
  1065. return TriState::True;
  1066. else
  1067. return TriState::False;
  1068. }
  1069. if (x_numeric.is_bigint() && y_numeric.is_bigint()) {
  1070. if (x_numeric.as_bigint().big_integer() < y_numeric.as_bigint().big_integer())
  1071. return TriState::True;
  1072. else
  1073. return TriState::False;
  1074. }
  1075. ASSERT((x_numeric.is_number() && y_numeric.is_bigint()) || (x_numeric.is_bigint() && y_numeric.is_number()));
  1076. bool x_lower_than_y;
  1077. if (x_numeric.is_number()) {
  1078. x_lower_than_y = x_numeric.is_integer()
  1079. ? Crypto::SignedBigInteger { x_numeric.as_i32() } < y_numeric.as_bigint().big_integer()
  1080. : (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());
  1081. } else {
  1082. x_lower_than_y = y_numeric.is_integer()
  1083. ? x_numeric.as_bigint().big_integer() < Crypto::SignedBigInteger { y_numeric.as_i32() }
  1084. : (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 });
  1085. }
  1086. if (x_lower_than_y)
  1087. return TriState::True;
  1088. else
  1089. return TriState::False;
  1090. }
  1091. size_t length_of_array_like(GlobalObject& global_object, Value value)
  1092. {
  1093. ASSERT(value.is_object());
  1094. auto& vm = global_object.vm();
  1095. auto result = value.as_object().get(vm.names.length);
  1096. if (vm.exception())
  1097. return 0;
  1098. return result.to_size_t(global_object);
  1099. }
  1100. }