V8是个比较麻烦的事情,不仅是下载编译的过程,不同的系统,不同的编译器,不同的C++版本,可能会出现不同的问题。之前编译的时候没有记录步骤。这次简单记录一下编译V8的过程。我的工作目录是/code/v8_code/。1编译V82编译V8为静态库3使用V81.编译V81下载工具:gitclonehttps://chromium.googlesource.com/chromium/tools/depot_tools.git2执行exportPATH=$PATH:/code/v8_code/depot_toolsfor后续执行命令使用。3执行gclientconfighttps://chromium.googlesource.com/v8/v8.git4执行gclientsync(成功后当前目录下会多出一个v8目录,此时有depot_tools和v8两个目录)5执行命令:aliasgm=/code/v8_code/v8/tools/dev/gm.py,然后cdv8进入v8源码目录。6执行gmx64.release,编译成功,新建一个hello.js,执行out/x64.release/d8hello.js可以看到相应的输出,或者执行out/x64.release/d8进入交互模式。2.将V8编译成静态库执行aliasv8gen=/code/v8_code/v8/tools/dev/v8gen.pyv8源码目录下执行v8genx64.release.sample生成配置文件,执行ninja-Cout.gn/x64。release.samplevv8_monolith编译静态库。3.使用V8我们可以在自己的项目中使用V8。这方面的例子已经很多了。Node.js是一个典型的例子,但是Node.js比较复杂,不利于快速了解如何使用V8。其实V8静态库和其他静态库也是一样的。下面以V8的hello-world为例,看看V8的使用方法。#include#include#include#include"include/libplatform/libplatform.h"#include"include/v8-context.h"#include"include/v8-初始化.h"#include"include/v8-isolate.h"#include"include/v8-local-handle.h"#include"include/v8-primitive.h"#include"include/v8-script.h"intmain(intargc,char*argv[]){//InitializeV8.v8::V8::InitializeICUDefaultLocation(argv[0]);v8::V8::InitializeExternalStartupData(argv[0]);std::unique_ptrplatform=v8::platform::NewDefaultPlatform();v8::V8::InitializePlatform(platform.get());v8::V8::Initialize();//CreateanewIsolateandmakeitthecurrentone.v8::Isolate::CreateParamscreate_params;create_params.array_buffer_allocator=v8::ArrayBuffer::Allocator::NewDefaultAllocator();v8::Isolate*isolate=v8::Isolate::New(create_params);{v8::Isolate::Scopeisolate_scope(isolate);//Createastack-allocatedhandlescope.v8::HandleScopehandle_scope(isolate);//Createanewcontext.v8::Localcontext=v8::Context::New(isolate);//进入编译运行thehelloworldscript.v8::Context::Scopecontext_scope(context);{//创建一个包含JavaScript源代码的字符串。v8::Localsource=v8::String::NewFromUtf8Literal(isolate,"'Hello'+',World!'");//编译源代码.v8::Localscript=v8::Script::Compile(context,source).ToLocalChecked();//运行脚本得到结果.v8::Localresult=script->Run(context).ToLocalChecked();//将结果转换为UTF8字符串并打印it.v8::String::Utf8Valueutf8(isolate,result);printf("%s\n",*utf8);}constcharcsource[]=R"(letbytes=newUint8Array([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]);letmodule=newWebAssembly.Module(bytes);letinstance=newWebAssembly.Instance(module);instance.exports.add(3,4);)";//创建包含JavaScript源代码的字符串.v8::Localsource=v8::String::NewFromUtf8Literal(isolate,csource);//Compilethesourcecode.v8::Localscript=v8::Script::Compile(context,source).ToLocalChecked();//Runthescripttogetthersult.v8::Localresult=script->Run(context).ToLocalChecked();//将结果转换为auint32andprintit.uint32_tnumber=result->Uint32Value(context).ToChecked();printf("3+4=%u\n",number);}}//DisposetheisolateandteardownV8.isolate->Dispose();v8::V8::Dispose();v8::V8::ShutdownPlatform();deletecreate_params.array_buffer_allocator;return0;}V8API使用流程为初始化V8,编译执行脚本,V8下执行g++-I.-Iincludesamples/hello-world.cc-ohello_world-lv8_monolith-Lout.gn/x64.release.sample/obj/-pthread-std=c++14-DV8_COMPRESS_POINTERS编译hello-world,执行./hello_world,查看co响应输出。