Skip to content

Commit

Permalink
Extracted common Node-API queuing code into macro
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
daniellockyer committed Dec 24, 2023
1 parent 2595304 commit c775b81
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 56 deletions.
7 changes: 1 addition & 6 deletions src/backup.cc
Original file line number Diff line number Diff line change
Expand Up @@ -178,12 +178,7 @@ void Backup::Work_BeginInitialize(Database::Baton* baton) {
assert(baton->db->open);
baton->db->pending++;
auto env = baton->db->Env();
int UNUSED(status) = napi_create_async_work(
env, NULL, Napi::String::New(env, "sqlite3.Backup.Initialize"),
Work_Initialize, Work_AfterInitialize, baton, &baton->request
);
assert(status == 0);
napi_queue_async_work(env, baton->request);
CREATE_WORK("sqlite3.Backup.Initialize", Work_Initialize, Work_AfterInitialize);
}

void Backup::Work_Initialize(napi_env e, void* data) {
Expand Down
31 changes: 6 additions & 25 deletions src/database.cc
Original file line number Diff line number Diff line change
Expand Up @@ -152,12 +152,7 @@ Database::Database(const Napi::CallbackInfo& info) : Napi::ObjectWrap<Database>(

void Database::Work_BeginOpen(Baton* baton) {
auto env = baton->db->Env();
int UNUSED(status) = napi_create_async_work(
env, NULL, Napi::String::New(env, "sqlite3.Database.Open"),
Work_Open, Work_AfterOpen, baton, &baton->request
);
assert(status == 0);
napi_queue_async_work(env, baton->request);
CREATE_WORK("sqlite3.Database.Open", Work_Open, Work_AfterOpen);
}

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

auto env = baton->db->Env();

int UNUSED(status) = napi_create_async_work(
env, NULL, Napi::String::New(env, "sqlite3.Database.Close"),
Work_Close, Work_AfterClose, baton, &baton->request
);
assert(status == 0);
napi_queue_async_work(env, baton->request);
CREATE_WORK("sqlite3.Database.Close", Work_Close, Work_AfterClose);
}

void Database::Work_Close(napi_env e, void* data) {
Expand Down Expand Up @@ -585,13 +574,9 @@ void Database::Work_BeginExec(Baton* baton) {
assert(baton->db->_handle);
assert(baton->db->pending == 0);
baton->db->pending++;

auto env = baton->db->Env();
int UNUSED(status) = napi_create_async_work(
env, NULL, Napi::String::New(env, "sqlite3.Database.Exec"),
Work_Exec, Work_AfterExec, baton, &baton->request
);
assert(status == 0);
napi_queue_async_work(env, baton->request);
CREATE_WORK("sqlite3.Database.Exec", Work_Exec, Work_AfterExec);
}

void Database::Work_Exec(napi_env e, void* data) {
Expand Down Expand Up @@ -694,13 +679,9 @@ void Database::Work_BeginLoadExtension(Baton* baton) {
assert(baton->db->_handle);
assert(baton->db->pending == 0);
baton->db->pending++;

auto env = baton->db->Env();
int UNUSED(status) = napi_create_async_work(
env, NULL, Napi::String::New(env, "sqlite3.Database.LoadExtension"),
Work_LoadExtension, Work_AfterLoadExtension, baton, &baton->request
);
assert(status == 0);
napi_queue_async_work(env, baton->request);
CREATE_WORK("sqlite3.Database.LoadExtension", Work_LoadExtension, Work_AfterLoadExtension);
}

void Database::Work_LoadExtension(napi_env e, void* data) {
Expand Down
34 changes: 15 additions & 19 deletions src/macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,6 @@ inline bool OtherIsInt(Napi::Number source) {
}
}

#ifdef UNUSED
#elif defined(__GNUC__)
# define UNUSED(x) UNUSED_ ## x __attribute__((unused))
#else
# define UNUSED(x) x
#endif

#define IS_FUNCTION(cb) \
!cb.IsUndefined() && cb.IsFunction()

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

#ifdef DEBUG
#define ASSERT_STATUS() assert(status == 0);
#else
#define ASSERT_STATUS() (void)status;
#endif

#define CREATE_WORK(name, workerFn, afterFn) \
int status = napi_create_async_work(env, NULL, Napi::String::New(env, name),\
workerFn, afterFn, baton, &baton->request); \
\
ASSERT_STATUS(); \
napi_queue_async_work(env, baton->request);

#define STATEMENT_BEGIN(type) \
assert(baton); \
assert(baton->stmt); \
Expand All @@ -153,12 +159,7 @@ inline bool OtherIsInt(Napi::Number source) {
baton->stmt->locked = true; \
baton->stmt->db->pending++; \
auto env = baton->stmt->Env(); \
int UNUSED(status) = napi_create_async_work( \
env, NULL, Napi::String::New(env, "sqlite3.Statement."#type), \
Work_##type, Work_After##type, baton, &baton->request \
); \
assert(status == 0); \
napi_queue_async_work(env, baton->request);
CREATE_WORK("sqlite3.Statement."#type, Work_##type, Work_After##type);

#define STATEMENT_INIT(type) \
type* baton = static_cast<type*>(data); \
Expand Down Expand Up @@ -189,12 +190,7 @@ inline bool OtherIsInt(Napi::Number source) {
baton->backup->locked = true; \
baton->backup->db->pending++; \
auto env = baton->backup->Env(); \
int UNUSED(status) = napi_create_async_work( \
env, NULL, Napi::String::New(env, "sqlite3.Backup."#type), \
Work_##type, Work_After##type, baton, &baton->request \
); \
assert(status == 0); \
napi_queue_async_work(env, baton->request);
CREATE_WORK("sqlite3.Backup."#type, Work_##type, Work_After##type);

#define BACKUP_INIT(type) \
type* baton = static_cast<type*>(data); \
Expand Down
8 changes: 2 additions & 6 deletions src/statement.cc
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,9 @@ Statement::Statement(const Napi::CallbackInfo& info) : Napi::ObjectWrap<Statemen
void Statement::Work_BeginPrepare(Database::Baton* baton) {
assert(baton->db->open);
baton->db->pending++;

auto env = baton->db->Env();
int UNUSED(status) = napi_create_async_work(
env, NULL, Napi::String::New(env, "sqlite3.Statement.Prepare"),
Work_Prepare, Work_AfterPrepare, baton, &baton->request
);
assert(status == 0);
napi_queue_async_work(env, baton->request);
CREATE_WORK("sqlite3.Statement.Prepare", Work_Prepare, Work_AfterPrepare);
}

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

0 comments on commit c775b81

Please sign in to comment.