Skip to content

Commit 1b7f896

Browse files
committed
fix(runtime): warn when an instance supplies a member the class held no function for
A class member defined as a non-function data property, such as run: null, was exempt like an accessor, so Java calling a function an instance assigned under that name logged nothing. Only accessors stay exempt now.
1 parent 08d7ae3 commit 1b7f896

3 files changed

Lines changed: 34 additions & 10 deletions

File tree

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,22 @@ describe("Java->JS calls through a function other than the class's own", functio
103103
expect(warnings("DispatchWarnPrototypeMethod")).toBe(1);
104104
});
105105

106+
it("calls an instance's function for a member the class defined as null and warns", function () {
107+
var ownRuns = 0;
108+
var C = java.lang.Object.extend("DispatchWarnNullMember", {
109+
interfaces: [java.lang.Runnable],
110+
run: null
111+
});
112+
var instance = new C();
113+
instance.run = function () { ownRuns++; };
114+
115+
runFromJava(instance);
116+
runFromJava(instance);
117+
118+
expect(ownRuns).toBe(2);
119+
expect(warnings("DispatchWarnNullMember")).toBe(1);
120+
});
121+
106122
it("warns once however often and on however many instances the call repeats", function () {
107123
var ownRuns = 0;
108124
var C = java.lang.Object.extend("DispatchWarnRepeated", {

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

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,16 @@ DispatchOverrides::DispatchOverrides(Isolate* isolate, Local<Object> implementat
4545
// A descriptor inherits from Object.prototype, and an accessor's has no own
4646
// value: reading it there could run a getter.
4747
Local<Value> descriptor;
48-
Local<Value> value;
4948
if (implementationObject->GetOwnPropertyDescriptor(context, name.As<String>())
5049
.ToLocal(&descriptor) &&
5150
descriptor->IsObject() &&
52-
descriptor.As<Object>()->HasOwnProperty(context, valueKey).FromMaybe(false) &&
53-
descriptor.As<Object>()->Get(context, valueKey).ToLocal(&value) &&
54-
value->IsFunction()) {
55-
entry.function.Reset(isolate, value.As<Function>());
51+
descriptor.As<Object>()->HasOwnProperty(context, valueKey).FromMaybe(false)) {
52+
entry.dataProperty = true;
53+
Local<Value> value;
54+
if (descriptor.As<Object>()->Get(context, valueKey).ToLocal(&value) &&
55+
value->IsFunction()) {
56+
entry.function.Reset(isolate, value.As<Function>());
57+
}
5658
}
5759

5860
captured_.push_back(std::move(entry));
@@ -79,13 +81,16 @@ void DispatchOverrides::CheckSlow(Isolate* isolate, Local<Context> context,
7981
}
8082

8183
if (captured != nullptr) {
82-
if (captured->function.IsEmpty()) {
84+
if (!captured->dataProperty) {
8385
entry.quiet = true;
8486
return;
8587
}
86-
entry.expected.Reset(isolate, captured->function);
87-
if (entry.expected == method) {
88-
return;
88+
// A data property that held no function never matches what Java calls.
89+
if (!captured->function.IsEmpty()) {
90+
entry.expected.Reset(isolate, captured->function);
91+
if (entry.expected == method) {
92+
return;
93+
}
8994
}
9095
} else if (!DefinedAfterExtend(isolate, context, instance, methodName)) {
9196
// The class does not implement the method, so the call resolved to

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,11 @@ class DispatchOverrides {
6363
private:
6464
struct Captured {
6565
v8::Global<v8::String> name;
66-
// Empty when the property was an accessor or held no function.
66+
// Empty unless the property was a data property holding a function.
6767
v8::Global<v8::Function> function;
68+
// False for an accessor, whose calls are never checked: what they resolve
69+
// to depends on its getter.
70+
bool dataProperty = false;
6871
};
6972

7073
// Indexed by the id Java's MethodNameIds gives the method name; ids are

0 commit comments

Comments
 (0)