上一篇整理的Google V8 Javascript 引擎的编译,接下来在cygwin下面进行个应用测试,看看这个引擎如何实现JavaScript的。
吐槽:在浏览上编程都n年了,如今从底层来看看这些如何来实现,相信有兴趣的人都会忍不住的。一起来吧;
准备好文件:
头文件
/usr/include/v8.h
类文件
/usr/lib/libv8.so
接下来准备,在v8的源码中已有实例的源文件
F:\pbase\v8-trunk\samples\lineprocesssor.cc,process.cc,shell.cc
看一下源码shell.cc,这个毕竟复杂,但是比较合适研究。
在编译的out目录下
F:\pbase\v8-trunk\out\samples生成类似的
shell.target.ia32.mk
shell.target.native.mk
首先查找到编译输出libv8.so的输出路径
F:\pbase\v8-trunk\out\native\obj.target\tools\gyp
下面包括静态库和动态库
编写测试例子 testv8.cc
#include <v8.h>
#include <string.h>
using namespace v8;
Handle<Context> CreateShellContext(Isolate* isolate);
int main(int argc, char* argv[]) {
Isolate* isolate = Isolate::GetCurrent();
HandleScope handle_scope(isolate);
Handle<Context> context = CreateShellContext(isolate);
// Create a string containing the JavaScript source code.
Handle<String> source = String::New("'Hello' + ', World!'");
// Compile the source code.
Handle<Script> script = Script::Compile(source);
// Run the script to get the result.
Handle<Value> result = script->Run();
V8::Dispose();
// Convert the result to an ASCII string and print it.
String::AsciiValue ascii(result);
printf("%s\n", *ascii);
return 0;
}
Handle<Context> CreateShellContext(Isolate* isolate) {
// Create a template for the global object.
Handle<ObjectTemplate> global =ObjectTemplate::New();
return Context::New(isolate, NULL, global);
}
存在路径在/usr/test下
在/usr/include/v8下面包含v8.h文件
将动态库libv8.so和静态库libv8_base.ia32.a都复制到当前目录下,同时也存在于/usr/lib下
测试编译
g++ -I /usr/include/v8 testv8.cc -L /usr/lib/libv8.so -o testv8.exe -lpthread
编译报错
# g++-I /usr/include/v8 testv8.cc -L /usr/lib/libv8.so -o testv8.exe -lpthread
/tmp/ccqGRdEk.o:testv8.cc:(.text+0x7): undefined reference to `v8::ObjectTemplate::New()'
/tmp/ccqGRdEk.o:testv8.cc:(.text+0x38): undefined reference to `v8::Contex