12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640 |
- /*
- * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
- * Copyright (c) 2020-2022, Linus Groh <linusg@serenityos.org>
- *
- * SPDX-License-Identifier: BSD-2-Clause
- */
- #include <AK/AllOf.h>
- #include <AK/Assertions.h>
- #include <AK/CharacterTypes.h>
- #include <AK/String.h>
- #include <AK/StringBuilder.h>
- #include <AK/Utf8View.h>
- #include <LibCrypto/BigInt/SignedBigInteger.h>
- #include <LibCrypto/NumberTheory/ModularFunctions.h>
- #include <LibJS/Runtime/AbstractOperations.h>
- #include <LibJS/Runtime/Accessor.h>
- #include <LibJS/Runtime/Array.h>
- #include <LibJS/Runtime/BigInt.h>
- #include <LibJS/Runtime/BigIntObject.h>
- #include <LibJS/Runtime/BooleanObject.h>
- #include <LibJS/Runtime/BoundFunction.h>
- #include <LibJS/Runtime/Completion.h>
- #include <LibJS/Runtime/Error.h>
- #include <LibJS/Runtime/FunctionObject.h>
- #include <LibJS/Runtime/GlobalObject.h>
- #include <LibJS/Runtime/NativeFunction.h>
- #include <LibJS/Runtime/NumberObject.h>
- #include <LibJS/Runtime/Object.h>
- #include <LibJS/Runtime/PrimitiveString.h>
- #include <LibJS/Runtime/ProxyObject.h>
- #include <LibJS/Runtime/RegExpObject.h>
- #include <LibJS/Runtime/StringObject.h>
- #include <LibJS/Runtime/StringPrototype.h>
- #include <LibJS/Runtime/SymbolObject.h>
- #include <LibJS/Runtime/VM.h>
- #include <LibJS/Runtime/Value.h>
- #include <math.h>
- namespace JS {
- static inline bool same_type_for_equality(Value const& lhs, Value const& rhs)
- {
- if (lhs.type() == rhs.type())
- return true;
- if (lhs.is_number() && rhs.is_number())
- return true;
- return false;
- }
- static const Crypto::SignedBigInteger BIGINT_ZERO { 0 };
- ALWAYS_INLINE bool both_number(Value const& lhs, Value const& rhs)
- {
- return lhs.is_number() && rhs.is_number();
- }
- ALWAYS_INLINE bool both_bigint(Value const& lhs, Value const& rhs)
- {
- return lhs.is_bigint() && rhs.is_bigint();
- }
- // 6.1.6.1.20 Number::toString ( x ), https://tc39.es/ecma262/#sec-numeric-types-number-tostring
- static String double_to_string(double d)
- {
- if (isnan(d))
- return "NaN";
- if (d == +0.0 || d == -0.0)
- return "0";
- if (d < +0.0) {
- StringBuilder builder;
- builder.append('-');
- builder.append(double_to_string(-d));
- return builder.to_string();
- }
- if (d == static_cast<double>(INFINITY))
- return "Infinity";
- StringBuilder number_string_builder;
- size_t start_index = 0;
- size_t end_index = 0;
- size_t int_part_end = 0;
- // generate integer part (reversed)
- double int_part;
- double frac_part;
- frac_part = modf(d, &int_part);
- while (int_part > 0) {
- number_string_builder.append('0' + (int)fmod(int_part, 10));
- end_index++;
- int_part = floor(int_part / 10);
- }
- auto reversed_integer_part = number_string_builder.to_string().reverse();
- number_string_builder.clear();
- number_string_builder.append(reversed_integer_part);
- int_part_end = end_index;
- int exponent = 0;
- // generate fractional part
- while (frac_part > 0) {
- double old_frac_part = frac_part;
- frac_part *= 10;
- frac_part = modf(frac_part, &int_part);
- if (old_frac_part == frac_part)
- break;
- number_string_builder.append('0' + (int)int_part);
- end_index++;
- exponent--;
- }
- auto number_string = number_string_builder.to_string();
- // FIXME: Remove this hack.
- // FIXME: Instead find the shortest round-trippable representation.
- // Remove decimals after the 15th position
- if (end_index > int_part_end + 15) {
- exponent += end_index - int_part_end - 15;
- end_index = int_part_end + 15;
- }
- // remove leading zeroes
- while (start_index < end_index && number_string[start_index] == '0') {
- start_index++;
- }
- // remove trailing zeroes
- while (end_index > 0 && number_string[end_index - 1] == '0') {
- end_index--;
- exponent++;
- }
- if (end_index <= start_index)
- return "0";
- auto digits = number_string.substring_view(start_index, end_index - start_index);
- int number_of_digits = end_index - start_index;
- exponent += number_of_digits;
- StringBuilder builder;
- if (number_of_digits <= exponent && exponent <= 21) {
- builder.append(digits);
- builder.append(String::repeated('0', exponent - number_of_digits));
- return builder.to_string();
- }
- if (0 < exponent && exponent <= 21) {
- builder.append(digits.substring_view(0, exponent));
- builder.append('.');
- builder.append(digits.substring_view(exponent));
- return builder.to_string();
- }
- if (-6 < exponent && exponent <= 0) {
- builder.append("0.");
- builder.append(String::repeated('0', -exponent));
- builder.append(digits);
- return builder.to_string();
- }
- if (number_of_digits == 1) {
- builder.append(digits);
- builder.append('e');
- if (exponent - 1 > 0)
- builder.append('+');
- else
- builder.append('-');
- builder.append(String::number(AK::abs(exponent - 1)));
- return builder.to_string();
- }
- builder.append(digits[0]);
- builder.append('.');
- builder.append(digits.substring_view(1));
- builder.append('e');
- if (exponent - 1 > 0)
- builder.append('+');
- else
- builder.append('-');
- builder.append(String::number(AK::abs(exponent - 1)));
- return builder.to_string();
- }
- // 7.2.2 IsArray ( argument ), https://tc39.es/ecma262/#sec-isarray
- ThrowCompletionOr<bool> Value::is_array(GlobalObject& global_object) const
- {
- auto& vm = global_object.vm();
- if (!is_object())
- return false;
- auto& object = as_object();
- if (is<Array>(object))
- return true;
- if (is<ProxyObject>(object)) {
- auto& proxy = static_cast<ProxyObject const&>(object);
- if (proxy.is_revoked())
- return vm.throw_completion<TypeError>(global_object, ErrorType::ProxyRevoked);
- return Value(&proxy.target()).is_array(global_object);
- }
- return false;
- }
- Array& Value::as_array()
- {
- VERIFY(is_object() && is<Array>(as_object()));
- return static_cast<Array&>(*m_value.as_object);
- }
- // 7.2.3 IsCallable ( argument ), https://tc39.es/ecma262/#sec-iscallable
- bool Value::is_function() const
- {
- return is_object() && as_object().is_function();
- }
- FunctionObject& Value::as_function()
- {
- VERIFY(is_function());
- return static_cast<FunctionObject&>(as_object());
- }
- FunctionObject const& Value::as_function() const
- {
- VERIFY(is_function());
- return static_cast<FunctionObject const&>(as_object());
- }
- // 7.2.4 IsConstructor ( argument ), https://tc39.es/ecma262/#sec-isconstructor
- bool Value::is_constructor() const
- {
- // 1. If Type(argument) is not Object, return false.
- if (!is_function())
- return false;
- // 2. If argument has a [[Construct]] internal method, return true.
- if (as_function().has_constructor())
- return true;
- // 3. Return false.
- return false;
- }
- // 7.2.8 IsRegExp ( argument ), https://tc39.es/ecma262/#sec-isregexp
- ThrowCompletionOr<bool> Value::is_regexp(GlobalObject& global_object) const
- {
- if (!is_object())
- return false;
- auto& vm = global_object.vm();
- auto matcher = TRY(as_object().get(*vm.well_known_symbol_match()));
- if (!matcher.is_undefined())
- return matcher.to_boolean();
- return is<RegExpObject>(as_object());
- }
- // 13.5.3 The typeof Operator, https://tc39.es/ecma262/#sec-typeof-operator
- String Value::typeof() const
- {
- switch (m_type) {
- case Value::Type::Undefined:
- return "undefined";
- case Value::Type::Null:
- return "object";
- case Value::Type::Int32:
- case Value::Type::Double:
- return "number";
- case Value::Type::String:
- return "string";
- case Value::Type::Object:
- // B.3.7.3 Changes to the typeof Operator, https://tc39.es/ecma262/#sec-IsHTMLDDA-internal-slot-typeof
- if (as_object().is_htmldda())
- return "undefined";
- if (is_function())
- return "function";
- return "object";
- case Value::Type::Boolean:
- return "boolean";
- case Value::Type::Symbol:
- return "symbol";
- case Value::Type::BigInt:
- return "bigint";
- default:
- VERIFY_NOT_REACHED();
- }
- }
- String Value::to_string_without_side_effects() const
- {
- switch (m_type) {
- case Type::Undefined:
- return "undefined";
- case Type::Null:
- return "null";
- case Type::Boolean:
- return m_value.as_bool ? "true" : "false";
- case Type::Int32:
- return String::number(m_value.as_i32);
- case Type::Double:
- return double_to_string(m_value.as_double);
- case Type::String:
- return m_value.as_string->string();
- case Type::Symbol:
- return m_value.as_symbol->to_string();
- case Type::BigInt:
- return m_value.as_bigint->to_string();
- case Type::Object:
- return String::formatted("[object {}]", as_object().class_name());
- case Type::Accessor:
- return "<accessor>";
- default:
- VERIFY_NOT_REACHED();
- }
- }
- ThrowCompletionOr<PrimitiveString*> Value::to_primitive_string(GlobalObject& global_object)
- {
- if (is_string())
- return &as_string();
- auto string = TRY(to_string(global_object));
- return js_string(global_object.heap(), string);
- }
- // 7.1.17 ToString ( argument ), https://tc39.es/ecma262/#sec-tostring
- ThrowCompletionOr<String> Value::to_string(GlobalObject& global_object) const
- {
- auto& vm = global_object.vm();
- switch (m_type) {
- case Type::Undefined:
- return "undefined"sv;
- case Type::Null:
- return "null"sv;
- case Type::Boolean:
- return m_value.as_bool ? "true"sv : "false"sv;
- case Type::Int32:
- return String::number(m_value.as_i32);
- case Type::Double:
- return double_to_string(m_value.as_double);
- case Type::String:
- return m_value.as_string->string();
- case Type::Symbol:
- return vm.throw_completion<TypeError>(global_object, ErrorType::Convert, "symbol", "string");
- case Type::BigInt:
- return m_value.as_bigint->big_integer().to_base(10);
- case Type::Object: {
- auto primitive_value = TRY(to_primitive(global_object, PreferredType::String));
- return primitive_value.to_string(global_object);
- }
- default:
- VERIFY_NOT_REACHED();
- }
- }
- ThrowCompletionOr<Utf16String> Value::to_utf16_string(GlobalObject& global_object) const
- {
- if (m_type == Type::String)
- return m_value.as_string->utf16_string();
- auto utf8_string = TRY(to_string(global_object));
- return Utf16String(utf8_string);
- }
- // 7.1.2 ToBoolean ( argument ), https://tc39.es/ecma262/#sec-toboolean
- bool Value::to_boolean() const
- {
- switch (m_type) {
- case Type::Undefined:
- case Type::Null:
- return false;
- case Type::Boolean:
- return m_value.as_bool;
- case Type::Int32:
- return m_value.as_i32 != 0;
- case Type::Double:
- if (is_nan())
- return false;
- return m_value.as_double != 0;
- case Type::String:
- return !m_value.as_string->string().is_empty();
- case Type::Symbol:
- return true;
- case Type::BigInt:
- return m_value.as_bigint->big_integer() != BIGINT_ZERO;
- case Type::Object:
- // B.3.7.1 Changes to ToBoolean, https://tc39.es/ecma262/#sec-IsHTMLDDA-internal-slot-to-boolean
- if (m_value.as_object->is_htmldda())
- return false;
- return true;
- default:
- VERIFY_NOT_REACHED();
- }
- }
- // 7.1.1 ToPrimitive ( input [ , preferredType ] ), https://tc39.es/ecma262/#sec-toprimitive
- ThrowCompletionOr<Value> Value::to_primitive(GlobalObject& global_object, PreferredType preferred_type) const
- {
- auto get_hint_for_preferred_type = [&]() -> String {
- switch (preferred_type) {
- case PreferredType::Default:
- return "default";
- case PreferredType::String:
- return "string";
- case PreferredType::Number:
- return "number";
- default:
- VERIFY_NOT_REACHED();
- }
- };
- if (is_object()) {
- auto& vm = global_object.vm();
- auto to_primitive_method = TRY(get_method(global_object, *vm.well_known_symbol_to_primitive()));
- if (to_primitive_method) {
- auto hint = get_hint_for_preferred_type();
- auto result = TRY(call(global_object, *to_primitive_method, *this, js_string(vm, hint)));
- if (!result.is_object())
- return result;
- return vm.throw_completion<TypeError>(global_object, ErrorType::ToPrimitiveReturnedObject, to_string_without_side_effects(), hint);
- }
- if (preferred_type == PreferredType::Default)
- preferred_type = PreferredType::Number;
- return as_object().ordinary_to_primitive(preferred_type);
- }
- return *this;
- }
- // 7.1.18 ToObject ( argument ), https://tc39.es/ecma262/#sec-toobject
- ThrowCompletionOr<Object*> Value::to_object(GlobalObject& global_object) const
- {
- switch (m_type) {
- case Type::Undefined:
- case Type::Null:
- return global_object.vm().throw_completion<TypeError>(global_object, ErrorType::ToObjectNullOrUndefined);
- case Type::Boolean:
- return BooleanObject::create(global_object, m_value.as_bool);
- case Type::Int32:
- case Type::Double:
- return NumberObject::create(global_object, as_double());
- case Type::String:
- return StringObject::create(global_object, *m_value.as_string, *global_object.string_prototype());
- case Type::Symbol:
- return SymbolObject::create(global_object, *m_value.as_symbol);
- case Type::BigInt:
- return BigIntObject::create(global_object, *m_value.as_bigint);
- case Type::Object:
- return &const_cast<Object&>(as_object());
- default:
- VERIFY_NOT_REACHED();
- }
- }
- // 7.1.3 ToNumeric ( value ), https://tc39.es/ecma262/#sec-tonumeric
- FLATTEN ThrowCompletionOr<Value> Value::to_numeric(GlobalObject& global_object) const
- {
- auto primitive = TRY(to_primitive(global_object, Value::PreferredType::Number));
- if (primitive.is_bigint())
- return primitive;
- return primitive.to_number(global_object);
- }
- // 7.1.4 ToNumber ( argument ), https://tc39.es/ecma262/#sec-tonumber
- ThrowCompletionOr<Value> Value::to_number(GlobalObject& global_object) const
- {
- switch (m_type) {
- case Type::Undefined:
- return js_nan();
- case Type::Null:
- return Value(0);
- case Type::Boolean:
- return Value(m_value.as_bool ? 1 : 0);
- case Type::Int32:
- case Type::Double:
- return *this;
- case Type::String: {
- String string = Utf8View(as_string().string()).trim(whitespace_characters, AK::TrimMode::Both).as_string();
- if (string.is_empty())
- return Value(0);
- if (string == "Infinity" || string == "+Infinity")
- return js_infinity();
- if (string == "-Infinity")
- return js_negative_infinity();
- char* endptr;
- auto parsed_double = strtod(string.characters(), &endptr);
- if (*endptr)
- return js_nan();
- return Value(parsed_double);
- }
- case Type::Symbol:
- return global_object.vm().throw_completion<TypeError>(global_object, ErrorType::Convert, "symbol", "number");
- case Type::BigInt:
- return global_object.vm().throw_completion<TypeError>(global_object, ErrorType::Convert, "BigInt", "number");
- case Type::Object: {
- auto primitive = TRY(to_primitive(global_object, PreferredType::Number));
- return primitive.to_number(global_object);
- }
- default:
- VERIFY_NOT_REACHED();
- }
- }
- // 7.1.13 ToBigInt ( argument ), https://tc39.es/ecma262/#sec-tobigint
- ThrowCompletionOr<BigInt*> Value::to_bigint(GlobalObject& global_object) const
- {
- auto& vm = global_object.vm();
- auto primitive = TRY(to_primitive(global_object, PreferredType::Number));
- switch (primitive.type()) {
- case Type::Undefined:
- return vm.throw_completion<TypeError>(global_object, ErrorType::Convert, "undefined", "BigInt");
- case Type::Null:
- return vm.throw_completion<TypeError>(global_object, ErrorType::Convert, "null", "BigInt");
- case Type::Boolean: {
- auto value = primitive.as_bool() ? 1 : 0;
- return js_bigint(vm, Crypto::SignedBigInteger { value });
- }
- case Type::BigInt:
- return &primitive.as_bigint();
- case Type::Int32:
- case Type::Double:
- return vm.throw_completion<TypeError>(global_object, ErrorType::Convert, "number", "BigInt");
- case Type::String: {
- // 1. Let n be StringToBigInt(prim).
- auto bigint = primitive.string_to_bigint(global_object);
- // 2. If n is undefined, throw a SyntaxError exception.
- if (!bigint.has_value())
- return vm.throw_completion<SyntaxError>(global_object, ErrorType::BigIntInvalidValue, primitive);
- // 3. Return n.
- return bigint.release_value();
- }
- case Type::Symbol:
- return vm.throw_completion<TypeError>(global_object, ErrorType::Convert, "symbol", "BigInt");
- default:
- VERIFY_NOT_REACHED();
- }
- }
- struct BigIntParseResult {
- StringView literal;
- u8 base { 10 };
- bool is_negative { false };
- };
- static Optional<BigIntParseResult> parse_bigint_text(StringView text)
- {
- BigIntParseResult result {};
- auto parse_for_prefixed_base = [&](auto lower_prefix, auto upper_prefix, auto validator) {
- if (text.length() <= 2)
- return false;
- if (!text.starts_with(lower_prefix) && !text.starts_with(upper_prefix))
- return false;
- return all_of(text.substring_view(2), validator);
- };
- if (parse_for_prefixed_base("0b"sv, "0B"sv, is_ascii_binary_digit)) {
- result.literal = text.substring_view(2);
- result.base = 2;
- } else if (parse_for_prefixed_base("0o"sv, "0O"sv, is_ascii_octal_digit)) {
- result.literal = text.substring_view(2);
- result.base = 8;
- } else if (parse_for_prefixed_base("0x"sv, "0X"sv, is_ascii_hex_digit)) {
- result.literal = text.substring_view(2);
- result.base = 16;
- } else {
- if (text.starts_with('-')) {
- text = text.substring_view(1);
- result.is_negative = true;
- } else if (text.starts_with('+')) {
- text = text.substring_view(1);
- }
- if (!all_of(text, is_ascii_digit))
- return {};
- result.literal = text;
- result.base = 10;
- }
- return result;
- }
- // 7.1.14 StringToBigInt ( str ), https://tc39.es/ecma262/#sec-stringtobigint
- Optional<BigInt*> Value::string_to_bigint(GlobalObject& global_object) const
- {
- VERIFY(is_string());
- // 1. Let text be StringToCodePoints(str).
- auto text = as_string().string().view().trim_whitespace();
- // 2. Let literal be ParseText(text, StringIntegerLiteral).
- auto result = parse_bigint_text(text);
- // 3. If literal is a List of errors, return undefined.
- if (!result.has_value())
- return {};
- // 4. Let mv be the MV of literal.
- // 5. Assert: mv is an integer.
- auto bigint = Crypto::SignedBigInteger::from_base(result->base, result->literal);
- if (result->is_negative && (bigint != BIGINT_ZERO))
- bigint.negate();
- // 6. Return ℤ(mv).
- return js_bigint(global_object.vm(), move(bigint));
- }
- // 7.1.15 ToBigInt64 ( argument ), https://tc39.es/ecma262/#sec-tobigint64
- ThrowCompletionOr<i64> Value::to_bigint_int64(GlobalObject& global_object) const
- {
- auto* bigint = TRY(to_bigint(global_object));
- return static_cast<i64>(bigint->big_integer().to_u64());
- }
- // 7.1.16 ToBigUint64 ( argument ), https://tc39.es/ecma262/#sec-tobiguint64
- ThrowCompletionOr<u64> Value::to_bigint_uint64(GlobalObject& global_object) const
- {
- auto* bigint = TRY(to_bigint(global_object));
- return bigint->big_integer().to_u64();
- }
- ThrowCompletionOr<double> Value::to_double(GlobalObject& global_object) const
- {
- return TRY(to_number(global_object)).as_double();
- }
- // 7.1.19 ToPropertyKey ( argument ), https://tc39.es/ecma262/#sec-topropertykey
- ThrowCompletionOr<PropertyKey> Value::to_property_key(GlobalObject& global_object) const
- {
- if (type() == Type::Int32 && as_i32() >= 0)
- return PropertyKey { as_i32() };
- auto key = TRY(to_primitive(global_object, PreferredType::String));
- if (key.is_symbol())
- return &key.as_symbol();
- return TRY(key.to_string(global_object));
- }
- ThrowCompletionOr<i32> Value::to_i32_slow_case(GlobalObject& global_object) const
- {
- VERIFY(type() != Type::Int32);
- double value = TRY(to_number(global_object)).as_double();
- if (!isfinite(value) || value == 0)
- return 0;
- auto abs = fabs(value);
- auto int_val = floor(abs);
- if (signbit(value))
- int_val = -int_val;
- auto remainder = fmod(int_val, 4294967296.0);
- auto int32bit = remainder >= 0.0 ? remainder : remainder + 4294967296.0; // The notation “x modulo y” computes a value k of the same sign as y
- if (int32bit >= 2147483648.0)
- int32bit -= 4294967296.0;
- return static_cast<i32>(int32bit);
- }
- ThrowCompletionOr<i32> Value::to_i32(GlobalObject& global_object) const
- {
- if (m_type == Type::Int32)
- return m_value.as_i32;
- return to_i32_slow_case(global_object);
- }
- // 7.1.7 ToUint32 ( argument ), https://tc39.es/ecma262/#sec-touint32
- ThrowCompletionOr<u32> Value::to_u32(GlobalObject& global_object) const
- {
- double value = TRY(to_number(global_object)).as_double();
- if (!isfinite(value) || value == 0)
- return 0;
- auto int_val = floor(fabs(value));
- if (signbit(value))
- int_val = -int_val;
- auto int32bit = fmod(int_val, NumericLimits<u32>::max() + 1.0);
- // Cast to i64 here to ensure that the double --> u32 cast doesn't invoke undefined behavior
- // Otherwise, negative numbers cause a UBSAN warning.
- return static_cast<u32>(static_cast<i64>(int32bit));
- }
- // 7.1.8 ToInt16 ( argument ), https://tc39.es/ecma262/#sec-toint16
- ThrowCompletionOr<i16> Value::to_i16(GlobalObject& global_object) const
- {
- double value = TRY(to_number(global_object)).as_double();
- if (!isfinite(value) || value == 0)
- return 0;
- auto abs = fabs(value);
- auto int_val = floor(abs);
- if (signbit(value))
- int_val = -int_val;
- auto remainder = fmod(int_val, 65536.0);
- auto int16bit = remainder >= 0.0 ? remainder : remainder + 65536.0; // The notation “x modulo y” computes a value k of the same sign as y
- if (int16bit >= 32768.0)
- int16bit -= 65536.0;
- return static_cast<i16>(int16bit);
- }
- // 7.1.9 ToUint16 ( argument ), https://tc39.es/ecma262/#sec-touint16
- ThrowCompletionOr<u16> Value::to_u16(GlobalObject& global_object) const
- {
- double value = TRY(to_number(global_object)).as_double();
- if (!isfinite(value) || value == 0)
- return 0;
- auto int_val = floor(fabs(value));
- if (signbit(value))
- int_val = -int_val;
- auto int16bit = fmod(int_val, NumericLimits<u16>::max() + 1.0);
- if (int16bit < 0)
- int16bit += NumericLimits<u16>::max() + 1.0;
- return static_cast<u16>(int16bit);
- }
- // 7.1.10 ToInt8 ( argument ), https://tc39.es/ecma262/#sec-toint8
- ThrowCompletionOr<i8> Value::to_i8(GlobalObject& global_object) const
- {
- double value = TRY(to_number(global_object)).as_double();
- if (!isfinite(value) || value == 0)
- return 0;
- auto abs = fabs(value);
- auto int_val = floor(abs);
- if (signbit(value))
- int_val = -int_val;
- auto remainder = fmod(int_val, 256.0);
- auto int8bit = remainder >= 0.0 ? remainder : remainder + 256.0; // The notation “x modulo y” computes a value k of the same sign as y
- if (int8bit >= 128.0)
- int8bit -= 256.0;
- return static_cast<i8>(int8bit);
- }
- // 7.1.11 ToUint8 ( argument ), https://tc39.es/ecma262/#sec-touint8
- ThrowCompletionOr<u8> Value::to_u8(GlobalObject& global_object) const
- {
- double value = TRY(to_number(global_object)).as_double();
- if (!isfinite(value) || value == 0)
- return 0;
- auto int_val = floor(fabs(value));
- if (signbit(value))
- int_val = -int_val;
- auto int8bit = fmod(int_val, NumericLimits<u8>::max() + 1.0);
- if (int8bit < 0)
- int8bit += NumericLimits<u8>::max() + 1.0;
- return static_cast<u8>(int8bit);
- }
- // 7.1.12 ToUint8Clamp ( argument ), https://tc39.es/ecma262/#sec-touint8clamp
- ThrowCompletionOr<u8> Value::to_u8_clamp(GlobalObject& global_object) const
- {
- auto number = TRY(to_number(global_object));
- if (number.is_nan())
- return 0;
- double value = number.as_double();
- if (value <= 0.0)
- return 0;
- if (value >= 255.0)
- return 255;
- auto int_val = floor(value);
- if (int_val + 0.5 < value)
- return static_cast<u8>(int_val + 1.0);
- if (value < int_val + 0.5)
- return static_cast<u8>(int_val);
- if (fmod(int_val, 2.0) == 1.0)
- return static_cast<u8>(int_val + 1.0);
- return static_cast<u8>(int_val);
- }
- // 7.1.20 ToLength ( argument ), https://tc39.es/ecma262/#sec-tolength
- ThrowCompletionOr<size_t> Value::to_length(GlobalObject& global_object) const
- {
- auto len = TRY(to_integer_or_infinity(global_object));
- if (len <= 0)
- return 0;
- // 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.
- constexpr double length_limit = sizeof(void*) == 4 ? NumericLimits<size_t>::max() : MAX_ARRAY_LIKE_INDEX;
- return min(len, length_limit);
- }
- // 7.1.22 ToIndex ( argument ), https://tc39.es/ecma262/#sec-toindex
- ThrowCompletionOr<size_t> Value::to_index(GlobalObject& global_object) const
- {
- auto& vm = global_object.vm();
- if (is_undefined())
- return 0;
- auto integer_index = TRY(to_integer_or_infinity(global_object));
- if (integer_index < 0)
- return vm.throw_completion<RangeError>(global_object, ErrorType::InvalidIndex);
- auto index = MUST(Value(integer_index).to_length(global_object));
- if (integer_index != index)
- return vm.throw_completion<RangeError>(global_object, ErrorType::InvalidIndex);
- return index;
- }
- // 7.1.5 ToIntegerOrInfinity ( argument ), https://tc39.es/ecma262/#sec-tointegerorinfinity
- ThrowCompletionOr<double> Value::to_integer_or_infinity(GlobalObject& global_object) const
- {
- auto number = TRY(to_number(global_object));
- if (number.is_nan() || number.as_double() == 0)
- return 0;
- if (number.is_infinity())
- return number.as_double();
- auto integer = floor(fabs(number.as_double()));
- if (number.as_double() < 0 && integer != 0)
- integer = -integer;
- return integer;
- }
- // Standalone variant using plain doubles for cases where we already got numbers and know the AO won't throw.
- double to_integer_or_infinity(double number)
- {
- // 1. Let number be ? ToNumber(argument).
- // 2. If number is NaN, +0𝔽, or -0𝔽, return 0.
- if (isnan(number) || number == 0)
- return 0;
- // 3. If number is +∞𝔽, return +∞.
- if (__builtin_isinf_sign(number) > 0)
- return static_cast<double>(INFINITY);
- // 4. If number is -∞𝔽, return -∞.
- if (__builtin_isinf_sign(number) < 0)
- return static_cast<double>(-INFINITY);
- // 5. Let integer be floor(abs(ℝ(number))).
- auto integer = floor(fabs(number));
- // 6. If number < -0𝔽, set integer to -integer.
- if (number < 0 && integer != 0)
- integer = -integer;
- // 7. Return integer.
- return integer;
- }
- // 7.3.3 GetV ( V, P ), https://tc39.es/ecma262/#sec-getv
- ThrowCompletionOr<Value> Value::get(GlobalObject& global_object, PropertyKey const& property_key) const
- {
- // 1. Assert: IsPropertyKey(P) is true.
- VERIFY(property_key.is_valid());
- // 2. Let O be ? ToObject(V).
- auto* object = TRY(to_object(global_object));
- // 3. Return ? O.[[Get]](P, V).
- return TRY(object->internal_get(property_key, *this));
- }
- // 7.3.11 GetMethod ( V, P ), https://tc39.es/ecma262/#sec-getmethod
- ThrowCompletionOr<FunctionObject*> Value::get_method(GlobalObject& global_object, PropertyKey const& property_key) const
- {
- auto& vm = global_object.vm();
- // 1. Assert: IsPropertyKey(P) is true.
- VERIFY(property_key.is_valid());
- // 2. Let func be ? GetV(V, P).
- auto function = TRY(get(global_object, property_key));
- // 3. If func is either undefined or null, return undefined.
- if (function.is_nullish())
- return nullptr;
- // 4. If IsCallable(func) is false, throw a TypeError exception.
- if (!function.is_function())
- return vm.throw_completion<TypeError>(global_object, ErrorType::NotAFunction, function.to_string_without_side_effects());
- // 5. Return func.
- return &function.as_function();
- }
- // 13.10 Relational Operators, https://tc39.es/ecma262/#sec-relational-operators
- ThrowCompletionOr<Value> greater_than(GlobalObject& global_object, Value lhs, Value rhs)
- {
- if (lhs.type() == Value::Type::Int32 && rhs.type() == Value::Type::Int32)
- return lhs.as_i32() > rhs.as_i32();
- TriState relation = TRY(is_less_than(global_object, false, lhs, rhs));
- if (relation == TriState::Unknown)
- return Value(false);
- return Value(relation == TriState::True);
- }
- // 13.10 Relational Operators, https://tc39.es/ecma262/#sec-relational-operators
- ThrowCompletionOr<Value> greater_than_equals(GlobalObject& global_object, Value lhs, Value rhs)
- {
- if (lhs.type() == Value::Type::Int32 && rhs.type() == Value::Type::Int32)
- return lhs.as_i32() >= rhs.as_i32();
- TriState relation = TRY(is_less_than(global_object, true, lhs, rhs));
- if (relation == TriState::Unknown || relation == TriState::True)
- return Value(false);
- return Value(true);
- }
- // 13.10 Relational Operators, https://tc39.es/ecma262/#sec-relational-operators
- ThrowCompletionOr<Value> less_than(GlobalObject& global_object, Value lhs, Value rhs)
- {
- if (lhs.type() == Value::Type::Int32 && rhs.type() == Value::Type::Int32)
- return lhs.as_i32() < rhs.as_i32();
- TriState relation = TRY(is_less_than(global_object, true, lhs, rhs));
- if (relation == TriState::Unknown)
- return Value(false);
- return Value(relation == TriState::True);
- }
- // 13.10 Relational Operators, https://tc39.es/ecma262/#sec-relational-operators
- ThrowCompletionOr<Value> less_than_equals(GlobalObject& global_object, Value lhs, Value rhs)
- {
- if (lhs.type() == Value::Type::Int32 && rhs.type() == Value::Type::Int32)
- return lhs.as_i32() <= rhs.as_i32();
- TriState relation = TRY(is_less_than(global_object, false, lhs, rhs));
- if (relation == TriState::Unknown || relation == TriState::True)
- return Value(false);
- return Value(true);
- }
- // 13.12 Binary Bitwise Operators, https://tc39.es/ecma262/#sec-binary-bitwise-operators
- ThrowCompletionOr<Value> bitwise_and(GlobalObject& global_object, Value lhs, Value rhs)
- {
- auto& vm = global_object.vm();
- auto lhs_numeric = TRY(lhs.to_numeric(global_object));
- auto rhs_numeric = TRY(rhs.to_numeric(global_object));
- if (both_number(lhs_numeric, rhs_numeric)) {
- if (!lhs_numeric.is_finite_number() || !rhs_numeric.is_finite_number())
- return Value(0);
- return Value(TRY(lhs_numeric.to_i32(global_object)) & TRY(rhs_numeric.to_i32(global_object)));
- }
- if (both_bigint(lhs_numeric, rhs_numeric))
- return Value(js_bigint(vm, lhs_numeric.as_bigint().big_integer().bitwise_and(rhs_numeric.as_bigint().big_integer())));
- return vm.throw_completion<TypeError>(global_object, ErrorType::BigIntBadOperatorOtherType, "bitwise AND");
- }
- // 13.12 Binary Bitwise Operators, https://tc39.es/ecma262/#sec-binary-bitwise-operators
- ThrowCompletionOr<Value> bitwise_or(GlobalObject& global_object, Value lhs, Value rhs)
- {
- auto& vm = global_object.vm();
- auto lhs_numeric = TRY(lhs.to_numeric(global_object));
- auto rhs_numeric = TRY(rhs.to_numeric(global_object));
- if (both_number(lhs_numeric, rhs_numeric)) {
- if (!lhs_numeric.is_finite_number() && !rhs_numeric.is_finite_number())
- return Value(0);
- if (!lhs_numeric.is_finite_number())
- return rhs_numeric;
- if (!rhs_numeric.is_finite_number())
- return lhs_numeric;
- return Value(TRY(lhs_numeric.to_i32(global_object)) | TRY(rhs_numeric.to_i32(global_object)));
- }
- if (both_bigint(lhs_numeric, rhs_numeric))
- return Value(js_bigint(vm, lhs_numeric.as_bigint().big_integer().bitwise_or(rhs_numeric.as_bigint().big_integer())));
- return vm.throw_completion<TypeError>(global_object, ErrorType::BigIntBadOperatorOtherType, "bitwise OR");
- }
- // 13.12 Binary Bitwise Operators, https://tc39.es/ecma262/#sec-binary-bitwise-operators
- ThrowCompletionOr<Value> bitwise_xor(GlobalObject& global_object, Value lhs, Value rhs)
- {
- auto& vm = global_object.vm();
- auto lhs_numeric = TRY(lhs.to_numeric(global_object));
- auto rhs_numeric = TRY(rhs.to_numeric(global_object));
- if (both_number(lhs_numeric, rhs_numeric)) {
- if (!lhs_numeric.is_finite_number() && !rhs_numeric.is_finite_number())
- return Value(0);
- if (!lhs_numeric.is_finite_number())
- return rhs_numeric;
- if (!rhs_numeric.is_finite_number())
- return lhs_numeric;
- return Value(TRY(lhs_numeric.to_i32(global_object)) ^ TRY(rhs_numeric.to_i32(global_object)));
- }
- if (both_bigint(lhs_numeric, rhs_numeric))
- return Value(js_bigint(vm, lhs_numeric.as_bigint().big_integer().bitwise_xor(rhs_numeric.as_bigint().big_integer())));
- return vm.throw_completion<TypeError>(global_object, ErrorType::BigIntBadOperatorOtherType, "bitwise XOR");
- }
- // 13.5.6 Bitwise NOT Operator ( ~ ), https://tc39.es/ecma262/#sec-bitwise-not-operator
- ThrowCompletionOr<Value> bitwise_not(GlobalObject& global_object, Value lhs)
- {
- auto& vm = global_object.vm();
- auto lhs_numeric = TRY(lhs.to_numeric(global_object));
- if (lhs_numeric.is_number())
- return Value(~TRY(lhs_numeric.to_i32(global_object)));
- return Value(js_bigint(vm, lhs_numeric.as_bigint().big_integer().bitwise_not()));
- }
- // 13.5.4 Unary + Operator, https://tc39.es/ecma262/#sec-unary-plus-operator
- ThrowCompletionOr<Value> unary_plus(GlobalObject& global_object, Value lhs)
- {
- return TRY(lhs.to_number(global_object));
- }
- // 13.5.5 Unary - Operator, https://tc39.es/ecma262/#sec-unary-minus-operator
- ThrowCompletionOr<Value> unary_minus(GlobalObject& global_object, Value lhs)
- {
- auto& vm = global_object.vm();
- auto lhs_numeric = TRY(lhs.to_numeric(global_object));
- if (lhs_numeric.is_number()) {
- if (lhs_numeric.is_nan())
- return js_nan();
- return Value(-lhs_numeric.as_double());
- }
- if (lhs_numeric.as_bigint().big_integer() == BIGINT_ZERO)
- return Value(js_bigint(vm, BIGINT_ZERO));
- auto big_integer_negated = lhs_numeric.as_bigint().big_integer();
- big_integer_negated.negate();
- return Value(js_bigint(vm, big_integer_negated));
- }
- // 13.9.1 The Left Shift Operator ( << ), https://tc39.es/ecma262/#sec-left-shift-operator
- ThrowCompletionOr<Value> left_shift(GlobalObject& global_object, Value lhs, Value rhs)
- {
- auto& vm = global_object.vm();
- auto lhs_numeric = TRY(lhs.to_numeric(global_object));
- auto rhs_numeric = TRY(rhs.to_numeric(global_object));
- if (both_number(lhs_numeric, rhs_numeric)) {
- if (!lhs_numeric.is_finite_number())
- return Value(0);
- if (!rhs_numeric.is_finite_number())
- return lhs_numeric;
- // Ok, so this performs toNumber() again but that "can't" throw
- auto lhs_i32 = MUST(lhs_numeric.to_i32(global_object));
- auto rhs_u32 = MUST(rhs_numeric.to_u32(global_object)) % 32;
- return Value(lhs_i32 << rhs_u32);
- }
- if (both_bigint(lhs_numeric, rhs_numeric)) {
- auto multiplier_divisor = Crypto::SignedBigInteger { Crypto::NumberTheory::Power(Crypto::UnsignedBigInteger(2), rhs_numeric.as_bigint().big_integer().unsigned_value()) };
- if (rhs_numeric.as_bigint().big_integer().is_negative())
- return Value(js_bigint(vm, lhs_numeric.as_bigint().big_integer().divided_by(multiplier_divisor).quotient));
- else
- return Value(js_bigint(vm, lhs_numeric.as_bigint().big_integer().multiplied_by(multiplier_divisor)));
- }
- return vm.throw_completion<TypeError>(global_object, ErrorType::BigIntBadOperatorOtherType, "left-shift");
- }
- // 13.9.2 The Signed Right Shift Operator ( >> ), https://tc39.es/ecma262/#sec-signed-right-shift-operator
- ThrowCompletionOr<Value> right_shift(GlobalObject& global_object, Value lhs, Value rhs)
- {
- auto& vm = global_object.vm();
- auto lhs_numeric = TRY(lhs.to_numeric(global_object));
- auto rhs_numeric = TRY(rhs.to_numeric(global_object));
- if (both_number(lhs_numeric, rhs_numeric)) {
- if (!lhs_numeric.is_finite_number())
- return Value(0);
- if (!rhs_numeric.is_finite_number())
- return lhs_numeric;
- auto lhs_i32 = MUST(lhs_numeric.to_i32(global_object));
- auto rhs_u32 = MUST(rhs_numeric.to_u32(global_object)) % 32;
- return Value(lhs_i32 >> rhs_u32);
- }
- if (both_bigint(lhs_numeric, rhs_numeric)) {
- auto rhs_negated = rhs_numeric.as_bigint().big_integer();
- rhs_negated.negate();
- return left_shift(global_object, lhs, js_bigint(vm, rhs_negated));
- }
- return vm.throw_completion<TypeError>(global_object, ErrorType::BigIntBadOperatorOtherType, "right-shift");
- }
- // 13.9.3 The Unsigned Right Shift Operator ( >>> ), https://tc39.es/ecma262/#sec-unsigned-right-shift-operator
- ThrowCompletionOr<Value> unsigned_right_shift(GlobalObject& global_object, Value lhs, Value rhs)
- {
- auto& vm = global_object.vm();
- auto lhs_numeric = TRY(lhs.to_numeric(global_object));
- auto rhs_numeric = TRY(rhs.to_numeric(global_object));
- if (both_number(lhs_numeric, rhs_numeric)) {
- if (!lhs_numeric.is_finite_number())
- return Value(0);
- if (!rhs_numeric.is_finite_number())
- return lhs_numeric;
- // Ok, so this performs toNumber() again but that "can't" throw
- auto lhs_u32 = MUST(lhs_numeric.to_u32(global_object));
- auto rhs_u32 = MUST(rhs_numeric.to_u32(global_object)) % 32;
- return Value(lhs_u32 >> rhs_u32);
- }
- return vm.throw_completion<TypeError>(global_object, ErrorType::BigIntBadOperator, "unsigned right-shift");
- }
- // https://tc39.es/ecma262/#string-concatenation
- static PrimitiveString* concatenate_strings(GlobalObject& global_object, PrimitiveString const& lhs, PrimitiveString const& rhs)
- {
- auto& vm = global_object.vm();
- if (lhs.has_utf16_string() && rhs.has_utf16_string()) {
- auto const& lhs_string = lhs.utf16_string();
- auto const& rhs_string = rhs.utf16_string();
- Vector<u16, 1> combined;
- combined.ensure_capacity(lhs_string.length_in_code_units() + rhs_string.length_in_code_units());
- combined.extend(lhs_string.string());
- combined.extend(rhs_string.string());
- return js_string(vm, Utf16String(move(combined)));
- }
- auto const& lhs_string = lhs.string();
- auto const& rhs_string = rhs.string();
- StringBuilder builder(lhs_string.length() + rhs_string.length());
- auto return_combined_strings = [&]() {
- builder.append(lhs_string);
- builder.append(rhs_string);
- return js_string(vm, builder.to_string());
- };
- // Surrogates encoded as UTF-8 are 3 bytes.
- if ((lhs_string.length() < 3) || (rhs_string.length() < 3))
- return return_combined_strings();
- auto lhs_leading_byte = static_cast<u8>(lhs_string[lhs_string.length() - 3]);
- auto rhs_leading_byte = static_cast<u8>(rhs_string[0]);
- if ((lhs_leading_byte & 0xf0) != 0xe0)
- return return_combined_strings();
- if ((rhs_leading_byte & 0xf0) != 0xe0)
- return return_combined_strings();
- auto high_surrogate = *Utf8View(lhs_string.substring_view(lhs_string.length() - 3)).begin();
- auto low_surrogate = *Utf8View(rhs_string).begin();
- if (!Utf16View::is_high_surrogate(high_surrogate) || !Utf16View::is_low_surrogate(low_surrogate))
- return return_combined_strings();
- builder.append(lhs_string.substring_view(0, lhs_string.length() - 3));
- builder.append_code_point(Utf16View::decode_surrogate_pair(high_surrogate, low_surrogate));
- builder.append(rhs_string.substring_view(3));
- return js_string(vm, builder.to_string());
- }
- // 13.8.1 The Addition Operator ( + ), https://tc39.es/ecma262/#sec-addition-operator-plus
- ThrowCompletionOr<Value> add(GlobalObject& global_object, Value lhs, Value rhs)
- {
- if (both_number(lhs, rhs)) {
- if (lhs.type() == Value::Type::Int32 && rhs.type() == Value::Type::Int32) {
- Checked<i32> result;
- result = MUST(lhs.to_i32(global_object));
- result += MUST(rhs.to_i32(global_object));
- if (!result.has_overflow())
- return Value(result.value());
- }
- return Value(lhs.as_double() + rhs.as_double());
- }
- auto& vm = global_object.vm();
- auto lhs_primitive = TRY(lhs.to_primitive(global_object));
- auto rhs_primitive = TRY(rhs.to_primitive(global_object));
- if (lhs_primitive.is_string() || rhs_primitive.is_string()) {
- auto lhs_string = TRY(lhs_primitive.to_primitive_string(global_object));
- auto rhs_string = TRY(rhs_primitive.to_primitive_string(global_object));
- return concatenate_strings(global_object, *lhs_string, *rhs_string);
- }
- auto lhs_numeric = TRY(lhs_primitive.to_numeric(global_object));
- auto rhs_numeric = TRY(rhs_primitive.to_numeric(global_object));
- if (both_number(lhs_numeric, rhs_numeric))
- return Value(lhs_numeric.as_double() + rhs_numeric.as_double());
- if (both_bigint(lhs_numeric, rhs_numeric))
- return Value(js_bigint(vm, lhs_numeric.as_bigint().big_integer().plus(rhs_numeric.as_bigint().big_integer())));
- return vm.throw_completion<TypeError>(global_object, ErrorType::BigIntBadOperatorOtherType, "addition");
- }
- // 13.8.2 The Subtraction Operator ( - ), https://tc39.es/ecma262/#sec-subtraction-operator-minus
- ThrowCompletionOr<Value> sub(GlobalObject& global_object, Value lhs, Value rhs)
- {
- auto& vm = global_object.vm();
- auto lhs_numeric = TRY(lhs.to_numeric(global_object));
- auto rhs_numeric = TRY(rhs.to_numeric(global_object));
- if (both_number(lhs_numeric, rhs_numeric))
- return Value(lhs_numeric.as_double() - rhs_numeric.as_double());
- if (both_bigint(lhs_numeric, rhs_numeric))
- return Value(js_bigint(vm, lhs_numeric.as_bigint().big_integer().minus(rhs_numeric.as_bigint().big_integer())));
- return vm.throw_completion<TypeError>(global_object, ErrorType::BigIntBadOperatorOtherType, "subtraction");
- }
- // 13.7 Multiplicative Operators, https://tc39.es/ecma262/#sec-multiplicative-operators
- ThrowCompletionOr<Value> mul(GlobalObject& global_object, Value lhs, Value rhs)
- {
- auto& vm = global_object.vm();
- auto lhs_numeric = TRY(lhs.to_numeric(global_object));
- auto rhs_numeric = TRY(rhs.to_numeric(global_object));
- if (both_number(lhs_numeric, rhs_numeric))
- return Value(lhs_numeric.as_double() * rhs_numeric.as_double());
- if (both_bigint(lhs_numeric, rhs_numeric))
- return Value(js_bigint(vm, lhs_numeric.as_bigint().big_integer().multiplied_by(rhs_numeric.as_bigint().big_integer())));
- return vm.throw_completion<TypeError>(global_object, ErrorType::BigIntBadOperatorOtherType, "multiplication");
- }
- // 13.7 Multiplicative Operators, https://tc39.es/ecma262/#sec-multiplicative-operators
- ThrowCompletionOr<Value> div(GlobalObject& global_object, Value lhs, Value rhs)
- {
- auto& vm = global_object.vm();
- auto lhs_numeric = TRY(lhs.to_numeric(global_object));
- auto rhs_numeric = TRY(rhs.to_numeric(global_object));
- if (both_number(lhs_numeric, rhs_numeric))
- return Value(lhs_numeric.as_double() / rhs_numeric.as_double());
- if (both_bigint(lhs_numeric, rhs_numeric)) {
- if (rhs_numeric.as_bigint().big_integer() == BIGINT_ZERO)
- return vm.throw_completion<RangeError>(global_object, ErrorType::DivisionByZero);
- return Value(js_bigint(vm, lhs_numeric.as_bigint().big_integer().divided_by(rhs_numeric.as_bigint().big_integer()).quotient));
- }
- return vm.throw_completion<TypeError>(global_object, ErrorType::BigIntBadOperatorOtherType, "division");
- }
- // 13.7 Multiplicative Operators, https://tc39.es/ecma262/#sec-multiplicative-operators
- ThrowCompletionOr<Value> mod(GlobalObject& global_object, Value lhs, Value rhs)
- {
- auto& vm = global_object.vm();
- auto lhs_numeric = TRY(lhs.to_numeric(global_object));
- auto rhs_numeric = TRY(rhs.to_numeric(global_object));
- if (both_number(lhs_numeric, rhs_numeric)) {
- // 6.1.6.1.6 Number::remainder ( n, d ), https://tc39.es/ecma262/#sec-numeric-types-number-remainder
- // The ECMA specification is describing the mathematical definition of modulus
- // implemented by fmod.
- auto n = lhs_numeric.as_double();
- auto d = rhs_numeric.as_double();
- return Value(fmod(n, d));
- }
- if (both_bigint(lhs_numeric, rhs_numeric)) {
- if (rhs_numeric.as_bigint().big_integer() == BIGINT_ZERO)
- return vm.throw_completion<RangeError>(global_object, ErrorType::DivisionByZero);
- return Value(js_bigint(vm, lhs_numeric.as_bigint().big_integer().divided_by(rhs_numeric.as_bigint().big_integer()).remainder));
- }
- return vm.throw_completion<TypeError>(global_object, ErrorType::BigIntBadOperatorOtherType, "modulo");
- }
- static Value exp_double(Value base, Value exponent)
- {
- VERIFY(both_number(base, exponent));
- if (exponent.is_nan())
- return js_nan();
- if (exponent.is_positive_zero() || exponent.is_negative_zero())
- return Value(1);
- if (base.is_nan())
- return js_nan();
- if (base.is_positive_infinity())
- return exponent.as_double() > 0 ? js_infinity() : Value(0);
- if (base.is_negative_infinity()) {
- auto is_odd_integral_number = exponent.is_integral_number() && (exponent.as_i32() % 2 != 0);
- if (exponent.as_double() > 0)
- return is_odd_integral_number ? js_negative_infinity() : js_infinity();
- else
- return is_odd_integral_number ? Value(-0.0) : Value(0);
- }
- if (base.is_positive_zero())
- return exponent.as_double() > 0 ? Value(0) : js_infinity();
- if (base.is_negative_zero()) {
- auto is_odd_integral_number = exponent.is_integral_number() && (exponent.as_i32() % 2 != 0);
- if (exponent.as_double() > 0)
- return is_odd_integral_number ? Value(-0.0) : Value(0);
- else
- return is_odd_integral_number ? js_negative_infinity() : js_infinity();
- }
- VERIFY(base.is_finite_number() && !base.is_positive_zero() && !base.is_negative_zero());
- if (exponent.is_positive_infinity()) {
- auto absolute_base = fabs(base.as_double());
- if (absolute_base > 1)
- return js_infinity();
- else if (absolute_base == 1)
- return js_nan();
- else if (absolute_base < 1)
- return Value(0);
- }
- if (exponent.is_negative_infinity()) {
- auto absolute_base = fabs(base.as_double());
- if (absolute_base > 1)
- return Value(0);
- else if (absolute_base == 1)
- return js_nan();
- else if (absolute_base < 1)
- return js_infinity();
- }
- VERIFY(exponent.is_finite_number() && !exponent.is_positive_zero() && !exponent.is_negative_zero());
- if (base.as_double() < 0 && !exponent.is_integral_number())
- return js_nan();
- return Value(::pow(base.as_double(), exponent.as_double()));
- }
- // 13.6 Exponentiation Operator, https://tc39.es/ecma262/#sec-exp-operator
- ThrowCompletionOr<Value> exp(GlobalObject& global_object, Value lhs, Value rhs)
- {
- auto& vm = global_object.vm();
- auto lhs_numeric = TRY(lhs.to_numeric(global_object));
- auto rhs_numeric = TRY(rhs.to_numeric(global_object));
- if (both_number(lhs_numeric, rhs_numeric))
- return exp_double(lhs_numeric, rhs_numeric);
- if (both_bigint(lhs_numeric, rhs_numeric)) {
- if (rhs_numeric.as_bigint().big_integer().is_negative())
- return vm.throw_completion<RangeError>(global_object, ErrorType::NegativeExponent);
- return Value(js_bigint(vm, Crypto::NumberTheory::Power(lhs_numeric.as_bigint().big_integer(), rhs_numeric.as_bigint().big_integer())));
- }
- return vm.throw_completion<TypeError>(global_object, ErrorType::BigIntBadOperatorOtherType, "exponentiation");
- }
- ThrowCompletionOr<Value> in(GlobalObject& global_object, Value lhs, Value rhs)
- {
- if (!rhs.is_object())
- return global_object.vm().throw_completion<TypeError>(global_object, ErrorType::InOperatorWithObject);
- auto lhs_property_key = TRY(lhs.to_property_key(global_object));
- return Value(TRY(rhs.as_object().has_property(lhs_property_key)));
- }
- // 13.10.2 InstanceofOperator ( V, target ), https://tc39.es/ecma262/#sec-instanceofoperator
- ThrowCompletionOr<Value> instance_of(GlobalObject& global_object, Value lhs, Value rhs)
- {
- auto& vm = global_object.vm();
- if (!rhs.is_object())
- return vm.throw_completion<TypeError>(global_object, ErrorType::NotAnObject, rhs.to_string_without_side_effects());
- auto has_instance_method = TRY(rhs.get_method(global_object, *vm.well_known_symbol_has_instance()));
- if (has_instance_method) {
- auto has_instance_result = TRY(call(global_object, *has_instance_method, rhs, lhs));
- return Value(has_instance_result.to_boolean());
- }
- if (!rhs.is_function())
- return vm.throw_completion<TypeError>(global_object, ErrorType::NotAFunction, rhs.to_string_without_side_effects());
- return TRY(ordinary_has_instance(global_object, lhs, rhs));
- }
- // 7.3.22 OrdinaryHasInstance ( C, O ), https://tc39.es/ecma262/#sec-ordinaryhasinstance
- ThrowCompletionOr<Value> ordinary_has_instance(GlobalObject& global_object, Value lhs, Value rhs)
- {
- auto& vm = global_object.vm();
- if (!rhs.is_function())
- return Value(false);
- auto& rhs_function = rhs.as_function();
- if (is<BoundFunction>(rhs_function)) {
- auto& bound_target = static_cast<BoundFunction const&>(rhs_function);
- return instance_of(global_object, lhs, Value(&bound_target.bound_target_function()));
- }
- if (!lhs.is_object())
- return Value(false);
- Object* lhs_object = &lhs.as_object();
- auto rhs_prototype = TRY(rhs_function.get(vm.names.prototype));
- if (!rhs_prototype.is_object())
- return vm.throw_completion<TypeError>(global_object, ErrorType::InstanceOfOperatorBadPrototype, rhs.to_string_without_side_effects());
- while (true) {
- lhs_object = TRY(lhs_object->internal_get_prototype_of());
- if (!lhs_object)
- return Value(false);
- if (same_value(rhs_prototype, lhs_object))
- return Value(true);
- }
- }
- // 7.2.10 SameValue ( x, y ), https://tc39.es/ecma262/#sec-samevalue
- bool same_value(Value lhs, Value rhs)
- {
- if (!same_type_for_equality(lhs, rhs))
- return false;
- if (lhs.is_number()) {
- if (lhs.is_nan() && rhs.is_nan())
- return true;
- if (lhs.is_positive_zero() && rhs.is_negative_zero())
- return false;
- if (lhs.is_negative_zero() && rhs.is_positive_zero())
- return false;
- return lhs.as_double() == rhs.as_double();
- }
- if (lhs.is_bigint()) {
- auto lhs_big_integer = lhs.as_bigint().big_integer();
- auto rhs_big_integer = rhs.as_bigint().big_integer();
- if (lhs_big_integer == BIGINT_ZERO && rhs_big_integer == BIGINT_ZERO && lhs_big_integer.is_negative() != rhs_big_integer.is_negative())
- return false;
- return lhs_big_integer == rhs_big_integer;
- }
- return same_value_non_numeric(lhs, rhs);
- }
- // 7.2.11 SameValueZero ( x, y ), https://tc39.es/ecma262/#sec-samevaluezero
- bool same_value_zero(Value lhs, Value rhs)
- {
- if (!same_type_for_equality(lhs, rhs))
- return false;
- if (lhs.is_number()) {
- if (lhs.is_nan() && rhs.is_nan())
- return true;
- return lhs.as_double() == rhs.as_double();
- }
- if (lhs.is_bigint())
- return lhs.as_bigint().big_integer() == rhs.as_bigint().big_integer();
- return same_value_non_numeric(lhs, rhs);
- }
- // 7.2.12 SameValueNonNumeric ( x, y ), https://tc39.es/ecma262/#sec-samevaluenonnumeric
- bool same_value_non_numeric(Value lhs, Value rhs)
- {
- VERIFY(!lhs.is_number() && !lhs.is_bigint());
- VERIFY(same_type_for_equality(lhs, rhs));
- switch (lhs.type()) {
- case Value::Type::Undefined:
- case Value::Type::Null:
- return true;
- case Value::Type::String:
- return lhs.as_string().string() == rhs.as_string().string();
- case Value::Type::Symbol:
- return &lhs.as_symbol() == &rhs.as_symbol();
- case Value::Type::Boolean:
- return lhs.as_bool() == rhs.as_bool();
- case Value::Type::Object:
- return &lhs.as_object() == &rhs.as_object();
- default:
- VERIFY_NOT_REACHED();
- }
- }
- // 7.2.15 IsStrictlyEqual ( x, y ), https://tc39.es/ecma262/#sec-isstrictlyequal
- bool is_strictly_equal(Value lhs, Value rhs)
- {
- if (!same_type_for_equality(lhs, rhs))
- return false;
- if (lhs.is_number()) {
- if (lhs.is_nan() || rhs.is_nan())
- return false;
- if (lhs.as_double() == rhs.as_double())
- return true;
- return false;
- }
- if (lhs.is_bigint())
- return lhs.as_bigint().big_integer() == rhs.as_bigint().big_integer();
- return same_value_non_numeric(lhs, rhs);
- }
- // 7.2.14 IsLooselyEqual ( x, y ), https://tc39.es/ecma262/#sec-islooselyequal
- ThrowCompletionOr<bool> is_loosely_equal(GlobalObject& global_object, Value lhs, Value rhs)
- {
- // 1. If Type(x) is the same as Type(y), then
- if (same_type_for_equality(lhs, rhs)) {
- // a. Return IsStrictlyEqual(x, y).
- return is_strictly_equal(lhs, rhs);
- }
- // 2. If x is null and y is undefined, return true.
- // 3. If x is undefined and y is null, return true.
- if (lhs.is_nullish() && rhs.is_nullish())
- return true;
- // 4. NOTE: This step is replaced in section B.3.6.2.
- // B.3.6.2 Changes to IsLooselyEqual, https://tc39.es/ecma262/#sec-IsHTMLDDA-internal-slot-aec
- // 4. Perform the following steps:
- // a. If Type(x) is Object and x has an [[IsHTMLDDA]] internal slot and y is either null or undefined, return true.
- if (lhs.is_object() && lhs.as_object().is_htmldda() && rhs.is_nullish())
- return true;
- // b. If x is either null or undefined and Type(y) is Object and y has an [[IsHTMLDDA]] internal slot, return true.
- if (lhs.is_nullish() && rhs.is_object() && rhs.as_object().is_htmldda())
- return true;
- // == End of B.3.6.2 ==
- // 5. If Type(x) is Number and Type(y) is String, return ! IsLooselyEqual(x, ! ToNumber(y)).
- if (lhs.is_number() && rhs.is_string())
- return is_loosely_equal(global_object, lhs, MUST(rhs.to_number(global_object)));
- // 6. If Type(x) is String and Type(y) is Number, return ! IsLooselyEqual(! ToNumber(x), y).
- if (lhs.is_string() && rhs.is_number())
- return is_loosely_equal(global_object, MUST(lhs.to_number(global_object)), rhs);
- // 7. If Type(x) is BigInt and Type(y) is String, then
- if (lhs.is_bigint() && rhs.is_string()) {
- // a. Let n be StringToBigInt(y).
- auto bigint = rhs.string_to_bigint(global_object);
- // b. If n is undefined, return false.
- if (!bigint.has_value())
- return false;
- // c. Return ! IsLooselyEqual(x, n).
- return is_loosely_equal(global_object, lhs, *bigint);
- }
- // 8. If Type(x) is String and Type(y) is BigInt, return ! IsLooselyEqual(y, x).
- if (lhs.is_string() && rhs.is_bigint())
- return is_loosely_equal(global_object, rhs, lhs);
- // 9. If Type(x) is Boolean, return ! IsLooselyEqual(! ToNumber(x), y).
- if (lhs.is_boolean())
- return is_loosely_equal(global_object, MUST(lhs.to_number(global_object)), rhs);
- // 10. If Type(y) is Boolean, return ! IsLooselyEqual(x, ! ToNumber(y)).
- if (rhs.is_boolean())
- return is_loosely_equal(global_object, lhs, MUST(rhs.to_number(global_object)));
- // 11. If Type(x) is either String, Number, BigInt, or Symbol and Type(y) is Object, return ! IsLooselyEqual(x, ? ToPrimitive(y)).
- if ((lhs.is_string() || lhs.is_number() || lhs.is_bigint() || lhs.is_symbol()) && rhs.is_object()) {
- auto rhs_primitive = TRY(rhs.to_primitive(global_object));
- return is_loosely_equal(global_object, lhs, rhs_primitive);
- }
- // 12. If Type(x) is Object and Type(y) is either String, Number, BigInt, or Symbol, return ! IsLooselyEqual(? ToPrimitive(x), y).
- if (lhs.is_object() && (rhs.is_string() || rhs.is_number() || rhs.is_bigint() || rhs.is_symbol())) {
- auto lhs_primitive = TRY(lhs.to_primitive(global_object));
- return is_loosely_equal(global_object, lhs_primitive, rhs);
- }
- // 13. If Type(x) is BigInt and Type(y) is Number, or if Type(x) is Number and Type(y) is BigInt, then
- if ((lhs.is_bigint() && rhs.is_number()) || (lhs.is_number() && rhs.is_bigint())) {
- // a. If x or y are any of NaN, +∞𝔽, or -∞𝔽, return false.
- if (lhs.is_nan() || lhs.is_infinity() || rhs.is_nan() || rhs.is_infinity())
- return false;
- // b. If ℝ(x) = ℝ(y), return true; otherwise return false.
- if ((lhs.is_number() && !lhs.is_integral_number()) || (rhs.is_number() && !rhs.is_integral_number()))
- return false;
- if (lhs.is_number())
- return Crypto::SignedBigInteger { MUST(lhs.to_i32(global_object)) } == rhs.as_bigint().big_integer();
- else
- return Crypto::SignedBigInteger { MUST(rhs.to_i32(global_object)) } == lhs.as_bigint().big_integer();
- }
- // 14. Return false.
- return false;
- }
- // 7.2.13 IsLessThan ( x, y, LeftFirst ), https://tc39.es/ecma262/#sec-islessthan
- ThrowCompletionOr<TriState> is_less_than(GlobalObject& global_object, bool left_first, Value lhs, Value rhs)
- {
- Value x_primitive;
- Value y_primitive;
- if (left_first) {
- x_primitive = TRY(lhs.to_primitive(global_object, Value::PreferredType::Number));
- y_primitive = TRY(rhs.to_primitive(global_object, Value::PreferredType::Number));
- } else {
- y_primitive = TRY(lhs.to_primitive(global_object, Value::PreferredType::Number));
- x_primitive = TRY(rhs.to_primitive(global_object, Value::PreferredType::Number));
- }
- if (x_primitive.is_string() && y_primitive.is_string()) {
- auto x_string = x_primitive.as_string().string();
- auto y_string = y_primitive.as_string().string();
- Utf8View x_code_points { x_string };
- Utf8View y_code_points { y_string };
- if (x_code_points.starts_with(y_code_points))
- return TriState::False;
- if (y_code_points.starts_with(x_code_points))
- return TriState::True;
- for (auto k = x_code_points.begin(), l = y_code_points.begin();
- k != x_code_points.end() && l != y_code_points.end();
- ++k, ++l) {
- if (*k != *l) {
- if (*k < *l) {
- return TriState::True;
- } else {
- return TriState::False;
- }
- }
- }
- VERIFY_NOT_REACHED();
- }
- if (x_primitive.is_bigint() && y_primitive.is_string()) {
- auto y_bigint = y_primitive.string_to_bigint(global_object);
- if (!y_bigint.has_value())
- return TriState::Unknown;
- if (x_primitive.as_bigint().big_integer() < (*y_bigint)->big_integer())
- return TriState::True;
- return TriState::False;
- }
- if (x_primitive.is_string() && y_primitive.is_bigint()) {
- auto x_bigint = x_primitive.string_to_bigint(global_object);
- if (!x_bigint.has_value())
- return TriState::Unknown;
- if ((*x_bigint)->big_integer() < y_primitive.as_bigint().big_integer())
- return TriState::True;
- return TriState::False;
- }
- auto x_numeric = TRY(x_primitive.to_numeric(global_object));
- auto y_numeric = TRY(y_primitive.to_numeric(global_object));
- if (x_numeric.is_nan() || y_numeric.is_nan())
- return TriState::Unknown;
- if (x_numeric.is_positive_infinity() || y_numeric.is_negative_infinity())
- return TriState::False;
- if (x_numeric.is_negative_infinity() || y_numeric.is_positive_infinity())
- return TriState::True;
- if (x_numeric.is_number() && y_numeric.is_number()) {
- if (x_numeric.as_double() < y_numeric.as_double())
- return TriState::True;
- else
- return TriState::False;
- }
- if (x_numeric.is_bigint() && y_numeric.is_bigint()) {
- if (x_numeric.as_bigint().big_integer() < y_numeric.as_bigint().big_integer())
- return TriState::True;
- else
- return TriState::False;
- }
- VERIFY((x_numeric.is_number() && y_numeric.is_bigint()) || (x_numeric.is_bigint() && y_numeric.is_number()));
- bool x_lower_than_y;
- if (x_numeric.is_number()) {
- x_lower_than_y = x_numeric.is_integral_number()
- ? Crypto::SignedBigInteger { MUST(x_numeric.to_i32(global_object)) } < y_numeric.as_bigint().big_integer()
- : (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());
- } else {
- x_lower_than_y = y_numeric.is_integral_number()
- ? 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)) } || x_numeric.as_bigint().big_integer() < Crypto::SignedBigInteger { MUST(y_numeric.to_i32(global_object)) + 1 });
- }
- if (x_lower_than_y)
- return TriState::True;
- else
- return TriState::False;
- }
- // 7.3.21 Invoke ( V, P [ , argumentsList ] ), https://tc39.es/ecma262/#sec-invoke
- ThrowCompletionOr<Value> Value::invoke_internal(GlobalObject& global_object, PropertyKey const& property_key, Optional<MarkedVector<Value>> arguments)
- {
- auto& vm = global_object.vm();
- auto property = TRY(get(global_object, property_key));
- if (!property.is_function())
- return vm.throw_completion<TypeError>(global_object, ErrorType::NotAFunction, property.to_string_without_side_effects());
- return call(global_object, property.as_function(), *this, move(arguments));
- }
- }
|