Value.cpp 60 KB

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