]> git.saurik.com Git - apple/javascriptcore.git/blob - runtime/JSPromiseConstructor.cpp
JavaScriptCore-7601.1.46.3.tar.gz
[apple/javascriptcore.git] / runtime / JSPromiseConstructor.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 "JSPromiseConstructor.h"
28
29 #if ENABLE(PROMISES)
30
31 #include "Error.h"
32 #include "Exception.h"
33 #include "IteratorOperations.h"
34 #include "JSCBuiltins.h"
35 #include "JSCJSValueInlines.h"
36 #include "JSCellInlines.h"
37 #include "JSPromise.h"
38 #include "JSPromisePrototype.h"
39 #include "Lookup.h"
40 #include "NumberObject.h"
41 #include "StructureInlines.h"
42
43 namespace JSC {
44
45 STATIC_ASSERT_IS_TRIVIALLY_DESTRUCTIBLE(JSPromiseConstructor);
46
47 }
48
49 #include "JSPromiseConstructor.lut.h"
50
51 namespace JSC {
52
53 const ClassInfo JSPromiseConstructor::s_info = { "Function", &InternalFunction::s_info, &promiseConstructorTable, CREATE_METHOD_TABLE(JSPromiseConstructor) };
54
55 /* Source for JSPromiseConstructor.lut.h
56 @begin promiseConstructorTable
57 resolve JSPromiseConstructorFuncResolve DontEnum|Function 1
58 reject JSPromiseConstructorFuncReject DontEnum|Function 1
59 race JSPromiseConstructorFuncRace DontEnum|Function 1
60 all JSPromiseConstructorFuncAll DontEnum|Function 1
61 @end
62 */
63
64 JSPromiseConstructor* JSPromiseConstructor::create(VM& vm, Structure* structure, JSPromisePrototype* promisePrototype)
65 {
66 JSPromiseConstructor* constructor = new (NotNull, allocateCell<JSPromiseConstructor>(vm.heap)) JSPromiseConstructor(vm, structure);
67 constructor->finishCreation(vm, promisePrototype);
68 return constructor;
69 }
70
71 Structure* JSPromiseConstructor::createStructure(VM& vm, JSGlobalObject* globalObject, JSValue prototype)
72 {
73 return Structure::create(vm, globalObject, prototype, TypeInfo(ObjectType, StructureFlags), info());
74 }
75
76 JSPromiseConstructor::JSPromiseConstructor(VM& vm, Structure* structure)
77 : InternalFunction(vm, structure)
78 {
79 }
80
81 void JSPromiseConstructor::finishCreation(VM& vm, JSPromisePrototype* promisePrototype)
82 {
83 Base::finishCreation(vm, ASCIILiteral("Promise"));
84 putDirectWithoutTransition(vm, vm.propertyNames->prototype, promisePrototype, DontEnum | DontDelete | ReadOnly);
85 putDirectWithoutTransition(vm, vm.propertyNames->length, jsNumber(1), ReadOnly | DontEnum | DontDelete);
86 }
87
88 static EncodedJSValue JSC_HOST_CALL constructPromise(ExecState* exec)
89 {
90 VM& vm = exec->vm();
91 JSGlobalObject* globalObject = exec->callee()->globalObject();
92
93 JSPromise* promise = JSPromise::create(vm, globalObject);
94
95 JSFunction* initializePromise = globalObject->initializePromiseFunction();
96 CallData callData;
97 CallType callType = getCallData(initializePromise, callData);
98 ASSERT(callType != CallTypeNone);
99
100 MarkedArgumentBuffer arguments;
101 arguments.append(exec->argument(0));
102 call(exec, initializePromise, callType, callData, promise, arguments);
103
104 return JSValue::encode(promise);
105 }
106
107 ConstructType JSPromiseConstructor::getConstructData(JSCell*, ConstructData& constructData)
108 {
109 constructData.native.function = constructPromise;
110 return ConstructTypeHost;
111 }
112
113 CallType JSPromiseConstructor::getCallData(JSCell*, CallData& callData)
114 {
115 callData.native.function = constructPromise;
116 return CallTypeHost;
117 }
118
119 bool JSPromiseConstructor::getOwnPropertySlot(JSObject* object, ExecState* exec, PropertyName propertyName, PropertySlot& slot)
120 {
121 return getStaticFunctionSlot<InternalFunction>(exec, promiseConstructorTable, jsCast<JSPromiseConstructor*>(object), propertyName, slot);
122 }
123
124 JSPromise* constructPromise(ExecState* exec, JSGlobalObject* globalObject, JSFunction* resolver)
125 {
126 JSPromiseConstructor* promiseConstructor = globalObject->promiseConstructor();
127
128 ConstructData constructData;
129 ConstructType constructType = getConstructData(promiseConstructor, constructData);
130 ASSERT(constructType != ConstructTypeNone);
131
132 MarkedArgumentBuffer arguments;
133 arguments.append(resolver);
134
135 return jsCast<JSPromise*>(construct(exec, promiseConstructor, constructType, constructData, arguments));
136 }
137
138 } // namespace JSC
139
140 #endif // ENABLE(PROMISES)