Value.cpp 46 KB

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