Value.cpp 60 KB

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