Value.cpp 36 KB

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