Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(194)

Unified Diff: src/d8.cc

Issue 2458963003: Add Shell::PrintErr and expose it in the d8 shell as printErr (Closed)
Patch Set: Normalize and unify Print/PrintErr Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « src/d8.h ('k') | test/mjsunit/print.js » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/d8.cc
diff --git a/src/d8.cc b/src/d8.cc
index 69072a22e4fcde79672050c0b99c9b05f9749866..202ecbd0e3aefe917166ebe52ae096aca6f9ea20 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,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) {
.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));
« no previous file with comments | « src/d8.h ('k') | test/mjsunit/print.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698