Home | History | Annotate | Download | only in samples
      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 int main(int argc, char* argv[]) {
     15   // Initialize V8.
     16   V8::InitializeICUDefaultLocation(argv[0]);
     17   V8::InitializeExternalStartupData(argv[0]);
     18   Platform* platform = platform::CreateDefaultPlatform();
     19   V8::InitializePlatform(platform);
     20   V8::Initialize();
     21 
     22   // Create a new Isolate and make it the current one.
     23   Isolate::CreateParams create_params;
     24   create_params.array_buffer_allocator =
     25       v8::ArrayBuffer::Allocator::NewDefaultAllocator();
     26   Isolate* isolate = Isolate::New(create_params);
     27   {
     28     Isolate::Scope isolate_scope(isolate);
     29 
     30     // Create a stack-allocated handle scope.
     31     HandleScope handle_scope(isolate);
     32 
     33     // Create a new context.
     34     Local<Context> context = Context::New(isolate);
     35 
     36     // Enter the context for compiling and running the hello world script.
     37     Context::Scope context_scope(context);
     38 
     39     // Create a string containing the JavaScript source code.
     40     Local<String> source =
     41         String::NewFromUtf8(isolate, "'Hello' + ', World!'",
     42                             NewStringType::kNormal).ToLocalChecked();
     43 
     44     // Compile the source code.
     45     Local<Script> script = Script::Compile(context, source).ToLocalChecked();
     46 
     47     // Run the script to get the result.
     48     Local<Value> result = script->Run(context).ToLocalChecked();
     49 
     50     // Convert the result to an UTF8 string and print it.
     51     String::Utf8Value utf8(result);
     52     printf("%s\n", *utf8);
     53   }
     54 
     55   // Dispose the isolate and tear down V8.
     56   isolate->Dispose();
     57   V8::Dispose();
     58   V8::ShutdownPlatform();
     59   delete platform;
     60   delete create_params.array_buffer_allocator;
     61   return 0;
     62 }
     63