| 1 | /* |
| 2 | * Copyright (C) 2013-2019 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 | |
| 39 | using namespace JSC; |
| 40 | |
| 41 | namespace Deprecated { |
| 42 | |
| 43 | void ScriptCallArgumentHandler::appendArgument(const String& argument) |
| 44 | { |
| 45 | VM& vm = m_globalObject->vm(); |
| 46 | JSLockHolder lock(vm); |
| 47 | m_arguments.append(jsString(vm, argument)); |
| 48 | } |
| 49 | |
| 50 | void ScriptCallArgumentHandler::appendArgument(const char* argument) |
| 51 | { |
| 52 | VM& vm = m_globalObject->vm(); |
| 53 | JSLockHolder lock(vm); |
| 54 | m_arguments.append(jsString(vm, String(argument))); |
| 55 | } |
| 56 | |
| 57 | void ScriptCallArgumentHandler::appendArgument(JSValue argument) |
| 58 | { |
| 59 | m_arguments.append(argument); |
| 60 | } |
| 61 | |
| 62 | void ScriptCallArgumentHandler::appendArgument(long argument) |
| 63 | { |
| 64 | JSLockHolder lock(m_globalObject); |
| 65 | m_arguments.append(jsNumber(argument)); |
| 66 | } |
| 67 | |
| 68 | void ScriptCallArgumentHandler::appendArgument(long long argument) |
| 69 | { |
| 70 | JSLockHolder lock(m_globalObject); |
| 71 | m_arguments.append(jsNumber(argument)); |
| 72 | } |
| 73 | |
| 74 | void ScriptCallArgumentHandler::appendArgument(unsigned int argument) |
| 75 | { |
| 76 | JSLockHolder lock(m_globalObject); |
| 77 | m_arguments.append(jsNumber(argument)); |
| 78 | } |
| 79 | |
| 80 | void ScriptCallArgumentHandler::appendArgument(uint64_t argument) |
| 81 | { |
| 82 | JSLockHolder lock(m_globalObject); |
| 83 | m_arguments.append(jsNumber(argument)); |
| 84 | } |
| 85 | |
| 86 | void ScriptCallArgumentHandler::appendArgument(int argument) |
| 87 | { |
| 88 | JSLockHolder lock(m_globalObject); |
| 89 | m_arguments.append(jsNumber(argument)); |
| 90 | } |
| 91 | |
| 92 | void ScriptCallArgumentHandler::appendArgument(bool argument) |
| 93 | { |
| 94 | m_arguments.append(jsBoolean(argument)); |
| 95 | } |
| 96 | |
| 97 | ScriptFunctionCall::ScriptFunctionCall(const Deprecated::ScriptObject& thisObject, const String& name, ScriptFunctionCallHandler callHandler) |
| 98 | : ScriptCallArgumentHandler(thisObject.globalObject()) |
| 99 | , m_callHandler(callHandler) |
| 100 | , m_thisObject(thisObject) |
| 101 | , m_name(name) |
| 102 | { |
| 103 | } |
| 104 | |
| 105 | Expected<JSValue, NakedPtr<Exception>> ScriptFunctionCall::call() |
| 106 | { |
| 107 | JSObject* thisObject = m_thisObject.jsObject(); |
| 108 | |
| 109 | VM& vm = m_globalObject->vm(); |
| 110 | JSLockHolder lock(vm); |
| 111 | auto scope = DECLARE_THROW_SCOPE(vm); |
| 112 | |
| 113 | JSValue function = thisObject->get(m_globalObject, Identifier::fromString(vm, m_name)); |
| 114 | if (UNLIKELY(scope.exception())) |
| 115 | return makeUnexpected(scope.exception()); |
| 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_globalObject, function, callType, callData, thisObject, m_arguments, exception); |
| 126 | else |
| 127 | result = JSC::call(m_globalObject, 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 | if (!isTerminatedExecutionException(vm, exception)) |
| 132 | return makeUnexpected(exception); |
| 133 | return { }; |
| 134 | } |
| 135 | |
| 136 | return result; |
| 137 | } |
| 138 | |
| 139 | } // namespace Deprecated |
| 140 | |