1 | /* |
---|
2 | * Copyright (C) 2011 Apple Inc. All rights reserved. |
---|
3 | * |
---|
4 | * Redistribution and use in source and binary forms, with or without |
---|
5 | * modification, are permitted provided that the following conditions |
---|
6 | * are met: |
---|
7 | * 1. Redistributions of source code must retain the above copyright |
---|
8 | * notice, this list of conditions and the following disclaimer. |
---|
9 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
10 | * notice, this list of conditions and the following disclaimer in the |
---|
11 | * documentation and/or other materials provided with the distribution. |
---|
12 | * |
---|
13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' |
---|
14 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
---|
15 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
---|
16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS |
---|
17 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
---|
18 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
---|
19 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
---|
20 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
---|
21 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
---|
22 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
---|
23 | * THE POSSIBILITY OF SUCH DAMAGE. |
---|
24 | */ |
---|
25 | |
---|
26 | #pragma once |
---|
27 | |
---|
28 | #include "ParserModes.h" |
---|
29 | #include "ParserTokens.h" |
---|
30 | #include <wtf/Vector.h> |
---|
31 | #include <wtf/text/UniquedStringImpl.h> |
---|
32 | #include <wtf/text/WTFString.h> |
---|
33 | |
---|
34 | namespace JSC { |
---|
35 | |
---|
36 | struct SourceProviderCacheItemCreationParameters { |
---|
37 | unsigned lastTokenLine; |
---|
38 | unsigned lastTokenStartOffset; |
---|
39 | unsigned lastTokenEndOffset; |
---|
40 | unsigned lastTokenLineStartOffset; |
---|
41 | unsigned endFunctionOffset; |
---|
42 | unsigned parameterCount; |
---|
43 | bool needsFullActivation; |
---|
44 | bool usesEval; |
---|
45 | LexicalScopeFeatures lexicalScopeFeatures; |
---|
46 | bool needsSuperBinding; |
---|
47 | InnerArrowFunctionCodeFeatures innerArrowFunctionFeatures; |
---|
48 | Vector<UniquedStringImpl*, 8> usedVariables; |
---|
49 | bool isBodyArrowExpression { false }; |
---|
50 | JSTokenType tokenType { CLOSEBRACE }; |
---|
51 | ConstructorKind constructorKind; |
---|
52 | SuperBinding expectedSuperBinding; |
---|
53 | }; |
---|
54 | |
---|
55 | #if COMPILER(MSVC) |
---|
56 | #pragma warning(push) |
---|
57 | #pragma warning(disable: 4200) // Disable "zero-sized array in struct/union" warning |
---|
58 | #endif |
---|
59 | |
---|
60 | DECLARE_ALLOCATOR_WITH_HEAP_IDENTIFIER(SourceProviderCacheItem); |
---|
61 | class SourceProviderCacheItem { |
---|
62 | WTF_MAKE_STRUCT_FAST_ALLOCATED_WITH_HEAP_IDENTIFIER(SourceProviderCacheItem); |
---|
63 | public: |
---|
64 | static std::unique_ptr<SourceProviderCacheItem> create(const SourceProviderCacheItemCreationParameters&); |
---|
65 | ~SourceProviderCacheItem(); |
---|
66 | |
---|
67 | JSToken endFunctionToken() const |
---|
68 | { |
---|
69 | JSToken token; |
---|
70 | token.m_type = isBodyArrowExpression ? static_cast<JSTokenType>(tokenType) : CLOSEBRACE; |
---|
71 | token.m_data.offset = lastTokenStartOffset; |
---|
72 | token.m_location.startOffset = lastTokenStartOffset; |
---|
73 | token.m_location.endOffset = lastTokenEndOffset; |
---|
74 | token.m_location.line = lastTokenLine; |
---|
75 | token.m_location.lineStartOffset = lastTokenLineStartOffset; |
---|
76 | // token.m_location.sourceOffset is initialized once by the client. So, |
---|
77 | // we do not need to set it here. |
---|
78 | return token; |
---|
79 | } |
---|
80 | |
---|
81 | LexicalScopeFeatures lexicalScopeFeatures() const |
---|
82 | { |
---|
83 | LexicalScopeFeatures features = NoLexicalFeatures; |
---|
84 | if (strictMode) |
---|
85 | features |= StrictModeLexicalFeature; |
---|
86 | return features; |
---|
87 | } |
---|
88 | |
---|
89 | bool needsFullActivation : 1; |
---|
90 | unsigned endFunctionOffset : 31; |
---|
91 | bool usesEval : 1; |
---|
92 | unsigned lastTokenLine : 31; |
---|
93 | bool strictMode : 1; |
---|
94 | unsigned lastTokenStartOffset : 31; |
---|
95 | unsigned expectedSuperBinding : 1; // SuperBinding |
---|
96 | unsigned lastTokenEndOffset: 31; |
---|
97 | bool needsSuperBinding: 1; |
---|
98 | unsigned parameterCount : 31; |
---|
99 | unsigned lastTokenLineStartOffset : 31; |
---|
100 | bool isBodyArrowExpression : 1; |
---|
101 | unsigned usedVariablesCount; |
---|
102 | unsigned tokenType : 24; // JSTokenType |
---|
103 | unsigned innerArrowFunctionFeatures : 6; // InnerArrowFunctionCodeFeatures |
---|
104 | unsigned constructorKind : 2; // ConstructorKind |
---|
105 | |
---|
106 | PackedPtr<UniquedStringImpl>* usedVariables() const { return const_cast<PackedPtr<UniquedStringImpl>*>(m_variables); } |
---|
107 | |
---|
108 | private: |
---|
109 | SourceProviderCacheItem(const SourceProviderCacheItemCreationParameters&); |
---|
110 | |
---|
111 | PackedPtr<UniquedStringImpl> m_variables[0]; |
---|
112 | }; |
---|
113 | |
---|
114 | inline SourceProviderCacheItem::~SourceProviderCacheItem() |
---|
115 | { |
---|
116 | for (unsigned i = 0; i < usedVariablesCount; ++i) |
---|
117 | m_variables[i]->deref(); |
---|
118 | } |
---|
119 | |
---|
120 | inline std::unique_ptr<SourceProviderCacheItem> SourceProviderCacheItem::create(const SourceProviderCacheItemCreationParameters& parameters) |
---|
121 | { |
---|
122 | size_t variableCount = parameters.usedVariables.size(); |
---|
123 | size_t objectSize = sizeof(SourceProviderCacheItem) + sizeof(UniquedStringImpl*) * variableCount; |
---|
124 | void* slot = SourceProviderCacheItemMalloc::malloc(objectSize); |
---|
125 | return std::unique_ptr<SourceProviderCacheItem>(new (slot) SourceProviderCacheItem(parameters)); |
---|
126 | } |
---|
127 | |
---|
128 | inline SourceProviderCacheItem::SourceProviderCacheItem(const SourceProviderCacheItemCreationParameters& parameters) |
---|
129 | : needsFullActivation(parameters.needsFullActivation) |
---|
130 | , endFunctionOffset(parameters.endFunctionOffset) |
---|
131 | , usesEval(parameters.usesEval) |
---|
132 | , lastTokenLine(parameters.lastTokenLine) |
---|
133 | , strictMode(parameters.lexicalScopeFeatures & StrictModeLexicalFeature) |
---|
134 | , lastTokenStartOffset(parameters.lastTokenStartOffset) |
---|
135 | , expectedSuperBinding(static_cast<unsigned>(parameters.expectedSuperBinding)) |
---|
136 | , lastTokenEndOffset(parameters.lastTokenEndOffset) |
---|
137 | , needsSuperBinding(parameters.needsSuperBinding) |
---|
138 | , parameterCount(parameters.parameterCount) |
---|
139 | , lastTokenLineStartOffset(parameters.lastTokenLineStartOffset) |
---|
140 | , isBodyArrowExpression(parameters.isBodyArrowExpression) |
---|
141 | , usedVariablesCount(parameters.usedVariables.size()) |
---|
142 | , tokenType(static_cast<unsigned>(parameters.tokenType)) |
---|
143 | , innerArrowFunctionFeatures(static_cast<unsigned>(parameters.innerArrowFunctionFeatures)) |
---|
144 | , constructorKind(static_cast<unsigned>(parameters.constructorKind)) |
---|
145 | { |
---|
146 | ASSERT(tokenType == static_cast<unsigned>(parameters.tokenType)); |
---|
147 | ASSERT(innerArrowFunctionFeatures == static_cast<unsigned>(parameters.innerArrowFunctionFeatures)); |
---|
148 | ASSERT(constructorKind == static_cast<unsigned>(parameters.constructorKind)); |
---|
149 | ASSERT(expectedSuperBinding == static_cast<unsigned>(parameters.expectedSuperBinding)); |
---|
150 | for (unsigned i = 0; i < usedVariablesCount; ++i) { |
---|
151 | auto* pointer = parameters.usedVariables[i]; |
---|
152 | pointer->ref(); |
---|
153 | m_variables[i] = pointer; |
---|
154 | } |
---|
155 | } |
---|
156 | |
---|
157 | #if COMPILER(MSVC) |
---|
158 | #pragma warning(pop) |
---|
159 | #endif |
---|
160 | |
---|
161 | } // namespace JSC |
---|