Skip to content

Commit 08d3f04

Browse files
author
Mihail Slavchev
committed
add unit test for issue #221
1 parent c27e977 commit 08d3f04

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

src/jni/ObjectManager.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,14 @@ Local<Object> ObjectManager::CreateJSWrapperHelper(jint javaObjectID, const stri
223223

224224
void ObjectManager::Link(const Local<Object>& object, uint32_t javaObjectID, jclass clazz)
225225
{
226+
int internalFieldCound = NativeScriptExtension::GetInternalFieldCount(object);
227+
const int count = static_cast<int>(MetadataNodeKeys::END);
228+
if (internalFieldCound != count)
229+
{
230+
string errMsg("Trying to link invalid 'this' to a Java object");
231+
throw NativeScriptException(errMsg);
232+
}
233+
226234
auto isolate = Isolate::GetCurrent();
227235

228236
DEBUG_WRITE("Linking js object: %d and java instance id: %d", object->GetIdentityHash(), javaObjectID);

test-app/assets/app/tests/testAsserts.js

+25
Original file line numberDiff line numberDiff line change
@@ -528,4 +528,29 @@ describe("Tests that app does not crashes (no hard-fail asserts)", function () {
528528
}
529529
expect(exceptionThrown).toBe(true);
530530
});
531+
532+
it("should throw an exception when trying to link invalid this to a Java object", function () {
533+
var __extends = function (d, b) {
534+
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
535+
function __() { this.constructor = d; }
536+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
537+
};
538+
var MyObject = (function (_super) {
539+
__extends(MyObject, _super);
540+
function MyObject(name) {
541+
_super.call(this);
542+
return __native(this);
543+
}
544+
return MyObject;
545+
})(java.lang.Object);
546+
547+
var exceptionThrown;
548+
try {
549+
var o = new MyObject();
550+
exceptionThrown = false;
551+
} catch (e) {
552+
exceptionThrown = true;
553+
}
554+
expect(exceptionThrown).toBe(true);
555+
});
531556
});

0 commit comments

Comments
 (0)