Value.cpp 48 KB

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