-
Notifications
You must be signed in to change notification settings - Fork 12.4k
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
Normative changes to decorator "extra initializer" evaluation order #56606
Comments
What are "extra initializers"? In the example code given there is no initialization at all as far as I can tell. |
In the example code, function dec(target, context) {
if (context.kind === "field") {
return function (value) { return value; }; // injects a transformation into "normal" initialization.
}
} Extra initializers are added by a decorator via function dec(target, context) {
context.addInitializer(function() {
registerThisInstance(this);
});
} |
Here's an example of the difference in timing: class C {
@dec a = 1;
}
function dec(target, context) {
context.addInitializer(function() {
this.a; // before: undefined
// after: 2
});
return function (value) {
this.a; // before/after: undefined
value; // 1
return value + 1;
};
} |
Acknowledgement
Comment
Per https://github.com/pzuraq/ecma262/pull/12/files, which achieved consensus in the November 2023 TC39 meeting, the "extra initializers" for fields/auto-accessors (added by
context.addInitializer
) now need to be run independently for each field, immediately after the field itself is initialized and defined on the class instance.This results in the need to change our emit for Stage 3 decorators as follows:
Given:
We currently emit (some parts elided):
Here, we collect all "instance extra initializers" in a single array and evaluate them immediately after the
this
value is bound in the constructor (i.e. immediately upon entry to the constructor in a base class, or immediately aftersuper()
is called in a derived class). When fields are present, we evaluate the extra initializers as part of evaluating the initializer for the first field of the class.After this change, we need to emit something like this instead:
Here, we still run the extra initializers for methods/getters/setters first, but we run the extra initializers for
a
after it is defined on the instance, but before we initializeb
, and run the initializers forb
at the top of the constructor, which is evaluated afterb
has been defined on the instance.The text was updated successfully, but these errors were encountered: