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

Commit

Permalink
Hold a reference to input buffers until we're done processing them
Browse files Browse the repository at this point in the history
  • Loading branch information
nstepien committed May 20, 2018
1 parent d97cc3f commit b5b50dd
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 10 deletions.
5 changes: 3 additions & 2 deletions src/dec/stream_decode.cc
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ napi_value StreamDecode::Transform(napi_env env, napi_callback_info info) {

napi_get_buffer_info(env, argv[0], (void**)&obj->next_in, &obj->available_in);

napi_create_reference(env, argv[1], 1, &obj->cb);
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);
Expand Down Expand Up @@ -98,7 +99,7 @@ napi_value StreamDecode::Flush(napi_env env, napi_callback_info info) {
StreamDecode* obj;
napi_unwrap(env, jsthis, reinterpret_cast<void**>(&obj));

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

bool isAsync;
napi_get_value_bool(env, argv[1], &isAsync);
Expand Down
3 changes: 2 additions & 1 deletion src/dec/stream_decode.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ 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_ref cb;
napi_ref bufref = NULL;
napi_ref cbref = NULL;
const uint8_t* next_in;
size_t available_in;
BrotliDecoderState* state;
Expand Down
8 changes: 6 additions & 2 deletions src/dec/stream_decode_tasks.cc
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,13 @@ void CompleteDecode(napi_env env, napi_status, void* data) {
napi_get_null(env, &null);

napi_value cb;
napi_get_reference_value(env, obj->cb, &cb);
napi_get_reference_value(env, obj->cbref, &cb);

napi_delete_reference(env, obj->cb);
if (obj->bufref != NULL) {
napi_delete_reference(env, obj->bufref);
obj->bufref = NULL;
}
napi_delete_reference(env, obj->cbref);

if (obj->hasError) {
napi_value msg;
Expand Down
5 changes: 3 additions & 2 deletions src/enc/stream_encode.cc
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ napi_value StreamEncode::Transform(napi_env env, napi_callback_info info) {

napi_get_buffer_info(env, argv[0], (void**)&obj->next_in, &obj->available_in);

napi_create_reference(env, argv[1], 1, &obj->cb);
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);
Expand Down Expand Up @@ -141,7 +142,7 @@ napi_value StreamEncode::Flush(napi_env env, napi_callback_info info) {
bool isFinish;
napi_get_value_bool(env, argv[0], &isFinish);

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

bool isAsync;
napi_get_value_bool(env, argv[2], &isAsync);
Expand Down
3 changes: 2 additions & 1 deletion src/enc/stream_encode.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ 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_ref cb;
napi_ref bufref = NULL;
napi_ref cbref = NULL;
BrotliEncoderOperation op;
const uint8_t* next_in;
size_t available_in;
Expand Down
8 changes: 6 additions & 2 deletions src/enc/stream_encode_tasks.cc
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,13 @@ void CompleteEncode(napi_env env, napi_status, void* data) {
napi_get_null(env, &null);

napi_value cb;
napi_get_reference_value(env, obj->cb, &cb);
napi_get_reference_value(env, obj->cbref, &cb);

napi_delete_reference(env, obj->cb);
if (obj->bufref != NULL) {
napi_delete_reference(env, obj->bufref);
obj->bufref = NULL;
}
napi_delete_reference(env, obj->cbref);

if (obj->hasError) {
napi_value msg;
Expand Down

0 comments on commit b5b50dd

Please sign in to comment.