Value.cpp 36 KB

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