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

Side by Side Diff: samples/process.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 | « samples/lineprocessor.cc ('k') | samples/samples.gyp » ('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 143 matching lines...) Expand 10 before | Expand all | Expand 10 after
154 154
155 // Execute the script and fetch the Process method. 155 // Execute the script and fetch the Process method.
156 bool JsHttpRequestProcessor::Initialize(map<string, string>* opts, 156 bool JsHttpRequestProcessor::Initialize(map<string, string>* opts,
157 map<string, string>* output) { 157 map<string, string>* output) {
158 // Create a handle scope to hold the temporary references. 158 // Create a handle scope to hold the temporary references.
159 HandleScope handle_scope(GetIsolate()); 159 HandleScope handle_scope(GetIsolate());
160 160
161 // Create a template for the global object where we set the 161 // Create a template for the global object where we set the
162 // built-in global functions. 162 // built-in global functions.
163 Handle<ObjectTemplate> global = ObjectTemplate::New(); 163 Handle<ObjectTemplate> global = ObjectTemplate::New();
164 global->Set(String::New("log"), FunctionTemplate::New(LogCallback)); 164 global->Set(String::NewFromUtf8(GetIsolate(), "log"),
165 FunctionTemplate::New(LogCallback));
165 166
166 // Each processor gets its own context so different processors don't 167 // Each processor gets its own context so different processors don't
167 // affect each other. Context::New returns a persistent handle which 168 // affect each other. Context::New returns a persistent handle which
168 // is what we need for the reference to remain after we return from 169 // is what we need for the reference to remain after we return from
169 // this method. That persistent handle has to be disposed in the 170 // this method. That persistent handle has to be disposed in the
170 // destructor. 171 // destructor.
171 v8::Handle<v8::Context> context = Context::New(GetIsolate(), NULL, global); 172 v8::Handle<v8::Context> context = Context::New(GetIsolate(), NULL, global);
172 context_.Reset(GetIsolate(), context); 173 context_.Reset(GetIsolate(), context);
173 174
174 // Enter the new context so all the following operations take place 175 // Enter the new context so all the following operations take place
175 // within it. 176 // within it.
176 Context::Scope context_scope(context); 177 Context::Scope context_scope(context);
177 178
178 // Make the options mapping available within the context 179 // Make the options mapping available within the context
179 if (!InstallMaps(opts, output)) 180 if (!InstallMaps(opts, output))
180 return false; 181 return false;
181 182
182 // Compile and run the script 183 // Compile and run the script
183 if (!ExecuteScript(script_)) 184 if (!ExecuteScript(script_))
184 return false; 185 return false;
185 186
186 // The script compiled and ran correctly. Now we fetch out the 187 // The script compiled and ran correctly. Now we fetch out the
187 // Process function from the global object. 188 // Process function from the global object.
188 Handle<String> process_name = String::New("Process"); 189 Handle<String> process_name = String::NewFromUtf8(GetIsolate(), "Process");
189 Handle<Value> process_val = context->Global()->Get(process_name); 190 Handle<Value> process_val = context->Global()->Get(process_name);
190 191
191 // If there is no Process function, or if it is not a function, 192 // If there is no Process function, or if it is not a function,
192 // bail out 193 // bail out
193 if (!process_val->IsFunction()) return false; 194 if (!process_val->IsFunction()) return false;
194 195
195 // It is a function; cast it to a Function 196 // It is a function; cast it to a Function
196 Handle<Function> process_fun = Handle<Function>::Cast(process_val); 197 Handle<Function> process_fun = Handle<Function>::Cast(process_val);
197 198
198 // Store the function in a Persistent handle, since we also want 199 // Store the function in a Persistent handle, since we also want
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
237 map<string, string>* output) { 238 map<string, string>* output) {
238 HandleScope handle_scope(GetIsolate()); 239 HandleScope handle_scope(GetIsolate());
239 240
240 // Wrap the map object in a JavaScript wrapper 241 // Wrap the map object in a JavaScript wrapper
241 Handle<Object> opts_obj = WrapMap(opts); 242 Handle<Object> opts_obj = WrapMap(opts);
242 243
243 v8::Local<v8::Context> context = 244 v8::Local<v8::Context> context =
244 v8::Local<v8::Context>::New(GetIsolate(), context_); 245 v8::Local<v8::Context>::New(GetIsolate(), context_);
245 246
246 // Set the options object as a property on the global object. 247 // Set the options object as a property on the global object.
247 context->Global()->Set(String::New("options"), opts_obj); 248 context->Global()->Set(String::NewFromUtf8(GetIsolate(), "options"),
249 opts_obj);
248 250
249 Handle<Object> output_obj = WrapMap(output); 251 Handle<Object> output_obj = WrapMap(output);
250 context->Global()->Set(String::New("output"), output_obj); 252 context->Global()->Set(String::NewFromUtf8(GetIsolate(), "output"),
253 output_obj);
251 254
252 return true; 255 return true;
253 } 256 }
254 257
255 258
256 bool JsHttpRequestProcessor::Process(HttpRequest* request) { 259 bool JsHttpRequestProcessor::Process(HttpRequest* request) {
257 // Create a handle scope to keep the temporary object references. 260 // Create a handle scope to keep the temporary object references.
258 HandleScope handle_scope(GetIsolate()); 261 HandleScope handle_scope(GetIsolate());
259 262
260 v8::Local<v8::Context> context = 263 v8::Local<v8::Context> context =
(...skipping 23 matching lines...) Expand all
284 } else { 287 } else {
285 return true; 288 return true;
286 } 289 }
287 } 290 }
288 291
289 292
290 JsHttpRequestProcessor::~JsHttpRequestProcessor() { 293 JsHttpRequestProcessor::~JsHttpRequestProcessor() {
291 // Dispose the persistent handles. When noone else has any 294 // Dispose the persistent handles. When noone else has any
292 // references to the objects stored in the handles they will be 295 // references to the objects stored in the handles they will be
293 // automatically reclaimed. 296 // automatically reclaimed.
294 context_.Dispose(); 297 context_.Reset();
295 process_.Dispose(); 298 process_.Reset();
296 } 299 }
297 300
298 301
299 Persistent<ObjectTemplate> JsHttpRequestProcessor::request_template_; 302 Persistent<ObjectTemplate> JsHttpRequestProcessor::request_template_;
300 Persistent<ObjectTemplate> JsHttpRequestProcessor::map_template_; 303 Persistent<ObjectTemplate> JsHttpRequestProcessor::map_template_;
301 304
302 305
303 // ----------------------------------- 306 // -----------------------------------
304 // --- A c c e s s i n g M a p s --- 307 // --- A c c e s s i n g M a p s ---
305 // ----------------------------------- 308 // -----------------------------------
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
363 string key = ObjectToString(name); 366 string key = ObjectToString(name);
364 367
365 // Look up the value if it exists using the standard STL ideom. 368 // Look up the value if it exists using the standard STL ideom.
366 map<string, string>::iterator iter = obj->find(key); 369 map<string, string>::iterator iter = obj->find(key);
367 370
368 // If the key is not present return an empty handle as signal 371 // If the key is not present return an empty handle as signal
369 if (iter == obj->end()) return; 372 if (iter == obj->end()) return;
370 373
371 // Otherwise fetch the value and wrap it in a JavaScript string 374 // Otherwise fetch the value and wrap it in a JavaScript string
372 const string& value = (*iter).second; 375 const string& value = (*iter).second;
373 info.GetReturnValue().Set( 376 info.GetReturnValue().Set(String::NewFromUtf8(
374 String::New(value.c_str(), static_cast<int>(value.length()))); 377 info.GetIsolate(), value.c_str(), String::kNormalString,
378 static_cast<int>(value.length())));
375 } 379 }
376 380
377 381
378 void JsHttpRequestProcessor::MapSet(Local<String> name, 382 void JsHttpRequestProcessor::MapSet(Local<String> name,
379 Local<Value> value_obj, 383 Local<Value> value_obj,
380 const PropertyCallbackInfo<Value>& info) { 384 const PropertyCallbackInfo<Value>& info) {
381 // Fetch the map wrapped by this object. 385 // Fetch the map wrapped by this object.
382 map<string, string>* obj = UnwrapMap(info.Holder()); 386 map<string, string>* obj = UnwrapMap(info.Holder());
383 387
384 // Convert the key and value to std::strings. 388 // Convert the key and value to std::strings.
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
458 462
459 void JsHttpRequestProcessor::GetPath(Local<String> name, 463 void JsHttpRequestProcessor::GetPath(Local<String> name,
460 const PropertyCallbackInfo<Value>& info) { 464 const PropertyCallbackInfo<Value>& info) {
461 // Extract the C++ request object from the JavaScript wrapper. 465 // Extract the C++ request object from the JavaScript wrapper.
462 HttpRequest* request = UnwrapRequest(info.Holder()); 466 HttpRequest* request = UnwrapRequest(info.Holder());
463 467
464 // Fetch the path. 468 // Fetch the path.
465 const string& path = request->Path(); 469 const string& path = request->Path();
466 470
467 // Wrap the result in a JavaScript string and return it. 471 // Wrap the result in a JavaScript string and return it.
468 info.GetReturnValue().Set( 472 info.GetReturnValue().Set(String::NewFromUtf8(
469 String::New(path.c_str(), static_cast<int>(path.length()))); 473 info.GetIsolate(), path.c_str(), String::kNormalString,
474 static_cast<int>(path.length())));
470 } 475 }
471 476
472 477
473 void JsHttpRequestProcessor::GetReferrer( 478 void JsHttpRequestProcessor::GetReferrer(
474 Local<String> name, 479 Local<String> name,
475 const PropertyCallbackInfo<Value>& info) { 480 const PropertyCallbackInfo<Value>& info) {
476 HttpRequest* request = UnwrapRequest(info.Holder()); 481 HttpRequest* request = UnwrapRequest(info.Holder());
477 const string& path = request->Referrer(); 482 const string& path = request->Referrer();
478 info.GetReturnValue().Set( 483 info.GetReturnValue().Set(String::NewFromUtf8(
479 String::New(path.c_str(), static_cast<int>(path.length()))); 484 info.GetIsolate(), path.c_str(), String::kNormalString,
485 static_cast<int>(path.length())));
480 } 486 }
481 487
482 488
483 void JsHttpRequestProcessor::GetHost(Local<String> name, 489 void JsHttpRequestProcessor::GetHost(Local<String> name,
484 const PropertyCallbackInfo<Value>& info) { 490 const PropertyCallbackInfo<Value>& info) {
485 HttpRequest* request = UnwrapRequest(info.Holder()); 491 HttpRequest* request = UnwrapRequest(info.Holder());
486 const string& path = request->Host(); 492 const string& path = request->Host();
487 info.GetReturnValue().Set( 493 info.GetReturnValue().Set(String::NewFromUtf8(
488 String::New(path.c_str(), static_cast<int>(path.length()))); 494 info.GetIsolate(), path.c_str(), String::kNormalString,
495 static_cast<int>(path.length())));
489 } 496 }
490 497
491 498
492 void JsHttpRequestProcessor::GetUserAgent( 499 void JsHttpRequestProcessor::GetUserAgent(
493 Local<String> name, 500 Local<String> name,
494 const PropertyCallbackInfo<Value>& info) { 501 const PropertyCallbackInfo<Value>& info) {
495 HttpRequest* request = UnwrapRequest(info.Holder()); 502 HttpRequest* request = UnwrapRequest(info.Holder());
496 const string& path = request->UserAgent(); 503 const string& path = request->UserAgent();
497 info.GetReturnValue().Set( 504 info.GetReturnValue().Set(String::NewFromUtf8(
498 String::New(path.c_str(), static_cast<int>(path.length()))); 505 info.GetIsolate(), path.c_str(), String::kNormalString,
506 static_cast<int>(path.length())));
499 } 507 }
500 508
501 509
502 Handle<ObjectTemplate> JsHttpRequestProcessor::MakeRequestTemplate( 510 Handle<ObjectTemplate> JsHttpRequestProcessor::MakeRequestTemplate(
503 Isolate* isolate) { 511 Isolate* isolate) {
504 HandleScope handle_scope(isolate); 512 HandleScope handle_scope(isolate);
505 513
506 Handle<ObjectTemplate> result = ObjectTemplate::New(); 514 Handle<ObjectTemplate> result = ObjectTemplate::New();
507 result->SetInternalFieldCount(1); 515 result->SetInternalFieldCount(1);
508 516
509 // Add accessors for each of the fields of the request. 517 // Add accessors for each of the fields of the request.
510 result->SetAccessor(String::NewSymbol("path"), GetPath); 518 result->SetAccessor(
511 result->SetAccessor(String::NewSymbol("referrer"), GetReferrer); 519 String::NewFromUtf8(isolate, "path", String::kInternalizedString),
512 result->SetAccessor(String::NewSymbol("host"), GetHost); 520 GetPath);
513 result->SetAccessor(String::NewSymbol("userAgent"), GetUserAgent); 521 result->SetAccessor(
522 String::NewFromUtf8(isolate, "referrer", String::kInternalizedString),
523 GetReferrer);
524 result->SetAccessor(
525 String::NewFromUtf8(isolate, "host", String::kInternalizedString),
526 GetHost);
527 result->SetAccessor(
528 String::NewFromUtf8(isolate, "userAgent", String::kInternalizedString),
529 GetUserAgent);
514 530
515 // Again, return the result through the current handle scope. 531 // Again, return the result through the current handle scope.
516 return handle_scope.Close(result); 532 return handle_scope.Close(result);
517 } 533 }
518 534
519 535
520 // --- Test --- 536 // --- Test ---
521 537
522 538
523 void HttpRequestProcessor::Log(const char* event) { 539 void HttpRequestProcessor::Log(const char* event) {
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
568 } else { 584 } else {
569 string key = arg.substr(0, index); 585 string key = arg.substr(0, index);
570 string value = arg.substr(index+1); 586 string value = arg.substr(index+1);
571 options[key] = value; 587 options[key] = value;
572 } 588 }
573 } 589 }
574 } 590 }
575 591
576 592
577 // Reads a file into a v8 string. 593 // Reads a file into a v8 string.
578 Handle<String> ReadFile(const string& name) { 594 Handle<String> ReadFile(Isolate* isolate, const string& name) {
579 FILE* file = fopen(name.c_str(), "rb"); 595 FILE* file = fopen(name.c_str(), "rb");
580 if (file == NULL) return Handle<String>(); 596 if (file == NULL) return Handle<String>();
581 597
582 fseek(file, 0, SEEK_END); 598 fseek(file, 0, SEEK_END);
583 int size = ftell(file); 599 int size = ftell(file);
584 rewind(file); 600 rewind(file);
585 601
586 char* chars = new char[size + 1]; 602 char* chars = new char[size + 1];
587 chars[size] = '\0'; 603 chars[size] = '\0';
588 for (int i = 0; i < size;) { 604 for (int i = 0; i < size;) {
589 int read = static_cast<int>(fread(&chars[i], 1, size - i, file)); 605 int read = static_cast<int>(fread(&chars[i], 1, size - i, file));
590 i += read; 606 i += read;
591 } 607 }
592 fclose(file); 608 fclose(file);
593 Handle<String> result = String::New(chars, size); 609 Handle<String> result =
610 String::NewFromUtf8(isolate, chars, String::kNormalString, size);
594 delete[] chars; 611 delete[] chars;
595 return result; 612 return result;
596 } 613 }
597 614
598 615
599 const int kSampleSize = 6; 616 const int kSampleSize = 6;
600 StringHttpRequest kSampleRequests[kSampleSize] = { 617 StringHttpRequest kSampleRequests[kSampleSize] = {
601 StringHttpRequest("/process.cc", "localhost", "google.com", "firefox"), 618 StringHttpRequest("/process.cc", "localhost", "google.com", "firefox"),
602 StringHttpRequest("/", "localhost", "google.net", "firefox"), 619 StringHttpRequest("/", "localhost", "google.net", "firefox"),
603 StringHttpRequest("/", "localhost", "google.org", "safari"), 620 StringHttpRequest("/", "localhost", "google.org", "safari"),
(...skipping 25 matching lines...) Expand all
629 v8::V8::InitializeICU(); 646 v8::V8::InitializeICU();
630 map<string, string> options; 647 map<string, string> options;
631 string file; 648 string file;
632 ParseOptions(argc, argv, options, &file); 649 ParseOptions(argc, argv, options, &file);
633 if (file.empty()) { 650 if (file.empty()) {
634 fprintf(stderr, "No script was specified.\n"); 651 fprintf(stderr, "No script was specified.\n");
635 return 1; 652 return 1;
636 } 653 }
637 Isolate* isolate = Isolate::GetCurrent(); 654 Isolate* isolate = Isolate::GetCurrent();
638 HandleScope scope(isolate); 655 HandleScope scope(isolate);
639 Handle<String> source = ReadFile(file); 656 Handle<String> source = ReadFile(isolate, file);
640 if (source.IsEmpty()) { 657 if (source.IsEmpty()) {
641 fprintf(stderr, "Error reading '%s'.\n", file.c_str()); 658 fprintf(stderr, "Error reading '%s'.\n", file.c_str());
642 return 1; 659 return 1;
643 } 660 }
644 JsHttpRequestProcessor processor(isolate, source); 661 JsHttpRequestProcessor processor(isolate, source);
645 map<string, string> output; 662 map<string, string> output;
646 if (!processor.Initialize(&options, &output)) { 663 if (!processor.Initialize(&options, &output)) {
647 fprintf(stderr, "Error initializing processor.\n"); 664 fprintf(stderr, "Error initializing processor.\n");
648 return 1; 665 return 1;
649 } 666 }
650 if (!ProcessEntries(&processor, kSampleSize, kSampleRequests)) 667 if (!ProcessEntries(&processor, kSampleSize, kSampleRequests))
651 return 1; 668 return 1;
652 PrintMap(&output); 669 PrintMap(&output);
653 } 670 }
OLDNEW
« no previous file with comments | « samples/lineprocessor.cc ('k') | samples/samples.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698