source: webkit/trunk/JavaScriptCore/VM/ExceptionHelpers.cpp@ 34372

Last change on this file since 34372 was 34372, checked in by [email protected], 17 years ago

2008-06-04 Sam Weinig <[email protected]>

Reviewed by Maciej Stachowiak.

Big cleanup of formatting and whitespace.

File size: 4.0 KB
Line 
1/*
2 * Copyright (C) 2008 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
29#include "config.h"
30#include "ExceptionHelpers.h"
31
32#include "ExecState.h"
33#include "nodes.h"
34#include "object.h"
35
36namespace KJS {
37
38static void substitute(UString& string, const UString& substring)
39{
40 int position = string.find("%s");
41 ASSERT(position != -1);
42 UString newString = string.substr(0, position);
43 newString.append(substring);
44 newString.append(string.substr(position + 2));
45 string = newString;
46}
47
48JSValue* createError(ExecState* exec, ErrorType e, const char* msg)
49{
50 return Error::create(exec, e, msg, -1, -1, 0);
51}
52
53JSValue* createError(ExecState* exec, ErrorType e, const char* msg, const Identifier& label)
54{
55 UString message = msg;
56 substitute(message, label.ustring());
57 return Error::create(exec, e, message, -1, -1, 0);
58}
59
60JSValue* createError(ExecState* exec, ErrorType e, const char* msg, JSValue* v, Node* expr)
61{
62 UString message = msg;
63 substitute(message, v->toString(exec));
64 if (expr)
65 substitute(message, expr->toString());
66 return Error::create(exec, e, message, -1, -1, 0);
67}
68
69JSValue* createError(ExecState* exec, ErrorType e, const char* msg, JSValue* v)
70{
71 UString message = msg;
72 substitute(message, v->toString(exec));
73 return Error::create(exec, e, message, -1, -1, 0);
74}
75
76JSValue* createStackOverflowError(ExecState* exec)
77{
78 return createError(exec, RangeError, "Maximum call stack size exceeded.");
79}
80
81JSValue* createUndefinedVariableError(ExecState* exec, const Identifier& ident)
82{
83 return createError(exec, ReferenceError, "Can't find variable: %s", ident);
84}
85
86JSValue* createInvalidParamError(ExecState* exec, const char* op, JSValue* v)
87{
88 UString message = "'%s' is not a valid argument for '%s'";
89 substitute(message, v->toString(exec));
90 substitute(message, op);
91 return Error::create(exec, TypeError, message, -1, -1, 0);
92}
93
94JSValue* createNotAConstructorError(ExecState* exec, JSValue* value, Node* expr)
95{
96 if (expr)
97 return createError(exec, TypeError, "Value %s (result of expression %s) is not a constructor. Cannot be used with new.", value, expr);
98 return createError(exec, TypeError, "Value %s is not a constructor. Cannot be used with new.", value);
99}
100
101JSValue* createNotAFunctionError(ExecState* exec, JSValue* value, Node* expr)
102{
103 if (expr)
104 return createError(exec, TypeError, "Value %s (result of expression %s) does not allow function calls.", value, expr);
105 return createError(exec, TypeError, "Value %s does not allow function calls.", value);
106}
107
108} // namespace KJS
Note: See TracBrowser for help on using the repository browser.