Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

N-API #16

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,20 @@
"target_name": "volume",
"sources": [ "src/volume.cc" ],
"include_dirs" : [
"<!(node -e \"require('nan')\")"
]
"<!@(node -p \"require('node-addon-api').include\")"
],
"dependencies": [
"<!(node -p \"require('node-addon-api').gyp\")"
],
"cflags!": [ "-fno-exceptions" ],
"cflags+": [ "-fvisibility=hidden" ],
"cflags_cc!": [ "-fno-exceptions" ],
"xcode_settings": {
"GCC_ENABLE_CPP_EXCEPTIONS": "YES",
"CLANG_CXX_LIBRARY": "libc++",
"MACOSX_DEPLOYMENT_TARGET": "10.7",
"GCC_SYMBOLS_PRIVATE_EXTERN": "YES"
}
}
]
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@
"url": "https://github.com/LinusU/node-alias.git"
},
"dependencies": {
"nan": "^2.4.0"
"node-addon-api": "^1.6.3"
}
}
17 changes: 10 additions & 7 deletions src/impl-apple-cheetah.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@

#define NAPI_VERSION 3
#include <napi.h>
#include <Carbon/Carbon.h>
#include <CoreFoundation/CFURL.h>
#include <CoreFoundation/CFString.h>
Expand All @@ -20,10 +22,11 @@ const char* OSErrDescription(OSErr err) {
return "Could not get volume name";
}

NAN_METHOD(MethodGetVolumeName) {
Nan::Utf8String aPath(info[0]);
Napi::Value MethodGetVolumeName(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
std::string aPath = info[0].As<Napi::String>();

CFStringRef volumePath = CFStringCreateWithCString(NULL, *aPath, kCFStringEncodingUTF8);
CFStringRef volumePath = CFStringCreateWithCString(NULL, aPath.c_str(), kCFStringEncodingUTF8);
CFURLRef url = CFURLCreateWithFileSystemPath(NULL, volumePath, kCFURLPOSIXPathStyle, true);

OSErr err;
Expand All @@ -32,16 +35,16 @@ NAN_METHOD(MethodGetVolumeName) {
HFSUniStr255 outString;

if (CFURLGetFSRef(url, &urlFS) == false) {
return Nan::ThrowError("Failed to convert URL to file or directory object");
throw Napi::Error::New(env, "Failed to convert URL to file or directory object");
}

if ((err = FSGetCatalogInfo(&urlFS, kFSCatInfoVolume, &urlInfo, NULL, NULL, NULL)) != noErr) {
return Nan::ThrowError(OSErrDescription(err));
throw Napi::Error::New(env, OSErrDescription(err));
}

if ((err = FSGetVolumeInfo(urlInfo.volume, 0, NULL, kFSVolInfoNone, NULL, &outString, NULL)) != noErr) {
return Nan::ThrowError(OSErrDescription(err));
throw Napi::Error::New(env, OSErrDescription(err));
}

info.GetReturnValue().Set(Nan::New<v8::String>(outString.unicode, outString.length).ToLocalChecked());
return Napi::String::New(env, reinterpret_cast<char16_t *>(outString.unicode), outString.length);
}
26 changes: 12 additions & 14 deletions src/impl-apple-lion.cc
Original file line number Diff line number Diff line change
@@ -1,43 +1,41 @@

#define NAPI_VERSION 3
#include <napi.h>
#include <CoreFoundation/CFURL.h>
#include <CoreFoundation/CFString.h>

using v8::String;
using v8::Exception;
using v8::Local;
using v8::Value;

Local<String> MYCFStringGetV8String(CFStringRef aString) {
Napi::String MYCFStringGetV8String(Napi::Env env, CFStringRef aString) {
if (aString == NULL) {
return Nan::EmptyString();
return Napi::String::New(env, "");
}

CFIndex length = CFStringGetLength(aString);
CFIndex maxSize = CFStringGetMaximumSizeForEncoding(length, kCFStringEncodingUTF8);
char *buffer = (char *) malloc(maxSize);

if (!CFStringGetCString(aString, buffer, maxSize, kCFStringEncodingUTF8)) {
return Nan::EmptyString();
return Napi::String::New(env, "");
}

Local<String> result = Nan::New(buffer).ToLocalChecked();
Napi::String result = Napi::String::New(env, buffer);
free(buffer);

return result;
}

NAN_METHOD(MethodGetVolumeName) {
Nan::Utf8String aPath(info[0]);
Napi::Value MethodGetVolumeName(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
std::string aPath = info[0].As<Napi::String>();

CFStringRef out;
CFErrorRef error;

CFStringRef volumePath = CFStringCreateWithCString(NULL, *aPath, kCFStringEncodingUTF8);
CFStringRef volumePath = CFStringCreateWithCString(NULL, aPath.c_str(), kCFStringEncodingUTF8);
CFURLRef url = CFURLCreateWithFileSystemPath(NULL, volumePath, kCFURLPOSIXPathStyle, true);

if(!CFURLCopyResourcePropertyForKey(url, kCFURLVolumeNameKey, &out, &error)) {
return Nan::ThrowError(MYCFStringGetV8String(CFErrorCopyDescription(error)));
throw Napi::Error::New(env, MYCFStringGetV8String(env, CFErrorCopyDescription(error)));
}

info.GetReturnValue().Set(MYCFStringGetV8String(out));
return MYCFStringGetV8String(env, out);
}
14 changes: 4 additions & 10 deletions src/volume.cc
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@

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

#ifdef __APPLE__
#include "impl-apple.cc"
#else
#error This platform is not implemented yet
#endif

using v8::FunctionTemplate;

NAN_MODULE_INIT(Initialize) {
Nan::Set(target, Nan::New("getVolumeName").ToLocalChecked(),
Nan::GetFunction(Nan::New<FunctionTemplate>(MethodGetVolumeName)).ToLocalChecked());
Napi::Object Initialize(Napi::Env env, Napi::Object exports) {
exports["getVolumeName"] = Napi::Function::New(env, MethodGetVolumeName, "getVolumeName");
return exports;
}

NODE_MODULE(volume, Initialize)
NODE_API_MODULE(volume, Initialize)