V8

1,944 views
1,735 views

Published on

0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,944
On SlideShare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
33
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

V8

  1. 1. V8 javascript engine 이혁재 2011.06.1 2
  2. 2. 목차• 소개• 장단점• 빌드• 사용 예제• v8 엔진이 빠른 이유
  3. 3. 소개• 구글 크롬에 사용된 자바스크립트 엔진• open source( New BSD License )• c++ 구현됨 ( 일부는 내장 javascript 로 구 현 )• windows / *nix / mac os X• x86 / ARM
  4. 4. 장점• 속도 : lua < v8 < lua-jit• 64bit version 지원• javascript 의 언어적 장점 – C 와 비슷 – Bit operation( <<, >> ) – 당연하게도 switch 가 있다 .• google 에서 작성 – 안전성 , 업데이트 주기• 인력 구하기 – 게임업계 외에서는 javascript 인력이 많다
  5. 5. 단점• 문서 ( 예제 )• 유저 라이브러리 확장• corutine 같은 기능이 없음• binder
  6. 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. 7. dll 버전 사용하기• USING_V8_SHARED – dll 을 사용하는 응용프로그램에서 전역적으로 define 해야 함 – debug 버전  v8_g.lib, v8_g.dll – release 버전  v8.lib, v8.dll
  8. 8. 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;}
  9. 9. 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;}
  10. 10. 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;}
  11. 11. 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;}
  12. 12. 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;}
  13. 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. 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. 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. 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. 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. 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. 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. 20. javascript 에서 c++ class 접근lass Pointublic: Point(int x, int y) : x_(x), y_(y) { } int x_, y_;;
  21. 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. 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. 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. 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. 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. 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. 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. 28. 한글 처리 ( 1 )• class String – 3 가지 nested class 를 가짐 class String { class AsciiValue { … }; // ASCII class Value { … }; // UTF-16 class Utf8Value { … }; // UTF-8 };
  29. 29. 한글처리 ( 2 ) • String 클래스 내부 클래스 중에서 Value 를 사 용 – input: unicode – output: unicodeHandle<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. 30. v8 이 빠른 이유 ?• Just-In-Time Compile• Garbage Collection – precise GC ( 자바가 사용한 gc 방식 ) – 더 진보됨• Inline Cache – property 찾은 결과 보관하여 cache• Hidden Classes
  31. 31. JIT
  32. 32. Hidden Classes( 1 )
  33. 33. Hidden Classes( 2 )
  34. 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. 35. Q&A

×