Value.cpp 39 KB

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