blob: 77f4d2779b40573ade4743aeaa7d9c4b65708811 [file] [log] [blame]
/*
* Copyright (C) 2013 Google Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Google Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "bindings/v8/ScriptPromiseResolver.h"
#include "RuntimeEnabledFeatures.h"
#include "bindings/v8/ScriptState.h"
#include "bindings/v8/ScriptValue.h"
#include "bindings/v8/V8Binding.h"
#include "bindings/v8/V8DOMWrapper.h"
#include "bindings/v8/custom/V8PromiseCustom.h"
#include "wtf/MainThread.h"
#include <v8.h>
namespace WebCore {
ScriptPromiseResolver::ScriptPromiseResolver(v8::Handle<v8::Object> creationContext, v8::Isolate* isolate)
{
ASSERT(RuntimeEnabledFeatures::promiseEnabled());
v8::Local<v8::Object> promise, resolver;
V8PromiseCustom::createPromise(creationContext, &promise, &resolver, isolate);
m_promise.set(isolate, promise);
m_resolver.set(isolate, resolver);
}
ScriptPromiseResolver::~ScriptPromiseResolver()
{
detach();
}
PassRefPtr<ScriptPromiseResolver> ScriptPromiseResolver::create(ScriptExecutionContext* context)
{
ASSERT(isMainThread());
ASSERT(context);
v8::Isolate* isolate = v8::Isolate::GetCurrent();
v8::HandleScope scope(isolate);
return adoptRef(new ScriptPromiseResolver(toV8Context(context, DOMWrapperWorld::current())->Global(), isolate));
}
PassRefPtr<ScriptPromiseResolver> ScriptPromiseResolver::create()
{
ASSERT(isMainThread());
v8::Isolate* isolate = v8::Isolate::GetCurrent();
v8::HandleScope scope(isolate);
return adoptRef(new ScriptPromiseResolver(v8::Object::New(), isolate));
}
bool ScriptPromiseResolver::isPending() const
{
ASSERT(isMainThread());
v8::HandleScope scope(v8::Isolate::GetCurrent());
return isPendingInternal();
}
void ScriptPromiseResolver::detach()
{
ASSERT(isMainThread());
v8::Isolate* isolate = v8::Isolate::GetCurrent();
v8::HandleScope scope(isolate);
detachPromise();
reject(v8::Undefined(isolate), isolate);
m_resolver.clear();
}
void ScriptPromiseResolver::fulfill(v8::Handle<v8::Value> value, v8::Isolate* isolate)
{
ASSERT(isMainThread());
if (!isPendingInternal())
return;
V8PromiseCustom::fulfillResolver(m_resolver.get(), value, V8PromiseCustom::Asynchronous, isolate);
m_resolver.clear();
}
void ScriptPromiseResolver::resolve(v8::Handle<v8::Value> value, v8::Isolate* isolate)
{
ASSERT(isMainThread());
if (!isPendingInternal())
return;
V8PromiseCustom::resolveResolver(m_resolver.get(), value, V8PromiseCustom::Asynchronous, isolate);
m_resolver.clear();
}
void ScriptPromiseResolver::reject(v8::Handle<v8::Value> value, v8::Isolate* isolate)
{
ASSERT(isMainThread());
if (!isPendingInternal())
return;
V8PromiseCustom::rejectResolver(m_resolver.get(), value, V8PromiseCustom::Asynchronous, isolate);
m_resolver.clear();
}
void ScriptPromiseResolver::fulfill(ScriptValue value)
{
ASSERT(isMainThread());
v8::Isolate* isolate = v8::Isolate::GetCurrent();
v8::HandleScope scope(isolate);
fulfill(value.v8Value(), isolate);
}
void ScriptPromiseResolver::resolve(ScriptValue value)
{
ASSERT(isMainThread());
v8::Isolate* isolate = v8::Isolate::GetCurrent();
v8::HandleScope scope(isolate);
resolve(value.v8Value(), isolate);
}
void ScriptPromiseResolver::reject(ScriptValue value)
{
ASSERT(isMainThread());
v8::Isolate* isolate = v8::Isolate::GetCurrent();
v8::HandleScope scope(isolate);
reject(value.v8Value(), isolate);
}
bool ScriptPromiseResolver::isPendingInternal() const
{
ASSERT(isMainThread());
if (m_resolver.isEmpty() || V8PromiseCustom::isInternalDetached(m_resolver.get()))
return false;
v8::Local<v8::Object> internal = V8PromiseCustom::getInternal(m_resolver.get());
V8PromiseCustom::PromiseState state = V8PromiseCustom::getState(internal);
return state == V8PromiseCustom::Pending;
}
} // namespace WebCore