Home | History | Annotate | Download | only in runtime
      1 /*
      2  *  Copyright (C) 1999-2001 Harri Porten (porten (at) kde.org)
      3  *  Copyright (C) 2001 Peter Kelly (pmk (at) post.com)
      4  *  Copyright (C) 2003, 2004, 2005, 2006, 2008 Apple Inc. All rights reserved.
      5  *  Copyright (C) 2007 Eric Seidel (eric (at) 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 
     42 JSObject* createError(JSGlobalObject* globalObject, const UString& message)
     43 {
     44     ASSERT(!message.isEmpty());
     45     return ErrorInstance::create(&globalObject->globalData(), globalObject->errorStructure(), message);
     46 }
     47 
     48 JSObject* createEvalError(JSGlobalObject* globalObject, const UString& message)
     49 {
     50     ASSERT(!message.isEmpty());
     51     return ErrorInstance::create(&globalObject->globalData(), globalObject->evalErrorConstructor()->errorStructure(), message);
     52 }
     53 
     54 JSObject* createRangeError(JSGlobalObject* globalObject, const UString& message)
     55 {
     56     ASSERT(!message.isEmpty());
     57     return ErrorInstance::create(&globalObject->globalData(), globalObject->rangeErrorConstructor()->errorStructure(), message);
     58 }
     59 
     60 JSObject* createReferenceError(JSGlobalObject* globalObject, const UString& message)
     61 {
     62     ASSERT(!message.isEmpty());
     63     return ErrorInstance::create(&globalObject->globalData(), globalObject->referenceErrorConstructor()->errorStructure(), message);
     64 }
     65 
     66 JSObject* createSyntaxError(JSGlobalObject* globalObject, const UString& message)
     67 {
     68     ASSERT(!message.isEmpty());
     69     return ErrorInstance::create(&globalObject->globalData(), globalObject->syntaxErrorConstructor()->errorStructure(), message);
     70 }
     71 
     72 JSObject* createTypeError(JSGlobalObject* globalObject, const UString& message)
     73 {
     74     ASSERT(!message.isEmpty());
     75     return ErrorInstance::create(&globalObject->globalData(), globalObject->typeErrorConstructor()->errorStructure(), message);
     76 }
     77 
     78 JSObject* createURIError(JSGlobalObject* globalObject, const UString& message)
     79 {
     80     ASSERT(!message.isEmpty());
     81     return ErrorInstance::create(&globalObject->globalData(), globalObject->URIErrorConstructor()->errorStructure(), message);
     82 }
     83 
     84 JSObject* createError(ExecState* exec, const UString& message)
     85 {
     86     return createError(exec->lexicalGlobalObject(), message);
     87 }
     88 
     89 JSObject* createEvalError(ExecState* exec, const UString& message)
     90 {
     91     return createEvalError(exec->lexicalGlobalObject(), message);
     92 }
     93 
     94 JSObject* createRangeError(ExecState* exec, const UString& message)
     95 {
     96     return createRangeError(exec->lexicalGlobalObject(), message);
     97 }
     98 
     99 JSObject* createReferenceError(ExecState* exec, const UString& message)
    100 {
    101     return createReferenceError(exec->lexicalGlobalObject(), message);
    102 }
    103 
    104 JSObject* createSyntaxError(ExecState* exec, const UString& message)
    105 {
    106     return createSyntaxError(exec->lexicalGlobalObject(), message);
    107 }
    108 
    109 JSObject* createTypeError(ExecState* exec, const UString& message)
    110 {
    111     return createTypeError(exec->lexicalGlobalObject(), message);
    112 }
    113 
    114 JSObject* createURIError(ExecState* exec, const UString& message)
    115 {
    116     return createURIError(exec->lexicalGlobalObject(), message);
    117 }
    118 
    119 JSObject* addErrorInfo(JSGlobalData* globalData, JSObject* error, int line, const SourceCode& source)
    120 {
    121     intptr_t sourceID = source.provider()->asID();
    122     const UString& sourceURL = source.provider()->url();
    123 
    124     if (line != -1)
    125         error->putWithAttributes(globalData, Identifier(globalData, linePropertyName), jsNumber(line), ReadOnly | DontDelete);
    126     if (sourceID != -1)
    127         error->putWithAttributes(globalData, Identifier(globalData, sourceIdPropertyName), jsNumber((double)sourceID), ReadOnly | DontDelete);
    128     if (!sourceURL.isNull())
    129         error->putWithAttributes(globalData, Identifier(globalData, sourceURLPropertyName), jsString(globalData, sourceURL), ReadOnly | DontDelete);
    130 
    131     return error;
    132 }
    133 
    134 JSObject* addErrorInfo(ExecState* exec, JSObject* error, int line, const SourceCode& source)
    135 {
    136     return addErrorInfo(&exec->globalData(), error, line, source);
    137 }
    138 
    139 bool hasErrorInfo(ExecState* exec, JSObject* error)
    140 {
    141     return error->hasProperty(exec, Identifier(exec, linePropertyName))
    142         || error->hasProperty(exec, Identifier(exec, sourceIdPropertyName))
    143         || error->hasProperty(exec, Identifier(exec, sourceURLPropertyName));
    144 }
    145 
    146 JSValue throwError(ExecState* exec, JSValue error)
    147 {
    148     exec->globalData().exception = error;
    149     return error;
    150 }
    151 
    152 JSObject* throwError(ExecState* exec, JSObject* error)
    153 {
    154     exec->globalData().exception = error;
    155     return error;
    156 }
    157 
    158 JSObject* throwTypeError(ExecState* exec)
    159 {
    160     return throwError(exec, createTypeError(exec, "Type error"));
    161 }
    162 
    163 JSObject* throwSyntaxError(ExecState* exec)
    164 {
    165     return throwError(exec, createSyntaxError(exec, "Syntax error"));
    166 }
    167 
    168 class StrictModeTypeErrorFunction : public InternalFunction {
    169 public:
    170     StrictModeTypeErrorFunction(ExecState* exec, JSGlobalObject* globalObject, Structure* structure, const UString& message)
    171         : InternalFunction(&exec->globalData(), globalObject, structure, exec->globalData().propertyNames->emptyIdentifier)
    172         , m_message(message)
    173     {
    174     }
    175 
    176     static EncodedJSValue JSC_HOST_CALL constructThrowTypeError(ExecState* exec)
    177     {
    178         throwTypeError(exec, static_cast<StrictModeTypeErrorFunction*>(exec->callee())->m_message);
    179         return JSValue::encode(jsNull());
    180     }
    181 
    182     ConstructType getConstructData(ConstructData& constructData)
    183     {
    184         constructData.native.function = constructThrowTypeError;
    185         return ConstructTypeHost;
    186     }
    187 
    188     static EncodedJSValue JSC_HOST_CALL callThrowTypeError(ExecState* exec)
    189     {
    190         throwTypeError(exec, static_cast<StrictModeTypeErrorFunction*>(exec->callee())->m_message);
    191         return JSValue::encode(jsNull());
    192     }
    193 
    194     CallType getCallData(CallData& callData)
    195     {
    196         callData.native.function = callThrowTypeError;
    197         return CallTypeHost;
    198     }
    199 
    200 private:
    201     UString m_message;
    202 };
    203 
    204 ASSERT_CLASS_FITS_IN_CELL(StrictModeTypeErrorFunction);
    205 
    206 JSValue createTypeErrorFunction(ExecState* exec, const UString& message)
    207 {
    208     return new (exec) StrictModeTypeErrorFunction(exec, exec->lexicalGlobalObject(), exec->lexicalGlobalObject()->internalFunctionStructure(), message);
    209 }
    210 
    211 } // namespace JSC
    212