Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

box expression is bad #43

Closed
Louis-J opened this issue Jan 20, 2022 · 1 comment
Closed

box expression is bad #43

Louis-J opened this issue Jan 20, 2022 · 1 comment

Comments

@Louis-J
Copy link

Louis-J commented Jan 20, 2022

i want to return an int as object in my function, but i got several false results

YConstantExpression yconstant0 = new YConstantExpression(0, typeof(int));
YExpression.TypeAs(YExpression.Box(yconstant0), typeof(object)) // Fatal error. System.AccessViolationException: Attempted to read or write protected memory.
YExpression.Convert(YExpression.Box(yconstant0), typeof(object)) // Fatal error. System.AccessViolationException: Attempted to read or write protected memory.
YExpression.Convert(YExpression.Box(yconstant0), typeof(object), true) // Fatal error. System.AccessViolationException: Attempted to read or write protected memory.
YExpression.TypeAs(yconstant0, typeof(object)) // Unhandled exception. System.NullReferenceException: Object reference not set to an instance of an object.
YExpression.Convert(yconstant0, typeof(object)) // Unhandled exception. System.NullReferenceException: Object reference not set to an instance of an object.
YExpression.Convert(yconstant0, typeof(object), true) // got a null object
YExpression.Box(yconstant0) // Fatal error. System.AccessViolationException: Attempted to read or write protected memory.

os: win10 x64
dotnet: net6.0 x64
code:

static Func<int, object> TestFib()
{
    var ymethod = YExpression.Lambda<Func<int, object>>(
        "ymethod",
        YExpression.Condition(
            // if (n < 2)
            ynParamI0 < yconstant2,
            // return n;
            // YExpression.TypeAs(YExpression.Box(ynParamI0), typeof(object)),
            // YExpression.Convert(YExpression.Box(ynParamI0), typeof(object)),
            // YExpression.TypeAs(ynParamI0, typeof(object)),
            // YExpression.Convert(ynParamI0, typeof(object)),

            // YExpression.TypeAs(YExpression.Box(yconstant0), typeof(object)),
            // YExpression.Convert(YExpression.Box(yconstant0), typeof(object)),
            // YExpression.Convert(YExpression.Box(yconstant0), typeof(object), true),
            // YExpression.TypeAs(yconstant0, typeof(object)),
            // YExpression.Convert(yconstant0, typeof(object)),
            // YExpression.Convert(yconstant0, typeof(object), true),
            YExpression.Box(yconstant0),

            // new YConstantExpression(0, typeof(object)),

            // else return (int)method(n - 1) + (int)method(n - 2);
            YExpression.TypeAs(
                YExpression.Unbox(YExpression.Invoke(ymethodInvokeIO, ynParamI0 - yconstant1), typeof(int)) +
                    YExpression.Unbox(YExpression.Invoke(ymethodInvokeIO, ynParamI0 - yconstant2), typeof(int)),
                typeof(object)
            )
        ),
        ynParamI0
    );
    var ypackExpr = YExpression.Lambda<Func<Func<int, object>>>(
        "ypackExpr",
        YExpression.Block(
            // Func<int, object> fibC0 = null;
            new[] { ymethodInvokeIO }.AsSequence(),
            YExpression.Assign(ymethodInvokeIO, ymethod),
            ymethodInvokeIO
        )
    );
    return ypackExpr.CompileWithNestedLambdas()();
}
@ackava
Copy link
Contributor

ackava commented Jan 21, 2022

TypeAs only works for object type, it will not work for value types such as int, you cannot write a as int.

However, your YExpression.Unbox will anyway give you int, you have to use Box again as shown below.

            // else return (int)method(n - 1) + (int)method(n - 2);
            // Use Box, not TypeAs
            YExpression.Box(
                YExpression.Unbox(YExpression.Invoke(ymethodInvokeIO, ynParamI0 - yconstant1), typeof(int)) +
                    YExpression.Unbox(YExpression.Invoke(ymethodInvokeIO, ynParamI0 - yconstant2), typeof(int)),
            )

@ackava ackava closed this as completed Mar 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants