• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
appendArgument(const ScriptObject & argument)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 
appendArgument(const ScriptValue & argument)56 void ScriptCallArgumentHandler::appendArgument(const ScriptValue& argument)
57 {
58     m_arguments.append(argument);
59 }
60 
appendArgument(const String & argument)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 
appendArgument(const char * argument)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 
appendArgument(long argument)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 
appendArgument(long long argument)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 
appendArgument(unsigned int argument)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 
appendArgument(unsigned long argument)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 
appendArgument(int argument)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 
appendArgument(bool argument)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 
ScriptFunctionCall(const ScriptObject & thisObject,const String & name)116 ScriptFunctionCall::ScriptFunctionCall(const ScriptObject& thisObject, const String& name)
117     : ScriptCallArgumentHandler(thisObject.scriptState())
118     , m_thisObject(thisObject)
119     , m_name(name)
120 {
121 }
122 
call(bool & hadException,bool reportExceptions)123 ScriptValue ScriptFunctionCall::call(bool& hadException, bool reportExceptions)
124 {
125     ScriptScope scope(m_scriptState, reportExceptions);
126 
127     v8::Handle<v8::Object> thisObject = m_thisObject.v8Object();
128     v8::Local<v8::Value> value = thisObject->Get(v8String(m_scriptState->isolate(), m_name));
129     if (!scope.success()) {
130         hadException = true;
131         return ScriptValue();
132     }
133 
134     ASSERT(value->IsFunction());
135 
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());
141     }
142 
143     v8::Local<v8::Value> result = V8ScriptRunner::callFunction(function, getExecutionContext(), thisObject, m_arguments.size(), info.get(), m_scriptState->isolate());
144     if (!scope.success()) {
145         hadException = true;
146         return ScriptValue();
147     }
148 
149     return ScriptValue(result, m_scriptState->isolate());
150 }
151 
call()152 ScriptValue ScriptFunctionCall::call()
153 {
154     bool hadException = false;
155     return call(hadException);
156 }
157 
construct(bool & hadException,bool reportExceptions)158 ScriptObject ScriptFunctionCall::construct(bool& hadException, bool reportExceptions)
159 {
160     ScriptScope scope(m_scriptState, reportExceptions);
161 
162     v8::Handle<v8::Object> thisObject = m_thisObject.v8Object();
163     v8::Local<v8::Value> value = thisObject->Get(v8String(m_scriptState->isolate(), m_name));
164     if (!scope.success()) {
165         hadException = true;
166         return ScriptObject();
167     }
168 
169     ASSERT(value->IsFunction());
170 
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();
175 
176     v8::Local<v8::Object> result = V8ObjectConstructor::newInstance(constructor, m_arguments.size(), info.get());
177     if (!scope.success()) {
178         hadException = true;
179         return ScriptObject();
180     }
181 
182     return ScriptObject(m_scriptState, result);
183 }
184 
ScriptCallback(ScriptState * state,const ScriptValue & function)185 ScriptCallback::ScriptCallback(ScriptState* state, const ScriptValue& function)
186     : ScriptCallArgumentHandler(state)
187     , m_scriptState(state)
188     , m_function(function)
189 {
190 }
191 
call()192 ScriptValue ScriptCallback::call()
193 {
194     v8::Isolate* isolate = v8::Isolate::GetCurrent();
195     ASSERT(isolate->InContext());
196     ASSERT(m_function.v8Value()->IsFunction());
197 
198     v8::TryCatch exceptionCatcher;
199     exceptionCatcher.SetVerbose(true);
200     v8::Handle<v8::Object> object = isolate->GetCurrentContext()->Global();
201     v8::Handle<v8::Function> function = v8::Handle<v8::Function>::Cast(m_function.v8Value());
202 
203     OwnPtr<v8::Handle<v8::Value>[]> info = adoptArrayPtr(new v8::Handle<v8::Value>[m_arguments.size()]);
204     for (size_t i = 0; i < m_arguments.size(); ++i)
205         info[i] = m_arguments[i].v8Value();
206 
207     v8::Handle<v8::Value> result = ScriptController::callFunction(m_scriptState->executionContext(), function, object, m_arguments.size(), info.get(), m_scriptState->isolate());
208     return ScriptValue(result, m_scriptState->isolate());
209 }
210 
211 } // namespace WebCore
212