Value.cpp 32 KB

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