Skip to content

Instantly share code, notes, and snippets.

@lukicdarkoo
Created July 29, 2016 11:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lukicdarkoo/0388d3b6a3e4c5187e8797dbcf6f60e8 to your computer and use it in GitHub Desktop.
Save lukicdarkoo/0388d3b6a3e4c5187e8797dbcf6f60e8 to your computer and use it in GitHub Desktop.
Node.js and C++ bindings with events (EventEmitter)
{
"targets": [
{
"target_name": "Robot",
"sources": [ "Robot.cc"],
"include_dirs": [
"<!(node -e \"require('nan')\")"
]
}
]
}
/*
Coded by Darko Lukic <lukicdarkoo@gmail.com>
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)
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