2015-05-21 11:19:32 +00:00
|
|
|
// Copyright 2015 the V8 project authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "include/libplatform/libplatform.h"
|
|
|
|
#include "include/v8.h"
|
|
|
|
|
|
|
|
int main(int argc, char* argv[]) {
|
|
|
|
// Initialize V8.
|
2017-09-04 10:47:27 +00:00
|
|
|
v8::V8::InitializeICUDefaultLocation(argv[0]);
|
|
|
|
v8::V8::InitializeExternalStartupData(argv[0]);
|
|
|
|
v8::Platform* platform = v8::platform::CreateDefaultPlatform();
|
|
|
|
v8::V8::InitializePlatform(platform);
|
|
|
|
v8::V8::Initialize();
|
2015-05-21 11:19:32 +00:00
|
|
|
|
|
|
|
// Create a new Isolate and make it the current one.
|
2017-09-04 10:47:27 +00:00
|
|
|
v8::Isolate::CreateParams create_params;
|
2016-06-29 07:39:45 +00:00
|
|
|
create_params.array_buffer_allocator =
|
|
|
|
v8::ArrayBuffer::Allocator::NewDefaultAllocator();
|
2017-09-04 10:47:27 +00:00
|
|
|
v8::Isolate* isolate = v8::Isolate::New(create_params);
|
2015-05-21 11:19:32 +00:00
|
|
|
{
|
2017-09-04 10:47:27 +00:00
|
|
|
v8::Isolate::Scope isolate_scope(isolate);
|
2015-05-21 11:19:32 +00:00
|
|
|
|
|
|
|
// Create a stack-allocated handle scope.
|
2017-09-04 10:47:27 +00:00
|
|
|
v8::HandleScope handle_scope(isolate);
|
2015-05-21 11:19:32 +00:00
|
|
|
|
|
|
|
// Create a new context.
|
2017-09-04 10:47:27 +00:00
|
|
|
v8::Local<v8::Context> context = v8::Context::New(isolate);
|
2015-05-21 11:19:32 +00:00
|
|
|
|
|
|
|
// Enter the context for compiling and running the hello world script.
|
2017-09-04 10:47:27 +00:00
|
|
|
v8::Context::Scope context_scope(context);
|
2015-05-21 11:19:32 +00:00
|
|
|
|
|
|
|
// Create a string containing the JavaScript source code.
|
2017-09-04 10:47:27 +00:00
|
|
|
v8::Local<v8::String> source =
|
|
|
|
v8::String::NewFromUtf8(isolate, "'Hello' + ', World!'",
|
|
|
|
v8::NewStringType::kNormal)
|
|
|
|
.ToLocalChecked();
|
2015-05-21 11:19:32 +00:00
|
|
|
|
|
|
|
// Compile the source code.
|
2017-09-04 10:47:27 +00:00
|
|
|
v8::Local<v8::Script> script =
|
|
|
|
v8::Script::Compile(context, source).ToLocalChecked();
|
2015-05-21 11:19:32 +00:00
|
|
|
|
|
|
|
// Run the script to get the result.
|
2017-09-04 10:47:27 +00:00
|
|
|
v8::Local<v8::Value> result = script->Run(context).ToLocalChecked();
|
2015-05-21 11:19:32 +00:00
|
|
|
|
|
|
|
// Convert the result to an UTF8 string and print it.
|
2017-09-04 10:47:27 +00:00
|
|
|
v8::String::Utf8Value utf8(isolate, result);
|
2015-05-21 11:19:32 +00:00
|
|
|
printf("%s\n", *utf8);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Dispose the isolate and tear down V8.
|
|
|
|
isolate->Dispose();
|
2017-09-04 10:47:27 +00:00
|
|
|
v8::V8::Dispose();
|
|
|
|
v8::V8::ShutdownPlatform();
|
2015-05-21 11:19:32 +00:00
|
|
|
delete platform;
|
2016-06-29 07:39:45 +00:00
|
|
|
delete create_params.array_buffer_allocator;
|
2015-05-21 11:19:32 +00:00
|
|
|
return 0;
|
|
|
|
}
|