Google V8 JavaScript应用测试实例

上一篇整理的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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值