[JSC] Generate put_by_val_direct for indexed identifiers instead of put_by_id with...
authorutatane.tea@gmail.com <utatane.tea@gmail.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 26 May 2015 17:26:40 +0000 (17:26 +0000)
committerutatane.tea@gmail.com <utatane.tea@gmail.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 26 May 2015 17:26:40 +0000 (17:26 +0000)
https://bugs.webkit.org/show_bug.cgi?id=145360

Reviewed by Darin Adler.

JSObject::putDirect only accepts non-indexed properties.
So when generating put_by_id (with direct postfix) for indexed property,
we should generate put_by_val_direct instead.

* bytecompiler/BytecodeGenerator.cpp:
(JSC::BytecodeGenerator::emitDirectPutById):
* bytecompiler/NodesCodegen.cpp:
(JSC::PropertyListNode::emitPutConstantProperty):
* tests/stress/put-by-id-direct-should-be-done-for-non-index-property.js: Added.

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@184859 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Source/JavaScriptCore/ChangeLog
Source/JavaScriptCore/bytecompiler/BytecodeGenerator.cpp
Source/JavaScriptCore/bytecompiler/NodesCodegen.cpp
Source/JavaScriptCore/tests/stress/put-by-id-direct-should-be-done-for-non-index-property.js [new file with mode: 0644]

index b65c4a6..d8b2fc4 100644 (file)
@@ -1,3 +1,20 @@
+2015-05-26  Yusuke Suzuki  <utatane.tea@gmail.com>
+
+        [JSC] Generate put_by_val_direct for indexed identifiers instead of put_by_id with direct postfix
+        https://bugs.webkit.org/show_bug.cgi?id=145360
+
+        Reviewed by Darin Adler.
+
+        JSObject::putDirect only accepts non-indexed properties.
+        So when generating put_by_id (with direct postfix) for indexed property,
+        we should generate put_by_val_direct instead.
+
+        * bytecompiler/BytecodeGenerator.cpp:
+        (JSC::BytecodeGenerator::emitDirectPutById):
+        * bytecompiler/NodesCodegen.cpp:
+        (JSC::PropertyListNode::emitPutConstantProperty):
+        * tests/stress/put-by-id-direct-should-be-done-for-non-index-property.js: Added.
+
 2015-05-24  Jordan Harband  <ljharb@gmail.com>
 
         Array#findIndex/find should not skip holes
 2015-05-24  Jordan Harband  <ljharb@gmail.com>
 
         Array#findIndex/find should not skip holes
index 4a5757b..ca48799 100644 (file)
@@ -1531,6 +1531,7 @@ RegisterID* BytecodeGenerator::emitPutById(RegisterID* base, const Identifier& p
 
 RegisterID* BytecodeGenerator::emitDirectPutById(RegisterID* base, const Identifier& property, RegisterID* value, PropertyNode::PutType putType)
 {
 
 RegisterID* BytecodeGenerator::emitDirectPutById(RegisterID* base, const Identifier& property, RegisterID* value, PropertyNode::PutType putType)
 {
+    ASSERT(!parseIndex(property));
     unsigned propertyIndex = addConstant(property);
 
     m_staticPropertyAnalyzer.putById(base->index(), propertyIndex);
     unsigned propertyIndex = addConstant(property);
 
     m_staticPropertyAnalyzer.putById(base->index(), propertyIndex);
@@ -1545,7 +1546,7 @@ RegisterID* BytecodeGenerator::emitDirectPutById(RegisterID* base, const Identif
     instructions().append(0);
     instructions().append(0);
     instructions().append(0);
     instructions().append(0);
     instructions().append(0);
     instructions().append(0);
-    instructions().append(putType == PropertyNode::KnownDirect || (property != m_vm->propertyNames->underscoreProto && !parseIndex(property)));
+    instructions().append(putType == PropertyNode::KnownDirect || property != m_vm->propertyNames->underscoreProto);
     return value;
 }
 
     return value;
 }
 
index 6159138..9ec91cb 100644 (file)
@@ -569,8 +569,15 @@ void PropertyListNode::emitPutConstantProperty(BytecodeGenerator& generator, Reg
             value.get(), nullptr, nullptr, BytecodeGenerator::PropertyConfigurable | BytecodeGenerator::PropertyWritable, m_position);
         return;
     }
             value.get(), nullptr, nullptr, BytecodeGenerator::PropertyConfigurable | BytecodeGenerator::PropertyWritable, m_position);
         return;
     }
-    if (node.name()) {
-        generator.emitDirectPutById(newObj, *node.name(), value.get(), node.putType());
+    if (const auto* identifier = node.name()) {
+        Optional<uint32_t> optionalIndex = parseIndex(*identifier);
+        if (!optionalIndex) {
+            generator.emitDirectPutById(newObj, *identifier, value.get(), node.putType());
+            return;
+        }
+
+        RefPtr<RegisterID> index = generator.emitLoad(generator.newTemporary(), jsNumber(optionalIndex.value()));
+        generator.emitDirectPutByVal(newObj, index.get(), value.get());
         return;
     }
     RefPtr<RegisterID> propertyName = generator.emitNode(node.m_expression);
         return;
     }
     RefPtr<RegisterID> propertyName = generator.emitNode(node.m_expression);
diff --git a/Source/JavaScriptCore/tests/stress/put-by-id-direct-should-be-done-for-non-index-property.js b/Source/JavaScriptCore/tests/stress/put-by-id-direct-should-be-done-for-non-index-property.js
new file mode 100644 (file)
index 0000000..0159522
--- /dev/null
@@ -0,0 +1,41 @@
+(function () {
+    var object = {
+        2: 2
+    };
+
+    var result = object[2];
+    if (result !== 2)
+        throw new Error('bad value:' + result);
+}());
+
+
+(function () {
+    var object = {
+        get 2() {
+            return 1;
+        },
+        set 2(value) {
+            throw new Error(2);
+        },
+    };
+
+    var result = object[2];
+    if (result !== 1)
+        throw new Error('bad value:' + result);
+}());
+
+(function () {
+    var object = {
+        get 2() {
+            return 1;
+        },
+        set 2(value) {
+            throw new Error(2);
+        },
+        2: 2,  // Do not throw new Error(2)
+    };
+
+    var result = object[2];
+    if (result !== 2)
+        throw new Error('bad value:' + result);
+}());