本系列总结自己对于V8的学习过程,希望能对后来者可以有所帮助。
hello v8。 主要介绍V8的编译以及最简单的实现。
一、check out 源码 地址: http://v8.googlecode.com/svn/trunk/
二、编译成库。
1、需要使用scons,去官网scons.org可以找到。 http://scons.org/download.php。解压以后,用命令行运行 python setup.py install。
2、转到V8的目录,运行 scons mode=debug library=static snapshot=on 需要等不少时间,会出来两个.a文件
3、把v8目录下的include文件夹以及libv8_g.a拷到工程下,添加就可以使用了。具体到ios的xcode项目,编译还需要更改语言Compile Sources As选项为Objective-C++,或者使用mm文件。然后模拟器可以调试,设备没法编译。我尝试用arch=arm编译设备.a,但是不能编译成功,暂时没有找到可以编译ios设备的办法,所以ios上只能自己把玩先。
三、hello world.
#include "v8.h"
using namespace v8;
- (void)printHello
{
HandleScope handle_scope;
Persistent<Context> context = Context::New();
Context::Scope context_scope(context);
Handle<String> source = String::New("'Hello V8'");
Handle<Script> script = Script::Compile(source);
Handle<Value> result = script->Run();
String::Utf8Value utf8Result(result);
NSString *resultString = [NSString stringWithCString:*utf8Result
encoding:NSUTF8StringEncoding];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Result"
message:resultString
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
[alert release];
context.Dispose();
}
大功告成,运行一段纯字符串脚本,然后alert出来。