Skip to content

Instantly share code, notes, and snippets.

@tmpvar
Created May 23, 2010 21:53
Show Gist options
  • Select an option

  • Save tmpvar/411282 to your computer and use it in GitHub Desktop.

Select an option

Save tmpvar/411282 to your computer and use it in GitHub Desktop.
#include <node.h>
#include <node_events.h>
#include <stdlib.h>
using namespace v8;
using namespace node;
class Parser : EventEmitter {
public:
static void
Initialize (v8::Handle<v8::Object> target)
{
HandleScope scope;
Local<FunctionTemplate> t = FunctionTemplate::New(New);
t->Inherit(EventEmitter::constructor_template);
t->InstanceTemplate()->SetInternalFieldCount(1);
NODE_SET_PROTOTYPE_METHOD(t, "parse", Parse);
target->Set(String::NewSymbol("Parser"), t->GetFunction());
}
protected:
Parser() : EventEmitter()
{
}
static Handle<Value>
Parse(const v8::Arguments& args) {
//HandleScope scope;
return String::New("foo");
}
static Handle<Value>
New (const Arguments& args)
{
HandleScope scope;
Parser *parser = new Parser();
parser->Wrap(args.This());
return args.This();
}
};
extern "C" void
init (Handle<Object> target)
{
HandleScope scope;
Parser::Initialize(target);
}
tmpvar@tmpvar:~/work/javascript/addon-test$ cat sanity.js && echo "=======" && node sanity.js
var test = require("./build/default/addon-test"), sys = require("sys");
sys.puts(sys.inspect(test));
=======
{ Parser: [Function] }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment