现在的位置: 首页 > 综合 > 正文

Google V8 JavaScript应用测试实例

2013年12月05日 ⁄ 综合 ⁄ 共 10398字 ⁄ 字号 评论关闭

上一篇整理的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::Context::New(v8::Isolate*, v8::ExtensionConfiguration*, v8::Handle<v8::ObjectTemplate>, v8::Handle<v8::Value>)'
/tmp/ccqGRdEk.o:testv8.cc:(.text+0x5d): undefined reference to `v8::Isolate::GetCurrent()'
/tmp/ccqGRdEk.o:testv8.cc:(.text+0x72): undefined reference to `v8::HandleScope::HandleScope(v8::Isolate*)'
/tmp/ccqGRdEk.o:testv8.cc:(.text+0x93): undefined reference to `v8::Isolate::GetCurrent()'
/tmp/ccqGRdEk.o:testv8.cc:(.text+0xb1): undefined reference to `v8::String::NewFromUtf8(v8::Isolate*, char const*, v8::String::NewStringType, int)'
/tmp/ccqGRdEk.o:testv8.cc:(.text+0xe3): undefined reference to `v8::Script::Compile(v8::Handle<v8::String>, v8::ScriptOrigin*, v8::ScriptData*, v8::Handle<v8::String>)'
/tmp/ccqGRdEk.o:testv8.cc:(.text+0xf7): undefined reference to `v8::Script::Run()'
/tmp/ccqGRdEk.o:testv8.cc:(.text+0x105): undefined reference to `v8::V8::Dispose()'
/tmp/ccqGRdEk.o:testv8.cc:(.text+0x117): undefined reference to `v8::String::AsciiValue::AsciiValue(v8::Handle<v8::Value>)'
/tmp/ccqGRdEk.o:testv8.cc:(.text+0x13c): undefined reference to `v8::String::AsciiValue::~AsciiValue()'
/tmp/ccqGRdEk.o:testv8.cc:(.text+0x155): undefined reference to `v8::String::AsciiValue::~AsciiValue()'
/tmp/ccqGRdEk.o:testv8.cc:(.text+0x168): undefined reference to `v8::HandleScope::~HandleScope()'
/tmp/ccqGRdEk.o:testv8.cc:(.text+0x18d): undefined reference to `v8::HandleScope::~HandleScope()'
collect2: ld returned 1 exit status

分析原因,连接库的方式存在问题

g++ -I /usr/include/v8 testv8.cc -o testv8 /usr/test/libv8_base.ia32.a -lpthread

huareal@gpx /usr/test
# g++ -I /usr/include/v8 testv8.cc -o testv8 /usr/test/libv8_base.ia32.a -lpthread
/usr/test/libv8_base.ia32.a(heap.o):heap.cc:(.text+0x181a2): undefined reference to `v8::internal::NativesCollection<(v8::internal::NativeType)0>::GetBuiltinsCount()'
/usr/test/libv8_base.ia32.a(heap.o):heap.cc:(.text$_ZN2v88internal8Snapshot9IsEnabledEv[v8::internal::Snapshot::IsEnabled()]+0x4): undefined reference to `v8::internal::Snapshot::size_'
/usr/test/libv8_base.ia32.a(snapshot-common.o):snapshot-common.cc:(.text+0x7): undefined reference to `v8::internal::Snapshot::new_space_used_'
/usr/test/libv8_base.ia32.a(snapshot-common.o):snapshot-common.cc:(.text+0x23): undefined reference to `v8::internal::Snapshot::pointer_space_used_'
/usr/test/libv8_base.ia32.a(snapshot-common.o):snapshot-common.cc:(.text+0x3f): undefined reference to `v8::internal::Snapshot::data_space_used_'
/usr/test/libv8_base.ia32.a(snapshot-common.o):snapshot-common.cc:(.text+0x5b): undefined reference to `v8::internal::Snapshot::code_space_used_'
/usr/test/libv8_base.ia32.a(snapshot-common.o):snapshot-common.cc:(.text+0x77): undefined reference to `v8::internal::Snapshot::map_space_used_'
/usr/test/libv8_base.ia32.a(snapshot-common.o):snapshot-common.cc:(.text+0x93): undefined reference to `v8::internal::Snapshot::cell_space_used_'
/usr/test/libv8_base.ia32.a(snapshot-common.o):snapshot-common.cc:(.text+0xaf): undefined reference to `v8::internal::Snapshot::property_cell_space_used_'
/usr/test/libv8_base.ia32.a(snapshot-common.o):snapshot-common.cc:(.text+0xd0): undefined reference to `v8::internal::Snapshot::size_'
/usr/test/libv8_base.ia32.a(snapshot-common.o):snapshot-common.cc:(.text+0xe8): undefined reference to `v8::internal::Snapshot::context_size_'
/usr/test/libv8_base.ia32.a(snapshot-common.o):snapshot-common.cc:(.text+0x103): undefined reference to `v8::internal::Snapshot::context_raw_size_'
/usr/test/libv8_base.ia32.a(snapshot-common.o):snapshot-common.cc:(.text+0x109): undefined reference to `v8::internal::Snapshot::context_raw_data_'
/usr/test/libv8_base.ia32.a(snapshot-common.o):snapshot-common.cc:(.text+0x133): undefined reference to `v8::internal::Snapshot::context_new_space_used_'
/usr/test/libv8_base.ia32.a(snapshot-common.o):snapshot-common.cc:(.text+0x14f): undefined reference to `v8::internal::Snapshot::context_pointer_space_used_'
/usr/test/libv8_base.ia32.a(snapshot-common.o):snapshot-common.cc:(.text+0x16b): undefined reference to `v8::internal::Snapshot::context_data_space_used_'
/usr/test/libv8_base.ia32.a(snapshot-common.o):snapshot-common.cc:(.text+0x187): undefined reference to `v8::internal::Snapshot::context_code_space_used_'
/usr/test/libv8_base.ia32.a(snapshot-common.o):snapshot-common.cc:(.text+0x1a3): undefined reference to `v8::internal::Snapshot::context_map_space_used_'
/usr/test/libv8_base.ia32.a(snapshot-common.o):snapshot-common.cc:(.text+0x1bf): undefined reference to `v8::internal::Snapshot::context_cell_space_used_'
/usr/test/libv8_base.ia32.a(snapshot-common.o):snapshot-common.cc:(.text+0x1db): undefined reference to `v8::internal::Snapshot::context_property_cell_space_used_'
/usr/test/libv8_base.ia32.a(snapshot-common.o):snapshot-common.cc:(.text+0x712): undefined reference to `v8::internal::Snapshot::size_'
/usr/test/libv8_base.ia32.a(snapshot-common.o):snapshot-common.cc:(.text+0x71f): undefined reference to `v8::internal::Snapshot::raw_size_'
/usr/test/libv8_base.ia32.a(snapshot-common.o):snapshot-common.cc:(.text+0x725): undefined reference to `v8::internal::Snapshot::raw_data_'
/usr/test/libv8_base.ia32.a(bootstrapper.o):bootstrapper.cc:(.text+0x4933): undefined reference to `v8::internal::NativesCollection<(v8::internal::NativeType)1>::GetScriptName(int)'
/usr/test/libv8_base.ia32.a(bootstrapper.o):bootstrapper.cc:(.text+0x4952): undefined reference to `v8::internal::NativesCollection<(v8::internal::NativeType)1>::GetRawScriptSource(int)'
/usr/test/libv8_base.ia32.a(bootstrapper.o):bootstrapper.cc:(.text+0x499d): undefined reference to `v8::internal::NativesCollection<(v8::internal::NativeType)1>::GetDebuggerCount()'
/usr/test/libv8_base.ia32.a(bootstrapper.o):bootstrapper.cc:(.text+0x49be): undefined reference to `v8::internal::NativesCollection<(v8::internal::NativeType)1>::GetScriptName(int)'
/usr/test/libv8_base.ia32.a(bootstrapper.o):bootstrapper.cc:(.text+0x4a3a): undefined reference to `v8::internal::NativesCollection<(v8::internal::NativeType)1>::GetScriptName(int)'
/usr/test/libv8_base.ia32.a(bootstrapper.o):bootstrapper.cc:(.text+0x4ab6): undefined reference to `v8::internal::NativesCollection<(v8::internal::NativeType)1>::GetScriptName(int)'
/usr/test/libv8_base.ia32.a(bootstrapper.o):bootstrapper.cc:(.text+0x4b32): undefined reference to `v8::internal::NativesCollection<(v8::internal::NativeType)1>::GetScriptName(int)'
/usr/test/libv8_base.ia32.a(bootstrapper.o):bootstrapper.cc:(.text+0x4bae): undefined reference to `v8::internal::NativesCollection<(v8::internal::NativeType)1>::GetScriptName(int)'
/usr/test/libv8_base.ia32.a(bootstrapper.o):bootstrapper.cc:(.text+0x4c2a): more undefined references to `v8::internal::NativesCollection<(v8::internal::NativeType)1>::GetScriptName(int)' follow
/usr/test/libv8_base.ia32.a(bootstrapper.o):bootstrapper.cc:(.text+0x4e83): undefined reference to `v8::internal::NativesCollection<(v8::internal::NativeType)1>::GetBuiltinsCount()'
/usr/test/libv8_base.ia32.a(bootstrapper.o):bootstrapper.cc:(.text+0x118e1): undefined reference to `v8::internal::NativesCollection<(v8::internal::NativeType)0>::GetRawScriptSource(int)'
/usr/test/libv8_base.ia32.a(bootstrapper.o):bootstrapper.cc:(.text+0x11a29): undefined reference to `v8::internal::NativesCollection<(v8::internal::NativeType)0>::GetScriptName(int)'
/usr/test/libv8_base.ia32.a(bootstrapper.o):bootstrapper.cc:(.text+0x140cd): undefined reference to `v8::internal::NativesCollection<(v8::internal::NativeType)0>::GetDebuggerCount()'
/usr/test/libv8_base.ia32.a(bootstrapper.o):bootstrapper.cc:(.text+0x1413b): undefined reference to `v8::internal::NativesCollection<(v8::internal::NativeType)0>::GetBuiltinsCount()'
/usr/test/libv8_base.ia32.a(debug.o):debug.cc:(.text+0x4e49): undefined reference to `v8::internal::NativesCollection<(v8::internal::NativeType)0>::GetScriptName(int)'
/usr/test/libv8_base.ia32.a(debug.o):debug.cc:(.text+0x5ef3): undefined reference to `v8::internal::NativesCollection<(v8::internal::NativeType)0>::GetIndex(char const*)'
/usr/test/libv8_base.ia32.a(debug.o):debug.cc:(.text+0x5f0e): undefined reference to `v8::internal::NativesCollection<(v8::internal::NativeType)0>::GetIndex(char const*)'
/usr/test/libv8_base.ia32.a(debug.o):debug.cc:(.text+0x5f54): undefined reference to `v8::internal::NativesCollection<(v8::internal::NativeType)0>::GetIndex(char const*)'
/usr/test/libv8_base.ia32.a(serialize.o):serialize.cc:(.text+0x1a5c): undefined reference to `v8::internal::NativesCollection<(v8::internal::NativeType)0>::GetBuiltinsCount()'
/usr/test/libv8_base.ia32.a(serialize.o):serialize.cc:(.text+0x4d7a): undefined reference to `v8::internal::NativesCollection<(v8::internal::NativeType)0>::GetBuiltinsCount()'
/usr/test/libv8_base.ia32.a(serialize.o):serialize.cc:(.text+0x7e92): undefined reference to `v8::internal::NativesCollection<(v8::internal::NativeType)0>::GetRawScriptSource(int)'
collect2: ld returned 1 exit status

貌似还有有连接错误。

# g++ -I /usr/include/v8 testv8.cc -o testv82.exe /usr/test/libv8.so -lpthread

连接动态库,编译成功。

抱歉!评论已关闭.