Skip to content

Commit

Permalink
feat: Support node@12.
Browse files Browse the repository at this point in the history
BREAKING CHANGE: Drop support for node@6, but add support for node@12.

Merge pull request #20 from kant2002/kant/node-12-randytarampi

@kant2002 Sorry for the long wait here. Life got busy. 😅

LGTM though. I'll adjust the test build/minimum node versions to match in a following PR.

Thanks! 🙏
  • Loading branch information
randytarampi authored Jan 5, 2020
2 parents 590f825 + 40e189b commit b1883eb
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/decoder/decoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,6 @@ int gifReadCB(GifFileType * gif, GifByteType * buf, int length);
NAN_METHOD(decodeJpegBuffer);
NAN_METHOD(decodePngBuffer);
NAN_METHOD(decodeGifBuffer);
void initAll(Handle<Object> exports);
void initAll(v8::Local<v8::Object> exports);

#endif
2 changes: 1 addition & 1 deletion src/encoder/encoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,6 @@ NAN_METHOD(encodeToGifBuffer);
void pngWriteCB(png_structp png_ptr, png_bytep data, png_size_t length);
int gifWriteCB(GifFileType * gif, const GifByteType * chunk, int len);
void remapTransparentPixels(unsigned char * target, const unsigned char * map, size_t width, size_t height, int transColor, int threshold);
void initAll(Handle<Object> exports);
void initAll(v8::Local<v8::Object> exports);

#endif
19 changes: 13 additions & 6 deletions src/encoder/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,24 @@ NAN_METHOD(encodeToPngBuffer) {
size_t width = info[1].As<Integer>()->Value();
size_t height = info[2].As<Integer>()->Value();
int compression = info[3].As<Integer>()->Value();
bool interlaced = info[4]->BooleanValue();
bool trans = info[5]->BooleanValue();
bool interlaced = info[4].As<Boolean>()->Value();
bool trans = info[5].As<Boolean>()->Value();

char * metadata;

if (info[6]->IsNull() || info[6]->IsUndefined()) {
metadata = NULL;
} else {
int metadata_len = info[6].As<String>()->Utf8Length();
if (!info[6]->IsString())
{
Nan::ThrowError("Metadata should be a string");
return;
}

int metadata_len = Nan::DecodeBytes(info[6], Nan::Encoding::UTF8);
metadata = (char *)malloc((metadata_len + 1) * sizeof(char));
info[6].As<String>()->WriteUtf8(metadata);
memset(metadata, 0, (metadata_len + 1) * sizeof(char));
Nan::DecodeWrite(metadata, metadata_len, info[6], Nan::Encoding::UTF8);
}

Nan::Callback * callback = new Nan::Callback(info[7].As<Function>());
Expand Down Expand Up @@ -66,8 +73,8 @@ NAN_METHOD(encodeToGifBuffer) {
size_t height = info[2].As<Integer>()->Value();
int cmapSize = info[3].As<Integer>()->Value();
int colors = info[4].As<Integer>()->Value();
bool interlaced = info[5]->BooleanValue();
bool trans = info[6]->BooleanValue();
bool interlaced = info[5].As<Boolean>()->Value();
bool trans = info[6].As<Boolean>()->Value();
int threshold = info[7].As<Integer>()->Value();
Nan::Callback * callback = new Nan::Callback(info[8].As<Function>());

Expand Down
17 changes: 10 additions & 7 deletions src/image/image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

Nan::Persistent<Function> LwipImage::constructor;

void LwipImage::Init(Handle<Object> exports) {
void LwipImage::Init(v8::Local<v8::Object> exports) {
v8::Local<v8::Context> context = exports->CreationContext();
Nan::HandleScope scope;

// Prepare constructor template
Local<FunctionTemplate> tpl = Nan::New<FunctionTemplate>(New);
tpl->InstanceTemplate()->SetInternalFieldCount(1);
Expand All @@ -22,10 +25,10 @@ void LwipImage::Init(Handle<Object> exports) {
Nan::SetPrototypeMethod(tpl, "opacify", opacify);
Nan::SetPrototypeMethod(tpl, "paste", paste);
Nan::SetPrototypeMethod(tpl, "setPixel", setPixel);
constructor.Reset(tpl->GetFunction());
constructor.Reset(tpl->GetFunction(context).ToLocalChecked());
exports->Set(
Nan::New("LwipImage").ToLocalChecked(),
tpl->GetFunction()
tpl->GetFunction(context).ToLocalChecked()
);
}

Expand Down Expand Up @@ -57,8 +60,8 @@ NAN_METHOD(LwipImage::New) {
// info[0] - pixels buffer
// info[1,2] - width and height
Local<Object> pixBuff = info[0].As<Object>();
size_t width = info[1]->NumberValue();
size_t height = info[2]->NumberValue();
size_t width = info[1].As<Number>()->Value();
size_t height = info[2].As<Number>()->Value();
unsigned char * pixels = (unsigned char *)Buffer::Data(pixBuff);
// TODO: handle CImg exception
LwipImage * obj = new LwipImage(pixels, width, height);
Expand Down Expand Up @@ -238,8 +241,8 @@ NAN_METHOD(LwipImage::crop) {
NAN_METHOD(LwipImage::mirror) {
Nan::HandleScope();

bool xaxis = info[0]->BooleanValue();
bool yaxis = info[1]->BooleanValue();
bool xaxis = Nan::To<bool>(info[0]).FromJust();
bool yaxis = Nan::To<bool>(info[1]).FromJust();
Nan::Callback * callback = new Nan::Callback(info[2].As<Function>());
CImg<unsigned char> * cimg = ObjectWrap::Unwrap<LwipImage>(info.This())->_cimg;

Expand Down
2 changes: 1 addition & 1 deletion src/image/image.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ using namespace node;
*/
class LwipImage : public node::ObjectWrap {
public:
static void Init(Handle<Object> exports);
static void Init(v8::Local<v8::Object> exports);
static NAN_METHOD(NewInstance);
static NAN_METHOD(New);
static NAN_METHOD(resize);
Expand Down
2 changes: 1 addition & 1 deletion src/image/init.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "image.h"

// create an init function for our node module
void InitAll(Handle<Object> exports) {
void InitAll(v8::Local<v8::Object> exports) {
LwipImage::Init(exports);
}

Expand Down
5 changes: 3 additions & 2 deletions src/image/setpixel_worker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ SetPixelWorker::SetPixelWorker(
unsigned char a,
CImg<unsigned char> * cimg,
Nan::Callback * callback
): Nan::AsyncWorker(callback), _left(left), _top(top), _r(r), _g(g), _b(b), _a(a),
): Nan::AsyncWorker(callback, "lwip::setpixel.start"), _left(left), _top(top), _r(r), _g(g), _b(b), _a(a),
_cimg(cimg) {}

SetPixelWorker::~SetPixelWorker() {}
Expand All @@ -29,5 +29,6 @@ void SetPixelWorker::HandleOKCallback () {
Local<Value> argv[] = {
Nan::Null()
};
callback->Call(1, argv);
Nan::AsyncResource resource("lwip::setpixel.HandleOKCallback");
callback->Call(1, argv, &resource);
}

0 comments on commit b1883eb

Please sign in to comment.