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

Add additional Compiler/Context functions for getting compiler configuration and main func/output types #10871

Merged

Conversation

SomeRanDev
Copy link
Contributor

@SomeRanDev SomeRanDev commented Dec 3, 2022

At the moment, it's impossible to get the compiler arguments and learn what the main function is with Haxe macros. This pull seeks to resolve this by exposing additional information from the context object in OCaml.

Input

There's a collection of static info regarding compiler input that could be helpful in Haxe macros, such as compiler arguments, main class path, platform, version, etc. The Compiler.getConfiguration function helps provide this info by exposing the "config" fields of the OCaml context object.

Output

Similarly, there is some output information within this context object that would also be nice to access from Context. At the current moment, only the JavaScript target can access the generated "main" typed expression and a list of haxe.macro.Types outputs using setCustomJSGenerator and JSGenApi.

The Context.getMainExpr() allows access to the main expression from any target at compile-time.

Context.getAllModuleTypes() on the other hand provides a list of the haxe.macro.ModuleTypes that are readily available after a certain point in the compilation process. In order to keep consistent with the capabilities of the JS target, the two OCaml functions used to converting between ModuleTypes and Types have also been exposed in haxe.macro.TypeTools.


Functions added

  • haxe.macro.Compiler.getConfiguration
  • haxe.macro.Context.getMainExpr
  • haxe.macro.Context.getAllModuleTypes
  • haxe.macro.TypeTools.toModuleType
  • haxe.macro.TypeTools.fromModuleType

Any thoughts are appreciated!

@Simn
Copy link
Member

Simn commented Dec 4, 2022

Maybe we should have something like Compiler.getConfiguration which returns an object, so that we don't need a new function for everything...

Also, I don't see why getAllTypes should exist, it conveys the same information as getAllModuleTypes, just in a different format.

@SomeRanDev
Copy link
Contributor Author

I'd absolutely love getConfiguration especially if it means everything not already visible from the (* config *) section can be added.

And yeahhh, originally just had getAllModuleTypes, but noticed the JSGen system converted and used Types, so copied and threw in that one because there's no function for (easily) converting between the two on the Haxe-side. But yeah, definitely would be better to remove that and instead expose or implement a Haxe version of the type_of_module_type func (and vise versa) in haxe.macro.TypeTools, so lemme try that.

Added `Compiler.getConfiguration` and its required typedefs/enums:
* `haxe.macro.Compiler.CompilerConfiguration`
* `haxe.macro.Compiler.PackageRule`
* `haxe.macro.PlatformConfig`
* `haxe.macro.PlatformConfig.CapturePolicy`
* `haxe.macro.PlatformConfig.VarScopingConfig`
* `haxe.macro.PlatformConfig.VarScope`
* `haxe.macro.PlatformConfig.VarScopingFlags`
* `haxe.macro.PlatformConfig.ExceptionsConfig`
@Simn
Copy link
Member

Simn commented Dec 5, 2022

Oh wow, you went all-in on that configuration part!

My only concern in that regard is that we now can't easily change these internal structures without also changing the public interface. I suppose we could add a documentation comment about that somewhere so that people know something might change between compiler versions. It's a rare occurrence anyway, but I want to keep the option open at least.

@SomeRanDev
Copy link
Contributor Author

Haha, just wanted to see what it would look like if everything was typed, then get some feedback and trim down from there. (;^_^) PlatformConfig might be a bit overkill, but definitely some things like checking for non-nullable primitive types and allowing target overload functions can be helpful in Haxe macros.

Alternatively, since most of the PlatformConfig fields are Bool, could perhaps make it a Map<String,Bool> (with some of the enums translated to Bool like VarScope translating to isScopeFunctionBased or isScopeBlockBased). Of course, having a typed system would be nicer, but a map would cut down a majority of the public api maintenance while still keeping 99% of the usability (or maybe it's worse since you'd still need to keep an updated list of possible map keys).

@SomeRanDev
Copy link
Contributor Author

Mostly copied the warning comment from haxe.macro.Type, seemed very applicable.

Let me know if there's anything else, and I'll work on adding tests in meantime! 👍

@SomeRanDev SomeRanDev changed the title Add additional Compiler/Context functions for getting input arguments and output types Add additional Compiler/Context functions for getting compiler configuration and main func/output types Dec 5, 2022
@SomeRanDev SomeRanDev requested a review from kLabz December 7, 2022 01:56
@Simn
Copy link
Member

Simn commented Dec 7, 2022

Nice!

There might be some minor inaccuracies in the documentation, but we can address that later. Thank you for the contribution!

@Simn Simn merged commit 851704f into HaxeFoundation:development Dec 7, 2022
@kLabz
Copy link
Contributor

kLabz commented Dec 7, 2022

Ha, had a minor report in the documentation indeed but was about to say lgtm but you just merged it :D

@skial skial mentioned this pull request Dec 7, 2022
1 task
@SomeRanDev
Copy link
Contributor Author

Oh wow, that was fast! Thank you all so much, you have no idea how much these features will help! 😁❤️

(Sorry about the docs, ooof.)

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

Successfully merging this pull request may close these issues.

3 participants