This source file includes following definitions.
- then
- cast
#include "config.h"
#include "bindings/v8/ScriptPromise.h"
#include "RuntimeEnabledFeatures.h"
#include "bindings/v8/V8Binding.h"
#include "bindings/v8/V8DOMWrapper.h"
#include "bindings/v8/custom/V8PromiseCustom.h"
#include <v8.h>
namespace WebCore {
ScriptPromise::ScriptPromise(v8::Handle<v8::Value> value, v8::Isolate* isolate)
{
if (value.IsEmpty())
return;
if (!V8PromiseCustom::isPromise(value, isolate) && !value->IsPromise()) {
m_promise = ScriptValue(v8::Handle<v8::Value>(), isolate);
V8ThrowException::throwTypeError("the given value is not a Promise", isolate);
return;
}
m_promise = ScriptValue(value, isolate);
}
ScriptPromise ScriptPromise::then(PassOwnPtr<ScriptFunction> onFulfilled, PassOwnPtr<ScriptFunction> onRejected)
{
if (m_promise.hasNoValue())
return ScriptPromise();
v8::Local<v8::Object> promise = m_promise.v8Value().As<v8::Object>();
v8::Local<v8::Function> v8OnFulfilled = adoptByGarbageCollector(onFulfilled);
v8::Local<v8::Function> v8OnRejected = adoptByGarbageCollector(onRejected);
if (V8PromiseCustom::isPromise(promise, isolate()))
return ScriptPromise(V8PromiseCustom::then(promise, v8OnFulfilled, v8OnRejected, isolate()), isolate());
ASSERT(promise->IsPromise());
v8::Local<v8::Promise> resultPromise = promise.As<v8::Promise>();
if (!v8OnFulfilled.IsEmpty()) {
resultPromise = resultPromise->Chain(v8OnFulfilled);
if (resultPromise.IsEmpty()) {
return ScriptPromise();
}
}
if (!v8OnRejected.IsEmpty())
resultPromise = resultPromise->Catch(v8OnRejected);
return ScriptPromise(resultPromise, isolate());
}
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()) {
return ScriptPromise();
}
resolver->Resolve(v8Value);
return ScriptPromise(resolver->GetPromise(), isolate);
}
return ScriptPromise(V8PromiseCustom::toPromise(v8Value, isolate), isolate);
}
}