1/*
2 * Copyright (C) 2016 Canon Inc.
3 * Copyright (C) 2016-2017 Apple Inc. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted, provided that the following conditions
7 * are required to be met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. Neither the name of Canon Inc. nor the names of
15 * its contributors may be used to endorse or promote products derived
16 * from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY CANON INC. AND ITS CONTRIBUTORS "AS IS" AND ANY
19 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL CANON INC. AND ITS CONTRIBUTORS BE LIABLE FOR
22 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30
31#include "config.h"
32#include "ReadableStreamDefaultController.h"
33
34#if ENABLE(STREAMS_API)
35
36#include "WebCoreJSClientData.h"
37#include <JavaScriptCore/CatchScope.h>
38#include <JavaScriptCore/HeapInlines.h>
39#include <JavaScriptCore/IdentifierInlines.h>
40#include <JavaScriptCore/JSObjectInlines.h>
41
42namespace WebCore {
43
44static inline JSC::JSValue callFunction(JSC::ExecState& state, JSC::JSValue jsFunction, JSC::JSValue thisValue, const JSC::ArgList& arguments)
45{
46 JSC::CallData callData;
47 auto callType = JSC::getCallData(state.vm(), jsFunction, callData);
48 return call(&state, jsFunction, callType, callData, thisValue, arguments);
49}
50
51JSC::JSValue ReadableStreamDefaultController::invoke(JSC::ExecState& state, JSC::JSObject& object, const char* propertyName, JSC::JSValue parameter)
52{
53 JSC::VM& vm = state.vm();
54 JSC::JSLockHolder lock(vm);
55 auto scope = DECLARE_THROW_SCOPE(vm);
56
57 auto function = object.get(&state, JSC::Identifier::fromString(&state, propertyName));
58 RETURN_IF_EXCEPTION(scope, JSC::JSValue());
59
60 if (!function.isFunction(vm)) {
61 if (!function.isUndefined())
62 throwTypeError(&state, scope, "ReadableStream trying to call a property that is not callable"_s);
63 return JSC::jsUndefined();
64 }
65
66 JSC::MarkedArgumentBuffer arguments;
67 arguments.append(parameter);
68 ASSERT(!arguments.hasOverflowed());
69
70 return callFunction(state, function, &object, arguments);
71}
72
73} // namespace WebCore
74
75#endif // ENABLE(STREAMS_API)
76