Value.cpp 35 KB

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