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 |
|
---|
36 | namespace KJS {
|
---|
37 |
|
---|
38 | static 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 |
|
---|
48 | JSValue* createError(ExecState* exec, ErrorType e, const char* msg)
|
---|
49 | {
|
---|
50 | return Error::create(exec, e, msg, -1, -1, 0); // lineNo(), currentSourceId(exec), currentSourceURL(exec)
|
---|
51 | }
|
---|
52 |
|
---|
53 | JSValue* 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); // lineNo(), currentSourceId(exec), currentSourceURL(exec)
|
---|
58 | }
|
---|
59 |
|
---|
60 | JSValue* 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 | else
|
---|
67 | substitute(message, "<<no string for expression>>");
|
---|
68 | return Error::create(exec, e, message, -1, -1, 0); //, lineNo(), currentSourceId(exec), currentSourceURL(exec));
|
---|
69 | }
|
---|
70 |
|
---|
71 | JSValue* createStackOverflowError(ExecState* exec)
|
---|
72 | {
|
---|
73 | return createError(exec, RangeError, "Maximum call stack size exceeded.");
|
---|
74 | }
|
---|
75 |
|
---|
76 | JSValue* createUndefinedVariableError(ExecState* exec, const Identifier& ident)
|
---|
77 | {
|
---|
78 | return createError(exec, ReferenceError, "Can't find variable: %s", ident);
|
---|
79 | }
|
---|
80 |
|
---|
81 | JSValue* createNotAnObjectError(ExecState* exec, JSValue* value, Node* expr)
|
---|
82 | {
|
---|
83 | return createError(exec, TypeError, "Value %s (result of expression %s) is not an object.", value, expr);
|
---|
84 | }
|
---|
85 |
|
---|
86 | JSValue* createNotAConstructorError(ExecState* exec, JSValue* value, Node* expr)
|
---|
87 | {
|
---|
88 | if (!value->isObject())
|
---|
89 | return createNotAnObjectError(exec, value, expr);
|
---|
90 | return createError(exec, TypeError, "Value %s (result of expression %s) is not a constructor. Cannot be used with new.", value, expr);
|
---|
91 | }
|
---|
92 |
|
---|
93 | JSValue* createNotAFunctionError(ExecState* exec, JSValue* value, Node* expr)
|
---|
94 | {
|
---|
95 | if (!value->isObject())
|
---|
96 | return createNotAnObjectError(exec, value, expr);
|
---|
97 | return createError(exec, TypeError, "Value %s (result of expression %s) does not allow function calls.", value, expr);
|
---|
98 | }
|
---|
99 |
|
---|
100 | }
|
---|