| 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 11 matching lines...) Expand all Loading... |
| 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | 27 |
| 28 #include <include/v8.h> | 28 #include <include/v8.h> |
| 29 | 29 |
| 30 #include <include/libplatform/libplatform.h> | 30 #include <include/libplatform/libplatform.h> |
| 31 | 31 |
| 32 #include <string.h> |
| 33 |
| 32 #include <map> | 34 #include <map> |
| 33 #include <string> | 35 #include <string> |
| 34 | 36 |
| 35 using namespace std; | 37 using namespace std; |
| 36 using namespace v8; | 38 using namespace v8; |
| 37 | 39 |
| 40 class ArrayBufferAllocator : public v8::ArrayBuffer::Allocator { |
| 41 public: |
| 42 virtual void* Allocate(size_t length) { |
| 43 void* data = AllocateUninitialized(length); |
| 44 return data == NULL ? data : memset(data, 0, length); |
| 45 } |
| 46 virtual void* AllocateUninitialized(size_t length) { return malloc(length); } |
| 47 virtual void Free(void* data, size_t) { free(data); } |
| 48 }; |
| 49 |
| 50 |
| 38 // These interfaces represent an existing request processing interface. | 51 // These interfaces represent an existing request processing interface. |
| 39 // The idea is to imagine a real application that uses these interfaces | 52 // The idea is to imagine a real application that uses these interfaces |
| 40 // and then add scripting capabilities that allow you to interact with | 53 // and then add scripting capabilities that allow you to interact with |
| 41 // the objects through JavaScript. | 54 // the objects through JavaScript. |
| 42 | 55 |
| 43 /** | 56 /** |
| 44 * A simplified http request. | 57 * A simplified http request. |
| 45 */ | 58 */ |
| 46 class HttpRequest { | 59 class HttpRequest { |
| 47 public: | 60 public: |
| (...skipping 593 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 641 pair<string, string> entry = *i; | 654 pair<string, string> entry = *i; |
| 642 printf("%s: %s\n", entry.first.c_str(), entry.second.c_str()); | 655 printf("%s: %s\n", entry.first.c_str(), entry.second.c_str()); |
| 643 } | 656 } |
| 644 } | 657 } |
| 645 | 658 |
| 646 | 659 |
| 647 int main(int argc, char* argv[]) { | 660 int main(int argc, char* argv[]) { |
| 648 v8::V8::InitializeICU(); | 661 v8::V8::InitializeICU(); |
| 649 v8::Platform* platform = v8::platform::CreateDefaultPlatform(); | 662 v8::Platform* platform = v8::platform::CreateDefaultPlatform(); |
| 650 v8::V8::InitializePlatform(platform); | 663 v8::V8::InitializePlatform(platform); |
| 664 ArrayBufferAllocator array_buffer_allocator; |
| 665 v8::V8::SetArrayBufferAllocator(&array_buffer_allocator); |
| 651 v8::V8::Initialize(); | 666 v8::V8::Initialize(); |
| 652 map<string, string> options; | 667 map<string, string> options; |
| 653 string file; | 668 string file; |
| 654 ParseOptions(argc, argv, &options, &file); | 669 ParseOptions(argc, argv, &options, &file); |
| 655 if (file.empty()) { | 670 if (file.empty()) { |
| 656 fprintf(stderr, "No script was specified.\n"); | 671 fprintf(stderr, "No script was specified.\n"); |
| 657 return 1; | 672 return 1; |
| 658 } | 673 } |
| 659 Isolate* isolate = Isolate::New(); | 674 Isolate* isolate = Isolate::New(); |
| 660 Isolate::Scope isolate_scope(isolate); | 675 Isolate::Scope isolate_scope(isolate); |
| 661 HandleScope scope(isolate); | 676 HandleScope scope(isolate); |
| 662 Handle<String> source = ReadFile(isolate, file); | 677 Handle<String> source = ReadFile(isolate, file); |
| 663 if (source.IsEmpty()) { | 678 if (source.IsEmpty()) { |
| 664 fprintf(stderr, "Error reading '%s'.\n", file.c_str()); | 679 fprintf(stderr, "Error reading '%s'.\n", file.c_str()); |
| 665 return 1; | 680 return 1; |
| 666 } | 681 } |
| 667 JsHttpRequestProcessor processor(isolate, source); | 682 JsHttpRequestProcessor processor(isolate, source); |
| 668 map<string, string> output; | 683 map<string, string> output; |
| 669 if (!processor.Initialize(&options, &output)) { | 684 if (!processor.Initialize(&options, &output)) { |
| 670 fprintf(stderr, "Error initializing processor.\n"); | 685 fprintf(stderr, "Error initializing processor.\n"); |
| 671 return 1; | 686 return 1; |
| 672 } | 687 } |
| 673 if (!ProcessEntries(&processor, kSampleSize, kSampleRequests)) | 688 if (!ProcessEntries(&processor, kSampleSize, kSampleRequests)) |
| 674 return 1; | 689 return 1; |
| 675 PrintMap(&output); | 690 PrintMap(&output); |
| 676 } | 691 } |
| OLD | NEW |