Commit fb96618e authored by jgravelle's avatar jgravelle Committed by Commit bot

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}
parent 70d2b685
......@@ -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(),
......
......@@ -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);
......
// 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");
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment