Skip to content

Commit 08d7ae3

Browse files
committed
fix(runtime): read only an own value from a captured descriptor
An accessor's property descriptor has no own value, so reading one walked to Object.prototype and could run a getter defined there during extend().
1 parent b4e542d commit 08d7ae3

2 files changed

Lines changed: 30 additions & 0 deletions

File tree

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,4 +164,31 @@ describe("Java->JS calls through a function other than the class's own", functio
164164
expect(ownRuns).toBe(1);
165165
expect(warnings("DispatchWarnJavaCreated")).toBe(1);
166166
});
167+
168+
// The getter on Object.prototype.value is installed only around extend(): while it
169+
// is there, Object.defineProperty reads every accessor descriptor as having a value.
170+
it("runs no inherited getter while extend() captures an accessor", function () {
171+
var inheritedReads = 0;
172+
var implementation = {
173+
interfaces: [java.lang.Runnable],
174+
run: function () {}
175+
};
176+
Object.defineProperty(implementation, "state", {
177+
get: function () { return 1; },
178+
enumerable: true,
179+
configurable: true
180+
});
181+
182+
Object.defineProperty(Object.prototype, "value", {
183+
get: function () { inheritedReads++; return undefined; },
184+
configurable: true
185+
});
186+
try {
187+
java.lang.Object.extend("DispatchWarnAccessorCapture", implementation);
188+
} finally {
189+
delete Object.prototype.value;
190+
}
191+
192+
expect(inheritedReads).toBe(0);
193+
});
167194
});

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,14 @@ DispatchOverrides::DispatchOverrides(Isolate* isolate, Local<Object> implementat
4242
Captured entry;
4343
entry.name.Reset(isolate, name.As<String>());
4444

45+
// A descriptor inherits from Object.prototype, and an accessor's has no own
46+
// value: reading it there could run a getter.
4547
Local<Value> descriptor;
4648
Local<Value> value;
4749
if (implementationObject->GetOwnPropertyDescriptor(context, name.As<String>())
4850
.ToLocal(&descriptor) &&
4951
descriptor->IsObject() &&
52+
descriptor.As<Object>()->HasOwnProperty(context, valueKey).FromMaybe(false) &&
5053
descriptor.As<Object>()->Get(context, valueKey).ToLocal(&value) &&
5154
value->IsFunction()) {
5255
entry.function.Reset(isolate, value.As<Function>());

0 commit comments

Comments
 (0)