Index: test/inspector-protocol/utils-extension.cc |
diff --git a/test/inspector-protocol/utils-extension.cc b/test/inspector-protocol/utils-extension.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..d82994964f21524726fbaacd72c1427ed1c7f5f8 |
--- /dev/null |
+++ b/test/inspector-protocol/utils-extension.cc |
@@ -0,0 +1,67 @@ |
+// Copyright 2016 the V8 project authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "test/inspector-protocol/utils-extension.h" |
+ |
+#include "v8-platform.h" |
+ |
+namespace v8_inspector { |
+ |
+v8::Local<v8::FunctionTemplate> UtilsExtension::GetNativeFunctionTemplate( |
+ v8::Isolate* isolate, v8::Local<v8::String> name) { |
+ v8::Local<v8::Context> context = isolate->GetCurrentContext(); |
+ if (name->Equals(context, v8::String::NewFromUtf8(isolate, "print", |
+ v8::NewStringType::kNormal) |
+ .ToLocalChecked()) |
+ .FromJust()) { |
+ return v8::FunctionTemplate::New(isolate, UtilsExtension::Print); |
+ } else if (name->Equals(context, |
+ v8::String::NewFromUtf8(isolate, "quit", |
+ v8::NewStringType::kNormal) |
+ .ToLocalChecked()) |
+ .FromJust()) { |
+ return v8::FunctionTemplate::New(isolate, UtilsExtension::Quit); |
+ } |
+ return v8::Local<v8::FunctionTemplate>(); |
+} |
+ |
+void UtilsExtension::Print(const v8::FunctionCallbackInfo<v8::Value>& args) { |
+ for (int i = 0; i < args.Length(); i++) { |
+ v8::HandleScope handle_scope(args.GetIsolate()); |
+ if (i != 0) { |
+ printf(" "); |
+ } |
+ |
+ // Explicitly catch potential exceptions in toString(). |
+ v8::TryCatch try_catch(args.GetIsolate()); |
+ v8::Local<v8::Value> arg = args[i]; |
+ v8::Local<v8::String> str_obj; |
+ |
+ if (arg->IsSymbol()) { |
+ arg = v8::Local<v8::Symbol>::Cast(arg)->Name(); |
+ } |
+ if (!arg->ToString(args.GetIsolate()->GetCurrentContext()) |
+ .ToLocal(&str_obj)) { |
+ try_catch.ReThrow(); |
+ return; |
+ } |
+ |
+ v8::String::Utf8Value str(str_obj); |
+ int n = static_cast<int>(fwrite(*str, sizeof(**str), str.length(), stdout)); |
+ if (n != str.length()) { |
+ printf("Error in fwrite\n"); |
+ Quit(args); |
+ } |
+ } |
+ printf("\n"); |
+ fflush(stdout); |
+} |
+ |
+void UtilsExtension::Quit(const v8::FunctionCallbackInfo<v8::Value>&) { |
+ fflush(stdout); |
+ fflush(stderr); |
+ _exit(0); |
+} |
+ |
+} // namespace v8 |