Skip to content

Commit 655b116

Browse files
Cherry Pick DYN-1476: AllowRankReductionAttribute on Zero Touch Properties (#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
1 parent 1746867 commit 655b116

File tree

5 files changed

+88
-2
lines changed

5 files changed

+88
-2
lines changed

src/Engine/ProtoCore/FFI/CLRDLLModule.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ private ClassDeclNode ParseSystemType(Type type, string alias)
393393
bool isDisposable = typeof(IDisposable).IsAssignableFrom(type);
394394
MethodInfo[] methods = type.GetMethods(flags);
395395
bool hasDisposeMethod = false;
396-
396+
397397
foreach (var m in methods)
398398
{
399399
if (SupressesImport(m, mGetterAttributes))
@@ -782,7 +782,10 @@ private void RegisterFunctionPointer(string functionName, MemberInfo method, Lis
782782
if (CoreUtils.IsDisposeMethod(functionName))
783783
f = new DisposeFunctionPointer(Module, method, retype);
784784
else if (CoreUtils.IsGetter(functionName))
785+
{
785786
f = new GetterFunctionPointer(Module, functionName, method, retype);
787+
(f as GetterFunctionPointer).ReflectionInfo.CheckForRankReductionAttribute(mGetterAttributes);
788+
}
786789
else
787790
f = new CLRFFIFunctionPointer(Module, functionName, method, argTypes, retype);
788791

src/Engine/ProtoCore/FFI/CLRFFIFunctionPointer.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,31 @@ protected FFIMemberInfo(MemberInfo info)
6464
Info = info;
6565
}
6666

67+
/// <summary>
68+
/// This method is used to copy the AllowRankReductionAttribute
69+
/// from a Zero Touch property to the property's getter function.
70+
/// </summary>
71+
/// <param name="getterAttributes"></param>
72+
internal void CheckForRankReductionAttribute(Dictionary<MethodInfo, Attribute[]> getterAttributes)
73+
{
74+
var info = Info as MethodInfo;
75+
if (info != null)
76+
{
77+
Attribute[] attributes = null;
78+
if (getterAttributes.TryGetValue(info, out attributes))
79+
{
80+
foreach (var attr in attributes)
81+
{
82+
if (attr is AllowRankReductionAttribute)
83+
{
84+
mAllowRankReduction = true;
85+
mRankReducer = attr as AllowRankReductionAttribute;
86+
}
87+
}
88+
}
89+
}
90+
}
91+
6792
public object ReduceReturnedCollectionToSingleton(object collection)
6893
{
6994
if (null == mAllowRankReduction)

src/Libraries/DesignScriptBuiltin/Dictionary.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ public IDictionary<string, object> Components()
5858
/// Produces the values in a Dictionary.
5959
/// </summary>
6060
/// <returns name="values">The values of the Dictionary</returns>
61-
[AllowRankReduction]
61+
// [AllowRankReduction]
62+
// TODO: Consider adding this attribute in 3.0 (DYN-1697)
6263
public IEnumerable<object> Values => D.Values;
6364

6465
/// <summary>

test/Engine/FFITarget/TestObjects.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,4 +457,35 @@ public void Dispose()
457457
}
458458
}
459459

460+
public class TestRankReduce
461+
{
462+
private string RankReduceTestField;
463+
464+
[AllowRankReduction]
465+
public List<string> RankReduceProperty
466+
{
467+
get { return new List<string> { RankReduceTestField }; }
468+
}
469+
470+
public List<string> Property
471+
{
472+
get { return new List<string> { RankReduceTestField }; }
473+
}
474+
475+
public TestRankReduce(string s)
476+
{
477+
RankReduceTestField = s;
478+
}
479+
480+
[AllowRankReduction]
481+
public List<string> RankReduceMethod()
482+
{
483+
return new List<string> { RankReduceTestField };
484+
}
485+
486+
public List<string> Method()
487+
{
488+
return new List<string> { RankReduceTestField };
489+
}
490+
}
460491
}

test/Engine/ProtoTest/FFITests/CSFFITest.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1387,6 +1387,32 @@ public void InheritenceMethodInvokeWithoutImportDerivedClass()
13871387
thisTest.Verify("value", new double[] { 1, 2, 3 });
13881388
thisTest.Verify("newPoint", FFITarget.DummyPoint.ByCoordinates(2, 4, 6));
13891389
}
1390+
1391+
[Test]
1392+
public void AllowRankReductionAttributeWorksForProperty()
1393+
{
1394+
string code =
1395+
@"import(""FFITarget.dll"");
1396+
rankReduceTestObject = FFITarget.TestRankReduce(""test"");
1397+
property = rankReduceTestObject.Property;
1398+
reducedProperty = rankReduceTestObject.RankReduceProperty; ";
1399+
thisTest.RunScriptSource(code);
1400+
thisTest.Verify("property", new List<string> { "test" });
1401+
thisTest.Verify("reducedProperty", "test");
1402+
}
1403+
1404+
[Test]
1405+
public void AllowRankReductionAttributeWorksForMethod()
1406+
{
1407+
string code =
1408+
@"import(""FFITarget.dll"");
1409+
rankReduceTestObject = FFITarget.TestRankReduce(""test"");
1410+
method = rankReduceTestObject.Method();
1411+
reducedMethod = rankReduceTestObject.RankReduceMethod(); ";
1412+
thisTest.RunScriptSource(code);
1413+
thisTest.Verify("method", new List<string> { "test" });
1414+
thisTest.Verify("reducedMethod", "test");
1415+
}
13901416
}
13911417
}
13921418

0 commit comments

Comments
 (0)