| 1 | /* |
| 2 | * Copyright (C) 2012, 2016 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 "RegExpCachedResult.h" |
| 28 | |
| 29 | #include "JSCInlines.h" |
| 30 | #include "RegExpCache.h" |
| 31 | #include "RegExpMatchesArray.h" |
| 32 | |
| 33 | namespace JSC { |
| 34 | |
| 35 | void RegExpCachedResult::visitAggregate(SlotVisitor& visitor) |
| 36 | { |
| 37 | visitor.append(m_lastInput); |
| 38 | visitor.append(m_lastRegExp); |
| 39 | if (m_reified) { |
| 40 | visitor.append(m_reifiedInput); |
| 41 | visitor.append(m_reifiedResult); |
| 42 | visitor.append(m_reifiedLeftContext); |
| 43 | visitor.append(m_reifiedRightContext); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | JSArray* RegExpCachedResult::lastResult(ExecState* exec, JSObject* owner) |
| 48 | { |
| 49 | VM& vm = exec->vm(); |
| 50 | auto scope = DECLARE_THROW_SCOPE(vm); |
| 51 | |
| 52 | if (!m_reified) { |
| 53 | m_reifiedInput.set(vm, owner, m_lastInput.get()); |
| 54 | if (!m_lastRegExp) |
| 55 | m_lastRegExp.set(vm, owner, vm.regExpCache()->ensureEmptyRegExp(vm)); |
| 56 | |
| 57 | JSArray* result = nullptr; |
| 58 | if (m_result) |
| 59 | result = createRegExpMatchesArray(exec, exec->lexicalGlobalObject(), m_lastInput.get(), m_lastRegExp.get(), m_result.start); |
| 60 | else |
| 61 | result = createEmptyRegExpMatchesArray(exec->lexicalGlobalObject(), m_lastInput.get(), m_lastRegExp.get()); |
| 62 | RETURN_IF_EXCEPTION(scope, nullptr); |
| 63 | |
| 64 | m_reifiedResult.setWithoutWriteBarrier(result); |
| 65 | m_reifiedLeftContext.clear(); |
| 66 | m_reifiedRightContext.clear(); |
| 67 | m_reified = true; |
| 68 | vm.heap.writeBarrier(owner); |
| 69 | } |
| 70 | return m_reifiedResult.get(); |
| 71 | } |
| 72 | |
| 73 | JSString* RegExpCachedResult::leftContext(ExecState* exec, JSObject* owner) |
| 74 | { |
| 75 | // Make sure we're reified. |
| 76 | VM& vm = exec->vm(); |
| 77 | auto scope = DECLARE_THROW_SCOPE(vm); |
| 78 | |
| 79 | lastResult(exec, owner); |
| 80 | RETURN_IF_EXCEPTION(scope, nullptr); |
| 81 | |
| 82 | if (!m_reifiedLeftContext) { |
| 83 | JSString* leftContext = jsSubstring(exec, m_reifiedInput.get(), 0, m_result.start); |
| 84 | RETURN_IF_EXCEPTION(scope, nullptr); |
| 85 | m_reifiedLeftContext.set(vm, owner, leftContext); |
| 86 | } |
| 87 | return m_reifiedLeftContext.get(); |
| 88 | } |
| 89 | |
| 90 | JSString* RegExpCachedResult::rightContext(ExecState* exec, JSObject* owner) |
| 91 | { |
| 92 | // Make sure we're reified. |
| 93 | VM& vm = exec->vm(); |
| 94 | auto scope = DECLARE_THROW_SCOPE(vm); |
| 95 | |
| 96 | lastResult(exec, owner); |
| 97 | RETURN_IF_EXCEPTION(scope, nullptr); |
| 98 | |
| 99 | if (!m_reifiedRightContext) { |
| 100 | unsigned length = m_reifiedInput->length(); |
| 101 | JSString* rightContext = jsSubstring(exec, m_reifiedInput.get(), m_result.end, length - m_result.end); |
| 102 | RETURN_IF_EXCEPTION(scope, nullptr); |
| 103 | m_reifiedRightContext.set(vm, owner, rightContext); |
| 104 | } |
| 105 | return m_reifiedRightContext.get(); |
| 106 | } |
| 107 | |
| 108 | void RegExpCachedResult::setInput(ExecState* exec, JSObject* owner, JSString* input) |
| 109 | { |
| 110 | // Make sure we're reified, otherwise m_reifiedInput will be ignored. |
| 111 | VM& vm = exec->vm(); |
| 112 | auto scope = DECLARE_THROW_SCOPE(vm); |
| 113 | |
| 114 | lastResult(exec, owner); |
| 115 | RETURN_IF_EXCEPTION(scope, void()); |
| 116 | leftContext(exec, owner); |
| 117 | RETURN_IF_EXCEPTION(scope, void()); |
| 118 | rightContext(exec, owner); |
| 119 | RETURN_IF_EXCEPTION(scope, void()); |
| 120 | ASSERT(m_reified); |
| 121 | m_reifiedInput.set(vm, owner, input); |
| 122 | } |
| 123 | |
| 124 | } // namespace JSC |
| 125 | |