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

Actually set the prototype when using a __proto__ property #4112

Merged
merged 2 commits into from May 29, 2021
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
15 changes: 7 additions & 8 deletions src/ast/nodes/ObjectExpression.ts
Expand Up @@ -17,12 +17,7 @@ import Literal from './Literal';
import * as NodeType from './NodeType';
import Property from './Property';
import SpreadElement from './SpreadElement';
import {
ExpressionEntity,
LiteralValueOrUnknown,
UNKNOWN_EXPRESSION,
UnknownValue
} from './shared/Expression';
import { ExpressionEntity, LiteralValueOrUnknown, UnknownValue } from './shared/Expression';
import { NodeBase } from './shared/Node';
import { ObjectEntity, ObjectProperty } from './shared/ObjectEntity';
import { OBJECT_PROTOTYPE } from './shared/ObjectPrototype';
Expand Down Expand Up @@ -112,6 +107,7 @@ export default class ObjectExpression extends NodeBase implements DeoptimizableE
if (this.objectEntity !== null) {
return this.objectEntity;
}
let prototype: ExpressionEntity | null = OBJECT_PROTOTYPE;
const properties: ObjectProperty[] = [];
for (const property of this.properties) {
if (property instanceof SpreadElement) {
Expand All @@ -137,12 +133,15 @@ export default class ObjectExpression extends NodeBase implements DeoptimizableE
? property.key.name
: String((property.key as Literal).value);
if (key === '__proto__' && property.kind === 'init') {
properties.unshift({ key: UnknownKey, kind: 'init', property: UNKNOWN_EXPRESSION });
prototype =
property.value instanceof Literal && property.value.value === null
? null
: property.value;
continue;
}
}
properties.push({ key, kind: property.kind, property });
}
return (this.objectEntity = new ObjectEntity(properties, OBJECT_PROTOTYPE));
return (this.objectEntity = new ObjectEntity(properties, prototype));
}
}
3 changes: 3 additions & 0 deletions test/form/samples/proto-null/_config.js
@@ -0,0 +1,3 @@
module.exports = {
description: 'handles getters and setters on __proto__ properties'
};
2 changes: 2 additions & 0 deletions test/form/samples/proto-null/_expected.js
@@ -0,0 +1,2 @@
console.log('retained');
console.log('retained');
5 changes: 5 additions & 0 deletions test/form/samples/proto-null/main.js
@@ -0,0 +1,5 @@
const a = { __proto__: null };
if (a.hasOwnProperty) console.log('removed');
else console.log('retained');
if (a.foo) console.log('removed');
else console.log('retained');
3 changes: 3 additions & 0 deletions test/function/samples/proto-accessors/_config.js
@@ -0,0 +1,3 @@
module.exports = {
description: 'handles getters and setters on __proto__ properties'
};
21 changes: 21 additions & 0 deletions test/function/samples/proto-accessors/main.js
@@ -0,0 +1,21 @@
let getter_effect = 'FAIL';
let setter_effect = 'FAIL';
let proto = {
get foo() {
getter_effect = 'PASS';
},
set bar(value) {
setter_effect = 'PASS';
}
};
let obj1 = {
__proto__: proto
};
let obj2 = {
__proto__: proto
};
let unused = obj1.foo;
obj2.bar = 0;

assert.strictEqual(getter_effect, 'PASS');
assert.strictEqual(setter_effect, 'PASS');