|
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 "ScriptFunctionCall.h" |
|
33 |
|
34 #include "ScriptScope.h" |
|
35 #include "ScriptState.h" |
|
36 #include "ScriptString.h" |
|
37 #include "ScriptValue.h" |
|
38 |
|
39 #include "V8Binding.h" |
|
40 #include "V8Proxy.h" |
|
41 #include "V8Utilities.h" |
|
42 |
|
43 #include <v8.h> |
|
44 #include <wtf/OwnArrayPtr.h> |
|
45 |
|
46 namespace WebCore { |
|
47 |
|
48 ScriptFunctionCall::ScriptFunctionCall(const ScriptObject& thisObject, const String& name) |
|
49 : m_scriptState(thisObject.scriptState()) |
|
50 , m_thisObject(thisObject) |
|
51 , m_name(name) |
|
52 { |
|
53 } |
|
54 |
|
55 void ScriptFunctionCall::appendArgument(const ScriptObject& argument) |
|
56 { |
|
57 if (argument.scriptState() != m_scriptState) { |
|
58 ASSERT_NOT_REACHED(); |
|
59 return; |
|
60 } |
|
61 m_arguments.append(argument); |
|
62 } |
|
63 |
|
64 void ScriptFunctionCall::appendArgument(const ScriptString& argument) |
|
65 { |
|
66 ScriptScope scope(m_scriptState); |
|
67 m_arguments.append(v8String(argument)); |
|
68 } |
|
69 |
|
70 void ScriptFunctionCall::appendArgument(const ScriptValue& argument) |
|
71 { |
|
72 m_arguments.append(argument); |
|
73 } |
|
74 |
|
75 void ScriptFunctionCall::appendArgument(const String& argument) |
|
76 { |
|
77 ScriptScope scope(m_scriptState); |
|
78 m_arguments.append(v8String(argument)); |
|
79 } |
|
80 |
|
81 void ScriptFunctionCall::appendArgument(const char* argument) |
|
82 { |
|
83 ScriptScope scope(m_scriptState); |
|
84 m_arguments.append(v8String(argument)); |
|
85 } |
|
86 |
|
87 void ScriptFunctionCall::appendArgument(long argument) |
|
88 { |
|
89 ScriptScope scope(m_scriptState); |
|
90 m_arguments.append(v8::Number::New(argument)); |
|
91 } |
|
92 |
|
93 void ScriptFunctionCall::appendArgument(long long argument) |
|
94 { |
|
95 ScriptScope scope(m_scriptState); |
|
96 m_arguments.append(v8::Number::New(argument)); |
|
97 } |
|
98 |
|
99 void ScriptFunctionCall::appendArgument(unsigned int argument) |
|
100 { |
|
101 ScriptScope scope(m_scriptState); |
|
102 m_arguments.append(v8::Number::New(argument)); |
|
103 } |
|
104 |
|
105 void ScriptFunctionCall::appendArgument(unsigned long argument) |
|
106 { |
|
107 ScriptScope scope(m_scriptState); |
|
108 m_arguments.append(v8::Number::New(argument)); |
|
109 } |
|
110 |
|
111 void ScriptFunctionCall::appendArgument(int argument) |
|
112 { |
|
113 ScriptScope scope(m_scriptState); |
|
114 m_arguments.append(v8::Number::New(argument)); |
|
115 } |
|
116 |
|
117 void ScriptFunctionCall::appendArgument(bool argument) |
|
118 { |
|
119 m_arguments.append(v8Boolean(argument)); |
|
120 } |
|
121 |
|
122 ScriptValue ScriptFunctionCall::call(bool& hadException, bool reportExceptions) |
|
123 { |
|
124 ScriptScope scope(m_scriptState, reportExceptions); |
|
125 |
|
126 v8::Local<v8::Object> thisObject = m_thisObject.v8Object(); |
|
127 v8::Local<v8::Value> value = thisObject->Get(v8String(m_name)); |
|
128 if (!scope.success()) { |
|
129 hadException = true; |
|
130 return ScriptValue(); |
|
131 } |
|
132 |
|
133 ASSERT(value->IsFunction()); |
|
134 |
|
135 v8::Local<v8::Function> function(v8::Function::Cast(*value)); |
|
136 OwnArrayPtr<v8::Handle<v8::Value> > args(new v8::Handle<v8::Value>[m_arguments.size()]); |
|
137 for (size_t i = 0; i < m_arguments.size(); ++i) |
|
138 args[i] = m_arguments[i].v8Value(); |
|
139 |
|
140 v8::Local<v8::Value> result = function->Call(thisObject, m_arguments.size(), args.get()); |
|
141 if (!scope.success()) { |
|
142 hadException = true; |
|
143 return ScriptValue(); |
|
144 } |
|
145 |
|
146 return ScriptValue(result); |
|
147 } |
|
148 |
|
149 ScriptValue ScriptFunctionCall::call() |
|
150 { |
|
151 bool hadException = false; |
|
152 return call(hadException); |
|
153 } |
|
154 |
|
155 ScriptObject ScriptFunctionCall::construct(bool& hadException, bool reportExceptions) |
|
156 { |
|
157 ScriptScope scope(m_scriptState, reportExceptions); |
|
158 |
|
159 v8::Local<v8::Object> thisObject = m_thisObject.v8Object(); |
|
160 v8::Local<v8::Value> value = thisObject->Get(v8String(m_name)); |
|
161 if (!scope.success()) { |
|
162 hadException = true; |
|
163 return ScriptObject(); |
|
164 } |
|
165 |
|
166 ASSERT(value->IsFunction()); |
|
167 |
|
168 v8::Local<v8::Function> constructor(v8::Function::Cast(*value)); |
|
169 OwnArrayPtr<v8::Handle<v8::Value> > args(new v8::Handle<v8::Value>[m_arguments.size()]); |
|
170 for (size_t i = 0; i < m_arguments.size(); ++i) |
|
171 args[i] = m_arguments[i].v8Value(); |
|
172 |
|
173 v8::Local<v8::Object> result = SafeAllocation::newInstance(constructor, m_arguments.size(), args.get()); |
|
174 if (!scope.success()) { |
|
175 hadException = true; |
|
176 return ScriptObject(); |
|
177 } |
|
178 |
|
179 return ScriptObject(m_scriptState, result); |
|
180 } |
|
181 |
|
182 } // namespace WebCore |