Skip to content

Commit

Permalink
Lint cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
mrousavy committed Sep 1, 2023
1 parent f1bc0ec commit cb52419
Show file tree
Hide file tree
Showing 11 changed files with 768 additions and 858 deletions.
392 changes: 189 additions & 203 deletions android/src/main/cpp/MmkvHostObject.cpp

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions android/src/main/cpp/MmkvHostObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@

#pragma once

#include <jsi/jsi.h>
#include <MMKV.h>
#include <jsi/jsi.h>

using namespace facebook;

class JSI_EXPORT MmkvHostObject: public jsi::HostObject {
class JSI_EXPORT MmkvHostObject : public jsi::HostObject {
public:
MmkvHostObject(const std::string& instanceId, std::string path, std::string cryptKey);

Expand Down
105 changes: 56 additions & 49 deletions android/src/main/cpp/cpp-adapter.cpp
Original file line number Diff line number Diff line change
@@ -1,70 +1,77 @@
#include <jni.h>
#include <jsi/jsi.h>
#include <MMKV.h>
#include "MmkvHostObject.h"
#include "TypedArray.h"
#include <MMKV.h>
#include <jni.h>
#include <jsi/jsi.h>

using namespace facebook;

std::string getPropertyAsStringOrEmptyFromObject(jsi::Object& object, const std::string& propertyName, jsi::Runtime& runtime) {
jsi::Value value = object.getProperty(runtime, propertyName.c_str());
return value.isString() ? value.asString(runtime).utf8(runtime) : "";
std::string getPropertyAsStringOrEmptyFromObject(jsi::Object& object,
const std::string& propertyName,
jsi::Runtime& runtime) {
jsi::Value value = object.getProperty(runtime, propertyName.c_str());
return value.isString() ? value.asString(runtime).utf8(runtime) : "";
}

void install(jsi::Runtime& jsiRuntime) {
// MMKV.createNewInstance()
auto mmkvCreateNewInstance = jsi::Function::createFromHostFunction(jsiRuntime,
jsi::PropNameID::forAscii(jsiRuntime, "mmkvCreateNewInstance"),
1,
[](jsi::Runtime& runtime,
const jsi::Value& thisValue,
const jsi::Value* arguments,
size_t count) -> jsi::Value {
if (count != 1) {
throw jsi::JSError(runtime, "MMKV.createNewInstance(..) expects one argument (object)!");
}
jsi::Object config = arguments[0].asObject(runtime);
// MMKV.createNewInstance()
auto mmkvCreateNewInstance = jsi::Function::createFromHostFunction(
jsiRuntime, jsi::PropNameID::forAscii(jsiRuntime, "mmkvCreateNewInstance"), 1,
[](jsi::Runtime& runtime, const jsi::Value& thisValue, const jsi::Value* arguments,
size_t count) -> jsi::Value {
if (count != 1) {
throw jsi::JSError(runtime, "MMKV.createNewInstance(..) expects one argument (object)!");
}
jsi::Object config = arguments[0].asObject(runtime);

std::string instanceId = getPropertyAsStringOrEmptyFromObject(config, "id", runtime);
std::string path = getPropertyAsStringOrEmptyFromObject(config, "path", runtime);
std::string encryptionKey = getPropertyAsStringOrEmptyFromObject(config, "encryptionKey", runtime);
std::string instanceId = getPropertyAsStringOrEmptyFromObject(config, "id", runtime);
std::string path = getPropertyAsStringOrEmptyFromObject(config, "path", runtime);
std::string encryptionKey =
getPropertyAsStringOrEmptyFromObject(config, "encryptionKey", runtime);

auto instance = std::make_shared<MmkvHostObject>(instanceId, path, encryptionKey);
return jsi::Object::createFromHostObject(runtime, instance);
});
jsiRuntime.global().setProperty(jsiRuntime, "mmkvCreateNewInstance", std::move(mmkvCreateNewInstance));
auto instance = std::make_shared<MmkvHostObject>(instanceId, path, encryptionKey);
return jsi::Object::createFromHostObject(runtime, instance);
});
jsiRuntime.global().setProperty(jsiRuntime, "mmkvCreateNewInstance",
std::move(mmkvCreateNewInstance));

// Adds the PropNameIDCache object to the Runtime. If the Runtime gets destroyed, the Object gets destroyed and the cache gets invalidated.
auto propNameIdCache = std::make_shared<InvalidateCacheOnDestroy>(jsiRuntime);
jsiRuntime.global().setProperty(jsiRuntime, "mmkvArrayBufferPropNameIdCache", jsi::Object::createFromHostObject(jsiRuntime, propNameIdCache));
// Adds the PropNameIDCache object to the Runtime. If the Runtime gets destroyed, the Object gets
// destroyed and the cache gets invalidated.
auto propNameIdCache = std::make_shared<InvalidateCacheOnDestroy>(jsiRuntime);
jsiRuntime.global().setProperty(jsiRuntime, "mmkvArrayBufferPropNameIdCache",
jsi::Object::createFromHostObject(jsiRuntime, propNameIdCache));
}

std::string jstringToStdString(JNIEnv *env, jstring jStr) {
if (!jStr) return "";
std::string jstringToStdString(JNIEnv* env, jstring jStr) {
if (!jStr)
return "";

const auto stringClass = env->GetObjectClass(jStr);
const auto getBytes = env->GetMethodID(stringClass, "getBytes", "(Ljava/lang/String;)[B");
const auto stringJbytes = (jbyteArray) env->CallObjectMethod(jStr, getBytes, env->NewStringUTF("UTF-8"));
const auto stringClass = env->GetObjectClass(jStr);
const auto getBytes = env->GetMethodID(stringClass, "getBytes", "(Ljava/lang/String;)[B");
const auto stringJbytes =
(jbyteArray)env->CallObjectMethod(jStr, getBytes, env->NewStringUTF("UTF-8"));

auto length = (size_t) env->GetArrayLength(stringJbytes);
auto pBytes = env->GetByteArrayElements(stringJbytes, nullptr);
auto length = (size_t)env->GetArrayLength(stringJbytes);
auto pBytes = env->GetByteArrayElements(stringJbytes, nullptr);

std::string ret = std::string((char *)pBytes, length);
env->ReleaseByteArrayElements(stringJbytes, pBytes, JNI_ABORT);
std::string ret = std::string((char*)pBytes, length);
env->ReleaseByteArrayElements(stringJbytes, pBytes, JNI_ABORT);

env->DeleteLocalRef(stringJbytes);
env->DeleteLocalRef(stringClass);
return ret;
env->DeleteLocalRef(stringJbytes);
env->DeleteLocalRef(stringClass);
return ret;
}

extern "C"
JNIEXPORT void JNICALL
Java_com_reactnativemmkv_MmkvModule_nativeInstall(JNIEnv *env, jobject clazz, jlong jsiPtr, jstring path) {
MMKV::initializeMMKV(jstringToStdString(env, path));
extern "C" JNIEXPORT void JNICALL Java_com_reactnativemmkv_MmkvModule_nativeInstall(JNIEnv* env,
jobject clazz,
jlong jsiPtr,
jstring path) {
MMKV::initializeMMKV(jstringToStdString(env, path));

auto runtime = reinterpret_cast<jsi::Runtime*>(jsiPtr);
if (runtime) {
install(*runtime);
}
// if runtime was nullptr, MMKV will not be installed. This should only happen while Remote Debugging (Chrome), but will be weird either way.
auto runtime = reinterpret_cast<jsi::Runtime*>(jsiPtr);
if (runtime) {
install(*runtime);
}
// if runtime was nullptr, MMKV will not be installed. This should only happen while Remote
// Debugging (Chrome), but will be weird either way.
}
Loading

0 comments on commit cb52419

Please sign in to comment.