blob: 63c732d0ac190f5a592cb2b3e4e2936915640ba1 [file] [log] [blame]
kocienda66a6d362001-08-24 14:24:45 +00001/*
weinig@apple.coma4436412008-01-28 20:50:45 +00002* Copyright (C) 1999-2002 Harri Porten (porten@kde.org)
3* Copyright (C) 2001 Peter Kelly (pmk@post.com)
mark.lam@apple.com47c2f142016-03-16 18:16:32 +00004* Copyright (C) 2003-2009, 2013, 2016 Apple Inc. All rights reserved.
weinig@apple.coma4436412008-01-28 20:50:45 +00005* Copyright (C) 2007 Cameron Zwarich (cwzwarich@uwaterloo.ca)
6* Copyright (C) 2007 Maks Orlovich
7* Copyright (C) 2007 Eric Seidel <eric@webkit.org>
8*
9* This library is free software; you can redistribute it and/or
10* modify it under the terms of the GNU Library General Public
11* License as published by the Free Software Foundation; either
12* version 2 of the License, or (at your option) any later version.
13*
14* This library is distributed in the hope that it will be useful,
15* but WITHOUT ANY WARRANTY; without even the implied warranty of
16* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17* Library General Public License for more details.
18*
19* You should have received a copy of the GNU Library General Public License
20* along with this library; see the file COPYING.LIB. If not, write to
21* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22* Boston, MA 02110-1301, USA.
23*
24*/
kocienda66a6d362001-08-24 14:24:45 +000025
mjsb64c50a2005-10-03 21:13:12 +000026#include "config.h"
cwzwarich@webkit.org3ff0e6a2008-11-07 00:18:07 +000027#include "Nodes.h"
darin@apple.comd7f68222009-05-06 17:10:22 +000028#include "NodeConstructors.h"
kocienda66a6d362001-08-24 14:24:45 +000029
ggaren@apple.com5169fc92008-11-17 22:11:26 +000030#include "CallFrame.h"
darin@apple.come615d312009-05-05 23:09:52 +000031#include "Debugger.h"
oliver@apple.com4d51a9b2009-05-05 12:31:13 +000032#include "JIT.h"
oliver@apple.comfee5d6b2009-05-05 12:22:33 +000033#include "JSFunction.h"
mjs07781952007-10-25 07:16:49 +000034#include "JSGlobalObject.h"
ggaren@apple.com4dbab802008-10-31 19:59:08 +000035#include "LabelScope.h"
weinig@apple.com72d63492009-05-06 01:22:42 +000036#include "Lexer.h"
fpizlo@apple.comfb7eff22014-02-11 01:45:50 +000037#include "JSCInlines.h"
ggaren@apple.comc7006662007-12-08 00:39:50 +000038#include "Parser.h"
darinbd80a2a2007-10-23 07:00:37 +000039#include "PropertyNameArray.h"
cwzwarich@webkit.org12b8add2008-06-17 08:45:03 +000040#include "RegExpObject.h"
ggaren7213ee02007-10-16 23:25:33 +000041#include <wtf/Assertions.h>
slewis@apple.coma7615ca2008-07-12 05:51:33 +000042#include <wtf/RefCountedLeakCounter.h>
ap@webkit.org66868c02008-06-24 09:51:07 +000043#include <wtf/Threading.h>
kocienda66a6d362001-08-24 14:24:45 +000044
sfalken@apple.com861f9382008-06-24 19:50:50 +000045using namespace WTF;
46
cwzwarich@webkit.org3f782f62008-09-08 01:28:33 +000047namespace JSC {
kocienda66a6d362001-08-24 14:24:45 +000048
darin@apple.com85a65c32009-08-22 06:40:53 +000049
mjs6f821c82002-03-22 00:31:57 +000050// ------------------------------ StatementNode --------------------------------
darin98e28602005-08-16 00:47:46 +000051
mark.lam@apple.com5b45f902013-07-09 16:15:12 +000052void StatementNode::setLoc(unsigned firstLine, unsigned lastLine, int startOffset, int lineStartOffset)
commit-queue@webkit.org1b331cb2012-08-06 03:16:46 +000053{
commit-queue@webkit.org1b331cb2012-08-06 03:16:46 +000054 m_lastLine = lastLine;
mark.lam@apple.com3b256ca2013-07-30 17:01:40 +000055 m_position = JSTextPosition(firstLine, startOffset, lineStartOffset);
56 ASSERT(m_position.offset >= m_position.lineStartOffset);
mjs6f821c82002-03-22 00:31:57 +000057}
58
darin@apple.com5e49bae2007-12-21 21:54:51 +000059// ------------------------------ SourceElements --------------------------------
60
darin@apple.com59c37152009-05-10 22:32:34 +000061void SourceElements::append(StatementNode* statement)
mjs6f821c82002-03-22 00:31:57 +000062{
mjs@apple.comd0af8292007-12-24 10:13:00 +000063 if (statement->isEmptyStatement())
64 return;
ggaren@apple.com86cb7be2014-12-09 01:53:53 +000065
66 if (!m_head) {
67 m_head = statement;
68 m_tail = statement;
69 return;
70 }
71
72 m_tail->setNext(statement);
73 m_tail = statement;
darin@apple.com5e49bae2007-12-21 21:54:51 +000074}
75
barraclough@apple.comb467c4c2010-07-13 20:34:11 +000076StatementNode* SourceElements::singleStatement() const
darin@apple.com85a65c32009-08-22 06:40:53 +000077{
ggaren@apple.com86cb7be2014-12-09 01:53:53 +000078 return m_head == m_tail ? m_head : nullptr;
darin@apple.com85a65c32009-08-22 06:40:53 +000079}
80
wingo@igalia.com08f0e4c2012-03-07 10:18:43 +000081// ------------------------------ ScopeNode -----------------------------
mjs6f821c82002-03-22 00:31:57 +000082
ggaren@apple.comc0ccae02014-12-03 23:23:56 +000083ScopeNode::ScopeNode(ParserArena& parserArena, const JSTokenLocation& startLocation, const JSTokenLocation& endLocation, bool inStrictContext)
mark.lam@apple.comfaa53932013-03-20 09:09:38 +000084 : StatementNode(endLocation)
ggaren@apple.com9e5d58e2014-12-05 00:59:33 +000085 , ParserArenaRoot(parserArena)
mark.lam@apple.comfaa53932013-03-20 09:09:38 +000086 , m_startLineNumber(startLocation.line)
mark.lam@apple.com5b45f902013-07-09 16:15:12 +000087 , m_startStartOffset(startLocation.startOffset)
88 , m_startLineStartOffset(startLocation.lineStartOffset)
oliver@apple.com6bc11372012-05-07 23:35:52 +000089 , m_features(inStrictContext ? StrictModeFeature : NoFeatures)
gskachkov@gmail.coma198dd12016-02-28 19:14:26 +000090 , m_innerArrowFunctionCodeFeatures(NoInnerArrowFunctionFeatures)
wingo@igalia.com08f0e4c2012-03-07 10:18:43 +000091 , m_numConstants(0)
92 , m_statements(0)
kocienda66a6d362001-08-24 14:24:45 +000093{
wingo@igalia.com08f0e4c2012-03-07 10:18:43 +000094}
95
sbarati@apple.com432a3c92016-04-03 19:45:05 +000096ScopeNode::ScopeNode(ParserArena& parserArena, const JSTokenLocation& startLocation, const JSTokenLocation& endLocation, const SourceCode& source, SourceElements* children, VariableEnvironment& varEnvironment, FunctionStack&& funcStack, VariableEnvironment& lexicalVariables, UniquedStringImplPtrSet&& sloppyModeHoistedFunctions, CodeFeatures features, InnerArrowFunctionCodeFeatures innerArrowFunctionCodeFeatures, int numConstants)
mark.lam@apple.comfaa53932013-03-20 09:09:38 +000097 : StatementNode(endLocation)
ggaren@apple.com9e5d58e2014-12-05 00:59:33 +000098 , ParserArenaRoot(parserArena)
sbarati@apple.com8e614bd2016-03-10 02:04:20 +000099 , VariableEnvironmentNode(lexicalVariables, WTFMove(funcStack))
mark.lam@apple.comfaa53932013-03-20 09:09:38 +0000100 , m_startLineNumber(startLocation.line)
mark.lam@apple.com5b45f902013-07-09 16:15:12 +0000101 , m_startStartOffset(startLocation.startOffset)
102 , m_startLineStartOffset(startLocation.lineStartOffset)
oliver@apple.com6bc11372012-05-07 23:35:52 +0000103 , m_features(features)
gskachkov@gmail.coma198dd12016-02-28 19:14:26 +0000104 , m_innerArrowFunctionCodeFeatures(innerArrowFunctionCodeFeatures)
wingo@igalia.com08f0e4c2012-03-07 10:18:43 +0000105 , m_source(source)
sbarati@apple.com432a3c92016-04-03 19:45:05 +0000106 , m_sloppyModeHoistedFunctions(WTFMove(sloppyModeHoistedFunctions))
wingo@igalia.com08f0e4c2012-03-07 10:18:43 +0000107 , m_numConstants(numConstants)
108 , m_statements(children)
109{
saambarati1@gmail.com144f17c2015-07-15 21:41:08 +0000110 m_varDeclarations.swap(varEnvironment);
weinig@apple.com9b5bf062008-11-20 23:23:59 +0000111}
112
darin@apple.com85a65c32009-08-22 06:40:53 +0000113StatementNode* ScopeNode::singleStatement() const
114{
wingo@igalia.com08f0e4c2012-03-07 10:18:43 +0000115 return m_statements ? m_statements->singleStatement() : 0;
darin@apple.com85a65c32009-08-22 06:40:53 +0000116}
117
darin@apple.com1076ac22008-01-27 09:38:01 +0000118// ------------------------------ ProgramNode -----------------------------
119
sbarati@apple.com432a3c92016-04-03 19:45:05 +0000120ProgramNode::ProgramNode(ParserArena& parserArena, const JSTokenLocation& startLocation, const JSTokenLocation& endLocation, unsigned startColumn, unsigned endColumn, SourceElements* children, VariableEnvironment& varEnvironment, FunctionStack&& funcStack, VariableEnvironment& lexicalVariables, UniquedStringImplPtrSet&& sloppyModeHoistedFunctions, FunctionParameters*, const SourceCode& source, CodeFeatures features, InnerArrowFunctionCodeFeatures innerArrowFunctionCodeFeatures, int numConstants)
121 : ScopeNode(parserArena, startLocation, endLocation, source, children, varEnvironment, WTFMove(funcStack), lexicalVariables, WTFMove(sloppyModeHoistedFunctions), features, innerArrowFunctionCodeFeatures, numConstants)
mark.lam@apple.com5b45f902013-07-09 16:15:12 +0000122 , m_startColumn(startColumn)
mark.lam@apple.comfa35e782013-11-19 21:55:16 +0000123 , m_endColumn(endColumn)
ggaren@apple.come65a2902007-12-11 05:47:41 +0000124{
125}
126
utatane.tea@gmail.com9f9c9752015-08-04 21:26:49 +0000127// ------------------------------ ModuleProgramNode -----------------------------
128
sbarati@apple.com432a3c92016-04-03 19:45:05 +0000129ModuleProgramNode::ModuleProgramNode(ParserArena& parserArena, const JSTokenLocation& startLocation, const JSTokenLocation& endLocation, unsigned startColumn, unsigned endColumn, SourceElements* children, VariableEnvironment& varEnvironment, FunctionStack&& funcStack, VariableEnvironment& lexicalVariables, UniquedStringImplPtrSet&& sloppyModeHoistedFunctions, FunctionParameters*, const SourceCode& source, CodeFeatures features, InnerArrowFunctionCodeFeatures innerArrowFunctionCodeFeatures, int numConstants)
130 : ScopeNode(parserArena, startLocation, endLocation, source, children, varEnvironment, WTFMove(funcStack), lexicalVariables, WTFMove(sloppyModeHoistedFunctions), features, innerArrowFunctionCodeFeatures, numConstants)
utatane.tea@gmail.com9f9c9752015-08-04 21:26:49 +0000131 , m_startColumn(startColumn)
132 , m_endColumn(endColumn)
133{
134}
135
darin@apple.com1076ac22008-01-27 09:38:01 +0000136// ------------------------------ EvalNode -----------------------------
137
sbarati@apple.com432a3c92016-04-03 19:45:05 +0000138EvalNode::EvalNode(ParserArena& parserArena, const JSTokenLocation& startLocation, const JSTokenLocation& endLocation, unsigned, unsigned endColumn, SourceElements* children, VariableEnvironment& varEnvironment, FunctionStack&& funcStack, VariableEnvironment& lexicalVariables, UniquedStringImplPtrSet&& sloppyModeHoistedFunctions, FunctionParameters*, const SourceCode& source, CodeFeatures features, InnerArrowFunctionCodeFeatures innerArrowFunctionCodeFeatures, int numConstants)
139 : ScopeNode(parserArena, startLocation, endLocation, source, children, varEnvironment, WTFMove(funcStack), lexicalVariables, WTFMove(sloppyModeHoistedFunctions), features, innerArrowFunctionCodeFeatures, numConstants)
mark.lam@apple.comfa35e782013-11-19 21:55:16 +0000140 , m_endColumn(endColumn)
ggaren@apple.come65a2902007-12-11 05:47:41 +0000141{
142}
143
ggaren@apple.come7afe402015-08-10 20:24:35 +0000144// ------------------------------ FunctionMetadataNode -----------------------------
darin@apple.com1076ac22008-01-27 09:38:01 +0000145
ggaren@apple.come7afe402015-08-10 20:24:35 +0000146FunctionMetadataNode::FunctionMetadataNode(
ggaren@apple.comd3e1f862015-03-20 23:37:51 +0000147 ParserArena&, const JSTokenLocation& startLocation,
148 const JSTokenLocation& endLocation, unsigned startColumn, unsigned endColumn,
saambarati1@gmail.comc497d152015-07-17 18:48:30 +0000149 int functionKeywordStart, int functionNameStart, int parametersStart, bool isInStrictContext,
commit-queue@webkit.orgabdc6172016-02-24 20:46:44 +0000150 ConstructorKind constructorKind, SuperBinding superBinding, unsigned parameterCount, SourceParseMode mode, bool isArrowFunctionBodyExpression)
ggaren@apple.come7afe402015-08-10 20:24:35 +0000151 : Node(endLocation)
ggaren@apple.comd3e1f862015-03-20 23:37:51 +0000152 , m_startColumn(startColumn)
153 , m_endColumn(endColumn)
154 , m_functionKeywordStart(functionKeywordStart)
155 , m_functionNameStart(functionNameStart)
156 , m_parametersStart(parametersStart)
157 , m_startStartOffset(startLocation.startOffset)
saambarati1@gmail.comc497d152015-07-17 18:48:30 +0000158 , m_parameterCount(parameterCount)
159 , m_parseMode(mode)
ggaren@apple.comd3e1f862015-03-20 23:37:51 +0000160 , m_isInStrictContext(isInStrictContext)
utatane.tea@gmail.comfdd9bf42015-12-02 03:16:28 +0000161 , m_superBinding(static_cast<unsigned>(superBinding))
ggaren@apple.comd3e1f862015-03-20 23:37:51 +0000162 , m_constructorKind(static_cast<unsigned>(constructorKind))
commit-queue@webkit.org04b24472015-08-25 19:10:29 +0000163 , m_isArrowFunctionBodyExpression(isArrowFunctionBodyExpression)
weinig@apple.com9b5bf062008-11-20 23:23:59 +0000164{
utatane.tea@gmail.comfdd9bf42015-12-02 03:16:28 +0000165 ASSERT(m_superBinding == static_cast<unsigned>(superBinding));
rniwa@webkit.orge6f83492015-03-13 23:01:51 +0000166 ASSERT(m_constructorKind == static_cast<unsigned>(constructorKind));
weinig@apple.com9b5bf062008-11-20 23:23:59 +0000167}
168
mark.lam@apple.com89ae9982016-03-17 20:38:56 +0000169void FunctionMetadataNode::finishParsing(const SourceCode& source, const Identifier& ident, FunctionMode functionMode)
ggaren@apple.com6f851d72014-12-04 23:47:49 +0000170{
171 m_source = source;
ggaren@apple.com6f851d72014-12-04 23:47:49 +0000172 m_ident = ident;
173 m_functionMode = functionMode;
174}
175
ggaren@apple.come7afe402015-08-10 20:24:35 +0000176void FunctionMetadataNode::setEndPosition(JSTextPosition position)
ggaren@apple.com6f851d72014-12-04 23:47:49 +0000177{
178 m_lastLine = position.line;
179 m_endColumn = position.offset - position.lineStartOffset;
180}
181
ggaren@apple.com9e5d58e2014-12-05 00:59:33 +0000182// ------------------------------ FunctionNode -----------------------------
ggaren@apple.com6f851d72014-12-04 23:47:49 +0000183
sbarati@apple.com432a3c92016-04-03 19:45:05 +0000184FunctionNode::FunctionNode(ParserArena& parserArena, const JSTokenLocation& startLocation, const JSTokenLocation& endLocation, unsigned startColumn, unsigned endColumn, SourceElements* children, VariableEnvironment& varEnvironment, FunctionStack&& funcStack, VariableEnvironment& lexicalVariables, UniquedStringImplPtrSet&& sloppyModeHoistedFunctions, FunctionParameters* parameters, const SourceCode& sourceCode, CodeFeatures features, InnerArrowFunctionCodeFeatures innerArrowFunctionCodeFeatures, int numConstants)
185 : ScopeNode(parserArena, startLocation, endLocation, sourceCode, children, varEnvironment, WTFMove(funcStack), lexicalVariables, WTFMove(sloppyModeHoistedFunctions), features, innerArrowFunctionCodeFeatures, numConstants)
saambarati1@gmail.comc497d152015-07-17 18:48:30 +0000186 , m_parameters(parameters)
mark.lam@apple.com5b45f902013-07-09 16:15:12 +0000187 , m_startColumn(startColumn)
mark.lam@apple.comfa35e782013-11-19 21:55:16 +0000188 , m_endColumn(endColumn)
ggaren@apple.come65a2902007-12-11 05:47:41 +0000189{
190}
191
mark.lam@apple.com89ae9982016-03-17 20:38:56 +0000192void FunctionNode::finishParsing(const Identifier& ident, FunctionMode functionMode)
mark.lam@apple.comfa35e782013-11-19 21:55:16 +0000193{
ggaren@apple.com6f851d72014-12-04 23:47:49 +0000194 ASSERT(!source().isNull());
ggaren@apple.com6f851d72014-12-04 23:47:49 +0000195 m_ident = ident;
196 m_functionMode = functionMode;
mark.lam@apple.comfa35e782013-11-19 21:55:16 +0000197}
198
mark.lam@apple.com47c2f142016-03-16 18:16:32 +0000199bool PropertyListNode::hasStaticallyNamedProperty(const Identifier& propName)
200{
201 PropertyListNode* list = this;
202 while (list) {
203 const Identifier* currentNodeName = list->m_node->name();
204 if (currentNodeName && *currentNodeName == propName)
205 return true;
206 list = list->m_next;
207 }
208 return false;
209}
210
saambarati1@gmail.com144f17c2015-07-15 21:41:08 +0000211VariableEnvironmentNode::VariableEnvironmentNode(VariableEnvironment& lexicalVariables)
212{
213 m_lexicalVariables.swap(lexicalVariables);
214}
215
sbarati@apple.com8e614bd2016-03-10 02:04:20 +0000216VariableEnvironmentNode::VariableEnvironmentNode(VariableEnvironment& lexicalVariables, FunctionStack&& functionStack)
217{
218 m_lexicalVariables.swap(lexicalVariables);
219 m_functionStack = WTFMove(functionStack);
220}
221
cwzwarich@webkit.org3f782f62008-09-08 01:28:33 +0000222} // namespace JSC