Skip to content

Commit a97a4e9

Browse files
committed
fix: keep a Java-created extended instance on its class's current prototype
V8 copies instances of a template from a cached first instantiation, which keeps the prototype the extended class had then: after code reassigns the class's `prototype` property, Java-created instances still got the old one (the new spec fails without this). The wrapper now checks the class's current prototype and only sets it when it differs, which never happens unless the property was reassigned; a profile of the Java-created benchmark shows no SetPrototype calls.
1 parent c118b09 commit a97a4e9

2 files changed

Lines changed: 23 additions & 0 deletions

File tree

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,19 @@ describe("Java object wrappers", function () {
110110
expect(String(made.toString())).toBe("inner");
111111
});
112112

113+
it("give a Java-created extended instance its class's current prototype", function () {
114+
var Created = java.lang.Object.extend({ toString: function () { return "created"; } });
115+
var createdClass = new Created().getClass();
116+
com.tns.tests.JavaCreated.newInstance(createdClass);
117+
var replacement = Object.create(Object.getPrototypeOf(Created.prototype));
118+
Created.prototype = replacement;
119+
120+
var created = com.tns.tests.JavaCreated.newInstance(createdClass);
121+
122+
expect(Object.getPrototypeOf(created)).toBe(replacement);
123+
expect(String(created.toString())).toBe("created");
124+
});
125+
113126
it("hand back the same wrapper for the same Java object", function () {
114127
var builder = new java.lang.StringBuilder();
115128

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,16 @@ Local<Object> MetadataNode::CreateExtendedJSWrapper(Isolate* isolate, ObjectMana
198198
// running its constructor callback.
199199
auto context = Runtime::GetRuntime(isolate)->GetContext();
200200
if (cacheData.instanceTemplate->Get(isolate)->NewInstance(context).ToLocal(&extInstance)) {
201+
// V8 may copy instances from a cached instantiation of the template, which keeps the
202+
// prototype the class had then. This only matters if code reassigns the extended
203+
// class's `prototype` property, which nothing in the runtime or core does; consider
204+
// dropping the check.
205+
auto extdCtorFunc = Local<Function>::New(isolate, *cacheData.extendedCtorFunction);
206+
Local<Value> prototype;
207+
if (extdCtorFunc->Get(context, V8StringConstants::GetPrototype(isolate)).ToLocal(&prototype) &&
208+
!prototype->StrictEquals(extInstance->GetPrototype())) {
209+
extInstance->SetPrototype(context, prototype);
210+
}
201211
extInstance->SetInternalField(static_cast<int>(ObjectManager::MetadataNodeKeys::CallSuper), True(isolate));
202212
SetInstanceMetadata(isolate, extInstance, cacheData.node);
203213
}

0 commit comments

Comments
 (0)