Skip to content

Commit

Permalink
Enable dynamic casting of lambda result within container
Browse files Browse the repository at this point in the history
  • Loading branch information
Mooshua committed Aug 12, 2023
1 parent c1ec902 commit f29fbef
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// ========================
// Lilikoi.Tests::AllMethodsCalledHost.cs
// (c) 2023. Distributed under the MIT License
//
//
// -> Created: 22.12.2022
// -> Bumped: 06.02.2023
// ========================
Expand All @@ -11,7 +11,7 @@ public class AllMethodsCalledHost
{
[AllMethodsCalled] public AllMethodsCalledInject Inject;

public object Entry(AllMethodsCalledTest.AllMethodsCalledCounter test, [AllMethodsCalledParameter] object param)
public string Entry(AllMethodsCalledTest.AllMethodsCalledCounter test, [AllMethodsCalledParameter] object param)
{
test.EntryCalled = true;

Expand All @@ -20,6 +20,6 @@ public object Entry(AllMethodsCalledTest.AllMethodsCalledCounter test, [AllMetho
Assert.IsNotNull(Inject);
Assert.IsTrue(Inject.IsNotNull());

return new object();
return "Okay!";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ public void AllMethodsCalled()

Console.WriteLine(build.ToString());

build.Run<AllMethodsCalledCounter, object>(Instance);
var value = build.Run<AllMethodsCalledCounter, string>(Instance);

Assert.NotNull(value);

Assert.IsTrue(Instance.InjectCalled, "Injection was not invoked");
Assert.IsTrue(Instance.EntryCalled, "Entry was not invoked");
Expand Down
7 changes: 4 additions & 3 deletions Lilikoi/Compiler/Mahogany/Generator/WrapGenerator.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// ========================
// Lilikoi::WrapGenerator.cs
// (c) 2023. Distributed under the MIT License
//
//
// -> Created: 22.12.2022
// -> Bumped: 06.02.2023
// ========================
Expand Down Expand Up @@ -68,7 +68,8 @@ internal static Expression Before(MahoganyMethod method, Expression attribute)
setter,
Expression.IfThen(
Expression.IsTrue(Expression.Field(result, WRAPRESULT_STOP)),
Expression.Return(method.HaltTarget, Expression.Field(result, WRAPRESULT_VALUE))
Expression.Return(method.HaltTarget,
Expression.TypeAs( Expression.Field(result, WRAPRESULT_VALUE), method.Return) )
));

return Expression.Block(
Expand Down Expand Up @@ -99,4 +100,4 @@ internal static Expression After(MahoganyMethod method, Expression attribute)
{
return After(attribute, method.Named(MahoganyConstants.MOUNT_VAR), method.Named(MahoganyConstants.OUTPUT_VAR), method.Result);
}
}
}
4 changes: 2 additions & 2 deletions Lilikoi/Compiler/Mahogany/MahoganyMethod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ public LambdaExpression Lambda()
var lambdaBody = Expression.Block(
internalVariables,
Expression.Block(Temporaries.ToArray(), internalBody),
Expression.Return(HaltTarget, Named(MahoganyConstants.OUTPUT_VAR)),
Expression.Label(HaltTarget, Named(MahoganyConstants.OUTPUT_VAR))
Expression.Return(HaltTarget, Expression.TypeAs( Named(MahoganyConstants.OUTPUT_VAR), Return ) ),
Expression.Label(HaltTarget, Expression.TypeAs( Named(MahoganyConstants.OUTPUT_VAR), Return ) )
);

return Expression.Lambda(lambdaBody, "LilikoiContainer", parameters);
Expand Down
2 changes: 2 additions & 0 deletions Lilikoi/Compiler/Public/LilikoiMethod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ public LilikoiMethod Output<TOutput>()
Implementation.Result = typeof(TOutput);
Implementation.NamedVariables.Add(MahoganyConstants.OUTPUT_VAR, Expression.Parameter(typeof(TOutput), MahoganyConstants.OUTPUT_VAR));

if (!Implementation.Result.IsAssignableFrom(Implementation.Return))
throw new InvalidCastException($"Cannot cast to .Output<T>() result of {typeof(TOutput).FullName} from container host return of {Implementation.Return.FullName}");

return this;
}
Expand Down

0 comments on commit f29fbef

Please sign in to comment.