Skip to content

Commit

Permalink
added prototype example
Browse files Browse the repository at this point in the history
  • Loading branch information
bodokaiser committed Jun 8, 2013
1 parent 13e5a86 commit b28fa3c
Show file tree
Hide file tree
Showing 5 changed files with 157 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ This repository contains NodeJs C++ Addons for following JavaScript objects:
* [Array](http://github.com/bodokaiser/node-addons/blob/master/src/array.cc)
* [Buffer](http://github.com/bodokaiser/node-addons/blob/master/src/buffer.cc)
* [Function](http://github.com/bodokaiser/node-addons/blob/master/src/function.cc)
* [Prototype](http://github.com/bodokaiser/node-addons/blob/master/src/prototype.cc)

## License

Expand Down
5 changes: 5 additions & 0 deletions binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@
{
"target_name": "function",
"sources": ["src/function.cc"]
},
{
"target_name": "prototype",
"sources": ["src/prototype.cc"]
}

]
}
95 changes: 95 additions & 0 deletions src/prototype.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
#include "v8.h"
#include "node.h"
#include "prototype.h"

using namespace v8;

Prototype::Prototype() {};
Prototype::~Prototype() {};

void
Prototype::Initialize(Handle<Object> module) {
Local<FunctionTemplate> tpl = FunctionTemplate::New(New);

tpl->SetClassName(String::NewSymbol("Prototype"));

tpl->InstanceTemplate()
->SetInternalFieldCount(1);

tpl->PrototypeTemplate()
->Set(String::NewSymbol("get"),
FunctionTemplate::New(Get)->GetFunction());
tpl->PrototypeTemplate()
->Set(String::NewSymbol("set"),
FunctionTemplate::New(Set)->GetFunction());

Persistent<Function> constructor = Persistent<Function>::
New(tpl->GetFunction());

module->Set(String::NewSymbol("exports"), constructor);
}

Handle<Value>
Prototype::New(const Arguments &args) {
HandleScope scope;

Prototype * proto = new Prototype();

if (!args[0]->IsString()) {
ThrowException(Exception::TypeError(
String::New("Argument must be a String.")));

return scope.Close(Undefined());
}

proto->obj_ = Object::New();
proto->obj_->Set(String::NewSymbol("name"),
args[0]->ToString());

proto->Wrap(args.This());

return scope.Close(args.This());
}

Handle<Value>
Prototype::Get(const Arguments &args) {
HandleScope scope;

Prototype * proto = ObjectWrap::Unwrap<Prototype>(args.This());

if (!args[0]->IsString()) {
ThrowException(Exception::TypeError(
String::New("Argument must be a String.")));

return scope.Close(Undefined());
}

Local<String> str = args[0]->ToString();

return scope.Close(proto->obj_->Get(str));
}

Handle<Value>
Prototype::Set(const Arguments &args) {
HandleScope scope;

Prototype * proto = ObjectWrap::Unwrap<Prototype>(args.This());

if (!args[0]->IsString()) {
ThrowException(Exception::TypeError(
String::New("First argument must be a String.")));

return scope.Close(Undefined());
}

proto->obj_->Set(args[0]->ToString(), args[1]);

return scope.Close(args.This());
}

void
Initialize(Handle<Object> exports, Handle<Object> module) {
Prototype::Initialize(module);
}

NODE_MODULE(prototype, Initialize)
21 changes: 21 additions & 0 deletions src/prototype.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#ifndef PROTOTYPE_H
#define PROTOTYPE_H

#include "node.h"

class Prototype : public node::ObjectWrap {
public:
static void Initialize(v8::Handle<v8::Object> module);

private:
Prototype();
~Prototype();

static v8::Handle<v8::Value> New(const v8::Arguments &args);
static v8::Handle<v8::Value> Get(const v8::Arguments &args);
static v8::Handle<v8::Value> Set(const v8::Arguments &args);

v8::Handle<v8::Object> obj_;
};

#endif
35 changes: 35 additions & 0 deletions test/prototype.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
var chai = require('chai');

describe('Prototype', function() {

var Prototype = require('../build/Release/prototype');

it('should return an instance of Prototype', function() {
var proto = new Prototype('test');

chai.expect(proto).to.be.an.instanceof(Prototype);
});

describe('#get', function() {

it('should return "test"', function() {
var proto = new Prototype('test');

chai.expect(proto.get('name')).to.equal('test');
});

});

describe('#set', function() {

it('should set "hello" to "world"', function() {
var proto = new Prototype('test');

proto.set('hello', 'world');

chai.expect(proto.get('hello')).to.equal('world');
});

});

});

0 comments on commit b28fa3c

Please sign in to comment.