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

Unified 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 side-by-side diff with in-line comments
Download patch
Index: Source/bindings/v8/ScriptPromise.cpp
diff --git a/Source/bindings/v8/ScriptPromise.cpp b/Source/bindings/v8/ScriptPromise.cpp
index 307d9bd7aa2170e6269d0086d45f4d50027c7c57..22f99063367a519f8d87681343371b969f2ca5cf 100644
--- a/Source/bindings/v8/ScriptPromise.cpp
+++ b/Source/bindings/v8/ScriptPromise.cpp
@@ -31,9 +31,12 @@
#include "config.h"
#include "bindings/v8/ScriptPromise.h"
-#include "RuntimeEnabledFeatures.h"
+#include "bindings/v8/ExceptionMessages.h"
+#include "bindings/v8/ExceptionState.h"
+#include "bindings/v8/ScriptPromiseResolver.h"
#include "bindings/v8/V8Binding.h"
#include "bindings/v8/V8DOMWrapper.h"
+#include "bindings/v8/V8ThrowException.h"
#include "bindings/v8/custom/V8PromiseCustom.h"
#include <v8.h>
@@ -89,22 +92,73 @@ ScriptPromise ScriptPromise::cast(const ScriptValue& value)
{
if (value.hasNoValue())
return ScriptPromise();
- v8::Local<v8::Value> v8Value(value.v8Value());
- v8::Isolate* isolate = value.isolate();
- if (V8PromiseCustom::isPromise(v8Value, isolate) || v8Value->IsPromise()) {
- return ScriptPromise(v8Value, isolate);
- }
- if (RuntimeEnabledFeatures::scriptPromiseOnV8PromiseEnabled()) {
- v8::Local<v8::Promise::Resolver> resolver = v8::Promise::Resolver::New(isolate);
- if (resolver.IsEmpty()) {
- // The Promise constructor may return an empty value, for example
- // when the stack is exhausted.
- return ScriptPromise();
- }
- resolver->Resolve(v8Value);
- return ScriptPromise(resolver->GetPromise(), isolate);
- }
- return ScriptPromise(V8PromiseCustom::toPromise(v8Value, isolate), isolate);
+ return cast(value.v8Value(), value.isolate());
+}
+
+ScriptPromise ScriptPromise::cast(v8::Handle<v8::Value> value, v8::Isolate* isolate)
+{
+ if (value.IsEmpty())
+ return ScriptPromise();
+ if (V8PromiseCustom::isPromise(value, isolate) || value->IsPromise())
+ return ScriptPromise(value, isolate);
+
+ RefPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(isolate);
+ ScriptPromise promise = resolver->promise();
+ resolver->resolve(ScriptValue(value, isolate));
+ return promise;
+}
+
+ScriptPromise ScriptPromise::reject(const ScriptValue& value)
+{
+ if (value.hasNoValue())
+ return ScriptPromise();
+ return reject(value.v8Value(), value.isolate());
+}
+
+ScriptPromise ScriptPromise::reject(v8::Handle<v8::Value> value, v8::Isolate* isolate)
+{
+ if (value.IsEmpty())
+ return ScriptPromise();
+
+ RefPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(isolate);
+ ScriptPromise promise = resolver->promise();
+ resolver->reject(ScriptValue(value, isolate));
+ return promise;
+}
+
+ScriptPromise ScriptPromise::rejectWithError(V8ErrorType type, const String& message, v8::Isolate* isolate)
+{
+ return reject(V8ThrowException::createError(type, message, isolate), isolate);
+}
+
+ScriptPromise ScriptPromise::rejectWithError(v8::Handle<v8::Value> value, v8::Isolate* isolate)
+{
+ return reject(value, isolate);
+}
+
+ScriptPromise ScriptPromise::rejectWithTypeError(const String& message, v8::Isolate* isolate)
+{
+ return reject(V8ThrowException::createTypeError(message, isolate), isolate);
+}
+
+ScriptPromise ScriptPromise::rejectWithArityTypeErrorForMethod(
+ const char* method, const char* type, unsigned expected, unsigned providedLeastNumMandatoryParams, v8::Isolate* isolate)
+{
+ String message = ExceptionMessages::failedToExecute(method, type, ExceptionMessages::notEnoughArguments(expected, providedLeastNumMandatoryParams));
+ return rejectWithTypeError(message, isolate);
+}
+
+ScriptPromise ScriptPromise::rejectWithArityTypeErrorForConstructor(
+ const char* type, unsigned expected, unsigned providedLeastNumMandatoryParams, v8::Isolate* isolate)
+{
+ String message = ExceptionMessages::failedToConstruct(type, ExceptionMessages::notEnoughArguments(expected, providedLeastNumMandatoryParams));
+ return rejectWithTypeError(message, isolate);
+}
+
+ScriptPromise ScriptPromise::rejectWithArityTypeError(ExceptionState& exceptionState, unsigned expected, unsigned providedLeastNumMandatoryParams)
+{
+ exceptionState.throwTypeError(ExceptionMessages::notEnoughArguments(expected, providedLeastNumMandatoryParams));
+ return exceptionState.rejectedPromise();
}
} // namespace WebCore

Powered by Google App Engine
This is Rietveld 408576698