Skip to content

Commit

Permalink
[BUGFIX LTS] Don't run getters while applying mixins
Browse files Browse the repository at this point in the history
This change ensures that getters are never evaluated while applying
mixins.

It relies on the fact that all getters (including undecorated ones) get
converted into classic decorators when the mixin is originally created.
  • Loading branch information
wycats committed May 1, 2023
1 parent 744e536 commit 41da13e
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 7 deletions.
20 changes: 13 additions & 7 deletions packages/@ember/-internals/metal/lib/mixin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
ComputedDescriptor,
descriptorForDecorator,
descriptorForProperty,
isClassicDecorator,
makeComputedDecorator,
nativeDescDecorator,
} from './decorator';
Expand Down Expand Up @@ -237,7 +238,7 @@ function mergeMixins(
keys: string[],
keysWithSuper: string[]
): void {
let currentMixin;
let currentMixin: MixinLike | undefined;

for (let i = 0; i < mixins.length; i++) {
currentMixin = mixins[i];
Expand Down Expand Up @@ -304,12 +305,17 @@ function mergeProps(
let desc = meta.peekDescriptors(key);

if (desc === undefined) {
// The superclass did not have a CP, which means it may have
// observers or listeners on that property.
let prev = (values[key] = base[key]);

if (typeof prev === 'function') {
updateObserversAndListeners(base, key, prev, false);
// If the value is a classic decorator, we don't want to actually
// access it, because that will execute the decorator while we're
// building the class.
if (!isClassicDecorator(value)) {
// The superclass did not have a CP, which means it may have
// observers or listeners on that property.
let prev = (values[key] = base[key]);

if (typeof prev === 'function') {
updateObserversAndListeners(base, key, prev, false);
}
}
} else {
descs[key] = desc;
Expand Down
56 changes: 56 additions & 0 deletions packages/@ember/-internals/runtime/tests/mixins/accessor_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import EmberObject from '@ember/object';
import { moduleFor, RenderingTestCase } from 'internal-test-helpers';

moduleFor(
'runtime: Mixin Accessors',
class extends RenderingTestCase {
['@test works with getters'](assert) {
let value = 'building';

let Base = EmberObject.extend({
get foo() {
if (value === 'building') {
throw Error('base should not be called yet');
}

return "base's foo";
},
});

// force Base to be finalized so its properties will contain `foo`
Base.proto();

class Child extends Base {
get foo() {
if (value === 'building') {
throw Error('child should not be called yet');
}

return "child's foo";
}
}

Child.proto();

let Grandchild = Child.extend({
get foo() {
if (value === 'building') {
throw Error('grandchild should not be called yet');
}

return value;
},
});

let instance = Grandchild.create();

value = 'done building';

assert.equal(instance.foo, 'done building', 'getter defined correctly');

value = 'changed value';

assert.equal(instance.foo, 'changed value', 'the value is a real getter, not a snapshot');
}
}
);

0 comments on commit 41da13e

Please sign in to comment.