Closed Just-Feeshy closed 4 months ago
OOF, I think I merged this too quickly. When you said tests didn't work, I assume the tests just needed to be regenerated... but this actually breaks a lot of stuff. 😅
Mainly the direct String
manipulation breaks lambda assignments in constructors and other large constructions in there. For example, Issue38_CallFindFuncDataOnVar
test generates like this:
Main::Main():
_order_id(generate_order_id()), fn([&]() mutable { std::cout << "test/unit_testing/tests/Issue38_CallFindFuncDataOnVar/Main.hx:12: do something"s << std::endl)
{
};
this->fn();
}
I wrote this as a comment in the code, but I guess seeing how troublesome this is, I think my "TODO" has to happen immediately for this feature to be properly added. The correct way to do this would be to analyze the constructor function body's TypedExpr
. This can be done above the following code using bodyExpr
:
XComp.pushTrackLines(useCallStack);
body.push(Main.compileClassFuncExpr(bodyExpr));
XComp.popTrackLines();
If bodyExpr
is TBlock
, extract the expressions from it and read the expressions from the start of the list as long as they are TBinop(OpAssign, TField(e, name), e2)
expressions where the e
expression is a TConst(TThis)
. Filter out expressions where e2
is not simple or constant. Haxe always generates the this->NAME = INIT_VALUE expressions at the start of the constructor, so once those stop appearing, you can end the process. You would then need a system to ignore these expressions when generating in the Expressions compiler, not sure exactly how to do that yet. Hmmm...
Also, don't forget the fields are compiled before functions, so maybe the ones you want to initialize in the constructor can be stored there and then accessed later? There's a lot of ways this could be done, sorry my code is such a mess.
I assume that this is still under ctx.isConstructor
since we are only working with the constructor's fields.
This will most likely fail a good portion of unit tests due to the change in how constructors are compiled now. I added constructor list support for the compiler because I came across a situation with my own learning project that would almost require the argument to be put in the constructor list since the class's property variable was immutable. Let me show an example. I do recommend doing some further tests of your own to see if there might be any issues.
This is the Test.hx I used:
And here is how it would look before this PR:
And I received an error since I'm trying to overload an immutable variable. However, I really just want to initialize my variable with my argument.
Now with this PR, I can do exactly that without removing the
@:const
keyword:There we go! It works!