JavascriptConversion.cpp 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. #include "RuntimeLanguagePch.h"
  6. #include "RuntimeMathPch.h"
  7. #include "Library/JavascriptNumberObject.h"
  8. #include "Library/JavascriptStringObject.h"
  9. #include "Library/DateImplementation.h"
  10. #include "Library/JavascriptDate.h"
  11. using namespace Js;
  12. static const double k_2to16 = 65536.0;
  13. static const double k_2to31 = 2147483648.0;
  14. static const double k_2to32 = 4294967296.0;
  15. // ES5 9.10 indicates that this method should throw a TypeError if the supplied value is Undefined or Null.
  16. // Our implementation returns FALSE in this scenario, expecting the caller to throw the TypeError.
  17. // This allows the caller to provide more context in the error message without having to unnecessarily
  18. // construct the message string before knowing whether or not the object is coercible.
  19. BOOL JavascriptConversion::CheckObjectCoercible(Var aValue, ScriptContext* scriptContext)
  20. {
  21. return !JavascriptOperators::IsUndefinedOrNull(aValue);
  22. }
  23. //ES5 9.11 Undefined, Null, Boolean, Number, String - return false
  24. //If Object has a [[Call]] internal method, then return true, otherwise return false
  25. bool JavascriptConversion::IsCallable(Var aValue)
  26. {
  27. if (!VarIs<RecyclableObject>(aValue))
  28. {
  29. return false;
  30. }
  31. return IsCallable(UnsafeVarTo<RecyclableObject>(aValue));
  32. }
  33. bool JavascriptConversion::IsCallable(_In_ RecyclableObject* aValue)
  34. {
  35. Assert(VarIsCorrectType(aValue));
  36. JavascriptMethod entryPoint = aValue->GetEntryPoint();
  37. return RecyclableObject::DefaultEntryPoint != entryPoint;
  38. }
  39. //----------------------------------------------------------------------------
  40. // ES5 9.12 SameValue algorithm implementation.
  41. // 1. If Type(x) is different from Type(y), return false.
  42. // 2. If Type(x) is Undefined, return true.
  43. // 3. If Type(x) is Null, return true.
  44. // 4. If Type(x) is Number, then.
  45. // a. If x is NaN and y is NaN, return true.
  46. // b. If x is +0 and y is -0, return false.
  47. // c. If x is -0 and y is +0, return false.
  48. // d. If x is the same number value as y, return true.
  49. // e. Return false.
  50. // 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.
  51. // 6. If Type(x) is Boolean, return true if x and y are both true or both false; otherwise, return false.
  52. // 7. Return true if x and y refer to the same object. Otherwise, return false.
  53. //----------------------------------------------------------------------------
  54. template<bool zero>
  55. bool JavascriptConversion::SameValueCommon(Var aLeft, Var aRight)
  56. {
  57. if (aLeft == aRight)
  58. {
  59. return true;
  60. }
  61. TypeId leftType = JavascriptOperators::GetTypeId(aLeft);
  62. if (JavascriptOperators::IsUndefinedOrNullType(leftType))
  63. {
  64. return false;
  65. }
  66. TypeId rightType = JavascriptOperators::GetTypeId(aRight);
  67. double dblLeft, dblRight;
  68. switch (leftType)
  69. {
  70. case TypeIds_Integer:
  71. switch (rightType)
  72. {
  73. case TypeIds_Integer:
  74. return false;
  75. case TypeIds_Number:
  76. dblLeft = TaggedInt::ToDouble(aLeft);
  77. dblRight = JavascriptNumber::GetValue(aRight);
  78. goto CommonNumber;
  79. case TypeIds_Int64Number:
  80. {
  81. int leftValue = TaggedInt::ToInt32(aLeft);
  82. __int64 rightValue = UnsafeVarTo<JavascriptInt64Number>(aRight)->GetValue();
  83. return leftValue == rightValue;
  84. }
  85. case TypeIds_UInt64Number:
  86. {
  87. int leftValue = TaggedInt::ToInt32(aLeft);
  88. unsigned __int64 rightValue = VarTo<JavascriptInt64Number>(aRight)->GetValue();
  89. return leftValue == rightValue;
  90. }
  91. }
  92. break;
  93. case TypeIds_Int64Number:
  94. switch (rightType)
  95. {
  96. case TypeIds_Integer:
  97. {
  98. __int64 leftValue = UnsafeVarTo<JavascriptInt64Number>(aLeft)->GetValue();
  99. int rightValue = TaggedInt::ToInt32(aRight);
  100. return leftValue == rightValue;
  101. }
  102. case TypeIds_Number:
  103. dblLeft = (double)UnsafeVarTo<JavascriptInt64Number>(aLeft)->GetValue();
  104. dblRight = JavascriptNumber::GetValue(aRight);
  105. goto CommonNumber;
  106. case TypeIds_Int64Number:
  107. {
  108. __int64 leftValue = UnsafeVarTo<JavascriptInt64Number>(aLeft)->GetValue();
  109. __int64 rightValue = UnsafeVarTo<JavascriptInt64Number>(aRight)->GetValue();
  110. return leftValue == rightValue;
  111. }
  112. case TypeIds_UInt64Number:
  113. {
  114. __int64 leftValue = UnsafeVarTo<JavascriptInt64Number>(aLeft)->GetValue();
  115. unsigned __int64 rightValue = VarTo<JavascriptInt64Number>(aRight)->GetValue();
  116. return ((unsigned __int64)leftValue == rightValue);
  117. }
  118. }
  119. break;
  120. case TypeIds_UInt64Number:
  121. switch (rightType)
  122. {
  123. case TypeIds_Integer:
  124. {
  125. unsigned __int64 leftValue = UnsafeVarTo<JavascriptUInt64Number>(aLeft)->GetValue();
  126. __int64 rightValue = TaggedInt::ToInt32(aRight);
  127. return (leftValue == (unsigned __int64)rightValue);
  128. }
  129. case TypeIds_Number:
  130. dblLeft = (double)UnsafeVarTo<JavascriptUInt64Number>(aLeft)->GetValue();
  131. dblRight = JavascriptNumber::GetValue(aRight);
  132. goto CommonNumber;
  133. case TypeIds_Int64Number:
  134. {
  135. unsigned __int64 leftValue = UnsafeVarTo<JavascriptUInt64Number>(aLeft)->GetValue();
  136. __int64 rightValue = UnsafeVarTo<JavascriptInt64Number>(aRight)->GetValue();
  137. return (leftValue == (unsigned __int64)rightValue);
  138. }
  139. case TypeIds_UInt64Number:
  140. {
  141. unsigned __int64 leftValue = UnsafeVarTo<JavascriptUInt64Number>(aLeft)->GetValue();
  142. unsigned __int64 rightValue = VarTo<JavascriptInt64Number>(aRight)->GetValue();
  143. return leftValue == rightValue;
  144. }
  145. }
  146. break;
  147. case TypeIds_Number:
  148. switch (rightType)
  149. {
  150. case TypeIds_Integer:
  151. dblLeft = JavascriptNumber::GetValue(aLeft);
  152. dblRight = TaggedInt::ToDouble(aRight);
  153. goto CommonNumber;
  154. case TypeIds_Int64Number:
  155. dblLeft = JavascriptNumber::GetValue(aLeft);
  156. dblRight = (double)UnsafeVarTo<JavascriptInt64Number>(aRight)->GetValue();
  157. goto CommonNumber;
  158. case TypeIds_UInt64Number:
  159. dblLeft = JavascriptNumber::GetValue(aLeft);
  160. dblRight = (double)UnsafeVarTo<JavascriptUInt64Number>(aRight)->GetValue();
  161. goto CommonNumber;
  162. case TypeIds_Number:
  163. dblLeft = JavascriptNumber::GetValue(aLeft);
  164. dblRight = JavascriptNumber::GetValue(aRight);
  165. CommonNumber:
  166. if (JavascriptNumber::IsNan(dblLeft) && JavascriptNumber::IsNan(dblRight))
  167. {
  168. return true;
  169. }
  170. if (zero)
  171. {
  172. // SameValueZero(+0,-0) returns true;
  173. return dblLeft == dblRight;
  174. }
  175. else
  176. {
  177. // SameValue(+0,-0) returns false;
  178. return (NumberUtilities::LuLoDbl(dblLeft) == NumberUtilities::LuLoDbl(dblRight) &&
  179. NumberUtilities::LuHiDbl(dblLeft) == NumberUtilities::LuHiDbl(dblRight));
  180. }
  181. }
  182. break;
  183. case TypeIds_Boolean:
  184. return false;
  185. case TypeIds_String:
  186. switch (rightType)
  187. {
  188. case TypeIds_String:
  189. return JavascriptString::Equals(UnsafeVarTo<JavascriptString>(aLeft), UnsafeVarTo<JavascriptString>(aRight));
  190. }
  191. break;
  192. #if DBG
  193. case TypeIds_Symbol:
  194. if (rightType == TypeIds_Symbol)
  195. {
  196. JavascriptSymbol* leftSymbol = UnsafeVarTo<JavascriptSymbol>(aLeft);
  197. JavascriptSymbol* rightSymbol = UnsafeVarTo<JavascriptSymbol>(aRight);
  198. Assert(leftSymbol->GetValue() != rightSymbol->GetValue());
  199. }
  200. #endif
  201. default:
  202. break;
  203. }
  204. return false;
  205. }
  206. template bool JavascriptConversion::SameValueCommon<false>(Var aLeft, Var aRight);
  207. template bool JavascriptConversion::SameValueCommon<true>(Var aLeft, Var aRight);
  208. //----------------------------------------------------------------------------
  209. // ToObject() takes a value and converts it to an Object type
  210. // Implementation of ES5 9.9
  211. // The spec indicates that this method should throw a TypeError if the supplied value is Undefined or Null.
  212. // Our implementation returns FALSE in this scenario, expecting the caller to throw the TypeError.
  213. // This allows the caller to provide more context in the error message without having to unnecessarily
  214. // construct the message string before knowing whether or not the value can be converted to an object.
  215. //
  216. // Undefined Return FALSE.
  217. // Null Return FALSE.
  218. // Boolean Create a new Boolean object whose [[PrimitiveValue]]
  219. // internal property is set to the value of the boolean.
  220. // See 15.6 for a description of Boolean objects.
  221. // Return TRUE.
  222. // Number Create a new Number object whose [[PrimitiveValue]]
  223. // internal property is set to the value of the number.
  224. // See 15.7 for a description of Number objects.
  225. // Return TRUE.
  226. // String Create a new String object whose [[PrimitiveValue]]
  227. // internal property is set to the value of the string.
  228. // See 15.5 for a description of String objects.
  229. // Return TRUE.
  230. // Object The result is the input argument (no conversion).
  231. // Return TRUE.
  232. //----------------------------------------------------------------------------
  233. BOOL JavascriptConversion::ToObject(Var aValue, ScriptContext* scriptContext, RecyclableObject** object)
  234. {
  235. Assert(object);
  236. switch (JavascriptOperators::GetTypeId(aValue))
  237. {
  238. case TypeIds_Undefined:
  239. case TypeIds_Null:
  240. return FALSE;
  241. case TypeIds_Number:
  242. case TypeIds_Integer:
  243. *object = scriptContext->GetLibrary()->CreateNumberObject(aValue);
  244. return TRUE;
  245. default:
  246. *object = VarTo<RecyclableObject>(aValue)->ToObject(scriptContext);
  247. return TRUE;
  248. }
  249. }
  250. //----------------------------------------------------------------------------
  251. // ToPropertyKey() takes a value and converts it to a property key
  252. // Implementation of ES6 7.1.14
  253. //----------------------------------------------------------------------------
  254. void JavascriptConversion::ToPropertyKey(
  255. Var argument,
  256. _In_ ScriptContext* scriptContext,
  257. _Out_ const PropertyRecord** propertyRecord,
  258. _Out_opt_ PropertyString** propString)
  259. {
  260. Var key = JavascriptConversion::ToPrimitive<JavascriptHint::HintString>(argument, scriptContext);
  261. PropertyString * propertyString = nullptr;
  262. if (VarIs<JavascriptSymbol>(key))
  263. {
  264. // If we are looking up a property keyed by a symbol, we already have the PropertyId in the symbol
  265. *propertyRecord = UnsafeVarTo<JavascriptSymbol>(key)->GetValue();
  266. }
  267. else
  268. {
  269. // For all other types, convert the key into a string and use that as the property name
  270. JavascriptString * propName = JavascriptConversion::ToString(key, scriptContext);
  271. propName->GetPropertyRecord(propertyRecord);
  272. if (VarIs<PropertyString>(propName))
  273. {
  274. propertyString = UnsafeVarTo<PropertyString>(propName);
  275. }
  276. }
  277. if (propString)
  278. {
  279. *propString = propertyString;
  280. }
  281. }
  282. //----------------------------------------------------------------------------
  283. // ToPrimitive() takes a value and an optional argument and converts it to a non Object type
  284. // Implementation of ES5 9.1
  285. //
  286. // Undefined:The result equals the input argument (no conversion).
  287. // Null: The result equals the input argument (no conversion).
  288. // Boolean: The result equals the input argument (no conversion).
  289. // Number: The result equals the input argument (no conversion).
  290. // String: The result equals the input argument (no conversion).
  291. // VariantDate:Returns the value for variant date by calling ToPrimitive directly.
  292. // Object: Return a default value for the Object.
  293. // The default value of an object is retrieved by calling the [[DefaultValue]]
  294. // internal method of the object, passing the optional hint PreferredType.
  295. // The behavior of the [[DefaultValue]] internal method is defined by this specification
  296. // for all native ECMAScript objects (8.12.9).
  297. //----------------------------------------------------------------------------
  298. template <JavascriptHint hint>
  299. Var JavascriptConversion::ToPrimitive(_In_ Var aValue, _In_ ScriptContext * requestContext)
  300. {
  301. switch (JavascriptOperators::GetTypeId(aValue))
  302. {
  303. case TypeIds_Undefined:
  304. case TypeIds_Null:
  305. case TypeIds_Integer:
  306. case TypeIds_Boolean:
  307. case TypeIds_Number:
  308. case TypeIds_String:
  309. case TypeIds_Symbol:
  310. return aValue;
  311. case TypeIds_VariantDate:
  312. {
  313. Var result = nullptr;
  314. if (UnsafeVarTo<JavascriptVariantDate>(aValue)->ToPrimitive(hint, &result, requestContext) != TRUE)
  315. {
  316. result = nullptr;
  317. }
  318. return result;
  319. }
  320. case TypeIds_StringObject:
  321. {
  322. JavascriptStringObject * stringObject = UnsafeVarTo<JavascriptStringObject>(aValue);
  323. ScriptContext * objectScriptContext = stringObject->GetScriptContext();
  324. if (objectScriptContext->optimizationOverrides.GetSideEffects() & (hint == JavascriptHint::HintString ? SideEffects_ToString : SideEffects_ValueOf))
  325. {
  326. return MethodCallToPrimitive<hint>(stringObject, requestContext);
  327. }
  328. return CrossSite::MarshalVar(requestContext, stringObject->Unwrap(), objectScriptContext);
  329. }
  330. case TypeIds_NumberObject:
  331. {
  332. JavascriptNumberObject * numberObject = UnsafeVarTo<JavascriptNumberObject>(aValue);
  333. ScriptContext * objectScriptContext = numberObject->GetScriptContext();
  334. if (hint == JavascriptHint::HintString)
  335. {
  336. if (objectScriptContext->optimizationOverrides.GetSideEffects() & SideEffects_ToString)
  337. {
  338. return MethodCallToPrimitive<hint>(numberObject, requestContext);
  339. }
  340. return JavascriptNumber::ToStringRadix10(numberObject->GetValue(), requestContext);
  341. }
  342. else
  343. {
  344. if (objectScriptContext->optimizationOverrides.GetSideEffects() & SideEffects_ValueOf)
  345. {
  346. return MethodCallToPrimitive<hint>(numberObject, requestContext);
  347. }
  348. return CrossSite::MarshalVar(requestContext, numberObject->Unwrap(), objectScriptContext);
  349. }
  350. }
  351. case TypeIds_SymbolObject:
  352. {
  353. JavascriptSymbolObject* symbolObject = UnsafeVarTo<JavascriptSymbolObject>(aValue);
  354. return CrossSite::MarshalVar(requestContext, symbolObject->Unwrap(), symbolObject->GetScriptContext());
  355. }
  356. case TypeIds_Date:
  357. case TypeIds_WinRTDate:
  358. {
  359. JavascriptDate* dateObject = UnsafeVarTo<JavascriptDate>(aValue);
  360. if(hint == JavascriptHint::HintNumber)
  361. {
  362. if (dateObject->GetScriptContext()->optimizationOverrides.GetSideEffects() & SideEffects_ValueOf)
  363. {
  364. // if no Method exists this function falls back to OrdinaryToPrimitive
  365. // if IsES6ToPrimitiveEnabled flag is off we also fall back to OrdinaryToPrimitive
  366. return MethodCallToPrimitive<hint>(dateObject, requestContext);
  367. }
  368. return JavascriptNumber::ToVarNoCheck(dateObject->GetTime(), requestContext);
  369. }
  370. else
  371. {
  372. if (dateObject->GetScriptContext()->optimizationOverrides.GetSideEffects() & SideEffects_ToString)
  373. {
  374. // if no Method exists this function falls back to OrdinaryToPrimitive
  375. // if IsES6ToPrimitiveEnabled flag is off we also fall back to OrdinaryToPrimitive
  376. return MethodCallToPrimitive<hint>(dateObject, requestContext);
  377. }
  378. return JavascriptDate::ToString(dateObject, requestContext);
  379. }
  380. }
  381. // convert to JavascriptNumber
  382. case TypeIds_Int64Number:
  383. return UnsafeVarTo<JavascriptInt64Number>(aValue)->ToJavascriptNumber();
  384. case TypeIds_UInt64Number:
  385. return UnsafeVarTo<JavascriptUInt64Number>(aValue)->ToJavascriptNumber();
  386. default:
  387. // if no Method exists this function falls back to OrdinaryToPrimitive
  388. // if IsES6ToPrimitiveEnabled flag is off we also fall back to OrdinaryToPrimitive
  389. return MethodCallToPrimitive<hint>(UnsafeVarTo<RecyclableObject>(aValue), requestContext);
  390. }
  391. }
  392. //----------------------------------------------------------------------------
  393. //https://tc39.github.io/ecma262/#sec-canonicalnumericindexstring
  394. //1. Assert : Type(argument) is String.
  395. //2. If argument is "-0", then return -0.
  396. //3. Let n be ToNumber(argument).
  397. //4. If SameValue(ToString(n), argument) is false, then return undefined.
  398. //5. Return n.
  399. //----------------------------------------------------------------------------
  400. BOOL JavascriptConversion::CanonicalNumericIndexString(JavascriptString *aValue, double *indexValue, ScriptContext * scriptContext)
  401. {
  402. if (JavascriptString::IsNegZero(aValue))
  403. {
  404. *indexValue = -0;
  405. return TRUE;
  406. }
  407. double indexDoubleValue = aValue->ToDouble();
  408. if (JavascriptString::Equals(JavascriptNumber::ToStringRadix10(indexDoubleValue, scriptContext), aValue))
  409. {
  410. *indexValue = indexDoubleValue;
  411. return TRUE;
  412. }
  413. return FALSE;
  414. }
  415. template <JavascriptHint hint>
  416. Var JavascriptConversion::MethodCallToPrimitive(_In_ RecyclableObject* value, _In_ ScriptContext * requestContext)
  417. {
  418. Var result = nullptr;
  419. ScriptContext *const scriptContext = value->GetScriptContext();
  420. //7.3.9 GetMethod(V, P)
  421. // The abstract operation GetMethod is used to get the value of a specific property of an ECMAScript language value when the value of the
  422. // 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
  423. // property key. This abstract operation performs the following steps:
  424. // 1. Assert: IsPropertyKey(P) is true.
  425. // 2. Let func be ? GetV(V, P).
  426. // 3. If func is either undefined or null, return undefined.
  427. // 4. If IsCallable(func) is false, throw a TypeError exception.
  428. // 5. Return func.
  429. Var varMethod = nullptr;
  430. if (!requestContext->GetConfig()->IsES6ToPrimitiveEnabled()
  431. || JavascriptOperators::CheckIfObjectAndProtoChainHasNoSpecialProperties(value)
  432. || !JavascriptOperators::GetPropertyReference(value, PropertyIds::_symbolToPrimitive, &varMethod, requestContext)
  433. || JavascriptOperators::IsUndefinedOrNull(varMethod))
  434. {
  435. return OrdinaryToPrimitive<hint>(value, requestContext);
  436. }
  437. if (!VarIs<JavascriptFunction>(varMethod))
  438. {
  439. // Don't error if we disabled implicit calls
  440. JavascriptError::TryThrowTypeError(scriptContext, requestContext, JSERR_Property_NeedFunction, requestContext->GetPropertyName(PropertyIds::_symbolToPrimitive)->GetBuffer());
  441. return requestContext->GetLibrary()->GetNull();
  442. }
  443. // Let exoticToPrim be GetMethod(input, @@toPrimitive).
  444. JavascriptFunction* exoticToPrim = UnsafeVarTo<JavascriptFunction>(varMethod);
  445. JavascriptString* hintString = nullptr;
  446. if (hint == JavascriptHint::HintString)
  447. {
  448. hintString = requestContext->GetLibrary()->GetStringTypeDisplayString();
  449. }
  450. else if (hint == JavascriptHint::HintNumber)
  451. {
  452. hintString = requestContext->GetLibrary()->GetNumberTypeDisplayString();
  453. }
  454. else
  455. {
  456. hintString = requestContext->GetPropertyString(PropertyIds::default_);
  457. }
  458. // If exoticToPrim is not undefined, then
  459. Assert(nullptr != exoticToPrim);
  460. ThreadContext * threadContext = requestContext->GetThreadContext();
  461. result = threadContext->ExecuteImplicitCall(exoticToPrim, ImplicitCall_ToPrimitive, [=]()->Js::Var
  462. {
  463. // Stack object should have a pre-op bail on implicit call. We shouldn't see them here.
  464. Assert(!ThreadContext::IsOnStack(value));
  465. // Let result be the result of calling the[[Call]] internal method of exoticToPrim, with input as thisArgument and(hint) as argumentsList.
  466. return CALL_FUNCTION(threadContext, exoticToPrim, CallInfo(CallFlags_Value, 2), value, hintString);
  467. });
  468. if (!result)
  469. {
  470. // There was an implicit call and implicit calls are disabled. This would typically cause a bailout.
  471. Assert(threadContext->IsDisableImplicitCall());
  472. return requestContext->GetLibrary()->GetNull();
  473. }
  474. Assert(!CrossSite::NeedMarshalVar(result, requestContext));
  475. // If result is an ECMAScript language value and Type(result) is not Object, then return result.
  476. if (TaggedInt::Is(result) || !JavascriptOperators::IsObjectType(JavascriptOperators::GetTypeId(result)))
  477. {
  478. return result;
  479. }
  480. // Else, throw a TypeError exception.
  481. else
  482. {
  483. // Don't error if we disabled implicit calls
  484. JavascriptError::TryThrowTypeError(scriptContext, requestContext, JSERR_FunctionArgument_Invalid, _u("[Symbol.toPrimitive]"));
  485. return requestContext->GetLibrary()->GetNull();
  486. }
  487. }
  488. template <JavascriptHint hint>
  489. Var JavascriptConversion::OrdinaryToPrimitive(_In_ RecyclableObject* value, _In_ ScriptContext* requestContext)
  490. {
  491. Var result;
  492. if (!value->ToPrimitive(hint, &result, requestContext))
  493. {
  494. ScriptContext *const scriptContext = value->GetScriptContext();
  495. int32 hCode;
  496. switch (hint)
  497. {
  498. case JavascriptHint::HintNumber:
  499. hCode = JSERR_NeedNumber;
  500. break;
  501. case JavascriptHint::HintString:
  502. hCode = JSERR_NeedString;
  503. break;
  504. default:
  505. hCode = VBSERR_OLENoPropOrMethod;
  506. break;
  507. }
  508. JavascriptError::TryThrowTypeError(scriptContext, scriptContext, hCode);
  509. return requestContext->GetLibrary()->GetNull();
  510. }
  511. return result;
  512. }
  513. template Var JavascriptConversion::OrdinaryToPrimitive<JavascriptHint::HintNumber>(RecyclableObject* value, ScriptContext* requestContext);
  514. template Var JavascriptConversion::OrdinaryToPrimitive<JavascriptHint::HintString>(RecyclableObject* value, ScriptContext* requestContext);
  515. template Var JavascriptConversion::OrdinaryToPrimitive<JavascriptHint::None>(RecyclableObject* value, ScriptContext* requestContext);
  516. JavascriptString *JavascriptConversion::CoerseString(Var aValue, ScriptContext* scriptContext, const char16* apiNameForErrorMsg)
  517. {
  518. JIT_HELPER_REENTRANT_HEADER(Op_CoerseString);
  519. if (!JavascriptConversion::CheckObjectCoercible(aValue, scriptContext))
  520. {
  521. JavascriptError::ThrowTypeError(scriptContext, JSERR_This_NullOrUndefined, apiNameForErrorMsg);
  522. }
  523. return ToString(aValue, scriptContext);
  524. JIT_HELPER_END(Op_CoerseString);
  525. }
  526. //----------------------------------------------------------------------------
  527. // ToString - abstract operation
  528. // ES5 9.8
  529. //Input Type Result
  530. // Undefined
  531. // "undefined"
  532. // Null
  533. // "null"
  534. // Boolean
  535. // If the argument is true, then the result is "true". If the argument is false, then the result is "false".
  536. // Number
  537. // See 9.8.1 below.
  538. // String
  539. // Return the input argument (no conversion)
  540. // Object
  541. // Apply the following steps:
  542. // 1. Let primValue be ToPrimitive(input argument, hint String).
  543. // 2. Return ToString(primValue).
  544. //----------------------------------------------------------------------------
  545. JavascriptString *JavascriptConversion::ToString(Var aValue, ScriptContext* scriptContext)
  546. {
  547. JIT_HELPER_REENTRANT_HEADER(Op_ConvString);
  548. Assert(scriptContext->GetThreadContext()->IsScriptActive());
  549. BOOL fPrimitiveOnly = false;
  550. while(true)
  551. {
  552. switch (JavascriptOperators::GetTypeId(aValue))
  553. {
  554. case TypeIds_Undefined:
  555. return scriptContext->GetLibrary()->GetUndefinedDisplayString();
  556. case TypeIds_Null:
  557. return scriptContext->GetLibrary()->GetNullDisplayString();
  558. case TypeIds_Integer:
  559. return scriptContext->GetIntegerString(aValue);
  560. case TypeIds_Boolean:
  561. return UnsafeVarTo<JavascriptBoolean>(aValue)->GetValue() ? scriptContext->GetLibrary()->GetTrueDisplayString() : scriptContext->GetLibrary()->GetFalseDisplayString();
  562. case TypeIds_Number:
  563. return JavascriptNumber::ToStringRadix10(JavascriptNumber::GetValue(aValue), scriptContext);
  564. case TypeIds_Int64Number:
  565. {
  566. __int64 value = UnsafeVarTo<JavascriptInt64Number>(aValue)->GetValue();
  567. if (!TaggedInt::IsOverflow(value))
  568. {
  569. return scriptContext->GetIntegerString((int)value);
  570. }
  571. else
  572. {
  573. return JavascriptInt64Number::ToString(aValue, scriptContext);
  574. }
  575. }
  576. case TypeIds_UInt64Number:
  577. {
  578. unsigned __int64 value = UnsafeVarTo<JavascriptUInt64Number>(aValue)->GetValue();
  579. if (!TaggedInt::IsOverflow(value))
  580. {
  581. return scriptContext->GetIntegerString((uint)value);
  582. }
  583. else
  584. {
  585. return JavascriptUInt64Number::ToString(aValue, scriptContext);
  586. }
  587. }
  588. case TypeIds_String:
  589. {
  590. ScriptContext* aValueScriptContext = Js::UnsafeVarTo<Js::RecyclableObject>(aValue)->GetScriptContext();
  591. return UnsafeVarTo<JavascriptString>(CrossSite::MarshalVar(scriptContext,
  592. aValue, aValueScriptContext));
  593. }
  594. case TypeIds_VariantDate:
  595. return VarTo<JavascriptVariantDate>(aValue)->GetValueString(scriptContext);
  596. case TypeIds_Symbol:
  597. return UnsafeVarTo<JavascriptSymbol>(aValue)->ToString(scriptContext);
  598. case TypeIds_SymbolObject:
  599. return JavascriptSymbol::ToString(UnsafeVarTo<JavascriptSymbolObject>(aValue)->GetValue(), scriptContext);
  600. case TypeIds_GlobalObject:
  601. aValue = static_cast<Js::GlobalObject*>(aValue)->ToThis();
  602. // fall through
  603. default:
  604. {
  605. AssertMsg(JavascriptOperators::IsObject(aValue), "bad type object in conversion ToString");
  606. if(fPrimitiveOnly)
  607. {
  608. AssertMsg(FALSE, "wrong call in ToString, no dynamic objects should get here");
  609. JavascriptError::ThrowError(scriptContext, VBSERR_InternalError);
  610. }
  611. fPrimitiveOnly = true;
  612. aValue = ToPrimitive<JavascriptHint::HintString>(aValue, scriptContext);
  613. }
  614. }
  615. }
  616. JIT_HELPER_END(Op_ConvString);
  617. }
  618. JavascriptString *JavascriptConversion::ToLocaleString(Var aValue, ScriptContext* scriptContext)
  619. {
  620. switch (JavascriptOperators::GetTypeId(aValue))
  621. {
  622. case TypeIds_Undefined:
  623. return scriptContext->GetLibrary()->GetUndefinedDisplayString();
  624. case TypeIds_Null:
  625. return scriptContext->GetLibrary()->GetNullDisplayString();
  626. case TypeIds_Integer:
  627. return JavascriptNumber::ToLocaleString(TaggedInt::ToInt32(aValue), scriptContext);
  628. case TypeIds_Boolean:
  629. return UnsafeVarTo<JavascriptBoolean>(aValue)->GetValue() ? scriptContext->GetLibrary()->GetTrueDisplayString() : scriptContext->GetLibrary()->GetFalseDisplayString();
  630. case TypeIds_Int64Number:
  631. return JavascriptNumber::ToLocaleString((double)UnsafeVarTo<JavascriptInt64Number>(aValue)->GetValue(), scriptContext);
  632. case TypeIds_UInt64Number:
  633. return JavascriptNumber::ToLocaleString((double)UnsafeVarTo<JavascriptUInt64Number>(aValue)->GetValue(), scriptContext);
  634. case TypeIds_Number:
  635. return JavascriptNumber::ToLocaleString(JavascriptNumber::GetValue(aValue), scriptContext);
  636. case TypeIds_VariantDate:
  637. // Legacy behavior was to create an empty object and call toLocaleString on it, which would result in this value
  638. return scriptContext->GetLibrary()->GetObjectDisplayString();
  639. case TypeIds_Symbol:
  640. return UnsafeVarTo<JavascriptSymbol>(aValue)->ToString(scriptContext);
  641. default:
  642. {
  643. RecyclableObject* object = VarTo<RecyclableObject>(aValue);
  644. Var value = JavascriptOperators::GetProperty(object, PropertyIds::toLocaleString, scriptContext, NULL);
  645. if (JavascriptConversion::IsCallable(value))
  646. {
  647. RecyclableObject* toLocaleStringFunction = VarTo<RecyclableObject>(value);
  648. Var aResult = scriptContext->GetThreadContext()->ExecuteImplicitCall(toLocaleStringFunction, Js::ImplicitCall_ToPrimitive, [=]()->Js::Var
  649. {
  650. return CALL_FUNCTION(scriptContext->GetThreadContext(), toLocaleStringFunction, CallInfo(1), aValue);
  651. });
  652. if (VarIs<JavascriptString>(aResult))
  653. {
  654. return UnsafeVarTo<JavascriptString>(aResult);
  655. }
  656. else
  657. {
  658. return JavascriptConversion::ToString(aResult, scriptContext);
  659. }
  660. }
  661. JavascriptError::ThrowTypeError(scriptContext, JSERR_Property_NeedFunction, scriptContext->GetPropertyName(PropertyIds::toLocaleString)->GetBuffer());
  662. }
  663. }
  664. }
  665. //----------------------------------------------------------------------------
  666. // ToBoolean_Full:
  667. // (ES3.0: S9.2):
  668. //
  669. // Input Output
  670. // ----- ------
  671. // 'undefined' 'false'
  672. // 'null' 'false'
  673. // Boolean Value
  674. // Number 'false' if +0, -0, or Nan
  675. // 'true' otherwise
  676. // String 'false' if argument is ""
  677. // 'true' otherwise
  678. // Object 'true'
  679. // Falsy Object 'false'
  680. //----------------------------------------------------------------------------
  681. BOOL JavascriptConversion::ToBoolean_Full(Var aValue, ScriptContext* scriptContext)
  682. {
  683. AssertMsg(!TaggedInt::Is(aValue), "Should be detected");
  684. AssertMsg(VarIs<RecyclableObject>(aValue), "Should be handled already");
  685. auto type = UnsafeVarTo<RecyclableObject>(aValue)->GetType();
  686. switch (type->GetTypeId())
  687. {
  688. case TypeIds_Undefined:
  689. case TypeIds_Null:
  690. case TypeIds_VariantDate:
  691. return false;
  692. case TypeIds_Symbol:
  693. return true;
  694. case TypeIds_Boolean:
  695. return UnsafeVarTo<JavascriptBoolean>(aValue)->GetValue();
  696. #if !FLOATVAR
  697. case TypeIds_Number:
  698. {
  699. double value = JavascriptNumber::GetValue(aValue);
  700. return (!JavascriptNumber::IsNan(value)) && (!JavascriptNumber::IsZero(value));
  701. }
  702. #endif
  703. case TypeIds_Int64Number:
  704. {
  705. __int64 value = UnsafeVarTo<JavascriptInt64Number>(aValue)->GetValue();
  706. return value != 0;
  707. }
  708. case TypeIds_UInt64Number:
  709. {
  710. unsigned __int64 value = UnsafeVarTo<JavascriptUInt64Number>(aValue)->GetValue();
  711. return value != 0;
  712. }
  713. case TypeIds_String:
  714. {
  715. JavascriptString * pstValue = UnsafeVarTo<JavascriptString>(aValue);
  716. return pstValue->GetLength() > 0;
  717. }
  718. default:
  719. {
  720. AssertMsg(JavascriptOperators::IsObject(aValue), "bad type object in conversion ToBoolean");
  721. // Falsy objects evaluate to false when converted to Boolean.
  722. return !type->IsFalsy();
  723. }
  724. }
  725. }
  726. void JavascriptConversion::ToFloat_Helper(Var aValue, float *pResult, ScriptContext* scriptContext)
  727. {
  728. JIT_HELPER_REENTRANT_HEADER(Op_ConvFloat_Helper);
  729. *pResult = (float)ToNumber_Full(aValue, scriptContext);
  730. JIT_HELPER_END(Op_ConvFloat_Helper);
  731. }
  732. void JavascriptConversion::ToNumber_Helper(Var aValue, double *pResult, ScriptContext* scriptContext)
  733. {
  734. JIT_HELPER_REENTRANT_HEADER(Op_ConvNumber_Helper);
  735. Assert(Js::JavascriptStackWalker::ValidateTopJitFrame(scriptContext));
  736. *pResult = ToNumber_Full(aValue, scriptContext);
  737. JIT_HELPER_END(Op_ConvNumber_Helper);
  738. }
  739. // Used for the JIT's float type specialization
  740. // Convert aValue to double, but only allow primitives. Return false otherwise.
  741. BOOL JavascriptConversion::ToNumber_FromPrimitive(Var aValue, double *pResult, BOOL allowUndefined, ScriptContext* scriptContext)
  742. {
  743. JIT_HELPER_REENTRANT_HEADER(Op_ConvNumber_FromPrimitive);
  744. Assert(Js::JavascriptStackWalker::ValidateTopJitFrame(scriptContext));
  745. Assert(!TaggedNumber::Is(aValue));
  746. RecyclableObject *obj = VarTo<RecyclableObject>(aValue);
  747. // NOTE: Don't allow strings, otherwise JIT's float type specialization has to worry about concats
  748. if (obj->GetTypeId() >= TypeIds_String)
  749. {
  750. return false;
  751. }
  752. if (!allowUndefined && obj->GetTypeId() == TypeIds_Undefined)
  753. {
  754. return false;
  755. }
  756. *pResult = ToNumber_Full(aValue, scriptContext);
  757. return true;
  758. JIT_HELPER_END(Op_ConvNumber_FromPrimitive);
  759. }
  760. //----------------------------------------------------------------------------
  761. // ToNumber_Full:
  762. // Implements ES6 Draft Rev 26 July 18, 2014
  763. //
  764. // Undefined: NaN
  765. // Null: 0
  766. // boolean: v==true ? 1 : 0 ;
  767. // number: v (original number)
  768. // String: conversion by spec algorithm
  769. // object: ToNumber(PrimitiveValue(v, hint_number))
  770. // Symbol: TypeError
  771. //----------------------------------------------------------------------------
  772. double JavascriptConversion::ToNumber_Full(Var aValue,ScriptContext* scriptContext)
  773. {
  774. AssertMsg(!TaggedInt::Is(aValue), "Should be detected");
  775. ScriptContext * objectScriptContext = VarIs<RecyclableObject>(aValue) ? UnsafeVarTo<RecyclableObject>(aValue)->GetScriptContext() : nullptr;
  776. BOOL fPrimitiveOnly = false;
  777. while(true)
  778. {
  779. switch (JavascriptOperators::GetTypeId(aValue))
  780. {
  781. case TypeIds_Symbol:
  782. JavascriptError::TryThrowTypeError(objectScriptContext, scriptContext, JSERR_NeedNumber);
  783. // Fallthrough to return NaN if exceptions are disabled
  784. case TypeIds_Undefined:
  785. return JavascriptNumber::GetValue(scriptContext->GetLibrary()->GetNaN());
  786. case TypeIds_Null:
  787. return 0;
  788. case TypeIds_Integer:
  789. return TaggedInt::ToDouble(aValue);
  790. case TypeIds_Boolean:
  791. return UnsafeVarTo<JavascriptBoolean>(aValue)->GetValue() ? 1 : +0;
  792. case TypeIds_Number:
  793. return JavascriptNumber::GetValue(aValue);
  794. case TypeIds_Int64Number:
  795. return (double)UnsafeVarTo<JavascriptInt64Number>(aValue)->GetValue();
  796. case TypeIds_UInt64Number:
  797. return (double)UnsafeVarTo<JavascriptUInt64Number>(aValue)->GetValue();
  798. case TypeIds_String:
  799. return UnsafeVarTo<JavascriptString>(aValue)->ToDouble();
  800. case TypeIds_VariantDate:
  801. return Js::DateImplementation::GetTvUtc(Js::DateImplementation::JsLocalTimeFromVarDate(UnsafeVarTo<JavascriptVariantDate>(aValue)->GetValue()), scriptContext);
  802. default:
  803. {
  804. AssertMsg(JavascriptOperators::IsObject(aValue), "bad type object in conversion ToInteger");
  805. if(fPrimitiveOnly)
  806. {
  807. JavascriptError::ThrowError(scriptContext, VBSERR_OLENoPropOrMethod);
  808. }
  809. fPrimitiveOnly = true;
  810. aValue = ToPrimitive<JavascriptHint::HintNumber>(aValue, scriptContext);
  811. }
  812. }
  813. }
  814. }
  815. //----------------------------------------------------------------------------
  816. // second part of the ToInteger() implementation.(ES5.0: S9.4).
  817. //----------------------------------------------------------------------------
  818. double JavascriptConversion::ToInteger_Full(Var aValue,ScriptContext* scriptContext)
  819. {
  820. AssertMsg(!TaggedInt::Is(aValue), "Should be detected");
  821. ScriptContext * objectScriptContext = VarIs<RecyclableObject>(aValue) ? UnsafeVarTo<RecyclableObject>(aValue)->GetScriptContext() : nullptr;
  822. BOOL fPrimitiveOnly = false;
  823. while(true)
  824. {
  825. switch (JavascriptOperators::GetTypeId(aValue))
  826. {
  827. case TypeIds_Symbol:
  828. JavascriptError::TryThrowTypeError(objectScriptContext, scriptContext, JSERR_NeedNumber);
  829. // Fallthrough to return 0 if exceptions are disabled
  830. case TypeIds_Undefined:
  831. case TypeIds_Null:
  832. return 0;
  833. case TypeIds_Integer:
  834. return TaggedInt::ToInt32(aValue);
  835. case TypeIds_Boolean:
  836. return UnsafeVarTo<JavascriptBoolean>(aValue)->GetValue() ? 1 : +0;
  837. case TypeIds_Number:
  838. return ToInteger(JavascriptNumber::GetValue(aValue));
  839. case TypeIds_Int64Number:
  840. return ToInteger((double)UnsafeVarTo<JavascriptInt64Number>(aValue)->GetValue());
  841. case TypeIds_UInt64Number:
  842. return ToInteger((double)UnsafeVarTo<JavascriptUInt64Number>(aValue)->GetValue());
  843. case TypeIds_String:
  844. return ToInteger(UnsafeVarTo<JavascriptString>(aValue)->ToDouble());
  845. case TypeIds_VariantDate:
  846. return ToInteger(ToNumber_Full(aValue, scriptContext));
  847. default:
  848. {
  849. AssertMsg(JavascriptOperators::IsObject(aValue), "bad type object in conversion ToInteger");
  850. if(fPrimitiveOnly)
  851. {
  852. AssertMsg(FALSE, "wrong call in ToInteger_Full, no dynamic objects should get here");
  853. JavascriptError::ThrowError(scriptContext, VBSERR_OLENoPropOrMethod);
  854. }
  855. fPrimitiveOnly = true;
  856. aValue = ToPrimitive<JavascriptHint::HintNumber>(aValue, scriptContext);
  857. }
  858. }
  859. }
  860. }
  861. double JavascriptConversion::ToInteger(double val)
  862. {
  863. if(JavascriptNumber::IsNan(val))
  864. return 0;
  865. if(JavascriptNumber::IsPosInf(val) || JavascriptNumber::IsNegInf(val) ||
  866. JavascriptNumber::IsZero(val))
  867. {
  868. return val;
  869. }
  870. return ( ((val < 0) ? -1 : 1 ) * floor(fabs(val)));
  871. }
  872. //----------------------------------------------------------------------------
  873. // ToInt32() converts the given Var to an Int32 value, as described in
  874. // (ES3.0: S9.5).
  875. //----------------------------------------------------------------------------
  876. int32 JavascriptConversion::ToInt32_Full(Var aValue, ScriptContext* scriptContext)
  877. {
  878. JIT_HELPER_REENTRANT_HEADER(Conv_ToInt32_Full);
  879. Assert(Js::JavascriptStackWalker::ValidateTopJitFrame(scriptContext));
  880. AssertMsg(!TaggedInt::Is(aValue), "Should be detected");
  881. ScriptContext * objectScriptContext = VarIs<RecyclableObject>(aValue) ? UnsafeVarTo<RecyclableObject>(aValue)->GetScriptContext() : nullptr;
  882. // This is used when TaggedInt's overflow but remain under int32
  883. // so Number is our most critical case:
  884. TypeId typeId = JavascriptOperators::GetTypeId(aValue);
  885. if (typeId == TypeIds_Number)
  886. {
  887. return JavascriptMath::ToInt32Core(JavascriptNumber::GetValue(aValue));
  888. }
  889. switch (typeId)
  890. {
  891. case TypeIds_Symbol:
  892. JavascriptError::TryThrowTypeError(objectScriptContext, scriptContext, JSERR_NeedNumber);
  893. // Fallthrough to return 0 if exceptions are disabled
  894. case TypeIds_Undefined:
  895. case TypeIds_Null:
  896. return 0;
  897. case TypeIds_Integer:
  898. return TaggedInt::ToInt32(aValue);
  899. case TypeIds_Boolean:
  900. return UnsafeVarTo<JavascriptBoolean>(aValue)->GetValue() ? 1 : +0;
  901. case TypeIds_Int64Number:
  902. // we won't lose precision if the int64 is within 32bit boundary; otherwise we need to
  903. // treat it as double anyhow.
  904. return JavascriptMath::ToInt32Core((double)UnsafeVarTo<JavascriptInt64Number>(aValue)->GetValue());
  905. case TypeIds_UInt64Number:
  906. // we won't lose precision if the int64 is within 32bit boundary; otherwise we need to
  907. // treat it as double anyhow.
  908. return JavascriptMath::ToInt32Core((double)UnsafeVarTo<JavascriptUInt64Number>(aValue)->GetValue());
  909. case TypeIds_String:
  910. {
  911. double result;
  912. if (UnsafeVarTo<JavascriptString>(aValue)->ToDouble(&result))
  913. {
  914. return JavascriptMath::ToInt32Core(result);
  915. }
  916. // If the string isn't a valid number, ToDouble returns NaN, and ToInt32 of that is 0
  917. return 0;
  918. }
  919. case TypeIds_VariantDate:
  920. return ToInt32(ToNumber_Full(aValue, scriptContext));
  921. default:
  922. AssertMsg(JavascriptOperators::IsObject(aValue), "bad type object in conversion ToInteger32");
  923. aValue = ToPrimitive<JavascriptHint::HintNumber>(aValue, scriptContext);
  924. }
  925. switch (JavascriptOperators::GetTypeId(aValue))
  926. {
  927. case TypeIds_Symbol:
  928. JavascriptError::TryThrowTypeError(objectScriptContext, scriptContext, JSERR_NeedNumber);
  929. // Fallthrough to return 0 if exceptions are disabled
  930. case TypeIds_Undefined:
  931. case TypeIds_Null:
  932. return 0;
  933. case TypeIds_Integer:
  934. return TaggedInt::ToInt32(aValue);
  935. case TypeIds_Boolean:
  936. return UnsafeVarTo<JavascriptBoolean>(aValue)->GetValue() ? 1 : +0;
  937. case TypeIds_Number:
  938. return ToInt32(JavascriptNumber::GetValue(aValue));
  939. case TypeIds_Int64Number:
  940. // we won't lose precision if the int64 is within 32bit boundary; otherwise we need to
  941. // treat it as double anyhow.
  942. return JavascriptMath::ToInt32Core((double)UnsafeVarTo<JavascriptInt64Number>(aValue)->GetValue());
  943. case TypeIds_UInt64Number:
  944. // we won't lose precision if the int64 is within 32bit boundary; otherwise we need to
  945. // treat it as double anyhow.
  946. return JavascriptMath::ToInt32Core((double)UnsafeVarTo<JavascriptUInt64Number>(aValue)->GetValue());
  947. case TypeIds_String:
  948. {
  949. double result;
  950. if (UnsafeVarTo<JavascriptString>(aValue)->ToDouble(&result))
  951. {
  952. return ToInt32(result);
  953. }
  954. // If the string isn't a valid number, ToDouble returns NaN, and ToInt32 of that is 0
  955. return 0;
  956. }
  957. case TypeIds_VariantDate:
  958. return ToInt32(ToNumber_Full(aValue, scriptContext));
  959. default:
  960. AssertMsg(FALSE, "wrong call in ToInteger32_Full, no dynamic objects should get here.");
  961. JavascriptError::ThrowError(scriptContext, VBSERR_OLENoPropOrMethod);
  962. }
  963. JIT_HELPER_END(Conv_ToInt32_Full);
  964. }
  965. // a strict version of ToInt32 conversion that returns false for non int32 values like, inf, NaN, undef
  966. BOOL JavascriptConversion::ToInt32Finite(Var aValue, ScriptContext* scriptContext, int32* result)
  967. {
  968. ScriptContext * objectScriptContext = VarIs<RecyclableObject>(aValue) ? UnsafeVarTo<RecyclableObject>(aValue)->GetScriptContext() : nullptr;
  969. BOOL fPrimitiveOnly = false;
  970. while(true)
  971. {
  972. switch (JavascriptOperators::GetTypeId(aValue))
  973. {
  974. case TypeIds_Symbol:
  975. JavascriptError::TryThrowTypeError(objectScriptContext, scriptContext, JSERR_NeedNumber);
  976. // Fallthrough to return false and set result to 0 if exceptions are disabled
  977. case TypeIds_Undefined:
  978. *result = 0;
  979. return false;
  980. case TypeIds_Null:
  981. *result = 0;
  982. return true;
  983. case TypeIds_Integer:
  984. *result = TaggedInt::ToInt32(aValue);
  985. return true;
  986. case TypeIds_Boolean:
  987. *result = UnsafeVarTo<JavascriptBoolean>(aValue)->GetValue() ? 1 : +0;
  988. return true;
  989. case TypeIds_Number:
  990. return ToInt32Finite(JavascriptNumber::GetValue(aValue), result);
  991. case TypeIds_Int64Number:
  992. // we won't lose precision if the int64 is within 32bit boundary; otherwise we need to
  993. // treat it as double anyhow.
  994. return ToInt32Finite((double)UnsafeVarTo<JavascriptInt64Number>(aValue)->GetValue(), result);
  995. case TypeIds_UInt64Number:
  996. // we won't lose precision if the int64 is within 32bit boundary; otherwise we need to
  997. // treat it as double anyhow.
  998. return ToInt32Finite((double)UnsafeVarTo<JavascriptUInt64Number>(aValue)->GetValue(), result);
  999. case TypeIds_String:
  1000. return ToInt32Finite(UnsafeVarTo<JavascriptString>(aValue)->ToDouble(), result);
  1001. case TypeIds_VariantDate:
  1002. return ToInt32Finite(ToNumber_Full(aValue, scriptContext), result);
  1003. default:
  1004. {
  1005. AssertMsg(JavascriptOperators::IsObject(aValue), "bad type object in conversion ToInteger32");
  1006. if(fPrimitiveOnly)
  1007. {
  1008. AssertMsg(FALSE, "wrong call in ToInteger32_Full, no dynamic objects should get here");
  1009. JavascriptError::ThrowError(scriptContext, VBSERR_OLENoPropOrMethod);
  1010. }
  1011. fPrimitiveOnly = true;
  1012. aValue = ToPrimitive<JavascriptHint::HintNumber>(aValue, scriptContext);
  1013. }
  1014. }
  1015. }
  1016. }
  1017. int32 JavascriptConversion::ToInt32(double T1)
  1018. {
  1019. return JavascriptMath::ToInt32Core(T1);
  1020. }
  1021. __int64 JavascriptConversion::ToInt64(Var aValue, ScriptContext* scriptContext)
  1022. {
  1023. switch (JavascriptOperators::GetTypeId(aValue))
  1024. {
  1025. case TypeIds_Integer:
  1026. {
  1027. return TaggedInt::ToInt32(aValue);
  1028. }
  1029. case TypeIds_Int64Number:
  1030. {
  1031. JavascriptInt64Number* int64Number = UnsafeVarTo<JavascriptInt64Number>(aValue);
  1032. return int64Number->GetValue();
  1033. }
  1034. case TypeIds_UInt64Number:
  1035. {
  1036. JavascriptUInt64Number* uint64Number = UnsafeVarTo<JavascriptUInt64Number>(aValue);
  1037. return (__int64)uint64Number->GetValue();
  1038. }
  1039. case TypeIds_Number:
  1040. return JavascriptMath::TryToInt64(JavascriptNumber::GetValue(aValue));
  1041. default:
  1042. return (unsigned __int64)JavascriptConversion::ToInt32_Full(aValue, scriptContext);
  1043. }
  1044. }
  1045. unsigned __int64 JavascriptConversion::ToUInt64(Var aValue, ScriptContext* scriptContext)
  1046. {
  1047. switch (JavascriptOperators::GetTypeId(aValue))
  1048. {
  1049. case TypeIds_Integer:
  1050. {
  1051. return (unsigned __int64)TaggedInt::ToInt32(aValue);
  1052. }
  1053. case TypeIds_Int64Number:
  1054. {
  1055. JavascriptInt64Number* int64Number = UnsafeVarTo<JavascriptInt64Number>(aValue);
  1056. return (unsigned __int64)int64Number->GetValue();
  1057. }
  1058. case TypeIds_UInt64Number:
  1059. {
  1060. JavascriptUInt64Number* uint64Number = UnsafeVarTo<JavascriptUInt64Number>(aValue);
  1061. return uint64Number->GetValue();
  1062. }
  1063. case TypeIds_Number:
  1064. return static_cast<unsigned __int64>(JavascriptMath::TryToInt64(JavascriptNumber::GetValue(aValue)));
  1065. default:
  1066. return (unsigned __int64)JavascriptConversion::ToInt32_Full(aValue, scriptContext);
  1067. }
  1068. }
  1069. BOOL JavascriptConversion::ToInt32Finite(double value, int32* result)
  1070. {
  1071. if((!NumberUtilities::IsFinite(value)) || JavascriptNumber::IsNan(value))
  1072. {
  1073. *result = 0;
  1074. return false;
  1075. }
  1076. else
  1077. {
  1078. *result = JavascriptMath::ToInt32Core(value);
  1079. return true;
  1080. }
  1081. }
  1082. //----------------------------------------------------------------------------
  1083. // (ES3.0: S9.6).
  1084. //----------------------------------------------------------------------------
  1085. uint32 JavascriptConversion::ToUInt32_Full(Var aValue, ScriptContext* scriptContext)
  1086. {
  1087. JIT_HELPER_REENTRANT_HEADER(Conv_ToUInt32_Full);
  1088. AssertMsg(!TaggedInt::Is(aValue), "Should be detected");
  1089. ScriptContext * objectScriptContext = VarIs<RecyclableObject>(aValue) ? UnsafeVarTo<RecyclableObject>(aValue)->GetScriptContext() : nullptr;
  1090. BOOL fPrimitiveOnly = false;
  1091. while(true)
  1092. {
  1093. switch (JavascriptOperators::GetTypeId(aValue))
  1094. {
  1095. case TypeIds_Symbol:
  1096. JavascriptError::TryThrowTypeError(objectScriptContext, scriptContext, JSERR_NeedNumber);
  1097. // Fallthrough to return 0 if exceptions are disabled
  1098. case TypeIds_Undefined:
  1099. case TypeIds_Null:
  1100. return 0;
  1101. case TypeIds_Integer:
  1102. return TaggedInt::ToUInt32(aValue);
  1103. case TypeIds_Boolean:
  1104. return UnsafeVarTo<JavascriptBoolean>(aValue)->GetValue() ? 1 : +0;
  1105. case TypeIds_Number:
  1106. return JavascriptMath::ToUInt32(JavascriptNumber::GetValue(aValue));
  1107. case TypeIds_Int64Number:
  1108. // we won't lose precision if the int64 is within 32bit boundary; otherwise we need to
  1109. // treat it as double anyhow.
  1110. return JavascriptMath::ToUInt32((double)UnsafeVarTo<JavascriptInt64Number>(aValue)->GetValue());
  1111. case TypeIds_UInt64Number:
  1112. // we won't lose precision if the int64 is within 32bit boundary; otherwise we need to
  1113. // treat it as double anyhow.
  1114. return JavascriptMath::ToUInt32((double)UnsafeVarTo<JavascriptUInt64Number>(aValue)->GetValue());
  1115. case TypeIds_String:
  1116. {
  1117. double result;
  1118. if (UnsafeVarTo<JavascriptString>(aValue)->ToDouble(&result))
  1119. {
  1120. return JavascriptMath::ToUInt32(result);
  1121. }
  1122. // If the string isn't a valid number, ToDouble returns NaN, and ToUInt32 of that is 0
  1123. return 0;
  1124. }
  1125. case TypeIds_VariantDate:
  1126. return JavascriptMath::ToUInt32(ToNumber_Full(aValue, scriptContext));
  1127. default:
  1128. {
  1129. AssertMsg(JavascriptOperators::IsObject(aValue), "bad type object in conversion ToUInt32");
  1130. if(fPrimitiveOnly)
  1131. {
  1132. AssertMsg(FALSE, "wrong call in ToUInt32_Full, no dynamic objects should get here");
  1133. JavascriptError::ThrowError(scriptContext, VBSERR_OLENoPropOrMethod);
  1134. }
  1135. fPrimitiveOnly = true;
  1136. aValue = ToPrimitive<JavascriptHint::HintNumber>(aValue, scriptContext);
  1137. }
  1138. }
  1139. }
  1140. JIT_HELPER_END(Conv_ToUInt32_Full);
  1141. }
  1142. // Unable to put JIT_HELPER macro in .inl file, do instantiation here
  1143. JIT_HELPER_TEMPLATE(Conv_ToUInt32, Conv_ToUInt32)
  1144. JIT_HELPER_TEMPLATE(Conv_ToBoolean, Conv_ToBoolean)
  1145. uint32 JavascriptConversion::ToUInt32(double T1)
  1146. {
  1147. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(Conv_ToUInt32Core);
  1148. JIT_HELPER_SAME_ATTRIBUTES(Conv_ToInt32Core, Conv_ToUInt32Core);
  1149. // Same as doing ToInt32 and reinterpret the bits as uint32
  1150. return (uint32)JavascriptMath::ToInt32Core(T1);
  1151. JIT_HELPER_END(Conv_ToUInt32Core);
  1152. }
  1153. //----------------------------------------------------------------------------
  1154. // ToUInt16() converts the given Var to a UInt16 value, as described in
  1155. // (ES3.0: S9.6).
  1156. //----------------------------------------------------------------------------
  1157. uint16 JavascriptConversion::ToUInt16_Full(IN Var aValue, ScriptContext* scriptContext)
  1158. {
  1159. AssertMsg(!TaggedInt::Is(aValue), "Should be detected");
  1160. ScriptContext * objectScriptContext = VarIs<RecyclableObject>(aValue) ? UnsafeVarTo<RecyclableObject>(aValue)->GetScriptContext() : nullptr;
  1161. BOOL fPrimitiveOnly = false;
  1162. while(true)
  1163. {
  1164. switch (JavascriptOperators::GetTypeId(aValue))
  1165. {
  1166. case TypeIds_Symbol:
  1167. JavascriptError::TryThrowTypeError(objectScriptContext, scriptContext, JSERR_NeedNumber);
  1168. // Fallthrough to return 0 if exceptions are disabled
  1169. case TypeIds_Undefined:
  1170. case TypeIds_Null:
  1171. return 0;
  1172. case TypeIds_Integer:
  1173. return TaggedInt::ToUInt16(aValue);
  1174. case TypeIds_Boolean:
  1175. return UnsafeVarTo<JavascriptBoolean>(aValue)->GetValue() ? 1 : +0;
  1176. case TypeIds_Number:
  1177. return ToUInt16(JavascriptNumber::GetValue(aValue));
  1178. case TypeIds_Int64Number:
  1179. // we won't lose precision if the int64 is within 16bit boundary; otherwise we need to
  1180. // treat it as double anyhow.
  1181. return ToUInt16((double)UnsafeVarTo<JavascriptInt64Number>(aValue)->GetValue());
  1182. case TypeIds_UInt64Number:
  1183. // we won't lose precision if the int64 is within 16bit boundary; otherwise we need to
  1184. // treat it as double anyhow.
  1185. return ToUInt16((double)UnsafeVarTo<JavascriptUInt64Number>(aValue)->GetValue());
  1186. case TypeIds_String:
  1187. {
  1188. double result;
  1189. if (UnsafeVarTo<JavascriptString>(aValue)->ToDouble(&result))
  1190. {
  1191. return ToUInt16(result);
  1192. }
  1193. // If the string isn't a valid number, ToDouble is NaN, and ToUInt16 of that is 0
  1194. return 0;
  1195. }
  1196. case TypeIds_VariantDate:
  1197. return ToUInt16(ToNumber_Full(aValue, scriptContext));
  1198. default:
  1199. {
  1200. AssertMsg(JavascriptOperators::IsObject(aValue), "bad type object in conversion ToUIn16");
  1201. if(fPrimitiveOnly)
  1202. {
  1203. AssertMsg(FALSE, "wrong call in ToUInt16, no dynamic objects should get here");
  1204. JavascriptError::ThrowError(scriptContext, VBSERR_OLENoPropOrMethod);
  1205. }
  1206. fPrimitiveOnly = true;
  1207. aValue = ToPrimitive<JavascriptHint::HintNumber>(aValue, scriptContext);
  1208. }
  1209. }
  1210. }
  1211. }
  1212. uint16 JavascriptConversion::ToUInt16(double T1)
  1213. {
  1214. //
  1215. // VC does the right thing here, if we first convert to uint32 and then to uint16
  1216. // Spec says mod should be done.
  1217. //
  1218. uint32 result = JavascriptConversion::ToUInt32(T1);
  1219. #if defined(_M_IX86) && _MSC_FULL_VER < 160030329
  1220. // Well VC doesn't actually do the right thing... It takes (uint16)(uint32)double and removes the
  1221. // middle uint32 cast to (uint16)double, which isn't the same thing. Somehow, it only seems to be a
  1222. // problem for x86. Forcing a store to uint32 prevents the incorrect optimization.
  1223. //
  1224. // A bug has been filled in the Dev11 database: TF bug id #901495
  1225. // Fixed in compiler 16.00.30329.00
  1226. volatile uint32 volResult = result;
  1227. #endif
  1228. return (uint16) result;
  1229. }
  1230. int16 JavascriptConversion::ToInt16(double aValue)
  1231. {
  1232. return (int16)ToInt32(aValue);
  1233. }
  1234. int8 JavascriptConversion::ToInt8(double aValue)
  1235. {
  1236. return (int8)ToInt32(aValue);
  1237. }
  1238. uint8 JavascriptConversion::ToUInt8(double aValue)
  1239. {
  1240. return (uint8)ToUInt32(aValue);
  1241. }
  1242. JavascriptString * JavascriptConversion::ToPrimitiveString(Var aValue, ScriptContext * scriptContext)
  1243. {
  1244. JIT_HELPER_REENTRANT_HEADER(Op_ConvPrimitiveString);
  1245. return ToString(ToPrimitive<JavascriptHint::None>(aValue, scriptContext), scriptContext);
  1246. JIT_HELPER_END(Op_ConvPrimitiveString);
  1247. }
  1248. double JavascriptConversion::LongToDouble(__int64 aValue)
  1249. {
  1250. return static_cast<double>(aValue);
  1251. }
  1252. // Windows x64 version implemented in masm to work around precision limitation
  1253. #if !defined(_WIN32 ) || !defined(_M_X64)
  1254. double JavascriptConversion::ULongToDouble(unsigned __int64 aValue)
  1255. {
  1256. return static_cast<double>(aValue);
  1257. }
  1258. #endif
  1259. float JavascriptConversion::LongToFloat(__int64 aValue)
  1260. {
  1261. return static_cast<float>(aValue);
  1262. }
  1263. float JavascriptConversion::ULongToFloat (unsigned __int64 aValue)
  1264. {
  1265. return static_cast<float>(aValue);
  1266. }
  1267. int64 JavascriptConversion::ToLength(Var aValue, ScriptContext* scriptContext)
  1268. {
  1269. if (TaggedInt::Is(aValue))
  1270. {
  1271. int64 length = TaggedInt::ToInt64(aValue);
  1272. return (length < 0) ? 0 : length;
  1273. }
  1274. double length = JavascriptConversion::ToInteger(aValue, scriptContext);
  1275. if (length < 0.0 || JavascriptNumber::IsNegZero(length))
  1276. {
  1277. length = 0.0;
  1278. }
  1279. else if (length > Math::MAX_SAFE_INTEGER)
  1280. {
  1281. length = Math::MAX_SAFE_INTEGER;
  1282. }
  1283. return NumberUtilities::TryToInt64(length);
  1284. }
  1285. JavascriptBigInt *JavascriptConversion::ToBigInt(Var aValue, ScriptContext* scriptContext)
  1286. {
  1287. Assert(scriptContext->GetThreadContext()->IsScriptActive());
  1288. switch (JavascriptOperators::GetTypeId(aValue))
  1289. {
  1290. case TypeIds_BigInt:
  1291. break;
  1292. default:
  1293. AssertOrFailFastMsg(false, "do not support conversion of other types in ToBigInt");
  1294. }
  1295. return UnsafeVarTo<JavascriptBigInt>(aValue);
  1296. }