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

Side by Side Diff: samples/process.cc

Issue 15793007: Cutover v8 to use new style callbacks internally (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Created 7 years, 6 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 | « samples/lineprocessor.cc ('k') | samples/shell.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 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
95 // Wrap the options and output map in a JavaScript objects and 95 // Wrap the options and output map in a JavaScript objects and
96 // install it in the global namespace as 'options' and 'output'. 96 // install it in the global namespace as 'options' and 'output'.
97 bool InstallMaps(map<string, string>* opts, map<string, string>* output); 97 bool InstallMaps(map<string, string>* opts, map<string, string>* output);
98 98
99 // Constructs the template that describes the JavaScript wrapper 99 // Constructs the template that describes the JavaScript wrapper
100 // type for requests. 100 // type for requests.
101 static Handle<ObjectTemplate> MakeRequestTemplate(Isolate* isolate); 101 static Handle<ObjectTemplate> MakeRequestTemplate(Isolate* isolate);
102 static Handle<ObjectTemplate> MakeMapTemplate(Isolate* isolate); 102 static Handle<ObjectTemplate> MakeMapTemplate(Isolate* isolate);
103 103
104 // Callbacks that access the individual fields of request objects. 104 // Callbacks that access the individual fields of request objects.
105 static Handle<Value> GetPath(Local<String> name, const AccessorInfo& info); 105 static void GetPath(Local<String> name,
106 static Handle<Value> GetReferrer(Local<String> name, 106 const PropertyCallbackInfo<Value>& info);
107 const AccessorInfo& info); 107 static void GetReferrer(Local<String> name,
108 static Handle<Value> GetHost(Local<String> name, const AccessorInfo& info); 108 const PropertyCallbackInfo<Value>& info);
109 static Handle<Value> GetUserAgent(Local<String> name, 109 static void GetHost(Local<String> name,
110 const AccessorInfo& info); 110 const PropertyCallbackInfo<Value>& info);
111 static void GetUserAgent(Local<String> name,
112 const PropertyCallbackInfo<Value>& info);
111 113
112 // Callbacks that access maps 114 // Callbacks that access maps
113 static Handle<Value> MapGet(Local<String> name, const AccessorInfo& info); 115 static void MapGet(Local<String> name,
114 static Handle<Value> MapSet(Local<String> name, 116 const PropertyCallbackInfo<Value>& info);
115 Local<Value> value, 117 static void MapSet(Local<String> name,
116 const AccessorInfo& info); 118 Local<Value> value,
119 const PropertyCallbackInfo<Value>& info);
117 120
118 // Utility methods for wrapping C++ objects as JavaScript objects, 121 // Utility methods for wrapping C++ objects as JavaScript objects,
119 // and going back again. 122 // and going back again.
120 Handle<Object> WrapMap(map<string, string>* obj); 123 Handle<Object> WrapMap(map<string, string>* obj);
121 static map<string, string>* UnwrapMap(Handle<Object> obj); 124 static map<string, string>* UnwrapMap(Handle<Object> obj);
122 Handle<Object> WrapRequest(HttpRequest* obj); 125 Handle<Object> WrapRequest(HttpRequest* obj);
123 static HttpRequest* UnwrapRequest(Handle<Object> obj); 126 static HttpRequest* UnwrapRequest(Handle<Object> obj);
124 127
125 Isolate* GetIsolate() { return isolate_; } 128 Isolate* GetIsolate() { return isolate_; }
126 129
127 Isolate* isolate_; 130 Isolate* isolate_;
128 Handle<String> script_; 131 Handle<String> script_;
129 Persistent<Context> context_; 132 Persistent<Context> context_;
130 Persistent<Function> process_; 133 Persistent<Function> process_;
131 static Persistent<ObjectTemplate> request_template_; 134 static Persistent<ObjectTemplate> request_template_;
132 static Persistent<ObjectTemplate> map_template_; 135 static Persistent<ObjectTemplate> map_template_;
133 }; 136 };
134 137
135 // ------------------------- 138 // -------------------------
136 // --- P r o c e s s o r --- 139 // --- P r o c e s s o r ---
137 // ------------------------- 140 // -------------------------
138 141
139 142
140 static Handle<Value> LogCallback(const Arguments& args) { 143 static void LogCallback(const v8::FunctionCallbackInfo<v8::Value>& args) {
141 if (args.Length() < 1) return Undefined(); 144 if (args.Length() < 1) return;
142 HandleScope scope(args.GetIsolate()); 145 HandleScope scope(args.GetIsolate());
143 Handle<Value> arg = args[0]; 146 Handle<Value> arg = args[0];
144 String::Utf8Value value(arg); 147 String::Utf8Value value(arg);
145 HttpRequestProcessor::Log(*value); 148 HttpRequestProcessor::Log(*value);
146 return Undefined();
147 } 149 }
148 150
149 151
150 // Execute the script and fetch the Process method. 152 // Execute the script and fetch the Process method.
151 bool JsHttpRequestProcessor::Initialize(map<string, string>* opts, 153 bool JsHttpRequestProcessor::Initialize(map<string, string>* opts,
152 map<string, string>* output) { 154 map<string, string>* output) {
153 // Create a handle scope to hold the temporary references. 155 // Create a handle scope to hold the temporary references.
154 HandleScope handle_scope(GetIsolate()); 156 HandleScope handle_scope(GetIsolate());
155 157
156 // Create a template for the global object where we set the 158 // Create a template for the global object where we set the
(...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after
343 345
344 346
345 // Convert a JavaScript string to a std::string. To not bother too 347 // Convert a JavaScript string to a std::string. To not bother too
346 // much with string encodings we just use ascii. 348 // much with string encodings we just use ascii.
347 string ObjectToString(Local<Value> value) { 349 string ObjectToString(Local<Value> value) {
348 String::Utf8Value utf8_value(value); 350 String::Utf8Value utf8_value(value);
349 return string(*utf8_value); 351 return string(*utf8_value);
350 } 352 }
351 353
352 354
353 Handle<Value> JsHttpRequestProcessor::MapGet(Local<String> name, 355 void JsHttpRequestProcessor::MapGet(Local<String> name,
354 const AccessorInfo& info) { 356 const PropertyCallbackInfo<Value>& info) {
355 // Fetch the map wrapped by this object. 357 // Fetch the map wrapped by this object.
356 map<string, string>* obj = UnwrapMap(info.Holder()); 358 map<string, string>* obj = UnwrapMap(info.Holder());
357 359
358 // Convert the JavaScript string to a std::string. 360 // Convert the JavaScript string to a std::string.
359 string key = ObjectToString(name); 361 string key = ObjectToString(name);
360 362
361 // Look up the value if it exists using the standard STL ideom. 363 // Look up the value if it exists using the standard STL ideom.
362 map<string, string>::iterator iter = obj->find(key); 364 map<string, string>::iterator iter = obj->find(key);
363 365
364 // If the key is not present return an empty handle as signal 366 // If the key is not present return an empty handle as signal
365 if (iter == obj->end()) return Handle<Value>(); 367 if (iter == obj->end()) return;
366 368
367 // Otherwise fetch the value and wrap it in a JavaScript string 369 // Otherwise fetch the value and wrap it in a JavaScript string
368 const string& value = (*iter).second; 370 const string& value = (*iter).second;
369 return String::New(value.c_str(), static_cast<int>(value.length())); 371 info.GetReturnValue().Set(
372 String::New(value.c_str(), static_cast<int>(value.length())));
370 } 373 }
371 374
372 375
373 Handle<Value> JsHttpRequestProcessor::MapSet(Local<String> name, 376 void JsHttpRequestProcessor::MapSet(Local<String> name,
374 Local<Value> value_obj, 377 Local<Value> value_obj,
375 const AccessorInfo& info) { 378 const PropertyCallbackInfo<Value>& info) {
376 // Fetch the map wrapped by this object. 379 // Fetch the map wrapped by this object.
377 map<string, string>* obj = UnwrapMap(info.Holder()); 380 map<string, string>* obj = UnwrapMap(info.Holder());
378 381
379 // Convert the key and value to std::strings. 382 // Convert the key and value to std::strings.
380 string key = ObjectToString(name); 383 string key = ObjectToString(name);
381 string value = ObjectToString(value_obj); 384 string value = ObjectToString(value_obj);
382 385
383 // Update the map. 386 // Update the map.
384 (*obj)[key] = value; 387 (*obj)[key] = value;
385 388
386 // Return the value; any non-empty handle will work. 389 // Return the value; any non-empty handle will work.
387 return value_obj; 390 info.GetReturnValue().Set(value_obj);
388 } 391 }
389 392
390 393
391 Handle<ObjectTemplate> JsHttpRequestProcessor::MakeMapTemplate( 394 Handle<ObjectTemplate> JsHttpRequestProcessor::MakeMapTemplate(
392 Isolate* isolate) { 395 Isolate* isolate) {
393 HandleScope handle_scope(isolate); 396 HandleScope handle_scope(isolate);
394 397
395 Handle<ObjectTemplate> result = ObjectTemplate::New(); 398 Handle<ObjectTemplate> result = ObjectTemplate::New();
396 result->SetInternalFieldCount(1); 399 result->SetInternalFieldCount(1);
397 result->SetNamedPropertyHandler(MapGet, MapSet); 400 result->SetNamedPropertyHandler(MapGet, MapSet);
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
444 * Utility function that extracts the C++ http request object from a 447 * Utility function that extracts the C++ http request object from a
445 * wrapper object. 448 * wrapper object.
446 */ 449 */
447 HttpRequest* JsHttpRequestProcessor::UnwrapRequest(Handle<Object> obj) { 450 HttpRequest* JsHttpRequestProcessor::UnwrapRequest(Handle<Object> obj) {
448 Handle<External> field = Handle<External>::Cast(obj->GetInternalField(0)); 451 Handle<External> field = Handle<External>::Cast(obj->GetInternalField(0));
449 void* ptr = field->Value(); 452 void* ptr = field->Value();
450 return static_cast<HttpRequest*>(ptr); 453 return static_cast<HttpRequest*>(ptr);
451 } 454 }
452 455
453 456
454 Handle<Value> JsHttpRequestProcessor::GetPath(Local<String> name, 457 void JsHttpRequestProcessor::GetPath(Local<String> name,
455 const AccessorInfo& info) { 458 const PropertyCallbackInfo<Value>& info) {
456 // Extract the C++ request object from the JavaScript wrapper. 459 // Extract the C++ request object from the JavaScript wrapper.
457 HttpRequest* request = UnwrapRequest(info.Holder()); 460 HttpRequest* request = UnwrapRequest(info.Holder());
458 461
459 // Fetch the path. 462 // Fetch the path.
460 const string& path = request->Path(); 463 const string& path = request->Path();
461 464
462 // Wrap the result in a JavaScript string and return it. 465 // Wrap the result in a JavaScript string and return it.
463 return String::New(path.c_str(), static_cast<int>(path.length())); 466 info.GetReturnValue().Set(
467 String::New(path.c_str(), static_cast<int>(path.length())));
464 } 468 }
465 469
466 470
467 Handle<Value> JsHttpRequestProcessor::GetReferrer(Local<String> name, 471 void JsHttpRequestProcessor::GetReferrer(
468 const AccessorInfo& info) { 472 Local<String> name,
473 const PropertyCallbackInfo<Value>& info) {
469 HttpRequest* request = UnwrapRequest(info.Holder()); 474 HttpRequest* request = UnwrapRequest(info.Holder());
470 const string& path = request->Referrer(); 475 const string& path = request->Referrer();
471 return String::New(path.c_str(), static_cast<int>(path.length())); 476 info.GetReturnValue().Set(
477 String::New(path.c_str(), static_cast<int>(path.length())));
472 } 478 }
473 479
474 480
475 Handle<Value> JsHttpRequestProcessor::GetHost(Local<String> name, 481 void JsHttpRequestProcessor::GetHost(Local<String> name,
476 const AccessorInfo& info) { 482 const PropertyCallbackInfo<Value>& info) {
477 HttpRequest* request = UnwrapRequest(info.Holder()); 483 HttpRequest* request = UnwrapRequest(info.Holder());
478 const string& path = request->Host(); 484 const string& path = request->Host();
479 return String::New(path.c_str(), static_cast<int>(path.length())); 485 info.GetReturnValue().Set(
486 String::New(path.c_str(), static_cast<int>(path.length())));
480 } 487 }
481 488
482 489
483 Handle<Value> JsHttpRequestProcessor::GetUserAgent(Local<String> name, 490 void JsHttpRequestProcessor::GetUserAgent(
484 const AccessorInfo& info) { 491 Local<String> name,
492 const PropertyCallbackInfo<Value>& info) {
485 HttpRequest* request = UnwrapRequest(info.Holder()); 493 HttpRequest* request = UnwrapRequest(info.Holder());
486 const string& path = request->UserAgent(); 494 const string& path = request->UserAgent();
487 return String::New(path.c_str(), static_cast<int>(path.length())); 495 info.GetReturnValue().Set(
496 String::New(path.c_str(), static_cast<int>(path.length())));
488 } 497 }
489 498
490 499
491 Handle<ObjectTemplate> JsHttpRequestProcessor::MakeRequestTemplate( 500 Handle<ObjectTemplate> JsHttpRequestProcessor::MakeRequestTemplate(
492 Isolate* isolate) { 501 Isolate* isolate) {
493 HandleScope handle_scope(isolate); 502 HandleScope handle_scope(isolate);
494 503
495 Handle<ObjectTemplate> result = ObjectTemplate::New(); 504 Handle<ObjectTemplate> result = ObjectTemplate::New();
496 result->SetInternalFieldCount(1); 505 result->SetInternalFieldCount(1);
497 506
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after
632 JsHttpRequestProcessor processor(isolate, source); 641 JsHttpRequestProcessor processor(isolate, source);
633 map<string, string> output; 642 map<string, string> output;
634 if (!processor.Initialize(&options, &output)) { 643 if (!processor.Initialize(&options, &output)) {
635 fprintf(stderr, "Error initializing processor.\n"); 644 fprintf(stderr, "Error initializing processor.\n");
636 return 1; 645 return 1;
637 } 646 }
638 if (!ProcessEntries(&processor, kSampleSize, kSampleRequests)) 647 if (!ProcessEntries(&processor, kSampleSize, kSampleRequests))
639 return 1; 648 return 1;
640 PrintMap(&output); 649 PrintMap(&output);
641 } 650 }
OLDNEW
« no previous file with comments | « samples/lineprocessor.cc ('k') | samples/shell.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698