Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(350)

Side by Side Diff: Source/bindings/v8/ScriptPromise.cpp

Issue 232563003: API functions returning Promises should not throw exceptions. (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Created 6 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2013 Google Inc. All rights reserved. 2 * Copyright (C) 2013 Google Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are 5 * modification, are permitted provided that the following conditions are
6 * met: 6 * met:
7 * 7 *
8 * * Redistributions of source code must retain the above copyright 8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above 10 * * Redistributions in binary form must reproduce the above
(...skipping 13 matching lines...) Expand all
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */ 29 */
30 30
31 #include "config.h" 31 #include "config.h"
32 #include "bindings/v8/ScriptPromise.h" 32 #include "bindings/v8/ScriptPromise.h"
33 33
34 #include "RuntimeEnabledFeatures.h" 34 #include "bindings/v8/ExceptionMessages.h"
35 #include "bindings/v8/ExceptionState.h"
36 #include "bindings/v8/ScriptPromiseResolver.h"
35 #include "bindings/v8/V8Binding.h" 37 #include "bindings/v8/V8Binding.h"
36 #include "bindings/v8/V8DOMWrapper.h" 38 #include "bindings/v8/V8DOMWrapper.h"
39 #include "bindings/v8/V8ThrowException.h"
37 #include "bindings/v8/custom/V8PromiseCustom.h" 40 #include "bindings/v8/custom/V8PromiseCustom.h"
38 41
39 #include <v8.h> 42 #include <v8.h>
40 43
41 namespace WebCore { 44 namespace WebCore {
42 45
43 ScriptPromise::ScriptPromise(v8::Handle<v8::Value> value, v8::Isolate* isolate) 46 ScriptPromise::ScriptPromise(v8::Handle<v8::Value> value, v8::Isolate* isolate)
44 { 47 {
45 if (value.IsEmpty()) 48 if (value.IsEmpty())
46 return; 49 return;
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
82 if (!v8OnRejected.IsEmpty()) 85 if (!v8OnRejected.IsEmpty())
83 resultPromise = resultPromise->Catch(v8OnRejected); 86 resultPromise = resultPromise->Catch(v8OnRejected);
84 87
85 return ScriptPromise(resultPromise, isolate()); 88 return ScriptPromise(resultPromise, isolate());
86 } 89 }
87 90
88 ScriptPromise ScriptPromise::cast(const ScriptValue& value) 91 ScriptPromise ScriptPromise::cast(const ScriptValue& value)
89 { 92 {
90 if (value.hasNoValue()) 93 if (value.hasNoValue())
91 return ScriptPromise(); 94 return ScriptPromise();
92 v8::Local<v8::Value> v8Value(value.v8Value()); 95 return cast(value.v8Value(), value.isolate());
93 v8::Isolate* isolate = value.isolate(); 96 }
94 if (V8PromiseCustom::isPromise(v8Value, isolate) || v8Value->IsPromise()) { 97
95 return ScriptPromise(v8Value, isolate); 98 ScriptPromise ScriptPromise::cast(v8::Handle<v8::Value> value, v8::Isolate* isol ate)
96 } 99 {
97 if (RuntimeEnabledFeatures::scriptPromiseOnV8PromiseEnabled()) { 100 if (value.IsEmpty())
98 v8::Local<v8::Promise::Resolver> resolver = v8::Promise::Resolver::New(i solate); 101 return ScriptPromise();
99 if (resolver.IsEmpty()) { 102 if (V8PromiseCustom::isPromise(value, isolate) || value->IsPromise())
100 // The Promise constructor may return an empty value, for example 103 return ScriptPromise(value, isolate);
101 // when the stack is exhausted. 104
102 return ScriptPromise(); 105 RefPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(isola te);
103 } 106 ScriptPromise promise = resolver->promise();
104 resolver->Resolve(v8Value); 107 resolver->resolve(ScriptValue(value, isolate));
105 return ScriptPromise(resolver->GetPromise(), isolate); 108 return promise;
106 } 109 }
107 return ScriptPromise(V8PromiseCustom::toPromise(v8Value, isolate), isolate); 110
111 ScriptPromise ScriptPromise::reject(const ScriptValue& value)
112 {
113 if (value.hasNoValue())
114 return ScriptPromise();
115 return reject(value.v8Value(), value.isolate());
116 }
117
118 ScriptPromise ScriptPromise::reject(v8::Handle<v8::Value> value, v8::Isolate* is olate)
119 {
120 if (value.IsEmpty())
121 return ScriptPromise();
122
123 RefPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(isola te);
124 ScriptPromise promise = resolver->promise();
125 resolver->reject(ScriptValue(value, isolate));
126 return promise;
127 }
128
129 ScriptPromise ScriptPromise::rejectWithError(V8ErrorType type, const String& mes sage, v8::Isolate* isolate)
130 {
131 return reject(V8ThrowException::createError(type, message, isolate), isolate );
132 }
133
134 ScriptPromise ScriptPromise::rejectWithError(v8::Handle<v8::Value> value, v8::Is olate* isolate)
135 {
136 return reject(value, isolate);
137 }
138
139 ScriptPromise ScriptPromise::rejectWithTypeError(const String& message, v8::Isol ate* isolate)
140 {
141 return reject(V8ThrowException::createTypeError(message, isolate), isolate);
142 }
143
144 ScriptPromise ScriptPromise::rejectWithArityTypeErrorForMethod(
145 const char* method, const char* type, unsigned expected, unsigned providedLe astNumMandatoryParams, v8::Isolate* isolate)
146 {
147 String message = ExceptionMessages::failedToExecute(method, type, ExceptionM essages::notEnoughArguments(expected, providedLeastNumMandatoryParams));
148 return rejectWithTypeError(message, isolate);
149 }
150
151 ScriptPromise ScriptPromise::rejectWithArityTypeErrorForConstructor(
152 const char* type, unsigned expected, unsigned providedLeastNumMandatoryParam s, v8::Isolate* isolate)
153 {
154 String message = ExceptionMessages::failedToConstruct(type, ExceptionMessage s::notEnoughArguments(expected, providedLeastNumMandatoryParams));
155 return rejectWithTypeError(message, isolate);
156 }
157
158 ScriptPromise ScriptPromise::rejectWithArityTypeError(ExceptionState& exceptionS tate, unsigned expected, unsigned providedLeastNumMandatoryParams)
159 {
160 exceptionState.throwTypeError(ExceptionMessages::notEnoughArguments(expected , providedLeastNumMandatoryParams));
161 return exceptionState.rejectedPromise();
108 } 162 }
109 163
110 } // namespace WebCore 164 } // namespace WebCore
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698