Skip to content
This repository was archived by the owner on Dec 12, 2024. It is now read-only.

Commit 3c92b75

Browse files
Merge pull request #413 from xamarin/null-check-arguments
Added null checks to arguments
2 parents fa056ef + 8ded69a commit 3c92b75

File tree

8 files changed

+82
-17
lines changed

8 files changed

+82
-17
lines changed

Dynamo/Dynamo.CSLang/CSFunctionCall.cs

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,18 @@ public static CSLine FunctionCallLine (string identifier, bool isConstructor, pa
6767
new CommaListElementCollection<CSBaseExpression> (parameters), isConstructor));
6868
}
6969

70+
static CSIdentifier iNameOf = new CSIdentifier ("nameof");
71+
72+
public static CSFunctionCall Nameof (CSIdentifier id)
73+
{
74+
return FooOf (iNameOf, id);
75+
}
76+
77+
public static CSFunctionCall Nameof (string name)
78+
{
79+
return Nameof (new CSIdentifier (name));
80+
}
81+
7082
static CSIdentifier iTypeof = new CSIdentifier ("typeof");
7183

7284
public static CSFunctionCall Typeof (Type t)
@@ -76,28 +88,27 @@ public static CSFunctionCall Typeof (Type t)
7688

7789
public static CSFunctionCall Typeof (string t)
7890
{
79-
CommaListElementCollection<CSBaseExpression> parms = new CommaListElementCollection<CSBaseExpression> ();
80-
parms.Add (new CSIdentifier (t));
81-
return new CSFunctionCall (iTypeof, parms, false);
91+
return FooOf (iTypeof, new CSIdentifier (t));
8292
}
8393

8494
public static CSFunctionCall Typeof (CSSimpleType t)
8595
{
86-
CommaListElementCollection<CSBaseExpression> parms = new CommaListElementCollection<CSBaseExpression> ();
87-
parms.Add (new CSIdentifier (t.Name));
88-
return new CSFunctionCall (iTypeof, parms, false);
96+
return Typeof (t.Name);
8997
}
9098

91-
9299
static CSIdentifier iSizeof = new CSIdentifier ("sizeof");
93100

94101
public static CSFunctionCall Sizeof (CSBaseExpression expr)
95102
{
96-
CommaListElementCollection<CSBaseExpression> parms = new CommaListElementCollection<CSBaseExpression> ();
97-
parms.Add (expr);
98-
return new CSFunctionCall (iSizeof, parms, false);
103+
return FooOf (iSizeof, expr);
99104
}
100105

106+
static CSFunctionCall FooOf (CSIdentifier foo, CSBaseExpression parameter)
107+
{
108+
CommaListElementCollection<CSBaseExpression> parms = new CommaListElementCollection<CSBaseExpression> ();
109+
parms.Add (parameter);
110+
return new CSFunctionCall (foo, parms, false);
111+
}
101112
}
102113

103114
}

Dynamo/Dynamo.CSLang/CSThrow.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,13 @@ public static CSLine ThrowLine<T> (T exType, string message) where T : Exception
3434
args.Add (CSConstant.Val (message));
3535
return ThrowLine (exType, args);
3636
}
37+
38+
public static CSLine ThrowLine<T>(T exType, CSBaseExpression expr) where T : Exception
39+
{
40+
CommaListElementCollection<CSBaseExpression> args = new CommaListElementCollection<CSBaseExpression> ();
41+
args.Add (Exceptions.ThrowOnNull (expr, nameof (expr)));
42+
return ThrowLine (exType, args);
43+
}
3744
}
3845
}
3946

SwiftReflector/MarshalEngine.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1600,6 +1600,9 @@ CSBaseExpression MarshalNominal (CSParameter p)
16001600
// someCallToAPinvoke(new IntPtr(nameSwiftDataPtr))
16011601
// ...
16021602

1603+
if (p.Name.Name != "this")
1604+
preMarshalCode.Add (ThrowOnNull (p.Name));
1605+
16031606
string nomDataPtrName = Uniqueify (String.Format ("{0}SwiftDataPtr", p.Name.Name), identifiersUsed);
16041607
identifiersUsed.Add (nomDataPtrName);
16051608
var enumDataPtr = new CSIdentifier (nomDataPtrName);
@@ -1612,6 +1615,18 @@ CSBaseExpression MarshalNominal (CSParameter p)
16121615
return new CSCastExpression ("IntPtr", enumDataPtr);
16131616
}
16141617

1618+
CSLine ThrowOnNull (CSIdentifier parameterName)
1619+
{
1620+
// code to generate:
1621+
// if (p == null)
1622+
// throw new ArgumentNullException (nameof (p));
1623+
1624+
var ifTest = parameterName == CSConstant.Null;
1625+
var throwCall = CSThrow.ThrowLine (new ArgumentNullException (), CSFunctionCall.Nameof (parameterName));
1626+
var ifStatement = new CSIfElse (ifTest, CSCodeBlock.Create (throwCall), null);
1627+
return new CSLine (ifStatement, false);
1628+
}
1629+
16151630
CSBaseExpression MarshalClass (CSParameter p, NamedTypeSpec cl)
16161631
{
16171632

@@ -1631,7 +1646,9 @@ CSBaseExpression MarshalClass (CSParameter p, NamedTypeSpec cl)
16311646
// In the second case, we reassign the argument.
16321647

16331648
// I put all the swift_beginAccess/swift_endAccess code into
1634-
// StructMarshal.
1649+
// StructMarshal.
1650+
if (p.Name.Name != "this")
1651+
preMarshalCode.Add (ThrowOnNull (p.Name));
16351652

16361653
var fullClassName = cl.Name;
16371654
var backingFieldAccessor = NewClassCompiler.SafeBackingFieldAccessor (p.Name, use, fullClassName, typeMapper);

SwiftRuntimeLibrary.Mac/SwiftRuntimeLibrary.Mac.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,8 +211,8 @@
211211
<Compile Include="..\SwiftRuntimeLibrary\SwiftCharacter.cs">
212212
<Link>SwiftCharacter.cs</Link>
213213
</Compile>
214-
<Compile Include="..\SwiftRuntimeLibrary\Extensions.cs">
215-
<Link>Extensions.cs</Link>
214+
<Compile Include="..\SwiftRuntimeLibrary\Exceptions.cs">
215+
<Link>Exceptions.cs</Link>
216216
</Compile>
217217
<Compile Include="..\SwiftRuntimeLibrary\SwiftMarshal\ImportedTypeCache.cs">
218218
<Link>SwiftMarshal\ImportedTypeCache.cs</Link>

SwiftRuntimeLibrary.iOS/SwiftRuntimeLibrary.iOS.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@
136136
<Link>SwiftMarshal\DynamicLib.cs</Link>
137137
</Compile>
138138
<Compile Include="..\SwiftRuntimeLibrary\SwiftMarshal\Extensions.cs">
139-
<Link>SwiftMarshal\Extensions.cs</Link>
139+
<Link>SwiftMarshal\Extentions.cs</Link>
140140
</Compile>
141141
<Compile Include="..\SwiftRuntimeLibrary\SwiftMarshal\Memory.cs">
142142
<Link>SwiftMarshal\Memory.cs</Link>
@@ -216,8 +216,8 @@
216216
<Compile Include="..\SwiftRuntimeLibrary\SwiftCharacter.cs">
217217
<Link>SwiftCharacter.cs</Link>
218218
</Compile>
219-
<Compile Include="..\SwiftRuntimeLibrary\Extensions.cs">
220-
<Link>Extensions.cs</Link>
219+
<Compile Include="..\SwiftRuntimeLibrary\Exceptions.cs">
220+
<Link>Exceptions.cs</Link>
221221
</Compile>
222222
<Compile Include="..\SwiftRuntimeLibrary\SwiftMarshal\ImportedTypeCache.cs">
223223
<Link>SwiftMarshal\ImportedTypeCache.cs</Link>

SwiftRuntimeLibrary/SwiftRuntimeLibrary.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@
8888
<Compile Include="SwiftComparableProxy.cs" />
8989
<Compile Include="ISwiftComparable.cs" />
9090
<Compile Include="SwiftCharacter.cs" />
91-
<Compile Include="Extensions.cs" />
91+
<Compile Include="Exceptions.cs" />
9292
<Compile Include="SwiftMarshal\ImportedTypeCache.cs" />
9393
<Compile Include="..\tools\symbolicator\XamGlueConstants.cs">
9494
<Link>XamGlueConstants.cs</Link>

tests/tom-swifty-test/SwiftReflector/NewClassCompilerTests.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1468,6 +1468,36 @@ public init() {}
14681468
var callingCode = CSCodeBlock.Create (printer);
14691469
TestRunning.TestAndExecute (swiftCode, callingCode, "got here\n");
14701470
}
1471+
1472+
[Test]
1473+
public void DontPassMeNull ()
1474+
{
1475+
var swiftCode = @"
1476+
public class EasyToRepresent
1477+
{
1478+
public init () {}
1479+
}
1480+
1481+
public func reportIt (a: EasyToRepresent) -> Bool {
1482+
return true;
1483+
}
1484+
";
1485+
1486+
// try {
1487+
// TopLevelEntities.ReportIt (null);
1488+
// } catch (ArgumentNullException err) {
1489+
// Console.WriteLine("Here." + err.Message);
1490+
// }
1491+
1492+
var errID = new CSIdentifier ("err");
1493+
var callIt = CSFunctionCall.FunctionCallLine ("TopLevelEntities.ReportIt", false, CSConstant.Null);
1494+
var printer = CSFunctionCall.ConsoleWriteLine (CSConstant.Val ("Here.") + errID.Dot(new CSIdentifier ("Message")));
1495+
var tryCatch = new CSTryCatch (CSCodeBlock.Create (callIt),
1496+
typeof (ArgumentNullException), errID.Name, CSCodeBlock.Create (printer));
1497+
var callingCode = CSCodeBlock.Create (tryCatch);
1498+
1499+
TestRunning.TestAndExecute (swiftCode, callingCode, "Here.Value cannot be null.\nParameter name: a\n");
1500+
}
14711501
}
14721502
}
14731503

0 commit comments

Comments
 (0)