Chromium Code Reviews| Index: src/d8.cc |
| diff --git a/src/d8.cc b/src/d8.cc |
| index 69072a22e4fcde79672050c0b99c9b05f9749866..6b1fc3a5daa37ae3cd06a66112fac2e4da6a5da4 100644 |
| --- a/src/d8.cc |
| +++ b/src/d8.cc |
| @@ -933,19 +933,11 @@ void Shell::RealmSharedSet(Local<String> property, |
| data->realm_shared_.Reset(isolate, value); |
| } |
| - |
| -void Shell::Print(const v8::FunctionCallbackInfo<v8::Value>& args) { |
| - Write(args); |
| - printf("\n"); |
| - fflush(stdout); |
| -} |
| - |
| - |
| -void Shell::Write(const v8::FunctionCallbackInfo<v8::Value>& args) { |
| +void WriteToFile(FILE* file, const v8::FunctionCallbackInfo<v8::Value>& 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(). |
| @@ -963,14 +955,29 @@ void Shell::Write(const v8::FunctionCallbackInfo<v8::Value>& args) { |
| } |
| v8::String::Utf8Value str(str_obj); |
| - int n = static_cast<int>(fwrite(*str, sizeof(**str), str.length(), stdout)); |
| + int n = static_cast<int>(fwrite(*str, sizeof(**str), str.length(), file)); |
| if (n != str.length()) { |
| printf("Error in fwrite\n"); |
| - Exit(1); |
| + Shell::Exit(1); |
| } |
| } |
| } |
| +void Shell::Print(const v8::FunctionCallbackInfo<v8::Value>& args) { |
| + Write(args); |
|
Derek Schuff
2016/10/28 22:04:39
why not just `WriteToFile(stdout, args)`? Is `Writ
jgravelle
2016/10/28 22:17:25
Write gets exposed to JS so most likely yes.
There
|
| + printf("\n"); |
| + fflush(stdout); |
| +} |
| + |
| +void Shell::PrintErr(const v8::FunctionCallbackInfo<v8::Value>& args) { |
| + WriteToFile(stderr, args); |
| + fprintf(stderr, "\n"); |
| + fflush(stderr); |
| +} |
| + |
| +void Shell::Write(const v8::FunctionCallbackInfo<v8::Value>& args) { |
| + WriteToFile(stdout, args); |
| +} |
| void Shell::Read(const v8::FunctionCallbackInfo<v8::Value>& args) { |
| String::Utf8Value file(args[0]); |
| @@ -1387,6 +1394,10 @@ Local<ObjectTemplate> Shell::CreateGlobalTemplate(Isolate* isolate) { |
| .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(), |
| FunctionTemplate::New(isolate, Write)); |