diff --git a/src/d8.cc b/src/d8.cc index 6fef8452c8..f55e17cda9 100644 --- a/src/d8.cc +++ b/src/d8.cc @@ -934,19 +934,11 @@ void Shell::RealmSharedSet(Local property, data->realm_shared_.Reset(isolate, value); } - -void Shell::Print(const v8::FunctionCallbackInfo& args) { - Write(args); - printf("\n"); - fflush(stdout); -} - - -void Shell::Write(const v8::FunctionCallbackInfo& args) { +void WriteToFile(FILE* file, const v8::FunctionCallbackInfo& args) { for (int i = 0; i < args.Length(); i++) { HandleScope handle_scope(args.GetIsolate()); if (i != 0) { - printf(" "); + fprintf(file, " "); } // Explicitly catch potential exceptions in toString(). @@ -964,14 +956,32 @@ void Shell::Write(const v8::FunctionCallbackInfo& args) { } v8::String::Utf8Value str(str_obj); - int n = static_cast(fwrite(*str, sizeof(**str), str.length(), stdout)); + int n = static_cast(fwrite(*str, sizeof(**str), str.length(), file)); if (n != str.length()) { printf("Error in fwrite\n"); - Exit(1); + Shell::Exit(1); } } } +void WriteAndFlush(FILE* file, + const v8::FunctionCallbackInfo& args) { + WriteToFile(file, args); + fprintf(file, "\n"); + fflush(file); +} + +void Shell::Print(const v8::FunctionCallbackInfo& args) { + WriteAndFlush(stdout, args); +} + +void Shell::PrintErr(const v8::FunctionCallbackInfo& args) { + WriteAndFlush(stderr, args); +} + +void Shell::Write(const v8::FunctionCallbackInfo& args) { + WriteToFile(stdout, args); +} void Shell::Read(const v8::FunctionCallbackInfo& args) { String::Utf8Value file(args[0]); @@ -1387,6 +1397,10 @@ Local Shell::CreateGlobalTemplate(Isolate* isolate) { String::NewFromUtf8(isolate, "print", NewStringType::kNormal) .ToLocalChecked(), FunctionTemplate::New(isolate, Print)); + global_template->Set( + String::NewFromUtf8(isolate, "printErr", NewStringType::kNormal) + .ToLocalChecked(), + FunctionTemplate::New(isolate, PrintErr)); global_template->Set( String::NewFromUtf8(isolate, "write", NewStringType::kNormal) .ToLocalChecked(), diff --git a/src/d8.h b/src/d8.h index 16de77d570..5e7abafb04 100644 --- a/src/d8.h +++ b/src/d8.h @@ -372,6 +372,7 @@ class Shell : public i::AllStatic { const PropertyCallbackInfo& info); static void Print(const v8::FunctionCallbackInfo& args); + static void PrintErr(const v8::FunctionCallbackInfo& args); static void Write(const v8::FunctionCallbackInfo& args); static void QuitOnce(v8::FunctionCallbackInfo* args); static void Quit(const v8::FunctionCallbackInfo& args); diff --git a/test/mjsunit/print.js b/test/mjsunit/print.js new file mode 100644 index 0000000000..1ec3383e47 --- /dev/null +++ b/test/mjsunit/print.js @@ -0,0 +1,6 @@ +// 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. + +assertEquals("function", typeof print, "print should be defined"); +assertEquals("function", typeof printErr, "printErr should be defined");