Skip to content

Commit

Permalink
Fix HostObject impl
Browse files Browse the repository at this point in the history
  • Loading branch information
mrousavy committed Feb 20, 2024
1 parent ad36720 commit 48cdbcb
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 35 deletions.
15 changes: 7 additions & 8 deletions package/android/src/main/cpp/AndroidFilamentProxy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,17 @@ namespace margelo {

using namespace facebook;

AndroidFilamentProxy::AndroidFilamentProxy(jni::alias_ref<JFilamentProxy::javaobject> proxy)
: _proxy(jni::make_global(proxy)) {}
AndroidFilamentProxy::AndroidFilamentProxy(jni::alias_ref<JFilamentProxy::javaobject> proxy) : _proxy(jni::make_global(proxy)) {}

AndroidFilamentProxy::~AndroidFilamentProxy() {
// Hermes GC might destroy HostObjects on an arbitrary Thread which might not be
// connected to the JNI environment. To make sure fbjni can properly destroy
// the Java method, we connect to a JNI environment first.
jni::ThreadScope::WithClassLoader([&] { _proxy.reset(); });
// Hermes GC might destroy HostObjects on an arbitrary Thread which might not be
// connected to the JNI environment. To make sure fbjni can properly destroy
// the Java method, we connect to a JNI environment first.
jni::ThreadScope::WithClassLoader([&] { _proxy.reset(); });
}

int AndroidFilamentProxy::loadModel(const std::string &path) {
return _proxy->cthis()->loadModel(path);
int AndroidFilamentProxy::loadModel(const std::string& path) {
return _proxy->cthis()->loadModel(path);
}

} // namespace margelo
8 changes: 4 additions & 4 deletions package/android/src/main/cpp/Filament.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#include <fbjni/fbjni.h>
#include <jni.h>
#include "FilamentInstaller.h"
#include "JFilamentProxy.h"
#include <fbjni/fbjni.h>
#include <jni.h>

JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* vm, void*) {
return facebook::jni::initialize(vm, [] {
margelo::FilamentInstaller::registerNatives();
margelo::JFilamentProxy::registerNatives();
margelo::FilamentInstaller::registerNatives();
margelo::JFilamentProxy::registerNatives();
});
}
17 changes: 8 additions & 9 deletions package/android/src/main/cpp/FilamentInstaller.cpp
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
#include <jni.h>
#include <fbjni/fbjni.h>
#include <jsi/jsi.h>
#include "FilamentInstaller.h"
#include "AndroidFilamentProxy.h"
#include <fbjni/fbjni.h>
#include <jni.h>
#include <jsi/jsi.h>

namespace margelo {

void FilamentInstaller::install(jni::alias_ref<jni::JClass> clazz,
jni::alias_ref<JFilamentProxy::javaobject> proxy) {
jsi::Runtime& runtime = proxy->cthis()->getRuntime();
std::shared_ptr<AndroidFilamentProxy> filamentProxy = std::make_shared<AndroidFilamentProxy>(proxy);
runtime.global().setProperty(runtime, "__filamentProxy", jsi::Object::createFromHostObject(runtime, filamentProxy));
void FilamentInstaller::install(jni::alias_ref<jni::JClass> clazz, jni::alias_ref<JFilamentProxy::javaobject> proxy) {
jsi::Runtime& runtime = proxy->cthis()->getRuntime();
std::shared_ptr<AndroidFilamentProxy> filamentProxy = std::make_shared<AndroidFilamentProxy>(proxy);
runtime.global().setProperty(runtime, "__filamentProxy", jsi::Object::createFromHostObject(runtime, filamentProxy));
}

void FilamentInstaller::registerNatives() {
javaClassStatic()->registerNatives({makeNativeMethod("install", FilamentInstaller::install)});
javaClassStatic()->registerNatives({makeNativeMethod("install", FilamentInstaller::install)});
}

} // namespace margelo
2 changes: 1 addition & 1 deletion package/android/src/main/cpp/FilamentInstaller.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
// Created by Marc Rousavy on 20.02.24.
//

#include "JFilamentProxy.h"
#include <fbjni/fbjni.h>
#include <jni.h>
#include "JFilamentProxy.h"

namespace margelo {

Expand Down
10 changes: 5 additions & 5 deletions package/android/src/main/cpp/java-bindings/JFilamentProxy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ int JFilamentProxy::loadModel(const std::string& path) {
}

jsi::Runtime& JFilamentProxy::getRuntime() {
if (_runtime == nullptr) {
[[unlikely]];
throw std::runtime_error("JSI Runtime was null!");
}
return *_runtime;
if (_runtime == nullptr) {
[[unlikely]];
throw std::runtime_error("JSI Runtime was null!");
}
return *_runtime;
}

void JFilamentProxy::registerNatives() {
Expand Down
10 changes: 8 additions & 2 deletions package/cpp/FilamentProxy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,21 @@ jsi::Value FilamentProxy::get(jsi::Runtime& runtime, const jsi::PropNameID& prop
return jsi::Function::createFromHostFunction(
runtime, jsi::PropNameID::forUtf8(runtime, "loadModel"), 1,
[this](jsi::Runtime& runtime, const jsi::Value& thisValue, const jsi::Value* arguments, size_t count) -> jsi::Value {
if (count != 1) {
throw jsi::JSError(runtime, "loadModel: Expected 1 argument(s), but received " + std::to_string(count) + "!");
}
std::string path = arguments[0].asString(runtime).utf8(runtime);
// TODO(hanno): If this should be sync, we don't need a Promise.
return Promise::createPromise(runtime, [this](std::shared_ptr<Promise> promise) -> {
return Promise::createPromise(runtime, [=](std::shared_ptr<Promise> promise) {
// TODO(hanno): If this should be async, we need to run this on a separate Thread (ask me for a Thread Pool implementation),
// then dispatch back to the JS Thread for safely resolving the Promise using the CallInvoker.
auto model = this->loadModel();
auto model = this->loadModel(path);
promise->resolve(model);
});
});
}

return jsi::Value::undefined();
}

} // namespace margelo
5 changes: 2 additions & 3 deletions package/cpp/FilamentProxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,10 @@ using namespace facebook;

class FilamentProxy : public jsi::HostObject {
public:
virtual ~FilamentProxy();
virtual ~FilamentProxy() = 0;

private:
// TODO(hanno): implement
virtual int loadModel(const std::string& path);
virtual int loadModel(const std::string& path) = 0;

public:
std::vector<jsi::PropNameID> getPropertyNames(jsi::Runtime& runtime) override;
Expand Down
4 changes: 1 addition & 3 deletions package/ios/AppleFilamentProxy.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@

#include "FilamentProxy.h"

class AppleFilamentProxy: public FilamentProxy {

}
class AppleFilamentProxy : public FilamentProxy {}

0 comments on commit 48cdbcb

Please sign in to comment.