ExceptionHelpers.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * Copyright (C) 2008, 2009 Apple Inc. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
  14. * its contributors may be used to endorse or promote products derived
  15. * from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
  18. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  19. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  20. * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
  21. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  22. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  23. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  24. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  26. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. #include "config.h"
  29. #include "ExceptionHelpers.h"
  30. #include "CodeBlock.h"
  31. #include "CallFrame.h"
  32. #include "ErrorInstance.h"
  33. #include "JSGlobalObjectFunctions.h"
  34. #include "JSObject.h"
  35. #include "JSNotAnObject.h"
  36. #include "Interpreter.h"
  37. #include "Nodes.h"
  38. #include "Operations.h"
  39. namespace JSC {
  40. ASSERT_HAS_TRIVIAL_DESTRUCTOR(TerminatedExecutionError);
  41. const ClassInfo TerminatedExecutionError::s_info = { "TerminatedExecutionError", &Base::s_info, 0, 0, CREATE_METHOD_TABLE(TerminatedExecutionError) };
  42. JSValue TerminatedExecutionError::defaultValue(const JSObject*, ExecState* exec, PreferredPrimitiveType hint)
  43. {
  44. if (hint == PreferString)
  45. return jsNontrivialString(exec, String(ASCIILiteral("JavaScript execution terminated.")));
  46. return JSValue(QNaN);
  47. }
  48. JSObject* createTerminatedExecutionException(VM* vm)
  49. {
  50. return TerminatedExecutionError::create(*vm);
  51. }
  52. bool isTerminatedExecutionException(JSObject* object)
  53. {
  54. return object->inherits(&TerminatedExecutionError::s_info);
  55. }
  56. bool isTerminatedExecutionException(JSValue value)
  57. {
  58. return value.inherits(&TerminatedExecutionError::s_info);
  59. }
  60. JSObject* createStackOverflowError(ExecState* exec)
  61. {
  62. return createRangeError(exec, ASCIILiteral("Maximum call stack size exceeded."));
  63. }
  64. JSObject* createStackOverflowError(JSGlobalObject* globalObject)
  65. {
  66. return createRangeError(globalObject, ASCIILiteral("Maximum call stack size exceeded."));
  67. }
  68. JSObject* createUndefinedVariableError(ExecState* exec, const Identifier& ident)
  69. {
  70. String message(makeString("Can't find variable: ", ident.string()));
  71. return createReferenceError(exec, message);
  72. }
  73. JSObject* createInvalidParamError(ExecState* exec, const char* op, JSValue value)
  74. {
  75. String errorMessage = makeString("'", value.toString(exec)->value(exec), "' is not a valid argument for '", op, "'");
  76. JSObject* exception = createTypeError(exec, errorMessage);
  77. ASSERT(exception->isErrorInstance());
  78. static_cast<ErrorInstance*>(exception)->setAppendSourceToMessage();
  79. return exception;
  80. }
  81. JSObject* createNotAConstructorError(ExecState* exec, JSValue value)
  82. {
  83. String errorMessage = makeString("'", value.toString(exec)->value(exec), "' is not a constructor");
  84. JSObject* exception = createTypeError(exec, errorMessage);
  85. ASSERT(exception->isErrorInstance());
  86. static_cast<ErrorInstance*>(exception)->setAppendSourceToMessage();
  87. return exception;
  88. }
  89. JSObject* createNotAFunctionError(ExecState* exec, JSValue value)
  90. {
  91. String errorMessage = makeString("'", value.toString(exec)->value(exec), "' is not a function");
  92. JSObject* exception = createTypeError(exec, errorMessage);
  93. ASSERT(exception->isErrorInstance());
  94. static_cast<ErrorInstance*>(exception)->setAppendSourceToMessage();
  95. return exception;
  96. }
  97. JSObject* createNotAnObjectError(ExecState* exec, JSValue value)
  98. {
  99. String errorMessage = makeString("'", value.toString(exec)->value(exec), "' is not an object");
  100. JSObject* exception = createTypeError(exec, errorMessage);
  101. ASSERT(exception->isErrorInstance());
  102. static_cast<ErrorInstance*>(exception)->setAppendSourceToMessage();
  103. return exception;
  104. }
  105. JSObject* createErrorForInvalidGlobalAssignment(ExecState* exec, const String& propertyName)
  106. {
  107. return createReferenceError(exec, makeString("Strict mode forbids implicit creation of global property '", propertyName, "'"));
  108. }
  109. JSObject* createOutOfMemoryError(JSGlobalObject* globalObject)
  110. {
  111. return createError(globalObject, ASCIILiteral("Out of memory"));
  112. }
  113. JSObject* throwOutOfMemoryError(ExecState* exec)
  114. {
  115. return throwError(exec, createOutOfMemoryError(exec->lexicalGlobalObject()));
  116. }
  117. JSObject* throwStackOverflowError(ExecState* exec)
  118. {
  119. Interpreter::ErrorHandlingMode mode(exec);
  120. return throwError(exec, createStackOverflowError(exec));
  121. }
  122. JSObject* throwTerminatedExecutionException(ExecState* exec)
  123. {
  124. Interpreter::ErrorHandlingMode mode(exec);
  125. return throwError(exec, createTerminatedExecutionException(&exec->vm()));
  126. }
  127. } // namespace JSC