summaryrefslogtreecommitdiff
path: root/Source/JavaScriptCore/runtime/CodeCache.h
diff options
context:
space:
mode:
authorLorry Tar Creator <lorry-tar-importer@lorry>2017-06-27 06:07:23 +0000
committerLorry Tar Creator <lorry-tar-importer@lorry>2017-06-27 06:07:23 +0000
commit1bf1084f2b10c3b47fd1a588d85d21ed0eb41d0c (patch)
tree46dcd36c86e7fbc6e5df36deb463b33e9967a6f7 /Source/JavaScriptCore/runtime/CodeCache.h
parent32761a6cee1d0dee366b885b7b9c777e67885688 (diff)
downloadWebKitGtk-tarball-master.tar.gz
Diffstat (limited to 'Source/JavaScriptCore/runtime/CodeCache.h')
-rw-r--r--Source/JavaScriptCore/runtime/CodeCache.h195
1 files changed, 97 insertions, 98 deletions
diff --git a/Source/JavaScriptCore/runtime/CodeCache.h b/Source/JavaScriptCore/runtime/CodeCache.h
index f3ff7478d..b4ab8e56c 100644
--- a/Source/JavaScriptCore/runtime/CodeCache.h
+++ b/Source/JavaScriptCore/runtime/CodeCache.h
@@ -23,96 +23,42 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
-#ifndef CodeCache_h
-#define CodeCache_h
+#pragma once
-#include "CodeSpecializationKind.h"
+#include "BytecodeGenerator.h"
+#include "ExecutableInfo.h"
+#include "JSCInlines.h"
+#include "Parser.h"
#include "ParserModes.h"
-#include "SourceCode.h"
+#include "SourceCodeKey.h"
#include "Strong.h"
-#include "WeakRandom.h"
+#include "StrongInlines.h"
+#include "UnlinkedCodeBlock.h"
+#include "UnlinkedEvalCodeBlock.h"
+#include "UnlinkedModuleProgramCodeBlock.h"
+#include "UnlinkedProgramCodeBlock.h"
+#include "UnlinkedSourceCode.h"
#include <wtf/CurrentTime.h>
#include <wtf/Forward.h>
-#include <wtf/PassOwnPtr.h>
-#include <wtf/RandomNumber.h>
#include <wtf/text/WTFString.h>
namespace JSC {
class EvalExecutable;
-class FunctionBodyNode;
+class IndirectEvalExecutable;
class Identifier;
-class JSScope;
+class DirectEvalExecutable;
+class ModuleProgramExecutable;
+class ParserError;
class ProgramExecutable;
+class SourceCode;
class UnlinkedCodeBlock;
class UnlinkedEvalCodeBlock;
-class UnlinkedFunctionCodeBlock;
class UnlinkedFunctionExecutable;
+class UnlinkedModuleProgramCodeBlock;
class UnlinkedProgramCodeBlock;
class VM;
-struct ParserError;
-class SourceCode;
-class SourceProvider;
-
-class SourceCodeKey {
-public:
- enum CodeType { EvalType, ProgramType, FunctionType };
-
- SourceCodeKey()
- {
- }
-
- SourceCodeKey(const SourceCode& sourceCode, const String& name, CodeType codeType, JSParserStrictness jsParserStrictness)
- : m_sourceCode(sourceCode)
- , m_name(name)
- , m_flags((codeType << 1) | jsParserStrictness)
- , m_hash(string().impl()->hash())
- {
- }
-
- SourceCodeKey(WTF::HashTableDeletedValueType)
- : m_sourceCode(WTF::HashTableDeletedValue)
- {
- }
-
- bool isHashTableDeletedValue() const { return m_sourceCode.isHashTableDeletedValue(); }
-
- unsigned hash() const { return m_hash; }
-
- size_t length() const { return m_sourceCode.length(); }
-
- bool isNull() const { return m_sourceCode.isNull(); }
-
- // To save memory, we compute our string on demand. It's expected that source
- // providers cache their strings to make this efficient.
- String string() const { return m_sourceCode.toString(); }
-
- bool operator==(const SourceCodeKey& other) const
- {
- return m_hash == other.m_hash
- && length() == other.length()
- && m_flags == other.m_flags
- && m_name == other.m_name
- && string() == other.string();
- }
-
-private:
- SourceCode m_sourceCode;
- String m_name;
- unsigned m_flags;
- unsigned m_hash;
-};
-
-struct SourceCodeKeyHash {
- static unsigned hash(const SourceCodeKey& key) { return key.hash(); }
- static bool equal(const SourceCodeKey& a, const SourceCodeKey& b) { return a == b; }
- static const bool safeToCompareToEmptyOrDeleted = false;
-};
-
-struct SourceCodeKeyHashTraits : SimpleClassHashTraits<SourceCodeKey> {
- static const bool hasIsEmptyValueFunction = true;
- static bool isEmptyValue(const SourceCodeKey& sourceCodeKey) { return sourceCodeKey.isNull(); }
-};
+class VariableEnvironment;
struct SourceCodeValue {
SourceCodeValue()
@@ -131,7 +77,7 @@ struct SourceCodeValue {
class CodeCacheMap {
public:
- typedef HashMap<SourceCodeKey, SourceCodeValue, SourceCodeKeyHash, SourceCodeKeyHashTraits> MapType;
+ typedef HashMap<SourceCodeKey, SourceCodeValue, SourceCodeKey::Hash, SourceCodeKey::HashTraits> MapType;
typedef MapType::iterator iterator;
typedef MapType::AddResult AddResult;
@@ -145,18 +91,15 @@ public:
{
}
- AddResult add(const SourceCodeKey& key, const SourceCodeValue& value)
+ SourceCodeValue* findCacheAndUpdateAge(const SourceCodeKey& key)
{
prune();
- AddResult addResult = m_map.add(key, value);
- if (addResult.isNewEntry) {
- m_size += key.length();
- m_age += key.length();
- return addResult;
- }
+ iterator findResult = m_map.find(key);
+ if (findResult == m_map.end())
+ return nullptr;
- int64_t age = m_age - addResult.iterator->value.age;
+ int64_t age = m_age - findResult->value.age;
if (age > m_capacity) {
// A requested object is older than the cache's capacity. We can
// infer that requested objects are subject to high eviction probability,
@@ -171,7 +114,20 @@ public:
m_capacity = m_minCapacity;
}
- addResult.iterator->value.age = m_age;
+ findResult->value.age = m_age;
+ m_age += key.length();
+
+ return &findResult->value;
+ }
+
+ AddResult addCache(const SourceCodeKey& key, const SourceCodeValue& value)
+ {
+ prune();
+
+ AddResult addResult = m_map.add(key, value);
+ ASSERT(addResult.isNewEntry);
+
+ m_size += key.length();
m_age += key.length();
return addResult;
}
@@ -233,30 +189,73 @@ private:
int64_t m_age;
};
-// Caches top-level code such as <script>, eval(), new Function, and JSEvaluateScript().
+// Caches top-level code such as <script>, window.eval(), new Function, and JSEvaluateScript().
class CodeCache {
+ WTF_MAKE_FAST_ALLOCATED;
public:
- static PassOwnPtr<CodeCache> create() { return adoptPtr(new CodeCache); }
+ UnlinkedProgramCodeBlock* getUnlinkedProgramCodeBlock(VM&, ProgramExecutable*, const SourceCode&, JSParserStrictMode, DebuggerMode, ParserError&);
+ UnlinkedEvalCodeBlock* getUnlinkedEvalCodeBlock(VM&, IndirectEvalExecutable*, const SourceCode&, JSParserStrictMode, DebuggerMode, ParserError&, EvalContextType);
+ UnlinkedModuleProgramCodeBlock* getUnlinkedModuleProgramCodeBlock(VM&, ModuleProgramExecutable*, const SourceCode&, DebuggerMode, ParserError&);
+ UnlinkedFunctionExecutable* getUnlinkedGlobalFunctionExecutable(VM&, const Identifier&, const SourceCode&, DebuggerMode, ParserError&);
- UnlinkedProgramCodeBlock* getProgramCodeBlock(VM&, ProgramExecutable*, const SourceCode&, JSParserStrictness, DebuggerMode, ProfilerMode, ParserError&);
- UnlinkedEvalCodeBlock* getEvalCodeBlock(VM&, EvalExecutable*, const SourceCode&, JSParserStrictness, DebuggerMode, ProfilerMode, ParserError&);
- UnlinkedFunctionExecutable* getFunctionExecutableFromGlobalCode(VM&, const Identifier&, const SourceCode&, ParserError&);
- ~CodeCache();
-
- void clear()
- {
- m_sourceCode.clear();
- }
+ void clear() { m_sourceCode.clear(); }
private:
- CodeCache();
-
template <class UnlinkedCodeBlockType, class ExecutableType>
- UnlinkedCodeBlockType* getGlobalCodeBlock(VM&, ExecutableType*, const SourceCode&, JSParserStrictness, DebuggerMode, ProfilerMode, ParserError&);
+ UnlinkedCodeBlockType* getUnlinkedGlobalCodeBlock(VM&, ExecutableType*, const SourceCode&, JSParserStrictMode, JSParserScriptMode, DebuggerMode, ParserError&, EvalContextType);
CodeCacheMap m_sourceCode;
};
+template <typename T> struct CacheTypes { };
+
+template <> struct CacheTypes<UnlinkedProgramCodeBlock> {
+ typedef JSC::ProgramNode RootNode;
+ static const SourceCodeType codeType = SourceCodeType::ProgramType;
+ static const SourceParseMode parseMode = SourceParseMode::ProgramMode;
+};
+
+template <> struct CacheTypes<UnlinkedEvalCodeBlock> {
+ typedef JSC::EvalNode RootNode;
+ static const SourceCodeType codeType = SourceCodeType::EvalType;
+ static const SourceParseMode parseMode = SourceParseMode::ProgramMode;
+};
+
+template <> struct CacheTypes<UnlinkedModuleProgramCodeBlock> {
+ typedef JSC::ModuleProgramNode RootNode;
+ static const SourceCodeType codeType = SourceCodeType::ModuleType;
+ static const SourceParseMode parseMode = SourceParseMode::ModuleEvaluateMode;
+};
+
+template <class UnlinkedCodeBlockType, class ExecutableType>
+UnlinkedCodeBlockType* generateUnlinkedCodeBlock(VM& vm, ExecutableType* executable, const SourceCode& source, JSParserStrictMode strictMode, JSParserScriptMode scriptMode, DebuggerMode debuggerMode, ParserError& error, EvalContextType evalContextType, const VariableEnvironment* variablesUnderTDZ)
+{
+ typedef typename CacheTypes<UnlinkedCodeBlockType>::RootNode RootNode;
+ DerivedContextType derivedContextType = executable->derivedContextType();
+ std::unique_ptr<RootNode> rootNode = parse<RootNode>(
+ &vm, source, Identifier(), JSParserBuiltinMode::NotBuiltin, strictMode, scriptMode, CacheTypes<UnlinkedCodeBlockType>::parseMode, SuperBinding::NotNeeded, error, nullptr, ConstructorKind::None, derivedContextType, evalContextType);
+ if (!rootNode)
+ return nullptr;
+
+ unsigned lineCount = rootNode->lastLine() - rootNode->firstLine();
+ unsigned startColumn = rootNode->startColumn() + 1;
+ bool endColumnIsOnStartLine = !lineCount;
+ unsigned unlinkedEndColumn = rootNode->endColumn();
+ unsigned endColumn = unlinkedEndColumn + (endColumnIsOnStartLine ? startColumn : 1);
+ unsigned arrowContextFeature = executable->isArrowFunctionContext() ? ArrowFunctionContextFeature : 0;
+ executable->recordParse(rootNode->features() | arrowContextFeature, rootNode->hasCapturedVariables(), rootNode->lastLine(), endColumn);
+
+ UnlinkedCodeBlockType* unlinkedCodeBlock = UnlinkedCodeBlockType::create(&vm, executable->executableInfo(), debuggerMode);
+ unlinkedCodeBlock->recordParse(rootNode->features(), rootNode->hasCapturedVariables(), lineCount, unlinkedEndColumn);
+ unlinkedCodeBlock->setSourceURLDirective(source.provider()->sourceURL());
+ unlinkedCodeBlock->setSourceMappingURLDirective(source.provider()->sourceMappingURL());
+
+ error = BytecodeGenerator::generate(vm, rootNode.get(), unlinkedCodeBlock, debuggerMode, variablesUnderTDZ);
+
+ if (error.isValid())
+ return nullptr;
+
+ return unlinkedCodeBlock;
}
-#endif // CodeCache_h
+} // namespace JSC