Skip to content

Commit ffc3eeb

Browse files
authored
Fixes compilation warnings due to macros (#351)
* Fixes compilation warnings due to macros * Update deps * Fix libsql function * Clean up * Fix more libsql issues * Do not make pending queries static * Improve testing script * Fix ios script
1 parent f56ae24 commit ffc3eeb

File tree

14 files changed

+4071
-2547
lines changed

14 files changed

+4071
-2547
lines changed

cpp/DBHostObject.cpp

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ namespace react = facebook::react;
2020
void DBHostObject::flush_pending_reactive_queries(
2121
const std::shared_ptr<jsi::Value> &resolve) {
2222
invoker->invokeAsync(
23-
[this, resolve]() { resolve->asObject(rt).asFunction(rt).call(rt, {}); });
23+
[resolve](jsi::Runtime &rt) { resolve->asObject(rt).asFunction(rt).call(rt, {}); });
2424
}
2525
#else
2626
void DBHostObject::flush_pending_reactive_queries(
@@ -36,8 +36,8 @@ void DBHostObject::flush_pending_reactive_queries(
3636
metadata);
3737

3838
invoker->invokeAsync(
39-
[this, results = std::make_shared<std::vector<DumbHostObject>>(results),
40-
callback = query->callback, metadata, status = std::move(status)] {
39+
[results = std::make_shared<std::vector<DumbHostObject>>(results),
40+
callback = query->callback, metadata, status = std::move(status)](jsi::Runtime &rt) {
4141
auto jsiResult = create_result(rt, status, results.get(), metadata);
4242
callback->asObject(rt).asFunction(rt).call(rt, jsiResult);
4343
});
@@ -46,24 +46,24 @@ void DBHostObject::flush_pending_reactive_queries(
4646
pending_reactive_queries.clear();
4747

4848
invoker->invokeAsync(
49-
[this, resolve]() { resolve->asObject(rt).asFunction(rt).call(rt, {}); });
49+
[resolve](jsi::Runtime &rt) { resolve->asObject(rt).asFunction(rt).call(rt, {}); });
5050
}
5151

5252
void DBHostObject::on_commit() {
5353
invoker->invokeAsync(
54-
[this] { commit_hook_callback->asObject(rt).asFunction(rt).call(rt); });
54+
[this](jsi::Runtime &rt) { commit_hook_callback->asObject(rt).asFunction(rt).call(rt); });
5555
}
5656

5757
void DBHostObject::on_rollback() {
5858
invoker->invokeAsync(
59-
[this] { rollback_hook_callback->asObject(rt).asFunction(rt).call(rt); });
59+
[this](jsi::Runtime &rt) { rollback_hook_callback->asObject(rt).asFunction(rt).call(rt); });
6060
}
6161

6262
void DBHostObject::on_update(const std::string &table,
6363
const std::string &operation, long long row_id) {
6464
if (update_hook_callback != nullptr) {
6565
invoker->invokeAsync(
66-
[this, callback = update_hook_callback, table, operation, row_id] {
66+
[callback = update_hook_callback, table, operation, row_id](jsi::Runtime &rt) {
6767
auto res = jsi::Object(rt);
6868
res.setProperty(rt, "table", jsi::String::createFromUtf8(rt, table));
6969
res.setProperty(rt, "operation",
@@ -143,11 +143,11 @@ void DBHostObject::auto_register_update_hook() {
143143
// Remote connection constructor
144144
DBHostObject::DBHostObject(jsi::Runtime &rt, std::string &url,
145145
std::string &auth_token)
146-
: db_name(url), rt(rt) {
146+
: db_name(url) {
147147
_thread_pool = std::make_shared<ThreadPool>();
148148
db = opsqlite_libsql_open_remote(url, auth_token);
149149

150-
create_jsi_functions();
150+
create_jsi_functions(rt);
151151
}
152152

153153
// Sync connection constructor
@@ -156,15 +156,15 @@ DBHostObject::DBHostObject(jsi::Runtime &rt, std::string &db_name,
156156
std::string &auth_token, int sync_interval,
157157
bool offline, std::string &encryption_key,
158158
std::string &remote_encryption_key)
159-
: db_name(db_name), rt(rt) {
159+
: db_name(db_name) {
160160

161161
_thread_pool = std::make_shared<ThreadPool>();
162162

163163
db =
164164
opsqlite_libsql_open_sync(db_name, path, url, auth_token, sync_interval,
165165
offline, encryption_key, remote_encryption_key);
166166

167-
create_jsi_functions();
167+
create_jsi_functions(rt);
168168
}
169169

170170
#endif
@@ -174,7 +174,7 @@ DBHostObject::DBHostObject(jsi::Runtime &rt, std::string &base_path,
174174
std::string &crsqlite_path,
175175
std::string &sqlite_vec_path,
176176
std::string &encryption_key)
177-
: base_path(base_path), db_name(db_name), rt(rt) {
177+
: base_path(base_path), db_name(db_name) {
178178
_thread_pool = std::make_shared<ThreadPool>();
179179

180180
#ifdef OP_SQLITE_USE_SQLCIPHER
@@ -185,10 +185,10 @@ DBHostObject::DBHostObject(jsi::Runtime &rt, std::string &base_path,
185185
#else
186186
db = opsqlite_open(db_name, path, crsqlite_path, sqlite_vec_path);
187187
#endif
188-
create_jsi_functions();
188+
create_jsi_functions(rt);
189189
};
190190

191-
void DBHostObject::create_jsi_functions() {
191+
void DBHostObject::create_jsi_functions(jsi::Runtime &rt) {
192192
function_map["attach"] = HFN(this) {
193193
std::string secondary_db_path = std::string(base_path);
194194

@@ -427,19 +427,19 @@ void DBHostObject::create_jsi_functions() {
427427
});
428428

429429
#ifdef OP_SQLITE_USE_LIBSQL
430-
function_map["sync"] = HOSTFN("sync") {
430+
function_map["sync"] = HFN(this) {
431431
opsqlite_libsql_sync(db);
432432
return {};
433433
});
434434

435-
function_map["setReservedBytes"] = HOSTFN("setReservedBytes") {
436-
int32_t reserved_bytes = static_cast<int32_t>(args[0].asNumber());
435+
function_map["setReservedBytes"] =HFN(this) {
436+
auto reserved_bytes = static_cast<int32_t>(args[0].asNumber());
437437
opsqlite_libsql_set_reserved_bytes(db, reserved_bytes);
438438
return {};
439439
});
440440

441-
function_map["getReservedBytes"] = HOSTFN("getReservedBytes") {
442-
return jsi::Value(opsqlite_libsql_get_reserved_bytes(db));
441+
function_map["getReservedBytes"] = HFN(this) {
442+
return {opsqlite_libsql_get_reserved_bytes(db)};
443443
});
444444
#else
445445
function_map["loadFile"] = HFN(this) {
@@ -461,7 +461,7 @@ void DBHostObject::create_jsi_functions() {
461461
});
462462
});
463463

464-
function_map["updateHook"] = HOSTFN("updateHook") {
464+
function_map["updateHook"] = HFN(this) {
465465
auto callback = std::make_shared<jsi::Value>(rt, args[0]);
466466

467467
if (callback->isUndefined() || callback->isNull()) {
@@ -474,7 +474,7 @@ void DBHostObject::create_jsi_functions() {
474474
return {};
475475
});
476476

477-
function_map["commitHook"] = HOSTFN("commitHook") {
477+
function_map["commitHook"] = HFN(this) {
478478
if (count < 1) {
479479
throw std::runtime_error("[op-sqlite][commitHook] callback needed");
480480
}
@@ -490,7 +490,7 @@ void DBHostObject::create_jsi_functions() {
490490
return {};
491491
});
492492

493-
function_map["rollbackHook"] = HOSTFN("rollbackHook") {
493+
function_map["rollbackHook"] = HFN(this) {
494494
if (count < 1) {
495495
throw std::runtime_error("[op-sqlite][rollbackHook] callback needed");
496496
}
@@ -507,7 +507,7 @@ void DBHostObject::create_jsi_functions() {
507507
return {};
508508
});
509509

510-
function_map["loadExtension"] = HOSTFN("loadExtension") {
510+
function_map["loadExtension"] = HFN(this) {
511511
auto path = args[0].asString(rt).utf8(rt);
512512
std::string entry_point;
513513
if (count > 1 && args[1].isString()) {
@@ -518,7 +518,7 @@ void DBHostObject::create_jsi_functions() {
518518
return {};
519519
});
520520

521-
function_map["reactiveExecute"] = HOSTFN("reactiveExecute") {
521+
function_map["reactiveExecute"] = HFN(this) {
522522
auto query = args[0].asObject(rt);
523523

524524
const std::string query_str =
@@ -561,7 +561,7 @@ void DBHostObject::create_jsi_functions() {
561561

562562
auto_register_update_hook();
563563

564-
auto unsubscribe = HOSTFN("unsubscribe") {
564+
auto unsubscribe = HFN2(this, reactiveQuery) {
565565
auto it = std::find(reactive_queries.begin(), reactive_queries.end(),
566566
reactiveQuery);
567567
if (it != reactive_queries.end()) {
@@ -575,7 +575,7 @@ void DBHostObject::create_jsi_functions() {
575575
});
576576
#endif
577577

578-
function_map["prepareStatement"] = HOSTFN("prepareStatement") {
578+
function_map["prepareStatement"] = HFN(this) {
579579
auto query = args[0].asString(rt).utf8(rt);
580580
#ifdef OP_SQLITE_USE_LIBSQL
581581
libsql_stmt_t statement = opsqlite_libsql_prepare_statement(db, query);
@@ -588,7 +588,7 @@ void DBHostObject::create_jsi_functions() {
588588
return jsi::Object::createFromHostObject(rt, preparedStatementHostObject);
589589
});
590590

591-
function_map["getDbPath"] = HOSTFN("getDbPath") {
591+
function_map["getDbPath"] = HFN(this) {
592592
std::string path = std::string(base_path);
593593

594594
if (count == 1) {
@@ -639,11 +639,11 @@ std::vector<jsi::PropNameID> DBHostObject::getPropertyNames(jsi::Runtime &_rt) {
639639
return keys;
640640
}
641641

642-
jsi::Value DBHostObject::get(jsi::Runtime &_rt,
642+
jsi::Value DBHostObject::get(jsi::Runtime &rt,
643643
const jsi::PropNameID &propNameID) {
644644
auto name = propNameID.utf8(rt);
645645
if (function_map.count(name) != 1) {
646-
return HOST_STATIC_FN(name.c_str()) {
646+
return HFN(name) {
647647
throw std::runtime_error(
648648
"[op-sqlite] Function " + name +
649649
" not implemented for current backend (libsql or sqlcipher)");

cpp/DBHostObject.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,8 @@ class JSI_EXPORT DBHostObject : public jsi::HostObject {
7373
private:
7474
std::set<std::shared_ptr<ReactiveQuery>> pending_reactive_queries;
7575
void auto_register_update_hook();
76-
void create_jsi_functions();
77-
void
78-
flush_pending_reactive_queries(const std::shared_ptr<jsi::Value> &resolve);
76+
void create_jsi_functions(jsi::Runtime &rt);
77+
void flush_pending_reactive_queries(const std::shared_ptr<jsi::Value> &resolve);
7978

8079
std::unordered_map<std::string, jsi::Value> function_map;
8180
std::string base_path;
@@ -84,7 +83,6 @@ class JSI_EXPORT DBHostObject : public jsi::HostObject {
8483
std::shared_ptr<jsi::Value> update_hook_callback;
8584
std::shared_ptr<jsi::Value> commit_hook_callback;
8685
std::shared_ptr<jsi::Value> rollback_hook_callback;
87-
jsi::Runtime &rt;
8886
std::vector<std::shared_ptr<ReactiveQuery>> reactive_queries;
8987
std::vector<PendingReactiveInvocation> pending_reactive_invocations;
9088
bool is_update_hook_registered = false;

cpp/OPSqlite.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,14 @@ void invalidate() {
4343
}
4444

4545
void install(jsi::Runtime &rt,
46-
const std::shared_ptr<react::CallInvoker> &invoker,
46+
const std::shared_ptr<react::CallInvoker> &_invoker,
4747
const char *base_path, const char *crsqlite_path,
4848
const char *sqlite_vec_path) {
4949

5050
_base_path = std::string(base_path);
5151
_crsqlite_path = std::string(crsqlite_path);
5252
_sqlite_vec_path = std::string(sqlite_vec_path);
53-
opsqlite::invoker = invoker;
53+
opsqlite::invoker = _invoker;
5454
opsqlite::invalidated = false;
5555

5656
auto open = HFN0 {
@@ -85,23 +85,23 @@ void install(jsi::Runtime &rt,
8585
return jsi::Object::createFromHostObject(rt, db);
8686
});
8787

88-
auto is_sqlcipher = HOST_STATIC_FN("isSQLCipher") {
88+
auto is_sqlcipher = HFN(=) {
8989
#ifdef OP_SQLITE_USE_SQLCIPHER
9090
return true;
9191
#else
9292
return false;
9393
#endif
9494
});
9595

96-
auto is_ios_embedded = HOST_STATIC_FN("isIOSEmbedded") {
96+
auto is_ios_embedded = HFN(=) {
9797
#ifdef OP_SQLITE_USE_PHONE_VERSION
9898
return true;
9999
#else
100100
return false;
101101
#endif
102102
});
103103

104-
auto is_libsql = HOST_STATIC_FN("isLibsql") {
104+
auto is_libsql = HFN(=) {
105105
#ifdef OP_SQLITE_USE_LIBSQL
106106
return true;
107107
#else
@@ -110,7 +110,7 @@ void install(jsi::Runtime &rt,
110110
});
111111

112112
#ifdef OP_SQLITE_USE_LIBSQL
113-
auto open_remote = HOST_STATIC_FN("openRemote") {
113+
auto open_remote = HFN(=) {
114114
jsi::Object options = args[0].asObject(rt);
115115

116116
std::string url = options.getProperty(rt, "url").asString(rt).utf8(rt);
@@ -124,7 +124,7 @@ void install(jsi::Runtime &rt,
124124
return jsi::Object::createFromHostObject(rt, db);
125125
});
126126

127-
auto open_sync = HOST_STATIC_FN("openSync") {
127+
auto open_sync = HFN(=) {
128128
jsi::Object options = args[0].asObject(rt);
129129
std::string name = options.getProperty(rt, "name").asString(rt).utf8(rt);
130130
std::string path = std::string(_base_path);
@@ -162,7 +162,7 @@ void install(jsi::Runtime &rt,
162162
if (!location.empty()) {
163163
if (location == ":memory:") {
164164
path = ":memory:";
165-
} else if (location.rfind("/", 0) == 0) {
165+
} else if (location.rfind('/', 0) == 0) {
166166
path = location;
167167
} else {
168168
path = path + "/" + location;

cpp/PreparedStatementHostObject.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ jsi::Value PreparedStatementHostObject::get(jsi::Runtime &rt,
4848
}
4949

5050
if (name == "bindSync") {
51-
return HOSTFN("bindSync") {
51+
return HFN(this) {
5252
if (_stmt == nullptr) {
5353
throw std::runtime_error("statement has been freed");
5454
}
@@ -71,7 +71,7 @@ jsi::Value PreparedStatementHostObject::get(jsi::Runtime &rt,
7171
}
7272

7373
if (name == "execute") {
74-
return HOSTFN("execute") {
74+
return HFN(this) {
7575
if (_stmt == nullptr) {
7676
throw std::runtime_error("statement has been freed");
7777
}

cpp/macros.hpp

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,5 @@
11
#pragma once
22

3-
#define HOSTFN(name) \
4-
jsi::Function::createFromHostFunction( \
5-
rt, \
6-
jsi::PropNameID::forAscii(rt, name), \
7-
0, \
8-
[=, this](jsi::Runtime &rt, const jsi::Value &thisValue, const jsi::Value *args, size_t count) -> jsi::Value
9-
10-
#define HOST_STATIC_FN(name) \
11-
jsi::Function::createFromHostFunction( \
12-
rt, \
13-
jsi::PropNameID::forAscii(rt, name), \
14-
0, \
15-
[=](jsi::Runtime &rt, const jsi::Value &thisValue, const jsi::Value *args, size_t count) -> jsi::Value
16-
173
// Do not unroll into multi lines to avoid Xcode reporting the wrong lines
184
#define HFN0 jsi::Function::createFromHostFunction(rt, jsi::PropNameID::forAscii(rt, ""), 0, [](jsi::Runtime &rt, const jsi::Value &that, const jsi::Value *args, size_t count) -> jsi::Value
195
#define HFN(c1) jsi::Function::createFromHostFunction(rt, jsi::PropNameID::forAscii(rt, ""), 0, [c1](jsi::Runtime &rt, const jsi::Value &that, const jsi::Value *args, size_t count) -> jsi::Value

0 commit comments

Comments
 (0)