Writing Native Extension for Node

24
Writing Native Extension 2010년 10월 25일 월요일

description

Writing Native Extension for Node.js

Transcript of Writing Native Extension for Node

Page 1: Writing Native Extension for Node

Writing Native Extension

2010년 10월 25일 월요일

Page 2: Writing Native Extension for Node

index

• Architecture

• Creating Module

• Writing Native Extension

• wscript

• Build

• Usage

2010년 10월 25일 월요일

Page 3: Writing Native Extension for Node

Node binding

threadpool

(libeio)

eventloop

(libev)

V8

Node standard libraryJavaScript

C

architecture

2010년 10월 25일 월요일

Page 4: Writing Native Extension for Node

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일 월요일

Page 5: Writing Native Extension for Node

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일 월요일

Page 7: Writing Native Extension for Node

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일 월요일

Page 8: Writing Native Extension for Node

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일 월요일

Page 9: Writing Native Extension for Node

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일 월요일

Page 10: Writing Native Extension for Node

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일 월요일

Page 11: Writing Native Extension for Node

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일 월요일

Page 12: Writing Native Extension for Node

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일 월요일

Page 13: Writing Native Extension for Node

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일 월요일

Page 14: Writing Native Extension for Node

Build

$ pwd/path/to/node-hello/$ lshello.cc wscript$ node-waf configure build

2010년 10월 25일 월요일

Page 15: Writing Native Extension for Node

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일 월요일

Page 16: Writing Native Extension for Node

Build

$ pwd/path/to/node-hello/build/default$ lshello.node hello_1.o

2010년 10월 25일 월요일

Page 17: Writing Native Extension for Node

Usage

$ pwd/path/to/node-hello/$ lshello.cc wscript build

2010년 10월 25일 월요일

Page 18: Writing Native Extension for Node

Usage

$ pwd/path/to/node-hello/$ mkdir tests$ cd tests$ vi hello.js

2010년 10월 25일 월요일

Page 19: Writing Native Extension for Node

Test

#hello.jsvar addon = require(‘../build/default/hello’);console.log(addon);console.log(addon.hello);

2010년 10월 25일 월요일

Page 20: Writing Native Extension for Node

Test

$ pwd/path/to/node-hello/tests$ lshello.js$ node hello.js{ hello : ‘World’ }World

2010년 10월 25일 월요일

Page 21: Writing Native Extension for Node

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일 월요일

Page 22: Writing Native Extension for Node

QA

2010년 10월 25일 월요일

Page 24: Writing Native Extension for Node

Thanks );

2010년 10월 25일 월요일