-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
13e5a86
commit b28fa3c
Showing
5 changed files
with
157 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); | ||
|
||
}); | ||
|
||
}); |