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 };
36
37class SourceCodeFlags {
38 friend class CachedSourceCodeKey;
39
40public:
41 SourceCodeFlags() = default;
42
43 SourceCodeFlags(
44 SourceCodeType codeType, JSParserStrictMode strictMode, JSParserScriptMode scriptMode,
45 DerivedContextType derivedContextType, EvalContextType evalContextType, bool isArrowFunctionContext,
46 OptionSet<CodeGenerationMode> codeGenerationMode)
47 : m_flags(
48 (static_cast<unsigned>(codeGenerationMode.toRaw()) << 6) |
49 (static_cast<unsigned>(scriptMode) << 5) |
50 (static_cast<unsigned>(isArrowFunctionContext) << 4) |
51 (static_cast<unsigned>(evalContextType) << 3) |
52 (static_cast<unsigned>(derivedContextType) << 2) |
53 (static_cast<unsigned>(codeType) << 1) |
54 (static_cast<unsigned>(strictMode))
55 )
56 {
57 }
58
59 inline bool operator==(const SourceCodeFlags& rhs) const
60 {
61 return m_flags == rhs.m_flags;
62 }
63
64 unsigned bits() { return m_flags; }
65
66private:
67 unsigned m_flags { 0 };
68};
69
70class SourceCodeKey {
71 friend class CachedSourceCodeKey;
72
73public:
74 SourceCodeKey()
75 {
76 }
77
78 SourceCodeKey(
79 const UnlinkedSourceCode& sourceCode, const String& name, SourceCodeType codeType, JSParserStrictMode strictMode,
80 JSParserScriptMode scriptMode, DerivedContextType derivedContextType, EvalContextType evalContextType, bool isArrowFunctionContext,
81 OptionSet<CodeGenerationMode> codeGenerationMode, Optional<int> functionConstructorParametersEndPosition)
82 : m_sourceCode(sourceCode)
83 , m_name(name)
84 , m_flags(codeType, strictMode, scriptMode, derivedContextType, evalContextType, isArrowFunctionContext, codeGenerationMode)
85 , m_functionConstructorParametersEndPosition(functionConstructorParametersEndPosition.valueOr(-1))
86 , m_hash(sourceCode.hash() ^ m_flags.bits())
87 {
88 }
89
90 SourceCodeKey(WTF::HashTableDeletedValueType)
91 : m_sourceCode(WTF::HashTableDeletedValue)
92 {
93 }
94
95 bool isHashTableDeletedValue() const { return m_sourceCode.isHashTableDeletedValue(); }
96
97 unsigned hash() const { return m_hash; }
98
99 const UnlinkedSourceCode& source() const { return m_sourceCode; }
100
101 size_t length() const { return m_sourceCode.length(); }
102
103 bool isNull() const { return m_sourceCode.isNull(); }
104
105 // To save memory, we compute our string on demand. It's expected that source
106 // providers cache their strings to make this efficient.
107 StringView string() const { return m_sourceCode.view(); }
108
109 StringView host() const { return m_sourceCode.provider().url().host(); }
110
111 bool operator==(const SourceCodeKey& other) const
112 {
113 return m_hash == other.m_hash
114 && length() == other.length()
115 && m_flags == other.m_flags
116 && m_functionConstructorParametersEndPosition == other.m_functionConstructorParametersEndPosition
117 && m_name == other.m_name
118 && host() == other.host()
119 && string() == other.string();
120 }
121
122 bool operator!=(const SourceCodeKey& other) const
123 {
124 return !(*this == other);
125 }
126
127 struct Hash {
128 static unsigned hash(const SourceCodeKey& key) { return key.hash(); }
129 static bool equal(const SourceCodeKey& a, const SourceCodeKey& b) { return a == b; }
130 static const bool safeToCompareToEmptyOrDeleted = false;
131 };
132
133 struct HashTraits : SimpleClassHashTraits<SourceCodeKey> {
134 static const bool hasIsEmptyValueFunction = true;
135 static bool isEmptyValue(const SourceCodeKey& key) { return key.isNull(); }
136 };
137
138private:
139 UnlinkedSourceCode m_sourceCode;
140 String m_name;
141 SourceCodeFlags m_flags;
142 int m_functionConstructorParametersEndPosition;
143 unsigned m_hash;
144};
145
146} // namespace JSC
147