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
42namespace JSC {
43
44STATIC_ASSERT_IS_TRIVIALLY_DESTRUCTIBLE(JSPromiseConstructor);
45
46}
47
48#include "JSPromiseConstructor.lut.h"
49
50namespace JSC {
51
52const 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@end
61*/
62
63JSPromiseConstructor* JSPromiseConstructor::create(VM& vm, Structure* structure, JSPromisePrototype* promisePrototype, GetterSetter* speciesSymbol)
64{
65 JSPromiseConstructor* constructor = new (NotNull, allocateCell<JSPromiseConstructor>(vm.heap)) JSPromiseConstructor(vm, structure);
66 constructor->finishCreation(vm, promisePrototype, speciesSymbol);
67 constructor->addOwnInternalSlots(vm, structure->globalObject());
68 return constructor;
69}
70
71Structure* JSPromiseConstructor::createStructure(VM& vm, JSGlobalObject* globalObject, JSValue prototype)
72{
73 return Structure::create(vm, globalObject, prototype, TypeInfo(InternalFunctionType, StructureFlags), info());
74}
75
76
77static EncodedJSValue JSC_HOST_CALL callPromise(ExecState*);
78static EncodedJSValue JSC_HOST_CALL constructPromise(ExecState*);
79
80JSPromiseConstructor::JSPromiseConstructor(VM& vm, Structure* structure)
81 : Base(vm, structure, callPromise, constructPromise)
82{
83}
84
85JSPromiseConstructor::JSPromiseConstructor(VM& vm, Structure* structure, NativeFunction functionForCall, NativeFunction functionForConstruct)
86 : Base(vm, structure, functionForCall, functionForConstruct)
87{
88}
89
90void JSPromiseConstructor::finishCreation(VM& vm, JSPromisePrototype* promisePrototype, GetterSetter* speciesSymbol)
91{
92 Base::finishCreation(vm, vm.propertyNames->Promise.string(), NameVisibility::Visible, NameAdditionMode::WithoutStructureTransition);
93 putDirectWithoutTransition(vm, vm.propertyNames->prototype, promisePrototype, PropertyAttribute::DontEnum | PropertyAttribute::DontDelete | PropertyAttribute::ReadOnly);
94 putDirectWithoutTransition(vm, vm.propertyNames->length, jsNumber(1), PropertyAttribute::DontEnum | PropertyAttribute::ReadOnly);
95 putDirectNonIndexAccessorWithoutTransition(vm, vm.propertyNames->speciesSymbol, speciesSymbol, PropertyAttribute::Accessor | PropertyAttribute::ReadOnly | PropertyAttribute::DontEnum);
96}
97
98void JSPromiseConstructor::addOwnInternalSlots(VM& vm, JSGlobalObject* globalObject)
99{
100 JSC_BUILTIN_FUNCTION_WITHOUT_TRANSITION(vm.propertyNames->builtinNames().resolvePrivateName(), promiseConstructorResolveCodeGenerator, PropertyAttribute::DontEnum | PropertyAttribute::DontDelete | PropertyAttribute::ReadOnly);
101 JSC_BUILTIN_FUNCTION_WITHOUT_TRANSITION(vm.propertyNames->builtinNames().rejectPrivateName(), promiseConstructorRejectCodeGenerator, PropertyAttribute::DontEnum | PropertyAttribute::DontDelete | PropertyAttribute::ReadOnly);
102}
103
104static EncodedJSValue JSC_HOST_CALL constructPromise(ExecState* exec)
105{
106 VM& vm = exec->vm();
107 auto scope = DECLARE_THROW_SCOPE(vm);
108 JSGlobalObject* globalObject = exec->jsCallee()->globalObject(vm);
109
110 JSValue newTarget = exec->newTarget();
111 if (newTarget.isUndefined())
112 return throwVMTypeError(exec, scope);
113
114 Structure* promiseStructure = InternalFunction::createSubclassStructure(exec, exec->newTarget(), globalObject->promiseStructure());
115 RETURN_IF_EXCEPTION(scope, encodedJSValue());
116 JSPromise* promise = JSPromise::create(vm, promiseStructure);
117 promise->initialize(exec, globalObject, exec->argument(0));
118 RETURN_IF_EXCEPTION(scope, encodedJSValue());
119
120 return JSValue::encode(promise);
121}
122
123static EncodedJSValue JSC_HOST_CALL callPromise(ExecState* exec)
124{
125 VM& vm = exec->vm();
126 auto scope = DECLARE_THROW_SCOPE(vm);
127 return JSValue::encode(throwConstructorCannotBeCalledAsFunctionTypeError(exec, scope, "Promise"));
128}
129
130} // namespace JSC
131