JavascriptConversion.cpp 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441
  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. Var 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. key = propName;
  277. }
  278. if (propString)
  279. {
  280. *propString = propertyString;
  281. }
  282. return key;
  283. }
  284. //----------------------------------------------------------------------------
  285. // ToPrimitive() takes a value and an optional argument and converts it to a non Object type
  286. // Implementation of ES5 9.1
  287. //
  288. // Undefined:The result equals the input argument (no conversion).
  289. // Null: The result equals the input argument (no conversion).
  290. // Boolean: The result equals the input argument (no conversion).
  291. // Number: The result equals the input argument (no conversion).
  292. // String: The result equals the input argument (no conversion).
  293. // Object: Return a default value for the Object.
  294. // The default value of an object is retrieved by calling the [[DefaultValue]]
  295. // internal method of the object, passing the optional hint PreferredType.
  296. // The behavior of the [[DefaultValue]] internal method is defined by this specification
  297. // for all native ECMAScript objects (8.12.9).
  298. //----------------------------------------------------------------------------
  299. template <JavascriptHint hint>
  300. Var JavascriptConversion::ToPrimitive(_In_ Var aValue, _In_ ScriptContext * requestContext)
  301. {
  302. switch (JavascriptOperators::GetTypeId(aValue))
  303. {
  304. case TypeIds_Undefined:
  305. case TypeIds_Null:
  306. case TypeIds_Integer:
  307. case TypeIds_Boolean:
  308. case TypeIds_Number:
  309. case TypeIds_String:
  310. case TypeIds_Symbol:
  311. case TypeIds_BigInt:
  312. return aValue;
  313. case TypeIds_StringObject:
  314. {
  315. JavascriptStringObject * stringObject = UnsafeVarTo<JavascriptStringObject>(aValue);
  316. ScriptContext * objectScriptContext = stringObject->GetScriptContext();
  317. if (objectScriptContext->optimizationOverrides.GetSideEffects() & (hint == JavascriptHint::HintString ? SideEffects_ToString : SideEffects_ValueOf))
  318. {
  319. return MethodCallToPrimitive<hint>(stringObject, requestContext);
  320. }
  321. return CrossSite::MarshalVar(requestContext, stringObject->Unwrap(), objectScriptContext);
  322. }
  323. case TypeIds_NumberObject:
  324. {
  325. JavascriptNumberObject * numberObject = UnsafeVarTo<JavascriptNumberObject>(aValue);
  326. ScriptContext * objectScriptContext = numberObject->GetScriptContext();
  327. if (hint == JavascriptHint::HintString)
  328. {
  329. if (objectScriptContext->optimizationOverrides.GetSideEffects() & SideEffects_ToString)
  330. {
  331. return MethodCallToPrimitive<hint>(numberObject, requestContext);
  332. }
  333. return JavascriptNumber::ToStringRadix10(numberObject->GetValue(), requestContext);
  334. }
  335. else
  336. {
  337. if (objectScriptContext->optimizationOverrides.GetSideEffects() & SideEffects_ValueOf)
  338. {
  339. return MethodCallToPrimitive<hint>(numberObject, requestContext);
  340. }
  341. return CrossSite::MarshalVar(requestContext, numberObject->Unwrap(), objectScriptContext);
  342. }
  343. }
  344. case TypeIds_SymbolObject:
  345. {
  346. JavascriptSymbolObject* symbolObject = UnsafeVarTo<JavascriptSymbolObject>(aValue);
  347. ScriptContext* objectScriptContext = symbolObject->GetScriptContext();
  348. if (objectScriptContext->optimizationOverrides.GetSideEffects() & SideEffects_ToPrimitive)
  349. {
  350. return MethodCallToPrimitive<hint>(symbolObject, requestContext);
  351. }
  352. return CrossSite::MarshalVar(requestContext, symbolObject->Unwrap(), objectScriptContext);
  353. }
  354. case TypeIds_Date:
  355. {
  356. JavascriptDate* dateObject = UnsafeVarTo<JavascriptDate>(aValue);
  357. if(hint == JavascriptHint::HintNumber)
  358. {
  359. if (dateObject->GetScriptContext()->optimizationOverrides.GetSideEffects() & SideEffects_ValueOf)
  360. {
  361. // if no Method exists this function falls back to OrdinaryToPrimitive
  362. // if IsES6ToPrimitiveEnabled flag is off we also fall back to OrdinaryToPrimitive
  363. return MethodCallToPrimitive<hint>(dateObject, requestContext);
  364. }
  365. return JavascriptNumber::ToVarNoCheck(dateObject->GetTime(), requestContext);
  366. }
  367. else
  368. {
  369. if (dateObject->GetScriptContext()->optimizationOverrides.GetSideEffects() & SideEffects_ToString)
  370. {
  371. // if no Method exists this function falls back to OrdinaryToPrimitive
  372. // if IsES6ToPrimitiveEnabled flag is off we also fall back to OrdinaryToPrimitive
  373. return MethodCallToPrimitive<hint>(dateObject, requestContext);
  374. }
  375. return JavascriptDate::ToString(dateObject, requestContext);
  376. }
  377. }
  378. // convert to JavascriptNumber
  379. case TypeIds_Int64Number:
  380. return UnsafeVarTo<JavascriptInt64Number>(aValue)->ToJavascriptNumber();
  381. case TypeIds_UInt64Number:
  382. return UnsafeVarTo<JavascriptUInt64Number>(aValue)->ToJavascriptNumber();
  383. default:
  384. // if no Method exists this function falls back to OrdinaryToPrimitive
  385. // if IsES6ToPrimitiveEnabled flag is off we also fall back to OrdinaryToPrimitive
  386. return MethodCallToPrimitive<hint>(UnsafeVarTo<RecyclableObject>(aValue), requestContext);
  387. }
  388. }
  389. //----------------------------------------------------------------------------
  390. //https://tc39.github.io/ecma262/#sec-canonicalnumericindexstring
  391. //1. Assert : Type(argument) is String.
  392. //2. If argument is "-0", then return -0.
  393. //3. Let n be ToNumber(argument).
  394. //4. If SameValue(ToString(n), argument) is false, then return undefined.
  395. //5. Return n.
  396. //----------------------------------------------------------------------------
  397. BOOL JavascriptConversion::CanonicalNumericIndexString(JavascriptString *aValue, double *indexValue, ScriptContext * scriptContext)
  398. {
  399. if (JavascriptString::IsNegZero(aValue))
  400. {
  401. *indexValue = -0;
  402. return TRUE;
  403. }
  404. double indexDoubleValue = aValue->ToDouble();
  405. if (JavascriptString::Equals(JavascriptNumber::ToStringRadix10(indexDoubleValue, scriptContext), aValue))
  406. {
  407. *indexValue = indexDoubleValue;
  408. return TRUE;
  409. }
  410. return FALSE;
  411. }
  412. template <JavascriptHint hint>
  413. Var JavascriptConversion::MethodCallToPrimitive(_In_ RecyclableObject* value, _In_ ScriptContext * requestContext)
  414. {
  415. Var result = nullptr;
  416. ScriptContext *const scriptContext = value->GetScriptContext();
  417. //7.3.9 GetMethod(V, P)
  418. // The abstract operation GetMethod is used to get the value of a specific property of an ECMAScript language value when the value of the
  419. // 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
  420. // property key. This abstract operation performs the following steps:
  421. // 1. Assert: IsPropertyKey(P) is true.
  422. // 2. Let func be ? GetV(V, P).
  423. // 3. If func is either undefined or null, return undefined.
  424. // 4. If IsCallable(func) is false, throw a TypeError exception.
  425. // 5. Return func.
  426. Var varMethod = nullptr;
  427. if (!requestContext->GetConfig()->IsES6ToPrimitiveEnabled()
  428. || JavascriptOperators::CheckIfObjectAndProtoChainHasNoSpecialProperties(value)
  429. || !JavascriptOperators::GetPropertyReference(value, PropertyIds::_symbolToPrimitive, &varMethod, requestContext)
  430. || JavascriptOperators::IsUndefinedOrNull(varMethod))
  431. {
  432. return OrdinaryToPrimitive<hint>(value, requestContext);
  433. }
  434. if (!VarIs<JavascriptFunction>(varMethod))
  435. {
  436. // Don't error if we disabled implicit calls
  437. JavascriptError::TryThrowTypeError(scriptContext, requestContext, JSERR_Property_NeedFunction, requestContext->GetPropertyName(PropertyIds::_symbolToPrimitive)->GetBuffer());
  438. return requestContext->GetLibrary()->GetNull();
  439. }
  440. // Let exoticToPrim be GetMethod(input, @@toPrimitive).
  441. JavascriptFunction* exoticToPrim = UnsafeVarTo<JavascriptFunction>(varMethod);
  442. JavascriptString* hintString = nullptr;
  443. if (hint == JavascriptHint::HintString)
  444. {
  445. hintString = requestContext->GetLibrary()->GetStringTypeDisplayString();
  446. }
  447. else if (hint == JavascriptHint::HintNumber)
  448. {
  449. hintString = requestContext->GetLibrary()->GetNumberTypeDisplayString();
  450. }
  451. else
  452. {
  453. hintString = requestContext->GetPropertyString(PropertyIds::default_);
  454. }
  455. // If exoticToPrim is not undefined, then
  456. Assert(nullptr != exoticToPrim);
  457. ThreadContext * threadContext = requestContext->GetThreadContext();
  458. result = threadContext->ExecuteImplicitCall(exoticToPrim, ImplicitCall_ToPrimitive, [=]()->Js::Var
  459. {
  460. // Stack object should have a pre-op bail on implicit call. We shouldn't see them here.
  461. Assert(!ThreadContext::IsOnStack(value));
  462. // Let result be the result of calling the[[Call]] internal method of exoticToPrim, with input as thisArgument and(hint) as argumentsList.
  463. return CALL_FUNCTION(threadContext, exoticToPrim, CallInfo(CallFlags_Value, 2), value, hintString);
  464. });
  465. if (!result)
  466. {
  467. // There was an implicit call and implicit calls are disabled. This would typically cause a bailout.
  468. Assert(threadContext->IsDisableImplicitCall());
  469. return requestContext->GetLibrary()->GetNull();
  470. }
  471. Assert(!CrossSite::NeedMarshalVar(result, requestContext));
  472. // If result is an ECMAScript language value and Type(result) is not Object, then return result.
  473. if (TaggedInt::Is(result) || !JavascriptOperators::IsObjectType(JavascriptOperators::GetTypeId(result)))
  474. {
  475. return result;
  476. }
  477. // Else, throw a TypeError exception.
  478. else
  479. {
  480. // Don't error if we disabled implicit calls
  481. JavascriptError::TryThrowTypeError(scriptContext, requestContext, JSERR_FunctionArgument_Invalid, _u("[Symbol.toPrimitive]"));
  482. return requestContext->GetLibrary()->GetNull();
  483. }
  484. }
  485. template <JavascriptHint hint>
  486. Var JavascriptConversion::OrdinaryToPrimitive(_In_ RecyclableObject* value, _In_ ScriptContext* requestContext)
  487. {
  488. Var result;
  489. if (!value->ToPrimitive(hint, &result, requestContext))
  490. {
  491. ScriptContext *const scriptContext = value->GetScriptContext();
  492. int32 hCode;
  493. switch (hint)
  494. {
  495. case JavascriptHint::HintNumber:
  496. hCode = JSERR_NeedNumber;
  497. break;
  498. case JavascriptHint::HintString:
  499. hCode = JSERR_NeedString;
  500. break;
  501. default:
  502. hCode = VBSERR_OLENoPropOrMethod;
  503. break;
  504. }
  505. JavascriptError::TryThrowTypeError(scriptContext, scriptContext, hCode);
  506. return requestContext->GetLibrary()->GetNull();
  507. }
  508. return result;
  509. }
  510. template Var JavascriptConversion::OrdinaryToPrimitive<JavascriptHint::HintNumber>(RecyclableObject* value, ScriptContext* requestContext);
  511. template Var JavascriptConversion::OrdinaryToPrimitive<JavascriptHint::HintString>(RecyclableObject* value, ScriptContext* requestContext);
  512. template Var JavascriptConversion::OrdinaryToPrimitive<JavascriptHint::None>(RecyclableObject* value, ScriptContext* requestContext);
  513. JavascriptString *JavascriptConversion::CoerseString(Var aValue, ScriptContext* scriptContext, const char16* apiNameForErrorMsg)
  514. {
  515. JIT_HELPER_REENTRANT_HEADER(Op_CoerseString);
  516. if (!JavascriptConversion::CheckObjectCoercible(aValue, scriptContext))
  517. {
  518. JavascriptError::ThrowTypeError(scriptContext, JSERR_This_NullOrUndefined, apiNameForErrorMsg);
  519. }
  520. return ToString(aValue, scriptContext);
  521. JIT_HELPER_END(Op_CoerseString);
  522. }
  523. //----------------------------------------------------------------------------
  524. // ToString - abstract operation
  525. // ES5 9.8
  526. //Input Type Result
  527. // Undefined
  528. // "undefined"
  529. // Null
  530. // "null"
  531. // Boolean
  532. // If the argument is true, then the result is "true". If the argument is false, then the result is "false".
  533. // Number
  534. // See 9.8.1 below.
  535. // String
  536. // Return the input argument (no conversion)
  537. // Object
  538. // Apply the following steps:
  539. // 1. Let primValue be ToPrimitive(input argument, hint String).
  540. // 2. Return ToString(primValue).
  541. //----------------------------------------------------------------------------
  542. JavascriptString *JavascriptConversion::ToString(Var aValue, ScriptContext* scriptContext)
  543. {
  544. JIT_HELPER_REENTRANT_HEADER(Op_ConvString);
  545. Assert(scriptContext->GetThreadContext()->IsScriptActive());
  546. BOOL fPrimitiveOnly = false;
  547. while(true)
  548. {
  549. switch (JavascriptOperators::GetTypeId(aValue))
  550. {
  551. case TypeIds_Undefined:
  552. return scriptContext->GetLibrary()->GetUndefinedDisplayString();
  553. case TypeIds_Null:
  554. return scriptContext->GetLibrary()->GetNullDisplayString();
  555. case TypeIds_Integer:
  556. return scriptContext->GetIntegerString(aValue);
  557. case TypeIds_Boolean:
  558. return UnsafeVarTo<JavascriptBoolean>(aValue)->GetValue() ? scriptContext->GetLibrary()->GetTrueDisplayString() : scriptContext->GetLibrary()->GetFalseDisplayString();
  559. case TypeIds_Number:
  560. return JavascriptNumber::ToStringRadix10(JavascriptNumber::GetValue(aValue), scriptContext);
  561. case TypeIds_Int64Number:
  562. {
  563. __int64 value = UnsafeVarTo<JavascriptInt64Number>(aValue)->GetValue();
  564. if (!TaggedInt::IsOverflow(value))
  565. {
  566. return scriptContext->GetIntegerString((int)value);
  567. }
  568. else
  569. {
  570. return JavascriptInt64Number::ToString(aValue, scriptContext);
  571. }
  572. }
  573. case TypeIds_UInt64Number:
  574. {
  575. unsigned __int64 value = UnsafeVarTo<JavascriptUInt64Number>(aValue)->GetValue();
  576. if (!TaggedInt::IsOverflow(value))
  577. {
  578. return scriptContext->GetIntegerString((uint)value);
  579. }
  580. else
  581. {
  582. return JavascriptUInt64Number::ToString(aValue, scriptContext);
  583. }
  584. }
  585. case TypeIds_String:
  586. {
  587. ScriptContext* aValueScriptContext = Js::UnsafeVarTo<Js::RecyclableObject>(aValue)->GetScriptContext();
  588. return UnsafeVarTo<JavascriptString>(CrossSite::MarshalVar(scriptContext,
  589. aValue, aValueScriptContext));
  590. }
  591. case TypeIds_Symbol:
  592. return UnsafeVarTo<JavascriptSymbol>(aValue)->ToString(scriptContext);
  593. case TypeIds_SymbolObject:
  594. return JavascriptSymbol::ToString(UnsafeVarTo<JavascriptSymbolObject>(aValue)->GetValue(), scriptContext);
  595. case TypeIds_GlobalObject:
  596. aValue = static_cast<Js::GlobalObject*>(aValue)->ToThis();
  597. // fall through
  598. default:
  599. {
  600. AssertMsg(JavascriptOperators::IsObject(aValue), "bad type object in conversion ToString");
  601. if(fPrimitiveOnly)
  602. {
  603. AssertMsg(FALSE, "wrong call in ToString, no dynamic objects should get here");
  604. JavascriptError::ThrowError(scriptContext, VBSERR_InternalError);
  605. }
  606. fPrimitiveOnly = true;
  607. aValue = ToPrimitive<JavascriptHint::HintString>(aValue, scriptContext);
  608. }
  609. }
  610. }
  611. JIT_HELPER_END(Op_ConvString);
  612. }
  613. JavascriptString *JavascriptConversion::ToLocaleString(Var aValue, ScriptContext* scriptContext)
  614. {
  615. switch (JavascriptOperators::GetTypeId(aValue))
  616. {
  617. case TypeIds_Undefined:
  618. return scriptContext->GetLibrary()->GetUndefinedDisplayString();
  619. case TypeIds_Null:
  620. return scriptContext->GetLibrary()->GetNullDisplayString();
  621. case TypeIds_Integer:
  622. return JavascriptNumber::ToLocaleString(TaggedInt::ToInt32(aValue), scriptContext);
  623. case TypeIds_Boolean:
  624. return UnsafeVarTo<JavascriptBoolean>(aValue)->GetValue() ? scriptContext->GetLibrary()->GetTrueDisplayString() : scriptContext->GetLibrary()->GetFalseDisplayString();
  625. case TypeIds_Int64Number:
  626. return JavascriptNumber::ToLocaleString((double)UnsafeVarTo<JavascriptInt64Number>(aValue)->GetValue(), scriptContext);
  627. case TypeIds_UInt64Number:
  628. return JavascriptNumber::ToLocaleString((double)UnsafeVarTo<JavascriptUInt64Number>(aValue)->GetValue(), scriptContext);
  629. case TypeIds_Number:
  630. return JavascriptNumber::ToLocaleString(JavascriptNumber::GetValue(aValue), scriptContext);
  631. case TypeIds_Symbol:
  632. return UnsafeVarTo<JavascriptSymbol>(aValue)->ToString(scriptContext);
  633. default:
  634. {
  635. RecyclableObject* object = VarTo<RecyclableObject>(aValue);
  636. Var value = JavascriptOperators::GetProperty(object, PropertyIds::toLocaleString, scriptContext, NULL);
  637. if (JavascriptConversion::IsCallable(value))
  638. {
  639. RecyclableObject* toLocaleStringFunction = VarTo<RecyclableObject>(value);
  640. Var aResult = scriptContext->GetThreadContext()->ExecuteImplicitCall(toLocaleStringFunction, Js::ImplicitCall_ToPrimitive, [=]()->Js::Var
  641. {
  642. return CALL_FUNCTION(scriptContext->GetThreadContext(), toLocaleStringFunction, CallInfo(1), aValue);
  643. });
  644. if (VarIs<JavascriptString>(aResult))
  645. {
  646. return UnsafeVarTo<JavascriptString>(aResult);
  647. }
  648. else
  649. {
  650. return JavascriptConversion::ToString(aResult, scriptContext);
  651. }
  652. }
  653. JavascriptError::ThrowTypeError(scriptContext, JSERR_Property_NeedFunction, scriptContext->GetPropertyName(PropertyIds::toLocaleString)->GetBuffer());
  654. }
  655. }
  656. }
  657. //----------------------------------------------------------------------------
  658. // ToBoolean_Full:
  659. // (ES3.0: S9.2):
  660. //
  661. // Input Output
  662. // ----- ------
  663. // 'undefined' 'false'
  664. // 'null' 'false'
  665. // Boolean Value
  666. // Number 'false' if +0, -0, or Nan
  667. // 'true' otherwise
  668. // String 'false' if argument is ""
  669. // 'true' otherwise
  670. // Object 'true'
  671. // Falsy Object 'false'
  672. //----------------------------------------------------------------------------
  673. BOOL JavascriptConversion::ToBoolean_Full(Var aValue, ScriptContext* scriptContext)
  674. {
  675. AssertMsg(!TaggedInt::Is(aValue), "Should be detected");
  676. AssertMsg(VarIs<RecyclableObject>(aValue), "Should be handled already");
  677. auto type = UnsafeVarTo<RecyclableObject>(aValue)->GetType();
  678. switch (type->GetTypeId())
  679. {
  680. case TypeIds_Undefined:
  681. case TypeIds_Null:
  682. return false;
  683. case TypeIds_Symbol:
  684. return true;
  685. #if !FLOATVAR
  686. case TypeIds_Number:
  687. {
  688. double value = JavascriptNumber::GetValue(aValue);
  689. return (!JavascriptNumber::IsNan(value)) && (!JavascriptNumber::IsZero(value));
  690. }
  691. #endif
  692. case TypeIds_Int64Number:
  693. {
  694. __int64 value = UnsafeVarTo<JavascriptInt64Number>(aValue)->GetValue();
  695. return value != 0;
  696. }
  697. case TypeIds_UInt64Number:
  698. {
  699. unsigned __int64 value = UnsafeVarTo<JavascriptUInt64Number>(aValue)->GetValue();
  700. return value != 0;
  701. }
  702. case TypeIds_String:
  703. {
  704. JavascriptString * pstValue = UnsafeVarTo<JavascriptString>(aValue);
  705. return pstValue->GetLength() > 0;
  706. }
  707. default:
  708. {
  709. AssertMsg(JavascriptOperators::IsObject(aValue), "bad type object in conversion ToBoolean");
  710. // Falsy objects evaluate to false when converted to Boolean.
  711. return !type->IsFalsy();
  712. }
  713. }
  714. }
  715. void JavascriptConversion::ToFloat_Helper(Var aValue, float *pResult, ScriptContext* scriptContext)
  716. {
  717. JIT_HELPER_REENTRANT_HEADER(Op_ConvFloat_Helper);
  718. *pResult = (float)ToNumber_Full(aValue, scriptContext);
  719. JIT_HELPER_END(Op_ConvFloat_Helper);
  720. }
  721. void JavascriptConversion::ToNumber_Helper(Var aValue, double *pResult, ScriptContext* scriptContext)
  722. {
  723. JIT_HELPER_REENTRANT_HEADER(Op_ConvNumber_Helper);
  724. Assert(Js::JavascriptStackWalker::ValidateTopJitFrame(scriptContext));
  725. *pResult = ToNumber_Full(aValue, scriptContext);
  726. JIT_HELPER_END(Op_ConvNumber_Helper);
  727. }
  728. // Used for the JIT's float type specialization
  729. // Convert aValue to double, but only allow primitives. Return false otherwise.
  730. BOOL JavascriptConversion::ToNumber_FromPrimitive(Var aValue, double *pResult, BOOL allowUndefined, ScriptContext* scriptContext)
  731. {
  732. JIT_HELPER_REENTRANT_HEADER(Op_ConvNumber_FromPrimitive);
  733. Assert(Js::JavascriptStackWalker::ValidateTopJitFrame(scriptContext));
  734. Assert(!TaggedNumber::Is(aValue));
  735. RecyclableObject *obj = VarTo<RecyclableObject>(aValue);
  736. // NOTE: Don't allow strings, otherwise JIT's float type specialization has to worry about concats
  737. if (obj->GetTypeId() >= TypeIds_String)
  738. {
  739. return false;
  740. }
  741. if (!allowUndefined && obj->GetTypeId() == TypeIds_Undefined)
  742. {
  743. return false;
  744. }
  745. *pResult = ToNumber_Full(aValue, scriptContext);
  746. return true;
  747. JIT_HELPER_END(Op_ConvNumber_FromPrimitive);
  748. }
  749. //----------------------------------------------------------------------------
  750. // ToNumber_Full:
  751. // Implements ES6 Draft Rev 26 July 18, 2014
  752. //
  753. // Undefined: NaN
  754. // Null: 0
  755. // boolean: v==true ? 1 : 0 ;
  756. // number: v (original number)
  757. // String: conversion by spec algorithm
  758. // object: ToNumber(PrimitiveValue(v, hint_number))
  759. // Symbol: TypeError
  760. //----------------------------------------------------------------------------
  761. double JavascriptConversion::ToNumber_Full(Var aValue,ScriptContext* scriptContext)
  762. {
  763. AssertMsg(!TaggedInt::Is(aValue), "Should be detected");
  764. ScriptContext * objectScriptContext = VarIs<RecyclableObject>(aValue) ? UnsafeVarTo<RecyclableObject>(aValue)->GetScriptContext() : nullptr;
  765. BOOL fPrimitiveOnly = false;
  766. while(true)
  767. {
  768. switch (JavascriptOperators::GetTypeId(aValue))
  769. {
  770. case TypeIds_Symbol:
  771. JavascriptError::TryThrowTypeError(objectScriptContext, scriptContext, JSERR_NeedNumber);
  772. // Fallthrough to return NaN if exceptions are disabled
  773. case TypeIds_Undefined:
  774. return JavascriptNumber::GetValue(scriptContext->GetLibrary()->GetNaN());
  775. case TypeIds_Null:
  776. return 0;
  777. case TypeIds_Integer:
  778. return TaggedInt::ToDouble(aValue);
  779. case TypeIds_Boolean:
  780. return UnsafeVarTo<JavascriptBoolean>(aValue)->GetValue() ? 1 : +0;
  781. case TypeIds_Number:
  782. return JavascriptNumber::GetValue(aValue);
  783. case TypeIds_Int64Number:
  784. return (double)UnsafeVarTo<JavascriptInt64Number>(aValue)->GetValue();
  785. case TypeIds_UInt64Number:
  786. return (double)UnsafeVarTo<JavascriptUInt64Number>(aValue)->GetValue();
  787. case TypeIds_String:
  788. return UnsafeVarTo<JavascriptString>(aValue)->ToDouble();
  789. default:
  790. {
  791. AssertMsg(JavascriptOperators::IsObject(aValue), "bad type object in conversion ToInteger");
  792. if(fPrimitiveOnly)
  793. {
  794. JavascriptError::ThrowError(scriptContext, VBSERR_OLENoPropOrMethod);
  795. }
  796. fPrimitiveOnly = true;
  797. aValue = ToPrimitive<JavascriptHint::HintNumber>(aValue, scriptContext);
  798. }
  799. }
  800. }
  801. }
  802. //----------------------------------------------------------------------------
  803. // second part of the ToInteger() implementation.(ES5.0: S9.4).
  804. //----------------------------------------------------------------------------
  805. double JavascriptConversion::ToInteger_Full(Var aValue,ScriptContext* scriptContext)
  806. {
  807. AssertMsg(!TaggedInt::Is(aValue), "Should be detected");
  808. ScriptContext * objectScriptContext = VarIs<RecyclableObject>(aValue) ? UnsafeVarTo<RecyclableObject>(aValue)->GetScriptContext() : nullptr;
  809. BOOL fPrimitiveOnly = false;
  810. while(true)
  811. {
  812. switch (JavascriptOperators::GetTypeId(aValue))
  813. {
  814. case TypeIds_Symbol:
  815. JavascriptError::TryThrowTypeError(objectScriptContext, scriptContext, JSERR_NeedNumber);
  816. // Fallthrough to return 0 if exceptions are disabled
  817. case TypeIds_Undefined:
  818. case TypeIds_Null:
  819. return 0;
  820. case TypeIds_Integer:
  821. return TaggedInt::ToInt32(aValue);
  822. case TypeIds_Boolean:
  823. return UnsafeVarTo<JavascriptBoolean>(aValue)->GetValue() ? 1 : +0;
  824. case TypeIds_Number:
  825. return ToInteger(JavascriptNumber::GetValue(aValue));
  826. case TypeIds_Int64Number:
  827. return ToInteger((double)UnsafeVarTo<JavascriptInt64Number>(aValue)->GetValue());
  828. case TypeIds_UInt64Number:
  829. return ToInteger((double)UnsafeVarTo<JavascriptUInt64Number>(aValue)->GetValue());
  830. case TypeIds_String:
  831. return ToInteger(UnsafeVarTo<JavascriptString>(aValue)->ToDouble());
  832. default:
  833. {
  834. AssertMsg(JavascriptOperators::IsObject(aValue), "bad type object in conversion ToInteger");
  835. if(fPrimitiveOnly)
  836. {
  837. AssertMsg(FALSE, "wrong call in ToInteger_Full, no dynamic objects should get here");
  838. JavascriptError::ThrowError(scriptContext, VBSERR_OLENoPropOrMethod);
  839. }
  840. fPrimitiveOnly = true;
  841. aValue = ToPrimitive<JavascriptHint::HintNumber>(aValue, scriptContext);
  842. }
  843. }
  844. }
  845. }
  846. double JavascriptConversion::ToInteger(double val)
  847. {
  848. if(JavascriptNumber::IsNan(val))
  849. return 0;
  850. if(JavascriptNumber::IsPosInf(val) || JavascriptNumber::IsNegInf(val) ||
  851. JavascriptNumber::IsZero(val))
  852. {
  853. return val;
  854. }
  855. return ( ((val < 0) ? -1 : 1 ) * floor(fabs(val)));
  856. }
  857. //----------------------------------------------------------------------------
  858. // ToInt32() converts the given Var to an Int32 value, as described in
  859. // (ES3.0: S9.5).
  860. //----------------------------------------------------------------------------
  861. int32 JavascriptConversion::ToInt32_Full(Var aValue, ScriptContext* scriptContext)
  862. {
  863. JIT_HELPER_REENTRANT_HEADER(Conv_ToInt32_Full);
  864. Assert(Js::JavascriptStackWalker::ValidateTopJitFrame(scriptContext));
  865. AssertMsg(!TaggedInt::Is(aValue), "Should be detected");
  866. ScriptContext * objectScriptContext = VarIs<RecyclableObject>(aValue) ? UnsafeVarTo<RecyclableObject>(aValue)->GetScriptContext() : nullptr;
  867. // This is used when TaggedInt's overflow but remain under int32
  868. // so Number is our most critical case:
  869. TypeId typeId = JavascriptOperators::GetTypeId(aValue);
  870. if (typeId == TypeIds_Number)
  871. {
  872. return JavascriptMath::ToInt32Core(JavascriptNumber::GetValue(aValue));
  873. }
  874. switch (typeId)
  875. {
  876. case TypeIds_Symbol:
  877. JavascriptError::TryThrowTypeError(objectScriptContext, scriptContext, JSERR_NeedNumber);
  878. // Fallthrough to return 0 if exceptions are disabled
  879. case TypeIds_Undefined:
  880. case TypeIds_Null:
  881. return 0;
  882. case TypeIds_Integer:
  883. return TaggedInt::ToInt32(aValue);
  884. case TypeIds_Boolean:
  885. return UnsafeVarTo<JavascriptBoolean>(aValue)->GetValue() ? 1 : +0;
  886. case TypeIds_Int64Number:
  887. // we won't lose precision if the int64 is within 32bit boundary; otherwise we need to
  888. // treat it as double anyhow.
  889. return JavascriptMath::ToInt32Core((double)UnsafeVarTo<JavascriptInt64Number>(aValue)->GetValue());
  890. case TypeIds_UInt64Number:
  891. // we won't lose precision if the int64 is within 32bit boundary; otherwise we need to
  892. // treat it as double anyhow.
  893. return JavascriptMath::ToInt32Core((double)UnsafeVarTo<JavascriptUInt64Number>(aValue)->GetValue());
  894. case TypeIds_String:
  895. {
  896. double result;
  897. if (UnsafeVarTo<JavascriptString>(aValue)->ToDouble(&result))
  898. {
  899. return JavascriptMath::ToInt32Core(result);
  900. }
  901. // If the string isn't a valid number, ToDouble returns NaN, and ToInt32 of that is 0
  902. return 0;
  903. }
  904. default:
  905. AssertMsg(JavascriptOperators::IsObject(aValue), "bad type object in conversion ToInteger32");
  906. aValue = ToPrimitive<JavascriptHint::HintNumber>(aValue, scriptContext);
  907. }
  908. switch (JavascriptOperators::GetTypeId(aValue))
  909. {
  910. case TypeIds_Symbol:
  911. JavascriptError::TryThrowTypeError(objectScriptContext, scriptContext, JSERR_NeedNumber);
  912. // Fallthrough to return 0 if exceptions are disabled
  913. case TypeIds_Undefined:
  914. case TypeIds_Null:
  915. return 0;
  916. case TypeIds_Integer:
  917. return TaggedInt::ToInt32(aValue);
  918. case TypeIds_Boolean:
  919. return UnsafeVarTo<JavascriptBoolean>(aValue)->GetValue() ? 1 : +0;
  920. case TypeIds_Number:
  921. return ToInt32(JavascriptNumber::GetValue(aValue));
  922. case TypeIds_Int64Number:
  923. // we won't lose precision if the int64 is within 32bit boundary; otherwise we need to
  924. // treat it as double anyhow.
  925. return JavascriptMath::ToInt32Core((double)UnsafeVarTo<JavascriptInt64Number>(aValue)->GetValue());
  926. case TypeIds_UInt64Number:
  927. // we won't lose precision if the int64 is within 32bit boundary; otherwise we need to
  928. // treat it as double anyhow.
  929. return JavascriptMath::ToInt32Core((double)UnsafeVarTo<JavascriptUInt64Number>(aValue)->GetValue());
  930. case TypeIds_String:
  931. {
  932. double result;
  933. if (UnsafeVarTo<JavascriptString>(aValue)->ToDouble(&result))
  934. {
  935. return ToInt32(result);
  936. }
  937. // If the string isn't a valid number, ToDouble returns NaN, and ToInt32 of that is 0
  938. return 0;
  939. }
  940. default:
  941. AssertMsg(FALSE, "wrong call in ToInteger32_Full, no dynamic objects should get here.");
  942. JavascriptError::ThrowError(scriptContext, VBSERR_OLENoPropOrMethod);
  943. }
  944. JIT_HELPER_END(Conv_ToInt32_Full);
  945. }
  946. // a strict version of ToInt32 conversion that returns false for non int32 values like, inf, NaN, undef
  947. BOOL JavascriptConversion::ToInt32Finite(Var aValue, ScriptContext* scriptContext, int32* result)
  948. {
  949. ScriptContext * objectScriptContext = VarIs<RecyclableObject>(aValue) ? UnsafeVarTo<RecyclableObject>(aValue)->GetScriptContext() : nullptr;
  950. BOOL fPrimitiveOnly = false;
  951. while(true)
  952. {
  953. switch (JavascriptOperators::GetTypeId(aValue))
  954. {
  955. case TypeIds_Symbol:
  956. JavascriptError::TryThrowTypeError(objectScriptContext, scriptContext, JSERR_NeedNumber);
  957. // Fallthrough to return false and set result to 0 if exceptions are disabled
  958. case TypeIds_Undefined:
  959. *result = 0;
  960. return false;
  961. case TypeIds_Null:
  962. *result = 0;
  963. return true;
  964. case TypeIds_Integer:
  965. *result = TaggedInt::ToInt32(aValue);
  966. return true;
  967. case TypeIds_Boolean:
  968. *result = UnsafeVarTo<JavascriptBoolean>(aValue)->GetValue() ? 1 : +0;
  969. return true;
  970. case TypeIds_Number:
  971. return ToInt32Finite(JavascriptNumber::GetValue(aValue), result);
  972. case TypeIds_Int64Number:
  973. // we won't lose precision if the int64 is within 32bit boundary; otherwise we need to
  974. // treat it as double anyhow.
  975. return ToInt32Finite((double)UnsafeVarTo<JavascriptInt64Number>(aValue)->GetValue(), result);
  976. case TypeIds_UInt64Number:
  977. // we won't lose precision if the int64 is within 32bit boundary; otherwise we need to
  978. // treat it as double anyhow.
  979. return ToInt32Finite((double)UnsafeVarTo<JavascriptUInt64Number>(aValue)->GetValue(), result);
  980. case TypeIds_String:
  981. return ToInt32Finite(UnsafeVarTo<JavascriptString>(aValue)->ToDouble(), result);
  982. default:
  983. {
  984. AssertMsg(JavascriptOperators::IsObject(aValue), "bad type object in conversion ToInteger32");
  985. if(fPrimitiveOnly)
  986. {
  987. AssertMsg(FALSE, "wrong call in ToInteger32_Full, no dynamic objects should get here");
  988. JavascriptError::ThrowError(scriptContext, VBSERR_OLENoPropOrMethod);
  989. }
  990. fPrimitiveOnly = true;
  991. aValue = ToPrimitive<JavascriptHint::HintNumber>(aValue, scriptContext);
  992. }
  993. }
  994. }
  995. }
  996. int32 JavascriptConversion::ToInt32(double T1)
  997. {
  998. return JavascriptMath::ToInt32Core(T1);
  999. }
  1000. __int64 JavascriptConversion::ToInt64(Var aValue, ScriptContext* scriptContext)
  1001. {
  1002. switch (JavascriptOperators::GetTypeId(aValue))
  1003. {
  1004. case TypeIds_Integer:
  1005. {
  1006. return TaggedInt::ToInt32(aValue);
  1007. }
  1008. case TypeIds_Int64Number:
  1009. {
  1010. JavascriptInt64Number* int64Number = UnsafeVarTo<JavascriptInt64Number>(aValue);
  1011. return int64Number->GetValue();
  1012. }
  1013. case TypeIds_UInt64Number:
  1014. {
  1015. JavascriptUInt64Number* uint64Number = UnsafeVarTo<JavascriptUInt64Number>(aValue);
  1016. return (__int64)uint64Number->GetValue();
  1017. }
  1018. case TypeIds_Number:
  1019. return JavascriptMath::TryToInt64(JavascriptNumber::GetValue(aValue));
  1020. default:
  1021. return (unsigned __int64)JavascriptConversion::ToInt32_Full(aValue, scriptContext);
  1022. }
  1023. }
  1024. unsigned __int64 JavascriptConversion::ToUInt64(Var aValue, ScriptContext* scriptContext)
  1025. {
  1026. switch (JavascriptOperators::GetTypeId(aValue))
  1027. {
  1028. case TypeIds_Integer:
  1029. {
  1030. return (unsigned __int64)TaggedInt::ToInt32(aValue);
  1031. }
  1032. case TypeIds_Int64Number:
  1033. {
  1034. JavascriptInt64Number* int64Number = UnsafeVarTo<JavascriptInt64Number>(aValue);
  1035. return (unsigned __int64)int64Number->GetValue();
  1036. }
  1037. case TypeIds_UInt64Number:
  1038. {
  1039. JavascriptUInt64Number* uint64Number = UnsafeVarTo<JavascriptUInt64Number>(aValue);
  1040. return uint64Number->GetValue();
  1041. }
  1042. case TypeIds_Number:
  1043. return static_cast<unsigned __int64>(JavascriptMath::TryToInt64(JavascriptNumber::GetValue(aValue)));
  1044. default:
  1045. return (unsigned __int64)JavascriptConversion::ToInt32_Full(aValue, scriptContext);
  1046. }
  1047. }
  1048. BOOL JavascriptConversion::ToInt32Finite(double value, int32* result)
  1049. {
  1050. if((!NumberUtilities::IsFinite(value)) || JavascriptNumber::IsNan(value))
  1051. {
  1052. *result = 0;
  1053. return false;
  1054. }
  1055. else
  1056. {
  1057. *result = JavascriptMath::ToInt32Core(value);
  1058. return true;
  1059. }
  1060. }
  1061. //----------------------------------------------------------------------------
  1062. // (ES3.0: S9.6).
  1063. //----------------------------------------------------------------------------
  1064. uint32 JavascriptConversion::ToUInt32_Full(Var aValue, ScriptContext* scriptContext)
  1065. {
  1066. JIT_HELPER_REENTRANT_HEADER(Conv_ToUInt32_Full);
  1067. AssertMsg(!TaggedInt::Is(aValue), "Should be detected");
  1068. ScriptContext * objectScriptContext = VarIs<RecyclableObject>(aValue) ? UnsafeVarTo<RecyclableObject>(aValue)->GetScriptContext() : nullptr;
  1069. BOOL fPrimitiveOnly = false;
  1070. while(true)
  1071. {
  1072. switch (JavascriptOperators::GetTypeId(aValue))
  1073. {
  1074. case TypeIds_Symbol:
  1075. JavascriptError::TryThrowTypeError(objectScriptContext, scriptContext, JSERR_NeedNumber);
  1076. // Fallthrough to return 0 if exceptions are disabled
  1077. case TypeIds_Undefined:
  1078. case TypeIds_Null:
  1079. return 0;
  1080. case TypeIds_Integer:
  1081. return TaggedInt::ToUInt32(aValue);
  1082. case TypeIds_Boolean:
  1083. return UnsafeVarTo<JavascriptBoolean>(aValue)->GetValue() ? 1 : +0;
  1084. case TypeIds_Number:
  1085. return JavascriptMath::ToUInt32(JavascriptNumber::GetValue(aValue));
  1086. case TypeIds_Int64Number:
  1087. // we won't lose precision if the int64 is within 32bit boundary; otherwise we need to
  1088. // treat it as double anyhow.
  1089. return JavascriptMath::ToUInt32((double)UnsafeVarTo<JavascriptInt64Number>(aValue)->GetValue());
  1090. case TypeIds_UInt64Number:
  1091. // we won't lose precision if the int64 is within 32bit boundary; otherwise we need to
  1092. // treat it as double anyhow.
  1093. return JavascriptMath::ToUInt32((double)UnsafeVarTo<JavascriptUInt64Number>(aValue)->GetValue());
  1094. case TypeIds_String:
  1095. {
  1096. double result;
  1097. if (UnsafeVarTo<JavascriptString>(aValue)->ToDouble(&result))
  1098. {
  1099. return JavascriptMath::ToUInt32(result);
  1100. }
  1101. // If the string isn't a valid number, ToDouble returns NaN, and ToUInt32 of that is 0
  1102. return 0;
  1103. }
  1104. default:
  1105. {
  1106. AssertMsg(JavascriptOperators::IsObject(aValue), "bad type object in conversion ToUInt32");
  1107. if(fPrimitiveOnly)
  1108. {
  1109. AssertMsg(FALSE, "wrong call in ToUInt32_Full, no dynamic objects should get here");
  1110. JavascriptError::ThrowError(scriptContext, VBSERR_OLENoPropOrMethod);
  1111. }
  1112. fPrimitiveOnly = true;
  1113. aValue = ToPrimitive<JavascriptHint::HintNumber>(aValue, scriptContext);
  1114. }
  1115. }
  1116. }
  1117. JIT_HELPER_END(Conv_ToUInt32_Full);
  1118. }
  1119. // Unable to put JIT_HELPER macro in .inl file, do instantiation here
  1120. JIT_HELPER_TEMPLATE(Conv_ToUInt32, Conv_ToUInt32)
  1121. JIT_HELPER_TEMPLATE(Conv_ToBoolean, Conv_ToBoolean)
  1122. uint32 JavascriptConversion::ToUInt32(double T1)
  1123. {
  1124. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(Conv_ToUInt32Core);
  1125. JIT_HELPER_SAME_ATTRIBUTES(Conv_ToInt32Core, Conv_ToUInt32Core);
  1126. // Same as doing ToInt32 and reinterpret the bits as uint32
  1127. return (uint32)JavascriptMath::ToInt32Core(T1);
  1128. JIT_HELPER_END(Conv_ToUInt32Core);
  1129. }
  1130. //----------------------------------------------------------------------------
  1131. // ToUInt16() converts the given Var to a UInt16 value, as described in
  1132. // (ES3.0: S9.6).
  1133. //----------------------------------------------------------------------------
  1134. uint16 JavascriptConversion::ToUInt16_Full(IN Var aValue, ScriptContext* scriptContext)
  1135. {
  1136. AssertMsg(!TaggedInt::Is(aValue), "Should be detected");
  1137. ScriptContext * objectScriptContext = VarIs<RecyclableObject>(aValue) ? UnsafeVarTo<RecyclableObject>(aValue)->GetScriptContext() : nullptr;
  1138. BOOL fPrimitiveOnly = false;
  1139. while(true)
  1140. {
  1141. switch (JavascriptOperators::GetTypeId(aValue))
  1142. {
  1143. case TypeIds_Symbol:
  1144. JavascriptError::TryThrowTypeError(objectScriptContext, scriptContext, JSERR_NeedNumber);
  1145. // Fallthrough to return 0 if exceptions are disabled
  1146. case TypeIds_Undefined:
  1147. case TypeIds_Null:
  1148. return 0;
  1149. case TypeIds_Integer:
  1150. return TaggedInt::ToUInt16(aValue);
  1151. case TypeIds_Boolean:
  1152. return UnsafeVarTo<JavascriptBoolean>(aValue)->GetValue() ? 1 : +0;
  1153. case TypeIds_Number:
  1154. return ToUInt16(JavascriptNumber::GetValue(aValue));
  1155. case TypeIds_Int64Number:
  1156. // we won't lose precision if the int64 is within 16bit boundary; otherwise we need to
  1157. // treat it as double anyhow.
  1158. return ToUInt16((double)UnsafeVarTo<JavascriptInt64Number>(aValue)->GetValue());
  1159. case TypeIds_UInt64Number:
  1160. // we won't lose precision if the int64 is within 16bit boundary; otherwise we need to
  1161. // treat it as double anyhow.
  1162. return ToUInt16((double)UnsafeVarTo<JavascriptUInt64Number>(aValue)->GetValue());
  1163. case TypeIds_String:
  1164. {
  1165. double result;
  1166. if (UnsafeVarTo<JavascriptString>(aValue)->ToDouble(&result))
  1167. {
  1168. return ToUInt16(result);
  1169. }
  1170. // If the string isn't a valid number, ToDouble is NaN, and ToUInt16 of that is 0
  1171. return 0;
  1172. }
  1173. default:
  1174. {
  1175. AssertMsg(JavascriptOperators::IsObject(aValue), "bad type object in conversion ToUIn16");
  1176. if(fPrimitiveOnly)
  1177. {
  1178. AssertMsg(FALSE, "wrong call in ToUInt16, no dynamic objects should get here");
  1179. JavascriptError::ThrowError(scriptContext, VBSERR_OLENoPropOrMethod);
  1180. }
  1181. fPrimitiveOnly = true;
  1182. aValue = ToPrimitive<JavascriptHint::HintNumber>(aValue, scriptContext);
  1183. }
  1184. }
  1185. }
  1186. }
  1187. uint16 JavascriptConversion::ToUInt16(double T1)
  1188. {
  1189. //
  1190. // VC does the right thing here, if we first convert to uint32 and then to uint16
  1191. // Spec says mod should be done.
  1192. //
  1193. uint32 result = JavascriptConversion::ToUInt32(T1);
  1194. #if defined(_M_IX86) && _MSC_FULL_VER < 160030329
  1195. // Well VC doesn't actually do the right thing... It takes (uint16)(uint32)double and removes the
  1196. // middle uint32 cast to (uint16)double, which isn't the same thing. Somehow, it only seems to be a
  1197. // problem for x86. Forcing a store to uint32 prevents the incorrect optimization.
  1198. //
  1199. // A bug has been filled in the Dev11 database: TF bug id #901495
  1200. // Fixed in compiler 16.00.30329.00
  1201. volatile uint32 volResult = result;
  1202. #endif
  1203. return (uint16) result;
  1204. }
  1205. int16 JavascriptConversion::ToInt16(double aValue)
  1206. {
  1207. return (int16)ToInt32(aValue);
  1208. }
  1209. int8 JavascriptConversion::ToInt8(double aValue)
  1210. {
  1211. return (int8)ToInt32(aValue);
  1212. }
  1213. uint8 JavascriptConversion::ToUInt8(double aValue)
  1214. {
  1215. return (uint8)ToUInt32(aValue);
  1216. }
  1217. JavascriptString * JavascriptConversion::ToPrimitiveString(Var aValue, ScriptContext * scriptContext)
  1218. {
  1219. JIT_HELPER_REENTRANT_HEADER(Op_ConvPrimitiveString);
  1220. return ToString(ToPrimitive<JavascriptHint::None>(aValue, scriptContext), scriptContext);
  1221. JIT_HELPER_END(Op_ConvPrimitiveString);
  1222. }
  1223. double JavascriptConversion::LongToDouble(__int64 aValue)
  1224. {
  1225. return static_cast<double>(aValue);
  1226. }
  1227. // Windows x64 version implemented in masm to work around precision limitation
  1228. #if !defined(_WIN32 ) || !defined(_M_X64)
  1229. double JavascriptConversion::ULongToDouble(unsigned __int64 aValue)
  1230. {
  1231. return static_cast<double>(aValue);
  1232. }
  1233. #endif
  1234. float JavascriptConversion::LongToFloat(__int64 aValue)
  1235. {
  1236. return static_cast<float>(aValue);
  1237. }
  1238. float JavascriptConversion::ULongToFloat (unsigned __int64 aValue)
  1239. {
  1240. return static_cast<float>(aValue);
  1241. }
  1242. int64 JavascriptConversion::ToLength(Var aValue, ScriptContext* scriptContext)
  1243. {
  1244. if (TaggedInt::Is(aValue))
  1245. {
  1246. int64 length = TaggedInt::ToInt64(aValue);
  1247. return (length < 0) ? 0 : length;
  1248. }
  1249. double length = JavascriptConversion::ToInteger(aValue, scriptContext);
  1250. if (length < 0.0 || JavascriptNumber::IsNegZero(length))
  1251. {
  1252. length = 0.0;
  1253. }
  1254. else if (length > Math::MAX_SAFE_INTEGER)
  1255. {
  1256. length = Math::MAX_SAFE_INTEGER;
  1257. }
  1258. return NumberUtilities::TryToInt64(length);
  1259. }
  1260. JavascriptBigInt *JavascriptConversion::ToBigInt(Var aValue, ScriptContext* scriptContext)
  1261. {
  1262. Assert(scriptContext->GetThreadContext()->IsScriptActive());
  1263. switch (JavascriptOperators::GetTypeId(aValue))
  1264. {
  1265. case TypeIds_BigInt:
  1266. break;
  1267. default:
  1268. AssertOrFailFastMsg(false, "do not support conversion of other types in ToBigInt");
  1269. }
  1270. return UnsafeVarTo<JavascriptBigInt>(aValue);
  1271. }