Skip to content

Commit

Permalink
node >=0.11.x support
Browse files Browse the repository at this point in the history
  • Loading branch information
nekuz0r committed Feb 26, 2015
1 parent 5af1744 commit cda2a0e
Show file tree
Hide file tree
Showing 13 changed files with 311 additions and 100 deletions.
10 changes: 9 additions & 1 deletion src/addon.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,20 @@ bool find_int(const int value, const int array[], size_t s) {
return false;
}

#if NODE_VERSION_AT_LEAST(0, 11, 0)
void throw_error(v8::Isolate* isolate, const char* format, ...) {
#else
void throw_error(const char* format, ...) {
#endif
char buffer[256];
va_list args;
va_start(args, format);
vsnprintf(buffer, 156, format, args);
va_end(args);

v8::ThrowException(v8::Exception::Error(v8::String::New(buffer)));
#if NODE_VERSION_AT_LEAST(0, 11, 0)
isolate->ThrowException(v8::Exception::Error(v8::String::NewFromUtf8(isolate, buffer)));
#else
v8::ThrowException(v8::Exception::Error(v8::String::New(buffer)));
#endif
}
200 changes: 145 additions & 55 deletions src/addon.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define _ADDON_H_

#include <v8.h>
#include <node_version.h>

using namespace v8;

Expand All @@ -19,71 +20,157 @@

bool find_string(const char* string, const char* array[], size_t s);
bool find_int(const int value, const int array[], size_t s);
void throw_error(const char* fmt, ...);

#if NODE_VERSION_AT_LEAST(0, 11, 0)
void throw_error(v8::Isolate* isolate, const char* fmt, ...);
#else
void throw_error(const char* fmt, ...);
#endif

#if NODE_VERSION_AT_LEAST(0, 11, 0)

#define DECLARE(name) \
namespace nodemodule { \
static void name(const v8::FunctionCallbackInfo<v8::Value>& args); \
}

#define IMPLEMENT(name) \
void nodemodule::name(const v8::FunctionCallbackInfo<v8::Value>& args)

#define DECLARE(name) \
namespace nodemodule { \
static v8::Handle<v8::Value> name(const v8::Arguments& args); \
}
#define EXPORT_FUNCTION(name) \
target->Set(v8::String::NewFromUtf8(isolate, #name, v8::String::kInternalizedString), \
v8::FunctionTemplate::New(isolate, nodemodule::name)->GetFunction())

#define EXPORT_CONSTANT_INT(name) \
target->ForceSet(v8::String::NewFromUtf8(isolate, #name, v8::String::kInternalizedString), \
v8::Int32::New(isolate, name), static_cast<v8::PropertyAttribute>(v8::ReadOnly | v8::DontDelete));

#define EXPORT_CONSTANT_STRING(name) \
target->ForceSet(v8::String::NewFrontUtf8(isolate, #name, v8::String::kInternalizedString), \
v8::String::NewFromUtf8(isolate, name), static_cast<v8::PropertyAttribute>(v8::ReadOnly | v8::DontDelete));

#define IMPLEMENT(name) \
v8::Handle<v8::Value> nodemodule::name(const v8::Arguments& args)
#define EXPORT_CONSTANT_INT_ARRAY(name, array, length) \
{ \
v8::Local<v8::Array> arr = v8::Array::New(isolate, length); \
for (int i = 0; i < length; i++) { \
arr->Set(i, v8::Int32::New(isolate, array[i])); \
} \
target->ForceSet(v8::String::NewFromUtf8(isolate, #name, v8::String::kInternalizedString), \
arr, static_cast<v8::PropertyAttribute>(v8::ReadOnly | v8::DontDelete)); \
}

#define EXPORT_CONSTANT_STRING_ARRAY(name, array, length) \
{ \
v8::Local<v8::Array> arr = v8::Array::New(isolate, length); \
for (int i = 0; i < length; i++) { \
arr->Set(i, v8::String::NewFromUtf8(isolate, array[i])); \
} \
target->ForceSet(v8::String::NewFromUtf8(isolate, #name, v8::String::kInternalizedString), \
arr, static_cast<v8::PropertyAttribute>(v8::ReadOnly | v8::DontDelete)); \
}

#define EXPORT_FUNCTION(name) \
target->Set(v8::String::NewSymbol(#name), \
v8::FunctionTemplate::New(nodemodule::name)->GetFunction())
#define NODE_MODULE_INIT() \
namespace nodemodule { \
void init(v8::Handle<v8::Object> target); \
} \
void nodemodule::init(v8::Handle<v8::Object> target)

#define EXPORT_CONSTANT_INT(name) \
target->Set(v8::String::NewSymbol(#name), \
v8::Int32::New(name), static_cast<v8::PropertyAttribute>(v8::ReadOnly | v8::DontDelete));
#define NODE_MODULE_DECLARE(name) NODE_MODULE(name, nodemodule::init)
#define IMPLEMENT_EXPORT_INIT(name) void nodemodule::init##name(v8::Isolate* isolate, v8::Handle<v8::Object> target)
#define DECLARE_EXPORT_INIT(name) \
namespace nodemodule { \
void init##name(v8::Isolate* isolate, v8::Handle<v8::Object> target); \
}

#define INIT(name) nodemodule::init##name(isolate, target);

#define SCOPE_OPEN() \
v8::Isolate* isolate = args.GetIsolate(); \
v8::HandleScope scope(isolate)
#define SCOPE_CLOSE(obj) args.GetReturnValue().Set(obj)

#else

#define EXPORT_CONSTANT_STRING(name) \
target->Set(v8::String::NewSymbol(#name), \
v8::String::New(name), static_cast<v8::PropertyAttribute>(v8::ReadOnly | v8::DontDelete));
#define DECLARE(name) \
namespace nodemodule { \
static v8::Handle<v8::Value> name(const v8::Arguments& args); \
}

#define IMPLEMENT(name) \
v8::Handle<v8::Value> nodemodule::name(const v8::Arguments& args)

#define EXPORT_CONSTANT_INT_ARRAY(name, array, length) \
{ \
v8::Local<v8::Array> arr = v8::Array::New(); \
for (int i = 0; i < length; i++) { \
arr->Set(i, v8::Int32::New(array[i])); \
} \
target->Set(v8::String::NewSymbol(#name), arr, static_cast<v8::PropertyAttribute>(v8::ReadOnly | v8::DontDelete)); \
}
#define EXPORT_FUNCTION(name) \
target->Set(v8::String::NewSymbol(#name), \
v8::FunctionTemplate::New(nodemodule::name)->GetFunction())

#define EXPORT_CONSTANT_INT(name) \
target->Set(v8::String::NewSymbol(#name), \
v8::Int32::New(name), static_cast<v8::PropertyAttribute>(v8::ReadOnly | v8::DontDelete));

#define EXPORT_CONSTANT_STRING_ARRAY(name, array, length) \
{ \
v8::Local<v8::Array> arr = v8::Array::New(); \
for (int i = 0; i < length; i++) { \
arr->Set(i, v8::String::New(array[i])); \
#define EXPORT_CONSTANT_STRING(name) \
target->Set(v8::String::NewSymbol(#name), \
v8::String::New(name), static_cast<v8::PropertyAttribute>(v8::ReadOnly | v8::DontDelete));

#define EXPORT_CONSTANT_INT_ARRAY(name, array, length) \
{ \
v8::Local<v8::Array> arr = v8::Array::New(length); \
for (int i = 0; i < length; i++) { \
arr->Set(i, v8::Int32::New(array[i])); \
} \
target->Set(v8::String::NewSymbol(#name), arr, static_cast<v8::PropertyAttribute>(v8::ReadOnly | v8::DontDelete)); \
}

#define EXPORT_CONSTANT_STRING_ARRAY(name, array, length) \
{ \
v8::Local<v8::Array> arr = v8::Array::New(length); \
for (int i = 0; i < length; i++) { \
arr->Set(i, v8::String::New(array[i])); \
} \
target->Set(v8::String::NewSymbol(#name), arr, static_cast<v8::PropertyAttribute>(v8::ReadOnly | v8::DontDelete)); \
}

#define NODE_MODULE_INIT() \
namespace nodemodule { \
void init(v8::Handle<v8::Object> target); \
} \
target->Set(v8::String::NewSymbol(#name), arr, static_cast<v8::PropertyAttribute>(v8::ReadOnly | v8::DontDelete)); \
}

#define NODE_MODULE_INIT() \
namespace nodemodule { \
void init(v8::Handle<v8::Object> target); \
} \
void nodemodule::init(v8::Handle<v8::Object> target)
#define NODE_MODULE_DECLARE(name) NODE_MODULE(name, nodemodule::init)
#define IMPLEMENT_EXPORT_INIT(name) void nodemodule::init##name(v8::Handle<v8::Object> target)
#define DECLARE_EXPORT_INIT(name) \
namespace nodemodule { \
void init##name(v8::Handle<v8::Object> target); \
}
void nodemodule::init(v8::Handle<v8::Object> target)
#define NODE_MODULE_DECLARE(name) NODE_MODULE(name, nodemodule::init)
#define IMPLEMENT_EXPORT_INIT(name) void nodemodule::init##name(v8::Handle<v8::Object> target)
#define DECLARE_EXPORT_INIT(name) \
namespace nodemodule { \
void init##name(v8::Handle<v8::Object> target); \
}

#define INIT(name) nodemodule::init##name(target);

#define SCOPE_OPEN() v8::HandleScope scope
#define SCOPE_CLOSE(obj) return scope.Close(obj)

#endif


#define INIT(name) nodemodule::init##name(target);

#define SCOPE_OPEN() v8::HandleScope scope
#define SCOPE_CLOSE(obj) return scope.Close(obj)
#if NODE_VERSION_AT_LEAST(0, 11, 0)
#define UNDEFINED() v8::Undefined(isolate)
#define INT32(v) v8::Int32::New(isolate, v)
#define UINT32(v) v8::Uint32::New(isolate, v)
#define STRING(v) v8::String::NewFromUtf8(isolate, v)
#else
#define UNDEFINED() v8::Undefined()
#define INT32(v) v8::Int32::New(v)
#define UINT32(v) v8::Uint32::New(v)
#define STRING(v) v8::String::New(v)
#endif

#define UNDEFINED() v8::Undefined()
#define INT32(v) v8::Int32::New(v)
#define UINT32(v) v8::Uint32::New(v)
#define STRING(v) v8::String::New(v)

#define THROW_ERROR(fmt, ...) \
throw_error(fmt, __VA_ARGS__); \
SCOPE_CLOSE(UNDEFINED())
#if NODE_VERSION_AT_LEAST(0, 11, 0)
#define THROW_ERROR(fmt, ...) \
throw_error(isolate, fmt, __VA_ARGS__); \
SCOPE_CLOSE(UNDEFINED());
#else
#define THROW_ERROR(fmt, ...) \
throw_error(fmt, __VA_ARGS__); \
SCOPE_CLOSE(UNDEFINED())
#endif

#define SET_ARGUMENT_NAME(id, name) static const char* arg##id = #name
#define GET_ARGUMENT_NAME(id) arg##id
Expand Down Expand Up @@ -122,7 +209,10 @@
#define GET_ARGUMENT_AS_NUMBER(id) GET_ARGUMENT_AS_TYPE(id, NumberValue)
#define GET_ARGUMENT_AS_STRING(id) GET_ARGUMENT_AS_TYPE(id, ToString)
#define GET_ARGUMENT_AS_LOCAL_FUNCTION(id) v8::Local<v8::Function>::Cast(args[id])
#define GET_ARGUMENT_AS_PERSISTENT_FUNCTION(id) v8::Persistent<v8::Function>::New(GET_ARGUMENT_AS_LOCAL_FUNCTION(id))

#if !NODE_VERSION_AT_LEAST(0, 11, 0)
#define GET_ARGUMENT_AS_PERSISTENT_FUNCTION(id) v8::Persistent<v8::Function>::New(GET_ARGUMENT_AS_LOCAL_FUNCTION(id))
#endif

#define LIST(...) { __VA_ARGS__ }
#define CHECK_ARGUMENT_IN_STRINGS(id, value, T) \
Expand Down
12 changes: 10 additions & 2 deletions src/devlib/ds1302.cc
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,17 @@ IMPLEMENT(ds1302clockRead) {
int clockData[8];
::ds1302clockRead(clockData);

v8::Local<v8::Array> res = v8::Array::New(8);
#if NODE_VERSION_AT_LEAST(0, 11, 0)
v8::Local<v8::Array> res = v8::Array::New(isolate, 8);
#else
v8::Local<v8::Array> res = v8::Array::New(8);
#endif
for (int i = 0; i < 8; i++) {
res->Set(i, v8::Int32::New(clockData[i]));
#if NODE_VERSION_AT_LEAST(0, 11, 0)
res->Set(i, v8::Int32::New(isolate, clockData[i]));
#else
res->Set(i, v8::Int32::New(clockData[i]));
#endif
}

SCOPE_CLOSE(res);
Expand Down
6 changes: 5 additions & 1 deletion src/devlib/lcd.cc
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,11 @@ IMPLEMENT(lcdPuts) {
CHECK_ARGUMENT_TYPE_STRING(1);

int fd = GET_ARGUMENT_AS_INT32(0);
v8::String::AsciiValue data(GET_ARGUMENT_AS_STRING(1));
#if NODE_VERSION_AT_LEAST(0, 11, 0)
v8::String::Utf8Value data(GET_ARGUMENT_AS_STRING(1));
#else
v8::String::AsciiValue data(GET_ARGUMENT_AS_STRING(1));
#endif

::lcdPuts(fd, *data);

Expand Down
30 changes: 23 additions & 7 deletions src/devlib/lcd128x64.cc
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,15 @@ IMPLEMENT(lcd128x64orientCoordinates) {
int x, y;
::lcd128x64orientCoordinates(&x, &y);

v8::Local<v8::Array> res = v8::Array::New(2);
res->Set(0, v8::Int32::New(x));
res->Set(1, v8::Int32::New(y));
#if NODE_VERSION_AT_LEAST(0, 11, 0)
v8::Local<v8::Array> res = v8::Array::New(isolate, 2);
res->Set(0, v8::Int32::New(isolate, x));
res->Set(1, v8::Int32::New(isolate, y));
#else
v8::Local<v8::Array> res = v8::Array::New(2);
res->Set(0, v8::Int32::New(x));
res->Set(1, v8::Int32::New(y));
#endif

SCOPE_CLOSE(res);
}
Expand All @@ -75,9 +81,15 @@ IMPLEMENT(lcd128x64getScreenSize) {
int x, y;
::lcd128x64getScreenSize(&x, &y);

v8::Local<v8::Array> res = v8::Array::New(2);
res->Set(0, v8::Int32::New(x));
res->Set(1, v8::Int32::New(y));
#if NODE_VERSION_AT_LEAST(0, 11, 0)
v8::Local<v8::Array> res = v8::Array::New(isolate, 2);
res->Set(0, v8::Int32::New(isolate, x));
res->Set(1, v8::Int32::New(isolate, y));
#else
v8::Local<v8::Array> res = v8::Array::New(2);
res->Set(0, v8::Int32::New(x));
res->Set(1, v8::Int32::New(y));
#endif

SCOPE_CLOSE(res);
}
Expand Down Expand Up @@ -291,7 +303,11 @@ IMPLEMENT(lcd128x64puts) {

int x = GET_ARGUMENT_AS_INT32(0);
int y = GET_ARGUMENT_AS_INT32(1);
v8::String::AsciiValue string(GET_ARGUMENT_AS_STRING(2));
#if NODE_VERSION_AT_LEAST(0, 11, 0)
v8::String::Utf8Value string(GET_ARGUMENT_AS_STRING(2));
#else
v8::String::AsciiValue string(GET_ARGUMENT_AS_STRING(2));
#endif
int bgColor = GET_ARGUMENT_AS_INT32(3);
int fgColor = GET_ARGUMENT_AS_INT32(4);

Expand Down
36 changes: 28 additions & 8 deletions src/devlib/maxdetect.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,26 @@ IMPLEMENT(maxDetectRead) {
unsigned char buffer[4];
int res = ::maxDetectRead(pin, buffer);

v8::Local<v8::Array> data = v8::Array::New(4);
#if NODE_VERSION_AT_LEAST(0, 11, 0)
v8::Local<v8::Array> data = v8::Array::New(isolate, 4);
#else
v8::Local<v8::Array> data = v8::Array::New(4);
#endif
for (int i = 0; i < 4; i++) {
data->Set(i, Int32::New(buffer[i]));
#if NODE_VERSION_AT_LEAST(0, 11, 0)
data->Set(i, Int32::New(isolate, buffer[i]));
#else
data->Set(i, Int32::New(buffer[i]));
#endif
}

v8::Local<v8::Array> ret = v8::Array::New(2);
ret->Set(0, Int32::New(res));
#if NODE_VERSION_AT_LEAST(0, 11, 0)
v8::Local<v8::Array> ret = v8::Array::New(isolate, 2);
ret->Set(0, Int32::New(isolate, res));
#else
v8::Local<v8::Array> ret = v8::Array::New(2);
ret->Set(0, Int32::New(res));
#endif
ret->Set(1, data);

SCOPE_CLOSE(ret);
Expand All @@ -44,10 +57,17 @@ IMPLEMENT(readRHT03) {
int temp, rh;
int res = ::readRHT03(pin, &temp, &rh);

v8::Local<v8::Array> ret = v8::Array::New(3);
ret->Set(0, v8::Int32::New(res));
ret->Set(1, v8::Int32::New(temp));
ret->Set(2, v8::Int32::New(rh));
#if NODE_VERSION_AT_LEAST(0, 11, 0)
v8::Local<v8::Array> ret = v8::Array::New(isolate, 3);
ret->Set(0, v8::Int32::New(isolate, res));
ret->Set(1, v8::Int32::New(isolate, temp));
ret->Set(2, v8::Int32::New(isolate, rh));
#else
v8::Local<v8::Array> ret = v8::Array::New(3);
ret->Set(0, v8::Int32::New(res));
ret->Set(1, v8::Int32::New(temp));
ret->Set(2, v8::Int32::New(rh));
#endif

SCOPE_CLOSE(ret);
}
Expand Down
Loading

0 comments on commit cda2a0e

Please sign in to comment.