| OLD | NEW |
| 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 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 105 static void GetPath(Local<String> name, | 105 static void GetPath(Local<String> name, |
| 106 const PropertyCallbackInfo<Value>& info); | 106 const PropertyCallbackInfo<Value>& info); |
| 107 static void GetReferrer(Local<String> name, | 107 static void GetReferrer(Local<String> name, |
| 108 const PropertyCallbackInfo<Value>& info); | 108 const PropertyCallbackInfo<Value>& info); |
| 109 static void GetHost(Local<String> name, | 109 static void GetHost(Local<String> name, |
| 110 const PropertyCallbackInfo<Value>& info); | 110 const PropertyCallbackInfo<Value>& info); |
| 111 static void GetUserAgent(Local<String> name, | 111 static void GetUserAgent(Local<String> name, |
| 112 const PropertyCallbackInfo<Value>& info); | 112 const PropertyCallbackInfo<Value>& info); |
| 113 | 113 |
| 114 // Callbacks that access maps | 114 // Callbacks that access maps |
| 115 static void MapGet(Local<String> name, | 115 static void MapGet(Local<Name> name, const PropertyCallbackInfo<Value>& info); |
| 116 const PropertyCallbackInfo<Value>& info); | 116 static void MapSet(Local<Name> name, Local<Value> value, |
| 117 static void MapSet(Local<String> name, | |
| 118 Local<Value> value, | |
| 119 const PropertyCallbackInfo<Value>& info); | 117 const PropertyCallbackInfo<Value>& info); |
| 120 | 118 |
| 121 // Utility methods for wrapping C++ objects as JavaScript objects, | 119 // Utility methods for wrapping C++ objects as JavaScript objects, |
| 122 // and going back again. | 120 // and going back again. |
| 123 Handle<Object> WrapMap(map<string, string>* obj); | 121 Handle<Object> WrapMap(map<string, string>* obj); |
| 124 static map<string, string>* UnwrapMap(Handle<Object> obj); | 122 static map<string, string>* UnwrapMap(Handle<Object> obj); |
| 125 Handle<Object> WrapRequest(HttpRequest* obj); | 123 Handle<Object> WrapRequest(HttpRequest* obj); |
| 126 static HttpRequest* UnwrapRequest(Handle<Object> obj); | 124 static HttpRequest* UnwrapRequest(Handle<Object> obj); |
| 127 | 125 |
| 128 Isolate* GetIsolate() { return isolate_; } | 126 Isolate* GetIsolate() { return isolate_; } |
| (...skipping 219 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 348 | 346 |
| 349 | 347 |
| 350 // Convert a JavaScript string to a std::string. To not bother too | 348 // Convert a JavaScript string to a std::string. To not bother too |
| 351 // much with string encodings we just use ascii. | 349 // much with string encodings we just use ascii. |
| 352 string ObjectToString(Local<Value> value) { | 350 string ObjectToString(Local<Value> value) { |
| 353 String::Utf8Value utf8_value(value); | 351 String::Utf8Value utf8_value(value); |
| 354 return string(*utf8_value); | 352 return string(*utf8_value); |
| 355 } | 353 } |
| 356 | 354 |
| 357 | 355 |
| 358 void JsHttpRequestProcessor::MapGet(Local<String> name, | 356 void JsHttpRequestProcessor::MapGet(Local<Name> name, |
| 359 const PropertyCallbackInfo<Value>& info) { | 357 const PropertyCallbackInfo<Value>& info) { |
| 358 if (name->IsSymbol()) return; |
| 359 |
| 360 // Fetch the map wrapped by this object. | 360 // Fetch the map wrapped by this object. |
| 361 map<string, string>* obj = UnwrapMap(info.Holder()); | 361 map<string, string>* obj = UnwrapMap(info.Holder()); |
| 362 | 362 |
| 363 // Convert the JavaScript string to a std::string. | 363 // Convert the JavaScript string to a std::string. |
| 364 string key = ObjectToString(name); | 364 string key = ObjectToString(Local<String>::Cast(name)); |
| 365 | 365 |
| 366 // Look up the value if it exists using the standard STL ideom. | 366 // Look up the value if it exists using the standard STL ideom. |
| 367 map<string, string>::iterator iter = obj->find(key); | 367 map<string, string>::iterator iter = obj->find(key); |
| 368 | 368 |
| 369 // If the key is not present return an empty handle as signal | 369 // If the key is not present return an empty handle as signal |
| 370 if (iter == obj->end()) return; | 370 if (iter == obj->end()) return; |
| 371 | 371 |
| 372 // Otherwise fetch the value and wrap it in a JavaScript string | 372 // Otherwise fetch the value and wrap it in a JavaScript string |
| 373 const string& value = (*iter).second; | 373 const string& value = (*iter).second; |
| 374 info.GetReturnValue().Set(String::NewFromUtf8( | 374 info.GetReturnValue().Set(String::NewFromUtf8( |
| 375 info.GetIsolate(), value.c_str(), String::kNormalString, | 375 info.GetIsolate(), value.c_str(), String::kNormalString, |
| 376 static_cast<int>(value.length()))); | 376 static_cast<int>(value.length()))); |
| 377 } | 377 } |
| 378 | 378 |
| 379 | 379 |
| 380 void JsHttpRequestProcessor::MapSet(Local<String> name, | 380 void JsHttpRequestProcessor::MapSet(Local<Name> name, Local<Value> value_obj, |
| 381 Local<Value> value_obj, | |
| 382 const PropertyCallbackInfo<Value>& info) { | 381 const PropertyCallbackInfo<Value>& info) { |
| 382 if (name->IsSymbol()) return; |
| 383 |
| 383 // Fetch the map wrapped by this object. | 384 // Fetch the map wrapped by this object. |
| 384 map<string, string>* obj = UnwrapMap(info.Holder()); | 385 map<string, string>* obj = UnwrapMap(info.Holder()); |
| 385 | 386 |
| 386 // Convert the key and value to std::strings. | 387 // Convert the key and value to std::strings. |
| 387 string key = ObjectToString(name); | 388 string key = ObjectToString(Local<String>::Cast(name)); |
| 388 string value = ObjectToString(value_obj); | 389 string value = ObjectToString(value_obj); |
| 389 | 390 |
| 390 // Update the map. | 391 // Update the map. |
| 391 (*obj)[key] = value; | 392 (*obj)[key] = value; |
| 392 | 393 |
| 393 // Return the value; any non-empty handle will work. | 394 // Return the value; any non-empty handle will work. |
| 394 info.GetReturnValue().Set(value_obj); | 395 info.GetReturnValue().Set(value_obj); |
| 395 } | 396 } |
| 396 | 397 |
| 397 | 398 |
| 398 Handle<ObjectTemplate> JsHttpRequestProcessor::MakeMapTemplate( | 399 Handle<ObjectTemplate> JsHttpRequestProcessor::MakeMapTemplate( |
| 399 Isolate* isolate) { | 400 Isolate* isolate) { |
| 400 EscapableHandleScope handle_scope(isolate); | 401 EscapableHandleScope handle_scope(isolate); |
| 401 | 402 |
| 402 Local<ObjectTemplate> result = ObjectTemplate::New(isolate); | 403 Local<ObjectTemplate> result = ObjectTemplate::New(isolate); |
| 403 result->SetInternalFieldCount(1); | 404 result->SetInternalFieldCount(1); |
| 404 result->SetNamedPropertyHandler(MapGet, MapSet); | 405 result->SetHandler(NamedPropertyHandlerConfiguration(MapGet, MapSet)); |
| 405 | 406 |
| 406 // Again, return the result through the current handle scope. | 407 // Again, return the result through the current handle scope. |
| 407 return handle_scope.Escape(result); | 408 return handle_scope.Escape(result); |
| 408 } | 409 } |
| 409 | 410 |
| 410 | 411 |
| 411 // ------------------------------------------- | 412 // ------------------------------------------- |
| 412 // --- A c c e s s i n g R e q u e s t s --- | 413 // --- A c c e s s i n g R e q u e s t s --- |
| 413 // ------------------------------------------- | 414 // ------------------------------------------- |
| 414 | 415 |
| (...skipping 248 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 663 JsHttpRequestProcessor processor(isolate, source); | 664 JsHttpRequestProcessor processor(isolate, source); |
| 664 map<string, string> output; | 665 map<string, string> output; |
| 665 if (!processor.Initialize(&options, &output)) { | 666 if (!processor.Initialize(&options, &output)) { |
| 666 fprintf(stderr, "Error initializing processor.\n"); | 667 fprintf(stderr, "Error initializing processor.\n"); |
| 667 return 1; | 668 return 1; |
| 668 } | 669 } |
| 669 if (!ProcessEntries(&processor, kSampleSize, kSampleRequests)) | 670 if (!ProcessEntries(&processor, kSampleSize, kSampleRequests)) |
| 670 return 1; | 671 return 1; |
| 671 PrintMap(&output); | 672 PrintMap(&output); |
| 672 } | 673 } |
| OLD | NEW |