Error.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. #include "config.h"
  24. #include "Error.h"
  25. #include "ConstructData.h"
  26. #include "ErrorConstructor.h"
  27. #include "ExceptionHelpers.h"
  28. #include "FunctionPrototype.h"
  29. #include "JSArray.h"
  30. #include "JSFunction.h"
  31. #include "JSGlobalObject.h"
  32. #include "JSObject.h"
  33. #include "JSString.h"
  34. #include "NativeErrorConstructor.h"
  35. #include "Operations.h"
  36. #include "SourceCode.h"
  37. #include <wtf/text/StringBuilder.h>
  38. namespace JSC {
  39. static const char* linePropertyName = "line";
  40. static const char* sourceURLPropertyName = "sourceURL";
  41. JSObject* createError(JSGlobalObject* globalObject, const String& message)
  42. {
  43. ASSERT(!message.isEmpty());
  44. return ErrorInstance::create(globalObject->vm(), globalObject->errorStructure(), message);
  45. }
  46. JSObject* createEvalError(JSGlobalObject* globalObject, const String& message)
  47. {
  48. ASSERT(!message.isEmpty());
  49. return ErrorInstance::create(globalObject->vm(), globalObject->evalErrorConstructor()->errorStructure(), message);
  50. }
  51. JSObject* createRangeError(JSGlobalObject* globalObject, const String& message)
  52. {
  53. ASSERT(!message.isEmpty());
  54. return ErrorInstance::create(globalObject->vm(), globalObject->rangeErrorConstructor()->errorStructure(), message);
  55. }
  56. JSObject* createReferenceError(JSGlobalObject* globalObject, const String& message)
  57. {
  58. ASSERT(!message.isEmpty());
  59. return ErrorInstance::create(globalObject->vm(), globalObject->referenceErrorConstructor()->errorStructure(), message);
  60. }
  61. JSObject* createSyntaxError(JSGlobalObject* globalObject, const String& message)
  62. {
  63. ASSERT(!message.isEmpty());
  64. return ErrorInstance::create(globalObject->vm(), globalObject->syntaxErrorConstructor()->errorStructure(), message);
  65. }
  66. JSObject* createTypeError(JSGlobalObject* globalObject, const String& message)
  67. {
  68. ASSERT(!message.isEmpty());
  69. return ErrorInstance::create(globalObject->vm(), globalObject->typeErrorConstructor()->errorStructure(), message);
  70. }
  71. JSObject* createNotEnoughArgumentsError(JSGlobalObject* globalObject)
  72. {
  73. return createTypeError(globalObject, ASCIILiteral("Not enough arguments"));
  74. }
  75. JSObject* createURIError(JSGlobalObject* globalObject, const String& message)
  76. {
  77. ASSERT(!message.isEmpty());
  78. return ErrorInstance::create(globalObject->vm(), globalObject->URIErrorConstructor()->errorStructure(), message);
  79. }
  80. JSObject* createError(ExecState* exec, const String& message)
  81. {
  82. return createError(exec->lexicalGlobalObject(), message);
  83. }
  84. JSObject* createEvalError(ExecState* exec, const String& message)
  85. {
  86. return createEvalError(exec->lexicalGlobalObject(), message);
  87. }
  88. JSObject* createRangeError(ExecState* exec, const String& message)
  89. {
  90. return createRangeError(exec->lexicalGlobalObject(), message);
  91. }
  92. JSObject* createReferenceError(ExecState* exec, const String& message)
  93. {
  94. return createReferenceError(exec->lexicalGlobalObject(), message);
  95. }
  96. JSObject* createSyntaxError(ExecState* exec, const String& message)
  97. {
  98. return createSyntaxError(exec->lexicalGlobalObject(), message);
  99. }
  100. JSObject* createTypeError(ExecState* exec, const String& message)
  101. {
  102. return createTypeError(exec->lexicalGlobalObject(), message);
  103. }
  104. JSObject* createNotEnoughArgumentsError(ExecState* exec)
  105. {
  106. return createNotEnoughArgumentsError(exec->lexicalGlobalObject());
  107. }
  108. JSObject* createURIError(ExecState* exec, const String& message)
  109. {
  110. return createURIError(exec->lexicalGlobalObject(), message);
  111. }
  112. JSObject* addErrorInfo(CallFrame* callFrame, JSObject* error, int line, const SourceCode& source)
  113. {
  114. VM* vm = &callFrame->vm();
  115. const String& sourceURL = source.provider()->url();
  116. if (line != -1)
  117. error->putDirect(*vm, Identifier(vm, linePropertyName), jsNumber(line), ReadOnly | DontDelete);
  118. if (!sourceURL.isNull())
  119. error->putDirect(*vm, Identifier(vm, sourceURLPropertyName), jsString(vm, sourceURL), ReadOnly | DontDelete);
  120. vm->interpreter->addStackTraceIfNecessary(callFrame, error);
  121. return error;
  122. }
  123. bool hasErrorInfo(ExecState* exec, JSObject* error)
  124. {
  125. return error->hasProperty(exec, Identifier(exec, linePropertyName))
  126. || error->hasProperty(exec, Identifier(exec, sourceURLPropertyName));
  127. }
  128. JSValue throwError(ExecState* exec, JSValue error)
  129. {
  130. Interpreter::addStackTraceIfNecessary(exec, error);
  131. exec->vm().exception = error;
  132. return error;
  133. }
  134. JSObject* throwError(ExecState* exec, JSObject* error)
  135. {
  136. Interpreter::addStackTraceIfNecessary(exec, error);
  137. exec->vm().exception = error;
  138. return error;
  139. }
  140. JSObject* throwTypeError(ExecState* exec)
  141. {
  142. return throwError(exec, createTypeError(exec, ASCIILiteral("Type error")));
  143. }
  144. JSObject* throwSyntaxError(ExecState* exec)
  145. {
  146. return throwError(exec, createSyntaxError(exec, ASCIILiteral("Syntax error")));
  147. }
  148. const ClassInfo StrictModeTypeErrorFunction::s_info = { "Function", &Base::s_info, 0, 0, CREATE_METHOD_TABLE(StrictModeTypeErrorFunction) };
  149. void StrictModeTypeErrorFunction::destroy(JSCell* cell)
  150. {
  151. static_cast<StrictModeTypeErrorFunction*>(cell)->StrictModeTypeErrorFunction::~StrictModeTypeErrorFunction();
  152. }
  153. } // namespace JSC