JavaScriptのv8エンジンをiPhoneで動かしてみた(駄目だった。。)

とりあえず準備

JavaScript エンジン V8で遊ぼう』の通りに、Mac上にてコンパイルしてlibv8.aを作成する。

iPhoneにてv8のHello, world!

まず、View-Base Applicationのプロジェクトをいつもどおりに新規作成する。

特に説明はいらないと思う。

プロジェクトのファイルリストにlibv8.aを追加(私はプロジェクト直下に置いた)

こんな感じ

v8のインクルードパスを通す

v8のインクルードファイルを/usr/local/include/v8/includeに置いたのでそこを設定する。
(プロジェクト内に置いたらパスを通さなくても良いのかな??)

*.mを*.mmに拡張子変更する

全て

適当なイベントでv8のスクリプトを実行させる

ボタンを配置して、ボタンのプッシュイベントで以下のようなスクリプトを書く。

MainView.h

#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import"v8.h"

v8::Handle<v8::Value> print(const v8::Arguments& args);

@interface MainView : UIView {
	IBOutlet UIButton *helloButton;
}

- (IBAction)hello;

@end


MainView.mm

#import "MainView.h"

v8::Handle<v8::Value> print(const v8::Arguments& args) {
	v8::String::AsciiValue str(args[0]);
	printf("%s", *str);
	puts("");
	return v8::Undefined();
}

@implementation MainView

- (IBAction)hello {
	// 準備
	v8::HandleScope handle_scope;
	
	// グローバルオブジェクトの生成
	v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New();
	
	// 関数名の生成
	v8::Handle<v8::String> str_print = v8::String::New("print");
	// 関数オブジェクトの生成
	v8::Handle<v8::FunctionTemplate> fn_print = v8::FunctionTemplate::New(print);
	// グローバルオブジェクトのプロパティに関数を設定
	global->Set(str_print, fn_print);
	
	// グローバルオブジェクトから環境を生成
	v8::Handle<v8::Context> context = v8::Context::New(NULL, global);
	// 環境からスコープを生成
	v8::Context::Scope context_scope(context);
	
	// 実行する文字列を生成
	v8::Handle<v8::String> str = v8::String::New("print('Hello, world!')");
	// Undefined を生成(ファイル名がないから)
	v8::Handle<v8::Primitive> undefined = v8::Undefined();
	// コンパイル
	v8::Handle<v8::Script> script = v8::Script::Compile(str, undefined);
	
	// 実行
	v8::Handle<v8::Value> result = script->Run();		
}

@end

コンパイル&実行

Hello, world!はコンソールに表示される。(←サボりました)

こんなんで、動いたし(汗

で・・

v8を入れて何しよ・・・
というか、実機で動くのかな・・・

追記

実機では動きませんでした(泣
i386コンパイルしてるから駄目とかそんな感じなのかな??