Skip to content

Commit 0548624

Browse files
author
Mihail Slavchev
committed
fix issue #338
1 parent 37c97cd commit 0548624

4 files changed

Lines changed: 52 additions & 2 deletions

File tree

src/jni/FieldAccessor.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,13 @@ Local<Value> FieldAccessor::GetJavaField(const Local<Object>& target, FieldCallb
6060
if (!isStatic)
6161
{
6262
targetJavaObject = objectManager->GetJavaObjectByJsObject(target);
63+
64+
if (targetJavaObject == nullptr)
65+
{
66+
stringstream ss;
67+
ss << "Cannot access property '" << fieldData->name << "' because there is no corresponding Java object";
68+
throw NativeScriptException(ss.str());
69+
}
6370
}
6471

6572
auto fieldId = fieldData->fid;
@@ -283,6 +290,13 @@ void FieldAccessor::SetJavaField(const Local<Object>& target, const Local<Value>
283290
if (!isStatic)
284291
{
285292
targetJavaObject = objectManager->GetJavaObjectByJsObject(target);
293+
294+
if (targetJavaObject == nullptr)
295+
{
296+
stringstream ss;
297+
ss << "Cannot access property '" << fieldData->name << "' because there is no corresponding Java object";
298+
throw NativeScriptException(ss.str());
299+
}
286300
}
287301

288302
auto fieldId = fieldData->fid;

src/jni/MetadataNode.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,13 @@ void MetadataNode::FieldAccessorGetterCallback(Local<String> property, const Pro
284284
{
285285
auto thiz = info.This();
286286
auto fieldCallbackData = reinterpret_cast<FieldCallbackData*>(info.Data().As<External>()->Value());
287+
288+
if (!fieldCallbackData->isStatic && thiz->StrictEquals(info.Holder()))
289+
{
290+
info.GetReturnValue().SetUndefined();
291+
return;
292+
}
293+
287294
auto value = NativeScriptRuntime::GetJavaField(thiz, fieldCallbackData);
288295
info.GetReturnValue().Set(value);
289296
}
@@ -306,11 +313,15 @@ void MetadataNode::FieldAccessorSetterCallback(Local<String> property, Local<Val
306313
{
307314
try
308315
{
309-
DEBUG_WRITE("FieldAccessorSetterCallback");
310-
311316
auto thiz = info.This();
312317
auto fieldCallbackData = reinterpret_cast<FieldCallbackData*>(info.Data().As<External>()->Value());
313318

319+
if (!fieldCallbackData->isStatic && thiz->StrictEquals(info.Holder()))
320+
{
321+
info.GetReturnValue().SetUndefined();
322+
return;
323+
}
324+
314325
if (fieldCallbackData->isFinal)
315326
{
316327
stringstream ss;

test-app/assets/app/mainpage.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ require("./tests/testJniReferenceLeak");
4040
require("./tests/testNativeModules");
4141
require("./tests/requireExceptionTests");
4242
require("./tests/java-array-test");
43+
require("./tests/field-access-test");
4344

4445
var MainActivity = {
4546
onCreate: function (bundle) {
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
describe("Tests Java object field access", function () {
2+
it("should not crash when Java object protytype is enumerated", function () {
3+
var count = 0;
4+
var obj = android.view.ViewGroup.LayoutParams.prototype;
5+
for(var propertyName in obj) {
6+
__log(">>obj." + propertyName + "=" + obj[propertyName]);
7+
++count;
8+
}
9+
expect(count > 0).toBe(true);
10+
});
11+
12+
it("should not crash when Java property is acceess through plain JavaScritp object", function () {
13+
function createLayoutParams() {}
14+
createLayoutParams.prototype = new android.view.ViewGroup.LayoutParams(1, 2);
15+
var exceptionCaught = false;
16+
try {
17+
var p = createLayoutParams();
18+
p.width = 1;
19+
} catch(e) {
20+
exceptionCaught = true;
21+
}
22+
expect(exceptionCaught).toBe(true);
23+
});
24+
});

0 commit comments

Comments
 (0)