Value.cpp 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612
  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. // 7.3.3 GetV ( V, P ), https://tc39.es/ecma262/#sec-getv
  724. ThrowCompletionOr<Value> Value::get(GlobalObject& global_object, PropertyKey const& property_key) const
  725. {
  726. // 1. Assert: IsPropertyKey(P) is true.
  727. VERIFY(property_key.is_valid());
  728. // 2. Let O be ? ToObject(V).
  729. auto* object = TRY(to_object(global_object));
  730. // 3. Return ? O.[[Get]](P, V).
  731. return TRY(object->internal_get(property_key, *this));
  732. }
  733. // 7.3.11 GetMethod ( V, P ), https://tc39.es/ecma262/#sec-getmethod
  734. ThrowCompletionOr<FunctionObject*> Value::get_method(GlobalObject& global_object, PropertyKey const& property_key) const
  735. {
  736. auto& vm = global_object.vm();
  737. // 1. Assert: IsPropertyKey(P) is true.
  738. VERIFY(property_key.is_valid());
  739. // 2. Let func be ? GetV(V, P).
  740. auto function = TRY(get(global_object, property_key));
  741. // 3. If func is either undefined or null, return undefined.
  742. if (function.is_nullish())
  743. return nullptr;
  744. // 4. If IsCallable(func) is false, throw a TypeError exception.
  745. if (!function.is_function())
  746. return vm.throw_completion<TypeError>(global_object, ErrorType::NotAFunction, function.to_string_without_side_effects());
  747. // 5. Return func.
  748. return &function.as_function();
  749. }
  750. // 13.10 Relational Operators, https://tc39.es/ecma262/#sec-relational-operators
  751. ThrowCompletionOr<Value> greater_than(GlobalObject& global_object, Value lhs, Value rhs)
  752. {
  753. if (lhs.type() == Value::Type::Int32 && rhs.type() == Value::Type::Int32)
  754. return lhs.as_i32() > rhs.as_i32();
  755. TriState relation = TRY(is_less_than(global_object, false, lhs, rhs));
  756. if (relation == TriState::Unknown)
  757. return Value(false);
  758. return Value(relation == TriState::True);
  759. }
  760. // 13.10 Relational Operators, https://tc39.es/ecma262/#sec-relational-operators
  761. ThrowCompletionOr<Value> greater_than_equals(GlobalObject& global_object, Value lhs, Value rhs)
  762. {
  763. if (lhs.type() == Value::Type::Int32 && rhs.type() == Value::Type::Int32)
  764. return lhs.as_i32() >= rhs.as_i32();
  765. TriState relation = TRY(is_less_than(global_object, true, lhs, rhs));
  766. if (relation == TriState::Unknown || relation == TriState::True)
  767. return Value(false);
  768. return Value(true);
  769. }
  770. // 13.10 Relational Operators, https://tc39.es/ecma262/#sec-relational-operators
  771. ThrowCompletionOr<Value> less_than(GlobalObject& global_object, Value lhs, Value rhs)
  772. {
  773. if (lhs.type() == Value::Type::Int32 && rhs.type() == Value::Type::Int32)
  774. return lhs.as_i32() < rhs.as_i32();
  775. TriState relation = TRY(is_less_than(global_object, true, lhs, rhs));
  776. if (relation == TriState::Unknown)
  777. return Value(false);
  778. return Value(relation == TriState::True);
  779. }
  780. // 13.10 Relational Operators, https://tc39.es/ecma262/#sec-relational-operators
  781. ThrowCompletionOr<Value> less_than_equals(GlobalObject& global_object, Value lhs, Value rhs)
  782. {
  783. if (lhs.type() == Value::Type::Int32 && rhs.type() == Value::Type::Int32)
  784. return lhs.as_i32() <= rhs.as_i32();
  785. TriState relation = TRY(is_less_than(global_object, false, lhs, rhs));
  786. if (relation == TriState::Unknown || relation == TriState::True)
  787. return Value(false);
  788. return Value(true);
  789. }
  790. // 13.12 Binary Bitwise Operators, https://tc39.es/ecma262/#sec-binary-bitwise-operators
  791. ThrowCompletionOr<Value> bitwise_and(GlobalObject& global_object, Value lhs, Value rhs)
  792. {
  793. auto& vm = global_object.vm();
  794. auto lhs_numeric = TRY(lhs.to_numeric(global_object));
  795. auto rhs_numeric = TRY(rhs.to_numeric(global_object));
  796. if (both_number(lhs_numeric, rhs_numeric)) {
  797. if (!lhs_numeric.is_finite_number() || !rhs_numeric.is_finite_number())
  798. return Value(0);
  799. return Value(TRY(lhs_numeric.to_i32(global_object)) & TRY(rhs_numeric.to_i32(global_object)));
  800. }
  801. if (both_bigint(lhs_numeric, rhs_numeric))
  802. return Value(js_bigint(vm, lhs_numeric.as_bigint().big_integer().bitwise_and(rhs_numeric.as_bigint().big_integer())));
  803. return vm.throw_completion<TypeError>(global_object, ErrorType::BigIntBadOperatorOtherType, "bitwise AND");
  804. }
  805. // 13.12 Binary Bitwise Operators, https://tc39.es/ecma262/#sec-binary-bitwise-operators
  806. ThrowCompletionOr<Value> bitwise_or(GlobalObject& global_object, Value lhs, Value rhs)
  807. {
  808. auto& vm = global_object.vm();
  809. auto lhs_numeric = TRY(lhs.to_numeric(global_object));
  810. auto rhs_numeric = TRY(rhs.to_numeric(global_object));
  811. if (both_number(lhs_numeric, rhs_numeric)) {
  812. if (!lhs_numeric.is_finite_number() && !rhs_numeric.is_finite_number())
  813. return Value(0);
  814. if (!lhs_numeric.is_finite_number())
  815. return rhs_numeric;
  816. if (!rhs_numeric.is_finite_number())
  817. return lhs_numeric;
  818. return Value(TRY(lhs_numeric.to_i32(global_object)) | TRY(rhs_numeric.to_i32(global_object)));
  819. }
  820. if (both_bigint(lhs_numeric, rhs_numeric))
  821. return Value(js_bigint(vm, lhs_numeric.as_bigint().big_integer().bitwise_or(rhs_numeric.as_bigint().big_integer())));
  822. return vm.throw_completion<TypeError>(global_object, ErrorType::BigIntBadOperatorOtherType, "bitwise OR");
  823. }
  824. // 13.12 Binary Bitwise Operators, https://tc39.es/ecma262/#sec-binary-bitwise-operators
  825. ThrowCompletionOr<Value> bitwise_xor(GlobalObject& global_object, Value lhs, Value rhs)
  826. {
  827. auto& vm = global_object.vm();
  828. auto lhs_numeric = TRY(lhs.to_numeric(global_object));
  829. auto rhs_numeric = TRY(rhs.to_numeric(global_object));
  830. if (both_number(lhs_numeric, rhs_numeric)) {
  831. if (!lhs_numeric.is_finite_number() && !rhs_numeric.is_finite_number())
  832. return Value(0);
  833. if (!lhs_numeric.is_finite_number())
  834. return rhs_numeric;
  835. if (!rhs_numeric.is_finite_number())
  836. return lhs_numeric;
  837. return Value(TRY(lhs_numeric.to_i32(global_object)) ^ TRY(rhs_numeric.to_i32(global_object)));
  838. }
  839. if (both_bigint(lhs_numeric, rhs_numeric))
  840. return Value(js_bigint(vm, lhs_numeric.as_bigint().big_integer().bitwise_xor(rhs_numeric.as_bigint().big_integer())));
  841. return vm.throw_completion<TypeError>(global_object, ErrorType::BigIntBadOperatorOtherType, "bitwise XOR");
  842. }
  843. // 13.5.6 Bitwise NOT Operator ( ~ ), https://tc39.es/ecma262/#sec-bitwise-not-operator
  844. ThrowCompletionOr<Value> bitwise_not(GlobalObject& global_object, Value lhs)
  845. {
  846. auto& vm = global_object.vm();
  847. auto lhs_numeric = TRY(lhs.to_numeric(global_object));
  848. if (lhs_numeric.is_number())
  849. return Value(~TRY(lhs_numeric.to_i32(global_object)));
  850. return Value(js_bigint(vm, lhs_numeric.as_bigint().big_integer().bitwise_not()));
  851. }
  852. // 13.5.4 Unary + Operator, https://tc39.es/ecma262/#sec-unary-plus-operator
  853. ThrowCompletionOr<Value> unary_plus(GlobalObject& global_object, Value lhs)
  854. {
  855. return TRY(lhs.to_number(global_object));
  856. }
  857. // 13.5.5 Unary - Operator, https://tc39.es/ecma262/#sec-unary-minus-operator
  858. ThrowCompletionOr<Value> unary_minus(GlobalObject& global_object, Value lhs)
  859. {
  860. auto& vm = global_object.vm();
  861. auto lhs_numeric = TRY(lhs.to_numeric(global_object));
  862. if (lhs_numeric.is_number()) {
  863. if (lhs_numeric.is_nan())
  864. return js_nan();
  865. return Value(-lhs_numeric.as_double());
  866. }
  867. if (lhs_numeric.as_bigint().big_integer() == BIGINT_ZERO)
  868. return Value(js_bigint(vm, BIGINT_ZERO));
  869. auto big_integer_negated = lhs_numeric.as_bigint().big_integer();
  870. big_integer_negated.negate();
  871. return Value(js_bigint(vm, big_integer_negated));
  872. }
  873. // 13.9.1 The Left Shift Operator ( << ), https://tc39.es/ecma262/#sec-left-shift-operator
  874. ThrowCompletionOr<Value> left_shift(GlobalObject& global_object, Value lhs, Value rhs)
  875. {
  876. auto& vm = global_object.vm();
  877. auto lhs_numeric = TRY(lhs.to_numeric(global_object));
  878. auto rhs_numeric = TRY(rhs.to_numeric(global_object));
  879. if (both_number(lhs_numeric, rhs_numeric)) {
  880. if (!lhs_numeric.is_finite_number())
  881. return Value(0);
  882. if (!rhs_numeric.is_finite_number())
  883. return lhs_numeric;
  884. // Ok, so this performs toNumber() again but that "can't" throw
  885. auto lhs_i32 = MUST(lhs_numeric.to_i32(global_object));
  886. auto rhs_u32 = MUST(rhs_numeric.to_u32(global_object)) % 32;
  887. return Value(lhs_i32 << rhs_u32);
  888. }
  889. if (both_bigint(lhs_numeric, rhs_numeric)) {
  890. auto multiplier_divisor = Crypto::SignedBigInteger { Crypto::NumberTheory::Power(Crypto::UnsignedBigInteger(2), rhs_numeric.as_bigint().big_integer().unsigned_value()) };
  891. if (rhs_numeric.as_bigint().big_integer().is_negative())
  892. return Value(js_bigint(vm, lhs_numeric.as_bigint().big_integer().divided_by(multiplier_divisor).quotient));
  893. else
  894. return Value(js_bigint(vm, lhs_numeric.as_bigint().big_integer().multiplied_by(multiplier_divisor)));
  895. }
  896. return vm.throw_completion<TypeError>(global_object, ErrorType::BigIntBadOperatorOtherType, "left-shift");
  897. }
  898. // 13.9.2 The Signed Right Shift Operator ( >> ), https://tc39.es/ecma262/#sec-signed-right-shift-operator
  899. ThrowCompletionOr<Value> right_shift(GlobalObject& global_object, Value lhs, Value rhs)
  900. {
  901. auto& vm = global_object.vm();
  902. auto lhs_numeric = TRY(lhs.to_numeric(global_object));
  903. auto rhs_numeric = TRY(rhs.to_numeric(global_object));
  904. if (both_number(lhs_numeric, rhs_numeric)) {
  905. if (!lhs_numeric.is_finite_number())
  906. return Value(0);
  907. if (!rhs_numeric.is_finite_number())
  908. return lhs_numeric;
  909. auto lhs_i32 = MUST(lhs_numeric.to_i32(global_object));
  910. auto rhs_u32 = MUST(rhs_numeric.to_u32(global_object)) % 32;
  911. return Value(lhs_i32 >> rhs_u32);
  912. }
  913. if (both_bigint(lhs_numeric, rhs_numeric)) {
  914. auto rhs_negated = rhs_numeric.as_bigint().big_integer();
  915. rhs_negated.negate();
  916. return left_shift(global_object, lhs, js_bigint(vm, rhs_negated));
  917. }
  918. return vm.throw_completion<TypeError>(global_object, ErrorType::BigIntBadOperatorOtherType, "right-shift");
  919. }
  920. // 13.9.3 The Unsigned Right Shift Operator ( >>> ), https://tc39.es/ecma262/#sec-unsigned-right-shift-operator
  921. ThrowCompletionOr<Value> unsigned_right_shift(GlobalObject& global_object, Value lhs, Value rhs)
  922. {
  923. auto& vm = global_object.vm();
  924. auto lhs_numeric = TRY(lhs.to_numeric(global_object));
  925. auto rhs_numeric = TRY(rhs.to_numeric(global_object));
  926. if (both_number(lhs_numeric, rhs_numeric)) {
  927. if (!lhs_numeric.is_finite_number())
  928. return Value(0);
  929. if (!rhs_numeric.is_finite_number())
  930. return lhs_numeric;
  931. // Ok, so this performs toNumber() again but that "can't" throw
  932. auto lhs_u32 = MUST(lhs_numeric.to_u32(global_object));
  933. auto rhs_u32 = MUST(rhs_numeric.to_u32(global_object)) % 32;
  934. return Value(lhs_u32 >> rhs_u32);
  935. }
  936. return vm.throw_completion<TypeError>(global_object, ErrorType::BigIntBadOperator, "unsigned right-shift");
  937. }
  938. // https://tc39.es/ecma262/#string-concatenation
  939. static PrimitiveString* concatenate_strings(GlobalObject& global_object, PrimitiveString const& lhs, PrimitiveString const& rhs)
  940. {
  941. auto& vm = global_object.vm();
  942. if (lhs.has_utf16_string() && rhs.has_utf16_string()) {
  943. auto const& lhs_string = lhs.utf16_string();
  944. auto const& rhs_string = rhs.utf16_string();
  945. Vector<u16, 1> combined;
  946. combined.ensure_capacity(lhs_string.length_in_code_units() + rhs_string.length_in_code_units());
  947. combined.extend(lhs_string.string());
  948. combined.extend(rhs_string.string());
  949. return js_string(vm, Utf16String(move(combined)));
  950. }
  951. auto const& lhs_string = lhs.string();
  952. auto const& rhs_string = rhs.string();
  953. StringBuilder builder(lhs_string.length() + rhs_string.length());
  954. auto return_combined_strings = [&]() {
  955. builder.append(lhs_string);
  956. builder.append(rhs_string);
  957. return js_string(vm, builder.to_string());
  958. };
  959. // Surrogates encoded as UTF-8 are 3 bytes.
  960. if ((lhs_string.length() < 3) || (rhs_string.length() < 3))
  961. return return_combined_strings();
  962. auto lhs_leading_byte = static_cast<u8>(lhs_string[lhs_string.length() - 3]);
  963. auto rhs_leading_byte = static_cast<u8>(rhs_string[0]);
  964. if ((lhs_leading_byte & 0xf0) != 0xe0)
  965. return return_combined_strings();
  966. if ((rhs_leading_byte & 0xf0) != 0xe0)
  967. return return_combined_strings();
  968. auto high_surrogate = *Utf8View(lhs_string.substring_view(lhs_string.length() - 3)).begin();
  969. auto low_surrogate = *Utf8View(rhs_string).begin();
  970. if (!Utf16View::is_high_surrogate(high_surrogate) || !Utf16View::is_low_surrogate(low_surrogate))
  971. return return_combined_strings();
  972. builder.append(lhs_string.substring_view(0, lhs_string.length() - 3));
  973. builder.append_code_point(Utf16View::decode_surrogate_pair(high_surrogate, low_surrogate));
  974. builder.append(rhs_string.substring_view(3));
  975. return js_string(vm, builder.to_string());
  976. }
  977. // 13.8.1 The Addition Operator ( + ), https://tc39.es/ecma262/#sec-addition-operator-plus
  978. ThrowCompletionOr<Value> add(GlobalObject& global_object, Value lhs, Value rhs)
  979. {
  980. if (both_number(lhs, rhs)) {
  981. if (lhs.type() == Value::Type::Int32 && rhs.type() == Value::Type::Int32) {
  982. Checked<i32> result;
  983. result = MUST(lhs.to_i32(global_object));
  984. result += MUST(rhs.to_i32(global_object));
  985. if (!result.has_overflow())
  986. return Value(result.value());
  987. }
  988. return Value(lhs.as_double() + rhs.as_double());
  989. }
  990. auto& vm = global_object.vm();
  991. auto lhs_primitive = TRY(lhs.to_primitive(global_object));
  992. auto rhs_primitive = TRY(rhs.to_primitive(global_object));
  993. if (lhs_primitive.is_string() || rhs_primitive.is_string()) {
  994. auto lhs_string = TRY(lhs_primitive.to_primitive_string(global_object));
  995. auto rhs_string = TRY(rhs_primitive.to_primitive_string(global_object));
  996. return concatenate_strings(global_object, *lhs_string, *rhs_string);
  997. }
  998. auto lhs_numeric = TRY(lhs_primitive.to_numeric(global_object));
  999. auto rhs_numeric = TRY(rhs_primitive.to_numeric(global_object));
  1000. if (both_number(lhs_numeric, rhs_numeric))
  1001. return Value(lhs_numeric.as_double() + rhs_numeric.as_double());
  1002. if (both_bigint(lhs_numeric, rhs_numeric))
  1003. return Value(js_bigint(vm, lhs_numeric.as_bigint().big_integer().plus(rhs_numeric.as_bigint().big_integer())));
  1004. return vm.throw_completion<TypeError>(global_object, ErrorType::BigIntBadOperatorOtherType, "addition");
  1005. }
  1006. // 13.8.2 The Subtraction Operator ( - ), https://tc39.es/ecma262/#sec-subtraction-operator-minus
  1007. ThrowCompletionOr<Value> sub(GlobalObject& global_object, Value lhs, Value rhs)
  1008. {
  1009. auto& vm = global_object.vm();
  1010. auto lhs_numeric = TRY(lhs.to_numeric(global_object));
  1011. auto rhs_numeric = TRY(rhs.to_numeric(global_object));
  1012. if (both_number(lhs_numeric, rhs_numeric))
  1013. return Value(lhs_numeric.as_double() - rhs_numeric.as_double());
  1014. if (both_bigint(lhs_numeric, rhs_numeric))
  1015. return Value(js_bigint(vm, lhs_numeric.as_bigint().big_integer().minus(rhs_numeric.as_bigint().big_integer())));
  1016. return vm.throw_completion<TypeError>(global_object, ErrorType::BigIntBadOperatorOtherType, "subtraction");
  1017. }
  1018. // 13.7 Multiplicative Operators, https://tc39.es/ecma262/#sec-multiplicative-operators
  1019. ThrowCompletionOr<Value> mul(GlobalObject& global_object, Value lhs, Value rhs)
  1020. {
  1021. auto& vm = global_object.vm();
  1022. auto lhs_numeric = TRY(lhs.to_numeric(global_object));
  1023. auto rhs_numeric = TRY(rhs.to_numeric(global_object));
  1024. if (both_number(lhs_numeric, rhs_numeric))
  1025. return Value(lhs_numeric.as_double() * rhs_numeric.as_double());
  1026. if (both_bigint(lhs_numeric, rhs_numeric))
  1027. return Value(js_bigint(vm, lhs_numeric.as_bigint().big_integer().multiplied_by(rhs_numeric.as_bigint().big_integer())));
  1028. return vm.throw_completion<TypeError>(global_object, ErrorType::BigIntBadOperatorOtherType, "multiplication");
  1029. }
  1030. // 13.7 Multiplicative Operators, https://tc39.es/ecma262/#sec-multiplicative-operators
  1031. ThrowCompletionOr<Value> div(GlobalObject& global_object, Value lhs, Value rhs)
  1032. {
  1033. auto& vm = global_object.vm();
  1034. auto lhs_numeric = TRY(lhs.to_numeric(global_object));
  1035. auto rhs_numeric = TRY(rhs.to_numeric(global_object));
  1036. if (both_number(lhs_numeric, rhs_numeric))
  1037. return Value(lhs_numeric.as_double() / rhs_numeric.as_double());
  1038. if (both_bigint(lhs_numeric, rhs_numeric)) {
  1039. if (rhs_numeric.as_bigint().big_integer() == BIGINT_ZERO)
  1040. return vm.throw_completion<RangeError>(global_object, ErrorType::DivisionByZero);
  1041. return Value(js_bigint(vm, lhs_numeric.as_bigint().big_integer().divided_by(rhs_numeric.as_bigint().big_integer()).quotient));
  1042. }
  1043. return vm.throw_completion<TypeError>(global_object, ErrorType::BigIntBadOperatorOtherType, "division");
  1044. }
  1045. // 13.7 Multiplicative Operators, https://tc39.es/ecma262/#sec-multiplicative-operators
  1046. ThrowCompletionOr<Value> mod(GlobalObject& global_object, Value lhs, Value rhs)
  1047. {
  1048. auto& vm = global_object.vm();
  1049. auto lhs_numeric = TRY(lhs.to_numeric(global_object));
  1050. auto rhs_numeric = TRY(rhs.to_numeric(global_object));
  1051. if (both_number(lhs_numeric, rhs_numeric)) {
  1052. // 6.1.6.1.6 Number::remainder ( n, d ), https://tc39.es/ecma262/#sec-numeric-types-number-remainder
  1053. // The ECMA specification is describing the mathematical definition of modulus
  1054. // implemented by fmod.
  1055. auto n = lhs_numeric.as_double();
  1056. auto d = rhs_numeric.as_double();
  1057. return Value(fmod(n, d));
  1058. }
  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()).remainder));
  1063. }
  1064. return vm.throw_completion<TypeError>(global_object, ErrorType::BigIntBadOperatorOtherType, "modulo");
  1065. }
  1066. static Value exp_double(Value base, Value exponent)
  1067. {
  1068. VERIFY(both_number(base, exponent));
  1069. if (exponent.is_nan())
  1070. return js_nan();
  1071. if (exponent.is_positive_zero() || exponent.is_negative_zero())
  1072. return Value(1);
  1073. if (base.is_nan())
  1074. return js_nan();
  1075. if (base.is_positive_infinity())
  1076. return exponent.as_double() > 0 ? js_infinity() : Value(0);
  1077. if (base.is_negative_infinity()) {
  1078. auto is_odd_integral_number = exponent.is_integral_number() && (exponent.as_i32() % 2 != 0);
  1079. if (exponent.as_double() > 0)
  1080. return is_odd_integral_number ? js_negative_infinity() : js_infinity();
  1081. else
  1082. return is_odd_integral_number ? Value(-0.0) : Value(0);
  1083. }
  1084. if (base.is_positive_zero())
  1085. return exponent.as_double() > 0 ? Value(0) : js_infinity();
  1086. if (base.is_negative_zero()) {
  1087. auto is_odd_integral_number = exponent.is_integral_number() && (exponent.as_i32() % 2 != 0);
  1088. if (exponent.as_double() > 0)
  1089. return is_odd_integral_number ? Value(-0.0) : Value(0);
  1090. else
  1091. return is_odd_integral_number ? js_negative_infinity() : js_infinity();
  1092. }
  1093. VERIFY(base.is_finite_number() && !base.is_positive_zero() && !base.is_negative_zero());
  1094. if (exponent.is_positive_infinity()) {
  1095. auto absolute_base = fabs(base.as_double());
  1096. if (absolute_base > 1)
  1097. return js_infinity();
  1098. else if (absolute_base == 1)
  1099. return js_nan();
  1100. else if (absolute_base < 1)
  1101. return Value(0);
  1102. }
  1103. if (exponent.is_negative_infinity()) {
  1104. auto absolute_base = fabs(base.as_double());
  1105. if (absolute_base > 1)
  1106. return Value(0);
  1107. else if (absolute_base == 1)
  1108. return js_nan();
  1109. else if (absolute_base < 1)
  1110. return js_infinity();
  1111. }
  1112. VERIFY(exponent.is_finite_number() && !exponent.is_positive_zero() && !exponent.is_negative_zero());
  1113. if (base.as_double() < 0 && !exponent.is_integral_number())
  1114. return js_nan();
  1115. return Value(::pow(base.as_double(), exponent.as_double()));
  1116. }
  1117. // 13.6 Exponentiation Operator, https://tc39.es/ecma262/#sec-exp-operator
  1118. ThrowCompletionOr<Value> exp(GlobalObject& global_object, Value lhs, Value rhs)
  1119. {
  1120. auto& vm = global_object.vm();
  1121. auto lhs_numeric = TRY(lhs.to_numeric(global_object));
  1122. auto rhs_numeric = TRY(rhs.to_numeric(global_object));
  1123. if (both_number(lhs_numeric, rhs_numeric))
  1124. return exp_double(lhs_numeric, rhs_numeric);
  1125. if (both_bigint(lhs_numeric, rhs_numeric)) {
  1126. if (rhs_numeric.as_bigint().big_integer().is_negative())
  1127. return vm.throw_completion<RangeError>(global_object, ErrorType::NegativeExponent);
  1128. return Value(js_bigint(vm, Crypto::NumberTheory::Power(lhs_numeric.as_bigint().big_integer(), rhs_numeric.as_bigint().big_integer())));
  1129. }
  1130. return vm.throw_completion<TypeError>(global_object, ErrorType::BigIntBadOperatorOtherType, "exponentiation");
  1131. }
  1132. ThrowCompletionOr<Value> in(GlobalObject& global_object, Value lhs, Value rhs)
  1133. {
  1134. if (!rhs.is_object())
  1135. return global_object.vm().throw_completion<TypeError>(global_object, ErrorType::InOperatorWithObject);
  1136. auto lhs_property_key = TRY(lhs.to_property_key(global_object));
  1137. return Value(TRY(rhs.as_object().has_property(lhs_property_key)));
  1138. }
  1139. // 13.10.2 InstanceofOperator ( V, target ), https://tc39.es/ecma262/#sec-instanceofoperator
  1140. ThrowCompletionOr<Value> instance_of(GlobalObject& global_object, Value lhs, Value rhs)
  1141. {
  1142. auto& vm = global_object.vm();
  1143. if (!rhs.is_object())
  1144. return vm.throw_completion<TypeError>(global_object, ErrorType::NotAnObject, rhs.to_string_without_side_effects());
  1145. auto has_instance_method = TRY(rhs.get_method(global_object, *vm.well_known_symbol_has_instance()));
  1146. if (has_instance_method) {
  1147. auto has_instance_result = TRY(call(global_object, *has_instance_method, rhs, lhs));
  1148. return Value(has_instance_result.to_boolean());
  1149. }
  1150. if (!rhs.is_function())
  1151. return vm.throw_completion<TypeError>(global_object, ErrorType::NotAFunction, rhs.to_string_without_side_effects());
  1152. return TRY(ordinary_has_instance(global_object, lhs, rhs));
  1153. }
  1154. // 7.3.22 OrdinaryHasInstance ( C, O ), https://tc39.es/ecma262/#sec-ordinaryhasinstance
  1155. ThrowCompletionOr<Value> ordinary_has_instance(GlobalObject& global_object, Value lhs, Value rhs)
  1156. {
  1157. auto& vm = global_object.vm();
  1158. if (!rhs.is_function())
  1159. return Value(false);
  1160. auto& rhs_function = rhs.as_function();
  1161. if (is<BoundFunction>(rhs_function)) {
  1162. auto& bound_target = static_cast<BoundFunction const&>(rhs_function);
  1163. return instance_of(global_object, lhs, Value(&bound_target.bound_target_function()));
  1164. }
  1165. if (!lhs.is_object())
  1166. return Value(false);
  1167. Object* lhs_object = &lhs.as_object();
  1168. auto rhs_prototype = TRY(rhs_function.get(vm.names.prototype));
  1169. if (!rhs_prototype.is_object())
  1170. return vm.throw_completion<TypeError>(global_object, ErrorType::InstanceOfOperatorBadPrototype, rhs.to_string_without_side_effects());
  1171. while (true) {
  1172. lhs_object = TRY(lhs_object->internal_get_prototype_of());
  1173. if (!lhs_object)
  1174. return Value(false);
  1175. if (same_value(rhs_prototype, lhs_object))
  1176. return Value(true);
  1177. }
  1178. }
  1179. // 7.2.10 SameValue ( x, y ), https://tc39.es/ecma262/#sec-samevalue
  1180. bool same_value(Value lhs, Value rhs)
  1181. {
  1182. if (!same_type_for_equality(lhs, rhs))
  1183. return false;
  1184. if (lhs.is_number()) {
  1185. if (lhs.is_nan() && rhs.is_nan())
  1186. return true;
  1187. if (lhs.is_positive_zero() && rhs.is_negative_zero())
  1188. return false;
  1189. if (lhs.is_negative_zero() && rhs.is_positive_zero())
  1190. return false;
  1191. return lhs.as_double() == rhs.as_double();
  1192. }
  1193. if (lhs.is_bigint()) {
  1194. auto lhs_big_integer = lhs.as_bigint().big_integer();
  1195. auto rhs_big_integer = rhs.as_bigint().big_integer();
  1196. if (lhs_big_integer == BIGINT_ZERO && rhs_big_integer == BIGINT_ZERO && lhs_big_integer.is_negative() != rhs_big_integer.is_negative())
  1197. return false;
  1198. return lhs_big_integer == rhs_big_integer;
  1199. }
  1200. return same_value_non_numeric(lhs, rhs);
  1201. }
  1202. // 7.2.11 SameValueZero ( x, y ), https://tc39.es/ecma262/#sec-samevaluezero
  1203. bool same_value_zero(Value lhs, Value rhs)
  1204. {
  1205. if (!same_type_for_equality(lhs, rhs))
  1206. return false;
  1207. if (lhs.is_number()) {
  1208. if (lhs.is_nan() && rhs.is_nan())
  1209. return true;
  1210. return lhs.as_double() == rhs.as_double();
  1211. }
  1212. if (lhs.is_bigint())
  1213. return lhs.as_bigint().big_integer() == rhs.as_bigint().big_integer();
  1214. return same_value_non_numeric(lhs, rhs);
  1215. }
  1216. // 7.2.12 SameValueNonNumeric ( x, y ), https://tc39.es/ecma262/#sec-samevaluenonnumeric
  1217. bool same_value_non_numeric(Value lhs, Value rhs)
  1218. {
  1219. VERIFY(!lhs.is_number() && !lhs.is_bigint());
  1220. VERIFY(same_type_for_equality(lhs, rhs));
  1221. switch (lhs.type()) {
  1222. case Value::Type::Undefined:
  1223. case Value::Type::Null:
  1224. return true;
  1225. case Value::Type::String:
  1226. return lhs.as_string().string() == rhs.as_string().string();
  1227. case Value::Type::Symbol:
  1228. return &lhs.as_symbol() == &rhs.as_symbol();
  1229. case Value::Type::Boolean:
  1230. return lhs.as_bool() == rhs.as_bool();
  1231. case Value::Type::Object:
  1232. return &lhs.as_object() == &rhs.as_object();
  1233. default:
  1234. VERIFY_NOT_REACHED();
  1235. }
  1236. }
  1237. // 7.2.15 IsStrictlyEqual ( x, y ), https://tc39.es/ecma262/#sec-isstrictlyequal
  1238. bool is_strictly_equal(Value lhs, Value rhs)
  1239. {
  1240. if (!same_type_for_equality(lhs, rhs))
  1241. return false;
  1242. if (lhs.is_number()) {
  1243. if (lhs.is_nan() || rhs.is_nan())
  1244. return false;
  1245. if (lhs.as_double() == rhs.as_double())
  1246. return true;
  1247. return false;
  1248. }
  1249. if (lhs.is_bigint())
  1250. return lhs.as_bigint().big_integer() == rhs.as_bigint().big_integer();
  1251. return same_value_non_numeric(lhs, rhs);
  1252. }
  1253. // 7.2.14 IsLooselyEqual ( x, y ), https://tc39.es/ecma262/#sec-islooselyequal
  1254. ThrowCompletionOr<bool> is_loosely_equal(GlobalObject& global_object, Value lhs, Value rhs)
  1255. {
  1256. // 1. If Type(x) is the same as Type(y), then
  1257. if (same_type_for_equality(lhs, rhs)) {
  1258. // a. Return IsStrictlyEqual(x, y).
  1259. return is_strictly_equal(lhs, rhs);
  1260. }
  1261. // 2. If x is null and y is undefined, return true.
  1262. // 3. If x is undefined and y is null, return true.
  1263. if (lhs.is_nullish() && rhs.is_nullish())
  1264. return true;
  1265. // 4. NOTE: This step is replaced in section B.3.6.2.
  1266. // B.3.6.2 Changes to IsLooselyEqual, https://tc39.es/ecma262/#sec-IsHTMLDDA-internal-slot-aec
  1267. // 4. Perform the following steps:
  1268. // a. If Type(x) is Object and x has an [[IsHTMLDDA]] internal slot and y is either null or undefined, return true.
  1269. if (lhs.is_object() && lhs.as_object().is_htmldda() && rhs.is_nullish())
  1270. return true;
  1271. // b. If x is either null or undefined and Type(y) is Object and y has an [[IsHTMLDDA]] internal slot, return true.
  1272. if (lhs.is_nullish() && rhs.is_object() && rhs.as_object().is_htmldda())
  1273. return true;
  1274. // == End of B.3.6.2 ==
  1275. // 5. If Type(x) is Number and Type(y) is String, return ! IsLooselyEqual(x, ! ToNumber(y)).
  1276. if (lhs.is_number() && rhs.is_string())
  1277. return is_loosely_equal(global_object, lhs, MUST(rhs.to_number(global_object)));
  1278. // 6. If Type(x) is String and Type(y) is Number, return ! IsLooselyEqual(! ToNumber(x), y).
  1279. if (lhs.is_string() && rhs.is_number())
  1280. return is_loosely_equal(global_object, MUST(lhs.to_number(global_object)), rhs);
  1281. // 7. If Type(x) is BigInt and Type(y) is String, then
  1282. if (lhs.is_bigint() && rhs.is_string()) {
  1283. // a. Let n be StringToBigInt(y).
  1284. auto bigint = rhs.string_to_bigint(global_object);
  1285. // b. If n is undefined, return false.
  1286. if (!bigint.has_value())
  1287. return false;
  1288. // c. Return ! IsLooselyEqual(x, n).
  1289. return is_loosely_equal(global_object, lhs, *bigint);
  1290. }
  1291. // 8. If Type(x) is String and Type(y) is BigInt, return ! IsLooselyEqual(y, x).
  1292. if (lhs.is_string() && rhs.is_bigint())
  1293. return is_loosely_equal(global_object, rhs, lhs);
  1294. // 9. If Type(x) is Boolean, return ! IsLooselyEqual(! ToNumber(x), y).
  1295. if (lhs.is_boolean())
  1296. return is_loosely_equal(global_object, MUST(lhs.to_number(global_object)), rhs);
  1297. // 10. If Type(y) is Boolean, return ! IsLooselyEqual(x, ! ToNumber(y)).
  1298. if (rhs.is_boolean())
  1299. return is_loosely_equal(global_object, lhs, MUST(rhs.to_number(global_object)));
  1300. // 11. If Type(x) is either String, Number, BigInt, or Symbol and Type(y) is Object, return ! IsLooselyEqual(x, ? ToPrimitive(y)).
  1301. if ((lhs.is_string() || lhs.is_number() || lhs.is_bigint() || lhs.is_symbol()) && rhs.is_object()) {
  1302. auto rhs_primitive = TRY(rhs.to_primitive(global_object));
  1303. return is_loosely_equal(global_object, lhs, rhs_primitive);
  1304. }
  1305. // 12. If Type(x) is Object and Type(y) is either String, Number, BigInt, or Symbol, return ! IsLooselyEqual(? ToPrimitive(x), y).
  1306. if (lhs.is_object() && (rhs.is_string() || rhs.is_number() || rhs.is_bigint() || rhs.is_symbol())) {
  1307. auto lhs_primitive = TRY(lhs.to_primitive(global_object));
  1308. return is_loosely_equal(global_object, lhs_primitive, rhs);
  1309. }
  1310. // 13. If Type(x) is BigInt and Type(y) is Number, or if Type(x) is Number and Type(y) is BigInt, then
  1311. if ((lhs.is_bigint() && rhs.is_number()) || (lhs.is_number() && rhs.is_bigint())) {
  1312. // a. If x or y are any of NaN, +∞𝔽, or -∞𝔽, return false.
  1313. if (lhs.is_nan() || lhs.is_infinity() || rhs.is_nan() || rhs.is_infinity())
  1314. return false;
  1315. // b. If ℝ(x) = ℝ(y), return true; otherwise return false.
  1316. if ((lhs.is_number() && !lhs.is_integral_number()) || (rhs.is_number() && !rhs.is_integral_number()))
  1317. return false;
  1318. if (lhs.is_number())
  1319. return Crypto::SignedBigInteger { MUST(lhs.to_i32(global_object)) } == rhs.as_bigint().big_integer();
  1320. else
  1321. return Crypto::SignedBigInteger { MUST(rhs.to_i32(global_object)) } == lhs.as_bigint().big_integer();
  1322. }
  1323. // 14. Return false.
  1324. return false;
  1325. }
  1326. // 7.2.13 IsLessThan ( x, y, LeftFirst ), https://tc39.es/ecma262/#sec-islessthan
  1327. ThrowCompletionOr<TriState> is_less_than(GlobalObject& global_object, bool left_first, Value lhs, Value rhs)
  1328. {
  1329. Value x_primitive;
  1330. Value y_primitive;
  1331. if (left_first) {
  1332. x_primitive = TRY(lhs.to_primitive(global_object, Value::PreferredType::Number));
  1333. y_primitive = TRY(rhs.to_primitive(global_object, Value::PreferredType::Number));
  1334. } else {
  1335. y_primitive = TRY(lhs.to_primitive(global_object, Value::PreferredType::Number));
  1336. x_primitive = TRY(rhs.to_primitive(global_object, Value::PreferredType::Number));
  1337. }
  1338. if (x_primitive.is_string() && y_primitive.is_string()) {
  1339. auto x_string = x_primitive.as_string().string();
  1340. auto y_string = y_primitive.as_string().string();
  1341. Utf8View x_code_points { x_string };
  1342. Utf8View y_code_points { y_string };
  1343. if (x_code_points.starts_with(y_code_points))
  1344. return TriState::False;
  1345. if (y_code_points.starts_with(x_code_points))
  1346. return TriState::True;
  1347. for (auto k = x_code_points.begin(), l = y_code_points.begin();
  1348. k != x_code_points.end() && l != y_code_points.end();
  1349. ++k, ++l) {
  1350. if (*k != *l) {
  1351. if (*k < *l) {
  1352. return TriState::True;
  1353. } else {
  1354. return TriState::False;
  1355. }
  1356. }
  1357. }
  1358. VERIFY_NOT_REACHED();
  1359. }
  1360. if (x_primitive.is_bigint() && y_primitive.is_string()) {
  1361. auto y_bigint = y_primitive.string_to_bigint(global_object);
  1362. if (!y_bigint.has_value())
  1363. return TriState::Unknown;
  1364. if (x_primitive.as_bigint().big_integer() < (*y_bigint)->big_integer())
  1365. return TriState::True;
  1366. return TriState::False;
  1367. }
  1368. if (x_primitive.is_string() && y_primitive.is_bigint()) {
  1369. auto x_bigint = x_primitive.string_to_bigint(global_object);
  1370. if (!x_bigint.has_value())
  1371. return TriState::Unknown;
  1372. if ((*x_bigint)->big_integer() < y_primitive.as_bigint().big_integer())
  1373. return TriState::True;
  1374. return TriState::False;
  1375. }
  1376. auto x_numeric = TRY(x_primitive.to_numeric(global_object));
  1377. auto y_numeric = TRY(y_primitive.to_numeric(global_object));
  1378. if (x_numeric.is_nan() || y_numeric.is_nan())
  1379. return TriState::Unknown;
  1380. if (x_numeric.is_positive_infinity() || y_numeric.is_negative_infinity())
  1381. return TriState::False;
  1382. if (x_numeric.is_negative_infinity() || y_numeric.is_positive_infinity())
  1383. return TriState::True;
  1384. if (x_numeric.is_number() && y_numeric.is_number()) {
  1385. if (x_numeric.as_double() < y_numeric.as_double())
  1386. return TriState::True;
  1387. else
  1388. return TriState::False;
  1389. }
  1390. if (x_numeric.is_bigint() && y_numeric.is_bigint()) {
  1391. if (x_numeric.as_bigint().big_integer() < y_numeric.as_bigint().big_integer())
  1392. return TriState::True;
  1393. else
  1394. return TriState::False;
  1395. }
  1396. VERIFY((x_numeric.is_number() && y_numeric.is_bigint()) || (x_numeric.is_bigint() && y_numeric.is_number()));
  1397. bool x_lower_than_y;
  1398. if (x_numeric.is_number()) {
  1399. x_lower_than_y = x_numeric.is_integral_number()
  1400. ? Crypto::SignedBigInteger { MUST(x_numeric.to_i32(global_object)) } < y_numeric.as_bigint().big_integer()
  1401. : (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());
  1402. } else {
  1403. x_lower_than_y = y_numeric.is_integral_number()
  1404. ? x_numeric.as_bigint().big_integer() < Crypto::SignedBigInteger { MUST(y_numeric.to_i32(global_object)) }
  1405. : (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 });
  1406. }
  1407. if (x_lower_than_y)
  1408. return TriState::True;
  1409. else
  1410. return TriState::False;
  1411. }
  1412. // 7.3.21 Invoke ( V, P [ , argumentsList ] ), https://tc39.es/ecma262/#sec-invoke
  1413. ThrowCompletionOr<Value> Value::invoke_internal(GlobalObject& global_object, PropertyKey const& property_key, Optional<MarkedVector<Value>> arguments)
  1414. {
  1415. auto& vm = global_object.vm();
  1416. auto property = TRY(get(global_object, property_key));
  1417. if (!property.is_function())
  1418. return vm.throw_completion<TypeError>(global_object, ErrorType::NotAFunction, property.to_string_without_side_effects());
  1419. return call(global_object, property.as_function(), *this, move(arguments));
  1420. }
  1421. }