hello-world.cc 3.32 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
// 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.
14 15
  v8::V8::InitializeICUDefaultLocation(argv[0]);
  v8::V8::InitializeExternalStartupData(argv[0]);
16 17
  std::unique_ptr<v8::Platform> platform = v8::platform::NewDefaultPlatform();
  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 23
  create_params.array_buffer_allocator =
      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 39
    {
      // Create a string containing the JavaScript source code.
      v8::Local<v8::String> source =
40
          v8::String::NewFromUtf8Literal(isolate, "'Hello' + ', World!'");
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63

      // Compile the source code.
      v8::Local<v8::Script> script =
          v8::Script::Compile(context, source).ToLocalChecked();

      // Run the script to get the result.
      v8::Local<v8::Value> result = script->Run(context).ToLocalChecked();

      // Convert the result to an UTF8 string and print it.
      v8::String::Utf8Value utf8(isolate, result);
      printf("%s\n", *utf8);
    }

    {
      // Use the JavaScript API to generate a WebAssembly module.
      //
      // |bytes| contains the binary format for the following module:
      //
      //     (func (export "add") (param i32 i32) (result i32)
      //       get_local 0
      //       get_local 1
      //       i32.add)
      //
64
      const char csource[] = R"(
65 66 67 68 69 70 71 72 73 74 75 76 77
        let bytes = new Uint8Array([
          0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x07, 0x01,
          0x60, 0x02, 0x7f, 0x7f, 0x01, 0x7f, 0x03, 0x02, 0x01, 0x00, 0x07,
          0x07, 0x01, 0x03, 0x61, 0x64, 0x64, 0x00, 0x00, 0x0a, 0x09, 0x01,
          0x07, 0x00, 0x20, 0x00, 0x20, 0x01, 0x6a, 0x0b
        ]);
        let module = new WebAssembly.Module(bytes);
        let instance = new WebAssembly.Instance(module);
        instance.exports.add(3, 4);
      )";

      // Create a string containing the JavaScript source code.
      v8::Local<v8::String> source =
78
          v8::String::NewFromUtf8Literal(isolate, csource);
79 80 81 82 83 84 85 86 87 88 89 90

      // Compile the source code.
      v8::Local<v8::Script> script =
          v8::Script::Compile(context, source).ToLocalChecked();

      // Run the script to get the result.
      v8::Local<v8::Value> result = script->Run(context).ToLocalChecked();

      // Convert the result to a uint32 and print it.
      uint32_t number = result->Uint32Value(context).ToChecked();
      printf("3 + 4 = %u\n", number);
    }
91 92 93 94
  }

  // Dispose the isolate and tear down V8.
  isolate->Dispose();
95 96
  v8::V8::Dispose();
  v8::V8::ShutdownPlatform();
97
  delete create_params.array_buffer_allocator;
98 99
  return 0;
}