| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475 |
- //-------------------------------------------------------------------------------------------------------
- // Copyright (C) Microsoft. All rights reserved.
- // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
- //-------------------------------------------------------------------------------------------------------
- #include "RuntimeLanguagePch.h"
- #include "RuntimeMathPch.h"
- #include "Library/JavascriptNumberObject.h"
- #include "Library/JavascriptStringObject.h"
- #include "Library/DateImplementation.h"
- #include "Library/JavascriptDate.h"
- using namespace Js;
- static const double k_2to16 = 65536.0;
- static const double k_2to31 = 2147483648.0;
- static const double k_2to32 = 4294967296.0;
- // ES5 9.10 indicates that this method should throw a TypeError if the supplied value is Undefined or Null.
- // Our implementation returns FALSE in this scenario, expecting the caller to throw the TypeError.
- // This allows the caller to provide more context in the error message without having to unnecessarily
- // construct the message string before knowing whether or not the object is coercible.
- BOOL JavascriptConversion::CheckObjectCoercible(Var aValue, ScriptContext* scriptContext)
- {
- return !JavascriptOperators::IsUndefinedOrNull(aValue);
- }
- //ES5 9.11 Undefined, Null, Boolean, Number, String - return false
- //If Object has a [[Call]] internal method, then return true, otherwise return false
- bool JavascriptConversion::IsCallable(Var aValue)
- {
- if (!VarIs<RecyclableObject>(aValue))
- {
- return false;
- }
- return IsCallable(UnsafeVarTo<RecyclableObject>(aValue));
- }
- bool JavascriptConversion::IsCallable(_In_ RecyclableObject* aValue)
- {
- Assert(VarIsCorrectType(aValue));
- JavascriptMethod entryPoint = aValue->GetEntryPoint();
- return RecyclableObject::DefaultEntryPoint != entryPoint;
- }
- //----------------------------------------------------------------------------
- // ES5 9.12 SameValue algorithm implementation.
- // 1. If Type(x) is different from Type(y), return false.
- // 2. If Type(x) is Undefined, return true.
- // 3. If Type(x) is Null, return true.
- // 4. If Type(x) is Number, then.
- // a. If x is NaN and y is NaN, return true.
- // b. If x is +0 and y is -0, return false.
- // c. If x is -0 and y is +0, return false.
- // d. If x is the same number value as y, return true.
- // e. Return false.
- // 5. If Type(x) is String, then return true if x and y are exactly the same sequence of characters (same length and same characters in corresponding positions); otherwise, return false.
- // 6. If Type(x) is Boolean, return true if x and y are both true or both false; otherwise, return false.
- // 7. Return true if x and y refer to the same object. Otherwise, return false.
- //----------------------------------------------------------------------------
- template<bool zero>
- bool JavascriptConversion::SameValueCommon(Var aLeft, Var aRight)
- {
- if (aLeft == aRight)
- {
- return true;
- }
- TypeId leftType = JavascriptOperators::GetTypeId(aLeft);
- if (JavascriptOperators::IsUndefinedOrNullType(leftType))
- {
- return false;
- }
- TypeId rightType = JavascriptOperators::GetTypeId(aRight);
- double dblLeft, dblRight;
- switch (leftType)
- {
- case TypeIds_Integer:
- switch (rightType)
- {
- case TypeIds_Integer:
- return false;
- case TypeIds_Number:
- dblLeft = TaggedInt::ToDouble(aLeft);
- dblRight = JavascriptNumber::GetValue(aRight);
- goto CommonNumber;
- case TypeIds_Int64Number:
- {
- int leftValue = TaggedInt::ToInt32(aLeft);
- __int64 rightValue = UnsafeVarTo<JavascriptInt64Number>(aRight)->GetValue();
- return leftValue == rightValue;
- }
- case TypeIds_UInt64Number:
- {
- int leftValue = TaggedInt::ToInt32(aLeft);
- unsigned __int64 rightValue = VarTo<JavascriptInt64Number>(aRight)->GetValue();
- return leftValue == rightValue;
- }
- }
- break;
- case TypeIds_Int64Number:
- switch (rightType)
- {
- case TypeIds_Integer:
- {
- __int64 leftValue = UnsafeVarTo<JavascriptInt64Number>(aLeft)->GetValue();
- int rightValue = TaggedInt::ToInt32(aRight);
- return leftValue == rightValue;
- }
- case TypeIds_Number:
- dblLeft = (double)UnsafeVarTo<JavascriptInt64Number>(aLeft)->GetValue();
- dblRight = JavascriptNumber::GetValue(aRight);
- goto CommonNumber;
- case TypeIds_Int64Number:
- {
- __int64 leftValue = UnsafeVarTo<JavascriptInt64Number>(aLeft)->GetValue();
- __int64 rightValue = UnsafeVarTo<JavascriptInt64Number>(aRight)->GetValue();
- return leftValue == rightValue;
- }
- case TypeIds_UInt64Number:
- {
- __int64 leftValue = UnsafeVarTo<JavascriptInt64Number>(aLeft)->GetValue();
- unsigned __int64 rightValue = VarTo<JavascriptInt64Number>(aRight)->GetValue();
- return ((unsigned __int64)leftValue == rightValue);
- }
- }
- break;
- case TypeIds_UInt64Number:
- switch (rightType)
- {
- case TypeIds_Integer:
- {
- unsigned __int64 leftValue = UnsafeVarTo<JavascriptUInt64Number>(aLeft)->GetValue();
- __int64 rightValue = TaggedInt::ToInt32(aRight);
- return (leftValue == (unsigned __int64)rightValue);
- }
- case TypeIds_Number:
- dblLeft = (double)UnsafeVarTo<JavascriptUInt64Number>(aLeft)->GetValue();
- dblRight = JavascriptNumber::GetValue(aRight);
- goto CommonNumber;
- case TypeIds_Int64Number:
- {
- unsigned __int64 leftValue = UnsafeVarTo<JavascriptUInt64Number>(aLeft)->GetValue();
- __int64 rightValue = UnsafeVarTo<JavascriptInt64Number>(aRight)->GetValue();
- return (leftValue == (unsigned __int64)rightValue);
- }
- case TypeIds_UInt64Number:
- {
- unsigned __int64 leftValue = UnsafeVarTo<JavascriptUInt64Number>(aLeft)->GetValue();
- unsigned __int64 rightValue = VarTo<JavascriptInt64Number>(aRight)->GetValue();
- return leftValue == rightValue;
- }
- }
- break;
- case TypeIds_Number:
- switch (rightType)
- {
- case TypeIds_Integer:
- dblLeft = JavascriptNumber::GetValue(aLeft);
- dblRight = TaggedInt::ToDouble(aRight);
- goto CommonNumber;
- case TypeIds_Int64Number:
- dblLeft = JavascriptNumber::GetValue(aLeft);
- dblRight = (double)UnsafeVarTo<JavascriptInt64Number>(aRight)->GetValue();
- goto CommonNumber;
- case TypeIds_UInt64Number:
- dblLeft = JavascriptNumber::GetValue(aLeft);
- dblRight = (double)UnsafeVarTo<JavascriptUInt64Number>(aRight)->GetValue();
- goto CommonNumber;
- case TypeIds_Number:
- dblLeft = JavascriptNumber::GetValue(aLeft);
- dblRight = JavascriptNumber::GetValue(aRight);
- CommonNumber:
- if (JavascriptNumber::IsNan(dblLeft) && JavascriptNumber::IsNan(dblRight))
- {
- return true;
- }
- if (zero)
- {
- // SameValueZero(+0,-0) returns true;
- return dblLeft == dblRight;
- }
- else
- {
- // SameValue(+0,-0) returns false;
- return (NumberUtilities::LuLoDbl(dblLeft) == NumberUtilities::LuLoDbl(dblRight) &&
- NumberUtilities::LuHiDbl(dblLeft) == NumberUtilities::LuHiDbl(dblRight));
- }
- }
- break;
- case TypeIds_Boolean:
- return false;
- case TypeIds_String:
- switch (rightType)
- {
- case TypeIds_String:
- return JavascriptString::Equals(UnsafeVarTo<JavascriptString>(aLeft), UnsafeVarTo<JavascriptString>(aRight));
- }
- break;
- #if DBG
- case TypeIds_Symbol:
- if (rightType == TypeIds_Symbol)
- {
- JavascriptSymbol* leftSymbol = UnsafeVarTo<JavascriptSymbol>(aLeft);
- JavascriptSymbol* rightSymbol = UnsafeVarTo<JavascriptSymbol>(aRight);
- Assert(leftSymbol->GetValue() != rightSymbol->GetValue());
- }
- #endif
- default:
- break;
- }
- return false;
- }
- template bool JavascriptConversion::SameValueCommon<false>(Var aLeft, Var aRight);
- template bool JavascriptConversion::SameValueCommon<true>(Var aLeft, Var aRight);
- //----------------------------------------------------------------------------
- // ToObject() takes a value and converts it to an Object type
- // Implementation of ES5 9.9
- // The spec indicates that this method should throw a TypeError if the supplied value is Undefined or Null.
- // Our implementation returns FALSE in this scenario, expecting the caller to throw the TypeError.
- // This allows the caller to provide more context in the error message without having to unnecessarily
- // construct the message string before knowing whether or not the value can be converted to an object.
- //
- // Undefined Return FALSE.
- // Null Return FALSE.
- // Boolean Create a new Boolean object whose [[PrimitiveValue]]
- // internal property is set to the value of the boolean.
- // See 15.6 for a description of Boolean objects.
- // Return TRUE.
- // Number Create a new Number object whose [[PrimitiveValue]]
- // internal property is set to the value of the number.
- // See 15.7 for a description of Number objects.
- // Return TRUE.
- // String Create a new String object whose [[PrimitiveValue]]
- // internal property is set to the value of the string.
- // See 15.5 for a description of String objects.
- // Return TRUE.
- // Object The result is the input argument (no conversion).
- // Return TRUE.
- //----------------------------------------------------------------------------
- BOOL JavascriptConversion::ToObject(Var aValue, ScriptContext* scriptContext, RecyclableObject** object)
- {
- Assert(object);
- switch (JavascriptOperators::GetTypeId(aValue))
- {
- case TypeIds_Undefined:
- case TypeIds_Null:
- return FALSE;
- case TypeIds_Number:
- case TypeIds_Integer:
- *object = scriptContext->GetLibrary()->CreateNumberObject(aValue);
- return TRUE;
- default:
- *object = VarTo<RecyclableObject>(aValue)->ToObject(scriptContext);
- return TRUE;
- }
- }
- //----------------------------------------------------------------------------
- // ToPropertyKey() takes a value and converts it to a property key
- // Implementation of ES6 7.1.14
- //----------------------------------------------------------------------------
- void JavascriptConversion::ToPropertyKey(
- Var argument,
- _In_ ScriptContext* scriptContext,
- _Out_ const PropertyRecord** propertyRecord,
- _Out_opt_ PropertyString** propString)
- {
- Var key = JavascriptConversion::ToPrimitive<JavascriptHint::HintString>(argument, scriptContext);
- PropertyString * propertyString = nullptr;
- if (VarIs<JavascriptSymbol>(key))
- {
- // If we are looking up a property keyed by a symbol, we already have the PropertyId in the symbol
- *propertyRecord = UnsafeVarTo<JavascriptSymbol>(key)->GetValue();
- }
- else
- {
- // For all other types, convert the key into a string and use that as the property name
- JavascriptString * propName = JavascriptConversion::ToString(key, scriptContext);
- propName->GetPropertyRecord(propertyRecord);
- if (VarIs<PropertyString>(propName))
- {
- propertyString = UnsafeVarTo<PropertyString>(propName);
- }
- }
- if (propString)
- {
- *propString = propertyString;
- }
- }
- //----------------------------------------------------------------------------
- // ToPrimitive() takes a value and an optional argument and converts it to a non Object type
- // Implementation of ES5 9.1
- //
- // Undefined:The result equals the input argument (no conversion).
- // Null: The result equals the input argument (no conversion).
- // Boolean: The result equals the input argument (no conversion).
- // Number: The result equals the input argument (no conversion).
- // String: The result equals the input argument (no conversion).
- // VariantDate:Returns the value for variant date by calling ToPrimitive directly.
- // Object: Return a default value for the Object.
- // The default value of an object is retrieved by calling the [[DefaultValue]]
- // internal method of the object, passing the optional hint PreferredType.
- // The behavior of the [[DefaultValue]] internal method is defined by this specification
- // for all native ECMAScript objects (8.12.9).
- //----------------------------------------------------------------------------
- template <JavascriptHint hint>
- Var JavascriptConversion::ToPrimitive(_In_ Var aValue, _In_ ScriptContext * requestContext)
- {
- switch (JavascriptOperators::GetTypeId(aValue))
- {
- case TypeIds_Undefined:
- case TypeIds_Null:
- case TypeIds_Integer:
- case TypeIds_Boolean:
- case TypeIds_Number:
- case TypeIds_String:
- case TypeIds_Symbol:
- return aValue;
- case TypeIds_VariantDate:
- {
- Var result = nullptr;
- if (UnsafeVarTo<JavascriptVariantDate>(aValue)->ToPrimitive(hint, &result, requestContext) != TRUE)
- {
- result = nullptr;
- }
- return result;
- }
- case TypeIds_StringObject:
- {
- JavascriptStringObject * stringObject = UnsafeVarTo<JavascriptStringObject>(aValue);
- ScriptContext * objectScriptContext = stringObject->GetScriptContext();
- if (objectScriptContext->optimizationOverrides.GetSideEffects() & (hint == JavascriptHint::HintString ? SideEffects_ToString : SideEffects_ValueOf))
- {
- return MethodCallToPrimitive<hint>(stringObject, requestContext);
- }
- return CrossSite::MarshalVar(requestContext, stringObject->Unwrap(), objectScriptContext);
- }
- case TypeIds_NumberObject:
- {
- JavascriptNumberObject * numberObject = UnsafeVarTo<JavascriptNumberObject>(aValue);
- ScriptContext * objectScriptContext = numberObject->GetScriptContext();
- if (hint == JavascriptHint::HintString)
- {
- if (objectScriptContext->optimizationOverrides.GetSideEffects() & SideEffects_ToString)
- {
- return MethodCallToPrimitive<hint>(numberObject, requestContext);
- }
- return JavascriptNumber::ToStringRadix10(numberObject->GetValue(), requestContext);
- }
- else
- {
- if (objectScriptContext->optimizationOverrides.GetSideEffects() & SideEffects_ValueOf)
- {
- return MethodCallToPrimitive<hint>(numberObject, requestContext);
- }
- return CrossSite::MarshalVar(requestContext, numberObject->Unwrap(), objectScriptContext);
- }
- }
- case TypeIds_SymbolObject:
- {
- JavascriptSymbolObject* symbolObject = UnsafeVarTo<JavascriptSymbolObject>(aValue);
- return CrossSite::MarshalVar(requestContext, symbolObject->Unwrap(), symbolObject->GetScriptContext());
- }
- case TypeIds_Date:
- case TypeIds_WinRTDate:
- {
- JavascriptDate* dateObject = UnsafeVarTo<JavascriptDate>(aValue);
- if(hint == JavascriptHint::HintNumber)
- {
- if (dateObject->GetScriptContext()->optimizationOverrides.GetSideEffects() & SideEffects_ValueOf)
- {
- // if no Method exists this function falls back to OrdinaryToPrimitive
- // if IsES6ToPrimitiveEnabled flag is off we also fall back to OrdinaryToPrimitive
- return MethodCallToPrimitive<hint>(dateObject, requestContext);
- }
- return JavascriptNumber::ToVarNoCheck(dateObject->GetTime(), requestContext);
- }
- else
- {
- if (dateObject->GetScriptContext()->optimizationOverrides.GetSideEffects() & SideEffects_ToString)
- {
- // if no Method exists this function falls back to OrdinaryToPrimitive
- // if IsES6ToPrimitiveEnabled flag is off we also fall back to OrdinaryToPrimitive
- return MethodCallToPrimitive<hint>(dateObject, requestContext);
- }
- return JavascriptDate::ToString(dateObject, requestContext);
- }
- }
- // convert to JavascriptNumber
- case TypeIds_Int64Number:
- return UnsafeVarTo<JavascriptInt64Number>(aValue)->ToJavascriptNumber();
- case TypeIds_UInt64Number:
- return UnsafeVarTo<JavascriptUInt64Number>(aValue)->ToJavascriptNumber();
- default:
- // if no Method exists this function falls back to OrdinaryToPrimitive
- // if IsES6ToPrimitiveEnabled flag is off we also fall back to OrdinaryToPrimitive
- return MethodCallToPrimitive<hint>(UnsafeVarTo<RecyclableObject>(aValue), requestContext);
- }
- }
- //----------------------------------------------------------------------------
- //https://tc39.github.io/ecma262/#sec-canonicalnumericindexstring
- //1. Assert : Type(argument) is String.
- //2. If argument is "-0", then return -0.
- //3. Let n be ToNumber(argument).
- //4. If SameValue(ToString(n), argument) is false, then return undefined.
- //5. Return n.
- //----------------------------------------------------------------------------
- BOOL JavascriptConversion::CanonicalNumericIndexString(JavascriptString *aValue, double *indexValue, ScriptContext * scriptContext)
- {
- if (JavascriptString::IsNegZero(aValue))
- {
- *indexValue = -0;
- return TRUE;
- }
- double indexDoubleValue = aValue->ToDouble();
- if (JavascriptString::Equals(JavascriptNumber::ToStringRadix10(indexDoubleValue, scriptContext), aValue))
- {
- *indexValue = indexDoubleValue;
- return TRUE;
- }
- return FALSE;
- }
- template <JavascriptHint hint>
- Var JavascriptConversion::MethodCallToPrimitive(_In_ RecyclableObject* value, _In_ ScriptContext * requestContext)
- {
- Var result = nullptr;
- ScriptContext *const scriptContext = value->GetScriptContext();
- //7.3.9 GetMethod(V, P)
- // The abstract operation GetMethod is used to get the value of a specific property of an ECMAScript language value when the value of the
- // property is expected to be a function. The operation is called with arguments V and P where V is the ECMAScript language value, P is the
- // property key. This abstract operation performs the following steps:
- // 1. Assert: IsPropertyKey(P) is true.
- // 2. Let func be ? GetV(V, P).
- // 3. If func is either undefined or null, return undefined.
- // 4. If IsCallable(func) is false, throw a TypeError exception.
- // 5. Return func.
- Var varMethod = nullptr;
- if (!requestContext->GetConfig()->IsES6ToPrimitiveEnabled()
- || JavascriptOperators::CheckIfObjectAndProtoChainHasNoSpecialProperties(value)
- || !JavascriptOperators::GetPropertyReference(value, PropertyIds::_symbolToPrimitive, &varMethod, requestContext)
- || JavascriptOperators::IsUndefinedOrNull(varMethod))
- {
- return OrdinaryToPrimitive<hint>(value, requestContext);
- }
- if (!VarIs<JavascriptFunction>(varMethod))
- {
- // Don't error if we disabled implicit calls
- JavascriptError::TryThrowTypeError(scriptContext, requestContext, JSERR_Property_NeedFunction, requestContext->GetPropertyName(PropertyIds::_symbolToPrimitive)->GetBuffer());
- return requestContext->GetLibrary()->GetNull();
- }
- // Let exoticToPrim be GetMethod(input, @@toPrimitive).
- JavascriptFunction* exoticToPrim = UnsafeVarTo<JavascriptFunction>(varMethod);
- JavascriptString* hintString = nullptr;
- if (hint == JavascriptHint::HintString)
- {
- hintString = requestContext->GetLibrary()->GetStringTypeDisplayString();
- }
- else if (hint == JavascriptHint::HintNumber)
- {
- hintString = requestContext->GetLibrary()->GetNumberTypeDisplayString();
- }
- else
- {
- hintString = requestContext->GetPropertyString(PropertyIds::default_);
- }
- // If exoticToPrim is not undefined, then
- Assert(nullptr != exoticToPrim);
- ThreadContext * threadContext = requestContext->GetThreadContext();
- result = threadContext->ExecuteImplicitCall(exoticToPrim, ImplicitCall_ToPrimitive, [=]()->Js::Var
- {
- // Stack object should have a pre-op bail on implicit call. We shouldn't see them here.
- Assert(!ThreadContext::IsOnStack(value));
- // Let result be the result of calling the[[Call]] internal method of exoticToPrim, with input as thisArgument and(hint) as argumentsList.
- return CALL_FUNCTION(threadContext, exoticToPrim, CallInfo(CallFlags_Value, 2), value, hintString);
- });
- if (!result)
- {
- // There was an implicit call and implicit calls are disabled. This would typically cause a bailout.
- Assert(threadContext->IsDisableImplicitCall());
- return requestContext->GetLibrary()->GetNull();
- }
- Assert(!CrossSite::NeedMarshalVar(result, requestContext));
- // If result is an ECMAScript language value and Type(result) is not Object, then return result.
- if (TaggedInt::Is(result) || !JavascriptOperators::IsObjectType(JavascriptOperators::GetTypeId(result)))
- {
- return result;
- }
- // Else, throw a TypeError exception.
- else
- {
- // Don't error if we disabled implicit calls
- JavascriptError::TryThrowTypeError(scriptContext, requestContext, JSERR_FunctionArgument_Invalid, _u("[Symbol.toPrimitive]"));
- return requestContext->GetLibrary()->GetNull();
- }
- }
- template <JavascriptHint hint>
- Var JavascriptConversion::OrdinaryToPrimitive(_In_ RecyclableObject* value, _In_ ScriptContext* requestContext)
- {
- Var result;
- if (!value->ToPrimitive(hint, &result, requestContext))
- {
- ScriptContext *const scriptContext = value->GetScriptContext();
- int32 hCode;
- switch (hint)
- {
- case JavascriptHint::HintNumber:
- hCode = JSERR_NeedNumber;
- break;
- case JavascriptHint::HintString:
- hCode = JSERR_NeedString;
- break;
- default:
- hCode = VBSERR_OLENoPropOrMethod;
- break;
- }
- JavascriptError::TryThrowTypeError(scriptContext, scriptContext, hCode);
- return requestContext->GetLibrary()->GetNull();
- }
- return result;
- }
- template Var JavascriptConversion::OrdinaryToPrimitive<JavascriptHint::HintNumber>(RecyclableObject* value, ScriptContext* requestContext);
- template Var JavascriptConversion::OrdinaryToPrimitive<JavascriptHint::HintString>(RecyclableObject* value, ScriptContext* requestContext);
- template Var JavascriptConversion::OrdinaryToPrimitive<JavascriptHint::None>(RecyclableObject* value, ScriptContext* requestContext);
- JavascriptString *JavascriptConversion::CoerseString(Var aValue, ScriptContext* scriptContext, const char16* apiNameForErrorMsg)
- {
- JIT_HELPER_REENTRANT_HEADER(Op_CoerseString);
- if (!JavascriptConversion::CheckObjectCoercible(aValue, scriptContext))
- {
- JavascriptError::ThrowTypeError(scriptContext, JSERR_This_NullOrUndefined, apiNameForErrorMsg);
- }
- return ToString(aValue, scriptContext);
- JIT_HELPER_END(Op_CoerseString);
- }
- //----------------------------------------------------------------------------
- // ToString - abstract operation
- // ES5 9.8
- //Input Type Result
- // Undefined
- // "undefined"
- // Null
- // "null"
- // Boolean
- // If the argument is true, then the result is "true". If the argument is false, then the result is "false".
- // Number
- // See 9.8.1 below.
- // String
- // Return the input argument (no conversion)
- // Object
- // Apply the following steps:
- // 1. Let primValue be ToPrimitive(input argument, hint String).
- // 2. Return ToString(primValue).
- //----------------------------------------------------------------------------
- JavascriptString *JavascriptConversion::ToString(Var aValue, ScriptContext* scriptContext)
- {
- JIT_HELPER_REENTRANT_HEADER(Op_ConvString);
- Assert(scriptContext->GetThreadContext()->IsScriptActive());
- BOOL fPrimitiveOnly = false;
- while(true)
- {
- switch (JavascriptOperators::GetTypeId(aValue))
- {
- case TypeIds_Undefined:
- return scriptContext->GetLibrary()->GetUndefinedDisplayString();
- case TypeIds_Null:
- return scriptContext->GetLibrary()->GetNullDisplayString();
- case TypeIds_Integer:
- return scriptContext->GetIntegerString(aValue);
- case TypeIds_Boolean:
- return UnsafeVarTo<JavascriptBoolean>(aValue)->GetValue() ? scriptContext->GetLibrary()->GetTrueDisplayString() : scriptContext->GetLibrary()->GetFalseDisplayString();
- case TypeIds_Number:
- return JavascriptNumber::ToStringRadix10(JavascriptNumber::GetValue(aValue), scriptContext);
- case TypeIds_Int64Number:
- {
- __int64 value = UnsafeVarTo<JavascriptInt64Number>(aValue)->GetValue();
- if (!TaggedInt::IsOverflow(value))
- {
- return scriptContext->GetIntegerString((int)value);
- }
- else
- {
- return JavascriptInt64Number::ToString(aValue, scriptContext);
- }
- }
- case TypeIds_UInt64Number:
- {
- unsigned __int64 value = UnsafeVarTo<JavascriptUInt64Number>(aValue)->GetValue();
- if (!TaggedInt::IsOverflow(value))
- {
- return scriptContext->GetIntegerString((uint)value);
- }
- else
- {
- return JavascriptUInt64Number::ToString(aValue, scriptContext);
- }
- }
- case TypeIds_String:
- {
- ScriptContext* aValueScriptContext = Js::UnsafeVarTo<Js::RecyclableObject>(aValue)->GetScriptContext();
- return UnsafeVarTo<JavascriptString>(CrossSite::MarshalVar(scriptContext,
- aValue, aValueScriptContext));
- }
- case TypeIds_VariantDate:
- return VarTo<JavascriptVariantDate>(aValue)->GetValueString(scriptContext);
- case TypeIds_Symbol:
- return UnsafeVarTo<JavascriptSymbol>(aValue)->ToString(scriptContext);
- case TypeIds_SymbolObject:
- return JavascriptSymbol::ToString(UnsafeVarTo<JavascriptSymbolObject>(aValue)->GetValue(), scriptContext);
- case TypeIds_GlobalObject:
- aValue = static_cast<Js::GlobalObject*>(aValue)->ToThis();
- // fall through
- default:
- {
- AssertMsg(JavascriptOperators::IsObject(aValue), "bad type object in conversion ToString");
- if(fPrimitiveOnly)
- {
- AssertMsg(FALSE, "wrong call in ToString, no dynamic objects should get here");
- JavascriptError::ThrowError(scriptContext, VBSERR_InternalError);
- }
- fPrimitiveOnly = true;
- aValue = ToPrimitive<JavascriptHint::HintString>(aValue, scriptContext);
- }
- }
- }
- JIT_HELPER_END(Op_ConvString);
- }
- JavascriptString *JavascriptConversion::ToLocaleString(Var aValue, ScriptContext* scriptContext)
- {
- switch (JavascriptOperators::GetTypeId(aValue))
- {
- case TypeIds_Undefined:
- return scriptContext->GetLibrary()->GetUndefinedDisplayString();
- case TypeIds_Null:
- return scriptContext->GetLibrary()->GetNullDisplayString();
- case TypeIds_Integer:
- return JavascriptNumber::ToLocaleString(TaggedInt::ToInt32(aValue), scriptContext);
- case TypeIds_Boolean:
- return UnsafeVarTo<JavascriptBoolean>(aValue)->GetValue() ? scriptContext->GetLibrary()->GetTrueDisplayString() : scriptContext->GetLibrary()->GetFalseDisplayString();
- case TypeIds_Int64Number:
- return JavascriptNumber::ToLocaleString((double)UnsafeVarTo<JavascriptInt64Number>(aValue)->GetValue(), scriptContext);
- case TypeIds_UInt64Number:
- return JavascriptNumber::ToLocaleString((double)UnsafeVarTo<JavascriptUInt64Number>(aValue)->GetValue(), scriptContext);
- case TypeIds_Number:
- return JavascriptNumber::ToLocaleString(JavascriptNumber::GetValue(aValue), scriptContext);
- case TypeIds_VariantDate:
- // Legacy behavior was to create an empty object and call toLocaleString on it, which would result in this value
- return scriptContext->GetLibrary()->GetObjectDisplayString();
- case TypeIds_Symbol:
- return UnsafeVarTo<JavascriptSymbol>(aValue)->ToString(scriptContext);
- default:
- {
- RecyclableObject* object = VarTo<RecyclableObject>(aValue);
- Var value = JavascriptOperators::GetProperty(object, PropertyIds::toLocaleString, scriptContext, NULL);
- if (JavascriptConversion::IsCallable(value))
- {
- RecyclableObject* toLocaleStringFunction = VarTo<RecyclableObject>(value);
- Var aResult = scriptContext->GetThreadContext()->ExecuteImplicitCall(toLocaleStringFunction, Js::ImplicitCall_ToPrimitive, [=]()->Js::Var
- {
- return CALL_FUNCTION(scriptContext->GetThreadContext(), toLocaleStringFunction, CallInfo(1), aValue);
- });
- if (VarIs<JavascriptString>(aResult))
- {
- return UnsafeVarTo<JavascriptString>(aResult);
- }
- else
- {
- return JavascriptConversion::ToString(aResult, scriptContext);
- }
- }
- JavascriptError::ThrowTypeError(scriptContext, JSERR_Property_NeedFunction, scriptContext->GetPropertyName(PropertyIds::toLocaleString)->GetBuffer());
- }
- }
- }
- //----------------------------------------------------------------------------
- // ToBoolean_Full:
- // (ES3.0: S9.2):
- //
- // Input Output
- // ----- ------
- // 'undefined' 'false'
- // 'null' 'false'
- // Boolean Value
- // Number 'false' if +0, -0, or Nan
- // 'true' otherwise
- // String 'false' if argument is ""
- // 'true' otherwise
- // Object 'true'
- // Falsy Object 'false'
- //----------------------------------------------------------------------------
- BOOL JavascriptConversion::ToBoolean_Full(Var aValue, ScriptContext* scriptContext)
- {
- AssertMsg(!TaggedInt::Is(aValue), "Should be detected");
- AssertMsg(VarIs<RecyclableObject>(aValue), "Should be handled already");
- auto type = UnsafeVarTo<RecyclableObject>(aValue)->GetType();
- switch (type->GetTypeId())
- {
- case TypeIds_Undefined:
- case TypeIds_Null:
- case TypeIds_VariantDate:
- return false;
- case TypeIds_Symbol:
- return true;
- case TypeIds_Boolean:
- return UnsafeVarTo<JavascriptBoolean>(aValue)->GetValue();
- #if !FLOATVAR
- case TypeIds_Number:
- {
- double value = JavascriptNumber::GetValue(aValue);
- return (!JavascriptNumber::IsNan(value)) && (!JavascriptNumber::IsZero(value));
- }
- #endif
- case TypeIds_Int64Number:
- {
- __int64 value = UnsafeVarTo<JavascriptInt64Number>(aValue)->GetValue();
- return value != 0;
- }
- case TypeIds_UInt64Number:
- {
- unsigned __int64 value = UnsafeVarTo<JavascriptUInt64Number>(aValue)->GetValue();
- return value != 0;
- }
- case TypeIds_String:
- {
- JavascriptString * pstValue = UnsafeVarTo<JavascriptString>(aValue);
- return pstValue->GetLength() > 0;
- }
- default:
- {
- AssertMsg(JavascriptOperators::IsObject(aValue), "bad type object in conversion ToBoolean");
- // Falsy objects evaluate to false when converted to Boolean.
- return !type->IsFalsy();
- }
- }
- }
- void JavascriptConversion::ToFloat_Helper(Var aValue, float *pResult, ScriptContext* scriptContext)
- {
- JIT_HELPER_REENTRANT_HEADER(Op_ConvFloat_Helper);
- *pResult = (float)ToNumber_Full(aValue, scriptContext);
- JIT_HELPER_END(Op_ConvFloat_Helper);
- }
- void JavascriptConversion::ToNumber_Helper(Var aValue, double *pResult, ScriptContext* scriptContext)
- {
- JIT_HELPER_REENTRANT_HEADER(Op_ConvNumber_Helper);
- Assert(Js::JavascriptStackWalker::ValidateTopJitFrame(scriptContext));
- *pResult = ToNumber_Full(aValue, scriptContext);
- JIT_HELPER_END(Op_ConvNumber_Helper);
- }
- // Used for the JIT's float type specialization
- // Convert aValue to double, but only allow primitives. Return false otherwise.
- BOOL JavascriptConversion::ToNumber_FromPrimitive(Var aValue, double *pResult, BOOL allowUndefined, ScriptContext* scriptContext)
- {
- JIT_HELPER_REENTRANT_HEADER(Op_ConvNumber_FromPrimitive);
- Assert(Js::JavascriptStackWalker::ValidateTopJitFrame(scriptContext));
- Assert(!TaggedNumber::Is(aValue));
- RecyclableObject *obj = VarTo<RecyclableObject>(aValue);
- // NOTE: Don't allow strings, otherwise JIT's float type specialization has to worry about concats
- if (obj->GetTypeId() >= TypeIds_String)
- {
- return false;
- }
- if (!allowUndefined && obj->GetTypeId() == TypeIds_Undefined)
- {
- return false;
- }
- *pResult = ToNumber_Full(aValue, scriptContext);
- return true;
- JIT_HELPER_END(Op_ConvNumber_FromPrimitive);
- }
- //----------------------------------------------------------------------------
- // ToNumber_Full:
- // Implements ES6 Draft Rev 26 July 18, 2014
- //
- // Undefined: NaN
- // Null: 0
- // boolean: v==true ? 1 : 0 ;
- // number: v (original number)
- // String: conversion by spec algorithm
- // object: ToNumber(PrimitiveValue(v, hint_number))
- // Symbol: TypeError
- //----------------------------------------------------------------------------
- double JavascriptConversion::ToNumber_Full(Var aValue,ScriptContext* scriptContext)
- {
- AssertMsg(!TaggedInt::Is(aValue), "Should be detected");
- ScriptContext * objectScriptContext = VarIs<RecyclableObject>(aValue) ? UnsafeVarTo<RecyclableObject>(aValue)->GetScriptContext() : nullptr;
- BOOL fPrimitiveOnly = false;
- while(true)
- {
- switch (JavascriptOperators::GetTypeId(aValue))
- {
- case TypeIds_Symbol:
- JavascriptError::TryThrowTypeError(objectScriptContext, scriptContext, JSERR_NeedNumber);
- // Fallthrough to return NaN if exceptions are disabled
- case TypeIds_Undefined:
- return JavascriptNumber::GetValue(scriptContext->GetLibrary()->GetNaN());
- case TypeIds_Null:
- return 0;
- case TypeIds_Integer:
- return TaggedInt::ToDouble(aValue);
- case TypeIds_Boolean:
- return UnsafeVarTo<JavascriptBoolean>(aValue)->GetValue() ? 1 : +0;
- case TypeIds_Number:
- return JavascriptNumber::GetValue(aValue);
- case TypeIds_Int64Number:
- return (double)UnsafeVarTo<JavascriptInt64Number>(aValue)->GetValue();
- case TypeIds_UInt64Number:
- return (double)UnsafeVarTo<JavascriptUInt64Number>(aValue)->GetValue();
- case TypeIds_String:
- return UnsafeVarTo<JavascriptString>(aValue)->ToDouble();
- case TypeIds_VariantDate:
- return Js::DateImplementation::GetTvUtc(Js::DateImplementation::JsLocalTimeFromVarDate(UnsafeVarTo<JavascriptVariantDate>(aValue)->GetValue()), scriptContext);
- default:
- {
- AssertMsg(JavascriptOperators::IsObject(aValue), "bad type object in conversion ToInteger");
- if(fPrimitiveOnly)
- {
- JavascriptError::ThrowError(scriptContext, VBSERR_OLENoPropOrMethod);
- }
- fPrimitiveOnly = true;
- aValue = ToPrimitive<JavascriptHint::HintNumber>(aValue, scriptContext);
- }
- }
- }
- }
- //----------------------------------------------------------------------------
- // second part of the ToInteger() implementation.(ES5.0: S9.4).
- //----------------------------------------------------------------------------
- double JavascriptConversion::ToInteger_Full(Var aValue,ScriptContext* scriptContext)
- {
- AssertMsg(!TaggedInt::Is(aValue), "Should be detected");
- ScriptContext * objectScriptContext = VarIs<RecyclableObject>(aValue) ? UnsafeVarTo<RecyclableObject>(aValue)->GetScriptContext() : nullptr;
- BOOL fPrimitiveOnly = false;
- while(true)
- {
- switch (JavascriptOperators::GetTypeId(aValue))
- {
- case TypeIds_Symbol:
- JavascriptError::TryThrowTypeError(objectScriptContext, scriptContext, JSERR_NeedNumber);
- // Fallthrough to return 0 if exceptions are disabled
- case TypeIds_Undefined:
- case TypeIds_Null:
- return 0;
- case TypeIds_Integer:
- return TaggedInt::ToInt32(aValue);
- case TypeIds_Boolean:
- return UnsafeVarTo<JavascriptBoolean>(aValue)->GetValue() ? 1 : +0;
- case TypeIds_Number:
- return ToInteger(JavascriptNumber::GetValue(aValue));
- case TypeIds_Int64Number:
- return ToInteger((double)UnsafeVarTo<JavascriptInt64Number>(aValue)->GetValue());
- case TypeIds_UInt64Number:
- return ToInteger((double)UnsafeVarTo<JavascriptUInt64Number>(aValue)->GetValue());
- case TypeIds_String:
- return ToInteger(UnsafeVarTo<JavascriptString>(aValue)->ToDouble());
- case TypeIds_VariantDate:
- return ToInteger(ToNumber_Full(aValue, scriptContext));
- default:
- {
- AssertMsg(JavascriptOperators::IsObject(aValue), "bad type object in conversion ToInteger");
- if(fPrimitiveOnly)
- {
- AssertMsg(FALSE, "wrong call in ToInteger_Full, no dynamic objects should get here");
- JavascriptError::ThrowError(scriptContext, VBSERR_OLENoPropOrMethod);
- }
- fPrimitiveOnly = true;
- aValue = ToPrimitive<JavascriptHint::HintNumber>(aValue, scriptContext);
- }
- }
- }
- }
- double JavascriptConversion::ToInteger(double val)
- {
- if(JavascriptNumber::IsNan(val))
- return 0;
- if(JavascriptNumber::IsPosInf(val) || JavascriptNumber::IsNegInf(val) ||
- JavascriptNumber::IsZero(val))
- {
- return val;
- }
- return ( ((val < 0) ? -1 : 1 ) * floor(fabs(val)));
- }
- //----------------------------------------------------------------------------
- // ToInt32() converts the given Var to an Int32 value, as described in
- // (ES3.0: S9.5).
- //----------------------------------------------------------------------------
- int32 JavascriptConversion::ToInt32_Full(Var aValue, ScriptContext* scriptContext)
- {
- JIT_HELPER_REENTRANT_HEADER(Conv_ToInt32_Full);
- Assert(Js::JavascriptStackWalker::ValidateTopJitFrame(scriptContext));
- AssertMsg(!TaggedInt::Is(aValue), "Should be detected");
- ScriptContext * objectScriptContext = VarIs<RecyclableObject>(aValue) ? UnsafeVarTo<RecyclableObject>(aValue)->GetScriptContext() : nullptr;
- // This is used when TaggedInt's overflow but remain under int32
- // so Number is our most critical case:
- TypeId typeId = JavascriptOperators::GetTypeId(aValue);
- if (typeId == TypeIds_Number)
- {
- return JavascriptMath::ToInt32Core(JavascriptNumber::GetValue(aValue));
- }
- switch (typeId)
- {
- case TypeIds_Symbol:
- JavascriptError::TryThrowTypeError(objectScriptContext, scriptContext, JSERR_NeedNumber);
- // Fallthrough to return 0 if exceptions are disabled
- case TypeIds_Undefined:
- case TypeIds_Null:
- return 0;
- case TypeIds_Integer:
- return TaggedInt::ToInt32(aValue);
- case TypeIds_Boolean:
- return UnsafeVarTo<JavascriptBoolean>(aValue)->GetValue() ? 1 : +0;
- case TypeIds_Int64Number:
- // we won't lose precision if the int64 is within 32bit boundary; otherwise we need to
- // treat it as double anyhow.
- return JavascriptMath::ToInt32Core((double)UnsafeVarTo<JavascriptInt64Number>(aValue)->GetValue());
- case TypeIds_UInt64Number:
- // we won't lose precision if the int64 is within 32bit boundary; otherwise we need to
- // treat it as double anyhow.
- return JavascriptMath::ToInt32Core((double)UnsafeVarTo<JavascriptUInt64Number>(aValue)->GetValue());
- case TypeIds_String:
- {
- double result;
- if (UnsafeVarTo<JavascriptString>(aValue)->ToDouble(&result))
- {
- return JavascriptMath::ToInt32Core(result);
- }
- // If the string isn't a valid number, ToDouble returns NaN, and ToInt32 of that is 0
- return 0;
- }
- case TypeIds_VariantDate:
- return ToInt32(ToNumber_Full(aValue, scriptContext));
- default:
- AssertMsg(JavascriptOperators::IsObject(aValue), "bad type object in conversion ToInteger32");
- aValue = ToPrimitive<JavascriptHint::HintNumber>(aValue, scriptContext);
- }
- switch (JavascriptOperators::GetTypeId(aValue))
- {
- case TypeIds_Symbol:
- JavascriptError::TryThrowTypeError(objectScriptContext, scriptContext, JSERR_NeedNumber);
- // Fallthrough to return 0 if exceptions are disabled
- case TypeIds_Undefined:
- case TypeIds_Null:
- return 0;
- case TypeIds_Integer:
- return TaggedInt::ToInt32(aValue);
- case TypeIds_Boolean:
- return UnsafeVarTo<JavascriptBoolean>(aValue)->GetValue() ? 1 : +0;
- case TypeIds_Number:
- return ToInt32(JavascriptNumber::GetValue(aValue));
- case TypeIds_Int64Number:
- // we won't lose precision if the int64 is within 32bit boundary; otherwise we need to
- // treat it as double anyhow.
- return JavascriptMath::ToInt32Core((double)UnsafeVarTo<JavascriptInt64Number>(aValue)->GetValue());
- case TypeIds_UInt64Number:
- // we won't lose precision if the int64 is within 32bit boundary; otherwise we need to
- // treat it as double anyhow.
- return JavascriptMath::ToInt32Core((double)UnsafeVarTo<JavascriptUInt64Number>(aValue)->GetValue());
- case TypeIds_String:
- {
- double result;
- if (UnsafeVarTo<JavascriptString>(aValue)->ToDouble(&result))
- {
- return ToInt32(result);
- }
- // If the string isn't a valid number, ToDouble returns NaN, and ToInt32 of that is 0
- return 0;
- }
- case TypeIds_VariantDate:
- return ToInt32(ToNumber_Full(aValue, scriptContext));
- default:
- AssertMsg(FALSE, "wrong call in ToInteger32_Full, no dynamic objects should get here.");
- JavascriptError::ThrowError(scriptContext, VBSERR_OLENoPropOrMethod);
- }
- JIT_HELPER_END(Conv_ToInt32_Full);
- }
- // a strict version of ToInt32 conversion that returns false for non int32 values like, inf, NaN, undef
- BOOL JavascriptConversion::ToInt32Finite(Var aValue, ScriptContext* scriptContext, int32* result)
- {
- ScriptContext * objectScriptContext = VarIs<RecyclableObject>(aValue) ? UnsafeVarTo<RecyclableObject>(aValue)->GetScriptContext() : nullptr;
- BOOL fPrimitiveOnly = false;
- while(true)
- {
- switch (JavascriptOperators::GetTypeId(aValue))
- {
- case TypeIds_Symbol:
- JavascriptError::TryThrowTypeError(objectScriptContext, scriptContext, JSERR_NeedNumber);
- // Fallthrough to return false and set result to 0 if exceptions are disabled
- case TypeIds_Undefined:
- *result = 0;
- return false;
- case TypeIds_Null:
- *result = 0;
- return true;
- case TypeIds_Integer:
- *result = TaggedInt::ToInt32(aValue);
- return true;
- case TypeIds_Boolean:
- *result = UnsafeVarTo<JavascriptBoolean>(aValue)->GetValue() ? 1 : +0;
- return true;
- case TypeIds_Number:
- return ToInt32Finite(JavascriptNumber::GetValue(aValue), result);
- case TypeIds_Int64Number:
- // we won't lose precision if the int64 is within 32bit boundary; otherwise we need to
- // treat it as double anyhow.
- return ToInt32Finite((double)UnsafeVarTo<JavascriptInt64Number>(aValue)->GetValue(), result);
- case TypeIds_UInt64Number:
- // we won't lose precision if the int64 is within 32bit boundary; otherwise we need to
- // treat it as double anyhow.
- return ToInt32Finite((double)UnsafeVarTo<JavascriptUInt64Number>(aValue)->GetValue(), result);
- case TypeIds_String:
- return ToInt32Finite(UnsafeVarTo<JavascriptString>(aValue)->ToDouble(), result);
- case TypeIds_VariantDate:
- return ToInt32Finite(ToNumber_Full(aValue, scriptContext), result);
- default:
- {
- AssertMsg(JavascriptOperators::IsObject(aValue), "bad type object in conversion ToInteger32");
- if(fPrimitiveOnly)
- {
- AssertMsg(FALSE, "wrong call in ToInteger32_Full, no dynamic objects should get here");
- JavascriptError::ThrowError(scriptContext, VBSERR_OLENoPropOrMethod);
- }
- fPrimitiveOnly = true;
- aValue = ToPrimitive<JavascriptHint::HintNumber>(aValue, scriptContext);
- }
- }
- }
- }
- int32 JavascriptConversion::ToInt32(double T1)
- {
- return JavascriptMath::ToInt32Core(T1);
- }
- __int64 JavascriptConversion::ToInt64(Var aValue, ScriptContext* scriptContext)
- {
- switch (JavascriptOperators::GetTypeId(aValue))
- {
- case TypeIds_Integer:
- {
- return TaggedInt::ToInt32(aValue);
- }
- case TypeIds_Int64Number:
- {
- JavascriptInt64Number* int64Number = UnsafeVarTo<JavascriptInt64Number>(aValue);
- return int64Number->GetValue();
- }
- case TypeIds_UInt64Number:
- {
- JavascriptUInt64Number* uint64Number = UnsafeVarTo<JavascriptUInt64Number>(aValue);
- return (__int64)uint64Number->GetValue();
- }
- case TypeIds_Number:
- return JavascriptMath::TryToInt64(JavascriptNumber::GetValue(aValue));
- default:
- return (unsigned __int64)JavascriptConversion::ToInt32_Full(aValue, scriptContext);
- }
- }
- unsigned __int64 JavascriptConversion::ToUInt64(Var aValue, ScriptContext* scriptContext)
- {
- switch (JavascriptOperators::GetTypeId(aValue))
- {
- case TypeIds_Integer:
- {
- return (unsigned __int64)TaggedInt::ToInt32(aValue);
- }
- case TypeIds_Int64Number:
- {
- JavascriptInt64Number* int64Number = UnsafeVarTo<JavascriptInt64Number>(aValue);
- return (unsigned __int64)int64Number->GetValue();
- }
- case TypeIds_UInt64Number:
- {
- JavascriptUInt64Number* uint64Number = UnsafeVarTo<JavascriptUInt64Number>(aValue);
- return uint64Number->GetValue();
- }
- case TypeIds_Number:
- return static_cast<unsigned __int64>(JavascriptMath::TryToInt64(JavascriptNumber::GetValue(aValue)));
- default:
- return (unsigned __int64)JavascriptConversion::ToInt32_Full(aValue, scriptContext);
- }
- }
- BOOL JavascriptConversion::ToInt32Finite(double value, int32* result)
- {
- if((!NumberUtilities::IsFinite(value)) || JavascriptNumber::IsNan(value))
- {
- *result = 0;
- return false;
- }
- else
- {
- *result = JavascriptMath::ToInt32Core(value);
- return true;
- }
- }
- //----------------------------------------------------------------------------
- // (ES3.0: S9.6).
- //----------------------------------------------------------------------------
- uint32 JavascriptConversion::ToUInt32_Full(Var aValue, ScriptContext* scriptContext)
- {
- JIT_HELPER_REENTRANT_HEADER(Conv_ToUInt32_Full);
- AssertMsg(!TaggedInt::Is(aValue), "Should be detected");
- ScriptContext * objectScriptContext = VarIs<RecyclableObject>(aValue) ? UnsafeVarTo<RecyclableObject>(aValue)->GetScriptContext() : nullptr;
- BOOL fPrimitiveOnly = false;
- while(true)
- {
- switch (JavascriptOperators::GetTypeId(aValue))
- {
- case TypeIds_Symbol:
- JavascriptError::TryThrowTypeError(objectScriptContext, scriptContext, JSERR_NeedNumber);
- // Fallthrough to return 0 if exceptions are disabled
- case TypeIds_Undefined:
- case TypeIds_Null:
- return 0;
- case TypeIds_Integer:
- return TaggedInt::ToUInt32(aValue);
- case TypeIds_Boolean:
- return UnsafeVarTo<JavascriptBoolean>(aValue)->GetValue() ? 1 : +0;
- case TypeIds_Number:
- return JavascriptMath::ToUInt32(JavascriptNumber::GetValue(aValue));
- case TypeIds_Int64Number:
- // we won't lose precision if the int64 is within 32bit boundary; otherwise we need to
- // treat it as double anyhow.
- return JavascriptMath::ToUInt32((double)UnsafeVarTo<JavascriptInt64Number>(aValue)->GetValue());
- case TypeIds_UInt64Number:
- // we won't lose precision if the int64 is within 32bit boundary; otherwise we need to
- // treat it as double anyhow.
- return JavascriptMath::ToUInt32((double)UnsafeVarTo<JavascriptUInt64Number>(aValue)->GetValue());
- case TypeIds_String:
- {
- double result;
- if (UnsafeVarTo<JavascriptString>(aValue)->ToDouble(&result))
- {
- return JavascriptMath::ToUInt32(result);
- }
- // If the string isn't a valid number, ToDouble returns NaN, and ToUInt32 of that is 0
- return 0;
- }
- case TypeIds_VariantDate:
- return JavascriptMath::ToUInt32(ToNumber_Full(aValue, scriptContext));
- default:
- {
- AssertMsg(JavascriptOperators::IsObject(aValue), "bad type object in conversion ToUInt32");
- if(fPrimitiveOnly)
- {
- AssertMsg(FALSE, "wrong call in ToUInt32_Full, no dynamic objects should get here");
- JavascriptError::ThrowError(scriptContext, VBSERR_OLENoPropOrMethod);
- }
- fPrimitiveOnly = true;
- aValue = ToPrimitive<JavascriptHint::HintNumber>(aValue, scriptContext);
- }
- }
- }
- JIT_HELPER_END(Conv_ToUInt32_Full);
- }
- // Unable to put JIT_HELPER macro in .inl file, do instantiation here
- JIT_HELPER_TEMPLATE(Conv_ToUInt32, Conv_ToUInt32)
- JIT_HELPER_TEMPLATE(Conv_ToBoolean, Conv_ToBoolean)
- uint32 JavascriptConversion::ToUInt32(double T1)
- {
- JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(Conv_ToUInt32Core);
- JIT_HELPER_SAME_ATTRIBUTES(Conv_ToInt32Core, Conv_ToUInt32Core);
- // Same as doing ToInt32 and reinterpret the bits as uint32
- return (uint32)JavascriptMath::ToInt32Core(T1);
- JIT_HELPER_END(Conv_ToUInt32Core);
- }
- //----------------------------------------------------------------------------
- // ToUInt16() converts the given Var to a UInt16 value, as described in
- // (ES3.0: S9.6).
- //----------------------------------------------------------------------------
- uint16 JavascriptConversion::ToUInt16_Full(IN Var aValue, ScriptContext* scriptContext)
- {
- AssertMsg(!TaggedInt::Is(aValue), "Should be detected");
- ScriptContext * objectScriptContext = VarIs<RecyclableObject>(aValue) ? UnsafeVarTo<RecyclableObject>(aValue)->GetScriptContext() : nullptr;
- BOOL fPrimitiveOnly = false;
- while(true)
- {
- switch (JavascriptOperators::GetTypeId(aValue))
- {
- case TypeIds_Symbol:
- JavascriptError::TryThrowTypeError(objectScriptContext, scriptContext, JSERR_NeedNumber);
- // Fallthrough to return 0 if exceptions are disabled
- case TypeIds_Undefined:
- case TypeIds_Null:
- return 0;
- case TypeIds_Integer:
- return TaggedInt::ToUInt16(aValue);
- case TypeIds_Boolean:
- return UnsafeVarTo<JavascriptBoolean>(aValue)->GetValue() ? 1 : +0;
- case TypeIds_Number:
- return ToUInt16(JavascriptNumber::GetValue(aValue));
- case TypeIds_Int64Number:
- // we won't lose precision if the int64 is within 16bit boundary; otherwise we need to
- // treat it as double anyhow.
- return ToUInt16((double)UnsafeVarTo<JavascriptInt64Number>(aValue)->GetValue());
- case TypeIds_UInt64Number:
- // we won't lose precision if the int64 is within 16bit boundary; otherwise we need to
- // treat it as double anyhow.
- return ToUInt16((double)UnsafeVarTo<JavascriptUInt64Number>(aValue)->GetValue());
- case TypeIds_String:
- {
- double result;
- if (UnsafeVarTo<JavascriptString>(aValue)->ToDouble(&result))
- {
- return ToUInt16(result);
- }
- // If the string isn't a valid number, ToDouble is NaN, and ToUInt16 of that is 0
- return 0;
- }
- case TypeIds_VariantDate:
- return ToUInt16(ToNumber_Full(aValue, scriptContext));
- default:
- {
- AssertMsg(JavascriptOperators::IsObject(aValue), "bad type object in conversion ToUIn16");
- if(fPrimitiveOnly)
- {
- AssertMsg(FALSE, "wrong call in ToUInt16, no dynamic objects should get here");
- JavascriptError::ThrowError(scriptContext, VBSERR_OLENoPropOrMethod);
- }
- fPrimitiveOnly = true;
- aValue = ToPrimitive<JavascriptHint::HintNumber>(aValue, scriptContext);
- }
- }
- }
- }
- uint16 JavascriptConversion::ToUInt16(double T1)
- {
- //
- // VC does the right thing here, if we first convert to uint32 and then to uint16
- // Spec says mod should be done.
- //
- uint32 result = JavascriptConversion::ToUInt32(T1);
- #if defined(_M_IX86) && _MSC_FULL_VER < 160030329
- // Well VC doesn't actually do the right thing... It takes (uint16)(uint32)double and removes the
- // middle uint32 cast to (uint16)double, which isn't the same thing. Somehow, it only seems to be a
- // problem for x86. Forcing a store to uint32 prevents the incorrect optimization.
- //
- // A bug has been filled in the Dev11 database: TF bug id #901495
- // Fixed in compiler 16.00.30329.00
- volatile uint32 volResult = result;
- #endif
- return (uint16) result;
- }
- int16 JavascriptConversion::ToInt16(double aValue)
- {
- return (int16)ToInt32(aValue);
- }
- int8 JavascriptConversion::ToInt8(double aValue)
- {
- return (int8)ToInt32(aValue);
- }
- uint8 JavascriptConversion::ToUInt8(double aValue)
- {
- return (uint8)ToUInt32(aValue);
- }
- JavascriptString * JavascriptConversion::ToPrimitiveString(Var aValue, ScriptContext * scriptContext)
- {
- JIT_HELPER_REENTRANT_HEADER(Op_ConvPrimitiveString);
- return ToString(ToPrimitive<JavascriptHint::None>(aValue, scriptContext), scriptContext);
- JIT_HELPER_END(Op_ConvPrimitiveString);
- }
- double JavascriptConversion::LongToDouble(__int64 aValue)
- {
- return static_cast<double>(aValue);
- }
- // Windows x64 version implemented in masm to work around precision limitation
- #if !defined(_WIN32 ) || !defined(_M_X64)
- double JavascriptConversion::ULongToDouble(unsigned __int64 aValue)
- {
- return static_cast<double>(aValue);
- }
- #endif
- float JavascriptConversion::LongToFloat(__int64 aValue)
- {
- return static_cast<float>(aValue);
- }
- float JavascriptConversion::ULongToFloat (unsigned __int64 aValue)
- {
- return static_cast<float>(aValue);
- }
- int64 JavascriptConversion::ToLength(Var aValue, ScriptContext* scriptContext)
- {
- if (TaggedInt::Is(aValue))
- {
- int64 length = TaggedInt::ToInt64(aValue);
- return (length < 0) ? 0 : length;
- }
- double length = JavascriptConversion::ToInteger(aValue, scriptContext);
- if (length < 0.0 || JavascriptNumber::IsNegZero(length))
- {
- length = 0.0;
- }
- else if (length > Math::MAX_SAFE_INTEGER)
- {
- length = Math::MAX_SAFE_INTEGER;
- }
- return NumberUtilities::TryToInt64(length);
- }
- JavascriptBigInt *JavascriptConversion::ToBigInt(Var aValue, ScriptContext* scriptContext)
- {
- Assert(scriptContext->GetThreadContext()->IsScriptActive());
- switch (JavascriptOperators::GetTypeId(aValue))
- {
- case TypeIds_BigInt:
- break;
- default:
- AssertOrFailFastMsg(false, "do not support conversion of other types in ToBigInt");
- }
- return UnsafeVarTo<JavascriptBigInt>(aValue);
- }
|