Add Shell::PrintErr and expose it in the d8 shell as printErr

This function is implemented in other JavaScript shells

BUG=None

R=titzer

Review-Url: https://codereview.chromium.org/2458963003
Cr-Commit-Position: refs/heads/master@{#40677}
This commit is contained in:
jgravelle 2016-10-31 09:39:48 -07:00 committed by Commit bot
parent 70d2b685ae
commit fb96618ee0
3 changed files with 33 additions and 12 deletions

View File

@ -934,19 +934,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().
@ -964,14 +956,32 @@ 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 WriteAndFlush(FILE* file,
const v8::FunctionCallbackInfo<v8::Value>& args) {
WriteToFile(file, args);
fprintf(file, "\n");
fflush(file);
}
void Shell::Print(const v8::FunctionCallbackInfo<v8::Value>& args) {
WriteAndFlush(stdout, args);
}
void Shell::PrintErr(const v8::FunctionCallbackInfo<v8::Value>& args) {
WriteAndFlush(stderr, args);
}
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 +1397,10 @@ Local<ObjectTemplate> 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(),

View File

@ -372,6 +372,7 @@ class Shell : public i::AllStatic {
const PropertyCallbackInfo<void>& info);
static void Print(const v8::FunctionCallbackInfo<v8::Value>& args);
static void PrintErr(const v8::FunctionCallbackInfo<v8::Value>& args);
static void Write(const v8::FunctionCallbackInfo<v8::Value>& args);
static void QuitOnce(v8::FunctionCallbackInfo<v8::Value>* args);
static void Quit(const v8::FunctionCallbackInfo<v8::Value>& args);

6
test/mjsunit/print.js Normal file
View File

@ -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");