Value.cpp 61 KB

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