2 * Copyright (C) 2013 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
27 #include "JSPromiseConstructor.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"
40 #include "NumberObject.h"
41 #include "StructureInlines.h"
45 STATIC_ASSERT_IS_TRIVIALLY_DESTRUCTIBLE(JSPromiseConstructor
);
49 #include "JSPromiseConstructor.lut.h"
53 const ClassInfo
JSPromiseConstructor::s_info
= { "Function", &InternalFunction::s_info
, &promiseConstructorTable
, CREATE_METHOD_TABLE(JSPromiseConstructor
) };
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
64 JSPromiseConstructor
* JSPromiseConstructor::create(VM
& vm
, Structure
* structure
, JSPromisePrototype
* promisePrototype
)
66 JSPromiseConstructor
* constructor
= new (NotNull
, allocateCell
<JSPromiseConstructor
>(vm
.heap
)) JSPromiseConstructor(vm
, structure
);
67 constructor
->finishCreation(vm
, promisePrototype
);
71 Structure
* JSPromiseConstructor::createStructure(VM
& vm
, JSGlobalObject
* globalObject
, JSValue prototype
)
73 return Structure::create(vm
, globalObject
, prototype
, TypeInfo(ObjectType
, StructureFlags
), info());
76 JSPromiseConstructor::JSPromiseConstructor(VM
& vm
, Structure
* structure
)
77 : InternalFunction(vm
, structure
)
81 void JSPromiseConstructor::finishCreation(VM
& vm
, JSPromisePrototype
* promisePrototype
)
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
);
88 static EncodedJSValue JSC_HOST_CALL
constructPromise(ExecState
* exec
)
91 JSGlobalObject
* globalObject
= exec
->callee()->globalObject();
93 JSPromise
* promise
= JSPromise::create(vm
, globalObject
);
95 JSFunction
* initializePromise
= globalObject
->initializePromiseFunction();
97 CallType callType
= getCallData(initializePromise
, callData
);
98 ASSERT(callType
!= CallTypeNone
);
100 MarkedArgumentBuffer arguments
;
101 arguments
.append(exec
->argument(0));
102 call(exec
, initializePromise
, callType
, callData
, promise
, arguments
);
104 return JSValue::encode(promise
);
107 ConstructType
JSPromiseConstructor::getConstructData(JSCell
*, ConstructData
& constructData
)
109 constructData
.native
.function
= constructPromise
;
110 return ConstructTypeHost
;
113 CallType
JSPromiseConstructor::getCallData(JSCell
*, CallData
& callData
)
115 callData
.native
.function
= constructPromise
;
119 bool JSPromiseConstructor::getOwnPropertySlot(JSObject
* object
, ExecState
* exec
, PropertyName propertyName
, PropertySlot
& slot
)
121 return getStaticFunctionSlot
<InternalFunction
>(exec
, promiseConstructorTable
, jsCast
<JSPromiseConstructor
*>(object
), propertyName
, slot
);
124 JSPromise
* constructPromise(ExecState
* exec
, JSGlobalObject
* globalObject
, JSFunction
* resolver
)
126 JSPromiseConstructor
* promiseConstructor
= globalObject
->promiseConstructor();
128 ConstructData constructData
;
129 ConstructType constructType
= getConstructData(promiseConstructor
, constructData
);
130 ASSERT(constructType
!= ConstructTypeNone
);
132 MarkedArgumentBuffer arguments
;
133 arguments
.append(resolver
);
135 return jsCast
<JSPromise
*>(construct(exec
, promiseConstructor
, constructType
, constructData
, arguments
));
140 #endif // ENABLE(PROMISES)