1 /* 2 * Copyright (C) 2009 Google 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 are 6 * met: 7 * 8 * * Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * * Redistributions in binary form must reproduce the above 11 * copyright notice, this list of conditions and the following disclaimer 12 * in the documentation and/or other materials provided with the 13 * distribution. 14 * * Neither the name of Google Inc. nor the names of its 15 * contributors may be used to endorse or promote products derived from 16 * this software without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 */ 30 31 #include "config.h" 32 #include "ScriptFunctionCall.h" 33 34 #include "JSDOMBinding.h" 35 #include "ScriptString.h" 36 #include "ScriptValue.h" 37 38 #include <runtime/JSLock.h> 39 #include <runtime/UString.h> 40 41 using namespace JSC; 42 43 namespace WebCore { 44 45 ScriptFunctionCall::ScriptFunctionCall(const ScriptObject& thisObject, const String& name) 46 : m_exec(thisObject.scriptState()) 47 , m_thisObject(thisObject) 48 , m_name(name) 49 { 50 } 51 52 void ScriptFunctionCall::appendArgument(const ScriptObject& argument) 53 { 54 if (argument.scriptState() != m_exec) { 55 ASSERT_NOT_REACHED(); 56 return; 57 } 58 m_arguments.append(argument.jsObject()); 59 } 60 61 void ScriptFunctionCall::appendArgument(const ScriptString& argument) 62 { 63 m_arguments.append(jsString(m_exec, argument)); 64 } 65 66 void ScriptFunctionCall::appendArgument(const ScriptValue& argument) 67 { 68 m_arguments.append(argument.jsValue()); 69 } 70 71 void ScriptFunctionCall::appendArgument(const String& argument) 72 { 73 JSLock lock(SilenceAssertionsOnly); 74 m_arguments.append(jsString(m_exec, argument)); 75 } 76 77 void ScriptFunctionCall::appendArgument(const JSC::UString& argument) 78 { 79 JSLock lock(SilenceAssertionsOnly); 80 m_arguments.append(jsString(m_exec, argument)); 81 } 82 83 void ScriptFunctionCall::appendArgument(const char* argument) 84 { 85 JSLock lock(SilenceAssertionsOnly); 86 m_arguments.append(jsString(m_exec, UString(argument))); 87 } 88 89 void ScriptFunctionCall::appendArgument(JSC::JSValue argument) 90 { 91 m_arguments.append(argument); 92 } 93 94 void ScriptFunctionCall::appendArgument(long argument) 95 { 96 JSLock lock(SilenceAssertionsOnly); 97 m_arguments.append(jsNumber(m_exec, argument)); 98 } 99 100 void ScriptFunctionCall::appendArgument(long long argument) 101 { 102 JSLock lock(SilenceAssertionsOnly); 103 m_arguments.append(jsNumber(m_exec, argument)); 104 } 105 106 void ScriptFunctionCall::appendArgument(unsigned int argument) 107 { 108 JSLock lock(SilenceAssertionsOnly); 109 m_arguments.append(jsNumber(m_exec, argument)); 110 } 111 112 void ScriptFunctionCall::appendArgument(unsigned long argument) 113 { 114 JSLock lock(SilenceAssertionsOnly); 115 m_arguments.append(jsNumber(m_exec, argument)); 116 } 117 118 void ScriptFunctionCall::appendArgument(int argument) 119 { 120 JSLock lock(SilenceAssertionsOnly); 121 m_arguments.append(jsNumber(m_exec, argument)); 122 } 123 124 void ScriptFunctionCall::appendArgument(bool argument) 125 { 126 m_arguments.append(jsBoolean(argument)); 127 } 128 129 ScriptValue ScriptFunctionCall::call(bool& hadException, bool reportExceptions) 130 { 131 JSObject* thisObject = m_thisObject.jsObject(); 132 133 JSLock lock(SilenceAssertionsOnly); 134 135 JSValue function = thisObject->get(m_exec, Identifier(m_exec, m_name)); 136 if (m_exec->hadException()) { 137 if (reportExceptions) 138 reportException(m_exec, m_exec->exception()); 139 140 hadException = true; 141 return ScriptValue(); 142 } 143 144 CallData callData; 145 CallType callType = function.getCallData(callData); 146 if (callType == CallTypeNone) 147 return ScriptValue(); 148 149 JSValue result = JSC::call(m_exec, function, callType, callData, thisObject, m_arguments); 150 if (m_exec->hadException()) { 151 if (reportExceptions) 152 reportException(m_exec, m_exec->exception()); 153 154 hadException = true; 155 return ScriptValue(); 156 } 157 158 return ScriptValue(result); 159 } 160 161 ScriptValue ScriptFunctionCall::call() 162 { 163 bool hadException = false; 164 return call(hadException); 165 } 166 167 ScriptObject ScriptFunctionCall::construct(bool& hadException, bool reportExceptions) 168 { 169 JSObject* thisObject = m_thisObject.jsObject(); 170 171 JSLock lock(SilenceAssertionsOnly); 172 173 JSObject* constructor = asObject(thisObject->get(m_exec, Identifier(m_exec, m_name))); 174 if (m_exec->hadException()) { 175 if (reportExceptions) 176 reportException(m_exec, m_exec->exception()); 177 178 hadException = true; 179 return ScriptObject(); 180 } 181 182 ConstructData constructData; 183 ConstructType constructType = constructor->getConstructData(constructData); 184 if (constructType == ConstructTypeNone) 185 return ScriptObject(); 186 187 JSValue result = JSC::construct(m_exec, constructor, constructType, constructData, m_arguments); 188 if (m_exec->hadException()) { 189 if (reportExceptions) 190 reportException(m_exec, m_exec->exception()); 191 192 hadException = true; 193 return ScriptObject(); 194 } 195 196 return ScriptObject(m_exec, asObject(result)); 197 } 198 199 } // namespace WebCore 200