Value.cpp 38 KB

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