-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathcommon.h
More file actions
243 lines (217 loc) · 7.93 KB
/
common.h
File metadata and controls
243 lines (217 loc) · 7.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/*
* =====================================================================================
*
* Filename: common.h
*
* Description: Common utilities for bindings
*
* Version: 1.0
* Created: 19/12/2012 12:14:34
* Revision: none
* Compiler: gcc
*
* Author: Florent Jaby (FJ), florent.jaby@gmail.com
* Company: Florent Jaby
*
* =====================================================================================
*/
#ifndef NSP_COMMON_H
#define NSP_COMMON_H
#include <string.h>
#include <libspotify/api.h>
#include <node.h>
#include <node_buffer.h>
#include <assert.h>
/**
* In a C++ function called from JS, takes a spotify error code, tests if not OK and throws an exception
* with the appropriate error message
*/
#define NSP_THROW_IF_ERROR(error) if(error != SP_ERROR_OK) {return v8::ThrowException( \
v8::Exception::Error(v8::String::New(sp_error_message( error )))\
);}
/**
* Reads a value at the given key and returns a C boolean value
* @param o: the JS object on which to read at the given key
* @param name: the key to read from
* @return the boolean value read from the object
*/
inline bool NSP_BOOL_KEY(v8::Handle<v8::Object> o, const char* name) {
assert(o->IsObject());
v8::Handle<v8::Value> value = o->Get(v8::String::New(name));
assert(value->IsBoolean());
return value->BooleanValue();
}
/**
* Reads a value at the given key and returns a C int value
* @param o: the JS object on which to read at the given key
* @param name: the key to read from
* @return the int value read from the object
*/
inline int NSP_INT_KEY(v8::Handle<v8::Object> o, const char* name) {
assert(o->IsObject());
v8::Handle<v8::Value> value = o->Get(v8::String::New(name));
assert(value->IsNumber());
assert(value->IsUint32());
return value->Int32Value();
}
/**
* Reads a value at the given key and returns a C string value
* NOTE: this function allocates the needed space for the string
* it is the responsibility of the caller to free this pointer
* @param o: the JS object on which to read at the given key
* @param name: the key to read from
* @return the string value read from the object
*/
inline char* NSP_STRING_KEY(v8::Handle<v8::Object> o, const char* name) {
assert(o->IsObject());
v8::Handle<v8::Value> value = o->Get(v8::String::New(name));
if(value->IsNull()) {
return NULL;
}
assert(value->IsString());
char* v = new char[value->ToString()->Length()+1];
strcpy(v, *(v8::String::AsciiValue(value)));
return v;
}
/**
* Reads a value at the given key and returns a Node buffer object
* @param o: the JS object on which to read at the given key
* @param name: the key to read from
* @return the node buffer object read from the object
*/
inline char* NSP_BUFFER_KEY(v8::Handle<v8::Object> o, const char* name) {
assert(o->IsObject());
v8::Handle<v8::Value> value = o->Get(v8::String::New(name));
assert(node::Buffer::HasInstance(value));
return node::Buffer::Data(value->ToObject());
}
/**
* Reads a value at the given key and returns a C int value which is the
* length of a Node buffer object
* @param o: the JS object on which to read at the given key
* @param name: the key to read from
* @return the length of the buffer read from the object
*/
inline int NSP_BUFFERLENGTH_KEY(v8::Handle<v8::Object> o, const char* name) {
assert(o->IsObject());
v8::Handle<v8::Value> value = o->Get(v8::String::New(name));
assert(node::Buffer::HasInstance(value));
return node::Buffer::Length(value->ToObject());
}
/**
* Stands for Node+Spotify
* namespace in which I declare most of the stuff I need for these bindings
*/
namespace nsp {
/**
* A function to use as a JS function that does nothing and returns this
*/
v8::Handle<v8::Value> JsNoOp(const v8::Arguments&);
/**
* init the session related functions to the target module exports
*/
void init_session(v8::Handle<v8::Object> target);
/**
* init the search related functions to the target module exports
*/
void init_search(v8::Handle<v8::Object> target);
/**
* init the track related functions to the target module exports
*/
void init_track(v8::Handle<v8::Object> target);
/**
* init the player related functions to the target module exports
*/
void init_player(v8::Handle<v8::Object> target);
/**
* init the album related functions to the target module exports
*/
void init_album(v8::Handle<v8::Object> target);
/**
* init the artist related functions to the target module exports
*/
void init_artist(v8::Handle<v8::Object> target);
void init_artistbrowse(v8::Handle<v8::Object> target);
/**
* init the link related functions to the target module exports
*/
void init_link(v8::Handle<v8::Object> target);
/**
* init the playlistcontainer related functions to the target module exports
*/
void init_playlistcontainer(v8::Handle<v8::Object> target);
/**
* init the playlist related functions to the target module exports
*/
void init_playlist(v8::Handle<v8::Object> target);
/**
* This utility class allows to keep track of a C pointer that we attached
* to a JS object. It differs from node's ObjectWrap in the fact that it
* does not need a constructor and both attributes are public.
* Node's ObjectWrap is useful to wrap C++ classes whereas this class is useful
* to wrap C structs. THIS CLASS DOES NOT MANAGE C MEMORY ALLOCATION
*/
template <typename T>
class ObjectHandle {
public:
/**
* @constructor
* Create a new ObjectHandle object with the given name
* the name can be used later to identify the wrapped objects
*/
ObjectHandle(const char* name);
/**
* Utility function to retrieve an ObjectHandle from a JS object
* @param obj, the JS Object in which the ObjectHandle was wrapped
*/
static ObjectHandle<T>* Unwrap(v8::Handle<v8::Value> obj);
/**
* A pointer to the C struct (most often) that we want to wrap
* We do not allocate this
*/
T* pointer;
/**
* The JS Object that we set our pointer in
* We do create this one
*/
v8::Persistent<v8::Object> object;
/**
* Get the name of the ObjectHandle that we gave it during instanciation
*/
char* GetName() {
return *(v8::String::Utf8Value(name_));
}
protected:
private:
v8::Persistent<v8::String> name_;
/**
* Empty function to set as constructor for an FunctionTemplate
* @deprecated
*/
v8::Handle<v8::Value> New(const v8::Arguments& args) {
v8::HandleScope scope;
// do nothing;
return args.This();
}
};
template <typename T>
ObjectHandle<T>::ObjectHandle(const char* name) : pointer(NULL) {
v8::Local<v8::FunctionTemplate> tpl = v8::FunctionTemplate::New(JsNoOp);
name_ = v8::Persistent<v8::String>::New(
v8::String::NewSymbol(name == NULL ? "CObject" : name)
);
tpl->SetClassName(name_);
tpl->InstanceTemplate()->SetInternalFieldCount(1);
v8::Local<v8::ObjectTemplate> otpl = tpl->InstanceTemplate();
object = v8::Persistent<v8::Object>::New(otpl->NewInstance());
object->SetPointerInInternalField(0, this);
}
template <typename T>
ObjectHandle<T>* ObjectHandle<T>::Unwrap(v8::Handle<v8::Value> obj) {
assert(obj->IsObject());
v8::Handle<v8::Object> handle = obj->ToObject();
ObjectHandle<T>* ptr = node::ObjectWrap::Unwrap<ObjectHandle<T> >(handle);
return ptr;
}
}
#endif /* NSP_COMMON_H */