Skip to content

Commit

Permalink
Merge pull request #3 from Medium/nick-nan
Browse files Browse the repository at this point in the history
Use nan to make oid compatible with node 0.12
  • Loading branch information
nicks committed Feb 12, 2015
2 parents 8a94b6a + e9a0263 commit 4e1c5a9
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 27 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
build
oidNative.node
node_modules
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
language: node_js
node_js:
- "0.12"
- "0.10"
- "0.8"

2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
oid
===

[![Build Status](https://travis-ci.org/Medium/oid.svg)](https://travis-ci.org/Medium/oid)

This Node module provides a simple utility for object identity hashing
and two related classes. This can be useful any time you need to do
triple-equals (`===`) style comparisons across arbitrary numbers of
Expand Down
1 change: 1 addition & 0 deletions binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
{
'target_name': 'oidNative',
'sources': [ 'src/oidNative.cc' ],
'include_dirs': ["<!(node -e \"require('nan')\")"]
}
]
}
11 changes: 7 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
"name": "oid",
"version": "0.5.1",
"version": "1.0.0",
"keywords":
["object", "id", "identity", "hash", "hashcode", "objid", "oid"],
"description":
"Utilities for object identity and hashing",
"homepage": "https://github.com/Obvious/oid",
"homepage": "https://github.com/Medium/oid",
"repository": {
"type": "git",
"url": "https://github.com/Obvious/oid.git"
"url": "https://github.com/Medium/oid.git"
},
"licenses": [ {
"type": "Apache 2.0",
Expand All @@ -27,7 +27,10 @@

"main": "lib/oid.js",
"engine": {
"node": ">=0.6.0"
"node": ">=0.8.0"
},
"dependencies": {
"nan": "1.6.2"
},

"scripts": {
Expand Down
37 changes: 14 additions & 23 deletions src/oidNative.cc
Original file line number Diff line number Diff line change
@@ -1,31 +1,22 @@
// Copyright 2012 The Obvious Corporation.

#include <node.h>
#include <nan.h>
#include <v8.h>

using namespace v8;

/**
* Helper to schedule an exception with the given message and return
* undefined.
*/
static Handle<Value> scheduleException(const char* message) {
Local<Value> exception = Exception::Error(String::New(message));
ThrowException(exception);
return Undefined();
}

Handle<Value> ObjectIdHash(const Arguments& args) {
HandleScope scope;
NAN_METHOD(ObjectIdHash) {
NanScope();

Local<Value> val = args[0];
if (val->IsNull()) {
return scope.Close(Integer::New(99961)); // A prime number.
NanReturnValue(NanNew<Integer>(99961)); // A prime number.
}

Local<Object> obj = val->ToObject();
if (obj.IsEmpty()) {
return scheduleException("Not an object.");
return NanThrowError("Not an object.");
}

int hash = obj->GetIdentityHash() & 0x7fffffff;
Expand All @@ -38,15 +29,15 @@ Handle<Value> ObjectIdHash(const Arguments& args) {
hash = 1;
}

return scope.Close(Integer::New(hash));
NanReturnValue(NanNew<Integer>(hash));
}

Handle<Value> NumberIdHash(const Arguments& args) {
HandleScope scope;
NAN_METHOD(NumberIdHash) {
NanScope();

Local<Number> num = args[0]->ToNumber();
if (num.IsEmpty()) {
return scheduleException("Not a number.");
return NanThrowError("Not a number.");
}

union {
Expand All @@ -68,14 +59,14 @@ Handle<Value> NumberIdHash(const Arguments& args) {
hash = 1;
}

return scope.Close(Number::New((double) hash));
NanReturnValue(NanNew<Number>((double) hash));
}

void init(Handle<Object> target) {
target->Set(String::NewSymbol("objectIdHash"),
FunctionTemplate::New(ObjectIdHash)->GetFunction());
target->Set(String::NewSymbol("numberIdHash"),
FunctionTemplate::New(NumberIdHash)->GetFunction());
target->Set(NanNew<String>("objectIdHash"),
NanNew<FunctionTemplate>(ObjectIdHash)->GetFunction());
target->Set(NanNew<String>("numberIdHash"),
NanNew<FunctionTemplate>(NumberIdHash)->GetFunction());
}

NODE_MODULE(oidNative, init)

0 comments on commit 4e1c5a9

Please sign in to comment.