Value.cpp 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2020, 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/FlyString.h>
  28. #include <AK/String.h>
  29. #include <AK/StringBuilder.h>
  30. #include <AK/Utf8View.h>
  31. #include <LibCrypto/BigInt/SignedBigInteger.h>
  32. #include <LibCrypto/NumberTheory/ModularFunctions.h>
  33. #include <LibJS/Heap/Heap.h>
  34. #include <LibJS/Runtime/Accessor.h>
  35. #include <LibJS/Runtime/Array.h>
  36. #include <LibJS/Runtime/BigInt.h>
  37. #include <LibJS/Runtime/BigIntObject.h>
  38. #include <LibJS/Runtime/BooleanObject.h>
  39. #include <LibJS/Runtime/BoundFunction.h>
  40. #include <LibJS/Runtime/Error.h>
  41. #include <LibJS/Runtime/Function.h>
  42. #include <LibJS/Runtime/GlobalObject.h>
  43. #include <LibJS/Runtime/NumberObject.h>
  44. #include <LibJS/Runtime/Object.h>
  45. #include <LibJS/Runtime/PrimitiveString.h>
  46. #include <LibJS/Runtime/StringObject.h>
  47. #include <LibJS/Runtime/Symbol.h>
  48. #include <LibJS/Runtime/SymbolObject.h>
  49. #include <LibJS/Runtime/Value.h>
  50. #include <ctype.h>
  51. #include <math.h>
  52. namespace JS {
  53. static const Crypto::SignedBigInteger BIGINT_ZERO { 0 };
  54. static bool is_valid_bigint_value(String string)
  55. {
  56. string = string.trim_whitespace();
  57. if (string.length() > 1 && (string[0] == '-' || string[0] == '+'))
  58. string = string.substring_view(1, string.length() - 1);
  59. for (auto& ch : string) {
  60. if (!isdigit(ch))
  61. return false;
  62. }
  63. return true;
  64. }
  65. ALWAYS_INLINE bool both_number(const Value& lhs, const Value& rhs)
  66. {
  67. return lhs.is_number() && rhs.is_number();
  68. }
  69. ALWAYS_INLINE bool both_bigint(const Value& lhs, const Value& rhs)
  70. {
  71. return lhs.is_bigint() && rhs.is_bigint();
  72. }
  73. bool Value::is_array() const
  74. {
  75. return is_object() && as_object().is_array();
  76. }
  77. Array& Value::as_array()
  78. {
  79. ASSERT(is_array());
  80. return static_cast<Array&>(*m_value.as_object);
  81. }
  82. bool Value::is_function() const
  83. {
  84. return is_object() && as_object().is_function();
  85. }
  86. Function& Value::as_function()
  87. {
  88. ASSERT(is_function());
  89. return static_cast<Function&>(as_object());
  90. }
  91. String Value::to_string_without_side_effects() const
  92. {
  93. switch (m_type) {
  94. case Type::Undefined:
  95. return "undefined";
  96. case Type::Null:
  97. return "null";
  98. case Type::Boolean:
  99. return m_value.as_bool ? "true" : "false";
  100. case Type::Number:
  101. if (is_nan())
  102. return "NaN";
  103. if (is_infinity())
  104. return is_negative_infinity() ? "-Infinity" : "Infinity";
  105. if (is_integer())
  106. return String::number(as_i32());
  107. return String::format("%.4f", m_value.as_double);
  108. case Type::String:
  109. return m_value.as_string->string();
  110. case Type::Symbol:
  111. return m_value.as_symbol->to_string();
  112. case Type::BigInt:
  113. return m_value.as_bigint->to_string();
  114. case Type::Object:
  115. return String::format("[object %s]", as_object().class_name());
  116. case Type::Accessor:
  117. return "<accessor>";
  118. case Type::NativeProperty:
  119. return "<native-property>";
  120. default:
  121. ASSERT_NOT_REACHED();
  122. }
  123. }
  124. PrimitiveString* Value::to_primitive_string(GlobalObject& global_object)
  125. {
  126. if (is_string())
  127. return &as_string();
  128. auto string = to_string(global_object);
  129. if (global_object.vm().exception())
  130. return nullptr;
  131. return js_string(global_object.heap(), string);
  132. }
  133. String Value::to_string(GlobalObject& global_object) const
  134. {
  135. switch (m_type) {
  136. case Type::Undefined:
  137. return "undefined";
  138. case Type::Null:
  139. return "null";
  140. case Type::Boolean:
  141. return m_value.as_bool ? "true" : "false";
  142. case Type::Number:
  143. if (is_nan())
  144. return "NaN";
  145. if (is_infinity())
  146. return is_negative_infinity() ? "-Infinity" : "Infinity";
  147. if (is_integer())
  148. return String::number(as_i32());
  149. return String::format("%.4f", m_value.as_double);
  150. case Type::String:
  151. return m_value.as_string->string();
  152. case Type::Symbol:
  153. global_object.vm().throw_exception<TypeError>(global_object, ErrorType::Convert, "symbol", "string");
  154. return {};
  155. case Type::BigInt:
  156. return m_value.as_bigint->big_integer().to_base10();
  157. case Type::Object: {
  158. auto primitive_value = as_object().to_primitive(PreferredType::String);
  159. if (global_object.vm().exception())
  160. return {};
  161. return primitive_value.to_string(global_object);
  162. }
  163. default:
  164. ASSERT_NOT_REACHED();
  165. }
  166. }
  167. bool Value::to_boolean() const
  168. {
  169. switch (m_type) {
  170. case Type::Undefined:
  171. case Type::Null:
  172. return false;
  173. case Type::Boolean:
  174. return m_value.as_bool;
  175. case Type::Number:
  176. if (is_nan())
  177. return false;
  178. return m_value.as_double != 0;
  179. case Type::String:
  180. return !m_value.as_string->string().is_empty();
  181. case Type::Symbol:
  182. return true;
  183. case Type::BigInt:
  184. return m_value.as_bigint->big_integer() != BIGINT_ZERO;
  185. case Type::Object:
  186. return true;
  187. default:
  188. ASSERT_NOT_REACHED();
  189. }
  190. }
  191. Value Value::to_primitive(PreferredType preferred_type) const
  192. {
  193. if (is_object())
  194. return as_object().to_primitive(preferred_type);
  195. return *this;
  196. }
  197. Object* Value::to_object(GlobalObject& global_object) const
  198. {
  199. switch (m_type) {
  200. case Type::Undefined:
  201. case Type::Null:
  202. global_object.vm().throw_exception<TypeError>(global_object, ErrorType::ToObjectNullOrUndef);
  203. return nullptr;
  204. case Type::Boolean:
  205. return BooleanObject::create(global_object, m_value.as_bool);
  206. case Type::Number:
  207. return NumberObject::create(global_object, m_value.as_double);
  208. case Type::String:
  209. return StringObject::create(global_object, *m_value.as_string);
  210. case Type::Symbol:
  211. return SymbolObject::create(global_object, *m_value.as_symbol);
  212. case Type::BigInt:
  213. return BigIntObject::create(global_object, *m_value.as_bigint);
  214. case Type::Object:
  215. return &const_cast<Object&>(as_object());
  216. default:
  217. dbg() << "Dying because I can't to_object() on " << *this;
  218. ASSERT_NOT_REACHED();
  219. }
  220. }
  221. Value Value::to_numeric(GlobalObject& global_object) const
  222. {
  223. auto primitive = to_primitive(Value::PreferredType::Number);
  224. if (global_object.vm().exception())
  225. return {};
  226. if (primitive.is_bigint())
  227. return primitive;
  228. return primitive.to_number(global_object);
  229. }
  230. Value Value::to_number(GlobalObject& global_object) const
  231. {
  232. switch (m_type) {
  233. case Type::Undefined:
  234. return js_nan();
  235. case Type::Null:
  236. return Value(0);
  237. case Type::Boolean:
  238. return Value(m_value.as_bool ? 1 : 0);
  239. case Type::Number:
  240. return Value(m_value.as_double);
  241. case Type::String: {
  242. auto string = as_string().string().trim_whitespace();
  243. if (string.is_empty())
  244. return Value(0);
  245. if (string == "Infinity" || string == "+Infinity")
  246. return js_infinity();
  247. if (string == "-Infinity")
  248. return js_negative_infinity();
  249. char* endptr;
  250. auto parsed_double = strtod(string.characters(), &endptr);
  251. if (*endptr)
  252. return js_nan();
  253. return Value(parsed_double);
  254. }
  255. case Type::Symbol:
  256. global_object.vm().throw_exception<TypeError>(global_object, ErrorType::Convert, "symbol", "number");
  257. return {};
  258. case Type::BigInt:
  259. global_object.vm().throw_exception<TypeError>(global_object, ErrorType::Convert, "BigInt", "number");
  260. return {};
  261. case Type::Object: {
  262. auto primitive = m_value.as_object->to_primitive(PreferredType::Number);
  263. if (global_object.vm().exception())
  264. return {};
  265. return primitive.to_number(global_object);
  266. }
  267. default:
  268. ASSERT_NOT_REACHED();
  269. }
  270. }
  271. BigInt* Value::to_bigint(GlobalObject& global_object) const
  272. {
  273. auto& vm = global_object.vm();
  274. auto primitive = to_primitive(PreferredType::Number);
  275. if (vm.exception())
  276. return nullptr;
  277. switch (primitive.type()) {
  278. case Type::Undefined:
  279. vm.throw_exception<TypeError>(global_object, ErrorType::Convert, "undefined", "BigInt");
  280. return nullptr;
  281. case Type::Null:
  282. vm.throw_exception<TypeError>(global_object, ErrorType::Convert, "null", "BigInt");
  283. return nullptr;
  284. case Type::Boolean: {
  285. auto value = primitive.as_bool() ? 1 : 0;
  286. return js_bigint(vm.heap(), Crypto::SignedBigInteger { value });
  287. }
  288. case Type::BigInt:
  289. return &primitive.as_bigint();
  290. case Type::Number:
  291. vm.throw_exception<TypeError>(global_object, ErrorType::Convert, "number", "BigInt");
  292. return {};
  293. case Type::String: {
  294. auto& string = primitive.as_string().string();
  295. if (!is_valid_bigint_value(string)) {
  296. vm.throw_exception<SyntaxError>(global_object, ErrorType::BigIntInvalidValue, string.characters());
  297. return {};
  298. }
  299. return js_bigint(vm.heap(), Crypto::SignedBigInteger::from_base10(string.trim_whitespace()));
  300. }
  301. case Type::Symbol:
  302. vm.throw_exception<TypeError>(global_object, ErrorType::Convert, "symbol", "BigInt");
  303. return {};
  304. default:
  305. ASSERT_NOT_REACHED();
  306. }
  307. }
  308. i32 Value::as_i32() const
  309. {
  310. return static_cast<i32>(as_double());
  311. }
  312. size_t Value::as_size_t() const
  313. {
  314. ASSERT(as_double() >= 0);
  315. return min((double)as_i32(), MAX_ARRAY_LIKE_INDEX);
  316. }
  317. double Value::to_double(GlobalObject& global_object) const
  318. {
  319. auto number = to_number(global_object);
  320. if (global_object.vm().exception())
  321. return 0;
  322. return number.as_double();
  323. }
  324. i32 Value::to_i32(GlobalObject& global_object) const
  325. {
  326. auto number = to_number(global_object);
  327. if (global_object.vm().exception())
  328. return 0;
  329. if (number.is_nan())
  330. return 0;
  331. // FIXME: What about infinity though - that's UB...
  332. // Maybe NumericLimits<i32>::max() for +Infinity and NumericLimits<i32>::min() for -Infinity?
  333. return number.as_i32();
  334. }
  335. size_t Value::to_size_t(GlobalObject& global_object) const
  336. {
  337. if (is_empty())
  338. return 0;
  339. auto number = to_number(global_object);
  340. if (global_object.vm().exception())
  341. return 0;
  342. if (number.is_nan())
  343. return 0;
  344. if (number.as_double() <= 0)
  345. return 0;
  346. return number.as_size_t();
  347. }
  348. Value greater_than(GlobalObject& global_object, Value lhs, Value rhs)
  349. {
  350. TriState relation = abstract_relation(global_object, false, lhs, rhs);
  351. if (relation == TriState::Unknown)
  352. return Value(false);
  353. return Value(relation == TriState::True);
  354. }
  355. Value greater_than_equals(GlobalObject& global_object, Value lhs, Value rhs)
  356. {
  357. TriState relation = abstract_relation(global_object, true, lhs, rhs);
  358. if (relation == TriState::Unknown || relation == TriState::True)
  359. return Value(false);
  360. return Value(true);
  361. }
  362. Value less_than(GlobalObject& global_object, Value lhs, Value rhs)
  363. {
  364. TriState relation = abstract_relation(global_object, true, lhs, rhs);
  365. if (relation == TriState::Unknown)
  366. return Value(false);
  367. return Value(relation == TriState::True);
  368. }
  369. Value less_than_equals(GlobalObject& global_object, Value lhs, Value rhs)
  370. {
  371. TriState relation = abstract_relation(global_object, false, lhs, rhs);
  372. if (relation == TriState::Unknown || relation == TriState::True)
  373. return Value(false);
  374. return Value(true);
  375. }
  376. Value bitwise_and(GlobalObject& global_object, Value lhs, Value rhs)
  377. {
  378. auto lhs_numeric = lhs.to_numeric(global_object.global_object());
  379. if (global_object.vm().exception())
  380. return {};
  381. auto rhs_numeric = rhs.to_numeric(global_object.global_object());
  382. if (global_object.vm().exception())
  383. return {};
  384. if (both_number(lhs_numeric, rhs_numeric)) {
  385. if (!lhs_numeric.is_finite_number() || !rhs_numeric.is_finite_number())
  386. return Value(0);
  387. return Value((i32)lhs_numeric.as_double() & (i32)rhs_numeric.as_double());
  388. }
  389. if (both_bigint(lhs_numeric, rhs_numeric))
  390. return js_bigint(global_object.heap(), lhs_numeric.as_bigint().big_integer().bitwise_and(rhs_numeric.as_bigint().big_integer()));
  391. global_object.vm().throw_exception<TypeError>(global_object.global_object(), ErrorType::BigIntBadOperatorOtherType, "bitwise AND");
  392. return {};
  393. }
  394. Value bitwise_or(GlobalObject& global_object, Value lhs, Value rhs)
  395. {
  396. auto lhs_numeric = lhs.to_numeric(global_object.global_object());
  397. if (global_object.vm().exception())
  398. return {};
  399. auto rhs_numeric = rhs.to_numeric(global_object.global_object());
  400. if (global_object.vm().exception())
  401. return {};
  402. if (both_number(lhs_numeric, rhs_numeric)) {
  403. if (!lhs_numeric.is_finite_number() && !rhs_numeric.is_finite_number())
  404. return Value(0);
  405. if (!lhs_numeric.is_finite_number())
  406. return rhs_numeric;
  407. if (!rhs_numeric.is_finite_number())
  408. return lhs_numeric;
  409. return Value((i32)lhs_numeric.as_double() | (i32)rhs_numeric.as_double());
  410. }
  411. if (both_bigint(lhs_numeric, rhs_numeric))
  412. return js_bigint(global_object.heap(), lhs_numeric.as_bigint().big_integer().bitwise_or(rhs_numeric.as_bigint().big_integer()));
  413. global_object.vm().throw_exception<TypeError>(global_object.global_object(), ErrorType::BigIntBadOperatorOtherType, "bitwise OR");
  414. return {};
  415. }
  416. Value bitwise_xor(GlobalObject& global_object, Value lhs, Value rhs)
  417. {
  418. auto lhs_numeric = lhs.to_numeric(global_object.global_object());
  419. if (global_object.vm().exception())
  420. return {};
  421. auto rhs_numeric = rhs.to_numeric(global_object.global_object());
  422. if (global_object.vm().exception())
  423. return {};
  424. if (both_number(lhs_numeric, rhs_numeric)) {
  425. if (!lhs_numeric.is_finite_number() && !rhs_numeric.is_finite_number())
  426. return Value(0);
  427. if (!lhs_numeric.is_finite_number())
  428. return rhs_numeric;
  429. if (!rhs_numeric.is_finite_number())
  430. return lhs_numeric;
  431. return Value((i32)lhs_numeric.as_double() ^ (i32)rhs_numeric.as_double());
  432. }
  433. if (both_bigint(lhs_numeric, rhs_numeric))
  434. return js_bigint(global_object.heap(), lhs_numeric.as_bigint().big_integer().bitwise_xor(rhs_numeric.as_bigint().big_integer()));
  435. global_object.vm().throw_exception<TypeError>(global_object.global_object(), ErrorType::BigIntBadOperatorOtherType, "bitwise XOR");
  436. return {};
  437. }
  438. Value bitwise_not(GlobalObject& global_object, Value lhs)
  439. {
  440. auto lhs_numeric = lhs.to_numeric(global_object.global_object());
  441. if (global_object.vm().exception())
  442. return {};
  443. if (lhs_numeric.is_number())
  444. return Value(~(i32)lhs_numeric.as_double());
  445. auto big_integer_bitwise_not = lhs_numeric.as_bigint().big_integer();
  446. big_integer_bitwise_not = big_integer_bitwise_not.plus(Crypto::SignedBigInteger { 1 });
  447. big_integer_bitwise_not.negate();
  448. return js_bigint(global_object.heap(), big_integer_bitwise_not);
  449. }
  450. Value unary_plus(GlobalObject& global_object, Value lhs)
  451. {
  452. return lhs.to_number(global_object.global_object());
  453. }
  454. Value unary_minus(GlobalObject& global_object, Value lhs)
  455. {
  456. auto lhs_numeric = lhs.to_numeric(global_object.global_object());
  457. if (global_object.vm().exception())
  458. return {};
  459. if (lhs_numeric.is_number()) {
  460. if (lhs_numeric.is_nan())
  461. return js_nan();
  462. return Value(-lhs_numeric.as_double());
  463. }
  464. if (lhs_numeric.as_bigint().big_integer() == BIGINT_ZERO)
  465. return js_bigint(global_object.heap(), BIGINT_ZERO);
  466. auto big_integer_negated = lhs_numeric.as_bigint().big_integer();
  467. big_integer_negated.negate();
  468. return js_bigint(global_object.heap(), big_integer_negated);
  469. }
  470. Value left_shift(GlobalObject& global_object, Value lhs, Value rhs)
  471. {
  472. auto lhs_numeric = lhs.to_numeric(global_object.global_object());
  473. if (global_object.vm().exception())
  474. return {};
  475. auto rhs_numeric = rhs.to_numeric(global_object.global_object());
  476. if (global_object.vm().exception())
  477. return {};
  478. if (both_number(lhs_numeric, rhs_numeric)) {
  479. if (!lhs_numeric.is_finite_number())
  480. return Value(0);
  481. if (!rhs_numeric.is_finite_number())
  482. return lhs_numeric;
  483. return Value((i32)lhs_numeric.as_double() << (i32)rhs_numeric.as_double());
  484. }
  485. if (both_bigint(lhs_numeric, rhs_numeric))
  486. TODO();
  487. global_object.vm().throw_exception<TypeError>(global_object.global_object(), ErrorType::BigIntBadOperatorOtherType, "left-shift");
  488. return {};
  489. }
  490. Value right_shift(GlobalObject& global_object, Value lhs, Value rhs)
  491. {
  492. auto lhs_numeric = lhs.to_numeric(global_object.global_object());
  493. if (global_object.vm().exception())
  494. return {};
  495. auto rhs_numeric = rhs.to_numeric(global_object.global_object());
  496. if (global_object.vm().exception())
  497. return {};
  498. if (both_number(lhs_numeric, rhs_numeric)) {
  499. if (!lhs_numeric.is_finite_number())
  500. return Value(0);
  501. if (!rhs_numeric.is_finite_number())
  502. return lhs_numeric;
  503. return Value((i32)lhs_numeric.as_double() >> (i32)rhs_numeric.as_double());
  504. }
  505. if (both_bigint(lhs_numeric, rhs_numeric))
  506. TODO();
  507. global_object.vm().throw_exception<TypeError>(global_object.global_object(), ErrorType::BigIntBadOperatorOtherType, "right-shift");
  508. return {};
  509. }
  510. Value unsigned_right_shift(GlobalObject& global_object, Value lhs, Value rhs)
  511. {
  512. auto lhs_numeric = lhs.to_numeric(global_object.global_object());
  513. if (global_object.vm().exception())
  514. return {};
  515. auto rhs_numeric = rhs.to_numeric(global_object.global_object());
  516. if (global_object.vm().exception())
  517. return {};
  518. if (both_number(lhs_numeric, rhs_numeric)) {
  519. if (!lhs_numeric.is_finite_number())
  520. return Value(0);
  521. if (!rhs_numeric.is_finite_number())
  522. return lhs_numeric;
  523. return Value((unsigned)lhs_numeric.as_double() >> (i32)rhs_numeric.as_double());
  524. }
  525. global_object.vm().throw_exception<TypeError>(global_object.global_object(), ErrorType::BigIntBadOperator, "unsigned right-shift");
  526. return {};
  527. }
  528. Value add(GlobalObject& global_object, Value lhs, Value rhs)
  529. {
  530. auto lhs_primitive = lhs.to_primitive();
  531. if (global_object.vm().exception())
  532. return {};
  533. auto rhs_primitive = rhs.to_primitive();
  534. if (global_object.vm().exception())
  535. return {};
  536. if (lhs_primitive.is_string() || rhs_primitive.is_string()) {
  537. auto lhs_string = lhs_primitive.to_string(global_object.global_object());
  538. if (global_object.vm().exception())
  539. return {};
  540. auto rhs_string = rhs_primitive.to_string(global_object.global_object());
  541. if (global_object.vm().exception())
  542. return {};
  543. StringBuilder builder(lhs_string.length() + rhs_string.length());
  544. builder.append(lhs_string);
  545. builder.append(rhs_string);
  546. return js_string(global_object.heap(), builder.to_string());
  547. }
  548. auto lhs_numeric = lhs_primitive.to_numeric(global_object.global_object());
  549. if (global_object.vm().exception())
  550. return {};
  551. auto rhs_numeric = rhs_primitive.to_numeric(global_object.global_object());
  552. if (global_object.vm().exception())
  553. return {};
  554. if (both_number(lhs_numeric, rhs_numeric))
  555. return Value(lhs_numeric.as_double() + rhs_numeric.as_double());
  556. if (both_bigint(lhs_numeric, rhs_numeric))
  557. return js_bigint(global_object.heap(), lhs_numeric.as_bigint().big_integer().plus(rhs_numeric.as_bigint().big_integer()));
  558. global_object.vm().throw_exception<TypeError>(global_object.global_object(), ErrorType::BigIntBadOperatorOtherType, "addition");
  559. return {};
  560. }
  561. Value sub(GlobalObject& global_object, Value lhs, Value rhs)
  562. {
  563. auto lhs_numeric = lhs.to_numeric(global_object.global_object());
  564. if (global_object.vm().exception())
  565. return {};
  566. auto rhs_numeric = rhs.to_numeric(global_object.global_object());
  567. if (global_object.vm().exception())
  568. return {};
  569. if (both_number(lhs_numeric, rhs_numeric))
  570. return Value(lhs_numeric.as_double() - rhs_numeric.as_double());
  571. if (both_bigint(lhs_numeric, rhs_numeric))
  572. return js_bigint(global_object.heap(), lhs_numeric.as_bigint().big_integer().minus(rhs_numeric.as_bigint().big_integer()));
  573. global_object.vm().throw_exception<TypeError>(global_object.global_object(), ErrorType::BigIntBadOperatorOtherType, "subtraction");
  574. return {};
  575. }
  576. Value mul(GlobalObject& global_object, Value lhs, Value rhs)
  577. {
  578. auto lhs_numeric = lhs.to_numeric(global_object.global_object());
  579. if (global_object.vm().exception())
  580. return {};
  581. auto rhs_numeric = rhs.to_numeric(global_object.global_object());
  582. if (global_object.vm().exception())
  583. return {};
  584. if (both_number(lhs_numeric, rhs_numeric))
  585. return Value(lhs_numeric.as_double() * rhs_numeric.as_double());
  586. if (both_bigint(lhs_numeric, rhs_numeric))
  587. return js_bigint(global_object.heap(), lhs_numeric.as_bigint().big_integer().multiplied_by(rhs_numeric.as_bigint().big_integer()));
  588. global_object.vm().throw_exception<TypeError>(global_object.global_object(), ErrorType::BigIntBadOperatorOtherType, "multiplication");
  589. return {};
  590. }
  591. Value div(GlobalObject& global_object, Value lhs, Value rhs)
  592. {
  593. auto lhs_numeric = lhs.to_numeric(global_object.global_object());
  594. if (global_object.vm().exception())
  595. return {};
  596. auto rhs_numeric = rhs.to_numeric(global_object.global_object());
  597. if (global_object.vm().exception())
  598. return {};
  599. if (both_number(lhs_numeric, rhs_numeric))
  600. return Value(lhs_numeric.as_double() / rhs_numeric.as_double());
  601. if (both_bigint(lhs_numeric, rhs_numeric))
  602. return js_bigint(global_object.heap(), lhs_numeric.as_bigint().big_integer().divided_by(rhs_numeric.as_bigint().big_integer()).quotient);
  603. global_object.vm().throw_exception<TypeError>(global_object.global_object(), ErrorType::BigIntBadOperatorOtherType, "division");
  604. return {};
  605. }
  606. Value mod(GlobalObject& global_object, Value lhs, Value rhs)
  607. {
  608. auto lhs_numeric = lhs.to_numeric(global_object.global_object());
  609. if (global_object.vm().exception())
  610. return {};
  611. auto rhs_numeric = rhs.to_numeric(global_object.global_object());
  612. if (global_object.vm().exception())
  613. return {};
  614. if (both_number(lhs_numeric, rhs_numeric)) {
  615. if (lhs_numeric.is_nan() || rhs_numeric.is_nan())
  616. return js_nan();
  617. auto index = lhs_numeric.as_double();
  618. auto period = rhs_numeric.as_double();
  619. auto trunc = (double)(i32)(index / period);
  620. return Value(index - trunc * period);
  621. }
  622. if (both_bigint(lhs_numeric, rhs_numeric))
  623. return js_bigint(global_object.heap(), lhs_numeric.as_bigint().big_integer().divided_by(rhs_numeric.as_bigint().big_integer()).remainder);
  624. global_object.vm().throw_exception<TypeError>(global_object.global_object(), ErrorType::BigIntBadOperatorOtherType, "modulo");
  625. return {};
  626. }
  627. Value exp(GlobalObject& global_object, Value lhs, Value rhs)
  628. {
  629. auto& vm = global_object.vm();
  630. auto lhs_numeric = lhs.to_numeric(global_object);
  631. if (vm.exception())
  632. return {};
  633. auto rhs_numeric = rhs.to_numeric(global_object);
  634. if (vm.exception())
  635. return {};
  636. if (both_number(lhs_numeric, rhs_numeric))
  637. return Value(pow(lhs_numeric.as_double(), rhs_numeric.as_double()));
  638. if (both_bigint(lhs_numeric, rhs_numeric))
  639. return js_bigint(vm.heap(), Crypto::NumberTheory::Power(lhs_numeric.as_bigint().big_integer(), rhs_numeric.as_bigint().big_integer()));
  640. vm.throw_exception<TypeError>(global_object, ErrorType::BigIntBadOperatorOtherType, "exponentiation");
  641. return {};
  642. }
  643. Value in(GlobalObject& global_object, Value lhs, Value rhs)
  644. {
  645. if (!rhs.is_object()) {
  646. global_object.vm().throw_exception<TypeError>(global_object.global_object(), ErrorType::InOperatorWithObject);
  647. return {};
  648. }
  649. auto lhs_string = lhs.to_string(global_object.global_object());
  650. if (global_object.vm().exception())
  651. return {};
  652. return Value(rhs.as_object().has_property(lhs_string));
  653. }
  654. Value instance_of(GlobalObject& global_object, Value lhs, Value rhs)
  655. {
  656. auto& vm = global_object.vm();
  657. if (!rhs.is_object()) {
  658. vm.throw_exception<TypeError>(global_object, ErrorType::NotAnObject, rhs.to_string_without_side_effects().characters());
  659. return {};
  660. }
  661. auto has_instance_method = rhs.as_object().get(vm.well_known_symbol_has_instance());
  662. if (!has_instance_method.is_empty()) {
  663. if (!has_instance_method.is_function()) {
  664. vm.throw_exception<TypeError>(global_object, ErrorType::NotAFunction, has_instance_method.to_string_without_side_effects().characters());
  665. return {};
  666. }
  667. return Value(vm.call(has_instance_method.as_function(), rhs, lhs).to_boolean());
  668. }
  669. if (!rhs.is_function()) {
  670. vm.throw_exception<TypeError>(global_object, ErrorType::NotAFunction, rhs.to_string_without_side_effects().characters());
  671. return {};
  672. }
  673. return ordinary_has_instance(global_object, lhs, rhs);
  674. }
  675. Value ordinary_has_instance(GlobalObject& global_object, Value lhs, Value rhs)
  676. {
  677. auto& vm = global_object.vm();
  678. if (!rhs.is_function())
  679. return Value(false);
  680. auto& rhs_function = rhs.as_function();
  681. if (rhs_function.is_bound_function()) {
  682. auto& bound_target = static_cast<BoundFunction&>(rhs_function);
  683. return instance_of(global_object, lhs, Value(&bound_target.target_function()));
  684. }
  685. if (!lhs.is_object())
  686. return Value(false);
  687. Object* lhs_object = &lhs.as_object();
  688. auto rhs_prototype = rhs_function.get("prototype");
  689. if (vm.exception())
  690. return {};
  691. if (!rhs_prototype.is_object()) {
  692. vm.throw_exception<TypeError>(global_object, ErrorType::InstanceOfOperatorBadPrototype, rhs_prototype.to_string_without_side_effects().characters());
  693. return {};
  694. }
  695. while (true) {
  696. lhs_object = lhs_object->prototype();
  697. if (vm.exception())
  698. return {};
  699. if (!lhs_object)
  700. return Value(false);
  701. if (same_value(rhs_prototype, lhs_object))
  702. return Value(true);
  703. }
  704. }
  705. const LogStream& operator<<(const LogStream& stream, const Value& value)
  706. {
  707. return stream << (value.is_empty() ? "<empty>" : value.to_string_without_side_effects());
  708. }
  709. bool same_value(Value lhs, Value rhs)
  710. {
  711. if (lhs.type() != rhs.type())
  712. return false;
  713. if (lhs.is_number()) {
  714. if (lhs.is_nan() && rhs.is_nan())
  715. return true;
  716. if (lhs.is_positive_zero() && rhs.is_negative_zero())
  717. return false;
  718. if (lhs.is_negative_zero() && rhs.is_positive_zero())
  719. return false;
  720. return lhs.as_double() == rhs.as_double();
  721. }
  722. if (lhs.is_bigint()) {
  723. auto lhs_big_integer = lhs.as_bigint().big_integer();
  724. auto rhs_big_integer = rhs.as_bigint().big_integer();
  725. if (lhs_big_integer == BIGINT_ZERO && rhs_big_integer == BIGINT_ZERO && lhs_big_integer.is_negative() != rhs_big_integer.is_negative())
  726. return false;
  727. return lhs_big_integer == rhs_big_integer;
  728. }
  729. return same_value_non_numeric(lhs, rhs);
  730. }
  731. bool same_value_zero(Value lhs, Value rhs)
  732. {
  733. if (lhs.type() != rhs.type())
  734. return false;
  735. if (lhs.is_number()) {
  736. if (lhs.is_nan() && rhs.is_nan())
  737. return true;
  738. return lhs.as_double() == rhs.as_double();
  739. }
  740. if (lhs.is_bigint())
  741. return lhs.as_bigint().big_integer() == rhs.as_bigint().big_integer();
  742. return same_value_non_numeric(lhs, rhs);
  743. }
  744. bool same_value_non_numeric(Value lhs, Value rhs)
  745. {
  746. ASSERT(!lhs.is_number() && !lhs.is_bigint());
  747. ASSERT(lhs.type() == rhs.type());
  748. switch (lhs.type()) {
  749. case Value::Type::Undefined:
  750. case Value::Type::Null:
  751. return true;
  752. case Value::Type::String:
  753. return lhs.as_string().string() == rhs.as_string().string();
  754. case Value::Type::Symbol:
  755. return &lhs.as_symbol() == &rhs.as_symbol();
  756. case Value::Type::Boolean:
  757. return lhs.as_bool() == rhs.as_bool();
  758. case Value::Type::Object:
  759. return &lhs.as_object() == &rhs.as_object();
  760. default:
  761. ASSERT_NOT_REACHED();
  762. }
  763. }
  764. bool strict_eq(Value lhs, Value rhs)
  765. {
  766. if (lhs.type() != rhs.type())
  767. return false;
  768. if (lhs.is_number()) {
  769. if (lhs.is_nan() || rhs.is_nan())
  770. return false;
  771. if (lhs.as_double() == rhs.as_double())
  772. return true;
  773. return false;
  774. }
  775. if (lhs.is_bigint())
  776. return lhs.as_bigint().big_integer() == rhs.as_bigint().big_integer();
  777. return same_value_non_numeric(lhs, rhs);
  778. }
  779. bool abstract_eq(GlobalObject& global_object, Value lhs, Value rhs)
  780. {
  781. if (lhs.type() == rhs.type())
  782. return strict_eq(lhs, rhs);
  783. if ((lhs.is_undefined() || lhs.is_null()) && (rhs.is_undefined() || rhs.is_null()))
  784. return true;
  785. if (lhs.is_number() && rhs.is_string())
  786. return abstract_eq(global_object, lhs, rhs.to_number(global_object.global_object()));
  787. if (lhs.is_string() && rhs.is_number())
  788. return abstract_eq(global_object, lhs.to_number(global_object.global_object()), rhs);
  789. if (lhs.is_bigint() && rhs.is_string()) {
  790. auto& rhs_string = rhs.as_string().string();
  791. if (!is_valid_bigint_value(rhs_string))
  792. return false;
  793. return abstract_eq(global_object, lhs, js_bigint(global_object.heap(), Crypto::SignedBigInteger::from_base10(rhs_string)));
  794. }
  795. if (lhs.is_string() && rhs.is_bigint())
  796. return abstract_eq(global_object, rhs, lhs);
  797. if (lhs.is_boolean())
  798. return abstract_eq(global_object, lhs.to_number(global_object.global_object()), rhs);
  799. if (rhs.is_boolean())
  800. return abstract_eq(global_object, lhs, rhs.to_number(global_object.global_object()));
  801. if ((lhs.is_string() || lhs.is_number() || lhs.is_bigint() || lhs.is_symbol()) && rhs.is_object())
  802. return abstract_eq(global_object, lhs, rhs.to_primitive());
  803. if (lhs.is_object() && (rhs.is_string() || rhs.is_number() || lhs.is_bigint() || rhs.is_symbol()))
  804. return abstract_eq(global_object, lhs.to_primitive(), rhs);
  805. if ((lhs.is_bigint() && rhs.is_number()) || (lhs.is_number() && rhs.is_bigint())) {
  806. if (lhs.is_nan() || lhs.is_infinity() || rhs.is_nan() || rhs.is_infinity())
  807. return false;
  808. if ((lhs.is_number() && !lhs.is_integer()) || (rhs.is_number() && !rhs.is_integer()))
  809. return false;
  810. if (lhs.is_number())
  811. return Crypto::SignedBigInteger { lhs.as_i32() } == rhs.as_bigint().big_integer();
  812. else
  813. return Crypto::SignedBigInteger { rhs.as_i32() } == lhs.as_bigint().big_integer();
  814. }
  815. return false;
  816. }
  817. TriState abstract_relation(GlobalObject& global_object, bool left_first, Value lhs, Value rhs)
  818. {
  819. Value x_primitive;
  820. Value y_primitive;
  821. if (left_first) {
  822. x_primitive = lhs.to_primitive(Value::PreferredType::Number);
  823. if (global_object.vm().exception())
  824. return {};
  825. y_primitive = rhs.to_primitive(Value::PreferredType::Number);
  826. if (global_object.vm().exception())
  827. return {};
  828. } else {
  829. y_primitive = lhs.to_primitive(Value::PreferredType::Number);
  830. if (global_object.vm().exception())
  831. return {};
  832. x_primitive = rhs.to_primitive(Value::PreferredType::Number);
  833. if (global_object.vm().exception())
  834. return {};
  835. }
  836. if (x_primitive.is_string() && y_primitive.is_string()) {
  837. auto x_string = x_primitive.as_string().string();
  838. auto y_string = y_primitive.as_string().string();
  839. if (x_string.starts_with(y_string))
  840. return TriState::False;
  841. if (y_string.starts_with(x_string))
  842. return TriState::True;
  843. Utf8View x_code_points { x_string };
  844. Utf8View y_code_points { y_string };
  845. for (auto k = x_code_points.begin(), l = y_code_points.begin();
  846. k != x_code_points.end() && l != y_code_points.end();
  847. ++k, ++l) {
  848. if (*k != *l) {
  849. if (*k < *l) {
  850. return TriState::True;
  851. } else {
  852. return TriState::False;
  853. }
  854. }
  855. }
  856. ASSERT_NOT_REACHED();
  857. }
  858. if (x_primitive.is_bigint() && y_primitive.is_string()) {
  859. auto& y_string = y_primitive.as_string().string();
  860. if (!is_valid_bigint_value(y_string))
  861. return TriState::Unknown;
  862. if (x_primitive.as_bigint().big_integer() < Crypto::SignedBigInteger::from_base10(y_string))
  863. return TriState::True;
  864. else
  865. return TriState::False;
  866. }
  867. if (x_primitive.is_string() && y_primitive.is_bigint()) {
  868. auto& x_string = x_primitive.as_string().string();
  869. if (!is_valid_bigint_value(x_string))
  870. return TriState::Unknown;
  871. if (Crypto::SignedBigInteger::from_base10(x_string) < y_primitive.as_bigint().big_integer())
  872. return TriState::True;
  873. else
  874. return TriState::False;
  875. }
  876. auto x_numeric = x_primitive.to_numeric(global_object.global_object());
  877. if (global_object.vm().exception())
  878. return {};
  879. auto y_numeric = y_primitive.to_numeric(global_object.global_object());
  880. if (global_object.vm().exception())
  881. return {};
  882. if (x_numeric.is_nan() || y_numeric.is_nan())
  883. return TriState::Unknown;
  884. if (x_numeric.is_positive_infinity() || y_numeric.is_negative_infinity())
  885. return TriState::False;
  886. if (x_numeric.is_negative_infinity() || y_numeric.is_positive_infinity())
  887. return TriState::True;
  888. if (x_numeric.is_number() && y_numeric.is_number()) {
  889. if (x_numeric.as_double() < y_numeric.as_double())
  890. return TriState::True;
  891. else
  892. return TriState::False;
  893. }
  894. if (x_numeric.is_bigint() && y_numeric.is_bigint()) {
  895. if (x_numeric.as_bigint().big_integer() < y_numeric.as_bigint().big_integer())
  896. return TriState::True;
  897. else
  898. return TriState::False;
  899. }
  900. ASSERT((x_numeric.is_number() && y_numeric.is_bigint()) || (x_numeric.is_bigint() && y_numeric.is_number()));
  901. bool x_lower_than_y;
  902. if (x_numeric.is_number()) {
  903. x_lower_than_y = x_numeric.is_integer()
  904. ? Crypto::SignedBigInteger { x_numeric.as_i32() } < y_numeric.as_bigint().big_integer()
  905. : (Crypto::SignedBigInteger { x_numeric.as_i32() } < y_numeric.as_bigint().big_integer() || Crypto::SignedBigInteger { x_numeric.as_i32() + 1 } < y_numeric.as_bigint().big_integer());
  906. } else {
  907. x_lower_than_y = y_numeric.is_integer()
  908. ? x_numeric.as_bigint().big_integer() < Crypto::SignedBigInteger { y_numeric.as_i32() }
  909. : (x_numeric.as_bigint().big_integer() < Crypto::SignedBigInteger { y_numeric.as_i32() } || x_numeric.as_bigint().big_integer() < Crypto::SignedBigInteger { y_numeric.as_i32() + 1 });
  910. }
  911. if (x_lower_than_y)
  912. return TriState::True;
  913. else
  914. return TriState::False;
  915. }
  916. size_t length_of_array_like(GlobalObject& global_object, Value value)
  917. {
  918. ASSERT(value.is_object());
  919. auto result = value.as_object().get("length");
  920. if (global_object.vm().exception())
  921. return 0;
  922. return result.to_size_t(global_object);
  923. }
  924. }