2 * Copyright (C) 2012 Apple Inc. All Rights Reserved.
3 * Copyright (C) 2015 Yusuke Suzuki <utatane.tea@gmail.com>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
14 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include "ParserModes.h"
30 #include "UnlinkedSourceCode.h"
31 #include <wtf/HashTraits.h>
32 #include <wtf/Hasher.h>
36 enum class SourceCodeType { EvalType, ProgramType, FunctionType, ModuleType };
37 enum class TypeProfilerEnabled { No, Yes };
38 enum class ControlFlowProfilerEnabled { No, Yes };
40 class SourceCodeFlags {
42 SourceCodeFlags() = default;
45 SourceCodeType codeType, JSParserStrictMode strictMode, JSParserScriptMode scriptMode,
46 DerivedContextType derivedContextType, EvalContextType evalContextType, bool isArrowFunctionContext,
47 DebuggerMode debuggerMode, TypeProfilerEnabled typeProfilerEnabled, ControlFlowProfilerEnabled controlFlowProfilerEnabled)
49 (static_cast<unsigned>(debuggerMode) << 8) |
50 (static_cast<unsigned>(typeProfilerEnabled) << 7) |
51 (static_cast<unsigned>(controlFlowProfilerEnabled) << 6) |
52 (static_cast<unsigned>(scriptMode) << 5) |
53 (static_cast<unsigned>(isArrowFunctionContext) << 4) |
54 (static_cast<unsigned>(evalContextType) << 3) |
55 (static_cast<unsigned>(derivedContextType) << 2) |
56 (static_cast<unsigned>(codeType) << 1) |
57 (static_cast<unsigned>(strictMode))
62 inline bool operator==(const SourceCodeFlags& rhs) const
64 return m_flags == rhs.m_flags;
67 unsigned bits() { return m_flags; }
70 unsigned m_flags { 0 };
75 SourceCodeKey() = default;
78 const UnlinkedSourceCode& sourceCode, const String& name, SourceCodeType codeType, JSParserStrictMode strictMode,
79 JSParserScriptMode scriptMode, DerivedContextType derivedContextType, EvalContextType evalContextType, bool isArrowFunctionContext,
80 DebuggerMode debuggerMode, TypeProfilerEnabled typeProfilerEnabled, ControlFlowProfilerEnabled controlFlowProfilerEnabled, std::optional<int> functionConstructorParametersEndPosition)
81 : m_sourceCode(sourceCode)
83 , m_flags(codeType, strictMode, scriptMode, derivedContextType, evalContextType, isArrowFunctionContext, debuggerMode, typeProfilerEnabled, controlFlowProfilerEnabled)
84 , m_functionConstructorParametersEndPosition(functionConstructorParametersEndPosition.value_or(-1))
85 , m_hash(sourceCode.hash() ^ computeHash(m_flags.bits(), m_functionConstructorParametersEndPosition))
89 SourceCodeKey(WTF::HashTableDeletedValueType)
90 : m_sourceCode(WTF::HashTableDeletedValue)
94 bool isHashTableDeletedValue() const { return m_sourceCode.isHashTableDeletedValue(); }
96 unsigned hash() const { return m_hash; }
98 size_t length() const { return m_sourceCode.length(); }
100 bool isNull() const { return m_sourceCode.isNull(); }
102 // To save memory, we compute our string on demand. It's expected that source
103 // providers cache their strings to make this efficient.
104 StringView string() const { return m_sourceCode.view(); }
106 bool operator==(const SourceCodeKey& other) const
108 return m_hash == other.m_hash
109 && length() == other.length()
110 && m_flags == other.m_flags
111 && m_functionConstructorParametersEndPosition == other.m_functionConstructorParametersEndPosition
112 && m_name == other.m_name
113 && string() == other.string();
117 static unsigned hash(const SourceCodeKey& key) { return key.hash(); }
118 static bool equal(const SourceCodeKey& a, const SourceCodeKey& b) { return a == b; }
119 static const bool safeToCompareToEmptyOrDeleted = false;
122 struct HashTraits : SimpleClassHashTraits<SourceCodeKey> {
123 static const bool hasIsEmptyValueFunction = true;
124 static bool isEmptyValue(const SourceCodeKey& key) { return key.isNull(); }
128 UnlinkedSourceCode m_sourceCode;
130 SourceCodeFlags m_flags;
131 int m_functionConstructorParametersEndPosition { -1 };
132 unsigned m_hash { 0 };