]> git.saurik.com Git - apple/javascriptcore.git/blob - runtime/JSPromiseDeferred.cpp
JavaScriptCore-7601.1.46.3.tar.gz
[apple/javascriptcore.git] / runtime / JSPromiseDeferred.cpp
1 /*
2 * Copyright (C) 2013 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26 #include "config.h"
27 #include "JSPromiseDeferred.h"
28
29 #if ENABLE(PROMISES)
30
31 #include "BuiltinNames.h"
32 #include "Error.h"
33 #include "Exception.h"
34 #include "JSCJSValueInlines.h"
35 #include "JSCellInlines.h"
36 #include "JSPromise.h"
37 #include "JSPromiseConstructor.h"
38 #include "SlotVisitorInlines.h"
39 #include "StructureInlines.h"
40
41 namespace JSC {
42
43 const ClassInfo JSPromiseDeferred::s_info = { "JSPromiseDeferred", 0, 0, CREATE_METHOD_TABLE(JSPromiseDeferred) };
44
45 JSPromiseDeferred* JSPromiseDeferred::create(ExecState* exec, JSGlobalObject* globalObject)
46 {
47 VM& vm = exec->vm();
48
49 JSFunction* newPromiseDeferredFunction = globalObject->newPromiseDeferredFunction();
50 CallData callData;
51 CallType callType = JSC::getCallData(newPromiseDeferredFunction, callData);
52 ASSERT(callType != CallTypeNone);
53
54 MarkedArgumentBuffer arguments;
55 JSValue deferred = call(exec, newPromiseDeferredFunction, callType, callData, jsUndefined(), arguments);
56
57 JSValue promise = deferred.get(exec, vm.propertyNames->promisePrivateName);
58 ASSERT(promise.inherits(JSPromise::info()));
59 JSValue resolve = deferred.get(exec, vm.propertyNames->builtinNames().resolvePrivateName());
60 JSValue reject = deferred.get(exec, vm.propertyNames->builtinNames().rejectPrivateName());
61
62 return JSPromiseDeferred::create(vm, jsCast<JSPromise*>(promise), resolve, reject);
63 }
64
65 JSPromiseDeferred* JSPromiseDeferred::create(VM& vm, JSObject* promise, JSValue resolve, JSValue reject)
66 {
67 JSPromiseDeferred* deferred = new (NotNull, allocateCell<JSPromiseDeferred>(vm.heap)) JSPromiseDeferred(vm);
68 deferred->finishCreation(vm, promise, resolve, reject);
69 return deferred;
70 }
71
72 JSPromiseDeferred::JSPromiseDeferred(VM& vm)
73 : Base(vm, vm.promiseDeferredStructure.get())
74 {
75 }
76
77 void JSPromiseDeferred::finishCreation(VM& vm, JSObject* promise, JSValue resolve, JSValue reject)
78 {
79 Base::finishCreation(vm);
80 m_promise.set(vm, this, promise);
81 m_resolve.set(vm, this, resolve);
82 m_reject.set(vm, this, reject);
83 }
84
85 void JSPromiseDeferred::visitChildren(JSCell* cell, SlotVisitor& visitor)
86 {
87 JSPromiseDeferred* thisObject = jsCast<JSPromiseDeferred*>(cell);
88 ASSERT_GC_OBJECT_INHERITS(thisObject, info());
89
90 Base::visitChildren(thisObject, visitor);
91
92 visitor.append(&thisObject->m_promise);
93 visitor.append(&thisObject->m_resolve);
94 visitor.append(&thisObject->m_reject);
95 }
96
97 } // namespace JSC
98
99 #endif // ENABLE(PROMISES)