Skip to content

Commit

Permalink
Cherry Pick DYN-1476: AllowRankReductionAttribute on Zero Touch Prope…
Browse files Browse the repository at this point in the history
…rties (#9542) (#9549)

* DYN-1476: AllowRankReductionAttribute on Zero Touch seems to be broken (#9542)

* update

* Brought truncated search results and full query search out from behind debug menu. Deleted debug menu items.

* Added CheckForRankReductionAttribute() method to FFIMemberInfo

* Cleaned up CheckForRankReductionAttribute

* Added AllowRankReductionAttribute tests

* Comments. Change method name.

* Update tests, method name

* Corrected test names

* Fixed annoying diff

* DYN-1476 Follow Up: Remove Rank Reduction from Dictionary.Values (#9550)

* update

* Brought truncated search results and full query search out from behind debug menu. Deleted debug menu items.

* Removed AllowRankReduction from Dictionary.Values. Added TODO
  • Loading branch information
scottmitchell authored Mar 8, 2019
1 parent 1746867 commit 655b116
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 2 deletions.
5 changes: 4 additions & 1 deletion src/Engine/ProtoCore/FFI/CLRDLLModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ private ClassDeclNode ParseSystemType(Type type, string alias)
bool isDisposable = typeof(IDisposable).IsAssignableFrom(type);
MethodInfo[] methods = type.GetMethods(flags);
bool hasDisposeMethod = false;

foreach (var m in methods)
{
if (SupressesImport(m, mGetterAttributes))
Expand Down Expand Up @@ -782,7 +782,10 @@ private void RegisterFunctionPointer(string functionName, MemberInfo method, Lis
if (CoreUtils.IsDisposeMethod(functionName))
f = new DisposeFunctionPointer(Module, method, retype);
else if (CoreUtils.IsGetter(functionName))
{
f = new GetterFunctionPointer(Module, functionName, method, retype);
(f as GetterFunctionPointer).ReflectionInfo.CheckForRankReductionAttribute(mGetterAttributes);
}
else
f = new CLRFFIFunctionPointer(Module, functionName, method, argTypes, retype);

Expand Down
25 changes: 25 additions & 0 deletions src/Engine/ProtoCore/FFI/CLRFFIFunctionPointer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,31 @@ protected FFIMemberInfo(MemberInfo info)
Info = info;
}

/// <summary>
/// This method is used to copy the AllowRankReductionAttribute
/// from a Zero Touch property to the property's getter function.
/// </summary>
/// <param name="getterAttributes"></param>
internal void CheckForRankReductionAttribute(Dictionary<MethodInfo, Attribute[]> getterAttributes)
{
var info = Info as MethodInfo;
if (info != null)
{
Attribute[] attributes = null;
if (getterAttributes.TryGetValue(info, out attributes))
{
foreach (var attr in attributes)
{
if (attr is AllowRankReductionAttribute)
{
mAllowRankReduction = true;
mRankReducer = attr as AllowRankReductionAttribute;
}
}
}
}
}

public object ReduceReturnedCollectionToSingleton(object collection)
{
if (null == mAllowRankReduction)
Expand Down
3 changes: 2 additions & 1 deletion src/Libraries/DesignScriptBuiltin/Dictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ public IDictionary<string, object> Components()
/// Produces the values in a Dictionary.
/// </summary>
/// <returns name="values">The values of the Dictionary</returns>
[AllowRankReduction]
// [AllowRankReduction]
// TODO: Consider adding this attribute in 3.0 (DYN-1697)
public IEnumerable<object> Values => D.Values;

/// <summary>
Expand Down
31 changes: 31 additions & 0 deletions test/Engine/FFITarget/TestObjects.cs
Original file line number Diff line number Diff line change
Expand Up @@ -457,4 +457,35 @@ public void Dispose()
}
}

public class TestRankReduce
{
private string RankReduceTestField;

[AllowRankReduction]
public List<string> RankReduceProperty
{
get { return new List<string> { RankReduceTestField }; }
}

public List<string> Property
{
get { return new List<string> { RankReduceTestField }; }
}

public TestRankReduce(string s)
{
RankReduceTestField = s;
}

[AllowRankReduction]
public List<string> RankReduceMethod()
{
return new List<string> { RankReduceTestField };
}

public List<string> Method()
{
return new List<string> { RankReduceTestField };
}
}
}
26 changes: 26 additions & 0 deletions test/Engine/ProtoTest/FFITests/CSFFITest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1387,6 +1387,32 @@ public void InheritenceMethodInvokeWithoutImportDerivedClass()
thisTest.Verify("value", new double[] { 1, 2, 3 });
thisTest.Verify("newPoint", FFITarget.DummyPoint.ByCoordinates(2, 4, 6));
}

[Test]
public void AllowRankReductionAttributeWorksForProperty()
{
string code =
@"import(""FFITarget.dll"");
rankReduceTestObject = FFITarget.TestRankReduce(""test"");
property = rankReduceTestObject.Property;
reducedProperty = rankReduceTestObject.RankReduceProperty; ";
thisTest.RunScriptSource(code);
thisTest.Verify("property", new List<string> { "test" });
thisTest.Verify("reducedProperty", "test");
}

[Test]
public void AllowRankReductionAttributeWorksForMethod()
{
string code =
@"import(""FFITarget.dll"");
rankReduceTestObject = FFITarget.TestRankReduce(""test"");
method = rankReduceTestObject.Method();
reducedMethod = rankReduceTestObject.RankReduceMethod(); ";
thisTest.RunScriptSource(code);
thisTest.Verify("method", new List<string> { "test" });
thisTest.Verify("reducedMethod", "test");
}
}
}

Expand Down

0 comments on commit 655b116

Please sign in to comment.