Created
July 29, 2016 11:59
-
-
Save lukicdarkoo/0388d3b6a3e4c5187e8797dbcf6f60e8 to your computer and use it in GitHub Desktop.
Node.js and C++ bindings with events (EventEmitter)
This file contains 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
{ | |
"targets": [ | |
{ | |
"target_name": "Robot", | |
"sources": [ "Robot.cc"], | |
"include_dirs": [ | |
"<!(node -e \"require('nan')\")" | |
] | |
} | |
] | |
} |
This file contains 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
/* | |
Coded by Darko Lukic <[email protected]> | |
Node.js and C++ bindings with events (EventEmitter) | |
*/ | |
#include <nan.h> | |
#include <iostream> | |
using v8::Local; | |
using v8::FunctionTemplate; | |
using v8::Object; | |
using v8::Value; | |
using v8::Handle; | |
using v8::Isolate; | |
using v8::Exception; | |
using v8::Persistent; | |
using v8::Context; | |
using namespace std; | |
class Robot : public Nan::ObjectWrap { | |
public: | |
static void Init(Local<Object> exports) { | |
Nan::HandleScope scope; | |
Local<FunctionTemplate> tmpl = Nan::New<FunctionTemplate>(New); | |
tmpl->InstanceTemplate()->SetInternalFieldCount(1); | |
Nan::SetPrototypeMethod(tmpl, "getX", getX); | |
Nan::SetPrototypeMethod(tmpl, "setX", setX); | |
exports->Set(Nan::New("Robot").ToLocalChecked(), tmpl->GetFunction()); | |
} | |
static void New(const Nan::FunctionCallbackInfo<Value>& args) { | |
Nan::HandleScope scope; | |
Robot *robot = new Robot(); | |
robot->Wrap(args.This()); | |
args.GetReturnValue().Set(args.This()); | |
} | |
Robot() : x(0) {} | |
Persistent<Object> obj; | |
private: | |
static void getX(const Nan::FunctionCallbackInfo<Value>& args) { | |
Nan::HandleScope scope; | |
Robot *robot = ObjectWrap::Unwrap<Robot>(args.Holder()); | |
args.GetReturnValue().Set(robot->x++); | |
} | |
static void setX(const Nan::FunctionCallbackInfo<Value>& args) { | |
Nan::HandleScope scope; | |
Robot *robot = ObjectWrap::Unwrap<Robot>(args.Holder()); | |
// Set x | |
if (args.Length() != 1 || args[0]->IsInt32() == false) { | |
args.GetIsolate()->ThrowException(Exception::TypeError( | |
Nan::New("Function requires one argument").ToLocalChecked() | |
)); | |
return; | |
} | |
else { | |
robot->x = args[0]->Int32Value(); | |
} | |
(robot->obj).Reset(args.GetIsolate(), args.Holder()); | |
// Fire event | |
Handle<Value> argv[] = { | |
Nan::New("positionChanged").ToLocalChecked(), | |
Nan::New(robot->x) | |
}; | |
Nan::MakeCallback(Nan::New(robot->obj), "emit", 2, argv); | |
} | |
int x; | |
}; | |
NODE_MODULE(Robot, Robot::Init) |
This file contains 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
const Robot = require('bindings')('Robot').Robot; | |
const EventEmiter = require('events'); | |
const Util = require('util'); | |
Util.inherits(Robot, EventEmiter); | |
var robot = new Robot(); | |
robot.on('positionChanged', function(e) { console.log('positionChanged() ' + e); }); | |
robot.setX(3); | |
console.log(robot.getX()); | |
var robot2 = new Robot(); | |
console.log(robot2.getX()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment