Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Plamen5kov/issue #19 #8

Merged
merged 1 commit into from
Feb 2, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/assets/app/mainpage.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ require("./tests/testsForTypescript");
require("./tests/testGC");
require("./tests/testsMemoryManagement");
require("./tests/testIfAbleToRunExternalFile");
require("./tests/finalFieldsSetTests");

var MainActivity = com.tns.NativeScriptActivity.extends({
onCreate: function() {
Expand Down
24 changes: 24 additions & 0 deletions src/assets/app/tests/finalFieldsSetTests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
var Assert = function(condition, failMessage) {
if (condition == false) {
fail(failMessage);
}
}

var When_a_java_final_field_is_set_exception_is_thrown = function() {

var exceptionCaught = false;
var myButton1 = new com.tns.tests.Button1();
try
{
myButton1.STATIC_IMAGE_ID = "NEW STATIC IMAGE ID VALUE";
}
catch (e)
{
Log("Tried to set final field: " + e);
exceptionCaught = true;
}

Assert(exceptionCaught === true, "When_a_java_final_field_is_set_exception_is_thrown FAILED: Exception(illegal access) should be thrown");
}

When_a_java_final_field_is_set_exception_is_thrown();
Binary file modified src/assets/metadata/treeNodeStream.dat
Binary file not shown.
Binary file modified src/assets/metadata/treeStringsStream.dat
Binary file not shown.
Binary file modified src/assets/metadata/treeValueStream.dat
Binary file not shown.
1 change: 1 addition & 0 deletions src/jni/MetadataEntry.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ namespace tns
std::string declaringType;
int paramCount;
bool isStatic;
bool isFinal;
bool isMember;
};
}
Expand Down
26 changes: 21 additions & 5 deletions src/jni/MetadataNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ bool MetadataNode::IsMarkedAsSuper(const Handle<Object>& obj)
Handle<Object> MetadataNode::CreateClassProxy(Isolate *isolate)
{
auto existingClassProxy = GetExistingClassProxy(m_name);

if (!existingClassProxy.IsEmpty())
{
return existingClassProxy;
Expand Down Expand Up @@ -601,6 +602,7 @@ Handle<Object> MetadataNode::GetImplementationObject(const Handle<Object>& objec
void MetadataNode::GetterCallback(Local<String> property, const PropertyCallbackInfo<Value>& info)
{
string propName = ConvertToString(property);

if (propName.empty())
return;

Expand Down Expand Up @@ -1168,6 +1170,7 @@ vector<MetadataEntry> MetadataNode::GetMetadataCandidatesForTypeWithoutCustomMet
curPtr += sizeof(uint8_t) + sizeof(uint32_t);
}

//get candidates from instance methods metadata
uint16_t instanceMethodCout = *reinterpret_cast<uint16_t*>(curPtr);
curPtr += sizeof(uint16_t);
for (int i = 0; i < instanceMethodCout; i++)
Expand All @@ -1181,6 +1184,7 @@ vector<MetadataEntry> MetadataNode::GetMetadataCandidatesForTypeWithoutCustomMet
}
}

//get candidates from static methods metadata
uint16_t staticMethodCout = *reinterpret_cast<uint16_t*>(curPtr);
curPtr += sizeof(uint16_t);
for (int i = 0; i < staticMethodCout; i++)
Expand Down Expand Up @@ -1211,6 +1215,7 @@ vector<MetadataEntry> MetadataNode::GetMetadataCandidatesForTypeWithoutCustomMet
candidates.push_back(extendsEntry);
}

//get candidates from instance fields metadata
uint16_t instanceFieldCout = *reinterpret_cast<uint16_t*>(curPtr);
curPtr += sizeof(uint16_t);
for (int i = 0; i < instanceFieldCout; i++)
Expand All @@ -1224,6 +1229,7 @@ vector<MetadataEntry> MetadataNode::GetMetadataCandidatesForTypeWithoutCustomMet
}
}

//get candidates from static fields metadata
uint16_t staticFieldCout = *reinterpret_cast<uint16_t*>(curPtr);
curPtr += sizeof(uint16_t);
for (int i = 0; i < staticFieldCout; i++)
Expand Down Expand Up @@ -1327,20 +1333,21 @@ Handle<Function> MetadataNode::CreateFunction(const Handle<Object>& thiz, const
void MetadataNode::SetterCallback(Local<String> property, Local<Value> value, const PropertyCallbackInfo<Value>& info)
{
string propName = ConvertToString(property);

if (propName.empty())
return;

auto thiz = info.This();
MetadataNode *node = GetNodeFromHandle(thiz);

//
auto& cache = node->m_childCache;
auto itStart = cache.begin();
auto itEnd = cache.end();
auto itFound = find_if(itStart, itEnd, [&propName] (const MetadataCacheItem& item) { return item.name == propName; } );
if (itFound != itEnd)
{
const auto& cacheItem = *itFound;

if (cacheItem.type == MetadataCacheItemType::Field)
{
bool isStatic = cacheItem.entry.type == NodeType::StaticField;
Expand All @@ -1351,9 +1358,11 @@ void MetadataNode::SetterCallback(Local<String> property, Local<Value> value, co
{
assert(false);
}
return;
if (!cacheItem.entry.isFinal)
{
return;
}
}
//

DEBUG_WRITE("MetadataNode::SetterCallback propName='%s', node='%s', hash=%d", propName.c_str(), node->m_name.c_str(), thiz->GetIdentityHash());

Expand All @@ -1373,12 +1382,19 @@ void MetadataNode::SetterCallback(Local<String> property, Local<Value> value, co

if ((first.type == NodeType::Field) || (first.type == NodeType::StaticField))
{
if(first.isFinal)
{
Isolate *isolate(Isolate::GetCurrent());
Handle<String> exceptionMessage = ConvertToV8String("You are trying to SET a final field! Final fields can only be read.");
Local<Value> IllegalAccessException(exceptionMessage);

isolate->ThrowException(IllegalAccessException);
}
s_setJavaField(thiz, value, node->m_name, first.name, first.sig, first.declaringType, first.isStatic);
//

MetadataCacheItem cacheItem(propName, nullptr, MetadataCacheItemType::Field);
cacheItem.entry = first;
cache.push_back(cacheItem);
//
}
else
{
Expand Down
14 changes: 12 additions & 2 deletions src/jni/MetadataReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,21 +79,32 @@ MetadataEntry MetadataReader::ReadInstanceFieldEntry(uint8_t*& data)
MetadataEntry entry;
uint8_t *curPtr = data;

//read name
uint32_t nameOffset = *reinterpret_cast<uint32_t*>(curPtr);
string name = ReadName(m_nameData, nameOffset);
curPtr += sizeof(uint32_t);

uint16_t *nodeIdPtr = reinterpret_cast<uint16_t*>(curPtr);

// read node pointer id
uint16_t nodeId = *nodeIdPtr++;
string fieldTypeName = ReadTypeName(nodeId);

// read modifier
uint8_t *nodeFinalVarPtr = reinterpret_cast<uint8_t*>(nodeIdPtr);
uint8_t nodeFinalVar = *nodeFinalVarPtr;

if(nodeFinalVar == MetadataTreeNode::FINAL){
entry.isFinal = true;
nodeFinalVarPtr += sizeof(uint8_t);
}

entry.name = name;
entry.type = NodeType::Field;
entry.sig = fieldTypeName;
entry.isMember = true;

data = reinterpret_cast<uint8_t*>(nodeIdPtr);
data = nodeFinalVarPtr;

return entry;
}
Expand All @@ -104,7 +115,6 @@ MetadataEntry MetadataReader::ReadStaticFieldEntry(uint8_t*& data)
MetadataEntry entry = ReadInstanceFieldEntry(data);

uint16_t *nodeIdPtr = reinterpret_cast<uint16_t*>(data);

uint16_t nodeId = *nodeIdPtr++;
string declTypeName = ReadTypeName(nodeId);

Expand Down
1 change: 1 addition & 0 deletions src/jni/MetadataTreeNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ namespace tns
static const uint8_t STATIC = 1 << 2;
static const uint8_t ARRAY = 1 << 3;
static const uint8_t PRIMITIVE = 1 << 4;
static const uint8_t FINAL = 1 << 5;
static const uint8_t PRIMITIVE_BYTE = 1 + PRIMITIVE;
static const uint8_t PRIMITIVE_SHORT = 2 + PRIMITIVE;
static const uint8_t PRIMITIVE_INT = 3 + PRIMITIVE;
Expand Down
11 changes: 10 additions & 1 deletion src/src/com/tns/tests/Button1.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,16 @@ public int getInt()

private boolean IMAGE_ID_BOOL_PROP = false;


public String GetStaticImageId()
{
return STATIC_IMAGE_ID;
}

public static String SGetStaticImageId()
{
return STATIC_IMAGE_ID;
}

public Button1()
{
Log.d(logTag, "Button instance created from javascript");
Expand Down