Value.cpp 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2020-2022, Linus Groh <linusg@serenityos.org>
  4. * Copyright (c) 2022, David Tuin <davidot@serenityos.org>
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #include <AK/AllOf.h>
  9. #include <AK/Assertions.h>
  10. #include <AK/CharacterTypes.h>
  11. #include <AK/String.h>
  12. #include <AK/StringBuilder.h>
  13. #include <AK/Utf8View.h>
  14. #include <LibCrypto/BigInt/SignedBigInteger.h>
  15. #include <LibCrypto/NumberTheory/ModularFunctions.h>
  16. #include <LibJS/Runtime/AbstractOperations.h>
  17. #include <LibJS/Runtime/Accessor.h>
  18. #include <LibJS/Runtime/Array.h>
  19. #include <LibJS/Runtime/BigInt.h>
  20. #include <LibJS/Runtime/BigIntObject.h>
  21. #include <LibJS/Runtime/BooleanObject.h>
  22. #include <LibJS/Runtime/BoundFunction.h>
  23. #include <LibJS/Runtime/Completion.h>
  24. #include <LibJS/Runtime/Error.h>
  25. #include <LibJS/Runtime/FunctionObject.h>
  26. #include <LibJS/Runtime/GlobalObject.h>
  27. #include <LibJS/Runtime/NativeFunction.h>
  28. #include <LibJS/Runtime/NumberObject.h>
  29. #include <LibJS/Runtime/Object.h>
  30. #include <LibJS/Runtime/PrimitiveString.h>
  31. #include <LibJS/Runtime/ProxyObject.h>
  32. #include <LibJS/Runtime/RegExpObject.h>
  33. #include <LibJS/Runtime/StringObject.h>
  34. #include <LibJS/Runtime/StringPrototype.h>
  35. #include <LibJS/Runtime/SymbolObject.h>
  36. #include <LibJS/Runtime/VM.h>
  37. #include <LibJS/Runtime/Value.h>
  38. #include <math.h>
  39. namespace JS {
  40. static inline bool same_type_for_equality(Value const& lhs, Value const& rhs)
  41. {
  42. // If the top two bytes are identical then either:
  43. // both are NaN boxed Values with the same type
  44. // or they are doubles which happen to have the same top bytes.
  45. if ((lhs.encoded() & TAG_EXTRACTION) == (rhs.encoded() & TAG_EXTRACTION))
  46. return true;
  47. if (lhs.is_number() && rhs.is_number())
  48. return true;
  49. // One of the Values is not a number and they do not have the same tag
  50. return false;
  51. }
  52. static const Crypto::SignedBigInteger BIGINT_ZERO { 0 };
  53. ALWAYS_INLINE bool both_number(Value const& lhs, Value const& rhs)
  54. {
  55. return lhs.is_number() && rhs.is_number();
  56. }
  57. ALWAYS_INLINE bool both_bigint(Value const& lhs, Value const& rhs)
  58. {
  59. return lhs.is_bigint() && rhs.is_bigint();
  60. }
  61. // 6.1.6.1.20 Number::toString ( x ), https://tc39.es/ecma262/#sec-numeric-types-number-tostring
  62. static String double_to_string(double d)
  63. {
  64. if (isnan(d))
  65. return "NaN";
  66. if (d == +0.0 || d == -0.0)
  67. return "0";
  68. if (d < +0.0) {
  69. StringBuilder builder;
  70. builder.append('-');
  71. builder.append(double_to_string(-d));
  72. return builder.to_string();
  73. }
  74. if (d == static_cast<double>(INFINITY))
  75. return "Infinity";
  76. StringBuilder number_string_builder;
  77. size_t start_index = 0;
  78. size_t end_index = 0;
  79. size_t int_part_end = 0;
  80. // generate integer part (reversed)
  81. double int_part;
  82. double frac_part;
  83. frac_part = modf(d, &int_part);
  84. while (int_part > 0) {
  85. number_string_builder.append('0' + (int)fmod(int_part, 10));
  86. end_index++;
  87. int_part = floor(int_part / 10);
  88. }
  89. auto reversed_integer_part = number_string_builder.to_string().reverse();
  90. number_string_builder.clear();
  91. number_string_builder.append(reversed_integer_part);
  92. int_part_end = end_index;
  93. int exponent = 0;
  94. // generate fractional part
  95. while (frac_part > 0) {
  96. double old_frac_part = frac_part;
  97. frac_part *= 10;
  98. frac_part = modf(frac_part, &int_part);
  99. if (old_frac_part == frac_part)
  100. break;
  101. number_string_builder.append('0' + (int)int_part);
  102. end_index++;
  103. exponent--;
  104. }
  105. auto number_string = number_string_builder.to_string();
  106. // FIXME: Remove this hack.
  107. // FIXME: Instead find the shortest round-trippable representation.
  108. // Remove decimals after the 15th position
  109. if (end_index > int_part_end + 15) {
  110. exponent += end_index - int_part_end - 15;
  111. end_index = int_part_end + 15;
  112. }
  113. // remove leading zeroes
  114. while (start_index < end_index && number_string[start_index] == '0') {
  115. start_index++;
  116. }
  117. // remove trailing zeroes
  118. while (end_index > 0 && number_string[end_index - 1] == '0') {
  119. end_index--;
  120. exponent++;
  121. }
  122. if (end_index <= start_index)
  123. return "0";
  124. auto digits = number_string.substring_view(start_index, end_index - start_index);
  125. int number_of_digits = end_index - start_index;
  126. exponent += number_of_digits;
  127. StringBuilder builder;
  128. if (number_of_digits <= exponent && exponent <= 21) {
  129. builder.append(digits);
  130. builder.append(String::repeated('0', exponent - number_of_digits));
  131. return builder.to_string();
  132. }
  133. if (0 < exponent && exponent <= 21) {
  134. builder.append(digits.substring_view(0, exponent));
  135. builder.append('.');
  136. builder.append(digits.substring_view(exponent));
  137. return builder.to_string();
  138. }
  139. if (-6 < exponent && exponent <= 0) {
  140. builder.append("0."sv);
  141. builder.append(String::repeated('0', -exponent));
  142. builder.append(digits);
  143. return builder.to_string();
  144. }
  145. if (number_of_digits == 1) {
  146. builder.append(digits);
  147. builder.append('e');
  148. if (exponent - 1 > 0)
  149. builder.append('+');
  150. else
  151. builder.append('-');
  152. builder.append(String::number(AK::abs(exponent - 1)));
  153. return builder.to_string();
  154. }
  155. builder.append(digits[0]);
  156. builder.append('.');
  157. builder.append(digits.substring_view(1));
  158. builder.append('e');
  159. if (exponent - 1 > 0)
  160. builder.append('+');
  161. else
  162. builder.append('-');
  163. builder.append(String::number(AK::abs(exponent - 1)));
  164. return builder.to_string();
  165. }
  166. // 7.2.2 IsArray ( argument ), https://tc39.es/ecma262/#sec-isarray
  167. ThrowCompletionOr<bool> Value::is_array(VM& vm) const
  168. {
  169. if (!is_object())
  170. return false;
  171. auto& object = as_object();
  172. if (is<Array>(object))
  173. return true;
  174. if (is<ProxyObject>(object)) {
  175. auto& proxy = static_cast<ProxyObject const&>(object);
  176. if (proxy.is_revoked())
  177. return vm.throw_completion<TypeError>(ErrorType::ProxyRevoked);
  178. return Value(&proxy.target()).is_array(vm);
  179. }
  180. return false;
  181. }
  182. Array& Value::as_array()
  183. {
  184. VERIFY(is_object() && is<Array>(as_object()));
  185. return static_cast<Array&>(as_object());
  186. }
  187. // 7.2.3 IsCallable ( argument ), https://tc39.es/ecma262/#sec-iscallable
  188. bool Value::is_function() const
  189. {
  190. return is_object() && as_object().is_function();
  191. }
  192. FunctionObject& Value::as_function()
  193. {
  194. VERIFY(is_function());
  195. return static_cast<FunctionObject&>(as_object());
  196. }
  197. FunctionObject const& Value::as_function() const
  198. {
  199. VERIFY(is_function());
  200. return static_cast<FunctionObject const&>(as_object());
  201. }
  202. // 7.2.4 IsConstructor ( argument ), https://tc39.es/ecma262/#sec-isconstructor
  203. bool Value::is_constructor() const
  204. {
  205. // 1. If Type(argument) is not Object, return false.
  206. if (!is_function())
  207. return false;
  208. // 2. If argument has a [[Construct]] internal method, return true.
  209. if (as_function().has_constructor())
  210. return true;
  211. // 3. Return false.
  212. return false;
  213. }
  214. // 7.2.8 IsRegExp ( argument ), https://tc39.es/ecma262/#sec-isregexp
  215. ThrowCompletionOr<bool> Value::is_regexp(VM& vm) const
  216. {
  217. if (!is_object())
  218. return false;
  219. auto matcher = TRY(as_object().get(*vm.well_known_symbol_match()));
  220. if (!matcher.is_undefined())
  221. return matcher.to_boolean();
  222. return is<RegExpObject>(as_object());
  223. }
  224. // 13.5.3 The typeof Operator, https://tc39.es/ecma262/#sec-typeof-operator
  225. String Value::typeof() const
  226. {
  227. if (is_number())
  228. return "number";
  229. switch (m_value.tag) {
  230. case UNDEFINED_TAG:
  231. return "undefined";
  232. case NULL_TAG:
  233. return "object";
  234. case STRING_TAG:
  235. return "string";
  236. case OBJECT_TAG:
  237. // B.3.7.3 Changes to the typeof Operator, https://tc39.es/ecma262/#sec-IsHTMLDDA-internal-slot-typeof
  238. if (as_object().is_htmldda())
  239. return "undefined";
  240. if (is_function())
  241. return "function";
  242. return "object";
  243. case BOOLEAN_TAG:
  244. return "boolean";
  245. case SYMBOL_TAG:
  246. return "symbol";
  247. case BIGINT_TAG:
  248. return "bigint";
  249. default:
  250. VERIFY_NOT_REACHED();
  251. }
  252. }
  253. String Value::to_string_without_side_effects() const
  254. {
  255. if (is_double())
  256. return double_to_string(m_value.as_double);
  257. switch (m_value.tag) {
  258. case UNDEFINED_TAG:
  259. return "undefined";
  260. case NULL_TAG:
  261. return "null";
  262. case BOOLEAN_TAG:
  263. return as_bool() ? "true" : "false";
  264. case INT32_TAG:
  265. return String::number(as_i32());
  266. case STRING_TAG:
  267. return as_string().string();
  268. case SYMBOL_TAG:
  269. return as_symbol().to_string();
  270. case BIGINT_TAG:
  271. return as_bigint().to_string();
  272. case OBJECT_TAG:
  273. return String::formatted("[object {}]", as_object().class_name());
  274. case ACCESSOR_TAG:
  275. return "<accessor>";
  276. default:
  277. VERIFY_NOT_REACHED();
  278. }
  279. }
  280. ThrowCompletionOr<PrimitiveString*> Value::to_primitive_string(VM& vm)
  281. {
  282. if (is_string())
  283. return &as_string();
  284. auto string = TRY(to_string(vm));
  285. return js_string(vm, string);
  286. }
  287. // 7.1.17 ToString ( argument ), https://tc39.es/ecma262/#sec-tostring
  288. ThrowCompletionOr<String> Value::to_string(VM& vm) const
  289. {
  290. if (is_double())
  291. return double_to_string(m_value.as_double);
  292. switch (m_value.tag) {
  293. case UNDEFINED_TAG:
  294. return "undefined"sv;
  295. case NULL_TAG:
  296. return "null"sv;
  297. case BOOLEAN_TAG:
  298. return as_bool() ? "true"sv : "false"sv;
  299. case INT32_TAG:
  300. return String::number(as_i32());
  301. case STRING_TAG:
  302. return as_string().string();
  303. case SYMBOL_TAG:
  304. return vm.throw_completion<TypeError>(ErrorType::Convert, "symbol", "string");
  305. case BIGINT_TAG:
  306. return as_bigint().big_integer().to_base(10);
  307. case OBJECT_TAG: {
  308. auto primitive_value = TRY(to_primitive(vm, PreferredType::String));
  309. return primitive_value.to_string(vm);
  310. }
  311. default:
  312. VERIFY_NOT_REACHED();
  313. }
  314. }
  315. ThrowCompletionOr<Utf16String> Value::to_utf16_string(VM& vm) const
  316. {
  317. if (is_string())
  318. return as_string().utf16_string();
  319. auto utf8_string = TRY(to_string(vm));
  320. return Utf16String(utf8_string);
  321. }
  322. // 7.1.2 ToBoolean ( argument ), https://tc39.es/ecma262/#sec-toboolean
  323. bool Value::to_boolean() const
  324. {
  325. if (is_double()) {
  326. if (is_nan())
  327. return false;
  328. return m_value.as_double != 0;
  329. }
  330. switch (m_value.tag) {
  331. case UNDEFINED_TAG:
  332. case NULL_TAG:
  333. return false;
  334. case BOOLEAN_TAG:
  335. return as_bool();
  336. case INT32_TAG:
  337. return as_i32() != 0;
  338. case STRING_TAG:
  339. return !as_string().is_empty();
  340. case SYMBOL_TAG:
  341. return true;
  342. case BIGINT_TAG:
  343. return as_bigint().big_integer() != BIGINT_ZERO;
  344. case OBJECT_TAG:
  345. // B.3.7.1 Changes to ToBoolean, https://tc39.es/ecma262/#sec-IsHTMLDDA-internal-slot-to-boolean
  346. if (as_object().is_htmldda())
  347. return false;
  348. return true;
  349. default:
  350. VERIFY_NOT_REACHED();
  351. }
  352. }
  353. // 7.1.1 ToPrimitive ( input [ , preferredType ] ), https://tc39.es/ecma262/#sec-toprimitive
  354. ThrowCompletionOr<Value> Value::to_primitive(VM& vm, PreferredType preferred_type) const
  355. {
  356. auto get_hint_for_preferred_type = [&]() -> String {
  357. switch (preferred_type) {
  358. case PreferredType::Default:
  359. return "default";
  360. case PreferredType::String:
  361. return "string";
  362. case PreferredType::Number:
  363. return "number";
  364. default:
  365. VERIFY_NOT_REACHED();
  366. }
  367. };
  368. if (is_object()) {
  369. auto to_primitive_method = TRY(get_method(vm, *vm.well_known_symbol_to_primitive()));
  370. if (to_primitive_method) {
  371. auto hint = get_hint_for_preferred_type();
  372. auto result = TRY(call(vm, *to_primitive_method, *this, js_string(vm, hint)));
  373. if (!result.is_object())
  374. return result;
  375. return vm.throw_completion<TypeError>(ErrorType::ToPrimitiveReturnedObject, to_string_without_side_effects(), hint);
  376. }
  377. if (preferred_type == PreferredType::Default)
  378. preferred_type = PreferredType::Number;
  379. return as_object().ordinary_to_primitive(preferred_type);
  380. }
  381. return *this;
  382. }
  383. // 7.1.18 ToObject ( argument ), https://tc39.es/ecma262/#sec-toobject
  384. ThrowCompletionOr<Object*> Value::to_object(VM& vm) const
  385. {
  386. auto& realm = *vm.current_realm();
  387. VERIFY(!is_empty());
  388. if (is_number())
  389. return NumberObject::create(realm, as_double());
  390. switch (m_value.tag) {
  391. case UNDEFINED_TAG:
  392. case NULL_TAG:
  393. return vm.throw_completion<TypeError>(ErrorType::ToObjectNullOrUndefined);
  394. case BOOLEAN_TAG:
  395. return BooleanObject::create(realm, as_bool());
  396. case STRING_TAG:
  397. return StringObject::create(realm, const_cast<JS::PrimitiveString&>(as_string()), *realm.intrinsics().string_prototype());
  398. case SYMBOL_TAG:
  399. return SymbolObject::create(realm, const_cast<JS::Symbol&>(as_symbol()));
  400. case BIGINT_TAG:
  401. return BigIntObject::create(realm, const_cast<JS::BigInt&>(as_bigint()));
  402. case OBJECT_TAG:
  403. return &const_cast<Object&>(as_object());
  404. default:
  405. VERIFY_NOT_REACHED();
  406. }
  407. }
  408. // 7.1.3 ToNumeric ( value ), https://tc39.es/ecma262/#sec-tonumeric
  409. FLATTEN ThrowCompletionOr<Value> Value::to_numeric(VM& vm) const
  410. {
  411. auto primitive = TRY(to_primitive(vm, Value::PreferredType::Number));
  412. if (primitive.is_bigint())
  413. return primitive;
  414. return primitive.to_number(vm);
  415. }
  416. // 7.1.4 ToNumber ( argument ), https://tc39.es/ecma262/#sec-tonumber
  417. ThrowCompletionOr<Value> Value::to_number(VM& vm) const
  418. {
  419. VERIFY(!is_empty());
  420. if (is_number())
  421. return *this;
  422. switch (m_value.tag) {
  423. case UNDEFINED_TAG:
  424. return js_nan();
  425. case NULL_TAG:
  426. return Value(0);
  427. case BOOLEAN_TAG:
  428. return Value(as_bool() ? 1 : 0);
  429. case STRING_TAG: {
  430. String string = Utf8View(as_string().string()).trim(whitespace_characters, AK::TrimMode::Both).as_string();
  431. if (string.is_empty())
  432. return Value(0);
  433. if (string == "Infinity" || string == "+Infinity")
  434. return js_infinity();
  435. if (string == "-Infinity")
  436. return js_negative_infinity();
  437. char* endptr;
  438. auto parsed_double = strtod(string.characters(), &endptr);
  439. if (*endptr)
  440. return js_nan();
  441. // NOTE: Per the spec only exactly [+-]Infinity should result in infinity
  442. // but strtod gives infinity for any case-insensitive 'infinity' or 'inf' string.
  443. if (isinf(parsed_double) && string.contains('i', AK::CaseSensitivity::CaseInsensitive))
  444. return js_nan();
  445. return Value(parsed_double);
  446. }
  447. case SYMBOL_TAG:
  448. return vm.throw_completion<TypeError>(ErrorType::Convert, "symbol", "number");
  449. case BIGINT_TAG:
  450. return vm.throw_completion<TypeError>(ErrorType::Convert, "BigInt", "number");
  451. case OBJECT_TAG: {
  452. auto primitive = TRY(to_primitive(vm, PreferredType::Number));
  453. return primitive.to_number(vm);
  454. }
  455. default:
  456. VERIFY_NOT_REACHED();
  457. }
  458. }
  459. // 7.1.13 ToBigInt ( argument ), https://tc39.es/ecma262/#sec-tobigint
  460. ThrowCompletionOr<BigInt*> Value::to_bigint(VM& vm) const
  461. {
  462. auto primitive = TRY(to_primitive(vm, PreferredType::Number));
  463. VERIFY(!primitive.is_empty());
  464. if (primitive.is_number())
  465. return vm.throw_completion<TypeError>(ErrorType::Convert, "number", "BigInt");
  466. switch (primitive.m_value.tag) {
  467. case UNDEFINED_TAG:
  468. return vm.throw_completion<TypeError>(ErrorType::Convert, "undefined", "BigInt");
  469. case NULL_TAG:
  470. return vm.throw_completion<TypeError>(ErrorType::Convert, "null", "BigInt");
  471. case BOOLEAN_TAG: {
  472. auto value = primitive.as_bool() ? 1 : 0;
  473. return js_bigint(vm, Crypto::SignedBigInteger { value });
  474. }
  475. case BIGINT_TAG:
  476. return &primitive.as_bigint();
  477. case STRING_TAG: {
  478. // 1. Let n be ! StringToBigInt(prim).
  479. auto bigint = primitive.string_to_bigint(vm);
  480. // 2. If n is undefined, throw a SyntaxError exception.
  481. if (!bigint.has_value())
  482. return vm.throw_completion<SyntaxError>(ErrorType::BigIntInvalidValue, primitive);
  483. // 3. Return n.
  484. return bigint.release_value();
  485. }
  486. case SYMBOL_TAG:
  487. return vm.throw_completion<TypeError>(ErrorType::Convert, "symbol", "BigInt");
  488. default:
  489. VERIFY_NOT_REACHED();
  490. }
  491. }
  492. struct BigIntParseResult {
  493. StringView literal;
  494. u8 base { 10 };
  495. bool is_negative { false };
  496. };
  497. static Optional<BigIntParseResult> parse_bigint_text(StringView text)
  498. {
  499. BigIntParseResult result {};
  500. auto parse_for_prefixed_base = [&](auto lower_prefix, auto upper_prefix, auto validator) {
  501. if (text.length() <= 2)
  502. return false;
  503. if (!text.starts_with(lower_prefix) && !text.starts_with(upper_prefix))
  504. return false;
  505. return all_of(text.substring_view(2), validator);
  506. };
  507. if (parse_for_prefixed_base("0b"sv, "0B"sv, is_ascii_binary_digit)) {
  508. result.literal = text.substring_view(2);
  509. result.base = 2;
  510. } else if (parse_for_prefixed_base("0o"sv, "0O"sv, is_ascii_octal_digit)) {
  511. result.literal = text.substring_view(2);
  512. result.base = 8;
  513. } else if (parse_for_prefixed_base("0x"sv, "0X"sv, is_ascii_hex_digit)) {
  514. result.literal = text.substring_view(2);
  515. result.base = 16;
  516. } else {
  517. if (text.starts_with('-')) {
  518. text = text.substring_view(1);
  519. result.is_negative = true;
  520. } else if (text.starts_with('+')) {
  521. text = text.substring_view(1);
  522. }
  523. if (!all_of(text, is_ascii_digit))
  524. return {};
  525. result.literal = text;
  526. result.base = 10;
  527. }
  528. return result;
  529. }
  530. // 7.1.14 StringToBigInt ( str ), https://tc39.es/ecma262/#sec-stringtobigint
  531. Optional<BigInt*> Value::string_to_bigint(VM& vm) const
  532. {
  533. VERIFY(is_string());
  534. // 1. Let text be StringToCodePoints(str).
  535. auto text = as_string().string().view().trim_whitespace();
  536. // 2. Let literal be ParseText(text, StringIntegerLiteral).
  537. auto result = parse_bigint_text(text);
  538. // 3. If literal is a List of errors, return undefined.
  539. if (!result.has_value())
  540. return {};
  541. // 4. Let mv be the MV of literal.
  542. // 5. Assert: mv is an integer.
  543. auto bigint = Crypto::SignedBigInteger::from_base(result->base, result->literal);
  544. if (result->is_negative && (bigint != BIGINT_ZERO))
  545. bigint.negate();
  546. // 6. Return ℤ(mv).
  547. return js_bigint(vm, move(bigint));
  548. }
  549. // 7.1.15 ToBigInt64 ( argument ), https://tc39.es/ecma262/#sec-tobigint64
  550. ThrowCompletionOr<i64> Value::to_bigint_int64(VM& vm) const
  551. {
  552. auto* bigint = TRY(to_bigint(vm));
  553. return static_cast<i64>(bigint->big_integer().to_u64());
  554. }
  555. // 7.1.16 ToBigUint64 ( argument ), https://tc39.es/ecma262/#sec-tobiguint64
  556. ThrowCompletionOr<u64> Value::to_bigint_uint64(VM& vm) const
  557. {
  558. auto* bigint = TRY(to_bigint(vm));
  559. return bigint->big_integer().to_u64();
  560. }
  561. ThrowCompletionOr<double> Value::to_double(VM& vm) const
  562. {
  563. return TRY(to_number(vm)).as_double();
  564. }
  565. // 7.1.19 ToPropertyKey ( argument ), https://tc39.es/ecma262/#sec-topropertykey
  566. ThrowCompletionOr<PropertyKey> Value::to_property_key(VM& vm) const
  567. {
  568. if (is_int32() && as_i32() >= 0)
  569. return PropertyKey { as_i32() };
  570. auto key = TRY(to_primitive(vm, PreferredType::String));
  571. if (key.is_symbol())
  572. return &key.as_symbol();
  573. return TRY(key.to_string(vm));
  574. }
  575. ThrowCompletionOr<i32> Value::to_i32_slow_case(VM& vm) const
  576. {
  577. VERIFY(!is_int32());
  578. double value = TRY(to_number(vm)).as_double();
  579. if (!isfinite(value) || value == 0)
  580. return 0;
  581. auto abs = fabs(value);
  582. auto int_val = floor(abs);
  583. if (signbit(value))
  584. int_val = -int_val;
  585. auto remainder = fmod(int_val, 4294967296.0);
  586. auto int32bit = remainder >= 0.0 ? remainder : remainder + 4294967296.0; // The notation “x modulo y” computes a value k of the same sign as y
  587. if (int32bit >= 2147483648.0)
  588. int32bit -= 4294967296.0;
  589. return static_cast<i32>(int32bit);
  590. }
  591. ThrowCompletionOr<i32> Value::to_i32(VM& vm) const
  592. {
  593. if (is_int32())
  594. return as_i32();
  595. return to_i32_slow_case(vm);
  596. }
  597. // 7.1.7 ToUint32 ( argument ), https://tc39.es/ecma262/#sec-touint32
  598. ThrowCompletionOr<u32> Value::to_u32(VM& vm) const
  599. {
  600. double value = TRY(to_number(vm)).as_double();
  601. if (!isfinite(value) || value == 0)
  602. return 0;
  603. auto int_val = floor(fabs(value));
  604. if (signbit(value))
  605. int_val = -int_val;
  606. auto int32bit = fmod(int_val, NumericLimits<u32>::max() + 1.0);
  607. // Cast to i64 here to ensure that the double --> u32 cast doesn't invoke undefined behavior
  608. // Otherwise, negative numbers cause a UBSAN warning.
  609. return static_cast<u32>(static_cast<i64>(int32bit));
  610. }
  611. // 7.1.8 ToInt16 ( argument ), https://tc39.es/ecma262/#sec-toint16
  612. ThrowCompletionOr<i16> Value::to_i16(VM& vm) const
  613. {
  614. double value = TRY(to_number(vm)).as_double();
  615. if (!isfinite(value) || value == 0)
  616. return 0;
  617. auto abs = fabs(value);
  618. auto int_val = floor(abs);
  619. if (signbit(value))
  620. int_val = -int_val;
  621. auto remainder = fmod(int_val, 65536.0);
  622. auto int16bit = remainder >= 0.0 ? remainder : remainder + 65536.0; // The notation “x modulo y” computes a value k of the same sign as y
  623. if (int16bit >= 32768.0)
  624. int16bit -= 65536.0;
  625. return static_cast<i16>(int16bit);
  626. }
  627. // 7.1.9 ToUint16 ( argument ), https://tc39.es/ecma262/#sec-touint16
  628. ThrowCompletionOr<u16> Value::to_u16(VM& vm) const
  629. {
  630. double value = TRY(to_number(vm)).as_double();
  631. if (!isfinite(value) || value == 0)
  632. return 0;
  633. auto int_val = floor(fabs(value));
  634. if (signbit(value))
  635. int_val = -int_val;
  636. auto int16bit = fmod(int_val, NumericLimits<u16>::max() + 1.0);
  637. if (int16bit < 0)
  638. int16bit += NumericLimits<u16>::max() + 1.0;
  639. return static_cast<u16>(int16bit);
  640. }
  641. // 7.1.10 ToInt8 ( argument ), https://tc39.es/ecma262/#sec-toint8
  642. ThrowCompletionOr<i8> Value::to_i8(VM& vm) const
  643. {
  644. double value = TRY(to_number(vm)).as_double();
  645. if (!isfinite(value) || value == 0)
  646. return 0;
  647. auto abs = fabs(value);
  648. auto int_val = floor(abs);
  649. if (signbit(value))
  650. int_val = -int_val;
  651. auto remainder = fmod(int_val, 256.0);
  652. auto int8bit = remainder >= 0.0 ? remainder : remainder + 256.0; // The notation “x modulo y” computes a value k of the same sign as y
  653. if (int8bit >= 128.0)
  654. int8bit -= 256.0;
  655. return static_cast<i8>(int8bit);
  656. }
  657. // 7.1.11 ToUint8 ( argument ), https://tc39.es/ecma262/#sec-touint8
  658. ThrowCompletionOr<u8> Value::to_u8(VM& vm) const
  659. {
  660. double value = TRY(to_number(vm)).as_double();
  661. if (!isfinite(value) || value == 0)
  662. return 0;
  663. auto int_val = floor(fabs(value));
  664. if (signbit(value))
  665. int_val = -int_val;
  666. auto int8bit = fmod(int_val, NumericLimits<u8>::max() + 1.0);
  667. if (int8bit < 0)
  668. int8bit += NumericLimits<u8>::max() + 1.0;
  669. return static_cast<u8>(int8bit);
  670. }
  671. // 7.1.12 ToUint8Clamp ( argument ), https://tc39.es/ecma262/#sec-touint8clamp
  672. ThrowCompletionOr<u8> Value::to_u8_clamp(VM& vm) const
  673. {
  674. auto number = TRY(to_number(vm));
  675. if (number.is_nan())
  676. return 0;
  677. double value = number.as_double();
  678. if (value <= 0.0)
  679. return 0;
  680. if (value >= 255.0)
  681. return 255;
  682. auto int_val = floor(value);
  683. if (int_val + 0.5 < value)
  684. return static_cast<u8>(int_val + 1.0);
  685. if (value < int_val + 0.5)
  686. return static_cast<u8>(int_val);
  687. if (fmod(int_val, 2.0) == 1.0)
  688. return static_cast<u8>(int_val + 1.0);
  689. return static_cast<u8>(int_val);
  690. }
  691. // 7.1.20 ToLength ( argument ), https://tc39.es/ecma262/#sec-tolength
  692. ThrowCompletionOr<size_t> Value::to_length(VM& vm) const
  693. {
  694. auto len = TRY(to_integer_or_infinity(vm));
  695. if (len <= 0)
  696. return 0;
  697. // FIXME: The spec says that this function's output range is 0 - 2^53-1. But we don't want to overflow the size_t.
  698. constexpr double length_limit = sizeof(void*) == 4 ? NumericLimits<size_t>::max() : MAX_ARRAY_LIKE_INDEX;
  699. return min(len, length_limit);
  700. }
  701. // 7.1.22 ToIndex ( argument ), https://tc39.es/ecma262/#sec-toindex
  702. ThrowCompletionOr<size_t> Value::to_index(VM& vm) const
  703. {
  704. if (is_undefined())
  705. return 0;
  706. auto integer_index = TRY(to_integer_or_infinity(vm));
  707. if (integer_index < 0)
  708. return vm.throw_completion<RangeError>(ErrorType::InvalidIndex);
  709. auto index = MUST(Value(integer_index).to_length(vm));
  710. if (integer_index != index)
  711. return vm.throw_completion<RangeError>(ErrorType::InvalidIndex);
  712. return index;
  713. }
  714. // 7.1.5 ToIntegerOrInfinity ( argument ), https://tc39.es/ecma262/#sec-tointegerorinfinity
  715. ThrowCompletionOr<double> Value::to_integer_or_infinity(VM& vm) const
  716. {
  717. auto number = TRY(to_number(vm));
  718. if (number.is_nan() || number.as_double() == 0)
  719. return 0;
  720. if (number.is_infinity())
  721. return number.as_double();
  722. auto integer = floor(fabs(number.as_double()));
  723. if (number.as_double() < 0 && integer != 0)
  724. integer = -integer;
  725. return integer;
  726. }
  727. // Standalone variant using plain doubles for cases where we already got numbers and know the AO won't throw.
  728. double to_integer_or_infinity(double number)
  729. {
  730. // 1. Let number be ? ToNumber(argument).
  731. // 2. If number is NaN, +0𝔽, or -0𝔽, return 0.
  732. if (isnan(number) || number == 0)
  733. return 0;
  734. // 3. If number is +∞𝔽, return +∞.
  735. if (__builtin_isinf_sign(number) > 0)
  736. return static_cast<double>(INFINITY);
  737. // 4. If number is -∞𝔽, return -∞.
  738. if (__builtin_isinf_sign(number) < 0)
  739. return static_cast<double>(-INFINITY);
  740. // 5. Let integer be floor(abs(ℝ(number))).
  741. auto integer = floor(fabs(number));
  742. // 6. If number < -0𝔽, set integer to -integer.
  743. if (number < 0 && integer != 0)
  744. integer = -integer;
  745. // 7. Return integer.
  746. return integer;
  747. }
  748. // 7.3.3 GetV ( V, P ), https://tc39.es/ecma262/#sec-getv
  749. ThrowCompletionOr<Value> Value::get(VM& vm, PropertyKey const& property_key) const
  750. {
  751. // 1. Assert: IsPropertyKey(P) is true.
  752. VERIFY(property_key.is_valid());
  753. // 2. Let O be ? ToObject(V).
  754. auto* object = TRY(to_object(vm));
  755. // 3. Return ? O.[[Get]](P, V).
  756. return TRY(object->internal_get(property_key, *this));
  757. }
  758. // 7.3.11 GetMethod ( V, P ), https://tc39.es/ecma262/#sec-getmethod
  759. ThrowCompletionOr<FunctionObject*> Value::get_method(VM& vm, PropertyKey const& property_key) const
  760. {
  761. // 1. Assert: IsPropertyKey(P) is true.
  762. VERIFY(property_key.is_valid());
  763. // 2. Let func be ? GetV(V, P).
  764. auto function = TRY(get(vm, property_key));
  765. // 3. If func is either undefined or null, return undefined.
  766. if (function.is_nullish())
  767. return nullptr;
  768. // 4. If IsCallable(func) is false, throw a TypeError exception.
  769. if (!function.is_function())
  770. return vm.throw_completion<TypeError>(ErrorType::NotAFunction, function.to_string_without_side_effects());
  771. // 5. Return func.
  772. return &function.as_function();
  773. }
  774. // 13.10 Relational Operators, https://tc39.es/ecma262/#sec-relational-operators
  775. ThrowCompletionOr<Value> greater_than(VM& vm, Value lhs, Value rhs)
  776. {
  777. if (lhs.is_int32() && rhs.is_int32())
  778. return lhs.as_i32() > rhs.as_i32();
  779. TriState relation = TRY(is_less_than(vm, lhs, rhs, false));
  780. if (relation == TriState::Unknown)
  781. return Value(false);
  782. return Value(relation == TriState::True);
  783. }
  784. // 13.10 Relational Operators, https://tc39.es/ecma262/#sec-relational-operators
  785. ThrowCompletionOr<Value> greater_than_equals(VM& vm, Value lhs, Value rhs)
  786. {
  787. if (lhs.is_int32() && rhs.is_int32())
  788. return lhs.as_i32() >= rhs.as_i32();
  789. TriState relation = TRY(is_less_than(vm, lhs, rhs, true));
  790. if (relation == TriState::Unknown || relation == TriState::True)
  791. return Value(false);
  792. return Value(true);
  793. }
  794. // 13.10 Relational Operators, https://tc39.es/ecma262/#sec-relational-operators
  795. ThrowCompletionOr<Value> less_than(VM& vm, Value lhs, Value rhs)
  796. {
  797. if (lhs.is_int32() && rhs.is_int32())
  798. return lhs.as_i32() < rhs.as_i32();
  799. TriState relation = TRY(is_less_than(vm, lhs, rhs, true));
  800. if (relation == TriState::Unknown)
  801. return Value(false);
  802. return Value(relation == TriState::True);
  803. }
  804. // 13.10 Relational Operators, https://tc39.es/ecma262/#sec-relational-operators
  805. ThrowCompletionOr<Value> less_than_equals(VM& vm, Value lhs, Value rhs)
  806. {
  807. if (lhs.is_int32() && rhs.is_int32())
  808. return lhs.as_i32() <= rhs.as_i32();
  809. TriState relation = TRY(is_less_than(vm, lhs, rhs, false));
  810. if (relation == TriState::Unknown || relation == TriState::True)
  811. return Value(false);
  812. return Value(true);
  813. }
  814. // 13.12 Binary Bitwise Operators, https://tc39.es/ecma262/#sec-binary-bitwise-operators
  815. ThrowCompletionOr<Value> bitwise_and(VM& vm, Value lhs, Value rhs)
  816. {
  817. auto lhs_numeric = TRY(lhs.to_numeric(vm));
  818. auto rhs_numeric = TRY(rhs.to_numeric(vm));
  819. if (both_number(lhs_numeric, rhs_numeric)) {
  820. if (!lhs_numeric.is_finite_number() || !rhs_numeric.is_finite_number())
  821. return Value(0);
  822. return Value(TRY(lhs_numeric.to_i32(vm)) & TRY(rhs_numeric.to_i32(vm)));
  823. }
  824. if (both_bigint(lhs_numeric, rhs_numeric))
  825. return Value(js_bigint(vm, lhs_numeric.as_bigint().big_integer().bitwise_and(rhs_numeric.as_bigint().big_integer())));
  826. return vm.throw_completion<TypeError>(ErrorType::BigIntBadOperatorOtherType, "bitwise AND");
  827. }
  828. // 13.12 Binary Bitwise Operators, https://tc39.es/ecma262/#sec-binary-bitwise-operators
  829. ThrowCompletionOr<Value> bitwise_or(VM& vm, Value lhs, Value rhs)
  830. {
  831. auto lhs_numeric = TRY(lhs.to_numeric(vm));
  832. auto rhs_numeric = TRY(rhs.to_numeric(vm));
  833. if (both_number(lhs_numeric, rhs_numeric)) {
  834. if (!lhs_numeric.is_finite_number() && !rhs_numeric.is_finite_number())
  835. return Value(0);
  836. if (!lhs_numeric.is_finite_number())
  837. return rhs_numeric;
  838. if (!rhs_numeric.is_finite_number())
  839. return lhs_numeric;
  840. return Value(TRY(lhs_numeric.to_i32(vm)) | TRY(rhs_numeric.to_i32(vm)));
  841. }
  842. if (both_bigint(lhs_numeric, rhs_numeric))
  843. return Value(js_bigint(vm, lhs_numeric.as_bigint().big_integer().bitwise_or(rhs_numeric.as_bigint().big_integer())));
  844. return vm.throw_completion<TypeError>(ErrorType::BigIntBadOperatorOtherType, "bitwise OR");
  845. }
  846. // 13.12 Binary Bitwise Operators, https://tc39.es/ecma262/#sec-binary-bitwise-operators
  847. ThrowCompletionOr<Value> bitwise_xor(VM& vm, Value lhs, Value rhs)
  848. {
  849. auto lhs_numeric = TRY(lhs.to_numeric(vm));
  850. auto rhs_numeric = TRY(rhs.to_numeric(vm));
  851. if (both_number(lhs_numeric, rhs_numeric)) {
  852. if (!lhs_numeric.is_finite_number() && !rhs_numeric.is_finite_number())
  853. return Value(0);
  854. if (!lhs_numeric.is_finite_number())
  855. return rhs_numeric;
  856. if (!rhs_numeric.is_finite_number())
  857. return lhs_numeric;
  858. return Value(TRY(lhs_numeric.to_i32(vm)) ^ TRY(rhs_numeric.to_i32(vm)));
  859. }
  860. if (both_bigint(lhs_numeric, rhs_numeric))
  861. return Value(js_bigint(vm, lhs_numeric.as_bigint().big_integer().bitwise_xor(rhs_numeric.as_bigint().big_integer())));
  862. return vm.throw_completion<TypeError>(ErrorType::BigIntBadOperatorOtherType, "bitwise XOR");
  863. }
  864. // 13.5.6 Bitwise NOT Operator ( ~ ), https://tc39.es/ecma262/#sec-bitwise-not-operator
  865. ThrowCompletionOr<Value> bitwise_not(VM& vm, Value lhs)
  866. {
  867. auto lhs_numeric = TRY(lhs.to_numeric(vm));
  868. if (lhs_numeric.is_number())
  869. return Value(~TRY(lhs_numeric.to_i32(vm)));
  870. return Value(js_bigint(vm, lhs_numeric.as_bigint().big_integer().bitwise_not()));
  871. }
  872. // 13.5.4 Unary + Operator, https://tc39.es/ecma262/#sec-unary-plus-operator
  873. ThrowCompletionOr<Value> unary_plus(VM& vm, Value lhs)
  874. {
  875. return TRY(lhs.to_number(vm));
  876. }
  877. // 13.5.5 Unary - Operator, https://tc39.es/ecma262/#sec-unary-minus-operator
  878. ThrowCompletionOr<Value> unary_minus(VM& vm, Value lhs)
  879. {
  880. auto lhs_numeric = TRY(lhs.to_numeric(vm));
  881. if (lhs_numeric.is_number()) {
  882. if (lhs_numeric.is_nan())
  883. return js_nan();
  884. return Value(-lhs_numeric.as_double());
  885. }
  886. if (lhs_numeric.as_bigint().big_integer() == BIGINT_ZERO)
  887. return Value(js_bigint(vm, BIGINT_ZERO));
  888. auto big_integer_negated = lhs_numeric.as_bigint().big_integer();
  889. big_integer_negated.negate();
  890. return Value(js_bigint(vm, big_integer_negated));
  891. }
  892. // 13.9.1 The Left Shift Operator ( << ), https://tc39.es/ecma262/#sec-left-shift-operator
  893. ThrowCompletionOr<Value> left_shift(VM& vm, Value lhs, Value rhs)
  894. {
  895. auto lhs_numeric = TRY(lhs.to_numeric(vm));
  896. auto rhs_numeric = TRY(rhs.to_numeric(vm));
  897. if (both_number(lhs_numeric, rhs_numeric)) {
  898. if (!lhs_numeric.is_finite_number())
  899. return Value(0);
  900. if (!rhs_numeric.is_finite_number())
  901. return lhs_numeric;
  902. // Ok, so this performs toNumber() again but that "can't" throw
  903. auto lhs_i32 = MUST(lhs_numeric.to_i32(vm));
  904. auto rhs_u32 = MUST(rhs_numeric.to_u32(vm)) % 32;
  905. return Value(lhs_i32 << rhs_u32);
  906. }
  907. if (both_bigint(lhs_numeric, rhs_numeric)) {
  908. // 6.1.6.2.9 BigInt::leftShift ( x, y ), https://tc39.es/ecma262/#sec-numeric-types-bigint-leftShift
  909. auto multiplier_divisor = Crypto::SignedBigInteger { Crypto::NumberTheory::Power(Crypto::UnsignedBigInteger(2), rhs_numeric.as_bigint().big_integer().unsigned_value()) };
  910. // 1. If y < 0ℤ, then
  911. if (rhs_numeric.as_bigint().big_integer().is_negative()) {
  912. // a. Return the BigInt value that represents ℝ(x) / 2^-y, rounding down to the nearest integer, including for negative numbers.
  913. // NOTE: Since y is negative we can just do ℝ(x) / 2^|y|
  914. auto const& big_integer = lhs_numeric.as_bigint().big_integer();
  915. auto division_result = big_integer.divided_by(multiplier_divisor);
  916. // For positive initial values and no remainder just return quotient
  917. if (division_result.remainder.is_zero() || !big_integer.is_negative())
  918. return js_bigint(vm, division_result.quotient);
  919. // For negative round "down" to the next negative number
  920. return js_bigint(vm, division_result.quotient.minus(Crypto::SignedBigInteger { 1 }));
  921. }
  922. // 2. Return the BigInt value that represents ℝ(x) × 2^y.
  923. return Value(js_bigint(vm, lhs_numeric.as_bigint().big_integer().multiplied_by(multiplier_divisor)));
  924. }
  925. return vm.throw_completion<TypeError>(ErrorType::BigIntBadOperatorOtherType, "left-shift");
  926. }
  927. // 13.9.2 The Signed Right Shift Operator ( >> ), https://tc39.es/ecma262/#sec-signed-right-shift-operator
  928. ThrowCompletionOr<Value> right_shift(VM& vm, Value lhs, Value rhs)
  929. {
  930. auto lhs_numeric = TRY(lhs.to_numeric(vm));
  931. auto rhs_numeric = TRY(rhs.to_numeric(vm));
  932. if (both_number(lhs_numeric, rhs_numeric)) {
  933. if (!lhs_numeric.is_finite_number())
  934. return Value(0);
  935. if (!rhs_numeric.is_finite_number())
  936. return lhs_numeric;
  937. auto lhs_i32 = MUST(lhs_numeric.to_i32(vm));
  938. auto rhs_u32 = MUST(rhs_numeric.to_u32(vm)) % 32;
  939. return Value(lhs_i32 >> rhs_u32);
  940. }
  941. if (both_bigint(lhs_numeric, rhs_numeric)) {
  942. auto rhs_negated = rhs_numeric.as_bigint().big_integer();
  943. rhs_negated.negate();
  944. return left_shift(vm, lhs, js_bigint(vm, rhs_negated));
  945. }
  946. return vm.throw_completion<TypeError>(ErrorType::BigIntBadOperatorOtherType, "right-shift");
  947. }
  948. // 13.9.3 The Unsigned Right Shift Operator ( >>> ), https://tc39.es/ecma262/#sec-unsigned-right-shift-operator
  949. ThrowCompletionOr<Value> unsigned_right_shift(VM& vm, Value lhs, Value rhs)
  950. {
  951. auto lhs_numeric = TRY(lhs.to_numeric(vm));
  952. auto rhs_numeric = TRY(rhs.to_numeric(vm));
  953. if (both_number(lhs_numeric, rhs_numeric)) {
  954. if (!lhs_numeric.is_finite_number())
  955. return Value(0);
  956. if (!rhs_numeric.is_finite_number())
  957. return lhs_numeric;
  958. // Ok, so this performs toNumber() again but that "can't" throw
  959. auto lhs_u32 = MUST(lhs_numeric.to_u32(vm));
  960. auto rhs_u32 = MUST(rhs_numeric.to_u32(vm)) % 32;
  961. return Value(lhs_u32 >> rhs_u32);
  962. }
  963. return vm.throw_completion<TypeError>(ErrorType::BigIntBadOperator, "unsigned right-shift");
  964. }
  965. // 13.8.1 The Addition Operator ( + ), https://tc39.es/ecma262/#sec-addition-operator-plus
  966. ThrowCompletionOr<Value> add(VM& vm, Value lhs, Value rhs)
  967. {
  968. if (both_number(lhs, rhs)) {
  969. if (lhs.is_int32() && rhs.is_int32()) {
  970. Checked<i32> result;
  971. result = MUST(lhs.to_i32(vm));
  972. result += MUST(rhs.to_i32(vm));
  973. if (!result.has_overflow())
  974. return Value(result.value());
  975. }
  976. return Value(lhs.as_double() + rhs.as_double());
  977. }
  978. auto lhs_primitive = TRY(lhs.to_primitive(vm));
  979. auto rhs_primitive = TRY(rhs.to_primitive(vm));
  980. if (lhs_primitive.is_string() || rhs_primitive.is_string()) {
  981. auto lhs_string = TRY(lhs_primitive.to_primitive_string(vm));
  982. auto rhs_string = TRY(rhs_primitive.to_primitive_string(vm));
  983. return js_rope_string(vm, *lhs_string, *rhs_string);
  984. }
  985. auto lhs_numeric = TRY(lhs_primitive.to_numeric(vm));
  986. auto rhs_numeric = TRY(rhs_primitive.to_numeric(vm));
  987. if (both_number(lhs_numeric, rhs_numeric))
  988. return Value(lhs_numeric.as_double() + rhs_numeric.as_double());
  989. if (both_bigint(lhs_numeric, rhs_numeric))
  990. return Value(js_bigint(vm, lhs_numeric.as_bigint().big_integer().plus(rhs_numeric.as_bigint().big_integer())));
  991. return vm.throw_completion<TypeError>(ErrorType::BigIntBadOperatorOtherType, "addition");
  992. }
  993. // 13.8.2 The Subtraction Operator ( - ), https://tc39.es/ecma262/#sec-subtraction-operator-minus
  994. ThrowCompletionOr<Value> sub(VM& vm, Value lhs, Value rhs)
  995. {
  996. auto lhs_numeric = TRY(lhs.to_numeric(vm));
  997. auto rhs_numeric = TRY(rhs.to_numeric(vm));
  998. if (both_number(lhs_numeric, rhs_numeric)) {
  999. double lhsd = lhs_numeric.as_double();
  1000. double rhsd = rhs_numeric.as_double();
  1001. double interm = lhsd - rhsd;
  1002. return Value(interm);
  1003. }
  1004. if (both_bigint(lhs_numeric, rhs_numeric))
  1005. return Value(js_bigint(vm, lhs_numeric.as_bigint().big_integer().minus(rhs_numeric.as_bigint().big_integer())));
  1006. return vm.throw_completion<TypeError>(ErrorType::BigIntBadOperatorOtherType, "subtraction");
  1007. }
  1008. // 13.7 Multiplicative Operators, https://tc39.es/ecma262/#sec-multiplicative-operators
  1009. ThrowCompletionOr<Value> mul(VM& vm, Value lhs, Value rhs)
  1010. {
  1011. auto lhs_numeric = TRY(lhs.to_numeric(vm));
  1012. auto rhs_numeric = TRY(rhs.to_numeric(vm));
  1013. if (both_number(lhs_numeric, rhs_numeric))
  1014. return Value(lhs_numeric.as_double() * rhs_numeric.as_double());
  1015. if (both_bigint(lhs_numeric, rhs_numeric))
  1016. return Value(js_bigint(vm, lhs_numeric.as_bigint().big_integer().multiplied_by(rhs_numeric.as_bigint().big_integer())));
  1017. return vm.throw_completion<TypeError>(ErrorType::BigIntBadOperatorOtherType, "multiplication");
  1018. }
  1019. // 13.7 Multiplicative Operators, https://tc39.es/ecma262/#sec-multiplicative-operators
  1020. ThrowCompletionOr<Value> div(VM& vm, Value lhs, Value rhs)
  1021. {
  1022. auto lhs_numeric = TRY(lhs.to_numeric(vm));
  1023. auto rhs_numeric = TRY(rhs.to_numeric(vm));
  1024. if (both_number(lhs_numeric, rhs_numeric))
  1025. return Value(lhs_numeric.as_double() / rhs_numeric.as_double());
  1026. if (both_bigint(lhs_numeric, rhs_numeric)) {
  1027. if (rhs_numeric.as_bigint().big_integer() == BIGINT_ZERO)
  1028. return vm.throw_completion<RangeError>(ErrorType::DivisionByZero);
  1029. return Value(js_bigint(vm, lhs_numeric.as_bigint().big_integer().divided_by(rhs_numeric.as_bigint().big_integer()).quotient));
  1030. }
  1031. return vm.throw_completion<TypeError>(ErrorType::BigIntBadOperatorOtherType, "division");
  1032. }
  1033. // 13.7 Multiplicative Operators, https://tc39.es/ecma262/#sec-multiplicative-operators
  1034. ThrowCompletionOr<Value> mod(VM& vm, Value lhs, Value rhs)
  1035. {
  1036. auto lhs_numeric = TRY(lhs.to_numeric(vm));
  1037. auto rhs_numeric = TRY(rhs.to_numeric(vm));
  1038. if (both_number(lhs_numeric, rhs_numeric)) {
  1039. // 6.1.6.1.6 Number::remainder ( n, d ), https://tc39.es/ecma262/#sec-numeric-types-number-remainder
  1040. // The ECMA specification is describing the mathematical definition of modulus
  1041. // implemented by fmod.
  1042. auto n = lhs_numeric.as_double();
  1043. auto d = rhs_numeric.as_double();
  1044. return Value(fmod(n, d));
  1045. }
  1046. if (both_bigint(lhs_numeric, rhs_numeric)) {
  1047. if (rhs_numeric.as_bigint().big_integer() == BIGINT_ZERO)
  1048. return vm.throw_completion<RangeError>(ErrorType::DivisionByZero);
  1049. return Value(js_bigint(vm, lhs_numeric.as_bigint().big_integer().divided_by(rhs_numeric.as_bigint().big_integer()).remainder));
  1050. }
  1051. return vm.throw_completion<TypeError>(ErrorType::BigIntBadOperatorOtherType, "modulo");
  1052. }
  1053. static Value exp_double(Value base, Value exponent)
  1054. {
  1055. VERIFY(both_number(base, exponent));
  1056. if (exponent.is_nan())
  1057. return js_nan();
  1058. if (exponent.is_positive_zero() || exponent.is_negative_zero())
  1059. return Value(1);
  1060. if (base.is_nan())
  1061. return js_nan();
  1062. if (base.is_positive_infinity())
  1063. return exponent.as_double() > 0 ? js_infinity() : Value(0);
  1064. if (base.is_negative_infinity()) {
  1065. auto is_odd_integral_number = exponent.is_integral_number() && (static_cast<i32>(exponent.as_double()) % 2 != 0);
  1066. if (exponent.as_double() > 0)
  1067. return is_odd_integral_number ? js_negative_infinity() : js_infinity();
  1068. else
  1069. return is_odd_integral_number ? Value(-0.0) : Value(0);
  1070. }
  1071. if (base.is_positive_zero())
  1072. return exponent.as_double() > 0 ? Value(0) : js_infinity();
  1073. if (base.is_negative_zero()) {
  1074. auto is_odd_integral_number = exponent.is_integral_number() && (static_cast<i32>(exponent.as_double()) % 2 != 0);
  1075. if (exponent.as_double() > 0)
  1076. return is_odd_integral_number ? Value(-0.0) : Value(0);
  1077. else
  1078. return is_odd_integral_number ? js_negative_infinity() : js_infinity();
  1079. }
  1080. VERIFY(base.is_finite_number() && !base.is_positive_zero() && !base.is_negative_zero());
  1081. if (exponent.is_positive_infinity()) {
  1082. auto absolute_base = fabs(base.as_double());
  1083. if (absolute_base > 1)
  1084. return js_infinity();
  1085. else if (absolute_base == 1)
  1086. return js_nan();
  1087. else if (absolute_base < 1)
  1088. return Value(0);
  1089. }
  1090. if (exponent.is_negative_infinity()) {
  1091. auto absolute_base = fabs(base.as_double());
  1092. if (absolute_base > 1)
  1093. return Value(0);
  1094. else if (absolute_base == 1)
  1095. return js_nan();
  1096. else if (absolute_base < 1)
  1097. return js_infinity();
  1098. }
  1099. VERIFY(exponent.is_finite_number() && !exponent.is_positive_zero() && !exponent.is_negative_zero());
  1100. if (base.as_double() < 0 && !exponent.is_integral_number())
  1101. return js_nan();
  1102. return Value(::pow(base.as_double(), exponent.as_double()));
  1103. }
  1104. // 13.6 Exponentiation Operator, https://tc39.es/ecma262/#sec-exp-operator
  1105. ThrowCompletionOr<Value> exp(VM& vm, Value lhs, Value rhs)
  1106. {
  1107. auto lhs_numeric = TRY(lhs.to_numeric(vm));
  1108. auto rhs_numeric = TRY(rhs.to_numeric(vm));
  1109. if (both_number(lhs_numeric, rhs_numeric))
  1110. return exp_double(lhs_numeric, rhs_numeric);
  1111. if (both_bigint(lhs_numeric, rhs_numeric)) {
  1112. if (rhs_numeric.as_bigint().big_integer().is_negative())
  1113. return vm.throw_completion<RangeError>(ErrorType::NegativeExponent);
  1114. return Value(js_bigint(vm, Crypto::NumberTheory::Power(lhs_numeric.as_bigint().big_integer(), rhs_numeric.as_bigint().big_integer())));
  1115. }
  1116. return vm.throw_completion<TypeError>(ErrorType::BigIntBadOperatorOtherType, "exponentiation");
  1117. }
  1118. ThrowCompletionOr<Value> in(VM& vm, Value lhs, Value rhs)
  1119. {
  1120. if (!rhs.is_object())
  1121. return vm.throw_completion<TypeError>(ErrorType::InOperatorWithObject);
  1122. auto lhs_property_key = TRY(lhs.to_property_key(vm));
  1123. return Value(TRY(rhs.as_object().has_property(lhs_property_key)));
  1124. }
  1125. // 13.10.2 InstanceofOperator ( V, target ), https://tc39.es/ecma262/#sec-instanceofoperator
  1126. ThrowCompletionOr<Value> instance_of(VM& vm, Value lhs, Value rhs)
  1127. {
  1128. if (!rhs.is_object())
  1129. return vm.throw_completion<TypeError>(ErrorType::NotAnObject, rhs.to_string_without_side_effects());
  1130. auto has_instance_method = TRY(rhs.get_method(vm, *vm.well_known_symbol_has_instance()));
  1131. if (has_instance_method) {
  1132. auto has_instance_result = TRY(call(vm, *has_instance_method, rhs, lhs));
  1133. return Value(has_instance_result.to_boolean());
  1134. }
  1135. if (!rhs.is_function())
  1136. return vm.throw_completion<TypeError>(ErrorType::NotAFunction, rhs.to_string_without_side_effects());
  1137. return TRY(ordinary_has_instance(vm, lhs, rhs));
  1138. }
  1139. // 7.3.22 OrdinaryHasInstance ( C, O ), https://tc39.es/ecma262/#sec-ordinaryhasinstance
  1140. ThrowCompletionOr<Value> ordinary_has_instance(VM& vm, Value lhs, Value rhs)
  1141. {
  1142. if (!rhs.is_function())
  1143. return Value(false);
  1144. auto& rhs_function = rhs.as_function();
  1145. if (is<BoundFunction>(rhs_function)) {
  1146. auto& bound_target = static_cast<BoundFunction const&>(rhs_function);
  1147. return instance_of(vm, lhs, Value(&bound_target.bound_target_function()));
  1148. }
  1149. if (!lhs.is_object())
  1150. return Value(false);
  1151. Object* lhs_object = &lhs.as_object();
  1152. auto rhs_prototype = TRY(rhs_function.get(vm.names.prototype));
  1153. if (!rhs_prototype.is_object())
  1154. return vm.throw_completion<TypeError>(ErrorType::InstanceOfOperatorBadPrototype, rhs.to_string_without_side_effects());
  1155. while (true) {
  1156. lhs_object = TRY(lhs_object->internal_get_prototype_of());
  1157. if (!lhs_object)
  1158. return Value(false);
  1159. if (same_value(rhs_prototype, lhs_object))
  1160. return Value(true);
  1161. }
  1162. }
  1163. // 7.2.10 SameValue ( x, y ), https://tc39.es/ecma262/#sec-samevalue
  1164. bool same_value(Value lhs, Value rhs)
  1165. {
  1166. if (!same_type_for_equality(lhs, rhs))
  1167. return false;
  1168. if (lhs.is_number()) {
  1169. if (lhs.is_nan() && rhs.is_nan())
  1170. return true;
  1171. if (lhs.is_positive_zero() && rhs.is_negative_zero())
  1172. return false;
  1173. if (lhs.is_negative_zero() && rhs.is_positive_zero())
  1174. return false;
  1175. return lhs.as_double() == rhs.as_double();
  1176. }
  1177. if (lhs.is_bigint()) {
  1178. auto lhs_big_integer = lhs.as_bigint().big_integer();
  1179. auto rhs_big_integer = rhs.as_bigint().big_integer();
  1180. if (lhs_big_integer == BIGINT_ZERO && rhs_big_integer == BIGINT_ZERO && lhs_big_integer.is_negative() != rhs_big_integer.is_negative())
  1181. return false;
  1182. return lhs_big_integer == rhs_big_integer;
  1183. }
  1184. return same_value_non_numeric(lhs, rhs);
  1185. }
  1186. // 7.2.11 SameValueZero ( x, y ), https://tc39.es/ecma262/#sec-samevaluezero
  1187. bool same_value_zero(Value lhs, Value rhs)
  1188. {
  1189. if (!same_type_for_equality(lhs, rhs))
  1190. return false;
  1191. if (lhs.is_number()) {
  1192. if (lhs.is_nan() && rhs.is_nan())
  1193. return true;
  1194. return lhs.as_double() == rhs.as_double();
  1195. }
  1196. if (lhs.is_bigint())
  1197. return lhs.as_bigint().big_integer() == rhs.as_bigint().big_integer();
  1198. return same_value_non_numeric(lhs, rhs);
  1199. }
  1200. // 7.2.12 SameValueNonNumeric ( x, y ), https://tc39.es/ecma262/#sec-samevaluenonnumeric
  1201. bool same_value_non_numeric(Value lhs, Value rhs)
  1202. {
  1203. VERIFY(!lhs.is_number() && !lhs.is_bigint());
  1204. VERIFY(same_type_for_equality(lhs, rhs));
  1205. if (lhs.is_string())
  1206. return lhs.as_string().string() == rhs.as_string().string();
  1207. return lhs.m_value.encoded == rhs.m_value.encoded;
  1208. }
  1209. // 7.2.15 IsStrictlyEqual ( x, y ), https://tc39.es/ecma262/#sec-isstrictlyequal
  1210. bool is_strictly_equal(Value lhs, Value rhs)
  1211. {
  1212. if (!same_type_for_equality(lhs, rhs))
  1213. return false;
  1214. if (lhs.is_number()) {
  1215. if (lhs.is_nan() || rhs.is_nan())
  1216. return false;
  1217. if (lhs.as_double() == rhs.as_double())
  1218. return true;
  1219. return false;
  1220. }
  1221. if (lhs.is_bigint())
  1222. return lhs.as_bigint().big_integer() == rhs.as_bigint().big_integer();
  1223. return same_value_non_numeric(lhs, rhs);
  1224. }
  1225. // 7.2.14 IsLooselyEqual ( x, y ), https://tc39.es/ecma262/#sec-islooselyequal
  1226. ThrowCompletionOr<bool> is_loosely_equal(VM& vm, Value lhs, Value rhs)
  1227. {
  1228. // 1. If Type(x) is the same as Type(y), then
  1229. if (same_type_for_equality(lhs, rhs)) {
  1230. // a. Return IsStrictlyEqual(x, y).
  1231. return is_strictly_equal(lhs, rhs);
  1232. }
  1233. // 2. If x is null and y is undefined, return true.
  1234. // 3. If x is undefined and y is null, return true.
  1235. if (lhs.is_nullish() && rhs.is_nullish())
  1236. return true;
  1237. // 4. NOTE: This step is replaced in section B.3.6.2.
  1238. // B.3.6.2 Changes to IsLooselyEqual, https://tc39.es/ecma262/#sec-IsHTMLDDA-internal-slot-aec
  1239. // 4. Perform the following steps:
  1240. // a. If Type(x) is Object and x has an [[IsHTMLDDA]] internal slot and y is either null or undefined, return true.
  1241. if (lhs.is_object() && lhs.as_object().is_htmldda() && rhs.is_nullish())
  1242. return true;
  1243. // b. If x is either null or undefined and Type(y) is Object and y has an [[IsHTMLDDA]] internal slot, return true.
  1244. if (lhs.is_nullish() && rhs.is_object() && rhs.as_object().is_htmldda())
  1245. return true;
  1246. // == End of B.3.6.2 ==
  1247. // 5. If Type(x) is Number and Type(y) is String, return ! IsLooselyEqual(x, ! ToNumber(y)).
  1248. if (lhs.is_number() && rhs.is_string())
  1249. return is_loosely_equal(vm, lhs, MUST(rhs.to_number(vm)));
  1250. // 6. If Type(x) is String and Type(y) is Number, return ! IsLooselyEqual(! ToNumber(x), y).
  1251. if (lhs.is_string() && rhs.is_number())
  1252. return is_loosely_equal(vm, MUST(lhs.to_number(vm)), rhs);
  1253. // 7. If Type(x) is BigInt and Type(y) is String, then
  1254. if (lhs.is_bigint() && rhs.is_string()) {
  1255. // a. Let n be StringToBigInt(y).
  1256. auto bigint = rhs.string_to_bigint(vm);
  1257. // b. If n is undefined, return false.
  1258. if (!bigint.has_value())
  1259. return false;
  1260. // c. Return ! IsLooselyEqual(x, n).
  1261. return is_loosely_equal(vm, lhs, *bigint);
  1262. }
  1263. // 8. If Type(x) is String and Type(y) is BigInt, return ! IsLooselyEqual(y, x).
  1264. if (lhs.is_string() && rhs.is_bigint())
  1265. return is_loosely_equal(vm, rhs, lhs);
  1266. // 9. If Type(x) is Boolean, return ! IsLooselyEqual(! ToNumber(x), y).
  1267. if (lhs.is_boolean())
  1268. return is_loosely_equal(vm, MUST(lhs.to_number(vm)), rhs);
  1269. // 10. If Type(y) is Boolean, return ! IsLooselyEqual(x, ! ToNumber(y)).
  1270. if (rhs.is_boolean())
  1271. return is_loosely_equal(vm, lhs, MUST(rhs.to_number(vm)));
  1272. // 11. If Type(x) is either String, Number, BigInt, or Symbol and Type(y) is Object, return ! IsLooselyEqual(x, ? ToPrimitive(y)).
  1273. if ((lhs.is_string() || lhs.is_number() || lhs.is_bigint() || lhs.is_symbol()) && rhs.is_object()) {
  1274. auto rhs_primitive = TRY(rhs.to_primitive(vm));
  1275. return is_loosely_equal(vm, lhs, rhs_primitive);
  1276. }
  1277. // 12. If Type(x) is Object and Type(y) is either String, Number, BigInt, or Symbol, return ! IsLooselyEqual(? ToPrimitive(x), y).
  1278. if (lhs.is_object() && (rhs.is_string() || rhs.is_number() || rhs.is_bigint() || rhs.is_symbol())) {
  1279. auto lhs_primitive = TRY(lhs.to_primitive(vm));
  1280. return is_loosely_equal(vm, lhs_primitive, rhs);
  1281. }
  1282. // 13. If Type(x) is BigInt and Type(y) is Number, or if Type(x) is Number and Type(y) is BigInt, then
  1283. if ((lhs.is_bigint() && rhs.is_number()) || (lhs.is_number() && rhs.is_bigint())) {
  1284. // a. If x or y are any of NaN, +∞𝔽, or -∞𝔽, return false.
  1285. if (lhs.is_nan() || lhs.is_infinity() || rhs.is_nan() || rhs.is_infinity())
  1286. return false;
  1287. // b. If ℝ(x) = ℝ(y), return true; otherwise return false.
  1288. if ((lhs.is_number() && !lhs.is_integral_number()) || (rhs.is_number() && !rhs.is_integral_number()))
  1289. return false;
  1290. VERIFY(!lhs.is_nan() && !rhs.is_nan());
  1291. auto& number_side = lhs.is_number() ? lhs : rhs;
  1292. auto& bigint_side = lhs.is_number() ? rhs : lhs;
  1293. return bigint_side.as_bigint().big_integer().compare_to_double(number_side.as_double()) == Crypto::SignedBigInteger::CompareResult::DoubleEqualsBigInt;
  1294. }
  1295. // 14. Return false.
  1296. return false;
  1297. }
  1298. // 7.2.13 IsLessThan ( x, y, LeftFirst ), https://tc39.es/ecma262/#sec-islessthan
  1299. ThrowCompletionOr<TriState> is_less_than(VM& vm, Value lhs, Value rhs, bool left_first)
  1300. {
  1301. Value x_primitive;
  1302. Value y_primitive;
  1303. if (left_first) {
  1304. x_primitive = TRY(lhs.to_primitive(vm, Value::PreferredType::Number));
  1305. y_primitive = TRY(rhs.to_primitive(vm, Value::PreferredType::Number));
  1306. } else {
  1307. y_primitive = TRY(lhs.to_primitive(vm, Value::PreferredType::Number));
  1308. x_primitive = TRY(rhs.to_primitive(vm, Value::PreferredType::Number));
  1309. }
  1310. if (x_primitive.is_string() && y_primitive.is_string()) {
  1311. auto x_string = x_primitive.as_string().string();
  1312. auto y_string = y_primitive.as_string().string();
  1313. Utf8View x_code_points { x_string };
  1314. Utf8View y_code_points { y_string };
  1315. if (x_code_points.starts_with(y_code_points))
  1316. return TriState::False;
  1317. if (y_code_points.starts_with(x_code_points))
  1318. return TriState::True;
  1319. for (auto k = x_code_points.begin(), l = y_code_points.begin();
  1320. k != x_code_points.end() && l != y_code_points.end();
  1321. ++k, ++l) {
  1322. if (*k != *l) {
  1323. if (*k < *l) {
  1324. return TriState::True;
  1325. } else {
  1326. return TriState::False;
  1327. }
  1328. }
  1329. }
  1330. VERIFY_NOT_REACHED();
  1331. }
  1332. if (x_primitive.is_bigint() && y_primitive.is_string()) {
  1333. auto y_bigint = y_primitive.string_to_bigint(vm);
  1334. if (!y_bigint.has_value())
  1335. return TriState::Unknown;
  1336. if (x_primitive.as_bigint().big_integer() < (*y_bigint)->big_integer())
  1337. return TriState::True;
  1338. return TriState::False;
  1339. }
  1340. if (x_primitive.is_string() && y_primitive.is_bigint()) {
  1341. auto x_bigint = x_primitive.string_to_bigint(vm);
  1342. if (!x_bigint.has_value())
  1343. return TriState::Unknown;
  1344. if ((*x_bigint)->big_integer() < y_primitive.as_bigint().big_integer())
  1345. return TriState::True;
  1346. return TriState::False;
  1347. }
  1348. auto x_numeric = TRY(x_primitive.to_numeric(vm));
  1349. auto y_numeric = TRY(y_primitive.to_numeric(vm));
  1350. if (x_numeric.is_nan() || y_numeric.is_nan())
  1351. return TriState::Unknown;
  1352. if (x_numeric.is_positive_infinity() || y_numeric.is_negative_infinity())
  1353. return TriState::False;
  1354. if (x_numeric.is_negative_infinity() || y_numeric.is_positive_infinity())
  1355. return TriState::True;
  1356. if (x_numeric.is_number() && y_numeric.is_number()) {
  1357. if (x_numeric.as_double() < y_numeric.as_double())
  1358. return TriState::True;
  1359. else
  1360. return TriState::False;
  1361. }
  1362. if (x_numeric.is_bigint() && y_numeric.is_bigint()) {
  1363. if (x_numeric.as_bigint().big_integer() < y_numeric.as_bigint().big_integer())
  1364. return TriState::True;
  1365. else
  1366. return TriState::False;
  1367. }
  1368. VERIFY((x_numeric.is_number() && y_numeric.is_bigint()) || (x_numeric.is_bigint() && y_numeric.is_number()));
  1369. bool x_lower_than_y;
  1370. VERIFY(!x_numeric.is_nan() && !y_numeric.is_nan());
  1371. if (x_numeric.is_number()) {
  1372. x_lower_than_y = y_numeric.as_bigint().big_integer().compare_to_double(x_numeric.as_double())
  1373. == Crypto::SignedBigInteger::CompareResult::DoubleLessThanBigInt;
  1374. } else {
  1375. x_lower_than_y = x_numeric.as_bigint().big_integer().compare_to_double(y_numeric.as_double())
  1376. == Crypto::SignedBigInteger::CompareResult::DoubleGreaterThanBigInt;
  1377. }
  1378. if (x_lower_than_y)
  1379. return TriState::True;
  1380. else
  1381. return TriState::False;
  1382. }
  1383. // 7.3.21 Invoke ( V, P [ , argumentsList ] ), https://tc39.es/ecma262/#sec-invoke
  1384. ThrowCompletionOr<Value> Value::invoke_internal(VM& vm, PropertyKey const& property_key, Optional<MarkedVector<Value>> arguments)
  1385. {
  1386. auto property = TRY(get(vm, property_key));
  1387. if (!property.is_function())
  1388. return vm.throw_completion<TypeError>(ErrorType::NotAFunction, property.to_string_without_side_effects());
  1389. return call(vm, property.as_function(), *this, move(arguments));
  1390. }
  1391. }