JavaScriptCore/runtime/Error.cpp
changeset 0 4f2f89ce4247
equal deleted inserted replaced
-1:000000000000 0:4f2f89ce4247
       
     1 /*
       
     2  *  Copyright (C) 1999-2001 Harri Porten (porten@kde.org)
       
     3  *  Copyright (C) 2001 Peter Kelly (pmk@post.com)
       
     4  *  Copyright (C) 2003, 2004, 2005, 2006, 2008 Apple Inc. All rights reserved.
       
     5  *  Copyright (C) 2007 Eric Seidel (eric@webkit.org)
       
     6  *
       
     7  *  This library is free software; you can redistribute it and/or
       
     8  *  modify it under the terms of the GNU Library General Public
       
     9  *  License as published by the Free Software Foundation; either
       
    10  *  version 2 of the License, or (at your option) any later version.
       
    11  *
       
    12  *  This library is distributed in the hope that it will be useful,
       
    13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
       
    15  *  Library General Public License for more details.
       
    16  *
       
    17  *  You should have received a copy of the GNU Library General Public License
       
    18  *  along with this library; see the file COPYING.LIB.  If not, write to
       
    19  *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
       
    20  *  Boston, MA 02110-1301, USA.
       
    21  *
       
    22  */
       
    23 
       
    24 #include "config.h"
       
    25 #include "Error.h"
       
    26 
       
    27 #include "ConstructData.h"
       
    28 #include "ErrorConstructor.h"
       
    29 #include "JSFunction.h"
       
    30 #include "JSGlobalObject.h"
       
    31 #include "JSObject.h"
       
    32 #include "JSString.h"
       
    33 #include "NativeErrorConstructor.h"
       
    34 #include "SourceCode.h"
       
    35 
       
    36 namespace JSC {
       
    37 
       
    38 static const char* linePropertyName = "line";
       
    39 static const char* sourceIdPropertyName = "sourceId";
       
    40 static const char* sourceURLPropertyName = "sourceURL";
       
    41 static const char* expressionBeginOffsetPropertyName = "expressionBeginOffset";
       
    42 static const char* expressionCaretOffsetPropertyName = "expressionCaretOffset";
       
    43 static const char* expressionEndOffsetPropertyName = "expressionEndOffset";
       
    44 
       
    45 JSObject* createError(JSGlobalObject* globalObject, const UString& message)
       
    46 {
       
    47     ASSERT(!message.isEmpty());
       
    48     return ErrorInstance::create(globalObject->globalData(), globalObject->errorStructure(), message);
       
    49 }
       
    50 
       
    51 JSObject* createEvalError(JSGlobalObject* globalObject, const UString& message)
       
    52 {
       
    53     ASSERT(!message.isEmpty());
       
    54     return ErrorInstance::create(globalObject->globalData(), globalObject->evalErrorConstructor()->errorStructure(), message);
       
    55 }
       
    56 
       
    57 JSObject* createRangeError(JSGlobalObject* globalObject, const UString& message)
       
    58 {
       
    59     ASSERT(!message.isEmpty());
       
    60     return ErrorInstance::create(globalObject->globalData(), globalObject->rangeErrorConstructor()->errorStructure(), message);
       
    61 }
       
    62 
       
    63 JSObject* createReferenceError(JSGlobalObject* globalObject, const UString& message)
       
    64 {
       
    65     ASSERT(!message.isEmpty());
       
    66     return ErrorInstance::create(globalObject->globalData(), globalObject->referenceErrorConstructor()->errorStructure(), message);
       
    67 }
       
    68 
       
    69 JSObject* createSyntaxError(JSGlobalObject* globalObject, const UString& message)
       
    70 {
       
    71     ASSERT(!message.isEmpty());
       
    72     return ErrorInstance::create(globalObject->globalData(), globalObject->syntaxErrorConstructor()->errorStructure(), message);
       
    73 }
       
    74 
       
    75 JSObject* createTypeError(JSGlobalObject* globalObject, const UString& message)
       
    76 {
       
    77     ASSERT(!message.isEmpty());
       
    78     return ErrorInstance::create(globalObject->globalData(), globalObject->typeErrorConstructor()->errorStructure(), message);
       
    79 }
       
    80 
       
    81 JSObject* createURIError(JSGlobalObject* globalObject, const UString& message)
       
    82 {
       
    83     ASSERT(!message.isEmpty());
       
    84     return ErrorInstance::create(globalObject->globalData(), globalObject->URIErrorConstructor()->errorStructure(), message);
       
    85 }
       
    86 
       
    87 JSObject* createError(ExecState* exec, const UString& message)
       
    88 {
       
    89     return createError(exec->lexicalGlobalObject(), message);
       
    90 }
       
    91 
       
    92 JSObject* createEvalError(ExecState* exec, const UString& message)
       
    93 {
       
    94     return createEvalError(exec->lexicalGlobalObject(), message);
       
    95 }
       
    96 
       
    97 JSObject* createRangeError(ExecState* exec, const UString& message)
       
    98 {
       
    99     return createRangeError(exec->lexicalGlobalObject(), message);
       
   100 }
       
   101 
       
   102 JSObject* createReferenceError(ExecState* exec, const UString& message)
       
   103 {
       
   104     return createReferenceError(exec->lexicalGlobalObject(), message);
       
   105 }
       
   106 
       
   107 JSObject* createSyntaxError(ExecState* exec, const UString& message)
       
   108 {
       
   109     return createSyntaxError(exec->lexicalGlobalObject(), message);
       
   110 }
       
   111 
       
   112 JSObject* createTypeError(ExecState* exec, const UString& message)
       
   113 {
       
   114     return createTypeError(exec->lexicalGlobalObject(), message);
       
   115 }
       
   116 
       
   117 JSObject* createURIError(ExecState* exec, const UString& message)
       
   118 {
       
   119     return createURIError(exec->lexicalGlobalObject(), message);
       
   120 }
       
   121 
       
   122 static void addErrorSourceInfo(JSGlobalData* globalData, JSObject* error, int line, const SourceCode& source)
       
   123 {
       
   124     intptr_t sourceID = source.provider()->asID();
       
   125     const UString& sourceURL = source.provider()->url();
       
   126 
       
   127     if (line != -1)
       
   128         error->putWithAttributes(globalData, Identifier(globalData, linePropertyName), jsNumber(globalData, line), ReadOnly | DontDelete);
       
   129     if (sourceID != -1)
       
   130         error->putWithAttributes(globalData, Identifier(globalData, sourceIdPropertyName), jsNumber(globalData, (double)sourceID), ReadOnly | DontDelete);
       
   131     if (!sourceURL.isNull())
       
   132         error->putWithAttributes(globalData, Identifier(globalData, sourceURLPropertyName), jsString(globalData, sourceURL), ReadOnly | DontDelete);
       
   133 }
       
   134 
       
   135 static void addErrorDivotInfo(JSGlobalData* globalData, JSObject* error, int divotPoint, int startOffset, int endOffset, bool withCaret)
       
   136 {
       
   137     error->putWithAttributes(globalData, Identifier(globalData, expressionBeginOffsetPropertyName), jsNumber(globalData, divotPoint - startOffset), ReadOnly | DontDelete);
       
   138     error->putWithAttributes(globalData, Identifier(globalData, expressionEndOffsetPropertyName), jsNumber(globalData, divotPoint + endOffset), ReadOnly | DontDelete);
       
   139     if (withCaret)
       
   140         error->putWithAttributes(globalData, Identifier(globalData, expressionCaretOffsetPropertyName), jsNumber(globalData, divotPoint), ReadOnly | DontDelete);
       
   141 }
       
   142 
       
   143 JSObject* addErrorInfo(JSGlobalData* globalData, JSObject* error, int line, const SourceCode& source)
       
   144 {
       
   145     addErrorSourceInfo(globalData, error, line, source);
       
   146     return error;
       
   147 }
       
   148 
       
   149 JSObject* addErrorInfo(JSGlobalData* globalData, JSObject* error, int line, const SourceCode& source, int divotPoint, int startOffset, int endOffset, bool withCaret)
       
   150 {
       
   151     addErrorSourceInfo(globalData, error, line, source);
       
   152     addErrorDivotInfo(globalData, error, divotPoint, startOffset, endOffset, withCaret);
       
   153     return error;
       
   154 }
       
   155 
       
   156 JSObject* addErrorInfo(ExecState* exec, JSObject* error, int line, const SourceCode& source)
       
   157 {
       
   158     return addErrorInfo(&exec->globalData(), error, line, source);
       
   159 }
       
   160 
       
   161 JSObject* addErrorInfo(ExecState* exec, JSObject* error, int line, const SourceCode& source, int divotPoint, int startOffset, int endOffset, bool withCaret)
       
   162 {
       
   163     return addErrorInfo(&exec->globalData(), error, line, source, divotPoint, startOffset, endOffset, withCaret);
       
   164 }
       
   165 
       
   166 bool hasErrorInfo(ExecState* exec, JSObject* error)
       
   167 {
       
   168     return error->hasProperty(exec, Identifier(exec, linePropertyName))
       
   169         || error->hasProperty(exec, Identifier(exec, sourceIdPropertyName))
       
   170         || error->hasProperty(exec, Identifier(exec, sourceURLPropertyName))
       
   171         || error->hasProperty(exec, Identifier(exec, expressionBeginOffsetPropertyName))
       
   172         || error->hasProperty(exec, Identifier(exec, expressionCaretOffsetPropertyName))
       
   173         || error->hasProperty(exec, Identifier(exec, expressionEndOffsetPropertyName));
       
   174 }
       
   175 
       
   176 JSValue throwError(ExecState* exec, JSValue error)
       
   177 {
       
   178     exec->globalData().exception = error;
       
   179     return error;
       
   180 }
       
   181 
       
   182 JSObject* throwError(ExecState* exec, JSObject* error)
       
   183 {
       
   184     exec->globalData().exception = error;
       
   185     return error;
       
   186 }
       
   187 
       
   188 JSObject* throwTypeError(ExecState* exec)
       
   189 {
       
   190     return throwError(exec, createTypeError(exec, "Type error"));
       
   191 }
       
   192 
       
   193 JSObject* throwSyntaxError(ExecState* exec)
       
   194 {
       
   195     return throwError(exec, createSyntaxError(exec, "Syntax error"));
       
   196 }
       
   197 
       
   198 } // namespace JSC