Value.cpp 62 KB

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