From 8fd18a392c4844288811330dddfcf171c18ed93f Mon Sep 17 00:00:00 2001 From: Daniel Lockyer Date: Mon, 12 Dec 2022 13:56:54 +0700 Subject: [PATCH] Extracted function checking code into macro - done for DRY purposes when reading the code --- src/database.cc | 18 +++++++++--------- src/macros.h | 3 +++ src/statement.cc | 16 ++++++++-------- 3 files changed, 20 insertions(+), 17 deletions(-) diff --git a/src/database.cc b/src/database.cc index b16b1f768..15709ce3d 100644 --- a/src/database.cc +++ b/src/database.cc @@ -56,7 +56,7 @@ void Database::Process() { queue.pop(); std::unique_ptr 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; } @@ -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); } @@ -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) { @@ -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) { @@ -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); } @@ -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); } @@ -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); } @@ -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); } @@ -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); } diff --git a/src/macros.h b/src/macros.h index b46a8752a..344642d9d 100644 --- a/src/macros.h +++ b/src/macros.h @@ -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(); \ diff --git a/src/statement.cc b/src/statement.cc index 6adb123f3..f1b835ba2 100644 --- a/src/statement.cc +++ b/src/statement.cc @@ -77,7 +77,7 @@ template 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); } @@ -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); } @@ -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) }; @@ -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)); @@ -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())); @@ -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(); @@ -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); } @@ -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); } }