RegexHelper.cpp 99 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314
  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. // Parser Includes
  7. #include "DebugWriter.h"
  8. #include "RegexStats.h"
  9. #include "OctoquadIdentifier.h"
  10. #include "RegexCompileTime.h"
  11. #include "RegexParser.h"
  12. #include "RegexPattern.h"
  13. namespace Js
  14. {
  15. // ----------------------------------------------------------------------
  16. // Dynamic compilation
  17. // ----------------------------------------------------------------------
  18. // See also:
  19. // UnifiedRegex::Parser::Options(...)
  20. bool RegexHelper::GetFlags(Js::ScriptContext* scriptContext, __in_ecount(strLen) const char16* str, CharCount strLen, UnifiedRegex::RegexFlags &flags)
  21. {
  22. for (CharCount i = 0; i < strLen; i++)
  23. {
  24. switch (str[i])
  25. {
  26. case 'i':
  27. if ((flags & UnifiedRegex::IgnoreCaseRegexFlag) != 0)
  28. return false;
  29. flags = (UnifiedRegex::RegexFlags)(flags | UnifiedRegex::IgnoreCaseRegexFlag);
  30. break;
  31. case 'g':
  32. if ((flags & UnifiedRegex::GlobalRegexFlag) != 0)
  33. return false;
  34. flags = (UnifiedRegex::RegexFlags)(flags | UnifiedRegex::GlobalRegexFlag);
  35. break;
  36. case 'm':
  37. if ((flags & UnifiedRegex::MultilineRegexFlag) != 0)
  38. return false;
  39. flags = (UnifiedRegex::RegexFlags)(flags | UnifiedRegex::MultilineRegexFlag);
  40. break;
  41. case 'u':
  42. if (scriptContext->GetConfig()->IsES6UnicodeExtensionsEnabled())
  43. {
  44. if((flags & UnifiedRegex::UnicodeRegexFlag) != 0)
  45. return false;
  46. flags = (UnifiedRegex::RegexFlags)(flags | UnifiedRegex::UnicodeRegexFlag);
  47. break;
  48. }
  49. return false;
  50. case 'y':
  51. if (scriptContext->GetConfig()->IsES6RegExStickyEnabled())
  52. {
  53. if ((flags & UnifiedRegex::StickyRegexFlag) != 0)
  54. return false;
  55. flags = (UnifiedRegex::RegexFlags)(flags | UnifiedRegex::StickyRegexFlag);
  56. break;
  57. }
  58. return false;
  59. default:
  60. return false;
  61. }
  62. }
  63. return true;
  64. }
  65. UnifiedRegex::RegexPattern* RegexHelper::CompileDynamic(ScriptContext *scriptContext, const char16* psz, CharCount csz, const char16* pszOpts, CharCount cszOpts, bool isLiteralSource)
  66. {
  67. Assert(psz != 0 && psz[csz] == 0);
  68. Assert(pszOpts != 0 || cszOpts == 0);
  69. Assert(pszOpts == 0 || pszOpts[cszOpts] == 0);
  70. UnifiedRegex::RegexFlags flags = UnifiedRegex::NoRegexFlags;
  71. if (pszOpts != NULL)
  72. {
  73. if (!GetFlags(scriptContext, pszOpts, cszOpts, flags))
  74. {
  75. // Compile in order to throw appropriate error for ill-formed flags
  76. PrimCompileDynamic(scriptContext, psz, csz, pszOpts, cszOpts, isLiteralSource);
  77. Assert(false);
  78. }
  79. }
  80. if(isLiteralSource)
  81. {
  82. // The source is from a literal regex, so we're cloning a literal regex. Don't use the dynamic regex MRU map since
  83. // these literal regex patterns' lifetimes are tied with the function body.
  84. return PrimCompileDynamic(scriptContext, psz, csz, pszOpts, cszOpts, isLiteralSource);
  85. }
  86. UnifiedRegex::RegexKey lookupKey(psz, csz, flags);
  87. UnifiedRegex::RegexPattern* pattern = nullptr;
  88. RegexPatternMruMap* dynamicRegexMap = scriptContext->GetDynamicRegexMap();
  89. if (!dynamicRegexMap->TryGetValue(lookupKey, &pattern))
  90. {
  91. pattern = PrimCompileDynamic(scriptContext, psz, csz, pszOpts, cszOpts, isLiteralSource);
  92. // WARNING: Must calculate key again so that dictionary has copy of source associated with the pattern
  93. const auto source = pattern->GetSource();
  94. UnifiedRegex::RegexKey finalKey(source.GetBuffer(), source.GetLength(), flags);
  95. dynamicRegexMap->Add(finalKey, pattern);
  96. }
  97. return pattern;
  98. }
  99. UnifiedRegex::RegexPattern* RegexHelper::CompileDynamic(
  100. ScriptContext *scriptContext, const char16* psz, CharCount csz, UnifiedRegex::RegexFlags flags, bool isLiteralSource)
  101. {
  102. //
  103. // Regex compilations are mostly string parsing based. To avoid duplicating validation rules,
  104. // generate a trivial options string right here on the stack and delegate to the string parsing
  105. // based implementation.
  106. //
  107. const CharCount OPT_BUF_SIZE = 6;
  108. char16 opts[OPT_BUF_SIZE];
  109. CharCount i = 0;
  110. if (flags & UnifiedRegex::IgnoreCaseRegexFlag)
  111. {
  112. opts[i++] = _u('i');
  113. }
  114. if (flags & UnifiedRegex::GlobalRegexFlag)
  115. {
  116. opts[i++] = _u('g');
  117. }
  118. if (flags & UnifiedRegex::MultilineRegexFlag)
  119. {
  120. opts[i++] = _u('m');
  121. }
  122. if (flags & UnifiedRegex::UnicodeRegexFlag)
  123. {
  124. Assert(scriptContext->GetConfig()->IsES6UnicodeExtensionsEnabled());
  125. opts[i++] = _u('u');
  126. }
  127. if (flags & UnifiedRegex::StickyRegexFlag)
  128. {
  129. Assert(scriptContext->GetConfig()->IsES6RegExStickyEnabled());
  130. opts[i++] = _u('y');
  131. }
  132. Assert(i < OPT_BUF_SIZE);
  133. opts[i] = NULL;
  134. return CompileDynamic(scriptContext, psz, csz, opts, i, isLiteralSource);
  135. }
  136. UnifiedRegex::RegexPattern* RegexHelper::PrimCompileDynamic(ScriptContext *scriptContext, const char16* psz, CharCount csz, const char16* pszOpts, CharCount cszOpts, bool isLiteralSource)
  137. {
  138. PROBE_STACK_NO_DISPOSE(scriptContext, Js::Constants::MinStackRegex);
  139. // SEE ALSO: Scanner<EncodingPolicy>::ScanRegExpConstant()
  140. #ifdef PROFILE_EXEC
  141. scriptContext->ProfileBegin(Js::RegexCompilePhase);
  142. #endif
  143. ArenaAllocator* rtAllocator = scriptContext->RegexAllocator();
  144. #if ENABLE_REGEX_CONFIG_OPTIONS
  145. UnifiedRegex::DebugWriter *dw = 0;
  146. if (REGEX_CONFIG_FLAG(RegexDebug))
  147. dw = scriptContext->GetRegexDebugWriter();
  148. UnifiedRegex::RegexStats* stats = 0;
  149. #endif
  150. UnifiedRegex::RegexFlags flags = UnifiedRegex::NoRegexFlags;
  151. if(csz == 0 && cszOpts == 0)
  152. {
  153. // Fast path for compiling the empty regex with empty flags, for the RegExp constructor object and other cases.
  154. // These empty regexes are dynamic regexes and so this fast path only exists for dynamic regex compilation. The
  155. // standard chars in particular, do not need to be initialized to compile this regex.
  156. UnifiedRegex::Program* program = UnifiedRegex::Program::New(scriptContext->GetRecycler(), flags);
  157. UnifiedRegex::Parser<NullTerminatedUnicodeEncodingPolicy, false>::CaptureEmptySourceAndNoGroups(program);
  158. UnifiedRegex::RegexPattern* pattern = UnifiedRegex::RegexPattern::New(scriptContext, program, false);
  159. UnifiedRegex::Compiler::CompileEmptyRegex
  160. ( program
  161. , pattern
  162. #if ENABLE_REGEX_CONFIG_OPTIONS
  163. , dw
  164. , stats
  165. #endif
  166. );
  167. #ifdef PROFILE_EXEC
  168. scriptContext->ProfileEnd(Js::RegexCompilePhase);
  169. #endif
  170. return pattern;
  171. }
  172. #if ENABLE_REGEX_CONFIG_OPTIONS
  173. if (REGEX_CONFIG_FLAG(RegexProfile))
  174. scriptContext->GetRegexStatsDatabase()->BeginProfile();
  175. #endif
  176. BEGIN_TEMP_ALLOCATOR(ctAllocator, scriptContext, _u("UnifiedRegexParseAndCompile"));
  177. UnifiedRegex::StandardChars<char16>* standardChars = scriptContext->GetThreadContext()->GetStandardChars((char16*)0);
  178. UnifiedRegex::Node* root = 0;
  179. UnifiedRegex::Parser<NullTerminatedUnicodeEncodingPolicy, false> parser
  180. ( scriptContext
  181. , ctAllocator
  182. , standardChars
  183. , standardChars
  184. , false
  185. #if ENABLE_REGEX_CONFIG_OPTIONS
  186. , dw
  187. #endif
  188. );
  189. try
  190. {
  191. root = parser.ParseDynamic(psz, psz + csz, pszOpts, pszOpts + cszOpts, flags);
  192. }
  193. catch (UnifiedRegex::ParseError e)
  194. {
  195. END_TEMP_ALLOCATOR(ctAllocator, scriptContext);
  196. #ifdef PROFILE_EXEC
  197. scriptContext->ProfileEnd(Js::RegexCompilePhase);
  198. #endif
  199. Js::JavascriptError::ThrowSyntaxError(scriptContext, e.error);
  200. // never reached
  201. }
  202. const auto recycler = scriptContext->GetRecycler();
  203. UnifiedRegex::Program* program = UnifiedRegex::Program::New(recycler, flags);
  204. parser.CaptureSourceAndGroups(recycler, program, psz, csz, csz);
  205. UnifiedRegex::RegexPattern* pattern = UnifiedRegex::RegexPattern::New(scriptContext, program, isLiteralSource);
  206. #if ENABLE_REGEX_CONFIG_OPTIONS
  207. if (REGEX_CONFIG_FLAG(RegexProfile))
  208. {
  209. stats = scriptContext->GetRegexStatsDatabase()->GetRegexStats(pattern);
  210. scriptContext->GetRegexStatsDatabase()->EndProfile(stats, UnifiedRegex::RegexStats::Parse);
  211. }
  212. if (REGEX_CONFIG_FLAG(RegexTracing))
  213. {
  214. UnifiedRegex::DebugWriter* tw = scriptContext->GetRegexDebugWriter();
  215. tw->Print(_u("// REGEX COMPILE "));
  216. pattern->Print(tw);
  217. tw->EOL();
  218. }
  219. if (REGEX_CONFIG_FLAG(RegexProfile))
  220. scriptContext->GetRegexStatsDatabase()->BeginProfile();
  221. #endif
  222. UnifiedRegex::Compiler::Compile
  223. ( scriptContext
  224. , ctAllocator
  225. , rtAllocator
  226. , standardChars
  227. , program
  228. , root
  229. , parser.GetLitbuf()
  230. , pattern
  231. #if ENABLE_REGEX_CONFIG_OPTIONS
  232. , dw
  233. , stats
  234. #endif
  235. );
  236. #if ENABLE_REGEX_CONFIG_OPTIONS
  237. if (REGEX_CONFIG_FLAG(RegexProfile))
  238. scriptContext->GetRegexStatsDatabase()->EndProfile(stats, UnifiedRegex::RegexStats::Compile);
  239. #endif
  240. END_TEMP_ALLOCATOR(ctAllocator, scriptContext);
  241. #ifdef PROFILE_EXEC
  242. scriptContext->ProfileEnd(Js::RegexCompilePhase);
  243. #endif
  244. return pattern;
  245. }
  246. // ----------------------------------------------------------------------
  247. // Primitives
  248. // ----------------------------------------------------------------------
  249. #if ENABLE_REGEX_CONFIG_OPTIONS
  250. static void RegexHelperTrace(
  251. ScriptContext* scriptContext,
  252. UnifiedRegex::RegexStats::Use use,
  253. JavascriptRegExp* regExp,
  254. const char16 *const input,
  255. const CharCount inputLength,
  256. const char16 *const replace = 0,
  257. const CharCount replaceLength = 0)
  258. {
  259. Assert(regExp);
  260. Assert(input);
  261. if (REGEX_CONFIG_FLAG(RegexProfile))
  262. {
  263. UnifiedRegex::RegexStats* stats =
  264. scriptContext->GetRegexStatsDatabase()->GetRegexStats(regExp->GetPattern());
  265. stats->useCounts[use]++;
  266. stats->inputLength += inputLength;
  267. }
  268. if (REGEX_CONFIG_FLAG(RegexTracing))
  269. {
  270. UnifiedRegex::DebugWriter* w = scriptContext->GetRegexDebugWriter();
  271. w->Print(_u("%s("), UnifiedRegex::RegexStats::UseNames[use]);
  272. regExp->GetPattern()->Print(w);
  273. w->Print(_u(", "));
  274. if (!CONFIG_FLAG(Verbose) && inputLength > 1024)
  275. w->Print(_u("\"<string too large>\""));
  276. else
  277. w->PrintQuotedString(input, inputLength);
  278. if (replace != 0)
  279. {
  280. Assert(use == UnifiedRegex::RegexStats::Replace);
  281. w->Print(_u(", "));
  282. if (!CONFIG_FLAG(Verbose) && replaceLength > 1024)
  283. w->Print(_u("\"<string too large>\""));
  284. else
  285. w->PrintQuotedString(replace, replaceLength);
  286. }
  287. w->PrintEOL(_u(");"));
  288. w->Flush();
  289. }
  290. }
  291. static void RegexHelperTrace(ScriptContext* scriptContext, UnifiedRegex::RegexStats::Use use, JavascriptRegExp* regExp, JavascriptString* input)
  292. {
  293. Assert(regExp);
  294. Assert(input);
  295. EnterPinnedScope((volatile void**)& input);
  296. RegexHelperTrace(scriptContext, use, regExp, input->GetString(), input->GetLength());
  297. LeavePinnedScope(); // input
  298. }
  299. static void RegexHelperTrace(ScriptContext* scriptContext, UnifiedRegex::RegexStats::Use use, JavascriptRegExp* regExp, JavascriptString* input, JavascriptString* replace)
  300. {
  301. Assert(regExp);
  302. Assert(input);
  303. Assert(replace);
  304. EnterPinnedScope((volatile void**)& input);
  305. EnterPinnedScope((volatile void**)& replace);
  306. RegexHelperTrace(scriptContext, use, regExp, input->GetString(), input->GetLength(), replace->GetString(), replace->GetLength());
  307. LeavePinnedScope(); // replace
  308. LeavePinnedScope(); // input
  309. }
  310. #endif
  311. // ----------------------------------------------------------------------
  312. // Regex entry points
  313. // ----------------------------------------------------------------------
  314. struct RegexMatchState
  315. {
  316. const char16* input;
  317. TempArenaAllocatorObject* tempAllocatorObj;
  318. UnifiedRegex::Matcher* matcher;
  319. };
  320. template <bool updateHistory>
  321. Var RegexHelper::RegexMatchImpl(ScriptContext* scriptContext, RecyclableObject *thisObj, JavascriptString *input, bool noResult, void *const stackAllocationPointer)
  322. {
  323. ScriptConfiguration const * scriptConfig = scriptContext->GetConfig();
  324. // Normally, this check would be done in JavascriptRegExp::EntrySymbolMatch. However,
  325. // since the lowerer inlines String.prototype.match and directly calls the helper,
  326. // the check then would be bypassed. That's the reason we do the check here.
  327. if (scriptConfig->IsES6RegExSymbolsEnabled()
  328. && IsRegexSymbolMatchObservable(thisObj, scriptContext))
  329. {
  330. // We don't need to pass "updateHistory" here since the call to "exec" will handle it.
  331. return RegexEs6MatchImpl(scriptContext, thisObj, input, noResult, stackAllocationPointer);
  332. }
  333. else
  334. {
  335. PCWSTR varName = scriptConfig->IsES6RegExSymbolsEnabled()
  336. ? _u("RegExp.prototype[Symbol.match]")
  337. : _u("String.prototype.match");
  338. JavascriptRegExp* regularExpression = JavascriptRegExp::ToRegExp(thisObj, varName, scriptContext);
  339. return RegexEs5MatchImpl<updateHistory>(scriptContext, regularExpression, input, noResult, stackAllocationPointer);
  340. }
  341. }
  342. bool RegexHelper::IsRegexSymbolMatchObservable(RecyclableObject* instance, ScriptContext* scriptContext)
  343. {
  344. DynamicObject* regexPrototype = scriptContext->GetLibrary()->GetRegExpPrototype();
  345. return !JavascriptRegExp::HasOriginalRegExType(instance)
  346. || JavascriptRegExp::HasObservableExec(regexPrototype)
  347. || JavascriptRegExp::HasObservableGlobalFlag(regexPrototype)
  348. || JavascriptRegExp::HasObservableUnicodeFlag(regexPrototype);
  349. }
  350. Var RegexHelper::RegexEs6MatchImpl(ScriptContext* scriptContext, RecyclableObject *thisObj, JavascriptString *input, bool noResult, void *const stackAllocationPointer)
  351. {
  352. PCWSTR const varName = _u("RegExp.prototype[Symbol.match]");
  353. if (!JavascriptRegExp::GetGlobalProperty(thisObj, scriptContext))
  354. {
  355. return JavascriptRegExp::CallExec(thisObj, input, varName, scriptContext);
  356. }
  357. else
  358. {
  359. bool unicode = JavascriptRegExp::GetUnicodeProperty(thisObj, scriptContext);
  360. JavascriptRegExp::SetLastIndexProperty(thisObj, TaggedInt::ToVarUnchecked(0), scriptContext);
  361. JavascriptArray* arrayResult = nullptr;
  362. do
  363. {
  364. Var result = JavascriptRegExp::CallExec(thisObj, input, varName, scriptContext);
  365. if (JavascriptOperators::IsNull(result))
  366. {
  367. break;
  368. }
  369. RecyclableObject* resultObj = ExecResultToRecyclableObject(result);
  370. JavascriptString* matchStr = GetMatchStrFromResult(resultObj, scriptContext);
  371. if (arrayResult == nullptr)
  372. {
  373. arrayResult = scriptContext->GetLibrary()->CreateArray();
  374. }
  375. arrayResult->DirectAppendItem(matchStr);
  376. AdvanceLastIndex(thisObj, input, matchStr, unicode, scriptContext);
  377. }
  378. while (true);
  379. return arrayResult != nullptr
  380. ? arrayResult
  381. : scriptContext->GetLibrary()->GetNull();
  382. }
  383. }
  384. // String.prototype.match (ES5 15.5.4.10)
  385. template <bool updateHistory>
  386. Var RegexHelper::RegexEs5MatchImpl(ScriptContext* scriptContext, JavascriptRegExp *regularExpression, JavascriptString *input, bool noResult, void *const stackAllocationPointer)
  387. {
  388. JavascriptArray* arrayResult = nullptr;
  389. UnifiedRegex::RegexPattern* pattern = regularExpression->GetPattern();
  390. EnterPinnedScope((volatile void**)& input);
  391. const char16* inputStr = input->GetString();
  392. CharCount inputLength = input->GetLength();
  393. #if ENABLE_REGEX_CONFIG_OPTIONS
  394. RegexHelperTrace(scriptContext, UnifiedRegex::RegexStats::Match, regularExpression, input);
  395. #endif
  396. UnifiedRegex::GroupInfo lastSuccessfulMatch; // initially undefined
  397. UnifiedRegex::GroupInfo lastActualMatch; // initially undefined
  398. #ifdef REGEX_TRIGRAMS
  399. UnifiedRegex::TrigramAlphabet* trigramAlphabet = scriptContext->GetTrigramAlphabet();
  400. UnifiedRegex::TrigramInfo* trigramInfo = pattern->rep.unified.trigramInfo;
  401. if (trigramAlphabet != NULL && inputLength >= MinTrigramInputLength && trigramInfo != NULL)
  402. {
  403. if (trigramAlphabet->input == NULL)
  404. {
  405. trigramAlphabet->MegaMatch((char16*)inputStr, inputLength);
  406. }
  407. if (trigramInfo->isTrigramPattern)
  408. {
  409. if (trigramInfo->resultCount > 0)
  410. {
  411. lastSuccessfulMatch.offset = trigramInfo->offsets[trigramInfo->resultCount - 1];
  412. lastSuccessfulMatch.length = UnifiedRegex::TrigramInfo::PatternLength;
  413. }
  414. // else: leave lastMatch undefined
  415. // Make sure a matcher is allocated and holds valid last match in case the RegExp constructor
  416. // needs to fill-in details from the last match via JavascriptRegExpConstructor::EnsureValues
  417. Assert(pattern->rep.unified.program != 0);
  418. if (pattern->rep.unified.matcher == 0)
  419. pattern->rep.unified.matcher = UnifiedRegex::Matcher::New(scriptContext, pattern);
  420. *pattern->rep.unified.matcher->GroupIdToGroupInfo(0) = lastSuccessfulMatch;
  421. Assert(pattern->IsGlobal());
  422. arrayResult = CreateMatchResult(stackAllocationPointer, scriptContext, /* isGlobal */ true, pattern->NumGroups(), input);
  423. FinalizeMatchResult(scriptContext, /* isGlobal */ true, arrayResult, lastSuccessfulMatch);
  424. if (trigramInfo->resultCount > 0)
  425. {
  426. if (trigramInfo->hasCachedResultString)
  427. {
  428. for (int k = 0; k < trigramInfo->resultCount; k++)
  429. {
  430. arrayResult->DirectSetItemAt(k,
  431. static_cast<Js::JavascriptString*>(trigramInfo->cachedResult[k]));
  432. }
  433. }
  434. else
  435. {
  436. for (int k = 0; k < trigramInfo->resultCount; k++)
  437. {
  438. JavascriptString * str = SubString::New(input, trigramInfo->offsets[k], UnifiedRegex::TrigramInfo::PatternLength);
  439. trigramInfo->cachedResult[k] = str;
  440. arrayResult->DirectSetItemAt(k, str);
  441. }
  442. trigramInfo->hasCachedResultString = true;
  443. }
  444. } // otherwise, there are no results and null will be returned
  445. if (updateHistory)
  446. {
  447. PropagateLastMatch(scriptContext, /* isGlobal */ true, pattern->IsSticky(), regularExpression, input, lastSuccessfulMatch, lastActualMatch, true, true);
  448. }
  449. return lastSuccessfulMatch.IsUndefined() ? scriptContext->GetLibrary()->GetNull() : arrayResult;
  450. }
  451. }
  452. #endif
  453. // If global regex, result array holds substrings for each match, and group bindings are ignored
  454. // If non-global regex, result array holds overall substring and each group binding substring
  455. const bool isGlobal = pattern->IsGlobal();
  456. const bool isSticky = pattern->IsSticky();
  457. RegexMatchState state;
  458. // If global = false and sticky = true, set offset = lastIndex, else set offset = 0
  459. CharCount offset = 0;
  460. if (!isGlobal && isSticky)
  461. {
  462. offset = regularExpression->GetLastIndex();
  463. }
  464. uint32 globalIndex = 0;
  465. PrimBeginMatch(state, scriptContext, pattern, inputStr, inputLength, false);
  466. do
  467. {
  468. if (offset > inputLength)
  469. {
  470. lastActualMatch.Reset();
  471. break;
  472. }
  473. lastActualMatch = PrimMatch(state, scriptContext, pattern, inputLength, offset);
  474. if (lastActualMatch.IsUndefined())
  475. break;
  476. lastSuccessfulMatch = lastActualMatch;
  477. if (!noResult)
  478. {
  479. if (arrayResult == 0)
  480. arrayResult = CreateMatchResult(stackAllocationPointer, scriptContext, isGlobal, pattern->NumGroups(), input);
  481. JavascriptString *const matchedString = SubString::New(input, lastActualMatch.offset, lastActualMatch.length);
  482. if (isGlobal)
  483. arrayResult->DirectSetItemAt(globalIndex, matchedString);
  484. else
  485. {
  486. // The array's head segment up to length - 1 may not be filled. Write to the head segment element directly
  487. // instead of calling a helper that expects the segment to be pre-filled.
  488. Assert(globalIndex < arrayResult->GetHead()->length);
  489. static_cast<SparseArraySegment<Var> *>(arrayResult->GetHead())->elements[globalIndex] = matchedString;
  490. }
  491. globalIndex++;
  492. }
  493. offset = lastActualMatch.offset + max(lastActualMatch.length, static_cast<CharCountOrFlag>(1));
  494. } while (isGlobal);
  495. PrimEndMatch(state, scriptContext, pattern);
  496. if (updateHistory)
  497. {
  498. PropagateLastMatch(scriptContext, isGlobal, isSticky, regularExpression, input, lastSuccessfulMatch, lastActualMatch, true, true);
  499. }
  500. if (arrayResult == 0)
  501. {
  502. return scriptContext->GetLibrary()->GetNull();
  503. }
  504. const int numGroups = pattern->NumGroups();
  505. if (!isGlobal)
  506. {
  507. if (numGroups > 1)
  508. {
  509. // Overall match already captured in index 0 by above, so just grab the groups
  510. Var nonMatchValue = NonMatchValue(scriptContext, false);
  511. Field(Var) *elements = ((SparseArraySegment<Var>*)arrayResult->GetHead())->elements;
  512. for (uint groupId = 1; groupId < (uint)numGroups; groupId++)
  513. {
  514. Assert(groupId < arrayResult->GetHead()->left + arrayResult->GetHead()->length);
  515. elements[groupId] = GetGroup(scriptContext, pattern, input, nonMatchValue, groupId);
  516. }
  517. }
  518. FinalizeMatchResult(scriptContext, /* isGlobal */ false, arrayResult, lastSuccessfulMatch);
  519. }
  520. else
  521. {
  522. FinalizeMatchResult(scriptContext, /* isGlobal */ true, arrayResult, lastSuccessfulMatch);
  523. }
  524. LeavePinnedScope(); // input
  525. return arrayResult;
  526. }
  527. // RegExp.prototype.exec (ES5 15.10.6.2)
  528. Var RegexHelper::RegexExecImpl(ScriptContext* scriptContext, JavascriptRegExp* regularExpression, JavascriptString* input, bool noResult, void *const stackAllocationPointer)
  529. {
  530. UnifiedRegex::RegexPattern* pattern = regularExpression->GetPattern();
  531. #if ENABLE_REGEX_CONFIG_OPTIONS
  532. RegexHelperTrace(scriptContext, UnifiedRegex::RegexStats::Exec, regularExpression, input);
  533. #endif
  534. const bool isGlobal = pattern->IsGlobal();
  535. const bool isSticky = pattern->IsSticky();
  536. CharCount offset;
  537. CharCount inputLength = input->GetLength();
  538. if (!GetInitialOffset(isGlobal, isSticky, regularExpression, inputLength, offset))
  539. {
  540. return scriptContext->GetLibrary()->GetNull();
  541. }
  542. UnifiedRegex::GroupInfo match; // initially undefined
  543. if (offset <= inputLength)
  544. {
  545. const char16* inputStr = input->GetString();
  546. match = SimpleMatch(scriptContext, pattern, inputStr, inputLength, offset);
  547. }
  548. // else: match remains undefined
  549. PropagateLastMatch(scriptContext, isGlobal, isSticky, regularExpression, input, match, match, true, true);
  550. if (noResult || match.IsUndefined())
  551. {
  552. return scriptContext->GetLibrary()->GetNull();
  553. }
  554. const int numGroups = pattern->NumGroups();
  555. Assert(numGroups >= 0);
  556. JavascriptArray* result = CreateExecResult(stackAllocationPointer, scriptContext, numGroups, input, match);
  557. Var nonMatchValue = NonMatchValue(scriptContext, false);
  558. Field(Var) *elements = ((SparseArraySegment<Var>*)result->GetHead())->elements;
  559. for (uint groupId = 0; groupId < (uint)numGroups; groupId++)
  560. {
  561. Assert(groupId < result->GetHead()->left + result->GetHead()->length);
  562. elements[groupId] = GetGroup(scriptContext, pattern, input, nonMatchValue, groupId);
  563. }
  564. return result;
  565. }
  566. Var RegexHelper::RegexTest(ScriptContext* scriptContext, RecyclableObject* thisObj, JavascriptString *input)
  567. {
  568. if (scriptContext->GetConfig()->IsES6RegExSymbolsEnabled()
  569. && IsRegexTestObservable(thisObj, scriptContext))
  570. {
  571. return RegexEs6TestImpl(scriptContext, thisObj, input);
  572. }
  573. else
  574. {
  575. JavascriptRegExp* regularExpression =
  576. JavascriptRegExp::ToRegExp(thisObj, _u("RegExp.prototype.test"), scriptContext);
  577. return RegexEs5TestImpl(scriptContext, regularExpression, input);
  578. }
  579. }
  580. bool RegexHelper::IsRegexTestObservable(RecyclableObject* instance, ScriptContext* scriptContext)
  581. {
  582. DynamicObject* regexPrototype = scriptContext->GetLibrary()->GetRegExpPrototype();
  583. return !JavascriptRegExp::HasOriginalRegExType(instance)
  584. || JavascriptRegExp::HasObservableExec(regexPrototype);
  585. }
  586. Var RegexHelper::RegexEs6TestImpl(ScriptContext* scriptContext, RecyclableObject* thisObj, JavascriptString *input)
  587. {
  588. Var match = JavascriptRegExp::CallExec(thisObj, input, _u("RegExp.prototype.test"), scriptContext);
  589. return JavascriptBoolean::ToVar(!JavascriptOperators::IsNull(match), scriptContext);
  590. }
  591. // RegExp.prototype.test (ES5 15.10.6.3)
  592. Var RegexHelper::RegexEs5TestImpl(ScriptContext* scriptContext, JavascriptRegExp *regularExpression, JavascriptString *input)
  593. {
  594. UnifiedRegex::RegexPattern* pattern = regularExpression->GetPattern();
  595. const char16* inputStr = input->GetString();
  596. CharCount inputLength = input->GetLength();
  597. UnifiedRegex::GroupInfo match; // initially undefined
  598. #if ENABLE_REGEX_CONFIG_OPTIONS
  599. RegexHelperTrace(scriptContext, UnifiedRegex::RegexStats::Test, regularExpression, input);
  600. #endif
  601. const bool isGlobal = pattern->IsGlobal();
  602. const bool isSticky = pattern->IsSticky();
  603. CharCount offset;
  604. if (!GetInitialOffset(isGlobal, isSticky, regularExpression, inputLength, offset))
  605. return scriptContext->GetLibrary()->GetFalse();
  606. if (offset <= inputLength)
  607. {
  608. match = SimpleMatch(scriptContext, pattern, inputStr, inputLength, offset);
  609. }
  610. // else: match remains undefined
  611. PropagateLastMatch(scriptContext, isGlobal, isSticky, regularExpression, input, match, match, true, true);
  612. return JavascriptBoolean::ToVar(!match.IsUndefined(), scriptContext);
  613. }
  614. template<typename GroupFn>
  615. void RegexHelper::ReplaceFormatString
  616. ( ScriptContext* scriptContext
  617. , int numGroups
  618. , GroupFn getGroup
  619. , JavascriptString* input
  620. , const char16* matchedString
  621. , UnifiedRegex::GroupInfo match
  622. , JavascriptString* replace
  623. , int substitutions
  624. , __in_ecount(substitutions) CharCount* substitutionOffsets
  625. , CompoundString::Builder<64 * sizeof(void *) / sizeof(char16)>& concatenated )
  626. {
  627. Var nonMatchValue = NonMatchValue(scriptContext, false);
  628. const CharCount inputLength = input->GetLength();
  629. const char16* replaceStr = replace->GetString();
  630. const CharCount replaceLength = replace->GetLength();
  631. CharCount offset = 0;
  632. for (int i = 0; i < substitutions; i++)
  633. {
  634. CharCount substitutionOffset = substitutionOffsets[i];
  635. concatenated.Append(replace, offset, substitutionOffset - offset);
  636. char16 currentChar = replaceStr[substitutionOffset + 1];
  637. if (currentChar >= _u('0') && currentChar <= _u('9'))
  638. {
  639. int captureIndex = (int)(currentChar - _u('0'));
  640. offset = substitutionOffset + 2;
  641. if (offset < replaceLength)
  642. {
  643. currentChar = replaceStr[substitutionOffset + 2];
  644. if (currentChar >= _u('0') && currentChar <= _u('9'))
  645. {
  646. int tempCaptureIndex = (10 * captureIndex) + (int)(currentChar - _u('0'));
  647. if (tempCaptureIndex < numGroups)
  648. {
  649. captureIndex = tempCaptureIndex;
  650. offset = substitutionOffset + 3;
  651. }
  652. }
  653. }
  654. if (captureIndex < numGroups && (captureIndex != 0))
  655. {
  656. Var group = getGroup(captureIndex, nonMatchValue);
  657. if (JavascriptString::Is(group))
  658. concatenated.Append(JavascriptString::FromVar(group));
  659. else if (group != nonMatchValue)
  660. concatenated.Append(replace, substitutionOffset, offset - substitutionOffset);
  661. }
  662. else
  663. concatenated.Append(replace, substitutionOffset, offset - substitutionOffset);
  664. }
  665. else
  666. {
  667. switch (currentChar)
  668. {
  669. case _u('$'): // literal '$' character
  670. concatenated.Append(_u('$'));
  671. offset = substitutionOffset + 2;
  672. break;
  673. case _u('&'): // matched string
  674. concatenated.Append(matchedString, match.length);
  675. offset = substitutionOffset + 2;
  676. break;
  677. case _u('`'): // left context
  678. concatenated.Append(input, 0, match.offset);
  679. offset = substitutionOffset + 2;
  680. break;
  681. case _u('\''): // right context
  682. if (match.EndOffset() < inputLength)
  683. {
  684. concatenated.Append(input, match.EndOffset(), inputLength - match.EndOffset());
  685. }
  686. offset = substitutionOffset + 2;
  687. break;
  688. default:
  689. concatenated.Append(_u('$'));
  690. offset = substitutionOffset + 1;
  691. break;
  692. }
  693. }
  694. }
  695. concatenated.Append(replace, offset, replaceLength - offset);
  696. }
  697. int RegexHelper::GetReplaceSubstitutions(const char16 * const replaceStr, CharCount const replaceLength,
  698. ArenaAllocator * const tempAllocator, CharCount** const substitutionOffsetsOut)
  699. {
  700. int substitutions = 0;
  701. for (CharCount i = 0; i < replaceLength; i++)
  702. {
  703. if (replaceStr[i] == _u('$'))
  704. {
  705. if (++i < replaceLength)
  706. {
  707. substitutions++;
  708. }
  709. }
  710. }
  711. if (substitutions > 0)
  712. {
  713. CharCount* substitutionOffsets = AnewArray(tempAllocator, CharCount, substitutions);
  714. substitutions = 0;
  715. for (CharCount i = 0; i < replaceLength; i++)
  716. {
  717. if (replaceStr[i] == _u('$'))
  718. {
  719. if (i < (replaceLength - 1))
  720. {
  721. #pragma prefast(suppress:26000, "index doesn't overflow the buffer")
  722. substitutionOffsets[substitutions] = i;
  723. i++;
  724. substitutions++;
  725. }
  726. }
  727. }
  728. *substitutionOffsetsOut = substitutionOffsets;
  729. }
  730. return substitutions;
  731. }
  732. Var RegexHelper::RegexReplaceImpl(ScriptContext* scriptContext, RecyclableObject* thisObj, JavascriptString* input, JavascriptString* replace, bool noResult)
  733. {
  734. ScriptConfiguration const * scriptConfig = scriptContext->GetConfig();
  735. if (scriptConfig->IsES6RegExSymbolsEnabled() && IsRegexSymbolReplaceObservable(thisObj, scriptContext))
  736. {
  737. return RegexEs6ReplaceImpl(scriptContext, thisObj, input, replace, noResult);
  738. }
  739. else
  740. {
  741. PCWSTR varName = scriptConfig->IsES6RegExSymbolsEnabled()
  742. ? _u("RegExp.prototype[Symbol.replace]")
  743. : _u("String.prototype.replace");
  744. JavascriptRegExp* regularExpression = JavascriptRegExp::ToRegExp(thisObj, varName, scriptContext);
  745. return RegexEs5ReplaceImpl(scriptContext, regularExpression, input, replace, noResult);
  746. }
  747. }
  748. bool RegexHelper::IsRegexSymbolReplaceObservable(RecyclableObject* instance, ScriptContext* scriptContext)
  749. {
  750. DynamicObject* regexPrototype = scriptContext->GetLibrary()->GetRegExpPrototype();
  751. return !JavascriptRegExp::HasOriginalRegExType(instance)
  752. || JavascriptRegExp::HasObservableUnicodeFlag(regexPrototype)
  753. || JavascriptRegExp::HasObservableExec(regexPrototype)
  754. || JavascriptRegExp::HasObservableGlobalFlag(regexPrototype);
  755. }
  756. Var RegexHelper::RegexEs6ReplaceImpl(ScriptContext* scriptContext, RecyclableObject* thisObj, JavascriptString* input, JavascriptString* replace, bool noResult)
  757. {
  758. auto appendReplacement = [&](
  759. CompoundString::Builder<64 * sizeof(void *) / sizeof(char16)>& resultBuilder,
  760. ArenaAllocator* tempAlloc,
  761. JavascriptString* matchStr,
  762. int numberOfCaptures,
  763. Var* captures,
  764. CharCount position)
  765. {
  766. CharCount* substitutionOffsets = nullptr;
  767. int substitutions = GetReplaceSubstitutions(
  768. replace->GetString(),
  769. replace->GetLength(),
  770. tempAlloc,
  771. &substitutionOffsets);
  772. auto getGroup = [&](int captureIndex, Var nonMatchValue) {
  773. return captureIndex <= numberOfCaptures ? captures[captureIndex] : nonMatchValue;
  774. };
  775. UnifiedRegex::GroupInfo match(position, matchStr->GetLength());
  776. int numGroups = numberOfCaptures + 1; // Take group 0 into account.
  777. ReplaceFormatString(
  778. scriptContext,
  779. numGroups,
  780. getGroup,
  781. input,
  782. matchStr->GetString(),
  783. match,
  784. replace,
  785. substitutions,
  786. substitutionOffsets,
  787. resultBuilder);
  788. };
  789. return RegexEs6ReplaceImpl(scriptContext, thisObj, input, appendReplacement, noResult);
  790. }
  791. Var RegexHelper::RegexEs6ReplaceImpl(ScriptContext* scriptContext, RecyclableObject* thisObj, JavascriptString* input, JavascriptFunction* replaceFn)
  792. {
  793. auto appendReplacement = [&](
  794. CompoundString::Builder<64 * sizeof(void *) / sizeof(char16)>& resultBuilder,
  795. ArenaAllocator* tempAlloc,
  796. JavascriptString* matchStr,
  797. int numberOfCaptures,
  798. Var* captures,
  799. CharCount position)
  800. {
  801. // replaceFn Arguments:
  802. //
  803. // 0: this
  804. // 1: matched
  805. // 2: capture1
  806. // ...
  807. // N + 1: capture N
  808. // N + 2: position
  809. // N + 3: input
  810. // Number of captures can be at most 99, so we won't overflow.
  811. ushort argCount = (ushort) numberOfCaptures + 4;
  812. PROBE_STACK_NO_DISPOSE(scriptContext, argCount * sizeof(Var));
  813. Var* args = (Var*) _alloca(argCount * sizeof(Var));
  814. args[0] = scriptContext->GetLibrary()->GetUndefined();
  815. #pragma prefast(suppress:6386, "The write is within the bounds")
  816. args[1] = matchStr;
  817. for (int i = 1; i <= numberOfCaptures; ++i)
  818. {
  819. args[i + 1] = captures[i];
  820. }
  821. args[numberOfCaptures + 2] = JavascriptNumber::ToVar(position, scriptContext);
  822. args[numberOfCaptures + 3] = input;
  823. JavascriptString* replace = JavascriptConversion::ToString(
  824. replaceFn->CallFunction(Arguments(CallInfo(argCount), args)),
  825. scriptContext);
  826. resultBuilder.Append(replace);
  827. };
  828. return RegexEs6ReplaceImpl(scriptContext, thisObj, input, appendReplacement, /* noResult */ false);
  829. }
  830. template<typename ReplacementFn>
  831. Var RegexHelper::RegexEs6ReplaceImpl(ScriptContext* scriptContext, RecyclableObject* thisObj, JavascriptString* input, ReplacementFn appendReplacement, bool noResult)
  832. {
  833. bool global = JavascriptRegExp::GetGlobalProperty(thisObj, scriptContext);
  834. bool unicode = false; // Dummy value. It isn't used below unless "global" is "true".
  835. if (global)
  836. {
  837. unicode = JavascriptRegExp::GetUnicodeProperty(thisObj, scriptContext);
  838. JavascriptRegExp::SetLastIndexProperty(thisObj, TaggedInt::ToVarUnchecked(0), scriptContext);
  839. }
  840. JavascriptString* accumulatedResult = nullptr;
  841. Recycler* recycler = scriptContext->GetRecycler();
  842. JsUtil::List<RecyclableObject*>* results = RecyclerNew(recycler, JsUtil::List<RecyclableObject*>, recycler);
  843. while (true)
  844. {
  845. PCWSTR varName = _u("RegExp.prototype[Symbol.replace]");
  846. Var result = JavascriptRegExp::CallExec(thisObj, input, varName, scriptContext);
  847. if (JavascriptOperators::IsNull(result))
  848. {
  849. break;
  850. }
  851. RecyclableObject* resultObj = ExecResultToRecyclableObject(result);
  852. results->Add(resultObj);
  853. if (!global)
  854. {
  855. break;
  856. }
  857. JavascriptString* matchStr = GetMatchStrFromResult(resultObj, scriptContext);
  858. AdvanceLastIndex(thisObj, input, matchStr, unicode, scriptContext);
  859. }
  860. CompoundString::Builder<64 * sizeof(void *) / sizeof(char16)> accumulatedResultBuilder(scriptContext);
  861. CharCount inputLength = input->GetLength();
  862. CharCount nextSourcePosition = 0;
  863. size_t previousNumberOfCapturesToKeep = 0;
  864. Field(Var)* captures = nullptr;
  865. BEGIN_TEMP_ALLOCATOR(tempAlloc, scriptContext, _u("RegexHelper"))
  866. {
  867. results->Map([&](int resultIndex, RecyclableObject* resultObj) {
  868. int64 length = JavascriptConversion::ToLength(
  869. JavascriptOperators::GetProperty(resultObj, PropertyIds::length, scriptContext),
  870. scriptContext);
  871. uint64 numberOfCaptures = (uint64) max(length - 1, (int64) 0);
  872. JavascriptString* matchStr = GetMatchStrFromResult(resultObj, scriptContext);
  873. int64 index = JavascriptConversion::ToLength(
  874. JavascriptOperators::GetProperty(resultObj, PropertyIds::index, scriptContext),
  875. scriptContext);
  876. CharCount position = max(
  877. min(JavascriptRegExp::GetIndexOrMax(index), inputLength),
  878. (CharCount) 0);
  879. // Capture groups can be referenced using at most two digits.
  880. const uint64 maxNumberOfCaptures = 99;
  881. size_t numberOfCapturesToKeep = (size_t) min(numberOfCaptures, maxNumberOfCaptures);
  882. if (captures == nullptr)
  883. {
  884. captures = RecyclerNewArray(recycler, Field(Var), numberOfCapturesToKeep + 1);
  885. }
  886. else if (numberOfCapturesToKeep != previousNumberOfCapturesToKeep)
  887. {
  888. size_t existingBytes = (previousNumberOfCapturesToKeep + 1) * sizeof(Var*);
  889. size_t requestedBytes = (numberOfCapturesToKeep + 1) * sizeof(Var*);
  890. captures = (Field(Var)*) recycler->Realloc(captures, existingBytes, requestedBytes);
  891. }
  892. previousNumberOfCapturesToKeep = numberOfCapturesToKeep;
  893. for (uint64 i = 1; i <= numberOfCaptures; ++i)
  894. {
  895. Var nextCapture = JavascriptOperators::GetItem(resultObj, i, scriptContext);
  896. if (!JavascriptOperators::IsUndefined(nextCapture))
  897. {
  898. nextCapture = JavascriptConversion::ToString(nextCapture, scriptContext);
  899. }
  900. if (i <= numberOfCapturesToKeep)
  901. {
  902. captures[i] = nextCapture;
  903. }
  904. }
  905. if (position >= nextSourcePosition)
  906. {
  907. CharCount substringLength = position - nextSourcePosition;
  908. accumulatedResultBuilder.Append(input, nextSourcePosition, substringLength);
  909. EnterPinnedScope((volatile void**)& matchStr);
  910. appendReplacement(accumulatedResultBuilder, tempAlloc, matchStr, (int) numberOfCapturesToKeep, (Var*)captures, position);
  911. LeavePinnedScope(); // matchStr
  912. nextSourcePosition = JavascriptRegExp::AddIndex(position, matchStr->GetLength());
  913. }
  914. });
  915. }
  916. END_TEMP_ALLOCATOR(tempAlloc, scriptContext);
  917. if (nextSourcePosition < inputLength)
  918. {
  919. CharCount substringLength = inputLength - nextSourcePosition;
  920. accumulatedResultBuilder.Append(input, nextSourcePosition, substringLength);
  921. }
  922. accumulatedResult = accumulatedResultBuilder.ToString();
  923. Assert(accumulatedResult != nullptr);
  924. return accumulatedResult;
  925. }
  926. // String.prototype.replace, replace value has been converted to a string (ES5 15.5.4.11)
  927. Var RegexHelper::RegexEs5ReplaceImpl(ScriptContext* scriptContext, JavascriptRegExp* regularExpression, JavascriptString* input, JavascriptString* replace, bool noResult)
  928. {
  929. UnifiedRegex::RegexPattern* pattern = regularExpression->GetPattern();
  930. const char16* replaceStr = replace->GetString();
  931. CharCount replaceLength = replace->GetLength();
  932. const char16* inputStr = input->GetString();
  933. CharCount inputLength = input->GetLength();
  934. JavascriptString* newString = nullptr;
  935. #if ENABLE_REGEX_CONFIG_OPTIONS
  936. RegexHelperTrace(scriptContext, UnifiedRegex::RegexStats::Replace, regularExpression, input, replace);
  937. #endif
  938. RegexMatchState state;
  939. PrimBeginMatch(state, scriptContext, pattern, inputStr, inputLength, true);
  940. UnifiedRegex::GroupInfo lastActualMatch;
  941. UnifiedRegex::GroupInfo lastSuccessfulMatch;
  942. const bool isGlobal = pattern->IsGlobal();
  943. const bool isSticky = pattern->IsSticky();
  944. // If global = false and sticky = true, set offset = lastIndex, else set offset = 0
  945. CharCount offset = 0;
  946. if (!isGlobal && isSticky)
  947. {
  948. offset = regularExpression->GetLastIndex();
  949. }
  950. if (!noResult)
  951. {
  952. CharCount* substitutionOffsets = nullptr;
  953. int substitutions = GetReplaceSubstitutions(replaceStr, replaceLength,
  954. state.tempAllocatorObj->GetAllocator(), &substitutionOffsets);
  955. // Use to see if we already have partial result populated in concatenated
  956. CompoundString::Builder<64 * sizeof(void *) / sizeof(char16)> concatenated(scriptContext);
  957. // If lastIndex > 0, append input[0..offset] characters to the result
  958. if (offset > 0)
  959. {
  960. concatenated.Append(input, 0, min(offset, inputLength));
  961. }
  962. do
  963. {
  964. if (offset > inputLength)
  965. {
  966. lastActualMatch.Reset();
  967. break;
  968. }
  969. lastActualMatch = PrimMatch(state, scriptContext, pattern, inputLength, offset);
  970. if (lastActualMatch.IsUndefined())
  971. break;
  972. lastSuccessfulMatch = lastActualMatch;
  973. concatenated.Append(input, offset, lastActualMatch.offset - offset);
  974. if (substitutionOffsets != 0)
  975. {
  976. auto getGroup = [&](int captureIndex, Var nonMatchValue) {
  977. return GetGroup(scriptContext, pattern, input, nonMatchValue, captureIndex);
  978. };
  979. const char16* matchedString = inputStr + lastActualMatch.offset;
  980. ReplaceFormatString(scriptContext, pattern->NumGroups(), getGroup, input, matchedString, lastActualMatch, replace, substitutions, substitutionOffsets, concatenated);
  981. }
  982. else
  983. {
  984. concatenated.Append(replace);
  985. }
  986. if (lastActualMatch.length == 0)
  987. {
  988. if (lastActualMatch.offset < inputLength)
  989. {
  990. concatenated.Append(inputStr[lastActualMatch.offset]);
  991. }
  992. offset = lastActualMatch.offset + 1;
  993. }
  994. else
  995. {
  996. offset = lastActualMatch.EndOffset();
  997. }
  998. }
  999. while (isGlobal);
  1000. if (offset == 0)
  1001. {
  1002. // There was no successful match so the result is the input string.
  1003. newString = input;
  1004. }
  1005. else
  1006. {
  1007. if (offset < inputLength)
  1008. {
  1009. concatenated.Append(input, offset, inputLength - offset);
  1010. }
  1011. newString = concatenated.ToString();
  1012. }
  1013. substitutionOffsets = 0;
  1014. }
  1015. else
  1016. {
  1017. do
  1018. {
  1019. if (offset > inputLength)
  1020. {
  1021. lastActualMatch.Reset();
  1022. break;
  1023. }
  1024. lastActualMatch = PrimMatch(state, scriptContext, pattern, inputLength, offset);
  1025. if (lastActualMatch.IsUndefined())
  1026. break;
  1027. lastSuccessfulMatch = lastActualMatch;
  1028. offset = lastActualMatch.length == 0? lastActualMatch.offset + 1 : lastActualMatch.EndOffset();
  1029. }
  1030. while (isGlobal);
  1031. newString = scriptContext->GetLibrary()->GetEmptyString();
  1032. }
  1033. PrimEndMatch(state, scriptContext, pattern);
  1034. PropagateLastMatch(scriptContext, isGlobal, isSticky, regularExpression, input, lastSuccessfulMatch, lastActualMatch, true, true);
  1035. return newString;
  1036. }
  1037. Var RegexHelper::RegexReplaceImpl(ScriptContext* scriptContext, RecyclableObject* thisObj, JavascriptString* input, JavascriptFunction* replacefn)
  1038. {
  1039. ScriptConfiguration const * scriptConfig = scriptContext->GetConfig();
  1040. if (scriptConfig->IsES6RegExSymbolsEnabled() && IsRegexSymbolReplaceObservable(thisObj, scriptContext))
  1041. {
  1042. return RegexEs6ReplaceImpl(scriptContext, thisObj, input, replacefn);
  1043. }
  1044. else
  1045. {
  1046. PCWSTR varName = scriptConfig->IsES6RegExSymbolsEnabled()
  1047. ? _u("RegExp.prototype[Symbol.replace]")
  1048. : _u("String.prototype.replace");
  1049. JavascriptRegExp* regularExpression = JavascriptRegExp::ToRegExp(thisObj, varName, scriptContext);
  1050. return RegexEs5ReplaceImpl(scriptContext, regularExpression, input, replacefn);
  1051. }
  1052. }
  1053. // String.prototype.replace, replace value is a function (ES5 15.5.4.11)
  1054. Var RegexHelper::RegexEs5ReplaceImpl(ScriptContext* scriptContext, JavascriptRegExp* regularExpression, JavascriptString* input, JavascriptFunction* replacefn)
  1055. {
  1056. UnifiedRegex::RegexPattern* pattern = regularExpression->GetPattern();
  1057. JavascriptString* newString = nullptr;
  1058. const char16* inputStr = input->GetString();
  1059. CharCount inputLength = input->GetLength();
  1060. const int numGroups = pattern->NumGroups();
  1061. Var nonMatchValue = NonMatchValue(scriptContext, false);
  1062. UnifiedRegex::GroupInfo lastMatch; // initially undefined
  1063. #if ENABLE_REGEX_CONFIG_OPTIONS
  1064. RegexHelperTrace(scriptContext, UnifiedRegex::RegexStats::Replace, regularExpression, input, scriptContext->GetLibrary()->CreateStringFromCppLiteral(_u("<replace function>")));
  1065. #endif
  1066. RegexMatchState state;
  1067. PrimBeginMatch(state, scriptContext, pattern, inputStr, inputLength, false);
  1068. // NOTE: These must be kept out of the scope of the try below!
  1069. const bool isGlobal = pattern->IsGlobal();
  1070. const bool isSticky = pattern->IsSticky();
  1071. // If global = true, set lastIndex to 0 in case it is used in replacefn
  1072. if (isGlobal)
  1073. {
  1074. regularExpression->SetLastIndex(0);
  1075. }
  1076. // If global = false and sticky = true, set offset = lastIndex, else set offset = 0
  1077. CharCount offset = 0;
  1078. if (!isGlobal && isSticky)
  1079. {
  1080. offset = regularExpression->GetLastIndex();
  1081. }
  1082. CompoundString::Builder<64 * sizeof(void *) / sizeof(char16)> concatenated(scriptContext);
  1083. UnifiedRegex::GroupInfo lastActualMatch;
  1084. UnifiedRegex::GroupInfo lastSuccessfulMatch;
  1085. // Replace function must be called with arguments (<function's this>, group0, ..., groupn, offset, input)
  1086. // The garbage collector must know about this array since it is being passed back into script land
  1087. Var* replaceArgs;
  1088. PROBE_STACK_NO_DISPOSE(scriptContext, (numGroups + 3) * sizeof(Var));
  1089. replaceArgs = (Var*)_alloca((numGroups + 3) * sizeof(Var));
  1090. replaceArgs[0] = scriptContext->GetLibrary()->GetUndefined();
  1091. replaceArgs[numGroups + 2] = input;
  1092. if (offset > 0)
  1093. {
  1094. concatenated.Append(input, 0, min(offset, inputLength));
  1095. }
  1096. do
  1097. {
  1098. if (offset > inputLength)
  1099. {
  1100. lastActualMatch.Reset();
  1101. break;
  1102. }
  1103. lastActualMatch = PrimMatch(state, scriptContext, pattern, inputLength, offset);
  1104. if (lastActualMatch.IsUndefined())
  1105. break;
  1106. lastSuccessfulMatch = lastActualMatch;
  1107. for (int groupId = 0; groupId < numGroups; groupId++)
  1108. replaceArgs[groupId + 1] = GetGroup(scriptContext, pattern, input, nonMatchValue, groupId);
  1109. #pragma prefast(suppress:6386, "The write index numGroups + 1 is in the bound")
  1110. replaceArgs[numGroups + 1] = JavascriptNumber::ToVar(lastActualMatch.offset, scriptContext);
  1111. // The called function must see the global state updated by the current match
  1112. // (Should the function reach into a RegExp field, the pattern will still be valid, thus there's no
  1113. // danger of the primitive regex matcher being re-entered)
  1114. // WARNING: We go off into script land here, which way in turn invoke a regex operation, even on the
  1115. // same regex.
  1116. ThreadContext* threadContext = scriptContext->GetThreadContext();
  1117. Var replaceVar = threadContext->ExecuteImplicitCall(replacefn, ImplicitCall_Accessor, [=]()->Js::Var
  1118. {
  1119. return replacefn->CallFunction(Arguments(CallInfo((ushort)(numGroups + 3)), replaceArgs));
  1120. });
  1121. JavascriptString* replace = JavascriptConversion::ToString(replaceVar, scriptContext);
  1122. concatenated.Append(input, offset, lastActualMatch.offset - offset);
  1123. concatenated.Append(replace);
  1124. if (lastActualMatch.length == 0)
  1125. {
  1126. if (lastActualMatch.offset < inputLength)
  1127. {
  1128. concatenated.Append(inputStr[lastActualMatch.offset]);
  1129. }
  1130. offset = lastActualMatch.offset + 1;
  1131. }
  1132. else
  1133. {
  1134. offset = lastActualMatch.EndOffset();
  1135. }
  1136. }
  1137. while (isGlobal);
  1138. PrimEndMatch(state, scriptContext, pattern);
  1139. if (offset == 0)
  1140. {
  1141. // There was no successful match so the result is the input string.
  1142. newString = input;
  1143. }
  1144. else
  1145. {
  1146. if (offset < inputLength)
  1147. {
  1148. concatenated.Append(input, offset, inputLength - offset);
  1149. }
  1150. newString = concatenated.ToString();
  1151. }
  1152. PropagateLastMatch(scriptContext, isGlobal, isSticky, regularExpression, input, lastSuccessfulMatch, lastActualMatch, true, true);
  1153. return newString;
  1154. }
  1155. Var RegexHelper::StringReplace(JavascriptString* match, JavascriptString* input, JavascriptString* replace)
  1156. {
  1157. CharCount matchedIndex = JavascriptString::strstr(input, match, true);
  1158. if (matchedIndex == CharCountFlag)
  1159. {
  1160. return input;
  1161. }
  1162. const char16 *const replaceStr = replace->GetString();
  1163. // Unfortunately, due to the possibility of there being $ escapes, we can't just wmemcpy the replace string. Check if we
  1164. // have a small replace string that we can quickly scan for '$', to see if we can just wmemcpy.
  1165. bool definitelyNoEscapes = replace->GetLength() == 0;
  1166. if(!definitelyNoEscapes && replace->GetLength() <= 8)
  1167. {
  1168. CharCount i = 0;
  1169. for(; i < replace->GetLength() && replaceStr[i] != _u('$'); ++i);
  1170. definitelyNoEscapes = i >= replace->GetLength();
  1171. }
  1172. if(definitelyNoEscapes)
  1173. {
  1174. const char16* inputStr = input->GetString();
  1175. const char16* prefixStr = inputStr;
  1176. CharCount prefixLength = (CharCount)matchedIndex;
  1177. const char16* postfixStr = inputStr + prefixLength + match->GetLength();
  1178. CharCount postfixLength = input->GetLength() - prefixLength - match->GetLength();
  1179. CharCount newLength = prefixLength + postfixLength + replace->GetLength();
  1180. BufferStringBuilder bufferString(newLength, match->GetScriptContext());
  1181. bufferString.SetContent(prefixStr, prefixLength,
  1182. replaceStr, replace->GetLength(),
  1183. postfixStr, postfixLength);
  1184. return bufferString.ToString();
  1185. }
  1186. CompoundString::Builder<64 * sizeof(void *) / sizeof(char16)> concatenated(input->GetScriptContext());
  1187. // Copy portion of input string that precedes the matched substring
  1188. concatenated.Append(input, 0, matchedIndex);
  1189. // Copy the replace string with substitutions
  1190. CharCount i = 0, j = 0;
  1191. for(; j < replace->GetLength(); ++j)
  1192. {
  1193. if(replaceStr[j] == _u('$') && j + 1 < replace->GetLength())
  1194. {
  1195. switch(replaceStr[j + 1])
  1196. {
  1197. case _u('$'): // literal '$'
  1198. ++j;
  1199. concatenated.Append(replace, i, j - i);
  1200. i = j + 1;
  1201. break;
  1202. case _u('&'): // matched substring
  1203. concatenated.Append(replace, i, j - i);
  1204. concatenated.Append(match);
  1205. ++j;
  1206. i = j + 1;
  1207. break;
  1208. case _u('`'): // portion of input string that precedes the matched substring
  1209. concatenated.Append(replace, i, j - i);
  1210. concatenated.Append(input, 0, matchedIndex);
  1211. ++j;
  1212. i = j + 1;
  1213. break;
  1214. case _u('\''): // portion of input string that follows the matched substring
  1215. concatenated.Append(replace, i, j - i);
  1216. concatenated.Append(
  1217. input,
  1218. matchedIndex + match->GetLength(),
  1219. input->GetLength() - matchedIndex - match->GetLength());
  1220. ++j;
  1221. i = j + 1;
  1222. break;
  1223. default: // take both the initial '$' and the following character literally
  1224. ++j;
  1225. }
  1226. }
  1227. }
  1228. Assert(i <= j);
  1229. concatenated.Append(replace, i, j - i);
  1230. // Copy portion of input string that follows the matched substring
  1231. concatenated.Append(input, matchedIndex + match->GetLength(), input->GetLength() - matchedIndex - match->GetLength());
  1232. return concatenated.ToString();
  1233. }
  1234. Var RegexHelper::StringReplace(ScriptContext* scriptContext, JavascriptString* match, JavascriptString* input, JavascriptFunction* replacefn)
  1235. {
  1236. CharCount indexMatched = JavascriptString::strstr(input, match, true);
  1237. Assert(match->GetScriptContext() == scriptContext);
  1238. Assert(input->GetScriptContext() == scriptContext);
  1239. if (indexMatched != CharCountFlag)
  1240. {
  1241. ThreadContext* threadContext = scriptContext->GetThreadContext();
  1242. Var replaceVar = threadContext->ExecuteImplicitCall(replacefn, ImplicitCall_Accessor, [=]()->Js::Var
  1243. {
  1244. Var pThis = scriptContext->GetLibrary()->GetUndefined();
  1245. return CALL_FUNCTION(threadContext, replacefn, CallInfo(4), pThis, match, JavascriptNumber::ToVar((int)indexMatched, scriptContext), input);
  1246. });
  1247. JavascriptString* replace = JavascriptConversion::ToString(replaceVar, scriptContext);
  1248. const char16* inputStr = input->GetString();
  1249. const char16* prefixStr = inputStr;
  1250. CharCount prefixLength = indexMatched;
  1251. const char16* postfixStr = inputStr + prefixLength + match->GetLength();
  1252. CharCount postfixLength = input->GetLength() - prefixLength - match->GetLength();
  1253. CharCount newLength = prefixLength + postfixLength + replace->GetLength();
  1254. BufferStringBuilder bufferString(newLength, match->GetScriptContext());
  1255. bufferString.SetContent(prefixStr, prefixLength,
  1256. replace->GetString(), replace->GetLength(),
  1257. postfixStr, postfixLength);
  1258. return bufferString.ToString();
  1259. }
  1260. return input;
  1261. }
  1262. void RegexHelper::AppendSubString(ScriptContext* scriptContext, JavascriptArray* ary, JavascriptString* input, CharCount startInclusive, CharCount endExclusive)
  1263. {
  1264. Assert(endExclusive >= startInclusive);
  1265. Assert(endExclusive <= input->GetLength());
  1266. CharCount length = endExclusive - startInclusive;
  1267. JavascriptString* subString;
  1268. if (length == 0)
  1269. {
  1270. subString = scriptContext->GetLibrary()->GetEmptyString();
  1271. }
  1272. else if (length == 1)
  1273. {
  1274. subString = scriptContext->GetLibrary()->GetCharStringCache().GetStringForChar(input->GetString()[startInclusive]);
  1275. }
  1276. else
  1277. {
  1278. subString = SubString::New(input, startInclusive, length);
  1279. }
  1280. ary->DirectAppendItem(subString);
  1281. }
  1282. inline UnifiedRegex::RegexPattern *RegexHelper::GetSplitPattern(ScriptContext* scriptContext, JavascriptRegExp *regularExpression)
  1283. {
  1284. UnifiedRegex::RegexPattern* splitPattern = regularExpression->GetSplitPattern();
  1285. if (!splitPattern)
  1286. {
  1287. UnifiedRegex::RegexPattern* pattern = regularExpression->GetPattern();
  1288. bool isSticky = (pattern->GetFlags() & UnifiedRegex::StickyRegexFlag) != 0;
  1289. if (!isSticky)
  1290. {
  1291. splitPattern = pattern;
  1292. }
  1293. else
  1294. {
  1295. // When the sticky flag is present, the pattern will match the input only at
  1296. // the beginning since "lastIndex" is set to 0 before the first iteration.
  1297. // However, for split(), we need to look for the pattern anywhere in the input.
  1298. //
  1299. // One way to handle this is to use the original pattern with the sticky flag and
  1300. // when it fails, move to the next character and retry.
  1301. //
  1302. // Another way, which is implemented here, is to create another pattern without the
  1303. // sticky flag and have it automatically look for itself anywhere in the input. This
  1304. // way, we can also take advantage of the optimizations for the global search (e.g.,
  1305. // the Boyer-Moore string search).
  1306. InternalString source = pattern->GetSource();
  1307. UnifiedRegex::RegexFlags nonStickyFlags =
  1308. static_cast<UnifiedRegex::RegexFlags>(pattern->GetFlags() & ~UnifiedRegex::StickyRegexFlag);
  1309. splitPattern = CompileDynamic(
  1310. scriptContext,
  1311. source.GetBuffer(),
  1312. source.GetLength(),
  1313. nonStickyFlags,
  1314. pattern->IsLiteral());
  1315. }
  1316. regularExpression->SetSplitPattern(splitPattern);
  1317. }
  1318. return splitPattern;
  1319. }
  1320. Var RegexHelper::RegexSplitImpl(ScriptContext* scriptContext, RecyclableObject* thisObj, JavascriptString* input, CharCount limit, bool noResult, void *const stackAllocationPointer)
  1321. {
  1322. ScriptConfiguration const * scriptConfig = scriptContext->GetConfig();
  1323. if (scriptConfig->IsES6RegExSymbolsEnabled()
  1324. && IsRegexSymbolSplitObservable(thisObj, scriptContext))
  1325. {
  1326. return RegexEs6SplitImpl(scriptContext, thisObj, input, limit, noResult, stackAllocationPointer);
  1327. }
  1328. else
  1329. {
  1330. PCWSTR varName = scriptContext->GetConfig()->IsES6RegExSymbolsEnabled()
  1331. ? _u("RegExp.prototype[Symbol.split]")
  1332. : _u("String.prototype.split");
  1333. JavascriptRegExp* regularExpression = JavascriptRegExp::ToRegExp(thisObj, varName, scriptContext);
  1334. return RegexEs5SplitImpl(scriptContext, regularExpression, input, limit, noResult, stackAllocationPointer);
  1335. }
  1336. }
  1337. bool RegexHelper::IsRegexSymbolSplitObservable(RecyclableObject* instance, ScriptContext* scriptContext)
  1338. {
  1339. DynamicObject* regexPrototype = scriptContext->GetLibrary()->GetRegExpPrototype();
  1340. return !JavascriptRegExp::HasOriginalRegExType(instance)
  1341. || JavascriptRegExp::HasObservableConstructor(regexPrototype)
  1342. || JavascriptRegExp::HasObservableFlags(regexPrototype)
  1343. || JavascriptRegExp::HasObservableExec(regexPrototype);
  1344. }
  1345. Var RegexHelper::RegexEs6SplitImpl(ScriptContext* scriptContext, RecyclableObject* thisObj, JavascriptString* input, CharCount limit, bool noResult, void *const stackAllocationPointer)
  1346. {
  1347. PCWSTR const varName = _u("RegExp.prototype[Symbol.split]");
  1348. Var speciesConstructor = JavascriptOperators::SpeciesConstructor(
  1349. thisObj,
  1350. scriptContext->GetLibrary()->GetRegExpConstructor(),
  1351. scriptContext);
  1352. JavascriptString* flags = JavascriptConversion::ToString(
  1353. JavascriptOperators::GetProperty(thisObj, PropertyIds::flags, scriptContext),
  1354. scriptContext);
  1355. bool unicode = wcsstr(flags->GetString(), _u("u")) != nullptr;
  1356. flags = AppendStickyToFlagsIfNeeded(flags, scriptContext);
  1357. Js::Var args[] = { speciesConstructor, thisObj, flags };
  1358. Js::CallInfo callInfo(Js::CallFlags_New, _countof(args));
  1359. Var regEx = JavascriptOperators::NewScObject(
  1360. speciesConstructor,
  1361. Js::Arguments(callInfo, args),
  1362. scriptContext);
  1363. RecyclableObject* splitter = RecyclableObject::FromVar(regEx);
  1364. JavascriptArray* arrayResult = scriptContext->GetLibrary()->CreateArray();
  1365. if (limit == 0)
  1366. {
  1367. return arrayResult;
  1368. }
  1369. CharCount inputLength = input->GetLength();
  1370. if (inputLength == 0)
  1371. {
  1372. Var result = JavascriptRegExp::CallExec(splitter, input, varName, scriptContext);
  1373. if (!JavascriptOperators::IsNull(result))
  1374. {
  1375. return arrayResult;
  1376. }
  1377. arrayResult->DirectAppendItem(input);
  1378. return arrayResult;
  1379. }
  1380. CharCount substringStartIndex = 0; // 'p' in spec
  1381. CharCount substringEndIndex = substringStartIndex; // 'q' in spec
  1382. do // inputLength > 0
  1383. {
  1384. JavascriptRegExp::SetLastIndexProperty(splitter, substringEndIndex, scriptContext);
  1385. Var result = JavascriptRegExp::CallExec(splitter, input, varName, scriptContext); // 'z' in spec
  1386. if (JavascriptOperators::IsNull(result))
  1387. {
  1388. substringEndIndex = AdvanceStringIndex(input, substringEndIndex, unicode);
  1389. }
  1390. else
  1391. {
  1392. CharCount endIndex = JavascriptRegExp::GetLastIndexProperty(splitter, scriptContext); // 'e' in spec
  1393. endIndex = min(endIndex, inputLength);
  1394. if (endIndex == substringStartIndex)
  1395. {
  1396. substringEndIndex = AdvanceStringIndex(input, substringEndIndex, unicode);
  1397. }
  1398. else
  1399. {
  1400. AppendSubString(scriptContext, arrayResult, input, substringStartIndex, substringEndIndex);
  1401. if (arrayResult->GetLength() == limit)
  1402. {
  1403. return arrayResult;
  1404. }
  1405. substringStartIndex = endIndex;
  1406. RecyclableObject* resultObject = ExecResultToRecyclableObject(result);
  1407. int64 length = JavascriptConversion::ToLength(
  1408. JavascriptOperators::GetProperty(resultObject, PropertyIds::length, scriptContext),
  1409. scriptContext);
  1410. uint64 numberOfCaptures = max(length - 1, (int64) 0);
  1411. for (uint64 i = 1; i <= numberOfCaptures; ++i)
  1412. {
  1413. Var nextCapture = JavascriptOperators::GetItem(resultObject, i, scriptContext);
  1414. arrayResult->DirectAppendItem(nextCapture);
  1415. if (arrayResult->GetLength() == limit)
  1416. {
  1417. return arrayResult;
  1418. }
  1419. }
  1420. substringEndIndex = substringStartIndex;
  1421. }
  1422. }
  1423. }
  1424. while (substringEndIndex < inputLength);
  1425. AppendSubString(scriptContext, arrayResult, input, substringStartIndex, substringEndIndex);
  1426. return arrayResult;
  1427. }
  1428. JavascriptString* RegexHelper::AppendStickyToFlagsIfNeeded(JavascriptString* flags, ScriptContext* scriptContext)
  1429. {
  1430. const char16* flagsString = flags->GetString();
  1431. if (wcsstr(flagsString, _u("y")) == nullptr)
  1432. {
  1433. BEGIN_TEMP_ALLOCATOR(tempAlloc, scriptContext, _u("RegexHelper"))
  1434. {
  1435. StringBuilder<ArenaAllocator> bs(tempAlloc, flags->GetLength() + 1);
  1436. bs.Append(flagsString, flags->GetLength());
  1437. bs.Append(_u('y'));
  1438. flags = Js::JavascriptString::NewCopyBuffer(bs.Detach(), bs.Count(), scriptContext);
  1439. }
  1440. END_TEMP_ALLOCATOR(tempAlloc, scriptContext);
  1441. }
  1442. return flags;
  1443. }
  1444. // String.prototype.split (ES5 15.5.4.14)
  1445. Var RegexHelper::RegexEs5SplitImpl(ScriptContext* scriptContext, JavascriptRegExp* regularExpression, JavascriptString* input, CharCount limit, bool noResult, void *const stackAllocationPointer)
  1446. {
  1447. if (noResult && scriptContext->GetConfig()->SkipSplitOnNoResult())
  1448. {
  1449. // TODO: Fix this so that the side effect for PropagateLastMatch is done
  1450. return scriptContext->GetLibrary()->GetNull();
  1451. }
  1452. #if ENABLE_REGEX_CONFIG_OPTIONS
  1453. RegexHelperTrace(scriptContext, UnifiedRegex::RegexStats::Split, regularExpression, input);
  1454. #endif
  1455. JavascriptArray* ary = scriptContext->GetLibrary()->CreateArrayOnStack(stackAllocationPointer);
  1456. if (limit == 0)
  1457. {
  1458. // SPECIAL CASE: Zero limit
  1459. return ary;
  1460. }
  1461. UnifiedRegex::RegexPattern *splitPattern = GetSplitPattern(scriptContext, regularExpression);
  1462. const char16* inputStr = input->GetString();
  1463. CharCount inputLength = input->GetLength(); // s in spec
  1464. const int numGroups = splitPattern->NumGroups();
  1465. Var nonMatchValue = NonMatchValue(scriptContext, false);
  1466. UnifiedRegex::GroupInfo lastSuccessfulMatch; // initially undefined
  1467. RegexMatchState state;
  1468. PrimBeginMatch(state, scriptContext, splitPattern, inputStr, inputLength, false);
  1469. if (inputLength == 0)
  1470. {
  1471. // SPECIAL CASE: Empty string
  1472. UnifiedRegex::GroupInfo match = PrimMatch(state, scriptContext, splitPattern, inputLength, 0);
  1473. if (match.IsUndefined())
  1474. ary->DirectAppendItem(input);
  1475. else
  1476. lastSuccessfulMatch = match;
  1477. }
  1478. else
  1479. {
  1480. CharCount copyOffset = 0; // p in spec
  1481. CharCount startOffset = 0; // q in spec
  1482. CharCount inputLimit = inputLength;
  1483. while (startOffset < inputLimit)
  1484. {
  1485. UnifiedRegex::GroupInfo match = PrimMatch(state, scriptContext, splitPattern, inputLength, startOffset);
  1486. if (match.IsUndefined())
  1487. break;
  1488. lastSuccessfulMatch = match;
  1489. if (match.offset >= inputLimit)
  1490. break;
  1491. startOffset = match.offset;
  1492. CharCount endOffset = match.EndOffset(); // e in spec
  1493. if (endOffset == copyOffset)
  1494. startOffset++;
  1495. else
  1496. {
  1497. AppendSubString(scriptContext, ary, input, copyOffset, startOffset);
  1498. if (ary->GetLength() >= limit)
  1499. break;
  1500. startOffset = copyOffset = endOffset;
  1501. for (int groupId = 1; groupId < numGroups; groupId++)
  1502. {
  1503. ary->DirectAppendItem(GetGroup(scriptContext, splitPattern, input, nonMatchValue, groupId));
  1504. if (ary->GetLength() >= limit)
  1505. break;
  1506. }
  1507. }
  1508. }
  1509. if (ary->GetLength() < limit)
  1510. AppendSubString(scriptContext, ary, input, copyOffset, inputLength);
  1511. }
  1512. PrimEndMatch(state, scriptContext, splitPattern);
  1513. Assert(!splitPattern->IsSticky());
  1514. PropagateLastMatch
  1515. ( scriptContext
  1516. , splitPattern->IsGlobal()
  1517. , /* isSticky */ false
  1518. , regularExpression
  1519. , input
  1520. , lastSuccessfulMatch
  1521. , UnifiedRegex::GroupInfo()
  1522. , /* updateRegex */ true
  1523. , /* updateCtor */ true
  1524. , /* useSplitPattern */ true );
  1525. return ary;
  1526. }
  1527. UnifiedRegex::GroupInfo
  1528. RegexHelper::SimpleMatch(ScriptContext * scriptContext, UnifiedRegex::RegexPattern * pattern, const char16 * input, CharCount inputLength, CharCount offset)
  1529. {
  1530. RegexMatchState state;
  1531. PrimBeginMatch(state, scriptContext, pattern, input, inputLength, false);
  1532. UnifiedRegex::GroupInfo match = PrimMatch(state, scriptContext, pattern, inputLength, offset);
  1533. PrimEndMatch(state, scriptContext, pattern);
  1534. return match;
  1535. }
  1536. // String.prototype.search (ES5 15.5.4.12)
  1537. Var RegexHelper::RegexSearchImpl(ScriptContext* scriptContext, JavascriptRegExp* regularExpression, JavascriptString* input)
  1538. {
  1539. UnifiedRegex::RegexPattern* pattern = regularExpression->GetPattern();
  1540. const char16* inputStr = input->GetString();
  1541. CharCount inputLength = input->GetLength();
  1542. #if ENABLE_REGEX_CONFIG_OPTIONS
  1543. RegexHelperTrace(scriptContext, UnifiedRegex::RegexStats::Search, regularExpression, input);
  1544. #endif
  1545. UnifiedRegex::GroupInfo match = RegexHelper::SimpleMatch(scriptContext, pattern, inputStr, inputLength, 0);
  1546. PropagateLastMatch(scriptContext, pattern->IsGlobal(), pattern->IsSticky(), regularExpression, input, match, match, false, true);
  1547. return JavascriptNumber::ToVar(match.IsUndefined() ? -1 : (int32)match.offset, scriptContext);
  1548. }
  1549. // String.prototype.split (ES5 15.5.4.14)
  1550. Var RegexHelper::StringSplit(JavascriptString* match, JavascriptString* input, CharCount limit)
  1551. {
  1552. ScriptContext* scriptContext = match->GetScriptContext();
  1553. JavascriptArray* ary;
  1554. CharCount matchLen = match->GetLength();
  1555. if (matchLen == 0)
  1556. {
  1557. CharCount count = min(input->GetLength(), limit);
  1558. ary = scriptContext->GetLibrary()->CreateArray(count);
  1559. const char16 * charString = input->GetString();
  1560. for (CharCount i = 0; i < count; i++)
  1561. {
  1562. ary->DirectSetItemAt(i, scriptContext->GetLibrary()->GetCharStringCache().GetStringForChar(charString[i]));
  1563. }
  1564. }
  1565. else
  1566. {
  1567. CharCount i = 0;
  1568. CharCount offset = 0;
  1569. ary = scriptContext->GetLibrary()->CreateArray(0);
  1570. while (i < limit)
  1571. {
  1572. CharCount prevOffset = offset;
  1573. offset = JavascriptString::strstr(input, match, false, prevOffset);
  1574. if (offset != CharCountFlag)
  1575. {
  1576. ary->DirectSetItemAt(i++, SubString::New(input, prevOffset, offset-prevOffset));
  1577. offset += max(matchLen, static_cast<CharCount>(1));
  1578. if (offset > input->GetLength())
  1579. break;
  1580. }
  1581. else
  1582. {
  1583. ary->DirectSetItemAt(i++, SubString::New(input, prevOffset, input->GetLength() - prevOffset));
  1584. break;
  1585. }
  1586. }
  1587. }
  1588. return ary;
  1589. }
  1590. bool RegexHelper::IsResultNotUsed(CallFlags flags)
  1591. {
  1592. return !PHASE_OFF1(Js::RegexResultNotUsedPhase) && ((flags & CallFlags_NotUsed) != 0);
  1593. }
  1594. // ----------------------------------------------------------------------
  1595. // Primitives
  1596. // ----------------------------------------------------------------------
  1597. void RegexHelper::PrimBeginMatch(RegexMatchState& state, ScriptContext* scriptContext, UnifiedRegex::RegexPattern* pattern, const char16* input, CharCount inputLength, bool alwaysNeedAlloc)
  1598. {
  1599. state.input = input;
  1600. if (pattern->rep.unified.matcher == 0)
  1601. pattern->rep.unified.matcher = UnifiedRegex::Matcher::New(scriptContext, pattern);
  1602. if (alwaysNeedAlloc)
  1603. state.tempAllocatorObj = scriptContext->GetTemporaryAllocator(_u("RegexUnifiedExecTemp"));
  1604. else
  1605. state.tempAllocatorObj = 0;
  1606. }
  1607. UnifiedRegex::GroupInfo
  1608. RegexHelper::PrimMatch(RegexMatchState& state, ScriptContext* scriptContext, UnifiedRegex::RegexPattern* pattern, CharCount inputLength, CharCount offset)
  1609. {
  1610. Assert(pattern->rep.unified.program != 0);
  1611. Assert(pattern->rep.unified.matcher != 0);
  1612. #if ENABLE_REGEX_CONFIG_OPTIONS
  1613. UnifiedRegex::RegexStats* stats = 0;
  1614. if (REGEX_CONFIG_FLAG(RegexProfile))
  1615. {
  1616. stats = scriptContext->GetRegexStatsDatabase()->GetRegexStats(pattern);
  1617. scriptContext->GetRegexStatsDatabase()->BeginProfile();
  1618. }
  1619. UnifiedRegex::DebugWriter* w = 0;
  1620. if (REGEX_CONFIG_FLAG(RegexTracing) && CONFIG_FLAG(Verbose))
  1621. w = scriptContext->GetRegexDebugWriter();
  1622. #endif
  1623. pattern->rep.unified.matcher->Match
  1624. (state.input
  1625. , inputLength
  1626. , offset
  1627. , scriptContext
  1628. #if ENABLE_REGEX_CONFIG_OPTIONS
  1629. , stats
  1630. , w
  1631. #endif
  1632. );
  1633. #if ENABLE_REGEX_CONFIG_OPTIONS
  1634. if (REGEX_CONFIG_FLAG(RegexProfile))
  1635. scriptContext->GetRegexStatsDatabase()->EndProfile(stats, UnifiedRegex::RegexStats::Execute);
  1636. #endif
  1637. return pattern->GetGroup(0);
  1638. }
  1639. void RegexHelper::PrimEndMatch(RegexMatchState& state, ScriptContext* scriptContext, UnifiedRegex::RegexPattern* pattern)
  1640. {
  1641. if (state.tempAllocatorObj != 0)
  1642. scriptContext->ReleaseTemporaryAllocator(state.tempAllocatorObj);
  1643. }
  1644. Var RegexHelper::NonMatchValue(ScriptContext* scriptContext, bool isGlobalCtor)
  1645. {
  1646. // SPEC DEVIATION: The $n properties of the RegExp ctor use empty strings rather than undefined to represent
  1647. // the non-match value, even in ES5 mode.
  1648. if (isGlobalCtor)
  1649. return scriptContext->GetLibrary()->GetEmptyString();
  1650. else
  1651. return scriptContext->GetLibrary()->GetUndefined();
  1652. }
  1653. Var RegexHelper::GetString(ScriptContext* scriptContext, JavascriptString* input, Var nonMatchValue, UnifiedRegex::GroupInfo group)
  1654. {
  1655. if (group.IsUndefined())
  1656. return nonMatchValue;
  1657. switch (group.length)
  1658. {
  1659. case 0:
  1660. return scriptContext->GetLibrary()->GetEmptyString();
  1661. case 1:
  1662. {
  1663. const char16* inputStr = input->GetString();
  1664. return scriptContext->GetLibrary()->GetCharStringCache().GetStringForChar(inputStr[group.offset]);
  1665. }
  1666. case 2:
  1667. {
  1668. const char16* inputStr = input->GetString();
  1669. PropertyString* propString = scriptContext->GetPropertyString2(inputStr[group.offset], inputStr[group.offset + 1]);
  1670. if (propString != 0)
  1671. return propString;
  1672. // fall-through for default
  1673. }
  1674. default:
  1675. return SubString::New(input, group.offset, group.length);
  1676. }
  1677. }
  1678. Var RegexHelper::GetGroup(ScriptContext* scriptContext, UnifiedRegex::RegexPattern* pattern, JavascriptString* input, Var nonMatchValue, int groupId)
  1679. {
  1680. return GetString(scriptContext, input, nonMatchValue, pattern->GetGroup(groupId));
  1681. }
  1682. // ======================================================================
  1683. // Match results propagate into three places:
  1684. // - The match result array. Generally the array has string entries for the overall match substring,
  1685. // followed by final bindings for each group, plus the fields:
  1686. // - 'input': string used in match
  1687. // - 'index': index of first character of match in input
  1688. // - 'lastIndex' (IE extension): one plus index of last character of match in input
  1689. // However, for String.match with a global match, the result is an array of all match results
  1690. // (ignoring any group bindings). But in IE8 mode we also bind the above fields to that array,
  1691. // using the results of the last successful primitive match.
  1692. // - The regular expression object has writable field:
  1693. // - 'lastIndex': one plus index of last character of last match in last input
  1694. // - 'lastInput
  1695. // - (Host extension) The RegExp constructor object has fields:
  1696. // - '$n': last match substrings, using "" for undefined in all modes
  1697. // - etc (see JavascriptRegExpConstructorType.cpp)
  1698. //
  1699. // There are also three influences on what gets propagated where and when:
  1700. // - Whether the regular expression is global
  1701. // - Whether the primitive operations runs the regular expression until failure (e.g. String.match) or
  1702. // just once (e.g. RegExp.exec), or use the underlying matching machinery implicitly (e.g. String.split).
  1703. //
  1704. // Here are the rules:
  1705. // - RegExp is updated for the last *successful* primitive match, except for String.replace.
  1706. // In particular, for String.match with a global regex, the final failing match *does not* reset RegExp.
  1707. // - Except for String.search in EC5 mode (which does not update 'lastIndex'), the regular expressions
  1708. // lastIndex is updated as follows:
  1709. // - ES5 mode, if a primitive match fails then the regular expression 'lastIndex' is set to 0. In particular,
  1710. // the final failing primitive match for String.match with a global regex forces 'lastIndex' to be reset.
  1711. // However, if a primitive match succeeds then the regular expression 'lastIndex' is updated only for
  1712. // a global regex.
  1713. // for success. However:
  1714. // - The last failing match in a String.match with a global regex does NOT reset 'lastIndex'.
  1715. // - If the regular expression matched empty, the last index is set assuming the pattern actually matched
  1716. // one input character. This applies even if the pattern matched empty one beyond the end of the string
  1717. // in a String.match with a global regex (!). For our own sanity, we isolate this particular case
  1718. // within JavascriptRegExp when setting the lastIndexVar value.
  1719. // - In all modes, 'lastIndex' determines the starting search index only for global regular expressions.
  1720. //
  1721. // ======================================================================
  1722. void RegexHelper::PropagateLastMatch
  1723. ( ScriptContext* scriptContext
  1724. , bool isGlobal
  1725. , bool isSticky
  1726. , JavascriptRegExp* regularExpression
  1727. , JavascriptString* lastInput
  1728. , UnifiedRegex::GroupInfo lastSuccessfulMatch
  1729. , UnifiedRegex::GroupInfo lastActualMatch
  1730. , bool updateRegex
  1731. , bool updateCtor
  1732. , bool useSplitPattern )
  1733. {
  1734. if (updateRegex)
  1735. {
  1736. PropagateLastMatchToRegex(scriptContext, isGlobal, isSticky, regularExpression, lastSuccessfulMatch, lastActualMatch);
  1737. }
  1738. if (updateCtor)
  1739. {
  1740. PropagateLastMatchToCtor(scriptContext, regularExpression, lastInput, lastSuccessfulMatch, useSplitPattern);
  1741. }
  1742. }
  1743. void RegexHelper::PropagateLastMatchToRegex
  1744. ( ScriptContext* scriptContext
  1745. , bool isGlobal
  1746. , bool isSticky
  1747. , JavascriptRegExp* regularExpression
  1748. , UnifiedRegex::GroupInfo lastSuccessfulMatch
  1749. , UnifiedRegex::GroupInfo lastActualMatch )
  1750. {
  1751. if (lastActualMatch.IsUndefined())
  1752. {
  1753. regularExpression->SetLastIndex(0);
  1754. }
  1755. else if (isGlobal || isSticky)
  1756. {
  1757. CharCount lastIndex = lastActualMatch.EndOffset();
  1758. Assert(lastIndex <= MaxCharCount);
  1759. regularExpression->SetLastIndex((int32)lastIndex);
  1760. }
  1761. }
  1762. void RegexHelper::PropagateLastMatchToCtor
  1763. ( ScriptContext* scriptContext
  1764. , JavascriptRegExp* regularExpression
  1765. , JavascriptString* lastInput
  1766. , UnifiedRegex::GroupInfo lastSuccessfulMatch
  1767. , bool useSplitPattern )
  1768. {
  1769. Assert(lastInput);
  1770. if (!lastSuccessfulMatch.IsUndefined())
  1771. {
  1772. // Notes:
  1773. // - SPEC DEVIATION: The RegExp ctor holds some details of the last successful match on any regular expression.
  1774. // - For updating regex ctor's stats we are using entry function's context, rather than regex context,
  1775. // the rational is: use same context of RegExp.prototype, on which the function was called.
  1776. // So, if you call the function with remoteContext.regexInstance.exec.call(localRegexInstance, "match string"),
  1777. // we will update stats in the context related to the exec function, i.e. remoteContext.
  1778. // This is consistent with other browsers
  1779. UnifiedRegex::RegexPattern* pattern = useSplitPattern
  1780. ? regularExpression->GetSplitPattern()
  1781. : regularExpression->GetPattern();
  1782. scriptContext->GetLibrary()->GetRegExpConstructor()->SetLastMatch(pattern, lastInput, lastSuccessfulMatch);
  1783. }
  1784. }
  1785. bool RegexHelper::GetInitialOffset(bool isGlobal, bool isSticky, JavascriptRegExp* regularExpression, CharCount inputLength, CharCount& offset)
  1786. {
  1787. if (isGlobal || isSticky)
  1788. {
  1789. offset = regularExpression->GetLastIndex();
  1790. if (offset <= MaxCharCount)
  1791. return true;
  1792. else
  1793. {
  1794. regularExpression->SetLastIndex(0);
  1795. return false;
  1796. }
  1797. }
  1798. else
  1799. {
  1800. offset = 0;
  1801. return true;
  1802. }
  1803. }
  1804. JavascriptArray* RegexHelper::CreateMatchResult(void *const stackAllocationPointer, ScriptContext* scriptContext, bool isGlobal, int numGroups, JavascriptString* input)
  1805. {
  1806. if (isGlobal)
  1807. {
  1808. // Use an ordinary array, with default initial capacity
  1809. return scriptContext->GetLibrary()->CreateArrayOnStack(stackAllocationPointer);
  1810. }
  1811. else
  1812. return JavascriptRegularExpressionResult::Create(stackAllocationPointer, numGroups, input, scriptContext);
  1813. }
  1814. void RegexHelper::FinalizeMatchResult(ScriptContext* scriptContext, bool isGlobal, JavascriptArray* arr, UnifiedRegex::GroupInfo match)
  1815. {
  1816. if (!isGlobal)
  1817. JavascriptRegularExpressionResult::SetMatch(arr, match);
  1818. // else: arr is an ordinary array
  1819. }
  1820. JavascriptArray* RegexHelper::CreateExecResult(void *const stackAllocationPointer, ScriptContext* scriptContext, int numGroups, JavascriptString* input, UnifiedRegex::GroupInfo match)
  1821. {
  1822. JavascriptArray* res = JavascriptRegularExpressionResult::Create(stackAllocationPointer, numGroups, input, scriptContext);
  1823. JavascriptRegularExpressionResult::SetMatch(res, match);
  1824. return res;
  1825. }
  1826. template<bool mustMatchEntireInput>
  1827. BOOL RegexHelper::RegexTest_NonScript(ScriptContext* scriptContext, JavascriptRegExp* regularExpression, const char16 *const input, const CharCount inputLength)
  1828. {
  1829. // This version of the function should only be used when testing the regex against a non-javascript string. That is,
  1830. // this call was not initiated by script code. Hence, the RegExp constructor is not updated with the last match. If
  1831. // 'mustMatchEntireInput' is true, this function also ignores the global/sticky flag and the lastIndex property, since it tests
  1832. // for a match on the entire input string; in that case, the lastIndex property is not modified.
  1833. UnifiedRegex::RegexPattern* pattern = regularExpression->GetPattern();
  1834. UnifiedRegex::GroupInfo match; // initially undefined
  1835. #if ENABLE_REGEX_CONFIG_OPTIONS
  1836. RegexHelperTrace(scriptContext, UnifiedRegex::RegexStats::Test, regularExpression, input, inputLength);
  1837. #endif
  1838. const bool isGlobal = pattern->IsGlobal();
  1839. const bool isSticky = pattern->IsSticky();
  1840. CharCount offset;
  1841. if (mustMatchEntireInput)
  1842. offset = 0; // needs to match the entire input, so ignore 'lastIndex' and always start from the beginning
  1843. else if (!GetInitialOffset(isGlobal, isSticky, regularExpression, inputLength, offset))
  1844. return false;
  1845. if (mustMatchEntireInput || offset <= inputLength)
  1846. {
  1847. match = RegexHelper::SimpleMatch(scriptContext, pattern, input, inputLength, offset);
  1848. }
  1849. // else: match remains undefined
  1850. if (!mustMatchEntireInput) // don't update 'lastIndex' when mustMatchEntireInput is true since the global flag is ignored
  1851. {
  1852. PropagateLastMatchToRegex(scriptContext, isGlobal, isSticky, regularExpression, match, match);
  1853. }
  1854. return mustMatchEntireInput ? match.offset == 0 && match.length == inputLength : !match.IsUndefined();
  1855. }
  1856. // explicit instantiation
  1857. template BOOL RegexHelper::RegexTest_NonScript<true>(ScriptContext* scriptContext, JavascriptRegExp* regularExpression, const char16 *const input, const CharCount inputLength);
  1858. template BOOL RegexHelper::RegexTest_NonScript<false>(ScriptContext* scriptContext, JavascriptRegExp* regularExpression, const char16 *const input, const CharCount inputLength);
  1859. // Asserts if the value needs to be marshaled to target context.
  1860. // Returns the resulting value.
  1861. // This is supposed to be called for result/return value of the RegexXXX functions.
  1862. // static
  1863. template<typename T>
  1864. T RegexHelper::CheckCrossContextAndMarshalResult(T value, ScriptContext* targetContext)
  1865. {
  1866. Assert(targetContext);
  1867. Assert(!CrossSite::NeedMarshalVar(value, targetContext));
  1868. return value;
  1869. }
  1870. Var RegexHelper::RegexMatchResultUsed(ScriptContext* scriptContext, JavascriptRegExp* regularExpression, JavascriptString* input)
  1871. {
  1872. return RegexHelper::RegexMatch(scriptContext, regularExpression, input, false);
  1873. }
  1874. Var RegexHelper::RegexMatchResultUsedAndMayBeTemp(void *const stackAllocationPointer, ScriptContext* scriptContext, JavascriptRegExp* regularExpression, JavascriptString* input)
  1875. {
  1876. return RegexHelper::RegexMatch(scriptContext, regularExpression, input, false, stackAllocationPointer);
  1877. }
  1878. Var RegexHelper::RegexMatchResultNotUsed(ScriptContext* scriptContext, JavascriptRegExp* regularExpression, JavascriptString* input)
  1879. {
  1880. if (!PHASE_OFF1(Js::RegexResultNotUsedPhase))
  1881. {
  1882. return RegexHelper::RegexMatch(scriptContext, regularExpression, input, true);
  1883. }
  1884. else
  1885. {
  1886. return RegexHelper::RegexMatch(scriptContext, regularExpression, input, false);
  1887. }
  1888. }
  1889. Var RegexHelper::RegexMatch(ScriptContext* entryFunctionContext, RecyclableObject *thisObj, JavascriptString *input, bool noResult, void *const stackAllocationPointer)
  1890. {
  1891. Var result = RegexHelper::RegexMatchImpl<true>(entryFunctionContext, thisObj, input, noResult, stackAllocationPointer);
  1892. return RegexHelper::CheckCrossContextAndMarshalResult(result, entryFunctionContext);
  1893. }
  1894. Var RegexHelper::RegexMatchNoHistory(ScriptContext* entryFunctionContext, JavascriptRegExp *regularExpression, JavascriptString *input, bool noResult)
  1895. {
  1896. // RegexMatchNoHistory() is used only by Intl internally and there is no need for ES6
  1897. // observable RegExp actions. Therefore, we can directly use the ES5 logic.
  1898. Var result = RegexHelper::RegexEs5MatchImpl<false>(entryFunctionContext, regularExpression, input, noResult);
  1899. return RegexHelper::CheckCrossContextAndMarshalResult(result, entryFunctionContext);
  1900. }
  1901. Var RegexHelper::RegexExecResultUsed(ScriptContext* scriptContext, JavascriptRegExp* regularExpression, JavascriptString* input)
  1902. {
  1903. return RegexHelper::RegexExec(scriptContext, regularExpression, input, false);
  1904. }
  1905. Var RegexHelper::RegexExecResultUsedAndMayBeTemp(void *const stackAllocationPointer, ScriptContext* scriptContext, JavascriptRegExp* regularExpression, JavascriptString* input)
  1906. {
  1907. return RegexHelper::RegexExec(scriptContext, regularExpression, input, false, stackAllocationPointer);
  1908. }
  1909. Var RegexHelper::RegexExecResultNotUsed(ScriptContext* scriptContext, JavascriptRegExp* regularExpression, JavascriptString* input)
  1910. {
  1911. if (!PHASE_OFF1(Js::RegexResultNotUsedPhase))
  1912. {
  1913. return RegexHelper::RegexExec(scriptContext, regularExpression, input, true);
  1914. }
  1915. else
  1916. {
  1917. return RegexHelper::RegexExec(scriptContext, regularExpression, input, false);
  1918. }
  1919. }
  1920. Var RegexHelper::RegexExec(ScriptContext* entryFunctionContext, JavascriptRegExp* regularExpression, JavascriptString* input, bool noResult, void *const stackAllocationPointer)
  1921. {
  1922. Var result = RegexHelper::RegexExecImpl(entryFunctionContext, regularExpression, input, noResult, stackAllocationPointer);
  1923. return RegexHelper::CheckCrossContextAndMarshalResult(result, entryFunctionContext);
  1924. }
  1925. Var RegexHelper::RegexReplaceResultUsed(ScriptContext* entryFunctionContext, JavascriptRegExp* regularExpression, JavascriptString* input, JavascriptString* replace)
  1926. {
  1927. return entryFunctionContext->GetConfig()->IsES6RegExSymbolsEnabled()
  1928. ? RegexHelper::RegexReplace(entryFunctionContext, regularExpression, input, replace, false)
  1929. : RegexHelper::RegexEs5Replace(entryFunctionContext, regularExpression, input, replace, false);
  1930. }
  1931. Var RegexHelper::RegexReplaceResultNotUsed(ScriptContext* entryFunctionContext, JavascriptRegExp* regularExpression, JavascriptString* input, JavascriptString* replace)
  1932. {
  1933. if (!PHASE_OFF1(Js::RegexResultNotUsedPhase))
  1934. {
  1935. return entryFunctionContext->GetConfig()->IsES6RegExSymbolsEnabled()
  1936. ? RegexHelper::RegexReplace(entryFunctionContext, regularExpression, input, replace, true)
  1937. : RegexHelper::RegexEs5Replace(entryFunctionContext, regularExpression, input, replace, true);
  1938. }
  1939. else
  1940. {
  1941. return entryFunctionContext->GetConfig()->IsES6RegExSymbolsEnabled()
  1942. ? RegexHelper::RegexReplace(entryFunctionContext, regularExpression, input, replace, false)
  1943. : RegexHelper::RegexEs5Replace(entryFunctionContext, regularExpression, input, replace, false);
  1944. }
  1945. }
  1946. Var RegexHelper::RegexReplace(ScriptContext* entryFunctionContext, RecyclableObject* thisObj, JavascriptString* input, JavascriptString* replace, bool noResult)
  1947. {
  1948. Var result = RegexHelper::RegexReplaceImpl(entryFunctionContext, thisObj, input, replace, noResult);
  1949. return RegexHelper::CheckCrossContextAndMarshalResult(result, entryFunctionContext);
  1950. }
  1951. Var RegexHelper::RegexEs5Replace(ScriptContext* entryFunctionContext, JavascriptRegExp* regularExpression, JavascriptString* input, JavascriptString* replace, bool noResult)
  1952. {
  1953. // We can have RegexReplaceResult... functions defer their job to RegexReplace. However, their regularExpression argument
  1954. // would first be cast to RecyclableObject when the call is made, and then back to JavascriptRegExp in RegexReplaceImpl.
  1955. // The conversion back slows down the perf, so we use this ES5 version of RegexReplace in RegexReplaceResult... if we know
  1956. // that the ES6 logic isn't needed.
  1957. Var result = RegexHelper::RegexEs5ReplaceImpl(entryFunctionContext, regularExpression, input, replace, noResult);
  1958. return RegexHelper::CheckCrossContextAndMarshalResult(result, entryFunctionContext);
  1959. }
  1960. Var RegexHelper::RegexReplaceFunction(ScriptContext* entryFunctionContext, RecyclableObject* thisObj, JavascriptString* input, JavascriptFunction* replacefn)
  1961. {
  1962. Var result = RegexHelper::RegexReplaceImpl(entryFunctionContext, thisObj, input, replacefn);
  1963. return RegexHelper::CheckCrossContextAndMarshalResult(result, entryFunctionContext);
  1964. }
  1965. Var RegexHelper::RegexSearch(ScriptContext* entryFunctionContext, JavascriptRegExp* regularExpression, JavascriptString* input)
  1966. {
  1967. Var result = RegexHelper::RegexSearchImpl(entryFunctionContext, regularExpression, input);
  1968. return RegexHelper::CheckCrossContextAndMarshalResult(result, entryFunctionContext);
  1969. }
  1970. Var RegexHelper::RegexSplitResultUsed(ScriptContext* scriptContext, JavascriptRegExp* regularExpression, JavascriptString* input, CharCount limit)
  1971. {
  1972. return RegexHelper::RegexSplit(scriptContext, regularExpression, input, limit, false);
  1973. }
  1974. Var RegexHelper::RegexSplitResultUsedAndMayBeTemp(void *const stackAllocationPointer, ScriptContext* scriptContext, JavascriptRegExp* regularExpression, JavascriptString* input, CharCount limit)
  1975. {
  1976. return RegexHelper::RegexSplit(scriptContext, regularExpression, input, limit, false, stackAllocationPointer);
  1977. }
  1978. Var RegexHelper::RegexSplitResultNotUsed(ScriptContext* scriptContext, JavascriptRegExp* regularExpression, JavascriptString* input, CharCount limit)
  1979. {
  1980. if (!PHASE_OFF1(Js::RegexResultNotUsedPhase))
  1981. {
  1982. return RegexHelper::RegexSplit(scriptContext, regularExpression, input, limit, true);
  1983. }
  1984. else
  1985. {
  1986. return RegexHelper::RegexSplit(scriptContext, regularExpression, input, limit, false);
  1987. }
  1988. }
  1989. Var RegexHelper::RegexSplit(ScriptContext* entryFunctionContext, RecyclableObject* thisObj, JavascriptString* input, CharCount limit, bool noResult, void *const stackAllocationPointer)
  1990. {
  1991. Var result = RegexHelper::RegexSplitImpl(entryFunctionContext, thisObj, input, limit, noResult, stackAllocationPointer);
  1992. return RegexHelper::CheckCrossContextAndMarshalResult(result, entryFunctionContext);
  1993. }
  1994. RecyclableObject* RegexHelper::ExecResultToRecyclableObject(Var result)
  1995. {
  1996. // "result" is the result of the "exec" call. "CallExec" makes sure that it is either
  1997. // an Object or Null. RegExp algorithms have special conditions for when the result is Null,
  1998. // so we can directly cast to RecyclableObject.
  1999. Assert(!JavascriptOperators::IsNull(result));
  2000. return RecyclableObject::FromVar(result);
  2001. }
  2002. JavascriptString* RegexHelper::GetMatchStrFromResult(RecyclableObject* result, ScriptContext* scriptContext)
  2003. {
  2004. return JavascriptConversion::ToString(
  2005. JavascriptOperators::GetItem(result, (uint32)0, scriptContext),
  2006. scriptContext);
  2007. }
  2008. void RegexHelper::AdvanceLastIndex(
  2009. RecyclableObject* instance,
  2010. JavascriptString* input,
  2011. JavascriptString* matchStr,
  2012. bool unicode,
  2013. ScriptContext* scriptContext)
  2014. {
  2015. if (matchStr->GetLength() == 0)
  2016. {
  2017. CharCount lastIndex = JavascriptRegExp::GetLastIndexProperty(instance, scriptContext);
  2018. lastIndex = AdvanceStringIndex(input, lastIndex, unicode);
  2019. JavascriptRegExp::SetLastIndexProperty(instance, lastIndex, scriptContext);
  2020. }
  2021. }
  2022. CharCount RegexHelper::AdvanceStringIndex(JavascriptString* string, CharCount index, bool isUnicode)
  2023. {
  2024. // TODO: Change the increment to 2 depending on the "unicode" flag and
  2025. // the code point at "index". The increment is currently constant at 1
  2026. // in order to be compatible with the rest of the RegExp code.
  2027. return JavascriptRegExp::AddIndex(index, 1);
  2028. }
  2029. }