Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions runtime/src/main/java/com/tns/Runtime.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ void passUncaughtExceptionToJs(Throwable ex, String stackTrace) {
"Primitive types need to be manually wrapped in their respective Object wrappers.\n" +
"If you are creating an instance of an inner class, make sure to always provide reference to the outer `this` as the first argument.";

private final SparseArray<Object> strongInstances = new SparseArray<Object>();
private final HashMap<Integer, Object> strongInstances = new HashMap<>();

private final SparseArray<WeakReference<Object>> weakInstances = new SparseArray<WeakReference<Object>>();
private final HashMap<Integer, WeakReference<Object>> weakInstances = new HashMap<>();

private final NativeScriptHashMap<Object, Integer> strongJavaObjectToID = new NativeScriptHashMap<Object, Integer>();

Expand Down Expand Up @@ -801,7 +801,7 @@ private void makeInstanceWeak(int javaObjectID, boolean keepAsWeak) {
weakInstances.put(javaObjectID, new WeakReference<Object>(instance));
}

strongInstances.delete(javaObjectID);
strongInstances.remove(javaObjectID);
strongJavaObjectToID.remove(instance);
}

Expand Down Expand Up @@ -829,17 +829,17 @@ private boolean makeInstanceWeakAndCheckIfAlive(int javaObjectID) {
if (instance == null) {
// The Java was moved from strong to weak, and then the Java instance was collected.
weakInstances.remove(javaObjectID);
weakJavaObjectToID.remove(Integer.valueOf(javaObjectID));
weakJavaObjectToID.remove(javaObjectID);
return false;
} else {
return true;
}
}
} else {
strongInstances.delete(javaObjectID);
strongInstances.remove(javaObjectID);
strongJavaObjectToID.remove(instance);

weakJavaObjectToID.put(instance, Integer.valueOf(javaObjectID));
weakJavaObjectToID.put(instance, javaObjectID);
weakInstances.put(javaObjectID, new WeakReference<Object>(instance));

return true;
Expand All @@ -862,7 +862,7 @@ private void checkWeakObjectAreAlive(ByteBuffer input, ByteBuffer output, int le

if (instance == null) {
isReleased = 1;
weakInstances.delete(javaObjectId);
weakInstances.remove(javaObjectId);
} else {
isReleased = 0;
}
Expand All @@ -880,7 +880,11 @@ private Object getJavaObjectByID(int javaObjectID) throws Exception {
logger.write("Platform.getJavaObjectByID:" + javaObjectID);
}

Object instance = strongInstances.get(javaObjectID, keyNotFoundObject);
Object instance = strongInstances.get(javaObjectID);

if (instance == null) {
instance = keyNotFoundObject;
}

if (instance == keyNotFoundObject) {
WeakReference<Object> wr = weakInstances.get(javaObjectID);
Expand Down