聊一聊编译和使用V8

开发 前端
V8编译是个比较麻烦的事情,不仅是下载、编译的过程,不同系统、不同编译器、不同C++版本都可能会出现不同的问题。之前编译的时候没有记录步骤,这次简单记录一下编译V8的过程,我的工作目录是/code/v8_code/。

[[420164]]

V8编译是个比较麻烦的事情,不仅是下载、编译的过程,不同系统、不同编译器、不同C++版本都可能会出现不同的问题。之前编译的时候没有记录步骤,这次简单记录一下编译V8的过程,我的工作目录是/code/v8_code/。

  • 1 编译V8
  • 2 编译V8为静态库
  • 3 使用V8

一. 编译V8

1 下载工具:

  1. git clone https://chromium.googlesource.com/chromium/tools/depot_tools.git 

2 执行

  1. export PATH=$PATH:/code/v8_code/depot_tools 

后续执行命令用到。

3 执行

  1. gclient config https://chromium.googlesource.com/v8/v8.git 

4 执行gclient sync (成功后当前目录下多一个v8目录,这时候有depot_tools和v8两个目录)

5 执行命令:alias gm=/code/v8_code/v8/tools/dev/gm.py,然后cd v8进入v8源码目录。

6 执行gm x64.release,编译成功,新建一个hello.js,执行out/x64.release/d8 hello.js就看到相应输出,或者执行out/x64.release/d8进入互动模式。

二. 编译V8为静态库

执行

  1. alias v8gen=/code/v8_code/v8/tools/dev/v8gen.py 

v8源码目录执行v8gen x64.release.sample生成配置文件,执行

  1. ninja -C out.gn/x64.release.sample v8_monolith 

编译静态库。

三.使用V8

我们可以在自己的项目里使用V8,这个已经有不少的例子,Node.js就是典型的例子,不过Node.js比较复杂,不利于快速理解如何使用V8,其实V8静态库和其他的静态库是一样,下面以V8的hello-world为例子,看看如何使用V8。

  1. #include <stdio.h> 
  2. #include <stdlib.h> 
  3. #include <string.h> 
  4. #include "include/libplatform/libplatform.h" 
  5. #include "include/v8-context.h" 
  6. #include "include/v8-initialization.h" 
  7. #include "include/v8-isolate.h" 
  8. #include "include/v8-local-handle.h" 
  9. #include "include/v8-primitive.h" 
  10. #include "include/v8-script.h" 
  11.  
  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::NewFromUtf8Literal(isolate, "'Hello' + ', World!'"); 
  41.  
  42.       // Compile the source code. 
  43.       v8::Local<v8::Script> script = 
  44.           v8::Script::Compile(context, source).ToLocalChecked(); 
  45.  
  46.       // Run the script to get the result. 
  47.       v8::Local<v8::Value> result = script->Run(context).ToLocalChecked(); 
  48.  
  49.       // Convert the result to an UTF8 string and print it. 
  50.       v8::String::Utf8Value utf8(isolate, result); 
  51.       printf("%s\n", *utf8); 
  52.     } 
  53.  
  54.       const char csource[] = R"( 
  55.         let bytes = new Uint8Array([ 
  56.           0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x07, 0x01, 
  57.           0x60, 0x02, 0x7f, 0x7f, 0x01, 0x7f, 0x03, 0x02, 0x01, 0x00, 0x07, 
  58.           0x07, 0x01, 0x03, 0x61, 0x64, 0x64, 0x00, 0x00, 0x0a, 0x09, 0x01, 
  59.           0x07, 0x00, 0x20, 0x00, 0x20, 0x01, 0x6a, 0x0b 
  60.         ]); 
  61.         let module = new WebAssembly.Module(bytes); 
  62.         let instance = new WebAssembly.Instance(module); 
  63.         instance.exports.add(3, 4); 
  64.       )"; 
  65.  
  66.       // Create a string containing the JavaScript source code. 
  67.       v8::Local<v8::String> source = 
  68.           v8::String::NewFromUtf8Literal(isolate, csource); 
  69.  
  70.       // Compile the source code. 
  71.       v8::Local<v8::Script> script = 
  72.           v8::Script::Compile(context, source).ToLocalChecked(); 
  73.  
  74.       // Run the script to get the result. 
  75.       v8::Local<v8::Value> result = script->Run(context).ToLocalChecked(); 
  76.  
  77.       // Convert the result to a uint32 and print it. 
  78.       uint32_t number = result->Uint32Value(context).ToChecked(); 
  79.       printf("3 + 4 = %u\n", number); 
  80.     } 
  81.   } 
  82.  
  83.   // Dispose the isolate and tear down V8. 
  84.   isolate->Dispose(); 
  85.   v8::V8::Dispose(); 
  86.   v8::V8::ShutdownPlatform(); 
  87.   delete create_params.array_buffer_allocator; 
  88.   return 0; 

V8的API使用过程就是初始化V8,编译执行脚本,销毁V8。下面执行

  1. g++ -I. -Iinclude samples/hello-world.cc -o hello_world -lv8_monolith -Lout.gn/x64.release.sample/obj/ -pthread -std=c++14 -DV8_COMPRESS_POINTERS 

 

编译hello-world,执行./hello_world,看到相应输出。

 

责任编辑:武晓燕 来源: 编程杂技
相关推荐

2022-05-18 16:35:43

Redis内存运维

2023-06-02 07:45:39

2020-10-23 07:00:00

C++函数

2023-09-22 17:36:37

2021-01-28 22:31:33

分组密码算法

2020-05-22 08:16:07

PONGPONXG-PON

2018-06-07 13:17:12

契约测试单元测试API测试

2017-12-26 10:19:14

大数据问题缺陷

2022-08-08 08:25:21

Javajar 文件

2021-01-29 08:32:21

数据结构数组

2021-08-04 09:32:05

Typescript 技巧Partial

2023-05-15 08:38:58

模板方法模式

2018-11-29 09:13:47

CPU中断控制器

2019-02-13 14:15:59

Linux版本Fedora

2021-02-06 08:34:49

函数memoize文档

2023-07-06 13:56:14

微软Skype

2022-11-01 08:46:20

责任链模式对象

2020-10-15 06:56:51

MySQL排序

2023-06-25 09:44:00

一致性哈希数据库

2020-06-28 09:30:37

Linux内存操作系统
点赞
收藏

51CTO技术栈公众号