Skip to content

Commit 4cdb90c

Browse files
committed
feat(runtime): warn on own-property dispatch for interface literals
`new SomeInterface({...})` gives every instance its own implementation object, so it has no per-class record to compare against. For wrappers without a record, debug builds now check whether the receiver itself owns the method name and, if so, log the same warning once per Java class and method per runtime. This costs one own-property lookup per Java->JS call on those instances, in debug builds only; records keep the pointer compare. ObjectManager::GetJavaClass now returns null for an object without a link instead of dereferencing a null info. Specs: a plain interface literal called from Java does not warn; one whose instance gets its own `run` still runs it and warns once over two calls.
1 parent 67e816e commit 4cdb90c

5 files changed

Lines changed: 88 additions & 10 deletions

File tree

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

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// Java->JS calls look the method up on the instance on every call, so an own
22
// property of the instance or a method replaced after extend() is what runs. Debug
33
// builds keep calling it but log a warning once per class and method, since the
4-
// iOS runtime binds the class's methods at extend time instead. Every spec extends
5-
// a class of its own: the warning names the class, and each is logged only once.
4+
// iOS runtime binds the class's methods at extend time instead. Every extend() spec
5+
// uses a class of its own: the warning names the class, and each is logged only once.
66
// Interfaces are named in full so the static binding generator implements them.
77
describe("Java->JS calls through a function other than the class's own", function () {
88
var LogcatUtil = com.tns.tests.LogcatUtil;
@@ -120,6 +120,36 @@ describe("Java->JS calls through a function other than the class's own", functio
120120
expect(warnings("DispatchWarnRepeated")).toBe(1);
121121
});
122122

123+
// Every `new Runnable({...})` shares one Java class, so the spec without an override
124+
// runs first and both count only the warnings their own calls add.
125+
it("calls an interface implementation's method without a warning", function () {
126+
var runs = 0;
127+
var runnable = new java.lang.Runnable({ run: function () { runs++; } });
128+
var className = String(runnable.getClass().getName());
129+
var before = warnings(className);
130+
131+
runFromJava(runnable);
132+
133+
expect(runs).toBe(1);
134+
expect(warnings(className)).toBe(before);
135+
});
136+
137+
it("calls an own property of an interface implementation and warns once", function () {
138+
var implementationRuns = 0;
139+
var ownRuns = 0;
140+
var runnable = new java.lang.Runnable({ run: function () { implementationRuns++; } });
141+
var className = String(runnable.getClass().getName());
142+
var before = warnings(className);
143+
runnable.run = function () { ownRuns++; };
144+
145+
runFromJava(runnable);
146+
runFromJava(runnable);
147+
148+
expect(ownRuns).toBe(2);
149+
expect(implementationRuns).toBe(0);
150+
expect(warnings(className)).toBe(before + 1);
151+
});
152+
123153
it("calls an instance's own property on an instance Java created and warns", function () {
124154
var ownRuns = 0;
125155
var C = java.lang.Object.extend("DispatchWarnJavaCreated", {

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1254,6 +1254,8 @@ Local<Value> CallbackHandlers::CallJSMethod(Isolate *isolate, JNIEnv *_env,
12541254
#ifdef APPLICATION_IN_DEBUG
12551255
if (dispatchOverrides != nullptr) {
12561256
dispatchOverrides->Check(isolate, context, jsObject, methodName, methodNameId, jsMethod);
1257+
} else {
1258+
DispatchOverrides::CheckInstanceProperty(isolate, context, jsObject, methodName, methodNameId, jsMethod);
12571259
}
12581260
#endif
12591261
int argc = args.count;

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

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,14 @@
44

55
#include <android/log.h>
66

7+
#include <algorithm>
8+
#include <set>
79
#include <sstream>
10+
#include <utility>
811

912
#include "ArgConverter.h"
13+
#include "ObjectManager.h"
14+
#include "RuntimeState.h"
1015

1116
using namespace v8;
1217

@@ -88,7 +93,7 @@ void DispatchOverrides::CheckSlow(Isolate* isolate, Local<Context> context,
8893
}
8994

9095
entry.quiet = true;
91-
Warn(isolate, methodName, method);
96+
Warn(className_, methodName, method);
9297
}
9398

9499
bool DispatchOverrides::DefinedAfterExtend(Isolate* isolate, Local<Context> context,
@@ -107,9 +112,42 @@ bool DispatchOverrides::DefinedAfterExtend(Isolate* isolate, Local<Context> cont
107112
return implementationObject_.Get(isolate)->HasRealNamedProperty(context, name).FromMaybe(false);
108113
}
109114

110-
void DispatchOverrides::Warn(Isolate* isolate, Local<String> methodName, Local<Function> method) {
115+
namespace {
116+
// Keyed by jclass: an instance holds its class's global ref from the class cache,
117+
// which has one per class.
118+
struct InstancePropertyWarnings {
119+
std::set<std::pair<jclass, int>> warned;
120+
};
121+
} // namespace
122+
123+
void DispatchOverrides::CheckInstanceProperty(Isolate* isolate, Local<Context> context,
124+
Local<Object> instance, Local<String> methodName,
125+
int methodNameId, Local<Function> method) {
126+
if (methodNameId < 0 ||
127+
!instance->HasRealNamedProperty(context, methodName).FromMaybe(false)) {
128+
return;
129+
}
130+
131+
auto* warnings = RuntimeState::For<InstancePropertyWarnings>(isolate);
132+
auto* runtime = Runtime::TryGetRuntime(isolate);
133+
if (warnings == nullptr || runtime == nullptr) {
134+
return;
135+
}
136+
137+
jclass clazz = runtime->GetObjectManager()->GetJavaClass(instance);
138+
if (!warnings->warned.emplace(clazz, methodNameId).second) {
139+
return;
140+
}
141+
142+
std::string className = clazz != nullptr ? ObjectManager::GetClassName(clazz) : "a Java object";
143+
std::replace(className.begin(), className.end(), '/', '.');
144+
Warn(className, methodName, method);
145+
}
146+
147+
void DispatchOverrides::Warn(const std::string& className, Local<String> methodName,
148+
Local<Function> method) {
111149
std::stringstream ss;
112-
ss << "Java called \"" << ArgConverter::ConvertToString(methodName) << "\" on " << className_
150+
ss << "Java called \"" << ArgConverter::ConvertToString(methodName) << "\" on " << className
113151
<< " through a function other than the class's own implementation (an instance property "
114152
"or a method replaced after extend)";
115153

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,16 @@ class DispatchOverrides {
3232
DispatchOverrides(const DispatchOverrides&) = delete;
3333
DispatchOverrides& operator=(const DispatchOverrides&) = delete;
3434

35+
/*
36+
* For an instance whose class has no record: `new SomeInterface({...})` gives
37+
* each instance its own implementation object, its prototype, so only an own
38+
* property of the instance is reported. Same contract as Check.
39+
*/
40+
static void CheckInstanceProperty(v8::Isolate* isolate, v8::Local<v8::Context> context,
41+
v8::Local<v8::Object> instance,
42+
v8::Local<v8::String> methodName, int methodNameId,
43+
v8::Local<v8::Function> method);
44+
3545
// method is the function the call resolved to on instance. Never throws and
3646
// never runs JS: microtasks would drain before the dispatched method.
3747
void Check(v8::Isolate* isolate, v8::Local<v8::Context> context,
@@ -76,8 +86,8 @@ class DispatchOverrides {
7686
bool DefinedAfterExtend(v8::Isolate* isolate, v8::Local<v8::Context> context,
7787
v8::Local<v8::Object> instance, v8::Local<v8::String> name);
7888

79-
void Warn(v8::Isolate* isolate, v8::Local<v8::String> methodName,
80-
v8::Local<v8::Function> method);
89+
static void Warn(const std::string& className, v8::Local<v8::String> methodName,
90+
v8::Local<v8::Function> method);
8191

8292
std::string className_;
8393
v8::Global<v8::Object> implementationObject_;

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -308,9 +308,7 @@ jclass ObjectManager::GetJavaClass(const Local<Object>& instance) {
308308
DEBUG_WRITE("GetClass called");
309309

310310
JSInstanceInfo* jsInfo = GetJSInstanceInfo(instance);
311-
jclass clazz = jsInfo->objectClazz;
312-
313-
return clazz;
311+
return jsInfo != nullptr ? jsInfo->objectClazz : nullptr;
314312
}
315313

316314
void ObjectManager::SetJavaClass(const Local<Object>& instance, jclass clazz) {

0 commit comments

Comments
 (0)