cwzwarich@webkit.org | 95686fd | 2008-11-11 20:06:11 +0000 | [diff] [blame] | 1 | /* |
oliver@apple.com | c8f59e1 | 2009-01-14 22:14:55 +0000 | [diff] [blame] | 2 | * Copyright (C) 2008, 2009 Apple Inc. All rights reserved. |
cwzwarich@webkit.org | 95686fd | 2008-11-11 20:06:11 +0000 | [diff] [blame] | 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 | * |
| 8 | * 1. Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in the |
| 12 | * documentation and/or other materials provided with the distribution. |
| 13 | * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of |
| 14 | * its contributors may be used to endorse or promote products derived |
| 15 | * from this software without specific prior written permission. |
| 16 | * |
| 17 | * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY |
| 18 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 19 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 20 | * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY |
| 21 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 22 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 23 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 24 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | */ |
| 28 | |
| 29 | #ifndef EvalCodeCache_h |
| 30 | #define EvalCodeCache_h |
| 31 | |
barraclough@apple.com | 83651156 | 2009-08-15 01:14:00 +0000 | [diff] [blame] | 32 | #include "Executable.h" |
cwzwarich@webkit.org | 95686fd | 2008-11-11 20:06:11 +0000 | [diff] [blame] | 33 | #include "JSGlobalObject.h" |
| 34 | #include "Nodes.h" |
| 35 | #include "Parser.h" |
| 36 | #include "SourceCode.h" |
| 37 | #include "UString.h" |
| 38 | #include <wtf/HashMap.h> |
| 39 | #include <wtf/RefPtr.h> |
barraclough@apple.com | 185ce91 | 2010-04-21 19:13:50 +0000 | [diff] [blame] | 40 | #include <wtf/text/StringHash.h> |
cwzwarich@webkit.org | 95686fd | 2008-11-11 20:06:11 +0000 | [diff] [blame] | 41 | |
| 42 | namespace JSC { |
| 43 | |
| 44 | class EvalCodeCache { |
| 45 | public: |
barraclough@apple.com | 12710b6 | 2009-08-21 21:54:20 +0000 | [diff] [blame] | 46 | PassRefPtr<EvalExecutable> get(ExecState* exec, const UString& evalSource, ScopeChainNode* scopeChain, JSValue& exceptionValue) |
cwzwarich@webkit.org | 95686fd | 2008-11-11 20:06:11 +0000 | [diff] [blame] | 47 | { |
barraclough@apple.com | 12710b6 | 2009-08-21 21:54:20 +0000 | [diff] [blame] | 48 | RefPtr<EvalExecutable> evalExecutable; |
cwzwarich@webkit.org | 95686fd | 2008-11-11 20:06:11 +0000 | [diff] [blame] | 49 | |
barraclough@apple.com | c2527d6 | 2010-08-11 19:52:41 +0000 | [diff] [blame] | 50 | if (evalSource.length() < maxCacheableSourceLength && (*scopeChain->begin())->isVariableObject()) |
barraclough@apple.com | ee2085b | 2010-08-11 00:16:38 +0000 | [diff] [blame] | 51 | evalExecutable = m_cacheMap.get(evalSource.impl()); |
cwzwarich@webkit.org | 95686fd | 2008-11-11 20:06:11 +0000 | [diff] [blame] | 52 | |
barraclough@apple.com | 83651156 | 2009-08-15 01:14:00 +0000 | [diff] [blame] | 53 | if (!evalExecutable) { |
oliver@apple.com | 338f90a | 2009-09-23 00:40:58 +0000 | [diff] [blame] | 54 | evalExecutable = EvalExecutable::create(exec, makeSource(evalSource)); |
barraclough@apple.com | 1050f79 | 2009-08-25 02:53:51 +0000 | [diff] [blame] | 55 | exceptionValue = evalExecutable->compile(exec, scopeChain); |
barraclough@apple.com | 83651156 | 2009-08-15 01:14:00 +0000 | [diff] [blame] | 56 | if (exceptionValue) |
cwzwarich@webkit.org | 95686fd | 2008-11-11 20:06:11 +0000 | [diff] [blame] | 57 | return 0; |
barraclough@apple.com | 83651156 | 2009-08-15 01:14:00 +0000 | [diff] [blame] | 58 | |
barraclough@apple.com | c2527d6 | 2010-08-11 19:52:41 +0000 | [diff] [blame] | 59 | if (evalSource.length() < maxCacheableSourceLength && (*scopeChain->begin())->isVariableObject() && m_cacheMap.size() < maxCacheEntries) |
barraclough@apple.com | ee2085b | 2010-08-11 00:16:38 +0000 | [diff] [blame] | 60 | m_cacheMap.set(evalSource.impl(), evalExecutable); |
cwzwarich@webkit.org | 95686fd | 2008-11-11 20:06:11 +0000 | [diff] [blame] | 61 | } |
| 62 | |
barraclough@apple.com | 83651156 | 2009-08-15 01:14:00 +0000 | [diff] [blame] | 63 | return evalExecutable.release(); |
cwzwarich@webkit.org | 95686fd | 2008-11-11 20:06:11 +0000 | [diff] [blame] | 64 | } |
| 65 | |
weinig@apple.com | 67940d5 | 2008-12-09 00:14:58 +0000 | [diff] [blame] | 66 | bool isEmpty() const { return m_cacheMap.isEmpty(); } |
| 67 | |
cwzwarich@webkit.org | 95686fd | 2008-11-11 20:06:11 +0000 | [diff] [blame] | 68 | private: |
barraclough@apple.com | 2049d9a | 2010-02-15 21:03:45 +0000 | [diff] [blame] | 69 | static const unsigned maxCacheableSourceLength = 256; |
cwzwarich@webkit.org | 95686fd | 2008-11-11 20:06:11 +0000 | [diff] [blame] | 70 | static const int maxCacheEntries = 64; |
| 71 | |
barraclough@apple.com | ee2085b | 2010-08-11 00:16:38 +0000 | [diff] [blame] | 72 | typedef HashMap<RefPtr<StringImpl>, RefPtr<EvalExecutable> > EvalCacheMap; |
oliver@apple.com | c8f59e1 | 2009-01-14 22:14:55 +0000 | [diff] [blame] | 73 | EvalCacheMap m_cacheMap; |
cwzwarich@webkit.org | 95686fd | 2008-11-11 20:06:11 +0000 | [diff] [blame] | 74 | }; |
| 75 | |
| 76 | } // namespace JSC |
| 77 | |
| 78 | #endif // EvalCodeCache_h |