Value.cpp 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085
  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. // FIXME: This should be more sophisticated: don't cut off decimals, don't include trailing zeros
  110. return String::formatted("{:.4}", m_value.as_double);
  111. case Type::String:
  112. return m_value.as_string->string();
  113. case Type::Symbol:
  114. return m_value.as_symbol->to_string();
  115. case Type::BigInt:
  116. return m_value.as_bigint->to_string();
  117. case Type::Object:
  118. return String::formatted("[object {}]", as_object().class_name());
  119. case Type::Accessor:
  120. return "<accessor>";
  121. case Type::NativeProperty:
  122. return "<native-property>";
  123. default:
  124. ASSERT_NOT_REACHED();
  125. }
  126. }
  127. PrimitiveString* Value::to_primitive_string(GlobalObject& global_object)
  128. {
  129. if (is_string())
  130. return &as_string();
  131. auto string = to_string(global_object);
  132. if (global_object.vm().exception())
  133. return nullptr;
  134. return js_string(global_object.heap(), string);
  135. }
  136. String Value::to_string(GlobalObject& global_object, bool legacy_null_to_empty_string) const
  137. {
  138. switch (m_type) {
  139. case Type::Undefined:
  140. return "undefined";
  141. case Type::Null:
  142. return !legacy_null_to_empty_string ? "null" : String::empty();
  143. case Type::Boolean:
  144. return m_value.as_bool ? "true" : "false";
  145. case Type::Number:
  146. if (is_nan())
  147. return "NaN";
  148. if (is_infinity())
  149. return is_negative_infinity() ? "-Infinity" : "Infinity";
  150. if (is_integer())
  151. return String::number(as_i32());
  152. // FIXME: This should be more sophisticated: don't cut off decimals, don't include trailing zeros
  153. return String::formatted("{:.4}", m_value.as_double);
  154. case Type::String:
  155. return m_value.as_string->string();
  156. case Type::Symbol:
  157. global_object.vm().throw_exception<TypeError>(global_object, ErrorType::Convert, "symbol", "string");
  158. return {};
  159. case Type::BigInt:
  160. return m_value.as_bigint->big_integer().to_base10();
  161. case Type::Object: {
  162. auto primitive_value = to_primitive(PreferredType::String);
  163. if (global_object.vm().exception())
  164. return {};
  165. return primitive_value.to_string(global_object);
  166. }
  167. default:
  168. ASSERT_NOT_REACHED();
  169. }
  170. }
  171. bool Value::to_boolean() const
  172. {
  173. switch (m_type) {
  174. case Type::Undefined:
  175. case Type::Null:
  176. return false;
  177. case Type::Boolean:
  178. return m_value.as_bool;
  179. case Type::Number:
  180. if (is_nan())
  181. return false;
  182. return m_value.as_double != 0;
  183. case Type::String:
  184. return !m_value.as_string->string().is_empty();
  185. case Type::Symbol:
  186. return true;
  187. case Type::BigInt:
  188. return m_value.as_bigint->big_integer() != BIGINT_ZERO;
  189. case Type::Object:
  190. return true;
  191. default:
  192. ASSERT_NOT_REACHED();
  193. }
  194. }
  195. Value Value::to_primitive(PreferredType preferred_type) const
  196. {
  197. if (is_object()) {
  198. // FIXME: Also support @@toPrimitive
  199. if (preferred_type == PreferredType::Default)
  200. preferred_type = PreferredType::Number;
  201. return as_object().ordinary_to_primitive(preferred_type);
  202. }
  203. return *this;
  204. }
  205. Object* Value::to_object(GlobalObject& global_object) const
  206. {
  207. switch (m_type) {
  208. case Type::Undefined:
  209. case Type::Null:
  210. global_object.vm().throw_exception<TypeError>(global_object, ErrorType::ToObjectNullOrUndef);
  211. return nullptr;
  212. case Type::Boolean:
  213. return BooleanObject::create(global_object, m_value.as_bool);
  214. case Type::Number:
  215. return NumberObject::create(global_object, m_value.as_double);
  216. case Type::String:
  217. return StringObject::create(global_object, *m_value.as_string);
  218. case Type::Symbol:
  219. return SymbolObject::create(global_object, *m_value.as_symbol);
  220. case Type::BigInt:
  221. return BigIntObject::create(global_object, *m_value.as_bigint);
  222. case Type::Object:
  223. return &const_cast<Object&>(as_object());
  224. default:
  225. dbgln("Dying because I can't to_object() on {}", *this);
  226. ASSERT_NOT_REACHED();
  227. }
  228. }
  229. Value Value::to_numeric(GlobalObject& global_object) const
  230. {
  231. auto primitive = to_primitive(Value::PreferredType::Number);
  232. if (global_object.vm().exception())
  233. return {};
  234. if (primitive.is_bigint())
  235. return primitive;
  236. return primitive.to_number(global_object);
  237. }
  238. Value Value::to_number(GlobalObject& global_object) const
  239. {
  240. switch (m_type) {
  241. case Type::Undefined:
  242. return js_nan();
  243. case Type::Null:
  244. return Value(0);
  245. case Type::Boolean:
  246. return Value(m_value.as_bool ? 1 : 0);
  247. case Type::Number:
  248. return Value(m_value.as_double);
  249. case Type::String: {
  250. auto string = as_string().string().trim_whitespace();
  251. if (string.is_empty())
  252. return Value(0);
  253. if (string == "Infinity" || string == "+Infinity")
  254. return js_infinity();
  255. if (string == "-Infinity")
  256. return js_negative_infinity();
  257. char* endptr;
  258. auto parsed_double = strtod(string.characters(), &endptr);
  259. if (*endptr)
  260. return js_nan();
  261. return Value(parsed_double);
  262. }
  263. case Type::Symbol:
  264. global_object.vm().throw_exception<TypeError>(global_object, ErrorType::Convert, "symbol", "number");
  265. return {};
  266. case Type::BigInt:
  267. global_object.vm().throw_exception<TypeError>(global_object, ErrorType::Convert, "BigInt", "number");
  268. return {};
  269. case Type::Object: {
  270. auto primitive = to_primitive(PreferredType::Number);
  271. if (global_object.vm().exception())
  272. return {};
  273. return primitive.to_number(global_object);
  274. }
  275. default:
  276. ASSERT_NOT_REACHED();
  277. }
  278. }
  279. BigInt* Value::to_bigint(GlobalObject& global_object) const
  280. {
  281. auto& vm = global_object.vm();
  282. auto primitive = to_primitive(PreferredType::Number);
  283. if (vm.exception())
  284. return nullptr;
  285. switch (primitive.type()) {
  286. case Type::Undefined:
  287. vm.throw_exception<TypeError>(global_object, ErrorType::Convert, "undefined", "BigInt");
  288. return nullptr;
  289. case Type::Null:
  290. vm.throw_exception<TypeError>(global_object, ErrorType::Convert, "null", "BigInt");
  291. return nullptr;
  292. case Type::Boolean: {
  293. auto value = primitive.as_bool() ? 1 : 0;
  294. return js_bigint(vm.heap(), Crypto::SignedBigInteger { value });
  295. }
  296. case Type::BigInt:
  297. return &primitive.as_bigint();
  298. case Type::Number:
  299. vm.throw_exception<TypeError>(global_object, ErrorType::Convert, "number", "BigInt");
  300. return {};
  301. case Type::String: {
  302. auto& string = primitive.as_string().string();
  303. if (!is_valid_bigint_value(string)) {
  304. vm.throw_exception<SyntaxError>(global_object, ErrorType::BigIntInvalidValue, string);
  305. return {};
  306. }
  307. return js_bigint(vm.heap(), Crypto::SignedBigInteger::from_base10(string.trim_whitespace()));
  308. }
  309. case Type::Symbol:
  310. vm.throw_exception<TypeError>(global_object, ErrorType::Convert, "symbol", "BigInt");
  311. return {};
  312. default:
  313. ASSERT_NOT_REACHED();
  314. }
  315. }
  316. i32 Value::as_i32() const
  317. {
  318. return static_cast<i32>(as_double());
  319. }
  320. size_t Value::as_size_t() const
  321. {
  322. ASSERT(as_double() >= 0);
  323. return min((double)as_i32(), MAX_ARRAY_LIKE_INDEX);
  324. }
  325. double Value::to_double(GlobalObject& global_object) const
  326. {
  327. auto number = to_number(global_object);
  328. if (global_object.vm().exception())
  329. return 0;
  330. return number.as_double();
  331. }
  332. i32 Value::to_i32(GlobalObject& global_object) const
  333. {
  334. auto number = to_number(global_object);
  335. if (global_object.vm().exception())
  336. return 0;
  337. if (number.is_nan() || number.is_infinity())
  338. return 0;
  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. bool same_value(Value lhs, Value rhs)
  764. {
  765. if (lhs.type() != rhs.type())
  766. return false;
  767. if (lhs.is_number()) {
  768. if (lhs.is_nan() && rhs.is_nan())
  769. return true;
  770. if (lhs.is_positive_zero() && rhs.is_negative_zero())
  771. return false;
  772. if (lhs.is_negative_zero() && rhs.is_positive_zero())
  773. return false;
  774. return lhs.as_double() == rhs.as_double();
  775. }
  776. if (lhs.is_bigint()) {
  777. auto lhs_big_integer = lhs.as_bigint().big_integer();
  778. auto rhs_big_integer = rhs.as_bigint().big_integer();
  779. if (lhs_big_integer == BIGINT_ZERO && rhs_big_integer == BIGINT_ZERO && lhs_big_integer.is_negative() != rhs_big_integer.is_negative())
  780. return false;
  781. return lhs_big_integer == rhs_big_integer;
  782. }
  783. return same_value_non_numeric(lhs, rhs);
  784. }
  785. bool same_value_zero(Value lhs, Value rhs)
  786. {
  787. if (lhs.type() != rhs.type())
  788. return false;
  789. if (lhs.is_number()) {
  790. if (lhs.is_nan() && rhs.is_nan())
  791. return true;
  792. return lhs.as_double() == rhs.as_double();
  793. }
  794. if (lhs.is_bigint())
  795. return lhs.as_bigint().big_integer() == rhs.as_bigint().big_integer();
  796. return same_value_non_numeric(lhs, rhs);
  797. }
  798. bool same_value_non_numeric(Value lhs, Value rhs)
  799. {
  800. ASSERT(!lhs.is_number() && !lhs.is_bigint());
  801. ASSERT(lhs.type() == rhs.type());
  802. switch (lhs.type()) {
  803. case Value::Type::Undefined:
  804. case Value::Type::Null:
  805. return true;
  806. case Value::Type::String:
  807. return lhs.as_string().string() == rhs.as_string().string();
  808. case Value::Type::Symbol:
  809. return &lhs.as_symbol() == &rhs.as_symbol();
  810. case Value::Type::Boolean:
  811. return lhs.as_bool() == rhs.as_bool();
  812. case Value::Type::Object:
  813. return &lhs.as_object() == &rhs.as_object();
  814. default:
  815. ASSERT_NOT_REACHED();
  816. }
  817. }
  818. bool strict_eq(Value lhs, Value rhs)
  819. {
  820. if (lhs.type() != rhs.type())
  821. return false;
  822. if (lhs.is_number()) {
  823. if (lhs.is_nan() || rhs.is_nan())
  824. return false;
  825. if (lhs.as_double() == rhs.as_double())
  826. return true;
  827. return false;
  828. }
  829. if (lhs.is_bigint())
  830. return lhs.as_bigint().big_integer() == rhs.as_bigint().big_integer();
  831. return same_value_non_numeric(lhs, rhs);
  832. }
  833. bool abstract_eq(GlobalObject& global_object, Value lhs, Value rhs)
  834. {
  835. if (lhs.type() == rhs.type())
  836. return strict_eq(lhs, rhs);
  837. if (lhs.is_nullish() && rhs.is_nullish())
  838. return true;
  839. if (lhs.is_number() && rhs.is_string())
  840. return abstract_eq(global_object, lhs, rhs.to_number(global_object.global_object()));
  841. if (lhs.is_string() && rhs.is_number())
  842. return abstract_eq(global_object, lhs.to_number(global_object.global_object()), rhs);
  843. if (lhs.is_bigint() && rhs.is_string()) {
  844. auto& rhs_string = rhs.as_string().string();
  845. if (!is_valid_bigint_value(rhs_string))
  846. return false;
  847. return abstract_eq(global_object, lhs, js_bigint(global_object.heap(), Crypto::SignedBigInteger::from_base10(rhs_string)));
  848. }
  849. if (lhs.is_string() && rhs.is_bigint())
  850. return abstract_eq(global_object, rhs, lhs);
  851. if (lhs.is_boolean())
  852. return abstract_eq(global_object, lhs.to_number(global_object.global_object()), rhs);
  853. if (rhs.is_boolean())
  854. return abstract_eq(global_object, lhs, rhs.to_number(global_object.global_object()));
  855. if ((lhs.is_string() || lhs.is_number() || lhs.is_bigint() || lhs.is_symbol()) && rhs.is_object())
  856. return abstract_eq(global_object, lhs, rhs.to_primitive());
  857. if (lhs.is_object() && (rhs.is_string() || rhs.is_number() || lhs.is_bigint() || rhs.is_symbol()))
  858. return abstract_eq(global_object, lhs.to_primitive(), rhs);
  859. if ((lhs.is_bigint() && rhs.is_number()) || (lhs.is_number() && rhs.is_bigint())) {
  860. if (lhs.is_nan() || lhs.is_infinity() || rhs.is_nan() || rhs.is_infinity())
  861. return false;
  862. if ((lhs.is_number() && !lhs.is_integer()) || (rhs.is_number() && !rhs.is_integer()))
  863. return false;
  864. if (lhs.is_number())
  865. return Crypto::SignedBigInteger { lhs.as_i32() } == rhs.as_bigint().big_integer();
  866. else
  867. return Crypto::SignedBigInteger { rhs.as_i32() } == lhs.as_bigint().big_integer();
  868. }
  869. return false;
  870. }
  871. TriState abstract_relation(GlobalObject& global_object, bool left_first, Value lhs, Value rhs)
  872. {
  873. Value x_primitive;
  874. Value y_primitive;
  875. if (left_first) {
  876. x_primitive = lhs.to_primitive(Value::PreferredType::Number);
  877. if (global_object.vm().exception())
  878. return {};
  879. y_primitive = rhs.to_primitive(Value::PreferredType::Number);
  880. if (global_object.vm().exception())
  881. return {};
  882. } else {
  883. y_primitive = lhs.to_primitive(Value::PreferredType::Number);
  884. if (global_object.vm().exception())
  885. return {};
  886. x_primitive = rhs.to_primitive(Value::PreferredType::Number);
  887. if (global_object.vm().exception())
  888. return {};
  889. }
  890. if (x_primitive.is_string() && y_primitive.is_string()) {
  891. auto x_string = x_primitive.as_string().string();
  892. auto y_string = y_primitive.as_string().string();
  893. if (x_string.starts_with(y_string))
  894. return TriState::False;
  895. if (y_string.starts_with(x_string))
  896. return TriState::True;
  897. Utf8View x_code_points { x_string };
  898. Utf8View y_code_points { y_string };
  899. for (auto k = x_code_points.begin(), l = y_code_points.begin();
  900. k != x_code_points.end() && l != y_code_points.end();
  901. ++k, ++l) {
  902. if (*k != *l) {
  903. if (*k < *l) {
  904. return TriState::True;
  905. } else {
  906. return TriState::False;
  907. }
  908. }
  909. }
  910. ASSERT_NOT_REACHED();
  911. }
  912. if (x_primitive.is_bigint() && y_primitive.is_string()) {
  913. auto& y_string = y_primitive.as_string().string();
  914. if (!is_valid_bigint_value(y_string))
  915. return TriState::Unknown;
  916. if (x_primitive.as_bigint().big_integer() < Crypto::SignedBigInteger::from_base10(y_string))
  917. return TriState::True;
  918. else
  919. return TriState::False;
  920. }
  921. if (x_primitive.is_string() && y_primitive.is_bigint()) {
  922. auto& x_string = x_primitive.as_string().string();
  923. if (!is_valid_bigint_value(x_string))
  924. return TriState::Unknown;
  925. if (Crypto::SignedBigInteger::from_base10(x_string) < y_primitive.as_bigint().big_integer())
  926. return TriState::True;
  927. else
  928. return TriState::False;
  929. }
  930. auto x_numeric = x_primitive.to_numeric(global_object.global_object());
  931. if (global_object.vm().exception())
  932. return {};
  933. auto y_numeric = y_primitive.to_numeric(global_object.global_object());
  934. if (global_object.vm().exception())
  935. return {};
  936. if (x_numeric.is_nan() || y_numeric.is_nan())
  937. return TriState::Unknown;
  938. if (x_numeric.is_positive_infinity() || y_numeric.is_negative_infinity())
  939. return TriState::False;
  940. if (x_numeric.is_negative_infinity() || y_numeric.is_positive_infinity())
  941. return TriState::True;
  942. if (x_numeric.is_number() && y_numeric.is_number()) {
  943. if (x_numeric.as_double() < y_numeric.as_double())
  944. return TriState::True;
  945. else
  946. return TriState::False;
  947. }
  948. if (x_numeric.is_bigint() && y_numeric.is_bigint()) {
  949. if (x_numeric.as_bigint().big_integer() < y_numeric.as_bigint().big_integer())
  950. return TriState::True;
  951. else
  952. return TriState::False;
  953. }
  954. ASSERT((x_numeric.is_number() && y_numeric.is_bigint()) || (x_numeric.is_bigint() && y_numeric.is_number()));
  955. bool x_lower_than_y;
  956. if (x_numeric.is_number()) {
  957. x_lower_than_y = x_numeric.is_integer()
  958. ? Crypto::SignedBigInteger { x_numeric.as_i32() } < y_numeric.as_bigint().big_integer()
  959. : (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());
  960. } else {
  961. x_lower_than_y = y_numeric.is_integer()
  962. ? x_numeric.as_bigint().big_integer() < Crypto::SignedBigInteger { y_numeric.as_i32() }
  963. : (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 });
  964. }
  965. if (x_lower_than_y)
  966. return TriState::True;
  967. else
  968. return TriState::False;
  969. }
  970. size_t length_of_array_like(GlobalObject& global_object, Value value)
  971. {
  972. ASSERT(value.is_object());
  973. auto& vm = global_object.vm();
  974. auto result = value.as_object().get(vm.names.length);
  975. if (vm.exception())
  976. return 0;
  977. return result.to_size_t(global_object);
  978. }
  979. }