| OLD | NEW |
| 1 // Copyright 2015 the V8 project authors. All rights reserved. | 1 // Copyright 2015 the V8 project authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <stdio.h> | 5 #include <stdio.h> |
| 6 #include <stdlib.h> | 6 #include <stdlib.h> |
| 7 #include <string.h> | 7 #include <string.h> |
| 8 | 8 |
| 9 #include "include/libplatform/libplatform.h" | 9 #include "include/libplatform/libplatform.h" |
| 10 #include "include/v8.h" | 10 #include "include/v8.h" |
| 11 | 11 |
| 12 using namespace v8; | 12 using namespace v8; |
| 13 | 13 |
| 14 class ArrayBufferAllocator : public v8::ArrayBuffer::Allocator { | |
| 15 public: | |
| 16 virtual void* Allocate(size_t length) { | |
| 17 void* data = AllocateUninitialized(length); | |
| 18 return data == NULL ? data : memset(data, 0, length); | |
| 19 } | |
| 20 virtual void* AllocateUninitialized(size_t length) { return malloc(length); } | |
| 21 virtual void Free(void* data, size_t) { free(data); } | |
| 22 }; | |
| 23 | |
| 24 | |
| 25 int main(int argc, char* argv[]) { | 14 int main(int argc, char* argv[]) { |
| 26 // Initialize V8. | 15 // Initialize V8. |
| 27 V8::InitializeICUDefaultLocation(argv[0]); | 16 V8::InitializeICUDefaultLocation(argv[0]); |
| 28 V8::InitializeExternalStartupData(argv[0]); | 17 V8::InitializeExternalStartupData(argv[0]); |
| 29 Platform* platform = platform::CreateDefaultPlatform(); | 18 Platform* platform = platform::CreateDefaultPlatform(); |
| 30 V8::InitializePlatform(platform); | 19 V8::InitializePlatform(platform); |
| 31 V8::Initialize(); | 20 V8::Initialize(); |
| 32 | 21 |
| 33 // Create a new Isolate and make it the current one. | 22 // Create a new Isolate and make it the current one. |
| 34 ArrayBufferAllocator allocator; | |
| 35 Isolate::CreateParams create_params; | 23 Isolate::CreateParams create_params; |
| 36 create_params.array_buffer_allocator = &allocator; | 24 create_params.array_buffer_allocator = |
| 25 v8::ArrayBuffer::Allocator::NewDefaultAllocator(); |
| 37 Isolate* isolate = Isolate::New(create_params); | 26 Isolate* isolate = Isolate::New(create_params); |
| 38 { | 27 { |
| 39 Isolate::Scope isolate_scope(isolate); | 28 Isolate::Scope isolate_scope(isolate); |
| 40 | 29 |
| 41 // Create a stack-allocated handle scope. | 30 // Create a stack-allocated handle scope. |
| 42 HandleScope handle_scope(isolate); | 31 HandleScope handle_scope(isolate); |
| 43 | 32 |
| 44 // Create a new context. | 33 // Create a new context. |
| 45 Local<Context> context = Context::New(isolate); | 34 Local<Context> context = Context::New(isolate); |
| 46 | 35 |
| (...skipping 14 matching lines...) Expand all Loading... |
| 61 // Convert the result to an UTF8 string and print it. | 50 // Convert the result to an UTF8 string and print it. |
| 62 String::Utf8Value utf8(result); | 51 String::Utf8Value utf8(result); |
| 63 printf("%s\n", *utf8); | 52 printf("%s\n", *utf8); |
| 64 } | 53 } |
| 65 | 54 |
| 66 // Dispose the isolate and tear down V8. | 55 // Dispose the isolate and tear down V8. |
| 67 isolate->Dispose(); | 56 isolate->Dispose(); |
| 68 V8::Dispose(); | 57 V8::Dispose(); |
| 69 V8::ShutdownPlatform(); | 58 V8::ShutdownPlatform(); |
| 70 delete platform; | 59 delete platform; |
| 60 delete create_params.array_buffer_allocator; |
| 71 return 0; | 61 return 0; |
| 72 } | 62 } |
| OLD | NEW |