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 
main(int argc,char * argv[])12 int main(int argc, char* argv[]) {
13   // Initialize V8.
14   v8::V8::InitializeICUDefaultLocation(argv[0]);
15   v8::V8::InitializeExternalStartupData(argv[0]);
16   std::unique_ptr<v8::Platform> platform = v8::platform::NewDefaultPlatform();
17   v8::V8::InitializePlatform(platform.get());
18   v8::V8::Initialize();
19 
20   // Create a new Isolate and make it the current one.
21   v8::Isolate::CreateParams create_params;
22   create_params.array_buffer_allocator =
23       v8::ArrayBuffer::Allocator::NewDefaultAllocator();
24   v8::Isolate* isolate = v8::Isolate::New(create_params);
25   {
26     v8::Isolate::Scope isolate_scope(isolate);
27 
28     // Create a stack-allocated handle scope.
29     v8::HandleScope handle_scope(isolate);
30 
31     // Create a new context.
32     v8::Local<v8::Context> context = v8::Context::New(isolate);
33 
34     // Enter the context for compiling and running the hello world script.
35     v8::Context::Scope context_scope(context);
36 
37     {
38       // Create a string containing the JavaScript source code.
39       v8::Local<v8::String> source =
40           v8::String::NewFromUtf8(isolate, "'Hello' + ', World!'",
41                                   v8::NewStringType::kNormal)
42               .ToLocalChecked();
43 
44       // Compile the source code.
45       v8::Local<v8::Script> script =
46           v8::Script::Compile(context, source).ToLocalChecked();
47 
48       // Run the script to get the result.
49       v8::Local<v8::Value> result = script->Run(context).ToLocalChecked();
50 
51       // Convert the result to an UTF8 string and print it.
52       v8::String::Utf8Value utf8(isolate, result);
53       printf("%s\n", *utf8);
54     }
55 
56     {
57       // Use the JavaScript API to generate a WebAssembly module.
58       //
59       // |bytes| contains the binary format for the following module:
60       //
61       //     (func (export "add") (param i32 i32) (result i32)
62       //       get_local 0
63       //       get_local 1
64       //       i32.add)
65       //
66       const char* csource = R"(
67         let bytes = new Uint8Array([
68           0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x07, 0x01,
69           0x60, 0x02, 0x7f, 0x7f, 0x01, 0x7f, 0x03, 0x02, 0x01, 0x00, 0x07,
70           0x07, 0x01, 0x03, 0x61, 0x64, 0x64, 0x00, 0x00, 0x0a, 0x09, 0x01,
71           0x07, 0x00, 0x20, 0x00, 0x20, 0x01, 0x6a, 0x0b
72         ]);
73         let module = new WebAssembly.Module(bytes);
74         let instance = new WebAssembly.Instance(module);
75         instance.exports.add(3, 4);
76       )";
77 
78       // Create a string containing the JavaScript source code.
79       v8::Local<v8::String> source =
80           v8::String::NewFromUtf8(isolate, csource, v8::NewStringType::kNormal)
81               .ToLocalChecked();
82 
83       // Compile the source code.
84       v8::Local<v8::Script> script =
85           v8::Script::Compile(context, source).ToLocalChecked();
86 
87       // Run the script to get the result.
88       v8::Local<v8::Value> result = script->Run(context).ToLocalChecked();
89 
90       // Convert the result to a uint32 and print it.
91       uint32_t number = result->Uint32Value(context).ToChecked();
92       printf("3 + 4 = %u\n", number);
93     }
94   }
95 
96   // Dispose the isolate and tear down V8.
97   isolate->Dispose();
98   v8::V8::Dispose();
99   v8::V8::ShutdownPlatform();
100   delete create_params.array_buffer_allocator;
101   return 0;
102 }
103