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 lastTockenLine
;
39 unsigned lastTockenStartOffset
;
40 unsigned lastTockenEndOffset
;
41 unsigned lastTockenLineStartOffset
;
42 unsigned endFunctionOffset
;
43 bool needsFullActivation
;
46 Vector
<RefPtr
<UniquedStringImpl
>> usedVariables
;
47 Vector
<RefPtr
<UniquedStringImpl
>> writtenVariables
;
48 #if ENABLE(ES6_ARROWFUNCTION_SYNTAX)
49 bool isBodyArrowExpression
{ false };
50 JSTokenType tokenType
{ CLOSEBRACE
};
56 #pragma warning(disable: 4200) // Disable "zero-sized array in struct/union" warning
59 class SourceProviderCacheItem
{
60 WTF_MAKE_FAST_ALLOCATED
;
62 static std::unique_ptr
<SourceProviderCacheItem
> create(const SourceProviderCacheItemCreationParameters
&);
63 ~SourceProviderCacheItem();
65 JSToken
endFunctionToken() const
68 #if ENABLE(ES6_ARROWFUNCTION_SYNTAX)
69 token
.m_type
= isBodyArrowExpression
? tokenType
: CLOSEBRACE
;
71 token
.m_type
= CLOSEBRACE
;
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.
83 unsigned functionNameStart
: 31;
84 bool needsFullActivation
: 1;
86 unsigned endFunctionOffset
: 31;
87 unsigned lastTockenLine
: 31;
88 unsigned lastTockenStartOffset
: 31;
89 unsigned lastTockenEndOffset
: 31;
95 unsigned lastTockenLineStartOffset
;
96 unsigned usedVariablesCount
;
97 unsigned writtenVariablesCount
;
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
;
107 SourceProviderCacheItem(const SourceProviderCacheItemCreationParameters
&);
109 UniquedStringImpl
* m_variables
[0];
112 inline SourceProviderCacheItem::~SourceProviderCacheItem()
114 for (unsigned i
= 0; i
< usedVariablesCount
+ writtenVariablesCount
; ++i
)
115 m_variables
[i
]->deref();
118 inline std::unique_ptr
<SourceProviderCacheItem
> SourceProviderCacheItem::create(const SourceProviderCacheItemCreationParameters
& parameters
)
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
));
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
)
144 for (unsigned i
= 0; i
< usedVariablesCount
; ++i
, ++j
) {
145 m_variables
[j
] = parameters
.usedVariables
[i
].get();
146 m_variables
[j
]->ref();
148 for (unsigned i
= 0; i
< writtenVariablesCount
; ++i
, ++j
) {
149 m_variables
[j
] = parameters
.writtenVariables
[i
].get();
150 m_variables
[j
]->ref();
160 #endif // SourceProviderCacheItem_h