JavascriptString.cpp 139 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799
  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 "RuntimeLibraryPch.h"
  6. #include "DataStructures/BigInt.h"
  7. #include "Library/EngineInterfaceObject.h"
  8. #include "Library/IntlEngineInterfaceExtensionObject.h"
  9. namespace Js
  10. {
  11. // White Space characters are defined in ES6 Section 11.2
  12. // There are 26 white space characters we need to correctly class:
  13. //0x0009
  14. //0x000a
  15. //0x000b
  16. //0x000c
  17. //0x000d
  18. //0x0020
  19. //0x00a0
  20. //0x1680
  21. //0x180e
  22. //0x2000
  23. //0x2001
  24. //0x2002
  25. //0x2003
  26. //0x2004
  27. //0x2005
  28. //0x2006
  29. //0x2007
  30. //0x2008
  31. //0x2009
  32. //0x200a
  33. //0x2028
  34. //0x2029
  35. //0x202f
  36. //0x205f
  37. //0x3000
  38. //0xfeff
  39. bool IsWhiteSpaceCharacter(char16 ch)
  40. {
  41. return ch >= 0x9 &&
  42. (ch <= 0xd ||
  43. (ch <= 0x200a &&
  44. (ch >= 0x2000 || ch == 0x20 || ch == 0xa0 || ch == 0x1680 || ch == 0x180e)
  45. ) ||
  46. (ch >= 0x2028 &&
  47. (ch <= 0x2029 || ch == 0x202f || ch == 0x205f || ch == 0x3000 || ch == 0xfeff)
  48. )
  49. );
  50. }
  51. template <typename T, bool copyBuffer>
  52. JavascriptString* JavascriptString::NewWithBufferT(const char16 * content, charcount_t cchUseLength, ScriptContext * scriptContext)
  53. {
  54. AssertMsg(content != nullptr, "NULL value passed to JavascriptString::New");
  55. AssertMsg(IsValidCharCount(cchUseLength), "String length will overflow an int");
  56. switch (cchUseLength)
  57. {
  58. case 0:
  59. return scriptContext->GetLibrary()->GetEmptyString();
  60. case 1:
  61. return scriptContext->GetLibrary()->GetCharStringCache().GetStringForChar(*content);
  62. default:
  63. break;
  64. }
  65. Recycler* recycler = scriptContext->GetRecycler();
  66. StaticType * stringTypeStatic = scriptContext->GetLibrary()->GetStringTypeStatic();
  67. char16 const * buffer = content;
  68. charcount_t cchUseBoundLength = static_cast<charcount_t>(cchUseLength);
  69. if (copyBuffer)
  70. {
  71. buffer = JavascriptString::AllocateLeafAndCopySz(recycler, content, cchUseBoundLength);
  72. }
  73. return T::New(stringTypeStatic, buffer, cchUseBoundLength, recycler);
  74. }
  75. JavascriptString* JavascriptString::NewWithSz(__in_z const char16 * content, ScriptContext * scriptContext)
  76. {
  77. AssertMsg(content != nullptr, "NULL value passed to JavascriptString::New");
  78. return NewWithBuffer(content, GetBufferLength(content), scriptContext);
  79. }
  80. JavascriptString* JavascriptString::NewWithArenaSz(__in_z const char16 * content, ScriptContext * scriptContext)
  81. {
  82. AssertMsg(content != nullptr, "NULL value passed to JavascriptString::New");
  83. return NewWithArenaBuffer(content, GetBufferLength(content), scriptContext);
  84. }
  85. JavascriptString* JavascriptString::NewWithBuffer(__in_ecount(cchUseLength) const char16 * content, charcount_t cchUseLength, ScriptContext * scriptContext)
  86. {
  87. return NewWithBufferT<LiteralString, false>(content, cchUseLength, scriptContext);
  88. }
  89. JavascriptString* JavascriptString::NewWithArenaBuffer(__in_ecount(cchUseLength) const char16* content, charcount_t cchUseLength, ScriptContext* scriptContext)
  90. {
  91. return NewWithBufferT<ArenaLiteralString, false>(content, cchUseLength, scriptContext);
  92. }
  93. JavascriptString* JavascriptString::NewCopySz(__in_z const char16* content, ScriptContext* scriptContext)
  94. {
  95. return NewCopyBuffer(content, GetBufferLength(content), scriptContext);
  96. }
  97. JavascriptString* JavascriptString::NewCopyBuffer(__in_ecount(cchUseLength) const char16* content, charcount_t cchUseLength, ScriptContext* scriptContext)
  98. {
  99. return NewWithBufferT<LiteralString, true>(content, cchUseLength, scriptContext);
  100. }
  101. JavascriptString* JavascriptString::NewCopySzFromArena(__in_z const char16* content, ScriptContext* scriptContext, ArenaAllocator *arena)
  102. {
  103. AssertMsg(content != nullptr, "NULL value passed to JavascriptString::New");
  104. charcount_t cchUseLength = JavascriptString::GetBufferLength(content);
  105. char16* buffer = JavascriptString::AllocateAndCopySz(arena, content, cchUseLength);
  106. return ArenaLiteralString::New(scriptContext->GetLibrary()->GetStringTypeStatic(),
  107. buffer, cchUseLength, arena);
  108. }
  109. Var JavascriptString::NewInstance(RecyclableObject* function, CallInfo callInfo, ...)
  110. {
  111. PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
  112. ARGUMENTS(args, callInfo);
  113. ScriptContext* scriptContext = function->GetScriptContext();
  114. AssertMsg(args.Info.Count > 0, "Negative argument count");
  115. // SkipDefaultNewObject function flag should have prevented the default object from
  116. // being created, except when call true a host dispatch.
  117. Var newTarget = callInfo.Flags & CallFlags_NewTarget ? args.Values[args.Info.Count] : args[0];
  118. bool isCtorSuperCall = (callInfo.Flags & CallFlags_New) && newTarget != nullptr && RecyclableObject::Is(newTarget);
  119. Assert(isCtorSuperCall || !(callInfo.Flags & CallFlags_New) || args[0] == nullptr
  120. || JavascriptOperators::GetTypeId(args[0]) == TypeIds_HostDispatch);
  121. JavascriptString* str;
  122. Var result;
  123. if (args.Info.Count > 1)
  124. {
  125. if (JavascriptSymbol::Is(args[1]) && !(callInfo.Flags & CallFlags_New))
  126. {
  127. // By ES2015 21.1.1.1 step 2, calling the String constructor directly results in an explicit ToString, which does not throw.
  128. return JavascriptSymbol::ToString(JavascriptSymbol::FromVar(args[1])->GetValue(), scriptContext);
  129. // Calling with new is an implicit ToString on the Symbol, resulting in a throw. For this case we can let JavascriptConversion handle the call.
  130. }
  131. str = JavascriptConversion::ToString(args[1], scriptContext);
  132. }
  133. else
  134. {
  135. str = scriptContext->GetLibrary()->GetEmptyString();
  136. }
  137. if (callInfo.Flags & CallFlags_New)
  138. {
  139. result = scriptContext->GetLibrary()->CreateStringObject(str);
  140. }
  141. else
  142. {
  143. result = str;
  144. }
  145. return isCtorSuperCall ?
  146. JavascriptOperators::OrdinaryCreateFromConstructor(RecyclableObject::FromVar(newTarget), RecyclableObject::FromVar(result), nullptr, scriptContext) :
  147. result;
  148. }
  149. // static
  150. bool IsValidCharCount(size_t charCount)
  151. {
  152. return charCount <= JavascriptString::MaxCharLength;
  153. }
  154. JavascriptString::JavascriptString(StaticType * type)
  155. : RecyclableObject(type), m_charLength(0), m_pszValue(0)
  156. {
  157. Assert(type->GetTypeId() == TypeIds_String);
  158. }
  159. JavascriptString::JavascriptString(StaticType * type, charcount_t charLength, const char16* szValue)
  160. : RecyclableObject(type), m_charLength(charLength), m_pszValue(szValue)
  161. {
  162. Assert(type->GetTypeId() == TypeIds_String);
  163. AssertMsg(IsValidCharCount(charLength), "String length is out of range");
  164. }
  165. _Ret_range_(m_charLength, m_charLength)
  166. charcount_t JavascriptString::GetLength() const
  167. {
  168. return m_charLength;
  169. }
  170. int JavascriptString::GetLengthAsSignedInt() const
  171. {
  172. Assert(IsValidCharCount(m_charLength));
  173. return static_cast<int>(m_charLength);
  174. }
  175. const char16* JavascriptString::UnsafeGetBuffer() const
  176. {
  177. return m_pszValue;
  178. }
  179. void JavascriptString::SetLength(charcount_t newLength)
  180. {
  181. if (!IsValidCharCount(newLength))
  182. {
  183. JavascriptExceptionOperators::ThrowOutOfMemory(this->GetScriptContext());
  184. }
  185. m_charLength = newLength;
  186. }
  187. void JavascriptString::SetBuffer(const char16* buffer)
  188. {
  189. m_pszValue = buffer;
  190. }
  191. bool JavascriptString::IsValidIndexValue(charcount_t idx) const
  192. {
  193. return IsValidCharCount(idx) && idx < GetLength();
  194. }
  195. bool JavascriptString::Is(Var aValue)
  196. {
  197. return JavascriptOperators::GetTypeId(aValue) == TypeIds_String;
  198. }
  199. JavascriptString* JavascriptString::FromVar(Var aValue)
  200. {
  201. AssertMsg(Is(aValue), "Ensure var is actually a 'JavascriptString'");
  202. return static_cast<JavascriptString *>(RecyclableObject::FromVar(aValue));
  203. }
  204. charcount_t
  205. JavascriptString::GetBufferLength(const char16 * content)
  206. {
  207. size_t cchActual = wcslen(content);
  208. #if defined(_M_X64_OR_ARM64)
  209. if (!IsValidCharCount(cchActual))
  210. {
  211. // Limit javascript string to 31-bit length
  212. Js::Throw::OutOfMemory();
  213. }
  214. #else
  215. // There shouldn't be enough memory to have UINT_MAX character.
  216. // INT_MAX is the upper bound for 32-bit;
  217. Assert(IsValidCharCount(cchActual));
  218. #endif
  219. return static_cast<charcount_t>(cchActual);
  220. }
  221. charcount_t
  222. JavascriptString::GetBufferLength(
  223. const char16 * content, // Value to examine
  224. int charLengthOrMinusOne) // Optional length, in characters
  225. {
  226. //
  227. // Determine the actual length, in characters, not including a terminating '\0':
  228. // - If a length was not specified (charLength < 0), search for a terminating '\0'.
  229. //
  230. charcount_t cchActual;
  231. if (charLengthOrMinusOne < 0)
  232. {
  233. AssertMsg(charLengthOrMinusOne == -1, "The only negative value allowed is -1");
  234. cchActual = GetBufferLength(content);
  235. }
  236. else
  237. {
  238. cchActual = static_cast<charcount_t>(charLengthOrMinusOne);
  239. }
  240. #ifdef CHECK_STRING
  241. // removed this to accommodate much larger string constant in regex-dna.js
  242. if (cchActual > 64 * 1024)
  243. {
  244. //
  245. // String was probably not '\0' terminated:
  246. // - We need to validate that the string's contents always fit within 1 GB to avoid
  247. // overflow checking on 32-bit when using 'int' for 'byte *' pointer operations.
  248. //
  249. Throw::OutOfMemory(); // TODO: determine argument error
  250. }
  251. #endif
  252. return cchActual;
  253. }
  254. template< size_t N >
  255. Var JavascriptString::StringBracketHelper(Arguments args, ScriptContext *scriptContext, const char16(&tag)[N])
  256. {
  257. CompileAssert(0 < N && N <= JavascriptString::MaxCharLength);
  258. return StringBracketHelper(args, scriptContext, tag, static_cast<charcount_t>(N - 1), nullptr, 0);
  259. }
  260. template< size_t N1, size_t N2 >
  261. Var JavascriptString::StringBracketHelper(Arguments args, ScriptContext *scriptContext, const char16(&tag)[N1], const char16(&prop)[N2])
  262. {
  263. CompileAssert(0 < N1 && N1 <= JavascriptString::MaxCharLength);
  264. CompileAssert(0 < N2 && N2 <= JavascriptString::MaxCharLength);
  265. return StringBracketHelper(args, scriptContext, tag, static_cast<charcount_t>(N1 - 1), prop, static_cast<charcount_t>(N2 - 1));
  266. }
  267. BOOL JavascriptString::BufferEquals(__in_ecount(otherLength) LPCWSTR otherBuffer, __in charcount_t otherLength)
  268. {
  269. return otherLength == this->GetLength() &&
  270. JsUtil::CharacterBuffer<WCHAR>::StaticEquals(this->GetString(), otherBuffer, otherLength);
  271. }
  272. BOOL JavascriptString::HasItemAt(charcount_t index)
  273. {
  274. return IsValidIndexValue(index);
  275. }
  276. BOOL JavascriptString::GetItemAt(charcount_t index, Var* value)
  277. {
  278. if (!IsValidIndexValue(index))
  279. {
  280. return false;
  281. }
  282. char16 character = GetItem(index);
  283. *value = this->GetLibrary()->GetCharStringCache().GetStringForChar(character);
  284. return true;
  285. }
  286. char16 JavascriptString::GetItem(charcount_t index)
  287. {
  288. AssertMsg( IsValidIndexValue(index), "Must specify valid character");
  289. const char16 *str = this->GetString();
  290. return str[index];
  291. }
  292. void JavascriptString::CopyHelper(__out_ecount(countNeeded) char16 *dst, __in_ecount(countNeeded) const char16 * str, charcount_t countNeeded)
  293. {
  294. switch(countNeeded)
  295. {
  296. case 0:
  297. return;
  298. case 1:
  299. dst[0] = str[0];
  300. break;
  301. case 3:
  302. dst[2] = str[2];
  303. goto case_2;
  304. case 5:
  305. dst[4] = str[4];
  306. goto case_4;
  307. case 7:
  308. dst[6] = str[6];
  309. goto case_6;
  310. case 9:
  311. dst[8] = str[8];
  312. goto case_8;
  313. case 10:
  314. *(uint32 *)(dst+8) = *(uint32*)(str+8);
  315. // FALLTHROUGH
  316. case 8:
  317. case_8:
  318. *(uint32 *)(dst+6) = *(uint32*)(str+6);
  319. // FALLTHROUGH
  320. case 6:
  321. case_6:
  322. *(uint32 *)(dst+4) = *(uint32*)(str+4);
  323. // FALLTHROUGH
  324. case 4:
  325. case_4:
  326. *(uint32 *)(dst+2) = *(uint32*)(str+2);
  327. // FALLTHROUGH
  328. case 2:
  329. case_2:
  330. *(uint32 *)(dst) = *(uint32*)str;
  331. break;
  332. default:
  333. js_memcpy_s(dst, sizeof(char16) * countNeeded, str, sizeof(char16) * countNeeded);
  334. }
  335. }
  336. __inline JavascriptString* JavascriptString::ConcatDestructive(JavascriptString* pstRight)
  337. {
  338. Assert(pstRight);
  339. if(!IsFinalized())
  340. {
  341. if(CompoundString::Is(this))
  342. {
  343. return ConcatDestructive_Compound(pstRight);
  344. }
  345. if(VirtualTableInfo<ConcatString>::HasVirtualTable(this))
  346. {
  347. JavascriptString *const s = ConcatDestructive_ConcatToCompound(pstRight);
  348. if(s)
  349. {
  350. return s;
  351. }
  352. }
  353. }
  354. else
  355. {
  356. const CharCount leftLength = GetLength();
  357. const CharCount rightLength = pstRight->GetLength();
  358. if(leftLength == 0 || rightLength == 0)
  359. {
  360. return ConcatDestructive_OneEmpty(pstRight);
  361. }
  362. if(CompoundString::ShouldAppendChars(leftLength) && CompoundString::ShouldAppendChars(rightLength))
  363. {
  364. return ConcatDestructive_CompoundAppendChars(pstRight);
  365. }
  366. }
  367. #ifdef PROFILE_STRINGS
  368. StringProfiler::RecordConcatenation(GetScriptContext(), GetLength(), pstRight->GetLength(), ConcatType_ConcatTree);
  369. #endif
  370. if(PHASE_TRACE_StringConcat)
  371. {
  372. Output::Print(
  373. _u("JavascriptString::ConcatDestructive(\"%.8s%s\") - creating ConcatString\n"),
  374. pstRight->IsFinalized() ? pstRight->GetString() : _u(""),
  375. !pstRight->IsFinalized() || pstRight->GetLength() > 8 ? _u("...") : _u(""));
  376. Output::Flush();
  377. }
  378. return ConcatString::New(this, pstRight);
  379. }
  380. JavascriptString* JavascriptString::ConcatDestructive_Compound(JavascriptString* pstRight)
  381. {
  382. Assert(CompoundString::Is(this));
  383. Assert(pstRight);
  384. #ifdef PROFILE_STRINGS
  385. StringProfiler::RecordConcatenation(GetScriptContext(), GetLength(), pstRight->GetLength(), ConcatType_CompoundString);
  386. #endif
  387. if(PHASE_TRACE_StringConcat)
  388. {
  389. Output::Print(
  390. _u("JavascriptString::ConcatDestructive(\"%.8s%s\") - appending to CompoundString\n"),
  391. pstRight->IsFinalized() ? pstRight->GetString() : _u(""),
  392. !pstRight->IsFinalized() || pstRight->GetLength() > 8 ? _u("...") : _u(""));
  393. Output::Flush();
  394. }
  395. CompoundString *const leftCs = CompoundString::FromVar(this);
  396. leftCs->PrepareForAppend();
  397. leftCs->Append(pstRight);
  398. return this;
  399. }
  400. JavascriptString* JavascriptString::ConcatDestructive_ConcatToCompound(JavascriptString* pstRight)
  401. {
  402. Assert(VirtualTableInfo<ConcatString>::HasVirtualTable(this));
  403. Assert(pstRight);
  404. const ConcatString *const leftConcatString = static_cast<const ConcatString *>(this);
  405. JavascriptString *const leftLeftString = leftConcatString->LeftString();
  406. if(VirtualTableInfo<ConcatString>::HasVirtualTable(leftLeftString))
  407. {
  408. #ifdef PROFILE_STRINGS
  409. StringProfiler::RecordConcatenation(GetScriptContext(), GetLength(), pstRight->GetLength(), ConcatType_CompoundString);
  410. #endif
  411. if(PHASE_TRACE_StringConcat)
  412. {
  413. Output::Print(
  414. _u("JavascriptString::ConcatDestructive(\"%.8s%s\") - converting ConcatString to CompoundString\n"),
  415. pstRight->IsFinalized() ? pstRight->GetString() : _u(""),
  416. !pstRight->IsFinalized() || pstRight->GetLength() > 8 ? _u("...") : _u(""));
  417. Output::Flush();
  418. }
  419. const ConcatString *const leftLeftConcatString = static_cast<const ConcatString *>(leftConcatString->LeftString());
  420. CompoundString *const cs = CompoundString::NewWithPointerCapacity(8, GetLibrary());
  421. cs->Append(leftLeftConcatString->LeftString());
  422. cs->Append(leftLeftConcatString->RightString());
  423. cs->Append(leftConcatString->RightString());
  424. cs->Append(pstRight);
  425. return cs;
  426. }
  427. return nullptr;
  428. }
  429. JavascriptString* JavascriptString::ConcatDestructive_OneEmpty(JavascriptString* pstRight)
  430. {
  431. Assert(pstRight);
  432. Assert(GetLength() == 0 || pstRight->GetLength() == 0);
  433. #ifdef PROFILE_STRINGS
  434. StringProfiler::RecordConcatenation(GetScriptContext(), GetLength(), pstRight->GetLength());
  435. #endif
  436. if(PHASE_TRACE_StringConcat)
  437. {
  438. Output::Print(
  439. _u("JavascriptString::ConcatDestructive(\"%.8s%s\") - one side empty, using other side\n"),
  440. pstRight->IsFinalized() ? pstRight->GetString() : _u(""),
  441. !pstRight->IsFinalized() || pstRight->GetLength() > 8 ? _u("...") : _u(""));
  442. Output::Flush();
  443. }
  444. if(GetLength() == 0)
  445. {
  446. return CompoundString::GetImmutableOrScriptUnreferencedString(pstRight);
  447. }
  448. Assert(CompoundString::GetImmutableOrScriptUnreferencedString(this) == this);
  449. return this;
  450. }
  451. JavascriptString* JavascriptString::ConcatDestructive_CompoundAppendChars(JavascriptString* pstRight)
  452. {
  453. Assert(pstRight);
  454. Assert(
  455. GetLength() != 0 &&
  456. pstRight->GetLength() != 0 &&
  457. (CompoundString::ShouldAppendChars(GetLength()) || CompoundString::ShouldAppendChars(pstRight->GetLength())));
  458. #ifdef PROFILE_STRINGS
  459. StringProfiler::RecordConcatenation(GetScriptContext(), GetLength(), pstRight->GetLength(), ConcatType_CompoundString);
  460. #endif
  461. if(PHASE_TRACE_StringConcat)
  462. {
  463. Output::Print(
  464. _u("JavascriptString::ConcatDestructive(\"%.8s%s\") - creating CompoundString, appending chars\n"),
  465. pstRight->IsFinalized() ? pstRight->GetString() : _u(""),
  466. !pstRight->IsFinalized() || pstRight->GetLength() > 8 ? _u("...") : _u(""));
  467. Output::Flush();
  468. }
  469. CompoundString *const cs = CompoundString::NewWithPointerCapacity(4, GetLibrary());
  470. cs->AppendChars(this);
  471. cs->AppendChars(pstRight);
  472. return cs;
  473. }
  474. __inline JavascriptString* JavascriptString::Concat(JavascriptString* pstLeft, JavascriptString* pstRight)
  475. {
  476. AssertMsg(pstLeft != nullptr, "Must have a valid left string");
  477. AssertMsg(pstRight != nullptr, "Must have a valid right string");
  478. if(!pstLeft->IsFinalized())
  479. {
  480. if(CompoundString::Is(pstLeft))
  481. {
  482. return Concat_Compound(pstLeft, pstRight);
  483. }
  484. if(VirtualTableInfo<ConcatString>::HasVirtualTable(pstLeft))
  485. {
  486. return Concat_ConcatToCompound(pstLeft, pstRight);
  487. }
  488. }
  489. else if(pstLeft->GetLength() == 0 || pstRight->GetLength() == 0)
  490. {
  491. return Concat_OneEmpty(pstLeft, pstRight);
  492. }
  493. if(pstLeft->GetLength() != 1 || pstRight->GetLength() != 1)
  494. {
  495. #ifdef PROFILE_STRINGS
  496. StringProfiler::RecordConcatenation(pstLeft->GetScriptContext(), pstLeft->GetLength(), pstRight->GetLength(), ConcatType_ConcatTree);
  497. #endif
  498. if(PHASE_TRACE_StringConcat)
  499. {
  500. Output::Print(
  501. _u("JavascriptString::Concat(\"%.8s%s\") - creating ConcatString\n"),
  502. pstRight->IsFinalized() ? pstRight->GetString() : _u(""),
  503. !pstRight->IsFinalized() || pstRight->GetLength() > 8 ? _u("...") : _u(""));
  504. Output::Flush();
  505. }
  506. return ConcatString::New(pstLeft, pstRight);
  507. }
  508. return Concat_BothOneChar(pstLeft, pstRight);
  509. }
  510. JavascriptString* JavascriptString::Concat_Compound(JavascriptString * pstLeft, JavascriptString * pstRight)
  511. {
  512. Assert(pstLeft);
  513. Assert(CompoundString::Is(pstLeft));
  514. Assert(pstRight);
  515. #ifdef PROFILE_STRINGS
  516. StringProfiler::RecordConcatenation(pstLeft->GetScriptContext(), pstLeft->GetLength(), pstRight->GetLength(), ConcatType_CompoundString);
  517. #endif
  518. if(PHASE_TRACE_StringConcat)
  519. {
  520. Output::Print(
  521. _u("JavascriptString::Concat(\"%.8s%s\") - cloning CompoundString, appending to clone\n"),
  522. pstRight->IsFinalized() ? pstRight->GetString() : _u(""),
  523. !pstRight->IsFinalized() || pstRight->GetLength() > 8 ? _u("...") : _u(""));
  524. Output::Flush();
  525. }
  526. // This is not a left-dead concat, but we can reuse available space in the left string
  527. // because it may be accessible by script code, append to a clone.
  528. const bool needAppend = pstRight->GetLength() != 0;
  529. CompoundString *const leftCs = CompoundString::FromVar(pstLeft)->Clone(needAppend);
  530. if(needAppend)
  531. {
  532. leftCs->Append(pstRight);
  533. }
  534. return leftCs;
  535. }
  536. JavascriptString* JavascriptString::Concat_ConcatToCompound(JavascriptString * pstLeft, JavascriptString * pstRight)
  537. {
  538. Assert(pstLeft);
  539. Assert(VirtualTableInfo<ConcatString>::HasVirtualTable(pstLeft));
  540. Assert(pstRight);
  541. #ifdef PROFILE_STRINGS
  542. StringProfiler::RecordConcatenation(pstLeft->GetScriptContext(), pstLeft->GetLength(), pstRight->GetLength(), ConcatType_CompoundString);
  543. #endif
  544. if(PHASE_TRACE_StringConcat)
  545. {
  546. Output::Print(
  547. _u("JavascriptString::Concat(\"%.8s%s\") - converting ConcatString to CompoundString\n"),
  548. pstRight->IsFinalized() ? pstRight->GetString() : _u(""),
  549. !pstRight->IsFinalized() || pstRight->GetLength() > 8 ? _u("...") : _u(""));
  550. Output::Flush();
  551. }
  552. const ConcatString *const leftConcatString = static_cast<const ConcatString *>(pstLeft);
  553. CompoundString *const cs = CompoundString::NewWithPointerCapacity(8, pstLeft->GetLibrary());
  554. cs->Append(leftConcatString->LeftString());
  555. cs->Append(leftConcatString->RightString());
  556. cs->Append(pstRight);
  557. return cs;
  558. }
  559. JavascriptString* JavascriptString::Concat_OneEmpty(JavascriptString * pstLeft, JavascriptString * pstRight)
  560. {
  561. Assert(pstLeft);
  562. Assert(pstRight);
  563. Assert(pstLeft->GetLength() == 0 || pstRight->GetLength() == 0);
  564. #ifdef PROFILE_STRINGS
  565. StringProfiler::RecordConcatenation(pstLeft->GetScriptContext(), pstLeft->GetLength(), pstRight->GetLength());
  566. #endif
  567. if(PHASE_TRACE_StringConcat)
  568. {
  569. Output::Print(
  570. _u("JavascriptString::Concat(\"%.8s%s\") - one side empty, using other side\n"),
  571. pstRight->IsFinalized() ? pstRight->GetString() : _u(""),
  572. !pstRight->IsFinalized() || pstRight->GetLength() > 8 ? _u("...") : _u(""));
  573. Output::Flush();
  574. }
  575. if(pstLeft->GetLength() == 0)
  576. {
  577. return CompoundString::GetImmutableOrScriptUnreferencedString(pstRight);
  578. }
  579. Assert(CompoundString::GetImmutableOrScriptUnreferencedString(pstLeft) == pstLeft);
  580. return pstLeft;
  581. }
  582. JavascriptString* JavascriptString::Concat_BothOneChar(JavascriptString * pstLeft, JavascriptString * pstRight)
  583. {
  584. Assert(pstLeft);
  585. Assert(pstLeft->GetLength() == 1);
  586. Assert(pstRight);
  587. Assert(pstRight->GetLength() == 1);
  588. #ifdef PROFILE_STRINGS
  589. StringProfiler::RecordConcatenation(pstLeft->GetScriptContext(), pstLeft->GetLength(), pstRight->GetLength(), ConcatType_BufferString);
  590. #endif
  591. if(PHASE_TRACE_StringConcat)
  592. {
  593. Output::Print(
  594. _u("JavascriptString::Concat(\"%.8s%s\") - both sides length 1, creating BufferStringBuilder::WritableString\n"),
  595. pstRight->IsFinalized() ? pstRight->GetString() : _u(""),
  596. !pstRight->IsFinalized() || pstRight->GetLength() > 8 ? _u("...") : _u(""));
  597. Output::Flush();
  598. }
  599. ScriptContext* scriptContext = pstLeft->GetScriptContext();
  600. BufferStringBuilder builder(2, scriptContext);
  601. char16 * stringBuffer = builder.DangerousGetWritableBuffer();
  602. stringBuffer[0] = *pstLeft->GetString();
  603. stringBuffer[1] = *pstRight->GetString();
  604. return builder.ToString();
  605. }
  606. Var JavascriptString::EntryCharAt(RecyclableObject* function, CallInfo callInfo, ...)
  607. {
  608. PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
  609. ARGUMENTS(args, callInfo);
  610. ScriptContext* scriptContext = function->GetScriptContext();
  611. Assert(!(callInfo.Flags & CallFlags_New));
  612. //
  613. // General algorithm:
  614. // 1. Call CheckObjectCoercible passing the this value as its argument.
  615. // 2. Let S be the result of calling ToString, giving it the this value as its argument.
  616. // 3. Let position be ToInteger(pos).
  617. // 4. Let size be the number of characters in S.
  618. // 5. If position < 0 or position = size, return the empty string.
  619. // 6. Return a string of length 1, containing one character from S, where the first (leftmost) character in S is considered to be at position 0, the next one at position 1, and so on.
  620. // NOTE
  621. // The charAt function is intentionally generic; it does not require that its this value be a String object. Therefore, it can be transferred to other kinds of objects for use as a method.
  622. //
  623. JavascriptString * pThis = nullptr;
  624. GetThisStringArgument(args, scriptContext, _u("String.prototype.charAt"), &pThis);
  625. charcount_t idxPosition = 0;
  626. if (args.Info.Count > 1)
  627. {
  628. idxPosition = ConvertToIndex(args[1], scriptContext);
  629. }
  630. //
  631. // Get the character at the specified position.
  632. //
  633. Var value;
  634. if (pThis->GetItemAt(idxPosition, &value))
  635. {
  636. return value;
  637. }
  638. else
  639. {
  640. return scriptContext->GetLibrary()->GetEmptyString();
  641. }
  642. }
  643. Var JavascriptString::EntryCharCodeAt(RecyclableObject* function, CallInfo callInfo, ...)
  644. {
  645. PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
  646. ARGUMENTS(args, callInfo);
  647. ScriptContext* scriptContext = function->GetScriptContext();
  648. Assert(!(callInfo.Flags & CallFlags_New));
  649. //
  650. // General algorithm:
  651. // 1. Call CheckObjectCoercible passing the this value as its argument.
  652. // 2. Let S be the result of calling ToString, giving it the this value as its argument.
  653. // 3. Let position be ToInteger(pos).
  654. // 4. Let size be the number of characters in S.
  655. // 5. If position < 0 or position = size, return NaN.
  656. // 6. Return a value of Number type, whose value is the code unit value of the character at that position in the string S, where the first (leftmost) character in S is considered to be at position 0, the next one at position 1, and so on.
  657. // NOTE
  658. // The charCodeAt function is intentionally generic; it does not require that its this value be a String object. Therefore it can be transferred to other kinds of objects for use as a method.
  659. //
  660. JavascriptString * pThis = nullptr;
  661. GetThisStringArgument(args, scriptContext, _u("String.prototype.charCodeAt"), &pThis);
  662. charcount_t idxPosition = 0;
  663. if (args.Info.Count > 1)
  664. {
  665. idxPosition = ConvertToIndex(args[1], scriptContext);
  666. }
  667. //
  668. // Get the character at the specified position.
  669. //
  670. charcount_t charLength = pThis->GetLength();
  671. if (idxPosition >= charLength)
  672. {
  673. return scriptContext->GetLibrary()->GetNaN();
  674. }
  675. return TaggedInt::ToVarUnchecked(pThis->GetItem(idxPosition));
  676. }
  677. Var JavascriptString::EntryCodePointAt(RecyclableObject* function, CallInfo callInfo, ...)
  678. {
  679. PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
  680. ARGUMENTS(args, callInfo);
  681. ScriptContext* scriptContext = function->GetScriptContext();
  682. Assert(!(callInfo.Flags & CallFlags_New));
  683. JavascriptString * pThis = nullptr;
  684. GetThisStringArgument(args, scriptContext, _u("String.prototype.codePointAt"), &pThis);
  685. charcount_t idxPosition = 0;
  686. if (args.Info.Count > 1)
  687. {
  688. idxPosition = ConvertToIndex(args[1], scriptContext);
  689. }
  690. charcount_t charLength = pThis->GetLength();
  691. if (idxPosition >= charLength)
  692. {
  693. return scriptContext->GetLibrary()->GetUndefined();
  694. }
  695. // A surrogate pair consists of two characters, a lower part and a higher part.
  696. // Lower part is in range [0xD800 - 0xDBFF], while the higher is [0xDC00 - 0xDFFF].
  697. char16 first = pThis->GetItem(idxPosition);
  698. if (first >= 0xD800u && first < 0xDC00u && (uint)(idxPosition + 1) < pThis->GetLength())
  699. {
  700. char16 second = pThis->GetItem(idxPosition + 1);
  701. if (second >= 0xDC00 && second < 0xE000)
  702. {
  703. return TaggedInt::ToVarUnchecked(NumberUtilities::SurrogatePairAsCodePoint(first, second));
  704. }
  705. }
  706. return TaggedInt::ToVarUnchecked(first);
  707. }
  708. Var JavascriptString::EntryConcat(RecyclableObject* function, CallInfo callInfo, ...)
  709. {
  710. PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
  711. ARGUMENTS(args, callInfo);
  712. ScriptContext* scriptContext = function->GetScriptContext();
  713. Assert(!(callInfo.Flags & CallFlags_New));
  714. //
  715. // General algorithm:
  716. // 1. Call CheckObjectCoercible passing the this value as its argument.
  717. // 2. Let S be the result of calling ToString, giving it the this value as its argument.
  718. // 3. Let args be an internal list that is a copy of the argument list passed to this function.
  719. // 4. Let R be S.
  720. // 5. Repeat, while args is not empty
  721. // Remove the first element from args and let next be the value of that element.
  722. // Let R be the string value consisting of the characters in the previous value of R followed by the characters of ToString(next).
  723. // 6. Return R.
  724. //
  725. // NOTE
  726. // The concat function is intentionally generic; it does not require that its this value be a String object. Therefore it can be transferred to other kinds of objects for use as a method.
  727. //
  728. if(args.Info.Count == 0)
  729. {
  730. JavascriptError::ThrowTypeError(scriptContext, JSERR_This_NeedString, _u("String.prototype.concat"));
  731. }
  732. AssertMsg(args.Info.Count > 0, "Negative argument count");
  733. if (!JavascriptConversion::CheckObjectCoercible(args[0], scriptContext))
  734. {
  735. JavascriptError::ThrowTypeError(scriptContext, JSERR_This_NullOrUndefined, _u("String.prototype.concat"));
  736. }
  737. JavascriptString* pstr = nullptr;
  738. JavascriptString* accum = nullptr;
  739. for (uint index = 0; index < args.Info.Count; index++)
  740. {
  741. if (JavascriptString::Is(args[index]))
  742. {
  743. pstr = JavascriptString::FromVar(args[index]);
  744. }
  745. else
  746. {
  747. pstr = JavascriptConversion::ToString(args[index], scriptContext);
  748. }
  749. if (index == 0)
  750. {
  751. accum = pstr;
  752. }
  753. else
  754. {
  755. accum = Concat(accum,pstr);
  756. }
  757. }
  758. return accum;
  759. }
  760. Var JavascriptString::EntryFromCharCode(RecyclableObject* function, CallInfo callInfo, ...)
  761. {
  762. PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
  763. ARGUMENTS(args, callInfo);
  764. ScriptContext* scriptContext = function->GetScriptContext();
  765. Assert(!(callInfo.Flags & CallFlags_New));
  766. //
  767. // Construct a new string instance to contain all of the explicit parameters:
  768. // - Don't include the 'this' parameter.
  769. //
  770. if(args.Info.Count == 0)
  771. {
  772. JavascriptError::ThrowTypeError(scriptContext, JSERR_This_NeedString, _u("String.fromCharCode"));
  773. }
  774. AssertMsg(args.Info.Count > 0, "Negative argument count");
  775. int charLength = args.Info.Count - 1;
  776. // Special case for single char
  777. if( charLength == 1 )
  778. {
  779. char16 ch = JavascriptConversion::ToUInt16(args[1], scriptContext);
  780. return scriptContext->GetLibrary()->GetCharStringCache().GetStringForChar(ch);
  781. }
  782. BufferStringBuilder builder(charLength,scriptContext);
  783. char16 * stringBuffer = builder.DangerousGetWritableBuffer();
  784. //
  785. // Call ToUInt16 for each parameter, storing the character at the appropriate position.
  786. //
  787. for (uint idxArg = 1; idxArg < args.Info.Count; idxArg++)
  788. {
  789. *stringBuffer++ = JavascriptConversion::ToUInt16(args[idxArg], scriptContext);
  790. }
  791. //
  792. // Return the new string instance.
  793. //
  794. return builder.ToString();
  795. }
  796. Var JavascriptString::EntryFromCodePoint(RecyclableObject* function, CallInfo callInfo, ...)
  797. {
  798. PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
  799. ARGUMENTS(args, callInfo);
  800. ScriptContext* scriptContext = function->GetScriptContext();
  801. Assert(!(callInfo.Flags & CallFlags_New));
  802. AssertMsg(args.Info.Count > 0, "Negative argument count");
  803. if (args.Info.Count <= 1)
  804. {
  805. return scriptContext->GetLibrary()->GetEmptyString();
  806. }
  807. else if (args.Info.Count == 2)
  808. {
  809. // Special case for a single char string formed from only code point in range [0x0, 0xFFFF]
  810. double num = JavascriptConversion::ToNumber(args[1], scriptContext);
  811. if (!NumberUtilities::IsFinite(num))
  812. {
  813. JavascriptError::ThrowRangeError(scriptContext, JSERR_InvalidCodePoint);
  814. }
  815. if (num < 0 || num > 0x10FFFF || floor(num) != num)
  816. {
  817. JavascriptError::ThrowRangeErrorVar(scriptContext, JSERR_InvalidCodePoint, Js::JavascriptConversion::ToString(args[1], scriptContext)->GetSz());
  818. }
  819. if (num < 0x10000)
  820. {
  821. return scriptContext->GetLibrary()->GetCharStringCache().GetStringForChar((uint16)num);
  822. }
  823. }
  824. BEGIN_TEMP_ALLOCATOR(tempAllocator, scriptContext, _u("fromCodePoint"));
  825. // Create a temporary buffer that is double the arguments count (in case all are surrogate pairs)
  826. size_t bufferLength = (args.Info.Count - 1) * 2;
  827. char16 *tempBuffer = AnewArray(tempAllocator, char16, bufferLength);
  828. uint32 count = 0;
  829. for (uint i = 1; i < args.Info.Count; i++)
  830. {
  831. double num = JavascriptConversion::ToNumber(args[i], scriptContext);
  832. if (!NumberUtilities::IsFinite(num))
  833. {
  834. JavascriptError::ThrowRangeError(scriptContext, JSERR_InvalidCodePoint);
  835. }
  836. if (num < 0 || num > 0x10FFFF || floor(num) != num)
  837. {
  838. JavascriptError::ThrowRangeErrorVar(scriptContext, JSERR_InvalidCodePoint, Js::JavascriptConversion::ToString(args[i], scriptContext)->GetSz());
  839. }
  840. if (num < 0x10000)
  841. {
  842. __analysis_assume(count < bufferLength);
  843. Assert(count < bufferLength);
  844. #pragma prefast(suppress: 22102, "I have an assert in place to guard against overflow. Even though this should never happen.")
  845. tempBuffer[count] = (char16)num;
  846. count++;
  847. }
  848. else
  849. {
  850. __analysis_assume(count + 1 < bufferLength);
  851. Assert(count + 1 < bufferLength);
  852. NumberUtilities::CodePointAsSurrogatePair((codepoint_t)num, (tempBuffer + count), (tempBuffer + count + 1));
  853. count += 2;
  854. }
  855. }
  856. // Create a string of appropriate length
  857. __analysis_assume(count <= bufferLength);
  858. Assert(count <= bufferLength);
  859. JavascriptString *toReturn = JavascriptString::NewCopyBuffer(tempBuffer, count, scriptContext);
  860. END_TEMP_ALLOCATOR(tempAllocator, scriptContext);
  861. return toReturn;
  862. }
  863. Var JavascriptString::EntryIndexOf(RecyclableObject* function, CallInfo callInfo, ...)
  864. {
  865. PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
  866. ARGUMENTS(args, callInfo);
  867. AssertMsg(args.Info.Count > 0, "Should always have implicit 'this'");
  868. ScriptContext* scriptContext = function->GetScriptContext();
  869. Assert(!(callInfo.Flags & CallFlags_New));
  870. return JavascriptNumber::ToVar(IndexOf(args, scriptContext, _u("String.prototype.indexOf"), true), scriptContext);
  871. }
  872. int JavascriptString::IndexOf(ArgumentReader& args, ScriptContext* scriptContext, const char16* apiNameForErrorMsg, bool isRegExpAnAllowedArg)
  873. {
  874. // The algorithm steps in the spec are the same between String.prototype.indexOf and
  875. // String.prototype.includes, except that includes returns true if an index is found,
  876. // false otherwise. Share the implementation between these two APIs.
  877. //
  878. // 1. Call CheckObjectCoercible passing the this value as its argument.
  879. // 2. Let S be the result of calling ToString, giving it the this value as its argument.
  880. // 3. Let searchStr be ToString(searchString).
  881. // 4. Let pos be ToInteger(position). (If position is undefined, this step produces the value 0).
  882. // 5. Let len be the number of characters in S.
  883. // 6. Let start be min(max(pos, 0), len).
  884. // 7. Let searchLen be the number of characters in searchStr.
  885. // 8. Return the smallest possible integer k not smaller than start such that k+ searchLen is not greater than len, and for all nonnegative integers j less than searchLen, the character at position k+j of S is the same as the character at position j of searchStr); but if there is no such integer k, then return the value -1.
  886. // NOTE
  887. // The indexOf function is intentionally generic; it does not require that its this value be a String object. Therefore, it can be transferred to other kinds of objects for use as a method.
  888. //
  889. JavascriptString * pThis;
  890. JavascriptString * searchString;
  891. GetThisAndSearchStringArguments(args, scriptContext, apiNameForErrorMsg, &pThis, &searchString, isRegExpAnAllowedArg);
  892. int len = pThis->GetLength();
  893. int searchLen = searchString->GetLength();
  894. int position = 0;
  895. if (args.Info.Count > 2)
  896. {
  897. if (JavascriptOperators::IsUndefinedObject(args[2], scriptContext))
  898. {
  899. position = 0;
  900. }
  901. else
  902. {
  903. position = ConvertToIndex(args[2], scriptContext); // this is to adjust corner cases like MAX_VALUE
  904. position = min(max(position, 0), len); // adjust position within string limits
  905. }
  906. }
  907. // Zero length search strings are always found at the current search position
  908. if (searchLen == 0)
  909. {
  910. return position;
  911. }
  912. int result = -1;
  913. if (position < pThis->GetLengthAsSignedInt())
  914. {
  915. const char16* searchStr = searchString->GetString();
  916. const char16* inputStr = pThis->GetString();
  917. if (searchLen == 1)
  918. {
  919. int i = position;
  920. for(; i < len && inputStr[i] != *searchStr ; i++);
  921. if (i < len)
  922. {
  923. result = i;
  924. }
  925. }
  926. else
  927. {
  928. JmpTable jmpTable;
  929. bool fAsciiJumpTable = BuildLastCharForwardBoyerMooreTable(jmpTable, searchStr, searchLen);
  930. if (!fAsciiJumpTable)
  931. {
  932. result = JavascriptString::strstr(pThis, searchString, false, position);
  933. }
  934. else
  935. {
  936. result = IndexOfUsingJmpTable(jmpTable, inputStr, len, searchStr, searchLen, position);
  937. }
  938. }
  939. }
  940. return result;
  941. }
  942. Var JavascriptString::EntryLastIndexOf(RecyclableObject* function, CallInfo callInfo, ...)
  943. {
  944. PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
  945. ARGUMENTS(args, callInfo);
  946. ScriptContext* scriptContext = function->GetScriptContext();
  947. Assert(!(callInfo.Flags & CallFlags_New));
  948. //
  949. // 1. Call CheckObjectCoercible passing the this value as its argument.
  950. // 2. Let S be the result of calling ToString, giving it the this value as its argument.
  951. // 3. Let searchStr be ToString(searchString).
  952. // 4. Let numPos be ToNumber(position). (If position is undefined, this step produces the value NaN).
  953. // 5. If numPos is NaN, let pos be +?; otherwise, let pos be ToInteger(numPos).
  954. // 6. Let len be the number of characters in S.
  955. // 7. Let start min(max(pos, 0), len).
  956. // 8. Let searchLen be the number of characters in searchStr.
  957. // 9. Return the largest possible nonnegative integer k not larger than start such that k+ searchLen is not greater than len, and for all nonnegative integers j less than searchLen, the character at position k+j of S is the same as the character at position j of searchStr; but if there is no such integer k, then return the value -1.
  958. // NOTE
  959. // The lastIndexOf function is intentionally generic; it does not require that its this value be a String object. Therefore, it can be transferred to other kinds of objects for use as a method.
  960. //
  961. JavascriptString * pThis = nullptr;
  962. GetThisStringArgument(args, scriptContext, _u("String.prototype.lastIndexOf"), &pThis);
  963. // default search string if the search argument is not provided
  964. JavascriptString * searchArg;
  965. if(args.Info.Count > 1)
  966. {
  967. if (JavascriptString::Is(args[1]))
  968. {
  969. searchArg = JavascriptString::FromVar(args[1]);
  970. }
  971. else
  972. {
  973. searchArg = JavascriptConversion::ToString(args[1], scriptContext);
  974. }
  975. }
  976. else
  977. {
  978. searchArg = scriptContext->GetLibrary()->GetUndefinedDisplayString();
  979. }
  980. char16 const * const inputStr = pThis->GetString();
  981. const char16 * const searchStr = searchArg->GetString();
  982. const int len = pThis->GetLength();
  983. int position = len;
  984. int const searchLen = searchArg->GetLength();
  985. // Determine if the main string can't contain the search string by length
  986. if ( searchLen > len )
  987. {
  988. return JavascriptNumber::ToVar(-1, scriptContext);
  989. }
  990. if(args.Info.Count > 2)
  991. {
  992. double pos = JavascriptConversion::ToNumber(args[2], scriptContext);
  993. if (!JavascriptNumber::IsNan(pos))
  994. {
  995. pos = JavascriptConversion::ToInteger(pos);
  996. position = (int)min(max(pos, (double)0), (double)len); // adjust position within string limits
  997. }
  998. }
  999. int result = -1;
  1000. // Zero length search strings are always found at the current search position
  1001. if ( searchLen == 0 )
  1002. {
  1003. return JavascriptNumber::ToVar(position, scriptContext);
  1004. }
  1005. else if (searchLen == 1)
  1006. {
  1007. char16 const * current = inputStr + min(position,len - 1);
  1008. while (*current != *searchStr)
  1009. {
  1010. current--;
  1011. if (current < inputStr)
  1012. {
  1013. return JavascriptNumber::ToVar(result, scriptContext); //return -1
  1014. }
  1015. }
  1016. result = (int)(current - inputStr);
  1017. return JavascriptNumber::ToVar(result, scriptContext);
  1018. }
  1019. // Structure for a partial ASCII Boyer-Moore
  1020. JmpTable jmpTable;
  1021. bool fAsciiJumpTable = BuildFirstCharBackwardBoyerMooreTable(jmpTable, searchStr, searchLen);
  1022. if (!fAsciiJumpTable)
  1023. {
  1024. char16 const * start = inputStr;
  1025. char16 const * current = inputStr + min(position, len - 1);
  1026. char16 const * searchStrEnd = searchStr + searchLen - 1;
  1027. while (current >= (start + searchLen - 1))
  1028. {
  1029. char16 const * s1 = current;
  1030. char16 const * s2 = searchStrEnd;
  1031. while (s1 >= start && s2 >= searchStr && !(*s1 - *s2))
  1032. {
  1033. s1--, s2--;
  1034. }
  1035. if (s2 < searchStr)
  1036. {
  1037. result = (int)(current - start) - searchLen + 1;
  1038. return JavascriptNumber::ToVar(result, scriptContext);
  1039. }
  1040. current--;
  1041. }
  1042. return JavascriptNumber::ToVar(result, scriptContext);
  1043. }
  1044. else
  1045. {
  1046. result = LastIndexOfUsingJmpTable(jmpTable, inputStr, len, searchStr, searchLen, position);
  1047. }
  1048. return JavascriptNumber::ToVar(result, scriptContext);
  1049. }
  1050. // Performs common ES spec steps for getting this argument in string form:
  1051. // 1. Let O be CHeckObjectCoercible(this value).
  1052. // 2. Let S be ToString(O).
  1053. // 3. ReturnIfAbrupt(S).
  1054. void JavascriptString::GetThisStringArgument(ArgumentReader& args, ScriptContext* scriptContext, const char16* apiNameForErrorMsg, JavascriptString** ppThis)
  1055. {
  1056. if (args.Info.Count == 0)
  1057. {
  1058. JavascriptError::ThrowTypeError(scriptContext, JSERR_This_NeedString, apiNameForErrorMsg);
  1059. }
  1060. AssertMsg(args.Info.Count > 0, "Negative argument count");
  1061. JavascriptString * pThis;
  1062. if (JavascriptString::Is(args[0]))
  1063. {
  1064. pThis = JavascriptString::FromVar(args[0]);
  1065. }
  1066. else
  1067. {
  1068. pThis = JavascriptConversion::CoerseString(args[0], scriptContext , apiNameForErrorMsg);
  1069. }
  1070. *ppThis = pThis;
  1071. }
  1072. // Performs common ES spec steps for getting this and first parameter arguments in string form:
  1073. // 1. Let O be CHeckObjectCoercible(this value).
  1074. // 2. Let S be ToString(O).
  1075. // 3. ReturnIfAbrupt(S).
  1076. // 4. Let otherStr be ToString(firstArg).
  1077. // 5. ReturnIfAbrupt(otherStr).
  1078. void JavascriptString::GetThisAndSearchStringArguments(ArgumentReader& args, ScriptContext* scriptContext, const char16* apiNameForErrorMsg, JavascriptString** ppThis, JavascriptString** ppSearch, bool isRegExpAnAllowedArg)
  1079. {
  1080. GetThisStringArgument(args, scriptContext, apiNameForErrorMsg, ppThis);
  1081. JavascriptString * pSearch = scriptContext->GetLibrary()->GetUndefinedDisplayString();
  1082. if (args.Info.Count > 1)
  1083. {
  1084. if (!isRegExpAnAllowedArg && JavascriptRegExp::IsRegExpLike(args[1], scriptContext))
  1085. {
  1086. JavascriptError::ThrowTypeError(scriptContext, JSERR_FunctionArgument_FirstCannotBeRegExp, apiNameForErrorMsg);
  1087. }
  1088. else if (JavascriptString::Is(args[1]))
  1089. {
  1090. pSearch = JavascriptString::FromVar(args[1]);
  1091. }
  1092. else
  1093. {
  1094. pSearch = JavascriptConversion::ToString(args[1], scriptContext);
  1095. }
  1096. }
  1097. *ppSearch = pSearch;
  1098. }
  1099. Var JavascriptString::EntryLocaleCompare(RecyclableObject* function, CallInfo callInfo, ...)
  1100. {
  1101. PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
  1102. ARGUMENTS(args, callInfo);
  1103. ScriptContext* scriptContext = function->GetScriptContext();
  1104. Assert(!(callInfo.Flags & CallFlags_New));
  1105. if(args.Info.Count == 0)
  1106. {
  1107. JavascriptError::ThrowTypeError(scriptContext, JSERR_This_NeedString, _u("String.prototype.localeCompare"));
  1108. }
  1109. AssertMsg(args.Info.Count > 0, "Negative argument count");
  1110. JavascriptString * pThis;
  1111. JavascriptString * pThat;
  1112. GetThisAndSearchStringArguments(args, scriptContext, _u("String.prototype.localeCompare"), &pThis, &pThat, true);
  1113. #ifdef ENABLE_INTL_OBJECT
  1114. if (CONFIG_FLAG(IntlBuiltIns) && scriptContext->IsIntlEnabled())
  1115. {
  1116. EngineInterfaceObject* nativeEngineInterfaceObj = scriptContext->GetLibrary()->GetEngineInterfaceObject();
  1117. if (nativeEngineInterfaceObj)
  1118. {
  1119. IntlEngineInterfaceExtensionObject* intlExtensionObject = static_cast<IntlEngineInterfaceExtensionObject*>(nativeEngineInterfaceObj->GetEngineExtension(EngineInterfaceExtensionKind_Intl));
  1120. if (args.Info.Count == 2)
  1121. {
  1122. auto undefined = scriptContext->GetLibrary()->GetUndefined();
  1123. CallInfo toPass(callInfo.Flags, 7);
  1124. return intlExtensionObject->EntryIntl_CompareString(function, toPass, undefined, pThis, pThat, undefined, undefined, undefined, undefined);
  1125. }
  1126. else
  1127. {
  1128. JavascriptFunction* func = intlExtensionObject->GetStringLocaleCompare();
  1129. if (func)
  1130. {
  1131. return func->CallFunction(args);
  1132. }
  1133. // Initialize String.prototype.toLocaleCompare
  1134. scriptContext->GetLibrary()->InitializeIntlForStringPrototype();
  1135. func = intlExtensionObject->GetStringLocaleCompare();
  1136. if (func)
  1137. {
  1138. return func->CallFunction(args);
  1139. }
  1140. }
  1141. }
  1142. }
  1143. #endif
  1144. const char16* pThisStr = pThis->GetString();
  1145. int thisStrCount = pThis->GetLength();
  1146. const char16* pThatStr = pThat->GetString();
  1147. int thatStrCount = pThat->GetLength();
  1148. LCID lcid = GetUserDefaultLCID();
  1149. int result = CompareStringW(lcid, NULL, pThisStr, thisStrCount, pThatStr, thatStrCount );
  1150. if (result == 0)
  1151. {
  1152. // TODO there is no spec on the error thrown here.
  1153. // When the support for HR errors is implemented replace this with the same error reported by v5.8
  1154. JavascriptError::ThrowRangeError(function->GetScriptContext(),
  1155. VBSERR_InternalError /* TODO-ERROR: _u("Failed compare operation")*/ );
  1156. }
  1157. return JavascriptNumber::ToVar(result-2, scriptContext);
  1158. }
  1159. Var JavascriptString::EntryMatch(RecyclableObject* function, CallInfo callInfo, ...)
  1160. {
  1161. PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
  1162. ARGUMENTS(args, callInfo);
  1163. ScriptContext* scriptContext = function->GetScriptContext();
  1164. Assert(!(callInfo.Flags & CallFlags_New));
  1165. PCWSTR const varName = _u("String.prototype.match");
  1166. AUTO_TAG_NATIVE_LIBRARY_ENTRY(function, callInfo, varName);
  1167. auto fallback = [&](JavascriptString* stringObj)
  1168. {
  1169. Var regExp = (args.Info.Count > 1) ? args[1] : scriptContext->GetLibrary()->GetUndefined();
  1170. if (!scriptContext->GetConfig()->IsES6RegExSymbolsEnabled())
  1171. {
  1172. JavascriptRegExp * regExObj = JavascriptRegExp::CreateRegEx(regExp, nullptr, scriptContext);
  1173. return RegexHelper::RegexMatch(
  1174. scriptContext,
  1175. regExObj,
  1176. stringObj,
  1177. RegexHelper::IsResultNotUsed(callInfo.Flags));
  1178. }
  1179. else
  1180. {
  1181. JavascriptRegExp * regExObj = JavascriptRegExp::CreateRegExNoCoerce(regExp, nullptr, scriptContext);
  1182. Var symbolFn = GetRegExSymbolFunction(regExObj, PropertyIds::_symbolMatch, scriptContext);
  1183. return CallRegExSymbolFunction<1>(symbolFn, regExObj, args, varName, scriptContext);
  1184. }
  1185. };
  1186. return DelegateToRegExSymbolFunction<1>(args, PropertyIds::_symbolMatch, fallback, varName, scriptContext);
  1187. }
  1188. Var JavascriptString::EntryNormalize(RecyclableObject* function, CallInfo callInfo, ...)
  1189. {
  1190. PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
  1191. ARGUMENTS(args, callInfo);
  1192. ScriptContext* scriptContext = function->GetScriptContext();
  1193. Assert(!(callInfo.Flags & CallFlags_New));
  1194. JavascriptString *pThis = nullptr;
  1195. GetThisStringArgument(args, scriptContext, _u("String.prototype.normalize"), &pThis);
  1196. NORM_FORM form = NORM_FORM::NormalizationC;
  1197. if (args.Info.Count >= 2 && !(JavascriptOperators::IsUndefinedObject(args.Values[1])))
  1198. {
  1199. JavascriptString *formStr = nullptr;
  1200. if (JavascriptString::Is(args[1]))
  1201. {
  1202. formStr = JavascriptString::FromVar(args[1]);
  1203. }
  1204. else
  1205. {
  1206. formStr = JavascriptConversion::ToString(args[1], scriptContext);
  1207. }
  1208. if (formStr->BufferEquals(_u("NFD"), 3))
  1209. {
  1210. form = NORM_FORM::NormalizationD;
  1211. }
  1212. else if (formStr->BufferEquals(_u("NFKC"), 4))
  1213. {
  1214. form = NORM_FORM::NormalizationKC;
  1215. }
  1216. else if (formStr->BufferEquals(_u("NFKD"), 4))
  1217. {
  1218. form = NORM_FORM::NormalizationKD;
  1219. }
  1220. else if (!formStr->BufferEquals(_u("NFC"), 3))
  1221. {
  1222. JavascriptError::ThrowRangeErrorVar(scriptContext, JSERR_InvalidNormalizationForm, formStr->GetString());
  1223. }
  1224. }
  1225. if (IsNormalizedString(form, pThis->GetSz(), pThis->GetLength()))
  1226. {
  1227. return JavascriptString::NewWithSz(pThis->GetSz(), scriptContext);
  1228. }
  1229. BEGIN_TEMP_ALLOCATOR(tempAllocator, scriptContext, _u("normalize"));
  1230. charcount_t sizeEstimate = 0;
  1231. char16* buffer = pThis->GetNormalizedString(form, tempAllocator, sizeEstimate);
  1232. JavascriptString * retVal;
  1233. if (buffer == nullptr)
  1234. {
  1235. Assert(sizeEstimate == 0);
  1236. retVal = scriptContext->GetLibrary()->GetEmptyString();
  1237. }
  1238. else
  1239. {
  1240. retVal = JavascriptString::NewCopyBuffer(buffer, sizeEstimate, scriptContext);
  1241. }
  1242. END_TEMP_ALLOCATOR(tempAllocator, scriptContext);
  1243. return retVal;
  1244. }
  1245. ///----------------------------------------------------------------------------
  1246. /// String.raw(), as described in (ES6.0 (Draft 18): S21.1.2.4).
  1247. ///----------------------------------------------------------------------------
  1248. Var JavascriptString::EntryRaw(RecyclableObject* function, CallInfo callInfo, ...)
  1249. {
  1250. PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
  1251. ARGUMENTS(args, callInfo);
  1252. ScriptContext* scriptContext = function->GetScriptContext();
  1253. Assert(!(callInfo.Flags & CallFlags_New));
  1254. if (args.Info.Count < 2)
  1255. {
  1256. JavascriptError::ThrowTypeError(scriptContext, JSERR_FunctionArgument_NeedObject, _u("String.raw"));
  1257. }
  1258. RecyclableObject* callSite;
  1259. RecyclableObject* raw;
  1260. Var rawVar;
  1261. // Call ToObject on the first argument to get the callSite (which is also cooked string array)
  1262. // ToObject returns false if the parameter is null or undefined
  1263. if (!JavascriptConversion::ToObject(args[1], scriptContext, &callSite))
  1264. {
  1265. JavascriptError::ThrowTypeError(scriptContext, JSERR_FunctionArgument_NeedObject, _u("String.raw"));
  1266. }
  1267. // Get the raw property from the callSite object
  1268. if (!callSite->GetProperty(callSite, Js::PropertyIds::raw, &rawVar, nullptr, scriptContext))
  1269. {
  1270. rawVar = scriptContext->GetLibrary()->GetUndefined();
  1271. }
  1272. if (!JavascriptConversion::ToObject(rawVar, scriptContext, &raw))
  1273. {
  1274. JavascriptError::ThrowTypeError(scriptContext, JSERR_FunctionArgument_NeedObject, _u("String.raw"));
  1275. }
  1276. int64 length = JavascriptConversion::ToLength(JavascriptOperators::OP_GetLength(raw, scriptContext), scriptContext);
  1277. // If there are no raw strings (somehow), return empty string
  1278. if (length <= 0)
  1279. {
  1280. return scriptContext->GetLibrary()->GetEmptyString();
  1281. }
  1282. // Get the first raw string
  1283. Var var = JavascriptOperators::OP_GetElementI_UInt32(raw, 0, scriptContext);
  1284. JavascriptString* string = JavascriptConversion::ToString(var, scriptContext);
  1285. // If there is only one raw string, just return that one raw string (doesn't matter if there are replacements)
  1286. if (length == 1)
  1287. {
  1288. return string;
  1289. }
  1290. // We aren't going to bail early so let's create a StringBuilder and put the first raw string in there
  1291. CompoundString::Builder<64 * sizeof(void *) / sizeof(char16)> stringBuilder(scriptContext);
  1292. stringBuilder.Append(string);
  1293. // Each raw string is followed by a substitution expression except for the last one
  1294. // We will always have one more string constant than substitution expression
  1295. // `strcon1 ${expr1} strcon2 ${expr2} strcon3` = strcon1 + expr1 + strcon2 + expr2 + strcon3
  1296. //
  1297. // strcon1 --- step 1 (above)
  1298. // expr1 \__ step 2
  1299. // strcon2 /
  1300. // expr2 \__ step 3
  1301. // strcon3 /
  1302. for (uint32 i = 1; i < length; ++i)
  1303. {
  1304. // First append the next substitution expression
  1305. // If we have an arg at [i+1] use that one, otherwise empty string (which is nop)
  1306. if (i+1 < args.Info.Count)
  1307. {
  1308. string = JavascriptConversion::ToString(args[i+1], scriptContext);
  1309. stringBuilder.Append(string);
  1310. }
  1311. // Then append the next string (this will also cover the final string case)
  1312. var = JavascriptOperators::OP_GetElementI_UInt32(raw, i, scriptContext);
  1313. string = JavascriptConversion::ToString(var, scriptContext);
  1314. stringBuilder.Append(string);
  1315. }
  1316. // CompoundString::Builder has saved our lives
  1317. return stringBuilder.ToString();
  1318. }
  1319. Var JavascriptString::EntryReplace(RecyclableObject* function, CallInfo callInfo, ...)
  1320. {
  1321. PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
  1322. ARGUMENTS(args, callInfo);
  1323. ScriptContext* scriptContext = function->GetScriptContext();
  1324. PCWSTR const varName = _u("String.prototype.replace");
  1325. AUTO_TAG_NATIVE_LIBRARY_ENTRY(function, callInfo, varName);
  1326. Assert(!(callInfo.Flags & CallFlags_New));
  1327. auto fallback = [&](JavascriptString* stringObj)
  1328. {
  1329. return DoStringReplace(args, callInfo, stringObj, scriptContext);
  1330. };
  1331. return DelegateToRegExSymbolFunction<2>(args, PropertyIds::_symbolReplace, fallback, varName, scriptContext);
  1332. }
  1333. Var JavascriptString::DoStringReplace(Arguments& args, CallInfo& callInfo, JavascriptString* input, ScriptContext* scriptContext)
  1334. {
  1335. //
  1336. // TODO: Move argument processing into DirectCall with proper handling.
  1337. //
  1338. JavascriptRegExp * pRegEx = nullptr;
  1339. JavascriptString * pMatch = nullptr;
  1340. JavascriptString * pReplace = nullptr;
  1341. JavascriptFunction* replacefn = nullptr;
  1342. SearchValueHelper(scriptContext, ((args.Info.Count > 1)?args[1]:scriptContext->GetLibrary()->GetNull()), &pRegEx, &pMatch);
  1343. ReplaceValueHelper(scriptContext, ((args.Info.Count > 2) ? args[2] : scriptContext->GetLibrary()->GetUndefined()), &replacefn, &pReplace);
  1344. if (pRegEx != nullptr)
  1345. {
  1346. if (replacefn != nullptr)
  1347. {
  1348. return RegexHelper::RegexReplaceFunction(scriptContext, pRegEx, input, replacefn);
  1349. }
  1350. else
  1351. {
  1352. return RegexHelper::RegexReplace(scriptContext, pRegEx, input, pReplace, RegexHelper::IsResultNotUsed(callInfo.Flags));
  1353. }
  1354. }
  1355. AssertMsg(pMatch != nullptr, "Match string shouldn't be null");
  1356. if (replacefn != nullptr)
  1357. {
  1358. return RegexHelper::StringReplace(pMatch, input, replacefn);
  1359. }
  1360. else
  1361. {
  1362. if (callInfo.Flags & CallFlags_NotUsed)
  1363. {
  1364. return scriptContext->GetLibrary()->GetEmptyString();
  1365. }
  1366. return RegexHelper::StringReplace(pMatch, input, pReplace);
  1367. }
  1368. }
  1369. void JavascriptString::SearchValueHelper(ScriptContext* scriptContext, Var aValue, JavascriptRegExp ** ppSearchRegEx, JavascriptString ** ppSearchString)
  1370. {
  1371. *ppSearchRegEx = nullptr;
  1372. *ppSearchString = nullptr;
  1373. // When the config is enabled, the operation is handled by a Symbol function (e.g. Symbol.replace).
  1374. if (!scriptContext->GetConfig()->IsES6RegExSymbolsEnabled()
  1375. && JavascriptRegExp::Is(aValue))
  1376. {
  1377. *ppSearchRegEx = JavascriptRegExp::FromVar(aValue);
  1378. }
  1379. else if (JavascriptString::Is(aValue))
  1380. {
  1381. *ppSearchString = JavascriptString::FromVar(aValue);
  1382. }
  1383. else
  1384. {
  1385. *ppSearchString = JavascriptConversion::ToString(aValue, scriptContext);
  1386. }
  1387. }
  1388. void JavascriptString::ReplaceValueHelper(ScriptContext* scriptContext, Var aValue, JavascriptFunction ** ppReplaceFn, JavascriptString ** ppReplaceString)
  1389. {
  1390. *ppReplaceFn = nullptr;
  1391. *ppReplaceString = nullptr;
  1392. if (JavascriptFunction::Is(aValue))
  1393. {
  1394. *ppReplaceFn = JavascriptFunction::FromVar(aValue);
  1395. }
  1396. else if (JavascriptString::Is(aValue))
  1397. {
  1398. *ppReplaceString = JavascriptString::FromVar(aValue);
  1399. }
  1400. else
  1401. {
  1402. *ppReplaceString = JavascriptConversion::ToString(aValue, scriptContext);
  1403. }
  1404. }
  1405. Var JavascriptString::EntrySearch(RecyclableObject* function, CallInfo callInfo, ...)
  1406. {
  1407. PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
  1408. ARGUMENTS(args, callInfo);
  1409. ScriptContext* scriptContext = function->GetScriptContext();
  1410. Assert(!(callInfo.Flags & CallFlags_New));
  1411. PCWSTR const varName = _u("String.prototype.search");
  1412. AUTO_TAG_NATIVE_LIBRARY_ENTRY(function, callInfo, varName);
  1413. auto fallback = [&](JavascriptString* stringObj)
  1414. {
  1415. Var regExp = (args.Info.Count > 1) ? args[1] : scriptContext->GetLibrary()->GetUndefined();
  1416. if (!scriptContext->GetConfig()->IsES6RegExSymbolsEnabled())
  1417. {
  1418. JavascriptRegExp * regExObj = JavascriptRegExp::CreateRegEx(regExp, nullptr, scriptContext);
  1419. return RegexHelper::RegexSearch(scriptContext, regExObj, stringObj);
  1420. }
  1421. else
  1422. {
  1423. JavascriptRegExp * regExObj = JavascriptRegExp::CreateRegExNoCoerce(regExp, nullptr, scriptContext);
  1424. Var symbolFn = GetRegExSymbolFunction(regExObj, PropertyIds::_symbolSearch, scriptContext);
  1425. return CallRegExSymbolFunction<1>(symbolFn, regExObj, args, varName, scriptContext);
  1426. }
  1427. };
  1428. return DelegateToRegExSymbolFunction<1>(args, PropertyIds::_symbolSearch, fallback, varName, scriptContext);
  1429. }
  1430. template<int argCount, typename FallbackFn>
  1431. Var JavascriptString::DelegateToRegExSymbolFunction(ArgumentReader &args, PropertyId symbolPropertyId, FallbackFn fallback, PCWSTR varName, ScriptContext* scriptContext)
  1432. {
  1433. if (scriptContext->GetConfig()->IsES6RegExSymbolsEnabled())
  1434. {
  1435. if (args.Info.Count == 0 || !JavascriptConversion::CheckObjectCoercible(args[0], scriptContext))
  1436. {
  1437. JavascriptError::ThrowTypeError(scriptContext, JSERR_This_NullOrUndefined, varName);
  1438. }
  1439. if (args.Info.Count >= 2 && !JavascriptOperators::IsUndefinedOrNull(args[1]))
  1440. {
  1441. Var regExp = args[1];
  1442. Var symbolFn = GetRegExSymbolFunction(regExp, symbolPropertyId, scriptContext);
  1443. if (!JavascriptOperators::IsUndefinedOrNull(symbolFn))
  1444. {
  1445. return CallRegExSymbolFunction<argCount>(symbolFn, regExp, args, varName, scriptContext);
  1446. }
  1447. }
  1448. }
  1449. JavascriptString * pThis = nullptr;
  1450. GetThisStringArgument(args, scriptContext, varName, &pThis);
  1451. return fallback(pThis);
  1452. }
  1453. Var JavascriptString::GetRegExSymbolFunction(Var regExp, PropertyId propertyId, ScriptContext* scriptContext)
  1454. {
  1455. return JavascriptOperators::GetProperty(
  1456. RecyclableObject::FromVar(JavascriptOperators::ToObject(regExp, scriptContext)),
  1457. propertyId,
  1458. scriptContext);
  1459. }
  1460. template<int argCount>
  1461. Var JavascriptString::CallRegExSymbolFunction(Var fn, Var regExp, Arguments& args, PCWSTR const varName, ScriptContext* scriptContext)
  1462. {
  1463. if (!JavascriptConversion::IsCallable(fn))
  1464. {
  1465. JavascriptError::ThrowTypeError(scriptContext, JSERR_FunctionArgument_Invalid, varName);
  1466. }
  1467. RecyclableObject* fnObj = RecyclableObject::FromVar(fn);
  1468. return CallRegExFunction<argCount>(fnObj, regExp, args);
  1469. }
  1470. template<>
  1471. Var JavascriptString::CallRegExFunction<1>(RecyclableObject* fnObj, Var regExp, Arguments& args)
  1472. {
  1473. // args[0]: String
  1474. return fnObj->GetEntryPoint()(fnObj, CallInfo(CallFlags_Value, 2), regExp, args[0]);
  1475. }
  1476. template<>
  1477. Var JavascriptString::CallRegExFunction<2>(RecyclableObject* fnObj, Var regExp, Arguments& args)
  1478. {
  1479. // args[0]: String
  1480. // args[1]: RegExp (ignored since we need to create one when the argument is "undefined")
  1481. // args[2]: Var
  1482. if (args.Info.Count < 3)
  1483. {
  1484. return CallRegExFunction<1>(fnObj, regExp, args);
  1485. }
  1486. return fnObj->GetEntryPoint()(fnObj, CallInfo(CallFlags_Value, 3), regExp, args[0], args[2]);
  1487. }
  1488. Var JavascriptString::EntrySlice(RecyclableObject* function, CallInfo callInfo, ...)
  1489. {
  1490. PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
  1491. ARGUMENTS(args, callInfo);
  1492. ScriptContext* scriptContext = function->GetScriptContext();
  1493. Assert(!(callInfo.Flags & CallFlags_New));
  1494. JavascriptString * pThis = nullptr;
  1495. GetThisStringArgument(args, scriptContext, _u("String.prototype.slice"), &pThis);
  1496. int len = pThis->GetLength();
  1497. int idxStart = 0;
  1498. int idxEnd = len;
  1499. if (args.Info.Count > 1)
  1500. {
  1501. idxStart = JavascriptOperators::IsUndefinedObject(args[1], scriptContext) ? 0 : ConvertToIndex(args[1], scriptContext);
  1502. if (args.Info.Count > 2)
  1503. {
  1504. idxEnd = JavascriptOperators::IsUndefinedObject(args[2], scriptContext) ? len : ConvertToIndex(args[2], scriptContext);
  1505. }
  1506. }
  1507. if (idxStart < 0)
  1508. {
  1509. idxStart = max(len + idxStart, 0);
  1510. }
  1511. else if (idxStart > len)
  1512. {
  1513. idxStart = len;
  1514. }
  1515. if (idxEnd < 0)
  1516. {
  1517. idxEnd = max(len + idxEnd, 0);
  1518. }
  1519. else if (idxEnd > len )
  1520. {
  1521. idxEnd = len;
  1522. }
  1523. if (idxEnd < idxStart)
  1524. {
  1525. idxEnd = idxStart;
  1526. }
  1527. return SubstringCore(pThis, idxStart, idxEnd - idxStart, scriptContext);
  1528. }
  1529. Var JavascriptString::EntrySplit(RecyclableObject* function, CallInfo callInfo, ...)
  1530. {
  1531. PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
  1532. ARGUMENTS(args, callInfo);
  1533. ScriptContext* scriptContext = function->GetScriptContext();
  1534. Assert(!(callInfo.Flags & CallFlags_New));
  1535. PCWSTR const varName = _u("String.prototype.split");
  1536. AUTO_TAG_NATIVE_LIBRARY_ENTRY(function, callInfo, varName);
  1537. auto fallback = [&](JavascriptString* stringObj)
  1538. {
  1539. return DoStringSplit(args, callInfo, stringObj, scriptContext);
  1540. };
  1541. return DelegateToRegExSymbolFunction<2>(args, PropertyIds::_symbolSplit, fallback, varName, scriptContext);
  1542. }
  1543. Var JavascriptString::DoStringSplit(Arguments& args, CallInfo& callInfo, JavascriptString* input, ScriptContext* scriptContext)
  1544. {
  1545. if (args.Info.Count == 1)
  1546. {
  1547. JavascriptArray* ary = scriptContext->GetLibrary()->CreateArray(1);
  1548. ary->DirectSetItemAt(0, input);
  1549. return ary;
  1550. }
  1551. else
  1552. {
  1553. uint32 limit;
  1554. if (args.Info.Count < 3 || JavascriptOperators::IsUndefinedObject(args[2], scriptContext))
  1555. {
  1556. limit = UINT_MAX;
  1557. }
  1558. else
  1559. {
  1560. limit = JavascriptConversion::ToUInt32(args[2], scriptContext);
  1561. }
  1562. // When the config is enabled, the operation is handled by RegExp.prototype[@@split].
  1563. if (!scriptContext->GetConfig()->IsES6RegExSymbolsEnabled()
  1564. && JavascriptRegExp::Is(args[1]))
  1565. {
  1566. return RegexHelper::RegexSplit(scriptContext, JavascriptRegExp::FromVar(args[1]), input, limit,
  1567. RegexHelper::IsResultNotUsed(callInfo.Flags));
  1568. }
  1569. else
  1570. {
  1571. JavascriptString* separator = JavascriptConversion::ToString(args[1], scriptContext);
  1572. if (callInfo.Flags & CallFlags_NotUsed)
  1573. {
  1574. return scriptContext->GetLibrary()->GetNull();
  1575. }
  1576. if (!limit)
  1577. {
  1578. JavascriptArray* ary = scriptContext->GetLibrary()->CreateArray(0);
  1579. return ary;
  1580. }
  1581. if (JavascriptOperators::GetTypeId(args[1]) == TypeIds_Undefined)
  1582. {
  1583. JavascriptArray* ary = scriptContext->GetLibrary()->CreateArray(1);
  1584. ary->DirectSetItemAt(0, input);
  1585. return ary;
  1586. }
  1587. return RegexHelper::StringSplit(separator, input, limit);
  1588. }
  1589. }
  1590. }
  1591. Var JavascriptString::EntrySubstring(RecyclableObject* function, CallInfo callInfo, ...)
  1592. {
  1593. PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
  1594. ARGUMENTS(args, callInfo);
  1595. ScriptContext* scriptContext = function->GetScriptContext();
  1596. Assert(!(callInfo.Flags & CallFlags_New));
  1597. JavascriptString * pThis = nullptr;
  1598. GetThisStringArgument(args, scriptContext, _u("String.prototype.substring"), &pThis);
  1599. int len = pThis->GetLength();
  1600. int idxStart = 0;
  1601. int idxEnd = len;
  1602. if (args.Info.Count > 1)
  1603. {
  1604. idxStart = JavascriptOperators::IsUndefinedObject(args[1], scriptContext) ? 0 : ConvertToIndex(args[1], scriptContext);
  1605. if (args.Info.Count > 2)
  1606. {
  1607. idxEnd = JavascriptOperators::IsUndefinedObject(args[2], scriptContext) ? len : ConvertToIndex(args[2], scriptContext);
  1608. }
  1609. }
  1610. idxStart = min(max(idxStart, 0), len);
  1611. idxEnd = min(max(idxEnd, 0), len);
  1612. if(idxEnd < idxStart)
  1613. {
  1614. //swap
  1615. idxStart ^= idxEnd;
  1616. idxEnd ^= idxStart;
  1617. idxStart ^= idxEnd;
  1618. }
  1619. if (idxStart == 0 && idxEnd == len)
  1620. {
  1621. //return the string if we need to substring entire span
  1622. return pThis;
  1623. }
  1624. return SubstringCore(pThis, idxStart, idxEnd - idxStart, scriptContext);
  1625. }
  1626. Var JavascriptString::EntrySubstr(RecyclableObject* function, CallInfo callInfo, ...)
  1627. {
  1628. PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
  1629. ARGUMENTS(args, callInfo);
  1630. ScriptContext* scriptContext = function->GetScriptContext();
  1631. Assert(!(callInfo.Flags & CallFlags_New));
  1632. JavascriptString * pThis = nullptr;
  1633. GetThisStringArgument(args, scriptContext, _u("String.prototype.substr"), &pThis);
  1634. int len = pThis->GetLength();
  1635. int idxStart = 0;
  1636. int idxEnd = len;
  1637. if (args.Info.Count > 1)
  1638. {
  1639. idxStart = JavascriptOperators::IsUndefinedObject(args[1], scriptContext) ? 0 : ConvertToIndex(args[1], scriptContext);
  1640. if (args.Info.Count > 2)
  1641. {
  1642. idxEnd = JavascriptOperators::IsUndefinedObject(args[2], scriptContext) ? len : ConvertToIndex(args[2], scriptContext);
  1643. }
  1644. }
  1645. if (idxStart < 0)
  1646. {
  1647. idxStart = max(len + idxStart, 0);
  1648. }
  1649. else if (idxStart > len)
  1650. {
  1651. idxStart = len;
  1652. }
  1653. if (idxEnd < 0)
  1654. {
  1655. idxEnd = idxStart;
  1656. }
  1657. else if (idxEnd > len - idxStart)
  1658. {
  1659. idxEnd = len;
  1660. }
  1661. else
  1662. {
  1663. idxEnd += idxStart;
  1664. }
  1665. if (idxStart == 0 && idxEnd == len)
  1666. {
  1667. //return the string if we need to substr entire span
  1668. return pThis;
  1669. }
  1670. Assert(0 <= idxStart && idxStart <= idxEnd && idxEnd <= len);
  1671. return SubstringCore(pThis, idxStart, idxEnd - idxStart, scriptContext);
  1672. }
  1673. Var JavascriptString::SubstringCore(JavascriptString* pThis, int idxStart, int span, ScriptContext* scriptContext)
  1674. {
  1675. return SubString::New(pThis, idxStart, span);
  1676. }
  1677. Var JavascriptString::EntryPadStart(RecyclableObject* function, CallInfo callInfo, ...)
  1678. {
  1679. PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
  1680. ARGUMENTS(args, callInfo);
  1681. ScriptContext* scriptContext = function->GetScriptContext();
  1682. Assert(!(callInfo.Flags & CallFlags_New));
  1683. CHAKRATEL_LANGSTATS_INC_BUILTINCOUNT(PadStartCount);
  1684. JavascriptString * pThis = nullptr;
  1685. GetThisStringArgument(args, scriptContext, _u("String.prototype.padStart"), &pThis);
  1686. return PadCore(args, pThis, true /*isPadStart*/, scriptContext);
  1687. }
  1688. Var JavascriptString::EntryPadEnd(RecyclableObject* function, CallInfo callInfo, ...)
  1689. {
  1690. PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
  1691. ARGUMENTS(args, callInfo);
  1692. ScriptContext* scriptContext = function->GetScriptContext();
  1693. Assert(!(callInfo.Flags & CallFlags_New));
  1694. CHAKRATEL_LANGSTATS_INC_BUILTINCOUNT(PadEndCount);
  1695. JavascriptString * pThis = nullptr;
  1696. GetThisStringArgument(args, scriptContext, _u("String.prototype.padEnd"), &pThis);
  1697. return PadCore(args, pThis, false /*isPadStart*/, scriptContext);
  1698. }
  1699. JavascriptString* JavascriptString::PadCore(ArgumentReader& args, JavascriptString *mainString, bool isPadStart, ScriptContext* scriptContext)
  1700. {
  1701. Assert(mainString != nullptr);
  1702. Assert(args.Info.Count > 0);
  1703. if (args.Info.Count == 1)
  1704. {
  1705. return mainString;
  1706. }
  1707. int64 maxLength = JavascriptConversion::ToLength(args[1], scriptContext);
  1708. charcount_t currentLength = mainString->GetLength();
  1709. if (maxLength <= currentLength)
  1710. {
  1711. return mainString;
  1712. }
  1713. if (maxLength > JavascriptString::MaxCharLength)
  1714. {
  1715. Throw::OutOfMemory();
  1716. }
  1717. JavascriptString * fillerString = nullptr;
  1718. if (args.Info.Count > 2 && !JavascriptOperators::IsUndefinedObject(args[2], scriptContext))
  1719. {
  1720. JavascriptString *argStr = JavascriptConversion::ToString(args[2], scriptContext);
  1721. if (argStr->GetLength() > 0)
  1722. {
  1723. fillerString = argStr;
  1724. }
  1725. }
  1726. if (fillerString == nullptr)
  1727. {
  1728. fillerString = NewWithBuffer(_u(" "), 1, scriptContext);
  1729. }
  1730. Assert(fillerString->GetLength() > 0);
  1731. charcount_t fillLength = (charcount_t)(maxLength - currentLength);
  1732. charcount_t count = fillLength / fillerString->GetLength();
  1733. JavascriptString * finalPad = scriptContext->GetLibrary()->GetEmptyString();
  1734. if (count > 0)
  1735. {
  1736. finalPad = RepeatCore(fillerString, count, scriptContext);
  1737. fillLength -= (count * fillerString->GetLength());
  1738. }
  1739. if (fillLength > 0)
  1740. {
  1741. finalPad = Concat(finalPad, SubString::New(fillerString, 0, fillLength));
  1742. }
  1743. return isPadStart ? Concat(finalPad, mainString) : Concat(mainString, finalPad);
  1744. }
  1745. Var JavascriptString::EntryToLocaleLowerCase(RecyclableObject* function, CallInfo callInfo, ...)
  1746. {
  1747. PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
  1748. ARGUMENTS(args, callInfo);
  1749. ScriptContext* scriptContext = function->GetScriptContext();
  1750. Assert(!(callInfo.Flags & CallFlags_New));
  1751. return ToLocaleCaseHelper(args[0], false, scriptContext);
  1752. }
  1753. Var JavascriptString::EntryToLocaleUpperCase(RecyclableObject* function, CallInfo callInfo, ...)
  1754. {
  1755. PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
  1756. ARGUMENTS(args, callInfo);
  1757. ScriptContext* scriptContext = function->GetScriptContext();
  1758. Assert(!(callInfo.Flags & CallFlags_New));
  1759. return ToLocaleCaseHelper(args[0], true, scriptContext);
  1760. }
  1761. Var JavascriptString::EntryToLowerCase(RecyclableObject* function, CallInfo callInfo, ...)
  1762. {
  1763. PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
  1764. ARGUMENTS(args, callInfo);
  1765. ScriptContext* scriptContext = function->GetScriptContext();
  1766. Assert(!(callInfo.Flags & CallFlags_New));
  1767. JavascriptString * pThis = nullptr;
  1768. GetThisStringArgument(args, scriptContext, _u("String.prototype.toLowerCase"), &pThis);
  1769. // Fast path for one character strings
  1770. if (pThis->GetLength() == 1)
  1771. {
  1772. char16 inChar = pThis->GetString()[0];
  1773. char16 outChar = inChar;
  1774. #if DBG
  1775. DWORD converted =
  1776. #endif
  1777. CharLowerBuffW(&outChar, 1);
  1778. Assert(converted == 1);
  1779. return (inChar == outChar) ? pThis : scriptContext->GetLibrary()->GetCharStringCache().GetStringForChar(outChar);
  1780. }
  1781. return ToCaseCore(pThis, ToLower);
  1782. }
  1783. Var JavascriptString::EntryToString(RecyclableObject* function, CallInfo callInfo, ...)
  1784. {
  1785. PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
  1786. ARGUMENTS(args, callInfo);
  1787. ScriptContext* scriptContext = function->GetScriptContext();
  1788. Assert(!(callInfo.Flags & CallFlags_New));
  1789. if(args.Info.Count == 0)
  1790. {
  1791. JavascriptError::ThrowTypeError(scriptContext, JSERR_This_NeedString, _u("String.prototype.toString"));
  1792. }
  1793. AssertMsg(args.Info.Count > 0, "Negative argument count");
  1794. JavascriptString* str = nullptr;
  1795. if (!GetThisValueVar(args[0], &str, scriptContext))
  1796. {
  1797. if (JavascriptOperators::GetTypeId(args[0]) == TypeIds_HostDispatch)
  1798. {
  1799. Var result;
  1800. if (RecyclableObject::FromVar(args[0])->InvokeBuiltInOperationRemotely(EntryToString, args, &result))
  1801. {
  1802. return result;
  1803. }
  1804. }
  1805. JavascriptError::ThrowTypeError(scriptContext, JSERR_This_NeedString, _u("String.prototype.toString"));
  1806. }
  1807. return str;
  1808. }
  1809. Var JavascriptString::EntryToUpperCase(RecyclableObject* function, CallInfo callInfo, ...)
  1810. {
  1811. PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
  1812. ARGUMENTS(args, callInfo);
  1813. ScriptContext* scriptContext = function->GetScriptContext();
  1814. Assert(!(callInfo.Flags & CallFlags_New));
  1815. JavascriptString* pThis = nullptr;
  1816. GetThisStringArgument(args, scriptContext, _u("String.prototype.toUpperCase"), &pThis);
  1817. // Fast path for one character strings
  1818. if (pThis->GetLength() == 1)
  1819. {
  1820. char16 inChar = pThis->GetString()[0];
  1821. char16 outChar = inChar;
  1822. #if DBG
  1823. DWORD converted =
  1824. #endif
  1825. CharUpperBuffW(&outChar, 1);
  1826. Assert(converted == 1);
  1827. return (inChar == outChar) ? pThis : scriptContext->GetLibrary()->GetCharStringCache().GetStringForChar(outChar);
  1828. }
  1829. return ToCaseCore(pThis, ToUpper);
  1830. }
  1831. Var JavascriptString::ToCaseCore(JavascriptString* pThis, ToCase toCase)
  1832. {
  1833. charcount_t count = pThis->GetLength();
  1834. BufferStringBuilder builder(count, pThis->type->GetScriptContext());
  1835. const char16 *inStr = pThis->GetString();
  1836. char16 *outStr = builder.DangerousGetWritableBuffer();
  1837. char16* outStrLim = outStr + count;
  1838. char16 *o = outStr;
  1839. while (o < outStrLim)
  1840. {
  1841. *o++ = *inStr++;
  1842. }
  1843. if(toCase == ToUpper)
  1844. {
  1845. #if DBG
  1846. DWORD converted =
  1847. #endif
  1848. CharUpperBuffW(outStr, count);
  1849. Assert(converted == count);
  1850. }
  1851. else
  1852. {
  1853. Assert(toCase == ToLower);
  1854. #if DBG
  1855. DWORD converted =
  1856. #endif
  1857. CharLowerBuffW(outStr, count);
  1858. Assert(converted == count);
  1859. }
  1860. return builder.ToString();
  1861. }
  1862. Var JavascriptString::EntryTrim(RecyclableObject* function, CallInfo callInfo, ...)
  1863. {
  1864. PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
  1865. ARGUMENTS(args, callInfo);
  1866. ScriptContext* scriptContext = function->GetScriptContext();
  1867. CHAKRATEL_LANGSTATS_INC_BUILTINCOUNT(StringTrimCount);
  1868. Assert(!(callInfo.Flags & CallFlags_New));
  1869. //15.5.4.20 The following steps are taken:
  1870. //1. Call CheckObjectCoercible passing the this value as its argument.
  1871. //2. Let S be the result of calling ToString, giving it the this value as its argument.
  1872. //3. Let T be a string value that is a copy of S with both leading and trailing white space removed. The definition of white space is the union of WhiteSpace and LineTerminator.
  1873. //4. Return T.
  1874. JavascriptString* pThis = nullptr;
  1875. GetThisStringArgument(args, scriptContext, _u("String.prototype.trim"), &pThis);
  1876. return TrimLeftRightHelper<true /*trimLeft*/, true /*trimRight*/>(pThis, scriptContext);
  1877. }
  1878. Var JavascriptString::EntryTrimLeft(RecyclableObject* function, CallInfo callInfo, ...)
  1879. {
  1880. PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
  1881. ARGUMENTS(args, callInfo);
  1882. ScriptContext* scriptContext = function->GetScriptContext();
  1883. Assert(!(callInfo.Flags & CallFlags_New));
  1884. // 1.Let O be CheckObjectCoercible(this value) .
  1885. // 2.Let S be ToString(O) .
  1886. // 3.ReturnIfAbrupt(S).
  1887. // 4.Let T be a String value that is a copy of S with leading white space removed. The definition of white space is the union of WhiteSpace and )LineTerminator.
  1888. // When determining whether a Unicode code point is in Unicode general category "Zs", code unit sequences are interpreted as UTF-16 encoded code point sequences as specified in 6.1.4.
  1889. // 5.Return T.
  1890. JavascriptString* pThis = nullptr;
  1891. GetThisStringArgument(args, scriptContext, _u("String.prototype.trimLeft"), &pThis);
  1892. return TrimLeftRightHelper< true /*trimLeft*/, false /*trimRight*/>(pThis, scriptContext);
  1893. }
  1894. Var JavascriptString::EntryTrimRight(RecyclableObject* function, CallInfo callInfo, ...)
  1895. {
  1896. PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
  1897. ARGUMENTS(args, callInfo);
  1898. ScriptContext* scriptContext = function->GetScriptContext();
  1899. Assert(!(callInfo.Flags & CallFlags_New));
  1900. // 1.Let O be CheckObjectCoercible(this value) .
  1901. // 2.Let S be ToString(O) .
  1902. // 3.ReturnIfAbrupt(S).
  1903. // 4.Let T be a String value that is a copy of S with trailing white space removed.The definition of white space is the union of WhiteSpace and )LineTerminator.
  1904. // When determining whether a Unicode code point is in Unicode general category "Zs", code unit sequences are interpreted as UTF - 16 encoded code point sequences as specified in 6.1.4.
  1905. // 5.Return T.
  1906. JavascriptString* pThis = nullptr;
  1907. GetThisStringArgument(args, scriptContext, _u("String.prototype.trimRight"), &pThis);
  1908. return TrimLeftRightHelper<false /*trimLeft*/, true /*trimRight*/>(pThis, scriptContext);
  1909. }
  1910. template <bool trimLeft, bool trimRight>
  1911. Var JavascriptString::TrimLeftRightHelper(JavascriptString* arg, ScriptContext* scriptContext)
  1912. {
  1913. static_assert(trimLeft || trimRight, "bad template instance of TrimLeftRightHelper()");
  1914. int len = arg->GetLength();
  1915. const char16 *string = arg->GetString();
  1916. int idxStart = 0;
  1917. if (trimLeft)
  1918. {
  1919. for (; idxStart < len; idxStart++)
  1920. {
  1921. char16 ch = string[idxStart];
  1922. if (IsWhiteSpaceCharacter(ch))
  1923. {
  1924. continue;
  1925. }
  1926. break;
  1927. }
  1928. if (len == idxStart)
  1929. {
  1930. return (scriptContext->GetLibrary()->GetEmptyString());
  1931. }
  1932. }
  1933. int idxEnd = len - 1;
  1934. if (trimRight)
  1935. {
  1936. for (; idxEnd >= 0; idxEnd--)
  1937. {
  1938. char16 ch = string[idxEnd];
  1939. if (IsWhiteSpaceCharacter(ch))
  1940. {
  1941. continue;
  1942. }
  1943. break;
  1944. }
  1945. if (!trimLeft)
  1946. {
  1947. if (idxEnd < 0)
  1948. {
  1949. Assert(idxEnd == -1);
  1950. return (scriptContext->GetLibrary()->GetEmptyString());
  1951. }
  1952. }
  1953. else
  1954. {
  1955. Assert(idxEnd >= 0);
  1956. }
  1957. }
  1958. return SubstringCore(arg, idxStart, idxEnd - idxStart + 1, scriptContext);
  1959. }
  1960. ///----------------------------------------------------------------------------
  1961. /// Repeat() returns a new string equal to the toString(this) repeated n times,
  1962. /// as described in (ES6.0: S21.1.3.13).
  1963. ///----------------------------------------------------------------------------
  1964. Var JavascriptString::EntryRepeat(RecyclableObject* function, CallInfo callInfo, ...)
  1965. {
  1966. PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
  1967. ARGUMENTS(args, callInfo);
  1968. AssertMsg(args.Info.Count > 0, "Should always have implicit 'this'");
  1969. ScriptContext* scriptContext = function->GetScriptContext();
  1970. Assert(!(callInfo.Flags & CallFlags_New));
  1971. CHAKRATEL_LANGSTATS_INC_BUILTINCOUNT(RepeatCount);
  1972. JavascriptString* pThis = nullptr;
  1973. GetThisStringArgument(args, scriptContext, _u("String.prototype.repeat"), &pThis);
  1974. charcount_t count = 0;
  1975. if (args.Info.Count > 1)
  1976. {
  1977. if (!JavascriptOperators::IsUndefinedObject(args[1], scriptContext))
  1978. {
  1979. double countDbl = JavascriptConversion::ToInteger(args[1], scriptContext);
  1980. if (JavascriptNumber::IsPosInf(countDbl) || countDbl < 0.0)
  1981. {
  1982. JavascriptError::ThrowRangeError(scriptContext, JSERR_ArgumentOutOfRange, _u("String.prototype.repeat"));
  1983. }
  1984. count = NumberUtilities::LuFromDblNearest(countDbl);
  1985. }
  1986. }
  1987. if (count == 0 || pThis->GetLength() == 0)
  1988. {
  1989. return scriptContext->GetLibrary()->GetEmptyString();
  1990. }
  1991. else if (count == 1)
  1992. {
  1993. return pThis;
  1994. }
  1995. return RepeatCore(pThis, count, scriptContext);
  1996. }
  1997. JavascriptString* JavascriptString::RepeatCore(JavascriptString* currentString, charcount_t count, ScriptContext* scriptContext)
  1998. {
  1999. Assert(currentString != nullptr);
  2000. Assert(currentString->GetLength() > 0);
  2001. Assert(count > 0);
  2002. const char16* currentRawString = currentString->GetString();
  2003. int currentLength = currentString->GetLength();
  2004. charcount_t finalBufferCount = UInt32Math::Add(UInt32Math::Mul(count, currentLength), 1);
  2005. char16* buffer = RecyclerNewArrayLeaf(scriptContext->GetRecycler(), char16, finalBufferCount);
  2006. if (currentLength == 1)
  2007. {
  2008. wmemset(buffer, currentRawString[0], finalBufferCount - 1);
  2009. buffer[finalBufferCount - 1] = '\0';
  2010. }
  2011. else
  2012. {
  2013. char16* bufferDst = buffer;
  2014. size_t bufferDstSize = finalBufferCount;
  2015. for (charcount_t i = 0; i < count; i += 1)
  2016. {
  2017. js_wmemcpy_s(bufferDst, bufferDstSize, currentRawString, currentLength);
  2018. bufferDst += currentLength;
  2019. bufferDstSize -= currentLength;
  2020. }
  2021. Assert(bufferDstSize == 1);
  2022. *bufferDst = '\0';
  2023. }
  2024. return JavascriptString::NewWithBuffer(buffer, finalBufferCount - 1, scriptContext);
  2025. }
  2026. ///----------------------------------------------------------------------------
  2027. /// StartsWith() returns true if the given string matches the beginning of the
  2028. /// substring starting at the given position in toString(this), as described
  2029. /// in (ES6.0: S21.1.3.18).
  2030. ///----------------------------------------------------------------------------
  2031. Var JavascriptString::EntryStartsWith(RecyclableObject* function, CallInfo callInfo, ...)
  2032. {
  2033. PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
  2034. ARGUMENTS(args, callInfo);
  2035. AssertMsg(args.Info.Count > 0, "Should always have implicit 'this'");
  2036. ScriptContext* scriptContext = function->GetScriptContext();
  2037. Assert(!(callInfo.Flags & CallFlags_New));
  2038. CHAKRATEL_LANGSTATS_INC_BUILTINCOUNT(StartsWithCount);
  2039. JavascriptString * pThis;
  2040. JavascriptString * pSearch;
  2041. GetThisAndSearchStringArguments(args, scriptContext, _u("String.prototype.startsWith"), &pThis, &pSearch, false);
  2042. const char16* thisStr = pThis->GetString();
  2043. int thisStrLen = pThis->GetLength();
  2044. const char16* searchStr = pSearch->GetString();
  2045. int searchStrLen = pSearch->GetLength();
  2046. int startPosition = 0;
  2047. if (args.Info.Count > 2)
  2048. {
  2049. if (!JavascriptOperators::IsUndefinedObject(args[2], scriptContext))
  2050. {
  2051. startPosition = ConvertToIndex(args[2], scriptContext); // this is to adjust corner cases like MAX_VALUE
  2052. startPosition = min(max(startPosition, 0), thisStrLen);
  2053. }
  2054. }
  2055. // Avoid signed 32-bit int overflow if startPosition is large by subtracting searchStrLen from thisStrLen instead of
  2056. // adding searchStrLen and startPosition. The subtraction cannot underflow because maximum string length is
  2057. // MaxCharCount == INT_MAX-1. I.e. the RHS can be == 0 - (INT_MAX-1) == 1 - INT_MAX which would not underflow.
  2058. if (startPosition <= thisStrLen - searchStrLen)
  2059. {
  2060. Assert(searchStrLen <= thisStrLen - startPosition);
  2061. if (wmemcmp(thisStr + startPosition, searchStr, searchStrLen) == 0)
  2062. {
  2063. return scriptContext->GetLibrary()->GetTrue();
  2064. }
  2065. }
  2066. return scriptContext->GetLibrary()->GetFalse();
  2067. }
  2068. ///----------------------------------------------------------------------------
  2069. /// EndsWith() returns true if the given string matches the end of the
  2070. /// substring ending at the given position in toString(this), as described
  2071. /// in (ES6.0: S21.1.3.7).
  2072. ///----------------------------------------------------------------------------
  2073. Var JavascriptString::EntryEndsWith(RecyclableObject* function, CallInfo callInfo, ...)
  2074. {
  2075. PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
  2076. ARGUMENTS(args, callInfo);
  2077. AssertMsg(args.Info.Count > 0, "Should always have implicit 'this'");
  2078. ScriptContext* scriptContext = function->GetScriptContext();
  2079. Assert(!(callInfo.Flags & CallFlags_New));
  2080. CHAKRATEL_LANGSTATS_INC_BUILTINCOUNT(EndsWithCount);
  2081. JavascriptString * pThis;
  2082. JavascriptString * pSearch;
  2083. GetThisAndSearchStringArguments(args, scriptContext, _u("String.prototype.endsWith"), &pThis, &pSearch, false);
  2084. const char16* thisStr = pThis->GetString();
  2085. int thisStrLen = pThis->GetLength();
  2086. const char16* searchStr = pSearch->GetString();
  2087. int searchStrLen = pSearch->GetLength();
  2088. int endPosition = thisStrLen;
  2089. if (args.Info.Count > 2)
  2090. {
  2091. if (!JavascriptOperators::IsUndefinedObject(args[2], scriptContext))
  2092. {
  2093. endPosition = ConvertToIndex(args[2], scriptContext); // this is to adjust corner cases like MAX_VALUE
  2094. endPosition = min(max(endPosition, 0), thisStrLen);
  2095. }
  2096. }
  2097. int startPosition = endPosition - searchStrLen;
  2098. if (startPosition >= 0)
  2099. {
  2100. Assert(startPosition <= thisStrLen);
  2101. Assert(searchStrLen <= thisStrLen - startPosition);
  2102. if (wmemcmp(thisStr + startPosition, searchStr, searchStrLen) == 0)
  2103. {
  2104. return scriptContext->GetLibrary()->GetTrue();
  2105. }
  2106. }
  2107. return scriptContext->GetLibrary()->GetFalse();
  2108. }
  2109. ///----------------------------------------------------------------------------
  2110. /// Includes() returns true if the given string matches any substring (of the
  2111. /// same length) of the substring starting at the given position in
  2112. /// toString(this), as described in (ES6.0 (draft 33): S21.1.3.7).
  2113. ///----------------------------------------------------------------------------
  2114. Var JavascriptString::EntryIncludes(RecyclableObject* function, CallInfo callInfo, ...)
  2115. {
  2116. PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
  2117. ARGUMENTS(args, callInfo);
  2118. AssertMsg(args.Info.Count > 0, "Should always have implicit 'this'");
  2119. ScriptContext* scriptContext = function->GetScriptContext();
  2120. Assert(!(callInfo.Flags & CallFlags_New));
  2121. CHAKRATEL_LANGSTATS_INC_BUILTINCOUNT(ContainsCount);
  2122. return JavascriptBoolean::ToVar(IndexOf(args, scriptContext, _u("String.prototype.includes"), false) != -1, scriptContext);
  2123. }
  2124. Var JavascriptString::EntryValueOf(RecyclableObject* function, CallInfo callInfo, ...)
  2125. {
  2126. PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
  2127. ARGUMENTS(args, callInfo);
  2128. ScriptContext* scriptContext = function->GetScriptContext();
  2129. Assert(!(callInfo.Flags & CallFlags_New));
  2130. if(args.Info.Count == 0)
  2131. {
  2132. JavascriptError::ThrowTypeError(scriptContext, JSERR_This_NeedString, _u("String.prototype.valueOf"));
  2133. }
  2134. AssertMsg(args.Info.Count > 0, "Negative argument count");
  2135. JavascriptString* str = nullptr;
  2136. if (!GetThisValueVar(args[0], &str, scriptContext))
  2137. {
  2138. if (JavascriptOperators::GetTypeId(args[0]) == TypeIds_HostDispatch)
  2139. {
  2140. Var result;
  2141. if (RecyclableObject::FromVar(args[0])->InvokeBuiltInOperationRemotely(EntryValueOf, args, &result))
  2142. {
  2143. return result;
  2144. }
  2145. }
  2146. JavascriptError::ThrowTypeError(scriptContext, JSERR_This_NeedString, _u("String.prototype.valueOf"));
  2147. }
  2148. return str;
  2149. }
  2150. Var JavascriptString::EntrySymbolIterator(RecyclableObject* function, CallInfo callInfo, ...)
  2151. {
  2152. PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
  2153. ARGUMENTS(args, callInfo);
  2154. ScriptContext* scriptContext = function->GetScriptContext();
  2155. Assert(!(callInfo.Flags & CallFlags_New));
  2156. if (args.Info.Count == 0)
  2157. {
  2158. JavascriptError::ThrowTypeError(scriptContext, JSERR_This_NeedString, _u("String.prototype[Symbol.iterator]"));
  2159. }
  2160. AssertMsg(args.Info.Count > 0, "Negative argument count");
  2161. if (!JavascriptConversion::CheckObjectCoercible(args[0], scriptContext))
  2162. {
  2163. JavascriptError::ThrowTypeError(scriptContext, JSERR_This_NullOrUndefined, _u("String.prototype[Symbol.iterator]"));
  2164. }
  2165. JavascriptString* str = JavascriptConversion::ToString(args[0], scriptContext);
  2166. return scriptContext->GetLibrary()->CreateStringIterator(str);
  2167. }
  2168. const char16 * JavascriptString::GetSz()
  2169. {
  2170. Assert(m_pszValue[m_charLength] == _u('\0'));
  2171. return m_pszValue;
  2172. }
  2173. const char16 * JavascriptString::GetString()
  2174. {
  2175. if (!this->IsFinalized())
  2176. {
  2177. this->GetSz();
  2178. Assert(m_pszValue);
  2179. }
  2180. return m_pszValue;
  2181. }
  2182. void const * JavascriptString::GetOriginalStringReference()
  2183. {
  2184. // Just return the string buffer
  2185. return GetString();
  2186. }
  2187. size_t JavascriptString::GetAllocatedByteCount() const
  2188. {
  2189. if (!this->IsFinalized())
  2190. {
  2191. return 0;
  2192. }
  2193. return this->m_charLength * sizeof(WCHAR);
  2194. }
  2195. bool JavascriptString::IsSubstring() const
  2196. {
  2197. return false;
  2198. }
  2199. bool JavascriptString::IsNegZero(JavascriptString *string)
  2200. {
  2201. return string->GetLength() == 2 && wmemcmp(string->GetString(), _u("-0"), 2) == 0;
  2202. }
  2203. void JavascriptString::FinishCopy(__inout_xcount(m_charLength) char16 *const buffer, StringCopyInfoStack &nestedStringTreeCopyInfos)
  2204. {
  2205. while (!nestedStringTreeCopyInfos.IsEmpty())
  2206. {
  2207. const StringCopyInfo copyInfo(nestedStringTreeCopyInfos.Pop());
  2208. Assert(copyInfo.SourceString()->GetLength() <= GetLength());
  2209. Assert(copyInfo.DestinationBuffer() >= buffer);
  2210. Assert(copyInfo.DestinationBuffer() <= buffer + (GetLength() - copyInfo.SourceString()->GetLength()));
  2211. copyInfo.SourceString()->Copy(copyInfo.DestinationBuffer(), nestedStringTreeCopyInfos, 0);
  2212. }
  2213. }
  2214. void JavascriptString::CopyVirtual(
  2215. _Out_writes_(m_charLength) char16 *const buffer,
  2216. StringCopyInfoStack &nestedStringTreeCopyInfos,
  2217. const byte recursionDepth)
  2218. {
  2219. Assert(buffer);
  2220. Assert(!this->IsFinalized()); // CopyVirtual should only be called for unfinalized buffers
  2221. CopyHelper(buffer, GetString(), GetLength());
  2222. }
  2223. char16* JavascriptString::GetSzCopy()
  2224. {
  2225. return AllocateLeafAndCopySz(this->GetScriptContext()->GetRecycler(), GetString(), GetLength());
  2226. }
  2227. LPCWSTR JavascriptString::GetSzCopy(ArenaAllocator* alloc)
  2228. {
  2229. return AllocateAndCopySz(alloc, GetString(), GetLength());
  2230. }
  2231. /*
  2232. Table generated using the following:
  2233. var invalidValue = 37;
  2234. function toStringTable()
  2235. {
  2236. var stringTable = new Array(128);
  2237. for(var i = 0; i < 128; i++)
  2238. {
  2239. var ch = i;
  2240. if ('0'.charCodeAt(0) <= ch && '9'.charCodeAt(0) >= ch)
  2241. ch -= '0'.charCodeAt(0);
  2242. else if ('A'.charCodeAt(0) <= ch && 'Z'.charCodeAt(0) >= ch)
  2243. ch -= 'A'.charCodeAt(0) - 10;
  2244. else if ('a'.charCodeAt(0) <= ch && 'z'.charCodeAt(0) >= ch)
  2245. ch -= 'a'.charCodeAt(0) - 10;
  2246. else
  2247. ch = 37;
  2248. stringTable[i] = ch;
  2249. }
  2250. WScript.Echo("{" + stringTable + "}");
  2251. }
  2252. toStringTable();*/
  2253. const char JavascriptString::stringToIntegerMap[] = {
  2254. 37,37,37,37,37,37,37,37,37,37,37,37,37,37,37,37,37,37,37,37,37,37,37,37,37,37,37,37,37,37,37,37,37,37,37,37,37,37,37,37
  2255. ,37,37,37,37,37,37,37,37,0,1,2,3,4,5,6,7,8,9,37,37,37,37,37,37,37,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,
  2256. 28,29,30,31,32,33,34,35,37,37,37,37,37,37,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,
  2257. 37,37,37,37,37};
  2258. /*
  2259. Table generated using the following:
  2260. function logMaxUintTable()
  2261. {
  2262. var MAX_UINT = 4294967295;
  2263. var logTable = new Array(37);
  2264. logTable[0] = 0;
  2265. logTable[1] = 0;
  2266. for(var i = 2; i < logTable.length; i++)
  2267. {
  2268. logTable[i] = Math.floor(Math.log(MAX_UINT + 1) / Math.log(i));
  2269. }
  2270. WScript.Echo("{" + logTable + "}");
  2271. }
  2272. logMaxUintTable();
  2273. */
  2274. const uint8 JavascriptString::maxUintStringLengthTable[] =
  2275. { 0,0,32,20,16,13,12,11,10,10,9,9,8,8,8,8,8,7,7,7,7,7,7,7,6,6,6,6,6,6,6,6,6,6,6,6,6 };
  2276. // NumberUtil::FIntRadStrToDbl and parts of GlobalObject::EntryParseInt were refactored into ToInteger
  2277. Var JavascriptString::ToInteger(int radix)
  2278. {
  2279. AssertMsg(radix == 0 || radix >= 2 && radix <= 36, "'radix' is invalid");
  2280. const char16* pchStart = GetString();
  2281. const char16* pchEnd = pchStart + m_charLength;
  2282. const char16 *pch = this->GetScriptContext()->GetCharClassifier()->SkipWhiteSpace(pchStart, pchEnd);
  2283. bool isNegative = false;
  2284. switch (*pch)
  2285. {
  2286. case '-':
  2287. isNegative = true;
  2288. // Fall through.
  2289. case '+':
  2290. if(pch < pchEnd)
  2291. {
  2292. pch++;
  2293. }
  2294. break;
  2295. }
  2296. if (0 == radix)
  2297. {
  2298. if (pch < pchEnd && '0' != pch[0])
  2299. {
  2300. radix = 10;
  2301. }
  2302. else if (('x' == pch[1] || 'X' == pch[1]) && pchEnd - pch >= 2)
  2303. {
  2304. radix = 16;
  2305. pch += 2;
  2306. }
  2307. else
  2308. {
  2309. // ES5's 'parseInt' does not allow treating a string beginning with a '0' as an octal value. ES3 does not specify a
  2310. // behavior
  2311. radix = 10;
  2312. }
  2313. }
  2314. else if (16 == radix)
  2315. {
  2316. if('0' == pch[0] && ('x' == pch[1] || 'X' == pch[1]) && pchEnd - pch >= 2)
  2317. {
  2318. pch += 2;
  2319. }
  2320. }
  2321. Assert(radix <= _countof(maxUintStringLengthTable));
  2322. Assert(pchEnd >= pch);
  2323. size_t length = pchEnd - pch;
  2324. const char16 *const pchMin = pch;
  2325. __analysis_assume(radix < _countof(maxUintStringLengthTable));
  2326. if(length <= maxUintStringLengthTable[radix])
  2327. {
  2328. // Use uint32 as integer being parsed - much faster than BigInt
  2329. uint32 value = 0;
  2330. for ( ; pch < pchEnd ; pch++)
  2331. {
  2332. char16 ch = *pch;
  2333. if(ch >= _countof(stringToIntegerMap) || (ch = stringToIntegerMap[ch]) >= radix)
  2334. {
  2335. break;
  2336. }
  2337. uint32 beforeValue = value;
  2338. value = value * radix + ch;
  2339. AssertMsg(value >= beforeValue, "uint overflow");
  2340. }
  2341. if(pchMin == pch)
  2342. {
  2343. return GetScriptContext()->GetLibrary()->GetNaN();
  2344. }
  2345. if(isNegative)
  2346. {
  2347. // negative zero can only be represented by doubles
  2348. if(value <= INT_MAX && value != 0)
  2349. {
  2350. int32 result = -((int32)value);
  2351. return JavascriptNumber::ToVar(result, this->GetScriptContext());
  2352. }
  2353. double result = -((double)(value));
  2354. return JavascriptNumber::New(result, this->GetScriptContext());
  2355. }
  2356. return JavascriptNumber::ToVar(value, this->GetScriptContext());
  2357. }
  2358. BigInt bi;
  2359. for ( ; pch < pchEnd ; pch++)
  2360. {
  2361. char16 ch = *pch;
  2362. if(ch >= _countof(stringToIntegerMap) || (ch = stringToIntegerMap[ch]) >= radix)
  2363. {
  2364. break;
  2365. }
  2366. if (!bi.FMulAdd(radix, ch))
  2367. {
  2368. //Mimic IE8 which threw an OutOfMemory exception in this case.
  2369. JavascriptError::ThrowOutOfMemoryError(GetScriptContext());
  2370. }
  2371. // If we ever have more than 32 ulongs, the result must be infinite.
  2372. if (bi.Clu() > 32)
  2373. {
  2374. Var result = isNegative ?
  2375. GetScriptContext()->GetLibrary()->GetNegativeInfinite() :
  2376. GetScriptContext()->GetLibrary()->GetPositiveInfinite();
  2377. return result;
  2378. }
  2379. }
  2380. if (pchMin == pch)
  2381. {
  2382. return GetScriptContext()->GetLibrary()->GetNaN();
  2383. }
  2384. // Convert to a double.
  2385. double result = bi.GetDbl();
  2386. if(isNegative)
  2387. {
  2388. result = -result;
  2389. }
  2390. return Js::JavascriptNumber::ToVarIntCheck(result, GetScriptContext());
  2391. }
  2392. bool JavascriptString::ToDouble(double * result)
  2393. {
  2394. const char16* pch;
  2395. long len = this->m_charLength;
  2396. if (0 == len)
  2397. {
  2398. *result = 0;
  2399. return true;
  2400. }
  2401. if (1 == len && NumberUtilities::IsDigit(this->GetString()[0]))
  2402. {
  2403. *result = (double)(this->GetString()[0] - '0');
  2404. return true;
  2405. }
  2406. // TODO: Use GetString here instead of GetSz (need to modify DblFromHex and StrToDbl to take a length)
  2407. for (pch = this->GetSz(); IsWhiteSpaceCharacter(*pch); pch++)
  2408. ;
  2409. if (0 == *pch)
  2410. {
  2411. *result = 0;
  2412. return true;
  2413. }
  2414. bool isNumericLiteral = false;
  2415. if (*pch == '0')
  2416. {
  2417. const char16 *pchT = pch + 2;
  2418. switch (pch[1])
  2419. {
  2420. case 'x':
  2421. case 'X':
  2422. *result = NumberUtilities::DblFromHex(pchT, &pch);
  2423. isNumericLiteral = true;
  2424. break;
  2425. case 'o':
  2426. case 'O':
  2427. *result = NumberUtilities::DblFromOctal(pchT, &pch);
  2428. isNumericLiteral = true;
  2429. break;
  2430. case 'b':
  2431. case 'B':
  2432. *result = NumberUtilities::DblFromBinary(pchT, &pch);
  2433. isNumericLiteral = true;
  2434. break;
  2435. }
  2436. if (pchT == pch && isNumericLiteral)
  2437. {
  2438. *result = JavascriptNumber::NaN;
  2439. return false;
  2440. }
  2441. }
  2442. if (!isNumericLiteral)
  2443. {
  2444. *result = NumberUtilities::StrToDbl(pch, &pch, GetScriptContext());
  2445. }
  2446. while (IsWhiteSpaceCharacter(*pch))
  2447. pch++;
  2448. if (pch != this->m_pszValue + len)
  2449. {
  2450. *result = JavascriptNumber::NaN;
  2451. return false;
  2452. }
  2453. return true;
  2454. }
  2455. double JavascriptString::ToDouble()
  2456. {
  2457. double result;
  2458. this->ToDouble(&result);
  2459. return result;
  2460. }
  2461. bool JavascriptString::Equals(Var aLeft, Var aRight)
  2462. {
  2463. AssertMsg(JavascriptString::Is(aLeft) && JavascriptString::Is(aRight), "string comparison");
  2464. JavascriptString *leftString = JavascriptString::FromVar(aLeft);
  2465. JavascriptString *rightString = JavascriptString::FromVar(aRight);
  2466. if (leftString->GetLength() != rightString->GetLength())
  2467. {
  2468. return false;
  2469. }
  2470. if (wmemcmp(leftString->GetString(), rightString->GetString(), leftString->GetLength()) == 0)
  2471. {
  2472. return true;
  2473. }
  2474. return false;
  2475. }
  2476. //
  2477. // LessThan implements algorithm of ES5 11.8.5 step 4
  2478. // returns false for same string pattern
  2479. //
  2480. bool JavascriptString::LessThan(Var aLeft, Var aRight)
  2481. {
  2482. AssertMsg(JavascriptString::Is(aLeft) && JavascriptString::Is(aRight), "string LessThan");
  2483. JavascriptString *leftString = JavascriptString::FromVar(aLeft);
  2484. JavascriptString *rightString = JavascriptString::FromVar(aRight);
  2485. if (JavascriptString::strcmp(leftString, rightString) < 0)
  2486. {
  2487. return true;
  2488. }
  2489. return false;
  2490. }
  2491. // thisStringValue(value) abstract operation as defined in ES6.0 (Draft 25) Section 21.1.3
  2492. BOOL JavascriptString::GetThisValueVar(Var aValue, JavascriptString** pString, ScriptContext* scriptContext)
  2493. {
  2494. Assert(pString);
  2495. // 1. If Type(value) is String, return value.
  2496. if (JavascriptString::Is(aValue))
  2497. {
  2498. *pString = JavascriptString::FromVar(aValue);
  2499. return TRUE;
  2500. }
  2501. // 2. If Type(value) is Object and value has a [[StringData]] internal slot
  2502. else if ( JavascriptStringObject::Is(aValue))
  2503. {
  2504. JavascriptStringObject* pStringObj = JavascriptStringObject::FromVar(aValue);
  2505. // a. Let s be the value of value's [[StringData]] internal slot.
  2506. // b. If s is not undefined, then return s.
  2507. *pString = JavascriptString::FromVar(CrossSite::MarshalVar(scriptContext, pStringObj->Unwrap()));
  2508. return TRUE;
  2509. }
  2510. // 3. Throw a TypeError exception.
  2511. // Note: We don't throw a TypeError here, choosing to return FALSE and let the caller throw the error
  2512. return FALSE;
  2513. }
  2514. #ifdef TAGENTRY
  2515. #undef TAGENTRY
  2516. #endif
  2517. #define TAGENTRY(name, ...) \
  2518. Var JavascriptString::Entry##name(RecyclableObject* function, CallInfo callInfo, ...) \
  2519. { \
  2520. PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault); \
  2521. \
  2522. ARGUMENTS(args, callInfo); \
  2523. ScriptContext* scriptContext = function->GetScriptContext(); \
  2524. \
  2525. Assert(!(callInfo.Flags & CallFlags_New)); \
  2526. \
  2527. return StringBracketHelper(args, scriptContext, __VA_ARGS__); \
  2528. }
  2529. #include "JavascriptStringTagEntries.h"
  2530. #undef TAGENTRY
  2531. Var JavascriptString::StringBracketHelper(Arguments args, ScriptContext *scriptContext, __in_ecount(cchTag) char16 const *pszTag,
  2532. charcount_t cchTag, __in_ecount_opt(cchProp) char16 const *pszProp, charcount_t cchProp)
  2533. {
  2534. charcount_t cchThis;
  2535. charcount_t cchPropertyValue;
  2536. charcount_t cchTotalChars;
  2537. charcount_t ich;
  2538. JavascriptString * pThis;
  2539. JavascriptString * pPropertyValue = nullptr;
  2540. const char16 * propertyValueStr = nullptr;
  2541. uint quotesCount = 0;
  2542. const char16 quotStr[] = _u("&quot;");
  2543. const charcount_t quotStrLen = _countof(quotStr) - 1;
  2544. bool ES6FixesEnabled = scriptContext->GetConfig()->IsES6StringPrototypeFixEnabled();
  2545. // Assemble the component pieces of a string tag function (ex: String.prototype.link).
  2546. // In the general case, result is as below:
  2547. //
  2548. // pszProp = _u("href");
  2549. // pszTag = _u("a");
  2550. // pThis = JavascriptString::FromVar(args[0]);
  2551. // pPropertyValue = JavascriptString::FromVar(args[1]);
  2552. //
  2553. // pResult = _u("<a href=\"[[pPropertyValue]]\">[[pThis]]</a>");
  2554. //
  2555. // cchTotalChars = 5 // <></>
  2556. // + cchTag * 2 // a
  2557. // + cchProp // href
  2558. // + 4 // _=""
  2559. // + cchPropertyValue
  2560. // + cchThis;
  2561. //
  2562. // Note: With ES6FixesEnabled, we need to escape quote characters (_u('"')) in pPropertyValue.
  2563. // Note: Without ES6FixesEnabled, the tag and prop strings should be capitalized.
  2564. if(args.Info.Count == 0)
  2565. {
  2566. JavascriptError::ThrowTypeError(scriptContext, JSERR_NeedString);
  2567. }
  2568. if (ES6FixesEnabled)
  2569. {
  2570. if (!JavascriptConversion::CheckObjectCoercible(args[0], scriptContext))
  2571. {
  2572. JavascriptError::ThrowTypeError(scriptContext, JSERR_This_NullOrUndefined, pszTag);
  2573. }
  2574. }
  2575. if (JavascriptString::Is(args[0]))
  2576. {
  2577. pThis = JavascriptString::FromVar(args[0]);
  2578. }
  2579. else
  2580. {
  2581. pThis = JavascriptConversion::ToString(args[0], scriptContext);
  2582. }
  2583. cchThis = pThis->GetLength();
  2584. cchTotalChars = UInt32Math::Add(cchTag, cchTag);
  2585. // 5 is for the <></> characters
  2586. cchTotalChars = UInt32Math::Add(cchTotalChars, 5);
  2587. if (nullptr != pszProp)
  2588. {
  2589. // Need one string argument.
  2590. if (args.Info.Count >= 2)
  2591. {
  2592. if (JavascriptString::Is(args[1]))
  2593. {
  2594. pPropertyValue = JavascriptString::FromVar(args[1]);
  2595. }
  2596. else
  2597. {
  2598. pPropertyValue = JavascriptConversion::ToString(args[1], scriptContext);
  2599. }
  2600. }
  2601. else
  2602. {
  2603. pPropertyValue = scriptContext->GetLibrary()->GetUndefinedDisplayString();
  2604. }
  2605. cchPropertyValue = pPropertyValue->GetLength();
  2606. propertyValueStr = pPropertyValue->GetString();
  2607. if (ES6FixesEnabled)
  2608. {
  2609. // Count the number of " characters we need to escape.
  2610. for (ich = 0; ich < cchPropertyValue; ich++)
  2611. {
  2612. if (propertyValueStr[ich] == _u('"'))
  2613. {
  2614. ++quotesCount;
  2615. }
  2616. }
  2617. }
  2618. cchTotalChars = UInt32Math::Add(cchTotalChars, cchProp);
  2619. // 4 is for the _="" characters
  2620. cchTotalChars = UInt32Math::Add(cchTotalChars, 4);
  2621. if (ES6FixesEnabled)
  2622. {
  2623. // Account for the " escaping (&quot;)
  2624. cchTotalChars = UInt32Math::Add(cchTotalChars, UInt32Math::Mul(quotesCount, quotStrLen)) - quotesCount;
  2625. }
  2626. }
  2627. else
  2628. {
  2629. cchPropertyValue = 0;
  2630. cchProp = 0;
  2631. }
  2632. cchTotalChars = UInt32Math::Add(cchTotalChars, cchThis);
  2633. cchTotalChars = UInt32Math::Add(cchTotalChars, cchPropertyValue);
  2634. if (!IsValidCharCount(cchTotalChars) || cchTotalChars < cchThis || cchTotalChars < cchPropertyValue)
  2635. {
  2636. Js::JavascriptError::ThrowOutOfMemoryError(scriptContext);
  2637. }
  2638. BufferStringBuilder builder(cchTotalChars, scriptContext);
  2639. char16 *pResult = builder.DangerousGetWritableBuffer();
  2640. *pResult++ = _u('<');
  2641. for (ich = 0; ich < cchTag; ich++)
  2642. {
  2643. *pResult++ = ES6FixesEnabled ? pszTag[ich] : towupper(pszTag[ich]);
  2644. }
  2645. if (nullptr != pszProp)
  2646. {
  2647. *pResult++ = _u(' ');
  2648. for (ich = 0; ich < cchProp; ich++)
  2649. {
  2650. *pResult++ = ES6FixesEnabled ? pszProp[ich] : towupper(pszProp[ich]);
  2651. }
  2652. *pResult++ = _u('=');
  2653. *pResult++ = _u('"');
  2654. Assert(propertyValueStr != nullptr);
  2655. if (!ES6FixesEnabled || quotesCount == 0)
  2656. {
  2657. js_wmemcpy_s(pResult,
  2658. cchTotalChars - (pResult - builder.DangerousGetWritableBuffer() + 1),
  2659. propertyValueStr,
  2660. cchPropertyValue);
  2661. pResult += cchPropertyValue;
  2662. }
  2663. else {
  2664. for (ich = 0; ich < cchPropertyValue; ich++)
  2665. {
  2666. if (propertyValueStr[ich] == _u('"'))
  2667. {
  2668. charcount_t destLengthLeft = (cchTotalChars - (charcount_t)(pResult - builder.DangerousGetWritableBuffer() + 1));
  2669. // Copy the quote string into result beginning at the index where the quote would appear
  2670. js_wmemcpy_s(pResult,
  2671. destLengthLeft,
  2672. quotStr,
  2673. quotStrLen);
  2674. // Move result ahead by the length of the quote string
  2675. pResult += quotStrLen;
  2676. // We ate one of the quotes
  2677. quotesCount--;
  2678. // We only need to check to see if we have no more quotes after eating a quote
  2679. if (quotesCount == 0)
  2680. {
  2681. // Skip the quote character.
  2682. // Note: If ich is currently the last character (cchPropertyValue-1), it becomes cchPropertyValue after incrementing.
  2683. // At that point, cchPropertyValue - ich == 0 so we will not increment pResult and will call memcpy for zero bytes.
  2684. ich++;
  2685. // Copy the rest from the property value string starting at the index after the last quote
  2686. js_wmemcpy_s(pResult,
  2687. destLengthLeft - quotStrLen,
  2688. propertyValueStr + ich,
  2689. cchPropertyValue - ich);
  2690. // Move result ahead by the length of the rest of the property string
  2691. pResult += (cchPropertyValue - ich);
  2692. break;
  2693. }
  2694. }
  2695. else
  2696. {
  2697. // Each non-quote character just gets copied into result string
  2698. *pResult++ = propertyValueStr[ich];
  2699. }
  2700. }
  2701. }
  2702. *pResult++ = _u('"');
  2703. }
  2704. *pResult++ = _u('>');
  2705. const char16 *pThisString = pThis->GetString();
  2706. js_wmemcpy_s(pResult, cchTotalChars - (pResult - builder.DangerousGetWritableBuffer() + 1), pThisString, cchThis);
  2707. pResult += cchThis;
  2708. *pResult++ = _u('<');
  2709. *pResult++ = _u('/');
  2710. for (ich = 0; ich < cchTag; ich++)
  2711. {
  2712. *pResult++ = ES6FixesEnabled ? pszTag[ich] : towupper(pszTag[ich]);
  2713. }
  2714. *pResult++ = _u('>');
  2715. // Assert we ended at the right place.
  2716. AssertMsg((charcount_t)(pResult - builder.DangerousGetWritableBuffer()) == cchTotalChars, "Exceeded allocated string limit");
  2717. return builder.ToString();
  2718. }
  2719. Var JavascriptString::ToLocaleCaseHelper(Var thisObj, bool toUpper, ScriptContext *scriptContext)
  2720. {
  2721. JavascriptString * pThis;
  2722. if (JavascriptString::Is(thisObj))
  2723. {
  2724. pThis = JavascriptString::FromVar(thisObj);
  2725. }
  2726. else
  2727. {
  2728. pThis = JavascriptConversion::ToString(thisObj, scriptContext);
  2729. }
  2730. if (pThis->GetLength() == 0)
  2731. {
  2732. return pThis;
  2733. }
  2734. DWORD dwFlags = toUpper ? LCMAP_UPPERCASE : LCMAP_LOWERCASE;
  2735. dwFlags |= LCMAP_LINGUISTIC_CASING;
  2736. LCID lcid = GetUserDefaultLCID();
  2737. const char16* str = pThis->GetString();
  2738. // Get the number of chars in the mapped string.
  2739. int count = LCMapStringW(lcid, dwFlags, str, pThis->GetLength(), NULL, 0);
  2740. if (0 == count)
  2741. {
  2742. AssertMsg(FALSE, "LCMapString failed");
  2743. Throw::InternalError();
  2744. }
  2745. BufferStringBuilder builder(count, scriptContext);
  2746. char16 * stringBuffer = builder.DangerousGetWritableBuffer();
  2747. int count1 = LCMapStringW(lcid, dwFlags, str, count, stringBuffer, count);
  2748. if( 0 == count1 )
  2749. {
  2750. AssertMsg(FALSE, "LCMapString failed");
  2751. Throw::InternalError();
  2752. }
  2753. return builder.ToString();
  2754. }
  2755. int JavascriptString::IndexOfUsingJmpTable(JmpTable jmpTable, const char16* inputStr, int len, const char16* searchStr, int searchLen, int position)
  2756. {
  2757. int result = -1;
  2758. const char16 searchLast = searchStr[searchLen-1];
  2759. unsigned long lMatchedJump = searchLen;
  2760. if (jmpTable[searchLast].shift > 0)
  2761. {
  2762. lMatchedJump = jmpTable[searchLast].shift;
  2763. }
  2764. char16 const * p = inputStr + position + searchLen-1;
  2765. WCHAR c;
  2766. while(p < inputStr + len)
  2767. {
  2768. // first character match, keep checking
  2769. if (*p == searchLast)
  2770. {
  2771. if ( wmemcmp(p-searchLen+1, searchStr, searchLen) == 0 )
  2772. {
  2773. break;
  2774. }
  2775. p += lMatchedJump;
  2776. }
  2777. else
  2778. {
  2779. c = *p;
  2780. if ( 0 == ( c & ~0x7f ) && jmpTable[c].shift != 0 )
  2781. {
  2782. p += jmpTable[c].shift;
  2783. }
  2784. else
  2785. {
  2786. p += searchLen;
  2787. }
  2788. }
  2789. }
  2790. if (p >= inputStr+position && p < inputStr + len)
  2791. {
  2792. result = (int)(p - inputStr) - searchLen + 1;
  2793. }
  2794. return result;
  2795. }
  2796. int JavascriptString::LastIndexOfUsingJmpTable(JmpTable jmpTable, const char16* inputStr, int len, const char16* searchStr, int searchLen, int position)
  2797. {
  2798. const char16 searchFirst = searchStr[0];
  2799. unsigned long lMatchedJump = searchLen;
  2800. if (jmpTable[searchFirst].shift > 0)
  2801. {
  2802. lMatchedJump = jmpTable[searchFirst].shift;
  2803. }
  2804. WCHAR c;
  2805. char16 const * p = inputStr + min(len - searchLen, position);
  2806. while(p >= inputStr)
  2807. {
  2808. // first character match, keep checking
  2809. if (*p == searchFirst)
  2810. {
  2811. if ( wmemcmp(p, searchStr, searchLen) == 0 )
  2812. {
  2813. break;
  2814. }
  2815. p -= lMatchedJump;
  2816. }
  2817. else
  2818. {
  2819. c = *p;
  2820. if ( 0 == ( c & ~0x7f ) && jmpTable[c].shift != 0 )
  2821. {
  2822. p -= jmpTable[c].shift;
  2823. }
  2824. else
  2825. {
  2826. p -= searchLen;
  2827. }
  2828. }
  2829. }
  2830. return ((p >= inputStr) ? (int)(p - inputStr) : -1);
  2831. }
  2832. bool JavascriptString::BuildLastCharForwardBoyerMooreTable(JmpTable jmpTable, const char16* searchStr, int searchLen)
  2833. {
  2834. AssertMsg(searchLen >= 1, "Table for non-empty string");
  2835. memset(jmpTable, 0, sizeof(JmpTable));
  2836. const char16 * p2 = searchStr + searchLen - 1;
  2837. const char16 * const begin = searchStr;
  2838. // Determine if we can do a partial ASCII Boyer-Moore
  2839. while (p2 >= begin)
  2840. {
  2841. WCHAR c = *p2;
  2842. if ( 0 == ( c & ~0x7f ))
  2843. {
  2844. if ( jmpTable[c].shift == 0 )
  2845. {
  2846. jmpTable[c].shift = (unsigned long)(searchStr + searchLen - 1 - p2);
  2847. }
  2848. }
  2849. else
  2850. {
  2851. return false;
  2852. }
  2853. p2--;
  2854. }
  2855. return true;
  2856. }
  2857. bool JavascriptString::BuildFirstCharBackwardBoyerMooreTable(JmpTable jmpTable, const char16* searchStr, int searchLen)
  2858. {
  2859. AssertMsg(searchLen >= 1, "Table for non-empty string");
  2860. memset(jmpTable, 0, sizeof(JmpTable));
  2861. const char16 * p2 = searchStr;
  2862. const char16 * const end = searchStr + searchLen;
  2863. // Determine if we can do a partial ASCII Boyer-Moore
  2864. while (p2 < end)
  2865. {
  2866. WCHAR c = *p2;
  2867. if ( 0 == ( c & ~0x7f ))
  2868. {
  2869. if ( jmpTable[c].shift == 0 )
  2870. {
  2871. jmpTable[c].shift = (unsigned long)(p2 - searchStr);
  2872. }
  2873. }
  2874. else
  2875. {
  2876. return false;
  2877. }
  2878. p2++;
  2879. }
  2880. return true;
  2881. }
  2882. uint JavascriptString::strstr(JavascriptString *string, JavascriptString *substring, bool useBoyerMoore, uint start)
  2883. {
  2884. uint i;
  2885. const char16 *stringOrig = string->GetString();
  2886. uint stringLenOrig = string->GetLength();
  2887. const char16 *stringSz = stringOrig + start;
  2888. const char16 *substringSz = substring->GetString();
  2889. uint stringLen = stringLenOrig - start;
  2890. uint substringLen = substring->GetLength();
  2891. if (useBoyerMoore && substringLen > 2)
  2892. {
  2893. JmpTable jmpTable;
  2894. bool fAsciiJumpTable = BuildLastCharForwardBoyerMooreTable(jmpTable, substringSz, substringLen);
  2895. if (fAsciiJumpTable)
  2896. {
  2897. int result = IndexOfUsingJmpTable(jmpTable, stringOrig, stringLenOrig, substringSz, substringLen, start);
  2898. if (result != -1)
  2899. {
  2900. return result;
  2901. }
  2902. else
  2903. {
  2904. return (uint)-1;
  2905. }
  2906. }
  2907. }
  2908. if (stringLen >= substringLen)
  2909. {
  2910. // If substring is empty, it matches anything...
  2911. if (substringLen == 0)
  2912. {
  2913. return 0;
  2914. }
  2915. for (i = 0; i <= stringLen - substringLen; i++)
  2916. {
  2917. // Quick check for first character.
  2918. if (stringSz[i] == substringSz[0])
  2919. {
  2920. if (substringLen == 1 || memcmp(stringSz+i+1, substringSz+1, (substringLen-1)*sizeof(char16)) == 0)
  2921. {
  2922. return i + start;
  2923. }
  2924. }
  2925. }
  2926. }
  2927. return (uint)-1;
  2928. }
  2929. int JavascriptString::strcmp(JavascriptString *string1, JavascriptString *string2)
  2930. {
  2931. uint string1Len = string1->GetLength();
  2932. uint string2Len = string2->GetLength();
  2933. int result = wmemcmp(string1->GetString(), string2->GetString(), min(string1Len, string2Len));
  2934. return (result == 0) ? (int)(string1Len - string2Len) : result;
  2935. }
  2936. /*static*/ charcount_t JavascriptString::SafeSzSize(charcount_t cch)
  2937. {
  2938. // JavascriptString::MaxCharLength is valid; however, we are incrementing below by 1 and want to make sure we aren't overflowing
  2939. // Nor going outside of valid range.
  2940. if (cch >= JavascriptString::MaxCharLength)
  2941. {
  2942. Throw::OutOfMemory();
  2943. }
  2944. // Compute cch + 1, checking for overflow
  2945. ++cch;
  2946. return cch;
  2947. }
  2948. charcount_t JavascriptString::SafeSzSize() const
  2949. {
  2950. return SafeSzSize(GetLength());
  2951. }
  2952. /*static*/ __ecount(length+1) char16* JavascriptString::AllocateLeafAndCopySz(__in Recycler* recycler, __in_ecount(length) const char16* content, charcount_t length)
  2953. {
  2954. // Note: Intentionally not using SafeSzSize nor hoisting common
  2955. // sub-expression "length + 1" into a local variable otherwise
  2956. // Prefast gets confused and cannot track buffer's length.
  2957. // JavascriptString::MaxCharLength is valid; however, we are incrementing below by 1 and want to make sure we aren't overflowing
  2958. // Nor going outside of valid range.
  2959. if (length >= JavascriptString::MaxCharLength)
  2960. {
  2961. Throw::OutOfMemory();
  2962. }
  2963. charcount_t bufLen = length + 1;
  2964. // Allocate recycler memory to store the string plus a terminating NUL
  2965. char16* buffer = RecyclerNewArrayLeaf(recycler, char16, bufLen);
  2966. js_wmemcpy_s(buffer, bufLen, content, length);
  2967. buffer[length] = _u('\0');
  2968. return buffer;
  2969. }
  2970. /*static*/ __ecount(length+1) char16* JavascriptString::AllocateAndCopySz(__in ArenaAllocator* arena, __in_ecount(length) const char16* content, charcount_t length)
  2971. {
  2972. // Note: Intentionally not using SafeSzSize nor hoisting common
  2973. // sub-expression "length + 1" into a local variable otherwise
  2974. // Prefast gets confused and cannot track buffer's length.
  2975. // JavascriptString::MaxCharLength is valid; however, we are incrementing below by 1 and want to make sure we aren't overflowing
  2976. // Nor going outside of valid range.
  2977. if (length >= JavascriptString::MaxCharLength)
  2978. {
  2979. Throw::OutOfMemory();
  2980. }
  2981. // Allocate arena memory to store the string plus a terminating NUL
  2982. char16* buffer = AnewArray(arena, char16, length + 1);
  2983. js_wmemcpy_s(buffer, length + 1, content, length);
  2984. buffer[length] = _u('\0');
  2985. return buffer;
  2986. }
  2987. RecyclableObject * JavascriptString::CloneToScriptContext(ScriptContext* requestContext)
  2988. {
  2989. return JavascriptString::NewWithBuffer(this->GetSz(), this->GetLength(), requestContext);
  2990. }
  2991. charcount_t JavascriptString::ConvertToIndex(Var varIndex, ScriptContext *scriptContext)
  2992. {
  2993. if (TaggedInt::Is(varIndex))
  2994. {
  2995. return TaggedInt::ToInt32(varIndex);
  2996. }
  2997. return NumberUtilities::LwFromDblNearest(JavascriptConversion::ToInteger(varIndex, scriptContext));
  2998. }
  2999. char16* JavascriptString::GetNormalizedString(_NORM_FORM form, ArenaAllocator* tempAllocator, charcount_t& sizeOfNormalizedStringWithoutNullTerminator)
  3000. {
  3001. ScriptContext* scriptContext = this->GetScriptContext();
  3002. if (this->GetLength() == 0)
  3003. {
  3004. sizeOfNormalizedStringWithoutNullTerminator = 0;
  3005. return nullptr;
  3006. }
  3007. // IMPORTANT: Implementation Notes
  3008. // Normalize string estimates the required size of the buffer based on averages and other data.
  3009. // It is very hard to get a precise size from an input string without expanding/contracting it on the buffer.
  3010. // It is estimated that the maximum size the string after an NFC is 6x the input length, and 18x for NFD. This approach isn't very feasible as well.
  3011. // The approach taken is based on the simple example in the MSDN article.
  3012. // - Loop until the return value is either an error (apart from insufficient buffer size), or success.
  3013. // - Each time recreate a temporary buffer based on the last guess.
  3014. // - When creating the JS string, use the positive return value and copy the buffer across.
  3015. // Design choice for "guesses" comes from data Windows collected; and in most cases the loop will not iterate more than 2 times.
  3016. Assert(!IsNormalizedString(form, this->GetSz(), this->GetLength()));
  3017. //Get the first size estimate
  3018. int sizeEstimate = NormalizeString(form, this->GetSz(), this->GetLength() + 1, nullptr, 0);
  3019. char16 *tmpBuffer = nullptr;
  3020. DWORD lastError = ERROR_INSUFFICIENT_BUFFER;
  3021. //Loop while the size estimate is bigger than 0
  3022. while (lastError == ERROR_INSUFFICIENT_BUFFER)
  3023. {
  3024. tmpBuffer = AnewArray(tempAllocator, char16, sizeEstimate);
  3025. sizeEstimate = NormalizeString(form, this->GetSz(), this->GetLength() + 1, tmpBuffer, sizeEstimate);
  3026. // Success, sizeEstimate is the exact size including the null terminator
  3027. if (sizeEstimate > 0)
  3028. {
  3029. sizeOfNormalizedStringWithoutNullTerminator = sizeEstimate - 1;
  3030. return tmpBuffer;
  3031. }
  3032. //Anything less than 0, we have an error, flip sizeEstimate now. As both times we need to use it, we need positive anyways.
  3033. lastError = GetLastError();
  3034. sizeEstimate *= -1;
  3035. }
  3036. switch ((long)lastError)
  3037. {
  3038. case ERROR_INVALID_PARAMETER:
  3039. //some invalid parameter, coding error
  3040. AssertMsg(false, "ERROR_INVALID_PARAMETER check pointers passed to NormalizeString");
  3041. JavascriptError::ThrowRangeError(scriptContext, JSERR_FailedToNormalize);
  3042. break;
  3043. case ERROR_NO_UNICODE_TRANSLATION:
  3044. //the value returned is the negative index of an invalid unicode character
  3045. JavascriptError::ThrowRangeErrorVar(scriptContext, JSERR_InvalidUnicodeCharacter, sizeEstimate);
  3046. break;
  3047. case ERROR_SUCCESS:
  3048. //The actual size of the output string is zero.
  3049. //Theoretically only empty input string should produce this, which is handled above, thus the code path should not be hit.
  3050. AssertMsg(false, "This code path should not be hit, empty string case is handled above. Perhaps a false error (sizeEstimate <= 0; but lastError == 0; ERROR_SUCCESS and NO_ERRROR == 0)");
  3051. sizeOfNormalizedStringWithoutNullTerminator = 0;
  3052. return nullptr; // scriptContext->GetLibrary()->GetEmptyString();
  3053. break;
  3054. default:
  3055. AssertMsg(false, "Unknown error. MSDN documentation didn't specify any additional errors.");
  3056. JavascriptError::ThrowRangeError(scriptContext, JSERR_FailedToNormalize);
  3057. break;
  3058. }
  3059. }
  3060. void JavascriptString::InstantiateForceInlinedMembers()
  3061. {
  3062. // Force-inlined functions defined in a translation unit need a reference from an extern non-force-inlined function in
  3063. // the same translation unit to force an instantiation of the force-inlined function. Otherwise, if the force-inlined
  3064. // function is not referenced in the same translation unit, it will not be generated and the linker is not able to find
  3065. // the definition to inline the function in other translation units.
  3066. Assert(false);
  3067. JavascriptString *const s = nullptr;
  3068. s->ConcatDestructive(nullptr);
  3069. }
  3070. JavascriptString *
  3071. JavascriptString::Concat3(JavascriptString * pstLeft, JavascriptString * pstCenter, JavascriptString * pstRight)
  3072. {
  3073. ConcatStringMulti * concatString = ConcatStringMulti::New(3, pstLeft, pstCenter, pstLeft->GetScriptContext());
  3074. concatString->SetItem(2, pstRight);
  3075. return concatString;
  3076. }
  3077. BOOL JavascriptString::HasProperty(PropertyId propertyId)
  3078. {
  3079. if (propertyId == PropertyIds::length)
  3080. {
  3081. return true;
  3082. }
  3083. ScriptContext* scriptContext = GetScriptContext();
  3084. charcount_t index;
  3085. if (scriptContext->IsNumericPropertyId(propertyId, &index))
  3086. {
  3087. if (index < this->GetLength())
  3088. {
  3089. return true;
  3090. }
  3091. }
  3092. return false;
  3093. }
  3094. BOOL JavascriptString::IsEnumerable(PropertyId propertyId)
  3095. {
  3096. ScriptContext* scriptContext = GetScriptContext();
  3097. charcount_t index;
  3098. if (scriptContext->IsNumericPropertyId(propertyId, &index))
  3099. {
  3100. if (index < this->GetLength())
  3101. {
  3102. return true;
  3103. }
  3104. }
  3105. return false;
  3106. }
  3107. BOOL JavascriptString::GetProperty(Var originalInstance, PropertyId propertyId, Var* value, PropertyValueInfo* info, ScriptContext* requestContext)
  3108. {
  3109. return GetPropertyBuiltIns(propertyId, value);
  3110. }
  3111. BOOL JavascriptString::GetProperty(Var originalInstance, JavascriptString* propertyNameString, Var* value, PropertyValueInfo* info, ScriptContext* requestContext)
  3112. {
  3113. PropertyRecord const* propertyRecord;
  3114. this->GetScriptContext()->FindPropertyRecord(propertyNameString, &propertyRecord);
  3115. if (propertyRecord != nullptr && GetPropertyBuiltIns(propertyRecord->GetPropertyId(), value))
  3116. {
  3117. return true;
  3118. }
  3119. return false;
  3120. }
  3121. bool JavascriptString::GetPropertyBuiltIns(PropertyId propertyId, Var* value)
  3122. {
  3123. if (propertyId == PropertyIds::length)
  3124. {
  3125. *value = JavascriptNumber::ToVar(this->GetLength(), this->GetScriptContext());
  3126. return true;
  3127. }
  3128. return false;
  3129. }
  3130. BOOL JavascriptString::GetPropertyReference(Var originalInstance, PropertyId propertyId, Var* value, PropertyValueInfo* info, ScriptContext* requestContext)
  3131. {
  3132. return JavascriptString::GetProperty(originalInstance, propertyId, value, info, requestContext);
  3133. }
  3134. BOOL JavascriptString::SetItem(uint32 index, Var value, PropertyOperationFlags propertyOperationFlags)
  3135. {
  3136. if (this->HasItemAt(index))
  3137. {
  3138. JavascriptError::ThrowCantAssignIfStrictMode(propertyOperationFlags, this->GetScriptContext());
  3139. return FALSE;
  3140. }
  3141. return __super::SetItem(index, value, propertyOperationFlags);
  3142. }
  3143. BOOL JavascriptString::DeleteItem(uint32 index, PropertyOperationFlags propertyOperationFlags)
  3144. {
  3145. if (this->HasItemAt(index))
  3146. {
  3147. JavascriptError::ThrowCantDeleteIfStrictMode(propertyOperationFlags, this->GetScriptContext(), TaggedInt::ToString(index, this->GetScriptContext())->GetString());
  3148. return FALSE;
  3149. }
  3150. return __super::DeleteItem(index, propertyOperationFlags);
  3151. }
  3152. BOOL JavascriptString::HasItem(uint32 index)
  3153. {
  3154. return this->HasItemAt(index);
  3155. }
  3156. BOOL JavascriptString::GetItem(Var originalInstance, uint32 index, Var* value, ScriptContext* requestContext)
  3157. {
  3158. // String should always be marshalled to the current context
  3159. Assert(requestContext == this->GetScriptContext());
  3160. return this->GetItemAt(index, value);
  3161. }
  3162. BOOL JavascriptString::GetItemReference(Var originalInstance, uint32 index, Var* value, ScriptContext* requestContext)
  3163. {
  3164. // String should always be marshalled to the current context
  3165. return this->GetItemAt(index, value);
  3166. }
  3167. BOOL JavascriptString::GetEnumerator(BOOL enumNonEnumerable, Var* enumerator, ScriptContext * requestContext, bool preferSnapshotSemantics, bool enumSymbols)
  3168. {
  3169. *enumerator = RecyclerNew(GetScriptContext()->GetRecycler(), JavascriptStringEnumerator, this, requestContext);
  3170. return true;
  3171. }
  3172. BOOL JavascriptString::DeleteProperty(PropertyId propertyId, PropertyOperationFlags propertyOperationFlags)
  3173. {
  3174. if (propertyId == PropertyIds::length)
  3175. {
  3176. JavascriptError::ThrowCantDeleteIfStrictMode(propertyOperationFlags, this->GetScriptContext(), this->GetScriptContext()->GetPropertyName(propertyId)->GetBuffer());
  3177. return FALSE;
  3178. }
  3179. return __super::DeleteProperty(propertyId, propertyOperationFlags);
  3180. }
  3181. BOOL JavascriptString::GetDiagValueString(StringBuilder<ArenaAllocator>* stringBuilder, ScriptContext* requestContext)
  3182. {
  3183. stringBuilder->Append(_u('"'));
  3184. stringBuilder->Append(this->GetString(), this->GetLength());
  3185. stringBuilder->Append(_u('"'));
  3186. return TRUE;
  3187. }
  3188. BOOL JavascriptString::GetDiagTypeString(StringBuilder<ArenaAllocator>* stringBuilder, ScriptContext* requestContext)
  3189. {
  3190. stringBuilder->AppendCppLiteral(_u("String"));
  3191. return TRUE;
  3192. }
  3193. RecyclableObject* JavascriptString::ToObject(ScriptContext * requestContext)
  3194. {
  3195. return requestContext->GetLibrary()->CreateStringObject(this);
  3196. }
  3197. Var JavascriptString::GetTypeOfString(ScriptContext * requestContext)
  3198. {
  3199. return requestContext->GetLibrary()->GetStringTypeDisplayString();
  3200. }
  3201. }