Writing Native Extension for Node
description
Transcript of Writing Native Extension for Node
Writing Native Extension
2010년 10월 25일 월요일
index
• Architecture
• Creating Module
• Writing Native Extension
• wscript
• Build
• Usage
2010년 10월 25일 월요일
Node binding
threadpool
(libeio)
eventloop
(libev)
V8
Node standard libraryJavaScript
C
architecture
2010년 10월 25일 월요일
about libev
• C event loop library
• Interface for I/O
• a file descriptor to become readable, wait for a timer, or wait for a signal to received one
• http://pod.tst.eu/http://cvs.schmorp.de/libev/ev.pod
2010년 10월 25일 월요일
about libeio
• C thread pool library
• Event-based fully asynchronous POSIX I/O
• The goal is to enable you to write fully non-blocking programs
• http://pod.tst.eu/http://cvs.schmorp.de/libeio/eio.pod
2010년 10월 25일 월요일
Creating Modules
• Using CommonJS Module System
• Proposal to ECMA TC39
• Presentation to ECMA TC39
2010년 10월 25일 월요일
Write Module
var NodeModule = function(){ this._friendName = “world”; this._message = “hello“;}
NodeModule.prototype.hello = function(){ return this._message +” “+ this._friendName +”!”;};
NodeModule.prototype.myNameIs = function(name){ this._friendName = name || “world”;};
exports.NodeModule = NodeModule;
2010년 10월 25일 월요일
Usage on REPL
$ node$ Type ‘.help’ for options.node>node>var module = require(‘./helloworld’);node>var NodeModule = new module.NodeModule();node>NodeModule.hello();‘hello world!’node>NodeModule.myNameIs(‘rhio’);node>NodeModule.hello();‘hello rhio’
2010년 10월 25일 월요일
Usage Module
$ cat app.jsvar module = require(‘./helloworld’);var NodeModule = new module.NodeModule();console.log(NodeModule.hello());
NodeModule.myNameIs(‘rhio’))console.log(NodeModule.hello());$$ node app.jshello world!hello rhio!
2010년 10월 25일 월요일
Writing Native Extension
• Node statically compiles all its dependencies into the executable.
• node::ObjectWrap
• #include <v8.h>
• extern “C” void init (Handle<Object> target)
2010년 10월 25일 월요일
write hello.cc
#include <v8.h>
using namespace v8;
extern "C" voidinit (Handle<Object> target) { HandleScope scope; target->Set(String::New("hello"), String::New("World"));}
2010년 10월 25일 월요일
wscript
• node-waf : Waf-based build system for Node
• $ node-waf configure build
• Waf : Python-based framework for configuring, compiling and installing applications
2010년 10월 25일 월요일
wscript
srcdir = '.'blddir = 'build'VERSION = '0.0.1'
def set_options(opt): opt.tool_options('compiler_cxx')
def configure(conf): conf.check_tool('compiler_cxx') conf.check_tool('node_addon')
def build(bld): obj = bld.new_task_gen('cxx', 'shlib', 'node_addon') obj.target = 'hello' obj.source = 'hello.cc'
2010년 10월 25일 월요일
Build
$ pwd/path/to/node-hello/$ lshello.cc wscript$ node-waf configure build
2010년 10월 25일 월요일
Build
Checking for program g++ or c++ : /usr/bin/g++ Checking for program cpp : /usr/bin/cpp Checking for program ar : /usr/bin/ar Checking for program ranlib : /usr/bin/ranlib Checking for g++ : ok Checking for node path : ok /Users/rhio/.node_libraries Checking for node prefix : ok /Users/rhio/local 'configure' finished successfully (0.040s)Waf: Entering directory `/Users/rhio/node-apps/node-hello/build'[1/2] cxx: node-hello.cc -> build/default/node-hello_1.o[2/2] cxx_link: build/default/node-hello_1.o -> build/default/node-hello.nodeWaf: Leaving directory `/Users/rhio/node-apps/node-hello/build''build' finished successfully (0.533s)
2010년 10월 25일 월요일
Build
$ pwd/path/to/node-hello/build/default$ lshello.node hello_1.o
2010년 10월 25일 월요일
Usage
$ pwd/path/to/node-hello/$ lshello.cc wscript build
2010년 10월 25일 월요일
Usage
$ pwd/path/to/node-hello/$ mkdir tests$ cd tests$ vi hello.js
2010년 10월 25일 월요일
Test
#hello.jsvar addon = require(‘../build/default/hello’);console.log(addon);console.log(addon.hello);
2010년 10월 25일 월요일
Test
$ pwd/path/to/node-hello/tests$ lshello.js$ node hello.js{ hello : ‘World’ }World
2010년 10월 25일 월요일
Reference
• V8 Embedder’s Documentationhttp://code.google.com/intl/ko-KR/apis/v8/embed.html
• V8 Cookbookhttp://create.tpsitulsa.com/wiki/V8_Cookbook
• NativeClienthttp://code.google.com/p/nativeclient/
• Writing Node.js Native Extensionshttps://www.cloudkick.com/blog/2010/aug/23/writing-nodejs-native-extensions/
• http://github.com/ry/node_postgres
2010년 10월 25일 월요일
QA
2010년 10월 25일 월요일
endingAuthor : rhio.kim
@rhiokim
http://rhio.tistory.com
FRENDS
NHNJavaScript Ninija
2010년 10월 25일 월요일
Thanks );
2010년 10월 25일 월요일