Value.cpp 47 KB

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