2 * Copyright (C) 2012-2015 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include "RegExpMatchesArray.h"
29 #include "ButterflyInlines.h"
30 #include "JSCInlines.h"
34 static const PropertyOffset indexPropertyOffset
= 100;
35 static const PropertyOffset inputPropertyOffset
= 101;
37 static JSArray
* tryCreateUninitializedRegExpMatchesArray(VM
& vm
, Structure
* structure
, unsigned initialLength
)
39 unsigned vectorLength
= std::max(BASE_VECTOR_LEN
, initialLength
);
40 if (vectorLength
> MAX_STORAGE_VECTOR_LENGTH
)
44 if (!vm
.heap
.tryAllocateStorage(0, Butterfly::totalSize(0, structure
->outOfLineCapacity(), true, vectorLength
* sizeof(EncodedJSValue
)), &temp
))
46 Butterfly
* butterfly
= Butterfly::fromBase(temp
, 0, structure
->outOfLineCapacity());
47 butterfly
->setVectorLength(vectorLength
);
48 butterfly
->setPublicLength(initialLength
);
50 return JSArray::createWithButterfly(vm
, structure
, butterfly
);
53 JSArray
* createRegExpMatchesArray(ExecState
* exec
, JSString
* input
, RegExp
* regExp
, MatchResult result
)
57 JSArray
* array
= tryCreateUninitializedRegExpMatchesArray(vm
, exec
->lexicalGlobalObject()->regExpMatchesArrayStructure(), regExp
->numSubpatterns() + 1);
58 RELEASE_ASSERT(array
);
60 SamplingRegion
samplingRegion("Reifying substring properties");
62 array
->initializeIndex(vm
, 0, jsSubstring(exec
, input
, result
.start
, result
.end
- result
.start
), ArrayWithContiguous
);
64 if (unsigned numSubpatterns
= regExp
->numSubpatterns()) {
65 Vector
<int, 32> subpatternResults
;
66 int position
= regExp
->match(vm
, input
->value(exec
), result
.start
, subpatternResults
);
67 ASSERT_UNUSED(position
, position
>= 0 && static_cast<size_t>(position
) == result
.start
);
68 ASSERT(result
.start
== static_cast<size_t>(subpatternResults
[0]));
69 ASSERT(result
.end
== static_cast<size_t>(subpatternResults
[1]));
71 for (unsigned i
= 1; i
<= numSubpatterns
; ++i
) {
72 int start
= subpatternResults
[2 * i
];
74 array
->initializeIndex(vm
, i
, jsSubstring(exec
, input
, start
, subpatternResults
[2 * i
+ 1] - start
), ArrayWithContiguous
);
76 array
->initializeIndex(vm
, i
, jsUndefined(), ArrayWithContiguous
);
80 array
->putDirect(vm
, indexPropertyOffset
, jsNumber(result
.start
));
81 array
->putDirect(vm
, inputPropertyOffset
, input
);
86 Structure
* createRegExpMatchesArrayStructure(VM
& vm
, JSGlobalObject
& globalObject
)
88 Structure
* structure
= globalObject
.arrayStructureForIndexingTypeDuringAllocation(ArrayWithContiguous
);
89 PropertyOffset offset
;
90 structure
= structure
->addPropertyTransition(vm
, structure
, vm
.propertyNames
->index
, 0, offset
);
91 ASSERT(offset
== indexPropertyOffset
);
92 structure
= structure
->addPropertyTransition(vm
, structure
, vm
.propertyNames
->input
, 0, offset
);
93 ASSERT(offset
== inputPropertyOffset
);