Upstream version 5.34.92.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / bindings / v8 / ScriptFunctionCall.cpp
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 "bindings/v8/ScriptFunctionCall.h"
33
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"
42
43 #include <v8.h>
44
45 namespace WebCore {
46
47 void ScriptCallArgumentHandler::appendArgument(const ScriptObject& argument)
48 {
49     if (argument.scriptState() != m_scriptState) {
50         ASSERT_NOT_REACHED();
51         return;
52     }
53     m_arguments.append(argument);
54 }
55
56 void ScriptCallArgumentHandler::appendArgument(const ScriptValue& argument)
57 {
58     m_arguments.append(argument);
59 }
60
61 void ScriptCallArgumentHandler::appendArgument(const String& argument)
62 {
63     v8::Isolate* isolate = m_scriptState->isolate();
64     ScriptScope scope(m_scriptState);
65     m_arguments.append(ScriptValue(v8String(isolate, argument), isolate));
66 }
67
68 void ScriptCallArgumentHandler::appendArgument(const char* argument)
69 {
70     v8::Isolate* isolate = m_scriptState->isolate();
71     ScriptScope scope(m_scriptState);
72     m_arguments.append(ScriptValue(v8String(isolate, argument), isolate));
73 }
74
75 void ScriptCallArgumentHandler::appendArgument(long argument)
76 {
77     v8::Isolate* isolate = m_scriptState->isolate();
78     ScriptScope scope(m_scriptState);
79     m_arguments.append(ScriptValue(v8::Number::New(isolate, argument), isolate));
80 }
81
82 void ScriptCallArgumentHandler::appendArgument(long long argument)
83 {
84     v8::Isolate* isolate = m_scriptState->isolate();
85     ScriptScope scope(m_scriptState);
86     m_arguments.append(ScriptValue(v8::Number::New(isolate, argument), isolate));
87 }
88
89 void ScriptCallArgumentHandler::appendArgument(unsigned int argument)
90 {
91     v8::Isolate* isolate = m_scriptState->isolate();
92     ScriptScope scope(m_scriptState);
93     m_arguments.append(ScriptValue(v8::Number::New(isolate, argument), isolate));
94 }
95
96 void ScriptCallArgumentHandler::appendArgument(unsigned long argument)
97 {
98     v8::Isolate* isolate = m_scriptState->isolate();
99     ScriptScope scope(m_scriptState);
100     m_arguments.append(ScriptValue(v8::Number::New(isolate, argument), isolate));
101 }
102
103 void ScriptCallArgumentHandler::appendArgument(int argument)
104 {
105     v8::Isolate* isolate = m_scriptState->isolate();
106     ScriptScope scope(m_scriptState);
107     m_arguments.append(ScriptValue(v8::Number::New(isolate, argument), isolate));
108 }
109
110 void ScriptCallArgumentHandler::appendArgument(bool argument)
111 {
112     v8::Isolate* isolate = m_scriptState->isolate();
113     m_arguments.append(ScriptValue(v8Boolean(argument, isolate), isolate));
114 }
115
116 void ScriptCallArgumentHandler::appendArgument(const Vector<ScriptValue>& argument)
117 {
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));
124 }
125
126 ScriptFunctionCall::ScriptFunctionCall(const ScriptObject& thisObject, const String& name)
127     : ScriptCallArgumentHandler(thisObject.scriptState())
128     , m_thisObject(thisObject)
129     , m_name(name)
130 {
131 }
132
133 ScriptValue ScriptFunctionCall::call(bool& hadException, bool reportExceptions)
134 {
135     ScriptScope scope(m_scriptState, reportExceptions);
136
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()) {
140         hadException = true;
141         return ScriptValue();
142     }
143
144     ASSERT(value->IsFunction());
145
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());
151     }
152
153     v8::Local<v8::Value> result = V8ScriptRunner::callFunction(function, getExecutionContext(), thisObject, m_arguments.size(), info.get(), m_scriptState->isolate());
154     if (!scope.success()) {
155         hadException = true;
156         return ScriptValue();
157     }
158
159     return ScriptValue(result, m_scriptState->isolate());
160 }
161
162 ScriptValue ScriptFunctionCall::call()
163 {
164     bool hadException = false;
165     return call(hadException);
166 }
167
168 ScriptObject ScriptFunctionCall::construct(bool& hadException, bool reportExceptions)
169 {
170     ScriptScope scope(m_scriptState, reportExceptions);
171
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()) {
175         hadException = true;
176         return ScriptObject();
177     }
178
179     ASSERT(value->IsFunction());
180
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();
185
186     v8::Local<v8::Object> result = V8ObjectConstructor::newInstance(constructor, m_arguments.size(), info.get());
187     if (!scope.success()) {
188         hadException = true;
189         return ScriptObject();
190     }
191
192     return ScriptObject(m_scriptState, result);
193 }
194
195 ScriptCallback::ScriptCallback(ScriptState* state, const ScriptValue& function)
196     : ScriptCallArgumentHandler(state)
197     , m_scriptState(state)
198     , m_function(function)
199 {
200 }
201
202 ScriptValue ScriptCallback::call()
203 {
204     v8::Isolate* isolate = v8::Isolate::GetCurrent();
205     ASSERT(isolate->InContext());
206     ASSERT(m_function.v8Value()->IsFunction());
207
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());
212
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();
216
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());
219 }
220
221 } // namespace WebCore