SlideShare uma empresa Scribd logo
1 de 35
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

Mais conteúdo relacionado

Mais procurados

Compose Async with RxJS
Compose Async with RxJSCompose Async with RxJS
Compose Async with RxJSKyung Yeol Kim
 
Javascript Everywhere
Javascript EverywhereJavascript Everywhere
Javascript EverywherePascal Rettig
 
Building fast interpreters in Rust
Building fast interpreters in RustBuilding fast interpreters in Rust
Building fast interpreters in RustIngvar Stepanyan
 
Virtual machine and javascript engine
Virtual machine and javascript engineVirtual machine and javascript engine
Virtual machine and javascript engineDuoyi Wu
 
The Ring programming language version 1.9 book - Part 90 of 210
The Ring programming language version 1.9 book - Part 90 of 210The Ring programming language version 1.9 book - Part 90 of 210
The Ring programming language version 1.9 book - Part 90 of 210Mahmoud Samir Fayed
 
What’s new in C# 6
What’s new in C# 6What’s new in C# 6
What’s new in C# 6Fiyaz Hasan
 
Using Reflections and Automatic Code Generation
Using Reflections and Automatic Code GenerationUsing Reflections and Automatic Code Generation
Using Reflections and Automatic Code GenerationIvan Dolgushin
 
Backbone.js: Run your Application Inside The Browser
Backbone.js: Run your Application Inside The BrowserBackbone.js: Run your Application Inside The Browser
Backbone.js: Run your Application Inside The BrowserHoward Lewis Ship
 
The Ring programming language version 1.3 book - Part 84 of 88
The Ring programming language version 1.3 book - Part 84 of 88The Ring programming language version 1.3 book - Part 84 of 88
The Ring programming language version 1.3 book - Part 84 of 88Mahmoud Samir Fayed
 
Cascadia.js: Don't Cross the Streams
Cascadia.js: Don't Cross the StreamsCascadia.js: Don't Cross the Streams
Cascadia.js: Don't Cross the Streamsmattpodwysocki
 
Look Ma, “update DB to HTML5 using C++”, no hands! 
Look Ma, “update DB to HTML5 using C++”, no hands! Look Ma, “update DB to HTML5 using C++”, no hands! 
Look Ma, “update DB to HTML5 using C++”, no hands! aleks-f
 
Letswift19-clean-architecture
Letswift19-clean-architectureLetswift19-clean-architecture
Letswift19-clean-architectureJung Kim
 
Dynamic C++ Silicon Valley Code Camp 2012
Dynamic C++ Silicon Valley Code Camp 2012Dynamic C++ Silicon Valley Code Camp 2012
Dynamic C++ Silicon Valley Code Camp 2012aleks-f
 
A JIT Smalltalk VM written in itself
A JIT Smalltalk VM written in itselfA JIT Smalltalk VM written in itself
A JIT Smalltalk VM written in itselfESUG
 
.NET Multithreading and File I/O
.NET Multithreading and File I/O.NET Multithreading and File I/O
.NET Multithreading and File I/OJussi Pohjolainen
 
Scalaz By Example (An IO Taster) -- PDXScala Meetup Jan 2014
Scalaz By Example (An IO Taster) -- PDXScala Meetup Jan 2014Scalaz By Example (An IO Taster) -- PDXScala Meetup Jan 2014
Scalaz By Example (An IO Taster) -- PDXScala Meetup Jan 2014Susan Potter
 

Mais procurados (20)

Compose Async with RxJS
Compose Async with RxJSCompose Async with RxJS
Compose Async with RxJS
 
Day 1
Day 1Day 1
Day 1
 
Javascript Everywhere
Javascript EverywhereJavascript Everywhere
Javascript Everywhere
 
Building fast interpreters in Rust
Building fast interpreters in RustBuilding fast interpreters in Rust
Building fast interpreters in Rust
 
Virtual machine and javascript engine
Virtual machine and javascript engineVirtual machine and javascript engine
Virtual machine and javascript engine
 
The Ring programming language version 1.9 book - Part 90 of 210
The Ring programming language version 1.9 book - Part 90 of 210The Ring programming language version 1.9 book - Part 90 of 210
The Ring programming language version 1.9 book - Part 90 of 210
 
Rust ⇋ JavaScript
Rust ⇋ JavaScriptRust ⇋ JavaScript
Rust ⇋ JavaScript
 
What’s new in C# 6
What’s new in C# 6What’s new in C# 6
What’s new in C# 6
 
Using Reflections and Automatic Code Generation
Using Reflections and Automatic Code GenerationUsing Reflections and Automatic Code Generation
Using Reflections and Automatic Code Generation
 
Backbone.js: Run your Application Inside The Browser
Backbone.js: Run your Application Inside The BrowserBackbone.js: Run your Application Inside The Browser
Backbone.js: Run your Application Inside The Browser
 
The Ring programming language version 1.3 book - Part 84 of 88
The Ring programming language version 1.3 book - Part 84 of 88The Ring programming language version 1.3 book - Part 84 of 88
The Ring programming language version 1.3 book - Part 84 of 88
 
Cascadia.js: Don't Cross the Streams
Cascadia.js: Don't Cross the StreamsCascadia.js: Don't Cross the Streams
Cascadia.js: Don't Cross the Streams
 
Say It With Javascript
Say It With JavascriptSay It With Javascript
Say It With Javascript
 
Look Ma, “update DB to HTML5 using C++”, no hands! 
Look Ma, “update DB to HTML5 using C++”, no hands! Look Ma, “update DB to HTML5 using C++”, no hands! 
Look Ma, “update DB to HTML5 using C++”, no hands! 
 
Letswift19-clean-architecture
Letswift19-clean-architectureLetswift19-clean-architecture
Letswift19-clean-architecture
 
Dynamic C++ Silicon Valley Code Camp 2012
Dynamic C++ Silicon Valley Code Camp 2012Dynamic C++ Silicon Valley Code Camp 2012
Dynamic C++ Silicon Valley Code Camp 2012
 
A JIT Smalltalk VM written in itself
A JIT Smalltalk VM written in itselfA JIT Smalltalk VM written in itself
A JIT Smalltalk VM written in itself
 
.NET Multithreading and File I/O
.NET Multithreading and File I/O.NET Multithreading and File I/O
.NET Multithreading and File I/O
 
Scalaz By Example (An IO Taster) -- PDXScala Meetup Jan 2014
Scalaz By Example (An IO Taster) -- PDXScala Meetup Jan 2014Scalaz By Example (An IO Taster) -- PDXScala Meetup Jan 2014
Scalaz By Example (An IO Taster) -- PDXScala Meetup Jan 2014
 
Python Objects
Python ObjectsPython Objects
Python Objects
 

Semelhante a V8

An Introduction to Property Based Testing
An Introduction to Property Based TestingAn Introduction to Property Based Testing
An Introduction to Property Based TestingC4Media
 
Алексей Кутумов, Coroutines everywhere
Алексей Кутумов, Coroutines everywhereАлексей Кутумов, Coroutines everywhere
Алексей Кутумов, Coroutines everywhereSergey Platonov
 
JJUG CCC 2011 Spring
JJUG CCC 2011 SpringJJUG CCC 2011 Spring
JJUG CCC 2011 SpringKiyotaka Oku
 
How to Write Node.js Module
How to Write Node.js ModuleHow to Write Node.js Module
How to Write Node.js ModuleFred Chien
 
Expert JavaScript tricks of the masters
Expert JavaScript  tricks of the mastersExpert JavaScript  tricks of the masters
Expert JavaScript tricks of the mastersAra Pehlivanian
 
Store and Process Big Data with Hadoop and Cassandra
Store and Process Big Data with Hadoop and CassandraStore and Process Big Data with Hadoop and Cassandra
Store and Process Big Data with Hadoop and CassandraDeependra Ariyadewa
 
How to make Ajax work for you
How to make Ajax work for youHow to make Ajax work for you
How to make Ajax work for youSimon Willison
 
Introduction to the New Asynchronous API in the .NET Driver
Introduction to the New Asynchronous API in the .NET DriverIntroduction to the New Asynchronous API in the .NET Driver
Introduction to the New Asynchronous API in the .NET DriverMongoDB
 
Apache Commons - Don\'t re-invent the wheel
Apache Commons - Don\'t re-invent the wheelApache Commons - Don\'t re-invent the wheel
Apache Commons - Don\'t re-invent the wheeltcurdt
 
TypeScript Introduction
TypeScript IntroductionTypeScript Introduction
TypeScript IntroductionDmitry Sheiko
 
JavaScript Multithread or Single Thread.pptx
JavaScript Multithread or Single Thread.pptxJavaScript Multithread or Single Thread.pptx
JavaScript Multithread or Single Thread.pptxRAHITNATH
 
Cascading Through Hadoop for the Boulder JUG
Cascading Through Hadoop for the Boulder JUGCascading Through Hadoop for the Boulder JUG
Cascading Through Hadoop for the Boulder JUGMatthew McCullough
 
Better Open Source Enterprise C++ Web Services
Better Open Source Enterprise C++ Web ServicesBetter Open Source Enterprise C++ Web Services
Better Open Source Enterprise C++ Web ServicesWSO2
 
4Developers 2018: Evolution of C++ Class Design (Mariusz Łapiński)
4Developers 2018: Evolution of C++ Class Design (Mariusz Łapiński)4Developers 2018: Evolution of C++ Class Design (Mariusz Łapiński)
4Developers 2018: Evolution of C++ Class Design (Mariusz Łapiński)PROIDEA
 
NoSQL and JavaScript: a Love Story
NoSQL and JavaScript: a Love StoryNoSQL and JavaScript: a Love Story
NoSQL and JavaScript: a Love StoryAlexandre Morgaut
 
HTML5: huh, what is it good for?
HTML5: huh, what is it good for?HTML5: huh, what is it good for?
HTML5: huh, what is it good for?Remy Sharp
 

Semelhante a V8 (20)

An Introduction to Property Based Testing
An Introduction to Property Based TestingAn Introduction to Property Based Testing
An Introduction to Property Based Testing
 
Алексей Кутумов, Coroutines everywhere
Алексей Кутумов, Coroutines everywhereАлексей Кутумов, Coroutines everywhere
Алексей Кутумов, Coroutines everywhere
 
JJUG CCC 2011 Spring
JJUG CCC 2011 SpringJJUG CCC 2011 Spring
JJUG CCC 2011 Spring
 
How to Write Node.js Module
How to Write Node.js ModuleHow to Write Node.js Module
How to Write Node.js Module
 
Expert JavaScript tricks of the masters
Expert JavaScript  tricks of the mastersExpert JavaScript  tricks of the masters
Expert JavaScript tricks of the masters
 
Store and Process Big Data with Hadoop and Cassandra
Store and Process Big Data with Hadoop and CassandraStore and Process Big Data with Hadoop and Cassandra
Store and Process Big Data with Hadoop and Cassandra
 
How to make Ajax work for you
How to make Ajax work for youHow to make Ajax work for you
How to make Ajax work for you
 
Introduction to the New Asynchronous API in the .NET Driver
Introduction to the New Asynchronous API in the .NET DriverIntroduction to the New Asynchronous API in the .NET Driver
Introduction to the New Asynchronous API in the .NET Driver
 
Apache Commons - Don\'t re-invent the wheel
Apache Commons - Don\'t re-invent the wheelApache Commons - Don\'t re-invent the wheel
Apache Commons - Don\'t re-invent the wheel
 
TypeScript Introduction
TypeScript IntroductionTypeScript Introduction
TypeScript Introduction
 
JavaScript Multithread or Single Thread.pptx
JavaScript Multithread or Single Thread.pptxJavaScript Multithread or Single Thread.pptx
JavaScript Multithread or Single Thread.pptx
 
java sockets
 java sockets java sockets
java sockets
 
Cascading Through Hadoop for the Boulder JUG
Cascading Through Hadoop for the Boulder JUGCascading Through Hadoop for the Boulder JUG
Cascading Through Hadoop for the Boulder JUG
 
Better Open Source Enterprise C++ Web Services
Better Open Source Enterprise C++ Web ServicesBetter Open Source Enterprise C++ Web Services
Better Open Source Enterprise C++ Web Services
 
Jason parsing
Jason parsingJason parsing
Jason parsing
 
Not your Grandma's XQuery
Not your Grandma's XQueryNot your Grandma's XQuery
Not your Grandma's XQuery
 
4Developers 2018: Evolution of C++ Class Design (Mariusz Łapiński)
4Developers 2018: Evolution of C++ Class Design (Mariusz Łapiński)4Developers 2018: Evolution of C++ Class Design (Mariusz Łapiński)
4Developers 2018: Evolution of C++ Class Design (Mariusz Łapiński)
 
JavaScript Lessons 2023
JavaScript Lessons 2023JavaScript Lessons 2023
JavaScript Lessons 2023
 
NoSQL and JavaScript: a Love Story
NoSQL and JavaScript: a Love StoryNoSQL and JavaScript: a Love Story
NoSQL and JavaScript: a Love Story
 
HTML5: huh, what is it good for?
HTML5: huh, what is it good for?HTML5: huh, what is it good for?
HTML5: huh, what is it good for?
 

Mais de changehee lee

Gdc 14 bringing unreal engine 4 to open_gl
Gdc 14 bringing unreal engine 4 to open_glGdc 14 bringing unreal engine 4 to open_gl
Gdc 14 bringing unreal engine 4 to open_glchangehee lee
 
Smedberg niklas bringing_aaa_graphics
Smedberg niklas bringing_aaa_graphicsSmedberg niklas bringing_aaa_graphics
Smedberg niklas bringing_aaa_graphicschangehee lee
 
Fortugno nick design_and_monetization
Fortugno nick design_and_monetizationFortugno nick design_and_monetization
Fortugno nick design_and_monetizationchangehee lee
 
[Kgc2013] 모바일 엔진 개발기
[Kgc2013] 모바일 엔진 개발기[Kgc2013] 모바일 엔진 개발기
[Kgc2013] 모바일 엔진 개발기changehee lee
 
모바일 엔진 개발기
모바일 엔진 개발기모바일 엔진 개발기
모바일 엔진 개발기changehee lee
 
Mobile crossplatformchallenges siggraph
Mobile crossplatformchallenges siggraphMobile crossplatformchallenges siggraph
Mobile crossplatformchallenges siggraphchangehee lee
 
개발 과정 최적화 하기 내부툴로 더욱 강력한 개발하기 Stephen kennedy _(11시40분_103호)
개발 과정 최적화 하기 내부툴로 더욱 강력한 개발하기 Stephen kennedy _(11시40분_103호)개발 과정 최적화 하기 내부툴로 더욱 강력한 개발하기 Stephen kennedy _(11시40분_103호)
개발 과정 최적화 하기 내부툴로 더욱 강력한 개발하기 Stephen kennedy _(11시40분_103호)changehee lee
 
개발자여! 스터디를 하자!
개발자여! 스터디를 하자!개발자여! 스터디를 하자!
개발자여! 스터디를 하자!changehee lee
 
[Kgc2012] deferred forward 이창희
[Kgc2012] deferred forward 이창희[Kgc2012] deferred forward 이창희
[Kgc2012] deferred forward 이창희changehee lee
 
Gamificated game developing
Gamificated game developingGamificated game developing
Gamificated game developingchangehee lee
 
Windows to reality getting the most out of direct3 d 10 graphics in your games
Windows to reality   getting the most out of direct3 d 10 graphics in your gamesWindows to reality   getting the most out of direct3 d 10 graphics in your games
Windows to reality getting the most out of direct3 d 10 graphics in your gameschangehee lee
 
Basic ofreflectance kor
Basic ofreflectance korBasic ofreflectance kor
Basic ofreflectance korchangehee lee
 
Valve handbook low_res
Valve handbook low_resValve handbook low_res
Valve handbook low_reschangehee lee
 

Mais de changehee lee (20)

Visual shock vol.2
Visual shock   vol.2Visual shock   vol.2
Visual shock vol.2
 
Shader compilation
Shader compilationShader compilation
Shader compilation
 
Gdc 14 bringing unreal engine 4 to open_gl
Gdc 14 bringing unreal engine 4 to open_glGdc 14 bringing unreal engine 4 to open_gl
Gdc 14 bringing unreal engine 4 to open_gl
 
Smedberg niklas bringing_aaa_graphics
Smedberg niklas bringing_aaa_graphicsSmedberg niklas bringing_aaa_graphics
Smedberg niklas bringing_aaa_graphics
 
Fortugno nick design_and_monetization
Fortugno nick design_and_monetizationFortugno nick design_and_monetization
Fortugno nick design_and_monetization
 
카툰 렌더링
카툰 렌더링카툰 렌더링
카툰 렌더링
 
[Kgc2013] 모바일 엔진 개발기
[Kgc2013] 모바일 엔진 개발기[Kgc2013] 모바일 엔진 개발기
[Kgc2013] 모바일 엔진 개발기
 
Paper games 2013
Paper games 2013Paper games 2013
Paper games 2013
 
모바일 엔진 개발기
모바일 엔진 개발기모바일 엔진 개발기
모바일 엔진 개발기
 
Wecanmakeengine
WecanmakeengineWecanmakeengine
Wecanmakeengine
 
Mobile crossplatformchallenges siggraph
Mobile crossplatformchallenges siggraphMobile crossplatformchallenges siggraph
Mobile crossplatformchallenges siggraph
 
개발 과정 최적화 하기 내부툴로 더욱 강력한 개발하기 Stephen kennedy _(11시40분_103호)
개발 과정 최적화 하기 내부툴로 더욱 강력한 개발하기 Stephen kennedy _(11시40분_103호)개발 과정 최적화 하기 내부툴로 더욱 강력한 개발하기 Stephen kennedy _(11시40분_103호)
개발 과정 최적화 하기 내부툴로 더욱 강력한 개발하기 Stephen kennedy _(11시40분_103호)
 
개발자여! 스터디를 하자!
개발자여! 스터디를 하자!개발자여! 스터디를 하자!
개발자여! 스터디를 하자!
 
[Kgc2012] deferred forward 이창희
[Kgc2012] deferred forward 이창희[Kgc2012] deferred forward 이창희
[Kgc2012] deferred forward 이창희
 
Light prepass
Light prepassLight prepass
Light prepass
 
Gamificated game developing
Gamificated game developingGamificated game developing
Gamificated game developing
 
Windows to reality getting the most out of direct3 d 10 graphics in your games
Windows to reality   getting the most out of direct3 d 10 graphics in your gamesWindows to reality   getting the most out of direct3 d 10 graphics in your games
Windows to reality getting the most out of direct3 d 10 graphics in your games
 
Basic ofreflectance kor
Basic ofreflectance korBasic ofreflectance kor
Basic ofreflectance kor
 
C++11(최지웅)
C++11(최지웅)C++11(최지웅)
C++11(최지웅)
 
Valve handbook low_res
Valve handbook low_resValve handbook low_res
Valve handbook low_res
 

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> 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. 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. 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. 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. 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. 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. JIT
  • 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. Q&A