Skip to content
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,27 @@ describe("Test native counterpart release", function () {
expect(function () { global.__releaseNativeCounterpart(instance); }).toThrowError("Trying to release a non native object!");
});

it("wraps a call argument whose link JS released and dropped during the method lookup", function () {
var Caller = com.tns.tests.FreshArgCaller;
var target = new Caller.Target({ take: function () { return "unreachable"; } });
var lookups = 0;
Object.defineProperty(target, "take", {
configurable: true,
get: function () {
lookups++;
(function () {
global.__releaseNativeCounterpart(Caller.pending);
})();
gc();
return function (arg) {
return arg.toString();
};
}
});
expect(Caller.callWithNewObject(target)).toBe("fresh");
expect(lookups).toBe(1);
});

it("Calling the __releaseNativeCounterpart function with 0 arguments should throw exception", function(){
var errorMessage = "";

Expand Down
21 changes: 21 additions & 0 deletions test-app/app/src/main/java/com/tns/tests/FreshArgCaller.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.tns.tests;

// Passes JS an argument Java registers for that call only. The argument is also reachable
// through a field, which JS reads without a method call.
public class FreshArgCaller {
public interface Target {
String take(Object arg);
}

public static Object pending;

public static String callWithNewObject(Target target) {
StringBuilder arg = new StringBuilder("fresh");
pending = arg;
try {
return target.take(arg);
} finally {
pending = null;
}
}
}
3 changes: 3 additions & 0 deletions test-app/runtime/src/main/cpp/ArgConverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,10 @@ void ArgConverter::ConvertJavaArgsToJsArgs(Isolate* isolate, JNIEnv* env, const
// hint Java read without the isolate lock, or -1) and the class index above.
auto hintedObjectID = static_cast<jint>(bits);
auto classIndex = static_cast<int>((bits >> 32) & 0x7fffffff);
// JS run since the call started (the method lookup) may have reached the
// argument another way and released its id; it is then resolved again.
if ((bits & JavaCallArgs::FreshArg) != 0 &&
objectManager->ReleaseCount() == args.releasesAtStart &&
!objectManager->HasJsObject(hintedObjectID)) {
JniLocalRef arg(env->GetObjectArrayElement(args.refs, i));
jsArg = objectManager->CreateJSWrapper(hintedObjectID, classIndex, arg);
Expand Down
2 changes: 2 additions & 0 deletions test-app/runtime/src/main/cpp/ArgConverter.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ struct JavaCallArgs {
// Set once every argument has a JS value, so each object Java registered for this
// call (FreshArg) has a wrapper and nothing is left to release if the call fails.
mutable bool converted = false;
// ObjectManager::ReleaseCount() before any JS ran for this call.
mutable uint64_t releasesAtStart = 0;

// Set in an object argument's packed value when Java registered the object for this
// call; the low half is then its exact id and the next 31 bits its class index.
Expand Down
1 change: 1 addition & 0 deletions test-app/runtime/src/main/cpp/ObjectManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -901,6 +901,7 @@ void ObjectManager::ReleaseNativeCounterpart(v8::Local<v8::Object>& object) {
AssertIsolateLocked();
env.CallVoidMethod(m_javaRuntimeObject, RELEASE_NATIVE_INSTANCE_METHOD_ID,
javaObjectID);
m_releaseCount++;

// The registration outlives the link: it is dropped by the finalizer once
// the wrapper is collected.
Expand Down
5 changes: 5 additions & 0 deletions test-app/runtime/src/main/cpp/ObjectManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ class ObjectManager {
}
}

// Ids JS released explicitly (ReleaseNativeCounterpart). While this does not move, an
// id Java registered for a call is still registered.
uint64_t ReleaseCount() const { return m_releaseCount; }

// Whether a wrapper was ever created for this id and not yet collected.
bool HasJsObject(int javaObjectID) const {
return m_idToObject.Find(javaObjectID) != nullptr;
Expand Down Expand Up @@ -260,6 +264,7 @@ class ObjectManager {
bool m_flushScheduled = false;
// A flush failed; crossings leave the retry to the delayed task.
bool m_flushBackingOff = false;
uint64_t m_releaseCount = 0;

jweak GetJavaObjectByID(uint32_t javaObjectID);

Expand Down
1 change: 1 addition & 0 deletions test-app/runtime/src/main/cpp/Runtime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -634,6 +634,7 @@ jobject Runtime::CallJSMethodNative(JNIEnv* _env, jobject obj,
DEBUG_WRITE("CallJSMethodNative called javaObjectID=%d", javaObjectID);

m_objectManager->FlushFinalizedBatchIfPending();
args.releasesAtStart = m_objectManager->ReleaseCount();

// javaObjectID may be a hint read without the isolate lock; resolve it exactly when it
// does not name a linked wrapper.
Expand Down