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

Side by Side Diff: samples/lineprocessor.cc

Issue 83313002: Remove usage of deprecated APIs from samples (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Created 7 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 | Annotate | Revision Log
« no previous file with comments | « no previous file | samples/process.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 2012 the V8 project authors. All rights reserved. 1 // Copyright 2012 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 * fixes this issue. 92 * fixes this issue.
93 */ 93 */
94 94
95 enum MainCycleType { 95 enum MainCycleType {
96 CycleInCpp, 96 CycleInCpp,
97 CycleInJs 97 CycleInJs
98 }; 98 };
99 99
100 const char* ToCString(const v8::String::Utf8Value& value); 100 const char* ToCString(const v8::String::Utf8Value& value);
101 void ReportException(v8::Isolate* isolate, v8::TryCatch* handler); 101 void ReportException(v8::Isolate* isolate, v8::TryCatch* handler);
102 v8::Handle<v8::String> ReadFile(const char* name); 102 v8::Handle<v8::String> ReadFile(v8::Isolate* isolate, const char* name);
103 v8::Handle<v8::String> ReadLine(); 103 v8::Handle<v8::String> ReadLine();
104 104
105 void Print(const v8::FunctionCallbackInfo<v8::Value>& args); 105 void Print(const v8::FunctionCallbackInfo<v8::Value>& args);
106 void ReadLine(const v8::FunctionCallbackInfo<v8::Value>& args); 106 void ReadLine(const v8::FunctionCallbackInfo<v8::Value>& args);
107 bool RunCppCycle(v8::Handle<v8::Script> script, 107 bool RunCppCycle(v8::Handle<v8::Script> script,
108 v8::Local<v8::Context> context, 108 v8::Local<v8::Context> context,
109 bool report_exceptions); 109 bool report_exceptions);
110 110
111 111
112 #ifdef ENABLE_DEBUGGER_SUPPORT 112 #ifdef ENABLE_DEBUGGER_SUPPORT
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
167 support_callback = true; 167 support_callback = true;
168 } else if (strcmp(str, "--wait-for-connection") == 0) { 168 } else if (strcmp(str, "--wait-for-connection") == 0) {
169 wait_for_connection = true; 169 wait_for_connection = true;
170 } else if (strcmp(str, "-p") == 0 && i + 1 < argc) { 170 } else if (strcmp(str, "-p") == 0 && i + 1 < argc) {
171 port_number = atoi(argv[i + 1]); // NOLINT 171 port_number = atoi(argv[i + 1]); // NOLINT
172 i++; 172 i++;
173 #endif // ENABLE_DEBUGGER_SUPPORT 173 #endif // ENABLE_DEBUGGER_SUPPORT
174 } else if (strncmp(str, "--", 2) == 0) { 174 } else if (strncmp(str, "--", 2) == 0) {
175 printf("Warning: unknown flag %s.\nTry --help for options\n", str); 175 printf("Warning: unknown flag %s.\nTry --help for options\n", str);
176 } else if (strcmp(str, "-e") == 0 && i + 1 < argc) { 176 } else if (strcmp(str, "-e") == 0 && i + 1 < argc) {
177 script_source = v8::String::New(argv[i + 1]); 177 script_source = v8::String::NewFromUtf8(isolate, argv[i + 1]);
178 script_name = v8::String::New("unnamed"); 178 script_name = v8::String::NewFromUtf8(isolate, "unnamed");
179 i++; 179 i++;
180 script_param_counter++; 180 script_param_counter++;
181 } else { 181 } else {
182 // Use argument as a name of file to load. 182 // Use argument as a name of file to load.
183 script_source = ReadFile(str); 183 script_source = ReadFile(isolate, str);
184 script_name = v8::String::New(str); 184 script_name = v8::String::NewFromUtf8(isolate, str);
185 if (script_source.IsEmpty()) { 185 if (script_source.IsEmpty()) {
186 printf("Error reading '%s'\n", str); 186 printf("Error reading '%s'\n", str);
187 return 1; 187 return 1;
188 } 188 }
189 script_param_counter++; 189 script_param_counter++;
190 } 190 }
191 } 191 }
192 192
193 if (script_param_counter == 0) { 193 if (script_param_counter == 0) {
194 printf("Script is not specified\n"); 194 printf("Script is not specified\n");
195 return 1; 195 return 1;
196 } 196 }
197 if (script_param_counter != 1) { 197 if (script_param_counter != 1) {
198 printf("Only one script may be specified\n"); 198 printf("Only one script may be specified\n");
199 return 1; 199 return 1;
200 } 200 }
201 201
202 // Create a template for the global object. 202 // Create a template for the global object.
203 v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New(); 203 v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New();
204 204
205 // Bind the global 'print' function to the C++ Print callback. 205 // Bind the global 'print' function to the C++ Print callback.
206 global->Set(v8::String::New("print"), v8::FunctionTemplate::New(Print)); 206 global->Set(v8::String::NewFromUtf8(isolate, "print"),
207 v8::FunctionTemplate::New(Print));
207 208
208 if (cycle_type == CycleInJs) { 209 if (cycle_type == CycleInJs) {
209 // Bind the global 'read_line' function to the C++ Print callback. 210 // Bind the global 'read_line' function to the C++ Print callback.
210 global->Set(v8::String::New("read_line"), 211 global->Set(v8::String::NewFromUtf8(isolate, "read_line"),
211 v8::FunctionTemplate::New(ReadLine)); 212 v8::FunctionTemplate::New(ReadLine));
212 } 213 }
213 214
214 // Create a new execution environment containing the built-in 215 // Create a new execution environment containing the built-in
215 // functions 216 // functions
216 v8::Handle<v8::Context> context = v8::Context::New(isolate, NULL, global); 217 v8::Handle<v8::Context> context = v8::Context::New(isolate, NULL, global);
217 // Enter the newly created execution environment. 218 // Enter the newly created execution environment.
218 v8::Context::Scope context_scope(context); 219 v8::Context::Scope context_scope(context);
219 220
220 #ifdef ENABLE_DEBUGGER_SUPPORT 221 #ifdef ENABLE_DEBUGGER_SUPPORT
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
270 271
271 272
272 bool RunCppCycle(v8::Handle<v8::Script> script, 273 bool RunCppCycle(v8::Handle<v8::Script> script,
273 v8::Local<v8::Context> context, 274 v8::Local<v8::Context> context,
274 bool report_exceptions) { 275 bool report_exceptions) {
275 v8::Isolate* isolate = context->GetIsolate(); 276 v8::Isolate* isolate = context->GetIsolate();
276 #ifdef ENABLE_DEBUGGER_SUPPORT 277 #ifdef ENABLE_DEBUGGER_SUPPORT
277 v8::Locker lock(isolate); 278 v8::Locker lock(isolate);
278 #endif // ENABLE_DEBUGGER_SUPPORT 279 #endif // ENABLE_DEBUGGER_SUPPORT
279 280
280 v8::Handle<v8::String> fun_name = v8::String::New("ProcessLine"); 281 v8::Handle<v8::String> fun_name =
282 v8::String::NewFromUtf8(isolate, "ProcessLine");
281 v8::Handle<v8::Value> process_val = context->Global()->Get(fun_name); 283 v8::Handle<v8::Value> process_val = context->Global()->Get(fun_name);
282 284
283 // If there is no Process function, or if it is not a function, 285 // If there is no Process function, or if it is not a function,
284 // bail out 286 // bail out
285 if (!process_val->IsFunction()) { 287 if (!process_val->IsFunction()) {
286 printf("Error: Script does not declare 'ProcessLine' global function.\n"); 288 printf("Error: Script does not declare 'ProcessLine' global function.\n");
287 return 1; 289 return 1;
288 } 290 }
289 291
290 // It is a function; cast it to a Function 292 // It is a function; cast it to a Function
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
331 } 333 }
332 334
333 335
334 // Extracts a C string from a V8 Utf8Value. 336 // Extracts a C string from a V8 Utf8Value.
335 const char* ToCString(const v8::String::Utf8Value& value) { 337 const char* ToCString(const v8::String::Utf8Value& value) {
336 return *value ? *value : "<string conversion failed>"; 338 return *value ? *value : "<string conversion failed>";
337 } 339 }
338 340
339 341
340 // Reads a file into a v8 string. 342 // Reads a file into a v8 string.
341 v8::Handle<v8::String> ReadFile(const char* name) { 343 v8::Handle<v8::String> ReadFile(v8::Isolate* isolate, const char* name) {
342 FILE* file = fopen(name, "rb"); 344 FILE* file = fopen(name, "rb");
343 if (file == NULL) return v8::Handle<v8::String>(); 345 if (file == NULL) return v8::Handle<v8::String>();
344 346
345 fseek(file, 0, SEEK_END); 347 fseek(file, 0, SEEK_END);
346 int size = ftell(file); 348 int size = ftell(file);
347 rewind(file); 349 rewind(file);
348 350
349 char* chars = new char[size + 1]; 351 char* chars = new char[size + 1];
350 chars[size] = '\0'; 352 chars[size] = '\0';
351 for (int i = 0; i < size;) { 353 for (int i = 0; i < size;) {
352 int read = static_cast<int>(fread(&chars[i], 1, size - i, file)); 354 int read = static_cast<int>(fread(&chars[i], 1, size - i, file));
353 i += read; 355 i += read;
354 } 356 }
355 fclose(file); 357 fclose(file);
356 v8::Handle<v8::String> result = v8::String::New(chars, size); 358 v8::Handle<v8::String> result =
359 v8::String::NewFromUtf8(isolate, chars, v8::String::kNormalString, size);
357 delete[] chars; 360 delete[] chars;
358 return result; 361 return result;
359 } 362 }
360 363
361 364
362 void ReportException(v8::Isolate* isolate, v8::TryCatch* try_catch) { 365 void ReportException(v8::Isolate* isolate, v8::TryCatch* try_catch) {
363 v8::HandleScope handle_scope(isolate); 366 v8::HandleScope handle_scope(isolate);
364 v8::String::Utf8Value exception(try_catch->Exception()); 367 v8::String::Utf8Value exception(try_catch->Exception());
365 const char* exception_string = ToCString(exception); 368 const char* exception_string = ToCString(exception);
366 v8::Handle<v8::Message> message = try_catch->Message(); 369 v8::Handle<v8::Message> message = try_catch->Message();
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
410 } 413 }
411 printf("\n"); 414 printf("\n");
412 fflush(stdout); 415 fflush(stdout);
413 } 416 }
414 417
415 418
416 // The callback that is invoked by v8 whenever the JavaScript 'read_line' 419 // The callback that is invoked by v8 whenever the JavaScript 'read_line'
417 // function is called. Reads a string from standard input and returns. 420 // function is called. Reads a string from standard input and returns.
418 void ReadLine(const v8::FunctionCallbackInfo<v8::Value>& args) { 421 void ReadLine(const v8::FunctionCallbackInfo<v8::Value>& args) {
419 if (args.Length() > 0) { 422 if (args.Length() > 0) {
420 args.GetIsolate()->ThrowException(v8::String::New("Unexpected arguments")); 423 args.GetIsolate()->ThrowException(
424 v8::String::NewFromUtf8(args.GetIsolate(), "Unexpected arguments"));
421 return; 425 return;
422 } 426 }
423 args.GetReturnValue().Set(ReadLine()); 427 args.GetReturnValue().Set(ReadLine());
424 } 428 }
425 429
426 430
427 v8::Handle<v8::String> ReadLine() { 431 v8::Handle<v8::String> ReadLine() {
428 const int kBufferSize = 1024 + 1; 432 const int kBufferSize = 1024 + 1;
429 char buffer[kBufferSize]; 433 char buffer[kBufferSize];
430 434
431 char* res; 435 char* res;
432 { 436 {
433 #ifdef ENABLE_DEBUGGER_SUPPORT 437 #ifdef ENABLE_DEBUGGER_SUPPORT
434 v8::Unlocker unlocker(v8::Isolate::GetCurrent()); 438 v8::Unlocker unlocker(v8::Isolate::GetCurrent());
435 #endif // ENABLE_DEBUGGER_SUPPORT 439 #endif // ENABLE_DEBUGGER_SUPPORT
436 res = fgets(buffer, kBufferSize, stdin); 440 res = fgets(buffer, kBufferSize, stdin);
437 } 441 }
442 v8::Isolate* isolate = v8::Isolate::GetCurrent();
438 if (res == NULL) { 443 if (res == NULL) {
439 v8::Handle<v8::Primitive> t = v8::Undefined(v8::Isolate::GetCurrent()); 444 v8::Handle<v8::Primitive> t = v8::Undefined(isolate);
440 return v8::Handle<v8::String>::Cast(t); 445 return v8::Handle<v8::String>::Cast(t);
441 } 446 }
442 // Remove newline char 447 // Remove newline char
443 for (char* pos = buffer; *pos != '\0'; pos++) { 448 for (char* pos = buffer; *pos != '\0'; pos++) {
444 if (*pos == '\n') { 449 if (*pos == '\n') {
445 *pos = '\0'; 450 *pos = '\0';
446 break; 451 break;
447 } 452 }
448 } 453 }
449 return v8::String::New(buffer); 454 return v8::String::NewFromUtf8(isolate, buffer);
450 } 455 }
OLDNEW
« no previous file with comments | « no previous file | samples/process.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698