]> git.saurik.com Git - apple/javascriptcore.git/blob - runtime/RegExpMatchesArray.cpp
440cc95120efa14648f5cb3bbad9ddbf545e2851
[apple/javascriptcore.git] / runtime / RegExpMatchesArray.cpp
1 /*
2 * Copyright (C) 2012-2015 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
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.
12 *
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.
24 */
25
26 #include "config.h"
27 #include "RegExpMatchesArray.h"
28
29 #include "ButterflyInlines.h"
30 #include "JSCInlines.h"
31
32 namespace JSC {
33
34 static const PropertyOffset indexPropertyOffset = 100;
35 static const PropertyOffset inputPropertyOffset = 101;
36
37 static JSArray* tryCreateUninitializedRegExpMatchesArray(VM& vm, Structure* structure, unsigned initialLength)
38 {
39 unsigned vectorLength = std::max(BASE_VECTOR_LEN, initialLength);
40 if (vectorLength > MAX_STORAGE_VECTOR_LENGTH)
41 return 0;
42
43 void* temp;
44 if (!vm.heap.tryAllocateStorage(0, Butterfly::totalSize(0, structure->outOfLineCapacity(), true, vectorLength * sizeof(EncodedJSValue)), &temp))
45 return 0;
46 Butterfly* butterfly = Butterfly::fromBase(temp, 0, structure->outOfLineCapacity());
47 butterfly->setVectorLength(vectorLength);
48 butterfly->setPublicLength(initialLength);
49
50 return JSArray::createWithButterfly(vm, structure, butterfly);
51 }
52
53 JSArray* createRegExpMatchesArray(ExecState* exec, JSString* input, RegExp* regExp, MatchResult result)
54 {
55 ASSERT(result);
56 VM& vm = exec->vm();
57 JSArray* array = tryCreateUninitializedRegExpMatchesArray(vm, exec->lexicalGlobalObject()->regExpMatchesArrayStructure(), regExp->numSubpatterns() + 1);
58 RELEASE_ASSERT(array);
59
60 SamplingRegion samplingRegion("Reifying substring properties");
61
62 array->initializeIndex(vm, 0, jsSubstring(exec, input, result.start, result.end - result.start), ArrayWithContiguous);
63
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]));
70
71 for (unsigned i = 1; i <= numSubpatterns; ++i) {
72 int start = subpatternResults[2 * i];
73 if (start >= 0)
74 array->initializeIndex(vm, i, jsSubstring(exec, input, start, subpatternResults[2 * i + 1] - start), ArrayWithContiguous);
75 else
76 array->initializeIndex(vm, i, jsUndefined(), ArrayWithContiguous);
77 }
78 }
79
80 array->putDirect(vm, indexPropertyOffset, jsNumber(result.start));
81 array->putDirect(vm, inputPropertyOffset, input);
82
83 return array;
84 }
85
86 Structure* createRegExpMatchesArrayStructure(VM& vm, JSGlobalObject& globalObject)
87 {
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);
94 return structure;
95 }
96
97 } // namespace JSC