OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2009 Google Inc. All rights reserved. | 2 * Copyright (C) 2009 Google Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
6 * met: | 6 * met: |
7 * | 7 * |
8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
(...skipping 12 matching lines...) Expand all Loading... |
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29 */ | 29 */ |
30 | 30 |
31 #include "bindings/core/v8/ScriptFunctionCall.h" | 31 #include "bindings/core/v8/ScriptFunctionCall.h" |
32 | 32 |
33 #include "bindings/core/v8/ScriptController.h" | 33 #include "wtf/PassOwnPtr.h" |
34 #include "bindings/core/v8/ScriptState.h" | |
35 #include "bindings/core/v8/ScriptValue.h" | |
36 #include "bindings/core/v8/V8Binding.h" | |
37 #include "bindings/core/v8/V8ObjectConstructor.h" | |
38 #include "bindings/core/v8/V8ScriptRunner.h" | |
39 | 34 |
40 #include <v8.h> | 35 #include <v8.h> |
41 | 36 |
42 namespace blink { | 37 namespace blink { |
43 | 38 |
44 void ScriptCallArgumentHandler::appendArgument(v8::Local<v8::Value> value) | 39 static v8::Local<v8::String> v8String(v8::Isolate* isolate, const String& string
) |
45 { | 40 { |
46 m_arguments.append(ScriptValue(m_scriptState.get(), value)); | 41 v8::Local<v8::String> result; |
| 42 bool success = v8::String::NewFromUtf8(isolate, string.utf8().data(), v8::Ne
wStringType::kNormal).ToLocal(&result); |
| 43 ASSERT_UNUSED(success, success); |
| 44 return result; |
47 } | 45 } |
48 | 46 |
49 void ScriptCallArgumentHandler::appendArgument(const ScriptValue& argument) | 47 ScriptFunctionCall::ScriptFunctionCall(v8::Local<v8::Context> context, v8::Local
<v8::Value> value, const String& name) |
50 { | 48 : m_context(context) |
51 if (argument.scriptState() != m_scriptState) { | 49 , m_name(v8String(context->GetIsolate(), name)) |
52 appendUndefinedArgument(); | 50 , m_value(value) |
53 return; | |
54 } | |
55 m_arguments.append(argument); | |
56 } | |
57 | |
58 void ScriptCallArgumentHandler::appendArgument(const String& argument) | |
59 { | |
60 v8::Isolate* isolate = m_scriptState->isolate(); | |
61 ScriptState::Scope scope(m_scriptState.get()); | |
62 m_arguments.append(ScriptValue(m_scriptState.get(), v8String(isolate, argume
nt))); | |
63 } | |
64 | |
65 void ScriptCallArgumentHandler::appendArgument(int argument) | |
66 { | |
67 v8::Isolate* isolate = m_scriptState->isolate(); | |
68 ScriptState::Scope scope(m_scriptState.get()); | |
69 m_arguments.append(ScriptValue(m_scriptState.get(), v8::Number::New(isolate,
argument))); | |
70 } | |
71 | |
72 void ScriptCallArgumentHandler::appendArgument(bool argument) | |
73 { | |
74 v8::Isolate* isolate = m_scriptState->isolate(); | |
75 m_arguments.append(ScriptValue(m_scriptState.get(), v8Boolean(argument, isol
ate))); | |
76 } | |
77 | |
78 void ScriptCallArgumentHandler::appendUndefinedArgument() | |
79 { | |
80 v8::Isolate* isolate = m_scriptState->isolate(); | |
81 m_arguments.append(ScriptValue(m_scriptState.get(), v8::Undefined(isolate)))
; | |
82 } | |
83 | |
84 ScriptFunctionCall::ScriptFunctionCall(const ScriptValue& thisObject, const Stri
ng& name) | |
85 : ScriptCallArgumentHandler(thisObject.scriptState()) | |
86 , m_thisObject(thisObject) | |
87 , m_name(name) | |
88 { | 51 { |
89 } | 52 } |
90 | 53 |
91 ScriptValue ScriptFunctionCall::call(bool& hadException, bool reportExceptions) | 54 void ScriptFunctionCall::appendArgument(v8::Local<v8::Value> value) |
92 { | 55 { |
93 ScriptState::Scope scope(m_scriptState.get()); | 56 m_arguments.append(value); |
94 v8::TryCatch tryCatch(m_scriptState->isolate()); | 57 } |
| 58 |
| 59 void ScriptFunctionCall::appendArgument(const String& argument) |
| 60 { |
| 61 m_arguments.append(v8String(m_context->GetIsolate(), argument)); |
| 62 } |
| 63 |
| 64 void ScriptFunctionCall::appendArgument(int argument) |
| 65 { |
| 66 m_arguments.append(v8::Number::New(m_context->GetIsolate(), argument)); |
| 67 } |
| 68 |
| 69 void ScriptFunctionCall::appendArgument(bool argument) |
| 70 { |
| 71 m_arguments.append(argument ? v8::True(m_context->GetIsolate()) : v8::False(
m_context->GetIsolate())); |
| 72 } |
| 73 |
| 74 void ScriptFunctionCall::appendUndefinedArgument() |
| 75 { |
| 76 m_arguments.append(v8::Undefined(m_context->GetIsolate())); |
| 77 } |
| 78 |
| 79 v8::Local<v8::Value> ScriptFunctionCall::call(bool& hadException, bool reportExc
eptions) |
| 80 { |
| 81 v8::TryCatch tryCatch(m_context->GetIsolate()); |
95 tryCatch.SetVerbose(reportExceptions); | 82 tryCatch.SetVerbose(reportExceptions); |
96 | 83 |
97 ScriptValue result = callWithoutExceptionHandling(); | 84 v8::Local<v8::Value> result = callWithoutExceptionHandling(); |
98 hadException = tryCatch.HasCaught(); | 85 hadException = tryCatch.HasCaught(); |
99 return result; | 86 return result; |
100 } | 87 } |
101 | 88 |
102 ScriptValue ScriptFunctionCall::call() | 89 v8::Local<v8::Value> ScriptFunctionCall::call() |
103 { | 90 { |
104 bool hadException = false; | 91 bool hadException = false; |
105 return call(hadException); | 92 return call(hadException); |
106 } | 93 } |
107 | 94 |
108 ScriptValue ScriptFunctionCall::callWithoutExceptionHandling() | 95 v8::Local<v8::Value> ScriptFunctionCall::callWithoutExceptionHandling() |
109 { | 96 { |
110 ScriptState::Scope scope(m_scriptState.get()); | 97 v8::Local<v8::Object> thisObject = v8::Local<v8::Object>::Cast(m_value); |
111 v8::Local<v8::Object> thisObject = v8::Local<v8::Object>::Cast(m_thisObject.
v8Value()); | |
112 v8::Local<v8::Value> value; | 98 v8::Local<v8::Value> value; |
113 if (!thisObject->Get(m_scriptState->context(), v8String(m_scriptState->isola
te(), m_name)).ToLocal(&value)) | 99 if (!thisObject->Get(m_context, m_name).ToLocal(&value)) |
114 return ScriptValue(); | 100 return v8::Local<v8::Value>(); |
115 | 101 |
116 ASSERT(value->IsFunction()); | 102 ASSERT(value->IsFunction()); |
117 | 103 |
118 v8::Local<v8::Function> function = v8::Local<v8::Function>::Cast(value); | 104 v8::Local<v8::Function> function = v8::Local<v8::Function>::Cast(value); |
119 OwnPtr<v8::Local<v8::Value>[]> info = adoptArrayPtr(new v8::Local<v8::Value>
[m_arguments.size()]); | 105 OwnPtr<v8::Local<v8::Value>[]> info = adoptArrayPtr(new v8::Local<v8::Value>
[m_arguments.size()]); |
120 for (size_t i = 0; i < m_arguments.size(); ++i) { | 106 for (size_t i = 0; i < m_arguments.size(); ++i) { |
121 info[i] = m_arguments[i].v8Value(); | 107 info[i] = m_arguments[i]; |
122 ASSERT(!info[i].IsEmpty()); | 108 ASSERT(!info[i].IsEmpty()); |
123 } | 109 } |
124 | 110 |
125 v8::Local<v8::Value> result; | 111 v8::Local<v8::Value> result; |
126 if (!V8ScriptRunner::callFunction(function, m_scriptState->executionContext(
), thisObject, m_arguments.size(), info.get(), m_scriptState->isolate()).ToLocal
(&result)) | 112 if (!function->Call(m_context, thisObject, m_arguments.size(), info.get()).T
oLocal(&result)) |
127 return ScriptValue(); | 113 return v8::Local<v8::Value>(); |
128 return ScriptValue(m_scriptState.get(), result); | 114 return result; |
129 } | 115 } |
130 | 116 |
131 v8::Local<v8::Function> ScriptFunctionCall::function() | 117 v8::Local<v8::Function> ScriptFunctionCall::function() |
132 { | 118 { |
133 v8::TryCatch tryCatch(m_scriptState->isolate()); | 119 v8::TryCatch tryCatch(m_context->GetIsolate()); |
134 v8::Local<v8::Object> thisObject = v8::Local<v8::Object>::Cast(m_thisObject.
v8Value()); | 120 v8::Local<v8::Object> thisObject = v8::Local<v8::Object>::Cast(m_value); |
135 v8::Local<v8::Value> value; | 121 v8::Local<v8::Value> value; |
136 if (!thisObject->Get(m_scriptState->context(), v8String(m_scriptState->isola
te(), m_name)).ToLocal(&value)) | 122 if (!thisObject->Get(m_context, m_name).ToLocal(&value)) |
137 return v8::Local<v8::Function>(); | 123 return v8::Local<v8::Function>(); |
138 | 124 |
139 ASSERT(value->IsFunction()); | 125 ASSERT(value->IsFunction()); |
140 return v8::Local<v8::Function>::Cast(value); | 126 return v8::Local<v8::Function>::Cast(value); |
141 } | 127 } |
142 | 128 |
143 | |
144 } // namespace blink | 129 } // namespace blink |
OLD | NEW |