Skip to content

Instantly share code, notes, and snippets.

@benmonty
Created May 19, 2011 02:57
Show Gist options
  • Save benmonty/980104 to your computer and use it in GitHub Desktop.
Save benmonty/980104 to your computer and use it in GitHub Desktop.
simple node.js v8 example
> var p = require('./point');
> var o = new p.Point();
> o.setX(6);
> o.setY(9);
> console.log(o.get());
{ x: 6, y: 9 }
#include <node.h>
#include <v8.h>
#include "point.h"
using namespace v8;
using namespace node;
void Point::Initialize(Handle<Object> target){
HandleScope scope;
Local<FunctionTemplate> t = FunctionTemplate::New(New);
t->InstanceTemplate()->SetInternalFieldCount(1);
NODE_SET_PROTOTYPE_METHOD(t, "getX", Point::getX);
NODE_SET_PROTOTYPE_METHOD(t, "get", Point::get);
NODE_SET_PROTOTYPE_METHOD(t, "getY", Point::getY);
NODE_SET_PROTOTYPE_METHOD(t, "setX", Point::setX);
NODE_SET_PROTOTYPE_METHOD(t, "setY", Point::setY);
target->Set(String::NewSymbol("Point"), t->GetFunction());
}
Handle<Value> Point::getX(const Arguments &args){
HandleScope scope;
Point *p = ObjectWrap::Unwrap<Point>(args.This());
Local<Integer> result = Integer::New(p->x);
return scope.Close(result);
}
Handle<Value> Point::getY(const Arguments &args){
HandleScope scope;
Point *p = ObjectWrap::Unwrap<Point>(args.This());
Local<Integer> result = Integer::New(p->y);
return scope.Close(result);
}
Handle<Value> Point::setX(const Arguments &args){
HandleScope scope;
if(args.Length() != 1 || !args[0]->IsInt32()){
return ThrowException(Exception::TypeError(String::New("Argument must be an integer")));
}
Local<Integer> i = Local<Integer>::Cast(args[0]);
Point *p = ObjectWrap::Unwrap<Point>(args.This());
p->x = (int)(i->Int32Value());
return Undefined();
}
Handle<Value> Point::setY(const Arguments &args){
HandleScope scope;
if(args.Length() != 1 || !args[0]->IsInt32()){
return ThrowException(Exception::TypeError(String::New("Argument must be an integer")));
}
Local<Integer> i = Local<Integer>::Cast(args[0]);
Point *p = ObjectWrap::Unwrap<Point>(args.This());
p->y = (int)(i->Int32Value());
return Undefined();
}
Handle<Value> Point::get(const Arguments &args){
HandleScope scope;
Point *p = ObjectWrap::Unwrap<Point>(args.This());
Local<Object> result = Object::New();
result->Set(v8::String::New("x"), v8::Integer::New(p->x));
result->Set(v8::String::New("y"), v8::Integer::New(p->y));
return scope.Close(result);
}
Handle<Value> Point::New(const Arguments &args){
HandleScope scope;
Point *point = new Point();
point->Wrap(args.This());
return scope.Close(args.This());
}
extern "C" void init(Handle<Object> target){
HandleScope scope;
Point::Initialize(target);
};
#ifndef _POINT_H_
#define _POINT_H_
#include <v8.h>
#include <node.h>
class Point : node::ObjectWrap {
public:
static void Initialize(v8::Handle<v8::Object> target);
static v8::Handle<v8::Value> New(const v8::Arguments &args);
static v8::Handle<v8::Value> getX(const v8::Arguments &args);
static v8::Handle<v8::Value> get(const v8::Arguments &args);
static v8::Handle<v8::Value> getY(const v8::Arguments &args);
static v8::Handle<v8::Value> setX(const v8::Arguments &args);
static v8::Handle<v8::Value> setY(const v8::Arguments &args);
protected:
int x;
int y;
};
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment