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(isolate, argument), 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(isolate, argument), 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 void ScriptCallArgumentHandler::appendArgument(const Vector<ScriptValue>& argument)
118 v8::Isolate* isolate = m_scriptState->isolate();
119 ScriptScope scope(m_scriptState);
120 v8::Handle<v8::Array> result = v8::Array::New(isolate, argument.size());
121 for (size_t i = 0; i < argument.size(); ++i)
122 result->Set(v8::Integer::New(isolate, i), argument[i].v8Value());
123 m_arguments.append(ScriptValue(result, isolate));
126 ScriptFunctionCall::ScriptFunctionCall(const ScriptObject& thisObject, const String& name)
127 : ScriptCallArgumentHandler(thisObject.scriptState())
128 , m_thisObject(thisObject)
133 ScriptValue ScriptFunctionCall::call(bool& hadException, bool reportExceptions)
135 ScriptScope scope(m_scriptState, reportExceptions);
137 v8::Handle<v8::Object> thisObject = m_thisObject.v8Object();
138 v8::Local<v8::Value> value = thisObject->Get(v8String(m_scriptState->isolate(), m_name));
139 if (!scope.success()) {
141 return ScriptValue();
144 ASSERT(value->IsFunction());
146 v8::Local<v8::Function> function = v8::Local<v8::Function>::Cast(value);
147 OwnPtr<v8::Handle<v8::Value>[]> info = adoptArrayPtr(new v8::Handle<v8::Value>[m_arguments.size()]);
148 for (size_t i = 0; i < m_arguments.size(); ++i) {
149 info[i] = m_arguments[i].v8Value();
150 ASSERT(!info[i].IsEmpty());
153 v8::Local<v8::Value> result = V8ScriptRunner::callFunction(function, getExecutionContext(), thisObject, m_arguments.size(), info.get(), m_scriptState->isolate());
154 if (!scope.success()) {
156 return ScriptValue();
159 return ScriptValue(result, m_scriptState->isolate());
162 ScriptValue ScriptFunctionCall::call()
164 bool hadException = false;
165 return call(hadException);
168 ScriptObject ScriptFunctionCall::construct(bool& hadException, bool reportExceptions)
170 ScriptScope scope(m_scriptState, reportExceptions);
172 v8::Handle<v8::Object> thisObject = m_thisObject.v8Object();
173 v8::Local<v8::Value> value = thisObject->Get(v8String(m_scriptState->isolate(), m_name));
174 if (!scope.success()) {
176 return ScriptObject();
179 ASSERT(value->IsFunction());
181 v8::Local<v8::Function> constructor = v8::Local<v8::Function>::Cast(value);
182 OwnPtr<v8::Handle<v8::Value>[]> info = adoptArrayPtr(new v8::Handle<v8::Value>[m_arguments.size()]);
183 for (size_t i = 0; i < m_arguments.size(); ++i)
184 info[i] = m_arguments[i].v8Value();
186 v8::Local<v8::Object> result = V8ObjectConstructor::newInstance(constructor, m_arguments.size(), info.get());
187 if (!scope.success()) {
189 return ScriptObject();
192 return ScriptObject(m_scriptState, result);
195 ScriptCallback::ScriptCallback(ScriptState* state, const ScriptValue& function)
196 : ScriptCallArgumentHandler(state)
197 , m_scriptState(state)
198 , m_function(function)
202 ScriptValue ScriptCallback::call()
204 v8::Isolate* isolate = v8::Isolate::GetCurrent();
205 ASSERT(isolate->InContext());
206 ASSERT(m_function.v8Value()->IsFunction());
208 v8::TryCatch exceptionCatcher;
209 exceptionCatcher.SetVerbose(true);
210 v8::Handle<v8::Object> object = isolate->GetCurrentContext()->Global();
211 v8::Handle<v8::Function> function = v8::Handle<v8::Function>::Cast(m_function.v8Value());
213 OwnPtr<v8::Handle<v8::Value>[]> info = adoptArrayPtr(new v8::Handle<v8::Value>[m_arguments.size()]);
214 for (size_t i = 0; i < m_arguments.size(); ++i)
215 info[i] = m_arguments[i].v8Value();
217 v8::Handle<v8::Value> result = ScriptController::callFunction(m_scriptState->executionContext(), function, object, m_arguments.size(), info.get(), m_scriptState->isolate());
218 return ScriptValue(result, m_scriptState->isolate());
221 } // namespace WebCore