Value.cpp 56 KB

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