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

Commit

Permalink
Set coders as sync/async in their constructors
Browse files Browse the repository at this point in the history
  • Loading branch information
nstepien committed Jun 17, 2018
1 parent aa5a007 commit 8d118cd
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 44 deletions.
28 changes: 12 additions & 16 deletions src/dec/stream_decode.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

napi_ref StreamDecode::constructor;

StreamDecode::StreamDecode(napi_env env) {
StreamDecode::StreamDecode(napi_env env, napi_value async) {
napi_get_value_bool(env, async, &isAsync);
state = BrotliDecoderCreateInstance(Allocator::Alloc, Allocator::Free, &alloc);
alloc.ReportMemoryToV8(env);
}
Expand All @@ -29,11 +30,12 @@ napi_value StreamDecode::Init(napi_env env, napi_value exports) {
}

napi_value StreamDecode::New(napi_env env, napi_callback_info info) {
size_t argc = 0;
size_t argc = 1;
napi_value argv[1];
napi_value jsthis;
napi_get_cb_info(env, info, &argc, nullptr, &jsthis, nullptr);
napi_get_cb_info(env, info, &argc, argv, &jsthis, nullptr);

StreamDecode* obj = new StreamDecode(env);
StreamDecode* obj = new StreamDecode(env, argv[0]);

napi_wrap(env,
jsthis,
Expand All @@ -46,8 +48,8 @@ napi_value StreamDecode::New(napi_env env, napi_callback_info info) {
}

napi_value StreamDecode::Transform(napi_env env, napi_callback_info info) {
size_t argc = 3;
napi_value argv[3];
size_t argc = 2;
napi_value argv[2];
napi_value jsthis;
napi_get_cb_info(env, info, &argc, argv, &jsthis, nullptr);

Expand All @@ -59,10 +61,7 @@ napi_value StreamDecode::Transform(napi_env env, napi_callback_info info) {
napi_create_reference(env, argv[0], 1, &obj->bufref);
napi_create_reference(env, argv[1], 1, &obj->cbref);

bool isAsync;
napi_get_value_bool(env, argv[2], &isAsync);

if (isAsync) {
if (obj->isAsync) {
napi_value resource_name;
napi_create_string_utf8(env, "DecodeResource", NAPI_AUTO_LENGTH, &resource_name);

Expand All @@ -85,8 +84,8 @@ napi_value StreamDecode::Transform(napi_env env, napi_callback_info info) {
}

napi_value StreamDecode::Flush(napi_env env, napi_callback_info info) {
size_t argc = 2;
napi_value argv[2];
size_t argc = 1;
napi_value argv[1];
napi_value jsthis;
napi_get_cb_info(env, info, &argc, argv, &jsthis, nullptr);

Expand All @@ -95,13 +94,10 @@ napi_value StreamDecode::Flush(napi_env env, napi_callback_info info) {

napi_create_reference(env, argv[0], 1, &obj->cbref);

bool isAsync;
napi_get_value_bool(env, argv[1], &isAsync);

obj->next_in = nullptr;
obj->available_in = 0;

if (isAsync) {
if (obj->isAsync) {
napi_value resource_name;
napi_create_string_utf8(env, "DecodeResource", NAPI_AUTO_LENGTH, &resource_name);

Expand Down
13 changes: 7 additions & 6 deletions src/dec/stream_decode.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,17 @@ class StreamDecode : public StreamCoder {
static napi_value Init(napi_env env, napi_value exports);
static void Destructor(napi_env env, void* nativeObject, void* finalize_hint);

napi_async_work work = NULL;
napi_ref bufref = NULL;
napi_ref cbref = NULL;
bool isAsync = true;
bool hasError = false;
BrotliDecoderState* state;
const uint8_t* next_in;
size_t available_in;
BrotliDecoderState* state;
bool hasError = false;
napi_ref bufref = NULL;
napi_ref cbref = NULL;
napi_async_work work = NULL;

private:
explicit StreamDecode(napi_env env);
explicit StreamDecode(napi_env env, napi_value async);

static napi_value New(napi_env env, napi_callback_info info);
static napi_value Transform(napi_env env, napi_callback_info info);
Expand Down
27 changes: 11 additions & 16 deletions src/enc/stream_encode.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

napi_ref StreamEncode::constructor;

StreamEncode::StreamEncode(napi_env env, napi_value params) {
StreamEncode::StreamEncode(napi_env env, napi_value async, napi_value params) {
napi_get_value_bool(env, async, &isAsync);
state = BrotliEncoderCreateInstance(Allocator::Alloc, Allocator::Free, &alloc);

SetParameter(env, params, "mode", BROTLI_PARAM_MODE);
Expand Down Expand Up @@ -66,12 +67,12 @@ napi_value StreamEncode::Init(napi_env env, napi_value exports) {
}

napi_value StreamEncode::New(napi_env env, napi_callback_info info) {
size_t argc = 1;
napi_value argv[1];
size_t argc = 2;
napi_value argv[2];
napi_value jsthis;
napi_get_cb_info(env, info, &argc, argv, &jsthis, nullptr);

StreamEncode* obj = new StreamEncode(env, argv[0]);
StreamEncode* obj = new StreamEncode(env, argv[0], argv[1]);

napi_wrap(env,
jsthis,
Expand All @@ -84,8 +85,8 @@ napi_value StreamEncode::New(napi_env env, napi_callback_info info) {
}

napi_value StreamEncode::Transform(napi_env env, napi_callback_info info) {
size_t argc = 3;
napi_value argv[3];
size_t argc = 2;
napi_value argv[2];
napi_value jsthis;
napi_get_cb_info(env, info, &argc, argv, &jsthis, nullptr);

Expand All @@ -97,12 +98,9 @@ napi_value StreamEncode::Transform(napi_env env, napi_callback_info info) {
napi_create_reference(env, argv[0], 1, &obj->bufref);
napi_create_reference(env, argv[1], 1, &obj->cbref);

bool isAsync;
napi_get_value_bool(env, argv[2], &isAsync);

obj->op = BROTLI_OPERATION_PROCESS;

if (isAsync) {
if (obj->isAsync) {
napi_value resource_name;
napi_create_string_utf8(env, "EncodeResource", NAPI_AUTO_LENGTH, &resource_name);

Expand All @@ -125,8 +123,8 @@ napi_value StreamEncode::Transform(napi_env env, napi_callback_info info) {
}

napi_value StreamEncode::Flush(napi_env env, napi_callback_info info) {
size_t argc = 3;
napi_value argv[3];
size_t argc = 2;
napi_value argv[2];
napi_value jsthis;
napi_get_cb_info(env, info, &argc, argv, &jsthis, nullptr);

Expand All @@ -138,17 +136,14 @@ napi_value StreamEncode::Flush(napi_env env, napi_callback_info info) {

napi_create_reference(env, argv[1], 1, &obj->cbref);

bool isAsync;
napi_get_value_bool(env, argv[2], &isAsync);

obj->op = isFinish
? BROTLI_OPERATION_FINISH
: BROTLI_OPERATION_FLUSH;

obj->next_in = nullptr;
obj->available_in = 0;

if (isAsync) {
if (obj->isAsync) {
napi_value resource_name;
napi_create_string_utf8(env, "EncodeResource", NAPI_AUTO_LENGTH, &resource_name);

Expand Down
13 changes: 7 additions & 6 deletions src/enc/stream_encode.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,18 @@ class StreamEncode : public StreamCoder {
static napi_value Init(napi_env env, napi_value exports);
static void Destructor(napi_env env, void* nativeObject, void* finalize_hint);

napi_async_work work = NULL;
napi_ref bufref = NULL;
napi_ref cbref = NULL;
bool isAsync = true;
bool hasError = false;
BrotliEncoderState* state;
BrotliEncoderOperation op;
const uint8_t* next_in;
size_t available_in;
BrotliEncoderState* state;
bool hasError = false;
napi_ref bufref = NULL;
napi_ref cbref = NULL;
napi_async_work work = NULL;

private:
explicit StreamEncode(napi_env env, napi_value params);
explicit StreamEncode(napi_env env, napi_value async, napi_value params);
void SetParameter(napi_env env, napi_value params, const char* key, BrotliEncoderParameter p);

static napi_value New(napi_env env, napi_callback_info info);
Expand Down

0 comments on commit 8d118cd

Please sign in to comment.