Add support for Promises
https://bugs.webkit.org/show_bug.cgi?id=120260

Reviewed by Darin Adler.

Source/JavaScriptCore: 

Add an initial implementation of Promises - http://dom.spec.whatwg.org/#promises.
- Despite Promises being defined in the DOM, the implementation is being put in JSC
  in preparation for the Promises eventually being defined in ECMAScript.

* CMakeLists.txt:
* DerivedSources.make:
* DerivedSources.pri:
* GNUmakefile.list.am:
* JavaScriptCore.vcxproj/JavaScriptCore.vcxproj:
* JavaScriptCore.xcodeproj/project.pbxproj:
* Target.pri:
Add new files.

* jsc.cpp:
Update jsc's GlobalObjectMethodTable to stub out the new QueueTaskToEventLoop callback. This mean's
you can't quite use Promises with with the command line tool yet.
    
* interpreter/CallFrame.h:
(JSC::ExecState::promisePrototypeTable):
(JSC::ExecState::promiseConstructorTable):
(JSC::ExecState::promiseResolverPrototypeTable):
* runtime/VM.cpp:
(JSC::VM::VM):
(JSC::VM::~VM):
* runtime/VM.h:
Add supporting code for the new static lookup tables.

* runtime/CommonIdentifiers.h:
Add 3 new identifiers, "Promise", "PromiseResolver", and "then".

* runtime/JSGlobalObject.cpp:
(JSC::JSGlobalObject::reset):
(JSC::JSGlobalObject::visitChildren):
Add supporting code Promise and PromiseResolver's constructors and structures.

* runtime/JSGlobalObject.h:
(JSC::TaskContext::~TaskContext):
Add a new callback to the GlobalObjectMethodTable to post a task on the embedder's runloop.

(JSC::JSGlobalObject::promisePrototype):
(JSC::JSGlobalObject::promiseResolverPrototype):
(JSC::JSGlobalObject::promiseStructure):
(JSC::JSGlobalObject::promiseResolverStructure):
(JSC::JSGlobalObject::promiseCallbackStructure):
(JSC::JSGlobalObject::promiseWrapperCallbackStructure):
Add supporting code Promise and PromiseResolver's constructors and structures.

* runtime/JSPromise.cpp: Added.
* runtime/JSPromise.h: Added.
* runtime/JSPromiseCallback.cpp: Added.
* runtime/JSPromiseCallback.h: Added.
* runtime/JSPromiseConstructor.cpp: Added.
* runtime/JSPromiseConstructor.h: Added.
* runtime/JSPromisePrototype.cpp: Added.
* runtime/JSPromisePrototype.h: Added.
* runtime/JSPromiseResolver.cpp: Added.
* runtime/JSPromiseResolver.h: Added.
* runtime/JSPromiseResolverConstructor.cpp: Added.
* runtime/JSPromiseResolverConstructor.h: Added.
* runtime/JSPromiseResolverPrototype.cpp: Added.
* runtime/JSPromiseResolverPrototype.h: Added.
Add Promise implementation.

Source/WebCore: 

Add an initial implementation of Promises - http://dom.spec.whatwg.org/#promises.
- Despite Promises being defined in the DOM, the implementation is being put in JSC
  in preparation for the Promises eventually being defined in ECMAScript.

Tests: fast/js/Promise-already-fulfilled.html
       fast/js/Promise-already-rejected.html
       fast/js/Promise-already-resolved.html
       fast/js/Promise-catch-in-workers.html
       fast/js/Promise-catch.html
       fast/js/Promise-chain.html
       fast/js/Promise-exception.html
       fast/js/Promise-fulfill-in-workers.html
       fast/js/Promise-fulfill.html
       fast/js/Promise-init-in-workers.html
       fast/js/Promise-init.html
       fast/js/Promise-reject-in-workers.html
       fast/js/Promise-reject.html
       fast/js/Promise-resolve-chain.html
       fast/js/Promise-resolve-in-workers.html
       fast/js/Promise-resolve-with-then-exception.html
       fast/js/Promise-resolve-with-then-fulfill.html
       fast/js/Promise-resolve-with-then-reject.html
       fast/js/Promise-resolve.html
       fast/js/Promise-simple-fulfill-inside-callback.html
       fast/js/Promise-simple-fulfill.html
       fast/js/Promise-simple-in-workers.html
       fast/js/Promise-simple.html
       fast/js/Promise-static-fulfill.html
       fast/js/Promise-static-reject.html
       fast/js/Promise-static-resolve.html
       fast/js/Promise-then-in-workers.html
       fast/js/Promise-then-without-callbacks-in-workers.html
       fast/js/Promise-then-without-callbacks.html
       fast/js/Promise-then.html
       fast/js/Promise-types.html
       fast/js/Promise.html

* GNUmakefile.list.am:
* Target.pri:
* UseJSC.cmake:
* WebCore.vcxproj/WebCore.vcxproj:
* WebCore.vcxproj/WebCore.vcxproj.filters:
* WebCore.xcodeproj/project.pbxproj:
* bindings/js/JSBindingsAllInOne.cpp:
Add new files.

* bindings/js/JSDOMGlobalObjectTask.cpp: Added.
(WebCore::JSGlobalObjectCallback::create):
(WebCore::JSGlobalObjectCallback::~JSGlobalObjectCallback):
(WebCore::JSGlobalObjectCallback::call):
(WebCore::JSGlobalObjectCallback::JSGlobalObjectCallback):
(WebCore::JSGlobalObjectTask::JSGlobalObjectTask):
(WebCore::JSGlobalObjectTask::~JSGlobalObjectTask):
(WebCore::JSGlobalObjectTask::performTask):
* bindings/js/JSDOMGlobalObjectTask.h: Added.
(WebCore::JSGlobalObjectTask::create):
Add a new task type to be used with the GlobalObjectMethodTable's new QueueTaskToEventLoop callback.

* bindings/js/JSDOMWindowBase.cpp:
(WebCore::JSDOMWindowBase::queueTaskToEventLoop):
* bindings/js/JSDOMWindowBase.h:
Implement the GlobalObjectMethodTable callback, QueueTaskToEventLoop. 

* bindings/js/JSMainThreadExecState.h:
All using JSMainThreadExecState as a simple RAII object.

* bindings/js/JSWorkerGlobalScopeBase.cpp:
(WebCore::JSWorkerGlobalScopeBase::JSWorkerGlobalScopeBase):
(WebCore::JSWorkerGlobalScopeBase::allowsAccessFrom):
(WebCore::JSWorkerGlobalScopeBase::supportsProfiling):
(WebCore::JSWorkerGlobalScopeBase::supportsRichSourceInfo):
(WebCore::JSWorkerGlobalScopeBase::shouldInterruptScript):
(WebCore::JSWorkerGlobalScopeBase::javaScriptExperimentsEnabled):
(WebCore::JSWorkerGlobalScopeBase::queueTaskToEventLoop):
* bindings/js/JSWorkerGlobalScopeBase.h:
Add a GlobalObjectMethodTable and implement QueueTaskToEventLoop. Forward the other callbacks
to JSGlobalObject so they retain their existing behavior.

LayoutTests: 

Add tests adapted from the Mozilla and Blink projects.

* fast/js/Promise-already-fulfilled-expected.txt: Added.
* fast/js/Promise-already-fulfilled.html: Added.
* fast/js/Promise-already-rejected-expected.txt: Added.
* fast/js/Promise-already-rejected.html: Added.
* fast/js/Promise-already-resolved-expected.txt: Added.
* fast/js/Promise-already-resolved.html: Added.
* fast/js/Promise-catch-expected.txt: Added.
* fast/js/Promise-catch-in-workers-expected.txt: Added.
* fast/js/Promise-catch-in-workers.html: Added.
* fast/js/Promise-catch.html: Added.
* fast/js/Promise-chain-expected.txt: Added.
* fast/js/Promise-chain.html: Added.
* fast/js/Promise-exception-expected.txt: Added.
* fast/js/Promise-exception.html: Added.
* fast/js/Promise-expected.txt: Added.
* fast/js/Promise-fulfill-expected.txt: Added.
* fast/js/Promise-fulfill-in-workers-expected.txt: Added.
* fast/js/Promise-fulfill-in-workers.html: Added.
* fast/js/Promise-fulfill.html: Added.
* fast/js/Promise-init-expected.txt: Added.
* fast/js/Promise-init-in-workers-expected.txt: Added.
* fast/js/Promise-init-in-workers.html: Added.
* fast/js/Promise-init.html: Added.
* fast/js/Promise-reject-expected.txt: Added.
* fast/js/Promise-reject-in-workers-expected.txt: Added.
* fast/js/Promise-reject-in-workers.html: Added.
* fast/js/Promise-reject.html: Added.
* fast/js/Promise-resolve-chain-expected.txt: Added.
* fast/js/Promise-resolve-chain.html: Added.
* fast/js/Promise-resolve-expected.txt: Added.
* fast/js/Promise-resolve-in-workers-expected.txt: Added.
* fast/js/Promise-resolve-in-workers.html: Added.
* fast/js/Promise-resolve-with-then-exception-expected.txt: Added.
* fast/js/Promise-resolve-with-then-exception.html: Added.
* fast/js/Promise-resolve-with-then-fulfill-expected.txt: Added.
* fast/js/Promise-resolve-with-then-fulfill.html: Added.
* fast/js/Promise-resolve-with-then-reject-expected.txt: Added.
* fast/js/Promise-resolve-with-then-reject.html: Added.
* fast/js/Promise-resolve.html: Added.
* fast/js/Promise-simple-expected.txt: Added.
* fast/js/Promise-simple-fulfill-expected.txt: Added.
* fast/js/Promise-simple-fulfill-inside-callback-expected.txt: Added.
* fast/js/Promise-simple-fulfill-inside-callback.html: Added.
* fast/js/Promise-simple-fulfill.html: Added.
* fast/js/Promise-simple-in-workers-expected.txt: Added.
* fast/js/Promise-simple-in-workers.html: Added.
* fast/js/Promise-simple.html: Added.
* fast/js/Promise-static-fulfill-expected.txt: Added.
* fast/js/Promise-static-fulfill.html: Added.
* fast/js/Promise-static-reject-expected.txt: Added.
* fast/js/Promise-static-reject.html: Added.
* fast/js/Promise-static-resolve-expected.txt: Added.
* fast/js/Promise-static-resolve.html: Added.
* fast/js/Promise-then-expected.txt: Added.
* fast/js/Promise-then-in-workers-expected.txt: Added.
* fast/js/Promise-then-in-workers.html: Added.
* fast/js/Promise-then-without-callbacks-expected.txt: Added.
* fast/js/Promise-then-without-callbacks-in-workers-expected.txt: Added.
* fast/js/Promise-then-without-callbacks-in-workers.html: Added.
* fast/js/Promise-then-without-callbacks.html: Added.
* fast/js/Promise-then.html: Added.
* fast/js/Promise-types-expected.txt: Added.
* fast/js/Promise-types.html: Added.
* fast/js/Promise.html: Added.
* fast/js/resources/Promise-catch-in-workers.js: Added.
* fast/js/resources/Promise-fulfill-in-workers.js: Added.
* fast/js/resources/Promise-init-in-workers.js: Added.
* fast/js/resources/Promise-reject-in-workers.js: Added.
* fast/js/resources/Promise-resolve-in-workers.js: Added.
* fast/js/resources/Promise-simple-in-workers.js: Added.
* fast/js/resources/Promise-then-in-workers.js: Added.
* fast/js/resources/Promise-then-without-callbacks-in-workers.js: Added.


git-svn-id: http://svn.webkit.org/repository/webkit/trunk@154629 268f45cc-cd09-0410-ab3c-d52691b4dbfc
diff --git a/Source/JavaScriptCore/runtime/JSPromise.cpp b/Source/JavaScriptCore/runtime/JSPromise.cpp
new file mode 100644
index 0000000..20481cc
--- /dev/null
+++ b/Source/JavaScriptCore/runtime/JSPromise.cpp
@@ -0,0 +1,241 @@
+/*
+ * Copyright (C) 2013 Apple 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:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. 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.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS 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 APPLE INC. OR ITS 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 "JSPromise.h"
+
+#include "Error.h"
+#include "JSCJSValueInlines.h"
+#include "JSCellInlines.h"
+#include "JSPromiseResolver.h"
+#include "SlotVisitorInlines.h"
+#include "StrongInlines.h"
+#include "StructureInlines.h"
+
+namespace JSC {
+
+class JSPromiseTaskContext : public TaskContext {
+public:
+    static PassRefPtr<JSPromiseTaskContext> create(VM& vm, JSPromise* promise)
+    {
+        return adoptRef(new JSPromiseTaskContext(vm, promise));
+    }
+
+    JSPromise& promise() const { return *m_promise.get(); }
+
+private:
+    JSPromiseTaskContext(VM& vm, JSPromise* promise)
+    {
+        m_promise.set(vm, promise);
+    }
+
+    Strong<JSPromise> m_promise;
+};
+
+const ClassInfo JSPromise::s_info = { "Promise", &Base::s_info, 0, 0, CREATE_METHOD_TABLE(JSPromise) };
+
+JSPromise* JSPromise::create(VM& vm, Structure* structure)
+{
+    JSPromise* promise = new (NotNull, allocateCell<JSPromise>(vm.heap)) JSPromise(vm, structure);
+    promise->finishCreation(vm);
+    return promise;
+}
+
+JSPromise* JSPromise::createWithResolver(VM& vm, JSGlobalObject* globalObject)
+{
+    JSPromise* promise = new (NotNull, allocateCell<JSPromise>(vm.heap)) JSPromise(vm, globalObject->promiseStructure());
+    promise->finishCreation(vm);
+
+    JSPromiseResolver* resolver = JSPromiseResolver::create(vm, globalObject->promiseResolverStructure(), promise);
+    promise->setResolver(vm, resolver);
+
+    return promise;
+}
+
+Structure* JSPromise::createStructure(VM& vm, JSGlobalObject* globalObject, JSValue prototype)
+{
+    return Structure::create(vm, globalObject, prototype, TypeInfo(ObjectType, StructureFlags), info());
+}
+
+JSPromise::JSPromise(VM& vm, Structure* structure)
+    : JSDestructibleObject(vm, structure)
+    , m_state(Pending)
+{
+}
+
+void JSPromise::finishCreation(VM& vm)
+{
+    Base::finishCreation(vm);
+    ASSERT(inherits(info()));
+}
+
+void JSPromise::destroy(JSCell* cell)
+{
+    static_cast<JSPromise*>(cell)->JSPromise::~JSPromise();
+}
+
+void JSPromise::visitChildren(JSCell* cell, SlotVisitor& visitor)
+{
+    JSPromise* thisObject = jsCast<JSPromise*>(cell);
+    ASSERT_GC_OBJECT_INHERITS(thisObject, info());
+    COMPILE_ASSERT(StructureFlags & OverridesVisitChildren, OverridesVisitChildrenWithoutSettingFlag);
+    ASSERT(thisObject->structure()->typeInfo().overridesVisitChildren());
+
+    Base::visitChildren(thisObject, visitor);
+
+    visitor.append(&thisObject->m_resolver);
+    visitor.append(&thisObject->m_result);
+
+    for (size_t i = 0; i < thisObject->m_fulfillCallbacks.size(); ++i)
+        visitor.append(&thisObject->m_fulfillCallbacks[i]);
+    for (size_t i = 0; i < thisObject->m_rejectCallbacks.size(); ++i)
+        visitor.append(&thisObject->m_rejectCallbacks[i]);
+}
+
+void JSPromise::setResolver(VM& vm, JSPromiseResolver* resolver)
+{
+    m_resolver.set(vm, this, resolver);
+}
+
+JSPromiseResolver* JSPromise::resolver() const
+{
+    return m_resolver.get();
+}
+
+void JSPromise::setResult(VM& vm, JSValue result)
+{
+    m_result.set(vm, this, result);
+}
+
+JSValue JSPromise::result() const
+{
+    return m_result.get();
+}
+
+void JSPromise::setState(JSPromise::State state)
+{
+    ASSERT(m_state == Pending);
+    m_state = state;
+}
+
+JSPromise::State JSPromise::state() const
+{
+    return m_state;
+}
+
+void JSPromise::appendCallbacks(ExecState* exec, InternalFunction* fulfillCallback, InternalFunction* rejectCallback)
+{
+    // 1. Append fulfillCallback to promise's fulfill callbacks.
+    m_fulfillCallbacks.append(WriteBarrier<InternalFunction>(exec->vm(), this, fulfillCallback));
+    
+    // 2. Append rejectCallback to promise' reject callbacks.
+    m_rejectCallbacks.append(WriteBarrier<InternalFunction>(exec->vm(), this, rejectCallback));
+
+    // 3. If promise's state is fulfilled, queue a task to process promise's fulfill callbacks with promise's result.
+    if (m_state == Fulfilled) {
+        queueTaskToProcessFulfillCallbacks(exec);
+        return;
+    }
+
+    // 4. If promise's state is rejected, queue a task to process promise's reject callbacks with promise's result.
+    if (m_state == Rejected) {
+        queueTaskToProcessRejectCallbacks(exec);
+        return;
+    }
+}
+
+void JSPromise::queueTaskToProcessFulfillCallbacks(ExecState* exec)
+{
+    JSGlobalObject* globalObject = this->globalObject();
+    if (globalObject->globalObjectMethodTable()->queueTaskToEventLoop)
+        globalObject->globalObjectMethodTable()->queueTaskToEventLoop(globalObject, processFulfillCallbacksForTask, JSPromiseTaskContext::create(exec->vm(), this));
+    else
+        WTFLogAlways("ERROR: Event loop not supported.");
+}
+
+void JSPromise::queueTaskToProcessRejectCallbacks(ExecState* exec)
+{
+    JSGlobalObject* globalObject = this->globalObject();
+    if (globalObject->globalObjectMethodTable()->queueTaskToEventLoop)
+        globalObject->globalObjectMethodTable()->queueTaskToEventLoop(globalObject, processRejectCallbacksForTask, JSPromiseTaskContext::create(exec->vm(), this));
+    else
+        WTFLogAlways("ERROR: Event loop not supported.");
+}
+
+void JSPromise::processFulfillCallbacksForTask(ExecState* exec, TaskContext* taskContext)
+{
+    JSPromiseTaskContext* promiseTaskContext = static_cast<JSPromiseTaskContext*>(taskContext);
+    JSPromise& promise = promiseTaskContext->promise();
+
+    promise.processFulfillCallbacksWithValue(exec, promise.result());
+}
+
+void JSPromise::processRejectCallbacksForTask(ExecState* exec, TaskContext* taskContext)
+{
+    JSPromiseTaskContext* promiseTaskContext = static_cast<JSPromiseTaskContext*>(taskContext);
+    JSPromise& promise = promiseTaskContext->promise();
+
+    promise.processRejectCallbacksWithValue(exec, promise.result());
+}
+
+void JSPromise::processFulfillCallbacksWithValue(ExecState* exec, JSValue value)
+{
+    ASSERT(m_state == Fulfilled);
+
+    for (size_t i = 0; i < m_fulfillCallbacks.size(); ++i) {
+        JSValue callback = m_fulfillCallbacks[i].get();
+
+        CallData callData;
+        CallType callType = JSC::getCallData(callback, callData);
+        ASSERT(callType != CallTypeNone);
+
+        MarkedArgumentBuffer arguments;
+        arguments.append(value);
+        call(exec, callback, callType, callData, this, arguments);
+    }
+    
+    m_fulfillCallbacks.clear();
+}
+
+void JSPromise::processRejectCallbacksWithValue(ExecState* exec, JSValue value)
+{
+    ASSERT(m_state == Rejected);
+
+    for (size_t i = 0; i < m_rejectCallbacks.size(); ++i) {
+        JSValue callback = m_rejectCallbacks[i].get();
+
+        CallData callData;
+        CallType callType = JSC::getCallData(callback, callData);
+        ASSERT(callType != CallTypeNone);
+
+        MarkedArgumentBuffer arguments;
+        arguments.append(value);
+        call(exec, callback, callType, callData, this, arguments);
+    }
+    
+    m_rejectCallbacks.clear();
+}
+
+} // namespace JSC