Value.cpp 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342
  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. 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. size_t Value::as_size_t() const
  470. {
  471. VERIFY(as_double() >= 0);
  472. return min((double)as_i32(), MAX_ARRAY_LIKE_INDEX);
  473. }
  474. double Value::to_double(GlobalObject& global_object) const
  475. {
  476. auto number = to_number(global_object);
  477. if (global_object.vm().exception())
  478. return INVALID;
  479. return number.as_double();
  480. }
  481. i32 Value::to_i32_slow_case(GlobalObject& global_object) const
  482. {
  483. VERIFY(type() != Type::Int32);
  484. auto number = to_number(global_object);
  485. if (global_object.vm().exception())
  486. return INVALID;
  487. double value = number.as_double();
  488. if (!isfinite(value) || value == 0)
  489. return 0;
  490. auto abs = fabs(value);
  491. auto int_val = floor(abs);
  492. if (signbit(value))
  493. int_val = -int_val;
  494. auto int32bit = fmod(int_val, 4294967296.0);
  495. if (int32bit >= 2147483648.0)
  496. int32bit -= 4294967296.0;
  497. return static_cast<i32>(int32bit);
  498. }
  499. u32 Value::to_u32(GlobalObject& global_object) const
  500. {
  501. // 7.1.7 ToUint32, https://tc39.es/ecma262/#sec-touint32
  502. auto number = to_number(global_object);
  503. if (global_object.vm().exception())
  504. return INVALID;
  505. double value = number.as_double();
  506. if (!isfinite(value) || value == 0)
  507. return 0;
  508. auto int_val = floor(fabs(value));
  509. if (signbit(value))
  510. int_val = -int_val;
  511. auto int32bit = fmod(int_val, NumericLimits<u32>::max() + 1.0);
  512. return static_cast<u32>(int32bit);
  513. }
  514. size_t Value::to_length(GlobalObject& global_object) const
  515. {
  516. // 7.1.20 ToLength, https://tc39.es/ecma262/#sec-tolength
  517. auto& vm = global_object.vm();
  518. auto len = to_integer_or_infinity(global_object);
  519. if (vm.exception())
  520. return INVALID;
  521. if (len <= 0)
  522. return 0;
  523. return min(len, MAX_ARRAY_LIKE_INDEX);
  524. }
  525. size_t Value::to_index(GlobalObject& global_object) const
  526. {
  527. // 7.1.22 ToIndex, https://tc39.es/ecma262/#sec-toindex
  528. auto& vm = global_object.vm();
  529. if (is_undefined())
  530. return 0;
  531. auto integer_index = to_integer_or_infinity(global_object);
  532. if (vm.exception())
  533. return INVALID;
  534. if (integer_index < 0) {
  535. vm.throw_exception<RangeError>(global_object, ErrorType::InvalidIndex);
  536. return INVALID;
  537. }
  538. auto index = Value(integer_index).to_length(global_object);
  539. VERIFY(!vm.exception());
  540. if (integer_index != index) {
  541. vm.throw_exception<RangeError>(global_object, ErrorType::InvalidIndex);
  542. return INVALID;
  543. }
  544. return index;
  545. }
  546. double Value::to_integer_or_infinity(GlobalObject& global_object) const
  547. {
  548. // 7.1.5 ToIntegerOrInfinity, https://tc39.es/ecma262/#sec-tointegerorinfinity
  549. auto& vm = global_object.vm();
  550. auto number = to_number(global_object);
  551. if (vm.exception())
  552. return INVALID;
  553. if (number.is_nan() || number.as_double() == 0)
  554. return 0;
  555. if (number.is_infinity())
  556. return number.as_double();
  557. auto integer = floor(fabs(number.as_double()));
  558. if (number.as_double() < 0)
  559. integer = -integer;
  560. return integer;
  561. }
  562. Value greater_than(GlobalObject& global_object, Value lhs, Value rhs)
  563. {
  564. TriState relation = abstract_relation(global_object, false, lhs, rhs);
  565. if (relation == TriState::Unknown)
  566. return Value(false);
  567. return Value(relation == TriState::True);
  568. }
  569. Value greater_than_equals(GlobalObject& global_object, Value lhs, Value rhs)
  570. {
  571. TriState relation = abstract_relation(global_object, true, lhs, rhs);
  572. if (relation == TriState::Unknown || relation == TriState::True)
  573. return Value(false);
  574. return Value(true);
  575. }
  576. Value less_than(GlobalObject& global_object, Value lhs, Value rhs)
  577. {
  578. TriState relation = abstract_relation(global_object, true, lhs, rhs);
  579. if (relation == TriState::Unknown)
  580. return Value(false);
  581. return Value(relation == TriState::True);
  582. }
  583. Value less_than_equals(GlobalObject& global_object, Value lhs, Value rhs)
  584. {
  585. TriState relation = abstract_relation(global_object, false, lhs, rhs);
  586. if (relation == TriState::Unknown || relation == TriState::True)
  587. return Value(false);
  588. return Value(true);
  589. }
  590. Value bitwise_and(GlobalObject& global_object, Value lhs, Value rhs)
  591. {
  592. auto lhs_numeric = lhs.to_numeric(global_object);
  593. if (global_object.vm().exception())
  594. return {};
  595. auto rhs_numeric = rhs.to_numeric(global_object);
  596. if (global_object.vm().exception())
  597. return {};
  598. if (both_number(lhs_numeric, rhs_numeric)) {
  599. if (!lhs_numeric.is_finite_number() || !rhs_numeric.is_finite_number())
  600. return Value(0);
  601. return Value(lhs_numeric.to_i32(global_object) & rhs_numeric.to_i32(global_object));
  602. }
  603. if (both_bigint(lhs_numeric, rhs_numeric))
  604. return js_bigint(global_object.heap(), lhs_numeric.as_bigint().big_integer().bitwise_and(rhs_numeric.as_bigint().big_integer()));
  605. global_object.vm().throw_exception<TypeError>(global_object, ErrorType::BigIntBadOperatorOtherType, "bitwise AND");
  606. return {};
  607. }
  608. Value bitwise_or(GlobalObject& global_object, Value lhs, Value rhs)
  609. {
  610. auto lhs_numeric = lhs.to_numeric(global_object);
  611. if (global_object.vm().exception())
  612. return {};
  613. auto rhs_numeric = rhs.to_numeric(global_object);
  614. if (global_object.vm().exception())
  615. return {};
  616. if (both_number(lhs_numeric, rhs_numeric)) {
  617. if (!lhs_numeric.is_finite_number() && !rhs_numeric.is_finite_number())
  618. return Value(0);
  619. if (!lhs_numeric.is_finite_number())
  620. return rhs_numeric;
  621. if (!rhs_numeric.is_finite_number())
  622. return lhs_numeric;
  623. return Value(lhs_numeric.to_i32(global_object) | rhs_numeric.to_i32(global_object));
  624. }
  625. if (both_bigint(lhs_numeric, rhs_numeric))
  626. return js_bigint(global_object.heap(), lhs_numeric.as_bigint().big_integer().bitwise_or(rhs_numeric.as_bigint().big_integer()));
  627. global_object.vm().throw_exception<TypeError>(global_object, ErrorType::BigIntBadOperatorOtherType, "bitwise OR");
  628. return {};
  629. }
  630. Value bitwise_xor(GlobalObject& global_object, Value lhs, Value rhs)
  631. {
  632. auto lhs_numeric = lhs.to_numeric(global_object);
  633. if (global_object.vm().exception())
  634. return {};
  635. auto rhs_numeric = rhs.to_numeric(global_object);
  636. if (global_object.vm().exception())
  637. return {};
  638. if (both_number(lhs_numeric, rhs_numeric)) {
  639. if (!lhs_numeric.is_finite_number() && !rhs_numeric.is_finite_number())
  640. return Value(0);
  641. if (!lhs_numeric.is_finite_number())
  642. return rhs_numeric;
  643. if (!rhs_numeric.is_finite_number())
  644. return lhs_numeric;
  645. return Value(lhs_numeric.to_i32(global_object) ^ rhs_numeric.to_i32(global_object));
  646. }
  647. if (both_bigint(lhs_numeric, rhs_numeric))
  648. return js_bigint(global_object.heap(), lhs_numeric.as_bigint().big_integer().bitwise_xor(rhs_numeric.as_bigint().big_integer()));
  649. global_object.vm().throw_exception<TypeError>(global_object, ErrorType::BigIntBadOperatorOtherType, "bitwise XOR");
  650. return {};
  651. }
  652. Value bitwise_not(GlobalObject& global_object, Value lhs)
  653. {
  654. auto lhs_numeric = lhs.to_numeric(global_object);
  655. if (global_object.vm().exception())
  656. return {};
  657. if (lhs_numeric.is_number())
  658. return Value(~lhs_numeric.to_i32(global_object));
  659. auto big_integer_bitwise_not = lhs_numeric.as_bigint().big_integer();
  660. big_integer_bitwise_not = big_integer_bitwise_not.plus(Crypto::SignedBigInteger { 1 });
  661. big_integer_bitwise_not.negate();
  662. return js_bigint(global_object.heap(), big_integer_bitwise_not);
  663. }
  664. Value unary_plus(GlobalObject& global_object, Value lhs)
  665. {
  666. return lhs.to_number(global_object);
  667. }
  668. Value unary_minus(GlobalObject& global_object, Value lhs)
  669. {
  670. auto lhs_numeric = lhs.to_numeric(global_object);
  671. if (global_object.vm().exception())
  672. return {};
  673. if (lhs_numeric.is_number()) {
  674. if (lhs_numeric.is_nan())
  675. return js_nan();
  676. return Value(-lhs_numeric.as_double());
  677. }
  678. if (lhs_numeric.as_bigint().big_integer() == BIGINT_ZERO)
  679. return js_bigint(global_object.heap(), BIGINT_ZERO);
  680. auto big_integer_negated = lhs_numeric.as_bigint().big_integer();
  681. big_integer_negated.negate();
  682. return js_bigint(global_object.heap(), big_integer_negated);
  683. }
  684. Value left_shift(GlobalObject& global_object, Value lhs, Value rhs)
  685. {
  686. // 6.1.6.1.9 Number::leftShift
  687. // https://tc39.es/ecma262/#sec-numeric-types-number-leftShift
  688. auto lhs_numeric = lhs.to_numeric(global_object);
  689. if (global_object.vm().exception())
  690. return {};
  691. auto rhs_numeric = rhs.to_numeric(global_object);
  692. if (global_object.vm().exception())
  693. return {};
  694. if (both_number(lhs_numeric, rhs_numeric)) {
  695. if (!lhs_numeric.is_finite_number())
  696. return Value(0);
  697. if (!rhs_numeric.is_finite_number())
  698. return lhs_numeric;
  699. // Ok, so this performs toNumber() again but that "can't" throw
  700. auto lhs_i32 = lhs_numeric.to_i32(global_object);
  701. auto rhs_u32 = rhs_numeric.to_u32(global_object);
  702. return Value(lhs_i32 << rhs_u32);
  703. }
  704. if (both_bigint(lhs_numeric, rhs_numeric))
  705. TODO();
  706. global_object.vm().throw_exception<TypeError>(global_object, ErrorType::BigIntBadOperatorOtherType, "left-shift");
  707. return {};
  708. }
  709. Value right_shift(GlobalObject& global_object, Value lhs, Value rhs)
  710. {
  711. // 6.1.6.1.11 Number::signedRightShift
  712. // https://tc39.es/ecma262/#sec-numeric-types-number-signedRightShift
  713. auto lhs_numeric = lhs.to_numeric(global_object);
  714. if (global_object.vm().exception())
  715. return {};
  716. auto rhs_numeric = rhs.to_numeric(global_object);
  717. if (global_object.vm().exception())
  718. return {};
  719. if (both_number(lhs_numeric, rhs_numeric)) {
  720. if (!lhs_numeric.is_finite_number())
  721. return Value(0);
  722. if (!rhs_numeric.is_finite_number())
  723. return lhs_numeric;
  724. // Ok, so this performs toNumber() again but that "can't" throw
  725. auto lhs_i32 = lhs_numeric.to_i32(global_object);
  726. auto rhs_u32 = rhs_numeric.to_u32(global_object);
  727. return Value(lhs_i32 >> rhs_u32);
  728. }
  729. if (both_bigint(lhs_numeric, rhs_numeric))
  730. TODO();
  731. global_object.vm().throw_exception<TypeError>(global_object, ErrorType::BigIntBadOperatorOtherType, "right-shift");
  732. return {};
  733. }
  734. Value unsigned_right_shift(GlobalObject& global_object, Value lhs, Value rhs)
  735. {
  736. // 6.1.6.1.11 Number::unsignedRightShift
  737. // https://tc39.es/ecma262/#sec-numeric-types-number-unsignedRightShift
  738. auto lhs_numeric = lhs.to_numeric(global_object);
  739. if (global_object.vm().exception())
  740. return {};
  741. auto rhs_numeric = rhs.to_numeric(global_object);
  742. if (global_object.vm().exception())
  743. return {};
  744. if (both_number(lhs_numeric, rhs_numeric)) {
  745. if (!lhs_numeric.is_finite_number())
  746. return Value(0);
  747. if (!rhs_numeric.is_finite_number())
  748. return lhs_numeric;
  749. // Ok, so this performs toNumber() again but that "can't" throw
  750. auto lhs_u32 = lhs_numeric.to_u32(global_object);
  751. auto rhs_u32 = rhs_numeric.to_u32(global_object) % 32;
  752. return Value(lhs_u32 >> rhs_u32);
  753. }
  754. global_object.vm().throw_exception<TypeError>(global_object, ErrorType::BigIntBadOperator, "unsigned right-shift");
  755. return {};
  756. }
  757. Value add(GlobalObject& global_object, Value lhs, Value rhs)
  758. {
  759. if (both_number(lhs, rhs)) {
  760. if (lhs.type() == Value::Type::Int32 && rhs.type() == Value::Type::Int32) {
  761. Checked<i32> result;
  762. result = lhs.to_i32(global_object);
  763. result += rhs.to_i32(global_object);
  764. if (!result.has_overflow())
  765. return Value(result.value());
  766. }
  767. return Value(lhs.as_double() + rhs.as_double());
  768. }
  769. auto& vm = global_object.vm();
  770. auto lhs_primitive = lhs.to_primitive(global_object);
  771. if (vm.exception())
  772. return {};
  773. auto rhs_primitive = rhs.to_primitive(global_object);
  774. if (vm.exception())
  775. return {};
  776. if (lhs_primitive.is_string() || rhs_primitive.is_string()) {
  777. auto lhs_string = lhs_primitive.to_string(global_object);
  778. if (vm.exception())
  779. return {};
  780. auto rhs_string = rhs_primitive.to_string(global_object);
  781. if (vm.exception())
  782. return {};
  783. StringBuilder builder(lhs_string.length() + rhs_string.length());
  784. builder.append(lhs_string);
  785. builder.append(rhs_string);
  786. return js_string(vm.heap(), builder.to_string());
  787. }
  788. auto lhs_numeric = lhs_primitive.to_numeric(global_object);
  789. if (vm.exception())
  790. return {};
  791. auto rhs_numeric = rhs_primitive.to_numeric(global_object);
  792. if (vm.exception())
  793. return {};
  794. if (both_number(lhs_numeric, rhs_numeric))
  795. return Value(lhs_numeric.as_double() + rhs_numeric.as_double());
  796. if (both_bigint(lhs_numeric, rhs_numeric))
  797. return js_bigint(vm.heap(), lhs_numeric.as_bigint().big_integer().plus(rhs_numeric.as_bigint().big_integer()));
  798. vm.throw_exception<TypeError>(global_object, ErrorType::BigIntBadOperatorOtherType, "addition");
  799. return {};
  800. }
  801. Value sub(GlobalObject& global_object, Value lhs, Value rhs)
  802. {
  803. auto lhs_numeric = lhs.to_numeric(global_object);
  804. if (global_object.vm().exception())
  805. return {};
  806. auto rhs_numeric = rhs.to_numeric(global_object);
  807. if (global_object.vm().exception())
  808. return {};
  809. if (both_number(lhs_numeric, rhs_numeric))
  810. return Value(lhs_numeric.as_double() - rhs_numeric.as_double());
  811. if (both_bigint(lhs_numeric, rhs_numeric))
  812. return js_bigint(global_object.heap(), lhs_numeric.as_bigint().big_integer().minus(rhs_numeric.as_bigint().big_integer()));
  813. global_object.vm().throw_exception<TypeError>(global_object, ErrorType::BigIntBadOperatorOtherType, "subtraction");
  814. return {};
  815. }
  816. Value mul(GlobalObject& global_object, Value lhs, Value rhs)
  817. {
  818. auto lhs_numeric = lhs.to_numeric(global_object);
  819. if (global_object.vm().exception())
  820. return {};
  821. auto rhs_numeric = rhs.to_numeric(global_object);
  822. if (global_object.vm().exception())
  823. return {};
  824. if (both_number(lhs_numeric, rhs_numeric))
  825. return Value(lhs_numeric.as_double() * rhs_numeric.as_double());
  826. if (both_bigint(lhs_numeric, rhs_numeric))
  827. return js_bigint(global_object.heap(), lhs_numeric.as_bigint().big_integer().multiplied_by(rhs_numeric.as_bigint().big_integer()));
  828. global_object.vm().throw_exception<TypeError>(global_object, ErrorType::BigIntBadOperatorOtherType, "multiplication");
  829. return {};
  830. }
  831. Value div(GlobalObject& global_object, Value lhs, Value rhs)
  832. {
  833. auto& vm = global_object.vm();
  834. auto lhs_numeric = lhs.to_numeric(global_object);
  835. if (vm.exception())
  836. return {};
  837. auto rhs_numeric = rhs.to_numeric(global_object);
  838. if (vm.exception())
  839. return {};
  840. if (both_number(lhs_numeric, rhs_numeric))
  841. return Value(lhs_numeric.as_double() / rhs_numeric.as_double());
  842. if (both_bigint(lhs_numeric, rhs_numeric)) {
  843. if (rhs_numeric.as_bigint().big_integer() == BIGINT_ZERO) {
  844. vm.throw_exception<RangeError>(global_object, ErrorType::DivisionByZero);
  845. return {};
  846. }
  847. return js_bigint(global_object.heap(), lhs_numeric.as_bigint().big_integer().divided_by(rhs_numeric.as_bigint().big_integer()).quotient);
  848. }
  849. vm.throw_exception<TypeError>(global_object, ErrorType::BigIntBadOperatorOtherType, "division");
  850. return {};
  851. }
  852. Value mod(GlobalObject& global_object, Value lhs, Value rhs)
  853. {
  854. auto& vm = global_object.vm();
  855. auto lhs_numeric = lhs.to_numeric(global_object);
  856. if (vm.exception())
  857. return {};
  858. auto rhs_numeric = rhs.to_numeric(global_object);
  859. if (vm.exception())
  860. return {};
  861. if (both_number(lhs_numeric, rhs_numeric)) {
  862. if (lhs_numeric.is_nan() || rhs_numeric.is_nan())
  863. return js_nan();
  864. auto index = lhs_numeric.as_double();
  865. auto period = rhs_numeric.as_double();
  866. auto trunc = (double)(i32)(index / period);
  867. return Value(index - trunc * period);
  868. }
  869. if (both_bigint(lhs_numeric, rhs_numeric)) {
  870. if (rhs_numeric.as_bigint().big_integer() == BIGINT_ZERO) {
  871. vm.throw_exception<RangeError>(global_object, ErrorType::DivisionByZero);
  872. return {};
  873. }
  874. return js_bigint(global_object.heap(), lhs_numeric.as_bigint().big_integer().divided_by(rhs_numeric.as_bigint().big_integer()).remainder);
  875. }
  876. vm.throw_exception<TypeError>(global_object, ErrorType::BigIntBadOperatorOtherType, "modulo");
  877. return {};
  878. }
  879. Value exp(GlobalObject& global_object, Value lhs, Value rhs)
  880. {
  881. auto& vm = global_object.vm();
  882. auto lhs_numeric = lhs.to_numeric(global_object);
  883. if (vm.exception())
  884. return {};
  885. auto rhs_numeric = rhs.to_numeric(global_object);
  886. if (vm.exception())
  887. return {};
  888. if (both_number(lhs_numeric, rhs_numeric))
  889. return Value(pow(lhs_numeric.as_double(), rhs_numeric.as_double()));
  890. if (both_bigint(lhs_numeric, rhs_numeric)) {
  891. if (rhs_numeric.as_bigint().big_integer().is_negative()) {
  892. vm.throw_exception<RangeError>(global_object, ErrorType::NegativeExponent);
  893. return {};
  894. }
  895. return js_bigint(vm.heap(), Crypto::NumberTheory::Power(lhs_numeric.as_bigint().big_integer(), rhs_numeric.as_bigint().big_integer()));
  896. }
  897. vm.throw_exception<TypeError>(global_object, ErrorType::BigIntBadOperatorOtherType, "exponentiation");
  898. return {};
  899. }
  900. Value in(GlobalObject& global_object, Value lhs, Value rhs)
  901. {
  902. if (!rhs.is_object()) {
  903. global_object.vm().throw_exception<TypeError>(global_object, ErrorType::InOperatorWithObject);
  904. return {};
  905. }
  906. auto lhs_string = lhs.to_string(global_object);
  907. if (global_object.vm().exception())
  908. return {};
  909. return Value(rhs.as_object().has_property(lhs_string));
  910. }
  911. Value instance_of(GlobalObject& global_object, Value lhs, Value rhs)
  912. {
  913. auto& vm = global_object.vm();
  914. if (!rhs.is_object()) {
  915. vm.throw_exception<TypeError>(global_object, ErrorType::NotAnObject, rhs.to_string_without_side_effects());
  916. return {};
  917. }
  918. auto has_instance_method = get_method(global_object, Value(&rhs.as_object()), vm.well_known_symbol_has_instance());
  919. if (vm.exception())
  920. return {};
  921. if (has_instance_method) {
  922. auto has_instance_result = vm.call(*has_instance_method, rhs, lhs);
  923. if (vm.exception())
  924. return {};
  925. return Value(has_instance_result.to_boolean());
  926. }
  927. if (!rhs.is_function()) {
  928. vm.throw_exception<TypeError>(global_object, ErrorType::NotAFunction, rhs.to_string_without_side_effects());
  929. return {};
  930. }
  931. return ordinary_has_instance(global_object, lhs, rhs);
  932. }
  933. Value ordinary_has_instance(GlobalObject& global_object, Value lhs, Value rhs)
  934. {
  935. auto& vm = global_object.vm();
  936. if (!rhs.is_function())
  937. return Value(false);
  938. auto& rhs_function = rhs.as_function();
  939. if (is<BoundFunction>(rhs_function)) {
  940. auto& bound_target = static_cast<const BoundFunction&>(rhs_function);
  941. return instance_of(global_object, lhs, Value(&bound_target.target_function()));
  942. }
  943. if (!lhs.is_object())
  944. return Value(false);
  945. Object* lhs_object = &lhs.as_object();
  946. auto rhs_prototype = rhs_function.get(vm.names.prototype);
  947. if (vm.exception())
  948. return {};
  949. if (!rhs_prototype.is_object()) {
  950. vm.throw_exception<TypeError>(global_object, ErrorType::InstanceOfOperatorBadPrototype, rhs.to_string_without_side_effects());
  951. return {};
  952. }
  953. while (true) {
  954. lhs_object = lhs_object->prototype();
  955. if (vm.exception())
  956. return {};
  957. if (!lhs_object)
  958. return Value(false);
  959. if (same_value(rhs_prototype, lhs_object))
  960. return Value(true);
  961. }
  962. }
  963. bool same_value(Value lhs, Value rhs)
  964. {
  965. if (!same_type_for_equality(lhs, rhs))
  966. return false;
  967. if (lhs.is_number()) {
  968. if (lhs.is_nan() && rhs.is_nan())
  969. return true;
  970. if (lhs.is_positive_zero() && rhs.is_negative_zero())
  971. return false;
  972. if (lhs.is_negative_zero() && rhs.is_positive_zero())
  973. return false;
  974. return lhs.as_double() == rhs.as_double();
  975. }
  976. if (lhs.is_bigint()) {
  977. auto lhs_big_integer = lhs.as_bigint().big_integer();
  978. auto rhs_big_integer = rhs.as_bigint().big_integer();
  979. if (lhs_big_integer == BIGINT_ZERO && rhs_big_integer == BIGINT_ZERO && lhs_big_integer.is_negative() != rhs_big_integer.is_negative())
  980. return false;
  981. return lhs_big_integer == rhs_big_integer;
  982. }
  983. return same_value_non_numeric(lhs, rhs);
  984. }
  985. bool same_value_zero(Value lhs, Value rhs)
  986. {
  987. if (!same_type_for_equality(lhs, rhs))
  988. return false;
  989. if (lhs.is_number()) {
  990. if (lhs.is_nan() && rhs.is_nan())
  991. return true;
  992. return lhs.as_double() == rhs.as_double();
  993. }
  994. if (lhs.is_bigint())
  995. return lhs.as_bigint().big_integer() == rhs.as_bigint().big_integer();
  996. return same_value_non_numeric(lhs, rhs);
  997. }
  998. bool same_value_non_numeric(Value lhs, Value rhs)
  999. {
  1000. VERIFY(!lhs.is_number() && !lhs.is_bigint());
  1001. VERIFY(same_type_for_equality(lhs, rhs));
  1002. switch (lhs.type()) {
  1003. case Value::Type::Undefined:
  1004. case Value::Type::Null:
  1005. return true;
  1006. case Value::Type::String:
  1007. return lhs.as_string().string() == rhs.as_string().string();
  1008. case Value::Type::Symbol:
  1009. return &lhs.as_symbol() == &rhs.as_symbol();
  1010. case Value::Type::Boolean:
  1011. return lhs.as_bool() == rhs.as_bool();
  1012. case Value::Type::Object:
  1013. return &lhs.as_object() == &rhs.as_object();
  1014. default:
  1015. VERIFY_NOT_REACHED();
  1016. }
  1017. }
  1018. bool strict_eq(Value lhs, Value rhs)
  1019. {
  1020. if (!same_type_for_equality(lhs, rhs))
  1021. return false;
  1022. if (lhs.is_number()) {
  1023. if (lhs.is_nan() || rhs.is_nan())
  1024. return false;
  1025. if (lhs.as_double() == rhs.as_double())
  1026. return true;
  1027. return false;
  1028. }
  1029. if (lhs.is_bigint())
  1030. return lhs.as_bigint().big_integer() == rhs.as_bigint().big_integer();
  1031. return same_value_non_numeric(lhs, rhs);
  1032. }
  1033. bool abstract_eq(GlobalObject& global_object, Value lhs, Value rhs)
  1034. {
  1035. if (same_type_for_equality(lhs, rhs))
  1036. return strict_eq(lhs, rhs);
  1037. if (lhs.is_nullish() && rhs.is_nullish())
  1038. return true;
  1039. if (lhs.is_number() && rhs.is_string())
  1040. return abstract_eq(global_object, lhs, rhs.to_number(global_object));
  1041. if (lhs.is_string() && rhs.is_number())
  1042. return abstract_eq(global_object, lhs.to_number(global_object), rhs);
  1043. if (lhs.is_bigint() && rhs.is_string()) {
  1044. auto& rhs_string = rhs.as_string().string();
  1045. if (!is_valid_bigint_value(rhs_string))
  1046. return false;
  1047. return abstract_eq(global_object, lhs, js_bigint(global_object.heap(), Crypto::SignedBigInteger::from_base10(rhs_string)));
  1048. }
  1049. if (lhs.is_string() && rhs.is_bigint())
  1050. return abstract_eq(global_object, rhs, lhs);
  1051. if (lhs.is_boolean())
  1052. return abstract_eq(global_object, lhs.to_number(global_object), rhs);
  1053. if (rhs.is_boolean())
  1054. return abstract_eq(global_object, lhs, rhs.to_number(global_object));
  1055. if ((lhs.is_string() || lhs.is_number() || lhs.is_bigint() || lhs.is_symbol()) && rhs.is_object()) {
  1056. auto rhs_primitive = rhs.to_primitive(global_object);
  1057. if (global_object.vm().exception())
  1058. return false;
  1059. return abstract_eq(global_object, lhs, rhs_primitive);
  1060. }
  1061. if (lhs.is_object() && (rhs.is_string() || rhs.is_number() || lhs.is_bigint() || rhs.is_symbol())) {
  1062. auto lhs_primitive = lhs.to_primitive(global_object);
  1063. if (global_object.vm().exception())
  1064. return false;
  1065. return abstract_eq(global_object, lhs_primitive, rhs);
  1066. }
  1067. if ((lhs.is_bigint() && rhs.is_number()) || (lhs.is_number() && rhs.is_bigint())) {
  1068. if (lhs.is_nan() || lhs.is_infinity() || rhs.is_nan() || rhs.is_infinity())
  1069. return false;
  1070. if ((lhs.is_number() && !lhs.is_integer()) || (rhs.is_number() && !rhs.is_integer()))
  1071. return false;
  1072. if (lhs.is_number())
  1073. return Crypto::SignedBigInteger { lhs.to_i32(global_object) } == rhs.as_bigint().big_integer();
  1074. else
  1075. return Crypto::SignedBigInteger { rhs.to_i32(global_object) } == lhs.as_bigint().big_integer();
  1076. }
  1077. return false;
  1078. }
  1079. TriState abstract_relation(GlobalObject& global_object, bool left_first, Value lhs, Value rhs)
  1080. {
  1081. Value x_primitive;
  1082. Value y_primitive;
  1083. if (left_first) {
  1084. x_primitive = lhs.to_primitive(global_object, Value::PreferredType::Number);
  1085. if (global_object.vm().exception())
  1086. return {};
  1087. y_primitive = rhs.to_primitive(global_object, Value::PreferredType::Number);
  1088. if (global_object.vm().exception())
  1089. return {};
  1090. } else {
  1091. y_primitive = lhs.to_primitive(global_object, Value::PreferredType::Number);
  1092. if (global_object.vm().exception())
  1093. return {};
  1094. x_primitive = rhs.to_primitive(global_object, Value::PreferredType::Number);
  1095. if (global_object.vm().exception())
  1096. return {};
  1097. }
  1098. if (x_primitive.is_string() && y_primitive.is_string()) {
  1099. auto x_string = x_primitive.as_string().string();
  1100. auto y_string = y_primitive.as_string().string();
  1101. if (x_string.starts_with(y_string))
  1102. return TriState::False;
  1103. if (y_string.starts_with(x_string))
  1104. return TriState::True;
  1105. Utf8View x_code_points { x_string };
  1106. Utf8View y_code_points { y_string };
  1107. for (auto k = x_code_points.begin(), l = y_code_points.begin();
  1108. k != x_code_points.end() && l != y_code_points.end();
  1109. ++k, ++l) {
  1110. if (*k != *l) {
  1111. if (*k < *l) {
  1112. return TriState::True;
  1113. } else {
  1114. return TriState::False;
  1115. }
  1116. }
  1117. }
  1118. VERIFY_NOT_REACHED();
  1119. }
  1120. if (x_primitive.is_bigint() && y_primitive.is_string()) {
  1121. auto& y_string = y_primitive.as_string().string();
  1122. if (!is_valid_bigint_value(y_string))
  1123. return TriState::Unknown;
  1124. if (x_primitive.as_bigint().big_integer() < Crypto::SignedBigInteger::from_base10(y_string))
  1125. return TriState::True;
  1126. else
  1127. return TriState::False;
  1128. }
  1129. if (x_primitive.is_string() && y_primitive.is_bigint()) {
  1130. auto& x_string = x_primitive.as_string().string();
  1131. if (!is_valid_bigint_value(x_string))
  1132. return TriState::Unknown;
  1133. if (Crypto::SignedBigInteger::from_base10(x_string) < y_primitive.as_bigint().big_integer())
  1134. return TriState::True;
  1135. else
  1136. return TriState::False;
  1137. }
  1138. auto x_numeric = x_primitive.to_numeric(global_object);
  1139. if (global_object.vm().exception())
  1140. return {};
  1141. auto y_numeric = y_primitive.to_numeric(global_object);
  1142. if (global_object.vm().exception())
  1143. return {};
  1144. if (x_numeric.is_nan() || y_numeric.is_nan())
  1145. return TriState::Unknown;
  1146. if (x_numeric.is_positive_infinity() || y_numeric.is_negative_infinity())
  1147. return TriState::False;
  1148. if (x_numeric.is_negative_infinity() || y_numeric.is_positive_infinity())
  1149. return TriState::True;
  1150. if (x_numeric.is_number() && y_numeric.is_number()) {
  1151. if (x_numeric.as_double() < y_numeric.as_double())
  1152. return TriState::True;
  1153. else
  1154. return TriState::False;
  1155. }
  1156. if (x_numeric.is_bigint() && y_numeric.is_bigint()) {
  1157. if (x_numeric.as_bigint().big_integer() < y_numeric.as_bigint().big_integer())
  1158. return TriState::True;
  1159. else
  1160. return TriState::False;
  1161. }
  1162. VERIFY((x_numeric.is_number() && y_numeric.is_bigint()) || (x_numeric.is_bigint() && y_numeric.is_number()));
  1163. bool x_lower_than_y;
  1164. if (x_numeric.is_number()) {
  1165. x_lower_than_y = x_numeric.is_integer()
  1166. ? Crypto::SignedBigInteger { x_numeric.to_i32(global_object) } < y_numeric.as_bigint().big_integer()
  1167. : (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());
  1168. } else {
  1169. x_lower_than_y = y_numeric.is_integer()
  1170. ? x_numeric.as_bigint().big_integer() < Crypto::SignedBigInteger { y_numeric.to_i32(global_object) }
  1171. : (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 });
  1172. }
  1173. if (x_lower_than_y)
  1174. return TriState::True;
  1175. else
  1176. return TriState::False;
  1177. }
  1178. Function* get_method(GlobalObject& global_object, Value value, const PropertyName& property_name)
  1179. {
  1180. // 7.3.10 GetMethod, https://tc39.es/ecma262/#sec-getmethod
  1181. auto& vm = global_object.vm();
  1182. auto* object = value.to_object(global_object);
  1183. if (vm.exception())
  1184. return nullptr;
  1185. auto property_value = object->get(property_name);
  1186. if (vm.exception())
  1187. return nullptr;
  1188. if (property_value.is_empty() || property_value.is_nullish())
  1189. return nullptr;
  1190. if (!property_value.is_function()) {
  1191. vm.throw_exception<TypeError>(global_object, ErrorType::NotAFunction, property_value.to_string_without_side_effects());
  1192. return nullptr;
  1193. }
  1194. return &property_value.as_function();
  1195. }
  1196. size_t length_of_array_like(GlobalObject& global_object, const Object& object)
  1197. {
  1198. // 7.3.18 LengthOfArrayLike, https://tc39.es/ecma262/#sec-lengthofarraylike
  1199. auto& vm = global_object.vm();
  1200. auto result = object.get(vm.names.length).value_or(js_undefined());
  1201. if (vm.exception())
  1202. return INVALID;
  1203. return result.to_length(global_object);
  1204. }
  1205. }