Value.cpp 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2020-2021, Linus Groh <linusg@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/AllOf.h>
  8. #include <AK/String.h>
  9. #include <AK/StringBuilder.h>
  10. #include <AK/Utf8View.h>
  11. #include <LibCrypto/BigInt/SignedBigInteger.h>
  12. #include <LibCrypto/NumberTheory/ModularFunctions.h>
  13. #include <LibJS/Runtime/AbstractOperations.h>
  14. #include <LibJS/Runtime/Accessor.h>
  15. #include <LibJS/Runtime/Array.h>
  16. #include <LibJS/Runtime/BigInt.h>
  17. #include <LibJS/Runtime/BigIntObject.h>
  18. #include <LibJS/Runtime/BooleanObject.h>
  19. #include <LibJS/Runtime/BoundFunction.h>
  20. #include <LibJS/Runtime/Completion.h>
  21. #include <LibJS/Runtime/Error.h>
  22. #include <LibJS/Runtime/FunctionObject.h>
  23. #include <LibJS/Runtime/GlobalObject.h>
  24. #include <LibJS/Runtime/NativeFunction.h>
  25. #include <LibJS/Runtime/NumberObject.h>
  26. #include <LibJS/Runtime/Object.h>
  27. #include <LibJS/Runtime/PrimitiveString.h>
  28. #include <LibJS/Runtime/ProxyObject.h>
  29. #include <LibJS/Runtime/RegExpObject.h>
  30. #include <LibJS/Runtime/StringObject.h>
  31. #include <LibJS/Runtime/SymbolObject.h>
  32. #include <LibJS/Runtime/VM.h>
  33. #include <LibJS/Runtime/Value.h>
  34. #include <math.h>
  35. namespace JS {
  36. static inline bool same_type_for_equality(const Value& lhs, const Value& rhs)
  37. {
  38. if (lhs.type() == rhs.type())
  39. return true;
  40. if (lhs.is_number() && rhs.is_number())
  41. return true;
  42. return false;
  43. }
  44. static const Crypto::SignedBigInteger BIGINT_ZERO { 0 };
  45. static bool is_valid_bigint_value(StringView string)
  46. {
  47. string = string.trim_whitespace();
  48. if (string.length() > 1 && (string[0] == '-' || string[0] == '+'))
  49. string = string.substring_view(1, string.length() - 1);
  50. return all_of(string, [](auto ch) { return isdigit(ch); });
  51. }
  52. ALWAYS_INLINE bool both_number(const Value& lhs, const Value& rhs)
  53. {
  54. return lhs.is_number() && rhs.is_number();
  55. }
  56. ALWAYS_INLINE bool both_bigint(const Value& lhs, const Value& rhs)
  57. {
  58. return lhs.is_bigint() && rhs.is_bigint();
  59. }
  60. // 6.1.6.1.20 Number::toString ( x ), https://tc39.es/ecma262/#sec-numeric-types-number-tostring
  61. static String double_to_string(double d)
  62. {
  63. if (isnan(d))
  64. return "NaN";
  65. if (d == +0.0 || d == -0.0)
  66. return "0";
  67. if (d < +0.0) {
  68. StringBuilder builder;
  69. builder.append('-');
  70. builder.append(double_to_string(-d));
  71. return builder.to_string();
  72. }
  73. if (d == static_cast<double>(INFINITY))
  74. return "Infinity";
  75. StringBuilder number_string_builder;
  76. size_t start_index = 0;
  77. size_t end_index = 0;
  78. size_t int_part_end = 0;
  79. // generate integer part (reversed)
  80. double int_part;
  81. double frac_part;
  82. frac_part = modf(d, &int_part);
  83. while (int_part > 0) {
  84. number_string_builder.append('0' + (int)fmod(int_part, 10));
  85. end_index++;
  86. int_part = floor(int_part / 10);
  87. }
  88. auto reversed_integer_part = number_string_builder.to_string().reverse();
  89. number_string_builder.clear();
  90. number_string_builder.append(reversed_integer_part);
  91. int_part_end = end_index;
  92. int exponent = 0;
  93. // generate fractional part
  94. while (frac_part > 0) {
  95. double old_frac_part = frac_part;
  96. frac_part *= 10;
  97. frac_part = modf(frac_part, &int_part);
  98. if (old_frac_part == frac_part)
  99. break;
  100. number_string_builder.append('0' + (int)int_part);
  101. end_index++;
  102. exponent--;
  103. }
  104. auto number_string = number_string_builder.to_string();
  105. // FIXME: Remove this hack.
  106. // FIXME: Instead find the shortest round-trippable representation.
  107. // Remove decimals after the 15th position
  108. if (end_index > int_part_end + 15) {
  109. exponent += end_index - int_part_end - 15;
  110. end_index = int_part_end + 15;
  111. }
  112. // remove leading zeroes
  113. while (start_index < end_index && number_string[start_index] == '0') {
  114. start_index++;
  115. }
  116. // remove trailing zeroes
  117. while (end_index > 0 && number_string[end_index - 1] == '0') {
  118. end_index--;
  119. exponent++;
  120. }
  121. if (end_index <= start_index)
  122. return "0";
  123. auto digits = number_string.substring_view(start_index, end_index - start_index);
  124. int number_of_digits = end_index - start_index;
  125. exponent += number_of_digits;
  126. StringBuilder builder;
  127. if (number_of_digits <= exponent && exponent <= 21) {
  128. builder.append(digits);
  129. builder.append(String::repeated('0', exponent - number_of_digits));
  130. return builder.to_string();
  131. }
  132. if (0 < exponent && exponent <= 21) {
  133. builder.append(digits.substring_view(0, exponent));
  134. builder.append('.');
  135. builder.append(digits.substring_view(exponent));
  136. return builder.to_string();
  137. }
  138. if (-6 < exponent && exponent <= 0) {
  139. builder.append("0.");
  140. builder.append(String::repeated('0', -exponent));
  141. builder.append(digits);
  142. return builder.to_string();
  143. }
  144. if (number_of_digits == 1) {
  145. builder.append(digits);
  146. builder.append('e');
  147. if (exponent - 1 > 0)
  148. builder.append('+');
  149. else
  150. builder.append('-');
  151. builder.append(String::number(AK::abs(exponent - 1)));
  152. return builder.to_string();
  153. }
  154. builder.append(digits[0]);
  155. builder.append('.');
  156. builder.append(digits.substring_view(1));
  157. builder.append('e');
  158. if (exponent - 1 > 0)
  159. builder.append('+');
  160. else
  161. builder.append('-');
  162. builder.append(String::number(AK::abs(exponent - 1)));
  163. return builder.to_string();
  164. }
  165. // 7.2.2 IsArray ( argument ), https://tc39.es/ecma262/#sec-isarray
  166. ThrowCompletionOr<bool> Value::is_array(GlobalObject& global_object) const
  167. {
  168. auto& vm = global_object.vm();
  169. if (!is_object())
  170. return false;
  171. auto& object = as_object();
  172. if (is<Array>(object))
  173. return true;
  174. if (is<ProxyObject>(object)) {
  175. auto& proxy = static_cast<ProxyObject const&>(object);
  176. if (proxy.is_revoked())
  177. return vm.throw_completion<TypeError>(global_object, ErrorType::ProxyRevoked);
  178. return Value(&proxy.target()).is_array(global_object);
  179. }
  180. return false;
  181. }
  182. Array& Value::as_array()
  183. {
  184. VERIFY(is_object() && is<Array>(as_object()));
  185. return static_cast<Array&>(*m_value.as_object);
  186. }
  187. // 7.2.3 IsCallable ( argument ), https://tc39.es/ecma262/#sec-iscallable
  188. bool Value::is_function() const
  189. {
  190. return is_object() && as_object().is_function();
  191. }
  192. FunctionObject& Value::as_function()
  193. {
  194. VERIFY(is_function());
  195. return static_cast<FunctionObject&>(as_object());
  196. }
  197. FunctionObject const& Value::as_function() const
  198. {
  199. VERIFY(is_function());
  200. return static_cast<FunctionObject const&>(as_object());
  201. }
  202. // 7.2.4 IsConstructor ( argument ), https://tc39.es/ecma262/#sec-isconstructor
  203. bool Value::is_constructor() const
  204. {
  205. // 1. If Type(argument) is not Object, return false.
  206. if (!is_function())
  207. return false;
  208. // 2. If argument has a [[Construct]] internal method, return true.
  209. if (as_function().has_constructor())
  210. return true;
  211. // 3. Return false.
  212. return false;
  213. }
  214. // 7.2.8 IsRegExp ( argument ), https://tc39.es/ecma262/#sec-isregexp
  215. ThrowCompletionOr<bool> Value::is_regexp(GlobalObject& global_object) const
  216. {
  217. if (!is_object())
  218. return false;
  219. auto& vm = global_object.vm();
  220. auto matcher = TRY(as_object().get(*vm.well_known_symbol_match()));
  221. if (!matcher.is_undefined())
  222. return matcher.to_boolean();
  223. return is<RegExpObject>(as_object());
  224. }
  225. // 13.5.3 The typeof Operator, https://tc39.es/ecma262/#sec-typeof-operator
  226. String Value::typeof() const
  227. {
  228. switch (m_type) {
  229. case Value::Type::Undefined:
  230. return "undefined";
  231. case Value::Type::Null:
  232. return "object";
  233. case Value::Type::Int32:
  234. case Value::Type::Double:
  235. return "number";
  236. case Value::Type::String:
  237. return "string";
  238. case Value::Type::Object:
  239. // B.3.7.3 Changes to the typeof Operator, https://tc39.es/ecma262/#sec-IsHTMLDDA-internal-slot-typeof
  240. if (as_object().is_htmldda())
  241. return "undefined";
  242. if (is_function())
  243. return "function";
  244. return "object";
  245. case Value::Type::Boolean:
  246. return "boolean";
  247. case Value::Type::Symbol:
  248. return "symbol";
  249. case Value::Type::BigInt:
  250. return "bigint";
  251. default:
  252. VERIFY_NOT_REACHED();
  253. }
  254. }
  255. String Value::to_string_without_side_effects() const
  256. {
  257. switch (m_type) {
  258. case Type::Undefined:
  259. return "undefined";
  260. case Type::Null:
  261. return "null";
  262. case Type::Boolean:
  263. return m_value.as_bool ? "true" : "false";
  264. case Type::Int32:
  265. return String::number(m_value.as_i32);
  266. case Type::Double:
  267. return double_to_string(m_value.as_double);
  268. case Type::String:
  269. return m_value.as_string->string();
  270. case Type::Symbol:
  271. return m_value.as_symbol->to_string();
  272. case Type::BigInt:
  273. return m_value.as_bigint->to_string();
  274. case Type::Object:
  275. return String::formatted("[object {}]", as_object().class_name());
  276. case Type::Accessor:
  277. return "<accessor>";
  278. default:
  279. VERIFY_NOT_REACHED();
  280. }
  281. }
  282. ThrowCompletionOr<PrimitiveString*> Value::to_primitive_string(GlobalObject& global_object)
  283. {
  284. if (is_string())
  285. return &as_string();
  286. auto string = TRY(to_string(global_object));
  287. return js_string(global_object.heap(), string);
  288. }
  289. // 7.1.17 ToString ( argument ), https://tc39.es/ecma262/#sec-tostring
  290. ThrowCompletionOr<String> Value::to_string(GlobalObject& global_object) const
  291. {
  292. auto& vm = global_object.vm();
  293. switch (m_type) {
  294. case Type::Undefined:
  295. return "undefined"sv;
  296. case Type::Null:
  297. return "null"sv;
  298. case Type::Boolean:
  299. return m_value.as_bool ? "true"sv : "false"sv;
  300. case Type::Int32:
  301. return String::number(m_value.as_i32);
  302. case Type::Double:
  303. return double_to_string(m_value.as_double);
  304. case Type::String:
  305. return m_value.as_string->string();
  306. case Type::Symbol:
  307. return vm.throw_completion<TypeError>(global_object, ErrorType::Convert, "symbol", "string");
  308. case Type::BigInt:
  309. return m_value.as_bigint->big_integer().to_base(10);
  310. case Type::Object: {
  311. auto primitive_value = TRY(to_primitive(global_object, PreferredType::String));
  312. return primitive_value.to_string(global_object);
  313. }
  314. default:
  315. VERIFY_NOT_REACHED();
  316. }
  317. }
  318. ThrowCompletionOr<Utf16String> Value::to_utf16_string(GlobalObject& global_object) const
  319. {
  320. if (m_type == Type::String)
  321. return m_value.as_string->utf16_string();
  322. auto utf8_string = TRY(to_string(global_object));
  323. return Utf16String(utf8_string);
  324. }
  325. // 7.1.2 ToBoolean ( argument ), https://tc39.es/ecma262/#sec-toboolean
  326. bool Value::to_boolean() const
  327. {
  328. switch (m_type) {
  329. case Type::Undefined:
  330. case Type::Null:
  331. return false;
  332. case Type::Boolean:
  333. return m_value.as_bool;
  334. case Type::Int32:
  335. return m_value.as_i32 != 0;
  336. case Type::Double:
  337. if (is_nan())
  338. return false;
  339. return m_value.as_double != 0;
  340. case Type::String:
  341. return !m_value.as_string->string().is_empty();
  342. case Type::Symbol:
  343. return true;
  344. case Type::BigInt:
  345. return m_value.as_bigint->big_integer() != BIGINT_ZERO;
  346. case Type::Object:
  347. // B.3.7.1 Changes to ToBoolean, https://tc39.es/ecma262/#sec-IsHTMLDDA-internal-slot-to-boolean
  348. if (m_value.as_object->is_htmldda())
  349. return false;
  350. return true;
  351. default:
  352. VERIFY_NOT_REACHED();
  353. }
  354. }
  355. // 7.1.1 ToPrimitive ( input [ , preferredType ] ), https://tc39.es/ecma262/#sec-toprimitive
  356. ThrowCompletionOr<Value> Value::to_primitive(GlobalObject& global_object, PreferredType preferred_type) const
  357. {
  358. auto get_hint_for_preferred_type = [&]() -> String {
  359. switch (preferred_type) {
  360. case PreferredType::Default:
  361. return "default";
  362. case PreferredType::String:
  363. return "string";
  364. case PreferredType::Number:
  365. return "number";
  366. default:
  367. VERIFY_NOT_REACHED();
  368. }
  369. };
  370. if (is_object()) {
  371. auto& vm = global_object.vm();
  372. auto to_primitive_method = TRY(get_method(global_object, *vm.well_known_symbol_to_primitive()));
  373. if (to_primitive_method) {
  374. auto hint = get_hint_for_preferred_type();
  375. auto result = TRY(call(global_object, *to_primitive_method, *this, js_string(vm, hint)));
  376. if (!result.is_object())
  377. return result;
  378. return vm.throw_completion<TypeError>(global_object, ErrorType::ToPrimitiveReturnedObject, to_string_without_side_effects(), hint);
  379. }
  380. if (preferred_type == PreferredType::Default)
  381. preferred_type = PreferredType::Number;
  382. return as_object().ordinary_to_primitive(preferred_type);
  383. }
  384. return *this;
  385. }
  386. // 7.1.18 ToObject ( argument ), https://tc39.es/ecma262/#sec-toobject
  387. ThrowCompletionOr<Object*> Value::to_object(GlobalObject& global_object) const
  388. {
  389. switch (m_type) {
  390. case Type::Undefined:
  391. case Type::Null:
  392. return global_object.vm().throw_completion<TypeError>(global_object, ErrorType::ToObjectNullOrUndefined);
  393. case Type::Boolean:
  394. return BooleanObject::create(global_object, m_value.as_bool);
  395. case Type::Int32:
  396. case Type::Double:
  397. return NumberObject::create(global_object, as_double());
  398. case Type::String:
  399. return StringObject::create(global_object, *m_value.as_string, *global_object.string_prototype());
  400. case Type::Symbol:
  401. return SymbolObject::create(global_object, *m_value.as_symbol);
  402. case Type::BigInt:
  403. return BigIntObject::create(global_object, *m_value.as_bigint);
  404. case Type::Object:
  405. return &const_cast<Object&>(as_object());
  406. default:
  407. VERIFY_NOT_REACHED();
  408. }
  409. }
  410. // 7.1.3 ToNumeric ( value ), https://tc39.es/ecma262/#sec-tonumeric
  411. FLATTEN ThrowCompletionOr<Value> Value::to_numeric(GlobalObject& global_object) const
  412. {
  413. auto primitive = TRY(to_primitive(global_object, Value::PreferredType::Number));
  414. if (primitive.is_bigint())
  415. return primitive;
  416. return primitive.to_number(global_object);
  417. }
  418. // 7.1.4 ToNumber ( argument ), https://tc39.es/ecma262/#sec-tonumber
  419. ThrowCompletionOr<Value> Value::to_number(GlobalObject& global_object) const
  420. {
  421. switch (m_type) {
  422. case Type::Undefined:
  423. return js_nan();
  424. case Type::Null:
  425. return Value(0);
  426. case Type::Boolean:
  427. return Value(m_value.as_bool ? 1 : 0);
  428. case Type::Int32:
  429. case Type::Double:
  430. return *this;
  431. case Type::String: {
  432. auto string = as_string().string().trim_whitespace();
  433. if (string.is_empty())
  434. return Value(0);
  435. if (string == "Infinity" || string == "+Infinity")
  436. return js_infinity();
  437. if (string == "-Infinity")
  438. return js_negative_infinity();
  439. char* endptr;
  440. auto parsed_double = strtod(string.characters(), &endptr);
  441. if (*endptr)
  442. return js_nan();
  443. return Value(parsed_double);
  444. }
  445. case Type::Symbol:
  446. return global_object.vm().throw_completion<TypeError>(global_object, ErrorType::Convert, "symbol", "number");
  447. case Type::BigInt:
  448. return global_object.vm().throw_completion<TypeError>(global_object, ErrorType::Convert, "BigInt", "number");
  449. case Type::Object: {
  450. auto primitive = TRY(to_primitive(global_object, PreferredType::Number));
  451. return primitive.to_number(global_object);
  452. }
  453. default:
  454. VERIFY_NOT_REACHED();
  455. }
  456. }
  457. // 7.1.13 ToBigInt ( argument ), https://tc39.es/ecma262/#sec-tobigint
  458. ThrowCompletionOr<BigInt*> Value::to_bigint(GlobalObject& global_object) const
  459. {
  460. auto& vm = global_object.vm();
  461. auto primitive = TRY(to_primitive(global_object, PreferredType::Number));
  462. switch (primitive.type()) {
  463. case Type::Undefined:
  464. return vm.throw_completion<TypeError>(global_object, ErrorType::Convert, "undefined", "BigInt");
  465. case Type::Null:
  466. return vm.throw_completion<TypeError>(global_object, ErrorType::Convert, "null", "BigInt");
  467. case Type::Boolean: {
  468. auto value = primitive.as_bool() ? 1 : 0;
  469. return js_bigint(vm, Crypto::SignedBigInteger { value });
  470. }
  471. case Type::BigInt:
  472. return &primitive.as_bigint();
  473. case Type::Int32:
  474. case Type::Double:
  475. return vm.throw_completion<TypeError>(global_object, ErrorType::Convert, "number", "BigInt");
  476. case Type::String: {
  477. auto& string = primitive.as_string().string();
  478. if (!is_valid_bigint_value(string))
  479. return vm.throw_completion<SyntaxError>(global_object, ErrorType::BigIntInvalidValue, string);
  480. return js_bigint(vm, Crypto::SignedBigInteger::from_base(10, string.trim_whitespace()));
  481. }
  482. case Type::Symbol:
  483. return vm.throw_completion<TypeError>(global_object, ErrorType::Convert, "symbol", "BigInt");
  484. default:
  485. VERIFY_NOT_REACHED();
  486. }
  487. }
  488. // 7.1.15 ToBigInt64 ( argument ), https://tc39.es/ecma262/#sec-tobigint64
  489. ThrowCompletionOr<i64> Value::to_bigint_int64(GlobalObject& global_object) const
  490. {
  491. auto* bigint = TRY(to_bigint(global_object));
  492. return static_cast<i64>(bigint->big_integer().to_u64());
  493. }
  494. // 7.1.16 ToBigUint64 ( argument ), https://tc39.es/ecma262/#sec-tobiguint64
  495. ThrowCompletionOr<u64> Value::to_bigint_uint64(GlobalObject& global_object) const
  496. {
  497. auto* bigint = TRY(to_bigint(global_object));
  498. return bigint->big_integer().to_u64();
  499. }
  500. ThrowCompletionOr<double> Value::to_double(GlobalObject& global_object) const
  501. {
  502. return TRY(to_number(global_object)).as_double();
  503. }
  504. // 7.1.19 ToPropertyKey ( argument ), https://tc39.es/ecma262/#sec-topropertykey
  505. ThrowCompletionOr<PropertyKey> Value::to_property_key(GlobalObject& global_object) const
  506. {
  507. if (type() == Type::Int32 && as_i32() >= 0)
  508. return PropertyKey { as_i32() };
  509. auto key = TRY(to_primitive(global_object, PreferredType::String));
  510. if (key.is_symbol())
  511. return &key.as_symbol();
  512. return TRY(key.to_string(global_object));
  513. }
  514. ThrowCompletionOr<i32> Value::to_i32_slow_case(GlobalObject& global_object) const
  515. {
  516. VERIFY(type() != Type::Int32);
  517. double value = TRY(to_number(global_object)).as_double();
  518. if (!isfinite(value) || value == 0)
  519. return 0;
  520. auto abs = fabs(value);
  521. auto int_val = floor(abs);
  522. if (signbit(value))
  523. int_val = -int_val;
  524. auto remainder = fmod(int_val, 4294967296.0);
  525. auto int32bit = remainder >= 0.0 ? remainder : remainder + 4294967296.0; // The notation “x modulo y” computes a value k of the same sign as y
  526. if (int32bit >= 2147483648.0)
  527. int32bit -= 4294967296.0;
  528. return static_cast<i32>(int32bit);
  529. }
  530. ThrowCompletionOr<i32> Value::to_i32(GlobalObject& global_object) const
  531. {
  532. if (m_type == Type::Int32)
  533. return m_value.as_i32;
  534. return to_i32_slow_case(global_object);
  535. }
  536. // 7.1.7 ToUint32 ( argument ), https://tc39.es/ecma262/#sec-touint32
  537. ThrowCompletionOr<u32> Value::to_u32(GlobalObject& global_object) const
  538. {
  539. double value = TRY(to_number(global_object)).as_double();
  540. if (!isfinite(value) || value == 0)
  541. return 0;
  542. auto int_val = floor(fabs(value));
  543. if (signbit(value))
  544. int_val = -int_val;
  545. auto int32bit = fmod(int_val, NumericLimits<u32>::max() + 1.0);
  546. // Cast to i64 here to ensure that the double --> u32 cast doesn't invoke undefined behavior
  547. // Otherwise, negative numbers cause a UBSAN warning.
  548. return static_cast<u32>(static_cast<i64>(int32bit));
  549. }
  550. // 7.1.8 ToInt16 ( argument ), https://tc39.es/ecma262/#sec-toint16
  551. ThrowCompletionOr<i16> Value::to_i16(GlobalObject& global_object) const
  552. {
  553. double value = TRY(to_number(global_object)).as_double();
  554. if (!isfinite(value) || value == 0)
  555. return 0;
  556. auto abs = fabs(value);
  557. auto int_val = floor(abs);
  558. if (signbit(value))
  559. int_val = -int_val;
  560. auto remainder = fmod(int_val, 65536.0);
  561. auto int16bit = remainder >= 0.0 ? remainder : remainder + 65536.0; // The notation “x modulo y” computes a value k of the same sign as y
  562. if (int16bit >= 32768.0)
  563. int16bit -= 65536.0;
  564. return static_cast<i16>(int16bit);
  565. }
  566. // 7.1.9 ToUint16 ( argument ), https://tc39.es/ecma262/#sec-touint16
  567. ThrowCompletionOr<u16> Value::to_u16(GlobalObject& global_object) const
  568. {
  569. double value = TRY(to_number(global_object)).as_double();
  570. if (!isfinite(value) || value == 0)
  571. return 0;
  572. auto int_val = floor(fabs(value));
  573. if (signbit(value))
  574. int_val = -int_val;
  575. auto int16bit = fmod(int_val, NumericLimits<u16>::max() + 1.0);
  576. if (int16bit < 0)
  577. int16bit += NumericLimits<u16>::max() + 1.0;
  578. return static_cast<u16>(int16bit);
  579. }
  580. // 7.1.10 ToInt8 ( argument ), https://tc39.es/ecma262/#sec-toint8
  581. ThrowCompletionOr<i8> Value::to_i8(GlobalObject& global_object) const
  582. {
  583. double value = TRY(to_number(global_object)).as_double();
  584. if (!isfinite(value) || value == 0)
  585. return 0;
  586. auto abs = fabs(value);
  587. auto int_val = floor(abs);
  588. if (signbit(value))
  589. int_val = -int_val;
  590. auto remainder = fmod(int_val, 256.0);
  591. auto int8bit = remainder >= 0.0 ? remainder : remainder + 256.0; // The notation “x modulo y” computes a value k of the same sign as y
  592. if (int8bit >= 128.0)
  593. int8bit -= 256.0;
  594. return static_cast<i8>(int8bit);
  595. }
  596. // 7.1.11 ToUint8 ( argument ), https://tc39.es/ecma262/#sec-touint8
  597. ThrowCompletionOr<u8> Value::to_u8(GlobalObject& global_object) const
  598. {
  599. double value = TRY(to_number(global_object)).as_double();
  600. if (!isfinite(value) || value == 0)
  601. return 0;
  602. auto int_val = floor(fabs(value));
  603. if (signbit(value))
  604. int_val = -int_val;
  605. auto int8bit = fmod(int_val, NumericLimits<u8>::max() + 1.0);
  606. if (int8bit < 0)
  607. int8bit += NumericLimits<u8>::max() + 1.0;
  608. return static_cast<u8>(int8bit);
  609. }
  610. // 7.1.12 ToUint8Clamp ( argument ), https://tc39.es/ecma262/#sec-touint8clamp
  611. ThrowCompletionOr<u8> Value::to_u8_clamp(GlobalObject& global_object) const
  612. {
  613. auto number = TRY(to_number(global_object));
  614. if (number.is_nan())
  615. return 0;
  616. double value = number.as_double();
  617. if (value <= 0.0)
  618. return 0;
  619. if (value >= 255.0)
  620. return 255;
  621. auto int_val = floor(value);
  622. if (int_val + 0.5 < value)
  623. return static_cast<u8>(int_val + 1.0);
  624. if (value < int_val + 0.5)
  625. return static_cast<u8>(int_val);
  626. if (fmod(int_val, 2.0) == 1.0)
  627. return static_cast<u8>(int_val + 1.0);
  628. return static_cast<u8>(int_val);
  629. }
  630. // 7.1.20 ToLength ( argument ), https://tc39.es/ecma262/#sec-tolength
  631. ThrowCompletionOr<size_t> Value::to_length(GlobalObject& global_object) const
  632. {
  633. auto len = TRY(to_integer_or_infinity(global_object));
  634. if (len <= 0)
  635. return 0;
  636. // FIXME: The spec says that this function's output range is 0 - 2^53-1. But we don't want to overflow the size_t.
  637. constexpr double length_limit = sizeof(void*) == 4 ? NumericLimits<size_t>::max() : MAX_ARRAY_LIKE_INDEX;
  638. return min(len, length_limit);
  639. }
  640. // 7.1.22 ToIndex ( argument ), https://tc39.es/ecma262/#sec-toindex
  641. ThrowCompletionOr<size_t> Value::to_index(GlobalObject& global_object) const
  642. {
  643. auto& vm = global_object.vm();
  644. if (is_undefined())
  645. return 0;
  646. auto integer_index = TRY(to_integer_or_infinity(global_object));
  647. if (integer_index < 0)
  648. return vm.throw_completion<RangeError>(global_object, ErrorType::InvalidIndex);
  649. auto index = MUST(Value(integer_index).to_length(global_object));
  650. if (integer_index != index)
  651. return vm.throw_completion<RangeError>(global_object, ErrorType::InvalidIndex);
  652. return index;
  653. }
  654. // 7.1.5 ToIntegerOrInfinity ( argument ), https://tc39.es/ecma262/#sec-tointegerorinfinity
  655. ThrowCompletionOr<double> Value::to_integer_or_infinity(GlobalObject& global_object) const
  656. {
  657. auto number = TRY(to_number(global_object));
  658. if (number.is_nan() || number.as_double() == 0)
  659. return 0;
  660. if (number.is_infinity())
  661. return number.as_double();
  662. auto integer = floor(fabs(number.as_double()));
  663. if (number.as_double() < 0)
  664. integer = -integer;
  665. return integer;
  666. }
  667. // 7.3.3 GetV ( V, P ), https://tc39.es/ecma262/#sec-getv
  668. ThrowCompletionOr<Value> Value::get(GlobalObject& global_object, PropertyKey const& property_name) const
  669. {
  670. // 1. Assert: IsPropertyKey(P) is true.
  671. VERIFY(property_name.is_valid());
  672. // 2. Let O be ? ToObject(V).
  673. auto* object = TRY(to_object(global_object));
  674. // 3. Return ? O.[[Get]](P, V).
  675. return TRY(object->internal_get(property_name, *this));
  676. }
  677. // 7.3.11 GetMethod ( V, P ), https://tc39.es/ecma262/#sec-getmethod
  678. ThrowCompletionOr<FunctionObject*> Value::get_method(GlobalObject& global_object, PropertyKey const& property_name) const
  679. {
  680. auto& vm = global_object.vm();
  681. // 1. Assert: IsPropertyKey(P) is true.
  682. VERIFY(property_name.is_valid());
  683. // 2. Let func be ? GetV(V, P).
  684. auto function = TRY(get(global_object, property_name));
  685. // 3. If func is either undefined or null, return undefined.
  686. if (function.is_nullish())
  687. return nullptr;
  688. // 4. If IsCallable(func) is false, throw a TypeError exception.
  689. if (!function.is_function())
  690. return vm.throw_completion<TypeError>(global_object, ErrorType::NotAFunction, function.to_string_without_side_effects());
  691. // 5. Return func.
  692. return &function.as_function();
  693. }
  694. // 13.10 Relational Operators, https://tc39.es/ecma262/#sec-relational-operators
  695. ThrowCompletionOr<Value> greater_than(GlobalObject& global_object, Value lhs, Value rhs)
  696. {
  697. if (lhs.type() == Value::Type::Int32 && rhs.type() == Value::Type::Int32)
  698. return lhs.as_i32() > rhs.as_i32();
  699. TriState relation = TRY(is_less_than(global_object, false, lhs, rhs));
  700. if (relation == TriState::Unknown)
  701. return Value(false);
  702. return Value(relation == TriState::True);
  703. }
  704. // 13.10 Relational Operators, https://tc39.es/ecma262/#sec-relational-operators
  705. ThrowCompletionOr<Value> greater_than_equals(GlobalObject& global_object, Value lhs, Value rhs)
  706. {
  707. if (lhs.type() == Value::Type::Int32 && rhs.type() == Value::Type::Int32)
  708. return lhs.as_i32() >= rhs.as_i32();
  709. TriState relation = TRY(is_less_than(global_object, true, lhs, rhs));
  710. if (relation == TriState::Unknown || relation == TriState::True)
  711. return Value(false);
  712. return Value(true);
  713. }
  714. // 13.10 Relational Operators, https://tc39.es/ecma262/#sec-relational-operators
  715. ThrowCompletionOr<Value> less_than(GlobalObject& global_object, Value lhs, Value rhs)
  716. {
  717. if (lhs.type() == Value::Type::Int32 && rhs.type() == Value::Type::Int32)
  718. return lhs.as_i32() < rhs.as_i32();
  719. TriState relation = TRY(is_less_than(global_object, true, lhs, rhs));
  720. if (relation == TriState::Unknown)
  721. return Value(false);
  722. return Value(relation == TriState::True);
  723. }
  724. // 13.10 Relational Operators, https://tc39.es/ecma262/#sec-relational-operators
  725. ThrowCompletionOr<Value> less_than_equals(GlobalObject& global_object, Value lhs, Value rhs)
  726. {
  727. if (lhs.type() == Value::Type::Int32 && rhs.type() == Value::Type::Int32)
  728. return lhs.as_i32() <= rhs.as_i32();
  729. TriState relation = TRY(is_less_than(global_object, false, lhs, rhs));
  730. if (relation == TriState::Unknown || relation == TriState::True)
  731. return Value(false);
  732. return Value(true);
  733. }
  734. // 13.12 Binary Bitwise Operators, https://tc39.es/ecma262/#sec-binary-bitwise-operators
  735. ThrowCompletionOr<Value> bitwise_and(GlobalObject& global_object, Value lhs, Value rhs)
  736. {
  737. auto& vm = global_object.vm();
  738. auto lhs_numeric = TRY(lhs.to_numeric(global_object));
  739. auto rhs_numeric = TRY(rhs.to_numeric(global_object));
  740. if (both_number(lhs_numeric, rhs_numeric)) {
  741. if (!lhs_numeric.is_finite_number() || !rhs_numeric.is_finite_number())
  742. return Value(0);
  743. return Value(TRY(lhs_numeric.to_i32(global_object)) & TRY(rhs_numeric.to_i32(global_object)));
  744. }
  745. if (both_bigint(lhs_numeric, rhs_numeric))
  746. return Value(js_bigint(vm, lhs_numeric.as_bigint().big_integer().bitwise_and(rhs_numeric.as_bigint().big_integer())));
  747. return vm.throw_completion<TypeError>(global_object, ErrorType::BigIntBadOperatorOtherType, "bitwise AND");
  748. }
  749. // 13.12 Binary Bitwise Operators, https://tc39.es/ecma262/#sec-binary-bitwise-operators
  750. ThrowCompletionOr<Value> bitwise_or(GlobalObject& global_object, Value lhs, Value rhs)
  751. {
  752. auto& vm = global_object.vm();
  753. auto lhs_numeric = TRY(lhs.to_numeric(global_object));
  754. auto rhs_numeric = TRY(rhs.to_numeric(global_object));
  755. if (both_number(lhs_numeric, rhs_numeric)) {
  756. if (!lhs_numeric.is_finite_number() && !rhs_numeric.is_finite_number())
  757. return Value(0);
  758. if (!lhs_numeric.is_finite_number())
  759. return rhs_numeric;
  760. if (!rhs_numeric.is_finite_number())
  761. return lhs_numeric;
  762. return Value(TRY(lhs_numeric.to_i32(global_object)) | TRY(rhs_numeric.to_i32(global_object)));
  763. }
  764. if (both_bigint(lhs_numeric, rhs_numeric))
  765. return Value(js_bigint(vm, lhs_numeric.as_bigint().big_integer().bitwise_or(rhs_numeric.as_bigint().big_integer())));
  766. return vm.throw_completion<TypeError>(global_object, ErrorType::BigIntBadOperatorOtherType, "bitwise OR");
  767. }
  768. // 13.12 Binary Bitwise Operators, https://tc39.es/ecma262/#sec-binary-bitwise-operators
  769. ThrowCompletionOr<Value> bitwise_xor(GlobalObject& global_object, Value lhs, Value rhs)
  770. {
  771. auto& vm = global_object.vm();
  772. auto lhs_numeric = TRY(lhs.to_numeric(global_object));
  773. auto rhs_numeric = TRY(rhs.to_numeric(global_object));
  774. if (both_number(lhs_numeric, rhs_numeric)) {
  775. if (!lhs_numeric.is_finite_number() && !rhs_numeric.is_finite_number())
  776. return Value(0);
  777. if (!lhs_numeric.is_finite_number())
  778. return rhs_numeric;
  779. if (!rhs_numeric.is_finite_number())
  780. return lhs_numeric;
  781. return Value(TRY(lhs_numeric.to_i32(global_object)) ^ TRY(rhs_numeric.to_i32(global_object)));
  782. }
  783. if (both_bigint(lhs_numeric, rhs_numeric))
  784. return Value(js_bigint(vm, lhs_numeric.as_bigint().big_integer().bitwise_xor(rhs_numeric.as_bigint().big_integer())));
  785. return vm.throw_completion<TypeError>(global_object, ErrorType::BigIntBadOperatorOtherType, "bitwise XOR");
  786. }
  787. // 13.5.6 Bitwise NOT Operator ( ~ ), https://tc39.es/ecma262/#sec-bitwise-not-operator
  788. ThrowCompletionOr<Value> bitwise_not(GlobalObject& global_object, Value lhs)
  789. {
  790. auto& vm = global_object.vm();
  791. auto lhs_numeric = TRY(lhs.to_numeric(global_object));
  792. if (lhs_numeric.is_number())
  793. return Value(~TRY(lhs_numeric.to_i32(global_object)));
  794. return Value(js_bigint(vm, lhs_numeric.as_bigint().big_integer().bitwise_not()));
  795. }
  796. // 13.5.4 Unary + Operator, https://tc39.es/ecma262/#sec-unary-plus-operator
  797. ThrowCompletionOr<Value> unary_plus(GlobalObject& global_object, Value lhs)
  798. {
  799. return TRY(lhs.to_number(global_object));
  800. }
  801. // 13.5.5 Unary - Operator, https://tc39.es/ecma262/#sec-unary-minus-operator
  802. ThrowCompletionOr<Value> unary_minus(GlobalObject& global_object, Value lhs)
  803. {
  804. auto& vm = global_object.vm();
  805. auto lhs_numeric = TRY(lhs.to_numeric(global_object));
  806. if (lhs_numeric.is_number()) {
  807. if (lhs_numeric.is_nan())
  808. return js_nan();
  809. return Value(-lhs_numeric.as_double());
  810. }
  811. if (lhs_numeric.as_bigint().big_integer() == BIGINT_ZERO)
  812. return Value(js_bigint(vm, BIGINT_ZERO));
  813. auto big_integer_negated = lhs_numeric.as_bigint().big_integer();
  814. big_integer_negated.negate();
  815. return Value(js_bigint(vm, big_integer_negated));
  816. }
  817. // 13.9.1 The Left Shift Operator ( << ), https://tc39.es/ecma262/#sec-left-shift-operator
  818. ThrowCompletionOr<Value> left_shift(GlobalObject& global_object, Value lhs, Value rhs)
  819. {
  820. auto& vm = global_object.vm();
  821. auto lhs_numeric = TRY(lhs.to_numeric(global_object));
  822. auto rhs_numeric = TRY(rhs.to_numeric(global_object));
  823. if (both_number(lhs_numeric, rhs_numeric)) {
  824. if (!lhs_numeric.is_finite_number())
  825. return Value(0);
  826. if (!rhs_numeric.is_finite_number())
  827. return lhs_numeric;
  828. // Ok, so this performs toNumber() again but that "can't" throw
  829. auto lhs_i32 = MUST(lhs_numeric.to_i32(global_object));
  830. auto rhs_u32 = MUST(rhs_numeric.to_u32(global_object)) % 32;
  831. return Value(lhs_i32 << rhs_u32);
  832. }
  833. if (both_bigint(lhs_numeric, rhs_numeric)) {
  834. auto multiplier_divisor = Crypto::SignedBigInteger { Crypto::NumberTheory::Power(Crypto::UnsignedBigInteger(2), rhs_numeric.as_bigint().big_integer().unsigned_value()) };
  835. if (rhs_numeric.as_bigint().big_integer().is_negative())
  836. return Value(js_bigint(vm, lhs_numeric.as_bigint().big_integer().divided_by(multiplier_divisor).quotient));
  837. else
  838. return Value(js_bigint(vm, lhs_numeric.as_bigint().big_integer().multiplied_by(multiplier_divisor)));
  839. }
  840. return vm.throw_completion<TypeError>(global_object, ErrorType::BigIntBadOperatorOtherType, "left-shift");
  841. }
  842. // 13.9.2 The Signed Right Shift Operator ( >> ), https://tc39.es/ecma262/#sec-signed-right-shift-operator
  843. ThrowCompletionOr<Value> right_shift(GlobalObject& global_object, Value lhs, Value rhs)
  844. {
  845. auto& vm = global_object.vm();
  846. auto lhs_numeric = TRY(lhs.to_numeric(global_object));
  847. auto rhs_numeric = TRY(rhs.to_numeric(global_object));
  848. if (both_number(lhs_numeric, rhs_numeric)) {
  849. if (!lhs_numeric.is_finite_number())
  850. return Value(0);
  851. if (!rhs_numeric.is_finite_number())
  852. return lhs_numeric;
  853. auto lhs_i32 = MUST(lhs_numeric.to_i32(global_object));
  854. auto rhs_u32 = MUST(rhs_numeric.to_u32(global_object)) % 32;
  855. return Value(lhs_i32 >> rhs_u32);
  856. }
  857. if (both_bigint(lhs_numeric, rhs_numeric)) {
  858. auto rhs_negated = rhs_numeric.as_bigint().big_integer();
  859. rhs_negated.negate();
  860. return left_shift(global_object, lhs, js_bigint(vm, rhs_negated));
  861. }
  862. return vm.throw_completion<TypeError>(global_object, ErrorType::BigIntBadOperatorOtherType, "right-shift");
  863. }
  864. // 13.9.3 The Unsigned Right Shift Operator ( >>> ), https://tc39.es/ecma262/#sec-unsigned-right-shift-operator
  865. ThrowCompletionOr<Value> unsigned_right_shift(GlobalObject& global_object, Value lhs, Value rhs)
  866. {
  867. auto& vm = global_object.vm();
  868. auto lhs_numeric = TRY(lhs.to_numeric(global_object));
  869. auto rhs_numeric = TRY(rhs.to_numeric(global_object));
  870. if (both_number(lhs_numeric, rhs_numeric)) {
  871. if (!lhs_numeric.is_finite_number())
  872. return Value(0);
  873. if (!rhs_numeric.is_finite_number())
  874. return lhs_numeric;
  875. // Ok, so this performs toNumber() again but that "can't" throw
  876. auto lhs_u32 = MUST(lhs_numeric.to_u32(global_object));
  877. auto rhs_u32 = MUST(rhs_numeric.to_u32(global_object)) % 32;
  878. return Value(lhs_u32 >> rhs_u32);
  879. }
  880. return vm.throw_completion<TypeError>(global_object, ErrorType::BigIntBadOperator, "unsigned right-shift");
  881. }
  882. // https://tc39.es/ecma262/#string-concatenation
  883. static PrimitiveString* concatenate_strings(GlobalObject& global_object, PrimitiveString const& lhs, PrimitiveString const& rhs)
  884. {
  885. auto& vm = global_object.vm();
  886. if (lhs.has_utf16_string() && rhs.has_utf16_string()) {
  887. auto const& lhs_string = lhs.utf16_string();
  888. auto const& rhs_string = rhs.utf16_string();
  889. Vector<u16, 1> combined;
  890. combined.ensure_capacity(lhs_string.length_in_code_units() + rhs_string.length_in_code_units());
  891. combined.extend(lhs_string.string());
  892. combined.extend(rhs_string.string());
  893. return js_string(vm, Utf16String(move(combined)));
  894. }
  895. auto const& lhs_string = lhs.string();
  896. auto const& rhs_string = rhs.string();
  897. StringBuilder builder(lhs_string.length() + rhs_string.length());
  898. auto return_combined_strings = [&]() {
  899. builder.append(lhs_string);
  900. builder.append(rhs_string);
  901. return js_string(vm, builder.to_string());
  902. };
  903. // Surrogates encoded as UTF-8 are 3 bytes.
  904. if ((lhs_string.length() < 3) || (rhs_string.length() < 3))
  905. return return_combined_strings();
  906. auto lhs_leading_byte = static_cast<u8>(lhs_string[lhs_string.length() - 3]);
  907. auto rhs_leading_byte = static_cast<u8>(rhs_string[0]);
  908. if ((lhs_leading_byte & 0xf0) != 0xe0)
  909. return return_combined_strings();
  910. if ((rhs_leading_byte & 0xf0) != 0xe0)
  911. return return_combined_strings();
  912. auto high_surrogate = *Utf8View(lhs_string.substring_view(lhs_string.length() - 3)).begin();
  913. auto low_surrogate = *Utf8View(rhs_string).begin();
  914. if (!Utf16View::is_high_surrogate(high_surrogate) || !Utf16View::is_low_surrogate(low_surrogate))
  915. return return_combined_strings();
  916. builder.append(lhs_string.substring_view(0, lhs_string.length() - 3));
  917. builder.append_code_point(Utf16View::decode_surrogate_pair(high_surrogate, low_surrogate));
  918. builder.append(rhs_string.substring_view(3));
  919. return js_string(vm, builder.to_string());
  920. }
  921. // 13.8.1 The Addition Operator ( + ), https://tc39.es/ecma262/#sec-addition-operator-plus
  922. ThrowCompletionOr<Value> add(GlobalObject& global_object, Value lhs, Value rhs)
  923. {
  924. if (both_number(lhs, rhs)) {
  925. if (lhs.type() == Value::Type::Int32 && rhs.type() == Value::Type::Int32) {
  926. Checked<i32> result;
  927. result = MUST(lhs.to_i32(global_object));
  928. result += MUST(rhs.to_i32(global_object));
  929. if (!result.has_overflow())
  930. return Value(result.value());
  931. }
  932. return Value(lhs.as_double() + rhs.as_double());
  933. }
  934. auto& vm = global_object.vm();
  935. auto lhs_primitive = TRY(lhs.to_primitive(global_object));
  936. auto rhs_primitive = TRY(rhs.to_primitive(global_object));
  937. if (lhs_primitive.is_string() || rhs_primitive.is_string()) {
  938. auto lhs_string = TRY(lhs_primitive.to_primitive_string(global_object));
  939. auto rhs_string = TRY(rhs_primitive.to_primitive_string(global_object));
  940. return concatenate_strings(global_object, *lhs_string, *rhs_string);
  941. }
  942. auto lhs_numeric = TRY(lhs_primitive.to_numeric(global_object));
  943. auto rhs_numeric = TRY(rhs_primitive.to_numeric(global_object));
  944. if (both_number(lhs_numeric, rhs_numeric))
  945. return Value(lhs_numeric.as_double() + rhs_numeric.as_double());
  946. if (both_bigint(lhs_numeric, rhs_numeric))
  947. return Value(js_bigint(vm, lhs_numeric.as_bigint().big_integer().plus(rhs_numeric.as_bigint().big_integer())));
  948. return vm.throw_completion<TypeError>(global_object, ErrorType::BigIntBadOperatorOtherType, "addition");
  949. }
  950. // 13.8.2 The Subtraction Operator ( - ), https://tc39.es/ecma262/#sec-subtraction-operator-minus
  951. ThrowCompletionOr<Value> sub(GlobalObject& global_object, Value lhs, Value rhs)
  952. {
  953. auto& vm = global_object.vm();
  954. auto lhs_numeric = TRY(lhs.to_numeric(global_object));
  955. auto rhs_numeric = TRY(rhs.to_numeric(global_object));
  956. if (both_number(lhs_numeric, rhs_numeric))
  957. return Value(lhs_numeric.as_double() - rhs_numeric.as_double());
  958. if (both_bigint(lhs_numeric, rhs_numeric))
  959. return Value(js_bigint(vm, lhs_numeric.as_bigint().big_integer().minus(rhs_numeric.as_bigint().big_integer())));
  960. return vm.throw_completion<TypeError>(global_object, ErrorType::BigIntBadOperatorOtherType, "subtraction");
  961. }
  962. // 13.7 Multiplicative Operators, https://tc39.es/ecma262/#sec-multiplicative-operators
  963. ThrowCompletionOr<Value> mul(GlobalObject& global_object, Value lhs, Value rhs)
  964. {
  965. auto& vm = global_object.vm();
  966. auto lhs_numeric = TRY(lhs.to_numeric(global_object));
  967. auto rhs_numeric = TRY(rhs.to_numeric(global_object));
  968. if (both_number(lhs_numeric, rhs_numeric))
  969. return Value(lhs_numeric.as_double() * rhs_numeric.as_double());
  970. if (both_bigint(lhs_numeric, rhs_numeric))
  971. return Value(js_bigint(vm, lhs_numeric.as_bigint().big_integer().multiplied_by(rhs_numeric.as_bigint().big_integer())));
  972. return vm.throw_completion<TypeError>(global_object, ErrorType::BigIntBadOperatorOtherType, "multiplication");
  973. }
  974. // 13.7 Multiplicative Operators, https://tc39.es/ecma262/#sec-multiplicative-operators
  975. ThrowCompletionOr<Value> div(GlobalObject& global_object, Value lhs, Value rhs)
  976. {
  977. auto& vm = global_object.vm();
  978. auto lhs_numeric = TRY(lhs.to_numeric(global_object));
  979. auto rhs_numeric = TRY(rhs.to_numeric(global_object));
  980. if (both_number(lhs_numeric, rhs_numeric))
  981. return Value(lhs_numeric.as_double() / rhs_numeric.as_double());
  982. if (both_bigint(lhs_numeric, rhs_numeric)) {
  983. if (rhs_numeric.as_bigint().big_integer() == BIGINT_ZERO)
  984. return vm.throw_completion<RangeError>(global_object, ErrorType::DivisionByZero);
  985. return Value(js_bigint(vm, lhs_numeric.as_bigint().big_integer().divided_by(rhs_numeric.as_bigint().big_integer()).quotient));
  986. }
  987. return vm.throw_completion<TypeError>(global_object, ErrorType::BigIntBadOperatorOtherType, "division");
  988. }
  989. // 13.7 Multiplicative Operators, https://tc39.es/ecma262/#sec-multiplicative-operators
  990. ThrowCompletionOr<Value> mod(GlobalObject& global_object, Value lhs, Value rhs)
  991. {
  992. auto& vm = global_object.vm();
  993. auto lhs_numeric = TRY(lhs.to_numeric(global_object));
  994. auto rhs_numeric = TRY(rhs.to_numeric(global_object));
  995. if (both_number(lhs_numeric, rhs_numeric)) {
  996. // 6.1.6.1.6 Number::remainder ( n, d ), https://tc39.es/ecma262/#sec-numeric-types-number-remainder
  997. // 1. If n is NaN or d is NaN, return NaN.
  998. if (lhs_numeric.is_nan() || rhs_numeric.is_nan())
  999. return js_nan();
  1000. // 2. If n is +∞𝔽 or n is -∞𝔽, return NaN.
  1001. if (lhs_numeric.is_positive_infinity() || lhs_numeric.is_negative_infinity())
  1002. return js_nan();
  1003. // 3. If d is +∞𝔽 or d is -∞𝔽, return n.
  1004. if (rhs_numeric.is_positive_infinity() || rhs_numeric.is_negative_infinity())
  1005. return lhs_numeric;
  1006. // 4. If d is +0𝔽 or d is -0𝔽, return NaN.
  1007. if (rhs_numeric.is_positive_zero() || rhs_numeric.is_negative_zero())
  1008. return js_nan();
  1009. // 5. If n is +0𝔽 or n is -0𝔽, return n.
  1010. if (lhs_numeric.is_positive_zero() || lhs_numeric.is_negative_zero())
  1011. return lhs_numeric;
  1012. // 6. Assert: n and d are finite and non-zero.
  1013. auto index = lhs_numeric.as_double();
  1014. auto period = rhs_numeric.as_double();
  1015. auto trunc = (double)(i32)(index / period);
  1016. // 7. Let r be ℝ(n) - (ℝ(d) × q) where q is an integer that is negative if and only if n and d have opposite sign, and whose magnitude is as large as possible without exceeding the magnitude of ℝ(n) / ℝ(d).
  1017. // 8. Return 𝔽(r).
  1018. return Value(index - trunc * period);
  1019. }
  1020. if (both_bigint(lhs_numeric, rhs_numeric)) {
  1021. if (rhs_numeric.as_bigint().big_integer() == BIGINT_ZERO)
  1022. return vm.throw_completion<RangeError>(global_object, ErrorType::DivisionByZero);
  1023. return Value(js_bigint(vm, lhs_numeric.as_bigint().big_integer().divided_by(rhs_numeric.as_bigint().big_integer()).remainder));
  1024. }
  1025. return vm.throw_completion<TypeError>(global_object, ErrorType::BigIntBadOperatorOtherType, "modulo");
  1026. }
  1027. // 13.6 Exponentiation Operator, https://tc39.es/ecma262/#sec-exp-operator
  1028. ThrowCompletionOr<Value> exp(GlobalObject& global_object, Value lhs, Value rhs)
  1029. {
  1030. auto& vm = global_object.vm();
  1031. auto lhs_numeric = TRY(lhs.to_numeric(global_object));
  1032. auto rhs_numeric = TRY(rhs.to_numeric(global_object));
  1033. if (both_number(lhs_numeric, rhs_numeric))
  1034. return Value(pow(lhs_numeric.as_double(), rhs_numeric.as_double()));
  1035. if (both_bigint(lhs_numeric, rhs_numeric)) {
  1036. if (rhs_numeric.as_bigint().big_integer().is_negative())
  1037. return vm.throw_completion<RangeError>(global_object, ErrorType::NegativeExponent);
  1038. return Value(js_bigint(vm, Crypto::NumberTheory::Power(lhs_numeric.as_bigint().big_integer(), rhs_numeric.as_bigint().big_integer())));
  1039. }
  1040. return vm.throw_completion<TypeError>(global_object, ErrorType::BigIntBadOperatorOtherType, "exponentiation");
  1041. }
  1042. ThrowCompletionOr<Value> in(GlobalObject& global_object, Value lhs, Value rhs)
  1043. {
  1044. if (!rhs.is_object())
  1045. return global_object.vm().throw_completion<TypeError>(global_object, ErrorType::InOperatorWithObject);
  1046. auto lhs_property_key = TRY(lhs.to_property_key(global_object));
  1047. return Value(TRY(rhs.as_object().has_property(lhs_property_key)));
  1048. }
  1049. // 13.10.2 InstanceofOperator ( V, target ), https://tc39.es/ecma262/#sec-instanceofoperator
  1050. ThrowCompletionOr<Value> instance_of(GlobalObject& global_object, Value lhs, Value rhs)
  1051. {
  1052. auto& vm = global_object.vm();
  1053. if (!rhs.is_object())
  1054. return vm.throw_completion<TypeError>(global_object, ErrorType::NotAnObject, rhs.to_string_without_side_effects());
  1055. auto has_instance_method = TRY(rhs.get_method(global_object, *vm.well_known_symbol_has_instance()));
  1056. if (has_instance_method) {
  1057. auto has_instance_result = TRY(call(global_object, *has_instance_method, rhs, lhs));
  1058. return Value(has_instance_result.to_boolean());
  1059. }
  1060. if (!rhs.is_function())
  1061. return vm.throw_completion<TypeError>(global_object, ErrorType::NotAFunction, rhs.to_string_without_side_effects());
  1062. return TRY(ordinary_has_instance(global_object, lhs, rhs));
  1063. }
  1064. // 7.3.22 OrdinaryHasInstance ( C, O ), https://tc39.es/ecma262/#sec-ordinaryhasinstance
  1065. ThrowCompletionOr<Value> ordinary_has_instance(GlobalObject& global_object, Value lhs, Value rhs)
  1066. {
  1067. auto& vm = global_object.vm();
  1068. if (!rhs.is_function())
  1069. return Value(false);
  1070. auto& rhs_function = rhs.as_function();
  1071. if (is<BoundFunction>(rhs_function)) {
  1072. auto& bound_target = static_cast<const BoundFunction&>(rhs_function);
  1073. return instance_of(global_object, lhs, Value(&bound_target.bound_target_function()));
  1074. }
  1075. if (!lhs.is_object())
  1076. return Value(false);
  1077. Object* lhs_object = &lhs.as_object();
  1078. auto rhs_prototype = TRY(rhs_function.get(vm.names.prototype));
  1079. if (!rhs_prototype.is_object())
  1080. return vm.throw_completion<TypeError>(global_object, ErrorType::InstanceOfOperatorBadPrototype, rhs.to_string_without_side_effects());
  1081. while (true) {
  1082. lhs_object = TRY(lhs_object->internal_get_prototype_of());
  1083. if (!lhs_object)
  1084. return Value(false);
  1085. if (same_value(rhs_prototype, lhs_object))
  1086. return Value(true);
  1087. }
  1088. }
  1089. // 7.2.10 SameValue ( x, y ), https://tc39.es/ecma262/#sec-samevalue
  1090. bool same_value(Value lhs, Value rhs)
  1091. {
  1092. if (!same_type_for_equality(lhs, rhs))
  1093. return false;
  1094. if (lhs.is_number()) {
  1095. if (lhs.is_nan() && rhs.is_nan())
  1096. return true;
  1097. if (lhs.is_positive_zero() && rhs.is_negative_zero())
  1098. return false;
  1099. if (lhs.is_negative_zero() && rhs.is_positive_zero())
  1100. return false;
  1101. return lhs.as_double() == rhs.as_double();
  1102. }
  1103. if (lhs.is_bigint()) {
  1104. auto lhs_big_integer = lhs.as_bigint().big_integer();
  1105. auto rhs_big_integer = rhs.as_bigint().big_integer();
  1106. if (lhs_big_integer == BIGINT_ZERO && rhs_big_integer == BIGINT_ZERO && lhs_big_integer.is_negative() != rhs_big_integer.is_negative())
  1107. return false;
  1108. return lhs_big_integer == rhs_big_integer;
  1109. }
  1110. return same_value_non_numeric(lhs, rhs);
  1111. }
  1112. // 7.2.11 SameValueZero ( x, y ), https://tc39.es/ecma262/#sec-samevaluezero
  1113. bool same_value_zero(Value lhs, Value rhs)
  1114. {
  1115. if (!same_type_for_equality(lhs, rhs))
  1116. return false;
  1117. if (lhs.is_number()) {
  1118. if (lhs.is_nan() && rhs.is_nan())
  1119. return true;
  1120. return lhs.as_double() == rhs.as_double();
  1121. }
  1122. if (lhs.is_bigint())
  1123. return lhs.as_bigint().big_integer() == rhs.as_bigint().big_integer();
  1124. return same_value_non_numeric(lhs, rhs);
  1125. }
  1126. // 7.2.12 SameValueNonNumeric ( x, y ), https://tc39.es/ecma262/#sec-samevaluenonnumeric
  1127. bool same_value_non_numeric(Value lhs, Value rhs)
  1128. {
  1129. VERIFY(!lhs.is_number() && !lhs.is_bigint());
  1130. VERIFY(same_type_for_equality(lhs, rhs));
  1131. switch (lhs.type()) {
  1132. case Value::Type::Undefined:
  1133. case Value::Type::Null:
  1134. return true;
  1135. case Value::Type::String:
  1136. return lhs.as_string().string() == rhs.as_string().string();
  1137. case Value::Type::Symbol:
  1138. return &lhs.as_symbol() == &rhs.as_symbol();
  1139. case Value::Type::Boolean:
  1140. return lhs.as_bool() == rhs.as_bool();
  1141. case Value::Type::Object:
  1142. return &lhs.as_object() == &rhs.as_object();
  1143. default:
  1144. VERIFY_NOT_REACHED();
  1145. }
  1146. }
  1147. // 7.2.15 IsStrictlyEqual ( x, y ), https://tc39.es/ecma262/#sec-isstrictlyequal
  1148. bool is_strictly_equal(Value lhs, Value rhs)
  1149. {
  1150. if (!same_type_for_equality(lhs, rhs))
  1151. return false;
  1152. if (lhs.is_number()) {
  1153. if (lhs.is_nan() || rhs.is_nan())
  1154. return false;
  1155. if (lhs.as_double() == rhs.as_double())
  1156. return true;
  1157. return false;
  1158. }
  1159. if (lhs.is_bigint())
  1160. return lhs.as_bigint().big_integer() == rhs.as_bigint().big_integer();
  1161. return same_value_non_numeric(lhs, rhs);
  1162. }
  1163. // 7.2.14 IsLooselyEqual ( x, y ), https://tc39.es/ecma262/#sec-islooselyequal
  1164. ThrowCompletionOr<bool> is_loosely_equal(GlobalObject& global_object, Value lhs, Value rhs)
  1165. {
  1166. auto& vm = global_object.vm();
  1167. // 1. If Type(x) is the same as Type(y), then
  1168. if (same_type_for_equality(lhs, rhs)) {
  1169. // a. Return IsStrictlyEqual(x, y).
  1170. return is_strictly_equal(lhs, rhs);
  1171. }
  1172. // 2. If x is null and y is undefined, return true.
  1173. // 3. If x is undefined and y is null, return true.
  1174. if (lhs.is_nullish() && rhs.is_nullish())
  1175. return true;
  1176. // 4. NOTE: This step is replaced in section B.3.6.2.
  1177. // B.3.6.2 Changes to IsLooselyEqual, https://tc39.es/ecma262/#sec-IsHTMLDDA-internal-slot-aec
  1178. // 1. If Type(x) is Object and x has an [[IsHTMLDDA]] internal slot and y is either null or undefined, return true.
  1179. if (lhs.is_object() && lhs.as_object().is_htmldda() && rhs.is_nullish())
  1180. return true;
  1181. // 2. If x is either null or undefined and Type(y) is Object and y has an [[IsHTMLDDA]] internal slot, return true.
  1182. if (lhs.is_nullish() && rhs.is_object() && rhs.as_object().is_htmldda())
  1183. return true;
  1184. // == End of B.3.6.2 ==
  1185. // 5. If Type(x) is Number and Type(y) is String, return IsLooselyEqual(x, ! ToNumber(y)).
  1186. if (lhs.is_number() && rhs.is_string())
  1187. return is_loosely_equal(global_object, lhs, MUST(rhs.to_number(global_object)));
  1188. // 6. If Type(x) is String and Type(y) is Number, return IsLooselyEqual(! ToNumber(x), y).
  1189. if (lhs.is_string() && rhs.is_number())
  1190. return is_loosely_equal(global_object, MUST(lhs.to_number(global_object)), rhs);
  1191. // 7. If Type(x) is BigInt and Type(y) is String, then
  1192. if (lhs.is_bigint() && rhs.is_string()) {
  1193. auto& rhs_string = rhs.as_string().string();
  1194. // a. Let n be ! StringToBigInt(y).
  1195. // b. If n is NaN, return false.
  1196. if (!is_valid_bigint_value(rhs_string))
  1197. return false;
  1198. // c. Return IsLooselyEqual(x, n).
  1199. return is_loosely_equal(global_object, lhs, js_bigint(vm, Crypto::SignedBigInteger::from_base(10, rhs_string)));
  1200. }
  1201. // 8. If Type(x) is String and Type(y) is BigInt, return IsLooselyEqual(y, x).
  1202. if (lhs.is_string() && rhs.is_bigint())
  1203. return is_loosely_equal(global_object, rhs, lhs);
  1204. // 9. If Type(x) is Boolean, return IsLooselyEqual(! ToNumber(x), y).
  1205. if (lhs.is_boolean())
  1206. return is_loosely_equal(global_object, MUST(lhs.to_number(global_object)), rhs);
  1207. // 10. If Type(y) is Boolean, return IsLooselyEqual(x, ! ToNumber(y)).
  1208. if (rhs.is_boolean())
  1209. return is_loosely_equal(global_object, lhs, MUST(rhs.to_number(global_object)));
  1210. // 11. If Type(x) is either String, Number, BigInt, or Symbol and Type(y) is Object, return IsLooselyEqual(x, ? ToPrimitive(y)).
  1211. if ((lhs.is_string() || lhs.is_number() || lhs.is_bigint() || lhs.is_symbol()) && rhs.is_object()) {
  1212. auto rhs_primitive = TRY(rhs.to_primitive(global_object));
  1213. return is_loosely_equal(global_object, lhs, rhs_primitive);
  1214. }
  1215. // 12. If Type(x) is Object and Type(y) is either String, Number, BigInt, or Symbol, return IsLooselyEqual(? ToPrimitive(x), y).
  1216. if (lhs.is_object() && (rhs.is_string() || rhs.is_number() || rhs.is_bigint() || rhs.is_symbol())) {
  1217. auto lhs_primitive = TRY(lhs.to_primitive(global_object));
  1218. return is_loosely_equal(global_object, lhs_primitive, rhs);
  1219. }
  1220. // 13. If Type(x) is BigInt and Type(y) is Number, or if Type(x) is Number and Type(y) is BigInt, then
  1221. if ((lhs.is_bigint() && rhs.is_number()) || (lhs.is_number() && rhs.is_bigint())) {
  1222. // a. If x or y are any of NaN, +∞𝔽, or -∞𝔽, return false.
  1223. if (lhs.is_nan() || lhs.is_infinity() || rhs.is_nan() || rhs.is_infinity())
  1224. return false;
  1225. // b. If ℝ(x) = ℝ(y), return true; otherwise return false.
  1226. if ((lhs.is_number() && !lhs.is_integral_number()) || (rhs.is_number() && !rhs.is_integral_number()))
  1227. return false;
  1228. if (lhs.is_number())
  1229. return Crypto::SignedBigInteger { MUST(lhs.to_i32(global_object)) } == rhs.as_bigint().big_integer();
  1230. else
  1231. return Crypto::SignedBigInteger { MUST(rhs.to_i32(global_object)) } == lhs.as_bigint().big_integer();
  1232. }
  1233. // 14. Return false.
  1234. return false;
  1235. }
  1236. // 7.2.13 IsLessThan ( x, y, LeftFirst ), https://tc39.es/ecma262/#sec-islessthan
  1237. ThrowCompletionOr<TriState> is_less_than(GlobalObject& global_object, bool left_first, Value lhs, Value rhs)
  1238. {
  1239. Value x_primitive;
  1240. Value y_primitive;
  1241. if (left_first) {
  1242. x_primitive = TRY(lhs.to_primitive(global_object, Value::PreferredType::Number));
  1243. y_primitive = TRY(rhs.to_primitive(global_object, Value::PreferredType::Number));
  1244. } else {
  1245. y_primitive = TRY(lhs.to_primitive(global_object, Value::PreferredType::Number));
  1246. x_primitive = TRY(rhs.to_primitive(global_object, Value::PreferredType::Number));
  1247. }
  1248. if (x_primitive.is_string() && y_primitive.is_string()) {
  1249. auto x_string = x_primitive.as_string().string();
  1250. auto y_string = y_primitive.as_string().string();
  1251. Utf8View x_code_points { x_string };
  1252. Utf8View y_code_points { y_string };
  1253. if (x_code_points.starts_with(y_code_points))
  1254. return TriState::False;
  1255. if (y_code_points.starts_with(x_code_points))
  1256. return TriState::True;
  1257. for (auto k = x_code_points.begin(), l = y_code_points.begin();
  1258. k != x_code_points.end() && l != y_code_points.end();
  1259. ++k, ++l) {
  1260. if (*k != *l) {
  1261. if (*k < *l) {
  1262. return TriState::True;
  1263. } else {
  1264. return TriState::False;
  1265. }
  1266. }
  1267. }
  1268. VERIFY_NOT_REACHED();
  1269. }
  1270. if (x_primitive.is_bigint() && y_primitive.is_string()) {
  1271. auto& y_string = y_primitive.as_string().string();
  1272. if (!is_valid_bigint_value(y_string))
  1273. return TriState::Unknown;
  1274. if (x_primitive.as_bigint().big_integer() < Crypto::SignedBigInteger::from_base(10, y_string))
  1275. return TriState::True;
  1276. else
  1277. return TriState::False;
  1278. }
  1279. if (x_primitive.is_string() && y_primitive.is_bigint()) {
  1280. auto& x_string = x_primitive.as_string().string();
  1281. if (!is_valid_bigint_value(x_string))
  1282. return TriState::Unknown;
  1283. if (Crypto::SignedBigInteger::from_base(10, x_string) < y_primitive.as_bigint().big_integer())
  1284. return TriState::True;
  1285. else
  1286. return TriState::False;
  1287. }
  1288. auto x_numeric = TRY(x_primitive.to_numeric(global_object));
  1289. auto y_numeric = TRY(y_primitive.to_numeric(global_object));
  1290. if (x_numeric.is_nan() || y_numeric.is_nan())
  1291. return TriState::Unknown;
  1292. if (x_numeric.is_positive_infinity() || y_numeric.is_negative_infinity())
  1293. return TriState::False;
  1294. if (x_numeric.is_negative_infinity() || y_numeric.is_positive_infinity())
  1295. return TriState::True;
  1296. if (x_numeric.is_number() && y_numeric.is_number()) {
  1297. if (x_numeric.as_double() < y_numeric.as_double())
  1298. return TriState::True;
  1299. else
  1300. return TriState::False;
  1301. }
  1302. if (x_numeric.is_bigint() && y_numeric.is_bigint()) {
  1303. if (x_numeric.as_bigint().big_integer() < y_numeric.as_bigint().big_integer())
  1304. return TriState::True;
  1305. else
  1306. return TriState::False;
  1307. }
  1308. VERIFY((x_numeric.is_number() && y_numeric.is_bigint()) || (x_numeric.is_bigint() && y_numeric.is_number()));
  1309. bool x_lower_than_y;
  1310. if (x_numeric.is_number()) {
  1311. x_lower_than_y = x_numeric.is_integral_number()
  1312. ? Crypto::SignedBigInteger { MUST(x_numeric.to_i32(global_object)) } < y_numeric.as_bigint().big_integer()
  1313. : (Crypto::SignedBigInteger { MUST(x_numeric.to_i32(global_object)) } < y_numeric.as_bigint().big_integer() || Crypto::SignedBigInteger { MUST(x_numeric.to_i32(global_object)) + 1 } < y_numeric.as_bigint().big_integer());
  1314. } else {
  1315. x_lower_than_y = y_numeric.is_integral_number()
  1316. ? x_numeric.as_bigint().big_integer() < Crypto::SignedBigInteger { MUST(y_numeric.to_i32(global_object)) }
  1317. : (x_numeric.as_bigint().big_integer() < Crypto::SignedBigInteger { MUST(y_numeric.to_i32(global_object)) } || x_numeric.as_bigint().big_integer() < Crypto::SignedBigInteger { MUST(y_numeric.to_i32(global_object)) + 1 });
  1318. }
  1319. if (x_lower_than_y)
  1320. return TriState::True;
  1321. else
  1322. return TriState::False;
  1323. }
  1324. // 7.3.21 Invoke ( V, P [ , argumentsList ] ), https://tc39.es/ecma262/#sec-invoke
  1325. ThrowCompletionOr<Value> Value::invoke_internal(GlobalObject& global_object, JS::PropertyKey const& property_name, Optional<MarkedValueList> arguments)
  1326. {
  1327. auto& vm = global_object.vm();
  1328. auto property = TRY(get(global_object, property_name));
  1329. if (!property.is_function())
  1330. return vm.throw_completion<TypeError>(global_object, ErrorType::NotAFunction, property.to_string_without_side_effects());
  1331. return call(global_object, property.as_function(), *this, move(arguments));
  1332. }
  1333. }