• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2009 Google 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  * 1.  Redistributions of source code must retain the above copyright
8  *     notice, this list of conditions and the following disclaimer.
9  * 2.  Redistributions in binary form must reproduce the above copyright
10  *     notice, this list of conditions and the following disclaimer in the
11  *     documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16  * DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY
17  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23  */
24 
25 #include "config.h"
26 #include "bindings/core/v8/V8ThrowException.h"
27 
28 #include "bindings/core/v8/V8Binding.h"
29 #include "bindings/core/v8/V8DOMException.h"
30 #include "core/dom/DOMException.h"
31 #include "core/dom/ExceptionCode.h"
32 
33 namespace blink {
34 
domExceptionStackGetter(v8::Local<v8::String> name,const v8::PropertyCallbackInfo<v8::Value> & info)35 static void domExceptionStackGetter(v8::Local<v8::String> name, const v8::PropertyCallbackInfo<v8::Value>& info)
36 {
37     ASSERT(info.Data()->IsObject());
38     v8SetReturnValue(info, info.Data()->ToObject()->Get(v8AtomicString(info.GetIsolate(), "stack")));
39 }
40 
domExceptionStackSetter(v8::Local<v8::String> name,v8::Local<v8::Value> value,const v8::PropertyCallbackInfo<void> & info)41 static void domExceptionStackSetter(v8::Local<v8::String> name, v8::Local<v8::Value> value, const v8::PropertyCallbackInfo<void>& info)
42 {
43     ASSERT(info.Data()->IsObject());
44     info.Data()->ToObject()->Set(v8AtomicString(info.GetIsolate(), "stack"), value);
45 }
46 
createDOMException(int ec,const String & sanitizedMessage,const String & unsanitizedMessage,const v8::Handle<v8::Object> & creationContext,v8::Isolate * isolate)47 v8::Handle<v8::Value> V8ThrowException::createDOMException(int ec, const String& sanitizedMessage, const String& unsanitizedMessage, const v8::Handle<v8::Object>& creationContext, v8::Isolate* isolate)
48 {
49     if (ec <= 0 || v8::V8::IsExecutionTerminating())
50         return v8Undefined();
51 
52     ASSERT(ec == SecurityError || unsanitizedMessage.isEmpty());
53 
54     if (ec == V8GeneralError)
55         return V8ThrowException::createGeneralError(sanitizedMessage, isolate);
56     if (ec == V8TypeError)
57         return V8ThrowException::createTypeError(sanitizedMessage, isolate);
58     if (ec == V8RangeError)
59         return V8ThrowException::createRangeError(sanitizedMessage, isolate);
60     if (ec == V8SyntaxError)
61         return V8ThrowException::createSyntaxError(sanitizedMessage, isolate);
62     if (ec == V8ReferenceError)
63         return V8ThrowException::createReferenceError(sanitizedMessage, isolate);
64 
65     RefPtrWillBeRawPtr<DOMException> domException = DOMException::create(ec, sanitizedMessage, unsanitizedMessage);
66     v8::Handle<v8::Value> exception = toV8(domException, creationContext, isolate);
67 
68     if (exception.IsEmpty())
69         return v8Undefined();
70 
71     // Attach an Error object to the DOMException. This is then lazily used to get the stack value.
72     v8::Handle<v8::Value> error = v8::Exception::Error(v8String(isolate, domException->message()));
73     ASSERT(!error.IsEmpty());
74     ASSERT(exception->IsObject());
75     exception->ToObject()->SetAccessor(v8AtomicString(isolate, "stack"), domExceptionStackGetter, domExceptionStackSetter, error);
76 
77     return exception;
78 }
79 
throwDOMException(int ec,const String & sanitizedMessage,const String & unsanitizedMessage,const v8::Handle<v8::Object> & creationContext,v8::Isolate * isolate)80 v8::Handle<v8::Value> V8ThrowException::throwDOMException(int ec, const String& sanitizedMessage, const String& unsanitizedMessage, const v8::Handle<v8::Object>& creationContext, v8::Isolate* isolate)
81 {
82     ASSERT(ec == SecurityError || unsanitizedMessage.isEmpty());
83     v8::Handle<v8::Value> exception = createDOMException(ec, sanitizedMessage, unsanitizedMessage, creationContext, isolate);
84     if (exception.IsEmpty())
85         return v8Undefined();
86 
87     return V8ThrowException::throwException(exception, isolate);
88 }
89 
createGeneralError(const String & message,v8::Isolate * isolate)90 v8::Handle<v8::Value> V8ThrowException::createGeneralError(const String& message, v8::Isolate* isolate)
91 {
92     return v8::Exception::Error(v8String(isolate, message.isNull() ? "Error" : message));
93 }
94 
throwGeneralError(const String & message,v8::Isolate * isolate)95 v8::Handle<v8::Value> V8ThrowException::throwGeneralError(const String& message, v8::Isolate* isolate)
96 {
97     v8::Handle<v8::Value> exception = V8ThrowException::createGeneralError(message, isolate);
98     return V8ThrowException::throwException(exception, isolate);
99 }
100 
createTypeError(const String & message,v8::Isolate * isolate)101 v8::Handle<v8::Value> V8ThrowException::createTypeError(const String& message, v8::Isolate* isolate)
102 {
103     return v8::Exception::TypeError(v8String(isolate, message.isNull() ? "Type error" : message));
104 }
105 
throwTypeError(const String & message,v8::Isolate * isolate)106 v8::Handle<v8::Value> V8ThrowException::throwTypeError(const String& message, v8::Isolate* isolate)
107 {
108     v8::Handle<v8::Value> exception = V8ThrowException::createTypeError(message, isolate);
109     return V8ThrowException::throwException(exception, isolate);
110 }
111 
createRangeError(const String & message,v8::Isolate * isolate)112 v8::Handle<v8::Value> V8ThrowException::createRangeError(const String& message, v8::Isolate* isolate)
113 {
114     return v8::Exception::RangeError(v8String(isolate, message.isNull() ? "Range error" : message));
115 }
116 
throwRangeError(const String & message,v8::Isolate * isolate)117 v8::Handle<v8::Value> V8ThrowException::throwRangeError(const String& message, v8::Isolate* isolate)
118 {
119     v8::Handle<v8::Value> exception = V8ThrowException::createRangeError(message, isolate);
120     return V8ThrowException::throwException(exception, isolate);
121 }
122 
createSyntaxError(const String & message,v8::Isolate * isolate)123 v8::Handle<v8::Value> V8ThrowException::createSyntaxError(const String& message, v8::Isolate* isolate)
124 {
125     return v8::Exception::SyntaxError(v8String(isolate, message.isNull() ? "Syntax error" : message));
126 }
127 
throwSyntaxError(const String & message,v8::Isolate * isolate)128 v8::Handle<v8::Value> V8ThrowException::throwSyntaxError(const String& message, v8::Isolate* isolate)
129 {
130     v8::Handle<v8::Value> exception = V8ThrowException::createSyntaxError(message, isolate);
131     return V8ThrowException::throwException(exception, isolate);
132 }
133 
createReferenceError(const String & message,v8::Isolate * isolate)134 v8::Handle<v8::Value> V8ThrowException::createReferenceError(const String& message, v8::Isolate* isolate)
135 {
136     return v8::Exception::ReferenceError(v8String(isolate, message.isNull() ? "Reference error" : message));
137 }
138 
throwReferenceError(const String & message,v8::Isolate * isolate)139 v8::Handle<v8::Value> V8ThrowException::throwReferenceError(const String& message, v8::Isolate* isolate)
140 {
141     v8::Handle<v8::Value> exception = V8ThrowException::createReferenceError(message, isolate);
142     return V8ThrowException::throwException(exception, isolate);
143 }
144 
throwException(v8::Handle<v8::Value> exception,v8::Isolate * isolate)145 v8::Handle<v8::Value> V8ThrowException::throwException(v8::Handle<v8::Value> exception, v8::Isolate* isolate)
146 {
147     if (!v8::V8::IsExecutionTerminating())
148         isolate->ThrowException(exception);
149     return v8::Undefined(isolate);
150 }
151 
152 } // namespace blink
153