1/*
2 * Copyright (C) 2012 Apple Inc. All Rights Reserved.
3 * Copyright (C) 2015 Yusuke Suzuki <utatane.tea@gmail.com>
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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 *
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.
25 */
26
27#pragma once
28
29#include "ParserModes.h"
30#include "UnlinkedSourceCode.h"
31#include <wtf/HashTraits.h>
32
33namespace JSC {
34
35enum class SourceCodeType { EvalType, ProgramType, FunctionType, ModuleType };
36enum class TypeProfilerEnabled { No, Yes };
37enum class ControlFlowProfilerEnabled { No, Yes };
38
39class SourceCodeFlags {
40 friend class CachedSourceCodeKey;
41
42public:
43 SourceCodeFlags() = default;
44
45 SourceCodeFlags(
46 SourceCodeType codeType, JSParserStrictMode strictMode, JSParserScriptMode scriptMode,
47 DerivedContextType derivedContextType, EvalContextType evalContextType, bool isArrowFunctionContext,
48 DebuggerMode debuggerMode, TypeProfilerEnabled typeProfilerEnabled, ControlFlowProfilerEnabled controlFlowProfilerEnabled)
49 : m_flags(
50 (static_cast<unsigned>(debuggerMode) << 8) |
51 (static_cast<unsigned>(typeProfilerEnabled) << 7) |
52 (static_cast<unsigned>(controlFlowProfilerEnabled) << 6) |
53 (static_cast<unsigned>(scriptMode) << 5) |
54 (static_cast<unsigned>(isArrowFunctionContext) << 4) |
55 (static_cast<unsigned>(evalContextType) << 3) |
56 (static_cast<unsigned>(derivedContextType) << 2) |
57 (static_cast<unsigned>(codeType) << 1) |
58 (static_cast<unsigned>(strictMode))
59 )
60 {
61 }
62
63 inline bool operator==(const SourceCodeFlags& rhs) const
64 {
65 return m_flags == rhs.m_flags;
66 }
67
68 unsigned bits() { return m_flags; }
69
70private:
71 unsigned m_flags { 0 };
72};
73
74class SourceCodeKey {
75 friend class CachedSourceCodeKey;
76
77public:
78 SourceCodeKey()
79 {
80 }
81
82 SourceCodeKey(
83 const UnlinkedSourceCode& sourceCode, const String& name, SourceCodeType codeType, JSParserStrictMode strictMode,
84 JSParserScriptMode scriptMode, DerivedContextType derivedContextType, EvalContextType evalContextType, bool isArrowFunctionContext,
85 DebuggerMode debuggerMode, TypeProfilerEnabled typeProfilerEnabled, ControlFlowProfilerEnabled controlFlowProfilerEnabled, Optional<int> functionConstructorParametersEndPosition)
86 : m_sourceCode(sourceCode)
87 , m_name(name)
88 , m_flags(codeType, strictMode, scriptMode, derivedContextType, evalContextType, isArrowFunctionContext, debuggerMode, typeProfilerEnabled, controlFlowProfilerEnabled)
89 , m_functionConstructorParametersEndPosition(functionConstructorParametersEndPosition.valueOr(-1))
90 , m_hash(sourceCode.hash() ^ m_flags.bits())
91 {
92 }
93
94 SourceCodeKey(WTF::HashTableDeletedValueType)
95 : m_sourceCode(WTF::HashTableDeletedValue)
96 {
97 }
98
99 bool isHashTableDeletedValue() const { return m_sourceCode.isHashTableDeletedValue(); }
100
101 unsigned hash() const { return m_hash; }
102
103 const UnlinkedSourceCode& source() const { return m_sourceCode; }
104
105 size_t length() const { return m_sourceCode.length(); }
106
107 bool isNull() const { return m_sourceCode.isNull(); }
108
109 // To save memory, we compute our string on demand. It's expected that source
110 // providers cache their strings to make this efficient.
111 StringView string() const { return m_sourceCode.view(); }
112
113 StringView host() const { return m_sourceCode.provider().url().host(); }
114
115 bool operator==(const SourceCodeKey& other) const
116 {
117 return m_hash == other.m_hash
118 && length() == other.length()
119 && m_flags == other.m_flags
120 && m_functionConstructorParametersEndPosition == other.m_functionConstructorParametersEndPosition
121 && m_name == other.m_name
122 && host() == other.host()
123 && string() == other.string();
124 }
125
126 bool operator!=(const SourceCodeKey& other) const
127 {
128 return !(*this == other);
129 }
130
131 struct Hash {
132 static unsigned hash(const SourceCodeKey& key) { return key.hash(); }
133 static bool equal(const SourceCodeKey& a, const SourceCodeKey& b) { return a == b; }
134 static const bool safeToCompareToEmptyOrDeleted = false;
135 };
136
137 struct HashTraits : SimpleClassHashTraits<SourceCodeKey> {
138 static const bool hasIsEmptyValueFunction = true;
139 static bool isEmptyValue(const SourceCodeKey& key) { return key.isNull(); }
140 };
141
142private:
143 UnlinkedSourceCode m_sourceCode;
144 String m_name;
145 SourceCodeFlags m_flags;
146 int m_functionConstructorParametersEndPosition;
147 unsigned m_hash;
148};
149
150} // namespace JSC
151