#include "config.h"
#include "RegExpCache.h"
#include "JSCInlines.h"
#include "RegExpObject.h"
#include "StrongInlines.h"
namespace JSC {
RegExp* RegExpCache::lookupOrCreate(const String& patternString, RegExpFlags flags)
{
RegExpKey key(flags, patternString);
if (RegExp* regExp = m_weakCache.get(key))
return regExp;
RegExp* regExp = RegExp::createWithoutCaching(*m_vm, patternString, flags);
#if ENABLE(REGEXP_TRACING)
m_vm->addRegExpToTrace(regExp);
#endif
weakAdd(m_weakCache, key, Weak<RegExp>(regExp, this));
return regExp;
}
RegExpCache::RegExpCache(VM* vm)
: m_nextEntryInStrongCache(0)
, m_vm(vm)
{
}
void RegExpCache::finalize(Handle<Unknown> handle, void*)
{
RegExp* regExp = static_cast<RegExp*>(handle.get().asCell());
weakRemove(m_weakCache, regExp->key(), regExp);
}
void RegExpCache::addToStrongCache(RegExp* regExp)
{
String pattern = regExp->pattern();
if (pattern.length() > maxStrongCacheablePatternLength)
return;
m_strongCache[m_nextEntryInStrongCache].set(*m_vm, regExp);
m_nextEntryInStrongCache++;
if (m_nextEntryInStrongCache == maxStrongCacheableEntries)
m_nextEntryInStrongCache = 0;
}
void RegExpCache::deleteAllCode()
{
for (int i = 0; i < maxStrongCacheableEntries; i++)
m_strongCache[i].clear();
m_nextEntryInStrongCache = 0;
RegExpCacheMap::iterator end = m_weakCache.end();
for (RegExpCacheMap::iterator it = m_weakCache.begin(); it != end; ++it) {
RegExp* regExp = it->value.get();
if (!regExp) continue;
regExp->deleteCode();
}
}
}