1/*
2 * Copyright (C) 2013 Apple Inc. All rights reserved.
3 * Copyright (C) 2009 Google 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 are
7 * met:
8 *
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following disclaimer
13 * in the documentation and/or other materials provided with the
14 * distribution.
15 * * Neither the name of Google Inc. nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32#include "config.h"
33#include "ScriptFunctionCall.h"
34
35#include "JSCInlines.h"
36#include "JSLock.h"
37#include <wtf/text/WTFString.h>
38
39using namespace JSC;
40
41namespace Deprecated {
42
43void ScriptCallArgumentHandler::appendArgument(const String& argument)
44{
45 JSLockHolder lock(m_exec);
46 m_arguments.append(jsString(m_exec, argument));
47}
48
49void ScriptCallArgumentHandler::appendArgument(const char* argument)
50{
51 JSLockHolder lock(m_exec);
52 m_arguments.append(jsString(m_exec, String(argument)));
53}
54
55void ScriptCallArgumentHandler::appendArgument(JSValue argument)
56{
57 m_arguments.append(argument);
58}
59
60void ScriptCallArgumentHandler::appendArgument(long argument)
61{
62 JSLockHolder lock(m_exec);
63 m_arguments.append(jsNumber(argument));
64}
65
66void ScriptCallArgumentHandler::appendArgument(long long argument)
67{
68 JSLockHolder lock(m_exec);
69 m_arguments.append(jsNumber(argument));
70}
71
72void ScriptCallArgumentHandler::appendArgument(unsigned int argument)
73{
74 JSLockHolder lock(m_exec);
75 m_arguments.append(jsNumber(argument));
76}
77
78void ScriptCallArgumentHandler::appendArgument(uint64_t argument)
79{
80 JSLockHolder lock(m_exec);
81 m_arguments.append(jsNumber(argument));
82}
83
84void ScriptCallArgumentHandler::appendArgument(int argument)
85{
86 JSLockHolder lock(m_exec);
87 m_arguments.append(jsNumber(argument));
88}
89
90void ScriptCallArgumentHandler::appendArgument(bool argument)
91{
92 m_arguments.append(jsBoolean(argument));
93}
94
95ScriptFunctionCall::ScriptFunctionCall(const Deprecated::ScriptObject& thisObject, const String& name, ScriptFunctionCallHandler callHandler)
96 : ScriptCallArgumentHandler(thisObject.scriptState())
97 , m_callHandler(callHandler)
98 , m_thisObject(thisObject)
99 , m_name(name)
100{
101}
102
103JSValue ScriptFunctionCall::call(bool& hadException)
104{
105 JSObject* thisObject = m_thisObject.jsObject();
106
107 VM& vm = m_exec->vm();
108 JSLockHolder lock(vm);
109 auto scope = DECLARE_THROW_SCOPE(vm);
110
111 JSValue function = thisObject->get(m_exec, Identifier::fromString(m_exec, m_name));
112 if (UNLIKELY(scope.exception())) {
113 hadException = true;
114 return { };
115 }
116
117 CallData callData;
118 CallType callType = getCallData(vm, function, callData);
119 if (callType == CallType::None)
120 return { };
121
122 JSValue result;
123 NakedPtr<Exception> exception;
124 if (m_callHandler)
125 result = m_callHandler(m_exec, function, callType, callData, thisObject, m_arguments, exception);
126 else
127 result = JSC::call(m_exec, function, callType, callData, thisObject, m_arguments, exception);
128
129 if (exception) {
130 // Do not treat a terminated execution exception as having an exception. Just treat it as an empty result.
131 hadException = !isTerminatedExecutionException(vm, exception);
132 return { };
133 }
134
135 return result;
136}
137
138JSC::JSValue ScriptFunctionCall::call()
139{
140 bool hadException;
141 return call(hadException);
142}
143
144} // namespace Deprecated
145