Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

C++ EventEmitter example.

  • Loading branch information...
commit ed27fd275b4e4c9b3d5f10e751142072f50f371d 0 parents
@bnoordhuis authored
2  .gitignore
@@ -0,0 +1,2 @@
+.lock-wscript
+build/
1  README.md
@@ -0,0 +1 @@
+Example add-on that demonstrates how to emit events from C++ land.
77 event-emitter.cc
@@ -0,0 +1,77 @@
+/*
+ * Copyright (c) 2011, Ben Noordhuis <info@bnoordhuis.nl>
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <v8.h>
+#include <node.h>
+
+using namespace v8;
+using namespace node;
+
+namespace {
+
+struct Emitter: ObjectWrap {
+ static Handle<Value> New(const Arguments& args);
+ static Handle<Value> Ping(const Arguments& args);
+};
+
+
+Handle<Value> Emitter::New(const Arguments& args) {
+ HandleScope scope;
+
+ assert(args.IsConstructCall());
+ Emitter* self = new Emitter();
+ self->Wrap(args.This());
+
+ return scope.Close(args.This());
+}
+
+
+// emits ping event
+Handle<Value> Emitter::Ping(const Arguments& args) {
+ HandleScope scope;
+
+ Local<Value> emit_v = args.This()->Get(String::NewSymbol("emit"));
+ assert(emit_v->IsFunction());
+ Local<Function> emit_f = emit_v.As<Function>();
+
+ Handle<Value> argv[2] = {
+ String::New("ping"), // event name
+ args[0]->ToString() // argument
+ };
+
+ TryCatch tc;
+
+ emit_f->Call(args.This(), 2, argv);
+
+ if (tc.HasCaught())
+ FatalException(tc);
+
+ return Undefined();
+}
+
+
+extern "C" void init(Handle<Object> target) {
+ HandleScope scope;
+
+ Local<FunctionTemplate> t = FunctionTemplate::New(Emitter::New);
+ t->InstanceTemplate()->SetInternalFieldCount(1);
+ t->SetClassName(String::New("Emitter"));
+ NODE_SET_PROTOTYPE_METHOD(t, "ping", Emitter::Ping);
+
+ target->Set(String::NewSymbol("Emitter"), t->GetFunction());
+}
+
+} // anonymous namespace
28 event-emitter.js
@@ -0,0 +1,28 @@
+/*
+ * Copyright (c) 2011, Ben Noordhuis <info@bnoordhuis.nl>
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+// javascript shim that lets our object inherit from EventEmitter
+var Emitter = require(__dirname + '/build/Release/event-emitter.node').Emitter;
+var events = require('events');
+
+inherits(Emitter, events.EventEmitter);
+exports.Emitter = Emitter;
+
+// extend prototype
+function inherits(target, source) {
+ for (var k in source.prototype)
+ target.prototype[k] = source.prototype[k];
+}
26 test.js
@@ -0,0 +1,26 @@
+/*
+ * Copyright (c) 2011, Ben Noordhuis <info@bnoordhuis.nl>
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+var Emitter = require('./event-emitter').Emitter;
+
+var e = new Emitter();
+
+e.on('ping', function(s) {
+ console.error('You say ping, I say %s.', s);
+});
+
+// emits a 'ping' event from c++ land
+e.ping('pong');
11 wscript
@@ -0,0 +1,11 @@
+def set_options(ctx):
+ ctx.tool_options('compiler_cxx')
+
+def configure(ctx):
+ ctx.check_tool('compiler_cxx')
+ ctx.check_tool('node_addon')
+
+def build(ctx):
+ t = ctx.new_task_gen('cxx', 'shlib', 'node_addon')
+ t.target = 'event-emitter'
+ t.source = 'event-emitter.cc'
Please sign in to comment.
Something went wrong with that request. Please try again.