Skip to content

Commit c775b81

Browse files
committed
Extracted common Node-API queuing code into macro
- this fixes building in debug mode because the compiler (at least, on macOS) doesn't like the `assert(status == 0)` - this also DRYs up some common code by moving it into a macro
1 parent 2595304 commit c775b81

File tree

4 files changed

+24
-56
lines changed

4 files changed

+24
-56
lines changed

src/backup.cc

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -178,12 +178,7 @@ void Backup::Work_BeginInitialize(Database::Baton* baton) {
178178
assert(baton->db->open);
179179
baton->db->pending++;
180180
auto env = baton->db->Env();
181-
int UNUSED(status) = napi_create_async_work(
182-
env, NULL, Napi::String::New(env, "sqlite3.Backup.Initialize"),
183-
Work_Initialize, Work_AfterInitialize, baton, &baton->request
184-
);
185-
assert(status == 0);
186-
napi_queue_async_work(env, baton->request);
181+
CREATE_WORK("sqlite3.Backup.Initialize", Work_Initialize, Work_AfterInitialize);
187182
}
188183

189184
void Backup::Work_Initialize(napi_env e, void* data) {

src/database.cc

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -152,12 +152,7 @@ Database::Database(const Napi::CallbackInfo& info) : Napi::ObjectWrap<Database>(
152152

153153
void Database::Work_BeginOpen(Baton* baton) {
154154
auto env = baton->db->Env();
155-
int UNUSED(status) = napi_create_async_work(
156-
env, NULL, Napi::String::New(env, "sqlite3.Database.Open"),
157-
Work_Open, Work_AfterOpen, baton, &baton->request
158-
);
159-
assert(status == 0);
160-
napi_queue_async_work(env, baton->request);
155+
CREATE_WORK("sqlite3.Database.Open", Work_Open, Work_AfterOpen);
161156
}
162157

163158
void Database::Work_Open(napi_env e, void* data) {
@@ -245,13 +240,7 @@ void Database::Work_BeginClose(Baton* baton) {
245240
baton->db->closing = true;
246241

247242
auto env = baton->db->Env();
248-
249-
int UNUSED(status) = napi_create_async_work(
250-
env, NULL, Napi::String::New(env, "sqlite3.Database.Close"),
251-
Work_Close, Work_AfterClose, baton, &baton->request
252-
);
253-
assert(status == 0);
254-
napi_queue_async_work(env, baton->request);
243+
CREATE_WORK("sqlite3.Database.Close", Work_Close, Work_AfterClose);
255244
}
256245

257246
void Database::Work_Close(napi_env e, void* data) {
@@ -585,13 +574,9 @@ void Database::Work_BeginExec(Baton* baton) {
585574
assert(baton->db->_handle);
586575
assert(baton->db->pending == 0);
587576
baton->db->pending++;
577+
588578
auto env = baton->db->Env();
589-
int UNUSED(status) = napi_create_async_work(
590-
env, NULL, Napi::String::New(env, "sqlite3.Database.Exec"),
591-
Work_Exec, Work_AfterExec, baton, &baton->request
592-
);
593-
assert(status == 0);
594-
napi_queue_async_work(env, baton->request);
579+
CREATE_WORK("sqlite3.Database.Exec", Work_Exec, Work_AfterExec);
595580
}
596581

597582
void Database::Work_Exec(napi_env e, void* data) {
@@ -694,13 +679,9 @@ void Database::Work_BeginLoadExtension(Baton* baton) {
694679
assert(baton->db->_handle);
695680
assert(baton->db->pending == 0);
696681
baton->db->pending++;
682+
697683
auto env = baton->db->Env();
698-
int UNUSED(status) = napi_create_async_work(
699-
env, NULL, Napi::String::New(env, "sqlite3.Database.LoadExtension"),
700-
Work_LoadExtension, Work_AfterLoadExtension, baton, &baton->request
701-
);
702-
assert(status == 0);
703-
napi_queue_async_work(env, baton->request);
684+
CREATE_WORK("sqlite3.Database.LoadExtension", Work_LoadExtension, Work_AfterLoadExtension);
704685
}
705686

706687
void Database::Work_LoadExtension(napi_env e, void* data) {

src/macros.h

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,6 @@ inline bool OtherIsInt(Napi::Number source) {
2323
}
2424
}
2525

26-
#ifdef UNUSED
27-
#elif defined(__GNUC__)
28-
# define UNUSED(x) UNUSED_ ## x __attribute__((unused))
29-
#else
30-
# define UNUSED(x) x
31-
#endif
32-
3326
#define IS_FUNCTION(cb) \
3427
!cb.IsUndefined() && cb.IsFunction()
3528

@@ -144,6 +137,19 @@ inline bool OtherIsInt(Napi::Number source) {
144137
static void Work_##name(napi_env env, void* data); \
145138
static void Work_After##name(napi_env env, napi_status status, void* data);
146139

140+
#ifdef DEBUG
141+
#define ASSERT_STATUS() assert(status == 0);
142+
#else
143+
#define ASSERT_STATUS() (void)status;
144+
#endif
145+
146+
#define CREATE_WORK(name, workerFn, afterFn) \
147+
int status = napi_create_async_work(env, NULL, Napi::String::New(env, name),\
148+
workerFn, afterFn, baton, &baton->request); \
149+
\
150+
ASSERT_STATUS(); \
151+
napi_queue_async_work(env, baton->request);
152+
147153
#define STATEMENT_BEGIN(type) \
148154
assert(baton); \
149155
assert(baton->stmt); \
@@ -153,12 +159,7 @@ inline bool OtherIsInt(Napi::Number source) {
153159
baton->stmt->locked = true; \
154160
baton->stmt->db->pending++; \
155161
auto env = baton->stmt->Env(); \
156-
int UNUSED(status) = napi_create_async_work( \
157-
env, NULL, Napi::String::New(env, "sqlite3.Statement."#type), \
158-
Work_##type, Work_After##type, baton, &baton->request \
159-
); \
160-
assert(status == 0); \
161-
napi_queue_async_work(env, baton->request);
162+
CREATE_WORK("sqlite3.Statement."#type, Work_##type, Work_After##type);
162163

163164
#define STATEMENT_INIT(type) \
164165
type* baton = static_cast<type*>(data); \
@@ -189,12 +190,7 @@ inline bool OtherIsInt(Napi::Number source) {
189190
baton->backup->locked = true; \
190191
baton->backup->db->pending++; \
191192
auto env = baton->backup->Env(); \
192-
int UNUSED(status) = napi_create_async_work( \
193-
env, NULL, Napi::String::New(env, "sqlite3.Backup."#type), \
194-
Work_##type, Work_After##type, baton, &baton->request \
195-
); \
196-
assert(status == 0); \
197-
napi_queue_async_work(env, baton->request);
193+
CREATE_WORK("sqlite3.Backup."#type, Work_##type, Work_After##type);
198194

199195
#define BACKUP_INIT(type) \
200196
type* baton = static_cast<type*>(data); \

src/statement.cc

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -121,13 +121,9 @@ Statement::Statement(const Napi::CallbackInfo& info) : Napi::ObjectWrap<Statemen
121121
void Statement::Work_BeginPrepare(Database::Baton* baton) {
122122
assert(baton->db->open);
123123
baton->db->pending++;
124+
124125
auto env = baton->db->Env();
125-
int UNUSED(status) = napi_create_async_work(
126-
env, NULL, Napi::String::New(env, "sqlite3.Statement.Prepare"),
127-
Work_Prepare, Work_AfterPrepare, baton, &baton->request
128-
);
129-
assert(status == 0);
130-
napi_queue_async_work(env, baton->request);
126+
CREATE_WORK("sqlite3.Statement.Prepare", Work_Prepare, Work_AfterPrepare);
131127
}
132128

133129
void Statement::Work_Prepare(napi_env e, void* data) {

0 commit comments

Comments
 (0)