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

Side by Side 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, 1 month 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 unified diff | Download patch
« no previous file with comments | « src/d8.h ('k') | test/mjsunit/print.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2012 the V8 project authors. All rights reserved. 1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <errno.h> 5 #include <errno.h>
6 #include <stdlib.h> 6 #include <stdlib.h>
7 #include <string.h> 7 #include <string.h>
8 #include <sys/stat.h> 8 #include <sys/stat.h>
9 9
10 #include <algorithm> 10 #include <algorithm>
(...skipping 915 matching lines...) Expand 10 before | Expand all | Expand 10 after
926 } 926 }
927 927
928 void Shell::RealmSharedSet(Local<String> property, 928 void Shell::RealmSharedSet(Local<String> property,
929 Local<Value> value, 929 Local<Value> value,
930 const PropertyCallbackInfo<void>& info) { 930 const PropertyCallbackInfo<void>& info) {
931 Isolate* isolate = info.GetIsolate(); 931 Isolate* isolate = info.GetIsolate();
932 PerIsolateData* data = PerIsolateData::Get(isolate); 932 PerIsolateData* data = PerIsolateData::Get(isolate);
933 data->realm_shared_.Reset(isolate, value); 933 data->realm_shared_.Reset(isolate, value);
934 } 934 }
935 935
936 936 void WriteToFile(FILE* file, const v8::FunctionCallbackInfo<v8::Value>& args) {
937 void Shell::Print(const v8::FunctionCallbackInfo<v8::Value>& args) {
938 Write(args);
939 printf("\n");
940 fflush(stdout);
941 }
942
943
944 void Shell::Write(const v8::FunctionCallbackInfo<v8::Value>& args) {
945 for (int i = 0; i < args.Length(); i++) { 937 for (int i = 0; i < args.Length(); i++) {
946 HandleScope handle_scope(args.GetIsolate()); 938 HandleScope handle_scope(args.GetIsolate());
947 if (i != 0) { 939 if (i != 0) {
948 printf(" "); 940 fprintf(file, " ");
949 } 941 }
950 942
951 // Explicitly catch potential exceptions in toString(). 943 // Explicitly catch potential exceptions in toString().
952 v8::TryCatch try_catch(args.GetIsolate()); 944 v8::TryCatch try_catch(args.GetIsolate());
953 Local<Value> arg = args[i]; 945 Local<Value> arg = args[i];
954 Local<String> str_obj; 946 Local<String> str_obj;
955 947
956 if (arg->IsSymbol()) { 948 if (arg->IsSymbol()) {
957 arg = Local<Symbol>::Cast(arg)->Name(); 949 arg = Local<Symbol>::Cast(arg)->Name();
958 } 950 }
959 if (!arg->ToString(args.GetIsolate()->GetCurrentContext()) 951 if (!arg->ToString(args.GetIsolate()->GetCurrentContext())
960 .ToLocal(&str_obj)) { 952 .ToLocal(&str_obj)) {
961 try_catch.ReThrow(); 953 try_catch.ReThrow();
962 return; 954 return;
963 } 955 }
964 956
965 v8::String::Utf8Value str(str_obj); 957 v8::String::Utf8Value str(str_obj);
966 int n = static_cast<int>(fwrite(*str, sizeof(**str), str.length(), stdout)); 958 int n = static_cast<int>(fwrite(*str, sizeof(**str), str.length(), file));
967 if (n != str.length()) { 959 if (n != str.length()) {
968 printf("Error in fwrite\n"); 960 printf("Error in fwrite\n");
969 Exit(1); 961 Shell::Exit(1);
970 } 962 }
971 } 963 }
972 } 964 }
973 965
966 void WriteAndFlush(FILE* file,
967 const v8::FunctionCallbackInfo<v8::Value>& args) {
968 WriteToFile(file, args);
969 fprintf(file, "\n");
970 fflush(file);
971 }
972
973 void Shell::Print(const v8::FunctionCallbackInfo<v8::Value>& args) {
974 WriteAndFlush(stdout, args);
975 }
976
977 void Shell::PrintErr(const v8::FunctionCallbackInfo<v8::Value>& args) {
978 WriteAndFlush(stderr, args);
979 }
980
981 void Shell::Write(const v8::FunctionCallbackInfo<v8::Value>& args) {
982 WriteToFile(stdout, args);
983 }
974 984
975 void Shell::Read(const v8::FunctionCallbackInfo<v8::Value>& args) { 985 void Shell::Read(const v8::FunctionCallbackInfo<v8::Value>& args) {
976 String::Utf8Value file(args[0]); 986 String::Utf8Value file(args[0]);
977 if (*file == NULL) { 987 if (*file == NULL) {
978 Throw(args.GetIsolate(), "Error loading file"); 988 Throw(args.GetIsolate(), "Error loading file");
979 return; 989 return;
980 } 990 }
981 Local<String> source = ReadFile(args.GetIsolate(), *file); 991 Local<String> source = ReadFile(args.GetIsolate(), *file);
982 if (source.IsEmpty()) { 992 if (source.IsEmpty()) {
983 Throw(args.GetIsolate(), "Error loading file"); 993 Throw(args.GetIsolate(), "Error loading file");
(...skipping 396 matching lines...) Expand 10 before | Expand all | Expand 10 after
1380 } 1390 }
1381 1391
1382 1392
1383 Local<ObjectTemplate> Shell::CreateGlobalTemplate(Isolate* isolate) { 1393 Local<ObjectTemplate> Shell::CreateGlobalTemplate(Isolate* isolate) {
1384 Local<ObjectTemplate> global_template = ObjectTemplate::New(isolate); 1394 Local<ObjectTemplate> global_template = ObjectTemplate::New(isolate);
1385 global_template->Set( 1395 global_template->Set(
1386 String::NewFromUtf8(isolate, "print", NewStringType::kNormal) 1396 String::NewFromUtf8(isolate, "print", NewStringType::kNormal)
1387 .ToLocalChecked(), 1397 .ToLocalChecked(),
1388 FunctionTemplate::New(isolate, Print)); 1398 FunctionTemplate::New(isolate, Print));
1389 global_template->Set( 1399 global_template->Set(
1400 String::NewFromUtf8(isolate, "printErr", NewStringType::kNormal)
1401 .ToLocalChecked(),
1402 FunctionTemplate::New(isolate, PrintErr));
1403 global_template->Set(
1390 String::NewFromUtf8(isolate, "write", NewStringType::kNormal) 1404 String::NewFromUtf8(isolate, "write", NewStringType::kNormal)
1391 .ToLocalChecked(), 1405 .ToLocalChecked(),
1392 FunctionTemplate::New(isolate, Write)); 1406 FunctionTemplate::New(isolate, Write));
1393 global_template->Set( 1407 global_template->Set(
1394 String::NewFromUtf8(isolate, "read", NewStringType::kNormal) 1408 String::NewFromUtf8(isolate, "read", NewStringType::kNormal)
1395 .ToLocalChecked(), 1409 .ToLocalChecked(),
1396 FunctionTemplate::New(isolate, Read)); 1410 FunctionTemplate::New(isolate, Read));
1397 global_template->Set( 1411 global_template->Set(
1398 String::NewFromUtf8(isolate, "readbuffer", NewStringType::kNormal) 1412 String::NewFromUtf8(isolate, "readbuffer", NewStringType::kNormal)
1399 .ToLocalChecked(), 1413 .ToLocalChecked(),
(...skipping 1548 matching lines...) Expand 10 before | Expand all | Expand 10 after
2948 } 2962 }
2949 2963
2950 } // namespace v8 2964 } // namespace v8
2951 2965
2952 2966
2953 #ifndef GOOGLE3 2967 #ifndef GOOGLE3
2954 int main(int argc, char* argv[]) { 2968 int main(int argc, char* argv[]) {
2955 return v8::Shell::Main(argc, argv); 2969 return v8::Shell::Main(argc, argv);
2956 } 2970 }
2957 #endif 2971 #endif
OLDNEW
« 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