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 <locale.h> | 9 #include <locale.h> |
10 | 10 |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
46 return buffer; | 46 return buffer; |
47 } | 47 } |
48 | 48 |
49 class UtilsExtension : public v8::Extension { | 49 class UtilsExtension : public v8::Extension { |
50 public: | 50 public: |
51 UtilsExtension() | 51 UtilsExtension() |
52 : v8::Extension("v8_inspector/utils", | 52 : v8::Extension("v8_inspector/utils", |
53 "native function print();" | 53 "native function print();" |
54 "native function quit();" | 54 "native function quit();" |
55 "native function setlocale();" | 55 "native function setlocale();" |
| 56 "native function read();" |
56 "native function load();" | 57 "native function load();" |
57 "native function compileAndRunWithOrigin();" | 58 "native function compileAndRunWithOrigin();" |
58 "native function setCurrentTimeMSForTest();" | 59 "native function setCurrentTimeMSForTest();" |
59 "native function schedulePauseOnNextStatement();" | 60 "native function schedulePauseOnNextStatement();" |
60 "native function cancelPauseOnNextStatement();") {} | 61 "native function cancelPauseOnNextStatement();") {} |
61 virtual v8::Local<v8::FunctionTemplate> GetNativeFunctionTemplate( | 62 virtual v8::Local<v8::FunctionTemplate> GetNativeFunctionTemplate( |
62 v8::Isolate* isolate, v8::Local<v8::String> name) { | 63 v8::Isolate* isolate, v8::Local<v8::String> name) { |
63 v8::Local<v8::Context> context = isolate->GetCurrentContext(); | 64 v8::Local<v8::Context> context = isolate->GetCurrentContext(); |
64 if (name->Equals(context, v8::String::NewFromUtf8( | 65 if (name->Equals(context, v8::String::NewFromUtf8( |
65 isolate, "print", v8::NewStringType::kNormal) | 66 isolate, "print", v8::NewStringType::kNormal) |
66 .ToLocalChecked()) | 67 .ToLocalChecked()) |
67 .FromJust()) { | 68 .FromJust()) { |
68 return v8::FunctionTemplate::New(isolate, UtilsExtension::Print); | 69 return v8::FunctionTemplate::New(isolate, UtilsExtension::Print); |
69 } else if (name->Equals(context, | 70 } else if (name->Equals(context, |
70 v8::String::NewFromUtf8(isolate, "quit", | 71 v8::String::NewFromUtf8(isolate, "quit", |
71 v8::NewStringType::kNormal) | 72 v8::NewStringType::kNormal) |
72 .ToLocalChecked()) | 73 .ToLocalChecked()) |
73 .FromJust()) { | 74 .FromJust()) { |
74 return v8::FunctionTemplate::New(isolate, UtilsExtension::Quit); | 75 return v8::FunctionTemplate::New(isolate, UtilsExtension::Quit); |
75 } else if (name->Equals(context, | 76 } else if (name->Equals(context, |
76 v8::String::NewFromUtf8(isolate, "setlocale", | 77 v8::String::NewFromUtf8(isolate, "setlocale", |
77 v8::NewStringType::kNormal) | 78 v8::NewStringType::kNormal) |
78 .ToLocalChecked()) | 79 .ToLocalChecked()) |
79 .FromJust()) { | 80 .FromJust()) { |
80 return v8::FunctionTemplate::New(isolate, UtilsExtension::SetLocale); | 81 return v8::FunctionTemplate::New(isolate, UtilsExtension::SetLocale); |
81 } else if (name->Equals(context, | 82 } else if (name->Equals(context, |
| 83 v8::String::NewFromUtf8(isolate, "read", |
| 84 v8::NewStringType::kNormal) |
| 85 .ToLocalChecked()) |
| 86 .FromJust()) { |
| 87 return v8::FunctionTemplate::New(isolate, UtilsExtension::Read); |
| 88 } else if (name->Equals(context, |
82 v8::String::NewFromUtf8(isolate, "load", | 89 v8::String::NewFromUtf8(isolate, "load", |
83 v8::NewStringType::kNormal) | 90 v8::NewStringType::kNormal) |
84 .ToLocalChecked()) | 91 .ToLocalChecked()) |
85 .FromJust()) { | 92 .FromJust()) { |
86 return v8::FunctionTemplate::New(isolate, UtilsExtension::Load); | 93 return v8::FunctionTemplate::New(isolate, UtilsExtension::Load); |
87 } else if (name->Equals(context, v8::String::NewFromUtf8( | 94 } else if (name->Equals(context, v8::String::NewFromUtf8( |
88 isolate, "compileAndRunWithOrigin", | 95 isolate, "compileAndRunWithOrigin", |
89 v8::NewStringType::kNormal) | 96 v8::NewStringType::kNormal) |
90 .ToLocalChecked()) | 97 .ToLocalChecked()) |
91 .FromJust()) { | 98 .FromJust()) { |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
166 | 173 |
167 static void SetLocale(const v8::FunctionCallbackInfo<v8::Value>& args) { | 174 static void SetLocale(const v8::FunctionCallbackInfo<v8::Value>& args) { |
168 if (args.Length() != 1 || !args[0]->IsString()) { | 175 if (args.Length() != 1 || !args[0]->IsString()) { |
169 fprintf(stderr, "Internal error: setlocale get one string argument."); | 176 fprintf(stderr, "Internal error: setlocale get one string argument."); |
170 Exit(); | 177 Exit(); |
171 } | 178 } |
172 v8::String::Utf8Value str(args[0]); | 179 v8::String::Utf8Value str(args[0]); |
173 setlocale(LC_NUMERIC, *str); | 180 setlocale(LC_NUMERIC, *str); |
174 } | 181 } |
175 | 182 |
| 183 static bool ReadFile(v8::Isolate* isolate, v8::Local<v8::Value> name, |
| 184 v8::internal::Vector<const char>* chars) { |
| 185 v8::String::Utf8Value str(name); |
| 186 bool exists = false; |
| 187 std::string filename(*str, str.length()); |
| 188 *chars = v8::internal::ReadFile(filename.c_str(), &exists); |
| 189 if (!exists) { |
| 190 isolate->ThrowException( |
| 191 v8::String::NewFromUtf8(isolate, "Error reading file", |
| 192 v8::NewStringType::kNormal) |
| 193 .ToLocalChecked()); |
| 194 return false; |
| 195 } |
| 196 return true; |
| 197 } |
| 198 |
| 199 static void Read(const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 200 if (args.Length() != 1 || !args[0]->IsString()) { |
| 201 fprintf(stderr, "Internal error: read gets one string argument."); |
| 202 Exit(); |
| 203 } |
| 204 v8::internal::Vector<const char> chars; |
| 205 v8::Isolate* isolate = args.GetIsolate(); |
| 206 if (ReadFile(isolate, args[0], &chars)) { |
| 207 args.GetReturnValue().Set( |
| 208 v8::String::NewFromUtf8(isolate, chars.start(), |
| 209 v8::NewStringType::kNormal, chars.length()) |
| 210 .ToLocalChecked()); |
| 211 } |
| 212 } |
| 213 |
176 static void Load(const v8::FunctionCallbackInfo<v8::Value>& args) { | 214 static void Load(const v8::FunctionCallbackInfo<v8::Value>& args) { |
177 if (args.Length() != 1 || !args[0]->IsString()) { | 215 if (args.Length() != 1 || !args[0]->IsString()) { |
178 fprintf(stderr, "Internal error: load gets one string argument."); | 216 fprintf(stderr, "Internal error: load gets one string argument."); |
179 Exit(); | 217 Exit(); |
180 } | 218 } |
181 v8::String::Utf8Value str(args[0]); | 219 v8::internal::Vector<const char> chars; |
182 v8::Isolate* isolate = args.GetIsolate(); | 220 v8::Isolate* isolate = args.GetIsolate(); |
183 bool exists = false; | 221 if (ReadFile(isolate, args[0], &chars)) { |
184 std::string filename(*str, str.length()); | 222 ExecuteStringTask task(chars); |
185 v8::internal::Vector<const char> chars = | 223 v8::Global<v8::Context> context(isolate, isolate->GetCurrentContext()); |
186 v8::internal::ReadFile(filename.c_str(), &exists); | 224 task.Run(isolate, context); |
187 if (!exists) { | |
188 isolate->ThrowException( | |
189 v8::String::NewFromUtf8(isolate, "Error loading file", | |
190 v8::NewStringType::kNormal) | |
191 .ToLocalChecked()); | |
192 return; | |
193 } | 225 } |
194 ExecuteStringTask task(chars); | |
195 v8::Global<v8::Context> context(isolate, isolate->GetCurrentContext()); | |
196 task.Run(isolate, context); | |
197 } | 226 } |
198 | 227 |
199 static void CompileAndRunWithOrigin( | 228 static void CompileAndRunWithOrigin( |
200 const v8::FunctionCallbackInfo<v8::Value>& args) { | 229 const v8::FunctionCallbackInfo<v8::Value>& args) { |
201 if (args.Length() != 5 || !args[0]->IsString() || !args[1]->IsString() || | 230 if (args.Length() != 5 || !args[0]->IsString() || !args[1]->IsString() || |
202 !args[2]->IsInt32() || !args[3]->IsInt32() || !args[4]->IsBoolean()) { | 231 !args[2]->IsInt32() || !args[3]->IsInt32() || !args[4]->IsBoolean()) { |
203 fprintf(stderr, | 232 fprintf(stderr, |
204 "Internal error: compileAndRunWithOrigin(source, name, line, " | 233 "Internal error: compileAndRunWithOrigin(source, name, line, " |
205 "column, is_module)."); | 234 "column, is_module)."); |
206 Exit(); | 235 Exit(); |
(...skipping 377 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
584 argv[i]); | 613 argv[i]); |
585 Exit(); | 614 Exit(); |
586 } | 615 } |
587 frontend_runner.Append(new ExecuteStringTask(chars)); | 616 frontend_runner.Append(new ExecuteStringTask(chars)); |
588 } | 617 } |
589 | 618 |
590 frontend_runner.Join(); | 619 frontend_runner.Join(); |
591 backend_runner.Join(); | 620 backend_runner.Join(); |
592 return 0; | 621 return 0; |
593 } | 622 } |
OLD | NEW |