1/*
2 * Copyright (C) 1999-2001 Harri Porten (porten@kde.org)
3 * Copyright (C) 2003-2006, 2008-2009, 2013, 2016 Apple Inc. All rights reserved.
4 * Copyright (C) 2007 Samuel Weinig <sam@webkit.org>
5 * Copyright (C) 2009 Google, Inc. All rights reserved.
6 * Copyright (C) 2012 Ericsson AB. All rights reserved.
7 * Copyright (C) 2013 Michael Pruett <michael@68k.org>
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
24#pragma once
25
26#include "JSDOMOperation.h"
27#include "JSDOMPromiseDeferred.h"
28
29namespace WebCore {
30
31template<typename JSClass>
32class IDLOperationReturningPromise {
33public:
34 using ClassParameter = JSClass*;
35 using Operation = JSC::EncodedJSValue(JSC::ExecState*, ClassParameter, Ref<DeferredPromise>&&, JSC::ThrowScope&);
36 using StaticOperation = JSC::EncodedJSValue(JSC::ExecState*, Ref<DeferredPromise>&&, JSC::ThrowScope&);
37
38 template<Operation operation, PromiseExecutionScope executionScope, CastedThisErrorBehavior shouldThrow = CastedThisErrorBehavior::RejectPromise>
39 static JSC::EncodedJSValue call(JSC::ExecState& state, const char* operationName)
40 {
41 return JSC::JSValue::encode(callPromiseFunction<executionScope>(state, [&operationName] (JSC::ExecState& state, Ref<DeferredPromise>&& promise) {
42 auto throwScope = DECLARE_THROW_SCOPE(state.vm());
43
44 auto* thisObject = IDLOperation<JSClass>::cast(state);
45 if (shouldThrow != CastedThisErrorBehavior::Assert && UNLIKELY(!thisObject))
46 return rejectPromiseWithThisTypeError(promise.get(), JSClass::info()->className, operationName);
47
48 ASSERT(thisObject);
49 ASSERT_GC_OBJECT_INHERITS(thisObject, JSClass::info());
50
51 // FIXME: We should refactor the binding generated code to use references for state and thisObject.
52 return operation(&state, thisObject, WTFMove(promise), throwScope);
53 }));
54 }
55
56 // This function is a special case for custom operations want to handle the creation of the promise themselves.
57 // It is triggered via the extended attribute [ReturnsOwnPromise].
58 template<typename IDLOperation<JSClass>::Operation operation, CastedThisErrorBehavior shouldThrow = CastedThisErrorBehavior::RejectPromise>
59 static JSC::EncodedJSValue callReturningOwnPromise(JSC::ExecState& state, const char* operationName)
60 {
61 auto throwScope = DECLARE_THROW_SCOPE(state.vm());
62
63 auto* thisObject = IDLOperation<JSClass>::cast(state);
64 if (shouldThrow != CastedThisErrorBehavior::Assert && UNLIKELY(!thisObject))
65 return rejectPromiseWithThisTypeError(state, JSClass::info()->className, operationName);
66
67 ASSERT(thisObject);
68 ASSERT_GC_OBJECT_INHERITS(thisObject, JSClass::info());
69
70 // FIXME: We should refactor the binding generated code to use references for state and thisObject.
71 return operation(&state, thisObject, throwScope);
72 }
73
74 template<StaticOperation operation, PromiseExecutionScope executionScope, CastedThisErrorBehavior shouldThrow = CastedThisErrorBehavior::RejectPromise>
75 static JSC::EncodedJSValue callStatic(JSC::ExecState& state, const char*)
76 {
77 return JSC::JSValue::encode(callPromiseFunction<executionScope>(state, [] (JSC::ExecState& state, Ref<DeferredPromise>&& promise) {
78 auto throwScope = DECLARE_THROW_SCOPE(state.vm());
79
80 // FIXME: We should refactor the binding generated code to use references for state.
81 return operation(&state, WTFMove(promise), throwScope);
82 }));
83 }
84
85 // This function is a special case for custom operations want to handle the creation of the promise themselves.
86 // It is triggered via the extended attribute [ReturnsOwnPromise].
87 template<typename IDLOperation<JSClass>::StaticOperation operation, CastedThisErrorBehavior shouldThrow = CastedThisErrorBehavior::RejectPromise>
88 static JSC::EncodedJSValue callStaticReturningOwnPromise(JSC::ExecState& state, const char*)
89 {
90 auto throwScope = DECLARE_THROW_SCOPE(state.vm());
91
92 // FIXME: We should refactor the binding generated code to use references for state.
93 return operation(&state, throwScope);
94 }
95};
96
97} // namespace WebCore
98