Skip to content

Commit 07c1948

Browse files
committed
fix(runtime): count only released wrappers, and report counters as own properties
releasedAccesses counted a method call on any object without a Java link, such as a plain object a Java method was called on, not only a wrapper whose link was released. getWrapperLifetimeStats() built its result with Set, so a setter on Object.prototype could swallow a counter; it defines own data properties now.
1 parent 3a9df8b commit 07c1948

3 files changed

Lines changed: 28 additions & 3 deletions

File tree

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,26 @@ describe("Wrapper lifetime", function () {
5353
released.hashCode();
5454
}).toThrowError(/no longer has available Java instance counterpart/);
5555
expect(runtime.getWrapperLifetimeStats().releasedAccesses).toBe(before.releasedAccesses + 1);
56+
57+
// Not a wrapper at all: not a released access.
58+
expect(function () {
59+
new java.lang.Object().hashCode.call({});
60+
}).toThrow();
61+
expect(runtime.getWrapperLifetimeStats().releasedAccesses).toBe(before.releasedAccesses + 1);
62+
});
63+
64+
it("reports counters as own properties whatever Object.prototype defines", function () {
65+
Object.defineProperty(Object.prototype, "collected", {
66+
set: function () {},
67+
configurable: true
68+
});
69+
try {
70+
var stats = runtime.getWrapperLifetimeStats();
71+
expect(Object.prototype.hasOwnProperty.call(stats, "collected")).toBe(true);
72+
expect(typeof stats.collected).toBe("number");
73+
} finally {
74+
delete Object.prototype.collected;
75+
}
5676
});
5777

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

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,12 @@ 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();
531+
// A wrapper whose link is gone, not any object a method was called on.
532+
auto receiver = ObjectManager::UnwrapProxy(caller);
533+
if (receiver->IsObject() &&
534+
ObjectManager::IsJsRuntimeObject(receiver.As<Object>())) {
535+
objectManager->CountReleasedAccess();
536+
}
532537
ss << "Failed calling " << methodName << " on a " << className
533538
<< " instance. The JavaScript instance no longer has available Java instance counterpart.";
534539
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,8 @@ void GetWrapperLifetimeStatsCallback(const FunctionCallbackInfo<Value>& info) {
171171
Local<Context> context = isolate->GetCurrentContext();
172172
Local<Object> result = Object::New(isolate);
173173
auto set = [&](const char* name, uint64_t value) {
174-
(void) result->Set(context, ArgConverter::ConvertToV8String(isolate, name),
175-
Number::New(isolate, static_cast<double>(value)));
174+
(void) result->CreateDataProperty(context, ArgConverter::ConvertToV8String(isolate, name),
175+
Number::New(isolate, static_cast<double>(value)));
176176
};
177177
set("resurrections", stats.resurrections);
178178
set("weakened", stats.weakened);

0 commit comments

Comments
 (0)