Title: [180637] trunk/Source/_javascript_Core
Revision
180637
Author
[email protected]
Date
2015-02-25 14:29:26 -0800 (Wed, 25 Feb 2015)

Log Message

Make ParserError immutable by design
https://bugs.webkit.org/show_bug.cgi?id=141955

Reviewed by Geoffrey Garen.

This patch enforce that no field of ParserError can
be modified after the constructor.

* parser/ParserError.h:
Move the attributes to pack the integer + 2 bytes together.
This is irrelevant for memory impact, it is to remve a load-store
when copying by value.

Also move the attributes to be private.

(JSC::ParserError::isValid):
To client of the interface cared about the type of the error,
the only information needed was: is there an error.

(JSC::ParserError::ParserError):
(JSC::ParserError::syntaxErrorType):
(JSC::ParserError::token):
(JSC::ParserError::message):
(JSC::ParserError::line):
(JSC::ParserError::toErrorObject):
* API/JSScriptRef.cpp:
* builtins/BuiltinExecutables.cpp:
(JSC::BuiltinExecutables::createBuiltinExecutable):
* bytecode/UnlinkedCodeBlock.cpp:
(JSC::generateFunctionCodeBlock):
(JSC::UnlinkedFunctionExecutable::fromGlobalCode):
(JSC::UnlinkedFunctionExecutable::codeBlockFor):
* bytecode/UnlinkedCodeBlock.h:
* inspector/agents/InspectorRuntimeAgent.cpp:
(Inspector::InspectorRuntimeAgent::parse):
* jsc.cpp:
(runInteractive):
* parser/Parser.h:
(JSC::parse):
* runtime/CodeCache.cpp:
(JSC::CodeCache::getGlobalCodeBlock):
(JSC::CodeCache::getFunctionExecutableFromGlobalCode):
* runtime/CodeCache.h:
* runtime/Completion.h:
* runtime/Executable.cpp:
(JSC::ProgramExecutable::checkSyntax):
* runtime/JSGlobalObject.cpp:
(JSC::JSGlobalObject::createProgramCodeBlock):
(JSC::JSGlobalObject::createEvalCodeBlock):

Modified Paths

Diff

Modified: trunk/Source/_javascript_Core/API/JSScriptRef.cpp (180636 => 180637)


--- trunk/Source/_javascript_Core/API/JSScriptRef.cpp	2015-02-25 21:27:37 UTC (rev 180636)
+++ trunk/Source/_javascript_Core/API/JSScriptRef.cpp	2015-02-25 22:29:26 UTC (rev 180637)
@@ -89,10 +89,10 @@
     ParserError error;
     if (!parseScript(vm, SourceCode(result), error)) {
         if (errorMessage)
-            *errorMessage = OpaqueJSString::create(error.m_message).leakRef();
+            *errorMessage = OpaqueJSString::create(error.message()).leakRef();
         if (errorLine)
-            *errorLine = error.m_line;
-        return 0;
+            *errorLine = error.line();
+        return nullptr;
     }
 
     return result.release().leakRef();
@@ -110,10 +110,10 @@
     ParserError error;
     if (!parseScript(vm, SourceCode(result), error)) {
         if (errorMessage)
-            *errorMessage = OpaqueJSString::create(error.m_message).leakRef();
+            *errorMessage = OpaqueJSString::create(error.message()).leakRef();
         if (errorLine)
-            *errorLine = error.m_line;
-        return 0;
+            *errorLine = error.line();
+        return nullptr;
     }
 
     return result.release().leakRef();

Modified: trunk/Source/_javascript_Core/ChangeLog (180636 => 180637)


--- trunk/Source/_javascript_Core/ChangeLog	2015-02-25 21:27:37 UTC (rev 180636)
+++ trunk/Source/_javascript_Core/ChangeLog	2015-02-25 22:29:26 UTC (rev 180637)
@@ -1,3 +1,55 @@
+2015-02-25  Benjamin Poulain  <[email protected]>
+
+        Make ParserError immutable by design
+        https://bugs.webkit.org/show_bug.cgi?id=141955
+
+        Reviewed by Geoffrey Garen.
+
+        This patch enforce that no field of ParserError can
+        be modified after the constructor.
+
+        * parser/ParserError.h:
+        Move the attributes to pack the integer + 2 bytes together.
+        This is irrelevant for memory impact, it is to remve a load-store
+        when copying by value.
+
+        Also move the attributes to be private.
+
+        (JSC::ParserError::isValid):
+        To client of the interface cared about the type of the error,
+        the only information needed was: is there an error.
+
+        (JSC::ParserError::ParserError):
+        (JSC::ParserError::syntaxErrorType):
+        (JSC::ParserError::token):
+        (JSC::ParserError::message):
+        (JSC::ParserError::line):
+        (JSC::ParserError::toErrorObject):
+        * API/JSScriptRef.cpp:
+        * builtins/BuiltinExecutables.cpp:
+        (JSC::BuiltinExecutables::createBuiltinExecutable):
+        * bytecode/UnlinkedCodeBlock.cpp:
+        (JSC::generateFunctionCodeBlock):
+        (JSC::UnlinkedFunctionExecutable::fromGlobalCode):
+        (JSC::UnlinkedFunctionExecutable::codeBlockFor):
+        * bytecode/UnlinkedCodeBlock.h:
+        * inspector/agents/InspectorRuntimeAgent.cpp:
+        (Inspector::InspectorRuntimeAgent::parse):
+        * jsc.cpp:
+        (runInteractive):
+        * parser/Parser.h:
+        (JSC::parse):
+        * runtime/CodeCache.cpp:
+        (JSC::CodeCache::getGlobalCodeBlock):
+        (JSC::CodeCache::getFunctionExecutableFromGlobalCode):
+        * runtime/CodeCache.h:
+        * runtime/Completion.h:
+        * runtime/Executable.cpp:
+        (JSC::ProgramExecutable::checkSyntax):
+        * runtime/JSGlobalObject.cpp:
+        (JSC::JSGlobalObject::createProgramCodeBlock):
+        (JSC::JSGlobalObject::createEvalCodeBlock):
+
 2015-02-25  Filip Pizlo  <[email protected]>
 
         Need to pass RTLD_DEEPBIND to dlopen() to ensure that our LLVMOverrides take effect on Linux

Modified: trunk/Source/_javascript_Core/builtins/BuiltinExecutables.cpp (180636 => 180637)


--- trunk/Source/_javascript_Core/builtins/BuiltinExecutables.cpp	2015-02-25 21:27:37 UTC (rev 180636)
+++ trunk/Source/_javascript_Core/builtins/BuiltinExecutables.cpp	2015-02-25 22:29:26 UTC (rev 180637)
@@ -49,7 +49,7 @@
     std::unique_ptr<ProgramNode> program = parse<ProgramNode>(&m_vm, source, 0, Identifier(), JSParseBuiltin, JSParseProgramCode, error, &positionBeforeLastNewline);
 
     if (!program) {
-        dataLog("Fatal error compiling builtin function '", name.string(), "': ", error.m_message);
+        dataLog("Fatal error compiling builtin function '", name.string(), "': ", error.message());
         CRASH();
     }
 

Modified: trunk/Source/_javascript_Core/bytecode/UnlinkedCodeBlock.cpp (180636 => 180637)


--- trunk/Source/_javascript_Core/bytecode/UnlinkedCodeBlock.cpp	2015-02-25 21:27:37 UTC (rev 180636)
+++ trunk/Source/_javascript_Core/bytecode/UnlinkedCodeBlock.cpp	2015-02-25 22:29:26 UTC (rev 180637)
@@ -54,8 +54,8 @@
     std::unique_ptr<FunctionNode> function = parse<FunctionNode>(&vm, source, executable->parameters(), executable->name(), executable->toStrictness(), JSParseFunctionCode, error, 0, bodyIncludesBraces);
 
     if (!function) {
-        ASSERT(error.m_type != ParserError::ErrorNone);
-        return 0;
+        ASSERT(error.isValid());
+        return nullptr;
     }
 
     function->finishParsing(executable->parameters(), executable->name(), executable->functionMode());
@@ -64,8 +64,8 @@
     UnlinkedFunctionCodeBlock* result = UnlinkedFunctionCodeBlock::create(&vm, FunctionCode, ExecutableInfo(function->needsActivation(), function->usesEval(), function->isStrictMode(), kind == CodeForConstruct, functionKind == UnlinkedBuiltinFunction));
     auto generator(std::make_unique<BytecodeGenerator>(vm, function.get(), result, debuggerMode, profilerMode));
     error = generator->generate();
-    if (error.m_type != ParserError::ErrorNone)
-        return 0;
+    if (error.isValid())
+        return nullptr;
     return result;
 }
 
@@ -137,11 +137,11 @@
     UnlinkedFunctionExecutable* executable = codeCache->getFunctionExecutableFromGlobalCode(vm, name, source, error);
 
     if (exec->lexicalGlobalObject()->hasDebugger())
-        exec->lexicalGlobalObject()->debugger()->sourceParsed(exec, source.provider(), error.m_line, error.m_message);
+        exec->lexicalGlobalObject()->debugger()->sourceParsed(exec, source.provider(), error.line(), error.message());
 
-    if (error.m_type != ParserError::ErrorNone) {
+    if (error.isValid()) {
         *exception = error.toErrorObject(exec->lexicalGlobalObject(), source);
-        return 0;
+        return nullptr;
     }
 
     return executable;
@@ -162,8 +162,8 @@
 
     UnlinkedFunctionCodeBlock* result = generateFunctionCodeBlock(vm, this, source, specializationKind, debuggerMode, profilerMode, isBuiltinFunction() ? UnlinkedBuiltinFunction : UnlinkedNormalFunction, bodyIncludesBraces, error);
     
-    if (error.m_type != ParserError::ErrorNone)
-        return 0;
+    if (error.isValid())
+        return nullptr;
 
     switch (specializationKind) {
     case CodeForCall:

Modified: trunk/Source/_javascript_Core/bytecode/UnlinkedCodeBlock.h (180636 => 180637)


--- trunk/Source/_javascript_Core/bytecode/UnlinkedCodeBlock.h	2015-02-25 21:27:37 UTC (rev 180636)
+++ trunk/Source/_javascript_Core/bytecode/UnlinkedCodeBlock.h	2015-02-25 22:29:26 UTC (rev 180637)
@@ -50,7 +50,7 @@
 class FunctionExecutable;
 class FunctionParameters;
 class JSScope;
-struct ParserError;
+class ParserError;
 class ScriptExecutable;
 class SourceCode;
 class SourceProvider;

Modified: trunk/Source/_javascript_Core/inspector/agents/InspectorRuntimeAgent.cpp (180636 => 180637)


--- trunk/Source/_javascript_Core/inspector/agents/InspectorRuntimeAgent.cpp	2015-02-25 21:27:37 UTC (rev 180636)
+++ trunk/Source/_javascript_Core/inspector/agents/InspectorRuntimeAgent.cpp	2015-02-25 22:29:26 UTC (rev 180637)
@@ -93,7 +93,7 @@
     ParserError error;
     checkSyntax(vm, JSC::makeSource(_expression_), error);
 
-    switch (error.m_syntaxErrorType) {
+    switch (error.syntaxErrorType()) {
     case ParserError::SyntaxErrorNone:
         *result = Inspector::Protocol::Runtime::SyntaxErrorType::None;
         break;
@@ -108,9 +108,9 @@
         break;
     }
 
-    if (error.m_syntaxErrorType != ParserError::SyntaxErrorNone) {
-        *message = error.m_message;
-        range = buildErrorRangeObject(error.m_token.m_location);
+    if (error.syntaxErrorType() != ParserError::SyntaxErrorNone) {
+        *message = error.message();
+        range = buildErrorRangeObject(error.token().m_location);
     }
 }
 

Modified: trunk/Source/_javascript_Core/jsc.cpp (180636 => 180637)


--- trunk/Source/_javascript_Core/jsc.cpp	2015-02-25 21:27:37 UTC (rev 180636)
+++ trunk/Source/_javascript_Core/jsc.cpp	2015-02-25 22:29:26 UTC (rev 180637)
@@ -1319,10 +1319,10 @@
             if (!line[0])
                 break;
             add_history(line);
-        } while (error.m_syntaxErrorType == ParserError::SyntaxErrorRecoverable);
+        } while (error.syntaxErrorType() == ParserError::SyntaxErrorRecoverable);
         
-        if (error.m_type != ParserError::ErrorNone) {
-            printf("%s:%d\n", error.m_message.utf8().data(), error.m_line);
+        if (error.isValid()) {
+            printf("%s:%d\n", error.message().utf8().data(), error.line());
             continue;
         }
         

Modified: trunk/Source/_javascript_Core/parser/Parser.h (180636 => 180637)


--- trunk/Source/_javascript_Core/parser/Parser.h	2015-02-25 21:27:37 UTC (rev 180636)
+++ trunk/Source/_javascript_Core/parser/Parser.h	2015-02-25 22:29:26 UTC (rev 180637)
@@ -972,7 +972,7 @@
             *positionBeforeLastNewline = parser.positionBeforeLastNewline();
         if (strictness == JSParseBuiltin) {
             if (!result)
-                WTF::dataLog("Error compiling builtin: ", error.m_message, "\n");
+                WTF::dataLog("Error compiling builtin: ", error.message(), "\n");
             RELEASE_ASSERT(result);
             result->setClosedVariables(parser.closedVariables());
         }

Modified: trunk/Source/_javascript_Core/parser/ParserError.h (180636 => 180637)


--- trunk/Source/_javascript_Core/parser/ParserError.h	2015-02-25 21:27:37 UTC (rev 180636)
+++ trunk/Source/_javascript_Core/parser/ParserError.h	2015-02-25 22:29:26 UTC (rev 180637)
@@ -34,7 +34,8 @@
 
 namespace JSC {
 
-struct ParserError {
+class ParserError {
+public:
     enum SyntaxErrorType {
         SyntaxErrorNone,
         SyntaxErrorIrrecoverable,
@@ -50,47 +51,45 @@
         SyntaxError
     };
 
-    ErrorType m_type;
-    SyntaxErrorType m_syntaxErrorType;
-    JSToken m_token;
-    String m_message;
-    int m_line;
     ParserError()
         : m_type(ErrorNone)
         , m_syntaxErrorType(SyntaxErrorNone)
-        , m_line(-1)
     {
     }
     
     explicit ParserError(ErrorType type)
         : m_type(type)
         , m_syntaxErrorType(SyntaxErrorNone)
-        , m_line(-1)
     {
     }
 
     ParserError(ErrorType type, SyntaxErrorType syntaxError, JSToken token)
-        : m_type(type)
+        : m_token(token)
+        , m_type(type)
         , m_syntaxErrorType(syntaxError)
-        , m_token(token)
-        , m_line(-1)
     {
     }
 
     ParserError(ErrorType type, SyntaxErrorType syntaxError, JSToken token, String msg, int line)
-        : m_type(type)
-        , m_syntaxErrorType(syntaxError)
-        , m_token(token)
+        : m_token(token)
         , m_message(msg)
         , m_line(line)
+        , m_type(type)
+        , m_syntaxErrorType(syntaxError)
     {
     }
 
+    bool isValid() const { return m_type != ErrorNone; }
+    SyntaxErrorType syntaxErrorType() const { return m_syntaxErrorType; }
+    const JSToken& token() const { return m_token; }
+    const String& message() const { return m_message; }
+    int line() const { return m_line; }
+
     JSObject* toErrorObject(JSGlobalObject* globalObject, const SourceCode& source)
     {
         switch (m_type) {
         case ErrorNone:
-            return 0;
+            return nullptr;
         case SyntaxError:
             return addErrorInfo(globalObject->globalExec(), createSyntaxError(globalObject, m_message), m_line, source);
         case EvalError:
@@ -103,9 +102,15 @@
             return createOutOfMemoryError(globalObject);
         }
         CRASH();
-        return createOutOfMemoryError(globalObject); // Appease Qt bot
+        return nullptr;
     }
-#undef GET_ERROR_CODE
+
+private:
+    JSToken m_token;
+    String m_message;
+    int m_line { -1 };
+    ErrorType m_type;
+    SyntaxErrorType m_syntaxErrorType;
 };
 
 } // namespace JSC

Modified: trunk/Source/_javascript_Core/runtime/CodeCache.cpp (180636 => 180637)


--- trunk/Source/_javascript_Core/runtime/CodeCache.cpp	2015-02-25 21:27:37 UTC (rev 180636)
+++ trunk/Source/_javascript_Core/runtime/CodeCache.cpp	2015-02-25 22:29:26 UTC (rev 180637)
@@ -109,9 +109,9 @@
 
     auto generator = std::make_unique<BytecodeGenerator>(vm, rootNode.get(), unlinkedCodeBlock, debuggerMode, profilerMode);
     error = generator->generate();
-    if (error.m_type != ParserError::ErrorNone) {
+    if (error.isValid()) {
         m_sourceCode.remove(addResult.iterator);
-        return 0;
+        return nullptr;
     }
 
     if (!canCache) {
@@ -143,9 +143,9 @@
     JSTextPosition positionBeforeLastNewline;
     std::unique_ptr<ProgramNode> program = parse<ProgramNode>(&vm, source, 0, Identifier(), JSParseNormal, JSParseProgramCode, error, &positionBeforeLastNewline);
     if (!program) {
-        RELEASE_ASSERT(error.m_type != ParserError::ErrorNone);
+        RELEASE_ASSERT(error.isValid());
         m_sourceCode.remove(addResult.iterator);
-        return 0;
+        return nullptr;
     }
 
     // This function assumes an input string that would result in a single anonymous function _expression_.

Modified: trunk/Source/_javascript_Core/runtime/CodeCache.h (180636 => 180637)


--- trunk/Source/_javascript_Core/runtime/CodeCache.h	2015-02-25 21:27:37 UTC (rev 180636)
+++ trunk/Source/_javascript_Core/runtime/CodeCache.h	2015-02-25 22:29:26 UTC (rev 180637)
@@ -42,6 +42,7 @@
 class FunctionBodyNode;
 class Identifier;
 class JSScope;
+class ParserError;
 class ProgramExecutable;
 class UnlinkedCodeBlock;
 class UnlinkedEvalCodeBlock;
@@ -49,7 +50,6 @@
 class UnlinkedFunctionExecutable;
 class UnlinkedProgramCodeBlock;
 class VM;
-struct ParserError;
 class SourceCode;
 class SourceProvider;
 

Modified: trunk/Source/_javascript_Core/runtime/Completion.h (180636 => 180637)


--- trunk/Source/_javascript_Core/runtime/Completion.h	2015-02-25 21:27:37 UTC (rev 180636)
+++ trunk/Source/_javascript_Core/runtime/Completion.h	2015-02-25 22:29:26 UTC (rev 180637)
@@ -27,9 +27,9 @@
 
 namespace JSC {
     
-struct ParserError;
 class ExecState;
 class JSScope;
+class ParserError;
 class SourceCode;
 class VM;
 

Modified: trunk/Source/_javascript_Core/runtime/Executable.cpp (180636 => 180637)


--- trunk/Source/_javascript_Core/runtime/Executable.cpp	2015-02-25 21:27:37 UTC (rev 180636)
+++ trunk/Source/_javascript_Core/runtime/Executable.cpp	2015-02-25 22:29:26 UTC (rev 180637)
@@ -468,7 +468,7 @@
     std::unique_ptr<ProgramNode> programNode = parse<ProgramNode>(vm, m_source, 0, Identifier(), JSParseNormal, JSParseProgramCode, error);
     if (programNode)
         return 0;
-    ASSERT(error.m_type != ParserError::ErrorNone);
+    ASSERT(error.isValid());
     return error.toErrorObject(lexicalGlobalObject, m_source);
 }
 

Modified: trunk/Source/_javascript_Core/runtime/JSGlobalObject.cpp (180636 => 180637)


--- trunk/Source/_javascript_Core/runtime/JSGlobalObject.cpp	2015-02-25 21:27:37 UTC (rev 180636)
+++ trunk/Source/_javascript_Core/runtime/JSGlobalObject.cpp	2015-02-25 22:29:26 UTC (rev 180637)
@@ -781,11 +781,11 @@
     UnlinkedProgramCodeBlock* unlinkedCodeBlock = vm().codeCache()->getProgramCodeBlock(vm(), executable, executable->source(), strictness, debuggerMode, profilerMode, error);
 
     if (hasDebugger())
-        debugger()->sourceParsed(callFrame, executable->source().provider(), error.m_line, error.m_message);
+        debugger()->sourceParsed(callFrame, executable->source().provider(), error.line(), error.message());
 
-    if (error.m_type != ParserError::ErrorNone) {
+    if (error.isValid()) {
         *exception = error.toErrorObject(this, executable->source());
-        return 0;
+        return nullptr;
     }
     
     return unlinkedCodeBlock;
@@ -800,11 +800,11 @@
     UnlinkedEvalCodeBlock* unlinkedCodeBlock = vm().codeCache()->getEvalCodeBlock(vm(), executable, executable->source(), strictness, debuggerMode, profilerMode, error);
 
     if (hasDebugger())
-        debugger()->sourceParsed(callFrame, executable->source().provider(), error.m_line, error.m_message);
+        debugger()->sourceParsed(callFrame, executable->source().provider(), error.line(), error.message());
 
-    if (error.m_type != ParserError::ErrorNone) {
+    if (error.isValid()) {
         throwVMError(callFrame, error.toErrorObject(this, executable->source()));
-        return 0;
+        return nullptr;
     }
 
     return unlinkedCodeBlock;
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to