V8 javascript engine

               이혁재

                2011.06.1
                2
목차

• 소개
• 장단점
• 빌드
• 사용 예제
• v8 엔진이 빠른 이유
소개

• 구글 크롬에 사용된 자바스크립트 엔진
• open source( New BSD License )
• c++ 구현됨 ( 일부는 내장 javascript 로 구
  현 )
• windows / *nix / mac os X
• x86 / ARM
장점

• 속도 : lua < v8 < lua-jit
• 64bit version 지원
• javascript 의 언어적 장점
 – C 와 비슷
 – Bit operation( <<, >> )
 – 당연하게도 switch 가 있다 .
• google 에서 작성
 – 안전성 , 업데이트 주기
• 인력 구하기
 – 게임업계 외에서는 javascript 인력이 많다
단점

• 문서 ( 예제 )
• 유저 라이브러리 확장
• corutine 같은 기능이 없음
• binder
빌드

• 컴파일된 버젼이 없으니 받아서 빌드해야 함
• 선행 조건
  – python 과 scon(make 같은 것 )
  – python 부터 설치 하는게 편함
• 빌드 ( vc2008 기준 )
  – Vcvarsall.bat
  – scons env="PATH:%PATH%,INCLUDE:%INCLUDE
    %,LIB:%LIB%" msvcrt=shared mode=debug
    library=shared snapshot=off
dll 버전 사용하기

• USING_V8_SHARED
  – dll 을 사용하는 응용프로그램에서 전역적으로
    define 해야 함
  – debug 버전
   v8_g.lib, v8_g.dll
 – release 버전
   v8.lib, v8.dll
hello world

#include <v8.h>
using namespace v8;

int main(int argc, char* argv[]) {
    HandleScope handle_scope;
    Persistent<Context> context = Context::New();
    Context::Scope context_scope(context);
    Handle<String> source = String::New("'Hello' + ', World!'");
    Handle<Script> script = Script::Compile(source);
    Handle<Value> result = script->Run();
    context.Dispose();

     String::AsciiValue ascii(result);
     printf("%sn", *ascii);
     return 0;
}
hello world

#include <v8.h>
using namespace v8;

int main(int argc, char* argv[]) {
    HandleScope handle_scope;
    Persistent<Context> context = Context::New();
    Context::Scope context_scope(context);
    Handle<String> source = String::New("'Hello' + ', World!'");
    Handle<Script> script = Script::Compile(source);
    Handle<Value> result = script->Run();
    context.Dispose();

     String::AsciiValue ascii(result);
     printf("%sn", *ascii);
     return 0;
}
hello world

#include <v8.h>
using namespace v8;

int main(int argc, char* argv[]) {
    HandleScope handle_scope;
    Persistent<Context> context = Context::New();
    Context::Scope context_scope(context);
    Handle<String> source = String::New("'Hello' + ', World!'");
    Handle<Script> script = Script::Compile(source);
    Handle<Value> result = script->Run();
    context.Dispose();

     String::AsciiValue ascii(result);
     printf("%sn", *ascii);
     return 0;
}
hello world

#include <v8.h>
using namespace v8;

int main(int argc, char* argv[]) {
    HandleScope handle_scope;
    Persistent<Context> context = Context::New();
    Context::Scope context_scope(context);
    Handle<String> source = String::New("'Hello' + ', World!'");
    Handle<Script> script = Script::Compile(source);
    Handle<Value> result = script->Run();
    context.Dispose();

     String::AsciiValue ascii(result);
     printf("%sn", *ascii);
     return 0;
}
hello world

#include <v8.h>
using namespace v8;

int main(int argc, char* argv[]) {
    HandleScope handle_scope;
    Persistent<Context> context = Context::New();
    Context::Scope context_scope(context);
    Handle<String> source = String::New("'Hello' + ', World!'");
    Handle<Script> script = Script::Compile(source);
    Handle<Value> result = script->Run();
    context.Dispose();

     String::AsciiValue ascii(result);
     printf("%sn", *ascii);
     return 0;
}
javascript 에서 c++ 함수를 호출

static Handle<Value> LogCallback(const Arguments& args)
{
    HandleScope scope;
    printf("LogCallback calledn");
    return v8::Undefined();
}

void Function()
{
    HandleScope handle_scope;
    Handle<ObjectTemplate> global = ObjectTemplate::New();
    global->Set(String::New("log"), FunctionTemplate::New(LogCallback));
    Persistent<Context> context = Context::New( NULL, global );
    Context::Scope context_scope(context);
    Handle<String> source = String::New("log();");
    Handle<Script> script = Script::Compile(source);
    script->Run();
    context.Dispose();
}
javascript 에서 c++ 함수를 호출

static Handle<Value> LogCallback(const Arguments& args)
{
    HandleScope scope;
    printf("LogCallback calledn");
    return v8::Undefined();
}

void Function()
{
    HandleScope handle_scope;
    Handle<ObjectTemplate> global = ObjectTemplate::New();
    global->Set(String::New("log"), FunctionTemplate::New(LogCallback));
    Persistent<Context> context = Context::New( NULL, global );
    Context::Scope context_scope(context);
    Handle<String> source = String::New("log();");
    Handle<Script> script = Script::Compile(source);
    script->Run();
    context.Dispose();
}
javascript 에서 c++ 함수를 호출

static Handle<Value> LogCallback(const Arguments& args)
{
    HandleScope scope;
    printf("LogCallback calledn");
    return v8::Undefined();
}

void Function()
{
    HandleScope handle_scope;
    Handle<ObjectTemplate> global = ObjectTemplate::New();
    global->Set(String::New("log"), FunctionTemplate::New(LogCallback));
    Persistent<Context> context = Context::New( NULL, global );
    Context::Scope context_scope(context);
    Handle<String> source = String::New("log();");
    Handle<Script> script = Script::Compile(source);
    script->Run();
    context.Dispose();
}
javascript 에서 c++ 함수를 호출

static Handle<Value> LogCallback(const Arguments& args)
{
    HandleScope scope;
    printf("LogCallback calledn");
    return v8::Undefined();
}

void Function()
{
    HandleScope handle_scope;
    Handle<ObjectTemplate> global = ObjectTemplate::New();
    global->Set(String::New("log"), FunctionTemplate::New(LogCallback));
    Persistent<Context> context = Context::New( NULL, global );
    Context::Scope context_scope(context);
    Handle<String> source = String::New("log();");
    Handle<Script> script = Script::Compile(source);
    script->Run();
    context.Dispose();
}
javascript 에서 c++ 전역 변수 읽고 / 쓰기

int x, y;

Handle<Value> XGetter(Local<String> property, const AccessorInfo& info)
{
    return Integer::New(x);
}

void XSetter(Local<String> property, Local<Value> value, const AccessorInfo& info
{
    x = value->Int32Value();
}
javascript 에서 c++ 전역 변수 읽고 / 쓰기

void GlobalVar()
{
    HandleScope handle_scope;
    Handle<ObjectTemplate> global_templ = ObjectTemplate::New();

     global_templ->SetAccessor(String::New("x"), XGetter, XSetter);
     global_templ->SetAccessor(String::New("y"), YGetter, YSetter);

     Persistent<Context> context = Context::New(NULL, global_templ);
     Context::Scope context_scope(context);
     Handle<String> source = String::New("x = 10; y = 10; x = y + 20;");
     Handle<Script> script = Script::Compile(source);
     Handle<Value> result = script->Run();
     context.Dispose();

     String::AsciiValue ascii(result);
     printf("%sn", *ascii);
}
javascript 에서 c++ 전역 변수 읽고 / 쓰기

void GlobalVar()
{
    HandleScope handle_scope;
    Handle<ObjectTemplate> global_templ = ObjectTemplate::New();

     global_templ->SetAccessor(String::New("x"), XGetter, XSetter);
     global_templ->SetAccessor(String::New("y"), YGetter, YSetter);

     Persistent<Context> context = Context::New(NULL, global_templ);
     Context::Scope context_scope(context);
     Handle<String> source = String::New("x = 10; y = 10; x = y + 20;");
     Handle<Script> script = Script::Compile(source);
     Handle<Value> result = script->Run();
     context.Dispose();

     String::AsciiValue ascii(result);
     printf("%sn", *ascii);
}
javascript 에서 c++ class 접근

lass Point

ublic:
   Point(int x, int y) : x_(x), y_(y) { }
   int x_, y_;
;
javascript 에서 c++ class 접근

HandleScope handle_scope;
Handle<FunctionTemplate> point_templ = FunctionTemplate::New();
Handle<ObjectTemplate> point_inst = point_templ->InstanceTemplate();

point_inst->SetInternalFieldCount(1);
point_inst->SetAccessor(String::New("x"), GetPointX, SetPointX);
point_inst->SetAccessor(String::New("y"), GetPointY, SetPointY);

Persistent<Context> context = Context::New();
Context::Scope context_scope(context);
Point* p = new Point(0, 0);
Handle<Function> point_ctor = point_templ->GetFunction();
Local<Object> obj = point_ctor->NewInstance();
obj->SetInternalField(0, External::New(p));
context->Global()->Set(String::New("point"), obj);
Handle<String> source = String::New("point.x = 200;");
Handle<Script> script = Script::Compile(source);
Handle<Value> result = script->Run();
context.Dispose();
javascript 에서 c++ class 접근

HandleScope handle_scope;
Handle<FunctionTemplate> point_templ = FunctionTemplate::New();
Handle<ObjectTemplate> point_inst = point_templ->InstanceTemplate();

point_inst->SetInternalFieldCount(1);
point_inst->SetAccessor(String::New("x"), GetPointX, SetPointX);
point_inst->SetAccessor(String::New("y"), GetPointY, SetPointY);

Persistent<Context> context = Context::New();
Context::Scope context_scope(context);
Point* p = new Point(0, 0);
Handle<Function> point_ctor = point_templ->GetFunction();
Local<Object> obj = point_ctor->NewInstance();
obj->SetInternalField(0, External::New(p));
context->Global()->Set(String::New("point"), obj);
Handle<String> source = String::New("point.x = 200;");
Handle<Script> script = Script::Compile(source);
Handle<Value> result = script->Run();
context.Dispose();
javascript 에서 c++ class 접근

HandleScope handle_scope;
Handle<FunctionTemplate> point_templ = FunctionTemplate::New();
Handle<ObjectTemplate> point_inst = point_templ->InstanceTemplate();

point_inst->SetInternalFieldCount(1);
point_inst->SetAccessor(String::New("x"), GetPointX, SetPointX);
point_inst->SetAccessor(String::New("y"), GetPointY, SetPointY);

Persistent<Context> context = Context::New();
Context::Scope context_scope(context);
Point* p = new Point(0, 0);
Handle<Function> point_ctor = point_templ->GetFunction();
Local<Object> obj = point_ctor->NewInstance();
obj->SetInternalField(0, External::New(p));
context->Global()->Set(String::New("point"), obj);
Handle<String> source = String::New("point.x = 200;");
Handle<Script> script = Script::Compile(source);
Handle<Value> result = script->Run();
context.Dispose();
javascript 에서 c++ class 접근

HandleScope handle_scope;
Handle<FunctionTemplate> point_templ = FunctionTemplate::New();
Handle<ObjectTemplate> point_inst = point_templ->InstanceTemplate();

point_inst->SetInternalFieldCount(1);
point_inst->SetAccessor(String::New("x"), GetPointX, SetPointX);
point_inst->SetAccessor(String::New("y"), GetPointY, SetPointY);

Persistent<Context> context = Context::New();
Context::Scope context_scope(context);
Point* p = new Point(0, 0);
Handle<Function> point_ctor = point_templ->GetFunction();
Local<Object> obj = point_ctor->NewInstance();
obj->SetInternalField(0, External::New(p));
context->Global()->Set(String::New("point"), obj);
Handle<String> source = String::New("point.x = 200;");
Handle<Script> script = Script::Compile(source);
Handle<Value> result = script->Run();
context.Dispose();
javascript 에서 c++ class 접근

HandleScope handle_scope;
Handle<FunctionTemplate> point_templ = FunctionTemplate::New();
Handle<ObjectTemplate> point_inst = point_templ->InstanceTemplate();

point_inst->SetInternalFieldCount(1);
point_inst->SetAccessor(String::New("x"), GetPointX, SetPointX);
point_inst->SetAccessor(String::New("y"), GetPointY, SetPointY);

Persistent<Context> context = Context::New();
Context::Scope context_scope(context);
Point* p = new Point(0, 0);
Handle<Function> point_ctor = point_templ->GetFunction();
Local<Object> obj = point_ctor->NewInstance();
obj->SetInternalField(0, External::New(p));
context->Global()->Set(String::New("point"), obj);
Handle<String> source = String::New("point.x = 200;");
Handle<Script> script = Script::Compile(source);
Handle<Value> result = script->Run();
context.Dispose();
javascript 에서 c++ class 접근

HandleScope handle_scope;
Handle<FunctionTemplate> point_templ = FunctionTemplate::New();
Handle<ObjectTemplate> point_inst = point_templ->InstanceTemplate();

point_inst->SetInternalFieldCount(1);
point_inst->SetAccessor(String::New("x"), GetPointX, SetPointX);
point_inst->SetAccessor(String::New("y"), GetPointY, SetPointY);

Persistent<Context> context = Context::New();
Context::Scope context_scope(context);
Point* p = new Point(0, 0);
Handle<Function> point_ctor = point_templ->GetFunction();
Local<Object> obj = point_ctor->NewInstance();
obj->SetInternalField(0, External::New(p));
context->Global()->Set(String::New("point"), obj);
Handle<String> source = String::New("point.x = 200;");
Handle<Script> script = Script::Compile(source);
Handle<Value> result = script->Run();
context.Dispose();
javascript 에서 c++ class 접근

Handle<Value> GetPointX(Local<String> property, const AccessorInfo &info)
{
    Local<Object> self = info.Holder();
    Local<External> wrap = Local<External>::Cast(self->GetInternalField(0));
    void* ptr = wrap->Value();
    int value = static_cast<Point*>(ptr)->x_;
    return Integer::New(value);
}

void SetPointX(Local<String> property, Local<Value> value, const AccessorInfo& in
{
    Local<Object> self = info.Holder();
    Local<External> wrap = Local<External>::Cast(self->GetInternalField(0));
    void* ptr = wrap->Value();
    static_cast<Point*>(ptr)->x_ = value->Int32Value();
}
한글 처리 ( 1 )

• class String
  – 3 가지 nested class 를 가짐


 class String {

      class AsciiValue { … };   // ASCII
      class Value { … };         // UTF-16
      class Utf8Value { … };    // UTF-8

 };
한글처리 ( 2 )

    • String 클래스 내부 클래스 중에서 Value 를 사
      용
      – input: unicode
      – output: unicode

Handle<String> source = String::New(L"' 한 ' + ' 글 '");
Handle<Script> script = Script::Compile(source);
Handle<Value> result = script->Run();
String::Value str(result);
wchar_t* p = *str;
v8 이 빠른 이유 ?

• Just-In-Time Compile
• Garbage Collection
  – precise GC ( 자바가 사용한 gc 방식 )
  – 더 진보됨
• Inline Cache
  – property 찾은 결과 보관하여 cache
• Hidden Classes
JIT
Hidden Classes( 1 )
Hidden Classes( 2 )
reference

• 빌드
  – http://code.google.com/p/v8/wiki/BuildingOnWind
    ows
• embeding
  – http://code.google.com/intl/ko-
    KR/apis/v8/embed.html
• cproxyv8
  – http://code.google.com/p/cproxyv8/
• 질답 게시판
  – http://groups.google.com/group/v8-users/topics
• 왜 빠른가 ?
  – http://techon.nikkeibp.co.jp/article/HONSHI/20090106/163617/
Q&A

V8

  • 1.
    V8 javascript engine 이혁재 2011.06.1 2
  • 2.
    목차 • 소개 • 장단점 •빌드 • 사용 예제 • v8 엔진이 빠른 이유
  • 3.
    소개 • 구글 크롬에사용된 자바스크립트 엔진 • open source( New BSD License ) • c++ 구현됨 ( 일부는 내장 javascript 로 구 현 ) • windows / *nix / mac os X • x86 / ARM
  • 4.
    장점 • 속도 :lua < v8 < lua-jit • 64bit version 지원 • javascript 의 언어적 장점 – C 와 비슷 – Bit operation( <<, >> ) – 당연하게도 switch 가 있다 . • google 에서 작성 – 안전성 , 업데이트 주기 • 인력 구하기 – 게임업계 외에서는 javascript 인력이 많다
  • 5.
    단점 • 문서 (예제 ) • 유저 라이브러리 확장 • corutine 같은 기능이 없음 • binder
  • 6.
    빌드 • 컴파일된 버젼이없으니 받아서 빌드해야 함 • 선행 조건 – python 과 scon(make 같은 것 ) – python 부터 설치 하는게 편함 • 빌드 ( vc2008 기준 ) – Vcvarsall.bat – scons env="PATH:%PATH%,INCLUDE:%INCLUDE %,LIB:%LIB%" msvcrt=shared mode=debug library=shared snapshot=off
  • 7.
    dll 버전 사용하기 •USING_V8_SHARED – dll 을 사용하는 응용프로그램에서 전역적으로 define 해야 함 – debug 버전  v8_g.lib, v8_g.dll – release 버전  v8.lib, v8.dll
  • 8.
    hello world #include <v8.h> usingnamespace v8; int main(int argc, char* argv[]) { HandleScope handle_scope; Persistent<Context> context = Context::New(); Context::Scope context_scope(context); Handle<String> source = String::New("'Hello' + ', World!'"); Handle<Script> script = Script::Compile(source); Handle<Value> result = script->Run(); context.Dispose(); String::AsciiValue ascii(result); printf("%sn", *ascii); return 0; }
  • 9.
    hello world #include <v8.h> usingnamespace v8; int main(int argc, char* argv[]) { HandleScope handle_scope; Persistent<Context> context = Context::New(); Context::Scope context_scope(context); Handle<String> source = String::New("'Hello' + ', World!'"); Handle<Script> script = Script::Compile(source); Handle<Value> result = script->Run(); context.Dispose(); String::AsciiValue ascii(result); printf("%sn", *ascii); return 0; }
  • 10.
    hello world #include <v8.h> usingnamespace v8; int main(int argc, char* argv[]) { HandleScope handle_scope; Persistent<Context> context = Context::New(); Context::Scope context_scope(context); Handle<String> source = String::New("'Hello' + ', World!'"); Handle<Script> script = Script::Compile(source); Handle<Value> result = script->Run(); context.Dispose(); String::AsciiValue ascii(result); printf("%sn", *ascii); return 0; }
  • 11.
    hello world #include <v8.h> usingnamespace v8; int main(int argc, char* argv[]) { HandleScope handle_scope; Persistent<Context> context = Context::New(); Context::Scope context_scope(context); Handle<String> source = String::New("'Hello' + ', World!'"); Handle<Script> script = Script::Compile(source); Handle<Value> result = script->Run(); context.Dispose(); String::AsciiValue ascii(result); printf("%sn", *ascii); return 0; }
  • 12.
    hello world #include <v8.h> usingnamespace v8; int main(int argc, char* argv[]) { HandleScope handle_scope; Persistent<Context> context = Context::New(); Context::Scope context_scope(context); Handle<String> source = String::New("'Hello' + ', World!'"); Handle<Script> script = Script::Compile(source); Handle<Value> result = script->Run(); context.Dispose(); String::AsciiValue ascii(result); printf("%sn", *ascii); return 0; }
  • 13.
    javascript 에서 c++함수를 호출 static Handle<Value> LogCallback(const Arguments& args) { HandleScope scope; printf("LogCallback calledn"); return v8::Undefined(); } void Function() { HandleScope handle_scope; Handle<ObjectTemplate> global = ObjectTemplate::New(); global->Set(String::New("log"), FunctionTemplate::New(LogCallback)); Persistent<Context> context = Context::New( NULL, global ); Context::Scope context_scope(context); Handle<String> source = String::New("log();"); Handle<Script> script = Script::Compile(source); script->Run(); context.Dispose(); }
  • 14.
    javascript 에서 c++함수를 호출 static Handle<Value> LogCallback(const Arguments& args) { HandleScope scope; printf("LogCallback calledn"); return v8::Undefined(); } void Function() { HandleScope handle_scope; Handle<ObjectTemplate> global = ObjectTemplate::New(); global->Set(String::New("log"), FunctionTemplate::New(LogCallback)); Persistent<Context> context = Context::New( NULL, global ); Context::Scope context_scope(context); Handle<String> source = String::New("log();"); Handle<Script> script = Script::Compile(source); script->Run(); context.Dispose(); }
  • 15.
    javascript 에서 c++함수를 호출 static Handle<Value> LogCallback(const Arguments& args) { HandleScope scope; printf("LogCallback calledn"); return v8::Undefined(); } void Function() { HandleScope handle_scope; Handle<ObjectTemplate> global = ObjectTemplate::New(); global->Set(String::New("log"), FunctionTemplate::New(LogCallback)); Persistent<Context> context = Context::New( NULL, global ); Context::Scope context_scope(context); Handle<String> source = String::New("log();"); Handle<Script> script = Script::Compile(source); script->Run(); context.Dispose(); }
  • 16.
    javascript 에서 c++함수를 호출 static Handle<Value> LogCallback(const Arguments& args) { HandleScope scope; printf("LogCallback calledn"); return v8::Undefined(); } void Function() { HandleScope handle_scope; Handle<ObjectTemplate> global = ObjectTemplate::New(); global->Set(String::New("log"), FunctionTemplate::New(LogCallback)); Persistent<Context> context = Context::New( NULL, global ); Context::Scope context_scope(context); Handle<String> source = String::New("log();"); Handle<Script> script = Script::Compile(source); script->Run(); context.Dispose(); }
  • 17.
    javascript 에서 c++전역 변수 읽고 / 쓰기 int x, y; Handle<Value> XGetter(Local<String> property, const AccessorInfo& info) { return Integer::New(x); } void XSetter(Local<String> property, Local<Value> value, const AccessorInfo& info { x = value->Int32Value(); }
  • 18.
    javascript 에서 c++전역 변수 읽고 / 쓰기 void GlobalVar() { HandleScope handle_scope; Handle<ObjectTemplate> global_templ = ObjectTemplate::New(); global_templ->SetAccessor(String::New("x"), XGetter, XSetter); global_templ->SetAccessor(String::New("y"), YGetter, YSetter); Persistent<Context> context = Context::New(NULL, global_templ); Context::Scope context_scope(context); Handle<String> source = String::New("x = 10; y = 10; x = y + 20;"); Handle<Script> script = Script::Compile(source); Handle<Value> result = script->Run(); context.Dispose(); String::AsciiValue ascii(result); printf("%sn", *ascii); }
  • 19.
    javascript 에서 c++전역 변수 읽고 / 쓰기 void GlobalVar() { HandleScope handle_scope; Handle<ObjectTemplate> global_templ = ObjectTemplate::New(); global_templ->SetAccessor(String::New("x"), XGetter, XSetter); global_templ->SetAccessor(String::New("y"), YGetter, YSetter); Persistent<Context> context = Context::New(NULL, global_templ); Context::Scope context_scope(context); Handle<String> source = String::New("x = 10; y = 10; x = y + 20;"); Handle<Script> script = Script::Compile(source); Handle<Value> result = script->Run(); context.Dispose(); String::AsciiValue ascii(result); printf("%sn", *ascii); }
  • 20.
    javascript 에서 c++class 접근 lass Point ublic: Point(int x, int y) : x_(x), y_(y) { } int x_, y_; ;
  • 21.
    javascript 에서 c++class 접근 HandleScope handle_scope; Handle<FunctionTemplate> point_templ = FunctionTemplate::New(); Handle<ObjectTemplate> point_inst = point_templ->InstanceTemplate(); point_inst->SetInternalFieldCount(1); point_inst->SetAccessor(String::New("x"), GetPointX, SetPointX); point_inst->SetAccessor(String::New("y"), GetPointY, SetPointY); Persistent<Context> context = Context::New(); Context::Scope context_scope(context); Point* p = new Point(0, 0); Handle<Function> point_ctor = point_templ->GetFunction(); Local<Object> obj = point_ctor->NewInstance(); obj->SetInternalField(0, External::New(p)); context->Global()->Set(String::New("point"), obj); Handle<String> source = String::New("point.x = 200;"); Handle<Script> script = Script::Compile(source); Handle<Value> result = script->Run(); context.Dispose();
  • 22.
    javascript 에서 c++class 접근 HandleScope handle_scope; Handle<FunctionTemplate> point_templ = FunctionTemplate::New(); Handle<ObjectTemplate> point_inst = point_templ->InstanceTemplate(); point_inst->SetInternalFieldCount(1); point_inst->SetAccessor(String::New("x"), GetPointX, SetPointX); point_inst->SetAccessor(String::New("y"), GetPointY, SetPointY); Persistent<Context> context = Context::New(); Context::Scope context_scope(context); Point* p = new Point(0, 0); Handle<Function> point_ctor = point_templ->GetFunction(); Local<Object> obj = point_ctor->NewInstance(); obj->SetInternalField(0, External::New(p)); context->Global()->Set(String::New("point"), obj); Handle<String> source = String::New("point.x = 200;"); Handle<Script> script = Script::Compile(source); Handle<Value> result = script->Run(); context.Dispose();
  • 23.
    javascript 에서 c++class 접근 HandleScope handle_scope; Handle<FunctionTemplate> point_templ = FunctionTemplate::New(); Handle<ObjectTemplate> point_inst = point_templ->InstanceTemplate(); point_inst->SetInternalFieldCount(1); point_inst->SetAccessor(String::New("x"), GetPointX, SetPointX); point_inst->SetAccessor(String::New("y"), GetPointY, SetPointY); Persistent<Context> context = Context::New(); Context::Scope context_scope(context); Point* p = new Point(0, 0); Handle<Function> point_ctor = point_templ->GetFunction(); Local<Object> obj = point_ctor->NewInstance(); obj->SetInternalField(0, External::New(p)); context->Global()->Set(String::New("point"), obj); Handle<String> source = String::New("point.x = 200;"); Handle<Script> script = Script::Compile(source); Handle<Value> result = script->Run(); context.Dispose();
  • 24.
    javascript 에서 c++class 접근 HandleScope handle_scope; Handle<FunctionTemplate> point_templ = FunctionTemplate::New(); Handle<ObjectTemplate> point_inst = point_templ->InstanceTemplate(); point_inst->SetInternalFieldCount(1); point_inst->SetAccessor(String::New("x"), GetPointX, SetPointX); point_inst->SetAccessor(String::New("y"), GetPointY, SetPointY); Persistent<Context> context = Context::New(); Context::Scope context_scope(context); Point* p = new Point(0, 0); Handle<Function> point_ctor = point_templ->GetFunction(); Local<Object> obj = point_ctor->NewInstance(); obj->SetInternalField(0, External::New(p)); context->Global()->Set(String::New("point"), obj); Handle<String> source = String::New("point.x = 200;"); Handle<Script> script = Script::Compile(source); Handle<Value> result = script->Run(); context.Dispose();
  • 25.
    javascript 에서 c++class 접근 HandleScope handle_scope; Handle<FunctionTemplate> point_templ = FunctionTemplate::New(); Handle<ObjectTemplate> point_inst = point_templ->InstanceTemplate(); point_inst->SetInternalFieldCount(1); point_inst->SetAccessor(String::New("x"), GetPointX, SetPointX); point_inst->SetAccessor(String::New("y"), GetPointY, SetPointY); Persistent<Context> context = Context::New(); Context::Scope context_scope(context); Point* p = new Point(0, 0); Handle<Function> point_ctor = point_templ->GetFunction(); Local<Object> obj = point_ctor->NewInstance(); obj->SetInternalField(0, External::New(p)); context->Global()->Set(String::New("point"), obj); Handle<String> source = String::New("point.x = 200;"); Handle<Script> script = Script::Compile(source); Handle<Value> result = script->Run(); context.Dispose();
  • 26.
    javascript 에서 c++class 접근 HandleScope handle_scope; Handle<FunctionTemplate> point_templ = FunctionTemplate::New(); Handle<ObjectTemplate> point_inst = point_templ->InstanceTemplate(); point_inst->SetInternalFieldCount(1); point_inst->SetAccessor(String::New("x"), GetPointX, SetPointX); point_inst->SetAccessor(String::New("y"), GetPointY, SetPointY); Persistent<Context> context = Context::New(); Context::Scope context_scope(context); Point* p = new Point(0, 0); Handle<Function> point_ctor = point_templ->GetFunction(); Local<Object> obj = point_ctor->NewInstance(); obj->SetInternalField(0, External::New(p)); context->Global()->Set(String::New("point"), obj); Handle<String> source = String::New("point.x = 200;"); Handle<Script> script = Script::Compile(source); Handle<Value> result = script->Run(); context.Dispose();
  • 27.
    javascript 에서 c++class 접근 Handle<Value> GetPointX(Local<String> property, const AccessorInfo &info) { Local<Object> self = info.Holder(); Local<External> wrap = Local<External>::Cast(self->GetInternalField(0)); void* ptr = wrap->Value(); int value = static_cast<Point*>(ptr)->x_; return Integer::New(value); } void SetPointX(Local<String> property, Local<Value> value, const AccessorInfo& in { Local<Object> self = info.Holder(); Local<External> wrap = Local<External>::Cast(self->GetInternalField(0)); void* ptr = wrap->Value(); static_cast<Point*>(ptr)->x_ = value->Int32Value(); }
  • 28.
    한글 처리 (1 ) • class String – 3 가지 nested class 를 가짐 class String { class AsciiValue { … }; // ASCII class Value { … }; // UTF-16 class Utf8Value { … }; // UTF-8 };
  • 29.
    한글처리 ( 2) • String 클래스 내부 클래스 중에서 Value 를 사 용 – input: unicode – output: unicode Handle<String> source = String::New(L"' 한 ' + ' 글 '"); Handle<Script> script = Script::Compile(source); Handle<Value> result = script->Run(); String::Value str(result); wchar_t* p = *str;
  • 30.
    v8 이 빠른이유 ? • Just-In-Time Compile • Garbage Collection – precise GC ( 자바가 사용한 gc 방식 ) – 더 진보됨 • Inline Cache – property 찾은 결과 보관하여 cache • Hidden Classes
  • 31.
  • 32.
  • 33.
  • 34.
    reference • 빌드 – http://code.google.com/p/v8/wiki/BuildingOnWind ows • embeding – http://code.google.com/intl/ko- KR/apis/v8/embed.html • cproxyv8 – http://code.google.com/p/cproxyv8/ • 질답 게시판 – http://groups.google.com/group/v8-users/topics • 왜 빠른가 ? – http://techon.nikkeibp.co.jp/article/HONSHI/20090106/163617/
  • 35.