Skip to content

Commit 2d09006

Browse files
committed
Merge pull request #6 from joshuawarner32/master
lib: integrate nan for broader node version support
2 parents 8f02af6 + 200f702 commit 2d09006

File tree

5 files changed

+52
-38
lines changed

5 files changed

+52
-38
lines changed

binding.gyp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22
"targets": [
33
{
44
"target_name": "volume",
5-
"sources": [ "src/volume.cc" ]
5+
"sources": [ "src/volume.cc" ],
6+
"include_dirs" : [
7+
"<!(node -e \"require('nan')\")"
8+
]
69
}
710
]
811
}

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
"author": "Linus Unnebäck <[email protected]>",
77
"contributors": [
88
"Linus Unnebäck <[email protected]>",
9-
"Davide Liessi <[email protected]>"
9+
"Davide Liessi <[email protected]>",
10+
"Joshua Warner <[email protected]>"
1011
],
1112
"devDependencies": {
1213
"fs-temp": "^0.1.0",
@@ -18,5 +19,8 @@
1819
"repository": {
1920
"type": "git",
2021
"url": "https://github.com/LinusU/node-alias.git"
22+
},
23+
"dependencies": {
24+
"nan": "^1.4.1"
2125
}
2226
}

src/impl-apple-cheetah.cc

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,31 @@
33
#include <CoreFoundation/CFURL.h>
44
#include <CoreFoundation/CFString.h>
55

6-
using namespace v8;
6+
using v8::String;
7+
using v8::Local;
78

8-
Local<String> OSErrDescription(OSErr err) {
9+
const char* OSErrDescription(OSErr err) {
910

1011
switch (err) {
11-
case nsvErr: return String::New("Volume not found");
12-
case ioErr: return String::New("I/O error.");
13-
case bdNamErr: return String::New("Bad filename or volume name.");
14-
case mFulErr: return String::New("Memory full (open) or file won't fit (load)");
15-
case tmfoErr: return String::New("Too many files open.");
16-
case fnfErr: return String::New("File or directory not found; incomplete pathname.");
17-
case volOffLinErr: return String::New("Volume is offline.");
18-
case nsDrvErr: return String::New("No such drive.");
19-
case dirNFErr: return String::New("Directory not found or incomplete pathname.");
20-
case tmwdoErr: return String::New("Too many working directories open.");
12+
case nsvErr: return "Volume not found";
13+
case ioErr: return "I/O error.";
14+
case bdNamErr: return "Bad filename or volume name.";
15+
case mFulErr: return "Memory full (open) or file won't fit (load)";
16+
case tmfoErr: return "Too many files open.";
17+
case fnfErr: return "File or directory not found; incomplete pathname.";
18+
case volOffLinErr: return "Volume is offline.";
19+
case nsDrvErr: return "No such drive.";
20+
case dirNFErr: return "Directory not found or incomplete pathname.";
21+
case tmwdoErr: return "Too many working directories open.";
2122
}
2223

23-
return String::New("Could not get volume name");
24+
return "Could not get volume name";
2425
}
2526

26-
v8::Handle<Value> MethodGetVolumeName(const Arguments& args) {
27-
HandleScope scope;
27+
NAN_METHOD(MethodGetVolumeName) {
28+
NanScope();
2829

29-
String::AsciiValue aPath(args[0]);
30+
NanAsciiString aPath(args[0]);
3031

3132
CFStringRef volumePath = CFStringCreateWithCString(NULL, *aPath, kCFStringEncodingUTF8);
3233
CFURLRef url = CFURLCreateWithFileSystemPath(NULL, volumePath, kCFURLPOSIXPathStyle, true);
@@ -38,19 +39,19 @@ v8::Handle<Value> MethodGetVolumeName(const Arguments& args) {
3839
Local<String> errorDesc;
3940

4041
if (CFURLGetFSRef(url, &urlFS) == false) {
41-
ThrowException(Exception::Error(String::New("Failed to convert URL to file or directory object"))->ToObject());
42-
return scope.Close(Undefined());
42+
NanThrowError("Failed to convert URL to file or directory object");
43+
NanReturnUndefined();
4344
}
4445

4546
if ((err = FSGetCatalogInfo(&urlFS, kFSCatInfoVolume, &urlInfo, NULL, NULL, NULL)) != noErr) {
46-
ThrowException(Exception::Error(OSErrDescription(err))->ToObject());
47-
return scope.Close(Undefined());
47+
NanThrowError(OSErrDescription(err));
48+
NanReturnUndefined();
4849
}
4950

5051
if ((err = FSGetVolumeInfo(urlInfo.volume, 0, NULL, kFSVolInfoNone, NULL, &outString, NULL)) != noErr) {
51-
ThrowException(Exception::Error(OSErrDescription(err))->ToObject());
52-
return scope.Close(Undefined());
52+
NanThrowError(OSErrDescription(err));
53+
NanReturnUndefined();
5354
}
5455

55-
return scope.Close(String::New(outString.unicode, outString.length));
56+
NanReturnValue(NanNew(outString.unicode, outString.length));
5657
}

src/impl-apple-lion.cc

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@
22
#include <CoreFoundation/CFURL.h>
33
#include <CoreFoundation/CFString.h>
44

5-
using namespace v8;
5+
using v8::String;
6+
using v8::Exception;
7+
using v8::Local;
8+
using v8::Value;
69

710
Local<String> MYCFStringGetV8String(CFStringRef aString) {
811

912
if (aString == NULL) {
10-
return String::New("");
13+
return NanNew("");
1114
}
1215

1316
CFIndex length = CFStringGetLength(aString);
@@ -16,19 +19,19 @@ Local<String> MYCFStringGetV8String(CFStringRef aString) {
1619

1720
if (CFStringGetCString(aString, buffer, maxSize, kCFStringEncodingUTF8)) {
1821

19-
Local<String> result = String::New(buffer);
22+
Local<String> result = NanNew(buffer);
2023
free(buffer);
2124

2225
return result;
2326
}
2427

25-
return String::New("");
28+
return NanNew("");
2629
}
2730

28-
Handle<Value> MethodGetVolumeName(const Arguments& args) {
29-
HandleScope scope;
31+
NAN_METHOD(MethodGetVolumeName) {
32+
NanScope();
3033

31-
String::AsciiValue aPath(args[0]);
34+
NanAsciiString aPath(args[0]);
3235

3336
CFStringRef out;
3437
CFErrorRef error;
@@ -40,12 +43,12 @@ Handle<Value> MethodGetVolumeName(const Arguments& args) {
4043

4144
Local<String> result = MYCFStringGetV8String(out);
4245

43-
return scope.Close(result);
46+
NanReturnValue(result);
4447
} else {
4548

4649
Local<String> desc = MYCFStringGetV8String(CFErrorCopyDescription(error));
47-
ThrowException(Exception::Error(desc)->ToObject());
50+
NanThrowError(Exception::Error(desc)->ToObject());
4851

49-
return scope.Close(Undefined());
52+
NanReturnUndefined();
5053
}
5154
}

src/volume.cc

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11

2+
#include <nan.h>
23
#include <node.h>
34
#include <v8.h>
45

@@ -8,8 +9,10 @@
89
#error This platform is not implemented yet
910
#endif
1011

11-
void init(v8::Handle<v8::Object> exports) {
12-
exports->Set(v8::String::NewSymbol("getVolumeName"), v8::FunctionTemplate::New(MethodGetVolumeName)->GetFunction());
12+
using v8::FunctionTemplate;
13+
14+
void Initialize(v8::Handle<v8::Object> exports) {
15+
exports->Set(NanNew("getVolumeName"), NanNew<FunctionTemplate>(MethodGetVolumeName)->GetFunction());
1316
}
1417

15-
NODE_MODULE(volume, init)
18+
NODE_MODULE(volume, Initialize)

0 commit comments

Comments
 (0)