source: webkit/trunk/JavaScriptCore/parser/Parser.cpp@ 38635

Last change on this file since 38635 was 38635, checked in by [email protected], 17 years ago

2008-11-20 Sam Weinig <[email protected]>

Reviewed by Darin Adler.

Patch for https://bugs.webkit.org/show_bug.cgi?id=22385
<rdar://problem/6390179>
Lazily reparse FunctionBodyNodes on first execution.

  • Saves 57MB on Membuster head.
  • bytecompiler/BytecodeGenerator.cpp: (JSC::BytecodeGenerator::generate): Remove vector shrinking since this is now handled by destroying the ScopeNodeData after generation.
  • parser/Grammar.y: Add alternate NoNode version of the grammar that does not create nodes. This is used to lazily create FunctionBodyNodes on first execution.
  • parser/Lexer.cpp: (JSC::Lexer::setCode): Fix bug where on reparse, the Lexer was confused about what position and length meant. Position is the current position in the original data buffer (important for getting correct line/column information) and length the end offset in the original buffer.
  • parser/Lexer.h: (JSC::Lexer::sourceCode): Positions are relative to the beginning of the buffer.
  • parser/Nodes.cpp: (JSC::ScopeNodeData::ScopeNodeData): Move initialization of ScopeNode data here. (JSC::ScopeNode::ScopeNode): Add constructor that only sets the JSGlobalData for FunctionBodyNode stubs. (JSC::ScopeNode::~ScopeNode): Release m_children now that we don't inherit from BlockNode. (JSC::ScopeNode::releaseNodes): Ditto. (JSC::EvalNode::generateBytecode): Only shrink m_children, as we need to keep around the rest of the data. (JSC::FunctionBodyNode::FunctionBodyNode): Add constructor that only sets the JSGlobalData. (JSC::FunctionBodyNode::create): Ditto. (JSC::FunctionBodyNode::generateBytecode): If we don't have the data, do a reparse to construct it. Then after generation, destroy the data. (JSC::ProgramNode::generateBytecode): After generation, destroy the AST data.
  • parser/Nodes.h: (JSC::ExpressionNode::): Add isFuncExprNode for FunctionConstructor. (JSC::StatementNode::): Add isExprStatementNode for FunctionConstructor. (JSC::ExprStatementNode::): Ditto. (JSC::ExprStatementNode::expr): Add accessor for FunctionConstructor. (JSC::FuncExprNode::): Add isFuncExprNode for FunctionConstructor

(JSC::ScopeNode::adoptData): Adopts a ScopeNodeData.
(JSC::ScopeNode::data): Accessor for ScopeNodeData.
(JSC::ScopeNode::destroyData): Deletes the ScopeNodeData.
(JSC::ScopeNode::setFeatures): Added.
(JSC::ScopeNode::varStack): Added assert.
(JSC::ScopeNode::functionStack): Ditto.
(JSC::ScopeNode::children): Ditto.
(JSC::ScopeNode::neededConstants): Ditto.
Factor m_varStack, m_functionStack, m_children and m_numConstants into ScopeNodeData.

  • parser/Parser.cpp: (JSC::Parser::reparse): Reparse the SourceCode in the FunctionBodyNode and set set up the ScopeNodeData for it.
  • parser/Parser.h:
  • parser/SourceCode.h: (JSC::SourceCode::endOffset): Added for use in the lexer.
  • runtime/FunctionConstructor.cpp: (JSC::getFunctionBody): Assuming a ProgramNode with one FunctionExpression in it, get the FunctionBodyNode. Any issues signifies a parse failure in constructFunction. (JSC::constructFunction): Make parsing functions in the form new Function(""), easier by concatenating the strings together (with some glue) and parsing the function expression as a ProgramNode from which we can receive the FunctionBodyNode. This has the added benefit of not having special parsing code for the arguments and lazily constructing the FunctionBodyNode's AST on first execution.
  • runtime/Identifier.h: (JSC::operator!=): Added.
  • Property svn:eol-style set to native
File size: 3.2 KB
Line 
1/*
2 * Copyright (C) 1999-2001 Harri Porten ([email protected])
3 * Copyright (C) 2001 Peter Kelly ([email protected])
4 * Copyright (C) 2003, 2006, 2007, 2008 Apple Inc. All rights reserved.
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
15 *
16 * You should have received a copy of the GNU Library General Public License
17 * along with this library; see the file COPYING.LIB. If not, write to
18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
20 *
21 */
22
23#include "config.h"
24#include "Parser.h"
25
26#include "Debugger.h"
27#include "Lexer.h"
28#include <wtf/HashSet.h>
29#include <wtf/Vector.h>
30
31extern int kjsyyparse(void*);
32
33namespace JSC {
34
35void Parser::parse(JSGlobalData* globalData, int* errLine, UString* errMsg)
36{
37 ASSERT(!m_sourceElements);
38
39 int defaultErrLine;
40 UString defaultErrMsg;
41
42 if (!errLine)
43 errLine = &defaultErrLine;
44 if (!errMsg)
45 errMsg = &defaultErrMsg;
46
47 *errLine = -1;
48 *errMsg = 0;
49
50 Lexer& lexer = *globalData->lexer;
51 lexer.setCode(*m_source);
52
53 int parseError = kjsyyparse(globalData);
54 bool lexError = lexer.sawError();
55 lexer.clear();
56
57 ParserRefCounted::deleteNewObjects(globalData);
58
59 if (parseError || lexError) {
60 *errLine = lexer.lineNo();
61 *errMsg = "Parse error";
62 m_sourceElements.clear();
63 }
64}
65
66void Parser::reparse(JSGlobalData* globalData, FunctionBodyNode* functionBodyNode)
67{
68 ASSERT(!functionBodyNode->data());
69
70 m_source = &functionBodyNode->source();
71 parse(globalData, 0, 0);
72 ASSERT(m_sourceElements);
73 functionBodyNode->adoptData(auto_ptr<ScopeNodeData>(new ScopeNodeData(m_sourceElements.get(),
74 m_varDeclarations ? &m_varDeclarations->data : 0,
75 m_funcDeclarations ? &m_funcDeclarations->data : 0,
76 m_numConstants)));
77 bool usesArguments = functionBodyNode->usesArguments();
78 functionBodyNode->setFeatures(m_features);
79 if (usesArguments && !functionBodyNode->usesArguments())
80 functionBodyNode->setUsesArguments();
81
82 m_source = 0;
83 m_sourceElements = 0;
84 m_varDeclarations = 0;
85 m_funcDeclarations = 0;
86}
87
88void Parser::didFinishParsing(SourceElements* sourceElements, ParserRefCountedData<DeclarationStacks::VarStack>* varStack,
89 ParserRefCountedData<DeclarationStacks::FunctionStack>* funcStack, CodeFeatures features, int lastLine, int numConstants)
90{
91 m_sourceElements = sourceElements;
92 m_varDeclarations = varStack;
93 m_funcDeclarations = funcStack;
94 m_features = features;
95 m_lastLine = lastLine;
96 m_numConstants = numConstants;
97}
98
99} // namespace JSC
Note: See TracBrowser for help on using the repository browser.