Skip to content

Commit

Permalink
Updates libspotify to use nan.
Browse files Browse the repository at this point in the history
  • Loading branch information
tcr committed Mar 16, 2015
1 parent a0722a3 commit fea64dc
Show file tree
Hide file tree
Showing 11 changed files with 410 additions and 536 deletions.
3 changes: 3 additions & 0 deletions binding.gyp
Expand Up @@ -14,6 +14,9 @@
"src/track.cc",
"src/playlist.cc"
],
"include_dirs" : [
"<!(node -e \"require('nan')\")"
],
"cflags": ["-Wall"],
"conditions" : [
[
Expand Down
7 changes: 4 additions & 3 deletions package.json
Expand Up @@ -29,10 +29,11 @@
"node": ">0.10.0"
},
"dependencies": {
"bindings": "~1.1.1",
"i": "~0.3",
"backoff": "~1.2",
"bindings": "~1.0.0",
"format": "0.1",
"backoff": "~2.0"
"i": "~0.3",
"nan": "^1.7.0"
},
"devDependencies": {
"nodeunit": "~0.7",
Expand Down
103 changes: 9 additions & 94 deletions src/album.cc
Expand Up @@ -25,8 +25,8 @@ using namespace nsp;
/**
* JS album_is_loaded implementation. checks if a given album is loaded
*/
static Handle<Value> Album_Is_Loaded(const Arguments& args) {
HandleScope scope;
NAN_METHOD(Album_Is_Loaded) {
NanScope();

// test arguments sanity
assert(args.Length() == 1);
Expand All @@ -38,14 +38,14 @@ static Handle<Value> Album_Is_Loaded(const Arguments& args) {
// actually call sp_album_is_loaded
bool loaded = sp_album_is_loaded(album->pointer);

return scope.Close(Boolean::New(loaded));
NanReturnValue(NanNew<Boolean>(loaded));
}

/**
* JS album_name implementation. checks if a given album is loaded
*/
static Handle<Value> Album_Name(const Arguments& args) {
HandleScope scope;
NAN_METHOD(Album_Name) {
NanScope();

// test arguments sanity
assert(args.Length() == 1);
Expand All @@ -56,66 +56,14 @@ static Handle<Value> Album_Name(const Arguments& args) {

const char* name = sp_album_name(album->pointer);

return scope.Close(String::New(name));
}

/**
* JS album_year implementation. checks if a given album is loaded
*/
static Handle<Value> Album_Year(const Arguments& args) {
HandleScope scope;

// test arguments sanity
assert(args.Length() == 1);
assert(args[0]->IsObject());

// gets sp_album pointer from given object
ObjectHandle<sp_album>* album = ObjectHandle<sp_album>::Unwrap(args[0]);

const int year = sp_album_year(album->pointer);

return scope.Close(Number::New(year));
}

/**
* JS album_type implementation. checks if a given album is loaded
*/
static Handle<Value> Album_Type(const Arguments& args) {
HandleScope scope;

// test arguments sanity
assert(args.Length() == 1);
assert(args[0]->IsObject());

// gets sp_album pointer from given object
ObjectHandle<sp_album>* album = ObjectHandle<sp_album>::Unwrap(args[0]);

sp_albumtype type = sp_album_type(album->pointer);
Handle<Value> res;
switch (type) {
case SP_ALBUMTYPE_ALBUM:
res = String::New("ALBUM");
break;
case SP_ALBUMTYPE_SINGLE:
res = String::New("SINGLE");
break;
case SP_ALBUMTYPE_COMPILATION:
res = String::New("COMPILATION");
break;
case SP_ALBUMTYPE_UNKNOWN:
default:
res = String::New("UNKNOWN");
break;
}

return scope.Close(res);
NanReturnValue(NanNew<String>(name));
}

/**
* JS album_artist implementation. checks if a given album is loaded
*/
static Handle<Value> Album_Artist(const Arguments& args) {
HandleScope scope;
NAN_METHOD(Album_Artist) {
NanScope();

// test arguments sanity
assert(args.Length() == 1);
Expand All @@ -128,39 +76,7 @@ static Handle<Value> Album_Artist(const Arguments& args) {
ObjectHandle<sp_artist>* artist = new ObjectHandle<sp_artist>("sp_artist");
artist->pointer = spartist;

return scope.Close(artist->object);
}

/**
* Callback for sp_image_add_load_callback in Album_Cover().
* It calls the passed JS function callback and passes the raw image data as
* parameter as soon as the regarding image loading process has finished.
*/
void cb_image_loaded_album(sp_image *image, void *userdata) {
Persistent<Function> callback = static_cast<Function*>(userdata);
size_t image_size;
const void *image_data = sp_image_data(image, &image_size);

// Create a C++ world slow buffer:
node::Buffer *slowBuffer= node::Buffer::New(image_size);
memcpy(node::Buffer::Data(slowBuffer), image_data, image_size);

// Get the Buffer constructor from the JavaScript world:
Local<Object> globalObj = Context::GetCurrent()->Global();
Local<Function> bufferConstructor = Local<Function>::Cast(globalObj->Get(String::New("Buffer")));
Handle<Value> constructorArgs[3] = { slowBuffer->handle_, Integer::New(image_size), Integer::New(0) };

// Create a JavaScript buffer using the slow buffer:
Local<Object> actualBuffer = bufferConstructor->NewInstance(3, constructorArgs);

// Pass everything to the JavaScript callback:
const unsigned argc = 1;
Local<Value> argv[argc] = { actualBuffer };
callback->Call(callback, argc, argv);

// Clean up:
callback.Dispose();
sp_image_release(image);
NanReturnValue(artist->object);
}

/**
Expand Down Expand Up @@ -205,4 +121,3 @@ void nsp::init_album(Handle<Object> target) {
target->Set(v8::String::NewSymbol("SP_IMAGE_SIZE_NORMAL"), v8::Int32::New(static_cast<int>(SP_IMAGE_SIZE_NORMAL)), ReadOnly);
target->Set(v8::String::NewSymbol("SP_IMAGE_SIZE_LARGE"), v8::Int32::New(static_cast<int>(SP_IMAGE_SIZE_LARGE)), ReadOnly);
}

12 changes: 6 additions & 6 deletions src/artist.cc
Expand Up @@ -25,8 +25,8 @@ using namespace nsp;
/**
* JS artist_is_loaded implementation. checks if a given artist is loaded
*/
static Handle<Value> Artist_Is_Loaded(const Arguments& args) {
HandleScope scope;
NAN_METHOD(Artist_Is_Loaded) {
NanScope();

// test arguments sanity
assert(args.Length() == 1);
Expand All @@ -38,14 +38,14 @@ static Handle<Value> Artist_Is_Loaded(const Arguments& args) {
// actually call sp_artist_is_loaded
bool loaded = sp_artist_is_loaded(artist->pointer);

return scope.Close(Boolean::New(loaded));
NanReturnValue(NanNew<Boolean>(loaded));
}

/**
* JS artist_name implementation. checks if a given artist is loaded
*/
static Handle<Value> Artist_Name(const Arguments& args) {
HandleScope scope;
NAN_METHOD(Artist_Name) {
NanScope();

// test arguments sanity
assert(args.Length() == 1);
Expand All @@ -56,7 +56,7 @@ static Handle<Value> Artist_Name(const Arguments& args) {

const char* name = sp_artist_name(artist->pointer);

return scope.Close(String::New(name));
NanReturnValue(NanNew<String>(name));
}

void nsp::init_artist(Handle<Object> target) {
Expand Down
14 changes: 3 additions & 11 deletions src/binding.cc
Expand Up @@ -16,15 +16,12 @@
* =====================================================================================
*/

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

extern "C" {
void init (v8::Handle<v8::Object> target)
{
v8::HandleScope scope;

void InitAll(Handle<Object> target) {
// initializing all modules
nsp::init_album(target);
nsp::init_artist(target);
Expand All @@ -33,11 +30,6 @@ extern "C" {
nsp::init_search(target);
nsp::init_session(target);
nsp::init_track(target);
nsp::init_playlistcontainer(target);
nsp::init_playlist(target);

}
}


NODE_MODULE(spotify, init);
NODE_MODULE(spotify, InitAll);

0 comments on commit fea64dc

Please sign in to comment.