Skip to content

Commit

Permalink
Extracted function checking code into macro
Browse files Browse the repository at this point in the history
- done for DRY purposes when reading the code
  • Loading branch information
daniellockyer committed Dec 12, 2022
1 parent 5c94f75 commit 8fd18a3
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 17 deletions.
18 changes: 9 additions & 9 deletions src/database.cc
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ void Database::Process() {
queue.pop();
std::unique_ptr<Baton> baton(call->baton);
Napi::Function cb = baton->callback.Value();
if (!cb.IsUndefined() && cb.IsFunction()) {
if (IS_FUNCTION(cb)) {
TRY_CATCH_CALL(this->Value(), cb, 1, argv);
called = true;
}
Expand Down Expand Up @@ -97,7 +97,7 @@ void Database::Schedule(Work_Callback callback, Baton* baton, bool exclusive) {
// We don't call the actual callback, so we have to make sure that
// the baton gets destroyed.
delete baton;
if (!cb.IsUndefined() && cb.IsFunction()) {
if (IS_FUNCTION(cb)) {
Napi::Value argv[] = { exception };
TRY_CATCH_CALL(Value(), cb, 1, argv);
}
Expand Down Expand Up @@ -202,7 +202,7 @@ void Database::Work_AfterOpen(napi_env e, napi_status status, void* data) {

Napi::Function cb = baton->callback.Value();

if (!cb.IsUndefined() && cb.IsFunction()) {
if (IS_FUNCTION(cb)) {
TRY_CATCH_CALL(db->Value(), cb, 1, argv);
}
else if (!db->open) {
Expand Down Expand Up @@ -294,7 +294,7 @@ void Database::Work_AfterClose(napi_env e, napi_status status, void* data) {
Napi::Function cb = baton->callback.Value();

// Fire callbacks.
if (!cb.IsUndefined() && cb.IsFunction()) {
if (IS_FUNCTION(cb)) {
TRY_CATCH_CALL(db->Value(), cb, 1, argv);
}
else if (db->open) {
Expand Down Expand Up @@ -630,7 +630,7 @@ void Database::Work_AfterExec(napi_env e, napi_status status, void* data) {
if (baton->status != SQLITE_OK) {
EXCEPTION(Napi::String::New(env, baton->message.c_str()), baton->status, exception);

if (!cb.IsUndefined() && cb.IsFunction()) {
if (IS_FUNCTION(cb)) {
Napi::Value argv[] = { exception };
TRY_CATCH_CALL(db->Value(), cb, 1, argv);
}
Expand All @@ -639,7 +639,7 @@ void Database::Work_AfterExec(napi_env e, napi_status status, void* data) {
EMIT_EVENT(db->Value(), 2, info);
}
}
else if (!cb.IsUndefined() && cb.IsFunction()) {
else if (IS_FUNCTION(cb)) {
Napi::Value argv[] = { env.Null() };
TRY_CATCH_CALL(db->Value(), cb, 1, argv);
}
Expand Down Expand Up @@ -671,7 +671,7 @@ void Database::Work_Wait(Baton* b) {
assert(baton->db->pending == 0);

Napi::Function cb = baton->callback.Value();
if (!cb.IsUndefined() && cb.IsFunction()) {
if (IS_FUNCTION(cb)) {
Napi::Value argv[] = { env.Null() };
TRY_CATCH_CALL(baton->db->Value(), cb, 1, argv);
}
Expand Down Expand Up @@ -742,7 +742,7 @@ void Database::Work_AfterLoadExtension(napi_env e, napi_status status, void* dat
if (baton->status != SQLITE_OK) {
EXCEPTION(Napi::String::New(env, baton->message.c_str()), baton->status, exception);

if (!cb.IsUndefined() && cb.IsFunction()) {
if (IS_FUNCTION(cb)) {
Napi::Value argv[] = { exception };
TRY_CATCH_CALL(db->Value(), cb, 1, argv);
}
Expand All @@ -751,7 +751,7 @@ void Database::Work_AfterLoadExtension(napi_env e, napi_status status, void* dat
EMIT_EVENT(db->Value(), 2, info);
}
}
else if (!cb.IsUndefined() && cb.IsFunction()) {
else if (IS_FUNCTION(cb)) {
Napi::Value argv[] = { env.Null() };
TRY_CATCH_CALL(db->Value(), cb, 1, argv);
}
Expand Down
3 changes: 3 additions & 0 deletions src/macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ inline bool OtherIsInt(Napi::Number source) {
}
}

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

#define REQUIRE_ARGUMENTS(n) \
if (info.Length() < (n)) { \
Napi::TypeError::New(env, "Expected " #n "arguments").ThrowAsJavaScriptException(); \
Expand Down
16 changes: 8 additions & 8 deletions src/statement.cc
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ template <class T> void Statement::Error(T* baton) {

Napi::Function cb = baton->callback.Value();

if (!cb.IsUndefined() && cb.IsFunction()) {
if (IS_FUNCTION(cb)) {
Napi::Value argv[] = { exception };
TRY_CATCH_CALL(stmt->Value(), cb, 1, argv);
}
Expand Down Expand Up @@ -375,7 +375,7 @@ void Statement::Work_AfterBind(napi_env e, napi_status status, void* data) {
else {
// Fire callbacks.
Napi::Function cb = baton->callback.Value();
if (!cb.IsUndefined() && cb.IsFunction()) {
if (IS_FUNCTION(cb)) {
Napi::Value argv[] = { env.Null() };
TRY_CATCH_CALL(stmt->Value(), cb, 1, argv);
}
Expand Down Expand Up @@ -442,7 +442,7 @@ void Statement::Work_AfterGet(napi_env e, napi_status status, void* data) {
else {
// Fire callbacks.
Napi::Function cb = baton->callback.Value();
if (!cb.IsUndefined() && cb.IsFunction()) {
if (IS_FUNCTION(cb)) {
if (stmt->status == SQLITE_ROW) {
// Create the result array from the data we acquired.
Napi::Value argv[] = { env.Null(), RowToJS(env, &baton->row) };
Expand Down Expand Up @@ -516,7 +516,7 @@ void Statement::Work_AfterRun(napi_env e, napi_status status, void* data) {
else {
// Fire callbacks.
Napi::Function cb = baton->callback.Value();
if (!cb.IsUndefined() && cb.IsFunction()) {
if (IS_FUNCTION(cb)) {
(stmt->Value()).Set(Napi::String::New(env, "lastID"), Napi::Number::New(env, baton->inserted_id));
(stmt->Value()).Set( Napi::String::New(env, "changes"), Napi::Number::New(env, baton->changes));

Expand Down Expand Up @@ -586,7 +586,7 @@ void Statement::Work_AfterAll(napi_env e, napi_status status, void* data) {
else {
// Fire callbacks.
Napi::Function cb = baton->callback.Value();
if (!cb.IsUndefined() && cb.IsFunction()) {
if (IS_FUNCTION(cb)) {
if (baton->rows.size()) {
// Create the result array from the data we acquired.
Napi::Array result(Napi::Array::New(env, baton->rows.size()));
Expand Down Expand Up @@ -716,7 +716,7 @@ void Statement::AsyncEach(uv_async_t* handle) {
}

Napi::Function cb = async->item_cb.Value();
if (!cb.IsUndefined() && cb.IsFunction()) {
if (IS_FUNCTION(cb)) {
Napi::Value argv[2];
argv[0] = env.Null();

Expand Down Expand Up @@ -791,7 +791,7 @@ void Statement::Work_AfterReset(napi_env e, napi_status status, void* data) {

// Fire callbacks.
Napi::Function cb = baton->callback.Value();
if (!cb.IsUndefined() && cb.IsFunction()) {
if (IS_FUNCTION(cb)) {
Napi::Value argv[] = { env.Null() };
TRY_CATCH_CALL(stmt->Value(), cb, 1, argv);
}
Expand Down Expand Up @@ -893,7 +893,7 @@ void Statement::Finalize_(Baton* b) {

// Fire callback in case there was one.
Napi::Function cb = baton->callback.Value();
if (!cb.IsUndefined() && cb.IsFunction()) {
if (IS_FUNCTION(cb)) {
TRY_CATCH_CALL(baton->stmt->Value(), cb, 0, NULL);
}
}
Expand Down

0 comments on commit 8fd18a3

Please sign in to comment.