Value.cpp 53 KB

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