Created
February 3, 2011 20:41
-
-
Save polaris/810153 to your computer and use it in GitHub Desktop.
node.js extension causing a segmentation fault
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <v8.h> | |
#include <node.h> | |
#include <node_events.h> | |
#include <boost/thread.hpp> | |
#include <boost/bind.hpp> | |
#include <iostream> | |
using namespace node; | |
using namespace v8; | |
using namespace boost; | |
class HelloWorld : public EventEmitter | |
{ | |
private: | |
ev_async * completed_; | |
public: | |
static void Init(Handle<Object> target) | |
{ | |
HandleScope scope; | |
Local<FunctionTemplate> t = FunctionTemplate::New(New); | |
t->Inherit(EventEmitter::constructor_template); | |
t->InstanceTemplate()->SetInternalFieldCount(1); | |
NODE_SET_PROTOTYPE_METHOD(t, "trigger", Trigger); | |
target->Set(String::NewSymbol("HelloWorld"), t->GetFunction()); | |
} | |
static void Complete(EV_P_ ev_async * w, int revents) | |
{ | |
HelloWorld * hw = static_cast<HelloWorld *>(w->data); | |
hw->Emit(String::New("ping"), 0, NULL); | |
} | |
HelloWorld() | |
{ | |
completed_ = new ev_async(); | |
} | |
~HelloWorld() | |
{ | |
ev_async_stop(EV_DEFAULT_UC_ completed_); | |
delete completed_; | |
} | |
private: | |
static Handle<Value> New(const Arguments& args) | |
{ | |
HandleScope scope; | |
HelloWorld * hw = new HelloWorld(); | |
hw->completed_->data = hw; | |
ev_async_init(hw->completed_, HelloWorld::Complete); | |
ev_async_start(EV_DEFAULT_UC_ hw->completed_); | |
ev_unref(EV_DEFAULT_UC); | |
hw->Wrap(args.This()); | |
return args.This(); | |
} | |
static Handle<Value> Trigger(const Arguments & args) | |
{ | |
HandleScope scope; | |
HelloWorld * hw = ObjectWrap::Unwrap<HelloWorld>(args.This()); | |
thread t(bind(& HelloWorld::threadFunc, hw)); | |
return Undefined(); | |
} | |
void threadFunc() | |
{ | |
HandleScope scope; | |
sleep(0); | |
ev_async_send(EV_DEFAULT_UC_ completed_); | |
} | |
}; | |
extern "C" { | |
static void init(Handle<Object> target) | |
{ | |
HandleScope scope; | |
HelloWorld::Init(target); | |
} | |
NODE_MODULE(helloworld, init); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var sys = require('sys'); | |
var hello = require('./build/default/helloworld'); | |
var hi = new hello.HelloWorld(); | |
hi.addListener('ping', function () { | |
sys.puts('pong'); | |
}); | |
hi.trigger(); | |
var http = require('http'); | |
http.createServer(function (req, res) { | |
res.end('Hello World\n'); | |
}).listen(8124, "127.0.0.1"); | |
sys.puts('Server running at http://127.0.0.1:8124/'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.cxxflags = ["-g", "-D_FILE_OFFSET_BITS=64", "-D_LARGEFILE_SOURCE", "-Wall"] | |
obj.target = "helloworld" | |
obj.source = "helloworld.cc" | |
obj.includes = ["../../boost/include/"] | |
obj.lib = ["boost_thread"] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment