OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include <stdio.h> |
| 6 #include <stdlib.h> |
| 7 #include <string.h> |
| 8 |
| 9 #include "include/libplatform/libplatform.h" |
| 10 #include "include/v8.h" |
| 11 |
| 12 using namespace v8; |
| 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[]) { |
| 26 // Initialize V8. |
| 27 V8::InitializeICU(); |
| 28 Platform* platform = platform::CreateDefaultPlatform(); |
| 29 V8::InitializePlatform(platform); |
| 30 V8::Initialize(); |
| 31 |
| 32 // Create a new Isolate and make it the current one. |
| 33 ArrayBufferAllocator allocator; |
| 34 Isolate::CreateParams create_params; |
| 35 create_params.array_buffer_allocator = &allocator; |
| 36 Isolate* isolate = Isolate::New(create_params); |
| 37 { |
| 38 Isolate::Scope isolate_scope(isolate); |
| 39 |
| 40 // Create a stack-allocated handle scope. |
| 41 HandleScope handle_scope(isolate); |
| 42 |
| 43 // Create a new context. |
| 44 Local<Context> context = Context::New(isolate); |
| 45 |
| 46 // Enter the context for compiling and running the hello world script. |
| 47 Context::Scope context_scope(context); |
| 48 |
| 49 // Create a string containing the JavaScript source code. |
| 50 Local<String> source = String::NewFromUtf8(isolate, "'Hello' + ', World!'"); |
| 51 |
| 52 // Compile the source code. |
| 53 Local<Script> script = Script::Compile(source); |
| 54 |
| 55 // Run the script to get the result. |
| 56 Local<Value> result = script->Run(); |
| 57 |
| 58 // Convert the result to an UTF8 string and print it. |
| 59 String::Utf8Value utf8(result); |
| 60 printf("%s\n", *utf8); |
| 61 } |
| 62 |
| 63 // Dispose the isolate and tear down V8. |
| 64 isolate->Dispose(); |
| 65 V8::Dispose(); |
| 66 V8::ShutdownPlatform(); |
| 67 delete platform; |
| 68 return 0; |
| 69 } |
OLD | NEW |