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 293 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
304 | 304 |
305 | 305 |
306 // ----------------------------------- | 306 // ----------------------------------- |
307 // --- 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 --- |
308 // ----------------------------------- | 308 // ----------------------------------- |
309 | 309 |
310 // Utility function that wraps a C++ http request object in a | 310 // Utility function that wraps a C++ http request object in a |
311 // JavaScript object. | 311 // JavaScript object. |
312 Handle<Object> JsHttpRequestProcessor::WrapMap(map<string, string>* obj) { | 312 Handle<Object> JsHttpRequestProcessor::WrapMap(map<string, string>* obj) { |
313 // Handle scope for temporary handles. | 313 // Handle scope for temporary handles. |
314 HandleScope handle_scope(GetIsolate()); | 314 EscapableHandleScope handle_scope(GetIsolate()); |
315 | 315 |
316 // Fetch the template for creating JavaScript map wrappers. | 316 // Fetch the template for creating JavaScript map wrappers. |
317 // It only has to be created once, which we do on demand. | 317 // It only has to be created once, which we do on demand. |
318 if (map_template_.IsEmpty()) { | 318 if (map_template_.IsEmpty()) { |
319 Handle<ObjectTemplate> raw_template = MakeMapTemplate(GetIsolate()); | 319 Handle<ObjectTemplate> raw_template = MakeMapTemplate(GetIsolate()); |
320 map_template_.Reset(GetIsolate(), raw_template); | 320 map_template_.Reset(GetIsolate(), raw_template); |
321 } | 321 } |
322 Handle<ObjectTemplate> templ = | 322 Handle<ObjectTemplate> templ = |
323 Local<ObjectTemplate>::New(GetIsolate(), map_template_); | 323 Local<ObjectTemplate>::New(GetIsolate(), map_template_); |
324 | 324 |
325 // Create an empty map wrapper. | 325 // Create an empty map wrapper. |
326 Handle<Object> result = templ->NewInstance(); | 326 Local<Object> result = templ->NewInstance(); |
327 | 327 |
328 // Wrap the raw C++ pointer in an External so it can be referenced | 328 // Wrap the raw C++ pointer in an External so it can be referenced |
329 // from within JavaScript. | 329 // from within JavaScript. |
330 Handle<External> map_ptr = External::New(GetIsolate(), obj); | 330 Handle<External> map_ptr = External::New(GetIsolate(), obj); |
331 | 331 |
332 // Store the map pointer in the JavaScript wrapper. | 332 // Store the map pointer in the JavaScript wrapper. |
333 result->SetInternalField(0, map_ptr); | 333 result->SetInternalField(0, map_ptr); |
334 | 334 |
335 // Return the result through the current handle scope. Since each | 335 // Return the result through the current handle scope. Since each |
336 // of these handles will go away when the handle scope is deleted | 336 // of these handles will go away when the handle scope is deleted |
337 // we need to call Close to let one, the result, escape into the | 337 // we need to call Close to let one, the result, escape into the |
338 // outer handle scope. | 338 // outer handle scope. |
339 return handle_scope.Close(result); | 339 return handle_scope.Escape(result); |
340 } | 340 } |
341 | 341 |
342 | 342 |
343 // Utility function that extracts the C++ map pointer from a wrapper | 343 // Utility function that extracts the C++ map pointer from a wrapper |
344 // object. | 344 // object. |
345 map<string, string>* JsHttpRequestProcessor::UnwrapMap(Handle<Object> obj) { | 345 map<string, string>* JsHttpRequestProcessor::UnwrapMap(Handle<Object> obj) { |
346 Handle<External> field = Handle<External>::Cast(obj->GetInternalField(0)); | 346 Handle<External> field = Handle<External>::Cast(obj->GetInternalField(0)); |
347 void* ptr = field->Value(); | 347 void* ptr = field->Value(); |
348 return static_cast<map<string, string>*>(ptr); | 348 return static_cast<map<string, string>*>(ptr); |
349 } | 349 } |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
392 // Update the map. | 392 // Update the map. |
393 (*obj)[key] = value; | 393 (*obj)[key] = value; |
394 | 394 |
395 // Return the value; any non-empty handle will work. | 395 // Return the value; any non-empty handle will work. |
396 info.GetReturnValue().Set(value_obj); | 396 info.GetReturnValue().Set(value_obj); |
397 } | 397 } |
398 | 398 |
399 | 399 |
400 Handle<ObjectTemplate> JsHttpRequestProcessor::MakeMapTemplate( | 400 Handle<ObjectTemplate> JsHttpRequestProcessor::MakeMapTemplate( |
401 Isolate* isolate) { | 401 Isolate* isolate) { |
402 HandleScope handle_scope(isolate); | 402 EscapableHandleScope handle_scope(isolate); |
403 | 403 |
404 Handle<ObjectTemplate> result = ObjectTemplate::New(); | 404 Local<ObjectTemplate> result = ObjectTemplate::New(); |
405 result->SetInternalFieldCount(1); | 405 result->SetInternalFieldCount(1); |
406 result->SetNamedPropertyHandler(MapGet, MapSet); | 406 result->SetNamedPropertyHandler(MapGet, MapSet); |
407 | 407 |
408 // Again, return the result through the current handle scope. | 408 // Again, return the result through the current handle scope. |
409 return handle_scope.Close(result); | 409 return handle_scope.Escape(result); |
410 } | 410 } |
411 | 411 |
412 | 412 |
413 // ------------------------------------------- | 413 // ------------------------------------------- |
414 // --- A c c e s s i n g R e q u e s t s --- | 414 // --- A c c e s s i n g R e q u e s t s --- |
415 // ------------------------------------------- | 415 // ------------------------------------------- |
416 | 416 |
417 /** | 417 /** |
418 * Utility function that wraps a C++ http request object in a | 418 * Utility function that wraps a C++ http request object in a |
419 * JavaScript object. | 419 * JavaScript object. |
420 */ | 420 */ |
421 Handle<Object> JsHttpRequestProcessor::WrapRequest(HttpRequest* request) { | 421 Handle<Object> JsHttpRequestProcessor::WrapRequest(HttpRequest* request) { |
422 // Handle scope for temporary handles. | 422 // Handle scope for temporary handles. |
423 HandleScope handle_scope(GetIsolate()); | 423 EscapableHandleScope handle_scope(GetIsolate()); |
424 | 424 |
425 // Fetch the template for creating JavaScript http request wrappers. | 425 // Fetch the template for creating JavaScript http request wrappers. |
426 // It only has to be created once, which we do on demand. | 426 // It only has to be created once, which we do on demand. |
427 if (request_template_.IsEmpty()) { | 427 if (request_template_.IsEmpty()) { |
428 Handle<ObjectTemplate> raw_template = MakeRequestTemplate(GetIsolate()); | 428 Handle<ObjectTemplate> raw_template = MakeRequestTemplate(GetIsolate()); |
429 request_template_.Reset(GetIsolate(), raw_template); | 429 request_template_.Reset(GetIsolate(), raw_template); |
430 } | 430 } |
431 Handle<ObjectTemplate> templ = | 431 Handle<ObjectTemplate> templ = |
432 Local<ObjectTemplate>::New(GetIsolate(), request_template_); | 432 Local<ObjectTemplate>::New(GetIsolate(), request_template_); |
433 | 433 |
434 // Create an empty http request wrapper. | 434 // Create an empty http request wrapper. |
435 Handle<Object> result = templ->NewInstance(); | 435 Local<Object> result = templ->NewInstance(); |
436 | 436 |
437 // Wrap the raw C++ pointer in an External so it can be referenced | 437 // Wrap the raw C++ pointer in an External so it can be referenced |
438 // from within JavaScript. | 438 // from within JavaScript. |
439 Handle<External> request_ptr = External::New(GetIsolate(), request); | 439 Handle<External> request_ptr = External::New(GetIsolate(), request); |
440 | 440 |
441 // Store the request pointer in the JavaScript wrapper. | 441 // Store the request pointer in the JavaScript wrapper. |
442 result->SetInternalField(0, request_ptr); | 442 result->SetInternalField(0, request_ptr); |
443 | 443 |
444 // Return the result through the current handle scope. Since each | 444 // Return the result through the current handle scope. Since each |
445 // of these handles will go away when the handle scope is deleted | 445 // of these handles will go away when the handle scope is deleted |
446 // we need to call Close to let one, the result, escape into the | 446 // we need to call Close to let one, the result, escape into the |
447 // outer handle scope. | 447 // outer handle scope. |
448 return handle_scope.Close(result); | 448 return handle_scope.Escape(result); |
449 } | 449 } |
450 | 450 |
451 | 451 |
452 /** | 452 /** |
453 * Utility function that extracts the C++ http request object from a | 453 * Utility function that extracts the C++ http request object from a |
454 * wrapper object. | 454 * wrapper object. |
455 */ | 455 */ |
456 HttpRequest* JsHttpRequestProcessor::UnwrapRequest(Handle<Object> obj) { | 456 HttpRequest* JsHttpRequestProcessor::UnwrapRequest(Handle<Object> obj) { |
457 Handle<External> field = Handle<External>::Cast(obj->GetInternalField(0)); | 457 Handle<External> field = Handle<External>::Cast(obj->GetInternalField(0)); |
458 void* ptr = field->Value(); | 458 void* ptr = field->Value(); |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
502 HttpRequest* request = UnwrapRequest(info.Holder()); | 502 HttpRequest* request = UnwrapRequest(info.Holder()); |
503 const string& path = request->UserAgent(); | 503 const string& path = request->UserAgent(); |
504 info.GetReturnValue().Set(String::NewFromUtf8( | 504 info.GetReturnValue().Set(String::NewFromUtf8( |
505 info.GetIsolate(), path.c_str(), String::kNormalString, | 505 info.GetIsolate(), path.c_str(), String::kNormalString, |
506 static_cast<int>(path.length()))); | 506 static_cast<int>(path.length()))); |
507 } | 507 } |
508 | 508 |
509 | 509 |
510 Handle<ObjectTemplate> JsHttpRequestProcessor::MakeRequestTemplate( | 510 Handle<ObjectTemplate> JsHttpRequestProcessor::MakeRequestTemplate( |
511 Isolate* isolate) { | 511 Isolate* isolate) { |
512 HandleScope handle_scope(isolate); | 512 EscapableHandleScope handle_scope(isolate); |
513 | 513 |
514 Handle<ObjectTemplate> result = ObjectTemplate::New(); | 514 Local<ObjectTemplate> result = ObjectTemplate::New(); |
515 result->SetInternalFieldCount(1); | 515 result->SetInternalFieldCount(1); |
516 | 516 |
517 // Add accessors for each of the fields of the request. | 517 // Add accessors for each of the fields of the request. |
518 result->SetAccessor( | 518 result->SetAccessor( |
519 String::NewFromUtf8(isolate, "path", String::kInternalizedString), | 519 String::NewFromUtf8(isolate, "path", String::kInternalizedString), |
520 GetPath); | 520 GetPath); |
521 result->SetAccessor( | 521 result->SetAccessor( |
522 String::NewFromUtf8(isolate, "referrer", String::kInternalizedString), | 522 String::NewFromUtf8(isolate, "referrer", String::kInternalizedString), |
523 GetReferrer); | 523 GetReferrer); |
524 result->SetAccessor( | 524 result->SetAccessor( |
525 String::NewFromUtf8(isolate, "host", String::kInternalizedString), | 525 String::NewFromUtf8(isolate, "host", String::kInternalizedString), |
526 GetHost); | 526 GetHost); |
527 result->SetAccessor( | 527 result->SetAccessor( |
528 String::NewFromUtf8(isolate, "userAgent", String::kInternalizedString), | 528 String::NewFromUtf8(isolate, "userAgent", String::kInternalizedString), |
529 GetUserAgent); | 529 GetUserAgent); |
530 | 530 |
531 // Again, return the result through the current handle scope. | 531 // Again, return the result through the current handle scope. |
532 return handle_scope.Close(result); | 532 return handle_scope.Escape(result); |
533 } | 533 } |
534 | 534 |
535 | 535 |
536 // --- Test --- | 536 // --- Test --- |
537 | 537 |
538 | 538 |
539 void HttpRequestProcessor::Log(const char* event) { | 539 void HttpRequestProcessor::Log(const char* event) { |
540 printf("Logged: %s\n", event); | 540 printf("Logged: %s\n", event); |
541 } | 541 } |
542 | 542 |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
661 JsHttpRequestProcessor processor(isolate, source); | 661 JsHttpRequestProcessor processor(isolate, source); |
662 map<string, string> output; | 662 map<string, string> output; |
663 if (!processor.Initialize(&options, &output)) { | 663 if (!processor.Initialize(&options, &output)) { |
664 fprintf(stderr, "Error initializing processor.\n"); | 664 fprintf(stderr, "Error initializing processor.\n"); |
665 return 1; | 665 return 1; |
666 } | 666 } |
667 if (!ProcessEntries(&processor, kSampleSize, kSampleRequests)) | 667 if (!ProcessEntries(&processor, kSampleSize, kSampleRequests)) |
668 return 1; | 668 return 1; |
669 PrintMap(&output); | 669 PrintMap(&output); |
670 } | 670 } |
OLD | NEW |