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

fix(ivy): detect frozen flyweight objects in definitions and unfreeze #25755

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/core/src/render3/definition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ import {Type} from '../type';
import {BaseDef, ComponentDefFeature, ComponentDefInternal, ComponentQuery, ComponentTemplate, ComponentType, DirectiveDefFeature, DirectiveDefInternal, DirectiveType, DirectiveTypesOrFactory, PipeDefInternal, PipeType, PipeTypesOrFactory} from './interfaces/definition';
import {CssSelectorList, SelectorFlags} from './interfaces/projection';

const EMPTY: {} = {};
const EMPTY_ARRAY: any[] = [];
export const EMPTY: {} = {};
export const EMPTY_ARRAY: any[] = [];
if (typeof ngDevMode !== 'undefined' && ngDevMode) {
Object.freeze(EMPTY);
Object.freeze(EMPTY_ARRAY);
Expand Down
23 changes: 23 additions & 0 deletions packages/core/src/render3/features/inherit_definition_feature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import {Type} from '../../type';
import {fillProperties} from '../../util/property';
import {EMPTY, EMPTY_ARRAY} from '../definition';
import {ComponentDefInternal, ComponentTemplate, DirectiveDefFeature, DirectiveDefInternal, RenderFlags} from '../interfaces/definition';


Expand Down Expand Up @@ -47,6 +48,16 @@ export function InheritDefinitionFeature(
}

const baseDef = (superType as any).ngBaseDef;

// Some fields in the definition may be empty, if there were no values to put in them that
// would've justified object creation. Unwrap them if necessary.
if (baseDef || superDef) {
const writeableDef = definition as any;
writeableDef.inputs = maybeUnwrapEmpty(definition.inputs);
writeableDef.declaredInputs = maybeUnwrapEmpty(definition.declaredInputs);
writeableDef.outputs = maybeUnwrapEmpty(definition.outputs);
}

if (baseDef) {
// Merge inputs and outputs
fillProperties(definition.inputs, baseDef.inputs);
Expand Down Expand Up @@ -162,3 +173,15 @@ export function InheritDefinitionFeature(
superType = Object.getPrototypeOf(superType);
}
}

function maybeUnwrapEmpty<T>(value: T[]): T[];
function maybeUnwrapEmpty<T>(value: T): T;
function maybeUnwrapEmpty(value: any): any {
if (value === EMPTY) {
return {};
} else if (Array.isArray(value) && value === EMPTY_ARRAY) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is Array.isArray(value) needed? Wouldn't direct identity check always be faster for both Array and non-Array value?

https://jsperf.com/isarray-vs-identity/1

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@benlesh Can you fix that

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

return [];
} else {
return value;
}
}
35 changes: 35 additions & 0 deletions packages/core/test/render3/Inherit_definition_feature_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/

import {EventEmitter, Output} from '../../src/core';
import {EMPTY} from '../../src/render3/definition';
import {InheritDefinitionFeature} from '../../src/render3/features/inherit_definition_feature';
import {ComponentDefInternal, DirectiveDefInternal, RenderFlags, defineBase, defineComponent, defineDirective} from '../../src/render3/index';

Expand Down Expand Up @@ -127,6 +128,40 @@ describe('InheritDefinitionFeature', () => {
});
});

it('should detect EMPTY inputs and outputs', () => {
class SuperDirective {
static ngDirectiveDef = defineDirective({
inputs: {
testIn: 'testIn',
},
outputs: {
testOut: 'testOut',
},
type: SuperDirective,
selectors: [['', 'superDir', '']],
factory: () => new SuperDirective(),
});
}

class SubDirective extends SuperDirective {
static ngDirectiveDef = defineDirective({
type: SubDirective,
selectors: [['', 'subDir', '']],
factory: () => new SubDirective(),
features: [InheritDefinitionFeature]
});
}

const subDef = SubDirective.ngDirectiveDef as DirectiveDefInternal<any>;

expect(subDef.inputs).toEqual({
testIn: 'testIn',
});
expect(subDef.outputs).toEqual({
testOut: 'testOut',
});
});

it('should inherit inputs from ngBaseDefs along the way', () => {

class Class5 {
Expand Down