Value.cpp 58 KB

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