Skip to content

Commit

Permalink
add try catch to CLR Object Marshaler getStringValue (#10952)
Browse files Browse the repository at this point in the history
* add try catch
add test

* add new class to ffitarget
  • Loading branch information
mjkkirschner authored Jul 30, 2020
1 parent 1da0701 commit 15bdd51
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 16 deletions.
27 changes: 17 additions & 10 deletions src/Engine/ProtoCore/FFI/CLRObjectMarshaler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1256,22 +1256,29 @@ public override string GetStringValue(StackValue dsObject)

if (clrObject != null)
{
if (!DumpXmlProperties)
{
return clrObject.ToString();
}
else
try
{
XmlWriterSettings settings = new XmlWriterSettings { Indent = false, OmitXmlDeclaration = true };
using (StringWriter sw = new StringWriter())
if (!DumpXmlProperties)
{
using (XmlWriter xw = XmlTextWriter.Create(sw, settings))
return clrObject.ToString();
}
else
{
XmlWriterSettings settings = new XmlWriterSettings { Indent = false, OmitXmlDeclaration = true };
using (StringWriter sw = new StringWriter())
{
GeneratePrimaryPropertiesAsXml(clrObject, xw);
using (XmlWriter xw = XmlTextWriter.Create(sw, settings))
{
GeneratePrimaryPropertiesAsXml(clrObject, xw);
}
return sw.ToString();
}
return sw.ToString();
}
}
catch
{
return string.Empty;
}
}
else
{
Expand Down
22 changes: 17 additions & 5 deletions test/Engine/FFITarget/ClassFunctionality.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ public class ClassFunctionality : IDisposable
private int intVal;
public int IntVal {
get { return intVal; }
set {
set {
this.intVal = value;
this.classProperty = new ValueContainer(intVal);
}
}

public ClassFunctionality()
{

}

public ClassFunctionality(int intVal)
Expand Down Expand Up @@ -103,8 +103,8 @@ public void Dispose()
{
StaticProp++;
}


}

public class ValueContainer
Expand All @@ -116,7 +116,7 @@ public ValueContainer(int value)

public ValueContainer Square()
{
return new ValueContainer(SomeValue * SomeValue);
return new ValueContainer(SomeValue * SomeValue);
}

public int SomeValue { get; set; }
Expand Down Expand Up @@ -154,4 +154,16 @@ public void Dispose()
{
}
}

public class ClassWithExceptionToString{

public static ClassWithExceptionToString Construct()
{
return new ClassWithExceptionToString();
}
public override string ToString()
{
throw new NotImplementedException();
}
}
}
15 changes: 14 additions & 1 deletion test/Engine/ProtoTest/FFITests/FFITest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public void T021_Vector_ByCoordinates_1458422_Regress()
thisTest.Verify("vc1Value", 3);
thisTest.Verify("vc2Value", 3);

}
}

[Test]
[Category("SmokeTest")]
Expand Down Expand Up @@ -180,5 +180,18 @@ public void TestDefaultArgumentAttribute()
string expression = o as string;
Assert.IsTrue(expression.Equals("TestData.GetFloat()"));
}

[Test]
public void TestToStringDoesNotThrow()
{
string code = @"
import (ClassWithExceptionToString from ""FFITarget.dll"");
x = ClassWithExceptionToString.Construct();
";
var mirror = thisTest.RunScriptSource(code);
var x = mirror.GetValue("x");
Assert.DoesNotThrow(() => { mirror.GetStringValue(x,this.core.Heap, 0); });

}
}
}

0 comments on commit 15bdd51

Please sign in to comment.