OLD | NEW |
1 // Copyright 2016 the V8 project authors. All rights reserved. | 1 // Copyright 2016 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #if !defined(_WIN32) && !defined(_WIN64) | 5 #if !defined(_WIN32) && !defined(_WIN64) |
6 #include <unistd.h> // NOLINT | 6 #include <unistd.h> // NOLINT |
7 #endif // !defined(_WIN32) && !defined(_WIN64) | 7 #endif // !defined(_WIN32) && !defined(_WIN64) |
8 | 8 |
9 #include "include/libplatform/libplatform.h" | 9 #include "include/libplatform/libplatform.h" |
10 #include "include/v8.h" | 10 #include "include/v8.h" |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
107 InspectorClientImpl::InspectorFromContext(context); | 107 InspectorClientImpl::InspectorFromContext(context); |
108 if (inspector) inspector->willExecuteScript(context, function->ScriptId()); | 108 if (inspector) inspector->willExecuteScript(context, function->ScriptId()); |
109 result = function->Call(context, context->Global(), 0, nullptr); | 109 result = function->Call(context, context->Global(), 0, nullptr); |
110 if (inspector) inspector->didExecuteScript(context); | 110 if (inspector) inspector->didExecuteScript(context); |
111 } | 111 } |
112 | 112 |
113 private: | 113 private: |
114 v8::Global<v8::Function> function_; | 114 v8::Global<v8::Function> function_; |
115 }; | 115 }; |
116 | 116 |
| 117 v8::internal::Vector<uint16_t> ToVector(v8::Local<v8::String> str) { |
| 118 v8::internal::Vector<uint16_t> buffer = |
| 119 v8::internal::Vector<uint16_t>::New(str->Length()); |
| 120 str->Write(buffer.start(), 0, str->Length()); |
| 121 return buffer; |
| 122 } |
| 123 |
117 class SetTimeoutExtension : public v8::Extension { | 124 class SetTimeoutExtension : public v8::Extension { |
118 public: | 125 public: |
119 SetTimeoutExtension() | 126 SetTimeoutExtension() |
120 : v8::Extension("v8_inspector/setTimeout", | 127 : v8::Extension("v8_inspector/setTimeout", |
121 "native function setTimeout();") {} | 128 "native function setTimeout();") {} |
122 | 129 |
123 virtual v8::Local<v8::FunctionTemplate> GetNativeFunctionTemplate( | 130 virtual v8::Local<v8::FunctionTemplate> GetNativeFunctionTemplate( |
124 v8::Isolate* isolate, v8::Local<v8::String> name) { | 131 v8::Isolate* isolate, v8::Local<v8::String> name) { |
125 return v8::FunctionTemplate::New(isolate, SetTimeoutExtension::SetTimeout); | 132 return v8::FunctionTemplate::New(isolate, SetTimeoutExtension::SetTimeout); |
126 } | 133 } |
127 | 134 |
128 private: | 135 private: |
129 static void SetTimeout(const v8::FunctionCallbackInfo<v8::Value>& args) { | 136 static void SetTimeout(const v8::FunctionCallbackInfo<v8::Value>& args) { |
130 if (args.Length() != 2 || !args[1]->IsNumber() || | 137 if (args.Length() != 2 || !args[1]->IsNumber() || |
131 (!args[0]->IsFunction() && !args[0]->IsString()) || | 138 (!args[0]->IsFunction() && !args[0]->IsString()) || |
132 args[1].As<v8::Number>()->Value() != 0.0) { | 139 args[1].As<v8::Number>()->Value() != 0.0) { |
133 fprintf(stderr, | 140 fprintf(stderr, |
134 "Internal error: only setTimeout(function, 0) is supported."); | 141 "Internal error: only setTimeout(function, 0) is supported."); |
135 Exit(); | 142 Exit(); |
136 } | 143 } |
137 v8::Local<v8::Context> context = args.GetIsolate()->GetCurrentContext(); | 144 v8::Local<v8::Context> context = args.GetIsolate()->GetCurrentContext(); |
138 if (args[0]->IsFunction()) { | 145 if (args[0]->IsFunction()) { |
139 TaskRunner::FromContext(context)->Append(new SetTimeoutTask( | 146 TaskRunner::FromContext(context)->Append(new SetTimeoutTask( |
140 args.GetIsolate(), v8::Local<v8::Function>::Cast(args[0]))); | 147 args.GetIsolate(), v8::Local<v8::Function>::Cast(args[0]))); |
141 } else { | 148 } else { |
142 v8::Local<v8::String> data = args[0].As<v8::String>(); | 149 TaskRunner::FromContext(context)->Append( |
143 std::unique_ptr<uint16_t[]> buffer(new uint16_t[data->Length()]); | 150 new ExecuteStringTask(ToVector(args[0].As<v8::String>()))); |
144 data->Write(reinterpret_cast<uint16_t*>(buffer.get()), 0, data->Length()); | |
145 v8_inspector::String16 source = | |
146 v8_inspector::String16(buffer.get(), data->Length()); | |
147 TaskRunner::FromContext(context)->Append(new ExecuteStringTask(source)); | |
148 } | 151 } |
149 } | 152 } |
150 }; | 153 }; |
151 | 154 |
152 v8_inspector::String16 ToString16(const v8_inspector::StringView& string) { | 155 v8::Local<v8::String> ToString(v8::Isolate* isolate, |
| 156 const v8_inspector::StringView& string) { |
153 if (string.is8Bit()) | 157 if (string.is8Bit()) |
154 return v8_inspector::String16( | 158 return v8::String::NewFromOneByte(isolate, string.characters8(), |
155 reinterpret_cast<const char*>(string.characters8()), string.length()); | 159 v8::NewStringType::kNormal, |
156 return v8_inspector::String16( | 160 string.length()) |
157 reinterpret_cast<const uint16_t*>(string.characters16()), | 161 .ToLocalChecked(); |
158 string.length()); | 162 else |
| 163 return v8::String::NewFromTwoByte(isolate, string.characters16(), |
| 164 v8::NewStringType::kNormal, |
| 165 string.length()) |
| 166 .ToLocalChecked(); |
159 } | 167 } |
160 | 168 |
161 class FrontendChannelImpl : public InspectorClientImpl::FrontendChannel { | 169 class FrontendChannelImpl : public InspectorClientImpl::FrontendChannel { |
162 public: | 170 public: |
163 explicit FrontendChannelImpl(TaskRunner* frontend_task_runner) | 171 explicit FrontendChannelImpl(TaskRunner* frontend_task_runner) |
164 : frontend_task_runner_(frontend_task_runner) {} | 172 : frontend_task_runner_(frontend_task_runner) {} |
165 virtual ~FrontendChannelImpl() {} | 173 virtual ~FrontendChannelImpl() {} |
166 | 174 |
167 void SendMessageToFrontend(const v8_inspector::StringView& message) final { | 175 void SendMessageToFrontend(const v8_inspector::StringView& message) final { |
168 v8_inspector::String16Builder script; | 176 v8::Isolate* isolate = v8::Isolate::GetCurrent(); |
169 script.append("InspectorTest._dispatchMessage("); | 177 v8::HandleScope scope(v8::Isolate::GetCurrent()); |
170 script.append(ToString16(message)); | 178 |
171 script.append(")"); | 179 v8::Local<v8::String> prefix = |
172 frontend_task_runner_->Append(new ExecuteStringTask(script.toString())); | 180 v8::String::NewFromUtf8(isolate, "InspectorTest._dispatchMessage(", |
| 181 v8::NewStringType::kInternalized) |
| 182 .ToLocalChecked(); |
| 183 v8::Local<v8::String> message_string = ToString(isolate, message); |
| 184 v8::Local<v8::String> suffix = |
| 185 v8::String::NewFromUtf8(isolate, ")", v8::NewStringType::kInternalized) |
| 186 .ToLocalChecked(); |
| 187 |
| 188 v8::Local<v8::String> result = v8::String::Concat(prefix, message_string); |
| 189 result = v8::String::Concat(result, suffix); |
| 190 |
| 191 frontend_task_runner_->Append(new ExecuteStringTask(ToVector(result))); |
173 } | 192 } |
174 | 193 |
175 private: | 194 private: |
176 TaskRunner* frontend_task_runner_; | 195 TaskRunner* frontend_task_runner_; |
177 }; | 196 }; |
178 | 197 |
179 } // namespace | 198 } // namespace |
180 | 199 |
181 int main(int argc, char* argv[]) { | 200 int main(int argc, char* argv[]) { |
182 v8::V8::InitializeICUDefaultLocation(argv[0]); | 201 v8::V8::InitializeICUDefaultLocation(argv[0]); |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
218 if (argv[i][0] == '-') break; | 237 if (argv[i][0] == '-') break; |
219 | 238 |
220 bool exists = false; | 239 bool exists = false; |
221 v8::internal::Vector<const char> chars = | 240 v8::internal::Vector<const char> chars = |
222 v8::internal::ReadFile(argv[i], &exists, true); | 241 v8::internal::ReadFile(argv[i], &exists, true); |
223 if (!exists) { | 242 if (!exists) { |
224 fprintf(stderr, "Internal error: script file doesn't exists: %s\n", | 243 fprintf(stderr, "Internal error: script file doesn't exists: %s\n", |
225 argv[i]); | 244 argv[i]); |
226 Exit(); | 245 Exit(); |
227 } | 246 } |
228 v8_inspector::String16 source = | 247 frontend_runner.Append(new ExecuteStringTask(chars)); |
229 v8_inspector::String16::fromUTF8(chars.start(), chars.length()); | |
230 frontend_runner.Append(new ExecuteStringTask(source)); | |
231 } | 248 } |
232 | 249 |
233 frontend_runner.Join(); | 250 frontend_runner.Join(); |
234 return 0; | 251 return 0; |
235 } | 252 } |
OLD | NEW |