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 "bindings/v8/ScriptFunctionCall.h"
34 #include "bindings/v8/ScriptController.h"
35 #include "bindings/v8/ScriptScope.h"
36 #include "bindings/v8/ScriptState.h"
37 #include "bindings/v8/ScriptValue.h"
38 #include "bindings/v8/V8Binding.h"
39 #include "bindings/v8/V8ObjectConstructor.h"
40 #include "bindings/v8/V8ScriptRunner.h"
41 #include "bindings/v8/V8Utilities.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 v8::Isolate* isolate = m_scriptState->isolate();
64 ScriptScope scope(m_scriptState);
65 m_arguments.append(ScriptValue(v8String(argument, isolate), isolate));
68 void ScriptCallArgumentHandler::appendArgument(const char* argument)
70 v8::Isolate* isolate = m_scriptState->isolate();
71 ScriptScope scope(m_scriptState);
72 m_arguments.append(ScriptValue(v8String(argument, isolate), isolate));
75 void ScriptCallArgumentHandler::appendArgument(long argument)
77 v8::Isolate* isolate = m_scriptState->isolate();
78 ScriptScope scope(m_scriptState);
79 m_arguments.append(ScriptValue(v8::Number::New(isolate, argument), isolate));
82 void ScriptCallArgumentHandler::appendArgument(long long argument)
84 v8::Isolate* isolate = m_scriptState->isolate();
85 ScriptScope scope(m_scriptState);
86 m_arguments.append(ScriptValue(v8::Number::New(isolate, argument), isolate));
89 void ScriptCallArgumentHandler::appendArgument(unsigned int argument)
91 v8::Isolate* isolate = m_scriptState->isolate();
92 ScriptScope scope(m_scriptState);
93 m_arguments.append(ScriptValue(v8::Number::New(isolate, argument), isolate));
96 void ScriptCallArgumentHandler::appendArgument(unsigned long argument)
98 v8::Isolate* isolate = m_scriptState->isolate();
99 ScriptScope scope(m_scriptState);
100 m_arguments.append(ScriptValue(v8::Number::New(isolate, argument), isolate));
103 void ScriptCallArgumentHandler::appendArgument(int argument)
105 v8::Isolate* isolate = m_scriptState->isolate();
106 ScriptScope scope(m_scriptState);
107 m_arguments.append(ScriptValue(v8::Number::New(isolate, argument), isolate));
110 void ScriptCallArgumentHandler::appendArgument(bool argument)
112 v8::Isolate* isolate = m_scriptState->isolate();
113 m_arguments.append(ScriptValue(v8Boolean(argument, isolate), isolate));
116 ScriptFunctionCall::ScriptFunctionCall(const ScriptObject& thisObject, const String& name)
117 : ScriptCallArgumentHandler(thisObject.scriptState())
118 , m_thisObject(thisObject)
123 ScriptValue ScriptFunctionCall::call(bool& hadException, bool reportExceptions)
125 ScriptScope scope(m_scriptState, reportExceptions);
127 v8::Handle<v8::Object> thisObject = m_thisObject.v8Object();
128 v8::Local<v8::Value> value = thisObject->Get(v8String(m_name, m_scriptState->isolate()));
129 if (!scope.success()) {
131 return ScriptValue();
134 ASSERT(value->IsFunction());
136 v8::Local<v8::Function> function = v8::Local<v8::Function>::Cast(value);
137 OwnPtr<v8::Handle<v8::Value>[]> info = adoptArrayPtr(new v8::Handle<v8::Value>[m_arguments.size()]);
138 for (size_t i = 0; i < m_arguments.size(); ++i) {
139 info[i] = m_arguments[i].v8Value();
140 ASSERT(!info[i].IsEmpty());
143 v8::Local<v8::Value> result = V8ScriptRunner::callFunction(function, getExecutionContext(), thisObject, m_arguments.size(), info.get(), m_scriptState->isolate());
144 if (!scope.success()) {
146 return ScriptValue();
149 return ScriptValue(result, m_scriptState->isolate());
152 ScriptValue ScriptFunctionCall::call()
154 bool hadException = false;
155 return call(hadException);
158 ScriptObject ScriptFunctionCall::construct(bool& hadException, bool reportExceptions)
160 ScriptScope scope(m_scriptState, reportExceptions);
162 v8::Handle<v8::Object> thisObject = m_thisObject.v8Object();
163 v8::Local<v8::Value> value = thisObject->Get(v8String(m_name, m_scriptState->isolate()));
164 if (!scope.success()) {
166 return ScriptObject();
169 ASSERT(value->IsFunction());
171 v8::Local<v8::Function> constructor = v8::Local<v8::Function>::Cast(value);
172 OwnPtr<v8::Handle<v8::Value>[]> info = adoptArrayPtr(new v8::Handle<v8::Value>[m_arguments.size()]);
173 for (size_t i = 0; i < m_arguments.size(); ++i)
174 info[i] = m_arguments[i].v8Value();
176 v8::Local<v8::Object> result = V8ObjectConstructor::newInstance(constructor, m_arguments.size(), info.get());
177 if (!scope.success()) {
179 return ScriptObject();
182 return ScriptObject(m_scriptState, result);
185 ScriptCallback::ScriptCallback(ScriptState* state, const ScriptValue& function)
186 : ScriptCallArgumentHandler(state)
187 , m_scriptState(state)
188 , m_function(function)
192 ScriptValue ScriptCallback::call()
194 ASSERT(v8::Context::InContext());
195 ASSERT(m_function.v8Value()->IsFunction());
197 v8::TryCatch exceptionCatcher;
198 exceptionCatcher.SetVerbose(true);
199 v8::Handle<v8::Object> object = v8::Context::GetCurrent()->Global();
200 v8::Handle<v8::Function> function = v8::Handle<v8::Function>::Cast(m_function.v8Value());
202 OwnPtr<v8::Handle<v8::Value>[]> info = adoptArrayPtr(new v8::Handle<v8::Value>[m_arguments.size()]);
203 for (size_t i = 0; i < m_arguments.size(); ++i)
204 info[i] = m_arguments[i].v8Value();
206 v8::Handle<v8::Value> result = ScriptController::callFunction(m_scriptState->executionContext(), function, object, m_arguments.size(), info.get(), m_scriptState->isolate());
207 return ScriptValue(result, m_scriptState->isolate());
210 } // namespace WebCore