Value.cpp 44 KB

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