JavascriptString.cpp 146 KB

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