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

Side by Side Diff: src/d8.cc

Issue 2458963003: Add Shell::PrintErr and expose it in the d8 shell as printErr (Closed)
Patch Set: Add simple test for 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 Shell::Print(const v8::FunctionCallbackInfo<v8::Value>& args) {
967 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
968 printf("\n");
969 fflush(stdout);
970 }
971
972 void Shell::PrintErr(const v8::FunctionCallbackInfo<v8::Value>& args) {
973 WriteToFile(stderr, args);
974 fprintf(stderr, "\n");
975 fflush(stderr);
976 }
977
978 void Shell::Write(const v8::FunctionCallbackInfo<v8::Value>& args) {
979 WriteToFile(stdout, args);
980 }
974 981
975 void Shell::Read(const v8::FunctionCallbackInfo<v8::Value>& args) { 982 void Shell::Read(const v8::FunctionCallbackInfo<v8::Value>& args) {
976 String::Utf8Value file(args[0]); 983 String::Utf8Value file(args[0]);
977 if (*file == NULL) { 984 if (*file == NULL) {
978 Throw(args.GetIsolate(), "Error loading file"); 985 Throw(args.GetIsolate(), "Error loading file");
979 return; 986 return;
980 } 987 }
981 Local<String> source = ReadFile(args.GetIsolate(), *file); 988 Local<String> source = ReadFile(args.GetIsolate(), *file);
982 if (source.IsEmpty()) { 989 if (source.IsEmpty()) {
983 Throw(args.GetIsolate(), "Error loading file"); 990 Throw(args.GetIsolate(), "Error loading file");
(...skipping 396 matching lines...) Expand 10 before | Expand all | Expand 10 after
1380 } 1387 }
1381 1388
1382 1389
1383 Local<ObjectTemplate> Shell::CreateGlobalTemplate(Isolate* isolate) { 1390 Local<ObjectTemplate> Shell::CreateGlobalTemplate(Isolate* isolate) {
1384 Local<ObjectTemplate> global_template = ObjectTemplate::New(isolate); 1391 Local<ObjectTemplate> global_template = ObjectTemplate::New(isolate);
1385 global_template->Set( 1392 global_template->Set(
1386 String::NewFromUtf8(isolate, "print", NewStringType::kNormal) 1393 String::NewFromUtf8(isolate, "print", NewStringType::kNormal)
1387 .ToLocalChecked(), 1394 .ToLocalChecked(),
1388 FunctionTemplate::New(isolate, Print)); 1395 FunctionTemplate::New(isolate, Print));
1389 global_template->Set( 1396 global_template->Set(
1397 String::NewFromUtf8(isolate, "printErr", NewStringType::kNormal)
1398 .ToLocalChecked(),
1399 FunctionTemplate::New(isolate, PrintErr));
1400 global_template->Set(
1390 String::NewFromUtf8(isolate, "write", NewStringType::kNormal) 1401 String::NewFromUtf8(isolate, "write", NewStringType::kNormal)
1391 .ToLocalChecked(), 1402 .ToLocalChecked(),
1392 FunctionTemplate::New(isolate, Write)); 1403 FunctionTemplate::New(isolate, Write));
1393 global_template->Set( 1404 global_template->Set(
1394 String::NewFromUtf8(isolate, "read", NewStringType::kNormal) 1405 String::NewFromUtf8(isolate, "read", NewStringType::kNormal)
1395 .ToLocalChecked(), 1406 .ToLocalChecked(),
1396 FunctionTemplate::New(isolate, Read)); 1407 FunctionTemplate::New(isolate, Read));
1397 global_template->Set( 1408 global_template->Set(
1398 String::NewFromUtf8(isolate, "readbuffer", NewStringType::kNormal) 1409 String::NewFromUtf8(isolate, "readbuffer", NewStringType::kNormal)
1399 .ToLocalChecked(), 1410 .ToLocalChecked(),
(...skipping 1548 matching lines...) Expand 10 before | Expand all | Expand 10 after
2948 } 2959 }
2949 2960
2950 } // namespace v8 2961 } // namespace v8
2951 2962
2952 2963
2953 #ifndef GOOGLE3 2964 #ifndef GOOGLE3
2954 int main(int argc, char* argv[]) { 2965 int main(int argc, char* argv[]) {
2955 return v8::Shell::Main(argc, argv); 2966 return v8::Shell::Main(argc, argv);
2956 } 2967 }
2957 #endif 2968 #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