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

Side by Side Diff: samples/shell.cc

Issue 20081: Add explicit null checks after string conversions in the shells. (Closed) Base URL: http://v8.googlecode.com/svn/branches/bleeding_edge/
Patch Set: '' Created 11 years, 10 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 unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « include/v8.h ('k') | src/d8.cc » ('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 2008 the V8 project authors. All rights reserved. 1 // Copyright 2008 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are 3 // modification, are permitted provided that the following conditions are
4 // met: 4 // met:
5 // 5 //
6 // * Redistributions of source code must retain the above copyright 6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer. 7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above 8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following 9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided 10 // disclaimer in the documentation and/or other materials provided
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
92 } 92 }
93 if (!ExecuteString(source, file_name, false, true)) 93 if (!ExecuteString(source, file_name, false, true))
94 return 1; 94 return 1;
95 } 95 }
96 } 96 }
97 if (run_shell) RunShell(context); 97 if (run_shell) RunShell(context);
98 return 0; 98 return 0;
99 } 99 }
100 100
101 101
102 // Extracts a C string from a V8 Utf8Value.
103 const char* ToCString(const v8::String::Utf8Value& value) {
104 return *value ? *value : "<string conversion failed>";
105 }
106
107
102 // The callback that is invoked by v8 whenever the JavaScript 'print' 108 // The callback that is invoked by v8 whenever the JavaScript 'print'
103 // function is called. Prints its arguments on stdout separated by 109 // function is called. Prints its arguments on stdout separated by
104 // spaces and ending with a newline. 110 // spaces and ending with a newline.
105 v8::Handle<v8::Value> Print(const v8::Arguments& args) { 111 v8::Handle<v8::Value> Print(const v8::Arguments& args) {
106 bool first = true; 112 bool first = true;
107 for (int i = 0; i < args.Length(); i++) { 113 for (int i = 0; i < args.Length(); i++) {
108 v8::HandleScope handle_scope; 114 v8::HandleScope handle_scope;
109 if (first) { 115 if (first) {
110 first = false; 116 first = false;
111 } else { 117 } else {
112 printf(" "); 118 printf(" ");
113 } 119 }
114 v8::String::Utf8Value str(args[i]); 120 v8::String::Utf8Value str(args[i]);
115 printf("%s", *str); 121 const char* cstr = ToCString(str);
122 printf("%s", cstr);
116 } 123 }
117 printf("\n"); 124 printf("\n");
118 return v8::Undefined(); 125 return v8::Undefined();
119 } 126 }
120 127
121 128
122 // The callback that is invoked by v8 whenever the JavaScript 'load' 129 // The callback that is invoked by v8 whenever the JavaScript 'load'
123 // function is called. Loads, compiles and executes its argument 130 // function is called. Loads, compiles and executes its argument
124 // JavaScript file. 131 // JavaScript file.
125 v8::Handle<v8::Value> Load(const v8::Arguments& args) { 132 v8::Handle<v8::Value> Load(const v8::Arguments& args) {
126 for (int i = 0; i < args.Length(); i++) { 133 for (int i = 0; i < args.Length(); i++) {
127 v8::HandleScope handle_scope; 134 v8::HandleScope handle_scope;
128 v8::String::Utf8Value file(args[i]); 135 v8::String::Utf8Value file(args[i]);
136 if (*file == NULL) {
137 return v8::ThrowException(v8::String::New("Error loading file"));
138 }
129 v8::Handle<v8::String> source = ReadFile(*file); 139 v8::Handle<v8::String> source = ReadFile(*file);
130 if (source.IsEmpty()) { 140 if (source.IsEmpty()) {
131 return v8::ThrowException(v8::String::New("Error loading file")); 141 return v8::ThrowException(v8::String::New("Error loading file"));
132 } 142 }
133 if (!ExecuteString(source, v8::String::New(*file), false, false)) { 143 if (!ExecuteString(source, v8::String::New(*file), false, false)) {
134 return v8::ThrowException(v8::String::New("Error executing file")); 144 return v8::ThrowException(v8::String::New("Error executing file"));
135 } 145 }
136 } 146 }
137 return v8::Undefined(); 147 return v8::Undefined();
138 } 148 }
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
213 if (result.IsEmpty()) { 223 if (result.IsEmpty()) {
214 // Print errors that happened during execution. 224 // Print errors that happened during execution.
215 if (report_exceptions) 225 if (report_exceptions)
216 ReportException(&try_catch); 226 ReportException(&try_catch);
217 return false; 227 return false;
218 } else { 228 } else {
219 if (print_result && !result->IsUndefined()) { 229 if (print_result && !result->IsUndefined()) {
220 // If all went well and the result wasn't undefined then print 230 // If all went well and the result wasn't undefined then print
221 // the returned value. 231 // the returned value.
222 v8::String::Utf8Value str(result); 232 v8::String::Utf8Value str(result);
223 printf("%s\n", *str); 233 const char* cstr = ToCString(str);
234 printf("%s\n", cstr);
224 } 235 }
225 return true; 236 return true;
226 } 237 }
227 } 238 }
228 } 239 }
229 240
230 241
231 void ReportException(v8::TryCatch* try_catch) { 242 void ReportException(v8::TryCatch* try_catch) {
232 v8::HandleScope handle_scope; 243 v8::HandleScope handle_scope;
233 v8::String::Utf8Value exception(try_catch->Exception()); 244 v8::String::Utf8Value exception(try_catch->Exception());
245 const char* exception_string = ToCString(exception);
234 v8::Handle<v8::Message> message = try_catch->Message(); 246 v8::Handle<v8::Message> message = try_catch->Message();
235 if (message.IsEmpty()) { 247 if (message.IsEmpty()) {
236 // V8 didn't provide any extra information about this error; just 248 // V8 didn't provide any extra information about this error; just
237 // print the exception. 249 // print the exception.
238 printf("%s\n", *exception); 250 printf("%s\n", exception_string);
239 } else { 251 } else {
240 // Print (filename):(line number): (message). 252 // Print (filename):(line number): (message).
241 v8::String::Utf8Value filename(message->GetScriptResourceName()); 253 v8::String::Utf8Value filename(message->GetScriptResourceName());
254 const char* filename_string = ToCString(filename);
242 int linenum = message->GetLineNumber(); 255 int linenum = message->GetLineNumber();
243 printf("%s:%i: %s\n", *filename, linenum, *exception); 256 printf("%s:%i: %s\n", filename_string, linenum, exception_string);
244 // Print line of source code. 257 // Print line of source code.
245 v8::String::Utf8Value sourceline(message->GetSourceLine()); 258 v8::String::Utf8Value sourceline(message->GetSourceLine());
246 printf("%s\n", *sourceline); 259 const char* sourceline_string = ToCString(sourceline);
260 printf("%s\n", sourceline_string);
247 // Print wavy underline (GetUnderline is deprecated). 261 // Print wavy underline (GetUnderline is deprecated).
248 int start = message->GetStartColumn(); 262 int start = message->GetStartColumn();
249 for (int i = 0; i < start; i++) { 263 for (int i = 0; i < start; i++) {
250 printf(" "); 264 printf(" ");
251 } 265 }
252 int end = message->GetEndColumn(); 266 int end = message->GetEndColumn();
253 for (int i = start; i < end; i++) { 267 for (int i = start; i < end; i++) {
254 printf("^"); 268 printf("^");
255 } 269 }
256 printf("\n"); 270 printf("\n");
257 } 271 }
258 } 272 }
OLDNEW
« no previous file with comments | « include/v8.h ('k') | src/d8.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698