Skip to content

Instantly share code, notes, and snippets.

@bmizerany
Created May 24, 2010 02:13
Show Gist options
  • Save bmizerany/411455 to your computer and use it in GitHub Desktop.
Save bmizerany/411455 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;
#define _PROTO_DATA_SYMBOL String::NewSymbol("data")
class RequestParser : 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("RequestParser"), t->GetFunction());
}
protected:
RequestParser() : EventEmitter() { }
static Handle<Value>
New (const Arguments& args)
{
HandleScope scope;
args.This()->Set(
_PROTO_DATA_SYMBOL,
String::Empty()
);
RequestParser *parser = new RequestParser();
parser->Wrap(args.This());
return args.This();
}
static Handle<Value>
Parse(const Arguments& args) {
RequestParser *parser = ObjectWrap::Unwrap<RequestParser>(args.This());
HandleScope scope;
if (args.Length() == 0 || !args[0]->IsString()) {
return ThrowException(
Exception::Error(String::New("Must give data string as argument"))
);
}
Handle<Value> data = args.This()->Get(_PROTO_DATA_SYMBOL);
Local<String> temp = String::Concat(data, String::New("foobar"));
args.This()->Set(_PROTO_DATA_SYMBOL, temp);
return Undefined();
}
};
extern "C" void
init (Handle<Object> target)
{
HandleScope scope;
RequestParser::Initialize(target);
}
/*
Checking for program g++ or c++ : /usr/bin/g++
Checking for program cpp : /usr/bin/cpp
Checking for program ar : /usr/bin/ar
Checking for program ranlib : /usr/bin/ranlib
Checking for g++ : ok
Checking for node prefix : ok /usr/local
'configure' finished successfully (0.091s)
Waf: Entering directory `/Users/blake/code/borg/build'
[1/2] cxx: proto.cc -> build/default/proto_1.o
../proto.cc:77:1: error: unterminated comment
/usr/local/include/node/v8.h: In constructor ‘v8::Handle<T>::Handle(v8::Handle<S>) [with S = v8::Value, T = v8::String]’:
../proto.cc:62: instantiated from here
/usr/local/include/node/v8.h:216: error: invalid conversion from ‘v8::Value*’ to ‘v8::String*’
Waf: Leaving directory `/Users/blake/code/borg/build'
Build failed: -> task failed (err #1):
{task: cxx proto.cc -> proto_1.o}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment