成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

聊一聊編譯和使用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

大數據問題缺陷

2019-02-13 14:15:59

Linux版本Fedora

2021-08-04 09:32:05

Typescript 技巧Partial

2021-01-29 08:32:21

數據結構數組

2021-02-06 08:34:49

函數memoize文檔

2022-08-08 08:25:21

Javajar 文件

2023-07-06 13:56:14

微軟Skype

2023-05-15 08:38:58

模板方法模式

2022-11-01 08:46:20

責任鏈模式對象

2018-11-29 09:13:47

CPU中斷控制器

2020-10-15 06:56:51

MySQL排序

2020-08-12 08:34:16

開發安全We

2021-01-01 09:01:05

前端組件化設計
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 亚洲小说图片 | 中文字幕日韩欧美 | 日韩视频在线免费观看 | 99久热 | 精品三级在线观看 | 日韩欧美在线免费 | 天天干天天操天天爽 | 中文在线www | 国产精品久久久爽爽爽麻豆色哟哟 | 日韩一级免费电影 | 国产精品亚洲精品日韩已方 | 午夜视频在线观看视频 | 久热免费在线 | 欧美日韩国产高清 | 涩涩视频大全 | 国产一区二区三区免费观看视频 | 天天艹日日干 | 亚洲精品乱码8久久久久久日本 | 久久精品中文 | 日本精品一区二区三区在线观看视频 | 久久免费视频网 | 国产丝袜一区二区三区免费视频 | 日韩国产一区二区三区 | 高清国产午夜精品久久久久久 | 国产免费福利在线 | 51ⅴ精品国产91久久久久久 | 久久视频免费观看 | 欧美另类视频 | 国产九一精品 | 国产aⅴ爽av久久久久久久 | 视频一区中文字幕 | 91免费在线看 | a亚洲精品 | 亚洲最大的黄色网址 | 成人黄色电影免费 | 久久久av一区 | 一区二区视频在线 | 久久免费福利 | 精品一区在线 | 国产午夜精品视频 | 亚洲xxxxx |