2828
2929namespace JSC {
3030
31+ JSArray* tryCreateUninitializedRegExpMatchesArrayHavingABadTime (ObjectInitializationScope& scope, GCDeferralContext* deferralContext, Structure* structure, unsigned initialLength, std::span<const JSValue> outOfLineProperties)
32+ {
33+ VM & vm = scope.vm ();
34+
35+ Butterfly* butterfly = JSArray::tryCreateUninitializedRestrictedButterfly (vm, deferralContext, structure, initialLength);
36+ if (!butterfly) [[unlikely]]
37+ return nullptr ;
38+
39+ initializeRegExpMatchesArrayOutOfLineProperties (butterfly, structure, outOfLineProperties);
40+
41+ JSArray* result = JSArray::createWithButterfly (vm, deferralContext, structure, butterfly);
42+
43+ scope.notifyAllocated (result);
44+ return result;
45+ }
46+
3147JSArray* createEmptyRegExpMatchesArray (JSGlobalObject* globalObject, JSString* input, RegExp* regExp)
3248{
3349 VM & vm = globalObject->vm ();
3450 JSArray* array;
3551
3652 // FIXME: This should handle array allocation errors gracefully.
3753 // https://bugs.webkit.org/show_bug.cgi?id=155144
38-
54+
3955 GCDeferralContext deferralContext (vm);
4056 ObjectInitializationScope scope (vm);
4157
58+ const std::array<JSValue, 4 > outOfLineProperties { jsNumber (-1 ), input, jsUndefined (), jsUndefined () };
59+ std::span<const JSValue> properties = std::span { outOfLineProperties }.first (regExp->hasIndices () ? 4 : 3 );
60+
4261 if (globalObject->isHavingABadTime ()) [[unlikely]] {
43- array = JSArray::tryCreateUninitializedRestricted (scope, &deferralContext,
44- regExp->hasIndices () ? globalObject->regExpMatchesArrayWithIndicesStructure () : globalObject->regExpMatchesArrayStructure (), regExp->numSubpatterns () + 1 );
62+ array = tryCreateUninitializedRegExpMatchesArrayHavingABadTime (scope, &deferralContext,
63+ regExp->hasIndices () ? globalObject->regExpMatchesArrayWithIndicesStructure () : globalObject->regExpMatchesArrayStructure (), regExp->numSubpatterns () + 1 , properties );
4564 // FIXME: we should probably throw an out of memory error here, but
4665 // when making this change we should check that all clients of this
4766 // function will correctly handle an exception being thrown from here.
@@ -56,22 +75,17 @@ JSArray* createEmptyRegExpMatchesArray(JSGlobalObject* globalObject, JSString* i
5675 }
5776 } else {
5877 array = tryCreateUninitializedRegExpMatchesArray (scope, &deferralContext,
59- regExp->hasIndices () ? globalObject->regExpMatchesArrayWithIndicesStructure () : globalObject->regExpMatchesArrayStructure (), regExp->numSubpatterns () + 1 );
78+ regExp->hasIndices () ? globalObject->regExpMatchesArrayWithIndicesStructure () : globalObject->regExpMatchesArrayStructure (), regExp->numSubpatterns () + 1 , properties );
6079 RELEASE_ASSERT (array);
61-
80+
6281 array->initializeIndexWithoutBarrier (scope, 0 , jsEmptyString (vm), ArrayWithContiguous);
63-
82+
6483 if (unsigned numSubpatterns = regExp->numSubpatterns ()) {
6584 for (unsigned i = 1 ; i <= numSubpatterns; ++i)
6685 array->initializeIndexWithoutBarrier (scope, i, jsUndefined (), ArrayWithContiguous);
6786 }
6887 }
6988
70- array->putDirectWithoutBarrier (RegExpMatchesArrayIndexPropertyOffset, jsNumber (-1 ));
71- array->putDirectWithoutBarrier (RegExpMatchesArrayInputPropertyOffset, input);
72- array->putDirectWithoutBarrier (RegExpMatchesArrayGroupsPropertyOffset, jsUndefined ());
73- if (regExp->hasIndices ())
74- array->putDirectWithoutBarrier (RegExpMatchesArrayIndicesPropertyOffset, jsUndefined ());
7589 return array;
7690}
7791
@@ -95,28 +109,25 @@ JSArray* createRegExpMatchesArrayWithGroupsOrIndices(VM& vm, JSGlobalObject* glo
95109 }
96110 Structure* matchStructure = createIndices ? globalObject->regExpMatchesArrayWithIndicesStructure () : globalObject->regExpMatchesArrayStructure ();
97111
98- auto setProperties = [&] () {
99- array->putDirectOffset (vm, RegExpMatchesArrayIndexPropertyOffset, jsNumber (result.start ));
100- array->putDirectOffset (vm, RegExpMatchesArrayInputPropertyOffset, input);
101- array->putDirectOffset (vm, RegExpMatchesArrayGroupsPropertyOffset, hasNamedCaptures ? groups : jsUndefined ());
102-
103- ASSERT (!array->butterfly ()->preCapacity (matchStructure));
104- auto capacity = matchStructure->outOfLineCapacity ();
105- auto size = matchStructure->outOfLineSize ();
106- gcSafeZeroMemory (static_cast <JSValue*>(array->butterfly ()->base (0 , capacity)), (capacity - size) * sizeof (JSValue));
112+ auto createArrays = [&](ObjectInitializationScope& matchesArrayScope, ObjectInitializationScope& indicesArrayScope, GCDeferralContext& deferralContext, bool isHavingABadTime) {
113+ auto tryCreate = [&](ObjectInitializationScope& scope, Structure* structure, std::span<const JSValue> outOfLineProperties) {
114+ if (isHavingABadTime) [[unlikely]]
115+ return tryCreateUninitializedRegExpMatchesArrayHavingABadTime (scope, &deferralContext, structure, numSubpatterns + 1 , outOfLineProperties);
116+ return tryCreateUninitializedRegExpMatchesArray (scope, &deferralContext, structure, numSubpatterns + 1 , outOfLineProperties);
117+ };
107118
119+ // FIXME: we should probably throw an out of memory error here, but first every client of
120+ // this function has to be checked to handle an exception thrown from here.
121+ // https://bugs.webkit.org/show_bug.cgi?id=169786
108122 if (createIndices) {
109- array->putDirectOffset (vm, RegExpMatchesArrayIndicesPropertyOffset, indicesArray);
110-
111- Structure* indicesStructure = globalObject->regExpMatchesIndicesArrayStructure ();
112-
113- indicesArray->putDirectOffset (vm, RegExpMatchesIndicesGroupsPropertyOffset, indicesGroups ? indicesGroups : jsUndefined ());
114-
115- ASSERT (!indicesArray->butterfly ()->preCapacity (indicesStructure));
116- auto indicesCapacity = indicesStructure->outOfLineCapacity ();
117- auto indicesSize = indicesStructure->outOfLineSize ();
118- gcSafeZeroMemory (static_cast <JSValue*>(indicesArray->butterfly ()->base (0 , indicesCapacity)), (indicesCapacity - indicesSize) * sizeof (JSValue));
123+ const std::array<JSValue, 1 > indicesProperties { indicesGroups ? indicesGroups : jsUndefined () };
124+ indicesArray = tryCreate (indicesArrayScope, globalObject->regExpMatchesIndicesArrayStructure (), indicesProperties);
125+ RELEASE_ASSERT (indicesArray);
119126 }
127+
128+ const std::array<JSValue, 4 > properties { jsNumber (result.start ), input, hasNamedCaptures ? groups : jsUndefined (), indicesArray };
129+ array = tryCreate (matchesArrayScope, matchStructure, std::span { properties }.first (createIndices ? 4 : 3 ));
130+ RELEASE_ASSERT (array);
120131 };
121132
122133 auto createIndexArray = [&] (GCDeferralContext& deferralContext, int start, int end) {
@@ -133,18 +144,7 @@ JSArray* createRegExpMatchesArrayWithGroupsOrIndices(VM& vm, JSGlobalObject* glo
133144 GCDeferralContext deferralContext (vm);
134145 ObjectInitializationScope matchesArrayScope (vm);
135146 ObjectInitializationScope indicesArrayScope (vm);
136- array = JSArray::tryCreateUninitializedRestricted (matchesArrayScope, &deferralContext, matchStructure, numSubpatterns + 1 );
137-
138- if (createIndices)
139- indicesArray = JSArray::tryCreateUninitializedRestricted (indicesArrayScope, &deferralContext, globalObject->regExpMatchesIndicesArrayStructure (), numSubpatterns + 1 );
140-
141- // FIXME: we should probably throw an out of memory error here, but
142- // when making this change we should check that all clients of this
143- // function will correctly handle an exception being thrown from here.
144- // https://bugs.webkit.org/show_bug.cgi?id=169786
145- RELEASE_ASSERT (array);
146-
147- setProperties ();
147+ createArrays (matchesArrayScope, indicesArrayScope, deferralContext, true );
148148
149149 array->initializeIndexWithoutBarrier (matchesArrayScope, 0 , jsSubstringOfResolved (vm, &deferralContext, input, result.start , result.end - result.start ));
150150
@@ -174,18 +174,7 @@ JSArray* createRegExpMatchesArrayWithGroupsOrIndices(VM& vm, JSGlobalObject* glo
174174 GCDeferralContext deferralContext (vm);
175175 ObjectInitializationScope matchesArrayScope (vm);
176176 ObjectInitializationScope indicesArrayScope (vm);
177- array = tryCreateUninitializedRegExpMatchesArray (matchesArrayScope, &deferralContext, matchStructure, numSubpatterns + 1 );
178-
179- if (createIndices)
180- indicesArray = tryCreateUninitializedRegExpMatchesArray (indicesArrayScope, &deferralContext, globalObject->regExpMatchesIndicesArrayStructure (), numSubpatterns + 1 );
181-
182- // FIXME: we should probably throw an out of memory error here, but
183- // when making this change we should check that all clients of this
184- // function will correctly handle an exception being thrown from here.
185- // https://bugs.webkit.org/show_bug.cgi?id=169786
186- RELEASE_ASSERT (array);
187-
188- setProperties ();
177+ createArrays (matchesArrayScope, indicesArrayScope, deferralContext, false );
189178
190179 array->initializeIndexWithoutBarrier (matchesArrayScope, 0 , jsSubstringOfResolved (vm, &deferralContext, input, result.start , result.end - result.start ), ArrayWithContiguous);
191180
@@ -262,19 +251,10 @@ JSArray* createRegExpMatchesArrayForPlainRegExpHavingABadTime(VM& vm, JSGlobalOb
262251
263252 // FIXME: This should handle array allocation errors gracefully.
264253 // https://bugs.webkit.org/show_bug.cgi?id=155144
265- JSArray* array = JSArray::tryCreateUninitializedRestricted (matchesArrayScope, &deferralContext, matchStructure, numSubpatterns + 1 );
254+ const std::array<JSValue, 3 > outOfLineProperties { jsNumber (result.start ), input, jsUndefined () };
255+ JSArray* array = tryCreateUninitializedRegExpMatchesArrayHavingABadTime (matchesArrayScope, &deferralContext, matchStructure, numSubpatterns + 1 , outOfLineProperties);
266256 RELEASE_ASSERT (array);
267257
268- array->putDirectOffset (vm, RegExpMatchesArrayIndexPropertyOffset, jsNumber (result.start ));
269- array->putDirectOffset (vm, RegExpMatchesArrayInputPropertyOffset, input);
270- array->putDirectOffset (vm, RegExpMatchesArrayGroupsPropertyOffset, jsUndefined ());
271-
272- ASSERT (!array->butterfly ()->preCapacity (matchStructure));
273- auto capacity = matchStructure->outOfLineCapacity ();
274- auto size = matchStructure->outOfLineSize ();
275- if (capacity > size) [[unlikely]]
276- gcSafeZeroMemory (static_cast <JSValue*>(array->butterfly ()->base (0 , capacity)), (capacity - size) * sizeof (JSValue));
277-
278258 array->initializeIndexWithoutBarrier (matchesArrayScope, 0 , jsSubstringOfResolved (vm, &deferralContext, input, result.start , result.end - result.start ));
279259 for (unsigned i = 1 ; i <= numSubpatterns; ++i) {
280260 int start = subpatternResults[2 * i];
0 commit comments