Value.cpp 50 KB

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