Skip to content

Commit 8fd18a3

Browse files
committed
Extracted function checking code into macro
- done for DRY purposes when reading the code
1 parent 5c94f75 commit 8fd18a3

File tree

3 files changed

+20
-17
lines changed

3 files changed

+20
-17
lines changed

src/database.cc

+9-9
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ void Database::Process() {
5656
queue.pop();
5757
std::unique_ptr<Baton> baton(call->baton);
5858
Napi::Function cb = baton->callback.Value();
59-
if (!cb.IsUndefined() && cb.IsFunction()) {
59+
if (IS_FUNCTION(cb)) {
6060
TRY_CATCH_CALL(this->Value(), cb, 1, argv);
6161
called = true;
6262
}
@@ -97,7 +97,7 @@ void Database::Schedule(Work_Callback callback, Baton* baton, bool exclusive) {
9797
// We don't call the actual callback, so we have to make sure that
9898
// the baton gets destroyed.
9999
delete baton;
100-
if (!cb.IsUndefined() && cb.IsFunction()) {
100+
if (IS_FUNCTION(cb)) {
101101
Napi::Value argv[] = { exception };
102102
TRY_CATCH_CALL(Value(), cb, 1, argv);
103103
}
@@ -202,7 +202,7 @@ void Database::Work_AfterOpen(napi_env e, napi_status status, void* data) {
202202

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

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

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

633-
if (!cb.IsUndefined() && cb.IsFunction()) {
633+
if (IS_FUNCTION(cb)) {
634634
Napi::Value argv[] = { exception };
635635
TRY_CATCH_CALL(db->Value(), cb, 1, argv);
636636
}
@@ -639,7 +639,7 @@ void Database::Work_AfterExec(napi_env e, napi_status status, void* data) {
639639
EMIT_EVENT(db->Value(), 2, info);
640640
}
641641
}
642-
else if (!cb.IsUndefined() && cb.IsFunction()) {
642+
else if (IS_FUNCTION(cb)) {
643643
Napi::Value argv[] = { env.Null() };
644644
TRY_CATCH_CALL(db->Value(), cb, 1, argv);
645645
}
@@ -671,7 +671,7 @@ void Database::Work_Wait(Baton* b) {
671671
assert(baton->db->pending == 0);
672672

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

745-
if (!cb.IsUndefined() && cb.IsFunction()) {
745+
if (IS_FUNCTION(cb)) {
746746
Napi::Value argv[] = { exception };
747747
TRY_CATCH_CALL(db->Value(), cb, 1, argv);
748748
}
@@ -751,7 +751,7 @@ void Database::Work_AfterLoadExtension(napi_env e, napi_status status, void* dat
751751
EMIT_EVENT(db->Value(), 2, info);
752752
}
753753
}
754-
else if (!cb.IsUndefined() && cb.IsFunction()) {
754+
else if (IS_FUNCTION(cb)) {
755755
Napi::Value argv[] = { env.Null() };
756756
TRY_CATCH_CALL(db->Value(), cb, 1, argv);
757757
}

src/macros.h

+3
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ inline bool OtherIsInt(Napi::Number source) {
2323
}
2424
}
2525

26+
#define IS_FUNCTION(cb) \
27+
!cb.IsUndefined() && cb.IsFunction()
28+
2629
#define REQUIRE_ARGUMENTS(n) \
2730
if (info.Length() < (n)) { \
2831
Napi::TypeError::New(env, "Expected " #n "arguments").ThrowAsJavaScriptException(); \

src/statement.cc

+8-8
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ template <class T> void Statement::Error(T* baton) {
7777

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

80-
if (!cb.IsUndefined() && cb.IsFunction()) {
80+
if (IS_FUNCTION(cb)) {
8181
Napi::Value argv[] = { exception };
8282
TRY_CATCH_CALL(stmt->Value(), cb, 1, argv);
8383
}
@@ -375,7 +375,7 @@ void Statement::Work_AfterBind(napi_env e, napi_status status, void* data) {
375375
else {
376376
// Fire callbacks.
377377
Napi::Function cb = baton->callback.Value();
378-
if (!cb.IsUndefined() && cb.IsFunction()) {
378+
if (IS_FUNCTION(cb)) {
379379
Napi::Value argv[] = { env.Null() };
380380
TRY_CATCH_CALL(stmt->Value(), cb, 1, argv);
381381
}
@@ -442,7 +442,7 @@ void Statement::Work_AfterGet(napi_env e, napi_status status, void* data) {
442442
else {
443443
// Fire callbacks.
444444
Napi::Function cb = baton->callback.Value();
445-
if (!cb.IsUndefined() && cb.IsFunction()) {
445+
if (IS_FUNCTION(cb)) {
446446
if (stmt->status == SQLITE_ROW) {
447447
// Create the result array from the data we acquired.
448448
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) {
516516
else {
517517
// Fire callbacks.
518518
Napi::Function cb = baton->callback.Value();
519-
if (!cb.IsUndefined() && cb.IsFunction()) {
519+
if (IS_FUNCTION(cb)) {
520520
(stmt->Value()).Set(Napi::String::New(env, "lastID"), Napi::Number::New(env, baton->inserted_id));
521521
(stmt->Value()).Set( Napi::String::New(env, "changes"), Napi::Number::New(env, baton->changes));
522522

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

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

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

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

894894
// Fire callback in case there was one.
895895
Napi::Function cb = baton->callback.Value();
896-
if (!cb.IsUndefined() && cb.IsFunction()) {
896+
if (IS_FUNCTION(cb)) {
897897
TRY_CATCH_CALL(baton->stmt->Value(), cb, 0, NULL);
898898
}
899899
}

0 commit comments

Comments
 (0)