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
34namespace JSC {
35
36struct 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 bool strictMode;
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
60class SourceProviderCacheItem {
61 WTF_MAKE_FAST_ALLOCATED;
62public:
63 static std::unique_ptr<SourceProviderCacheItem> create(const SourceProviderCacheItemCreationParameters&);
64 ~SourceProviderCacheItem();
65
66 JSToken endFunctionToken() const
67 {
68 JSToken token;
69 token.m_type = isBodyArrowExpression ? static_cast<JSTokenType>(tokenType) : CLOSEBRACE;
70 token.m_data.offset = lastTokenStartOffset;
71 token.m_location.startOffset = lastTokenStartOffset;
72 token.m_location.endOffset = lastTokenEndOffset;
73 token.m_location.line = lastTokenLine;
74 token.m_location.lineStartOffset = lastTokenLineStartOffset;
75 // token.m_location.sourceOffset is initialized once by the client. So,
76 // we do not need to set it here.
77 return token;
78 }
79
80 bool needsFullActivation : 1;
81 unsigned endFunctionOffset : 31;
82 bool usesEval : 1;
83 unsigned lastTokenLine : 31;
84 bool strictMode : 1;
85 unsigned lastTokenStartOffset : 31;
86 unsigned expectedSuperBinding : 1; // SuperBinding
87 unsigned lastTokenEndOffset: 31;
88 bool needsSuperBinding: 1;
89 unsigned parameterCount : 31;
90 unsigned lastTokenLineStartOffset : 31;
91 bool isBodyArrowExpression : 1;
92 unsigned usedVariablesCount;
93 unsigned tokenType : 24; // JSTokenType
94 unsigned innerArrowFunctionFeatures : 6; // InnerArrowFunctionCodeFeatures
95 unsigned constructorKind : 2; // ConstructorKind
96
97 UniquedStringImpl** usedVariables() const { return const_cast<UniquedStringImpl**>(m_variables); }
98
99private:
100 SourceProviderCacheItem(const SourceProviderCacheItemCreationParameters&);
101
102 UniquedStringImpl* m_variables[0];
103};
104
105inline SourceProviderCacheItem::~SourceProviderCacheItem()
106{
107 for (unsigned i = 0; i < usedVariablesCount; ++i)
108 m_variables[i]->deref();
109}
110
111inline std::unique_ptr<SourceProviderCacheItem> SourceProviderCacheItem::create(const SourceProviderCacheItemCreationParameters& parameters)
112{
113 size_t variableCount = parameters.usedVariables.size();
114 size_t objectSize = sizeof(SourceProviderCacheItem) + sizeof(UniquedStringImpl*) * variableCount;
115 void* slot = fastMalloc(objectSize);
116 return std::unique_ptr<SourceProviderCacheItem>(new (slot) SourceProviderCacheItem(parameters));
117}
118
119inline SourceProviderCacheItem::SourceProviderCacheItem(const SourceProviderCacheItemCreationParameters& parameters)
120 : needsFullActivation(parameters.needsFullActivation)
121 , endFunctionOffset(parameters.endFunctionOffset)
122 , usesEval(parameters.usesEval)
123 , lastTokenLine(parameters.lastTokenLine)
124 , strictMode(parameters.strictMode)
125 , lastTokenStartOffset(parameters.lastTokenStartOffset)
126 , expectedSuperBinding(static_cast<unsigned>(parameters.expectedSuperBinding))
127 , lastTokenEndOffset(parameters.lastTokenEndOffset)
128 , needsSuperBinding(parameters.needsSuperBinding)
129 , parameterCount(parameters.parameterCount)
130 , lastTokenLineStartOffset(parameters.lastTokenLineStartOffset)
131 , isBodyArrowExpression(parameters.isBodyArrowExpression)
132 , usedVariablesCount(parameters.usedVariables.size())
133 , tokenType(static_cast<unsigned>(parameters.tokenType))
134 , innerArrowFunctionFeatures(static_cast<unsigned>(parameters.innerArrowFunctionFeatures))
135 , constructorKind(static_cast<unsigned>(parameters.constructorKind))
136{
137 ASSERT(tokenType == static_cast<unsigned>(parameters.tokenType));
138 ASSERT(innerArrowFunctionFeatures == static_cast<unsigned>(parameters.innerArrowFunctionFeatures));
139 ASSERT(constructorKind == static_cast<unsigned>(parameters.constructorKind));
140 ASSERT(expectedSuperBinding == static_cast<unsigned>(parameters.expectedSuperBinding));
141 for (unsigned i = 0; i < usedVariablesCount; ++i) {
142 m_variables[i] = parameters.usedVariables[i];
143 m_variables[i]->ref();
144 }
145}
146
147#if COMPILER(MSVC)
148#pragma warning(pop)
149#endif
150
151} // namespace JSC
152