blob: d0644d2df17195ad7e24b5cb5c3725ac821660f4 [file] [log] [blame]
antti@apple.combe333a72011-01-25 18:44:11 +00001/*
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
ryanhaddad@apple.com22104f52016-09-28 17:08:17 +000026#pragma once
msaboff@apple.com2e5003e2011-10-31 22:13:01 +000027
annulen@yandex.ru6712c2d2017-06-25 17:40:30 +000028#include "ParserModes.h"
msaboff@apple.com2e5003e2011-10-31 22:13:01 +000029#include "ParserTokens.h"
antti@apple.combe333a72011-01-25 18:44:11 +000030#include <wtf/Vector.h>
utatane.tea@gmail.com8268d392015-05-23 18:41:53 +000031#include <wtf/text/UniquedStringImpl.h>
antti@apple.combe333a72011-01-25 18:44:11 +000032#include <wtf/text/WTFString.h>
33
34namespace JSC {
35
akling@apple.come93cba92013-01-28 06:34:03 +000036struct SourceProviderCacheItemCreationParameters {
mark.lam@apple.comfa35e782013-11-19 21:55:16 +000037 unsigned functionNameStart;
sbarati@apple.come1671a12016-04-25 19:08:53 +000038 unsigned lastTokenLine;
39 unsigned lastTokenStartOffset;
40 unsigned lastTokenEndOffset;
41 unsigned lastTokenLineStartOffset;
commit-queue@webkit.org2a979b32015-06-26 06:49:20 +000042 unsigned endFunctionOffset;
saambarati1@gmail.comc497d152015-07-17 18:48:30 +000043 unsigned parameterCount;
commit-queue@webkit.orga1e1d572016-09-22 19:11:23 +000044 unsigned functionLength;
akling@apple.come93cba92013-01-28 06:34:03 +000045 bool needsFullActivation;
46 bool usesEval;
47 bool strictMode;
gskachkov@gmail.com82b84582017-01-20 11:43:24 +000048 bool needsSuperBinding;
gskachkov@gmail.coma198dd12016-02-28 19:14:26 +000049 InnerArrowFunctionCodeFeatures innerArrowFunctionFeatures;
sbarati@apple.comd60fffd2016-04-21 23:30:36 +000050 Vector<UniquedStringImpl*, 8> usedVariables;
commit-queue@webkit.org2a979b32015-06-26 06:49:20 +000051 bool isBodyArrowExpression { false };
52 JSTokenType tokenType { CLOSEBRACE };
sbarati@apple.come1671a12016-04-25 19:08:53 +000053 ConstructorKind constructorKind;
54 SuperBinding expectedSuperBinding;
akling@apple.come93cba92013-01-28 06:34:03 +000055};
56
57#if COMPILER(MSVC)
58#pragma warning(push)
59#pragma warning(disable: 4200) // Disable "zero-sized array in struct/union" warning
60#endif
61
antti@apple.combe333a72011-01-25 18:44:11 +000062class SourceProviderCacheItem {
zoltan@webkit.orgc6395da2012-09-04 19:35:54 +000063 WTF_MAKE_FAST_ALLOCATED;
antti@apple.combe333a72011-01-25 18:44:11 +000064public:
andersca@apple.com2a6c4892013-09-26 20:44:02 +000065 static std::unique_ptr<SourceProviderCacheItem> create(const SourceProviderCacheItemCreationParameters&);
akling@apple.come93cba92013-01-28 06:34:03 +000066 ~SourceProviderCacheItem();
67
commit-queue@webkit.org2a979b32015-06-26 06:49:20 +000068 JSToken endFunctionToken() const
antti@apple.combe333a72011-01-25 18:44:11 +000069 {
70 JSToken token;
commit-queue@webkit.org2a979b32015-06-26 06:49:20 +000071 token.m_type = isBodyArrowExpression ? tokenType : CLOSEBRACE;
sbarati@apple.come1671a12016-04-25 19:08:53 +000072 token.m_data.offset = lastTokenStartOffset;
73 token.m_location.startOffset = lastTokenStartOffset;
74 token.m_location.endOffset = lastTokenEndOffset;
75 token.m_location.line = lastTokenLine;
76 token.m_location.lineStartOffset = lastTokenLineStartOffset;
mark.lam@apple.com5b45f902013-07-09 16:15:12 +000077 // token.m_location.sourceOffset is initialized once by the client. So,
78 // we do not need to set it here.
antti@apple.combe333a72011-01-25 18:44:11 +000079 return token;
80 }
oliver@apple.comd6b8be42012-11-28 21:11:53 +000081
mark.lam@apple.comfa35e782013-11-19 21:55:16 +000082 unsigned functionNameStart : 31;
oliver@apple.comd6b8be42012-11-28 21:11:53 +000083 bool needsFullActivation : 1;
commit-queue@webkit.org2a979b32015-06-26 06:49:20 +000084 unsigned endFunctionOffset : 31;
oliver@apple.comd6b8be42012-11-28 21:11:53 +000085 bool usesEval : 1;
sbarati@apple.come1671a12016-04-25 19:08:53 +000086 unsigned lastTokenLine : 31;
oliver@apple.comd6b8be42012-11-28 21:11:53 +000087 bool strictMode : 1;
sbarati@apple.come1671a12016-04-25 19:08:53 +000088 unsigned lastTokenStartOffset : 31;
89 unsigned lastTokenEndOffset: 31;
90 unsigned constructorKind : 2; // ConstructorKind
91 unsigned parameterCount : 31;
92 unsigned expectedSuperBinding : 1; // SuperBinding
gskachkov@gmail.com82b84582017-01-20 11:43:24 +000093 bool needsSuperBinding: 1;
commit-queue@webkit.orga1e1d572016-09-22 19:11:23 +000094 unsigned functionLength;
sbarati@apple.come1671a12016-04-25 19:08:53 +000095 unsigned lastTokenLineStartOffset;
akling@apple.come93cba92013-01-28 06:34:03 +000096 unsigned usedVariablesCount;
sbarati@apple.come1671a12016-04-25 19:08:53 +000097 InnerArrowFunctionCodeFeatures innerArrowFunctionFeatures;
commit-queue@webkit.org2a979b32015-06-26 06:49:20 +000098 bool isBodyArrowExpression;
99 JSTokenType tokenType;
akling@apple.come93cba92013-01-28 06:34:03 +0000100
sbarati@apple.come1671a12016-04-25 19:08:53 +0000101 UniquedStringImpl** usedVariables() const { return const_cast<UniquedStringImpl**>(m_variables); }
102
akling@apple.come93cba92013-01-28 06:34:03 +0000103private:
104 SourceProviderCacheItem(const SourceProviderCacheItemCreationParameters&);
105
utatane.tea@gmail.com8268d392015-05-23 18:41:53 +0000106 UniquedStringImpl* m_variables[0];
antti@apple.combe333a72011-01-25 18:44:11 +0000107};
108
akling@apple.come93cba92013-01-28 06:34:03 +0000109inline SourceProviderCacheItem::~SourceProviderCacheItem()
110{
sbarati@apple.comd60fffd2016-04-21 23:30:36 +0000111 for (unsigned i = 0; i < usedVariablesCount; ++i)
akling@apple.come93cba92013-01-28 06:34:03 +0000112 m_variables[i]->deref();
113}
114
andersca@apple.com2a6c4892013-09-26 20:44:02 +0000115inline std::unique_ptr<SourceProviderCacheItem> SourceProviderCacheItem::create(const SourceProviderCacheItemCreationParameters& parameters)
akling@apple.come93cba92013-01-28 06:34:03 +0000116{
sbarati@apple.comd60fffd2016-04-21 23:30:36 +0000117 size_t variableCount = parameters.usedVariables.size();
utatane.tea@gmail.com8268d392015-05-23 18:41:53 +0000118 size_t objectSize = sizeof(SourceProviderCacheItem) + sizeof(UniquedStringImpl*) * variableCount;
akling@apple.come93cba92013-01-28 06:34:03 +0000119 void* slot = fastMalloc(objectSize);
andersca@apple.com2a6c4892013-09-26 20:44:02 +0000120 return std::unique_ptr<SourceProviderCacheItem>(new (slot) SourceProviderCacheItem(parameters));
akling@apple.come93cba92013-01-28 06:34:03 +0000121}
122
123inline SourceProviderCacheItem::SourceProviderCacheItem(const SourceProviderCacheItemCreationParameters& parameters)
mark.lam@apple.comfa35e782013-11-19 21:55:16 +0000124 : functionNameStart(parameters.functionNameStart)
akling@apple.come93cba92013-01-28 06:34:03 +0000125 , needsFullActivation(parameters.needsFullActivation)
commit-queue@webkit.org2a979b32015-06-26 06:49:20 +0000126 , endFunctionOffset(parameters.endFunctionOffset)
akling@apple.come93cba92013-01-28 06:34:03 +0000127 , usesEval(parameters.usesEval)
sbarati@apple.come1671a12016-04-25 19:08:53 +0000128 , lastTokenLine(parameters.lastTokenLine)
akling@apple.come93cba92013-01-28 06:34:03 +0000129 , strictMode(parameters.strictMode)
sbarati@apple.come1671a12016-04-25 19:08:53 +0000130 , lastTokenStartOffset(parameters.lastTokenStartOffset)
131 , lastTokenEndOffset(parameters.lastTokenEndOffset)
132 , constructorKind(static_cast<unsigned>(parameters.constructorKind))
133 , parameterCount(parameters.parameterCount)
134 , expectedSuperBinding(static_cast<unsigned>(parameters.expectedSuperBinding))
gskachkov@gmail.com82b84582017-01-20 11:43:24 +0000135 , needsSuperBinding(parameters.needsSuperBinding)
commit-queue@webkit.orga1e1d572016-09-22 19:11:23 +0000136 , functionLength(parameters.functionLength)
sbarati@apple.come1671a12016-04-25 19:08:53 +0000137 , lastTokenLineStartOffset(parameters.lastTokenLineStartOffset)
akling@apple.come93cba92013-01-28 06:34:03 +0000138 , usedVariablesCount(parameters.usedVariables.size())
sbarati@apple.come1671a12016-04-25 19:08:53 +0000139 , innerArrowFunctionFeatures(parameters.innerArrowFunctionFeatures)
commit-queue@webkit.org2a979b32015-06-26 06:49:20 +0000140 , isBodyArrowExpression(parameters.isBodyArrowExpression)
141 , tokenType(parameters.tokenType)
akling@apple.come93cba92013-01-28 06:34:03 +0000142{
sbarati@apple.comd60fffd2016-04-21 23:30:36 +0000143 for (unsigned i = 0; i < usedVariablesCount; ++i) {
144 m_variables[i] = parameters.usedVariables[i];
145 m_variables[i]->ref();
akling@apple.come93cba92013-01-28 06:34:03 +0000146 }
147}
148
149#if COMPILER(MSVC)
150#pragma warning(pop)
151#endif
152
ryanhaddad@apple.com22104f52016-09-28 17:08:17 +0000153} // namespace JSC