2 * Copyright (C) 2011 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
26 #ifndef SourceProviderCacheItem_h
27 #define SourceProviderCacheItem_h
29 #include "ParserTokens.h"
30 #include <wtf/Vector.h>
31 #include <wtf/text/UniquedStringImpl.h>
32 #include <wtf/text/WTFString.h>
36 struct SourceProviderCacheItemCreationParameters {
37 unsigned functionNameStart;
38 unsigned closeBraceLine;
39 unsigned closeBraceOffset;
40 unsigned closeBraceLineStartOffset;
41 bool needsFullActivation;
44 Vector<RefPtr<UniquedStringImpl>> usedVariables;
45 Vector<RefPtr<UniquedStringImpl>> writtenVariables;
50 #pragma warning(disable: 4200) // Disable "zero-sized array in struct/union" warning
53 class SourceProviderCacheItem {
54 WTF_MAKE_FAST_ALLOCATED;
56 static std::unique_ptr<SourceProviderCacheItem> create(const SourceProviderCacheItemCreationParameters&);
57 ~SourceProviderCacheItem();
59 JSToken closeBraceToken() const
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.
73 unsigned functionNameStart : 31;
74 bool needsFullActivation : 1;
76 unsigned closeBraceLine : 31;
79 unsigned closeBraceOffset : 31;
82 unsigned closeBraceLineStartOffset;
83 unsigned usedVariablesCount;
84 unsigned writtenVariablesCount;
86 UniquedStringImpl** usedVariables() const { return const_cast<UniquedStringImpl**>(m_variables); }
87 UniquedStringImpl** writtenVariables() const { return const_cast<UniquedStringImpl**>(&m_variables[usedVariablesCount]); }
90 SourceProviderCacheItem(const SourceProviderCacheItemCreationParameters&);
92 UniquedStringImpl* m_variables[0];
95 inline SourceProviderCacheItem::~SourceProviderCacheItem()
97 for (unsigned i = 0; i < usedVariablesCount + writtenVariablesCount; ++i)
98 m_variables[i]->deref();
101 inline std::unique_ptr<SourceProviderCacheItem> SourceProviderCacheItem::create(const SourceProviderCacheItemCreationParameters& parameters)
103 size_t variableCount = parameters.writtenVariables.size() + parameters.usedVariables.size();
104 size_t objectSize = sizeof(SourceProviderCacheItem) + sizeof(UniquedStringImpl*) * variableCount;
105 void* slot = fastMalloc(objectSize);
106 return std::unique_ptr<SourceProviderCacheItem>(new (slot) SourceProviderCacheItem(parameters));
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())
121 for (unsigned i = 0; i < usedVariablesCount; ++i, ++j) {
122 m_variables[j] = parameters.usedVariables[i].get();
123 m_variables[j]->ref();
125 for (unsigned i = 0; i < writtenVariablesCount; ++i, ++j) {
126 m_variables[j] = parameters.writtenVariables[i].get();
127 m_variables[j]->ref();
137 #endif // SourceProviderCacheItem_h