053fed21cac809037d341905fc318fe11940913f
[WebKit.git] / Source / JavaScriptCore / runtime / JSPromiseConstructor.cpp
1 /*
2  * Copyright (C) 2013, 2016 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 #include "BuiltinNames.h"
30 #include "Error.h"
31 #include "Exception.h"
32 #include "GetterSetter.h"
33 #include "IteratorOperations.h"
34 #include "JSCBuiltins.h"
35 #include "JSCInlines.h"
36 #include "JSFunction.h"
37 #include "JSPromise.h"
38 #include "JSPromisePrototype.h"
39 #include "Lookup.h"
40 #include "NumberObject.h"
41
42 namespace JSC {
43
44 STATIC_ASSERT_IS_TRIVIALLY_DESTRUCTIBLE(JSPromiseConstructor);
45
46 }
47
48 #include "JSPromiseConstructor.lut.h"
49
50 namespace JSC {
51
52 const ClassInfo JSPromiseConstructor::s_info = { "Function", &Base::s_info, &promiseConstructorTable, nullptr, CREATE_METHOD_TABLE(JSPromiseConstructor) };
53
54 /* Source for JSPromiseConstructor.lut.h
55 @begin promiseConstructorTable
56   resolve         JSBuiltin             DontEnum|Function 1
57   reject          JSBuiltin             DontEnum|Function 1
58   race            JSBuiltin             DontEnum|Function 1
59   all             JSBuiltin             DontEnum|Function 1
60   allSettled      JSBuiltin             DontEnum|Function 1
61 @end
62 */
63
64 JSPromiseConstructor* JSPromiseConstructor::create(VM& vm, Structure* structure, JSPromisePrototype* promisePrototype, GetterSetter* speciesSymbol)
65 {
66     JSPromiseConstructor* constructor = new (NotNull, allocateCell<JSPromiseConstructor>(vm.heap)) JSPromiseConstructor(vm, structure);
67     constructor->finishCreation(vm, promisePrototype, speciesSymbol);
68     constructor->addOwnInternalSlots(vm, structure->globalObject());
69     return constructor;
70 }
71
72 Structure* JSPromiseConstructor::createStructure(VM& vm, JSGlobalObject* globalObject, JSValue prototype)
73 {
74     return Structure::create(vm, globalObject, prototype, TypeInfo(InternalFunctionType, StructureFlags), info());
75 }
76
77
78 static EncodedJSValue JSC_HOST_CALL callPromise(ExecState*);
79 static EncodedJSValue JSC_HOST_CALL constructPromise(ExecState*);
80
81 JSPromiseConstructor::JSPromiseConstructor(VM& vm, Structure* structure)
82     : Base(vm, structure, callPromise, constructPromise)
83 {
84 }
85
86 JSPromiseConstructor::JSPromiseConstructor(VM& vm, Structure* structure, NativeFunction functionForCall, NativeFunction functionForConstruct)
87     : Base(vm, structure, functionForCall, functionForConstruct)
88 {
89 }
90
91 void JSPromiseConstructor::finishCreation(VM& vm, JSPromisePrototype* promisePrototype, GetterSetter* speciesSymbol)
92 {
93     Base::finishCreation(vm, vm.propertyNames->Promise.string(), NameVisibility::Visible, NameAdditionMode::WithoutStructureTransition);
94     putDirectWithoutTransition(vm, vm.propertyNames->prototype, promisePrototype, PropertyAttribute::DontEnum | PropertyAttribute::DontDelete | PropertyAttribute::ReadOnly);
95     putDirectWithoutTransition(vm, vm.propertyNames->length, jsNumber(1), PropertyAttribute::DontEnum | PropertyAttribute::ReadOnly);
96     putDirectNonIndexAccessorWithoutTransition(vm, vm.propertyNames->speciesSymbol, speciesSymbol, PropertyAttribute::Accessor | PropertyAttribute::ReadOnly | PropertyAttribute::DontEnum);
97 }
98
99 void JSPromiseConstructor::addOwnInternalSlots(VM& vm, JSGlobalObject* globalObject)
100 {
101     JSC_BUILTIN_FUNCTION_WITHOUT_TRANSITION(vm.propertyNames->builtinNames().resolvePrivateName(), promiseConstructorResolveCodeGenerator, PropertyAttribute::DontEnum | PropertyAttribute::DontDelete | PropertyAttribute::ReadOnly);
102     JSC_BUILTIN_FUNCTION_WITHOUT_TRANSITION(vm.propertyNames->builtinNames().rejectPrivateName(), promiseConstructorRejectCodeGenerator, PropertyAttribute::DontEnum | PropertyAttribute::DontDelete | PropertyAttribute::ReadOnly);
103 }
104
105 static EncodedJSValue JSC_HOST_CALL constructPromise(ExecState* exec)
106 {
107     VM& vm = exec->vm();
108     auto scope = DECLARE_THROW_SCOPE(vm);
109     JSGlobalObject* globalObject = exec->jsCallee()->globalObject(vm);
110
111     JSValue newTarget = exec->newTarget();
112     if (newTarget.isUndefined())
113         return throwVMTypeError(exec, scope);
114
115     Structure* promiseStructure = InternalFunction::createSubclassStructure(exec, exec->newTarget(), globalObject->promiseStructure());
116     RETURN_IF_EXCEPTION(scope, encodedJSValue());
117     JSPromise* promise = JSPromise::create(vm, promiseStructure);
118     promise->initialize(exec, globalObject,  exec->argument(0));
119     RETURN_IF_EXCEPTION(scope, encodedJSValue());
120
121     return JSValue::encode(promise);
122 }
123
124 static EncodedJSValue JSC_HOST_CALL callPromise(ExecState* exec)
125 {
126     VM& vm = exec->vm();
127     auto scope = DECLARE_THROW_SCOPE(vm);
128     return JSValue::encode(throwConstructorCannotBeCalledAsFunctionTypeError(exec, scope, "Promise"));
129 }
130
131 } // namespace JSC