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

Commit 8d118cd

Browse files
committed
Set coders as sync/async in their constructors
1 parent aa5a007 commit 8d118cd

File tree

4 files changed

+37
-44
lines changed

4 files changed

+37
-44
lines changed

src/dec/stream_decode.cc

+12-16
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
napi_ref StreamDecode::constructor;
44

5-
StreamDecode::StreamDecode(napi_env env) {
5+
StreamDecode::StreamDecode(napi_env env, napi_value async) {
6+
napi_get_value_bool(env, async, &isAsync);
67
state = BrotliDecoderCreateInstance(Allocator::Alloc, Allocator::Free, &alloc);
78
alloc.ReportMemoryToV8(env);
89
}
@@ -29,11 +30,12 @@ napi_value StreamDecode::Init(napi_env env, napi_value exports) {
2930
}
3031

3132
napi_value StreamDecode::New(napi_env env, napi_callback_info info) {
32-
size_t argc = 0;
33+
size_t argc = 1;
34+
napi_value argv[1];
3335
napi_value jsthis;
34-
napi_get_cb_info(env, info, &argc, nullptr, &jsthis, nullptr);
36+
napi_get_cb_info(env, info, &argc, argv, &jsthis, nullptr);
3537

36-
StreamDecode* obj = new StreamDecode(env);
38+
StreamDecode* obj = new StreamDecode(env, argv[0]);
3739

3840
napi_wrap(env,
3941
jsthis,
@@ -46,8 +48,8 @@ napi_value StreamDecode::New(napi_env env, napi_callback_info info) {
4648
}
4749

4850
napi_value StreamDecode::Transform(napi_env env, napi_callback_info info) {
49-
size_t argc = 3;
50-
napi_value argv[3];
51+
size_t argc = 2;
52+
napi_value argv[2];
5153
napi_value jsthis;
5254
napi_get_cb_info(env, info, &argc, argv, &jsthis, nullptr);
5355

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

62-
bool isAsync;
63-
napi_get_value_bool(env, argv[2], &isAsync);
64-
65-
if (isAsync) {
64+
if (obj->isAsync) {
6665
napi_value resource_name;
6766
napi_create_string_utf8(env, "DecodeResource", NAPI_AUTO_LENGTH, &resource_name);
6867

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

8786
napi_value StreamDecode::Flush(napi_env env, napi_callback_info info) {
88-
size_t argc = 2;
89-
napi_value argv[2];
87+
size_t argc = 1;
88+
napi_value argv[1];
9089
napi_value jsthis;
9190
napi_get_cb_info(env, info, &argc, argv, &jsthis, nullptr);
9291

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

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

98-
bool isAsync;
99-
napi_get_value_bool(env, argv[1], &isAsync);
100-
10197
obj->next_in = nullptr;
10298
obj->available_in = 0;
10399

104-
if (isAsync) {
100+
if (obj->isAsync) {
105101
napi_value resource_name;
106102
napi_create_string_utf8(env, "DecodeResource", NAPI_AUTO_LENGTH, &resource_name);
107103

src/dec/stream_decode.h

+7-6
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,17 @@ class StreamDecode : public StreamCoder {
1111
static napi_value Init(napi_env env, napi_value exports);
1212
static void Destructor(napi_env env, void* nativeObject, void* finalize_hint);
1313

14-
napi_async_work work = NULL;
15-
napi_ref bufref = NULL;
16-
napi_ref cbref = NULL;
14+
bool isAsync = true;
15+
bool hasError = false;
16+
BrotliDecoderState* state;
1717
const uint8_t* next_in;
1818
size_t available_in;
19-
BrotliDecoderState* state;
20-
bool hasError = false;
19+
napi_ref bufref = NULL;
20+
napi_ref cbref = NULL;
21+
napi_async_work work = NULL;
2122

2223
private:
23-
explicit StreamDecode(napi_env env);
24+
explicit StreamDecode(napi_env env, napi_value async);
2425

2526
static napi_value New(napi_env env, napi_callback_info info);
2627
static napi_value Transform(napi_env env, napi_callback_info info);

src/enc/stream_encode.cc

+11-16
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
napi_ref StreamEncode::constructor;
44

5-
StreamEncode::StreamEncode(napi_env env, napi_value params) {
5+
StreamEncode::StreamEncode(napi_env env, napi_value async, napi_value params) {
6+
napi_get_value_bool(env, async, &isAsync);
67
state = BrotliEncoderCreateInstance(Allocator::Alloc, Allocator::Free, &alloc);
78

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

6869
napi_value StreamEncode::New(napi_env env, napi_callback_info info) {
69-
size_t argc = 1;
70-
napi_value argv[1];
70+
size_t argc = 2;
71+
napi_value argv[2];
7172
napi_value jsthis;
7273
napi_get_cb_info(env, info, &argc, argv, &jsthis, nullptr);
7374

74-
StreamEncode* obj = new StreamEncode(env, argv[0]);
75+
StreamEncode* obj = new StreamEncode(env, argv[0], argv[1]);
7576

7677
napi_wrap(env,
7778
jsthis,
@@ -84,8 +85,8 @@ napi_value StreamEncode::New(napi_env env, napi_callback_info info) {
8485
}
8586

8687
napi_value StreamEncode::Transform(napi_env env, napi_callback_info info) {
87-
size_t argc = 3;
88-
napi_value argv[3];
88+
size_t argc = 2;
89+
napi_value argv[2];
8990
napi_value jsthis;
9091
napi_get_cb_info(env, info, &argc, argv, &jsthis, nullptr);
9192

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

100-
bool isAsync;
101-
napi_get_value_bool(env, argv[2], &isAsync);
102-
103101
obj->op = BROTLI_OPERATION_PROCESS;
104102

105-
if (isAsync) {
103+
if (obj->isAsync) {
106104
napi_value resource_name;
107105
napi_create_string_utf8(env, "EncodeResource", NAPI_AUTO_LENGTH, &resource_name);
108106

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

127125
napi_value StreamEncode::Flush(napi_env env, napi_callback_info info) {
128-
size_t argc = 3;
129-
napi_value argv[3];
126+
size_t argc = 2;
127+
napi_value argv[2];
130128
napi_value jsthis;
131129
napi_get_cb_info(env, info, &argc, argv, &jsthis, nullptr);
132130

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

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

141-
bool isAsync;
142-
napi_get_value_bool(env, argv[2], &isAsync);
143-
144139
obj->op = isFinish
145140
? BROTLI_OPERATION_FINISH
146141
: BROTLI_OPERATION_FLUSH;
147142

148143
obj->next_in = nullptr;
149144
obj->available_in = 0;
150145

151-
if (isAsync) {
146+
if (obj->isAsync) {
152147
napi_value resource_name;
153148
napi_create_string_utf8(env, "EncodeResource", NAPI_AUTO_LENGTH, &resource_name);
154149

src/enc/stream_encode.h

+7-6
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,18 @@ class StreamEncode : public StreamCoder {
1111
static napi_value Init(napi_env env, napi_value exports);
1212
static void Destructor(napi_env env, void* nativeObject, void* finalize_hint);
1313

14-
napi_async_work work = NULL;
15-
napi_ref bufref = NULL;
16-
napi_ref cbref = NULL;
14+
bool isAsync = true;
15+
bool hasError = false;
16+
BrotliEncoderState* state;
1717
BrotliEncoderOperation op;
1818
const uint8_t* next_in;
1919
size_t available_in;
20-
BrotliEncoderState* state;
21-
bool hasError = false;
20+
napi_ref bufref = NULL;
21+
napi_ref cbref = NULL;
22+
napi_async_work work = NULL;
2223

2324
private:
24-
explicit StreamEncode(napi_env env, napi_value params);
25+
explicit StreamEncode(napi_env env, napi_value async, napi_value params);
2526
void SetParameter(napi_env env, napi_value params, const char* key, BrotliEncoderParameter p);
2627

2728
static napi_value New(napi_env env, napi_callback_info info);

0 commit comments

Comments
 (0)