Skip to content

Commit

Permalink
vector common scope
Browse files Browse the repository at this point in the history
Signed-off-by: brocollie08 <[email protected]>
  • Loading branch information
brocollie08 committed Feb 7, 2025
1 parent 9fb37b1 commit 75ed389
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 48 deletions.
6 changes: 1 addition & 5 deletions jvm/hermes/src/main/jni/JHermesRuntime.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,7 @@ class JHermesRuntime : public HybridClass<JHermesRuntime, JJSIRuntime> {
}

void release() {
runtimeScope_->valueScope->clear();
runtimeScope_->objectScope->clear();
runtimeScope_->functionScope->clear();
runtimeScope_->arrayScope->clear();
runtimeScope_->symbolScope->clear();
runtimeScope_->sharedScope->clear();
if (jConfig_) jConfig_.reset();
if (runtime_) runtime_.reset();
}
Expand Down
39 changes: 29 additions & 10 deletions jvm/hermes/src/main/jni/JJSIValue.h
Original file line number Diff line number Diff line change
Expand Up @@ -174,15 +174,19 @@ class JJSIValue : public JJSIValueHybridClass {

Value& get_value() const {
if (!tracked && value_) return *value_;
if (auto lock = weakRef_.lock()) return *lock;
/*if (auto lock = weakRef_.lock()) {
if (auto value = get_if<Value>(&*lock)) {
return *value;
}
}*/

throwNativeHandleReleasedException("Value");
}
private:
friend HybridBase;
shared_ptr<RuntimeScope> scope_;
bool tracked = true;
std::weak_ptr<Value> weakRef_;
std::weak_ptr<VariantType> weakRef_;
std::unique_ptr<Value> value_;
};

Expand Down Expand Up @@ -231,15 +235,19 @@ class JJSIObject : public JJSIObjectHybridClass {
}

Object& get_object() const {
if (auto lock = weakRef_.lock()) return *lock;
/*if (auto lock = weakRef_.lock()) {
if (auto obj = get_if<Object>(&*lock)) {
return *obj;
}
}*/

throwNativeHandleReleasedException("Object");
}
private:
friend HybridBase;
friend class JJSIValue;
std::shared_ptr<RuntimeScope> scope_;
std::weak_ptr<Object> weakRef_;
std::weak_ptr<VariantType> weakRef_;
};

class JJSIArray : public JJSIArrayHybridClass {
Expand Down Expand Up @@ -273,14 +281,18 @@ class JJSIArray : public JJSIArrayHybridClass {
}

Array& get_array() const {
if (auto lock = weakRef_.lock()) return *lock;
/*if (auto lock = weakRef_.lock()) {
if (auto array = get_if<Array>(&*lock)) {
return *array;
}
};*/

throwNativeHandleReleasedException("Array");
}
private:
friend HybridBase;
shared_ptr<RuntimeScope> scope_;
weak_ptr<Array> weakRef_;
weak_ptr<VariantType> weakRef_;
};

struct JJSIHostFunction : JavaClass<JJSIHostFunction> {
Expand Down Expand Up @@ -326,14 +338,17 @@ class JJSIFunction : public JJSIFunctionHybridClass {
}

Function& get_function() const {
if (auto lock = weakRef_.lock()) return *lock;
/*if (auto lock = weakRef_.lock()) {
if (auto function = get_if<Function>(&*lock))
return *function;
}*/

throwNativeHandleReleasedException("Function");
}
private:
friend HybridBase;
shared_ptr<RuntimeScope> scope_;
weak_ptr<Function> weakRef_;
weak_ptr<VariantType> weakRef_;
};

class JJSISymbol : public JJSISymbolHybridClass {
Expand Down Expand Up @@ -365,13 +380,17 @@ class JJSISymbol : public JJSISymbolHybridClass {
}

Symbol& get_symbol() const {
if (auto lock = weakRef_.lock()) return *lock;
/*if (auto lock = weakRef_.lock()) {
if (auto symbol = get_if<Symbol>(&*lock)){
return *symbol;
}
}*/

throwNativeHandleReleasedException("Symbol");
}
private:
friend HybridBase;
shared_ptr<RuntimeScope> scope_;
weak_ptr<Symbol> weakRef_;
weak_ptr<VariantType> weakRef_;
};
};
40 changes: 20 additions & 20 deletions jvm/hermes/src/main/jni/RuntimeScope.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,38 @@

namespace intuit::playerui {

std::weak_ptr<Value> RuntimeScope::trackValue(Value value) {
std::shared_ptr<Value> sp = make_shared<Value>(std::move(value));
valueScope->insert(sp);
std::weak_ptr<Value> wp = sp;
std::weak_ptr<VariantType> RuntimeScope::trackValue(Value value) {
std::shared_ptr<VariantType> sp = make_shared<VariantType>(std::move(value));
sharedScope->push_back(sp);
std::weak_ptr<VariantType> wp = sp;
return wp;
}

std::weak_ptr<Function> RuntimeScope::trackFunction(Function value) {
std::shared_ptr<Function> sp = make_shared<Function>(std::move(value));
functionScope->insert(sp);
std::weak_ptr<Function> wp = sp;
std::weak_ptr<VariantType> RuntimeScope::trackFunction(Function value) {
std::shared_ptr<VariantType> sp = make_shared<VariantType>(std::move(value));
sharedScope->push_back(sp);
std::weak_ptr<VariantType> wp = sp;
return wp;
}

std::weak_ptr<Array> RuntimeScope::trackArray(Array value) {
std::shared_ptr<Array> sp = make_shared<Array>(std::move(value));
arrayScope->insert(sp);
std::weak_ptr<Array> wp = sp;
std::weak_ptr<VariantType> RuntimeScope::trackArray(Array value) {
std::shared_ptr<VariantType> sp = make_shared<VariantType>(std::move(value));
sharedScope->push_back(sp);
std::weak_ptr<VariantType> wp = sp;
return wp;
}

std::weak_ptr<Object> RuntimeScope::trackObject(Object value) {
std::shared_ptr<Object> sp = make_shared<Object>(std::move(value));
objectScope->insert(sp);
std::weak_ptr<Object> wp = sp;
std::weak_ptr<VariantType> RuntimeScope::trackObject(Object value) {
std::shared_ptr<VariantType> sp = make_shared<VariantType>(std::move(value));
sharedScope->push_back(sp);
std::weak_ptr<VariantType> wp = sp;
return wp;
}

std::weak_ptr<Symbol> RuntimeScope::trackSymbol(Symbol value) {
std::shared_ptr<Symbol> sp = make_shared<Symbol>(std::move(value));
symbolScope->insert(sp);
std::weak_ptr<Symbol> wp = sp;
std::weak_ptr<VariantType> RuntimeScope::trackSymbol(Symbol value) {
std::shared_ptr<VariantType> sp = make_shared<VariantType>(std::move(value));
sharedScope->push_back(sp);
std::weak_ptr<VariantType> wp = sp;
return wp;
}

Expand Down
23 changes: 10 additions & 13 deletions jvm/hermes/src/main/jni/RuntimeScope.h
Original file line number Diff line number Diff line change
@@ -1,33 +1,30 @@
#pragma once

#include <jsi/jsi.h>
#include <set>
#include <unordered_map>
#include <vector>
#include <variant>

using namespace std;
using namespace facebook::jsi;

namespace intuit::playerui {

using VariantType = variant<Value, Object, Array, Function, Symbol>;

class RuntimeScope {
public:
unique_ptr<set<shared_ptr<Value>>> valueScope;
unique_ptr<set<shared_ptr<Object>>> objectScope;
unique_ptr<set<shared_ptr<Array>>> arrayScope;
unique_ptr<set<shared_ptr<Function>>> functionScope;
unique_ptr<set<shared_ptr<Symbol>>> symbolScope;
unique_ptr<vector<shared_ptr<VariantType>>> sharedScope;

std::weak_ptr<Value> trackValue(Value value);
std::weak_ptr<VariantType> trackValue(Value value);

std::weak_ptr<Function> trackFunction(Function value);
std::weak_ptr<VariantType> trackFunction(Function value);

std::weak_ptr<Object> trackObject(Object value);
std::weak_ptr<VariantType> trackObject(Object value);

std::weak_ptr<Array> trackArray(Array value);
std::weak_ptr<VariantType> trackArray(Array value);

std::weak_ptr<Symbol> trackSymbol(Symbol value);
std::weak_ptr<VariantType> trackSymbol(Symbol value);

explicit RuntimeScope(): valueScope(make_unique<set<shared_ptr<Value>>>()), objectScope(make_unique<set<shared_ptr<Object>>>()), arrayScope(make_unique<set<shared_ptr<Array>>>()), functionScope(make_unique<set<shared_ptr<Function>>>()), symbolScope(make_unique<set<shared_ptr<Symbol>>>()){}
explicit RuntimeScope(): sharedScope(make_unique<vector<shared_ptr<VariantType>>>()){}
};
}

0 comments on commit 75ed389

Please sign in to comment.