Skip to content

Commit 3a9df8b

Browse files
committed
feat(runtime): count method calls on released wrappers as released accesses
1 parent 652d080 commit 3a9df8b

5 files changed

Lines changed: 16 additions & 6 deletions

File tree

‎docs/ns-builtin-modules.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ every build on cold paths only:
184184
| `weakened` | Java objects held weakly because their wrapper was collected |
185185
| `collected` | wrappers released because Java had collected their object |
186186
| `releasedExplicitly` | links released through `__releaseNativeCounterpart` |
187-
| `releasedAccesses` | times JS used a wrapper whose Java object Java had already collected |
187+
| `releasedAccesses` | times JS used a wrapper whose Java object Java had already collected, or called a method on a wrapper whose link was released |
188188

189189
Remote-module security (`security.allowRemoteModules`,
190190
`security.remoteModuleAllowlist`) is **not** part of this surface. Those

‎test-app/app/src/main/assets/app/tests/testWrapperLifetime.js‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,15 @@ describe("Wrapper lifetime", function () {
4444
expect(runtime.getWrapperLifetimeStats()).not.toBe(stats);
4545
});
4646

47-
it("counts explicit releases", function () {
48-
var before = runtime.getWrapperLifetimeStats().releasedExplicitly;
49-
__releaseNativeCounterpart(new java.lang.Object());
50-
expect(runtime.getWrapperLifetimeStats().releasedExplicitly).toBe(before + 1);
47+
it("counts explicit releases, and calls on a released wrapper", function () {
48+
var before = runtime.getWrapperLifetimeStats();
49+
var released = new java.lang.Object();
50+
__releaseNativeCounterpart(released);
51+
expect(runtime.getWrapperLifetimeStats().releasedExplicitly).toBe(before.releasedExplicitly + 1);
52+
expect(function () {
53+
released.hashCode();
54+
}).toThrowError(/no longer has available Java instance counterpart/);
55+
expect(runtime.getWrapperLifetimeStats().releasedAccesses).toBe(before.releasedAccesses + 1);
5156
});
5257

5358
if (mode !== "resurrecting") {

‎test-app/runtime/src/main/cpp/CallbackHandlers.cpp‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,7 @@ void CallbackHandlers::CallJavaMethod(const Local<Object> &caller, const string
528528
ss << "No java object found on which to call \"" << methodName
529529
<< "\" method. It is possible your Javascript object is not linked with the corresponding Java class. Try passing context(this) to the constructor function.";
530530
} else {
531+
objectManager->CountReleasedAccess();
531532
ss << "Failed calling " << methodName << " on a " << className
532533
<< " instance. The JavaScript instance no longer has available Java instance counterpart.";
533534
}

‎test-app/runtime/src/main/cpp/ObjectManager.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,9 @@ class ObjectManager {
160160

161161
const WrapperLifetimeStats& GetLifetimeStats() const { return m_lifetimeStats; }
162162

163+
// JS called a method on a wrapper whose link is gone.
164+
void CountReleasedAccess() { m_lifetimeStats.releasedAccesses++; }
165+
163166
private:
164167
static void InitializeJNI();
165168

‎test-app/runtime/src/main/cpp/WrapperLifetime.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ struct WrapperLifetimeStats {
5151
uint64_t collected = 0;
5252
// Links released through __releaseNativeCounterpart.
5353
uint64_t releasedExplicitly = 0;
54-
// Times JS used a wrapper whose Java object was already collected.
54+
// Times JS used a wrapper whose Java object was already collected, or
55+
// called a method on one whose link was released.
5556
uint64_t releasedAccesses = 0;
5657
};
5758

0 commit comments

Comments
 (0)