Title: [255047] trunk/Source/_javascript_Core
Revision
255047
Author
sbar...@apple.com
Date
2020-01-23 18:54:37 -0800 (Thu, 23 Jan 2020)

Log Message

Get rid of didFinishParsing and make parseInner return its results
https://bugs.webkit.org/show_bug.cgi?id=206706

Reviewed by Mark Lam and Keith Miller and Yusuke Suzuki.

This is paving the way for eagerly parsing immediately invoked functions.
Before, we'd just end up setting member fields inside ::didFinishParsing,
and then read them out inside ::parse. However, this is not going to work
when we are in a world where we're generating AST nodes for more than one
function at a time. This patch paves the way for that.

* parser/Parser.cpp:
(JSC::Parser<LexerType>::Parser):
(JSC::Parser<LexerType>::parseInner):
(JSC::Parser<LexerType>::didFinishParsing): Deleted.
* parser/Parser.h:
(JSC::Parser<LexerType>::parse):

Modified Paths

Diff

Modified: trunk/Source/_javascript_Core/ChangeLog (255046 => 255047)


--- trunk/Source/_javascript_Core/ChangeLog	2020-01-24 02:22:11 UTC (rev 255046)
+++ trunk/Source/_javascript_Core/ChangeLog	2020-01-24 02:54:37 UTC (rev 255047)
@@ -1,3 +1,23 @@
+2020-01-23  Saam Barati  <sbar...@apple.com>
+
+        Get rid of didFinishParsing and make parseInner return its results
+        https://bugs.webkit.org/show_bug.cgi?id=206706
+
+        Reviewed by Mark Lam and Keith Miller and Yusuke Suzuki.
+
+        This is paving the way for eagerly parsing immediately invoked functions.
+        Before, we'd just end up setting member fields inside ::didFinishParsing,
+        and then read them out inside ::parse. However, this is not going to work
+        when we are in a world where we're generating AST nodes for more than one
+        function at a time. This patch paves the way for that.
+
+        * parser/Parser.cpp:
+        (JSC::Parser<LexerType>::Parser):
+        (JSC::Parser<LexerType>::parseInner):
+        (JSC::Parser<LexerType>::didFinishParsing): Deleted.
+        * parser/Parser.h:
+        (JSC::Parser<LexerType>::parse):
+
 2020-01-23  Robin Morisset  <rmoris...@apple.com>
 
         Don't include BytecodeStructs.h in CommonSlowPaths.h

Modified: trunk/Source/_javascript_Core/parser/Parser.cpp (255046 => 255047)


--- trunk/Source/_javascript_Core/parser/Parser.cpp	2020-01-24 02:22:11 UTC (rev 255046)
+++ trunk/Source/_javascript_Core/parser/Parser.cpp	2020-01-24 02:54:37 UTC (rev 255047)
@@ -132,7 +132,6 @@
     , m_hasStackOverflow(false)
     , m_allowsIn(true)
     , m_statementDepth(0)
-    , m_sourceElements(0)
     , m_parsingBuiltin(builtinMode == JSParserBuiltinMode::Builtin)
     , m_scriptMode(scriptMode)
     , m_superBinding(superBinding)
@@ -208,24 +207,23 @@
 }
 
 template <typename LexerType>
-String Parser<LexerType>::parseInner(const Identifier& calleeName, SourceParseMode parseMode, ParsingContext parsingContext, Optional<int> functionConstructorParametersEndPosition, const Vector<JSTextPosition>* instanceFieldLocations)
+Expected<typename Parser<LexerType>::ParseInnerResult, String> Parser<LexerType>::parseInner(const Identifier& calleeName, SourceParseMode parseMode, ParsingContext parsingContext, Optional<int> functionConstructorParametersEndPosition, const Vector<JSTextPosition>* instanceFieldLocations)
 {
-    String parseError = String();
-
     ASTBuilder context(const_cast<VM&>(m_vm), m_parserArena, const_cast<SourceCode*>(m_source));
     ScopeRef scope = currentScope();
     scope->setIsLexicalScope();
     SetForScope<FunctionParsePhase> functionParsePhasePoisoner(m_parserState.functionParsePhase, FunctionParsePhase::Body);
 
+    FunctionParameters* parameters = nullptr;
     bool isArrowFunctionBodyExpression = parseMode == SourceParseMode::AsyncArrowFunctionBodyMode && !match(OPENBRACE);
     if (m_lexer->isReparsingFunction()) {
         ParserFunctionInfo<ASTBuilder> functionInfo;
         if (isGeneratorOrAsyncFunctionBodyParseMode(parseMode))
-            m_parameters = createGeneratorParameters(context, functionInfo.parameterCount);
+            parameters = createGeneratorParameters(context, functionInfo.parameterCount);
         else if (parseMode == SourceParseMode::InstanceFieldInitializerMode)
-            m_parameters = context.createFormalParameterList();
+            parameters = context.createFormalParameterList();
         else
-            m_parameters = parseFunctionParameters(context, parseMode, functionInfo);
+            parameters = parseFunctionParameters(context, parseMode, functionInfo);
 
         if (SourceParseModeSet(SourceParseMode::ArrowFunctionMode, SourceParseMode::AsyncArrowFunctionMode).contains(parseMode) && !hasError()) {
             // The only way we could have an error wile reparsing is if we run out of stack space.
@@ -264,12 +262,8 @@
     }
 
     bool validEnding = consume(EOFTOK);
-    if (!sourceElements || !validEnding) {
-        if (hasError())
-            parseError = m_errorMessage;
-        else
-            parseError = "Parser error"_s;
-    }
+    if (!sourceElements || !validEnding)
+        return makeUnexpected(hasError() ? m_errorMessage : "Parser error"_s);
 
     IdentifierSet capturedVariables;
     UniquedStringImplPtrSet sloppyModeHoistedFunctions;
@@ -293,7 +287,7 @@
     if (m_seenTaggedTemplate)
         features |= NoEvalCacheFeature;
 
-#ifndef NDEBUG
+#if ASSERT_ENABLED
     if (m_parsingBuiltin && isProgramParseMode(parseMode)) {
         VariableEnvironment& lexicalVariables = scope->lexicalVariables();
         const HashSet<UniquedStringImpl*>& closedVariableCandidates = scope->closedVariableCandidates();
@@ -306,25 +300,12 @@
             }
         }
     }
-#endif // NDEBUG
-    didFinishParsing(sourceElements, scope->takeFunctionDeclarations(), varDeclarations, WTFMove(sloppyModeHoistedFunctions), features, context.numConstants());
+#endif // ASSERT_ENABLED
 
-    return parseError;
+    return ParseInnerResult { parameters, sourceElements, scope->takeFunctionDeclarations(), WTFMove(varDeclarations), WTFMove(sloppyModeHoistedFunctions), features, context.numConstants() };
 }
 
 template <typename LexerType>
-void Parser<LexerType>::didFinishParsing(SourceElements* sourceElements, DeclarationStacks::FunctionStack&& funcStack, 
-    VariableEnvironment& varDeclarations, UniquedStringImplPtrSet&& sloppyModeHoistedFunctions, CodeFeatures features, int numConstants)
-{
-    m_sourceElements = sourceElements;
-    m_funcDeclarations = WTFMove(funcStack);
-    m_varDeclarations.swap(varDeclarations);
-    m_features = features;
-    m_sloppyModeHoistedFunctions = WTFMove(sloppyModeHoistedFunctions);
-    m_numConstants = numConstants;
-}
-
-template <typename LexerType>
 bool Parser<LexerType>::isArrowFunctionParameters()
 {
     if (match(OPENPAREN)) {

Modified: trunk/Source/_javascript_Core/parser/Parser.h (255046 => 255047)


--- trunk/Source/_javascript_Core/parser/Parser.h	2020-01-24 02:22:11 UTC (rev 255046)
+++ trunk/Source/_javascript_Core/parser/Parser.h	2020-01-24 02:54:37 UTC (rev 255047)
@@ -1372,7 +1372,16 @@
 
     Parser();
 
-    String parseInner(const Identifier&, SourceParseMode, ParsingContext, Optional<int> functionConstructorParametersEndPosition = WTF::nullopt, const Vector<JSTextPosition>* = nullptr);
+    struct ParseInnerResult {
+        FunctionParameters* parameters;
+        SourceElements* sourceElements;
+        DeclarationStacks::FunctionStack functionDeclarations;
+        VariableEnvironment varDeclarations;
+        UniquedStringImplPtrSet sloppyModeHoistedFunctions;
+        CodeFeatures features;
+        int numConstants;
+    };
+    Expected<ParseInnerResult, String> parseInner(const Identifier&, SourceParseMode, ParsingContext, Optional<int> functionConstructorParametersEndPosition = WTF::nullopt, const Vector<JSTextPosition>* = nullptr);
 
     void didFinishParsing(SourceElements*, DeclarationStacks::FunctionStack&&, VariableEnvironment&, UniquedStringImplPtrSet&&, CodeFeatures, int);
 
@@ -1896,7 +1905,6 @@
     const SourceCode* m_source;
     ParserArena m_parserArena;
     std::unique_ptr<LexerType> m_lexer;
-    FunctionParameters* m_parameters { nullptr };
 
     ParserState m_parserState;
     
@@ -1907,16 +1915,10 @@
     JSTextPosition m_lastTokenEndPosition;
     int m_statementDepth;
     RefPtr<SourceProviderCache> m_functionCache;
-    SourceElements* m_sourceElements;
     bool m_parsingBuiltin;
     JSParserScriptMode m_scriptMode;
     SuperBinding m_superBinding;
     ConstructorKind m_defaultConstructorKindForTopLevelFunction;
-    VariableEnvironment m_varDeclarations;
-    DeclarationStacks::FunctionStack m_funcDeclarations;
-    UniquedStringImplPtrSet m_sloppyModeHoistedFunctions;
-    CodeFeatures m_features;
-    int m_numConstants;
     ExpressionErrorClassifier* m_expressionErrorClassifier;
     bool m_isEvalContext;
     bool m_immediateParentAllowsFunctionDeclarationInStatement;
@@ -1937,8 +1939,6 @@
     if (ParsedNode::scopeIsFunction)
         m_lexer->setIsReparsingFunction();
 
-    m_sourceElements = 0;
-
     errLine = -1;
     errMsg = String();
 
@@ -1946,7 +1946,7 @@
     ASSERT(m_source->startColumn() > OrdinalNumber::beforeFirst());
     unsigned startColumn = m_source->startColumn().zeroBasedInt();
 
-    String parseError = parseInner(calleeName, parseMode, parsingContext, functionConstructorParametersEndPosition, instanceFieldLocations);
+    auto parseResult = parseInner(calleeName, parseMode, parsingContext, functionConstructorParametersEndPosition, instanceFieldLocations);
 
     int lineNumber = m_lexer->lineNumber();
     bool lexError = m_lexer->sawError();
@@ -1954,14 +1954,13 @@
     ASSERT(lexErrorMessage.isNull() != lexError);
     m_lexer->clear();
 
-    if (!parseError.isNull() || lexError) {
+    if (!parseResult || lexError) {
         errLine = lineNumber;
-        errMsg = !lexErrorMessage.isNull() ? lexErrorMessage : parseError;
-        m_sourceElements = 0;
+        errMsg = !lexErrorMessage.isNull() ? lexErrorMessage : parseResult.error();
     }
 
     std::unique_ptr<ParsedNode> result;
-    if (m_sourceElements) {
+    if (parseResult) {
         JSTokenLocation endLocation;
         endLocation.line = m_lexer->lineNumber();
         endLocation.lineStartOffset = m_lexer->currentLineStartOffset();
@@ -1972,16 +1971,16 @@
                                     endLocation,
                                     startColumn,
                                     endColumn,
-                                    m_sourceElements,
-                                    m_varDeclarations,
-                                    WTFMove(m_funcDeclarations),
+                                    parseResult.value().sourceElements,
+                                    parseResult.value().varDeclarations,
+                                    WTFMove(parseResult.value().functionDeclarations),
                                     currentScope()->finalizeLexicalEnvironment(),
-                                    WTFMove(m_sloppyModeHoistedFunctions),
-                                    m_parameters,
+                                    WTFMove(parseResult.value().sloppyModeHoistedFunctions),
+                                    parseResult.value().parameters,
                                     *m_source,
-                                    m_features,
+                                    parseResult.value().features,
                                     currentScope()->innerArrowFunctionFeatures(),
-                                    m_numConstants,
+                                    parseResult.value().numConstants,
                                     WTFMove(m_moduleScopeData));
         result->setLoc(m_source->firstLine().oneBasedInt(), m_lexer->lineNumber(), m_lexer->currentOffset(), m_lexer->currentLineStartOffset());
         result->setEndOffset(m_lexer->currentOffset());
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to