Value.cpp 35 KB

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