OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "test/inspector-protocol/utils-extension.h" |
| 6 |
| 7 #include "v8-platform.h" |
| 8 |
| 9 namespace v8_inspector { |
| 10 |
| 11 v8::Local<v8::FunctionTemplate> UtilsExtension::GetNativeFunctionTemplate( |
| 12 v8::Isolate* isolate, v8::Local<v8::String> name) { |
| 13 v8::Local<v8::Context> context = isolate->GetCurrentContext(); |
| 14 if (name->Equals(context, v8::String::NewFromUtf8(isolate, "print", |
| 15 v8::NewStringType::kNormal) |
| 16 .ToLocalChecked()) |
| 17 .FromJust()) { |
| 18 return v8::FunctionTemplate::New(isolate, UtilsExtension::Print); |
| 19 } else if (name->Equals(context, |
| 20 v8::String::NewFromUtf8(isolate, "quit", |
| 21 v8::NewStringType::kNormal) |
| 22 .ToLocalChecked()) |
| 23 .FromJust()) { |
| 24 return v8::FunctionTemplate::New(isolate, UtilsExtension::Quit); |
| 25 } |
| 26 return v8::Local<v8::FunctionTemplate>(); |
| 27 } |
| 28 |
| 29 void UtilsExtension::Print(const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 30 for (int i = 0; i < args.Length(); i++) { |
| 31 v8::HandleScope handle_scope(args.GetIsolate()); |
| 32 if (i != 0) { |
| 33 printf(" "); |
| 34 } |
| 35 |
| 36 // Explicitly catch potential exceptions in toString(). |
| 37 v8::TryCatch try_catch(args.GetIsolate()); |
| 38 v8::Local<v8::Value> arg = args[i]; |
| 39 v8::Local<v8::String> str_obj; |
| 40 |
| 41 if (arg->IsSymbol()) { |
| 42 arg = v8::Local<v8::Symbol>::Cast(arg)->Name(); |
| 43 } |
| 44 if (!arg->ToString(args.GetIsolate()->GetCurrentContext()) |
| 45 .ToLocal(&str_obj)) { |
| 46 try_catch.ReThrow(); |
| 47 return; |
| 48 } |
| 49 |
| 50 v8::String::Utf8Value str(str_obj); |
| 51 int n = static_cast<int>(fwrite(*str, sizeof(**str), str.length(), stdout)); |
| 52 if (n != str.length()) { |
| 53 printf("Error in fwrite\n"); |
| 54 Quit(args); |
| 55 } |
| 56 } |
| 57 printf("\n"); |
| 58 fflush(stdout); |
| 59 } |
| 60 |
| 61 void UtilsExtension::Quit(const v8::FunctionCallbackInfo<v8::Value>&) { |
| 62 fflush(stdout); |
| 63 fflush(stderr); |
| 64 _exit(0); |
| 65 } |
| 66 |
| 67 } // namespace v8 |
OLD | NEW |