Skip to content

Commit

Permalink
v8 7.4.98-p1
Browse files Browse the repository at this point in the history
  • Loading branch information
ruslo committed Feb 24, 2019
1 parent d9cc6ee commit 18c2650
Show file tree
Hide file tree
Showing 5 changed files with 167 additions and 0 deletions.
1 change: 1 addition & 0 deletions cmake/configs/default.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,7 @@ hunter_default_version(type_safe VERSION 0.2.1-p1)
hunter_default_version(units VERSION 3.0.0.alpha-2)
hunter_default_version(utf8 VERSION 2.3.4-p1)
hunter_default_version(util_linux VERSION 2.30.1)
hunter_default_version(v8 VERSION 7.4.98-p1)
hunter_default_version(vectorial VERSION 0.0.0-ae7dc88-p0)
hunter_default_version(vorbis VERSION 1.3.6-p1)
hunter_default_version(websocketpp VERSION 0.8.1-p0)
Expand Down
24 changes: 24 additions & 0 deletions cmake/projects/v8/hunter.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Copyright (c) 2016-2019, Ruslan Baratov
# All rights reserved.

# !!! DO NOT PLACE HEADER GUARDS HERE !!!

include(hunter_add_version)
include(hunter_cacheable)
include(hunter_download)
include(hunter_pick_scheme)

hunter_add_version(
PACKAGE_NAME
v8
VERSION
7.4.98-p1
URL
"https://github.com/hunter-packages/v8/archive/v7.4.98-p1.tar.gz"
SHA1
44805a2abbfc65fa73dbbfaa0383135dae9d05af
)

hunter_pick_scheme(DEFAULT url_sha1_cmake)
hunter_cacheable(v8)
hunter_download(PACKAGE_NAME v8)
21 changes: 21 additions & 0 deletions docs/packages/pkg/v8.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
.. spelling::

v8

.. index::
single: unsorted ; v8

.. _pkg.v8:

v8
==

- https://v8.dev/
- `Official GitHub <https://github.com/v8/v8>`__
- `Hunterized <https://github.com/hunter-packages/v8>`__
- `Example <https://github.com/ruslo/hunter/blob/master/examples/v8/CMakeLists.txt>`__

.. literalinclude:: /../examples/v8/CMakeLists.txt
:language: cmake
:start-after: # DOCUMENTATION_START {
:end-before: # DOCUMENTATION_END }
18 changes: 18 additions & 0 deletions examples/v8/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Copyright (c) 2016-2018, Ruslan Baratov
# All rights reserved.

cmake_minimum_required(VERSION 3.2)

# Emulate HunterGate:
# * https://github.com/hunter-packages/gate
include("../common.cmake")

project(download-v8)

# DOCUMENTATION_START {
hunter_add_package(v8)
find_package(v8 CONFIG REQUIRED)

add_executable(boo boo.cpp)
target_link_libraries(boo PUBLIC v8::v8_libplatform v8::v8_base)
# DOCUMENTATION_END }
103 changes: 103 additions & 0 deletions examples/v8/boo.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
// 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 <libplatform/libplatform.h>
#include <v8.h>

int main(int argc, char* argv[]) {
// Initialize V8.
v8::V8::InitializeICUDefaultLocation(argv[0]);
v8::V8::InitializeExternalStartupData(argv[0]);
std::unique_ptr<v8::Platform> platform = v8::platform::NewDefaultPlatform();
v8::V8::InitializePlatform(platform.get());
v8::V8::Initialize();

// Create a new Isolate and make it the current one.
v8::Isolate::CreateParams create_params;

create_params.array_buffer_allocator =
v8::ArrayBuffer::Allocator::NewDefaultAllocator();
v8::Isolate* isolate = v8::Isolate::New(create_params);
{
v8::Isolate::Scope isolate_scope(isolate);

// Create a stack-allocated handle scope.
v8::HandleScope handle_scope(isolate);

// Create a new context.
v8::Local<v8::Context> context = v8::Context::New(isolate);

// Enter the context for compiling and running the hello world script.
v8::Context::Scope context_scope(context);

{
// Create a string containing the JavaScript source code.
v8::Local<v8::String> source =
v8::String::NewFromUtf8(isolate, "'Hello' + ', World!'",
v8::NewStringType::kNormal)
.ToLocalChecked();

// 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)
//
const char* csource = R"(
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 =
v8::String::NewFromUtf8(isolate, csource, v8::NewStringType::kNormal)
.ToLocalChecked();

// 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);
}
}

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

0 comments on commit 18c2650

Please sign in to comment.