Value.cpp 48 KB

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