Skip to content

Commit ed27fd2

Browse files
committed
C++ EventEmitter example.
0 parents  commit ed27fd2

File tree

6 files changed

+145
-0
lines changed

6 files changed

+145
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.lock-wscript
2+
build/

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Example add-on that demonstrates how to emit events from C++ land.

event-emitter.cc

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* Copyright (c) 2011, Ben Noordhuis <info@bnoordhuis.nl>
3+
*
4+
* Permission to use, copy, modify, and/or distribute this software for any
5+
* purpose with or without fee is hereby granted, provided that the above
6+
* copyright notice and this permission notice appear in all copies.
7+
*
8+
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9+
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10+
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11+
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12+
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13+
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14+
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15+
*/
16+
17+
#include <v8.h>
18+
#include <node.h>
19+
20+
using namespace v8;
21+
using namespace node;
22+
23+
namespace {
24+
25+
struct Emitter: ObjectWrap {
26+
static Handle<Value> New(const Arguments& args);
27+
static Handle<Value> Ping(const Arguments& args);
28+
};
29+
30+
31+
Handle<Value> Emitter::New(const Arguments& args) {
32+
HandleScope scope;
33+
34+
assert(args.IsConstructCall());
35+
Emitter* self = new Emitter();
36+
self->Wrap(args.This());
37+
38+
return scope.Close(args.This());
39+
}
40+
41+
42+
// emits ping event
43+
Handle<Value> Emitter::Ping(const Arguments& args) {
44+
HandleScope scope;
45+
46+
Local<Value> emit_v = args.This()->Get(String::NewSymbol("emit"));
47+
assert(emit_v->IsFunction());
48+
Local<Function> emit_f = emit_v.As<Function>();
49+
50+
Handle<Value> argv[2] = {
51+
String::New("ping"), // event name
52+
args[0]->ToString() // argument
53+
};
54+
55+
TryCatch tc;
56+
57+
emit_f->Call(args.This(), 2, argv);
58+
59+
if (tc.HasCaught())
60+
FatalException(tc);
61+
62+
return Undefined();
63+
}
64+
65+
66+
extern "C" void init(Handle<Object> target) {
67+
HandleScope scope;
68+
69+
Local<FunctionTemplate> t = FunctionTemplate::New(Emitter::New);
70+
t->InstanceTemplate()->SetInternalFieldCount(1);
71+
t->SetClassName(String::New("Emitter"));
72+
NODE_SET_PROTOTYPE_METHOD(t, "ping", Emitter::Ping);
73+
74+
target->Set(String::NewSymbol("Emitter"), t->GetFunction());
75+
}
76+
77+
} // anonymous namespace

event-emitter.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright (c) 2011, Ben Noordhuis <info@bnoordhuis.nl>
3+
*
4+
* Permission to use, copy, modify, and/or distribute this software for any
5+
* purpose with or without fee is hereby granted, provided that the above
6+
* copyright notice and this permission notice appear in all copies.
7+
*
8+
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9+
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10+
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11+
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12+
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13+
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14+
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15+
*/
16+
17+
// javascript shim that lets our object inherit from EventEmitter
18+
var Emitter = require(__dirname + '/build/Release/event-emitter.node').Emitter;
19+
var events = require('events');
20+
21+
inherits(Emitter, events.EventEmitter);
22+
exports.Emitter = Emitter;
23+
24+
// extend prototype
25+
function inherits(target, source) {
26+
for (var k in source.prototype)
27+
target.prototype[k] = source.prototype[k];
28+
}

test.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright (c) 2011, Ben Noordhuis <info@bnoordhuis.nl>
3+
*
4+
* Permission to use, copy, modify, and/or distribute this software for any
5+
* purpose with or without fee is hereby granted, provided that the above
6+
* copyright notice and this permission notice appear in all copies.
7+
*
8+
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9+
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10+
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11+
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12+
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13+
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14+
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15+
*/
16+
17+
var Emitter = require('./event-emitter').Emitter;
18+
19+
var e = new Emitter();
20+
21+
e.on('ping', function(s) {
22+
console.error('You say ping, I say %s.', s);
23+
});
24+
25+
// emits a 'ping' event from c++ land
26+
e.ping('pong');

wscript

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
def set_options(ctx):
2+
ctx.tool_options('compiler_cxx')
3+
4+
def configure(ctx):
5+
ctx.check_tool('compiler_cxx')
6+
ctx.check_tool('node_addon')
7+
8+
def build(ctx):
9+
t = ctx.new_task_gen('cxx', 'shlib', 'node_addon')
10+
t.target = 'event-emitter'
11+
t.source = 'event-emitter.cc'

0 commit comments

Comments
 (0)