]> git.saurik.com Git - apple/javascriptcore.git/blob - parser/SourceProviderCacheItem.h
c0c507f591a300f1943586066f4465322aa1c9e1
[apple/javascriptcore.git] / parser / SourceProviderCacheItem.h
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 #ifndef SourceProviderCacheItem_h
27 #define SourceProviderCacheItem_h
28
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 functionNameStart;
38 unsigned lastTockenLine;
39 unsigned lastTockenStartOffset;
40 unsigned lastTockenEndOffset;
41 unsigned lastTockenLineStartOffset;
42 unsigned endFunctionOffset;
43 bool needsFullActivation;
44 bool usesEval;
45 bool strictMode;
46 Vector<RefPtr<UniquedStringImpl>> usedVariables;
47 Vector<RefPtr<UniquedStringImpl>> writtenVariables;
48 #if ENABLE(ES6_ARROWFUNCTION_SYNTAX)
49 bool isBodyArrowExpression { false };
50 JSTokenType tokenType { CLOSEBRACE };
51 #endif
52 };
53
54 #if COMPILER(MSVC)
55 #pragma warning(push)
56 #pragma warning(disable: 4200) // Disable "zero-sized array in struct/union" warning
57 #endif
58
59 class SourceProviderCacheItem {
60 WTF_MAKE_FAST_ALLOCATED;
61 public:
62 static std::unique_ptr<SourceProviderCacheItem> create(const SourceProviderCacheItemCreationParameters&);
63 ~SourceProviderCacheItem();
64
65 JSToken endFunctionToken() const
66 {
67 JSToken token;
68 #if ENABLE(ES6_ARROWFUNCTION_SYNTAX)
69 token.m_type = isBodyArrowExpression ? tokenType : CLOSEBRACE;
70 #else
71 token.m_type = CLOSEBRACE;
72 #endif
73 token.m_data.offset = lastTockenStartOffset;
74 token.m_location.startOffset = lastTockenStartOffset;
75 token.m_location.endOffset = lastTockenEndOffset;
76 token.m_location.line = lastTockenLine;
77 token.m_location.lineStartOffset = lastTockenLineStartOffset;
78 // token.m_location.sourceOffset is initialized once by the client. So,
79 // we do not need to set it here.
80 return token;
81 }
82
83 unsigned functionNameStart : 31;
84 bool needsFullActivation : 1;
85
86 unsigned endFunctionOffset : 31;
87 unsigned lastTockenLine : 31;
88 unsigned lastTockenStartOffset : 31;
89 unsigned lastTockenEndOffset: 31;
90
91 bool usesEval : 1;
92
93 bool strictMode : 1;
94
95 unsigned lastTockenLineStartOffset;
96 unsigned usedVariablesCount;
97 unsigned writtenVariablesCount;
98
99 UniquedStringImpl** usedVariables() const { return const_cast<UniquedStringImpl**>(m_variables); }
100 UniquedStringImpl** writtenVariables() const { return const_cast<UniquedStringImpl**>(&m_variables[usedVariablesCount]); }
101 #if ENABLE(ES6_ARROWFUNCTION_SYNTAX)
102 bool isBodyArrowExpression;
103 JSTokenType tokenType;
104 #endif
105
106 private:
107 SourceProviderCacheItem(const SourceProviderCacheItemCreationParameters&);
108
109 UniquedStringImpl* m_variables[0];
110 };
111
112 inline SourceProviderCacheItem::~SourceProviderCacheItem()
113 {
114 for (unsigned i = 0; i < usedVariablesCount + writtenVariablesCount; ++i)
115 m_variables[i]->deref();
116 }
117
118 inline std::unique_ptr<SourceProviderCacheItem> SourceProviderCacheItem::create(const SourceProviderCacheItemCreationParameters& parameters)
119 {
120 size_t variableCount = parameters.writtenVariables.size() + parameters.usedVariables.size();
121 size_t objectSize = sizeof(SourceProviderCacheItem) + sizeof(UniquedStringImpl*) * variableCount;
122 void* slot = fastMalloc(objectSize);
123 return std::unique_ptr<SourceProviderCacheItem>(new (slot) SourceProviderCacheItem(parameters));
124 }
125
126 inline SourceProviderCacheItem::SourceProviderCacheItem(const SourceProviderCacheItemCreationParameters& parameters)
127 : functionNameStart(parameters.functionNameStart)
128 , needsFullActivation(parameters.needsFullActivation)
129 , endFunctionOffset(parameters.endFunctionOffset)
130 , lastTockenLine(parameters.lastTockenLine)
131 , lastTockenStartOffset(parameters.lastTockenStartOffset)
132 , lastTockenEndOffset(parameters.lastTockenEndOffset)
133 , usesEval(parameters.usesEval)
134 , strictMode(parameters.strictMode)
135 , lastTockenLineStartOffset(parameters.lastTockenLineStartOffset)
136 , usedVariablesCount(parameters.usedVariables.size())
137 , writtenVariablesCount(parameters.writtenVariables.size())
138 #if ENABLE(ES6_ARROWFUNCTION_SYNTAX)
139 , isBodyArrowExpression(parameters.isBodyArrowExpression)
140 , tokenType(parameters.tokenType)
141 #endif
142 {
143 unsigned j = 0;
144 for (unsigned i = 0; i < usedVariablesCount; ++i, ++j) {
145 m_variables[j] = parameters.usedVariables[i].get();
146 m_variables[j]->ref();
147 }
148 for (unsigned i = 0; i < writtenVariablesCount; ++i, ++j) {
149 m_variables[j] = parameters.writtenVariables[i].get();
150 m_variables[j]->ref();
151 }
152 }
153
154 #if COMPILER(MSVC)
155 #pragma warning(pop)
156 #endif
157
158 }
159
160 #endif // SourceProviderCacheItem_h