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

Rework reflections #4

Merged
merged 126 commits into from
Feb 2, 2024
Merged

Rework reflections #4

merged 126 commits into from
Feb 2, 2024

Conversation

aedart
Copy link
Owner

@aedart aedart commented Feb 2, 2024

This PR started out with an idea to add a @reflect() decorator that could store parts of the decorator context as metadata. However, this proved to be way too cumbersome and the benefits were not as desired. So, instead, a simply isConstructor() implementation was added instead which is able to determine if a target can be invoked new, based on the Function.isCallable() / Function.isConstructor() proposal. The source is heavily inspired by that of Core-JS.

In addition, a new targetMeta() decorator has been added, which is able to associate metadata directly with a target element, e.g. class instance or method reference.

See the docs and CHANGELOG.md for additional details.

aedart added 30 commits April 24, 2023 17:45
Also, added contracts and support packages as peed dependencies.
This component will be removed, since most of its current utility methods are replaced by the support package's objects utils.
Reflection utilities will, for now at least, be added directly into the support package, so that circular dependencies can be prevented - e.g. a possible future port of the "callback reference", which could benefit from reflections.
Desired features are going to be added in the support package, in the reflections submodule.
In the case of anonymous classes, the name will be undefined.
There is no point in having a Reflection object know anything about how it is being stored inside meta, nor how it should be populated.
Also added as weak reference and modified the encoder to also handle owner property.
Caused by poor merge of Changelog
Internally, a weak reference is kept. But when accessing the property, the object should be returned, if no garbage collected.
aedart added 26 commits January 30, 2024 12:33
Shows that obtaining nested object properties inside an array works.
This also shows that working with array index / keys, when setting or checking for existence works as intended.
The new decorate is based on the experimental @reflect() decorator (which will be removed) and is able to associate a target with specific metadata, by storing a reference address to the owning class and metadata key (prefix), where it can be retrieved again. It works for classes and class methods, but sadly some inheritance will not work as desired for static methods, due to no late "this" binding in static blocks or members.
The @reflect() decorator, along with all related components are now no longer needed. The idea was "good", but very cumbersome and possible too performance demanding. The new @targetMeta() decorator has taken the best out of this experiment and can now be used to associate metadata directly with class methods, which is good enough for now. Developers can use it to store their own "reflection-like" data as metadata, if needed.
In the end, the caching mechanism might not perform as desired. In large applications, with many classes that are tested, new entries will just be added to the WeakMap and possibly never truly removed / garbage collected.
Also, refactored inherit test of static method meta.
Due to the context kind enum, an array was created with several "undefined" entries, which would end up wasting a bit more memory per class' target metadata.
Intended for inheriting target meta of static methods, in situations when these are overwritten in sub-classes.
Hopefully this decreases the overall memory usage of storing the keys / paths to actual metadata a lot.
@aedart aedart added the enhancement New feature or request label Feb 2, 2024
@aedart aedart self-assigned this Feb 2, 2024
@aedart aedart merged commit 44928b2 into main Feb 2, 2024
2 checks passed
@aedart aedart deleted the rework-reflections branch February 2, 2024 14:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant