Value.cpp 38 KB

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