Skip to content
This repository has been archived by the owner on Jun 6, 2021. It is now read-only.

Commit

Permalink
Follow the most up to date NAN ObjectWrap guide for constructors
Browse files Browse the repository at this point in the history
  • Loading branch information
nstepien committed Nov 4, 2015
1 parent 202f156 commit 1dd8e7e
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 12 deletions.
14 changes: 9 additions & 5 deletions src/dec/stream_decode.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@

using namespace v8;

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

StreamDecode::StreamDecode() {
BrotliStateInit(&state);
output = BrotliInitBufferOutput(&mem_output);
Expand All @@ -15,14 +13,15 @@ StreamDecode::~StreamDecode() {

void StreamDecode::Init(Local<Object> target) {
Local<FunctionTemplate> tpl = Nan::New<FunctionTemplate>(New);
tpl->SetClassName(Nan::New<String>("StreamDecode").ToLocalChecked());
tpl->SetClassName(Nan::New("StreamDecode").ToLocalChecked());
tpl->InstanceTemplate()->SetInternalFieldCount(1);

Nan::SetPrototypeMethod(tpl, "transform", Transform);
Nan::SetPrototypeMethod(tpl, "flush", Flush);

constructor.Reset(tpl->GetFunction());
Nan::Set(target, Nan::New<String>("StreamDecode").ToLocalChecked(), tpl->GetFunction());
constructor().Reset(Nan::GetFunction(tpl).ToLocalChecked());
Nan::Set(target, Nan::New("StreamDecode").ToLocalChecked(),
Nan::GetFunction(tpl).ToLocalChecked());
}

NAN_METHOD(StreamDecode::New) {
Expand Down Expand Up @@ -50,3 +49,8 @@ NAN_METHOD(StreamDecode::Flush) {
Nan::Callback *callback = new Nan::Callback(info[0].As<Function>());
Nan::AsyncQueueWorker(new StreamDecodeWorker(callback, obj, true));
}

Nan::Persistent<Function> & StreamDecode::constructor() {
static Nan::Persistent<Function> my_constructor;
return my_constructor;
}
2 changes: 1 addition & 1 deletion src/dec/stream_decode.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class StreamDecode : public Nan::ObjectWrap {
static NAN_METHOD(New);
static NAN_METHOD(Transform);
static NAN_METHOD(Flush);
static Nan::Persistent<v8::Function> constructor;
static inline Nan::Persistent<v8::Function> & constructor();
BrotliMemInput mem_input;
};

Expand Down
14 changes: 9 additions & 5 deletions src/enc/stream_encode.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
using namespace v8;
using namespace brotli;

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

StreamEncode::StreamEncode(BrotliParams params) {
compressor = new BrotliCompressor(params);
}
Expand All @@ -16,15 +14,16 @@ StreamEncode::~StreamEncode() {

void StreamEncode::Init(Local<Object> target) {
Local<FunctionTemplate> tpl = Nan::New<FunctionTemplate>(New);
tpl->SetClassName(Nan::New<String>("StreamEncode").ToLocalChecked());
tpl->SetClassName(Nan::New("StreamEncode").ToLocalChecked());
tpl->InstanceTemplate()->SetInternalFieldCount(1);

Nan::SetPrototypeMethod(tpl, "getBlockSize", GetBlockSize);
Nan::SetPrototypeMethod(tpl, "copy", Copy);
Nan::SetPrototypeMethod(tpl, "encode", Encode);

constructor.Reset(tpl->GetFunction());
Nan::Set(target, Nan::New<String>("StreamEncode").ToLocalChecked(), tpl->GetFunction());
constructor().Reset(Nan::GetFunction(tpl).ToLocalChecked());
Nan::Set(target, Nan::New("StreamEncode").ToLocalChecked(),
Nan::GetFunction(tpl).ToLocalChecked());
}

NAN_METHOD(StreamEncode::New) {
Expand Down Expand Up @@ -55,3 +54,8 @@ NAN_METHOD(StreamEncode::Encode) {
Nan::Callback *callback = new Nan::Callback(info[1].As<Function>());
Nan::AsyncQueueWorker(new StreamEncodeWorker(callback, obj->compressor, is_last));
}

Nan::Persistent<Function> & StreamEncode::constructor() {
static Nan::Persistent<Function> my_constructor;
return my_constructor;
}
2 changes: 1 addition & 1 deletion src/enc/stream_encode.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class StreamEncode : public Nan::ObjectWrap {
static NAN_METHOD(GetBlockSize);
static NAN_METHOD(Copy);
static NAN_METHOD(Encode);
static Nan::Persistent<v8::Function> constructor;
static inline Nan::Persistent<v8::Function> & constructor();
brotli::BrotliCompressor *compressor;
};

Expand Down

0 comments on commit 1dd8e7e

Please sign in to comment.