Value.cpp 35 KB

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