]> git.saurik.com Git - apple/javascriptcore.git/blob - parser/SourceProviderCacheItem.h
JavaScriptCore-7600.1.4.16.1.tar.gz
[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/PassOwnPtr.h>
31 #include <wtf/Vector.h>
32 #include <wtf/text/WTFString.h>
33
34 namespace JSC {
35
36 struct SourceProviderCacheItemCreationParameters {
37 unsigned functionNameStart;
38 unsigned closeBraceLine;
39 unsigned closeBraceOffset;
40 unsigned closeBraceLineStartOffset;
41 bool needsFullActivation;
42 bool usesEval;
43 bool strictMode;
44 Vector<RefPtr<StringImpl>> usedVariables;
45 Vector<RefPtr<StringImpl>> writtenVariables;
46 };
47
48 #if COMPILER(MSVC)
49 #pragma warning(push)
50 #pragma warning(disable: 4200) // Disable "zero-sized array in struct/union" warning
51 #endif
52
53 class SourceProviderCacheItem {
54 WTF_MAKE_FAST_ALLOCATED;
55 public:
56 static std::unique_ptr<SourceProviderCacheItem> create(const SourceProviderCacheItemCreationParameters&);
57 ~SourceProviderCacheItem();
58
59 JSToken closeBraceToken() const
60 {
61 JSToken token;
62 token.m_type = CLOSEBRACE;
63 token.m_data.offset = closeBraceOffset;
64 token.m_location.startOffset = closeBraceOffset;
65 token.m_location.endOffset = closeBraceOffset + 1;
66 token.m_location.line = closeBraceLine;
67 token.m_location.lineStartOffset = closeBraceLineStartOffset;
68 // token.m_location.sourceOffset is initialized once by the client. So,
69 // we do not need to set it here.
70 return token;
71 }
72
73 unsigned functionNameStart : 31;
74 bool needsFullActivation : 1;
75
76 unsigned closeBraceLine : 31;
77 bool usesEval : 1;
78
79 unsigned closeBraceOffset : 31;
80 bool strictMode : 1;
81
82 unsigned closeBraceLineStartOffset;
83 unsigned usedVariablesCount;
84 unsigned writtenVariablesCount;
85
86 StringImpl** usedVariables() const { return const_cast<StringImpl**>(m_variables); }
87 StringImpl** writtenVariables() const { return const_cast<StringImpl**>(&m_variables[usedVariablesCount]); }
88
89 private:
90 SourceProviderCacheItem(const SourceProviderCacheItemCreationParameters&);
91
92 StringImpl* m_variables[0];
93 };
94
95 inline SourceProviderCacheItem::~SourceProviderCacheItem()
96 {
97 for (unsigned i = 0; i < usedVariablesCount + writtenVariablesCount; ++i)
98 m_variables[i]->deref();
99 }
100
101 inline std::unique_ptr<SourceProviderCacheItem> SourceProviderCacheItem::create(const SourceProviderCacheItemCreationParameters& parameters)
102 {
103 size_t variableCount = parameters.writtenVariables.size() + parameters.usedVariables.size();
104 size_t objectSize = sizeof(SourceProviderCacheItem) + sizeof(StringImpl*) * variableCount;
105 void* slot = fastMalloc(objectSize);
106 return std::unique_ptr<SourceProviderCacheItem>(new (slot) SourceProviderCacheItem(parameters));
107 }
108
109 inline SourceProviderCacheItem::SourceProviderCacheItem(const SourceProviderCacheItemCreationParameters& parameters)
110 : functionNameStart(parameters.functionNameStart)
111 , needsFullActivation(parameters.needsFullActivation)
112 , closeBraceLine(parameters.closeBraceLine)
113 , usesEval(parameters.usesEval)
114 , closeBraceOffset(parameters.closeBraceOffset)
115 , strictMode(parameters.strictMode)
116 , closeBraceLineStartOffset(parameters.closeBraceLineStartOffset)
117 , usedVariablesCount(parameters.usedVariables.size())
118 , writtenVariablesCount(parameters.writtenVariables.size())
119 {
120 unsigned j = 0;
121 for (unsigned i = 0; i < usedVariablesCount; ++i, ++j) {
122 m_variables[j] = parameters.usedVariables[i].get();
123 m_variables[j]->ref();
124 }
125 for (unsigned i = 0; i < writtenVariablesCount; ++i, ++j) {
126 m_variables[j] = parameters.writtenVariables[i].get();
127 m_variables[j]->ref();
128 }
129 }
130
131 #if COMPILER(MSVC)
132 #pragma warning(pop)
133 #endif
134
135 }
136
137 #endif // SourceProviderCacheItem_h