Skip to content

Commit

Permalink
Fix a bug in IRGen when dealing with mixed literal and computed props
Browse files Browse the repository at this point in the history
Summary:
In IRGen when generating IR for object initialization, we always generate StoreNewOwnPropertyInst for every literal property. This is to assume that every property must always be new. However with the introduction of computed property, this is no longer true, as a computed property could in theory define any property.
To fix this, whenever we encounter a computed property, we can no longer generate StoreNewOwnPropertyInst in the future, but always generate StoreOwnPropertyInst instead.

Reviewed By: avp

Differential Revision: D16231160

fbshipit-source-id: 581dbae96d0e0d38ce53278c0d97aaec9f916966
  • Loading branch information
lxfind authored and facebook-github-bot committed Jul 13, 2019
1 parent 50b71c2 commit 31c5683
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
16 changes: 14 additions & 2 deletions lib/IRGen/ESTreeIRGen-expr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,12 @@ Value *ESTreeIRGen::genObjectExpr(ESTree::ObjectExpressionNode *Expr) {
auto Obj =
Builder.createAllocObjectInst(propMap.size() + numComputed, objectParent);

// haveSeenComputedProp tracks whether we have processed a computed property.
// Once we do, for all future properties, we can no longer generate
// StoreNewOwnPropertyInst because the computed property could have already
// defined any property.
bool haveSeenComputedProp = false;

// Initialize all properties. We check whether the value of each property
// will be overwritten (by comparing against what we have saved in propMap).
// In that case we still compute the value (it could have side effects), but
Expand Down Expand Up @@ -514,6 +520,7 @@ Value *ESTreeIRGen::genObjectExpr(ESTree::ObjectExpressionNode *Expr) {
Builder.createStoreOwnPropertyInst(
value, Obj, key, IRBuilder::PropEnumerable::Yes);
}
haveSeenComputedProp = true;
continue;
}

Expand Down Expand Up @@ -585,8 +592,13 @@ Value *ESTreeIRGen::genObjectExpr(ESTree::ObjectExpressionNode *Expr) {

// Only store the value if it won't be overwritten.
if (propMap[keyStr].valueNode == prop->_value) {
Builder.createStoreNewOwnPropertyInst(
value, Obj, Key, IRBuilder::PropEnumerable::Yes);
if (haveSeenComputedProp) {
Builder.createStoreOwnPropertyInst(
value, Obj, Key, IRBuilder::PropEnumerable::Yes);
} else {
Builder.createStoreNewOwnPropertyInst(
value, Obj, Key, IRBuilder::PropEnumerable::Yes);
}
} else {
Builder.getModule()->getContext().getSourceErrorManager().warning(
propMap[keyStr].getSourceRange(),
Expand Down
15 changes: 15 additions & 0 deletions test/hermes/es6/computed-props.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,18 @@ print(getset['x']);
// CHECK-NEXT: 13
getset['x'] = 101;
// CHECK-NEXT: set 101

var mixed_props = {
['b']: 'b',
b: 'B',
};

print(Object.keys(mixed_props));
// CHECK-NEXT: b

var getters = {
get ['x']() {},
x: 3,
};
print(getters.x);
// CHECK-NEXT: 3

0 comments on commit 31c5683

Please sign in to comment.