Value.cpp 47 KB

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