2 * Copyright (C) 2009 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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
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.
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.
32 #include "ScriptFunctionCall.h"
34 #include "ScriptScope.h"
35 #include "ScriptState.h"
36 #include "ScriptValue.h"
38 #include "V8Binding.h"
40 #include "V8Utilities.h"
43 #include <wtf/OwnArrayPtr.h>
47 void ScriptCallArgumentHandler::appendArgument(const ScriptObject& argument)
49 if (argument.scriptState() != m_scriptState) {
53 m_arguments.append(argument);
56 void ScriptCallArgumentHandler::appendArgument(const ScriptValue& argument)
58 m_arguments.append(argument);
61 void ScriptCallArgumentHandler::appendArgument(const String& argument)
63 ScriptScope scope(m_scriptState);
64 m_arguments.append(v8String(argument));
67 void ScriptCallArgumentHandler::appendArgument(const char* argument)
69 ScriptScope scope(m_scriptState);
70 m_arguments.append(v8String(argument));
73 void ScriptCallArgumentHandler::appendArgument(long argument)
75 ScriptScope scope(m_scriptState);
76 m_arguments.append(v8::Number::New(argument));
79 void ScriptCallArgumentHandler::appendArgument(long long argument)
81 ScriptScope scope(m_scriptState);
82 m_arguments.append(v8::Number::New(argument));
85 void ScriptCallArgumentHandler::appendArgument(unsigned int argument)
87 ScriptScope scope(m_scriptState);
88 m_arguments.append(v8::Number::New(argument));
91 void ScriptCallArgumentHandler::appendArgument(unsigned long argument)
93 ScriptScope scope(m_scriptState);
94 m_arguments.append(v8::Number::New(argument));
97 void ScriptCallArgumentHandler::appendArgument(int argument)
99 ScriptScope scope(m_scriptState);
100 m_arguments.append(v8::Number::New(argument));
103 void ScriptCallArgumentHandler::appendArgument(bool argument)
105 m_arguments.append(v8Boolean(argument));
108 ScriptFunctionCall::ScriptFunctionCall(const ScriptObject& thisObject, const String& name)
109 : ScriptCallArgumentHandler(thisObject.scriptState())
110 , m_thisObject(thisObject)
115 ScriptValue ScriptFunctionCall::call(bool& hadException, bool reportExceptions)
117 ScriptScope scope(m_scriptState, reportExceptions);
119 v8::Local<v8::Object> thisObject = m_thisObject.v8Object();
120 v8::Local<v8::Value> value = thisObject->Get(v8String(m_name));
121 if (!scope.success()) {
123 return ScriptValue();
126 ASSERT(value->IsFunction());
128 v8::Local<v8::Function> function(v8::Function::Cast(*value));
129 OwnArrayPtr<v8::Handle<v8::Value> > args = adoptArrayPtr(new v8::Handle<v8::Value>[m_arguments.size()]);
130 for (size_t i = 0; i < m_arguments.size(); ++i)
131 args[i] = m_arguments[i].v8Value();
133 v8::Local<v8::Value> result = function->Call(thisObject, m_arguments.size(), args.get());
134 if (!scope.success()) {
136 return ScriptValue();
139 return ScriptValue(result);
142 ScriptValue ScriptFunctionCall::call()
144 bool hadException = false;
145 return call(hadException);
148 ScriptObject ScriptFunctionCall::construct(bool& hadException, bool reportExceptions)
150 ScriptScope scope(m_scriptState, reportExceptions);
152 v8::Local<v8::Object> thisObject = m_thisObject.v8Object();
153 v8::Local<v8::Value> value = thisObject->Get(v8String(m_name));
154 if (!scope.success()) {
156 return ScriptObject();
159 ASSERT(value->IsFunction());
161 v8::Local<v8::Function> constructor(v8::Function::Cast(*value));
162 OwnArrayPtr<v8::Handle<v8::Value> > args = adoptArrayPtr(new v8::Handle<v8::Value>[m_arguments.size()]);
163 for (size_t i = 0; i < m_arguments.size(); ++i)
164 args[i] = m_arguments[i].v8Value();
166 v8::Local<v8::Object> result = SafeAllocation::newInstance(constructor, m_arguments.size(), args.get());
167 if (!scope.success()) {
169 return ScriptObject();
172 return ScriptObject(m_scriptState, result);
175 ScriptCallback::ScriptCallback(ScriptState* state, ScriptValue function)
176 : ScriptCallArgumentHandler(state)
177 , m_function(function)
181 ScriptValue ScriptCallback::call()
183 bool hadException = false;
184 return call(hadException);
187 ScriptValue ScriptCallback::call(bool& hadException)
189 ASSERT(v8::Context::InContext());
190 ASSERT(m_function.v8Value()->IsFunction());
192 v8::TryCatch exceptionCatcher;
193 v8::Handle<v8::Object> object = v8::Context::GetCurrent()->Global();
194 v8::Handle<v8::Function> function = v8::Handle<v8::Function>::Cast(m_function.v8Value());
196 OwnArrayPtr<v8::Handle<v8::Value> > args = adoptArrayPtr(new v8::Handle<v8::Value>[m_arguments.size()]);
197 for (size_t i = 0; i < m_arguments.size(); ++i)
198 args[i] = m_arguments[i].v8Value();
200 v8::Handle<v8::Value> result = V8Proxy::instrumentedCallFunction(0 /* page */, function, object, m_arguments.size(), args.get());
202 if (exceptionCatcher.HasCaught()) {
204 m_scriptState->setException(exceptionCatcher.Exception());
205 return ScriptValue();
208 return ScriptValue(result);
211 } // namespace WebCore