| Index: Source/bindings/v8/V8PromiseUtilities.cpp
|
| diff --git a/Source/bindings/v8/V8PromiseUtilities.cpp b/Source/bindings/v8/V8PromiseUtilities.cpp
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..41e0df33820d3017676a2a06b0e50c36b40dda89
|
| --- /dev/null
|
| +++ b/Source/bindings/v8/V8PromiseUtilities.cpp
|
| @@ -0,0 +1,216 @@
|
| +/*
|
| + * 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/V8PromiseUtilities.h"
|
| +
|
| +#include "PromiseCustomScript.h"
|
| +#include "PromiseResolverCustomScript.h"
|
| +#include "V8PromiseResolver.h"
|
| +#include "bindings/v8/ScriptFunctionCall.h"
|
| +#include "bindings/v8/ScriptValue.h"
|
| +#include "bindings/v8/V8Binding.h"
|
| +#include "bindings/v8/V8HiddenPropertyName.h"
|
| +#include "bindings/v8/V8ScriptRunner.h"
|
| +#include "bindings/v8/V8ThrowException.h"
|
| +#include "core/dom/Document.h"
|
| +#include "core/dom/ExceptionCode.h"
|
| +#include "core/page/DOMWindow.h"
|
| +#include "core/platform/Task.h"
|
| +#include "wtf/Functional.h"
|
| +#include "wtf/PassOwnPtr.h"
|
| +
|
| +#include <v8.h>
|
| +
|
| +namespace WebCore {
|
| +namespace {
|
| +class PromiseCallbackTask : public ScriptExecutionContext::Task {
|
| +public:
|
| + PromiseCallbackTask(v8::Handle<v8::Function> callback): m_callback(callback) { }
|
| + virtual ~PromiseCallbackTask() { }
|
| +
|
| + virtual void performTask(ScriptExecutionContext*) OVERRIDE;
|
| +
|
| +private:
|
| + ScriptValue m_callback;
|
| +};
|
| +
|
| +void PromiseCallbackTask::performTask(ScriptExecutionContext* context)
|
| +{
|
| + ASSERT(context->isDocument());
|
| + ScriptState* state = mainWorldScriptState(static_cast<Document*>(context)->frame());
|
| + v8::HandleScope handleScope;
|
| + if (!state) {
|
| + // We can't execute the task. Ignore it.
|
| + return;
|
| + }
|
| + v8::Handle<v8::Context> v8context = state->context();
|
| + if (v8context.IsEmpty()) {
|
| + // We can't execute the task. Ignore it.
|
| + return;
|
| + }
|
| + v8::Context::Scope scope(v8context);
|
| + ScriptCallback(state, m_callback).call();
|
| +};
|
| +
|
| +v8::Handle<v8::Value> postTask(const v8::Arguments& args)
|
| +{
|
| + DOMWindow* window = activeDOMWindow();
|
| + if (args.Length() != 1 || args[0].IsEmpty() || !args[0]->IsFunction())
|
| + return V8ThrowException::throwError(v8TypeError, "postTask takes exact one function argument.", args.GetIsolate());
|
| + v8::Handle<v8::Function> callback = args[0].As<v8::Function>();
|
| + if (!window)
|
| + return V8ThrowException::setDOMException(INVALID_STATE_ERR, args.GetIsolate());
|
| + Document* document = window->document();
|
| + if (!document)
|
| + return V8ThrowException::setDOMException(INVALID_STATE_ERR, args.GetIsolate());
|
| + document->postTask(adoptPtr(new PromiseCallbackTask(callback)));
|
| + return v8Undefined();
|
| +}
|
| +
|
| +} // unnamed namespace
|
| +
|
| +v8::Handle<v8::Function> V8PromiseUtilities::promiseConstructor(v8::Isolate* isolate)
|
| +{
|
| + const char name[] = "Promise";
|
| + v8::Local<v8::Object> global = isolate->GetCurrentContext()->Global();
|
| + v8::Local<v8::Value> placeholderValue = global->Get(v8::String::NewSymbol(name));
|
| + if (placeholderValue.IsEmpty() || !placeholderValue->IsObject())
|
| + return V8ThrowException::throwError(v8GeneralError, "The placeholder for Promise is not found.", isolate).As<v8::Function>();
|
| + v8::Local<v8::Object> placeholder = placeholderValue.As<v8::Object>();
|
| + v8::Handle<v8::Value> value = placeholder->GetHiddenValue(V8HiddenPropertyName::jsConstructor());
|
| + if (!value.IsEmpty() && value->IsFunction()) {
|
| + // Return the stored constructor.
|
| + return value.As<v8::Function>();
|
| + }
|
| +
|
| + value = V8ScriptRunner::compileAndRunInternalScript(v8String(String(Promise_js, sizeof(Promise_js)), isolate), isolate);
|
| + if (value.IsEmpty())
|
| + return v8::Handle<v8::Function>();
|
| + if (!value->IsFunction())
|
| + return V8ThrowException::throwError(v8TypeError, "Promise implementation must be a function.", isolate).As<v8::Function>();
|
| +
|
| + v8::Handle<v8::Function> impl = value.As<v8::Function>();
|
| + v8::Handle<v8::Value> args[] = {
|
| + v8::FunctionTemplate::New(postTask)->GetFunction(),
|
| + };
|
| + value = impl->Call(v8::Object::New(), sizeof(args) / sizeof(args[0]), &args[0]);
|
| + if (value.IsEmpty())
|
| + return v8::Handle<v8::Function>();
|
| + if (!value->IsFunction())
|
| + return V8ThrowException::throwError(v8TypeError, "Promise constructor must be a function.", isolate).As<v8::Function>();
|
| + placeholder->SetHiddenValue(V8HiddenPropertyName::jsConstructor(), value);
|
| + return value.As<v8::Function>();
|
| +}
|
| +
|
| +v8::Handle<v8::Value> V8PromiseUtilities::promisePrototype(v8::Isolate* isolate)
|
| +{
|
| + v8::Handle<v8::Object> constructor = promiseConstructor(isolate);
|
| + if (constructor.IsEmpty())
|
| + return constructor;
|
| + return constructor->Get(v8::String::NewSymbol("prototype"));
|
| +}
|
| +
|
| +v8::Handle<v8::Function> V8PromiseUtilities::promiseResolverConstructor(v8::Isolate* isolate)
|
| +{
|
| + const char name[] = "PromiseResolver";
|
| + v8::Local<v8::Object> global = isolate->GetCurrentContext()->Global();
|
| + v8::Local<v8::Value> placeholderValue = global->Get(v8::String::NewSymbol(name));
|
| + if (placeholderValue.IsEmpty() || !placeholderValue->IsObject())
|
| + return V8ThrowException::throwError(v8GeneralError, "The placeholder for Promise is not found.", isolate).As<v8::Function>();
|
| + v8::Local<v8::Object> placeholder = placeholderValue.As<v8::Object>();
|
| + v8::Handle<v8::Value> value = placeholder->GetHiddenValue(V8HiddenPropertyName::jsConstructor());
|
| + if (!value.IsEmpty() && value->IsFunction()) {
|
| + // Return the stored constructor.
|
| + return value.As<v8::Function>();
|
| + }
|
| + value = V8ScriptRunner::compileAndRunInternalScript(v8String(String(PromiseResolver_js, sizeof(PromiseResolver_js)), isolate), isolate);
|
| + if (value.IsEmpty())
|
| + return v8::Handle<v8::Function>();
|
| + if (!value->IsFunction())
|
| + return V8ThrowException::throwError(v8TypeError, "PromiseResolver implementation must be a function.", isolate).As<v8::Function>();
|
| +
|
| + v8::Handle<v8::Function> impl = value.As<v8::Function>();
|
| + v8::Handle<v8::Value> postTaskWrapper = v8::FunctionTemplate::New(postTask)->GetFunction();
|
| + value = impl->Call(v8::Object::New(), 1, &postTaskWrapper);
|
| + if (value.IsEmpty())
|
| + return v8::Handle<v8::Function>();
|
| + if (!value->IsFunction())
|
| + return V8ThrowException::throwError(v8TypeError, "Promise constructor must be a function.", isolate).As<v8::Function>();
|
| + placeholder->SetHiddenValue(V8HiddenPropertyName::jsConstructor(), value);
|
| + return value.As<v8::Function>();
|
| +}
|
| +
|
| +v8::Handle<v8::Value> V8PromiseUtilities::promiseResolverPrototype(v8::Isolate* isolate)
|
| +{
|
| + v8::Handle<v8::Object> constructor = promiseResolverConstructor(isolate);
|
| + if (constructor.IsEmpty())
|
| + return constructor;
|
| + return constructor->Get(v8::String::NewSymbol("prototype"));
|
| +}
|
| +
|
| +v8::Handle<v8::Value> V8PromiseUtilities::callUnwrappedMethod(const char* name, const v8::FunctionCallbackInfo<v8::Value>& args, v8::Handle<v8::Value> prototype)
|
| +{
|
| + Vector<v8::Handle<v8::Value> > arguments;
|
| + for (int i = 0; i < args.Length(); ++i)
|
| + arguments.append(args[i]);
|
| + return callUnwrappedMethod(name, args.This(), arguments.size(), arguments.data(), args.GetIsolate(), prototype);
|
| +}
|
| +
|
| +v8::Handle<v8::Value> V8PromiseUtilities::callUnwrappedMethod(const char* name, v8::Handle<v8::Object> thisObject,
|
| + int argc, v8::Handle<v8::Value> argv[], v8::Isolate* isolate, v8::Handle<v8::Value> prototype)
|
| +{
|
| + v8::Handle<v8::Value> that = thisObject->GetInternalField(v8DOMWrapperObjectIndex);
|
| + if (that.IsEmpty() || !that->IsObject())
|
| + return V8ThrowException::throwError(v8TypeError, "This object is not an object.", isolate);
|
| +
|
| + if (prototype.IsEmpty() || !prototype->IsObject())
|
| + return V8ThrowException::throwError(v8TypeError, "The prototypex is not an object.", isolate);
|
| +
|
| + v8::Local<v8::Value> property = prototype.As<v8::Object>()->Get(v8::String::NewSymbol(name));
|
| + if (property.IsEmpty() || !property->IsFunction())
|
| + return V8ThrowException::throwError(v8TypeError, (std::string("The property ") + name + " is not a function.").c_str(), isolate);
|
| + return property.As<v8::Function>()->Call(that.As<v8::Object>(), argc, argv);
|
| +}
|
| +
|
| +v8::Handle<v8::Value> V8PromiseUtilities::callStatic(const char* name, const v8::FunctionCallbackInfo<v8::Value>& args, v8::Handle<v8::Value> constructor)
|
| +{
|
| + if (constructor.IsEmpty() || !constructor->IsObject())
|
| + return V8ThrowException::throwError(v8TypeError, "The constructor object is not found.", args.GetIsolate());
|
| + v8::Local<v8::Value> property = constructor.As<v8::Object>()->Get(v8::String::NewSymbol(name));
|
| + if (property.IsEmpty() || !property->IsFunction())
|
| + return V8ThrowException::throwError(v8TypeError, (std::string("The property ") + name + " is not a function.").c_str(), args.GetIsolate());
|
| + Vector<v8::Handle<v8::Value> > arguments;
|
| + for (int i = 0; i < args.Length(); ++i)
|
| + arguments.append(args[i]);
|
| + return property.As<v8::Function>()->Call(args.This(), arguments.size(), arguments.data());
|
| +}
|
| +
|
| +} // namespace WebCore
|
|
|