Skip to content

Commit

Permalink
rename clear state to invalidate
Browse files Browse the repository at this point in the history
  • Loading branch information
ospfranco committed Dec 17, 2024
1 parent 9034145 commit 6fb0d97
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 43 deletions.
2 changes: 1 addition & 1 deletion android/cpp-adapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ struct OPSQLiteBridge : jni::JavaClass<OPSQLiteBridge> {
}

static void clearStateNativeJsi(jni::alias_ref<jni::JObject> thiz) {
opsqlite::clearState();
opsqlite::invalidate();
}
};

Expand Down
2 changes: 1 addition & 1 deletion android/src/main/java/com/op/sqlite/OPSQLiteBridge.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class OPSQLiteBridge {
)
}

fun clearState() {
fun invalidate() {
clearStateNativeJsi()
}

Expand Down
2 changes: 1 addition & 1 deletion android/src/main/java/com/op/sqlite/OPSQLiteModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ internal class OPSQLiteModule(context: ReactApplicationContext?) : ReactContextB

override fun invalidate() {
super.invalidate()
OPSQLiteBridge.instance.clearState()
OPSQLiteBridge.instance.invalidate()
}

companion object {
Expand Down
5 changes: 4 additions & 1 deletion cpp/DBHostObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -841,7 +841,10 @@ void DBHostObject::set(jsi::Runtime &_rt, const jsi::PropNameID &name,
void DBHostObject::invalidate() {
invalidated = true;
_thread_pool->restartPool();
opsqlite_close(db);
if (db != nullptr) {
opsqlite_close(db);
db = nullptr;
}
}

DBHostObject::~DBHostObject() { invalidate(); }
Expand Down
9 changes: 6 additions & 3 deletions cpp/bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,13 @@ std::vector<std::shared_ptr<DBHostObject>> dbs;

// React native will try to clean the module on JS context invalidation
// (CodePush/Hot Reload) The clearState function is called
void clearState() {
void invalidate() {
for (const auto &db : dbs) {
db->invalidate();
}

// Clear our existing vector of shared pointers so they can be garbage
// collected
dbs.clear();
}

Expand Down Expand Up @@ -75,8 +78,8 @@ void install(jsi::Runtime &rt,
}

std::shared_ptr<DBHostObject> db = std::make_shared<DBHostObject>(
rt, path, invoker, name, path, _crsqlite_path,
_sqlite_vec_path, encryptionKey);
rt, path, invoker, name, path, _crsqlite_path, _sqlite_vec_path,
encryptionKey);
dbs.emplace_back(db);
return jsi::Object::createFromHostObject(rt, db);
});
Expand Down
5 changes: 3 additions & 2 deletions cpp/bindings.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ namespace opsqlite {
namespace jsi = facebook::jsi;
namespace react = facebook::react;

void install(jsi::Runtime &rt, const std::shared_ptr<react::CallInvoker>& invoker,
void install(jsi::Runtime &rt,
const std::shared_ptr<react::CallInvoker> &invoker,
const char *base_path, const char *crsqlite_path,
const char *sqlite_vec_path);
void clearState();
void invalidate();

} // namespace opsqlite
86 changes: 58 additions & 28 deletions cpp/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,34 +14,64 @@ namespace opsqlite {
namespace jsi = facebook::jsi;

jsi::Value toJSI(jsi::Runtime &rt, const JSVariant &value) {
return std::visit(
[&](auto &&v) -> jsi::Value {
using T = std::decay_t<decltype(v)>;
if constexpr (std::is_same_v<T, bool>) {
return jsi::Value(v);
} else if constexpr (std::is_same_v<T, int>) {
return jsi::Value(v);
} else if constexpr (std::is_same_v<T, long long>) {
return jsi::Value(
static_cast<double>(v)); // JSI doesn't support long long
} else if constexpr (std::is_same_v<T, double>) {
return jsi::Value(v);
} else if constexpr (std::is_same_v<T, std::string>) {
return jsi::String::createFromUtf8(rt, v);
} else if constexpr (std::is_same_v<T, ArrayBuffer>) {
static jsi::Function array_buffer_ctor =
rt.global().getPropertyAsFunction(rt, "ArrayBuffer");
jsi::Object o =
array_buffer_ctor.callAsConstructor(rt, static_cast<int>(v.size))
.getObject(rt);
jsi::ArrayBuffer buf = o.getArrayBuffer(rt);
memcpy(buf.data(rt), v.data.get(), v.size);
return o;
} else {
return jsi::Value::null();
}
},
value);
if (std::holds_alternative<bool>(value)) {
return std::get<bool>(value);
} else if (std::holds_alternative<int>(value)) {
return jsi::Value(std::get<int>(value));
} else if (std::holds_alternative<long long>(value)) {
return jsi::Value(static_cast<double>(std::get<long long>(value)));
} else if (std::holds_alternative<double>(value)) {
return jsi::Value(std::get<double>(value));
} else if (std::holds_alternative<std::string>(value)) {
auto str = std::get<std::string>(value);
return jsi::String::createFromUtf8(rt, str);
} else if (std::holds_alternative<ArrayBuffer>(value)) {
auto jsBuffer = std::get<ArrayBuffer>(value);
jsi::Function array_buffer_ctor =
rt.global().getPropertyAsFunction(rt, "ArrayBuffer");
jsi::Object o = array_buffer_ctor.callAsConstructor(rt, (int)jsBuffer.size)
.getObject(rt);
jsi::ArrayBuffer buf = o.getArrayBuffer(rt);
memcpy(buf.data(rt), jsBuffer.data.get(), jsBuffer.size);
return o;
}

return jsi::Value::null();

// I wanted to use the visitor pattern here but on the ArrayBuffer case it is
// somehow throwing a pointer exception Somehow the v.size or v.data.get() is
// loosing the data when called from the lambda I'm guessing the I created the
// shared pointer wrong and the memory is being freed before the lambda is
// called
// return std::visit(
// [&](auto &&v) -> jsi::Value {
// using T = std::decay_t<decltype(v)>;
// if constexpr (std::is_same_v<T, bool>) {
// return jsi::Value(v);
// } else if constexpr (std::is_same_v<T, int>) {
// return jsi::Value(v);
// } else if constexpr (std::is_same_v<T, long long>) {
// return jsi::Value(
// static_cast<double>(v)); // JSI doesn't support long long
// } else if constexpr (std::is_same_v<T, double>) {
// return jsi::Value(v);
// } else if constexpr (std::is_same_v<T, std::string>) {
// return jsi::String::createFromUtf8(rt, v);
// } else if constexpr (std::is_same_v<T, ArrayBuffer>) {
// static jsi::Function buffer_constructor =
// rt.global().getPropertyAsFunction(rt, "ArrayBuffer");
// jsi::Object o =
// buffer_constructor.callAsConstructor(rt,
// static_cast<int>(v.size))
// .getObject(rt);
// jsi::ArrayBuffer buf = o.getArrayBuffer(rt);
// memcpy(buf.data(rt), v.data.get(), v.size);
// return o;
// } else {
// return jsi::Value::null();
// }
// },
// value);
}

JSVariant toVariant(jsi::Runtime &rt, const jsi::Value &value) {
Expand Down
7 changes: 2 additions & 5 deletions example/src/tests/blob.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ export function blobTests() {
beforeEach(async () => {
try {
if (db) {
db.close();
db.delete();
}

Expand All @@ -31,12 +30,10 @@ export function blobTests() {

afterAll(() => {
if (db) {
db.close();
db.delete();
// @ts-ignore
db = null;
}
});

it('ArrayBuffer', async () => {
const uint8 = new Uint8Array(2);
uint8[0] = 42;
Expand All @@ -46,7 +43,7 @@ export function blobTests() {
uint8.buffer,
]);

const result = await db.execute('SELECT content FROM BlobTable');
const result = await db.execute('SELECT content FROM BlobTable;');

const finalUint8 = new Uint8Array(result.rows[0]!.content as any);
expect(finalUint8[0]).to.equal(42);
Expand Down
2 changes: 1 addition & 1 deletion ios/OPSQLite.mm
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ - (NSDictionary *)getConstants {
// #endif

- (void)invalidate {
opsqlite::clearState();
opsqlite::invalidate();
}

@end

0 comments on commit 6fb0d97

Please sign in to comment.