Value.cpp 33 KB

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