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 known globals #5072

Merged
merged 3 commits into from
Jul 28, 2023
Merged
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
44 changes: 39 additions & 5 deletions src/ast/nodes/shared/knownGlobals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
import { doNothing } from '../../../utils/doNothing';
import type { HasEffectsContext } from '../../ExecutionContext';
import type { NodeInteractionCalled } from '../../NodeInteractions';
import { NODE_INTERACTION_UNKNOWN_ASSIGNMENT } from '../../NodeInteractions';
import {
NODE_INTERACTION_UNKNOWN_ACCESS,
NODE_INTERACTION_UNKNOWN_ASSIGNMENT
} from '../../NodeInteractions';
import type { ObjectPath } from '../../utils/PathTracker';
import {
SymbolToStringTag,
Expand All @@ -12,7 +15,7 @@ import {
} from '../../utils/PathTracker';
import ArrayExpression from '../ArrayExpression';
import type { LiteralValueOrUnknown } from './Expression';
import { UnknownTruthyValue } from './Expression';
import { ExpressionEntity, UnknownTruthyValue } from './Expression';

const ValueProperties = Symbol('Value Properties');

Expand Down Expand Up @@ -52,6 +55,22 @@ const PURE_WITH_ARRAY: ValueDescription = {
}
};

const GETTER_ACCESS: ValueDescription = {
deoptimizeArgumentsOnCall: doNothing,
getLiteralValue: getTruthyLiteralValue,
hasEffectsWhenCalled({ args }, context) {
const [_thisArgument, firstArgument] = args;
return (
!(firstArgument instanceof ExpressionEntity) ||
firstArgument.hasEffectsOnInteractionAtPath(
UNKNOWN_PATH,
NODE_INTERACTION_UNKNOWN_ACCESS,
context
)
);
}
};

// We use shortened variables to reduce file size here
/* OBJECT */
const O: GlobalDescription = {
Expand All @@ -65,6 +84,12 @@ const PF: GlobalDescription = {
[ValueProperties]: PURE
};

/* PURE FUNCTION IF FIRST ARG DOES NOT CONTAIN A GETTER */
const PF_NO_GETTER: GlobalDescription = {
__proto__: null,
[ValueProperties]: GETTER_ACCESS
};

/* FUNCTION THAT MUTATES FIRST ARG WITHOUT TRIGGERING ACCESSORS */
const MUTATES_ARG_WITHOUT_ACCESSOR: GlobalDescription = {
__proto__: null,
Expand Down Expand Up @@ -253,7 +278,8 @@ const knownGlobals: GlobalDescription = {
isSealed: PF,
keys: PF,
fromEntries: O,
entries: PF,
entries: PF_NO_GETTER,
values: PF_NO_GETTER,
prototype: O
},
parseFloat: PF,
Expand Down Expand Up @@ -350,16 +376,24 @@ const knownGlobals: GlobalDescription = {
[ValueProperties]: IMPURE,
Collator: INTL_MEMBER,
DateTimeFormat: INTL_MEMBER,
DisplayNames: INTL_MEMBER,
ListFormat: INTL_MEMBER,
Locale: INTL_MEMBER,
NumberFormat: INTL_MEMBER,
PluralRules: INTL_MEMBER,
RelativeTimeFormat: INTL_MEMBER
RelativeTimeFormat: INTL_MEMBER,
Segmenter: INTL_MEMBER
},
setInterval: C,
setTimeout: C,
TextDecoder: C,
TextEncoder: C,
URL: C,
URL: {
__proto__: null,
[ValueProperties]: IMPURE,
prototype: O,
canParse: PF
},
URLSearchParams: C,

// Browser specific globals
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
description: 'retain functions that accept a object with a getter that has side effects'
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
let effects = 0;

const objWithEffect = {
get foo() {
effects++;
return 'foo';
}
};

Object.values(objWithEffect);
Object.entries(objWithEffect);

assert.equal(effects, 2);
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
let effects = 0;

const objWithEffect = {
get foo() {
effects++;
return 'foo';
}
};

const objWithoutEffect1 = {
get foo() {
return 'foo';
}
};
const objWithoutEffect2 = {
foo: 'foo'
};

Object.values(objWithEffect);
Object.entries(objWithEffect);

Object.values(objWithoutEffect1);
Object.entries(objWithoutEffect1);
Object.values(objWithoutEffect2);
Object.entries(objWithoutEffect2);

assert.equal(effects, 2);