Skip to content

Commit

Permalink
Return correct type indices for primitive types referred to by their …
Browse files Browse the repository at this point in the history
…CLR type names (#12956)

* return correct type indices for primitive types referred to by their CLR type names

* revert whitespace change only file

* review comments
  • Loading branch information
aparajit-pratap authored Jun 6, 2022
1 parent 03afb60 commit 3561ce9
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/Engine/ProtoCore/Lang/Type.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,15 @@ public class TypeSystem
public Dictionary<ProtoCore.DSASM.AddressType, int> addressTypeClassMap { get; set; }
private static Dictionary<PrimitiveType, string> primitiveTypeNames;

private static readonly Dictionary<string, string> clrToDSTypeMap = new Dictionary<string, string>
{
{ typeof(string).FullName, "string" },
{ typeof(long).FullName, "int" },
{ typeof(double).FullName, "double" },
{ typeof(bool).FullName, "bool" },
{ typeof(char).FullName, "char" }
};

public TypeSystem()
{
SetTypeSystem();
Expand Down Expand Up @@ -314,7 +323,13 @@ public string GetType(int UID)
public int GetType(string ident)
{
Validity.Assert(null != classTable);
return classTable.IndexOf(ident);
var index = classTable.IndexOf(ident);
if (index != Constants.kInvalidIndex) return index;

if(clrToDSTypeMap.TryGetValue(ident, out string dsType))
return classTable.IndexOf(dsType);

return Constants.kInvalidIndex;
}

public int GetType(StackValue sv)
Expand Down
21 changes: 21 additions & 0 deletions test/Engine/ProtoTest/Associative/BuiltinMethodsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,27 @@ public void BIM10_RemoveIfNot()
thisTest.Verify("t", 2);
}

[Test]
//Test "RemoveIfNot()"
public void List_RemoveIfNot_CLRType()
{
String code =
@"import(""BuiltIn.ds"");
a = [""This is "",""a very complex "",""array"",1,2.0,3,false,4.0,5,6.0,true,[2,3.1415926],null,false,'c'];
b = List.RemoveIfNot(a, ""System.Int64"");
c = List.RemoveIfNot(a, ""System.Double"");
d = List.RemoveIfNot(a, ""System.Boolean"");
e = List.RemoveIfNot(a, ""System.Char"");
f = List.RemoveIfNot(a, ""System.String"");
";
ExecutionMirror mirror = thisTest.RunScriptSource(code);
thisTest.Verify("b", new int[] { 1, 3, 5 });
thisTest.Verify("c", new double[] { 2.0, 4.0, 6.0 });
thisTest.Verify("d", new bool[] { false, true, false });
thisTest.Verify("e", new char[] { 'c' });
thisTest.Verify("f", new string[] { "This is ", "a very complex ", "array" });
}

[Test]
//Test "Reverse()"
public void BIM11_Reverse()
Expand Down

0 comments on commit 3561ce9

Please sign in to comment.