Skip to content

Commit

Permalink
support enum default values in imported zt methods (#13925)
Browse files Browse the repository at this point in the history
* better error messages and fix some bugs and ui

* comment

* small change

* expose some methods for testing

* support enum default val
  • Loading branch information
mjkkirschner authored Apr 25, 2023
1 parent c35aeee commit a8f45b4
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/Engine/ProtoCore/FFI/CLRDLLModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -848,7 +848,19 @@ private ArgumentSignatureNode ParseArgumentSignature(MethodBase method)
var defaultValue = parameter.DefaultValue;
if (defaultValue != null)
{
var rhs = AstFactory.BuildPrimitiveNodeFromObject(defaultValue);
//param is an enum, which is imported as a class - if we've imported it we can generate a
//an assignment to class.get_field call for a specific enum value.
AssociativeNode rhs = null;
if (parameter.ParameterType.IsEnum)
{
rhs = AstFactory.BuildFunctionCall(parameter.ParameterType.FullName,
$"{ProtoCore.DSASM.Constants.kGetterPrefix}{parameter.ParameterType.GetEnumName(defaultValue)}", new List<AssociativeNode>());
}
else
{
rhs = AstFactory.BuildPrimitiveNodeFromObject(defaultValue);

}
paramNode.NameNode = AstFactory.BuildBinaryExpression(lhs, rhs, ProtoCore.DSASM.Operator.assign);
}
}
Expand Down
16 changes: 16 additions & 0 deletions test/Engine/FFITarget/Enum.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FFITarget
{
public class EnumReferencingClass
{
public string TestEnumDefaultVal(Days day = Days.Friday)
{
return $"{day} is the best";
}
}
}
13 changes: 13 additions & 0 deletions test/Engine/ProtoTest/FFITests/FFITest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -239,5 +239,18 @@ public void Test_FFIImportExceptionContainsNameOfType()
Assert.IsTrue(ex.Message.Contains($"error importing {typeof(FFITarget.HiddenDisposer).FullName}"));

}
[Test]
public void Test_FFI_CanImportEnumDefaultValue()
{
string code = @"
import (EnumReferencingClass from ""FFITarget.dll"");
x = EnumReferencingClass();
val = x.TestEnumDefaultVal();
";

thisTest.RunScriptSource(code);
thisTest.Verify("val", "Friday is the best");
}
}
}

0 comments on commit a8f45b4

Please sign in to comment.